[scenario]
#textdomain wesnoth-sof
name= _ "Caverns of Flame"
id=9_Caverns_of_Flame
map_data="{@campaigns/Sceptre_of_Fire/maps/9_Caverns_of_Flame.map}"
next_scenario=Epilogue
turns=-1
victory_when_enemies_defeated=no

	{DEFAULT_MUSIC_PLAYLIST}

	{UNDERGROUND}

	[side]
	type=Dwarvish Fighter
	description=Rugnur
	side=1
	canrecruit=1
	recruit=
	controller=human
	shroud=yes
	[/side]

	[side]
	no_leader=yes
	side=2
		[ai]
			[target]
			side=7
			value=100
			[/target]
			[target]
			side=1
			value=5
			[/target]
			[target]
			side=3,4,5,6
			value=0.5
			[/target]
		grouping=none
		aggression=1.0
		[/ai]

	[/side]

	[side]
	type=Orcish Warlord
	side=3
	canrecruit=1
	recruit=Orcish Warrior,Troll Warrior
	{GOLD 200 220 240}
	{INCOME 20 25 30}
		[ai]
		recruitment_pattern=fighter
			[target]
			side=1
			value=10
			[/target]
		[/ai]
	team_name=orc
	[/side]

	[side]
	type=Orcish Warlord
	side=4
	canrecruit=1
	recruit=Orcish Warrior,Troll Warrior
	{GOLD 200 220 240}
	{INCOME 20 25 30}
		[ai]
		recruitment_pattern=fighter
			[target]
			side=1
			value=10
			[/target]
		[/ai]
	team_name=orc
	[/side]

	[side]
	type=Troll Warrior
	side=5
	canrecruit=1
	recruit=Orcish Warrior,Troll
	{GOLD 150 200 250}
	{INCOME 5 6 7}
		[ai]
		recruitment_pattern=fighter
			[target]
			side=1
			value=10
			[/target]
			[target]
			side=2
			value=1
			[/target]
		[/ai]
	team_name=orc
	[/side]

	[side]
	type=Troll Warrior
	side=6
	canrecruit=1
	controller=ai
	recruit=Orcish Warrior,Troll
	{GOLD 150 200 250}
	{INCOME 5 6 7}
		[ai]
		recruitment_pattern=fighter
			[target]
			side=1
			value=10
			[/target]
			[target]
			side=2
			value=1
			[/target]
		[/ai]
	team_name=orc
	[/side]

	#the dummy is on side 7
	[side]
	side=7
	no_leader=yes
	[/side]

	[story]
		[part]
		story= _ "There was no exit from the caverns of Knalga. They had reached the realms of the orcs, with the elves hot on their trail. And there was no exit from those caves."
		[/part]
		[part]
		story= _ "So the dwarves had no way out. They could not leave the caverns that they had entered."
		[/part]
		[part]
		background=maps/wesnoth.png
		show_title=yes
		{DOT 335 141}
		{DOT 350 123}
		{DOT 338 110}
		{CROSS 323 108}
		[/part]
	[/story]

	{SOF_DEATHS}

	[event]
	name=prestart
		[objectives]
		side=1
			[objective]
			description= _ "Explore as much of the cave as possible"
			condition=win
			[/objective]
			[objective]
			description= _ "Death of Rugnur"
			condition=lose
			[/objective]
			[objective]
			description= _ "Death of Krawg"
			condition=lose
			[/objective]
			[objective]
			description= _ "Death of Thursagan"
			condition=lose
			[/objective]
		[/objectives]
	[/event]

	[event]
	name=start
		#recall heroes and recall 14 others with a DARN ANNOYING MACRO
		{HEROMACRO Rugnur}
		{HEROMACRO Baglur}
		{HEROMACRO Krawg}
		{HEROMACRO Thursagan}
		{ROLEMACRO 1}
		{ROLEMACRO 2}
		{ROLEMACRO 3}
		{ROLEMACRO 4}
		{ROLEMACRO 5}
		{ROLEMACRO 6}
		{ROLEMACRO 7}
		{ROLEMACRO 8}
		{ROLEMACRO 9}
		{ROLEMACRO 10}
		{ROLEMACRO 11}
		{ROLEMACRO 12}
		[unstore_unit]
		variable=tempRugnur
		[/unstore_unit]
		[clear_variable]
		name=tempRugnur
		[/clear_variable]
		[redraw]
		[/redraw]
		{RECALLMACRO Baglur}
		{RECALLMACRO Krawg}
		{RECALLMACRO Thursagan}
		{RECALLMACRO 1}
		{RECALLMACRO 2}
		{RECALLMACRO 3}
		{RECALLMACRO 4}
		{RECALLMACRO 5}
		{RECALLMACRO 6}
		{RECALLMACRO 7}
		{RECALLMACRO 8}
		{RECALLMACRO 9}
		{RECALLMACRO 10}
		{RECALLMACRO 11}
		{RECALLMACRO 12}
		#remove the keep
		[terrain]
		x,y=15,4
		letter=Uu
		[/terrain]
		#orcs/trolls start out
		[unit]
		x,y=17,21
		side=5
		type=Troll
		ai_special=guardian
		description=Guardian
		user_description= _ "Guardian"
		[/unit]
		[unit]
		x,y=15,24
		side=5
		type=Orcish Warrior
		ai_special=guardian
		description=Guardian
		user_description= _ "Guardian"
		[/unit]
		[unit]
		x,y=16,25
		side=5
		type=Orcish Warrior
		ai_special=guardian
		description=Guardian
		user_description= _ "Guardian"
		[/unit]
		[unit]
		x,y=19,21
		side=6
		type=Troll
		ai_special=guardian
		description=Guardian
		user_description= _ "Guardian"
		[/unit]
		[unit]
		x,y=21,24
		side=6
		type=Orcish Warrior
		ai_special=guardian
		description=Guardian
		user_description= _ "Guardian"
		[/unit]
		[unit]
		x,y=20,25
		side=6
		type=Orcish Warrior
		ai_special=guardian
		description=Guardian
		user_description= _ "Guardian"
		[/unit]
		#messages
		[message]
		description=Rugnur
		message= _ "Well, I think we've gotten a good distance ahead of the elves. What do we do now?"
		[/message]
		[message]
		description=Thursagan
		message= _ "We should keep going. Remember what Khrakrahs said, about this being a volcano? I think we should try to cause it to erupt. It will kill all the elves, and we might be able to find a safe place so the lava doesn't kill us."
		[/message]
		[message]
		description=Rugnur
		message= _ "This sounds... attemptable."
		[/message]
		[message]
		description=Baglur
		message= _ "Yes, an interesting plan. How do you propose we do this?"
		[/message]
		[message]
		description=Thursagan
		message= _ "I have a suspicion that there is an opening somewhere near here than leads to the surface. Somewhere near there, there is probably a magic object of some kind that will cause the lava to flow."
		[/message]
		[message]
		description=Rugnur
		message= _ "Very well. We will continue to explore until we find it."
		[/message]
		[message]
		description=Krawg
		# Krawg smells orcs in these caves
		message= _ "Krawg sme' o'cz in iz 'ave!"
		[/message]
		[message]
		description=Thursagan
		message= _ "Indeed, there may be orcs lurking here. We should beware of them."
		[/message]
		[set_variable]
		name=overlayturn
		value=0
		[/set_variable]
	[/event]

#define RANDOM_MERCENARY_2 TYPE
{RANDOM_TRAIT_UNIT (
type={TYPE}
side=2
x,y=15,4
generate_description=yes)}
#enddef

	#elves come
	[event]
	name=turn 4
		[move_unit_fake]
		type=Elvish Marshal
		x=15
		y=1,4
		[/move_unit_fake]
		[unit]
		type=Elvish Marshal
		side=2
		description=Aendan
		user_description= _ "Aendan"
		canrecruit=1
		x,y=15,4
		[/unit]
		[message]
		description=Aendan
		message= _ "We have caught up to the dwarves. Now, prepare to destroy them!"
		[/message]
		{RANDOM_MERCENARY_2 (Dwarvish Lord)}
		{RANDOM_MERCENARY_2 (Dwarvish Lord)}
		{RANDOM_MERCENARY_2 (Dwarvish Lord)}
		{RANDOM_MERCENARY_2 (Dwarvish Lord)}
#ifdef HARD
		{RANDOM_MERCENARY_2 (Dwarvish Lord)}
#endif
		{RANDOM_MERCENARY_2 (Dwarvish Dragonguard)}
		{RANDOM_MERCENARY_2 (Dwarvish Dragonguard)}
		{RANDOM_MERCENARY_2 (Dwarvish Dragonguard)}
		{RANDOM_MERCENARY_2 (Dwarvish Dragonguard)}
#ifdef HARD
		{RANDOM_MERCENARY_2 (Dwarvish Dragonguard)}
#endif
		{RANDOM_MERCENARY_2 (Elvish Champion)}
		{RANDOM_MERCENARY_2 (Elvish Champion)}
		{RANDOM_MERCENARY_2 (Elvish Champion)}
		{RANDOM_MERCENARY_2 (Elvish Champion)}
#ifdef HARD
		{RANDOM_MERCENARY_2 (Elvish Champion)}
#endif
		{RANDOM_MERCENARY_2 (Elvish Avenger)}
		{RANDOM_MERCENARY_2 (Elvish Avenger)}
		{RANDOM_MERCENARY_2 (Elvish Avenger)}
		{RANDOM_MERCENARY_2 (Elvish Avenger)}
#ifdef HARD
		{RANDOM_MERCENARY_2 (Elvish Avenger)}
#endif
		{RANDOM_MERCENARY_2 (Elvish Outrider)}
		{RANDOM_MERCENARY_2 (Elvish Outrider)}
		{RANDOM_MERCENARY_2 (Elvish Outrider)}
		{RANDOM_MERCENARY_2 (Elvish Outrider)}
#ifdef HARD
		{RANDOM_MERCENARY_2 (Elvish Outrider)}
#endif
		[message]
		description=Rugnur
		message= _ "Thursagan! The elves are right on our tail; what should we do?"
		[/message]
		[message]
		description=Thursagan
		message= _ "We could try to set off the volcano as soon as possible. Meanwhile, we should draw the elves further into the caves, so they can't escape when it does erupt."
		[/message]
	[/event]

	#when the elves lose troops, the orcs gain them
	[event]
	name=die
	first_time_only=no
		[filter]
		side=2
		[/filter]
		[gold]
		side=3
		amount=50
		[/gold]
		[gold]
		side=4
		amount=50
		[/gold]
	[/event]


	######NOTICE: I don't know why, but sighted events that trigger upon a human move no longer work (if they ever did). So consider this event broken. The following one is broken too, but doesn't cause as many problems, so it will stay in for now.
	#seeing the trapped orcs
#	[event]
#	name=sighted
#		[filter]
#		side=1
#		[/filter]
#		[filter_second]
#		side=3,4
#		[/filter_second]
#		{DEBUG_MSG blah}
#		[if]
#			[variable]
#			name=overlayturn
#			equals=0
#			[/variable]
#			[then]
#				[message]
#				description=Rugnur
#				message= _ "Look, those orcs can't get to us! There's cave wall in the way!"
#				[/message]
#				[message]
#				description=Thursagan
#				message= _ "True; but, perhaps later they will find a way out."
#				[/message]
#			[/then]
#		[/if]
#	[/event]

	#seeing the guards of the glyph
	[event]
	name=sighted
		[filter_second]
		description=Guardian
		side=5,6
		[/filter_second]
		[message]
		description=Rugnur
		message= _ "These orcs appear to be guarding something."
		[/message]
		[message]
		description=Thursagan
		message= _ "It is probably the magical object. Attack them!"
		[/message]
		[objectives]
		side=1
			[objective]
			description= _ "Move onto the glyph in the center of the volcano"
			condition=win
			[/objective]
			[objective]
			description= _ "Death of Rugnur"
			condition=lose
			[/objective]
			[objective]
			description= _ "Death of Krawg"
			condition=lose
			[/objective]
			[objective]
			description= _ "Death of Thursagan"
			condition=lose
			[/objective]
		[/objectives]
	[/event]

#define CHANGE_MAP TURN
[if]
	[variable]
	name=overlayturn
	numerical_equals={TURN}
	[/variable]
	[then]
		[terrain_mask]
		x,y=1,1
		mask="{@campaigns/Sceptre_of_Fire/maps/9_overlay/9_o{TURN}.map}"
			[rule]
			old=uoD3456'R[]
			new=l
			terrain=Rr,Ds
			[/rule]
			[rule]
			old=ElW
			new=l
			terrain=Chw,Ql
			[/rule]
			[rule]
			use_old=yes
			[/rule]
		[/terrain_mask]
	[/then]
[/if]
#enddef

	#the glyph
	[item]
	x,y=18,23
	image=scenery/rune1.png
	[/item]

	#triggering the volcano
	[event]
	name=moveto
		[filter]
		x,y=18,23
		side=1
		[/filter]
		{TREMOR}
		{TREMOR}
		[message]
		description=Thursagan
		message= _ "That's it! The volcano will erupt soon now that you've triggered it."
		[/message]
		[terrain]
		x=12,19
		y=4-5,4-5
		letter=Uu
		[/terrain]
		{TREMOR}
		{TREMOR}
		[message]
		description=Rugnur
		message= _ "Look, it has knocked down some of the walls! Those orcs have a way out now!"
		[/message]
		[scroll_to]
		x,y=14,1
		[/scroll_to]
		{TREMOR}
		{TREMOR}
		[message]
		description=Baglur
		message= _ "Well, we've succeeded in stopping the elves from getting the sceptre, I would say. But what do we do now?!"
		[/message]
		{TREMOR}
		{TREMOR}
		[message]
		description=Thursagan
		message= _ "Now, we must fight the orcs until we die, it's our only option."
		[/message]
		{TREMOR}
		{TREMOR}
		[message]
		description=Rugnur
		message= _ "We cannot let any of the elves or their mercenaries escape; they would tell where the sceptre is. Make sure they all die!"
		[/message]
		{TREMOR}
		{TREMOR}
		[message]
		description=Thursagan
		message= _ "We have to kill them quickly, before they realize what is happening. And remember, the lava spreads quickly, suddenly, and dangerously. The only place safe from it is the area near the glyph."
		[/message]
		{TREMOR}
		{TREMOR}
		[set_variable]
		name=overlayturn
		value=0
		[/set_variable]
		[event]
		name=new turn
		first_time_only=no
			#a turn counter that starts when you set off the volcano
			[set_variable]
			name=overlayturn
			add=1
			[/set_variable]
			#the elves try to escape 5 turns after you set off the volcano
			[if]
				[variable]
				name=overlayturn
				numerical_equals=5
				[/variable]
				[then]
					[role]
					role=smartelf
					side=2
					type=Elvish Marshal,Elvish Avenger,Elvish Champion,Elvish Outrider,Dwarvish Lord,Dwarvish Dragonguard
					[/role]
					{TREMOR}
					{TREMOR}
					[message]
					role=smartelf
					message= _ "Wait, what's going on? The volcano is going to erupt! Everyone, try to get out of these caves!"
					[/message]
					[unit]
					x,y=12,1
					side=7
					type=Galleon
					ai_special=guardian
					[/unit]
					#once they try to escape, if they move to the top 2 rows you lose
					[event]
					name=moveto
						[filter]
						x=14-16
						y=1-2
						side=2
						[/filter]
						[message]
						speaker=unit
						message= _ "I must go report to my superiors!"
						[/message]
						[message]
						description=Thursagan
						image=portraits/thursagan.png
						message= _ "We have allowed an enemy to escape; we have failed."
						[/message]
						[endlevel]
						result=defeat
						[/endlevel]
					[/event]
				[/then]
			[/if]
			#the lava is spreading and killing things every turn
			{TREMOR}
			{TREMOR}
			{CHANGE_MAP 1}
			{CHANGE_MAP 2}
			{CHANGE_MAP 3}
			{CHANGE_MAP 4}
			{CHANGE_MAP 5}
			{CHANGE_MAP 6}
			{CHANGE_MAP 7}
			[store_locations]
				[filter]
					[not]
					type=Gryphon,Gryphon Rider,Gryphon Master
					[/not]
				side=1,2,3,4,5,6
				[/filter]
			x=1-32
			y=1-32
			terrain=Chw,Ql
			variable=lavadead
			[/store_locations]
			{FOREACH_FORMAT lavadead i}
				[kill]
				x=$lavadead_i_x
				y=$lavadead_i_y
				animate=yes
				fire_event=yes
				[/kill]
			{NEXT i}
			[clear_variable]
			name=lavadead
			[/clear_variable]
			#after 7 turns you win
			[if]
				[variable]
				name=overlayturn
				numerical_equals=7
				[/variable]
				[then]
					{TREMOR}
					{TREMOR}
					[role]
					role=dumbelf
					side=2
					type=Elvish Marshal,Elvish Avenger,Elvish Champion,Elvish Outrider,Dwarvish Lord,Dwarvish Dragonguard
					[/role]
					[message]
					role=dumbelf
					message= _ "Aghh!!! The volcano is erupting, and now our exit has been blocked off!!!"
					[/message]
					[endlevel]
					result=victory
					bonus=no
					[/endlevel]
				[/then]
			[/if]
		[/event]
		#when all the elves die, you win
		[event]
		name=die
		first_time_only=no
			[filter]
			side=2
			[/filter]
			[if]
				[have_unit]
				side=2
				[/have_unit]
				[then]
				[/then]
				[else]
					[message]
					description=Rugnur
					message= _ "We have hidden the sceptre well, but now the volcano is going to erupt and kill us all."
					[/message]
					#all the lava flow comes immediately
					#lava 2
					[terrain_mask]
					x,y=1,1
					mask="{@campaigns/Sceptre_of_Fire/maps/9_overlay/9_o2.map}"
					[/terrain_mask]
					[store_locations]
						[filter]
							[not]
							type=Gryphon,Gryphon Rider,Gryphon Master
							[/not]
						side=1,2,3,4,5,6
						[/filter]
					x=1-32
					y=1-32
					terrain=Chw,Ql
					variable=lavadead
					[/store_locations]
					{FOREACH_FORMAT lavadead i}
						[kill]
						x=$lavadead_i_x
						y=$lavadead_i_y
						animate=yes
						[/kill]
					{NEXT i}
					#lava 4
					[terrain_mask]
					x,y=1,1
					mask="{@campaigns/Sceptre_of_Fire/maps/9_overlay/9_o4.map}"
					[/terrain_mask]
					[store_locations]
						[filter]
							[not]
							type=Gryphon,Gryphon Rider,Gryphon Master
							[/not]
						side=1,2,3,4,5,6
						[/filter]
					x=1-32
					y=1-32
					terrain=Chw,Ql
					variable=lavadead
					[/store_locations]
					{FOREACH_FORMAT lavadead i}
						[kill]
						x=$lavadead_i_x
						y=$lavadead_i_y
						animate=yes
						[/kill]
					{NEXT i}
					#lava 5
					[terrain_mask]
					x,y=1,1
					mask="{@campaigns/Sceptre_of_Fire/maps/9_overlay/9_o5.map}"
					[/terrain_mask]
					[store_locations]
						[filter]
							[not]
							type=Gryphon,Gryphon Rider,Gryphon Master
							[/not]
						side=1,2,3,4,5,6
						[/filter]
					x=1-32
					y=1-32
					terrain=Chw,Ql
					variable=lavadead
					[/store_locations]
					{FOREACH_FORMAT lavadead i}
						[kill]
						x=$lavadead_i_x
						y=$lavadead_i_y
						animate=yes
						[/kill]
					{NEXT i}
					#lava 6
					[terrain_mask]
					x,y=1,1
					mask="{@campaigns/Sceptre_of_Fire/maps/9_overlay/9_o6.map}"
					[/terrain_mask]
					[store_locations]
						[filter]
							[not]
							type=Gryphon,Gryphon Rider,Gryphon Master
							[/not]
						side=1,2,3,4,5,6
						[/filter]
					x=1-32
					y=1-32
					terrain=Chw,Ql
					variable=lavadead
					[/store_locations]
					{FOREACH_FORMAT lavadead i}
						[kill]
						x=$lavadead_i_x
						y=$lavadead_i_y
						animate=yes
						[/kill]
					{NEXT i}
					#lava 7
					[terrain_mask]
					x,y=1,1
					mask="{@campaigns/Sceptre_of_Fire/maps/9_overlay/9_o7.map}"
					[/terrain_mask]
					[store_locations]
						[filter]
							[not]
							type=Gryphon,Gryphon Rider,Gryphon Master
							[/not]
						side=1,2,3,4,5,6
						[/filter]
					x=1-32
					y=1-32
					terrain=Chw,Ql
					variable=lavadead
					[/store_locations]
					{FOREACH_FORMAT lavadead i}
						[kill]
						x=$lavadead_i_x
						y=$lavadead_i_y
						animate=yes
						[/kill]
					{NEXT i}
					#and then you win
					[endlevel]
					result=victory
					bonus=yes
					[/endlevel]
				[/else]
			[/if]
		[/event]
		#the objectives, they are a-changin'
		[objectives]
		side=1
			[objective]
			description= _ "Kill all of the elves before one of them escapes from the caves"
			condition=win
			[/objective]
			[objective]
			description= _ "Death of Rugnur"
			condition=lose
			[/objective]
			[objective]
			description= _ "Death of Krawg"
			condition=lose
			[/objective]
			[objective]
			description= _ "Death of Thursagan"
			condition=lose
			[/objective]
		[/objectives]
	[/event]

	#when you win, Krawg flies out of the mountain
	[event]
	name=victory
		#kill everyone in a sequence that makes it look like the cave is being flooded by lava
		#lava 8
		[terrain_mask]
		x,y=1,1
		mask="{@campaigns/Sceptre_of_Fire/maps/9_overlay/9_o8.map}"
		[/terrain_mask]
		[store_locations]
			[filter]
				[not]
				type=Gryphon,Gryphon Rider,Gryphon Master
				[/not]
			side=1,2,3,4,5,6
			[/filter]
		x=1-32
		y=1-32
		terrain=Chw,Ql
		variable=lavadead
		[/store_locations]
		{FOREACH_FORMAT lavadead i}
			[kill]
			x=$lavadead_i_x
			y=$lavadead_i_y
			animate=yes
			[/kill]
		{NEXT i}
		#lava 9
		[terrain_mask]
		x,y=1,1
		mask="{@campaigns/Sceptre_of_Fire/maps/9_overlay/9_o9.map}"
		[/terrain_mask]
		[store_locations]
			[filter]
				[not]
				type=Gryphon,Gryphon Rider,Gryphon Master
				[/not]
			side=1,2,3,4,5,6
			[/filter]
		x=1-32
		y=1-32
		terrain=Chw,Ql
		variable=lavadead
		[/store_locations]
		{FOREACH_FORMAT lavadead i}
			[kill]
			x=$lavadead_i_x
			y=$lavadead_i_y
			animate=yes
			[/kill]
		{NEXT i}
		#lava final
		[terrain_mask]
		x,y=1,1
		mask="{@campaigns/Sceptre_of_Fire/maps/9_overlay/9_of.map}"
		[/terrain_mask]
		[store_locations]
			[filter]
				[not]
				type=Gryphon,Gryphon Rider,Gryphon Master
				[/not]
			side=1,2,3,4,5,6
			[/filter]
		x=1-32
		y=1-32
		terrain=Chw,Ql
		variable=lavadead
		[/store_locations]
		{FOREACH_FORMAT lavadead i}
			[kill]
			x=$lavadead_i_x
			y=$lavadead_i_y
			animate=yes
			[/kill]
		{NEXT i}
		#clear the variable
		[clear_variable]
		name=lavadead
		[/clear_variable]
		#krawg leaves
		[kill]
			[not]
			description=Krawg
			[/not]
		animate=yes
		[/kill]
		[message]
		description=Krawg
		message= _ "Hm... Me le' no'w!"
		[/message]
		[store_unit]
			[filter]
			description=Krawg
			[/filter]
		variable=krawg
		kill=yes
		[/store_unit]
		{VARIABLE_OP move_x format (|$krawg.x|,18)}
		{VARIABLE_OP move_y format (|$krawg.y|,23)}
		[move_unit_fake]
		type=$krawg.type
		x=$move_x
		y=$move_y
		[/move_unit_fake]
		[unstore_unit]
		variable=alanin
		[/unstore_unit]
	[/event]
[/scenario]
