ZVSE
_WARNING_#1=IMPORTANT! This file is not in a plain text format. NEVER use any editor except ERM_S for making any kind of changes!
ERMS_PoweredBy=ERM Scripter v. 2003.12.24.1820
ERMS_ScriptDate=1.5(May).2004

** Random Hero by Bonizag 
** Wogify Name: script72.erm 
** Updated: April 14, 2004  (Fnord Update: May 1, 2004)

** variables used:        v846, v849, v851, v855-v856, v867 for long term
                          
** string variables used: z1-z3, z78-z90 temporarily

** flags used:            781, 783, 786 for long term
                          
** functions used:        7797-7798, 7825-7827

** hero variables used:   w81-w83

!#UN:P72/?v846; //check if option is enabled

!#TM41&v846=1:S1/999/1/255; //init timer 41 
!#VRv855:S-2;

!#IF&v846=1:V781/0; //init flag 781
!#IF&v846=1:V786/0; //786 set false for the first battle

!?TM41&v846=1; //timer trigger
!!VRv849:Sc0; //get  current day

!?TM41&v849=1/v846=1;
!!VRh:S0; //see whether there is an unused color
!!OW:I0/?f/?g;
!!VRh&g=1:S1;
!!OW:I1/?f/?g;
!!VRh&g=1:S1;
!!OW:I2/?f/?g;
!!VRh&g=1:S1;
!!OW:I3/?f/?g;
!!VRh&g=1:S1;
!!OW:I4/?f/?g;
!!VRh&g=1:S1;
!!OW:I5/?f/?g;
!!VRh&g=1:S1;
!!OW:I6/?f/?g;
!!VRh&g=1:S1;
!!OW:I7/?f/?g;
!!VRh&g=1:S1;
!!VRv846&h=0:S0;

!?OB54&v846=1/1000; //monster trigger 
!!IF:V786/1;
 
!?BA0&v846=1/1000; //before battle begins 
 
!!IF:V781/0; //assume there is no random hero for battle actions
!!IF:V783/0; //assume no army camp fight

!?BA0&786/v846=1/1000; //continue if hero fights army camps (wandering monsters excluded)
//section: hero fights a monster stack

!!BA:H0/?f; //attacker in vf
!!BA:H1/?g; //defender in vg

!!IF:V783/1; //save flag 786 into 783
!!VRh&g<0:S1 R99; //make a random value from 1 to 100
!!VRh&g<0:%4; //25% chance to fight against a hero
!!VRv855&g<0/h=0:S-2; //init v855
!!VRi&g<0/h=0:S0 T154; //get a time based random hero number 0-154, 155 excluded because it is reserved for Asmodeus script
!!VRv856&g<0/h=0:S0; //init v856, counts number of searched heroes
!!VRv849&g<0/h=0:Si;
!!DO7798/1/1000/1&g<0/h=0:P; //get the hero who fights the battle, v855 will contain the number of the hero
!!BA&g<0/h=0:H1/v855; //give the hero to fight for monsters

//this flag state needed for placing machines 
!!IF:V786/0; //hero don't fight against monsters' hero by default 
!!IF&g<0/h=0/v855>=0:V786/1; //hero fights against monsters' hero 

//this flag state needed after battle
!!IF:V781/0; //hero doesn't fight against monsters' hero by default 
!!IF&g<0/h=0/v855>=0:V781/1; //hero fights against monsters' hero 
 
!?BR&v846=1/1000; //battle round trigger for placing war machines when battle begins 
 
!!IF&v997=0:V786/0; //if asmodeus sends devils there will be no random hero

!?BG0&781/v846=1/1000; //random hero

!!BH1:N?f; //get random hero's number
!!BG&f>=0:Q?v851 A?g; //get attacking side index and action
!!BG&v851=1/f>=0/g=4:A0; //if random hero tries to retreat force him/her to perform an other action

!?BA1&v846=1/1000; 
 
!!IF:V786/0; //set flag 786 false for the next battle 
!!HEv855&781:O-1/1; //set random hero's owner back to -1 after battle
 
!$OB54&v846=1; //monster trigger 
!!IF:V786/0;

!?FU7797;
//sees if hero is in prison
//x1: greatest object number
//x16: object number (from back)
//x2: object type
//x3: hero number

!!VRy1:Sx1 +1 -x16; //run backward 
!!UN:Ux2/-1/y1/1; //v1 v2 v3 coordinates
!!HEv1/v2/v3:N?y2; //get hero number at prison's position
!!IF&y2=x3:V786/1; //set flag if the hero is in prison

!?FU7798;
//gets the hero who fights the battle on the monsters' side

!!IF:V786/0; //init

!!VRy19:Sv849 %155; //to ensure cycling
!!HEy19:O?y1; //get the owner

!!OW:V0/?y2/?y3; //get the heroes who can be hired in taverns
!!OW:V1/?y4/?y5;
!!OW:V2/?y6/?y7;
!!OW:V3/?y8/?y9;
!!OW:V4/?y10/?y11;
!!OW:V5/?y12/?y13;
!!OW:V6/?y14/?y15;
!!OW:V7/?y16/?y17;

!!OW:Iy1/?y20/?y21; //get owner info
!!IF&y1<>-1/y21<>1:V786/1; //if owner of the hero is active call again

!!IF&y19=y2/y2<>-1:V786/1; //if hero can be hired in any tavern call again
!!IF&y19=y3/y3<>-1:V786/1;
!!IF&y19=y4/y4<>-1:V786/1;
!!IF&y19=y5/y5<>-1:V786/1;
!!IF&y19=y6/y6<>-1:V786/1;
!!IF&y19=y7/y7<>-1:V786/1;
!!IF&y19=y8/y8<>-1:V786/1;
!!IF&y19=y9/y9<>-1:V786/1;
!!IF&y19=y10/y10<>-1:V786/1;
!!IF&y19=y11/y11<>-1:V786/1;
!!IF&y19=y12/y12<>-1:V786/1;
!!IF&y19=y13/y13<>-1:V786/1;
!!IF&y19=y14/y14<>-1:V786/1;
!!IF&y19=y15/y15<>-1:V786/1;
!!IF&y19=y16/y16<>-1:V786/1;
!!IF&y19=y17/y17<>-1:V786/1;

!!UN&-786:U62/-1/?y18; //get the number of prisons on the map
!!DO7797/1/y18/1&-786:Py18/62/y19; //iterate through prison's and see if this hero is in any of them

!!VRv855&-786:Sy19; //save hero number only if we found a valid hero

!?FU7798&-786;
!!DO7827/0/7/1:P; //get an owner for the random hero - returned in v849
!!HEv855:Ov849/1;

!?FU7798;

!!VRv849&786:+1;
!!VRv856:+1;

!!VRx16:S1000;
!!VRx16&786/v856<154:S1; //continue search if the hero is owned, can be hired in taverns or is prisoned

!?FU7825;
//dismisses war machines that could be bought originally in heroes3 and gives back gold spent on them
//x1 hero number

!!HEx1:A2/6/?y1/?y2 A2/4/?y1/?y3 A2/5/?y1/?y4 ; //y2 y3 y4 number of equipped machines
!!IF:Wx1; //use hero's w vars
!!HEx1:O?y1; //vf hero owner
!!HEx1&w81=0/y2=1:A3/6/1/1; //remove first aid tent that was bought right now
!!OW&w81=0/y2=1:Ry1/6/d750; //give back gold spent on first aid tent
!!HEx1&w82=0/y3=1:A3/4/1/1; //remove ballista that was bought right now
!!OW&w82=0/y3=1:Ry1/6/d750; //give back gold spent on ballista
!!HEx1&w83=0/y4=1:A3/5/1/1; //remove ammo cart that was bought right now
!!OW&w83=0/y4=1:Ry1/6/d750; //give back gold spent on ammo cart

!?FU7826;
//corrects hero's w vars according to war machine equipment
//x1: hero number

!!IF:Wx1; // use hero's w variables
!!HEx1:A2/6/?y1/?y2; //y2: nr of equipped first aid tents
!!VRw81&y2=0/w81<>0:S0; 
!!VRw81&y2=1/w81=0:S1; 

!!HEx1:A2/4/?y1/?y2; //y2: nr of equipped ballistas
!!VRw82&y2=0/w82<>0:S0; 
!!VRw82&y2=1/w82=0:S1; 

!!HEx1:A2/5/?y1/?y2; //y2: nr of equipped ammo carts
!!VRw83&y2=0/w83<>0:S0; 
!!VRw83&y2=1/w83=0:S1; 

!?FU7827;
//x16: color for random hero that needs to be tested

!!OW:Ix16/?y1/?y2; //check color status
!!VRv849&y2=1:Sx16; //v849 gets the color number if the color is unused
!!VRx16&y2=1:S7; //jump out from do receiver if color is found

