#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}"

    {STORY_IN_THE_TUNNELS_OF_THE_TROLLS}

    {SCENARIO_MUSIC "the_deep_path.ogg"}

    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
        #this is the default
        colour=red
        id=Kaleh
        type=Desert Fighter
        canrecruit=yes
        gold=200
        {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
        user_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
        user_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
        user_team_name=_"Monsters"

#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,fighter
#endif
#ifdef NORMAL
            recruitment_pattern=fighter,fighter,mixed fighter
#endif
#ifdef HARD
            recruitment_pattern=fighter,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
        user_team_name=_"Monsters"

#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,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
        user_team_name=_"Monsters"

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

    {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

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

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

            note={EARLY_FINISH_BONUS_NOTE}+{NEW_GOLD_CARRYOVER_NOTE_40}
        [/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]
            id=Nym
        [/recall]
        [recall]
            id=Zhul
        [/recall]
        [recall]
            id=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
            id=Grimnir
            name= _ "Grimnir"
            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
            id=Troll Interrogator
            name= _ "Troll Interrogator"
            x=34
            y=5
            side=3
            ai_special=guardian
            facing=sw
            [modifications]
                {TRAIT_STRONG}
                {TRAIT_INTELLIGENT}
            [/modifications]
        [/unit]

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

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

#ifdef HARD
        [unit]
            type=Troll Whelp
            name= _ "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
            role=Troll High Shaman
            name= _ "Troll High Shaman"
            x=16
            y=22
            side=4
            ai_special=guardian
            [modifications]
                {TRAIT_LOYAL}
                {TRAIT_INTELLIGENT}
                [object]
                    id=t3
                    silent=yes
                    [effect]
                        apply_to=movement_costs
                        replace=yes
                        [movement_costs]
                            cave={UNREACHABLE}
                            flat={UNREACHABLE}
                        [/movement_costs]
                    [/effect]
                [/object]
            [/modifications]
            {IS_LOYAL}
        [/unit]

        [unit]
            type=Troll Shaman
            role=Troll High Shaman
            name= _ "Troll High Shaman"
            x=18
            y=23
            side=4
            ai_special=guardian
            [modifications]
                {TRAIT_LOYAL}
                {TRAIT_RESILIENT}
                [object]
                    id=t4
                    silent=yes
                    [effect]
                        apply_to=movement_costs
                        replace=yes
                        [movement_costs]
                            cave={UNREACHABLE}
                            flat={UNREACHABLE}
                        [/movement_costs]
                    [/effect]
                [/object]
            [/modifications]
            {IS_LOYAL}
        [/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]
            speaker=Grimnir
            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]
            speaker=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]
            speaker=Zhul
            message= _ "So you dug these tunnels all by yourselves?"
        [/message]

        [message]
            speaker=Grimnir
            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]
            speaker=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]
            speaker=Grimnir
            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]
                    id=Troll Brute
                    name= _ "Troll Brute"
                    type=Troll
                    x=23
                    y=9
                    side=3
                    canrecruit=yes
                    upkeep=free
                    [modifications]
                        {TRAIT_STRONG}
                        {TRAIT_RESILIENT}
                    [/modifications]
                [/unit]

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

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

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

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

                # Have Grimnir blow the explosives to destroy the wall

                [message]
                    speaker=Grimnir
                    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]

                [redraw]
                [/redraw]

                {PLACE_IMAGE scenery/rubble.png 16 7}

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

                # Have Grimnir say goodbye

                [message]
                    speaker=Grimnir
                    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]
                    id=Grimnir
                    animate=no
                    fire_event=no
                [/kill]

                [move_unit_fake]
                    type=Dwarvish Pathfinder
                    side=2
                    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}

                [redraw][/redraw]

                # Have troll guards talk

                [message]
                    speaker=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= _ "Grimnir 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=interrogation

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

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

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

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

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

        [message]
            speaker=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]
            speaker=Ulg
            message= _ "Yes master, I'll make him suffer."
        [/message]

        {MOVE_UNIT id=Ulg 40 4}

        [animate_unit]
            [filter]
                id=Troll Interrogator
            [/filter]
            flag=attack
            hits=yes
            [primary_attack]
                name=club
            [/primary_attack]
            [facing]
                [filter]
                    id=Wounded Dwarf
                [/filter]
            [/facing]

            [animate]
                [filter]
                    id=Wounded Dwarf
                [/filter]
                flag=defend
                hits=yes
                [secondary_attack]
                    name=axe
                [/secondary_attack]
                [facing]
                    [filter]
                        id=Troll Interrogator
                    [/filter]
                [/facing]
            [/animate]
        [/animate_unit]

        [message]
            speaker=Wounded Dwarf
            message= _ "Aaahhhh!"   # wmllint: no spellcheck
        [/message]

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

        [message]
            speaker=$explorer.id
            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]
        name=rogrimir

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

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

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

    [event]
        name=moveto
        first_time_only=no

        [if]
            [have_unit]
                [and]
                    id=Troll Interrogator

                    [or]
                        id=Ulg
                    [/or]
                [/and]
                [filter_vision]
                    visible=yes
                    viewing_side=1
                [/filter_vision]
            [/have_unit]

            [variable]
                name=interrogation_seen
                boolean_equals=no
            [/variable]

            [then]
                {CHECK_EXPLORER}
                [fire_event]
                    name=interrogation
                [/fire_event]

                {VARIABLE interrogation_seen yes}

                # Event 2.2: Wounded Dwarf

                # Troll whelp tries to kill wounded Dwarvish Stalwart

                [event]
                    name=moveto
                    first_time_only=no

                    [if]
                        [have_unit]
                            id=Ulg
                            side=3
                            [filter_vision]
                                visible=yes
                                viewing_side=1
                            [/filter_vision]
                        [/have_unit]

                        [then]
                            [fire_event]
                                name=rogrimir
                            [/fire_event]
                        [/then]
                        [else]
                            [allow_undo]
                            [/allow_undo]
                        [/else]
                    [/if]
                [/event]
            [/then]
            [else]
                [allow_undo]
                [/allow_undo]
            [/else]
        [/if]
    [/event]

    # when Ulg dies, Rogrimir thanks player

    [event]
        name=die

        [filter]
            id=Ulg
        [/filter]

        [message]
            speaker=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]

        {MODIFY_UNIT id=Rogrimir side  1}
        {MODIFY_UNIT id=Rogrimir moves 5}
    [/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=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.id
                    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]
                        id=$unit_on_lava.id
                        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 Dust Devil
        [redraw]
        [/redraw]

        [store_locations]
            x=1-33
            y=11-27
            [filter]
                side=1,3,4
                [not]
                    role=Guardian Phoenix
                [/not]
                [not]
                    id=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.id
                    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]
            speaker=$explorer.id
            message= _ "Whoa. This place is hot."
        [/message]

        [message]
            speaker=$explorer.id
            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]
            role=Troll High Shaman
            message= _ "They have broken through the outer guard-line. 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]

        [redraw][/redraw]

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

        [message]
            speaker=$explorer.id
            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]
            speaker=$explorer.id
            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

        [heal_unit]
            [filter]
                x,y=22,26
            [/filter]
            amount=10
            animate=yes
        [/heal_unit]
    [/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]
                    role=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
                {NAMED_NOTRAIT_UNIT 5 (Lava Monster) 26 21 () ( _ "Guardian Phoenix")}
                [+unit]
                    role=Guardian Phoenix
                [/unit]
#endif

#ifdef NORMAL
                {NAMED_NOTRAIT_UNIT 5 (Lava Monster2) 26 21 () ( _ "Guardian Phoenix")}
                [+unit]
                    role=Guardian Phoenix
                [/unit]
#endif

#ifdef HARD
                {NAMED_NOTRAIT_UNIT 5 (Lava Monster3) 26 21 () ( _ "Guardian Phoenix")}
                [+unit]
                    role=Guardian Phoenix
                [/unit]
#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
                        {NAMED_NOTRAIT_UNIT 5 (Lava Monster) 26 21 () ( _ "Guardian Phoenix")}
                        [+unit]
                            role=Guardian Phoenix
                        [/unit]
#endif

#ifdef NORMAL
                        {NAMED_NOTRAIT_UNIT 5 (Lava Monster2) 26 21 () ( _ "Guardian Phoenix")}
                        [+unit]
                            role=Guardian Phoenix
                        [/unit]
#endif

#ifdef HARD
                        {NAMED_NOTRAIT_UNIT 5 (Lava Monster3) 26 21 () ( _ "Guardian Phoenix")}
                        [+unit]
                            role=Guardian Phoenix
                        [/unit]
#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]
            role=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]
                    role=Troll High Shaman
                    message= _ "Arise, hallowed guardians, and destroy them!"
                [/message]

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

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

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

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

                {CHECK_EXPLORER}
                [message]
                    speaker=$explorer.id
                    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]
                    role=Troll High Shaman
                    message= _ "Arise and attack them now, while they are vulnerable!"
                [/message]

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

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

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

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

                {CHECK_EXPLORER}
                [message]
                    speaker=$explorer.id
                    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]
                    role=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
                {NAMED_NOTRAIT_UNIT 5 (Lava Monster) 22 16 () ( _ "Fire Guardian")}
                {NAMED_NOTRAIT_UNIT 5 (Lava Monster) 23 17 () ( _ "Fire Guardian")}
#endif

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

#ifdef HARD
                {NAMED_NOTRAIT_UNIT 5 (Lava Monster3) 23 17 () ( _ "Fire Guardian")}
                {NAMED_NOTRAIT_UNIT 5 (Lava Monster3) 22 16 () ( _ "Fire Guardian")}
                {NAMED_NOTRAIT_UNIT 5 (Lava Monster3) 22 17 () ( _ "Fire Guardian")}
#endif
                {CHECK_EXPLORER}
                [message]
                    speaker=$explorer.id
                    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
            role=Troll High Shaman
            name= _ "Troll High Shaman"
            x=16
            y=22
            side=4
            [modifications]
                {TRAIT_LOYAL}
                {TRAIT_INTELLIGENT}
                [object]
                    id=t2
                    silent=yes
                    [effect]
                        apply_to=movement_costs
                        replace=yes
                        [movement_costs]
                            cave={UNREACHABLE}
                            flat={UNREACHABLE}
                        [/movement_costs]
                    [/effect]
                [/object]
            [/modifications]
            {IS_LOYAL}
        [/unit]

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

        [move_unit_fake]
            type=Troll Shaman
            side=3
            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

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

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

        [unit]
            type=Troll Shaman
            role=Troll High Shaman
            name= _ "Troll High Shaman"
            x=26
            y=28
            side=4
            [modifications]
                {TRAIT_LOYAL}
                {TRAIT_RESILIENT}
                [object]
                    id=t1
                    silent=yes
                    [effect]
                        apply_to=movement_costs
                        replace=yes
                        [movement_costs]
                            cave={UNREACHABLE}
                            flat={UNREACHABLE}
                        [/movement_costs]
                    [/effect]
                [/object]
            [/modifications]
            {IS_LOYAL}
        [/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
            id=Troll Chieftain
            name= _ "Troll Chieftain"
            canrecruit=yes
            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

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

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

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

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

        [message]
            speaker=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]
            speaker=Troll Chieftain
            message= _ "Destroy the invaders! Make them pay for their slaughter!"
        [/message]
    [/event]

    # Event 24: Troll Undead Uprising

#define TROLL_UNDEAD_UPRISING

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

    {NAMED_NOTRAIT_UNIT 4 (Walking Corpse) 39 22 (troll_wc_1) ("")}
    {INVOKE_TROLL_VARIATION id=troll_wc_1}

    {NAMED_NOTRAIT_UNIT 4 (Walking Corpse) 40 28 (troll_wc_2) ("")}
    {INVOKE_TROLL_VARIATION id=troll_wc_2}

    {NAMED_NOTRAIT_UNIT 4 (Walking Corpse) 36 23 (troll_wc_3) ("")}
    {INVOKE_TROLL_VARIATION id=troll_wc_3}

    {NAMED_NOTRAIT_UNIT 4 (Walking Corpse) 37 29 (troll_wc_4) ("")}
    {INVOKE_TROLL_VARIATION id=troll_wc_4}

#ifdef NORMAL

    {NAMED_NOTRAIT_UNIT 4 (Walking Corpse) 33 25 (troll_wc_5) ("")}
    {INVOKE_TROLL_VARIATION id=troll_wc_5}

    {NAMED_NOTRAIT_UNIT 4 (Walking Corpse) 34 28 (troll_wc_6) ("")}
    {INVOKE_TROLL_VARIATION id=troll_wc_6}

#endif

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

    {NAMED_NOTRAIT_UNIT 4 (Walking Corpse) 43 26 (troll_wc_6) ("")}
    {INVOKE_TROLL_VARIATION id=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=last breath

        [filter]
            id=Troll Chieftain
        [/filter]

        [message]
            speaker=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
            # wmllint: local spelling Da
            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
            side=2
            x=41,41,42,43,44,45,45
            y=15,16,16,17,17,18,19
        [/move_unit_fake]

        {NAMED_NOTRAIT_UNIT 2 (Dwarvish Pathfinder) 45 19 (Grimnir) ( _ "Grimnir")}

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

        [redraw][/redraw]

        [delay]
            time=200
        [/delay]

        [message]
            speaker=Grimnir
            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]
            speaker=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]
            speaker=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]
            speaker=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]
            speaker=Grimnir
            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]
            speaker=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
            {NEW_GOLD_CARRYOVER 40}
        [/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
        [/filter]

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

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

                {CHECK_EXPLORER}

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

                [terrain]
                    x,y=8,22
                    terrain=Uu
                [/terrain]

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

                [redraw][/redraw]

                [delay]
                    time=300
                [/delay]

                [message]
                    speaker=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]

                {CLEAR_VARIABLE explorer}
            [/then]

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

    # Event 26: Dwarf Corpse

    [event]
        name=moveto
        first_time_only=no

        [filter]
            x=14
            y=12-13
            side=1
        [/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]
                    id=Dwarf Ghost
                    animate=no
                [/kill]

                [terrain]
                    x=7-9
                    y=23
                    terrain=Re
                [/terrain]

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

                [redraw]
                [/redraw]

                [message]
                    speaker=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]
                    id=Dwarf Ghost
                    animate=yes
                [/kill]
            [/then]

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

    # Event 27: Troll Crypt Guardian

    [event]
        name=moveto

        [filter]
            x=7-9
            y=20-25
            side=1
        [/filter]

        {NAMED_NOTRAIT_UNIT 4 (Soulless) 7 25 cryptguard ( _ "Crypt Guardian")}
        {INVOKE_TROLL_VARIATION id=cryptguard}

        {CHECK_EXPLORER}

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

        {CLEAR_VARIABLE explorer}
    [/event]

    # Event 28: Troll Coffin

    [event]
        name=moveto

        [filter]
            x=5-6
            y=26
            side=1
        [/filter]

        {CHECK_EXPLORER}

        [message]
            speaker=$explorer.id
            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]

        {CLEAR_VARIABLE explorer}
    [/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]

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

                            [object]
                                [filter]
                                    x,y=$x1,$y1
                                [/filter]

                                id=Troll Wand
                                name= _ "Emerald Wand of Poison"
                                description= _ "This wand makes 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."

                        [command]
                            [allow_undo]
                            [/allow_undo]
                        [/command]
                    [/option]
                [/message]
            [/then]

            [else]
                [allow_undo]
                [/allow_undo]
            [/else]
        [/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]
            speaker=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]
            id=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}
[/scenario]
