ZVSE
ERMS_ScriptDate=26.10(October).2004
_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. 2004.10.5.945
WoGify Name: script01.erm
Updated: Sep. 24, 2004 by Hermann the Weird

WoGify ERM code: 101 (0-5) plus 111-130 (0-1) for individual rules
101 code value:
 0=choose map rules each game, defaulting to rules selected below
 1=choose map rules each game (no defaults)
 2=use rules selected in options dialogue
 3=use two random map rules
 4=use two random map rules from those selected in options dialogue
 5=disable all map rules

** Map Rules ERM script v2.0 - Donald X. Vaccarino
** Requires WOG version 3.58 or later
** Adds rules to a map on day one
** Several rules were suggested by Timothy Pulver
** WoGify Options editing by Timothy Pulver

** Modified by Thomas Franz (samot):
** - FU710 - 9th / 10th skill: added check for warfare
** - Heroes pick up double artifacts works only for artifacts that are on the map if you start the game
**   (modified ?OB5; new FU727, reuse of TM19 (script00), once at day 1);
*  - FU690 / FU701: Dwellings have Air Elementals instead of Boars if Neutral Town (script67) is active

** Rules: (Number in parenthesis is ERM code)
**   1. Fliers can't fly but instead aren't retaliated against. (111)
**   2. Monster speed is inverted. (112)
**   3. Monsters are half price on 7th day of week. (113)
**   4. Heroes create magic plains each day. (114)
**   5. All monsters attack twice. (115)
**   6. Monster stacks in hero armies grow by 4% each day, to 10000 max. (116)
**   7. Most dwellings also have boars. (117)
**   8. Heroes start at 5th level (+5000 experience). (118)
**   9. Heroes pick up double artifacts. (119)
**  10. Perpetual darkness. (120)
**  11. Heroes have double movement. (121)
**  12. Mines change resources once/week. (122)
**  13. +1 places can be revisited each month. (123)
**  14. Heroes get 9th-10th skills. (124)
**  15. A hero's upgraded 7th level monsters automatically become 8th level. (125)
**  16. Heroes gain an artifact each level. (126)
**  17. Rogues attack heroes at random. (127)
**  18. All creatures have a breath weapon. (128)
**  19. Shooters can't shoot but instead have +50% hit points. (129)
**  20. All towns have a grail (only used if no confluxes on map) (130)
**  21. All towns have Warlord's Banners in front of them. (239)

** To use this code, put it into a Global Event, set to never happen.
** Make sure this event is prior to other events that may be affected by it.

** Variables Used: z280-z299, z326-z328, z332-z354, v1-v4, v549-v560,
** v564-v579, v600-v602, v610-v639, v700-v739, v1219, v1262
** z680, z681-z697 (reserved for future rules!)
** Flags Used: 1-3, 875
** Functions Used: 690-727
** Timers Used: 84 (was 72); 19 (reuse from script00)
** Variables v549-v559, v572-v579, z280-z299, and z332-z354 must not be modified by other programs!
** PO:N(=15) for Artifacts on the map (OB-Type 5) if double artifact rule is on

** functions for use during initialization

** see if player x16 is with player x1 on team x2, if not use for their shroud

!?FU719;

!!OW:Tx16/?y1; [get team number if any]
!!VRy1&y1=-1:S-2; [treat no team as not the same team]
!!VRvv600&x1<>x16/x2<>y1:Sx16; [store player number if usable for shroud-placing]

** find nonteammate for player x16

!?FU720;

!!VRv600:S564 +x16; [number of variable holding non-teammate]
!!VRvv600:Sx16; [default for 8-player-team game]
!!OW:Tx16/?y1; [get team number if any]
!!DO719/0/7/1:Px16/y1; [check each player for not being on this team]

** end of initialization functions


** Initialization Code

!#VRv1:S0; [make sure v1 starts as 0]
!#UN:P101/?v1; [check wogify options number - 5 means no rules]
!#TM84&v1<>5:S1/999/1/255; [timer every day for all players]
!#VRv579:S0; [first 10 bits of rules - none chosen yet]
!#VRv578:S0; [previous day in game]
!#VRv577:S0; [creatures are normal price]
!#VRv576:S0; [second 11 bits of rules - none chosen yet]
!#UN:X?v574/?v575; [get size/underground]
!#VRv573:Sv574 :2; [get half of size = middle of map]
!#VRv572:S0; [day of last rogue attack]
!#VRv550:C1/2/4/8/16/32/64/128/256/512/1024; [bit table]

!#VRz332:Sz101000; [rule names for menu/papyrus]
!#VRz333:Sz101001;
!#VRz334:Sz101002;
!#VRz335:Sz101003;
!#VRz336:Sz101004;
!#VRz337:Sz101005;
!#VRz338:Sz101006;
!#VRz339:Sz101007;
!#VRz340:Sz101008;
!#VRz341:Sz101009;
!#VRz342:Sz101010;
!#VRz343:Sz101011;
!#VRz344:Sz101012;
!#VRz345:Sz101013;
!#VRz346:Sz101014;
!#VRz347:Sz101015;
!#VRz348:Sz101016;
!#VRz349:Sz101017;
!#VRz350:Sz101018;
!#VRz351:Sz101019;
!#VRz680:Sz101051;

!#DO720/0/7/1&v1<>5:P; [find an enemy for each player, for Perpetual Darkness]

** end of Initialization Code


** Timer routine

!?TM84;
!!VRv1:S0; [make sure v1 starts as 0]
!!UN:P101/?v1; [check wogify options number - 5 means no rules]
!!FU&v1=5:E; [Exit timer if map rules are disabled in wogify options]

!!VRy-1:Sc0; [get day of game]
!!VRy-2:Sy-1 %7; [get day of week in y-2]
!!VRy-3:S0;
!!VRy-3&y-1<>v578:S1; [y-3=1 if this is the first human turn on this day]
!!VRv578:Sy-1; [store day]
!!VRy-4:Sy-1 %28; [day of month]

!!FU690&y-1=1/y-3=1/v3340<>99:P; [choose rules and implement some]

!!VRv600:S0; [flag for 1st rule announced]
!!UN:P230/?y-7; [Check if Display Map Rules chosen at Map Start is enabled: y-7]
!!DO691/1/21/1&1000/y-1=1/y-3<>1/y-7=1/v3340<>99:P; [announce rules to humans after 1st player on day 1]

!!FU709:P3; [check for rule 3]
!!VRy-5:S0; [assume creatures should be normal price: y-5]
!!VRy-5&3/-1000:S1; [for AI with this rule they're always half price]
!!VRy-5&3/1000/y-2=0:S1; [for humans just half price on sunday]
!!DO694/0/211/1&y-5=1/v577=0:P; [make creatures half price when needed]
!!DO695/0/211/1&y-5=0/v577=1:P; [make creatures double price when needed]
!!VRv577:Sy-5; [remember whether creatures currently are normal or half price]

!!FU709:P6; [check for rule 6]
!!DO699/0/155/1&3:P; [grow hero monster stacks by 5%]

!!FU709:P8; [check for rule 8]
!!OW:C?y-5; [get current player: y-5]
!!DO702/0/155/1&3/y-1=1:Py-5; [upgrade this player's heroes to 5th level on day 1]

!!FU709:P11; [check for rule 11]
!!OW&3:H-1/2/0; [number of heroes current player has: v2]
!!DO703/1/v2/1&3/v2>0:P; [double hero movement of current player's heroes]

!!FU709:P12; [check for rule 12]
!!UN&3/y-2=1/y-3=1/y-1<>1:U53/-1/?y-1; [count mines: y-1]
!!VRv600:S-1; [reset coordinate index]
!!DO706/1/y-1/1&3/y-2=1/y-3=1/y-1<>1:P; [change mine production once/week]

!!FU709:P13; [check for rule 13]
!!DO704/0/155/1&3/y-4=1/y-1<>1:P; [clear learning stones etc. on day 1 of month]

!!FU709:P4; [check for rule 4]
!!DO696/0/155/1&3:P; [create magic plains by today's heroes]

!!FU709:P15; [check for rule 15]
!!DO697/0/155/1&3:P; [upgraded 7th level creatures become 8th for all heroes]

!!FU709:P10; [check for rule 10]
!!OW:C?y-6; [get current player: y-6]
!!OW:Iy-6/?y-5; [get AI 1 or human 0]
!!UN&3/y-5=1:Sv573/v573/0/y-6/v574; [reveal entire map, surface]
!!UN&3/y-5=1/v575=1:Sv573/v573/1/y-6/v574; [reveal entire map, underground]
!!FU721&3/y-5=0:Py-6; [replace shroud for human, to handle AI teammate]
!!UN&3/y-5=0:R1; [redraw screen]

** end of timer routine


** function to choose rules and implement rules that only take effect at start

!?FU690;

!!VRv1:S0; [make sure v1 starts as 0]
!!VRv2:S0; [make sure v2 starts as 0]
!!VRv3:S0; [make sure v3 starts as 0]
!!VRv4:S0; [make sure v4 starts as 0]
!!VRv579:S0; [initialize map rules page 1 to none selected]
!!VRv576:S0; [intialize map rules page 2 to none selected]
!!UN:P101/?v1; [check wogify options num: 0=choose rules each game but default to selected]
[                                         1=choose rules each game (no defaults)          ]
[                                         2=use rules selected in wogify options          ]
[                                         3=use random, 4=random from selected, 5=disable]

!!IF:V2/0; [Clear flag 2]
!!IF&v1<>3:V2/1; [If v1=3 use random rules]

 [Use Map Rules selected in WoGify if this option was chosen]
 [or set these numbers to start if option 1 was chosen]
!!DO723/111/120/1|v1=0/v1=2:P;  [set v579]
!!DO724/121/131/1|v1=0/v1=2:P;  [set v576]

!!VRz326&2/v1<=1:Sz101020;
!!IF&2/v1<=1:G0/579/v579/326/332/333/334/335/336/337/338/339/340/341; [get bit choices in v579]
!!VRz326&2/v1<=1:Sz101021;
!!IF&2/v1<=1:G0/576/v576/326/342/343/344/345/346/347/348/349/350/351/680; [get bit choices in v576]

!!UN|-2/v1=4:U98/8/?y1; [count confluxes]
!!VRy2|-2/v1=4:S19; [normally 20 rules]
!!VRy2&-2/y1=0:S20; [21 rules if no confluxes]
!!VRy2&v1=4/y1=0:S20; [21 rules if no confluxes]
!!VRy3&-2:S1 Ry2; [pick a random rule]
!!VRy3&-2/y1>0/y3>=20:+1; [skip conflux]
!!VRy4&-2:S1 Ry2; [pick another one]
!!VRy4&-2/y1>0/y4>=20:+1; [skip conflux]
!!VRy4&-2/y3=y4:S1 Ry2; [if the same, try again]
!!VRy4&-2/y1>0/y4>=20:+1; [skip conflux]
!!VRy4&-2/y3=y4:S1 Ry2; [if the same, try again]
!!VRy4&-2/y1>0/y4>=20:+1; [skip conflux]
!!VRy4&-2/y3=y4:S1 Ry2; [if the same, try again]
!!VRy4&-2/y1>0/y4>=20:+1; [skip conflux]
!!VRy4&-2/y3=y4:S1 Ry2; [if the same, try again, just one rule if still the same]
!!VRy4&-2/y1>0/y4>=20:+1; [skip conflux]
!!DO725/1/100/1&v1=4:Py2; [get 1st random rule from selected rules if that option was chosen]
!!DO726/1/100/1&v1=4:Py2; [get 2nd random rule from selected rules if that option was chosen]
!!VRy3&v1=4:Sv3; [random from selected rule 1]
!!VRy4&v1=4:Sv4; [random from selected rule 2]
!!FU722|-2/v1=4:Py3 Py4; [set the bits for our random rules]

!!DO718/1/21/1:P; [add papyrus messages for any rules that are active]

!!VRv600:S0; [flag for 1st rule announced]
!!UN:P230/?y6; [Check if Display Map Rules chosen at Map Start is enabled: y6]
!!DO691/1/21/1&y6=1:P; [announce rules to first player, who is always human]

!!UN:R5/1/1; [Change mouse cursor to "wait" hourglass symbol]
!!UN:R7/0/1; [Show mouse cursor and disable changing]

!!FU709:P1; [check for rule 1]
!!DO692/0/173/1&3:P; [fliers lose flying but can't be retaliated against]

!!FU709:P2; [check for rule 2]
!!VRv700:C25/21/18/15/13/11/9/7/6/5; [table of new monster speeds]
!!VRv710:C5/5/4/4/4/4/4/3/3/3;
!!DO693/0/173/1&3:P; [invert monster speeds]

!!FU709:P5; [check for rule 5]
!!DO698/0/173/1&3:P; [all monsters attack/shoot twice]

!!FU709:P7; [check for rule 7]
!!UN&3:U17/-1/?y1; [count dwellings]
!!VRv600:S-1; [reset coordinate index]
!!VRy4:S140; // set y4 to boars
!!UN:P67/=1; // check if Neutral town is active (flag1)
!!VRy4&1:S112; // if yes set to Air Elementals instead of boars
!!DO701/1/y1/1&3:Py4; [add boars / Air Elementals to dwellings]

!!FU709:P8; [check for rule 8]
!!DO702/0/155/1&3:P-1; [upgrade unowned heroes to 5th level]

!!FU709:P18; [check for rule 18]
!!DO715/0/173/1&3:P; [all monsters gain breath weapon]

!!FU709:P19; [check for rule 19]
!!DO716/0/173/1&3:P; [shooters lose shooting but gain +50% hit points]

!!FU709:P20; [check for rule 20]
!!UN&3:U98/-1/?y1; [count towns: y1]
!!UN&3:U98/8/?y5; [count confluxes: y5]
!!VRv600:S-1; [reset coordinate index]
!!DO717/1/y1/1&3/y1>0/y5=0:P; [build grails in towns]

!!FU709:P21; [check for rule 21]
!!UN&3:U98/-1/?y1; [Number of Towns on map: y1]
!!VRv1:S-1; [reset coordinate index]
!!DO26/1/y1/1&3/y1>0:P; [Place Warlord's Banners in front of towns]

!!UN:R7/0/0; [Show mouse cursor and enable changing]
!!UN:R5/0/0; [Restore default cursor]

** end of function


** function to announce a rule to human players if set

!?FU691;

!!FU709:Px16; [check for rule x16]

!!VRz326:Sz101022;
!!VRz326&v600=1:Sz101023;
!!VRv600&3:S1;

!!IF&3/x16=1:M1/z101024;
!!IF&3/x16=2:M1/z101025;
!!IF&3/x16=3:M1/z101026;
!!IF&3/x16=4:M1/z101027;
!!IF&3/x16=5:M1/z101028;
!!IF&3/x16=6:M1/z101029;
!!IF&3/x16=7:M1/z101030;
!!IF&3/x16=8:M1/z101031;
!!IF&3/x16=9:M1/z101032;
!!IF&3/x16=10:M1/z101033;
!!IF&3/x16=11:M1/z101034;
!!IF&3/x16=12:M1/z101035;
!!IF&3/x16=13:M1/z101036;
!!IF&3/x16=14:M1/z101037;
!!IF&3/x16=15:M1/z101038;
!!IF&3/x16=16:M1/z101039;
!!IF&3/x16=17:M1/z101040;
!!IF&3/x16=18:M1/z101041;
!!IF&3/x16=19:M1/z101042;
!!UN&3/x16=20:U98/8/?y1; [count confluxes: y1]
!!IF&3/x16=20/y1=0:M1/z101043;
!!IF&3/x16=21:M1/z101052;


** end of function


** function to make monster x16 lose flying but gain no-retaliation if it flew

!?FU692;

!!MA:Xx16/?y1; [get flags]
!!VRy2:Sy1;
!!VRy2:&2; [just look at flying bit]
!!VRy1:&-3; [lose flying]
!!VRy1&y2=2:|65536; [can't be retaliated against]
!!MA&x16<145|x16>149:Xx16/y1; [save flags, except for war machines]

** end of function


** function to invert the speed of monster x16

!?FU693;

!!MA:Sx16/?y1; [get speed]
!!VRy2:S3; [default of 3 for very fast monsters]
!!VRy3:S700 +y1;
!!VRy2&y1<20:Svy3; [look up new speed]
!!MA&x16<145|x16>149:Sx16/y2; [set speed, except for war machines]

** end of function


** function to make monster x16 cost half as much

!?FU694;

!!MA:Cx16/6/?y1; [get gold cost]
!!VRy1::2; [half price]
!!MA&x16<145|x16>149:Cx16/6/y1; [save gold cost, except for war machines]

** end of function


** function to make monster x16 cost twice as much

!?FU695;

!!MA:Cx16/6/?y1; [get gold cost]
!!VRy1:*2; [double price]
!!VRy2:Sy1 %10; [check low digit of price]
!!VRy1&y2=4:+1; [turn 4's to 5's]
!!MA&x16<145|x16>149:Cx16/6/y1; [save gold cost, except for war machines]

** end of function


** function to place magic plains by current player's hero

!?FU696;

!!OW:C?y1; [get current player]
!!HEx16:O?y2; [get hero's owner]
!!HEx16:P?y3/?y4/?y5; [get hero x/y/l]
!!VRy3:+2;
!!VRy4:+1; [place so that hero is kind of in the middle]
!!TRy3/y4/y5:P?y6; [get y6=0 if red square]
!!TRy3/y4/y5:E?y7; [get y7=0 if yellow square]
!!TRy3/y4/y5:T?y8/d/d/d/d/d/d/d; [get terrain in y8, 8 = water]
!!UN&y1=y2/y5=0/y6<>0/y7<>0/y8<>8:Iy3/y4/y5/46/0; [place magic plains]

 [If Passable Terrain script active, make squares passable again]
!!UN:P63/?y9; [Check if Passable Terrain script is enabled: y9]
!!FU&y9<>1:E; [Exit if Passable Terrain isn't enabled]
!!UN:X?y10/?y11;          [Map size: y10]

!!VRy12:Sy4 -3;  [Starting y-coordinate for loop (Magic Plains/Cursed Land): y12]
!!VRy13:Sy4 +0;  [Ending y-coordinate for loop (Magic Plains/Cursed Land): y13]
!!VRy12&y12<0:S0;         [No y-coordinate below 0: y12]
!!VRy13&y13>=y10:Sy10 -1; [No y-coordinate above map size: y13]

!!DO8/y12/y13/1:P0/y3/y5; [Make trees, mountains etc. passable again]

** end of function


** function to make hero's upgraded 7th level troops into 8th level

!?FU697;

!!OW:C?y1; [get current player]
!!HEx16:O?y2; [get hero's owner]
!!DO708/0/6/1&y1=y2:Px16; [check each slot]

** end of function


** function to make monster x16 attack/shoot twice

!?FU698;

!!MA:Xx16/?y1; [get flags]
!!VRy1:|32768; [set attack twice bit]
!!MA&x16<145|x16>149:Xx16/y1; [save flags, except for war machines]

** end of function


** function to make hero x16's monster stacks grow by 4%, to 10000

!?FU699;

!!OW:C?y1; [get current player]
!!HEx16:O?y2; [get hero's owner]
!!DO700/0/6/1&y1=y2:Px16; [handle each slot if it's this hero's player's turn]

** end of function


** function to make slot x16 of hero x1's army grow by 4%, to 10000

!?FU700;

!!HEx1:C0/x16/?y1/?y2; [get y1 type y2 qty]
!!VRy3:Sy2 *104 :100; [add 4%]
!!VRy3&y3>10000/y2<10000:S10000; [limit to 10000]
!!HEx1&y2<10000:C0/x16/y1/y3; [store new qty]

** end of function


** function to add boars to dwelling number x16 if not first level

!?FU701;
** x1 = new type (140 = boars, 112 = Air Elemental)
!!UN:U17/-1/-1/600; [get dwelling location into v600-v602]
!!DW600:M0/?y1/?y2; [get y1 type y2 qty in slot 0]
!!MA:Ly1/?y3; // get monster Level
!!DW600&y3>0:M1/x1/12; [12 boars / Air Elementals in slot 1]

** end of function


** function to make hero x16 at least 5th level if it has owner x1

!?FU702;

!!HEx16:O?y1; [get hero's owner]
!!HEx16&y1=x1:Ed5000; [give 5000 experience]

** end of function


** function to double hero x16's movement

!?FU703;

!!OW:H-1/3/x16; [store hero number of hero x16 in v3]
!!HEv3:W?y1; [get hero's movement: y1]
!!VRy1:*2; [double hero's movement: y1]
!!HEv3:Wy1 Gy1; [set hero's new movement plus initial hero movement values]

** end of function


** artifact trigger

!?OB5;

!!VRv1:S0; [make sure v1 starts as 0]
!!UN:P101/?v1; [check wogify options number - 5 means no rules]
!!FU&v1=5:E; [Exit if map rules aren't enabled]

!!PO998:N?y-5; // get custom number of the artifact-field
!!FU&y-5=0:E; // exit if =0 (usually an artifact left by the player)
!!PO998&y-5=15:N0; // set to 0 it was an "original" artifact (placed on the map by the map maker)

!!FU709:P9; [check for rule 9]
!!OB998:U?y-1; [get artifact number]
!!UN:Ay-1/?y-4; (Get enabled/disabled state)
!!HE-1:A2/y-1/?y-2/?y-3; [get y-2 number carried y-3 number equipped]
!!HE-1&y-2=0/3/y-4=0/y-1>=7:Ay-1; [give an extra copy if they don't already have one, and it's 7 (Centaur's Axe) or higher]
!!UN:N0/-1/y-1; [get name of artifact into z2]
!!IF&1000/y-2=0/3/y-4=0:M1/z101044;

** end of artifact trigger

!?TM19&$once$=1; // uses Timer19 from script00 (object replacement); only once at day 1
!!UN:P119/=1; !!FU&-1:E;  // check for double artifact rule, exit if disabled
!!UN:U5/-1/?y-1; // get number of artifacts on the map
!!VRv7:S-1;
!!DO727/1/y-1/1:P;


!?FU727;
!!UN:U5/-1/-1/7; // pos next artifact to v7-v9
!!PO7:N15; // set custom number for map field to 15 (used for double artifact rule)



** hero movement trigger

!?HM-1;

!!VRv1:S0; [make sure v1 starts as 0]
!!UN:P101/?v1; [check wogify options number - 5 means no rules]
!!FU&v1=5:E; [Exit if map rules aren't enabled]

!!FU709:P10; [check for rule 10]
!!FU711&3:P; [handle replacing shroud]

** end of hero movement trigger


** function to let hero x16 revisit objects

!?FU704;

!!HEx16:O?y1; [get owner]
!!OW:C?y2; [get current player]
!!DO705/0/9/1&y1=y2:Px16; [mark learning stones through schools of war as unvisited]

** end of function


** function to let hero x1 revisit object x16

!?FU705;

!!HEx1:Vx16/0; [set hero to have not visited all objects of this type]

** end of function


** function to change a mine's produced resource

!?FU706;

!!UN:U53/-1/-1/600; [get mine location into v600-v602]
!!VRy1:S0 R6; [random 0-6]
!!MN600:Ry1/1; [set random resource produced]

** end of function


** hero level-up trigger

!?HL-1;

!!VRv1:S0; [make sure v1 starts as 0]
!!UN:P101/?v1; [check wogify options number - 5 means no rules]
!!FU&v1=5:E; [Exit if map rules aren't enabled]

!!FU709:P14; [check for rule 14]
!!FU710&3:P; [handle giving hero a 9th-10th skill]

!!FU709:P16; [check for rule 16]
!!FU712&3:P; [give hero an artifact]

** end of hero level-up trigger


** function to count skills for a hero

!?FU707;

!!HE-1:Sx16/?y1; [check for presence of skill]
!!VRv549&y1>0:+1; [count it]

** end of function


** function to convert a stack of upgraded 7th level to 8th level
** hero x1, slot x16

!?FU708;

!!HEx1:C0/x16/?y1/?y2; [get y1 type y2 qty]
!!VRy1&y1=13:S150; [archangel]
!!VRy1&y1=27:S151; [gold dragon]
!!VRy1&y1=41:S152; [titan]
!!VRy1&y1=55:S153; [archdevil]
!!VRy1&y1=69:S154; [ghost dragon]
!!VRy1&y1=83:S155; [black dragon]
!!VRy1&y1=97:S156; [ancient behemoth]
!!VRy1&y1=111:S157; [chaos hydra]
!!VRy1&y1=131:S158; [phoenix]
!!VRy1&y1=210:S211; [wraith]
!!HEx1:C0/x16/y1/y2; [set creature type/qty]

** end of function


** function to set flag 3 if rule x1 has been chosen

!?FU709;

!!IF:V3/0;
!!VRy1:S579; [variable # of rule]
!!VRy1&x1>10:S576;
!!VRx1&x1<=20:-1 %10 +550; [variable # of bit value]
!!VRx1&x1=21:S560; [variable # of rule 21's bit value]
!!VRy2:Svy1 &vx1; [get just our bit]
!!IF&y2>0:V3/1; [set flag 3 if our bit set]

** end of function


** function to handle giving a 9th-10th skill to a hero going up a level

!?FU710;

!!VRv549:S0; [count of skills for this hero]
!!DO707/0/27/1:P; [count the skills, return in v549]

** added by Thomas Franz (samot): check for Warfare
!!UN:P193/?y99; // check if Warfare-Script is on
!!HE-1&y99=1:S20/?y98; // check if Hero knows Artillery (and the other two skills too if warfare is on);
!!VRv549&y99=1/y98>0:-2; // decrease v549 (number of known skills) by two if warfare is on and hero knows artillery / fist aid / ballistics (only one skill is visible)
!!FU|v549<8/v549>10:E; // exit if hero has not enough or to much skills

!!VRy1:S0 R27; [pick a random skill to offer]
!!HE-1:Sy1/?y2; [check for presence of random skill]
!!HE-1:B0/?z352; [get hero name]
!!UN:N4/353/y1; [get skill name]
!!HE-1:R2/?y3; [get hero sex]
!!VRz354&y3=0:Sz101045;
!!VRz354&y3=1:Sz101046;
!!VRy4:Sy1 *3 +3; [get picture of skill]
!!IF:V2/0; [assume won't learn skill]
!!IF&1000/y2=0/v549>=8/v549<=9:Q2/20/y4/2/z101047;
!!IF&-1000/y2=0/v549>=8/v549<=9:V2/1; [AI always learns it]
!!HE-1&2:Sy1/1; [give skill]

** end of function


** function to handle replacing shroud for a hero

!?FU711;

!!HE-1:O?y1; [get owner]
!!OW:Iy1/?y2; [get AI 1 or human 0]
!!FU721&y2=0:Py1; [do a Cover Of Darkness for a non-teammate of a human player]

** end of function


** function to give hero an artifact based on experience

!?FU712;

!!HE-1:E?y2; [get experience]
**Here's new part of code
!!UN&y2<6200:J6/2/?y3;
!!UN&y2>=6200/y2<20600:J6/4/?y3;
!!UN&y2>=20600/y2<57533:J6/8/?y3;
!!UN&y2>=57533:J6/16/?y3;

!!HE-1:A4/y3; [give (equip) artifact]
!!HE-1:B0/?z2; [get hero name]
!!HE-1:O?y4; [get hero owner]
!!OW&y4<>-1:Iy4/?y5; [get AI 1 or human 0]
!!IF&y4<>-1/y5=0:Q2/8/y3/1/z101048;

** end of function


** Object triggers for Rogue fights

!?OB12/-1; [campfire]
!!VRv1:S0; [make sure v1 starts as 0]
!!UN:P101/?v1; [check wogify options number - 5 means no rules]
!!FU713&v1<>5:P; [handle generating roving bands of Rogues]

!?OB17/-1; [most creature dwellings]
!!VRv1:S0; [make sure v1 starts as 0]
!!UN:P101/?v1; [check wogify options number - 5 means no rules]
!!FU713&v1<>5:P; [handle generating roving bands of Rogues]

!?OB29/-1; [flotsam]
!!VRv1:S0; [make sure v1 starts as 0]
!!UN:P101/?v1; [check wogify options number - 5 means no rules]
!!FU713&v1<>5:P1; [handle generating roving bands of Rogues]

!?OB32/-1; [garden of revelation]
!!VRv1:S0; [make sure v1 starts as 0]
!!UN:P101/?v1; [check wogify options number - 5 means no rules]
!!FU713&v1<>5:P; [handle generating roving bands of Rogues]

!?OB39/-1; [lean to]
!!VRv1:S0; [make sure v1 starts as 0]
!!UN:P101/?v1; [check wogify options number - 5 means no rules]
!!FU713&v1<>5:P; [handle generating roving bands of Rogues]

!?OB47/-1; [school of magic]
!!VRv1:S0; [make sure v1 starts as 0]
!!UN:P101/?v1; [check wogify options number - 5 means no rules]
!!FU713&v1<>5:P; [handle generating roving bands of Rogues]

!?OB51/-1; [mercenary camp]
!!VRv1:S0; [make sure v1 starts as 0]
!!UN:P101/?v1; [check wogify options number - 5 means no rules]
!!FU713&v1<>5:P; [handle generating roving bands of Rogues]

!?OB53/-1; [mine]
!!VRv1:S0; [make sure v1 starts as 0]
!!UN:P101/?v1; [check wogify options number - 5 means no rules]
!!FU713&v1<>5:P; [handle generating roving bands of Rogues]

!?OB55/-1; [mystical garden]
!!VRv1:S0; [make sure v1 starts as 0]
!!UN:P101/?v1; [check wogify options number - 5 means no rules]
!!FU713&v1<>5:P; [handle generating roving bands of Rogues]

!?OB57/-1; [obelisk]
!!VRv1:S0; [make sure v1 starts as 0]
!!UN:P101/?v1; [check wogify options number - 5 means no rules]
!!FU713&v1<>5:P; [handle generating roving bands of Rogues]

!?OB58/-1; [redwood observatory]
!!VRv1:S0; [make sure v1 starts as 0]
!!UN:P101/?v1; [check wogify options number - 5 means no rules]
!!FU713&v1<>5:P; [handle generating roving bands of Rogues]

!?OB60/-1; [pillar of fire]
!!VRv1:S0; [make sure v1 starts as 0]
!!UN:P101/?v1; [check wogify options number - 5 means no rules]
!!FU713&v1<>5:P; [handle generating roving bands of Rogues]

!?OB61/-1; [star axis]
!!VRv1:S0; [make sure v1 starts as 0]
!!UN:P101/?v1; [check wogify options number - 5 means no rules]
!!FU713&v1<>5:P; [handle generating roving bands of Rogues]

!?OB79/-1; [resource]
!!VRv1:S0; [make sure v1 starts as 0]
!!UN:P101/?v1; [check wogify options number - 5 means no rules]
!!FU713&v1<>5:P; [handle generating roving bands of Rogues]

!?OB82/-1; [sea chest]
!!VRv1:S0; [make sure v1 starts as 0]
!!UN:P101/?v1; [check wogify options number - 5 means no rules]
!!FU713&v1<>5:P1; [handle generating roving bands of Rogues]

!?OB88/-1; [shrine of magic incantation]
!!VRv1:S0; [make sure v1 starts as 0]
!!UN:P101/?v1; [check wogify options number - 5 means no rules]
!!FU713&v1<>5:P; [handle generating roving bands of Rogues]

!?OB94/-1; [stables]
!!VRv1:S0; [make sure v1 starts as 0]
!!UN:P101/?v1; [check wogify options number - 5 means no rules]
!!FU713&v1<>5:P; [handle generating roving bands of Rogues]

!?OB100/-1; [learning stone]
!!VRv1:S0; [make sure v1 starts as 0]
!!UN:P101/?v1; [check wogify options number - 5 means no rules]
!!FU713&v1<>5:P; [handle generating roving bands of Rogues]

!?OB101/-1; [treasure chest]
!!VRv1:S0; [make sure v1 starts as 0]
!!UN:P101/?v1; [check wogify options number - 5 means no rules]
!!FU713&v1<>5:P; [handle generating roving bands of Rogues]

!?OB102/-1; [tree of knowledge]
!!VRv1:S0; [make sure v1 starts as 0]
!!UN:P101/?v1; [check wogify options number - 5 means no rules]
!!FU713&v1<>5:P; [handle generating roving bands of Rogues]

!?OB104/-1; [university]
!!VRv1:S0; [make sure v1 starts as 0]
!!UN:P101/?v1; [check wogify options number - 5 means no rules]
!!FU713&v1<>5:P; [handle generating roving bands of Rogues]

!?OB105/-1; [wagon]
!!VRv1:S0; [make sure v1 starts as 0]
!!UN:P101/?v1; [check wogify options number - 5 means no rules]
!!FU713&v1<>5:P; [handle generating roving bands of Rogues]

!?OB107/-1; [school of war]
!!VRv1:S0; [make sure v1 starts as 0]
!!UN:P101/?v1; [check wogify options number - 5 means no rules]
!!FU713&v1<>5:P; [handle generating roving bands of Rogues]

!?OB109/-1; [water wheel]
!!VRv1:S0; [make sure v1 starts as 0]
!!UN:P101/?v1; [check wogify options number - 5 means no rules]
!!FU713&v1<>5:P; [handle generating roving bands of Rogues]

!?OB112/-1; [windmill]
!!VRv1:S0; [make sure v1 starts as 0]
!!UN:P101/?v1; [check wogify options number - 5 means no rules]
!!FU713&v1<>5:P; [handle generating roving bands of Rogues]

!?OB113/-1; [witch hut]
!!VRv1:S0; [make sure v1 starts as 0]
!!UN:P101/?v1; [check wogify options number - 5 means no rules]
!!FU713&v1<>5:P; [handle generating roving bands of Rogues]

** end of set of object triggers


** function to generate roving bands of Rogues

!?FU713;

!!FU709&x1=0:P17; [check for rule 17]
!!OW&x1=0:C?y1; [get current player]
!!OW&x1=0:Iy1/?y2; [get AI 1 or human 0]
!!VRy1&y2=0/x1=0:S0 R15; [1 in 16 chance for human]
!!VRy1&y2=1/x1=0:S0 R63; [1 in 64 chance for AI]
!!VRy3&x1=0:Sc0; [get current day]
!!IF&3/x1=0:V875/0;
!!FU714&3/y1=0/y3<>v572/x1=0:Py2/x1; [continue if odds are met and rule active and no other Rogues today]

!!FU&x1=0:E; [exit if land object]

!!FU709:P17; [check for rule 17]
!!OW:C?y1; [get current player]
!!OW:Iy1/?y2; [get AI 1 or human 0]
!!VRy1&y2=0:S0 R15; [1 in 16 chance for human]
!!VRy1&y2=1:S0 R63; [1 in 64 chance for AI]
!!VRy3:Sc0; [get current day]
!!IF&3:V875/0;
!!FU714&3/y1=0/y3<>v572:Py2/x1; [continue if odds are met and rule active and no other Rogues today]


** end of function


** function to actually cause a Rogue fight

!?FU714;

!!HE-1&x2=0:B0/?z326; [get hero name]
!!IF&x1=0/x2=0:Q1/21/143/1^%Z101049^;
!!VRy1&x2=0:Sc0; [get day of game]
!!VRy1&x2=0:+6;

!!VRy1&x2=0::7; [get week of game]
!!VRv1262&x2=0:Sy1 *10; [10 Rogues per elapsed week: v1262]

!!HE-1&x2=0:Ed/?y3/1; [get hero level]
!!VRy3&x2=0:*10; [hero level *10]
!!VRv1262&x2=0:+y3; [#Rogues = 10*week + 10*level]

!!HE-1&x2=0:P?y2/?y3/?y4; [get hero location]
!!IF&x2=0:V875/1;           (For mithril and chest scripts)
!!HE-1&x2=0:N?v1219; [get hero number]
!!HE-1&x2=0:Ty2/y3/y4/143/v1262; [provoke Rogue battle]
!!HEv1219&x2=0:O?y2; [get hero owner]
!!VRv1262&x2=0:*10 +300; [10 gold per Rogue + 300]
!!OW&y2<>-1/x2=0:Ry2/6/dv1262; [give gold if hero lived]
!!IF&y2<>-1/x2=0:V875/0;    (For mithril and chest scripts)
!!IF&x1=0/y2<>-1/x2=0:Q2/6/v1262/1/z101050;
!!VRv572&x2=0:Sc0; [set day of last Rogue fight]

!!FU&x2=0:E; [exit if land object]


!!HE-1:B0/?z326; [get hero name]
!!IF&x1=0:Q1/21/115/1^%Z101056^;
!!VRy1:Sc0; [get day of game]
!!VRy1:+6;

!!VRy1::7; [get week of game]
!!VRv1262:Sy1 *7; [10 Water Elementals per elapsed week: v1262]

!!HE-1:Ed/?y3/1; [get hero level]
!!VRy3:*7; [hero level *10]
!!VRv1262:+y3; [#Water Elementals = 10*week + 10*level]

!!HE-1:P?y2/?y3/?y4; [get hero location]
!!IF:V875/1;           (For mithril and chest scripts)
!!HE-1:N?v1219; [get hero number]
!!HE-1:Ty2/y3/y4/115/v1262; [provoke Water Elemental battle]
!!HEv1219:O?y2; [get hero owner]
!!VRv1262:*25; [25 gold per Rogue]
!d!OW&y2<>-1:Ry2/6/dv1262; [give gold if hero lived]
!!IF&y2<>-1:V875/0;    (For mithril and chest scripts)
!d!IF&x1=0/y2<>-1:Q2/6/v1262/1/z101050;
!!VRv572:Sc0; [set day of last Rogue fight]

** end of function


** function to make monster x16 gain a breath weapon

!?FU715;

!!MA:Xx16/?y1; [get flags]
!!VRy1:|8; [breath weapon]
!!MA&x16<145|x16>149:Xx16/y1; [save flags, except for war machines]

** end of function


** function to make monster x16 lose shooting but gain +50% hit points if it shot

!?FU716;

!!MA:Xx16/?y1; [get flags]
!!VRy2:Sy1;
!!VRy2:&4; [just look at shooting bit]
!!VRy1:&-5; [lose shooting]
!!VRy1:|4096; [no penalty in close combat]
!!MA&x16<145|x16>149:Xx16/y1; [save flags, except for war machines]

!!MA:Px16/?y3; [get hit points]
!!VRy3&y2=4:*15 :10; [+50% if creature shot]
!!MA&x16<145|x16>149:Px16/y3; [set hit points, except for war machines]

** end of function


** function to build a grail in town # x16

!?FU717;

!!UN:U98/-1/-1/600; [get town location into v600-v602]
!!CA600:B1/26; [build grail]
!!CA600:T?y1; [get town type]
!!CA600:O?y2; [get town owner]
!!UN&y1=2:Sv573/v573/0/y2/v574; [remove entire shroud, surface, for tower's skyship]
!!UN&y1=2:Sv573/v573/1/y2/v574; [remove entire shroud, underground]

** end of function


** function to add rule x16 to the papyrus if active

!?FU718;

!!FU709:Px16; [check for rule x16]
!!VRy1:S331 +x16; [z variable for this rule]
!!VRy1&x16>20:S659 +x16; [z variable for rule 21+]
!!VRy2:S0; [initialize y2 to 0: y2]
!!UN&x16=20:U98/8/?y2; [count confluxes if rule 20: y2]
!!QW&3/y2=0:Ay1/-2/-2/y1; [set papyrus message]

** end of function


** function to replace shroud for player x1 via Cover Of Darkness for their enemy

!?FU721;

!!VRy1:S564 +x1; [variable # of non-teammate player]
!!UN:Hv573/v573/0/vy1/v574; [replace entire shroud, surface]
!!UN&v575=1:Hv573/v573/1/vy1/v574; [replace entire shroud, underground]

** end of function


** function to set rule bit for numbered rule x1

!?FU722;

!!VRy1:S579; [variable # of bit]
!!VRy1&x1>10:S576;
!!VRx1&x1<21:-1 %10 +550; [variable # of bit value]
!!VRx1&x1=21:S560; [Warlord's Banner]
!!VRvy1:|vx1; [set bit]

** end of function


** functions to add map rules selected in WoGify options dialogue

!?FU723;

!!UN:Px16/?y1;      [Check if each map rule is enabled (111-120)
!!VRy2:Sx16 -111;   [Get loop number]
!!VRy3&y1=1:S550 +y2;  [Get corresponding variable with bit number]
!!VRv579&y1=1:+vy3;  [If it's enabled, add bit number to v579]

!?FU724;

!!UN:Px16/?y1;      [Check if each map rule is enabled (121-130)
!!UN&x16=131:P239/?y1; [Check if Warlord's Banner (rule 21) is enabled]
!!VRy2:Sx16 -121;   [Get loop number]
!!VRy3&y1=1:S550 +y2;  [Get corresponding variable with bit number]
!!VRv576&y1=1:+vy3;  [If it's enabled, add bit number to v576]

** end of functions

** functions choose two random rules, calling themselves again if a rule wasn't selected
** If it can't find two unique selected rules after 100 loops, use only 1 or none.

!?FU725;

!!VRv3:S1 Rx1; [Random rule]
!!VRv3&x1=19/v3>=20:+1; [skip conflux]
!!VRy1:Sv3 +110; [Convert to ERM number]
!!VRy1&v3=21:S239; [Warlord's Banners ERM Number]
!!UN:Py1/?y2; [See if it's selected in Wogify options]
!!VRv3&y2=0:S0; [set no rule found if it wasn't a selected rule]
!!VRx16&y2=1:S101; [exit loop if a selected rule is found]

!?FU726;

!!VRv4:S1 Rx1; [Random rule]
!!VRv4&x1=19/v4>=20:+1; [skip conflux]
!!VRy1:Sv4 +110; [Convert to ERM number]
!!VRy1&v4=21:S239; [Warlord's Banners ERM Number]
!!UN&v3<>v4:Py1/?y2; [See if it's selected in Wogify options]
!!VRv4&y2=0:S0; [set no rule found if it wasn't a selected rule]
!!VRx16&y2=1/v3<>v4:S101; [exit loop if a unique selected rule is found]

** end of functions


 [Put Warlord's Banners in front of Towns]
!?FU26;
!!UN:U98/-1/-1/1; [Store Town Coordinates in v1/v2/v3]
!!CA1:T?y2 O?y3; [Town Type: y2, Owner: y3]
!!IF:V1/0 V2/0; [Set Flags 1 and 2 to False]
!!VRy4:Sv1;    [Copy x coordinate to y4]
!!VRy5:Sv2 +1; [Copy y+1 coordinate to y5]
!!UN:X?y6/?y7; [Size of map: y6]
!!FU&y5>=y6:E; [Exit Function if Y coordinate will be off the bottom of the map]
!!UN:Ey4/y5/v3; [Check space directly in front of Town: Flag 1=True if blocked]
!!VRy4&1:Sv1 -1; [X coordinate 1 to the left of entrance: y4]
!!IF&y4<0/1:V2/1; [Set Flag 2 to True if X coordinate will be off the map to the left]
!!UN&1/-2:Ey4/y5/v3; [Check space to left in front of Town: Flag 1=True if blocked]
!!VRy4&1:Sv1 +1; [X coordinate 1 to the right of entrance: y4]
!!IF:V2/0; [Reset Flag 2 to False]
!!IF&y4>=y6/1:V2/1; [Set Flag 2 to True if X coordinate will be off the map to the right]
!!UN&1/-2:Ey4/y5/v3; [Check space to right in front of Town: Flag 1=True if blocked]
!!FU|1/2:E; [Exit if all spaces blocked or off the map]
!!UN:Iy4/y5/v3/5/156/0; [Place Warlord's Banner, no redraw]
!!UN:J2/?y9; [Player Difficulty level (0..4): y9]
!!VRy8&y9<=2:Sy2 +1; [Add 1 to town type (y2) for Easy/Normal/Hard levels: y8]
!!VRy10&y9=0:Sy2 -2; [Upgraded level 6 for Easy difficulty: y10]
!!VRy10&y9=1:Sy2 -1; [Non-upgraded level 7 for Normal difficulty: y10]
!!VRy10&y9=2:Sy2;    [Upgraded level 7 for Hard difficulty: y10]
!!VRy8&y9<=2:*13 +y10; [Level 6 or 7 Guardian (for Easy/Normal/Hard difficulty): y8]
!!VRy8&y9=0/y2=8:S121; [Set Magic Elemental for Conflux Town (for Easy difficulty): y8]
!!VRy8&y9=1/y2=8:S130; [Set Firebird for Conflux Town (for Normal difficulty): y8]
!!VRy8&y9=2/y2=8:S131; [Set Phoenix for Conflux Town (for Hard difficulty): y8]
!!VRy8&y9>=3:S150 +y2; [Level 8 Guardian (for Expert/Impossible difficulty): y8]
!!ARy4/y5/v3&y3>=0:X0 G0/y8/0; [If Town is owned, set 0 guardians and don't turn them on]
!!POy4/y5/v3&y3>=0:Oy3; [Set PO:O to town owner (y3)]
!!FU&y3>=0:E; [Exit if Town is owned: Banner is unguarded until month 2]
!!UN&y8>=0:N3/-1/y8/0; [Name of single monster: z-1]
!!ARy4/y5/v3:X1 G0/y8/1 M1/z101053; [Set 1 Guardian with message]

** end of function

 [Weekly Timer, only activates once (for first colour)]
!?TM2&$once$=1/$weekday$=$monday$/$day$>7;
!!FU709:P21; [check for rule 21]
!!FU&-3:E; [Exit if rule not enabled]
!!UN:U5/156/?y-10; [Number of Warlord's Banners on map: y-10]
!!FU&y-10<1:E; [Exit if no Banners on map]
!!VRy-11:Sc0 -1; [Current day -1: y-11]
!!VRy-1:Sy-11 :7 +1; [Current week: y-1]
!!VRy-22:Sy-1 :2 *2; [Check if it's every 2rd week: y-22]
!!VRy-2&y-22=y-1:S1; [Set y-2 to 1 every 2nd (even) week: y-2]
!!VRy-33:Sy-1 :3 *3; [Check if it's every 3rd week: y-33]
!!VRy-3&y-33=y-1:S1; [Set y-3 to 1 every 3rd week: y-3]
!!VRy-44:Sy-1 :4 *4; [Check if it's every 4th week: y-44]
!!VRy-4&y-44=y-1:S1; [Set y-4 to 1 every 4th week: y-4]
!!VRy-12:Sy-1 -1; [Current week -1: y-12]
!!VRy-55:Sy-12 :4 *4; [Check if it's the 1st of every month: y-55]
!!VRy-5&y-55=y-1:S1; [Set y-5 to 1 at the start of every month: y-5]
!!UN:J2/?y-6; [Player Difficulty level: y-6]
!!VRy-7&y-6=0/y-5=1:S1; [Set y-7 to 1 for Easy if start of a month: y-7]
!!VRy-7&y-6=1/y-5=1:S1; [Set y-7 to 1 for Normal if start of a month: y-7]
!!VRy-7&y-6=2/y-2=1:S1; [Set y-7 to 1 for Hard every 2 weeks: y-7]
!!VRy-7&y-6=3/y-5=1:S1; [Set y-7 to 1 for Expert if start of a month: y-7]
!!VRy-7&y-6=4/y-2=1:S1; [Set y-7 to 1 for Impossible every 2 weeks: y-7]
!!FU&y-7<>1:E; [Exit if not a banner guard increase day]
!!VRv1:S-1; [reset coordinate index]
!!DO28/1/y-10/1&y-10>0:P; [Loop through all banners]

** end of timer

 [Add 1 to Banner Guardians on day 1 of each month]
!?FU28;
!!UN:U5/156/-1/1; [Store Banner Coordinates in v1/v2/v3]
!!PO1:O?y2; [Read PO:N for Warlord's Banner: y2]
!!FU&y2=-2:E; [Exit if not a Banner in front of a town]
!!ARv1/v2/v3:G0/?y3/?y1; [Number of guards (y1), Type (y3)]
!!VRy1:+1; [Add 1 to number of guards: y1]
!!ARv1/v2/v3:X1 G0/d/y1; [Enable guards and set number of guards to y1]
!!UN&y3>=0/y1=1:N3/-1/y3/0; [Name of single monster: z-1]
!!UN&y3>=0/y1>1:N3/-1/y3/1; [Name of plural monsters: z-1]
!!ARv1/v2/v3&y1=1:M1/z101054; [Update message (if only 1 guard)]

!!ARv1/v2/v3&y1>1:M1/z101055; [Update message (if more than 1 guard)]

** end of function

