[scenario]
	id=14_The_Dwarven_Doors
	#textdomain wesnoth-httt
	name= _ "The Dwarven Doors"
	map_data="{campaigns/Heir_To_The_Throne/maps/Dwarven_Doors}"
	{SCENARIO_MUSIC "gameplay03.ogg"}
	{TURNS 26 20 15}
	victory_when_enemies_defeated=no

	{DUSK}
	{FIRST_WATCH}
	{SECOND_WATCH}
	{DAWN}
	{MORNING}
	{AFTERNOON}

	next_scenario=15_Plunging_into_the_Darkness

	{BIGMAP_DWARVEN_DOORS}

	[event]
	name=prestart
		[objectives]
		side=1
			[objective]
				description= _ "Move Konrad to entrance of the Dwarven Kingdom"
				condition=win
			[/objective]
			[objective]
				description= _ "Death of Konrad"
				condition=lose
			[/objective]
			[objective]
				description= _ "Death of Delfador"
				condition=lose
			[/objective]
			[objective]
				description= _ "Death of Kalenz"
				condition=lose
			[/objective]
		[/objectives]
	[/event]

	{PLACE_IMAGE misc/dwarven-doors.png 14 3}
	{PLACE_IMAGE misc/mine.png 25 2}

	[side]
	type=Commander
	description=Konrad
	user_description= _ "Konrad"
	side=1
	unrenamable=yes
	canrecruit=1
	controller=human
	[/side]

	[side]
	type=Orcish Warlord
	description=Knafa-Telfar
	user_description= _ "Knafa-Telfar"
	side=2
	canrecruit=1
	recruit=Troll Whelp,Orcish Grunt,Orcish Crossbowman
	{GOLD 100 120 140}
	{INCOME 14 18 22}
		[ai]
		{ATTACK_DEPTH 4 5 5}
		aggression=1.0
		grouping=no
		caution=-5.0
		[/ai]
	team_name=orcs
	[/side]

	[side]
	type=Orcish Warlord
	description=Urug-Tan
	user_description= _ "Urug-Tan"
	side=3
	canrecruit=1
	recruit=Troll Whelp,Orcish Grunt,Orcish Crossbowman
	{GOLD 75 95 115}
	{INCOME 12 14 18}
		[ai]
		{ATTACK_DEPTH 4 5 5}
		aggression=1.0
		grouping=no
		caution=-5.0
		[/ai]
	team_name=orcs
	[/side]

	[side]
	type=Orcish Warlord
	description=Shuuga-Mool
	user_description= _ "Shuuga-Mool"
	side=4
	canrecruit=1
	recruit=Orcish Grunt,Wolf Rider,Orcish Archer,Troll
	{GOLD 30 50 70}
	{INCOME 12 14 18}
	team_name=orcs
		[ai]
		{ATTACK_DEPTH 4 5 5}
		aggression=1.0
		grouping=no
		caution=-5.0
		[/ai]
	[/side]

	[side]
	side=5
	no_leader=yes
	controller=ai
	[/side]

	{STARTING_VILLAGES 2 20}
	{STARTING_VILLAGES 3 20}
	{STARTING_VILLAGES 4 20}
	[label]
	x=15
	y=21
	text= _ "Pillars of Thunedain"
	[/label]
	[label]
	x=14
	y=4
	text= _ "The Great Doors"
	[/label]

#define TALK_ABOUT_UNCLE
	[set_variable]
	name=uncle_smuggler
	value=1
	[/set_variable]
#enddef
#define LISAR_GUARD_DOORS
	{UNIT (Royal Guard) (Royal Guard) ( _ "Royal Guard") 2 8 44}
#enddef

	[event]
	name=prestart
	#
	# Randomly set the exit location - 1=the main door and 2=the mine
	#
	{VARIABLE_OP true_entrance_location random 1..2}
	[/event]

	[event]
	name=start
		[recall]
		description=Delfador
		[/recall]
		[recall]
		description=Kalenz
		[/recall]
		[role]
		type=Elvish Outrider,Elvish Rider,Elvish Scout,Lancer,Paladin,Grand Knight,Knight,Horseman
		role=Rider
		[/role]
		[recall]
		role=Rider
		[/recall]

		[redraw]
		[/redraw]
		[message]
		description=Delfador
		message= _ "At last, this is the entrance to the Dwarven Kingdoms."
		[/message]
		[message]
		description=Konrad
		message= _ "All I can see are ruins and poor villages."
		[/message]
		[message]
		description=Kalenz
		message= _ "The poor villagers that once lived here and traded with the dwarves are now held in slavery by the orcs."
		[/message]
		[message]
		description=Konrad
		message= _ "Slaves to the evil orcs? We must liberate them!"
		[/message]
		[message]
		description=Delfador
		message= _ "That would not be a wise choice, for our mission is to retrieve the Scepter of Fire. If we tarry in this place, hordes of orcs will surround us."
		[/message]
		[message]
		description=Kalenz
		message= _ "Konrad, heed the words of Delfador. We shall return to wrest the grip of the orcs from these lands. Look - orcs are already gathering. More are surely on their way."
		[/message]
		[message]
		description=Konrad
		message= _ "This does not please me, but I will listen to your advice."
		[/message]
		[message]
		role=Rider
		message= _ "Sir, I should like to scout out the path ahead. It would be disaster for us to find these doors sealed, leaving us as hunting fodder for the gathering horde."
		[/message]
		[message]
		description=Delfador
		message= _ "Excellent. We must find a way underground before we are overwhelmed."
		[/message]
		#
		# If you have outlaws in your party, you have the chance to find a special unit
		#
		[role]
		type=Footpad,Thug,Poacher,Outlaw,Trapper,Bandit
		role=Outlaw_Advisor
		[/role]
		[recall]
		role=Outlaw_Advisor
		[/recall]
		[redraw]
		[/redraw]
		[message]
		role=Outlaw_Advisor
		message= _ "My uncle used to smuggle... err... I mean... trade food for the dwarves. He could get grain carts in under the very noses of those ugly orcs."
		[/message]
		[message]
		role=Outlaw_Advisor
		message= _ "He must be hiding in one of those villages."
		[/message]
		[if]
			[have_unit]
			role=Outlaw_Advisor
			[/have_unit]
			[then]
			{TALK_ABOUT_UNCLE}
				[store_unit]
				variable=outlaw_advisor_store
					[filter]
					role=Outlaw_Advisor
					[/filter]
				[/store_unit]
				{VARIABLE outlaw_name $outlaw_advisor_store.user_description}
				{CLEAR_VARIABLE outlaw_advisor_store}
			[/then]
		[/if]
	[/event]

#
# Some colorful talking events to make the scenario more interesting
#

	[event]
	name=turn 2
		[message]
		description=Konrad
		message= _ "The defense of the dwarves must have been strong. Look at those ruined towers!"
		[/message]
		[message]
		description=Delfador
		message= _ "The battle outside was fierce and lasted a full half-year. But, the battles inside the tunnels were worse."
		[/message]
	[/event]

	[event]
	name=moveto
		[filter]
		description=Delfador
		x=11-19
		y=19-22
		[/filter]
		[message]
		description=Delfador
		message= _ "The pillars of Thunedain. He was a legendary dwarvish lord who made his last stand here. May we triumph where he fell."
		[/message]
		[message]
		description=Kalenz
		message= _ "I would settle for escape, though I know not which I dread more: foul orcs or fetid caves."
		[/message]
	[/event]

	[event]
	name=die
		[filter_second]
		description=Haldiel
		[/filter_second]
		[message]
		speaker=second_unit
		message= _ "Back to the abyss, spawn of filth!"
		[/message]
	[/event]

	[event]
	name=moveto
	first_time_only=no
		[filter]
		description=Uncle Somf
		[/filter]
			[store_unit]
			variable=outlaw_advisor_store
				[filter]
				role=Outlaw_Advisor
				[/filter]
			[/store_unit]

		# proximity check

		[store_locations]
		x=$x1
		y=$y1
		variable=uncle_check_loc
		radius=1
		[/store_locations]

		# The unit we want to be next to

		[store_unit]
		variable=outlaw_advisor_store
			[filter]
			role=Outlaw_Advisor
			[/filter]
		[/store_unit]


		# Iterate over each location checking to see if the outlaw target is standing next to Uncle Somf

		{FOREACH uncle_check_loc i}
			{VARIABLE_OP temp_x format $uncle_check_loc[$i].x}
			{VARIABLE_OP temp_x multiply -1}
			{VARIABLE_OP temp_x add $outlaw_advisor_store.x}
			{VARIABLE_OP temp_y format $uncle_check_loc[$i].y}
			{VARIABLE_OP temp_y multiply -1}
			{VARIABLE_OP temp_y add $outlaw_advisor_store.y}
			[if]
			[variable]
			name=temp_x
			numerical_equals=0
			[/variable]
			[then]
				[if]
				[variable]
				name=temp_y
				numerical_equals=0
				[/variable]
				[then]
					[if]
					[variable]
					name=outlaw_repartee
					numerical_equals=0
					[/variable]
					[then]
						{VARIABLE_OP message_text format (_ "How are we doing, $outlaw_name|?")}
						[message]
						description=Uncle Somf
						message=$message_text
						[/message]
						[message]
						description=$outlaw_name
						message= _ "The same as always, Uncle!"
						[/message]
						[message]
						description=Uncle Somf
						message= _ "That bad, huh?"
						[/message]
						{VARIABLE outlaw_repartee 1}
					[/then]
					[/if]
				[/then]
				[/if]
			[/then]
			[/if]
		{NEXT i}
	{CLEAR_VARIABLE temp_x}
	{CLEAR_VARIABLE temp_y}
	{CLEAR_VARIABLE outlaw_advisor_store}
	{CLEAR_VARIABLE uncle_check_loc}

	[/event]

#
# Finding the bandit uncle who gives you a big hint about the true exit
#
	[event]
		name=moveto
		[filter]
			side=1
			x=18
			y=24
		[/filter]
		[if]
			[variable]
			name=uncle_smuggler
			numerical_equals=1
			[/variable]
		[then]

			{UNIT (Bandit) (Uncle Somf) ( _ "Uncle Somf") 1 18 24}
			[redraw]
			[/redraw]
			#
			# The bandit's speech depends on who finds him
			#
			[store_unit]
			variable=outlaw_advisor_store
				[filter]
				role=Outlaw_Advisor
				[/filter]
			[/store_unit]
			[if]
				[variable]
				name=outlaw_advisor_store.x
				numerical_equals=18
				[/variable]
			[then]
				[if]
					[variable]
					name=outlaw_advisor_store.y
					numerical_equals=24
					[/variable]
				[then]
					{VARIABLE_OP message_text format (_ "$outlaw_name|! How have you been? I haven't seen you in years.")}
				[/then]
				[/if]
			[/then]
			[else]
				{VARIABLE_OP message_text format (_ "Haw! Any friend of $outlaw_name is a friend of mine too.")}
			[/else]
			[/if]

			[message]
			speaker=unit
			message= _ "Who... who's here?"
			[/message]
			[message]
				description=Uncle Somf
				message=$message_text
			[/message]
			{CLEAR_VARIABLE message_text}
			{CLEAR_VARIABLE outlaw_advisor_store}
			[message]
				description=Konrad
				message= _ "We need to make it into the caves of the dwarves."
			[/message]
			#
			# The bandit divulges the true location of the entrance
			#
			[if]
				[variable]
				name=true_entrance_location
				numerical_equals=1
				[/variable]
				[then]
					[message]
						description=Uncle Somf
						message= _ "The mine entrances were all collapsed intentionally during the fighting. The doors, while heavily defended, remain accessible. The orcish hordes that assault them are repulsed, but you may be able to sneak in unnoticed."
					[/message]
				[/then]
				[else]
					[message]
						description=Uncle Somf
						message= _ "The best way is through the mine tunnels. The orcs have never found all the mine entrances, and many still lead deep underground."
					[/message]
				[/else]
			[/if]

			[set_variable]
				name=uncle_smuggler
				value=2
			[/set_variable]
		[/then]
		[/if]
	[/event]

	#
	# When any side 1 unit reaches a possible exit, we find out if it is the correct
	# one. We don't know who will reach the exit, so the dialog must be general for
	# anyone. If the unit is Konrad, the victory event is handled separately.
	#
	[event]
	name=moveto
	first_time_only=yes
		[filter]
		side=1
		x=14
		y=3
		[/filter]
		#
		# If true_entrance_location = 1 then this is the true entrance
		#
		[if]
			[variable]
			name=true_entrance_location
			numerical_equals=1
			[/variable]
			[then]
				[message]
				speaker=unit
				message= _ "The doors... they can be moved!"
				[/message]
				[message]
				speaker=Delfador
				message= _ "Quickly, now, let us slip inside and hope the dwarves do not object..."
				[/message]
				[message]
				description=Kalenz
				message= _ "... and that the orcs do not follow."
				[/message]
			[/then]
			[else]
				[message]
					speaker=unit
					message= _ "The doors are closed and barred from the inside!"
				[/message]
				[message]
					description=Konrad
					message= _ "We can't get in! What should we do now?"
				[/message]
				[message]
					description=Kalenz
					message= _ "It is said that the orcs used old mine tunnels to surprise the dwarves. There appears to be one nearby, to the north-east."
				[/message]
				[message]
					description=Konrad
					message= _ "Then we must make it to that tunnel!"
				[/message]
			[/else]
		[/if]
	[/event]

	[event]
		name=moveto
		[filter]
			side=1
			x=25
			y=2
		[/filter]
		#
		# If true_entrance_location = 2 then this is the true entrance
		#
		[if]
			[variable]
			name=true_entrance_location
			numerical_equals=2
			[/variable]
			[then]
				[message]
				speaker=unit
				message= _ "This old mine seems to be connected to the main tunnels."
				[/message]
				[message]
				description=Kalenz
				message= _ "I am hesitant to enter. It will be so difficult in the darkness!"
				[/message]
				[message]
				description=Delfador
				message= _ "There is no time for idle chatter or delay of any kind. Onward!"
				[/message]
			[/then]
			[else]
				[message]
				speaker=unit
				message= _ "I have reached the mine entrance, but there is no tunnel here. Rock and rubble completely block the way."
				[/message]
				[message]
				description=Konrad
				message= _ "I hope we can make it through the main doors, then."
				[/message]
				[message]
				description=Delfador
				message= _ "It is our only choice now. Hurry!"
				[/message]
			[/else]
		[/if]
	[/event]

	#
	# These events end the level when Konrad makes it to the correct location.
	# This has to be a separate event from when any side 1 unit first reaches
	# a possible exit (the above events).
	#
	[event]
	name=moveto
	first_time_only=no
		[filter]
		description=Konrad
		x=14
		y=3
		[/filter]
		[if]
			[variable]
			name=true_entrance_location
			numerical_equals=1
			[/variable]
			[then]
				[message]
				description=Konrad
				message= _ "Pray that we live to see sunlight again."
				[/message]
				[set_variable]
				name=killed_enemies
				value=false
				[/set_variable]
				[endlevel]
				result=victory
				bonus=yes
				[/endlevel]
			[/then]
		[/if]
	[/event]
	[event]
	name=moveto
	first_time_only=no
		[filter]
		description=Konrad
		x=25
		y=2
		[/filter]
		[if]
			[variable]
			name=true_entrance_location
			numerical_equals=2
			[/variable]
			[then]
				[message]
				description=Konrad
				message= _ "Pray that we live to see sunlight again."
				[/message]
				[set_variable]
				name=killed_enemies
				value=false
				[/set_variable]
				[endlevel]
				result=victory
				bonus=yes
				[/endlevel]
			[/then]
		[/if]
	[/event]


	[event]
	name=enemies defeated
		[message]
		description=Kalenz
		message= _ "We have defeated the foul orcs guarding this land, but we must continue without rest. More will surely come!"
		[/message]
		[if]
			[variable]
			name=true_entrance_location
			numerical_equals=1
			[/variable]
			[then]
				[message]
				description=Delfador
				message= _ "Indeed we must not delay. Let us breach the great doors to the dwarven kingdom."
				[/message]
			[/then]
			[else]
				[message]
				description=Delfador
				message= _ "Indeed we must not delay. The mines in the northeast are the best way to enter."
				[/message]
			[/else]
		[/if]
	[/event]

	[event]
		name=moveto
		[filter]
			side=1
			x=10-15
			y=10-15
		[/filter]

		{UNIT (Cuttle Fish) (Cuttle Fish) ( _ "Cuttle Fish") 5 13 13}

		[message]
			description=Cuttle Fish
			message= _ "Ruarrrrr!!!"
		[/message]
		[message]
			speaker=unit
			message= _ "A monster was hiding in that lake!"
		[/message]
		[role]
		type=Thief,Rogue,Mage,Elvish Shaman,Elvish Druid,Elvish Archer,Elvish Fighter,Elvish Captain,Elvish Marshal,Horseman,Elvish Lord
		role=whiner
		[/role]
		[message]
			role=whiner
			message= _ "The legend was true! There are always tentacled creatures hiding in the lakes near the Dwarven Kingdoms."
		[/message]
	[/event]

	[event]
	name=victory

		[message]
		speaker=narrator
		image="wesnoth-icon.png"
		message= _ "But Konrad's party was not alone in entering the caves..."
		[/message]

		{UNIT (Princess) (Li'sar) ( _ "Li'sar") 2 8 44}
		{LISAR_GUARD_DOORS}
		{LISAR_GUARD_DOORS}
		{LISAR_GUARD_DOORS}
		{LISAR_GUARD_DOORS}
		{LISAR_GUARD_DOORS}
		{LISAR_GUARD_DOORS}
		[message]
		description=Li'sar
		message= _ "Whew! We make our way through the dangerous fog of the mountains, and now there is all this chaos before us! Come on, men! We must make it to the caves that lie ahead of us!"
		[/message]
		{CLEAR_VARIABLE true_entrance_location}
		{CLEAR_VARIABLE outlaw_name}
		{CLEAR_VARIABLE outlaw_repartee}
		{CLEAR_VARIABLE message_text}
	[/event]

	{campaigns/Heir_To_The_Throne/utils/deaths.cfg}

[/scenario]
