#textdomain wesnoth-utbs
# Level 6.a: In the Tunnels of the Trolls

[scenario]
    id="6a_In_the_Tunnels_of_the_Trolls"
    name= _ "In the Tunnels of the Trolls"

    map_data="{campaigns/Under_the_Burning_Suns/maps/06a_In_the_Tunnels_of_the_Trolls.map}"

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

    next_scenario=7_a_Dealing with Dwarves

    #max turns in scenario varies depending on difficulty
    {TURNS 62 58 56}
    victory_when_enemies_defeated=no

    {UNDERGROUND}

    #Elf player

    [side]
        side=1
        colour=red
        description=Kaleh
        type=Desert Fighter
        canrecruit=yes
        {INCOME 5 3 1}
        controller=human
        recruit=Desert Fighter, Desert Archer, Desert Hunter, Desert Shaman, Desert Scout
        shroud=yes
        fog=no
        team_name=dwarf_ally
    [/side]

    #Side=2 dwarf 1 (dwarf allies)
    [side]
        side=2
        colour=orange
        no_leader=yes
        gold=0
        income=0
        controller=ai
        shroud=no
        fog=no
        team_name=dwarf_ally
    [/side]

    #Side=3 troll 1 (advance guard trolls at outpost)
    [side]
        side=3
        colour=green
        no_leader=yes
        gold=0
        income=0
        controller=ai
        shroud=no
        fog=no
        team_name=monster

#ifdef EASY
        recruit=Troll Whelp, Troll
#endif

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

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

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

#ifdef NORMAL
            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.6
        [/ai]
    [/side]

    #Side=4 troll 2 (elite trolls in main lair)
    [side]
        side=4
        colour=brown
        no_leader=yes
        gold=0
        income=0
        controller=ai
        shroud=no
        fog=no
        team_name=monster

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

#ifdef NORMAL
        recruit=Troll Whelp, Troll, Troll Rocklobber, Troll Shaman
#endif

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

        [ai]
            recruitment_pattern=fighter,archer,fighter,mixed fighter

            # 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.6
        [/ai]
    [/side]

    #Side=5 fire guardians
    [side]
        side=5
        color=white
        no_leader=yes
        gold=0
        income=0
        controller=ai
        shroud=no
        fog=no
        team_name=monster

#ifdef EASY
        recruit=Vampire Bat, Blood Bat
#endif

#ifdef NORMAL
        recruit=Vampire Bat, Blood Bat, Dread Bat
#endif

#ifdef HARD
        recruit=Vampire Bat, Blood Bat, Dread Bat
#endif

        [ai]
            aggression=0.9
            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}

            #fire guardians can't leave lava cavern
            [avoid]
                x,y=39-46,77-81
            [/avoid]

            [avoid]
                x,y=30-34,92-100
            [/avoid]
        [/ai]
    [/side]

    [story]
        [part]
            story= _ "Chapter 6: The dwarf Grendel led us through a maze of twisting passages speaking scarcely a word. Finally, after what seemed like hours of marching, he stopped. He motioned us to be very quiet and we crept forward; all I could hear was the soft patter of feet and my heavy breathing. Even that little noise seemed to echo off the cramped walls of our rough-hewn passage. I was suddenly aware of the sheer mass of rock and earth above us and for a moment I despaired of ever seeing the sun again. Then I grabbed my sword with fresh determination and vowed to see this mission through."
        [/part]
    [/story]

    {@campaigns/Under_the_Burning_Suns/utils/dialog-macros.cfg}

    # 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

        # test units

        #{UNIT_ (Dwarvish Fighter) (Test) ( _ "Test") 1 23 7}

        #set starting scenario objectives

        [objectives]
            summary= _ "Starting Objectives:"
            [objective]
                description= _ "Kill Troll Leader"
                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 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]

            [else]
                [if]
                    [variable]
                        name=scen3
                        numerical_equals=1
                    [/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]
                [/if]

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

                    [then]
                        #go from 4 to 5

                        [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]

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

                        [allow_recruit]
                            type=Desert Fighter5, Desert Archer5, Desert Hunter5, Desert Shaman5, Desert Scout5
                            side=1
                        [/allow_recruit]
                    [/then]
                [/if]
            [/else]
        [/if]

        #secret troll tomb furnishings
        {PLACE_IMAGE items/coffin-closed.png 3 28}
        {PLACE_IMAGE items/bones.png 6 24}

        #troll burial grounds furnishings

        {PLACE_IMAGE items/burial.png 36 28}
        {PLACE_IMAGE items/burial.png 33 25}
        {PLACE_IMAGE items/burial.png 40 23}

        {PLACE_IMAGE items/bonestack.png 37 23}
        {PLACE_IMAGE items/bonestack.png 39 27}

        {PLACE_IMAGE scenery/monolith1.png 34 25}
        {PLACE_IMAGE scenery/monolith4.png 34 27}

        #other furnishings

        {PLACE_IMAGE items/bones.png 14 12}

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

        #initialize starting variables

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

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

        # damage done by sanding in lava used to be
        # 30 hp at Challenging and Hard difficulty

        [set_variable]
            name=lava_damage
            value=-25
        [/set_variable]

        # heat damage by just standing in the lava cavern

#ifdef EASY
        [set_variable]
            name=heat_damage
            value=-2
        [/set_variable]
#endif

#ifdef NORMAL
        [set_variable]
            name=heat_damage
            value=-3
        [/set_variable]
#endif

#ifdef HARD
        [set_variable]
            name=heat_damage
            value=-4
        [/set_variable]
#endif

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

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

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

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

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

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

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

        # create AI=guardian units. They can't move unless an enemy
        # moves nearby. I create them at the beginning because when the
        # player sees them, events will fire.

        [unit]
            type=Dwarvish Pathfinder
            description=Grendel
            user_description= _ "Grendel"
            x=7
            y=6
            side=2
            ai_special=guardian
            [modifications]
                {TRAIT_STRONG}
                {TRAIT_RESILIENT}
            [/modifications]
        [/unit]

        # Event 2.1
        #Troll interrogators guardian

        [unit]
            type=Troll
            description=Troll Interrogator
            user_description= _ "Troll Interrogator"
            x=34
            y=5
            side=3
            ai_special=guardian
            [modifications]
                {TRAIT_STRONG}
                {TRAIT_INTELLIGENT}
            [/modifications]
        [/unit]

        [unit]
            type=Troll Whelp
            description=Troll Assistant
            user_description= _ "Troll Assistant"
            x=32
            y=5
            side=3
            ai_special=guardian
            [modifications]
                {TRAIT_RESILIENT}
                {TRAIT_LOYAL}
            [/modifications]
        [/unit]

        [unit]
            type=Troll Whelp
            description=Ulg
            user_description= _ "Ulg"
            x=33
            y=7
            side=3
            ai_special=guardian
            [modifications]
                {TRAIT_STRONG}
                {TRAIT_LOYAL}
            [/modifications]
        [/unit]

#ifdef HARD
        [unit]
            type=Troll Whelp
            description=Troll Assistant
            user_description= _ "Troll Assistant"
            x=31
            y=4
            side=3
            ai_special=guardian
            [modifications]
                {TRAIT_STRONG}
                {TRAIT_RESILIENT}
            [/modifications]
        [/unit]
#endif

        # Event 3: Troll High Shamans guarding lava maze

        [unit]
            type=Troll Shaman
            description=Troll High Shaman
            user_description= _ "Troll High Shaman"
            x=16
            y=22
            side=4
            ai_special=guardian
            [modifications]
                {TRAIT_INTELLIGENT}
                {TRAIT_LOYAL}
                [object]
                    id=t3
                    silent=yes
                    [effect]
                        apply_to=movement_costs
                        replace=yes
                        [movement_costs]
                            cave=99
                            grassland=99
                        [/movement_costs]
                    [/effect]
                [/object]
            [/modifications]
        [/unit]

        [unit]
            type=Troll Shaman
            description=Troll High Shaman
            user_description= _ "Troll High Shaman"
            x=18
            y=23
            side=4
            ai_special=guardian
            [modifications]
                {TRAIT_RESILIENT}
                {TRAIT_LOYAL}
                [object]
                    id=t4
                    silent=yes
                    [effect]
                        apply_to=movement_costs
                        replace=yes
                        [movement_costs]
                            cave=99
                            grassland=99
                        [/movement_costs]
                    [/effect]
                [/object]
            [/modifications]
        [/unit]

        # reveal a bit of the escape passage to the player
        [remove_shroud]
            x,y=5-8,4-6
            side=1
        [/remove_shroud]
    [/event]

    # starting events

    [event]
        name=start

        # starting dialogue

        [message]
            description=Grendel
            message= _ "This passage leads very close to the trolls' main lair; you can hear them tromping back and forth just past the eastern wall. All that separates us from the trolls is a thin wall of stone. I've had me boys mine the end of the tunnel with explosives; when you move a unit adjacent to the final wall, I'll blow the charges and open the way. The troll leader should only be lightly defended; he's sent most of his best warriors to the front. You'll know the head troll when you see him, he's big, green and extra ugly."
        [/message]

        [message]
            description=Nym
            message= _ "I thought that's what all the trolls looked like. I suppose we'll just try to find the one that shouts the loudest."
        [/message]

        [message]
            description=Zhul
            message= _ "So you dug these tunnels all by yourselves?"
        [/message]

        [message]
            description=Grendel
            message= _ "Long ago, before the damned trolls invaded, we spent our days digging tunnels all through these mountains. Oh the ore and precious stones we mined! The mines were filled with the joyful sound of dwarven hammer and dwarven song and our jeweled halls glowed as brightly as the sun. Human and elf princes would pay us royally for the craftsmanship of our forges. But now the tunnels are silent and we spend our days hunting those accursed trolls. But there's no point dwelling on the past. There's more work to be done! Still, we have our honor and I promise you, do this for us and you will be rewarded handsomely."
        [/message]

        [message]
            description=Kaleh
            message= _ "It shall be done."
        [/message]
    [/event]

    # Event 1: Blow up wall, encounter advance troll guards

    # when player moves next wall trigger variable
    [event]
        name=moveto

        [filter]
            x,y=15,7
            side=1
        [/filter]

        [message]
            description=Grendel
            message= _ "Once you are done moving your people into position, I will blow the charges."
        [/message]

        [set_variable]
            name=destroy_wall
            value=1
        [/set_variable]
    [/event]

    # at start of player's next turn, blow charges and destroy wall

    [event]
        name=new turn
        first_time_only=no

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

            [then]
                [set_variable]
                    name=destroy_wall
                    value=2
                [/set_variable]

                # Modify troll's income

                [modify_side]
                    side=3

#ifdef EASY
                    income=10
                    gold=75
#endif
#ifdef NORMAL
                    income=12
                    gold=100
#endif
#ifdef HARD
                    income=14
                    gold=125
#endif
                [/modify_side]

                # Place troll guards

                # EASY:  1 Troll Leader, 2 troll whelps, 1 rocklobber
                # NORMAL: 1 Troll Leader, 3 troll whelps, 1 rocklobber
                # HARD: 1 Troll Leader, 3 troll whelps, 1 rocklobber, 1 troll

                # 1 Troll Leader
                [unit]
                    description=Troll Brute
                    user_description= _ "Troll Brute"
                    type=Troll
                    x=23
                    y=9
                    side=3
                    canrecruit=yes
                    upkeep=free
                    [modifications]
                        {TRAIT_STRONG}
                        {TRAIT_RESILIENT}
                    [/modifications]
                [/unit]

                # 1 Troll Rocklobber
                {UNIT_ (Troll Rocklobber) (Troll Guard) ( _ "Troll Guard") 3 23 8}

                # 0-1 extra Trolls
                {UNIT_ (Troll) (Troll Guard) ( _ "Troll Guard") 3 21 9}

                # 2-3 troll whelps occupy base and villages
                # 2 troll whelps in villages
                {UNIT_ (Troll Whelp) (Troll Guard) ( _ "Troll Guard") 3 21 5}
                {UNIT_ (Troll Whelp) (Troll Guard) ( _ "Troll Guard") 3 23 6}

#ifdef EASY
#else
                {UNIT_ (Troll Whelp) (Troll Guard) ( _ "Troll Guard") 3 21 19}
#endif
                # wmllint: recognize Troll Guard

                # Have Grendel blow the explosives to destroy the wall

                [message]
                    description=Grendel
                    message= _ "Fire in the hole!"
                [/message]

                # Have screen flash red

                [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]

                # Have wall shake, then be replaced by dirt, then add rubble

                [scroll]
                    x=20
                [/scroll]
                [scroll]
                    x=-20
                [/scroll]
                [scroll]
                    x=-20
                [/scroll]
                [scroll]
                    x=-20
                [/scroll]

                [terrain]
                    x,y=16,7
                    terrain=Re
                [/terrain]

                {PLACE_IMAGE scenery/rubble.png 16 7}

                # Reveal dwarven cavern
                [remove_shroud]
                    x=16-25
                    y=3-10
                    side=1
                [/remove_shroud]

                # Have Grendel say goodbye

                [message]
                    description=Grendel
                    message= _ "My work here is done. I must report back to my King. I have many more things to do before the day is done, but I will once you finish your mission. Fight well!"
                [/message]

                # Have him leave
                [kill]
                    description=Grendel
                    animate=no
                    fire_event=no
                [/kill]

                [move_unit_fake]
                    type=Dwarvish Pathfinder
                    x=7,6,5,4,3,2,1
                    y=6,5,5,4,5,4,4
                [/move_unit_fake]

                [terrain]
                    x,y=1,4
                    terrain=Xu
                [/terrain]

                {PLACE_IMAGE scenery/rubble.png 2 4}

                # Have troll guards talk

                [message]
                    description=Troll Guard
                    message= _ "Intruders! Kill them!"
                [/message]
            [/then]
        [/if]
    [/event]

    # Event 2: If player tries to go back up escape passage
    # This event is disabled becuase player can go back up escape passage
    # before he triggers Event 1 and ally leaves. Thus you could trap
    # a unit behind the cave-in. Since this event is just for flavor,
    # I'm disabling it.

    #[event]
    #name=moveto

    #[filter]
    #	x=2-5,4-5
    #	side=1
    #[/filter]

    #[message]
    #speaker=unit
    #message= _ "Grendel must have collapsed the tunnel. Perhaps he didn't want to #give the trolls an easy access route if they defeated us. So much an an escape #route, I guess we'll just have to be sure not to fail."
    #[/message]

    #[/event]

    # Event 2.1: Troll Interrogators

    # When player appears 3 trolls are interrogating a wounded dwarf
    # When they see the player they kill the dwarf. The other says
    # "The prisoners must not be allowed to escape. Kill the other prisoner
    # while we hold them off!"

    # Troll and Troll whelp try to hold off player

    # I create trolls well in advance of player entering cave
    # this makes sure that player can't move into trolls hexes
    # I am forced to make trolls into guardians to keep them
    # from moving prematurely

    # Moveto version (sighted version is disabled)

    [event]
        name=moveto

        [filter]
            x=27-35
            y=3-8
            side=1
        [/filter]

        [remove_shroud]
            x=28-35
            y=3-9
            side=1
        [/remove_shroud]

        [unit]
            type=Dwarvish Fighter
            description=Wounded Dwarf
            user_description= _ "Wounded Dwarf"
            x=33
            y=6
            side=2
            hitpoints=5
        [/unit]

        [message]
            description=Troll Interrogator
            message= _ "Tell us where your leader is hiding!"
        [/message]

        [message]
            description=Wounded Dwarf
            message= _ "Never!"
        [/message]

        [message]
            description=Troll Assistant
            message= _ "Master, look!"
        [/message]

        [message]
            description=Troll Interrogator
            message= _ "Hah! You think you can save your friends. You are wrong. Ulg, go kill the other prisoner. We will deal with these fools."
        [/message]

        [message]
            description=Ulg
            message= _ "Yes master, I'll make him suffer."
        [/message]

        [teleport]
            [filter]
                description=Ulg
            [/filter]
            x,y=40,4
        [/teleport]

        [move_unit_fake]
            type=Troll Whelp
            x=15,14,13,12,11,10
            y=48,48,48,47,47,46
        [/move_unit_fake]

        [hide_unit]
            description=Troll Interrogator
        [/hide_unit]

        [redraw]
        [/redraw]

        {PLACE_IMAGE units/trolls/grunt-attack-1.png 34 5}

        [redraw]
        [/redraw]

        [delay]
            time=300
        [/delay]

        [removeitem]
            x,y=34,5
        [/removeitem]

        [unhide_unit]
        [/unhide_unit]

        [message]
            description=Wounded Dwarf
            message= _ "Aaahhhh!"
        [/message]

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

        {CHECK_EXPLORER}
        [message]
            description=$explorer.description
            message= _ "If we move fast we might be able to save the other prisoner before he gets killed too."
        [/message]
        {CLEAR_VARIABLE explorer}
    [/event]

    # Event 2.2: Wounded Dwarf

    # Troll whelp tries to kill wounded Dwarvish Stalwart

    # (sighted version is disabled)

    [event]
        name=moveto

        [filter]
            x=35-41
            y=4-7
            side=1
        [/filter]

        [remove_shroud]
            x=34-42
            y=4-8
            side=1
        [/remove_shroud]

        [unit]
            type=Dwarvish Stalwart
            description=Rogrimir
            user_description= _ "Rogrimir"
            profile=portraits/rogrimir.png
            x=41
            y=4
            side=2
            hitpoints=20
            [modifications]
                {TRAIT_LOYAL}
                {TRAIT_QUICK}
            [/modifications]
        [/unit]

        [message]
            description=Ulg
            message= _ "I'm gonna make you squeal, dwarf!"
        [/message]
    [/event]

    # when Ulg dies, Rogrimir thanks player

    [event]
        name=die

        [filter]
            description=Ulg
        [/filter]

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

        [message]
            description=Rogrimir
            message= _ "I owe you my life. I can't believe I was captured when those all around me died fighting gloriously. I'm so ashamed. I could not protect them....but I will guard you with my life, even if I have to follow you to the ends of the earth. Now lead me to the trolls and let me avenge my friends' deaths!"
        [/message]

        [store_unit]
            [filter]
                description=Rogrimir
            [/filter]

            variable=dwarfstats
        [/store_unit]

        [set_variable]
            name=dwarfstats.side
            value=1
        [/set_variable]

        [set_variable]
            name=dwarfstats.moves
            value=5
        [/set_variable]

        [unstore_unit]
            variable=dwarfstats
        [/unstore_unit]

        {CLEAR_VARIABLE dwarfstats}
    [/event]

    # Event 3: Troll Lava Maze

    # lava is ironically an alias of snow. Elves take 3 moves to cross it,
    # except for mounted elves which take 4 (horses hate lava). Thus
    # ensuring that no elf can move more than 2 hexes on lava
    # with the exception of the desert shyde/star which can move 3 hexes

    # At the start of each turn, all units except the Desert Shyde/Star standing
    # on a lava hex take 25 damage. This damage can kill units.

    [event]
	name=new turn
	first_time_only=no
	[redraw]
		side=1
	[/redraw]
	[store_locations]
		terrain=Yl
		[filter]
			side=1,3,4
			[not]
				type=Zhul Shyde,Zhul Star,Desert Shyde,Desert Star,Dust Devil
			[/not]
		[/filter]
		variable=lava_hex
	[/store_locations]
	{VARIABLE i 0}
	{FOREACH lava_hex i}
		[store_unit]
			[filter]
				x,y=$lava_hex[$i].x,$lava_hex[$i].y
			[/filter]
			variable=unit_on_lava
		[/store_unit]
        # this code makes sure Kaleh stays unslowed
		[if]
			[variable]
				name=unslow_kaleh
				numerical_equals=1
			[/variable]
			[variable]
				name=unit_on_lava.description
				equals=Kaleh
			[/variable]
			[then]
				[set_variable]
					name=unit_on_lava.status.slowed
					value="off"
				[/set_variable]
			[/then]
		[/if]

		{VARIABLE heat_offset $heat_damage}
		{VARIABLE_OP heat_offset multiply -1}
		{VARIABLE damage_recived $lava_damage}
		{VARIABLE_OP damage_recived add $heat_offset}
		{CLEAR_VARIABLE heat_offset}

		{VARIABLE_OP unit_on_lava.hitpoints add $damage_recived}
		{VARIABLE damage_msg $damage_recived}
		{VARIABLE_OP damage_msg multiply -1}
		{CLEAR_VARIABLE damage_recived}
		# if unitstats.hitpoints is < 1, unstore unit and kill it,
		# else just unstore it
		[unstore_unit]
			variable=unit_on_lava
			text=$damage_msg
			{COLOR_HARM}
		[/unstore_unit]
		[if]
			[variable]
				name=unit_on_lava.hitpoints
				less_than=1
			[/variable]
			[then]
				[kill]
					description=$unit_on_lava.description
					fire_event=yes
					animate=yes
				[/kill]
			[/then]
		[/if]
		{CLEAR_VARIABLE unit_on_lava}
		{CLEAR_VARIABLE damage_msg}
	{NEXT i}

	{CLEAR_VARIABLE lava_hex}

    [/event]

    # At the start of each turn, all units in main cavern standing on a cave
    # floor hex, or lava hex, or a dirt hex or a road hex take 3,4,5 damage
    # from the heat. This damage is non-lethal.

    [event]
        name=new turn
        first_time_only=no

        # wmllint: recognize Guardian Phoenix
        # wmllint: recognize Dust Devil
        [redraw]
        [/redraw]

        [store_locations]
            x=1-33
            y=11-27
            [filter]
                side=1,3,4
                [not]
                    description=Guardian Phoenix
                [/not]
                [not]
                    description=Dust Devil
                [/not]
            [/filter]
            variable=locs
        [/store_locations]

        {FOREACH locs i}

        [set_variable]
            name=temp_x
            to_variable=locs[$i].x
        [/set_variable]

        [set_variable]
            name=temp_y
            to_variable=locs[$i].y
        [/set_variable]

        [store_unit]
            [filter]
                x,y=$temp_x,$temp_y
            [/filter]
            variable=unitstats
        [/store_unit]

        # this code makes sure Kaleh stays unslowed
        [if]
            [variable]
                name=unslow_kaleh
                numerical_equals=1
            [/variable]

            [variable]
                name=unitstats.description
                equals=Kaleh
            [/variable]

            [then]
                [set_variable]
                    name=unitstats.status.slowed
                    value="off"
                [/set_variable]
            [/then]
        [/if]

        [set_variable]
            name=unitstats.hitpoints
            add=$heat_damage
        [/set_variable]

        [set_variable]
            name=damage_msg
            format=$heat_damage
        [/set_variable]

        [set_variable]
            name=damage_msg
            multiply=-1
        [/set_variable]

        #if unitstats.hitpoints is < 1, then set hitpoints to 1
        [if]
            [variable]
                name=unitstats.hitpoints
                less_than=1
            [/variable]

            [then]
                [set_variable]
                    name=unitstats.hitpoints
                    value=1
                [/set_variable]
            [/then]
        [/if]

        [unstore_unit]
            text=$damage_msg
            {COLOR_HARM}
            variable=unitstats
        [/unstore_unit]

        {CLEAR_VARIABLE unitstats}

        {NEXT i}

        {CLEAR_VARIABLE locs}
        {CLEAR_VARIABLE damage_msg}
    [/event]

    #Initial moveto event to enter lava cavern

    [event]
        name=moveto

        [filter]
            x,y=22-30,10-20
            side=1
        [/filter]

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

        [remove_shroud]
            x=20-29
            y=23-27
            side=1
        [/remove_shroud]

        [remove_shroud]
            x=29-31
            y=13-15
            side=1
        [/remove_shroud]

        [place_shroud]
            x=13-14
            y=11-13
            side=1
        [/place_shroud]

        [place_shroud]
            x=11-12
            y=26-27
            side=1
        [/place_shroud]

        [set_variable]
            name=temp_damage
            value=$heat_damage
        [/set_variable]

        [set_variable]
            name=temp_damage
            multiply=-1
        [/set_variable]

        [redraw]
        [/redraw]

        {CHECK_EXPLORER}
        [message]
            description=$explorer.description
            message= _ "Woah. This place is hot."
        [/message]

        [message]
            description=$explorer.description
            message= _ "This cavern is so hot it's stifling; I can already feel my armor heating up. If we tarry here too long we'll roast alive. I don't even want to think about what would happen if I tried to walk across the lava. On the other hand, the lava does light up the cavern nicely. I'm just thankful the trolls constructed a bridge across the lava."
        [/message]

        [message]
            description=Troll High Shaman
            message= _ "They have broken through the outer guardpost. Destroy the bridge, they must not pass!"
        [/message]

        # Have screen flash red

        [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]

        # Have screen shake, then destroy most of bridge

        [scroll]
            x=20
        [/scroll]
        [scroll]
            x=-20
        [/scroll]
        [scroll]
            x=-20
        [/scroll]
        [scroll]
            x=-20
        [/scroll]

        [terrain]
            x=16,17,18,18,19,19,20,20,21,22,22,23,23,23,24,24,24
            y=20,22,20,19,20,19,19,17,17,17,16,17,16,15,16,15,14
            terrain=Yl
        [/terrain]

        [kill]
            description=Thrud
            animate=yes
            fire_event=no
        [/kill]

        [message]
            speaker=description=$explorer.description
            message= _ "Uh, I take that back. Still, the trolls don't seem to be advancing. I guess they think the lava can hold us back. Well, we'll show them. It will take more than a little heat to stop us!"
        [/message]
        {CLEAR_VARIABLE explorer}

        [message]
            speaker=narrator
            message= _ "Any unit that ends its turn on a lava hex, except the Desert Shyde and Star who can fly over the lava, will take 25 damage at the beginning of the next turn. This lava damage can kill units. Desert Shydes and Stars will just take $temp_damage damage per turn when flying over lava, though they too can die if they spend too much time over lava. Also because of the heat in the cavern, all units on cave floor hexes will take $temp_damage damage at the start of each turn. This heat damage can reduce a unit to 1 hit point, but it can't kill it. "
            image=wesnoth-icon.png
        [/message]

        {CLEAR_VARIABLE temp_damage}

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

        [set_variable]
            name=flame_counter
            value=5
        [/set_variable]
    [/event]

    # pool of water lets player heal halfway

    [event]
        name=moveto

        [filter]
            x,y=22,26
            side=1
        [/filter]

        [set_variable]
            name=temp_damage
            value=$heat_damage
        [/set_variable]

        [set_variable]
            name=temp_damage
            multiply=-1
        [/set_variable]

        {CHECK_EXPLORER}
        [message]
            description=$explorer.description
            message= _ "There is a small pool of water here. It must come from some spring deep underground. The water is cool and refreshing. Let me bathe in it a while and recover from the heat of that blasted cavern."
        [/message]
        {CLEAR_VARIABLE explorer}

        [message]
            speaker=narrator
            message= _ "At the start of each turn, any unit currently in this pool will heal 10 hitpoints and will not be affected by the heat in the cavern. Of course, if the unit leaves this pool, it will suffer the standard $temp_damage damage each turn from the heat."
            image=wesnoth-icon.png
        [/message]

        {CLEAR_VARIABLE temp_damage}
    [/event]

    # unit heals 10 hp a turn

    [event]
        name=new turn
        first_time_only=no

        [object]
            [filter]
                x,y=22,26
            [/filter]

            silent=yes
            duration="level"

            [effect]
                apply_to="hitpoints"
                increase=10
            [/effect]
        [/object]
    [/event]

    # This function fires each turn and checks to see if it should summon
    # a "Fire Pheonix" fire guardian, based on the value of the summon_flame
    # variable and on the value of the flame_counter

    # when the fire guardian unit dies, the flame_counter is reset to 0

    # each turn flame_counter is incremented until it reached 4 (on hard
    # difficulty) or 5, then a new fire guardian is summoned and
    # summon_flame is turned back to 0.

    # summon_flame=0: fire guardian has been created, is alive, don't create another
    # summon_flame=1: fire guardian has been killed, create another
    # summon_flame=2: create fire guardian for first time, elves are amazed
    # summon_flame=3: player has reached troll shamans, stop summoning fire guardians

    [event]
        name=new turn
        first_time_only=no

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

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

#ifdef HARD
            [variable]
                name=flame_counter
                greater_than=3
            [/variable]
#else
            [variable]
                name=flame_counter
                greater_than=4
            [/variable]
#endif

            [then]
                [message]
                    description=Troll High Shaman
                    message= _ "Arise! Arise and engulf the intruders in your holy fire!"
                [/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]

#ifdef EASY
                {UNIT_ (Lava Monster) (Guardian Phoenix) ( _ "Guardian Phoenix") 5 26 21}
#endif

#ifdef NORMAL
                {UNIT_ (Lava Monster2) (Guardian Phoenix) ( _ "Guardian Phoenix") 5 26 21}
#endif

#ifdef HARD
                {UNIT_ (Lava Monster3) (Guardian Phoenix) ( _ "Guardian Phoenix") 5 26 21}
#endif

                [scroll_to_unit]
                    x,y=26,21
                [/scroll_to_unit]

                [delay]
                    time=250
                [/delay]

                [message]
                    side=1
                    x,y=16-29,11-23
                    message= _ "What the heck is that? It sure doesn't look good. The last thing we need in here is even more fire."
                [/message]

                [set_variable]
                    name=summon_flame
                    value=0
                [/set_variable]
            [/then]

            [else]
                [if]
                    [variable]
                        name=summon_flame
                        numerical_equals=1
                    [/variable]

#ifdef HARD
                    [variable]
                        name=flame_counter
                        greater_than=3
                    [/variable]
#else
                    [variable]
                        name=flame_counter
                        greater_than=4
                    [/variable]
#endif

                    [then]
                        [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]

#ifdef EASY
                        {UNIT_ (Lava Monster) (Guardian Phoenix) ( _ "Guardian Phoenix") 5 26 21}
#endif

#ifdef NORMAL
                        {UNIT_ (Lava Monster2) (Guardian Phoenix) ( _ "Guardian Phoenix") 5 26 21}
#endif

#ifdef HARD
                        {UNIT_ (Lava Monster3) (Guardian Phoenix) ( _ "Guardian Phoenix") 5 26 21}
#endif

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

                        [delay]
                            time=250
                        [/delay]

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

                        [message]
                            side=1
                            x,y=16-29,11-23
                            message= _ "That thing just won't stay dead!"
                        [/message]
                    [/then]
                [/if]
            [/else]
        [/if]
    [/event]

    # when fire guardian dies, switch summon_flame back to 1
    # and reset flame_counter back to 0

    [event]
        name=die
        first_time_only=no

        [filter]
            description=Guardian Phoenix
        [/filter]

        [if]
            [variable]
                name=summon_flame
                less_than=3
            [/variable]

            [then]
                [set_variable]
                    name=summon_flame
                    value=1
                [/set_variable]

                [set_variable]
                    name=flame_counter
                    value=0
                [/set_variable]
            [/then]
        [/if]
    [/event]

    # Troll shamans raise more Fire Guardians out of the lava

    # Lava monster 1: y=18
    # Lava monster 2: y=25
    # Lava monster 3: x=20-21 y=18-19

    [event]
        name=moveto

        [filter]
            x=11-32
            y=18-25
            side=1
        [/filter]

        [if]
            [variable]
                name=summon_flame
                numerical_not_equals=3
            [/variable]

            [then]
                [message]
                    description=Troll High Shaman
                    message= _ "Arise, hallowed guardians, and destroy them!"
                [/message]

                [scroll_to]
                    x,y=26,17
                [/scroll_to]

#ifdef EASY
                {UNIT_ (Lava Monster) (Fire Guardian) ( _ "Fire Guardian") 5 26 17}
                {UNIT_ (Lava Monster) (Fire Guardian) ( _ "Fire Guardian") 5 26 18}
#endif

#ifdef NORMAL
                {UNIT_ (Lava Monster2) (Fire Guardian) ( _ "Fire Guardian") 5 26 17}
                {UNIT_ (Lava Monster2) (Fire Guardian) ( _ "Fire Guardian") 5 26 18}
#endif

#ifdef HARD
                {UNIT_ (Lava Monster3) (Fire Guardian) ( _ "Fire Guardian") 5 25 18}
                {UNIT_ (Lava Monster3) (Fire Guardian) ( _ "Fire Guardian") 5 26 17}
                {UNIT_ (Lava Monster3) (Fire Guardian) ( _ "Fire Guardian") 5 26 18}
#endif

                {CHECK_EXPLORER}
                [message]
                    description=$explorer.description
                    message= _ "Look, more fire guardians!"
                [/message]
                {CLEAR_VARIABLE explorer}
            [/then]
        [/if]
    [/event]

    [event]
        name=moveto

        [filter]
            x=23-29
            y=25-26
            side=1
        [/filter]

        [if]
            [variable]
                name=summon_flame
                numerical_not_equals=3
            [/variable]

            [then]
                [message]
                    description=Troll High Shaman
                    message= _ "Arise and attack them now, while they are vulnerable!"
                [/message]

                [scroll_to]
                    x,y=42,91
                [/scroll_to]

#ifdef EASY
                {UNIT_ (Lava Monster) (Fire Guardian) ( _ "Fire Guardian") 5 26 22}
                {UNIT_ (Lava Monster) (Fire Guardian) ( _ "Fire Guardian") 5 27 22}
#endif

#ifdef NORMAL
                {UNIT_ (Lava Monster2) (Fire Guardian) ( _ "Fire Guardian") 5 26 22}
                {UNIT_ (Lava Monster2) (Fire Guardian) ( _ "Fire Guardian") 5 27 22}
#endif

#ifdef HARD
                {UNIT_ (Lava Monster3) (Fire Guardian) ( _ "Fire Guardian") 5 26 21}
                {UNIT_ (Lava Monster3) (Fire Guardian) ( _ "Fire Guardian") 5 26 22}
                {UNIT_ (Lava Monster3) (Fire Guardian) ( _ "Fire Guardian") 5 27 22}
#endif

                {CHECK_EXPLORER}
                [message]
                    description=$explorer.description
                    message= _ "Oh great, even more fire guardians. When I get through this inferno I'm going to kill those trolls."
                [/message]
                {CLEAR_VARIABLE explorer}
            [/then]
        [/if]
    [/event]

    [event]
        name=moveto

        [filter]
            x=12-21
            y=17-24
            side=1
        [/filter]

        [if]
            [variable]
                name=summon_flame
                numerical_not_equals=3
            [/variable]

            [then]
                [message]
                    description=Troll High Shaman
                    message= _ "They must not be allowed to cross to the other side! Kill them!"
                [/message]

                [scroll_to]
                    x,y=22,16
                [/scroll_to]

#ifdef EASY
                {UNIT_ (Lava Monster) (Fire Guardian) ( _ "Fire Guardian") 5 22 16}
                {UNIT_ (Lava Monster) (Fire Guardian) ( _ "Fire Guardian") 5 23 17}
#endif

#ifdef NORMAL
                {UNIT_ (Lava Monster2) (Fire Guardian) ( _ "Fire Guardian") 5 22 16}
                {UNIT_ (Lava Monster2) (Fire Guardian) ( _ "Fire Guardian") 5 23 17}
#endif

#ifdef HARD
                {UNIT_ (Lava Monster3) (Fire Guardian) ( _ "Fire Guardian") 5 23 17}
                {UNIT_ (Lava Monster3) (Fire Guardian) ( _ "Fire Guardian") 5 22 16}
                {UNIT_ (Lava Monster3) (Fire Guardian) ( _ "Fire Guardian") 5 22 17}
#endif
                {CHECK_EXPLORER}
                [message]
                    description=$explorer.description
                    message= _ "How surprising, more fire guardians. I'm going to be really glad to get out of this cavern."
                [/message]
                {CLEAR_VARIABLE explorer}
            [/then]
        [/if]
    [/event]

    #at end of lava cavern, trolls come alive
    [event]
        name=moveto

        [filter]
            x=17-18,13-21
            y=18-19,20-28
            side=1
        [/filter]

        [set_variable]
            name=summon_flame
            value=3
        [/set_variable]

        [message]
            x,y=18,23
            message= _  "Despite the fire guardians, the elves have almost crossed the lava!"
        [/message]

        [message]
            x,y=16,22
            message= _  "They obviously weren't enough. You go alert the others and summon reinforcements. I will hold them off for as long as I can."
        [/message]

        [message]
            x,y=18,23
            message= _  "May Griknagh protect you. I'll be back soon!"
        [/message]

        #replace with a new troll shaman that can move
        [kill]
            x,y=16,22
            animate=no
        [/kill]

        [unit]
            type=Troll Shaman
            description=Troll High Shaman
            user_description= _ "Troll High Shaman"
            x=16
            y=22
            side=4
            [modifications]
                {TRAIT_INTELLIGENT}
                {TRAIT_LOYAL}
                [object]
                    id=t2
                    silent=yes
                    [effect]
                        apply_to=movement_costs
                        replace=yes
                        [movement_costs]
                            cave=99
                            grassland=99
                        [/movement_costs]
                    [/effect]
                [/object]
            [/modifications]
        [/unit]

        [kill]
            x,y=18,23
            animate=no
        [/kill]

        [move_unit_fake]
            type=Troll Shaman
            x=18,17,16,16,17,18,19,20,21
            y=23,24,24,25,26,27,28,28,29
        [/move_unit_fake]

        # Create reinforcements for shaman to return with
        # Create 2 troll whelps and on Challening and Hard add a troll shaman

        {UNIT_T (Troll Whelp) (Troll Reinforcements) ( _ "Troll Reinforcements") 3 27 28}
        {UNIT_T (Troll Whelp) (Troll Reinforcements) ( _ "Troll Reinforcements") 3 28 28}

        #on challenging/hard add a troll shaman
#ifdef EASY
#else

        [unit]
            type=Troll Shaman
            description=Troll High Shaman
            user_description= _ "Troll High Shaman"
            x=26
            y=28
            side=4
            [modifications]
                {TRAIT_RESILIENT}
                {TRAIT_LOYAL}
                [object]
                    id=t1
                    silent=yes
                    [effect]
                        apply_to=movement_costs
                        replace=yes
                        [movement_costs]
                            cave=99
                            grassland=99
                        [/movement_costs]
                    [/effect]
                [/object]
            [/modifications]
        [/unit]

#endif
    [/event]

    # Before you have gotten close to troll lair, keep them from gaining any gold

    [event]
        name=new turn
        first_time_only=no

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

            [then]
                [modify_side]
                    side=4
                    income=0
                    gold=0
                [/modify_side]
            [/then]
        [/if]
    [/event]

    # Event 23: Trigger Troll Lord's side

    [event]
        name=moveto

        [filter]
            x=27-44
            y=27-30
            side=1
        [/filter]

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

        #create troll leader
        [unit]
            type=Great Troll
            description=Troll Chieftain
            user_description= _ "Troll Chieftain"
            canrecruit=yes
            upkeep=full
            x=46
            y=22
            side=4
            [modifications]
                {TRAIT_STRONG}
                {TRAIT_RESILIENT}
            [/modifications]
        [/unit]

        [modify_side]
            side=4

#ifdef EASY
            income=2
            gold=120
#endif
#ifdef NORMAL
            income=4
            gold=140
#endif
#ifdef HARD
            income=6
            gold=160
#endif
        [/modify_side]

        #capture troll chieftan's villages

        [capture_village]
            x,y=39-49,20-29
            side=4
        [/capture_village]
    [/event]

    #reveal cavern when player enters

    [event]
        name=moveto

        [filter]
            x=33-39
            y=24-28
            side=1
        [/filter]

        #create troll defenders

        {UNIT_T (Troll Whelp) (Troll Guard) ( _ "Troll Guard") 4 36 25}
        {UNIT_T (Troll Whelp) (Troll Guard) ( _ "Troll Guard") 4 37 29}
        {UNIT_T (Troll Rocklobber) (Troll Guard) ( _ "Troll Guard") 4 39 27}

#ifdef EASY
        {UNIT_T (Troll Whelp) (Troll Guard) ( _ "Troll Guard") 4 42 25}
#else
        {UNIT_T (Troll) (Troll Guard) ( _ "Troll Guard") 4 42 25}
#endif

        [remove_shroud]
            x=33-50
            y=19-29
            side=1
        [/remove_shroud]

        [unit]
            type=Troll Shaman
            description=High Advisor
            user_description= _ "High Advisor"
            upkeep=free
            x=45
            y=24
            side=4
            ai_special=guardian
            [modifications]
                {TRAIT_LOYAL}
                {TRAIT_RESILIENT}
            [/modifications]
        [/unit]

        [message]
            description=High Advisor
            message= _ "Invade our most holy cavern at your peril. Long have we protected our sacred burial grounds from your foul kind, and we shall scatter your bones next to those of our ancestors."
        [/message]

        [message]
            description=Troll Chieftain
            message= _ "Destroy the invaders! Make them pay for their slaughter!"
        [/message]
    [/event]

    # Event 24: Troll Undead Uprising

#define TROLL_UNDEAD_UPRISING

    [message]
        description=High Advisor
        message= _ "Arise, our brothers of ages past! Arise and destroy the intruders!"
    [/message]

    {UNIT_ (Walking Corpse) (troll_wc_1) ("") 4 39 22}
    {INVOKE_TROLL_VARIATION description=troll_wc_1}

    {UNIT_ (Walking Corpse) (troll_wc_2) ("") 4 40 28}
    {INVOKE_TROLL_VARIATION description=troll_wc_2}

    {UNIT_ (Walking Corpse) (troll_wc_3) ("") 4 36 23}
    {INVOKE_TROLL_VARIATION description=troll_wc_3}

    {UNIT_ (Walking Corpse) (troll_wc_4) ("") 4 37 29}
    {INVOKE_TROLL_VARIATION description=troll_wc_4}

#ifdef NORMAL

    {UNIT_ (Walking Corpse) (troll_wc_5) ("") 4 33 25}
    {INVOKE_TROLL_VARIATION description=troll_wc_5}

    {UNIT_ (Walking Corpse) (troll_wc_6) ("") 4 34 28}
    {INVOKE_TROLL_VARIATION description=troll_wc_6}

#endif

#ifdef HARD
    {UNIT_ (Walking Corpse) (troll_wc_5) ("") 4 42 21}
    {INVOKE_TROLL_VARIATION description=troll_wc_5}

    {UNIT_ (Walking Corpse) (troll_wc_6) ("") 4 43 26}
    {INVOKE_TROLL_VARIATION description=troll_wc_6}

#endif

#enddef

    # count number of elves that have entered the trolls cave. Once 3 elves
    # are in the cave, then active the troll undead uprising

    [event]
        name=new turn
        first_time_only=no

        [store_locations]
            x=35-50
            y=23-29
            [filter]
                side=1
            [/filter]
            variable=troll_list
        [/store_locations]

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

        {CLEAR_VARIABLE troll_list}

        [if]
            [variable]
                name=temp
                greater_than_equal_to=3
            [/variable]

            [then]
                [if]
                    [variable]
                        name=troll_undead_arose
                        numerical_equals=0
                    [/variable]

                    [then]
                        {TROLL_UNDEAD_UPRISING}

                        [set_variable]
                            name=troll_undead_arose
                            value=1
                        [/set_variable]
                    [/then]
                [/if]
            [/then]
        [/if]
    [/event]

    [event]
        name=die

        [filter]
            description=Troll Chieftain
        [/filter]

        [message]
            description=Troll Chieftain
            message= _ "Argh! Curse you! May you never live to see daylight again!"
        [/message]

        [message]
            speaker=second_unit
            message= _ "And so, at last, it ends."
        [/message]

        [message]
            side=4
            message= _ "Da big troll is dead. Run for your lives!"
        [/message]

        [kill]
            side=4
            animate=no
        [/kill]

        [terrain]
            x=41,41,42,43,44,45,45
            y=15,16,16,17,17,18,19
            terrain=Uu
        [/terrain]

        [move_unit_fake]
            type=Dwarvish Scout
            x=41,41,42,43,44,45,45
            y=15,16,16,17,17,18,19
        [/move_unit_fake]

        {UNIT_ (Dwarvish Pathfinder) (Grendel) ( _ "Grendel") 2 45 19}

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

        [delay]
            time=200
        [/delay]

        [message]
            description=Grendel
            message= _ "News travels fast. The chaos you have sown has caused the foul trolls to start retreating. And now the architect of our suffering is dead. This war is far from over, but with your help we won this battle. You have our gratitude. Our King has instructed us to bring you to him; he wants to talk with you and reward you. He waits in our most hallowed hall, a place that no elf has seen for generations upon generations. It is a great honor, but you have done great deeds this day. Elves killing a troll chieftain! We will tell this story for years."
        [/message]

        [message]
            description=Nym
            message= _ "With their knowledge of all these secret tunnels, you'd think they could have led us straight here instead of making us go through those 'light defenses'. It would have saved us a lot of unnecessary fighting in actually getting to the troll chieftain."
        [/message]

        [message]
            description=Zhul
            message= _ "Perhaps they wanted to further test our prowess in battle. And besides, every troll we kill is one they don't have to. Still, I think we caused a bigger distraction then they were expecting."
        [/message]

        [message]
            description=Kaleh
            message= _ "Shhhh, you two. Yes, of course, we would be honored to come and meet your King. But first, we left many of our people back up near the entrance to the great cave where you first met us and I fear that even now those caves aren't safe. Can you help us escort my people to safety?"
        [/message]

        [message]
            description=Grendel
            message= _ "Hmmmm, yes, after what you have done, I think I could arrange something. We have a few larger halls that should hold your people, a bit cramped, but safe. Since you first arrived, we've had a few lads watch over them; you hid your folk in a good defensive location, but you can never be too sure. I'll get them to help escort your people to safety. We certainly wouldn't want any surprises."
        [/message]

        [message]
            description=Kaleh
            message= _ "You are full of surprises. But I feel better knowing a few of your kind were watching out for the rest of my people. All right everyone, no celebrating yet, we still have work left to do!"
        [/message]

        [endlevel]
            result=victory

            bonus=yes
        [/endlevel]
    [/event]

    # TROLL EASTER EGG EVENTS

    # Event 25: Dwarf Ghost

    [event]
        name=moveto
        first_time_only=no

        # wmllint: recognize Dwarf Ghost
        [filter]
            x=7-11
            y=17-21
            side=1
            [not]
                type=Dust Devil
            [/not]
        [/filter]

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

            [then]
                [if]
                    [variable]
                        name=met_ghost
                        numerical_equals=0
                    [/variable]

                    [then]
                        [set_variable]
                            name=met_ghost
                            value=1
                        [/set_variable]

                        [remove_shroud]
                            x=7-12
                            y=17-21
                            side=1
                        [/remove_shroud]

                        [message]
                            speaker=unit
                            message= _ "It's cooler here, there seems to be a draft to the west."
                        [/message]

                        {UNIT_ (Haunt) (Dwarf Ghost) ( _ "Dwarf Ghost") 2 8 20}

                        [delay]
                            time=300
                        [/delay]

                        [message]
                            description=Dwarf Ghost
                            message= _ "Hail, friend. Ages ago, I too fought the trolls and came to this place. But by ill luck I was burned to death by the lava and died nearby unblessed and unhonored. Find my body and grant me the peace I so dearly wish for. Do this and I will let you pass."
                        [/message]
                    [/then]
                [/if]
            [/then]
        [/if]
    [/event]

    # Event 26: Dwarf Corpse

    [event]
        name=moveto
        first_time_only=no

        [filter]
            x=14
            y=12-13
            side=1
            [not]
                type=Dust Devil
            [/not]
        [/filter]

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

            [then]
                [message]
                    speaker=unit
                    message= _ "Look, a crumbling skeleton. I think this might be the body of the dwarven ghost. It shouldn't take long to dig a shallow grave."
                [/message]

                [redraw]
                [/redraw]

                [delay]
                    time=1000
                [/delay]

                [message]
                    speaker=unit
                    message= _ "May Eloh, or whatever god you worship, grant you peace and safe passage to the afterlife. We will avenge your death."
                [/message]

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

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

                # kills dwarf ghost blocking passage
                [kill]
                    description=Dwarf Ghost
                    animate=no
                [/kill]

                # create new unit so that ghost can deliver final dialogue before leaving
                {UNIT_ (Haunt) (Dwarf Ghost) ( _ "Dwarf Ghost") 2 14 13}

                [message]
                    description=Dwarf Ghost
                    message= _ "Thank you. You have done for me what all my dwarven kin never could. I will no longer block your way. May Moradin bless you and watch over you. I leave now for the halls of my ancestors..."
                [/message]

                [kill]
                    description=Dwarf Ghost
                    animate=yes
                [/kill]
            [/then]
        [/if]
    [/event]

    # Event 27: Troll Crypt Guardian

    [event]
        name=moveto

        [filter]
            x=7-9
            y=20-25
            side=1
            [not]
                type=Dust Devil
            [/not]
        [/filter]

        {UNIT_ Soulless cryptguard ( _ "Crypt Guardian") 4 7 25}
        {INVOKE_TROLL_VARIATION description=cryptguard}

        [message]
            speaker=unit
            message= _ "This looks like a troll crypt. Whoever it was must have been very important, because they have their own undead guardian."
        [/message]
    [/event]

    # Event 28: Troll Coffin

    [event]
        name=moveto

        [filter]
            x=5-6
            y=26
            side=1
            [not]
                type=Dust Devil
            [/not]
        [/filter]

        [message]
            speaker=unit
            message= _ "There is a chasm here cutting off the end of the crypt. It must be rather recent, it cuts off the path leading to a rather ornate stone coffin."
        [/message]
    [/event]

    # Ransack Troll Coffin

    [event]
        name=moveto
        first_time_only=no

        [filter]
            x=3
            y=28
            side=1
            [not]
                type=Dust Devil
            [/not]
        [/filter]

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

            [then]
                [message]
                    speaker=unit

                    message= _ "For a troll this is quite an ornate tomb. The coffin itself is quite impressive. Inside, the skeleton has crumbled to dust and there are a few colored stones and trinkets, but what really sticks out is this emerald wand. I don't have much experience with magical items, but the asp with emerald eyes and large fangs carved around its shaft leave little doubt as to its power. We don't normally tolerate using poison, but extreme circumstances call for extreme measures and I have a feeling we may find this useful before our journey is over."
                    [option]
                        message= _ "It might be useful, I'll take it."
                        [command]
                            [set_variable]
                                name=took_wand
                                value=1
                            [/set_variable]
                        [/command]
                        [command]
                            [message]
                                speaker=unit
                                message= _ "The wand fits comfortably in my hand. It doesn't seem to have much of a range, but in close combat it could be quite useful."
                            [/message]
                        [/command]

                        [command]
                            [object]
                                [filter]
                                    x=3
                                    y=28
                                    side=1
                                [/filter]

                                id=Troll Wand
                                name= _ "Emerald Wand of Poison"
                                description= _ "This wand make this unit's melee attacks deal poison damage."

                                [effect]
                                    apply_to=attack
                                    range=melee
                                    [set_specials]
                                        {WEAPON_SPECIAL_POISON}
                                    [/set_specials]
                                [/effect]
                            [/object]
                        [/command]
                    [/option]

                    [option]
                        message= _ "On second thought, it's better to leave the dead in peace."
                    [/option]
                [/message]
            [/then]
        [/if]
    [/event]

    #at victory, clear variables:

    [event]
        name=victory

        {CLEAR_VARIABLE destroy_wall}

        {CLEAR_VARIABLE i}
        {CLEAR_VARIABLE temp}
        {CLEAR_VARIABLE tempx}
        {CLEAR_VARIABLE tempy}

        {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_troll_lair}
    [/event]

    [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]

    #if dwarf prisoner Rogrimir dies in battle, record him

    [event]
        name=die

        [filter]
            description=Rogrimir
        [/filter]

        [set_variable]
            name=rog_died
            value=1
        [/set_variable]
    [/event]

    {@campaigns/Under_the_Burning_Suns/utils/kaleh-abilities.cfg}
    {@campaigns/Under_the_Burning_Suns/utils/deaths.cfg}
    {KALEH_DEATH}
    {FRIEND_DEATH}
    {ELYSSA_DEATH}

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