BATTLEGROUND ACTIONS (BG)
RECEIVER

During combat, you may check/set/get parameters on actions taken by a monster stack. These receivers may be used/called only in the !?BG trigger section.

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.

!!BG:XXXX; During combat, you may check/set/get parameters on taken action

OPTIONS

A$; Type of an action:
   0 = Cancel Action (the stack can do a different action now but it may still be impossible to force it to do most actions through ERM).
   1 = Hero cast a spell
   2 = Walk
   3 = Defend
   4 = Retreat from the battle
   5 = Surrender
   6 = Walk and Attack
   7 = Shoot 
   8 = Wait
   9 = Catapult
 10 = Monster casts a spell (i.e. Faerie Dragons)
 11 = Healing by healing tent

i.e !!BG:A10; will make current stack cast a spell set with !!BG:S$;
C#1; Reset battle temporary variables
   #1 - stack to clean up, use -1 for whole array.
See BG:V for more info.
D$; Destination position (magic to monster, walk or attack to)
E$; Get a destination monster stack (destination position is used). 
-1 if no monster stack at that position
H$; Number of a hero owner (-1=no hero)
N$; Set/check/get number of a current stack (0...41)
The one that moves.
P?$;  Enemy to attack
"Keeps enemy to attack", not sure what exactly. Stack number? Position?
Most likely check only.
Q?$;  Current attacking side (0=left or 1=right)
Check only!
S?$; Get control number of action
Depends on action (BG:A):
BG:A1 Hero cast spell - $ stores spell number (See Format SP)
BG:A10 Monster cast spell - $ stores spell number (See Format SP)
BG:A6 Walk and attack - $ stores position to which the monster will move
BG:A5 Surrender - $ stores cost of surrender
For other actions it's -1 or the value from the last action, so consider it undefined.
V#1/#2/$3; Battle temporary variables
Array of 100 variables assigned to stacks. Syntax:
   #1 - stack index (0..99)
   #2 - index in array (0..99)
   $3 - get/set temporary value
Those values are NOT stored in save file. You may keep there any integer value. It does not matter either a real stack is present in the battle or not.
X$; Set/check/get second target of spell
BG:X gets/sets/checks additional parameter of a spell being cast.
It's the second target of Teleport and Sacrifice spells. For teleport, BG:D is the position of monster to be teleported and BG:X is the place where it will move. For sacrifice, BG:D is position of the corpse and BG:X is the victim position.


Here is an example. You'll need a hero with a Pikeman in the first slot and Angels in the others (for example).
ZVSE
!#VRv100:S0;
!?CM4&v100<3;
!!CM:R0;
!!BG&v100=0:N3 A6 S2 D1;
!!BG&v100=1:A1 S40 D1 X2;
!!BG&v100=2:A1 S63 D1 X93;
!!VRv100:+1;