REM ===============================================================================================
REM     Preamble stuff
REM ===============================================================================================







LEVEL_VERSION(1)
RUN_AFTER_VICTORY(1)

SET_CREATURE_MAX_LEVEL(ALL_PLAYERS,ANY_CREATURE,7)
SET_CREATURE_MAX_LEVEL(ALL_PLAYERS,IMP,7)

REM Flags
REM 	PLAYER0, FLAG7 - Hint crate flag.

REM Campaign flags
REM 	PLAYER0, CAMPAIGN_FLAG0 - Secret level unlock. Currently a 3-digit binary value per three levels (000 to 111,  1st map adds 001 (1), 2nd adds 010 (2), 3rd adds 100 (4))
REM 	PLAYER0, CAMPAIGN_FLAG1 - Secret level unlock. Currently a 3-digit binary value per three levels (000 to 111,  4th map adds 001 (1), 5th adds 010 (2), 6th adds 100 (4))
REM 	PLAYER0, CAMPAIGN_FLAG2 - Secret level unlock. Currently a 3-digit binary value per three levels (000 to 111,  7th map adds 001 (1), 8th adds 010 (2), 9th adds 100 (4))
REM 	PLAYER0, CAMPAIGN_FLAG3 - Secret level unlock. Currently a 3-digit binary value per three levels (000 to 111, 10th map adds 001 (1), 11th adds 010 (2), 12th adds 100 (4))
REM 	PLAYER0, CAMPAIGN_FLAG4 - Secret level unlock. Currently a 3-digit binary value per three levels (000 to 111, 13th map adds 001 (1), 14th adds 010 (2), 15th adds 100 (4))
REM 	PLAYER0, CAMPAIGN_FLAG5 - Secret level unlock. Currently a 3-digit binary value per three levels (000 to 111, 16th map adds 001 (1), 17th adds 010 (2), 18th adds 100 (4))
REM 	PLAYER0, CAMPAIGN_FLAG6 - Secret level unlock. Currently a 2-digit binary value per three levels (00  to  11, 19th map adds 001 (1), 20th adds 010 (2))

REM -- 

REM 	TO DO: PLAYER1, CAMPAIGN_FLAG[0-7] should be [Troll early, Mistress early, Orc early, Druid, Hound early, Tentacle early,	Then maybe add some for other creatures e.g. Dragon, Maiden, Eyegazer]

REM		TO DO: Troll early? Only for level 5 and 6 (unlock on level 6 but say something about it if not unlocked already) 
REM			"The Troll High Council were disgusted that you did not reclaim the Workshops and have refused to join you on this."

REM		TO DO: Mistress early? Only for level 7 and 8 (unlock on level 8 but say something about it if not unlocked already)
REM			"Did not find their sister, so are reluctant to join, unless your prowess for conquest sways them."

REM 	PLAYER1, CAMPAIGN_FLAG2 - Got Orc early? Only for level 8 and 9 (unlock on level 9 but say something about it if not unlocked already)

REM		TO DO: Better Hound (add Ember and others)
REM		TO DO: Better Tentacle (add Bubble and others)

REM -- 

REM		Bonus level unlocks
REM		PLAYER3,CAMPAIGN_FLAG0 - First bonus level choice (0: off, 1: start with two additional enhanced Imps (level 3), 2: Lower initial cost of Imps)
REM			TODO: Add this to levels (ah hell add it to every level in case people go back and mop up old levels), figure out how to lower initial cost (ask Loob about it, think you have to temporarily set a room to have the SACRIFICE property for the sacrifice to work)

REM 	TODO: GOLD: A) Start with extra gold (DK2-style unsellable treasure room around heart, can place traps on it) B) Gold Seam contains extra gold or you can mine it quicker or swap some gold seam with dense gold?
REM 	TODO: A) rooms research faster	B) spells research faster		
REM		OR	  A) bridge, guard post and workshop are unlocked from the start (where available)	B) Sight of evil, CTA, hold audience are unlocked from the start (where available)


REM 	TODO: A) traps manufactured quicker	B) traps have more shots/sell for more
REM 	OR	  A) can place traps on subtiles B) can be placed on bridges
REM 	OR	  C)  You start the map with a small handful of traps/doors available to place	D) cull weaker traps (idk make it a clever way to turn on/off)

REM 	TODO: A) rooms cheaper	B) spells cheaper
REM 	OR	  A)	Spells stronger	B) spells cheaper

REM 	TODO: A) Insects always/never in pool	B) Insects are stronger (fly speed and ranged fighter, spider stronger+drain, beetle rebound+armour and immune to gas)

REM 	TODO: A) Higher creature limit B) Creatures enter at a higher level/max level is higher


REM -- 

REM		TO DO: The trap unlock ones
REM		TO DO: Maybe a smaller one for misc things.

REM --------------------------------------------------------------

REM Custom Hint Crate special
REM Possibly replace ==N== with LAST_EVENT for the latter two blocks, I think if you make it PLAYER0 and it was in a library it'll go back there.

REM		Need to juggle these around:
REM		0 - Hint Crate						(Green)
REM		1 - Receive Imps					(Standard)
REM		2 -
REM		7 - Reward (secret objective done)	(Gold)		(Need to make this different every map)

REM -- 

SET_BOX_TOOLTIP(0, "Hint Crate")
ADD_OBJECT_TO_LEVEL(SPECBOX_CUSTOM,  ==N==, 0)
SET_FLAG(PLAYER0,FLAG7,0)

IF(PLAYER0,BOX0_ACTIVATED > 0)
	IF(PLAYER0,FLAG7 == 0)
		NEXT_COMMAND_REUSABLE
		QUICK_INFORMATION(25," ===HINT TEXT===",ALL_PLAYERS)
		NEXT_COMMAND_REUSABLE
		SET_TIMER(PLAYER0,TIMER7)
		NEXT_COMMAND_REUSABLE
		SET_FLAG(PLAYER0,BOX0_ACTIVATED,0)
		NEXT_COMMAND_REUSABLE
		SET_FLAG(PLAYER0,FLAG7,1)
	ENDIF
ENDIF

IF(PLAYER0,TIMER7 >= 100)
	IF(PLAYER0,FLAG7 == 1)
		NEXT_COMMAND_REUSABLE
		CREATE_EFFECT(54,==N==)
		NEXT_COMMAND_REUSABLE
		ADD_OBJECT_TO_LEVEL(SPECBOX_CUSTOM, ==N==, 0, PLAYER0)
		NEXT_COMMAND_REUSABLE
		SET_FLAG(PLAYER0,FLAG7,0)
	ENDIF
ENDIF

REM -- 

SET_BOX_TOOLTIP(1, "Receive Imps")

IF(PLAYER0,BOX1_ACTIVATED > 0)
	NEXT_COMMAND_REUSABLE
	ADD_CREATURE_TO_LEVEL(PLAYER0,IMP,LAST_EVENT,4,3,500,JUMP)
	NEXT_COMMAND_REUSABLE
	PLAY_MESSAGE(PLAYER0,SOUND,488)
	NEXT_COMMAND_REUSABLE
	SET_FLAG(PLAYER0,BOX1_ACTIVATED,0)
ENDIF

REM -- 

		PLAY_MESSAGE(PLAYER0,SOUND,159)
		ADD_OBJECT_TO_LEVEL(SPECBOX_TRANSFR,1,0)
		QUICK_INFORMATION(11,"Looks like the gods favour you. Use this gift wisely.",1)

REM --------------------------------------------------------------

REM -- Unlock secret for level, update campaign flag.

REM For level N, update PLAYER0,CAMPAIGN_FLAG	[(N-1)/3]	by adding	2^[N-1 MOD 3]	to it.

REM For K= N-1 MOD 3:
REM	----- K = 0 (1,4,7,10,13,16,19) -----

IF(PLAYER0,CAMPAIGN_FLAG[(N-1)/3] == 0)
	SET_FLAG(PLAYER2,CAMPAIGN_FLAG[(N-1)/3],1)
ENDIF
IF(PLAYER0,CAMPAIGN_FLAG[(N-1)/3] == 2)
	SET_FLAG(PLAYER2,CAMPAIGN_FLAG[(N-1)/3],3)
ENDIF
IF(PLAYER0,CAMPAIGN_FLAG[(N-1)/3] == 4)
	SET_FLAG(PLAYER2,CAMPAIGN_FLAG[(N-1)/3],5)
ENDIF
IF(PLAYER0,CAMPAIGN_FLAG[(N-1)/3] == 6)
	SET_FLAG(PLAYER2,CAMPAIGN_FLAG[(N-1)/3],7)
ENDIF

REM	----- K = 1 (2,5,8,11,14,17,20) -----

IF(PLAYER2,CAMPAIGN_FLAG[(N-1)/3] == 0)
	SET_FLAG(PLAYER2,CAMPAIGN_FLAG[(N-1)/3],2)
ENDIF
IF(PLAYER2,CAMPAIGN_FLAG[(N-1)/3] == 1)
	SET_FLAG(PLAYER2,CAMPAIGN_FLAG[(N-1)/3],3)
ENDIF
IF(PLAYER2,CAMPAIGN_FLAG[(N-1)/3] == 4)
	SET_FLAG(PLAYER2,CAMPAIGN_FLAG[(N-1)/3],6)
ENDIF
IF(PLAYER2,CAMPAIGN_FLAG[(N-1)/3] == 5)
	SET_FLAG(PLAYER2,CAMPAIGN_FLAG[(N-1)/3],7)
ENDIF

REM	----- K = 2 (3,6,9,12,15,18) -----

IF(PLAYER2,CAMPAIGN_FLAG[(N-1)/3] == 0)
	SET_FLAG(PLAYER2,CAMPAIGN_FLAG[(N-1)/3],4)
ENDIF
IF(PLAYER2,CAMPAIGN_FLAG[(N-1)/3] == 1)
	SET_FLAG(PLAYER2,CAMPAIGN_FLAG[(N-1)/3],5)
ENDIF
IF(PLAYER2,CAMPAIGN_FLAG[(N-1)/3] == 2)
	SET_FLAG(PLAYER2,CAMPAIGN_FLAG[(N-1)/3],6)
ENDIF
IF(PLAYER2,CAMPAIGN_FLAG[(N-1)/3] == 3)
	SET_FLAG(PLAYER2,CAMPAIGN_FLAG[(N-1)/3],7)
ENDIF

REM --------------------------------------------------------------

REM -- Simple

rem - prevent prison convert snow-balling -
IF( PLAYER0, SKELETON > 2 )
    SET_GAME_RULE( PrisonSkeletonChance, 80 )
ENDIF
IF( PLAYER0,SKELETON > 4 )
    SET_GAME_RULE( PrisonSkeletonChance, 60 )
ENDIF
IF( PLAYER0,SKELETON > 6 )
    SET_GAME_RULE( PrisonSkeletonChance, 40 )
ENDIF
IF( PLAYER0,SKELETON > 8 )
    SET_GAME_RULE( PrisonSkeletonChance, 20 )
ENDIF

REM -- More complex.

IF(PLAYER0,SKELETON <= 8)
    NEXT_COMMAND_REUSABLE
    SET_GAME_RULE(PrisonSkeletonChance, 40)
	IF(PLAYER0,SKELETON <= 6)
    	NEXT_COMMAND_REUSABLE
    	SET_GAME_RULE(PrisonSkeletonChance, 60)
		IF(PLAYER0,SKELETON <= 4)
			NEXT_COMMAND_REUSABLE
    		SET_GAME_RULE(PrisonSkeletonChance, 80)
			IF(PLAYER0, SKELETON <= 2)
    			NEXT_COMMAND_REUSABLE
				SET_GAME_RULE(PrisonSkeletonChance, 100)
			ENDIF
		ENDIF
	ENDIF
ENDIF
IF(PLAYER0,SKELETON > 8)
    NEXT_COMMAND_REUSABLE
    SET_GAME_RULE(PrisonSkeletonChance, 20)
ENDIF

REM ----------

rem - prevent torture convert snow-balling -
IF( PLAYER0, CREATURES_CONVERTED > 2 )
    SET_GAME_RULE( TortureDeathChance, 33 )
ENDIF
IF( PLAYER0, CREATURES_CONVERTED > 5 )
    SET_GAME_RULE( TortureDeathChance, 50 )
ENDIF
IF( PLAYER0,CREATURES_CONVERTED > 8 )
    SET_GAME_RULE( TortureDeathChance, 80 )
ENDIF

REM ----------

IF( PLAYER0,GHOST > 3 )
    SET_GAME_RULE( GhostConvertChance, 70 )
ENDIF
IF( PLAYER0,GHOST > 6 )
    SET_GAME_RULE( GhostConvertChance, 40 )
ENDIF
IF( PLAYER0,GHOST > 9 )
    SET_GAME_RULE( GhostConvertChance, 10 )
ENDIF

REM ----------

IF( PLAYER0,VAMPIRE > 2 )
    SET_GAME_RULE( BodiesForVampire, 15)
ENDIF
IF( PLAYER0,VAMPIRE > 4 )
    SET_GAME_RULE( BodiesForVampire, 20)
ENDIF

REM ----------

REM			TODO: Add one for temple recipes as well.

REM --------------------------------------------------------------

REM Level 6 onwards. Need to EXPORT_FLAG add the number of 
REM 	Archers (Arrow), Wizards (Hail), and any other creatures (Giants for Landmines? Samurai?)
REM The player owns or hero creatures he has in prison to the total at game won. If at any time the current amount owned (i.e. converted) + this total is higher than 15, the trap is unlocked permanently and this is marked with a campaign flag.

IF_AVAILABLE(PLAYER0,ARROW >= 1)
	IF(PLAYER0,ARCHER > 0)
		TRAP_AVAILABLE(PLAYER0,ARROW,1,0)
	ENDIF
ENDIF

IF_AVAILABLE(PLAYER0,FREEZE >= 1)
	IF(PLAYER0,WIZARD > 0)
		TRAP_AVAILABLE(PLAYER0, FREEZE,1,0)
	ENDIF
ENDIF


REM --------------------------------------------------------------

REM Unlocking creatures:
REM If Mistresses found in level 6, unlock Mistresses "early" (otherwise not available until level 8, and should have different QUICK_INFORMATION depending on which)
REM If Orc freed in level 7, unlock Orcs "early" (otherwise not available until level 9, and should have different QUICK_INFORMATION depending on which)
REM If Druids found in level 9, add Druid to pool (I think I already improved them)
REM If Hound objective complete in level 10, change Hound creature attraction, improve stats and spells etc, make available earlier (might just have to be a SET_CREATURE_PROPERTIES sort of thing each level, or maybe I just make a second creature and choose which to include?)
REM If Tentacle objective complete in level 11, change Tentacle creature attraction, improve stats and spells etc, make available earlier.

IF(PLAYER0,ORC > 0)
	QUICK_INFORMATION(5,"The freed warlord has shared his designs for the Barracks. His underlings have also joined as backup.")
	CREATURE_AVAILABLE(ALL_PLAYERS,ORC,1,0)
	ADD_CREATURE_TO_LEVEL(PLAYER0,ORC,4,2,1,0)
	ROOM_AVAILABLE(PLAYER0,BARRACKS,1,0)
	SET_FLAG(PLAYER1,CAMPAIGN_FLAG2,1)
ENDIF




IF(PLAYER0,GOOD_CREATURES_CONVERTED >= 10)
	QUICK_INFORMATION(12,"The remaining heroes of this realm have made a pact to die instead of joining you. Their loss, I suppose.")
	SET_CREATURE_PROPERTY(ARCHER,NO_IMPRISONMENT,1)
	SET_CREATURE_PROPERTY(THIEF,NO_IMPRISONMENT,1)
	SET_CREATURE_PROPERTY(BARBARIAN,NO_IMPRISONMENT,1)
	SET_CREATURE_PROPERTY(FAIRY,NO_IMPRISONMENT,1)
	SET_CREATURE_PROPERTY(GIANT,NO_IMPRISONMENT,1)
	SET_CREATURE_PROPERTY(KNIGHT,NO_IMPRISONMENT,1)
	SET_CREATURE_PROPERTY(MONK,NO_IMPRISONMENT,1)
	SET_CREATURE_PROPERTY(SAMURAI,NO_IMPRISONMENT,1)
	SET_CREATURE_PROPERTY(THIEF,NO_IMPRISONMENT,1)
	SET_CREATURE_PROPERTY(WITCH,NO_IMPRISONMENT,1)
	SET_CREATURE_PROPERTY(TUNNELLER,NO_IMPRISONMENT,1)
ENDIF


REM If objective failed - let the player know.
	SET_OBJECT_CONFIGURATION(SPECBOX_CUSTOM,AnimationID,INERTSPECIAL)
	SET_OBJECT_CONFIGURATION(SPECBOX_CUSTOM,AnimationSpeed,0)
	SET_OBJECT_CONFIGURATION(SPECBOX_CUSTOM,LightRadius,0)
	SET_OBJECT_CONFIGURATION(SPECBOX_CUSTOM,LightIntensity,0)



REM every once in a while...
COMPUTE_FLAG(PLAYER3,FLAG0,SET,PLAYER0,CAMPAIGN_FLAG5)
COMPUTE_FLAG(PLAYER3,FLAG0,INCREASE,PLAYER0,ARCHER)
REM if the amount is over the max, unlock it permanently, even if it's just
IF(PLAYER0,CAMPAIGN_FLAG5 >= 10)
	REM unlock arrow trap
ENDIF



Prison and Torture
rem - prevent prison convert snow-balling -
IF( PLAYER0, SKELETON > 2 )
    SET_GAME_RULE( PrisonSkeletonChance, 80 )
ENDIF
IF( PLAYER0,SKELETON > 4 )
    SET_GAME_RULE( PrisonSkeletonChance, 60 )
ENDIF
IF( PLAYER0,SKELETON > 6 )
    SET_GAME_RULE( PrisonSkeletonChance, 40 )
ENDIF
IF( PLAYER0,SKELETON > 8 )
    SET_GAME_RULE( PrisonSkeletonChance, 20 )
ENDIF



IF(PLAYER0,SKELETON <= 8)
    NEXT_COMMAND_REUSABLE
    SET_GAME_RULE(PrisonSkeletonChance, 40)
    IF(PLAYER0,SKELETON <= 6)
        NEXT_COMMAND_REUSABLE
        SET_GAME_RULE(PrisonSkeletonChance, 60)
        IF(PLAYER0,SKELETON <= 4)
            NEXT_COMMAND_REUSABLE
            SET_GAME_RULE(PrisonSkeletonChance, 80)
            IF(PLAYER0, SKELETON <= 2)
                NEXT_COMMAND_REUSABLE
                SET_GAME_RULE(PrisonSkeletonChance, 100)
            ENDIF
        ENDIF
    ENDIF
ENDIF
IF(PLAYER0,SKELETON > 8)
    NEXT_COMMAND_REUSABLE
    SET_GAME_RULE(PrisonSkeletonChance, 20)
ENDIF




rem - prevent torture convert snow-balling -
IF( PLAYER0, CREATURES_CONVERTED > 2 )
    SET_GAME_RULE( TortureDeathChance, 33 )
ENDIF
IF( PLAYER0, CREATURES_CONVERTED > 5 )
    SET_GAME_RULE( TortureDeathChance, 50 )
ENDIF
IF( PLAYER0,CREATURES_CONVERTED > 8 )
    SET_GAME_RULE( TortureDeathChance, 80 )
ENDIF

IF( PLAYER0,GHOST > 3 )
    SET_GAME_RULE( GhostConvertChance, 30 )
ENDIF

[add the ones for ghosts, temple recipes etc]

IF_AVAILABLE(PLAYER0,ARROW >= 1)
	IF(PLAYER0,ARCHER > 0)
		TRAP_AVAILABLE(PLAYER0,ARROW,1,0)
	ENDIF
ENDIF

IF_AVAILABLE(PLAYER0,HAIL >= 1)
	IF(PLAYER0,WIZARD > 0)
		TRAP_AVAILABLE(PLAYER0,HAIL,1,0)
	ENDIF
ENDIF
