#textdomain wesnoth-utbs

#define UNDEAD_TROLL_DESCRIPTION
_ "These undead trolls fight again in death as they did in life, except this time they smell much worse."+{SPECIAL_NOTES}+{SPECIAL_NOTES_PLAGUE} #enddef

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

    {UTBS_MAP 06a_In_the_Tunnels_of_the_Trolls.map}

    {STORY_IN_THE_TUNNELS_OF_THE_TROLLS}

    {SCENARIO_MUSIC "the_deep_path.ogg"}

    next_scenario=07a_Dealing_with_Dwarves
    {TURNS 66 62 60}
    victory_when_enemies_defeated=no

    {UNDERGROUND}

    #Elf player

    [side]
        side=1
        #this is the default
        color=red
        id=Kaleh
        type=Quenoth Youth
        canrecruit=yes
        gold=200
        {INCOME 5 3 1}
        controller=human
        shroud=yes
        fog=no
        team_name=dwarf_ally
        user_team_name= _ "team_name^Quenoth Elves"
        {FLAG_VARIANT long}
    [/side]

    #Side=2 dwarf 1 (dwarf allies)
    [side]
        side=2
        color=orange
        no_leader=yes
        gold=0
        income=0
        controller=ai
        shroud=no
        fog=no
        team_name=dwarf_ally
        user_team_name=_"Dwarf Ally"
        {FLAG_VARIANT knalgan}
    [/side]

    #Side=3 troll 1 (advance guard trolls at outpost)
    [side]
        side=3
        color=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

            aggression=0.6
        [/ai]
    [/side]

    #Side=4 troll 2 (elite trolls in main lair)
    [side]
        side=4
        color=brown
        no_leader=yes
        gold=0
        {NO_INCOME}
        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
            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

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

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

    [event]
        name=prestart

        #set starting scenario objectives

        [objectives]
            summary= _ "Starting Objectives:"
            [objective]
                description= _ "Kill troll leader"
                condition=win
            [/objective]
            [objective]
                description= _ "Death of Kaleh"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Nym"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Zhul"
                condition=lose
            [/objective]

            {TURNS_RUN_OUT}

            [gold_carryover]
                bonus=yes
                carryover_percentage=40
            [/gold_carryover]
        [/objectives]

        {INCREASE_RECRUIT_COSTS 1}

        # show where to go at start
        {PLACE_IMAGE items/gohere.png 15 7}

        #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 27}
        {PLACE_IMAGE items/burial.png 33 26}
        {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 28}

        #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

        # heat damage by just standing in the lava cavern

        [set_variable]
            name=heat_damage
            value={ON_DIFFICULTY 2 3 4}
        [/set_variable]

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

        [set_variable]
            name=flame_counter
            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
                    [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
                    [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, gray 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]

        {REMOVE_IMAGE 15 7}

        # at start of player’s next turn, blow charges and destroy wall
        [event]
            name=new turn

            # Modify troll's income

            [modify_side]
                side=3
                {INCOME 10 12 14}
                {GOLD 75 100 125}
            [/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
                [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")}

#ifndef EASY
            {NAMED_NOTRAIT_UNIT 3 (Troll Whelp) 21 19 () ( _ "Troll Guard")}
#endif

            # Have Grimnir blow the explosives to destroy the wall

            [message]
                speaker=Grimnir
                message= _ "Fire in the hole!"
            [/message]

            [sound]
                name=fuse.ogg
            [/sound]

            # Have screen flash red

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

            [redraw]
            [/redraw]

            [delay]
                time=1000
            [/delay]

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

            [redraw]
            [/redraw]

            [sound]
                name=explosion.ogg
            [/sound]

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

            [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 return 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=0-2,1
                y=4,5
                terrain=Xu
            [/terrain]

            {PLACE_IMAGE scenery/rubble.png 3 5}

            [redraw][/redraw]

            # Have troll guards talk

            [message]
                speaker=Troll Guard
                message= _ "Intruders! Kill them!"
            [/message]
        [/event]
    [/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= _ "Aaahh!" # 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/dwarves/fighter-2.png
            x=41
            y=4
            side=2
            hitpoints=20
            unrenamable=yes
            [modifications]
                {TRAIT_LOYAL}
                {TRAIT_QUICK}
            [/modifications]
            {IS_LOYAL}
        [/unit]

        [remove_shroud]
            x=34-37,38,39-41
            y=4-8,3-7,3-6
            side=1
        [/remove_shroud]

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

    [event]
        name=moveto

        [filter_condition]
            [have_unit]
                id=Troll Interrogator,Ulg
                [filter_vision]
                    visible=yes
                    side=1
                [/filter_vision]
            [/have_unit]
        [/filter_condition]

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

        # Event 2.2: Wounded Dwarf

        # Troll whelp tries to kill wounded Dwarvish Stalwart

        [event]
            name=moveto

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

            [fire_event]
                name=rogrimir
            [/fire_event]
        [/event]
    [/event]

    # when Ulg dies, Rogrimir thanks player

    [event]
        name=die

        [filter]
            id=Ulg
        [/filter]

        {VARIABLE saved_rogrimir yes}

        [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]
            [filter]
                id=Rogrimir
            [/filter]
            side=1
            moves=5
        [/modify_unit]
    [/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 shydes which can move 3 hexes

    # At the start of each turn, all units except winged ones standing
    # on a lava hex take 25 damage. This damage can kill units.

    [event]
        name=new turn
        first_time_only=no

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

        [harm_unit]
            [filter]
                side=1,3,4
                [not]
                    type=Quenoth Sun Sylph,Quenoth Shyde,Dust Devil
                [/not]
                [filter_location]
                    terrain=*^Yl
                [/filter_location]
            [/filter]
            amount=25
            damage_type=fire
            animate=yes
        [/harm_unit]
    [/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 2,3,4 damage
    # from the heat. This damage is non-lethal.

    [event]
        name=new turn
        first_time_only=no

        [harm_unit]
            [filter]
                side=1,3,4
                [not]
                    type=Dust Devil
                [/not]
                [filter_location]
                    x=1-33
                    y=11-27
                    terrain=!,*^Yl,Wwg
                [/filter_location]
            [/filter]
            amount=$heat_damage
            damage_type=fire
            kill=no
        [/harm_unit]
    [/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]

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

        [sound]
            name=rumble.ogg
        [/sound]

        # Have screen flash red

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

        [redraw]
        [/redraw]

        [delay]
            time=200
        [/delay]

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

        [redraw]
        [/redraw]

        [sound]
            name=flame-big.ogg
        [/sound]

        # Have screen shake, then destroy most of bridge

        {UTBS_SHAKE_SCREEN}

        [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=Qlf^Yl
        [/terrain]

        [redraw][/redraw]

        [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 those who can fly over the lava, will take 25 damage at the beginning of the next turn. This lava damage can kill units. Flying units will just take $heat_damage damage per turn when flying over lava, though they too can die if they spend too much time over it. Also because of the heat in the cavern, all units on cave floor hexes will take $heat_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]

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

        {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 $heat_damage damage each turn from the heat."
            image=wesnoth-icon.png
        [/message]
    [/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]

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

                [redraw]
                [/redraw]

                [delay]
                    time=100
                [/delay]

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

                [redraw]
                [/redraw]

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

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

#ifdef HARD
                {NAMED_NOTRAIT_UNIT 5 (Fire Guardian3) 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]
                        [color_adjust]
                            red,green,blue=255,0,0
                        [/color_adjust]

                        [redraw]
                        [/redraw]

                        [delay]
                            time=100
                        [/delay]

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

                        [redraw]
                        [/redraw]

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

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

#ifdef HARD
                        {NAMED_NOTRAIT_UNIT 5 (Fire Guardian3) 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]

                {NAMED_NOTRAIT_UNIT 5 (Fire Guardian2) 26 17 () ( _ "Fire Guardian")}
                {NAMED_NOTRAIT_UNIT 5 (Fire Guardian2) 26 18 () ( _ "Fire Guardian")}

#ifdef HARD
                {NAMED_NOTRAIT_UNIT 5 (Fire Guardian3) 25 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]

                {NAMED_NOTRAIT_UNIT 5 (Fire Guardian3) 26 22 () ( _ "Fire Guardian")}
                {NAMED_NOTRAIT_UNIT 5 (Fire Guardian3) 27 22 () ( _ "Fire Guardian")}
#ifdef HARD
                {NAMED_NOTRAIT_UNIT 5 (Fire Guardian3) 26 21 () ( _ "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]

                {NAMED_NOTRAIT_UNIT 5 (Fire Guardian3) 23 17 () ( _ "Fire Guardian")}
                {NAMED_NOTRAIT_UNIT 5 (Fire Guardian3) 22 16 () ( _ "Fire Guardian")}
#ifdef HARD
                {NAMED_NOTRAIT_UNIT 5 (Fire Guardian3) 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}
            [/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 29 () ( _ "Troll Reinforcements")}
        {NAMED_GENERIC_UNIT 3 (Troll Whelp) 28 28 () ( _ "Troll Reinforcements")}

        #on challenging/hard add a troll shaman
#ifndef EASY

        [unit]
            type=Troll Shaman
            role=Troll High Shaman
            name= _ "Troll High Shaman"
            x=26
            y=29
            side=4
            [modifications]
                {TRAIT_LOYAL}
                {TRAIT_RESILIENT}
            [/modifications]
            {IS_LOYAL}
        [/unit]

#endif
    [/event]

    # Event 23: Trigger Troll Lord's side

    [event]
        name=moveto

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

        #create troll leader
        [unit]
            type=Great Troll
            id=Troll Chieftain
            name= _ "Troll Chieftain"
            canrecruit=yes
            x=48
            y=22
            side=4
            [modifications]
                {TRAIT_STRONG}
                {TRAIT_RESILIENT}
            [/modifications]
        [/unit]

        [modify_side]
            side=4
            {INCOME 2 4 6}
            {GOLD 120 140 160}
        [/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"
            x=48
            y=23
            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 the murders they have done!"
        [/message]
    [/event]

    # Event 24: Troll Undead Uprising

    # 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

        [filter_condition]
            [have_unit]
                x=35-50
                y=23-29
                side=1
                count=3-99999
            [/have_unit]
        [/filter_condition]

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

        {UNIT 4 (Walking Corpse) 39 22 (variation=troll)}
        {UNIT 4 (Walking Corpse) 40 28 (variation=troll)}
        {UNIT 4 (Walking Corpse) 36 23 (variation=troll)}
        {UNIT 4 (Walking Corpse) 37 29 (variation=troll)}

#ifdef NORMAL
        {UNIT 4 (Walking Corpse) 34 25 (variation=troll)}
        {UNIT 4 (Walking Corpse) 34 28 (variation=troll)}
#endif

#ifdef HARD
        {UNIT 4 (Walking Corpse) 42 21 (variation=troll)}
        {UNIT 4 (Walking Corpse) 43 26 (variation=troll)}
#endif
        [modify_unit]
            [filter]
                type=Walking Corpse
                variation=troll
            [/filter]

            description={UNDEAD_TROLL_DESCRIPTION}
        [/modify_unit]
    [/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 than 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

        [filter]
            x=7-11
            y=17-21
            side=1
        [/filter]

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

    # Event 26: Dwarf Corpse

    [event]
        name=moveto

        [filter]
            x=14
            y=12-13
            side=1
        [/filter]

        [filter_condition]
            [have_unit]
                id=Dwarf Ghost
            [/have_unit]
        [/filter_condition]

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

        # 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. I leave now for the halls of my ancestors..."
        [/message]

        [kill]
            id=Dwarf Ghost
            animate=yes
        [/kill]
    [/event]

    # Event 27: Troll Crypt Guardian

    [event]
        name=moveto

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

        {NAMED_UNIT 4 (Soulless) 7 25 cryptguard ( _ "Crypt Guardian") (variation=troll)}
        [modify_unit]
            [filter]
                id=cryptguard
            [/filter]

            description={UNDEAD_TROLL_DESCRIPTION}
        [/modify_unit]

        {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; the edges are still raw and crumbling. It cuts off the path leading to a rather ornate stone coffin."
        [/message]

        {CLEAR_VARIABLE explorer}
    [/event]

    # Ransack Troll Coffin

    [event]
        name=moveto
        id=take_wand
        first_time_only=no

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

        [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 may find this useful before our journey is over."
            [option]
                label= _ "It might be useful, I’ll take it."
                [command]
                    [remove_event]
                        id=take_wand
                    [/remove_event]

                    [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]
                label= _ "On second thought, it’s better to leave the dead in peace."

                [command]
                    [allow_undo]
                    [/allow_undo]
                [/command]
            [/option]
        [/message]
    [/event]

    #at victory, clear variables:

    [event]
        name=victory

        {CLEAR_VARIABLE heat_damage}
        {CLEAR_VARIABLE summon_flame}
        {CLEAR_VARIABLE flame_counter}
    [/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]

    {UTBS_INCLUDE utils/deaths.cfg}
[/scenario]
