REM ****************************************************************************
REM  Campaigns Consolidation Project for KeeperFX strategy game.
REM ****************************************************************************
REM  Script for Level Blaise End
REM  Campaign: DK Original
REM  Authors:  based on Bullfrog script
REM            KeeperFX CCP Team
REM  Date:     17 Dec 1996 - 14 Mar 2018
REM  Copying and copyrights:
REM    This program is free software; you can redistribute it and/or modify
REM    it under the terms of the GNU General Public License as published by
REM    the Free Software Foundation; either version 2 of the License, or
REM    (at your option) any later version.
REM ****************************************************************************
LEVEL_VERSION(1)

REM Nothing new for the player

REM Flags used:
REM   PLAYER0,FLAG2 - State of party TWO continuous spawning; 0-before; 2-spawning; 3-after.
REM   PLAYER0,FLAG4 - Disables attack of the gold mining events chain; 0-attack allowed; 1-final attack disabled.
REM Timers used:
REM   PLAYER0,TIMER2 - Continuous spawning of party TWO after AP1 is activated
REM   PLAYER0,TIMER4 - Allows delayed spawning of small party when AP3 is activated
REM   PLAYER_GOOD,TIMER0 - Starts when Lord of the Land arrives; if he's not killed soon, brings reinforcements
REM   PLAYER_GOOD,TIMER1 - Starts when enough gold is dug out; triggers minor parties in the gold digging events chain
REM   PLAYER_GOOD,TIMER2 - Starts when a lot of gold is dug out; triggers further parties in the gold digging events chain
REM   PLAYER_GOOD,TIMER3 - Starts when heroes triggered by TIMER2 are dead; triggers final attack in the gold digging events chain

SET_GENERATE_SPEED(800)

START_MONEY(PLAYER0,10000)

MAX_CREATURES(PLAYER0,60)

ADD_CREATURE_TO_POOL(TROLL,5)
ADD_CREATURE_TO_POOL(SORCEROR,3)
ADD_CREATURE_TO_POOL(BILE_DEMON,7)
ADD_CREATURE_TO_POOL(DARK_MISTRESS,15)
ADD_CREATURE_TO_POOL(TENTACLE,15)
ADD_CREATURE_TO_POOL(HELL_HOUND,10)
ADD_CREATURE_TO_POOL(DRAGON,20)
ADD_CREATURE_TO_POOL(VAMPIRE,3)
ADD_CREATURE_TO_POOL(ORC,20)

REM Creature availability
CREATURE_AVAILABLE(ALL_PLAYERS,TROLL,1,0)
CREATURE_AVAILABLE(ALL_PLAYERS,SORCEROR,1,0)
CREATURE_AVAILABLE(ALL_PLAYERS,BILE_DEMON,1,0)
CREATURE_AVAILABLE(ALL_PLAYERS,DARK_MISTRESS,1,0)
CREATURE_AVAILABLE(ALL_PLAYERS,TENTACLE,1,0)
CREATURE_AVAILABLE(ALL_PLAYERS,HELL_HOUND,1,0)
CREATURE_AVAILABLE(ALL_PLAYERS,DRAGON,1,0)
CREATURE_AVAILABLE(ALL_PLAYERS,VAMPIRE,1,0)
CREATURE_AVAILABLE(ALL_PLAYERS,ORC,1,0)

REM Room availability
ROOM_AVAILABLE(ALL_PLAYERS,TREASURE,1,1)
ROOM_AVAILABLE(ALL_PLAYERS,LAIR,1,1)
ROOM_AVAILABLE(ALL_PLAYERS,GARDEN,1,1)
ROOM_AVAILABLE(ALL_PLAYERS,TRAINING,1,1)
ROOM_AVAILABLE(ALL_PLAYERS,RESEARCH,1,1)
ROOM_AVAILABLE(ALL_PLAYERS,WORKSHOP,1,0)
ROOM_AVAILABLE(ALL_PLAYERS,BARRACKS,1,0)
ROOM_AVAILABLE(ALL_PLAYERS,GUARD_POST,1,0)
ROOM_AVAILABLE(ALL_PLAYERS,BRIDGE,1,0)
ROOM_AVAILABLE(ALL_PLAYERS,PRISON,1,0)
ROOM_AVAILABLE(ALL_PLAYERS,TORTURE,1,0)
ROOM_AVAILABLE(ALL_PLAYERS,SCAVENGER,1,0)
ROOM_AVAILABLE(ALL_PLAYERS,TEMPLE,1,0)
ROOM_AVAILABLE(ALL_PLAYERS,GRAVEYARD,1,0)

REM Spells availability
MAGIC_AVAILABLE(ALL_PLAYERS,POWER_HAND,1,1)
MAGIC_AVAILABLE(ALL_PLAYERS,POWER_IMP,1,1)
MAGIC_AVAILABLE(ALL_PLAYERS,POWER_SLAP,1,1)
MAGIC_AVAILABLE(ALL_PLAYERS,POWER_SIGHT,1,0)
MAGIC_AVAILABLE(ALL_PLAYERS,POWER_CALL_TO_ARMS,1,0)
MAGIC_AVAILABLE(ALL_PLAYERS,POWER_HEAL_CREATURE,1,0)
MAGIC_AVAILABLE(ALL_PLAYERS,POWER_SPEED,1,0)
MAGIC_AVAILABLE(ALL_PLAYERS,POWER_PROTECT,1,0)
MAGIC_AVAILABLE(ALL_PLAYERS,POWER_CONCEAL,1,0)
MAGIC_AVAILABLE(ALL_PLAYERS,POWER_HOLD_AUDIENCE,1,0)
REM Don't give POWER_LIGHTNING and POWER_DESTROY_WALLS, as they can be found on the level

REM Doors and traps availability
DOOR_AVAILABLE(ALL_PLAYERS,WOOD,1,0)
TRAP_AVAILABLE(ALL_PLAYERS,ALARM,1,0)
DOOR_AVAILABLE(ALL_PLAYERS,BRACED,1,0)
TRAP_AVAILABLE(ALL_PLAYERS,POISON_GAS,1,0)
TRAP_AVAILABLE(ALL_PLAYERS,LAVA,1,0)
DOOR_AVAILABLE(ALL_PLAYERS,STEEL,1,0)
TRAP_AVAILABLE(ALL_PLAYERS,BOULDER,1,0)
DOOR_AVAILABLE(ALL_PLAYERS,MAGIC,1,0)
TRAP_AVAILABLE(ALL_PLAYERS,LIGHTNING,1,0)
TRAP_AVAILABLE(ALL_PLAYERS,WORD_OF_POWER,1,0)

CREATE_PARTY(ONE)
	ADD_TO_PARTY(ONE,THIEF,2,300,ATTACK_ENEMIES,0)
	ADD_TO_PARTY(ONE,THIEF,2,300,ATTACK_ENEMIES,0)
	ADD_TO_PARTY(ONE,THIEF,2,300,ATTACK_ENEMIES,0)
	ADD_TO_PARTY(ONE,ARCHER,4,400,ATTACK_ENEMIES,0)

CREATE_PARTY(TWO)
	ADD_TO_PARTY(TWO,ARCHER,3,400,ATTACK_ENEMIES,0)
	ADD_TO_PARTY(TWO,ARCHER,3,400,ATTACK_ENEMIES,0)
	ADD_TO_PARTY(TWO,ARCHER,3,400,ATTACK_ENEMIES,0)
	ADD_TO_PARTY(TWO,BARBARIAN,4,500,ATTACK_ENEMIES,0)

CREATE_PARTY(THREE)
	ADD_TO_PARTY(THREE,DWARFA,4,300,ATTACK_ROOMS,0)
	ADD_TO_PARTY(THREE,DWARFA,4,300,ATTACK_ROOMS,0)
	ADD_TO_PARTY(THREE,DWARFA,4,300,ATTACK_ROOMS,0)
	ADD_TO_PARTY(THREE,DWARFA,4,300,ATTACK_ROOMS,0)
	ADD_TO_PARTY(THREE,DWARFA,4,300,ATTACK_ROOMS,0)

CREATE_PARTY(FOUR)
	ADD_TO_PARTY(FOUR,WITCH,5,750,STEAL_SPELLS,0)
	ADD_TO_PARTY(FOUR,WITCH,5,750,STEAL_SPELLS,0)
	ADD_TO_PARTY(FOUR,WITCH,5,750,STEAL_SPELLS,0)
	ADD_TO_PARTY(FOUR,THIEF,5,500,STEAL_GOLD,0)

CREATE_PARTY(FIVE)
	ADD_TO_PARTY(FIVE,BARBARIAN,5,1000,DEFEND_PARTY,0)
	ADD_TO_PARTY(FIVE,FAIRY,5,1000,STEAL_SPELLS,0)
	ADD_TO_PARTY(FIVE,BARBARIAN,5,1000,DEFEND_PARTY,0)
	ADD_TO_PARTY(FIVE,FAIRY,5,1000,STEAL_SPELLS,0)
	ADD_TO_PARTY(FIVE,BARBARIAN,5,1000,DEFEND_PARTY,0)
	ADD_TO_PARTY(FIVE,FAIRY,7,1000,STEAL_SPELLS,0)

CREATE_PARTY(SIX)
	ADD_TO_PARTY(SIX,ARCHER,5,1250,ATTACK_ENEMIES,0)
	ADD_TO_PARTY(SIX,ARCHER,5,1250,ATTACK_ENEMIES,0)
	ADD_TO_PARTY(SIX,ARCHER,5,1250,ATTACK_ENEMIES,0)
	ADD_TO_PARTY(SIX,ARCHER,5,1250,ATTACK_ENEMIES,0)

CREATE_PARTY(SEVEN)
	ADD_TO_PARTY(SEVEN,BARBARIAN,7,2000,ATTACK_ENEMIES,0)
	ADD_TO_PARTY(SEVEN,BARBARIAN,7,2000,ATTACK_ENEMIES,0)
	ADD_TO_PARTY(SEVEN,BARBARIAN,7,2000,ATTACK_ENEMIES,0)

CREATE_PARTY(TROOPA)
	ADD_TO_PARTY(TROOPA,ARCHER,3,200,ATTACK_ENEMIES,0)
	ADD_TO_PARTY(TROOPA,ARCHER,3,200,ATTACK_ENEMIES,0)
	ADD_TO_PARTY(TROOPA,ARCHER,3,200,ATTACK_ENEMIES,0)
	ADD_TO_PARTY(TROOPA,ARCHER,3,200,ATTACK_ENEMIES,0)
	ADD_TO_PARTY(TROOPA,ARCHER,3,200,ATTACK_ENEMIES,0)

CREATE_PARTY(TROOPB)
	ADD_TO_PARTY(TROOPB,THIEF,4,300,ATTACK_ENEMIES,0)
	ADD_TO_PARTY(TROOPB,THIEF,4,300,ATTACK_ENEMIES,0)
	ADD_TO_PARTY(TROOPB,THIEF,4,300,ATTACK_ENEMIES,0)
	ADD_TO_PARTY(TROOPB,THIEF,4,300,ATTACK_ENEMIES,0)
	ADD_TO_PARTY(TROOPB,ARCHER,4,300,ATTACK_ENEMIES,0)
	ADD_TO_PARTY(TROOPB,ARCHER,4,300,ATTACK_ENEMIES,0)

CREATE_PARTY(CASTERS)
	ADD_TO_PARTY(CASTERS,WIZARD,4,500,ATTACK_ENEMIES,0)
	ADD_TO_PARTY(CASTERS,WIZARD,4,500,ATTACK_ENEMIES,0)
	ADD_TO_PARTY(CASTERS,WIZARD,4,500,ATTACK_ENEMIES,0)
	ADD_TO_PARTY(CASTERS,WITCH,4,500,ATTACK_ENEMIES,0)
	ADD_TO_PARTY(CASTERS,WITCH,4,500,ATTACK_ENEMIES,0)
	ADD_TO_PARTY(CASTERS,WITCH,4,500,ATTACK_ENEMIES,0)

CREATE_PARTY(BARBIES)
	ADD_TO_PARTY(BARBIES,BARBARIAN,5,750,ATTACK_ENEMIES,0)
	ADD_TO_PARTY(BARBIES,BARBARIAN,5,750,ATTACK_ENEMIES,0)
	ADD_TO_PARTY(BARBIES,BARBARIAN,5,750,ATTACK_ENEMIES,0)
	ADD_TO_PARTY(BARBIES,BARBARIAN,5,750,ATTACK_ENEMIES,0)
	ADD_TO_PARTY(BARBIES,BARBARIAN,5,750,ATTACK_ENEMIES,0)

CREATE_PARTY(GIANTS)
	ADD_TO_PARTY(GIANTS,GIANT,8,1750,ATTACK_ENEMIES,0)
	ADD_TO_PARTY(GIANTS,GIANT,8,1750,ATTACK_ENEMIES,0)
	ADD_TO_PARTY(GIANTS,GIANT,8,1750,ATTACK_ENEMIES,0)
	ADD_TO_PARTY(GIANTS,GIANT,8,1750,ATTACK_ENEMIES,0)
	ADD_TO_PARTY(GIANTS,GIANT,8,1750,ATTACK_ENEMIES,0)

CREATE_PARTY(FAIRIES)
	ADD_TO_PARTY(FAIRIES,FAIRY,8,1750,ATTACK_ENEMIES,0)
	ADD_TO_PARTY(FAIRIES,FAIRY,8,1750,ATTACK_ENEMIES,0)
	ADD_TO_PARTY(FAIRIES,FAIRY,8,1750,ATTACK_ENEMIES,0)
	ADD_TO_PARTY(FAIRIES,FAIRY,8,1750,ATTACK_ENEMIES,0)
	ADD_TO_PARTY(FAIRIES,FAIRY,8,1750,ATTACK_ENEMIES,0)
	ADD_TO_PARTY(FAIRIES,FAIRY,8,1750,ATTACK_ENEMIES,0)

CREATE_PARTY(LORDGUARD)
	ADD_TO_PARTY(LORDGUARD,BARBARIAN,5,1000,DEFEND_PARTY,0)
	ADD_TO_PARTY(LORDGUARD,GIANT,7,1500,DEFEND_PARTY,0)
	ADD_TO_PARTY(LORDGUARD,BARBARIAN,5,1000,DEFEND_PARTY,0)
	ADD_TO_PARTY(LORDGUARD,GIANT,7,1500,DEFEND_PARTY,0)
	ADD_TO_PARTY(LORDGUARD,FAIRY,7,1500,DEFEND_PARTY,0)
	ADD_TO_PARTY(LORDGUARD,FAIRY,7,1500,DEFEND_PARTY,0)

CREATE_PARTY(LANDLORD)
	ADD_TO_PARTY(LANDLORD,FAIRY,9,1200,DEFEND_PARTY,0)
	ADD_TO_PARTY(LANDLORD,FAIRY,9,1200,DEFEND_PARTY,0)
	ADD_TO_PARTY(LANDLORD,FAIRY,9,1200,DEFEND_PARTY,0)
	ADD_TO_PARTY(LANDLORD,FAIRY,9,1200,DEFEND_PARTY,0)
	ADD_TO_PARTY(LANDLORD,FAIRY,6,1200,DEFEND_PARTY,0)
	ADD_TO_PARTY(LANDLORD,FAIRY,6,1200,DEFEND_PARTY,0)
	ADD_TO_PARTY(LANDLORD,KNIGHT,7,1200,DEFEND_PARTY,0)

REM ****************************************************************************

REM "The guardians of this realm are asleep at the moment. Attack them as soon as you can or they'll be breakfasted, wide awake and inherently more difficult to slaughter."
DISPLAY_OBJECTIVE(127,PLAYER0)

REM Let's make a chain of events related to gold digging
IF(PLAYER0,TOTAL_GOLD_MINED >= 12000)
	REM "If you should encounter hero patrols, ensure that none of their number escape. Otherwise your presence will be revealed and then you will be in trouble."
	DISPLAY_INFORMATION(128,ALL_PLAYERS)
ENDIF

IF(PLAYER0,TOTAL_GOLD_MINED >= 15000)
	ADD_TUNNELLER_PARTY_TO_LEVEL(PLAYER_GOOD,ONE,-1,DUNGEON_HEART,0,2,250)
ENDIF

IF(PLAYER0,TOTAL_GOLD_MINED >= 25000)
	ADD_TUNNELLER_PARTY_TO_LEVEL(PLAYER_GOOD,TWO,-2,DUNGEON_HEART,0,3,250)
	SET_TIMER(PLAYER_GOOD,TIMER1)
ENDIF

IF(PLAYER_GOOD,TIMER1 >= 5000)
	ADD_TUNNELLER_PARTY_TO_LEVEL(PLAYER_GOOD,ONE,-2,DUNGEON_HEART,0,4,300)
ENDIF

REM If mined so much gold, the dungeon is quite advanced. Start timer for a large attack.
IF(PLAYER0,TOTAL_GOLD_MINED >= 35000)
	ADD_TUNNELLER_PARTY_TO_LEVEL(PLAYER_GOOD,TWO,-2,DUNGEON_HEART,0,5,250)
	SET_TIMER(PLAYER_GOOD,TIMER2)
ENDIF

REM Warn the player that something big is coming
IF(PLAYER_GOOD,TIMER2 >= 1500)
	ADD_TUNNELLER_PARTY_TO_LEVEL(PLAYER_GOOD,THREE,-2,DUNGEON_HEART,0,6,250)
	IF_CONTROLS(PLAYER_GOOD,DWARFA <= 0)
		REM "You're taking forever over this. Attack and destroy the heroes' stronghold soon, preferably before hell freezes over."
		DISPLAY_INFORMATION(129,PLAYER_GOOD)
		SET_TIMER(PLAYER_GOOD,TIMER3)
	ENDIF
ENDIF

REM Final attack of the gold mining events chain
IF(PLAYER_GOOD,TIMER3 >= 5000)
	IF(PLAYER0,FLAG4 == 0)
		ADD_TUNNELLER_PARTY_TO_LEVEL(PLAYER_GOOD,TROOPA,-1,DUNGEON_HEART,0,5,300)
		ADD_TUNNELLER_PARTY_TO_LEVEL(PLAYER_GOOD,TROOPB,-2,DUNGEON_HEART,0,5,300)
		ADD_TUNNELLER_PARTY_TO_LEVEL(PLAYER_GOOD,CASTERS,-1,DUNGEON_HEART,0,5,300)
		ADD_TUNNELLER_PARTY_TO_LEVEL(PLAYER_GOOD,BARBIES,-2,DUNGEON_HEART,0,5,300)
		ADD_TUNNELLER_PARTY_TO_LEVEL(PLAYER_GOOD,GIANTS,-1,DUNGEON_HEART,0,5,300)
		ADD_TUNNELLER_PARTY_TO_LEVEL(PLAYER_GOOD,FAIRIES,-2,DUNGEON_HEART,0,5,300)
	ENDIF
ENDIF

REM Less important action points, away from enemy dungeon

IF_ACTION_POINT(6,PLAYER0)
	ADD_CREATURE_TO_LEVEL(PLAYER_GOOD,ARCHER,6,6,6,500)
ENDIF

IF_ACTION_POINT(7,PLAYER0)
	ADD_CREATURE_TO_LEVEL(PLAYER_GOOD,THIEF,7,6,8,500)
ENDIF

IF_ACTION_POINT(8,PLAYER0)
	ADD_CREATURE_TO_LEVEL(PLAYER_GOOD,GIANT,8,6,8,500)
ENDIF

REM Action point away from enemy dungeon, but important, central one
IF_ACTION_POINT(2,PLAYER0)
	ADD_PARTY_TO_LEVEL(PLAYER_GOOD,SIX,-3,1)
	ADD_PARTY_TO_LEVEL(PLAYER_GOOD,SEVEN,-4,1)
	ADD_CREATURE_TO_LEVEL(PLAYER_GOOD,ARCHER,2,4,4,500)
	ADD_CREATURE_TO_LEVEL(PLAYER_GOOD,ARCHER,2,4,6,500)
ENDIF

REM Action point at entrance to hero fortress
IF_ACTION_POINT(1,PLAYER0)
	REM "The heroes of this realm have prepared for your coming by building an underground stronghold of their own."
	DISPLAY_OBJECTIVE(131,PLAYER0)
	REM Disable the gold mining events chain attack
	SET_FLAG(PLAYER0,FLAG4,1)
	REM Trigger the Lord of the Land and allow to win the game
	ADD_PARTY_TO_LEVEL(PLAYER_GOOD,LORDGUARD,5,1)
	ADD_PARTY_TO_LEVEL(PLAYER_GOOD,LANDLORD,PLAYER_GOOD,1)
	ADD_CREATURE_TO_LEVEL(PLAYER_GOOD,ARCHER,1,6,6,500)
	SET_TIMER(PLAYER_GOOD,TIMER0)
	REM Start continuous spawning of party TWO
	SET_TIMER(PLAYER0,TIMER2)
	SET_FLAG(PLAYER0,FLAG2,2)
ENDIF

REM Continuous spawning of party TWO
IF(PLAYER0,FLAG2 == 2)
	IF(PLAYER0,TIMER2 >= 2500)
		REM Spawn archers only to some limit
	    IF(PLAYER_GOOD,ARCHER < 32)
			NEXT_COMMAND_REUSABLE
			ADD_PARTY_TO_LEVEL(PLAYER_GOOD,TWO,PLAYER_GOOD,1)
		ENDIF
		NEXT_COMMAND_REUSABLE
		SET_TIMER(PLAYER0,TIMER2)
	ENDIF
ENDIF

REM Allow the player to take advantage of killing Lord of the Land quickly
IF(PLAYER_GOOD,TIMER0 >= 200)
	IF_CONTROLS(PLAYER_GOOD,KNIGHT <= 0)
		IF(PLAYER_GOOD,DUNGEON_DESTROYED == 1)
			REM Stop continuous spawning of party TWO (it will stop anyway with WIN_GAME)
			SET_FLAG(PLAYER0,FLAG2,3)
			REM "Another realm falls under your evil reign. Another cloud of misery and despair gathers. Lovely."
			DISPLAY_OBJECTIVE(162,PLAYER0)
			WIN_GAME
		ENDIF
	ENDIF
ENDIF

REM If the player is too slow in killing Lord of the Land, bring some reinforcements
REM This isn't that unusual - if the player is torturing the lord, we will most likely get here
IF(PLAYER_GOOD,TIMER0 >= 10000)
	REM "Enemy reinforcements have arrived. So what? Let's hear it for more killing."
	DISPLAY_INFORMATION(130,2)
	ADD_TUNNELLER_PARTY_TO_LEVEL(PLAYER_GOOD,ONE,-1,DUNGEON_HEART,0,2,250)
	ADD_TUNNELLER_PARTY_TO_LEVEL(PLAYER_GOOD,TWO,-1,DUNGEON_HEART,0,2,250)
	ADD_PARTY_TO_LEVEL(PLAYER_GOOD,THREE,PLAYER_GOOD,1)
	ADD_PARTY_TO_LEVEL(PLAYER_GOOD,FOUR,PLAYER_GOOD,1)
	ADD_TUNNELLER_PARTY_TO_LEVEL(PLAYER_GOOD,FIVE,-2,DUNGEON_HEART,0,2,250)
ENDIF

REM Action points triggered if the player went left instead of through central entrance
REM (or rammed through entrance and moved to the left, but that's unlikely).
REM Make this route easier, but still challenging.

IF_ACTION_POINT(3,PLAYER0)
	SET_TIMER(PLAYER0,TIMER4)
ENDIF

IF(PLAYER0,TIMER4 >= 200)
	ADD_PARTY_TO_LEVEL(PLAYER_GOOD,SEVEN,4,1)
ENDIF

IF_ACTION_POINT(5,PLAYER0)
	ADD_CREATURE_TO_LEVEL(PLAYER_GOOD,WIZARD,5,7,8,500)
ENDIF

IF_ACTION_POINT(11,PLAYER0)
	ADD_CREATURE_TO_LEVEL(PLAYER_GOOD,WIZARD,11,7,8,500)
ENDIF

REM Action points triggered if the player went right where there's no way in to hero fortress.
REM The player most likely does tourism; he can't enter the fortress from that side because
REM the DESTROY_WALLS is inside the fortress and he can't research it. So don't spawn too much.

IF_ACTION_POINT(9,PLAYER0)
	ADD_CREATURE_TO_LEVEL(PLAYER_GOOD,BARBARIAN,9,8,7,500)
ENDIF

IF_ACTION_POINT(10,PLAYER0)
	ADD_CREATURE_TO_LEVEL(PLAYER_GOOD,ARCHER,10,4,4,500)
	ADD_CREATURE_TO_LEVEL(PLAYER_GOOD,ARCHER,10,4,6,500)
ENDIF

REM ****************************************************************************
