BATTLE, MONSTER PARAMETERS (BM)
RECEIVER

You may check/set/get some parameters of a stack of monsters during combat (!?BR trigger)

Note: Now a usage of !!BG, !!BM, !!BH, !!BU, !!BF in complete AI battle is controlled in the ERM interpreter and you will get the corresponding error message.

!!BM#:XXXX;

Check/Set/Get some parameters of a stack of monsters 
   # - number of a stack (0...41). 
Usually it is used like 0...20 for a first (0) player and 21...41 for a second (1) player.

!!BM-1:XXXX;

Check/Set/Get some parameters of a active stack during combat. 

OPTIONS

A$; Attack with bonuses
B$; Number of monsters in the stack (at the beginning of the battle).
C#1/#2/#3/#4/#5; Cast a spell
   #1 - spell number (Format SP)
   #2 - position to cast
   #3 - Monster skill level (0...3)
   #4 - Hero (???) Spell power (0...3)
   #5 - Check 4 Target Monster existing (alive)

Note:
Parameter 5 must be a 1. This tells it to check if the target stack is alive (not wiped out) before casting, otherwise it will crash if it tries to cast on an invalid target. For the resurrection spells you might want to try a 0 here so that they can be used to revive dead stacks
D$; Defence with bonuses
E$; Spell numbers (not a number of a spell)
Comments:
This is the number of times remaining that this stack can cast its spell this combat. So Archangels will start with this value set to 1; after they Resurrect something, this will be 0. 
For BG triggers, the value changes after the trigger for them casting the spell.
F$; Monster flag (as for MA:X)

Comments
Four of the previously unknown MA:X values are used to handle
the flow of combat:
  0x01000000 = 16777216 - set if this stack has had morale this round
  0x02000000 = 33554432 - set if this stack is waiting
  0x04000000 = 67108864 - set if this stack is done acting for this round
  0x08000000 = 134217728 - set if this stack is defending
  0x00400000 = 4194304 - set if the stack is summoned (vanishes when killed)
  0x00800000 = 8388608 - set if the stack is a clone (blue colour and 1 hit kills)
  0x00C00000 = 12582912 - set for a stack that is created by the Clone spell. This is the sum of the bits above (summon) + (clone).

To check these bits, use BR:F, then use VR:& with the value corresponding to the bit you're interested in. So:
!!BRx16:F?i; [read flags]
!!VRi:&33554432; [just look at waiting bit]
!!IF&i>0:M^This stack is waiting.^;
G#/$1/$2; Set/check/get active spell parameters
   # - Spell number (Format SP)
   $1 - Set/check/get level in school of the spell
   $2 - Set/check/get a power 
H$; Hit points
I$; Side index as for heroes (0:left, 1:right)
J?$; Get or check for number of active spells
K#; Strike a monster with #damage points
L$; Hit points lost by the next monster in the stack
M#1/#2/#3; Apply a spell to the monster 
   #1 = number of spell (See Format SP)
   #2 = duration in turns 
   #3 = level of skill (0-no, 1-basic, 2-advanced, 3-expert)
Comments:
If you cast a spell that a monster already has, it will be ignored. But you can use "Dispel" spell :-)
In format SP, the special monster abilities aren't listed, but some were determined through testing:
70. Stone
71. Poison
72. Bind
73. Disease
74. Paralyze
75. Age
You can set those 6 fine with BR:M, except that a creature with Bind on it will never get to move (duration is ignored), and a Poisoned creature doesn't take damage in the first round of combat. And of course there is no graphic to show the condition being caused, but the condition itself is displayed correctly and works fine - a Stoned creature looks like it's made of stone and doesn't get to act, etc.

You can set any other spell with BM:M, even ones that have no meaning in combat - for example you can give a creature "summon boat" for 3 turns.
The graphic will show up when you right-click on the creature, and be listed as "summon boat." The creature will have a green number-of-creatures (indicating it's assumed to be a friendly spell), except for a few spells that show up as red (magic arrow and implosion were two).

Additionally spell numbers from 76 on appear to be used for creature abilities that have a picture and effect but don't permanently affect the target (i.e. aren't displayed when you right click on the creature later). You can set these for creatures with BR:M, but it either shows no picture (but shows the name if you put the mouse over the empty square), or crashes when you look at the creature (and it's not consistent - usually it crashes, but sometimes you see a blank square). 
And if you use BG:C to try to "cast" one of these spells, it either does nothing or crashes. However I worked out a few names anyway, and perhaps they'll be useful to know eventually:
76. Death Cloud
77. Thunderbolt
78. ?
79. Death Stare
80. Acid Breath

N$; Number of monsters in the stack (current).
O$; Number of a (hero's) army slot (0...6,-1)
Comments:
If O$ sets to -1, the stack will not appear in (hero's) army after the battle (summoned creatures).
P$; Position on a battle field
Q#1/#2/#3; Put a hex of Quick Sand or a Land Mine at a position
   #1 - Type (0=Quick Sand, 1=Land Mine)
   #2 - Position
   #3 - Redraw (1) or not (0)
Comments:
For Q command the position will be checked automatically for whether you can place a magic obstacle or not. 
You can use a stack number 0 for a left side and 21 for right side to be always sure that the magic obstacle will be placed (stack may be dead - all that is needed is a side number).
R$; Number of retaliations for attack (check for Royal Griffin)
Comments:
This is the number of retaliations this stack has left this round. It starts each round at 1 and becomes 0 once the stack retaliates. 
For Royal Griffins it's set to 5000 each round and counts down.
S$; Speed
T$; Type of a monster (Format C)
U1/$;
U2/$;
U3/$;
Setting monster stats.
!!BM...:U1/$; - get/check/set stack's minimum damage
!!BM...:U2/$; - get/check/set stack's maximum damage
!!BM...:U3/$; - get/check set stack's number of shots
U4/$;
Change a spell to cast for a creature:
!!BM..:U4/$;

This command may be used for monster spell checking and changing.
Comments:
   You may only change the spell for creatures that cast spells already but you should choose a spell of the same kind. For example, if a creature normally casts a spell on a friendly unit (say Ogre Magi's Blood Lust), you may change it to any other single-stack beneficial spell (say Mirth or Haste) but not to another type of spell (such as Slow or Magic Arrow), otherwise the AI will never use it. However, if you only need a human player to be able to cast the spell with that creature, almost all spells will work.
   This example also "fits" a Commander case. They are set to cast spells like Ogre Magi. So to make a "monster" Commander cast spells you need to set it to any correct value.
   Also note that this command has no effect on creatures whose spell is determined randomly each round, such as Master Genies and Faerie Dragons.
U5/$;
Stack's clones
!!BM..:U5/$;

   $ - index of this stack's clone.
    If there is no clone for this stack, it returns -1, otherwise it returns 0...41 - a number of the stack that IS a clone of this one.
   BTW, you can set it. So if you clone a stack and then reset this field to -1, the cloned stack will not be a clone of this one.
V#; Show combat animations
# is a number of animation
This will show the animation on the stack.
(See Format AC)
V#1/#2; Show combat animations with bad effect
This will show the animation on the stack.
#1 is a number of animation
#2=1 show stack getting hurt
#2=0 just show animation, same as V#
(See Format AC)
V#1/#2/#3; Show custom animation
#1 Type (see below)
#2 Bad effect (0 - just show the animation, 1 - show monster getting hit)
#3 Name of def file with animation (this can be any existing def file) Any z-variable or a ^^ constant

Types:
0 - The bottom of animation is at the bottom of the creature (e.g. Prayer)
1 - Animation center is in the center of the creature (e.g. Air Shield)
2 - Animation is over the creature (e.g. Lightning)
4 - Animation is near the attacking hero (e.g Land Mine)
15 - Animation is in the center of battlefield (flying Desrupting Ray, Death Cloud)
Flag 256 - semi-transparent animation (add it to the type)

How do we read the flag notation again? (e.g. 0x200) -- what does this mean? The value seemed to be always be 512. How does 0x200=512 again?
By Donald

To look at just one bit, use & with the value of your bit, then see if the result is nonzero.

0x200 is C notation for hexadecimal values. 0x means, look out, a hexadecimal is coming! Then the digits that follow are in base 16, where 0-9 equal 0-9, and A-F equal 10-15. The last digit is the 1's place, the digit before it 16's, the digit before it 16*16 = 256's, before that 16*16*16 = 4096's, and so on. It's just like base 10 except with 16's.

512 = 2 * 256. So in hexadecimal it's 0x200 - 2 * 256 + 0 * 16 + 0 * 1.

To read the value, load it into a variable, say v1, then use VRv1:&512, then FU1&v1<>0 or whatever. The & command clears all bits except the ones you specify. Thus it lets you ignore those bits and just see the ones you want. Similarly | sets the bits you specify, regardless of the other bits.

So if you want to know if a monster stack is defending, you go
!!BMx16:F?i; [read the flags into a 32-bit variable]
!!VRi:&134217728; [just look at the bit that means defending]
!!FU1&i<>0:P; [if defending call function 1]
!!FU1&i=134217728:P; [this also calls function 1 if defending]
!!FU2&i=0:P; [if not defending call function 2]