# Level 5: A Subterranean Struggle

[scenario]
#textdomain wesnoth-utbs

id="5_Struggle"
name= _ "A Subterranean Struggle"
label= _ "A Subterranean Struggle"

map_data="{campaigns/Under_the_Burning_Suns/maps/5StruggleNew5.map}"

[music]
name=underground.ogg
[/music]

#don't display snapshot of map in saved games
snapshot="no"

#max turns in scenario varies depending on difficulty
#ifdef EASY
turns="45"
#endif
#ifdef MEDIUM
turns="43"
#endif
#ifdef HARD
turns="41"
#endif

victory_when_enemies_defeated=no

{UNDERGROUND}

[side]
	side=1
	description=Kaleh
	type=Desert Fighter
	canrecruit=1
	{INCOME 7 5 3}
	controller=human
	shroud=yes
	fog=no
[/side]

#Side=2Ętroll 1 (blue)
[side]
	no_leader=yes
	side=2
	gold=0
	income=0
	controller=ai
	shroud=yes
	fog=no
	team_name=troll

	#ifdef EASY
	recruit=Troll Whelp, Troll, Troll Rocklobber
	#endif

	#ifdef MEDIUM
	recruit=Troll Whelp, Troll, Troll Rocklobber
	#endif

	#ifdef HARD
	recruit=Troll Whelp, Troll, Troll Rocklobber
	#endif

	[ai]
		#ifdef EASY
		recruitment_pattern=fighter,archer,fighter,mixed fighter
		#endif

		#ifdef MEDIUM
		recruitment_pattern=fighter,archer,fighter,mixed fighter
		#endif

		#ifdef HARD
		recruitment_pattern=fighter,archer,fighter,mixed fighter
		#endif

		aggression=0.8
		caution=0.05
		grouping=offensive

		# AI will attack a weak unit with a max of 3,4,5 units
		# depending on the difficulty (default=5)
		{ATTACK_DEPTH 3 4 5}

		[target]
		side=1
		value=10
		[/target]

		[target]
		side=4
		value=9
		[/target]

		[target]
		side=5
		value=9
		[/target]

		#[protect_location]
		#x,y=35,37
		#radius=7
		#target=20
		#[/protect_location]
	[/ai]
[/side]


#Side=3 troll 2 (green)
[side]
	side=3
	no_leader=yes
	gold=0
	income=0
	controller=ai
	shroud=no
	fog=no
	team_name=troll

	#ifdef EASY
	recruit=Troll Whelp, Troll, Troll Rocklobber
	#endif

	#ifdef MEDIUM
	recruit=Troll Whelp, Troll, Troll Rocklobber
	#endif

	#ifdef HARD
	recruit=Troll Whelp, Troll, Troll Rocklobber
	#endif

	[ai]
		#ifdef EASY
		recruitment_pattern=fighter,archer,fighter,mixed fighter
		#endif

		#ifdef MEDIUM
		recruitment_pattern=fighter,archer,fighter,mixed fighter
		#endif

		#ifdef HARD
		recruitment_pattern=fighter,archer,fighter,mixed fighter
		#endif


		# AI will attack a weak unit with a max of 3,4,5 units
		# depending on the difficulty (default=5)
		{ATTACK_DEPTH 3 4 5}

		aggression=0.8
		caution=0.05
		grouping=offensive

		[target]
		side=1
		value=10
		[/target]

		[target]
		side=4
		value=9
		[/target]

		[target]
		side=5
		value=9
		[/target]

		#[protect_location]
		#x,y=35,37
		#radius=7
		#target=20
		#[/protect_location]

	[/ai]
[/side]

#Side=4 dwarf 1 (yellow)
[side]
	side=4
	no_leader=yes
	gold=0
	income=0
	controller=ai
	shroud=no
	fog=no
	team_name=dwarf

	#ifdef EASY
	recruit=Dwarvish Fighter, Dwarvish Steelclad, Dwarvish Thunderer, Dwarvish Guardsman
	#endif

	#ifdef MEDIUM
	recruit=Dwarvish Fighter, Dwarvish Steelclad, Dwarvish Thunderer, Dwarvish Guardsman
	#endif

	#ifdef HARD
	recruit=Dwarvish Fighter, Dwarvish Steelclad, Dwarvish Thunderer, Dwarvish Guardsman
	#endif

	[ai]
		#ifdef EASY
		#recruitment_pattern=scout,fighter,archer,fighter
		#endif

		#ifdef MEDIUM
		#recruitment_pattern=scout,fighter,archer,fighter
		#endif

		#ifdef HARD
		#recruitment_pattern=scout,fighter,archer,fighter
		#endif

		# AI will attack a weak unit with a max of 3,4,5 units
		# depending on the difficulty (default=5)
		{ATTACK_DEPTH 3 4 5}

		aggression=0.8
		caution=0.05
		grouping=offensive

		[target]
		side=1
		value=10
		[/target]

		[target]
		side=2
		value=9
		[/target]

		[target]
		side=3
		value=9
		[/target]

		#[protect_location]
		#x,y=35,37
		#radius=7
		#target=20
		#[/protect_location]
	[/ai]
[/side]


#Side=5 dwarf 2 (purple)
[side]
	side=5
	no_leader=yes
	gold=0
	income=0
	controller=ai
	shroud=no
	fog=no
	team_name=dwarf

	#ifdef EASY
	recruit=Dwarvish Fighter, Dwarvish Steelclad, Dwarvish Thunderer, Dwarvish Guardsman
	#endif

	#ifdef MEDIUM
	recruit=Dwarvish Fighter, Dwarvish Steelclad, Dwarvish Thunderer, Dwarvish Guardsman
	#endif

	#ifdef HARD
	recruit=Dwarvish Fighter, Dwarvish Steelclad, Dwarvish Thunderer, Dwarvish Guardsman
	#endif

	[ai]
		#ifdef EASY
		#recruitment_pattern=scout,fighter,archer,fighter
		#endif

		#ifdef MEDIUM
		#recruitment_pattern=scout,fighter,archer,fighter
		#endif

		#ifdef HARD
		#recruitment_pattern=scout,fighter,archer,fighter
		#endif

		# AI will attack a weak unit with a max of 3,4,5 units
		# depending on the difficulty (default=5)
		{ATTACK_DEPTH 3 4 5}

		aggression=0.8
		caution=0.05
		grouping=offensive

		[target]
		side=1
		value=10
		[/target]

		[target]
		side=2
		value=9
		[/target]

		[target]
		side=3
		value=9
		[/target]

		#[protect_location]
		#x,y=35,37
		#radius=7
		#target=20
		#[/protect_location]
	[/ai]
[/side]

#Side=6 (not used)
[side]
	side=8
	no_leader=yes
	gold=0
	income=0
	controller=ai
	shroud=no
	fog=no
[/side]

#Side=7 (not used)
[side]
	side=8
	no_leader=yes
	gold=0
	income=0
	controller=ai
	shroud=no
	fog=no
[/side]

#Side=8 Ants
[side]
	side=8
	no_leader=yes
	gold=0
	income=0
	controller=ai
	shroud=no
	fog=no

	[ai]
		aggression=0.8
		caution=0.1

		village_value=0

		# AI will attack a weak unit with a max of 3,4,5 units
		# depending on the difficulty (default=5)
		{ATTACK_DEPTH 3 4 5}

		recruitment_pattern=fighter,fighter,fighter,fighter
		recruitment_ignore_bad_combat=yes

		#causes ants to target cave spider
		[target]
		type=Cave Spider
		value=150
		[/target]

		[target]
		side=1
		value=100
		[/target]

		passive_leader=yes
	[/ai]
[/side]

#Side=9 Assassin & Cave Spider
[side]
	side=9
	no_leader=yes
	gold=0
	income=0
	controller=ai
	shroud=yes
	fog=no

	[ai]
		# AI will attack a weak unit with a max of 4,5,6 units
		# depending on the difficulty (default=5)
		{ATTACK_DEPTH 3 4 5}

		aggression=0.90
		caution=0.10
	[/ai]

	#causes assassin to attack other units more than Kaleh
	[target]
	description=Kaleh
	value=20
	[/target]

[/side]

[story]
	[part]
	story= _ "Chapter 5: We plunged into the darkness, shepherding long lines of our people along the cramped passageway deeper and deeper beneath the roots of the mountains. We had brought along palm torches, and we had scavenged other torches from the orcs, so we had enough light sources, at least for the moment. Even so, the guttering torches shed little light, and shadows flickered everywhere. The walls were damp and clammy, the air seemed stale and the sound of our footsteps echoed up and down the passageway."
	[/part]

	[part]
	story= _ "This seemed about as alien an environment as we could ever be in. We were people of the open sands, and even during the long dark we could look up and orient ourselves by the stars glittering brightly in their heights. I knew logically that we could not go over those frozen mountains, and we could not go back, but I shivered at the thought of miles and miles of rock above me, and felt the weight of the mountain closing around me. The passage twisted and turned, and soon I lost all sense of direction, but since there were no side-passages, we had little choice but to keep going forward."
	[/part]

	[part]
	story= _ "Looking back, I think the bravest thing that my people ever did was to follow me into the darkness solely on the promises of Eloh and their faith in my leadership. I had no idea how long we would be trapped underground, or even which way we should go. I just hoped Eloh would guide us somehow. Nym said that people whispered that I could lead them through anything, and it was true that when we left I had hardly imagined that we would fight outlaws, undead, orcs and goblins. My previous life seemed like years ago, even though it had only been ten days since our village was demolished."
	[/part]

	[part]
	story= _ "What lurked in the darkness? Who were the unbelievers that Eloh so cryptically referred to? My heart beat loudly in my chest, everything seemed amplified down here. I had the strong suspicion that this was not a place that my people were meant to be. I strode onwards grimly; considering everything we had gone through so far, Uria be damned if I was going to be frightened now."
	[/part]
[/story]


# Prestart functions:
# set starting scenario objectives
# increase cost of recruiting units
# place item images on map
# recall main heroes
# initialize starting variables
# remove keep
# create elf units
# create AI=guardian starting units

[event]
name=prestart

#set starting scenario objectives

[objectives]
	summary= _ "Starting Objectives:"
	[objective]
		description= _ "Explore Underground"
		condition=win
	[/objective]
	[objective]
		description= _ "Defeat all Enemies"
		condition=win
	[/objective]
	[objective]
		description= _ "Death of Kaleh, Nym or Zhul"
		condition=lose
	[/objective]
[/objectives]


	#increase the cost of all units by 1
	[if]

		[variable]
		name=scen3
		numerical_equals=2
		[/variable]

		[then]

		#go from 2 to 3

		[disallow_recruit]
			type=Desert Fighter, Desert Archer, Desert Hunter, Desert Shaman, Desert Scout
			side=1
		[/disallow_recruit]

		[disallow_recruit]
			type=Desert Fighter2, Desert Archer2, Desert Hunter2, Desert Shaman2, Desert Scout2
			side=1
		[/disallow_recruit]

		[allow_recruit]
			type=Desert Fighter3, Desert Archer3, Desert Hunter3, Desert Shaman3, Desert Scout3
			side=1
		[/allow_recruit]

		[/then]

		[else]

		[if]
			[variable]
			name=scen3
			numerical_equals=1
			[/variable]

			[then]

			#go from 1 to 2

			[disallow_recruit]
				type=Desert Fighter, Desert Archer, Desert Hunter, Desert Shaman, Desert Scout
				side=1
			[/disallow_recruit]

			[allow_recruit]
				type=Desert Fighter2, Desert Archer2, Desert Hunter2, Desert Shaman2, Desert Scout2
				side=1
			[/allow_recruit]

			[/then]
		[/if]

		[if]
			[variable]
			name=scen3
			numerical_equals=3
			[/variable]

			[then]

			#go from 3 to 4

			[disallow_recruit]
				type=Desert Fighter, Desert Archer, Desert Hunter, Desert Shaman, Desert Scout
				side=1
			[/disallow_recruit]

			[disallow_recruit]
				type=Desert Fighter2, Desert Archer2, Desert Hunter2, Desert Shaman2, Desert Scout2
				side=1
			[/disallow_recruit]

			[disallow_recruit]
				type=Desert Fighter3, Desert Archer3, Desert Hunter3, Desert Shaman3, Desert Scout3
				side=1
			[/disallow_recruit]

			[allow_recruit]
				type=Desert Fighter4, Desert Archer4, Desert Hunter4, Desert Shaman4, Desert Scout4
				side=1
			[/allow_recruit]

			[/then]
		[/if]

		[/else]
	[/if]

	#fires lighting central cavern
	{PLACE_IMAGE scenery/fire1.png 31 32}
	{PLACE_IMAGE scenery/fire1.png 33 21}

	#central cavern furnishings

	{PLACE_IMAGE scenery/rock-cairn.png 42 21}
	{PLACE_IMAGE scenery/rock-cairn.png 38 20}
	{PLACE_IMAGE scenery/rock-cairn.png 30 20}

	{PLACE_IMAGE items/burial.png 27 33}
	{PLACE_IMAGE items/burial.png 35 34}
	{PLACE_IMAGE items/burial.png 43 33}

	#recall heroes
	[recall]
	description=Nym
	[/recall]
	[recall]
	description=Zhul
	[/recall]
	[recall]
	description=Elyssa
	[/recall]

	#initialize starting variables

	[set_variable]
	name=rog_died
	value=0
	[/set_variable]

	[set_variable]
	name=grog_died
	value=0
	[/set_variable]

	[set_variable]
	name=battle_turn_counter
	value=0
	[/set_variable]

	[set_variable]
	name=elvish_ally
	value=0
	[/set_variable]

	[set_variable]
	name=saw_cairn_or_totem
	value=0
	[/set_variable]

	[set_variable]
	name=dead_dwarf_leaders
	value=0
	[/set_variable]

	[set_variable]
	name=dead_troll_leaders
	value=0
	[/set_variable]

	#assassin_turn must be 2 or greater to fire

	{RANDOM 13..23}

	[set_variable]
	name=assassin_turn
	value=$random
	[/set_variable]

	[set_variable]
	name=call_assassin
	value=0
	[/set_variable]

	[set_variable]
	name=found_empty_hex
	value=0
	[/set_variable]

	[set_variable]
	name=enemy_in_hex
	value=0
	[/set_variable]

	[set_variable]
	name=ally_in_hex
	value=0
	[/set_variable]

	[set_variable]
	name=unslow_kaleh
	value=0
	[/set_variable]

	#if play kills both enemy leaders while assassin is alive, then
	#delay victory

	[set_variable]
	name=delay_victory
	value=0
	[/set_variable]

	#create starting elves

	[unit]
	type=Desert Fighter
	description=Nantheos
	user_description= _ "Nantheos"
	upkeep=full
	x=4
	y=23
	side=1
	[modifications]
		{TRAIT_QUICK}
		{TRAIT_STRONG}
	[/modifications]
	[/unit]

	[unit]
	type=Desert Archer
	description=Sylestria
	user_description= _ "Sylestria"
	upkeep=full
	x=5
	y=25
	side=1
	[modifications]
		{TRAIT_INTELLIGENT}
		{TRAIT_DEXTROUS}
	[/modifications]
	[/unit]

	# If Elyssa the Mage isn't there, boost starting group a bit

	[if]
		[have_unit]
			description=Elyssa
		[/have_unit]

		[then]
		# goody for you! ;-)
		[/then]
		[else]

		[unit]
		type=Desert Ranger
		description=Rygar
		user_description= _ "Rygar"
		x=6
		y=24
		side=1
		[modifications]
			{TRAIT_LOYAL}
			{TRAIT_DEXTROUS}
		[/modifications]
		[/unit]

		[/else]
	[/if]

[/event]

# starting events

# 1. create some villages manually, so that early finish bonus counts
# only the number of villages that a player can control

# 2. starting dialogue

[event]
name=start

	# there are two sets of villages in the tunnels around the great
	# central cave. One set will be erased when the player chooses
	# a side. To avoid duplication, I should make one set manually, so
	# only one set is added to the early finish bonus

	# tunnel villages in southern tunnels
	# village locations: (25,36) (34,37) (47,36) (37,47)

	[terrain]
	x,y=25,36
	letter=Uu^Vu
	[/terrain]

	[terrain]
	x,y=34,37
	letter=Uu^Vu
	[/terrain]

	[terrain]
	x,y=47,36
	letter=Uu^Vu
	[/terrain]

	[terrain]
	x,y=37,47
	letter=Uu^Vu
	[/terrain]

# starting dialogue

[if]
	[have_unit]
	description=Elyssa
	[/have_unit]

	[then]

	[message]
	description=Kaleh
	message= _ "You mentioned that Dwarves and Trolls often lived underground, Elyssa. I've only heard myths, have you every met a Dwarf or a Troll?"
	[/message]

	[message]
	description=Elyssa
	message= _ "No I haven't, I don't often explore underground unless I have to. There are lots of nasty things that lurk far away from the light of the suns. But I've read a bit about them, and have even met a few people who had dealings with Dwarves."

	[/message]

	[message]
	description=Nym
	message= _ "What are Dwarves like?"
	[/message]

	[message]
	description=Elyssa
	message= _ "They're a proud people, and some would say greedy. They love their gold and fine metals, and forge many beautiful things. I should warn you, they have little liking for Elves. There was some betrayal many years ago, though I don't know what happened."
	[/message]

	[message]
	description=Kaleh
	message= _ "And what about Trolls? I'm not sure I'd want to meet one face to face."
	[/message]

	[message]
	description=Elyssa
	message= _ "Trolls and Dwarves are natural enemies, living so close together. And many would say trolls are little more than brutes and savages. Trolls are huge and very strong, with skin as hard as stone, and can be fearsome foes. But I knew one man long ago who traded with a group of trolls and said they were quite honorable, as long as you didn't try to cheat them."
	[/message]

	[message]
	description=Zhul
	message= _ "Well, with Eloh's guidance, I hope we find these tunnels deserted. I'll be happy if our biggest problem is not getting lost. I have little wish to meet either Dwarves or Trolls. But Eloh will watch over us."
	[/message]

	[message]
	description=Kaleh
	message= _ "Will she? I got the impression she was powerless underground."
	[/message]

	[message]
	description=Zhul
	message= _ "Where did you get that idea? Certainly Eloh is strongest during the day, when the suns are shining down on us. But it is said that even in the darkest of nights she will protect her faithful. And back during the Golden Age holy elven warriors led great crusades against orcs and other foul things that hid underground, killing them with Eloh's aid. Faith is our shield, Kaleh. I think you should keep your doubts to yourself; it would not do to unduly worry our people. Eloh will always protect us, if we follow her path. "
	[/message]

	[message]
	description=Kaleh
	message= _ "Then let us hope the rest of our journey may be as uneventful as it has been this far."
	[/message]

	[/then]

	[else]

	[message]
	description=Kaleh
	message= _ "I've heard of dwarves, but do you have any idea, Zhul, what kinds of creatures we might encounter underground?"
	[/message]

	[message]
	description=Zhul
	message= _ "These tunnels are foreign to me; I know little more than you do. All I know about dwarves is from the few tales from the Golden Age."
	[/message]

	[message]
	description=Nym
	message= _ "What are they like?"
	[/message]

	[message]
	description=Zhul
	message= _ "They lived deep under the earth, mining gold and fine metals and forging many beautiful things. We were once allies during the Golden Age, but in the strife and chaos of the fall we lost all contact. I don't know if any survived. But in the golden age they were very helpful in our wars against the orcs and trolls."
	[/message]

	[message]
	description=Kaleh
	message= _ "What are trolls?"
	[/message]

	[message]
	description=Zhul
	message= _ "Trolls were huge green creatures as big as giants and very strong. They were reclusive creatures, hiding underground. We never had much contact with them, though some fought with the orcs in the great wars. They were mighty warriors. I'm sure they have all died off; I certainly would never want to meet one face to face."
	[/message]

	[message]
	description=Zhul
	message= _ "But with Eloh's guidance, I hope we find these tunnels deserted. I'd be happy if our biggest problem is not getting lost. Still, even underground Eloh will watch over us."
	[/message]

	[message]
	description=Kaleh
	message= _ "Will she? I got the impression she was powerless underground."
	[/message]

	[message]
	description=Zhul
	message= _ "Where did you get that idea? Certainly Eloh is strongest during the day, when the suns are shining down on us. But it is said that even in the darkest of nights she will protect her faithful. And back during the Golden Age holy elven warriors led great crusades against orcs and other foul things that hid underground, killing them with Eloh's aid. Faith is our shield, Kaleh. I think you should keep your doubts to yourself; it would not do to unduly worry our people. Eloh will always protect us, if we follow her path. "
	[/message]

	[message]
	description=Kaleh
	message= _ "Then let us hope the rest of our journey may be as uneventful as it has been this far."
	[/message]

	[/else]
[/if]

[/event]

# Event 1: Spider and Ants

[event]
name=moveto

[filter]
x=12-18
y=20-29
side=1
[/filter]

[message]
description=Nym
message= _ "All I'm saying is that these tunnels aren't as bad as I expected."
[/message]

[message]
description=Kaleh
message= _ "Ssshhhh, did you hear something?"
[/message]

[remove_shroud]
	side=1
	x=11-19
	y=24-27
[/remove_shroud]

#make ants easy: 3 medium: 4 hard: 5

{UNIT_ (Giant Ant) () ("") 8 17 26}
{UNIT_ (Giant Ant) () ("") 8 17 27}
{UNIT_ (Giant Ant) () ("") 8 18 26}

#ifdef EASY
#else
{UNIT_ (Giant Ant) () ("") 8 18 27}
#endif

#ifdef HARD
{UNIT_ (Giant Ant) () ("") 8 19 27}
#endif

#make spider
{UNIT_ (Cave Spider) () ("") 9 4 25}

[redraw]
[/redraw]

[scroll_to_unit]
	type=Giant Ant
[/scroll_to_unit]

[delay]
time=400
[/delay]

[message]
description=Zhul
message= _ "Ants. Very big ants. Maybe they won't be hostile."
[/message]

[scroll_to_unit]
	type=Cave Spider
[/scroll_to_unit]

[delay]
time=400
[/delay]

[message]
description=Kaleh
message= _ "On the other hand, that spider probably is."
[/message]

[message]
description=Nym
message= _ "Caught between a spider and its prey. Not a good place to be."
[/message]

[/event]

# If player escapes cave spider, then reward player by killing it

[event]
name=moveto

[filter]
x=18-24
y=26-29
side=1
[/filter]

[if]
	[have_unit]
	type=Cave Spider
	[/have_unit]

	[then]

	[kill]
	type=Cave Spider
	animate=yes
	[/kill]

	[message]
	description=Nym
	message= _ "Woah! Did you see that? That huge stalactite just fell and crushed the spider. Aren't we lucky!"
	[/message]

	[message]
	description=Zhul
	message= _ "Eloh must indeed be watching over us."
	[/message]

	[/then]
[/if]

[/event]

# When player approaches large cavern, a dying dwarf runs out and yells
# a warning.

[event]
name=moveto

[filter]
x=18-24
y=26-28
side=1
[/filter]

[message]
description=Nym
message= _ "You know, if all we discover down here are insects, I'll be very disappointed. "
[/message]

[if]
	[have_unit]
	description=Elyssa
	[/have_unit]

	[then]

	[message]
	description=Elyssa
	message= _ "Spiders aren't insects."
	[/message]

	[message]
	description=Nym
	message= _ "Thanks for the clarification."
	[/message]

	[/then]
[/if]

[move_unit_fake]
type=Dwarvish Fighter
x=25,24,23,22,21
y=28,28,29,28,29
[/move_unit_fake]

{UNIT_ (Dwarvish Fighter) (Wounded Dwarf) ( _ "Wounded Dwarf") 4 21 29}

[message]
description=Wounded Dwarf
message= _ "Help! They're everywhere!"
[/message]

[kill]
description=Wounded Dwarf
animate=yes
[/kill]

[message]
description=Kaleh
message= _ "Nym, your timing is impeccable."
[/message]

[if]
	[have_unit]
	description=Elyssa
	[/have_unit]

	[then]

	[message]
	description=Elyssa
	message= _ "That's a dwarf, but it looks like he's been beaten to a pulp."
	[/message]

	[/then]

	[else]

	[message]
	description=Zhul
	message= _ "Short and hairy, he must be a dwarf. But he's been beaten to a pulp."
	[/message]

	[/else]
[/if]

[message]
description=Kaleh
message= _ "I don't know what 'they' are, but we can't go back. Prepare yourselves for anything, everyone."
[/message]

[/event]


# EVENT 5 IS LISTED OUT OF ORDER BECAUSE FOR SOME REASON IT WASN'T
# FIRING WHEN PLACED AFTER EVENT 4.5. CURRENTLY THIS EVENT WORKS
# FINE WHEN IT IS HERE AND EVENT 6 FIRES FINE AFTER 4.5. SO EVEN
# THOUGH THE EVENTS ARE OUT OF ORDER, I'M LEAVING THEM THIS WAY
# BECAUSE IT SEEMS TO MAKE EVERYTHING WORK FINE.

# Event 5: Sighted dwarf/troll leader events

# NW dwarf
[event]
name=moveto

[filter]
x=23-31
y=8-17
side=1
[/filter]

[remove_shroud]
x=22-31
y=7-17
side=1
[/remove_shroud]

[if]
	[variable]
	name=elvish_ally
	numerical_equals=1
	[/variable]

	[then]

	[message]
	description=Fundin
	message= _ "What are you doing back here? The trolls hide in the southern tunnels, not this way."
	[/message]

	[/then]

	[else]

	# open other exit from cave, closed off to try to keep AI
	# from guarding it

	[terrain]
	x,y=27,8
	letter=Uu
	[/terrain]

	[message]
	description=Fundin
	message= _ "Treacherous elves, how can you fight with such horrid creatures as Trolls. I will cleave all in two with my axe!"
	[/message]

	[/else]
[/if]
[/event]

# NE dwarf
[event]
name=moveto

[filter]
x=41-51
y=10-16
side=1
[/filter]

[remove_shroud]
x=40-52
y=9-17
side=1
[/remove_shroud]

[if]
	[variable]
	name=elvish_ally
	numerical_equals=1
	[/variable]

	[then]

	[message]
	description=Nori
	message= _ "What are you doing back here? The trolls hide in the southern tunnels, not this way."
	[/message]

	[/then]

	[else]

	# open other exit from cave, closed off to try to keep AI
	# from guarding it

	[terrain]
	x,y=44,6
	letter=Uu
	[/terrain]

	[message]
	description=Nori
	message= _ "If you think you can take these caves from us, then you are fools. We are masters of fighting underground and we will die to defend our home. Fight on, my brothers!"
	[/message]

	[/else]
[/if]
[/event]

# SW troll
[event]
name=moveto

[filter]
x=23-31
y=37-44
side=1
[/filter]

[remove_shroud]
x=21-32
y=37-44
side=1
[/remove_shroud]

[if]
	[variable]
	name=elvish_ally
	numerical_equals=1
	[/variable]

	[then]

	[message]
	description=Thungar
	message= _ "Nasty Dwarves and Stinkin' Elves, we will smash you all!"
	[/message]

	[/then]

	[else]

	[message]
	description=Thungar
	message= _ "What you doing back here? Nasty dwarves are to the north, no dwarves this way. Go back and fight bravely."
	[/message]

	[/else]
[/if]
[/event]

# SE troll
[event]
name=moveto

[filter]
x=42-51
y=37-45
side=1
[/filter]

[remove_shroud]
x=42-51
y=37-46
side=1
[/remove_shroud]

[if]
	[variable]
	name=elvish_ally
	numerical_equals=1
	[/variable]

	[then]

	[message]
	description=Gnarl
	message= _ "Kill the elves! We must stop them here. This is our land, crush the intruders!"
	[/message]

	[/then]

	[else]

	[message]
	description=Gnarl
	message= _ "What you doing back here? Nasty dwarves are to the north, no dwarves this way. Go back and fight bravely."
	[/message]

	[/else]
[/if]
[/event]



# Event 2: Entering the large cavern

#player chooses one side (trolls or dwarves) then:

#assign allies
#create troll and dwarf leaders
#create dwarf and troll defenders, make one side attackers
#change ally and enemy gold/income
#capture all villages, destroy ally's tunnel villages
#seal off exits from allies caves
#set battle turn counter

[event]
name=moveto

[filter]
x=24-74
y=20-34
side=1
[/filter]

#create 14 dwarves and 12 trolls

#dwarves:
# 2 dwarvish steelclads, 3 dwarvish fighters, 1 dwarvish thunderguard, 2 dwarvish thunderers, 2 dwarvish guardsmen, 1 dwarvish ulfserker, 1 dwarvish steelclad (captain), and 1 dwarvish pathfinder and 1 dwarvish scout

#trolls:
# 6 troll whelps, 2 trolls, 2 troll rocklobbers, 1 shaman, 1 shaman leader

# on EASY and NORMAL, reduce number of trolls and dwarves
# if EASY:
# Dwarves:	remove 1 fighter (39,21), and 1 thunderer (35,20)
# Trolls:	remove 2 troll whelps (37,32) (34,34)
#		turn troll rocklobber into a whelp (33,32)

# if NORMAL:
# Dwarves:	remove 1 fighter (39,21)
# Trolls:	remove 2 troll whelps (37,32) (34,34)


#Western side
	#battling over the castle
	#1 dwarvish fighter, 1 dwarvish thunderer, 1 dwarvish guardsman

	{UNIT_T (Dwarvish Fighter) (Dwarf Defender) ( _ "Dwarf Defender") 4 30 27}
	{UNIT_T (Dwarvish Thunderer) (Dwarf Defender) ( _ "Dwarf Defender") 4 30 25}
	{UNIT_T (Dwarvish Guardsman) (Dwarf Defender) ( _ "Dwarf Defender") 4 29 30}

	#2 troll whelps
	{UNIT_T (Troll Whelp) (Troll Defender) ( _ "Troll Defender") 3 31 30}
	{UNIT_T (Troll Whelp) (Troll Defender) ( _ "Troll Defender") 2 29 28}

	#western reinforcements

	#1 dwarvish berserker, 1 dwarvish steelclad (captain), 1 dwarvish fighter, (added: 1 dwarvish scout)

	{UNIT_T (Dwarvish Ulfserker) (Dwarf Defender) ( _ "Dwarf Defender") 4 30 21}
	{UNIT_T (Dwarvish Steelclad) (Dwarf Leader) ( _ "Dwarf Leader") 4 32 20}
	{UNIT_T (Dwarvish Fighter) (Dwarf Defender) ( _ "Dwarf Defender") 5 34 22}

	{UNIT_T (Dwarvish Scout) (Dwarf Defender) ( _ "Dwarf Defender") 4 34 26}

	#2 troll whelps, 1 troll rocklobber, 1 shaman (captain)

	{UNIT_T (Troll Whelp) (Troll Defender) ( _ "Troll Defender") 2 29 31}

	#unit was originally a troll
	#ifdef HARD
	{UNIT_T (Troll Whelp) (Troll Defender) ( _ "Troll Defender") 2 34 31}
	#endif

	#ifdef EASY
	{UNIT_T (Troll Whelp) (Troll Defender) ( _ "Troll Defender") 2 33 32}
	#else
	{UNIT_T (Troll Rocklobber) (Troll Defender) ( _ "Troll Defender") 2 33 32}
	#endif

	{UNIT_T (Troll Shaman) (Troll Leader) ( _ "Troll Leader") 2 28 33}

#Center
	#1 dwarvish thunderer (N village)

	#ifdef EASY
	#else
	{UNIT_T (Dwarvish Thunderer) (Dwarf Defender) ( _ "Dwarf Defender") 5 35 20}
	#endif

#Eastern side
	#battling over the center

	#1 dwarvish steelclad, 1 dwarvish guardsman, 1 dwarvish thunderer
	#1 dwarvish pathfinder
	{UNIT_T (Dwarvish Steelclad) (Dwarf Defender) ( _ "Dwarf Defender") 5 40 26}
	{UNIT_T (Dwarvish Guardsman) (Dwarf Defender) ( _ "Dwarf Defender") 5 39 24}
	{UNIT_T (Dwarvish Thunderer) (Dwarf Defender) ( _ "Dwarf Defender") 5 42 23}
	{UNIT_T (Dwarvish Pathfinder) (Dwarf Defender) ( _ "Dwarf Defender") 5 44 26}

	#1 troll whelp, 1 troll, 1 troll rocklobber

	{UNIT_T (Troll Whelp) (Troll Defender) ( _ "Troll Defender") 2 41 27}
	{UNIT_T (Troll) (Troll Defender) ( _ "Troll Defender") 3 42 29}
	{UNIT_T (Troll Rocklobber) (Troll Defender) ( _ "Troll Defender") 3 39 29}

	#eastern reinforcements

	#1 dwarvish steelclad, 1 dwarvish fighter

	{UNIT_T (Dwarvish Steelclad) (Dwarf Defender) ( _ "Dwarf Defender") 5 42 21}

	#ifdef HARD
	{UNIT_T (Dwarvish Fighter) (Dwarf Defender) ( _ "Dwarf Defender") 5 39 21}
	#endif

	#2 troll whelps, 1 shaman

	#ifdef HARD
	{UNIT_T (Troll Whelp) (Troll Defender) ( _ "Troll Defender") 3 37 32}
	#endif
	{UNIT_T (Troll Whelp) (Troll Defender) ( _ "Troll Defender") 3 42 32}
	{UNIT_T (Troll Shaman) (Troll Defender) ( _ "Troll Defender") 3 44 31}

[redraw]
[/redraw]

#reveal large cave x: 25-47 y: 19-35
[remove_shroud]
	side=1
	x=25-47
	y=19-35
[/remove_shroud]

#dwarf/troll/elf dialogue

[message]
speaker=unit
message= _ "Woah."
[/message]

[scroll_to_unit]
x,y=34,22
[/scroll_to_unit]

[delay]
time=400
[/delay]

[scroll_to_unit]
x,y=42,23
[/scroll_to_unit]

[delay]
time=400
[/delay]

[scroll_to_unit]
x,y=42,29
[/scroll_to_unit]

[delay]
time=400
[/delay]

[scroll_to_unit]
x,y=33,32
[/scroll_to_unit]

[delay]
time=400
[/delay]

[scroll_to_unit]
x,y=29,27
[/scroll_to_unit]

[delay]
time=400
[/delay]

[message]
description=Nym
message= _ "I think I preferred the spider and the ants..."
[/message]

[message]
description=Dwarf Leader
message= _ "Stand firm boys, here they come!"
[/message]

[message]
description=Troll Leader
message= _ "You invade our tunnels, you slaughter our women and children, by Griknagh we will make you pay!"
[/message]

[message]
description=Dwarf Leader
message= _ "Tenacious savages, aren't they? But these tunnels are rich in ore, and we won't let a couple of trolls keep them from us. "
[/message]

[message]
description=Troll Leader
message= _ "Wait...What..Who are you?"
[/message]

[message]
description=Kaleh
message= _ "Uh...."
[/message]

[message]
description=Dwarf Leader
message= _ "What in Moradin's name are they?"
[/message]

[message]
x,y=30,27
message= _ "Wait a minute....blond hair, pointy ears, they must be Elves."
[/message]

[message]
x,y=30,25
message= _ "Elves!?! What in the nine hells are Elves doing down here?"
[/message]

[message]
description=Dwarf Leader
message= _ "Never mind that, who are you?"
[/message]

[message]
description=Kaleh
message= _ "I am Kaleh, and we are the Quenoth elves. What in Eloh's name is going on here?"
[/message]

[message]
description=Troll Leader
message= _ "They invade our land and kill our young. Dwarves always want more, always greedy for glittery rocks."
[/message]

[message]
description=Dwarf Leader
message= _ "Those monsters killed me boys. Kaleh, if you be of stout heart, help us drive these lummoxes from our tunnels."
[/message]

[message]
description=Troll Leader
message= _ "No, this is our home. Help us, little ones, and we will help you."
[/message]

[message]
description=Zhul
message= _ "There's too many of them for us to try to take them both on, and besides with all these branching tunnels we'll have no idea which way to go. I think we should take them up on their offer; ally ourselves with one of the factions so we can get their help in finding a way back to the surface."
[/message]

[message]
description=Nym
message= _ "But even if we do, what about all of our people? How can we safely escort them through this war zone?"
[/message]

[message]
description=Kaleh
message= _ "We won't. If we keep the majority of our people hidden back up the passage we should be able to protect them, at least for a little while. In the meantime, we'll go ahead and try to sort out this mess. I think you're right Zhul, if we're lucky we may just be able to negotiate a safe passage out of here."
[/message]

#choose side to ally with

#set ally variable (1=dwarf 2=troll) and change elvish allegiance

[message]
speaker=unit
message= _ "But they both look evenly matched. Who should we ally with?"

	[option]
	message= _  "Let's aid the Dwarves."

	[command]
		[set_variable]
		name=elvish_ally
		value=1
		[/set_variable]
	[/command]

	[command]
		[modify_side]
		side=1
		team_name=dwarf
		[/modify_side]
	[/command]

	[command]
		[message]
		description=Troll Leader
		message= _ "Bah! Your kind all the same. Everyone turns on Trolls. But you'll see, Griknagh will smash you all."
		[/message]
	[/command]

	#set new scenario objectives

[command]
	[objectives]
	summary= _ "New Objectives:"
	show=yes
		[objective]
			description= _ "Defeat Troll Leaders"
			condition=win
		[/objective]
		[objective]
			description= _ "Death of Kaleh, Nym or Zhul"
			condition=lose
		[/objective]
	[/objectives]
[/command]

	[/option]

	[option]
	message= _  "Let's aid the Trolls."

	[command]
		[set_variable]
		name=elvish_ally
		value=2
		[/set_variable]
	[/command]

	[command]
		[modify_side]
		side=1
		team_name=troll
		[/modify_side]
	[/command]

	[command]
		[message]
		description=Dwarf Leader
		message= _ "I knew elves couldn't be trusted. Foolish boy, you will regret your betrayal. Taste dwarven steel!"
		[/message]
	[/command]

	#set new scenario objectives
[command]
	[objectives]
	summary= _ "New Objectives:"
	show=yes
		[objective]
			description= _ "Defeat Dwarf Leaders"
			condition=win
		[/objective]
		[objective]
			description= _ "Death of Kaleh, Nym or Zhul"
			condition=lose
		[/objective]
	[/objectives]
[/command]

	[/option]
[/message]

# change music playing

[music]
name=battle.ogg
immediate=yes
[/music]

[message]
speaker=unit
message= _ "There seems to be an abandoned Dwarvish fortress right in front of us. If we can fight our way to the keep, we should be able to start rallying our warriors to help in the battle."
[/message]

#add enemy units in tunnels who will arrive in main cavern at turn 2

[if]
	[variable]
	name=elvish_ally
	numerical_equals=1
	[/variable]

	[then]

	# EASY: add 3 troll whelps, MED: add 3 trolls HARD: add 4 trolls
	# (on easier difficulties, remove trolls closest to elves)

	#ifdef HARD
	{UNIT_T (Troll Whelp) (Troll Skirmisher) ( _ "Troll Skirmisher") 2 25 37}
	#endif

	{UNIT_T (Troll Whelp) (Troll Skirmisher) ( _ "Troll Skirmisher") 3 33 38}

	{UNIT_T (Troll Whelp) (Troll Skirmisher) ( _ "Troll Skirmisher") 3 42 36}

	{UNIT_T (Troll Whelp) (Troll Skirmisher) ( _ "Troll Skirmisher") 3 46 35}

	[/then]

	[else]

	# add 3-4 dwarf enemies
	# if EASY difficulty, then remove Dwarvish Thunderer

	# (was a dwarvish steelclad)
	{UNIT_T (Dwarvish Fighter) (Dwarf Skirmisher) ( _ "Dwarf Skirmisher") 4 26 18}

	{UNIT_T (Dwarvish Pathfinder) (Dwarf Skirmisher) ( _ "Dwarf Skirmisher") 4 28 18}

	{UNIT_T (Dwarvish Berserker) (Dwarf Skirmisher) ( _ "Dwarf Skirmisher") 5 38 18}

	#ifdef EASY
	#else
	{UNIT_T (Dwarvish Thunderguard) (Dwarf Thunderer) ( _ "Dwarf Thunderer") 5 43 19}
	#endif

	{UNIT_T (Dwarvish Scout) (Dwarf Skirmisher) ( _ "Dwarf Skirmisher") 5 43 18}


	[/else]
[/if]

#increase recruitment options for enemies
#set gold/income for allies and enemies

# I think the trolls are slightly more powerful than the dwarves
# So I'm giving trolls 2 less income than dwarves

[if]
	[variable]
	name=elvish_ally
	numerical_equals=1
	[/variable]

	[then]
	#ally with dwarves

	[allow_recruit]
		type=Troll Shaman
		side=2
	[/allow_recruit]

	[allow_recruit]
		type=Troll Shaman
		side=3
	[/allow_recruit]

	#allies
	[modify_side]
		side=4

		#ifdef EASY
		income=-2
		gold=50
		#endif
		#ifdef MEDIUM
		income=-3
		gold=50
		#endif
		#ifdef HARD
		income=-4
		gold=25
		#endif
	[/modify_side]

	[modify_side]
		side=5

		#ifdef EASY
		income=-2
		gold=50
		#endif
		#ifdef MEDIUM
		income=-3
		gold=50
		#endif
		#ifdef HARD
		income=-4
		gold=25
		#endif
	[/modify_side]

	#enemies
	[modify_side]
		side=2

		#ifdef EASY
		income=3
		gold=100
		#endif
		#ifdef MEDIUM
		income=5
		gold=125
		#endif
		#ifdef HARD
		income=7
		gold=150
		#endif
	[/modify_side]

	[modify_side]
		side=3

		#ifdef EASY
		income=3
		gold=100
		#endif
		#ifdef MEDIUM
		income=5
		gold=125
		#endif
		#ifdef HARD
		income=7
		gold=150
		#endif
	[/modify_side]

	[/then]

	[else]
	#ally with trolls

	#ulfserkers are too deadly, especially for elves with no defense

	[allow_recruit]
		type=Dwarvish Pathfinder
		side=4
	[/allow_recruit]

	[allow_recruit]
		type=Dwarvish Pathfinder
		side=5
	[/allow_recruit]

	#allies
	[modify_side]
		side=2

		#ifdef EASY
		income=-2
		gold=50
		#endif
		#ifdef MEDIUM
		income=-3
		gold=50
		#endif
		#ifdef HARD
		income=-4
		gold=25
		#endif
	[/modify_side]

	[modify_side]
		side=3

		#ifdef EASY
		income=-2
		gold=50
		#endif
		#ifdef MEDIUM
		income=-3
		gold=50
		#endif
		#ifdef HARD
		income=-4
		gold=25
		#endif
	[/modify_side]

	#enemies
	[modify_side]
		side=4

		#ifdef EASY
		income=4
		gold=125
		#endif
		#ifdef MEDIUM
		income=6
		gold=150
		#endif
		#ifdef HARD
		income=8
		gold=150
		#endif
	[/modify_side]

	[modify_side]
		side=5

		#ifdef EASY
		income=4
		gold=125
		#endif
		#ifdef MEDIUM
		income=6
		gold=150
		#endif
		#ifdef HARD
		income=8
		gold=150
		#endif
	[/modify_side]

	[/else]
[/if]


#create 2 dwarf leaders (23,11) (45,10)
[unit]
	type=Dwarvish Explorer
	description=Fundin
	user_description= _ "Fundin"
	canrecruit=1
	upkeep=free
	x=23
	y=11
	side=4
	[modifications]
	{TRAIT_LOYAL}
	{TRAIT_RESILIENT}
	[/modifications]
[/unit]

[unit]
	type=Dwarvish Explorer
	description=Nori
	user_description= _ "Nori"
	canrecruit=1
	upkeep=full
	x=45
	y=10
	side=5
	[modifications]
	{TRAIT_STRONG}
	{TRAIT_QUICK}
	[/modifications]
[/unit]

#create 2 troll leaders (32,70) (52,72)
[unit]
	type=Troll Warrior
	description=Thungar
	user_description= _ "Thungar"
	canrecruit=1
	upkeep=free
	x=26
	y=43
	side=2
	[modifications]
	{TRAIT_LOYAL}
	{TRAIT_RESILIENT}
	[/modifications]
[/unit]

[unit]
	type=Troll Warrior
	description=Gnarl
	user_description= _ "Gnarl"
	canrecruit=1
	upkeep=full
	x=46
	y=45
	side=3
	[modifications]
	{TRAIT_STRONG}
	{TRAIT_QUICK}
	[/modifications]
[/unit]

[set_variable]
name=number_enemies
value=12
[/set_variable]

#change description of units on enemy side

[set_variable]
name=i
value=$number_enemies
[/set_variable]

{STARTLOOP i}

[if]
	[variable]
	name=elvish_ally
	numerical_equals=2
	[/variable]

	[then]

	[store_unit]
		[filter]
		description=Dwarf Defender
		[/filter]
	variable=unitstats
	[/store_unit]

	[set_variable]
		name=unitstats.description
		value=Dwarf Skirmisher
	[/set_variable]

	[set_variable]
		name=unitstats.user_description
		value=Dwarf Skirmisher
	[/set_variable]

	[unstore_unit]
		variable=unitstats
	[/unstore_unit]

	{CLEAR_VARIABLE unitstats}

	[/then]

	[else]

	[store_unit]
		[filter]
		description=Troll Defender
		[/filter]
	variable=unitstats
	[/store_unit]

	[set_variable]
		name=unitstats.description
		value=Troll Skirmisher
	[/set_variable]

	[set_variable]
		name=unitstats.user_description
		value=Troll Skirmisher
	[/set_variable]

	[unstore_unit]
		variable=unitstats
	[/unstore_unit]

	{CLEAR_VARIABLE unitstats}

	[/else]

[/if]

{CLEAR_VARIABLE unitstats}

{ENDLOOP i}


#destroy ally's villages in the tunnels
[if]
	[variable]
	name=elvish_ally
	numerical_equals=1
	[/variable]

	[then]
	# if allied with dwarves
	# destroy dwarvish villages in tunnels

	[terrain]
	x=28,35
	y=19,14
	letter=Uu
	[/terrain]

	[terrain]
	x=36,43
	y=17,18
	letter=Xu
	[/terrain]

	[/then]

	[else]
	# if allied with trolls
	# destroy troll villages in tunnels

	[terrain]
	x=25
	y=36
	letter=Uu^Uf
	[/terrain]

	[terrain]
	x=34
	y=37
	letter=Xu
	[/terrain]

	[terrain]
	x=47,37
	y=36,47
	letter=Uu
	[/terrain]

	[/else]
[/if]

# capture villages for each troll/dwarf leader

# troll 1 (side2)

# in troll cave and western tunnels
[capture_village]
x,y=22-35,36-43
side=2
[/capture_village]

# in main cavern
[capture_village]
x,y=31,30
side=2
[/capture_village]

# troll 2 (side3)

# in troll cave and eastern tunnels
[capture_village]
x,y=37-50,36-47
side=3
[/capture_village]

# in main cavern
[capture_village]
x,y=36,32
side=3
[/capture_village]

[capture_village]
x,y=40,26
side=3
[/capture_village]


# dwarf 1 (side4)

# in dwarf cave and western tunnels
[capture_village]
x,y=24-35,8-19
side=4
[/capture_village]

# in main cavern
[capture_village]
x,y=28,24
side=4
[/capture_village]


# dwarf 2 (side5)

# in dwarf cave and eastern tunnels
[capture_village]
x,y=36-51,10-18
side=5
[/capture_village]

# in main cavern
[capture_village]
x,y=35,27
side=5
[/capture_village]

#count number of turns since central cave battle started

[set_variable]
name=battle_turn_counter
value=1
[/set_variable]

[/event]


# Event 3: Enemy counter-attack (Battle Turn 3)

# if fighting dwarves: (3,4,5)
# run 2 dwarvish thunderguards down to western threatre,
# and 1 down to eastern bottleneck

# if fighting trolls: (3,4,5)
# run 2 troll shamans up to western threatre, and 1 up to eastern
# bottleneck (reduced to 1 on each side)

# they destroy (Easy:33%  Medium: 50% Hard: 50%) of your allies


#define ENEMY_ATTACK

	[if]
		[variable]
		name=elvish_ally
		numerical_equals=1
		[/variable]

		[then]
		#if allied with dwarves

		#west side

		[move_unit_fake]
		type=Troll Shaman
		x=22,23,24,25,26,27,27,27,28
		y=37,37,36,36,35,35,34,33,32
		[/move_unit_fake]

		{UNIT_T (Troll Shaman) (Troll Flamecaster) ( _ "Troll Flamecaster") 2 28 32}

		#ifdef HARD

		[move_unit_fake]
		type=Troll Shaman
		x=32,33,34,34,34,34,33,32,31,30
		y=38,38,37,36,35,34,34,33,33,32
		[/move_unit_fake]

		{UNIT_T (Troll Shaman) (Troll Flamecaster) ( _ "Troll Flamecaster") 2 32 32}
		#endif

		#east side

		[move_unit_fake]
		type=Troll Shaman
		x=47,47,47,46,45,44,43,42,42,42,42,41
		y=37,36,35,34,34,33,33,32,31,30,29,29
		[/move_unit_fake]

		{UNIT_T (Troll Shaman) (Troll Flamecaster) ( _ "Troll Flamecaster") 3 41 29}

		#ifdef EASY
		#else

		[move_unit_fake]
		type=Troll Shaman
		x=47,47,47,46,45,44,43,43,43,43,43
		y=27,26,25,24,24,23,23,22,21,20,19
		[/move_unit_fake]

		{UNIT_T (Troll Shaman) (Troll Flamecaster) ( _ "Troll Flamecaster") 3 43 19}
		#endif

		[message]
		description=Troll Flamecaster
		message= _ "Burn, burn and die!"
		[/message]

		[colour_adjust]
		red,green,blue=255,0,0
		[/colour_adjust]

		[redraw]
		[/redraw]

		[delay]
		time=100
		[/delay]

		[colour_adjust]
		red,green,blue=0,0,0
		[/colour_adjust]

		[redraw]
		[/redraw]

		[message]
		description=Dwarf Leader
		message= _ "Dive for cover!"
		[/message]

		[message]
		description=Kaleh
		message= _ "Those new troll shamans are decimating the dwarves with blasts of fire! This doesn't look good."
		[/message]

		#store dwarves in main cave in array
		#main cave = x 32-51 y 21-33
		#store length of array as X
		#divide X in half = Y
		#loop Y times:
		#	(store dwarves in main cave in array)
		#	(store length of array as Z)
		#	(Random 0 to Z-1)
		#	(Kill random dwarf)
		#	[set_variable]
		#	name=kill_x
		#	to_variable=locs[$i].x
		#	[/set_variable]
		#
		#	[set_variable]
		#	name=kill_y
		#	to_variable=locs[$i].y
		#	[/set_variable]

		[store_locations]
			x=24-46
			y=19-35
			[filter]
			description="Dwarf Defender"
			[/filter]
			variable=dwarf_list
		[/store_locations]

		[set_variable]
		name=number_of_dwarves
		value=$dwarf_list.length
		[/set_variable]

		[set_variable]
		name=number_to_kill
		value=$number_of_dwarves
		[/set_variable]

		#if easy kill 60%
		#if medium kill 70%
		#if hard kill 80%

		#ifdef EASY
		[set_variable]
		name=number_to_kill
		multiply=0.59
		[/set_variable]
		#endif

		#ifdef MEDIUM
		[set_variable]
		name=number_to_kill
		multiply=0.69
		[/set_variable]
		#endif

		#ifdef HARD
		[set_variable]
		name=number_to_kill
		multiply=0.79
		[/set_variable]
		#endif

		[set_variable]
		name=i
		value=$number_to_kill
		[/set_variable]

		{STARTLOOP i}

			[store_locations]
			x=24-46
			y=19-35
			[filter]
			description="Dwarf Defender"
			[/filter]
			variable=dwarf_victims
			[/store_locations]

			[set_variable]
			name=number_of_victims
			value=$dwarf_victims.length
			[/set_variable]

			#subtract 1 because arrays go from 0 to length-1
			[set_variable]
			name=number_of_victims
			add=-1
			[/set_variable]

			[set_variable]
			name=random_string
			format=0..$number_of_victims
			[/set_variable]

			[set_variable]
			name=death
			random=$random_string
			[/set_variable]

			[set_variable]
			name=kill_x
			to_variable=dwarf_victims[$death].x
			[/set_variable]

			[set_variable]
			name=kill_y
			to_variable=dwarf_victims[$death].y
			[/set_variable]

			#have some dwarves scream as they die

			[if]
				[variable]
				name=i
				numerical_equals=-1
				[/variable]

				[then]

				[message]
				x,y=$kill_x,$kill_y
				message= _ "Aaauuuggghhhh!"
				[/message]

				[/then]
			[/if]

			[if]
				[variable]
				name=i
				numerical_equals=-2
				[/variable]

				[then]

				[message]
				x,y=$kill_x,$kill_y
				message= _ "No!!!"
				[/message]

				[/then]
			[/if]

			[if]
				[variable]
				name=i
				numerical_equals=-3
				[/variable]

				[then]

				[message]
				x,y=$kill_x,$kill_y
				message= _ "Help me!!!"
				[/message]

				[/then]
			[/if]

			[kill]
			x,y=$kill_x,$kill_y
			animate=yes
			fire_event=no
			[/kill]

		{ENDLOOP i}

		[message]
		description=Dwarf Leader
		message= _ "More accursed troll magic. Fall back! "
		[/message]

		[message]
		description=Dwarf Leader
		message= _ "I need to go back and rally more reinforcements. We're hurtin', Kaleh, I'll need your men to cover for us. Do you best, boy, and may Moradin watch over you."
		[/message]

		[kill]
		description=Dwarf Leader
		animate=no
		[/kill]

		[/then]

		[else]
		#if allied with trolls

		#west side
		[move_unit_fake]
		type=Dwarvish Thunderguard
		x=26,27,28,29,30,30,30,30
		y=18,19,19,19,19,20,21,22
		[/move_unit_fake]

		{UNIT_T (Dwarvish Thunderguard) (Dwarf Grenadier) ( _ "Dwarf Grenadier") 4 30 22}

		[move_unit_fake]
		type=Dwarvish Thunderguard
		x=26,27,28,29,30,30,31,32,32
		y=18,19,19,19,19,20,21,21,22
		[/move_unit_fake]

		{UNIT_T (Dwarvish Thunderguard) (Dwarf Grenadier) ( _ "Dwarf Grenadier") 4 32 22}

		#ifdef HARD

		[move_unit_fake]
		type=Dwarvish Thunderguard
		x=26,27,28,29,30,30,31,32,33,34
		y=18,19,19,19,19,20,21,21,22,22
		[/move_unit_fake]

		{UNIT_T (Dwarvish Thunderguard) (Dwarf Grenadier) ( _ "Dwarf Grenadier") 4 34 22}
		#endif

		#east side

		[move_unit_fake]
		type=Dwarvish Thunderguard
		x=45,44,44,43,42,41,41,40
		y=18,18,19,20,20,21,22,22
		[/move_unit_fake]

		{UNIT_T (Dwarvish Thunderguard) (Dwarf Grenadier) ( _ "Dwarf Grenadier") 5 40 22}

		#ifdef EASY
		#else

		[move_unit_fake]
		type=Dwarvish Thunderguard
		x=45,44,44,43,43,42,42
		y=18,18,19,20,21,21,22
		[/move_unit_fake]

		{UNIT_T (Dwarvish Thunderguard) (Dwarf Grenadier) ( _ "Dwarf Grenadier") 5 42 22}
		#endif

		[message]
		description=Dwarf Grenadier
		message= _ "Let's blast those monsters back to the pits they spawned from! Fire in the hole!"
		[/message]

		[colour_adjust]
		red,green,blue=255,0,0
		[/colour_adjust]

		[redraw]
		[/redraw]

		[delay]
		time=100
		[/delay]

		[colour_adjust]
		red,green,blue=0,0,0
		[/colour_adjust]

		[redraw]
		[/redraw]

		[message]
		description=Troll Leader
		message= _ "More Dwarven trickery! Fall back!"
		[/message]

		[message]
		description=Kaleh
		message= _ "Those new dwarves are lobbing explosives at the trolls with devastating effect! I don't think the trolls can take this much longer."
		[/message]

		[store_locations]
			x=24-46
			y=19-35
			[filter]
			description="Troll Defender"
			[/filter]
			variable=troll_list
		[/store_locations]

		[set_variable]
		name=number_of_trolls
		value=$troll_list.length
		[/set_variable]

		[set_variable]
		name=number_to_kill
		value=$number_of_trolls
		[/set_variable]

		#if easy kill 50%
		#if medium kill 60%
		#if hard kill 75%

		#ifdef EASY
		[set_variable]
		name=number_to_kill
		multiply=0.49
		[/set_variable]
		#endif

		#ifdef MEDIUM
		[set_variable]
		name=number_to_kill
		multiply=0.59
		[/set_variable]
		#endif

		#ifdef HARD
		[set_variable]
		name=number_to_kill
		multiply=0.74
		[/set_variable]
		#endif

		[set_variable]
		name=i
		value=$number_to_kill
		[/set_variable]

		{LOOP i}

			[store_locations]
			x=24-46
			y=19-35
			[filter]
			description="Troll Defender"
			[/filter]
			variable=troll_victims
			[/store_locations]

			[set_variable]
			name=number_of_victims
			value=$troll_victims.length
			[/set_variable]

			#subtract 1 because arrays go from 0 to length-1
			[set_variable]
			name=number_of_victims
			add=-1
			[/set_variable]

			[set_variable]
			name=random_string
			format=0..$number_of_victims
			[/set_variable]

			[set_variable]
			name=death
			random=$random_string
			[/set_variable]

			[set_variable]
			name=kill_x
			to_variable=troll_victims[$death].x
			[/set_variable]

			[set_variable]
			name=kill_y
			to_variable=troll_victims[$death].y
			[/set_variable]

			#have some trolls scream as they die

			[if]
				[variable]
				name=i
				numerical_equals=-1
				[/variable]

				[then]

				[message]
				x,y=$kill_x,$kill_y
				message= _ "Aaauuuggghhhh!"
				[/message]

				[/then]
			[/if]

			[if]
				[variable]
				name=i
				numerical_equals=-2
				[/variable]

				[then]

				[message]
				x,y=$kill_x,$kill_y
				message= _ "No!!!"
				[/message]

				[/then]
			[/if]

			[if]
				[variable]
				name=i
				numerical_equals=-3
				[/variable]

				[then]

				[message]
				x,y=$kill_x,$kill_y
				message= _ "Gaggghhh!"
				[/message]

				[/then]
			[/if]

			[kill]
			x,y=$kill_x,$kill_y
			animate=yes
			fire_event=no
			[/kill]

		{NEXT i}

		[message]
		description=Troll Leader
		message= _ "I must go back and find more trolls to fight. You must hold them back, Kaleh. Be strong like rock. Griknagh will be with you."
		[/message]

		[kill]
		description=Troll Leader
		animate=no
		[/kill]

		[/else]
	[/if]

#enddef

# Event 4: Ally reinforcements
#	message: ally leader sent us to help you

#define ALLY_REINFORCEMENTS

	[if]

		[variable]
		name=elvish_ally
		numerical_equals=2
		[/variable]

		[then]

		# Troll
		# Easy: 3 whelps 1 troll shaman, 1 rock lobber
		# Medium: 3 whelps 1 troll shaman, 1 rock lobber
		# Hard: 2 whelps 1 troll shaman, 1 rock lobber

		[unit]
			type=Troll Shaman
			description=Thu'lok
			user_description= _ "Thu'lok"
			x=34
			y=32
			side=1
			[modifications]
				{TRAIT_LOYAL}
				{TRAIT_RESILIENT}
			[/modifications]
		[/unit]

		[unit]
			type=Troll Whelp
			description=Harpo
			user_description= _ "Harpo"
			x=33
			y=33
			side=1
			[modifications]
				{TRAIT_LOYAL}
				{TRAIT_STRONG}
			[/modifications]
		[/unit]

		[unit]
			type=Troll Whelp
			description=Groucho
			user_description= _ "Groucho"
			x=34
			y=33
			side=1
			[modifications]
				{TRAIT_LOYAL}
				{TRAIT_INTELLIGENT}
			[/modifications]
		[/unit]

		#ifdef HARD
		#else
		[unit]
			type=Troll Whelp
			user_description= _ "Chico"
			description=Chico
			x=35
			y=33
			side=1
			[modifications]
				{TRAIT_LOYAL}
				{TRAIT_RESILIENT}
			[/modifications]
		[/unit]
		#endif

		[unit]
			type=Troll Rocklobber
			user_description= _ "Groo"
			description=Groo
			x=33
			y=34
			side=1
			[modifications]
				{TRAIT_LOYAL}
				{TRAIT_QUICK}
			[/modifications]
		[/unit]

		[role]
		role=ally
		type=Troll Whelp
		[/role]

		[role]
		role=ally
		type=Troll Rocklobber
		[/role]

		[role]
		role=ally
		type=Troll Shaman
		[/role]

		[message]
		description=Thu'lok
		message= _ "Our leader sent us to help you. We fight for you until all the dwarves are dead. We will avenge the deaths of our people!"
		[/message]

		[/then]

		[else]

		# Dwarves
		# Easy: 2 dwarvish fighters, 1 thunderer, 1 berserker,
		# 1 dwarvish scout
		# Medium: 2 dwarvish fighters, 1 thunderer, 1 berserker,
		# 1 dwarvish scout
		# Hard: 1 dwarvish fighter, 1 thunderer, 1 berserker,
		# 1 dwarvish scout

		[unit]
			type=Dwarvish Fighter
			description=Dwalim
			user_description= _ "Dwalim"
			x=36
			y=21
			side=1
			[modifications]
				{TRAIT_LOYAL}
				{TRAIT_STRONG}
			[/modifications]
		[/unit]

		[unit]
			type=Dwarvish Pathfinder
			description=Moin
			user_description= _ "Moin"
			x=35
			y=21
			side=1
			[modifications]
				{TRAIT_LOYAL}
				{TRAIT_QUICK}
			[/modifications]
		[/unit]

		[unit]
			type=Dwarvish Thunderer
			description=Nordi
			user_description= _ "Nordi"
			x=37
			y=21
			side=1
			[modifications]
				{TRAIT_LOYAL}
				{TRAIT_INTELLIGENT}
			[/modifications]
		[/unit]

		[unit]
			type=Dwarvish Berserker
			description=Byorn
			user_description= _ "Byorn"
			x=38
			y=20
			side=1
			[modifications]
				{TRAIT_LOYAL}
				{TRAIT_RESILIENT}
			[/modifications]
		[/unit]

		#ifdef HARD
		#else

		[unit]
			type=Dwarvish Fighter
			description=Runin
			user_description= _ "Runin"
			x=36
			y=20
			side=1
			[modifications]
				{TRAIT_LOYAL}
				{TRAIT_QUICK}
			[/modifications]
		[/unit]

		[role]
		role=ally
		type=Dwarvish Fighter
		[/role]
		#endif

		[role]
		role=ally
		type=Dwarvish Fighter
		[/role]

		[role]
		role=ally
		type=Dwarvish Berserker
		[/role]

		[role]
		role=ally
		type=Dwarvish Thunderer
		[/role]

		[role]
		role=ally
		type=Dwarvish Pathfinder
		[/role]

		[message]
		description=Dwalim
		message= _ "Looks like we came just in time. Our chief told us we're to fight with you until all the trolls are dead. Tell us where to go, I want to kill me some troll!"
		[/message]

		[/else]
	[/if]

#enddef

# Battle Turn Counter
# Each turn increment counter, to keep track of when battle
# events should happen. 0 = off
# 1 = player just entered main cave, start incrementing counter each turn

# Turn after player enters main cave it is 2, next turn 3, 4, 5 and so on

# also at start of each turn, erase gold/income for sides that are dead
# or who haven't been activated yet

[event]
name=new turn
first_time_only=no

[if]
	[variable]
	name=battle_turn_counter
	greater_than=0
	[/variable]

	[then]

	[set_variable]
	name=battle_turn_counter
	add=1
	[/set_variable]

	[/then]
[/if]

# on turn 3 have winning enemy launch special attack
[if]
	[variable]
	name=battle_turn_counter
	numerical_equals=4
	[/variable]

	[then]

	{ENEMY_ATTACK}

	[/then]
[/if]

# on turn 6 have losing enemy bring reinforcements
[if]
	[variable]
	name=battle_turn_counter
	numerical_equals=7
	[/variable]

	[then]

	{ALLY_REINFORCEMENTS}

	[/then]
[/if]

# at a random time later, the assassin should appear and challenge Kaleh

[if]
	[variable]
	name=battle_turn_counter
	numerical_equals=$assassin_turn
	[/variable]

	[then]

	[set_variable]
	name=call_assassin
	value=1
	[/set_variable]

	[/then]
[/if]

[/event]


# Event 4.5: when player moves onto dwarf cairn or troll totems,
# his ally comments about it

#troll totems
[event]
name=moveto
first_time_only=no

[filter]
x=27,35,43
y=33,34,33
side=1
[/filter]

[if]
	[variable]
	name=elvish_ally
	numerical_equals=1
	[/variable]

	[variable]
	name=saw_cairn_or_totem
	numerical_equals=0
	[/variable]

	[have_unit]
	role=ally
	[/have_unit]

	[then]

	[set_variable]
	name=saw_cairn_or_totem
	value=1
	[/set_variable]

	[message]
	role=ally
	message= _ "The trolls display the skulls of their enemies as a way of marking their territory. How barbaric."
	[/message]

	[/then]
[/if]
[/event]

#dwarf cairns
[event]
name=moveto
first_time_only=no

[filter]
x=30,38,42
y=20,20,21
side=1
[/filter]

[if]
	[variable]
	name=elvish_ally
	numerical_equals=2
	[/variable]

	[variable]
	name=saw_cairn_or_totem
	numerical_equals=0
	[/variable]

	[have_unit]
	role=ally
	[/have_unit]

	[then]

	[set_variable]
	name=saw_cairn_or_totem
	value=1
	[/set_variable]

	[message]
	role=ally
	message= _ "The dwarves use stone cairns to mark their territory. What a waste of good throwing stones."
	[/message]

	[/then]
[/if]
[/event]


# Event 6: death events for each leader

#for each death check to see if other leader is dead, if so
#also send in dwarf/troll messenger with victory congratulations

#define ZURG_CONVERSATION

	[message]
	description=Zurg
	message= _ "Congratulations! Some of trolls didn't think you strong enough to beat dwarves."
	[/message]

	[message]
	speaker=Kaleh
	message= _ "Where did you come from?"
	[/message]

	[message]
	description=Zurg
	message= _ "There many secret tunnels that you sun dwellers not know of. Only troll know. We smarter than you think. Zurg would have killed dwarves himself, but he was just sent back from where real fighting is."
	[/message]

	[message]
	speaker=Zhul
	message= _ "The real fighting? I thought that was what we were waist-deep in?"
	[/message]

	[message]
	description=Zurg
	message= _ "While you fighting, another clan of dwarves sneak around and flank us. They tricksy like that. We must leave you and run back to defend women and little trolls. Dwarves never give up, many trolls die today, very hard fighting. But dwarves make mistake, you stronger than dwarf or troll thought. You trolls' secret weapon."
	[/message]

	[message]
	speaker=Nym
	message= _ "How do you mean?"
	[/message]

	[message]
	description=Zurg
	message= _ "Right before battle, we find secret passage just to the north leading straight to big dwarf stronghold. Hiding in stronghold is big important dwarf, directing the battle. Dwarves always think they best fighters around so they leave only a few dwarves guarding their stronghold. If you elves can break through dwarf defenses and kill dwarf chieftain, then it will do much damage to dwarves, make them afraid and confused, easy prey for trolls. You do this and we can drive them back. Then troll leader can help show you how to return to surface. You come with Zurg, he show you way to secret passage."
	[/message]

	[message]
	speaker=Elyssa
	message= _ "Their knowledge of these tunnels is uncanny. I could have sworn a minute ago that that wall was solid rock."
	[/message]

	[if]
		[have_unit]
		description=Cloaked Figure
		[/have_unit]

		[then]

		[message]
		description=Kaleh
		message= _ "Wait a moment Zurg, we must deal with this mysterious cloaked figure before we can follow you."
		[/message]

		#set new scenario objectives

		[objectives]
			summary= _ "New Objectives:"
			show=yes
			[objective]
				description= _ "Defeat Cloaked Figure"
				condition=win
			[/objective]
			[objective]
				description= _ "Death of Kaleh, Nym or Zhul"
				condition=lose
			[/objective]
		[/objectives]


		[set_variable]
		name=delay_victory
		value=1
		[/set_variable]

		[/then]

		[else]

		[message]
		description=Kaleh
		message= _ "It sounds like our work is not yet done. Very well, gather yourselves together, we must follow Zurg."
		[/message]

		[endlevel]
		result=victory
		next_scenario=6_2_HuntingDwarves
		bonus=yes
		[/endlevel]

		[/else]
	[/if]

#enddef


#define GRENDEL_CONVERSATION

	[message]
	description=Grendel
	message= _ "Congratulations, some of me boys didn't think you could beat the trolls."
	[/message]

	[message]
	speaker=Kaleh
	message= _ "Where did you come from?"
	[/message]

	[message]
	description=Grendel
	message= _ "Don't think you know all the tunnels and passages that twist through these caves, elf. I would have killed him myself, but I was just sent back from the main front of the battle."
	[/message]

	[message]
	speaker=Zhul
	message= _ "The front? I thought this was the front."
	[/message]

	[message]
	description=Grendel
	message= _ "While you were fighting a separate clan of trolls sneaked around our sentries and flanked us, attacking our supply depots. There are more of those stinking buggers than we had originally thought. To tell you the truth, we are hard pressed. We're going to have to pull back all our forces in these caves to reinforce the back lines. But your victory here has produced a unexpected opportunity."
	[/message]

	[message]
	speaker=Nym
	message= _ "It has?"
	[/message]

	[message]
	description=Grendel
	message= _ "Right before the trolls overran this area of the mines, our scouts had found an old tunnel south of here that leads almost straight to the main lair of this tribe of trolls. We believe that protected in the lair is one of their main leaders who is directing the battle. We were going to try to sneak in and lead a surprise attack, but frankly we didn't have enough dwarves to spare. If by using this passage you can sneak past their front lines and kill him, then it will throw the trolls into disarray and relieve the pressure on our front lines. If you do this our King has promised to help you return your people to the sunlit lands. When you're ready I'll show you the way. It's not far."
	[/message]

	[message]
	speaker=Elyssa
	message= _ "Their knowledge of these tunnels is uncanny. I could have sworn a minute ago that that wall was solid rock."
	[/message]

	[if]
		[have_unit]
		description=Cloaked Figure
		[/have_unit]

		[then]

		[message]
		description=Kaleh
		message= _ "Wait a moment Grendel, we must deal with this mysterious cloaked figure before we can follow you."
		[/message]

		[objectives]
			summary= _ "New Objectives:"
			show=yes
			[objective]
				description= _ "Defeat Cloaked Figure"
				condition=win
			[/objective]
			[objective]
				description= _ "Death of Kaleh, Nym or Zhul"
				condition=lose
			[/objective]
		[/objectives]

		[set_variable]
		name=delay_victory
		value=2
		[/set_variable]

		[/then]

		[else]

		[message]
		description=Kaleh
		message= _ "It sounds like our work is not yet done. Very well, gather yourselves together, we must follow Grendel."
		[/message]

		[endlevel]
		result=victory
		next_scenario=6_1_HuntingTrolls
		bonus=yes
		[/endlevel]

		[/else]
	[/if]



#enddef

#send in troll messenger to tell player to continue north

[event]
name=die

[filter]
description=Fundin
[/filter]

[message]
description=Fundin
message= _ "The rest is silence..."
[/message]

[kill]
description=Fundin
animate=yes
[/kill]

[set_variable]
name=dead_dwarf_leaders
add=1
[/set_variable]

[if]
	[variable]
	name=dead_dwarf_leaders
	numerical_equals=2
	[/variable]

	[then]

	[terrain]
	x=20,19,19,18,17
	y=14,15,16,16,17
	letter=Uu
	[/terrain]

	{UNIT_ (Troll Shaman) (Zurg) ( _ "Zurg") 2 20 14}

	[remove_shroud]
	x=19-23
	y=12-16
	side=1
	[/remove_shroud]

	[delay]
	time=200
	[/delay]

	{ZURG_CONVERSATION}

	[/then]
[/if]

[/event]

[event]
name=die

[filter]
description=Nori
[/filter]

[message]
description=Nori
message= _ "I go to my ancestors..."
[/message]

[kill]
description=Nori
animate=yes
[/kill]

[set_variable]
name=dead_dwarf_leaders
add=1
[/set_variable]

[if]
	[variable]
	name=dead_dwarf_leaders
	numerical_equals=2
	[/variable]

	[then]

	[terrain]
	x=51,52,53,54,55
	y=12,12,13,12,13
	letter=Uu
	[/terrain]

	{UNIT_ (Troll) (Zurg) ( _ "Zurg") 2 51 12}

	[remove_shroud]
	x=49-52
	y=10-13
	side=1
	[/remove_shroud]

	[delay]
	time=200
	[/delay]

	{ZURG_CONVERSATION}

	[/then]
[/if]

[/event]

#send in dwarf messenger to tell player to continue south

[event]
name=die

[filter]
description=Thungar
[/filter]

[message]
description=Thungar
message= _ "Arrggg!!!"
[/message]

[kill]
description=Thungar
animate=yes
[/kill]

[set_variable]
name=dead_troll_leaders
add=1
[/set_variable]

[if]
	[variable]
	name=dead_troll_leaders
	numerical_equals=2
	[/variable]

	[then]

	[terrain]
	x=52,53,53,52,52
	y=41,41,40,39,38
	letter=Uu
	[/terrain]

	{UNIT_ (Dwarvish Pathfinder) (Grendel) ( _ "Grendel") 5 52 41}

	[remove_shroud]
	x=46-53
	y=42-40
	side=1
	[/remove_shroud]

	[delay]
	time=200
	[/delay]

	{GRENDEL_CONVERSATION}

	[/then]
[/if]

[/event]

[event]
name=die

[filter]
description=Gnarl
[/filter]

[message]
description=Gnarl
message= _ "I will be avenged..."
[/message]

[kill]
description=Gnarl
animate=yes
[/kill]

[set_variable]
name=dead_troll_leaders
add=1
[/set_variable]

[if]
	[variable]
	name=dead_troll_leaders
	numerical_equals=2
	[/variable]

	[then]

	[terrain]
	x=21,20,19,18,17
	y=46,46,46,45,44
	letter=Uu
	[/terrain]

	[delay]
	time=200
	[/delay]

	{UNIT_ (Dwarvish Pathfinder) (Grendel) ( _ "Grendel") 5 21 46}

	[remove_shroud]
	x=20-26
	y=43-46
	side=1
	[/remove_shroud]

	{GRENDEL_CONVERSATION}

	[kill]
	description=Grendel
	animate=no
	[/kill]

	[terrain]
	x=21,20,19,18,17
	y=46,46,46,45,44
	letter=Xu
	[/terrain]

	[/then]
[/if]

[/event]


# Event 29: Return of the Assassin/Cloaked Figure (same guy, two names)

# This special macro places the cloaked figure and does the dialogue
# once I have found a hex with no cave wall or other obstruction

#define ASSASSIN_MACRO2 X_LOC Y_LOC

		[set_variable]
		name=found_empty_hex
		value=1
		[/set_variable]

		[store_locations]
		x={X_LOC}
		y={Y_LOC}
		[filter]
			[not]
			side=1
			[/not]
		[/filter]
		variable=temp
		[/store_locations]

		[set_variable]
		name=enemy_in_hex
		value=$temp.length
		[/set_variable]

		[set_variable]
		name=temp
		value=0
		[/set_variable]

		[store_locations]
		x={X_LOC}
		y={Y_LOC}
		[filter]
			side=1
		[/filter]
		variable=temp
		[/store_locations]

		[set_variable]
		name=ally_in_hex
		value=$temp.length
		[/set_variable]

		# if we found a ally or enemy in target hex,
		# then store and kill it

		[if]
			[or]
			[variable]
			name=enemy_in_hex
			numerical_equals=1
			[/variable]
			[/or]

			[or]
			[variable]
			name=ally_in_hex
			numerical_equals=1
			[/variable]
			[/or]

			[then]

			# kill/store occupant if there is one

			[store_unit]
			[filter]
				x={X_LOC}
				y={Y_LOC}
			[/filter]
			variable=unit_var
			[/store_unit]

			[kill]
			x,y={X_LOC},{Y_LOC}
			animate=no
			fire_event=no
			[/kill]

			[/then]
		[/if]

		# place assassin

		[unit]
			type=Dark Assassin2
			description=Cloaked Figure
			user_description= _ "Cloaked Figure"
			side=9
			x={X_LOC}
			y={Y_LOC}
			[modifications]
				{TRAIT_INTELLIGENT}
				{TRAIT_RESILIENT}
			[/modifications]
		[/unit]

		[message]
		description=Cloaked Figure
		image=portraits/cloaked.png
		message= _ "Did you think you escaped me Kaleh? I am your shadow, I will always be there until you pay for what you have done."
		[/message]

		[hide_unit]
		x,y={X_LOC},{Y_LOC}
		[/hide_unit]

		[redraw]
		[/redraw]

		{PLACE_IMAGE units/orcs/nightstalker-ranged-1.png {X_LOC} {Y_LOC}}

		[redraw]
		[/redraw]

		[delay]
		time=250
		[/delay]

		[removeitem]
		x={X_LOC}
		y={Y_LOC}
		[/removeitem]

		{PLACE_IMAGE units/orcs/nightstalker-ranged-2.png {X_LOC} {Y_LOC}}

		[redraw]
		[/redraw]

		[delay]
		time=250
		[/delay]

		[removeitem]
		x={X_LOC}
		y={Y_LOC}
		[/removeitem]

		[unhide_unit]
		[/unhide_unit]

		[store_unit]
		[filter]
			description=Kaleh
		[/filter]
		variable=unitstats
		[/store_unit]

		[set_variable]
		name=unitstats.status.slowed
		value="on"
		[/set_variable]

		[unstore_unit]
		variable=unitstats
		[/unstore_unit]

		{CLEAR_VARIABLE unitstats}

		[object]
			[filter]
				description=Kaleh
			[/filter]

			id=SlowKaleh
			silent=yes

			#ifdef EASY
			[effect]
				apply_to=hitpoints
				increase=-25%
			[/effect]
			#endif

			#ifdef MEDIUM
			[effect]
				apply_to=hitpoints
				increase=-33%
			[/effect]
			#endif

			#ifdef HARD
			[effect]
				apply_to=hitpoints
				increase=-50%
			[/effect]
			#endif
		[/object]

		[message]
		description=Kaleh
		message= _ "Ow!"
		[/message]

		[message]
		description=Cloaked Figure
		image=portraits/cloaked.png
		message= _ "You want to flee, don't you? But you cannot. They couldn't escape her either. Even death could not save them. She will devour us all. But first I shall have my revenge. Do the dance of death for me, Kaleh! Dance! Dance!"
		[/message]

#enddef

# Every turn check to see if the call_assassin flag was fired
# When it does, find the location of Kaleh and then find an adjacent
# hex the cloaked figure can pop up in. (any hex that is not cave wall,
# water, or chasm) (if a unit is there, then the cloaked figure just
# replaces it, and the unit reappears after the cloaked figure dies)

[event]
name=new turn
first_time_only=no

[if]
	[variable]
	name=call_assassin
	numerical_equals=1
	[/variable]

[then]

	[set_variable]
	name=call_assassin
	value=2
	[/set_variable]

# find location of Kaleh

[store_locations]
	[filter]
		description="Kaleh"
	[/filter]
	variable=kaleh_loc
[/store_locations]

[set_variable]
	name=kaleh_x
	to_variable=kaleh_loc[0].x
[/set_variable]

[set_variable]
	name=kaleh_y
	to_variable=kaleh_loc[0].y
[/set_variable]

{CLEAR_VARIABLE kaleh_loc}

# find adjacent hex that is not cave wall or chasm or water
# test these hexes
# when you find a hex that is valid, create cloaked figure there

# x+0 y+1

[if]
	[variable]
	name=found_empty_hex
	numerical_equals=0
	[/variable]

	[then]

	[set_variable]
	name=temp_x
	value=$kaleh_x
	[/set_variable]

	[set_variable]
	name=temp_y
	value=$kaleh_y
	[/set_variable]

	[set_variable]
	name=temp_y
	add=1
	[/set_variable]

	[store_locations]
		x=$temp_x
		y=$temp_y
		# 20070127 -- Mordante
		# removed ^ since unused and K since unused starting pos is Uu
		terrain=Uu, Re, Uu^Vu, Cud, Ce, Uh, Uu^Uf
		variable=hex_loc
	[/store_locations]

	[set_variable]
	name=temp
	value=$hex_loc.length
	[/set_variable]

	{CLEAR_VARIABLE hex_loc}

	[if]
		[variable]
		name=temp
		numerical_equals=1
		[/variable]

		[then]

		{ASSASSIN_MACRO2 $temp_x $temp_y}

		[/then]
	[/if]

	[/then]

[/if]


# x+1 y+0
[if]
	[variable]
	name=found_empty_hex
	numerical_equals=0
	[/variable]

	[then]

	[set_variable]
	name=temp_x
	value=$kaleh_x
	[/set_variable]

	[set_variable]
	name=temp_y
	value=$kaleh_y
	[/set_variable]

	[set_variable]
	name=temp_x
	add=1
	[/set_variable]

	[store_locations]
		x=$temp_x
		y=$temp_y
		# 20070127 -- Mordante
		# removed ^ since unused and K since unused starting pos is Uu
		terrain=Uu, Re, Uu^Vu, Cud, Ce, Uh, Uu^Uf
		variable=hex_loc
	[/store_locations]

	[set_variable]
	name=temp
	value=$hex_loc.length
	[/set_variable]

	{CLEAR_VARIABLE hex_loc}

	[if]
		[variable]
		name=temp
		numerical_equals=1
		[/variable]

		[then]

		{ASSASSIN_MACRO2 $temp_x $temp_y}

		[/then]
	[/if]

	[/then]
[/if]

# x+0 y-1

[if]
	[variable]
	name=found_empty_hex
	numerical_equals=0
	[/variable]

	[then]

	[set_variable]
	name=temp_x
	value=$kaleh_x
	[/set_variable]

	[set_variable]
	name=temp_y
	value=$kaleh_y
	[/set_variable]

	[set_variable]
	name=temp_y
	add=-1
	[/set_variable]

	[store_locations]
		x=$temp_x
		y=$temp_y
		# 20070127 -- Mordante
		# removed ^ since unused and K since unused starting pos is Uu
		terrain=Uu, Re, Uu^Vu, Cud, Ce, Uh, Uu^Uf
		variable=hex_loc
	[/store_locations]

	[set_variable]
	name=temp
	value=$hex_loc.length
	[/set_variable]

	{CLEAR_VARIABLE hex_loc}

	[if]
		[variable]
		name=temp
		numerical_equals=1
		[/variable]

		[then]

		{ASSASSIN_MACRO2 $temp_x $temp_y}

		[/then]
	[/if]

	[/then]
[/if]

# x-1 y+0

[if]
	[variable]
	name=found_empty_hex
	numerical_equals=0
	[/variable]

	[then]

	[set_variable]
	name=temp_x
	value=$kaleh_x
	[/set_variable]

	[set_variable]
	name=temp_y
	value=$kaleh_y
	[/set_variable]

	[set_variable]
	name=temp_x
	add=-1
	[/set_variable]

	[store_locations]
		x=$temp_x
		y=$temp_y
		# 20070127 -- Mordante
		# removed ^ since unused and K since unused starting pos is Uu
		terrain=Uu, Re, Uu^Vu, Cud, Ce, Uh, Uu^Uf
		variable=hex_loc
	[/store_locations]

	[set_variable]
	name=temp
	value=$hex_loc.length
	[/set_variable]

	{CLEAR_VARIABLE hex_loc}

	[if]
		[variable]
		name=temp
		numerical_equals=1
		[/variable]

		[then]

		{ASSASSIN_MACRO2 $temp_x $temp_y}

		[/then]
	[/if]

	[/then]
[/if]

# x+1 y+1

[if]
	[variable]
	name=found_empty_hex
	numerical_equals=0
	[/variable]

	[then]

	[set_variable]
	name=temp_x
	value=$kaleh_x
	[/set_variable]

	[set_variable]
	name=temp_y
	value=$kaleh_y
	[/set_variable]

	[set_variable]
	name=temp_x
	add=1
	[/set_variable]

	[set_variable]
	name=temp_y
	add=1
	[/set_variable]

	[store_locations]
		x=$temp_x
		y=$temp_y
		# 20070127 -- Mordante
		# removed ^ since unused and K since unused starting pos is Uu
		terrain=Uu, Re, Uu^Vu, Cud, Ce, Uh, Uu^Uf
		variable=hex_loc
	[/store_locations]

	[set_variable]
	name=temp
	value=$hex_loc.length
	[/set_variable]

	{CLEAR_VARIABLE hex_loc}

	[if]
		[variable]
		name=temp
		numerical_equals=1
		[/variable]

		[then]

		{ASSASSIN_MACRO2 $temp_x $temp_y}

		[/then]
	[/if]

	[/then]
[/if]

# x+1 y-1

[if]
	[variable]
	name=found_empty_hex
	numerical_equals=0
	[/variable]

	[then]

	[set_variable]
	name=temp_x
	value=$kaleh_x
	[/set_variable]

	[set_variable]
	name=temp_y
	value=$kaleh_y
	[/set_variable]

	[set_variable]
	name=temp_x
	add=1
	[/set_variable]

	[set_variable]
	name=temp_y
	add=-1
	[/set_variable]

	[store_locations]
		x=$temp_x
		y=$temp_y
		# 20070127 -- Mordante
		# removed ^ since unused and K since unused starting pos is Uu
		terrain=Uu, Re, Uu^Vu, Cud, Ce, Uh, Uu^Uf
		variable=hex_loc
	[/store_locations]

	[set_variable]
	name=temp
	value=$hex_loc.length
	[/set_variable]

	{CLEAR_VARIABLE hex_loc}

	[if]
		[variable]
		name=temp
		numerical_equals=1
		[/variable]

		[then]

		{ASSASSIN_MACRO2 $temp_x $temp_y}

		[/then]
	[/if]

	[/then]
[/if]

# x-1 y+1

[if]
	[variable]
	name=found_empty_hex
	numerical_equals=0
	[/variable]

	[then]

	[set_variable]
	name=temp_x
	value=$kaleh_x
	[/set_variable]

	[set_variable]
	name=temp_y
	value=$kaleh_y
	[/set_variable]

	[set_variable]
	name=temp_x
	add=-1
	[/set_variable]

	[set_variable]
	name=temp_y
	add=1
	[/set_variable]

	[store_locations]
		x=$temp_x
		y=$temp_y
		# 20070127 -- Mordante
		# removed ^ since unused and K since unused starting pos is Uu
		terrain=Uu, Re, Uu^Vu, Cud, Ce, Uh, Uu^Uf
		variable=hex_loc
	[/store_locations]

	[set_variable]
	name=temp
	value=$hex_loc.length
	[/set_variable]

	{CLEAR_VARIABLE hex_loc}

	[if]
		[variable]
		name=temp
		numerical_equals=1
		[/variable]

		[then]

		{ASSASSIN_MACRO2 $temp_x $temp_y}

		[/then]
	[/if]

	[/then]
[/if]

# x-1 y-1

[if]
	[variable]
	name=found_empty_hex
	numerical_equals=0
	[/variable]

	[then]

	[set_variable]
	name=temp_x
	value=$kaleh_x
	[/set_variable]

	[set_variable]
	name=temp_y
	value=$kaleh_y
	[/set_variable]

	[set_variable]
	name=temp_x
	add=-1
	[/set_variable]

	[set_variable]
	name=temp_y
	add=-1
	[/set_variable]

	[store_locations]
		x=$temp_x
		y=$temp_y
		# 20070127 -- Mordante
		# removed ^ since unused and K since unused starting pos is Uu
		terrain=Uu, Re, Uu^Vu, Cud, Ce, Uh, Uu^Uf
		variable=hex_loc
	[/store_locations]

	[set_variable]
	name=temp
	value=$hex_loc.length
	[/set_variable]

	{CLEAR_VARIABLE hex_loc}

	[if]
		[variable]
		name=temp
		numerical_equals=1
		[/variable]

		[then]

		{ASSASSIN_MACRO2 $temp_x $temp_y}

		[/then]
	[/if]

	[/then]
[/if]

	[/then]
[/if]

[/event]

# Each turn check to see if the cloaked figure is alive.
# If he is then keep Kaleh slowed.

[event]
name=new turn
first_time_only=no

	[if]
		[have_unit]
		description=Cloaked Figure
		[/have_unit]

		[then]
		[store_unit]
		[filter]
			description=Kaleh
		[/filter]
		variable=unitstats
		[/store_unit]

		[set_variable]
		name=unitstats.status.slowed
		value="on"
		[/set_variable]

		[unstore_unit]
		variable=unitstats
		[/unstore_unit]

		[/then]
	[/if]

[/event]


# When cloaked figure dies, return original occupant of hex
# if there was one. Also, if player has already killed both enemy
# leaders, then go to victory

[event]
name=die

[filter]
description=Cloaked Figure
[/filter]

[set_variable]
	name=unslow_kaleh
	value=1
[/set_variable]

[kill]
description=Cloaked Figure
animate=no
fire_event=no
[/kill]

[message]
description=Kaleh
message= _ "Where did he go? How does he disappear like that? And what in Uria's name was he ranting about? Whoever that is is starting to make me get edgy."
[/message]

#if there was an enemy unit in space that the cloaked figure appeared in
[if]

	[variable]
	name=enemy_in_hex
	numerical_equals=1
	[/variable]

	[then]

	#restore original occupant of hex

	[unstore_unit]
	variable=unit_var
	find_vacant=yes
	[/unstore_unit]

	[redraw]
	[/redraw]

	[message]
	description=$unit_var.description
	message= _ "Huh? What happened? No time to think, must keep fighting!"
	[/message]

	[/then]
[/if]

#if there was a friendly unit in space that the cloaked figure appeared in
[if]

	[variable]
	name=ally_in_hex
	numerical_equals=1
	[/variable]

	[then]

	#restore original occupant of hex

	[unstore_unit]
	variable=unit_var
	find_vacant=yes
	[/unstore_unit]

	[redraw]
	[/redraw]

	[message]
	description=$unit_var.description
	message= _ "Huh? What happened?"
	[/message]

	[message]
	description=Kaleh
	message= _ "Where did you go? Do you remember anything?"
	[/message]

	[message]
	description=$unit_var.description
	message= _ "No, someone grabbed me and knocked me out. I have no idea who it was."
	[/message]

	[/then]
[/if]

# if cloaked figure was delaying victory event from firing

[if]
	[variable]
	name=delay_victory
	numerical_equals=1
	[/variable]

	[then]

	[message]
		description=Kaleh
		message= _ "The cloaked figure is gone. Still, our work is not yet done. Gather yourselves together, we must follow Zurg."
	[/message]

	[endlevel]
	result=victory
	next_scenario=6_2_HuntingDwarves
	bonus=yes
	[/endlevel]

	[/then]
[/if]

[if]
	[variable]
	name=delay_victory
	numerical_equals=2
	[/variable]

	[then]

	[message]
		description=Kaleh
		message= _ "The cloaked figure is gone. Still, our work is not yet done. Gather yourselves together, we must follow Grendel."
	[/message]

	[endlevel]
	result=victory
	next_scenario=6_1_HuntingTrolls
	bonus=yes
	[/endlevel]

	[/then]
[/if]

[/event]

#at victory, clear variables:
[event]
name=victory

{CLEAR_VARIABLE i}
{CLEAR_VARIABLE number_enemies}
{CLEAR_VARIABLE battle_turn_counter}
{CLEAR_VARIABLE saw_cairn_or_totem}
{CLEAR_VARIABLE dead_dwarf_leaders}
{CLEAR_VARIABLE dead_troll_leaders}
{CLEAR_VARIABLE first_tunnel}
{CLEAR_VARIABLE bridge_blown}
{CLEAR_VARIABLE temp}
{CLEAR_VARIABLE tempx}
{CLEAR_VARIABLE tempy}
{CLEAR_VARIABLE tentacle_count}
{CLEAR_VARIABLE tentacle_deaths}
{CLEAR_VARIABLE tripped_bats}
{CLEAR_VARIABLE x_left}
{CLEAR_VARIABLE x_right}
{CLEAR_VARIABLE y_up}
{CLEAR_VARIABLE y_down}
{CLEAR_VARIABLE dwarf_door}
{CLEAR_VARIABLE dwarf_amulet}
{CLEAR_VARIABLE took_belt}

{CLEAR_VARIABLE lava_damage}
{CLEAR_VARIABLE heat_damage}
{CLEAR_VARIABLE summon_flame}
{CLEAR_VARIABLE flame_counter}
{CLEAR_VARIABLE troll_undead_arose}
{CLEAR_VARIABLE met_ghost}
{CLEAR_VARIABLE buried_dwarf}
{CLEAR_VARIABLE took_wand}
{CLEAR_VARIABLE entered_dwarf_lair}
{CLEAR_VARIABLE entered_troll_lair}

{CLEAR_VARIABLE assassin_turn}
{CLEAR_VARIABLE call_assassin}
{CLEAR_VARIABLE found_empty_hex}

{CLEAR_VARIABLE saw_interrogators}
{CLEAR_VARIABLE saw_sergeant}

{CLEAR_VARIABLE enemy_in_hex}
{CLEAR_VARIABLE ally_in_hex}
{CLEAR_VARIABLE unslow_kaleh}
{CLEAR_VARIABLE delay_victory}
{CLEAR_VARIABLE unitstats}

[/event]

# if player runs out of time, display time over message
[event]
name=time over

[message]
description=Kaleh
message= _ "Oh no, we took too long and enemy reinforcements have arrived. We'll surely be overwhelmed now!"
[/message]

[/event]

{@campaigns/Under_the_Burning_Suns/utils/deaths.cfg}

{@campaigns/Under_the_Burning_Suns/utils/global-events.cfg}
[/scenario]
