#textdomain wesnoth-utbs

# wmllint: directory spelling Griknagh

[scenario]
    id=05_A_Subterranean_Struggle
    name= _ "A Subterranean Struggle"
    next_scenario=06a_In_the_Tunnels_of_the_Trolls

    {UTBS_MAP 05_A_Subterranean_Struggle.map}
    victory_when_enemies_defeated=no

    {UNDERGROUND}
    {TURNS 45 43 41}

    #TODO add extra music
    {SCENARIO_MUSIC "underground.ogg"}
    {EXTRA_SCENARIO_MUSIC "legends_of_the_north.ogg"}
    {EXTRA_SCENARIO_MUSIC "the_dangerous_symphony.ogg"}

    {STORY_A_SUBTERRANEAN_STRUGGLE}

    [side]
        side=1
        id=Kaleh
        type=Quenoth Youth
        canrecruit=yes
        gold=200
        {INCOME 7 5 3}
        controller=human
        shroud=yes
        fog=no
        {FLAG_VARIANT long}
        user_team_name= _ "team_name^Quenoth Elves"
    [/side]

    #Side=2 troll 1 (blue)
    [side]
        no_leader=yes
        side=2
        gold=0
        income=0
        controller=ai
        shroud=no
        fog=no
        hidden=yes
        team_name=trolls
        user_team_name= _ "team_name^Trolls"

        recruit=Troll Whelp, Troll, Troll Rocklobber
        [ai]
            recruitment_pattern=fighter,fighter,mixed fighter
            aggression=0.8
            caution=0.05
            grouping=offensive

            [goal]
                name=target
                [criteria]
                    side=1
                [/criteria]
                value=10
            [/goal]
            [goal]
                name=target
                [criteria]
                    side=4
                [/criteria]
                value=9
            [/goal]
            [goal]
                name=target
                [criteria]
                    side=5
                [/criteria]
                value=9
            [/goal]
        [/ai]
    [/side]

    #Side=3 troll 2 (green)
    [side]
        side=3
        no_leader=yes
        gold=0
        income=0
        controller=ai
        shroud=no
        fog=no
        hidden=yes
        team_name=trolls
        user_team_name= _ "team_name^Trolls"

        recruit=Troll Whelp, Troll, Troll Rocklobber

        [ai]
            recruitment_pattern=fighter,fighter,mixed fighter

            aggression=0.8
            caution=0.05
            grouping=offensive

            [goal]
                name=target
                [criteria]
                    side=1
                [/criteria]
                value=10
            [/goal]
            [goal]
                name=target
                [criteria]
                    side=4
                [/criteria]
                value=9
            [/goal]
            [goal]
                name=target
                [criteria]
                    side=5
                [/criteria]
                value=9
            [/goal]
        [/ai]
    [/side]

    #Side=4 dwarf 1 (yellow)
    [side]
        side=4
        no_leader=yes
        gold=0
        income=0
        controller=ai
        shroud=no
        fog=no
        hidden=yes
        team_name=dwarves
        user_team_name= _ "team_name^Dwarves"

        recruit=Dwarvish Fighter, Dwarvish Steelclad, Dwarvish Thunderer, Dwarvish Guardsman

        [ai]
            aggression=0.8
            caution=0.05
            grouping=offensive

            [goal]
                name=target
                [criteria]
                    side=1
                [/criteria]
                value=10
            [/goal]
            [goal]
                name=target
                [criteria]
                    side=2
                [/criteria]
                value=9
            [/goal]
            [goal]
                name=target
                [criteria]
                    side=3
                [/criteria]
                value=9
            [/goal]
        [/ai]
        {FLAG_VARIANT knalgan}
    [/side]

    #Side=5 dwarf 2 (purple)
    [side]
        side=5
        no_leader=yes
        gold=0
        income=0
        controller=ai
        shroud=no
        fog=no
        hidden=yes
        team_name=dwarves
        user_team_name= _ "team_name^Dwarves"

        recruit=Dwarvish Fighter, Dwarvish Steelclad, Dwarvish Thunderer, Dwarvish Guardsman

        [ai]
            aggression=0.8
            caution=0.05
            grouping=offensive

            [goal]
                name=target
                [criteria]
                    side=1
                [/criteria]
                value=10
            [/goal]
            [goal]
                name=target
                [criteria]
                    side=2
                [/criteria]
                value=9
            [/goal]
            [goal]
                name=target
                [criteria]
                    side=3
                [/criteria]
                value=9
            [/goal]
        [/ai]
        {FLAG_VARIANT knalgan}
    [/side]

    #Side=6 Ants
    [side]
        side=6
        no_leader=yes
        gold=0
        income=0
        controller=ai
        shroud=no
        fog=no
        hidden=yes
        color=white

        [ai]
            aggression=0.8
            caution=0.1

            village_value=0

            recruitment_pattern=fighter,fighter,fighter,fighter

            #causes ants to target cave spider
            [goal]
                name=target
                [criteria]
                    type=Cave Spider
                [/criteria]
                value=150
            [/goal]

            [goal]
                name=target
                [criteria]
                    side=1
                [/criteria]
                value=100
            [/goal]

            passive_leader=yes
        [/ai]
    [/side]

    #Side=7 Assassin & Cave Spider
    [side]
        side=7
        no_leader=yes
        gold=0
        income=0
        controller=ai
        shroud=yes
        fog=no
        hidden=yes
        color=teal

        [ai]
            aggression=0.90
            caution=0.10
        [/ai]
    [/side]

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

    #fires lighting central cavern
    {ANIMATED_CAMPFIRE 31 32}
    {ANIMATED_CAMPFIRE 33 21}

    [event]
        name=prestart

        #set starting scenario objectives

        [objectives]
            summary= _ "Starting Objectives:"
            [objective]
                description= _ "Explore underground"
                condition=win
            [/objective]
            [objective]
                description= _ "Defeat enemy leaders"
                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}

        #central cavern furnishings

        {PLACE_IMAGE items/rock-cairn3.png 42 21}  # this image is in a different folder because it is not in core, just campaign
        {PLACE_IMAGE scenery/rock-cairn.png 38 20}
        {PLACE_IMAGE scenery/rock-cairn2.png 30 20}

        {PLACE_IMAGE items/burial.png 27 33}
        {PLACE_IMAGE items/burial2.png 35 34}
        {PLACE_IMAGE items/burial.png 43 33}

        #recall heroes
        [recall]
            id=Nym
        [/recall]

        [recall]
            id=Zhul
        [/recall]

        [recall]
            id=Elyssa
        [/recall]

        #create starting elves

        [unit]
            type=Quenoth Fighter
            name= _ "Nantheos"
            x=4
            y=23
            side=1
            [modifications]
                {TRAIT_QUICK}
                {TRAIT_STRONG}
            [/modifications]
        [/unit]

        [unit]
            type=Quenoth Fighter
            name= _ "Sylestria"
            x=5
            y=25
            side=1
            [modifications]
                {TRAIT_INTELLIGENT}
                {TRAIT_RESILIENT}
            [/modifications]
        [/unit]

        # If Elyssa the Mage isn't there, boost starting group a bit

        [if]
            [not]
                [have_unit]
                    id=Elyssa
                [/have_unit]
            [/not]
            [then]
                [unit]
                    type=Quenoth Flanker
                    name= _ "Rygar"
                    x=6
                    y=24
                    side=1
                    [modifications]
                        {TRAIT_LOYAL}
                        {TRAIT_RESILIENT}
                    [/modifications]
                    {IS_LOYAL}
                [/unit]
            [/then]
        [/if]
    [/event]

    # starting events

    [event]
        name=start

        # starting dialogue

        [if]
            [have_unit]
                id=Elyssa
            [/have_unit]

            [then]
                [message]
                    speaker=Kaleh
                    message= _ "You mentioned that dwarves and trolls often lived underground, Elyssa. I’ve only heard myths, have you ever met a dwarf or a troll?"
                [/message]

                [message]
                    speaker=Elyssa
                    message= _ "No I haven’t, I don’t often explore underground unless I have to. There are lots of nasty things that lurk far away from the light of the suns. But I’ve read a bit about them, and have even met a few people who had dealings with dwarves."
                [/message]

                [message]
                    speaker=Nym
                    message= _ "What are dwarves like?"
                [/message]

                [message]
                    speaker=Elyssa
                    message= _ "They’re a proud people, and some would say greedy. They love their gold and fine metals, and forge many beautiful things. I should warn you, they have little liking for elves. There was some betrayal many years ago, though I don’t know what happened."
                [/message]

                [message]
                    speaker=Kaleh
                    message= _ "And what about trolls? I’m not sure I’d want to meet one face to face."
                [/message]

                [message]
                    speaker=Elyssa
                    message= _ "Trolls and dwarves are natural enemies, living so close together. And many would say trolls are little more than brutes and savages. Trolls are huge and very strong, with skin as hard as stone, and can be fearsome foes. But I knew one man long ago who traded with a group of trolls and said they were quite honorable, as long as you didn’t try to cheat them."
                [/message]

                [message]
                    speaker=Zhul
                    message= _ "Well, with Eloh’s guidance, I hope we find these tunnels deserted. I’ll be happy if our biggest problem is not getting lost. I have little wish to meet either dwarves or trolls. But Eloh will watch over us."
                [/message]

                [message]
                    speaker=Kaleh
                    message= _ "Will she? I got the impression she was powerless underground."
                [/message]

                [message]
                    speaker=Zhul
                    message= _ "Where did you get that idea? Certainly Eloh is strongest during the day, when the suns are shining down on us. But it is said that even in the darkest of nights she will protect her faithful. And back during the Golden Age holy elven warriors led great crusades against orcs and other foul things that hid underground, killing them with Eloh’s aid. Faith is our shield, Kaleh. I think you should keep your doubts to yourself; it would not do to unduly worry our people. Eloh will always protect us, if we follow her path."
                [/message]

                [message]
                    speaker=Kaleh
                    message= _ "Then let us hope the rest of our journey may be as uneventful as it has been this far."
                [/message]
            [/then]

            [else]
                [message]
                    speaker=Kaleh
                    message= _ "I’ve heard of dwarves, but do you have any idea, Zhul, what kinds of creatures we might encounter underground?"
                [/message]

                [message]
                    speaker=Zhul
                    message= _ "These tunnels are foreign to me; I know little more than you do. All I know about dwarves is from the few tales from the Golden Age."
                [/message]

                [message]
                    speaker=Nym
                    message= _ "What are they like?"
                [/message]

                [message]
                    speaker=Zhul
                    message= _ "They lived deep under the earth, mining gold and fine metals and forging many beautiful things. We were once allies during the Golden Age, but in the strife and chaos of the fall we lost all contact. I don’t know if any survived. But in the golden age they were very helpful in our wars against the orcs and trolls."
                [/message]

                [message]
                    speaker=Kaleh
                    message= _ "What are trolls?"
                [/message]

                [message]
                    speaker=Zhul
                    message= _ "Trolls were huge gray creatures as big as giants and very strong. They were reclusive creatures, hiding underground. We never had much contact with them, though some fought with the orcs in the great wars. They were mighty warriors. I’m sure they have all died off; I certainly would never want to meet one face to face."
                [/message]

                [message]
                    speaker=Zhul
                    message= _ "But with Eloh’s guidance, I hope we find these tunnels deserted. I’d be happy if our biggest problem is not getting lost. Still, even underground Eloh will watch over us."
                [/message]

                [message]
                    speaker=Kaleh
                    message= _ "Will she? I got the impression she was powerless underground."
                [/message]

                [message]
                    speaker=Zhul
                    message= _ "Where did you get that idea? Certainly Eloh is strongest during the day, when the suns are shining down on us. But it is said that even in the darkest of nights she will protect her faithful. And back during the Golden Age holy elven warriors led great crusades against orcs and other foul things that hid underground, killing them with Eloh’s aid. Faith is our shield, Kaleh. I think you should keep your doubts to yourself; it would not do to unduly worry our people. Eloh will always protect us, if we follow her path."
                [/message]

                [message]
                    speaker=Kaleh
                    message= _ "Then let us hope the rest of our journey may be as uneventful as it has been this far."
                [/message]
            [/else]
        [/if]
    [/event]

    # Event 1: Spider and Ants

    [event]
        name=moveto

        [filter]
            x=12-18
            y=20-29
            side=1
        [/filter]

        [message]
            speaker=Nym
            message= _ "All I’m saying is that these tunnels aren’t as bad as I expected."
        [/message]

        [message]
            speaker=Kaleh
            # wmllint: local spelling Shhh
            message= _ "Shhh! Did you hear something?"
        [/message]

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

        #make ants easy: 3 medium: 4 hard: 5

        {NOTRAIT_UNIT 6 (Giant Ant) 17 26}
        {NOTRAIT_UNIT 6 (Giant Ant) 17 27}
        {NOTRAIT_UNIT 6 (Giant Ant) 18 26}

#ifndef EASY
        {NOTRAIT_UNIT 6 (Giant Ant) 18 27}
#endif

#ifdef HARD
        {NOTRAIT_UNIT 6 (Giant Ant) 19 27}
#endif

        #make spider
        {NOTRAIT_UNIT 7 (Cave Spider) 4 25}

        [redraw]
        [/redraw]

        [scroll_to_unit]
            type=Giant Ant
        [/scroll_to_unit]

        [delay]
            time=400
        [/delay]

        [message]
            speaker=Zhul
            message= _ "Ants. Very big ants. Maybe they won’t be hostile."
        [/message]

        [scroll_to_unit]
            type=Cave Spider
        [/scroll_to_unit]

        [delay]
            time=400
        [/delay]

        [message]
            speaker=Kaleh
            message= _ "On the other hand, that spider probably is."
        [/message]

        [message]
            speaker=Nym
            message= _ "Caught between a spider and its prey. Not a good place to be."
        [/message]
    [/event]

    # If player escapes cave spider, then reward player by killing it

    [event]
        name=moveto

        [filter]
            x=18-24
            y=26-29
            side=1
        [/filter]

        [if]
            [have_unit]
                type=Cave Spider
            [/have_unit]

            [then]
                [kill]
                    type=Cave Spider
                    animate=yes
                [/kill]

                [message]
                    speaker=Nym
                    message= _ "Whoa! Did you see that? That huge stalactite just fell and crushed the spider. Aren’t we lucky!"
                [/message]

                [message]
                    speaker=Zhul
                    message= _ "Eloh must indeed be watching over us."
                [/message]
            [/then]
        [/if]
    [/event]

    # When player approaches large cavern, a dying dwarf runs out and yells a warning.

    [event]
        name=moveto

        [filter]
            x=18-24
            y=26-28
            side=1
        [/filter]

        [message]
            speaker=Nym
            message= _ "You know, if all we discover down here are insects, I’ll be very disappointed."
        [/message]

        [if]
            [have_unit]
                id=Elyssa
            [/have_unit]

            [then]
                [message]
                    speaker=Elyssa
                    message= _ "Spiders aren’t insects."
                [/message]

                [message]
                    speaker=Nym
                    message= _ "Thanks for the clarification."
                [/message]
            [/then]
        [/if]
        [unit]
            type=Dwarvish Fighter
            side=4
            hitpoints=12
            id=Wounded Dwarf
            x=25
            y=28
        [/unit]
        [move_unit]
            id=Wounded Dwarf
            to_x=25,24,23,22,21
            to_y=28,28,29,28,29
        [/move_unit]

        [message]
            speaker=Wounded Dwarf
            message= _ "Help! They’re everywhere!"
        [/message]

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

        [message]
            speaker=Kaleh
            message= _ "Nym, your timing is impeccable."
        [/message]

        [if]
            [have_unit]
                id=Elyssa
            [/have_unit]

            [then]
                [message]
                    speaker=Elyssa
                    message= _ "That’s a dwarf, but it looks like he’s been beaten to a pulp."
                [/message]
            [/then]

            [else]
                [message]
                    speaker=Zhul
                    message= _ "Short and hairy, he must be a dwarf. But he’s been beaten to a pulp."
                [/message]
            [/else]
        [/if]

        [message]
            speaker=Kaleh
            message= _ "I don’t know what ‘they’ are, but we can’t go back. Prepare yourselves for anything, everyone."
        [/message]
    [/event]

    # EVENT 5 IS LISTED OUT OF ORDER BECAUSE FOR SOME REASON IT WASN'T
    # FIRING WHEN PLACED AFTER EVENT 4.5. CURRENTLY THIS EVENT WORKS
    # FINE WHEN IT IS HERE AND EVENT 6 FIRES FINE AFTER 4.5. SO EVEN
    # THOUGH THE EVENTS ARE OUT OF ORDER, I'M LEAVING THEM THIS WAY
    # BECAUSE IT SEEMS TO MAKE EVERYTHING WORK FINE.

    # Event 5: Sighted dwarf/troll leader events

#define DWARF_ALLY_APPROACH_MESSAGES
    {VARIABLE Fundin_approach_message (_ "What are you doing back here? The trolls hide in the southern tunnels, not this way.")}
    {VARIABLE Nori_approach_message (_ "What are you doing back here? The trolls hide in the southern tunnels, not this way.")}
    # wmllint: local spelling stinkin'
    {VARIABLE Thungar_approach_message (_ "Nasty dwarves and stinkin’ elves, we will smash you all!")}
    {VARIABLE Gnarl_approach_message (_ "Kill the elves! We must stop them here. This is our land, crush the intruders!")}
#enddef

#define TROLL_ALLY_APPROACH_MESSAGES
    {VARIABLE Fundin_approach_message (_ "Treacherous elves, how can you fight with such horrid creatures as trolls? I will cleave all in two with my axe!")}
    {VARIABLE Nori_approach_message (_ "If you think you can take these caves from us, then you are fools. We are masters of fighting underground and we will die to defend our home. Fight on, my brothers!")}
    {VARIABLE Thungar_approach_message (_ "What you doing back here? Nasty dwarves are to the north, no dwarves this way. Go back and fight bravely.")}
    {VARIABLE Gnarl_approach_message (_ "What you doing back here? Nasty dwarves are to the north, no dwarves this way. Go back and fight bravely.")}
#enddef

#define APPROACH_LEADER LEADER_ID ENTER_X_SPAN ENTER_Y_SPAN REMOVE_X_SPAN REMOVE_Y_SPAN
    [event]
        name=moveto

        [filter]
            x={ENTER_X_SPAN}
            y={ENTER_Y_SPAN}
            side=1
        [/filter]

        [remove_shroud]
            x={REMOVE_X_SPAN}
            y={REMOVE_Y_SPAN}
            side=1
        [/remove_shroud]

        [message]
            speaker={LEADER_ID}
            message=${LEADER_ID}_approach_message
        [/message]
    [/event]
#enddef

    # NW dwarf
    {APPROACH_LEADER Fundin 23-31 8-17 22-31 7-17}

    # NE dwarf
    {APPROACH_LEADER Nori 41-51 10-16 40-52 9-17}

    # SW troll
    {APPROACH_LEADER Thungar 23-31 37-44 21-32 37-44}

    # SE troll
    {APPROACH_LEADER Gnarl 42-51 37-45 42-51 37-46}
#undef APPROACH_LEADER

    # victory events for trolls and dwarves here to make the ally event easier to follow
#define CLOAK_KILL_OBJECTIVES
    [objectives]
        summary= _ "New Objectives:"
        silent=no
        [objective]
            description= _ "Defeat the Cloaked Figure"
            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]
#enddef

#define TROLL_ALLY_VICTORY
    [event]
        name=troll_victory_test

        [filter_condition]
            [have_unit]
                race=dwarf
                canrecruit=yes
                count=0
            [/have_unit]
        [/filter_condition]

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

        {NAMED_NOTRAIT_UNIT 2 (Troll Shaman) 20 14 (Zurg) ( _ "Zurg")}

        [remove_shroud]
            x=19-23
            y=12-16
            side=1
        [/remove_shroud]

        [redraw]
            side=1
        [/redraw]

        [delay]
            time=200
        [/delay]

        # wmllint: recognize Cloaked Figure
        [message]
            speaker=Zurg
            message= _ "Congratulations! Some of trolls didn’t think you strong enough to beat Dwarves."
        [/message]

        [message]
            speaker=Kaleh
            message= _ "Where did you come from?"
        [/message]

        [message]
            speaker=Zurg
            message= _ "There many secret tunnels that you sun dwellers not know of. Only troll know. We smarter than you think. Zurg would have killed dwarves himself, but he was just sent back from where real fighting is."
        [/message]

        [message]
            speaker=Zhul
            message= _ "The real fighting? I thought that was what we were waist-deep in?"
        [/message]

        [message]
            speaker=Zurg
            # wmllint: local spelling tricksy
            message= _ "While you fighting, another clan of dwarves sneak around and flank us. They tricksy like that. We must leave you and run back to defend women and little trolls. Dwarves never give up, many trolls die today, very hard fighting. But dwarves make mistake, you stronger than dwarf or troll thought. You trolls’ secret weapon."
        [/message]

        [message]
            speaker=Nym
            message= _ "How do you mean?"
        [/message]

        [message]
            speaker=Zurg
            message= _ "Right before battle, we find secret passage just to the north leading straight to big dwarf stronghold. Hiding in stronghold is big important dwarf, directing the battle. Dwarves always think they best fighters around so they leave only a few dwarves guarding their stronghold. If you elves can break through dwarf defenses and kill dwarf chieftain, then it will do much damage to dwarves, make them afraid and confused, easy prey for trolls. You do this and we can drive them back. Then troll leader can help show you how to return to surface. You come with Zurg, he show you way to secret passage."
        [/message]

        [message]
            speaker=Elyssa
            message= _ "Their knowledge of these tunnels is uncanny. I could have sworn a minute ago that that wall was solid rock."
        [/message]

        [if]
            [have_unit]
                id=Cloaked Figure
            [/have_unit]

            [then]
                [message]
                    speaker=Kaleh
                    message= _ "Wait a moment, Zurg, we must deal with this mysterious cloaked figure before we can follow you."
                [/message]

                #set new scenario objectives

                {CLOAK_KILL_OBJECTIVES}
            [/then]

            [else]
                [message]
                    speaker=Kaleh
                    message= _ "It sounds like our work is not yet done. Very well, gather yourselves together, we must follow Zurg."
                [/message]

                [endlevel]
                    result=victory
                    next_scenario=06b_In_the_Domain_of_the_Dwarves
                    bonus=yes
                    {NEW_GOLD_CARRYOVER 40}
                [/endlevel]
            [/else]
        [/if]
    [/event]
#enddef

#define DWARF_ALLY_VICTORY
    [event]
        name=dwarf_victory_test

        [filter_condition]
            [have_unit]
                race=troll
                canrecruit=yes
                count=0
            [/have_unit]
        [/filter_condition]

        [terrain]
            x=24,23,22,21,20,19,18
            y=44,45,45,46,46,46,45
            terrain=Uu
        [/terrain]

        [delay]
            time=200
        [/delay]

        {NAMED_NOTRAIT_UNIT 5 (Dwarvish Pathfinder) 21 46 (Grimnir) ( _ "Grimnir")}

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

        [redraw]
            side=1
        [/redraw]

        [message]
            speaker=Grimnir
            message= _ "Congratulations, some of me boys didn’t think you could beat the trolls."
        [/message]

        [message]
            speaker=Kaleh
            message= _ "Where did you come from?"
        [/message]

        [message]
            speaker=Grimnir
            message= _ "Don’t think you know all the tunnels and passages that twist through these caves, elf. I would have killed him myself, but I was just sent back from the main front of the battle."
        [/message]

        [message]
            speaker=Zhul
            message= _ "The front? I thought this was the front."
        [/message]

        [message]
            speaker=Grimnir
            message= _ "While you were fighting, a separate clan of trolls sneaked around our sentries and flanked us, attacking our supply depots. There are more of those stinking buggers than we had originally thought. To tell you the truth, we are hard pressed. We’re going to have to pull back all our forces in these caves to reinforce the back lines. But your victory here has produced a unexpected opportunity."
        [/message]

        [message]
            speaker=Nym
            message= _ "It has?"
        [/message]

        [message]
            speaker=Grimnir
            message= _ "Right before the trolls overran this area of the mines, our scouts had found an old tunnel south of here that leads almost straight to the main lair of this tribe of trolls. We believe that protected in the lair is one of their main leaders who is directing the battle. We were going to try to sneak in and lead a surprise attack, but frankly we didn’t have enough dwarves to spare. If by using this passage you can sneak past their front lines and kill him, then it will throw the trolls into disarray and relieve the pressure on our front lines. If you do this our King has promised to help you return your people to the sunlit lands. When you’re ready I’ll show you the way. It’s not far."
        [/message]

        [message]
            speaker=Elyssa
            message= _ "Their knowledge of these tunnels is uncanny. I could have sworn a minute ago that that wall was solid rock."
        [/message]

        [if]
            [have_unit]
                id=Cloaked Figure
            [/have_unit]

            [then]
                [message]
                    speaker=Kaleh
                    message= _ "Wait a moment, Grimnir, we must deal with this mysterious cloaked figure before we can follow you."
                [/message]

                {CLOAK_KILL_OBJECTIVES}
            [/then]

            [else]
                [message]
                    speaker=Kaleh
                    message= _ "It sounds like our work is not yet done. Very well, gather yourselves together, we must follow Grimnir."
                [/message]

                [endlevel]
                    result=victory
                    next_scenario=06a_In_the_Tunnels_of_the_Trolls
                    bonus=yes
                    {NEW_GOLD_CARRYOVER 40}
                [/endlevel]
            [/else]
        [/if]
    [/event]
#enddef

    # Event 2: Entering the large cavern

    #player chooses one side (trolls or dwarves) then:

    #assign allies
    #create troll and dwarf leaders
    #create dwarf and troll defenders, make one side attackers
    #change ally and enemy gold/income
    #capture all villages, destroy ally's tunnel villages
    #seal off exits from allies caves

    [event]
        name=moveto

        [filter]
            x=24-74
            y=20-34
            side=1
        [/filter]

        #create 14 dwarves and 12 trolls

        #dwarves:
        # 2 dwarvish steelclads, 3 dwarvish fighters, 1 dwarvish thunderguard, 2 dwarvish thunderers, 2 dwarvish guardsmen, 1 dwarvish ulfserker, 1 dwarvish steelclad (captain), and 1 dwarvish pathfinder and 1 dwarvish scout

        #trolls:
        # 6 troll whelps, 2 trolls, 2 troll rocklobbers, 1 shaman, 1 shaman leader

        # on EASY and NORMAL, reduce number of trolls and dwarves
        # if EASY:
        # Dwarves:  remove 1 fighter (39,21), and 1 thunderer (35,20)
        # Trolls:   remove 2 troll whelps (37,32) (34,34)
        #       turn troll rocklobber into a whelp (33,32)

        # if NORMAL:
        # Dwarves:  remove 1 fighter (39,21)
        # Trolls:   remove 2 troll whelps (37,32) (34,34)

        #Western side
        #battling over the castle
        #1 dwarvish fighter, 1 dwarvish thunderer, 1 dwarvish guardsman

#define DEFENDER SIDE TYPE X Y ROLE_STRING NAME_STRING
    [unit]
        side={SIDE}
        type={TYPE}
        name={NAME_STRING}
        x={X}
        y={Y}
        random_traits=yes
        role={ROLE_STRING}
    [/unit]
#enddef

        {NAMED_UNIT 4 (Dwarvish Fighter) 30 27 first_dwarf ( _ "Dwarf Defender") (role=Dwarf Defender)}
        {NAMED_UNIT 4 (Dwarvish Thunderer) 30 25 second_dwarf ( _ "Dwarf Defender") (role=Dwarf Defender)}
        {DEFENDER 4 (Dwarvish Guardsman) 29 30 (Dwarf Defender) ( _ "Dwarf Defender")}

        #2 troll whelps
        {DEFENDER 3 (Troll Whelp) 31 30 (Troll Defender) ( _ "Troll Defender")}
        {DEFENDER 2 (Troll Whelp) 29 28 (Troll Defender) ( _ "Troll Defender")}

        #western reinforcements

        #1 dwarvish berserker, 1 dwarvish steelclad (captain), 1 dwarvish fighter, (added: 1 dwarvish scout)

        {DEFENDER 4 (Dwarvish Ulfserker) 30 21 (Dwarf Defender) ( _ "Dwarf Defender")}
        {NAMED_GENERIC_UNIT 4 (Dwarvish Steelclad) 32 20 (Dwarf Leader) ( _ "Dwarf Leader")}
        {DEFENDER 5 (Dwarvish Fighter) 34 22 (Dwarf Defender) ( _ "Dwarf Defender")}

        {DEFENDER 4 (Dwarvish Scout) 34 26 (Dwarf Defender) ( _ "Dwarf Defender")}

        #2 troll whelps, 1 troll rocklobber, 1 shaman (captain)

        {DEFENDER 2 (Troll Whelp) 29 31 (Troll Defender) ( _ "Troll Defender")}

        #unit was originally a troll
#ifdef HARD
        {DEFENDER 2 (Troll Whelp) 34 31 (Troll Defender) ( _ "Troll Defender")}
#endif

#ifdef EASY
        {DEFENDER 2 (Troll Whelp) 33 32 (Troll Defender) ( _ "Troll Defender")}
#else
        {DEFENDER 2 (Troll Rocklobber) 33 32 (Troll Defender) ( _ "Troll Defender")}
#endif

        {NAMED_GENERIC_UNIT 2 (Troll Shaman) 28 33 (Troll Leader) ( _ "Troll Leader")}

        #Center
        #1 dwarvish thunderer (N village)

#ifndef EASY
        {DEFENDER 5 (Dwarvish Thunderer) 35 20 (Dwarf Defender) ( _ "Dwarf Defender")}
#endif

        #Eastern side
        #battling over the center

        #1 dwarvish steelclad, 1 dwarvish guardsman, 1 dwarvish thunderer
        #1 dwarvish pathfinder
        {DEFENDER 5 (Dwarvish Steelclad) 40 26 (Dwarf Defender) ( _ "Dwarf Defender")}
        {DEFENDER 5 (Dwarvish Guardsman) 39 24 (Dwarf Defender) ( _ "Dwarf Defender")}
        {DEFENDER 5 (Dwarvish Thunderer) 42 23 (Dwarf Defender) ( _ "Dwarf Defender")}
        {DEFENDER 5 (Dwarvish Pathfinder) 44 26 (Dwarf Defender) ( _ "Dwarf Defender")}

        #1 troll whelp, 1 troll, 1 troll rocklobber
        {DEFENDER 2 (Troll Whelp) 41 27 (Troll Defender) ( _ "Troll Defender")}
        {DEFENDER 3 (Troll) 42 29 (Troll Defender) ( _ "Troll Defender")}
        {DEFENDER 3 (Troll Rocklobber) 39 29 (Troll Defender) ( _ "Troll Defender")}

        #eastern reinforcements

        #1 dwarvish steelclad, 1 dwarvish fighter

        {DEFENDER 5 (Dwarvish Steelclad) 42 21 (Dwarf Defender) ( _ "Dwarf Defender")}

#ifdef HARD
        {DEFENDER 5 (Dwarvish Fighter) 39 21 (Dwarf Defender) ( _ "Dwarf Defender")}
#endif

        #2 troll whelps, 1 shaman

#ifdef HARD
        {DEFENDER 3 (Troll Whelp) 37 32 (Troll Defender) ( _ "Troll Defender")}
#endif
        {DEFENDER 3 (Troll Whelp) 42 32 (Troll Defender) ( _ "Troll Defender")}
        {DEFENDER 3 (Troll Shaman) 44 31 (Troll Defender) ( _ "Troll Defender")}

#undef DEFENDER

        # wmllint: recognize Dwarf Defender
        # wmllint: recognize Troll Defender

        [redraw]
        [/redraw]

        #reveal large cave x: 25-47 y: 19-35
        [remove_shroud]
            side=1
            x=25-47
            y=19-35
        [/remove_shroud]

        #dwarf/troll/elf dialogue

        {CHECK_EXPLORER}
        [message]
            speaker=$explorer.id
            message= _ "Whoa."
        [/message]

        # Unhide the dwarves' and trolls' sides
        [modify_side]
            side=2,3,4,5
            hidden=no
        [/modify_side]

        [scroll_to_unit]
            x,y=34,22
        [/scroll_to_unit]

        [delay]
            time=400
        [/delay]

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

        [delay]
            time=400
        [/delay]

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

        [delay]
            time=400
        [/delay]

        [scroll_to_unit]
            x,y=33,32
        [/scroll_to_unit]

        [delay]
            time=400
        [/delay]

        [scroll_to_unit]
            x,y=29,27
        [/scroll_to_unit]

        [delay]
            time=400
        [/delay]

        [message]
            speaker=Nym
            message= _ "I think I preferred the spider and the ants..."
        [/message]

        [message]
            speaker=Dwarf Leader
            message= _ "Stand firm, boys, here they come!"
        [/message]

        [message]
            speaker=Troll Leader
            message= _ "You invade our tunnels, you slaughter our people, by Griknagh we will make you pay!"
        [/message]

        [message]
            speaker=Dwarf Leader
            message= _ "Tenacious savages, aren’t they? But these tunnels are rich in ore, and we won’t let a couple of trolls keep them from us."
        [/message]

        [message]
            speaker=Troll Leader
            message= _ "Wait... What... Who are you?"
        [/message]

        [message]
            speaker=Kaleh
            message= _ "Uh..."
        [/message]

        [message]
            speaker=Dwarf Leader
            message= _ "What by the names of my forefathers are they?"
        [/message]

        [message]
            speaker=first_dwarf
            message= _ "Wait a minute... Vacuous eyes, pointy ears — they must be elves."
        [/message]

        [message]
            speaker=second_dwarf
            message= _ "Elves?! What in the nine hells are elves doing down here?"
        [/message]

        [message]
            speaker=Dwarf Leader
            message= _ "Never mind that, who are you?"
        [/message]

        [message]
            speaker=Kaleh
            message= _ "I am Kaleh, and we are the Quenoth elves. What in Eloh’s name is going on here?"
        [/message]

        [message]
            speaker=Troll Leader
            message= _ "They invade our land and kill our young. Dwarves always want more, always greedy for glittery rocks."
        [/message]

        [message]
            speaker=Dwarf Leader
            message= _ "Those monsters killed me boys. Kaleh, if you be of stout heart, help us drive these lummoxes from our tunnels."
        [/message]

        [message]
            speaker=Troll Leader
            message= _ "No, this is our home. Help us, little ones, and we will help you."
        [/message]

        [message]
            speaker=Zhul
            message= _ "There’s too many of them for us to try to take them both on, and besides with all these branching tunnels we’ll have no idea which way to go. I think we should take them up on their offer; ally ourselves with one of the factions so we can get their help in finding a way back to the surface."
        [/message]

        [message]
            speaker=Nym
            message= _ "But even if we do, what about all of our people? How can we safely escort them through this war zone?"
            image=portraits/nym_moody.png
        [/message]

        [message]
            speaker=Kaleh
            message= _ "We won’t. If we keep the majority of our people hidden back up the passage we should be able to protect them, at least for a little while. In the meantime, we’ll go ahead and try to sort out this mess. I think you’re right Zhul, if we’re lucky we may just be able to negotiate a safe passage out of here."
        [/message]

        #choose side to ally with

        #set ally variable (1=dwarf 2=troll) and change elvish allegiance

        [message]
            speaker=$explorer.id
            message= _ "But they both look evenly matched. Who should we ally with?"

            [option]
                label= _ "Let’s aid the dwarves."

                [command]
                    [set_variable]
                        name=ally_race
                        value="dwarf"
                    [/set_variable]

                    [modify_side]
                        side=1
                        team_name=dwarves
                        user_team_name= _ "team_name^Dwarves"
                    [/modify_side]

                    [message]
                        speaker=Troll Leader
                        message= _ "Bah! Your kind all the same. Everyone turns on trolls. But you’ll see, Griknagh will smash you all."
                    [/message]

                    {DWARF_ALLY_APPROACH_MESSAGES}
                    {DWARF_ALLY_VICTORY}
                    [event]
                        name=troll_victory_test
                        [endlevel]
                            result=defeat
                        [/endlevel]
                    [/event]

                    #set new scenario objectives

                    [objectives]
                        summary= _ "New Objectives:"
                        silent=no
                        [objective]
                            description= _ "Defeat troll leaders"
                            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]
                        [objective]
                            description= _ "Death of Fundin"
                            condition=lose
                        [/objective]
                        [objective]
                            description= _ "Death of Nori"
                            condition=lose
                        [/objective]

                        {TURNS_RUN_OUT}

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

            [option]
                label= _ "Let’s aid the trolls."

                [command]
                    [set_variable]
                        name=ally_race
                        value="troll"
                    [/set_variable]

                    [modify_side]
                        side=1
                        team_name=trolls
                        user_team_name= _ "team_name^Trolls"
                    [/modify_side]

                    [message]
                        speaker=Dwarf Leader
                        message= _ "I knew elves couldn’t be trusted. Foolish boy, you will regret your betrayal. Taste dwarven steel!"
                    [/message]

                    {TROLL_ALLY_APPROACH_MESSAGES}
                    {TROLL_ALLY_VICTORY}
                    [event]
                        name=dwarf_victory_test
                        [endlevel]
                            result=defeat
                        [/endlevel]
                    [/event]

                    #set new scenario objectives
                    [objectives]
                        summary= _ "New Objectives:"
                        silent=no
                        [objective]
                            description= _ "Defeat dwarf leaders"
                            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]
                        [objective]
                            description= _ "Death of Thungar"
                            condition=lose
                        [/objective]
                        [objective]
                            description= _ "Death of Gnarl"
                            condition=lose
                        [/objective]

                        {TURNS_RUN_OUT}

                        [gold_carryover]
                            bonus=yes
                            carryover_percentage=40
                        [/gold_carryover]
                    [/objectives]
                [/command]
            [/option]
        [/message]

        # change music playing

        [music]
            name=battle.ogg
            immediate=yes
        [/music]

        [message]
            speaker=$explorer.id
            message= _ "There seems to be an abandoned dwarvish fortress right in front of us. If we can fight our way to the keep, we should be able to start rallying our warriors to help in the battle."
        [/message]
        {CLEAR_VARIABLE explorer}

        #add enemy units in tunnels who will arrive in main cavern at turn 2

        [if]
            [variable]
                name=ally_race
                equals="dwarf"
            [/variable]

            [then]
                # EASY: add 3 troll whelps, MED: add 3 trolls HARD: add 4 trolls
                # (on easier difficulties, remove trolls closest to elves)

#ifdef HARD
                {NAMED_GENERIC_UNIT 2 (Troll Whelp) 25 36 () ( _ "Troll Skirmisher")}
#endif
                {NAMED_GENERIC_UNIT 3 (Troll Whelp) 33 38 () ( _ "Troll Skirmisher")}
                {NAMED_GENERIC_UNIT 3 (Troll Whelp) 42 35 () ( _ "Troll Skirmisher")}

                {NAMED_GENERIC_UNIT 3 (Troll Whelp) 46 35 () ( _ "Troll Skirmisher")}
            [/then]

            [else]
                # add 3-4 dwarf enemies
                # if EASY difficulty, then remove Dwarvish Thunderer

                # (was a dwarvish steelclad)
                {NAMED_GENERIC_UNIT 4 (Dwarvish Fighter) 26 18 () ( _ "Dwarf Skirmisher")}

                {NAMED_GENERIC_UNIT 4 (Dwarvish Pathfinder) 28 18 () ( _ "Dwarf Skirmisher")}

                {NAMED_GENERIC_UNIT 5 (Dwarvish Berserker) 38 18 () ( _ "Dwarf Skirmisher")}

#ifndef EASY
                {NAMED_GENERIC_UNIT 5 (Dwarvish Thunderguard) 44 19 (Dwarf Thunderer) ( _ "Dwarf Thunderer")}
#endif

                {NAMED_GENERIC_UNIT 5 (Dwarvish Scout) 43 18 () ( _ "Dwarf Skirmisher")}
            [/else]
        [/if]

        #increase recruitment options for enemies
        #set gold/income for allies and enemies

        # I think the trolls are slightly more powerful than the dwarves
        # So I'm giving trolls 2 less income than dwarves

        [if]
            [variable]
                name=ally_race
                equals=dwarf
            [/variable]

            [then]
                #ally with dwarves

                [allow_recruit]
                    type=Troll Shaman
                    side=2,3
                [/allow_recruit]

                #allies
                [modify_side]
                    [filter_side]
                        side=4,5
                    [/filter_side]
                    {INCOME -2 -3 -4}
                    {GOLD 50 50 25}
                [/modify_side]

                #enemies
                [modify_side]
                    [filter_side]
                        side=2,3
                    [/filter_side]
                    {INCOME 3 5 7}
                    {GOLD 100 125 150}
                [/modify_side]

                [modify_unit]
                    [filter]
                        role=Troll Defender
                    [/filter]
                    name= _ "Troll Skirmisher"
                [/modify_unit]

                # destroy dwarvish villages in tunnels and add troll villages
                [terrain]
                    x=28,35
                    y=19,14
                    terrain=Uu
                [/terrain]

                [terrain]
                    x=36,43
                    y=17,18
                    terrain=Xu
                [/terrain]

                [terrain]
                    x=25,34,47,37
                    y=36,37,36,47
                    terrain=Uu^Vu
                [/terrain]
            [/then]

            [else]
                #ally with trolls

                #ulfserkers are too deadly, especially for elves with no defense

                [allow_recruit]
                    type=Dwarvish Pathfinder
                    side=4,5
                [/allow_recruit]

                #allies
                [modify_side]
                    [filter_side]
                        side=2,3
                    [/filter_side]
                    {INCOME -2 -3 -4}
                    {GOLD 50 50 25}
                [/modify_side]

                #enemies
                [modify_side]
                    [filter_side]
                        side=4,5
                    [/filter_side]
                    {INCOME 4 6 8}
                    {GOLD 125 150 150}
                [/modify_side]

                [modify_unit]
                    [filter]
                        role=Dwarf Defender
                    [/filter]
                    name= _ "Dwarf Skirmisher"
                [/modify_unit]
            [/else]
        [/if]

        #create 2 dwarf leaders (26,12) (45,13)
        [unit]
            type=Dwarvish Explorer
            id=Fundin
            name= _ "Fundin"
            canrecruit=yes
            x=26
            y=12
            side=4
            [modifications]
                {TRAIT_RESILIENT}
            [/modifications]
        [/unit]

        [unit]
            type=Dwarvish Explorer
            id=Nori
            name= _ "Nori"
            canrecruit=yes
            x=45
            y=13
            side=5
            [modifications]
                {TRAIT_STRONG}
                {TRAIT_QUICK}
            [/modifications]
        [/unit]

        #create 2 troll leaders (26,40) (46,42)
        [unit]
            type=Troll Warrior
            id=Thungar
            name= _ "Thungar"
            canrecruit=yes
            x=26
            y=40
            side=2
            [modifications]
                {TRAIT_RESILIENT}
            [/modifications]
        [/unit]

        [unit]
            type=Troll Warrior
            id=Gnarl
            name= _ "Gnarl"
            canrecruit=yes
            x=46
            y=42
            side=3
            [modifications]
                {TRAIT_STRONG}
                {TRAIT_QUICK}
            [/modifications]
        [/unit]

        # capture villages for each troll/dwarf leader

        # troll 1 (side2)

        # in troll cave and western tunnels
        [capture_village]
            x,y=22-35,36-43
            side=2
        [/capture_village]

        # in main cavern
        [capture_village]
            x,y=36,32
            side=2
        [/capture_village]

        # troll 2 (side3)

        # in troll cave and eastern tunnels
        [capture_village]
            x,y=37-50,36-47
            side=3
        [/capture_village]

        # in main cavern
        [capture_village]
            x,y=31,30
            side=3
        [/capture_village]

        [capture_village]
            x,y=26,28
            side=3
        [/capture_village]

        # dwarf 1 (side4)

        # in dwarf cave and western tunnels
        [capture_village]
            x,y=24-35,8-19
            side=4
        [/capture_village]

        # in main cavern
        [capture_village]
            x,y=28,24
            side=4
        [/capture_village]

        [capture_village]
            x,y=35,27
            side=4
        [/capture_village]

        # dwarf 2 (side5)

        # in dwarf cave and eastern tunnels
        [capture_village]
            x,y=36-51,10-18
            side=5
        [/capture_village]

        # in main cavern
        [capture_village]
            x,y=40,26
            side=5
        [/capture_village]

        [fire_event]
            name=queue_battle_events
        [/fire_event]
    [/event]

    # Event 3: Enemy counter-attack (Battle Turn 3)

    # if fighting dwarves: (3,4,5)
    # run 2 dwarvish thunderguards down to western threatre,
    # and 1 down to eastern bottleneck

    # if fighting trolls: (3,4,5)
    # run 2 troll shamans up to western threatre, and 1 up to eastern
    # bottleneck (reduced to 1 on each side)

    [event]
        name=enemy_attack

        [if]
            [variable]
                name=ally_race
                equals=dwarf
            [/variable]

            [then]
                #if allied with dwarves

                #west side

                [move_unit_fake]
                    type=Troll Shaman
                    side=2
                    x=27,27,28
                    y=39,33,32
                [/move_unit_fake]

                {NAMED_GENERIC_UNIT 2 (Troll Shaman) 28 32 (Troll Flamecaster) ( _ "Troll Flamecaster")}

#ifdef HARD

                [move_unit_fake]
                    type=Troll Shaman
                    side=2
                    x=33,33,32
                    y=39,33,32
                [/move_unit_fake]

                {NAMED_GENERIC_UNIT 2 (Troll Shaman) 32 32 () ( _ "Troll Flamecaster")}
#endif

                #east side

                [move_unit_fake]
                    type=Troll Shaman
                    side=3
                    x=38,42,42,41
                    y=37,35,29,29
                [/move_unit_fake]

                {NAMED_GENERIC_UNIT 3 (Troll Shaman) 41 29 () ( _ "Troll Flamecaster")}

#ifndef EASY

                [move_unit_fake]
                    type=Troll Shaman
                    side=3
                    x=45,45
                    y=40,30
                [/move_unit_fake]

                {NAMED_GENERIC_UNIT 3 (Troll Shaman) 45 30 () ( _ "Troll Flamecaster")}
#endif

                [message]
                    speaker=Troll Flamecaster
                    message= _ "Burn, burn and die!"
                [/message]

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

                [redraw]
                [/redraw]

                [sound]
                    name=fire.wav
                [/sound]

                [delay]
                    time=100
                [/delay]

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

                [redraw]
                [/redraw]

                [message]
                    speaker=Dwarf Leader
                    message= _ "Dive for cover!"
                [/message]

                [message]
                    speaker=Kaleh
                    message= _ "Those new troll shamans are decimating the dwarves with blasts of fire! This doesn’t look good."
                [/message]

                [store_unit]
                    [filter]
                        role="Dwarf Defender"
                        x=24-46
                        y=19-35
                    [/filter]
                    variable=victims
                    kill=no
                [/store_unit]

                #if easy kill 50%
                #if medium kill 60%
                #if hard kill 80%

                {VARIABLE deaths $victims.length}
                {VARIABLE_OP deaths multiply {ON_DIFFICULTY 0.6 0.7 0.8}}
                {VARIABLE_OP deaths round 0}

                #{DEBUG_MSG "Killing $deaths defenders out of $victims.length"}

                {VARIABLE i 1}
                {REPEAT $deaths (
                    {RANDOM "1..$victims.length"}
                    {VARIABLE_OP random sub 1}

                    {VARIABLE casualty $victims[$random].id}

                    #have some dwarves scream as they die
                    [switch]
                        variable=i

                        [case]
                            value=1

                            [message]
                                id=$casualty
                                message= _ "Aauughh!"
                            [/message]
                        [/case]

                        [case]
                            value=2

                            [message]
                                id=$casualty
                                message= _ "No...!"
                            [/message]
                        [/case]

                        [case]
                            value=3

                            [message]
                                id=$casualty
                                message= _ "Help me!!"
                            [/message]
                        [/case]
                    [/switch]

                    [kill]
                        id=$casualty
                        animate=yes
                        fire_event=no
                    [/kill]

                    {VARIABLE_OP i add 1}

                    # Update the victims array
                    [store_unit]
                        [filter]
                            find_in=victims
                            [not]
                                id=$casualty
                            [/not]
                        [/filter]

                        variable=victims
                        kill=no
                    [/store_unit]
                )}

                [message]
                    speaker=Dwarf Leader
                    message= _ "More accursed troll magic. Fall back!"
                [/message]

                [message]
                    speaker=Dwarf Leader
                    # wmllint: local spelling hurtin'
                    message= _ "I need to go back and rally more reinforcements. We’re hurtin’, Kaleh, I’ll need your men to cover for us. Do your best, boy, and may your ancestors watch over you."
                [/message]

                [kill]
                    id=Dwarf Leader
                    animate=no
                [/kill]
            [/then]

            [else]
                #if allied with trolls

                #west side
                [move_unit_fake]
                    type=Dwarvish Thunderguard
                    side=4
                    x=26,27,28,29,30,30,30,30
                    y=18,19,19,19,19,20,21,22
                [/move_unit_fake]

                {NAMED_GENERIC_UNIT 4 (Dwarvish Thunderguard) 30 22 (Dwarf Grenadier) ( _ "Dwarf Grenadier")}

                [move_unit_fake]
                    type=Dwarvish Thunderguard
                    side=4
                    x=26,27,28,29,30,30,31,32,32
                    y=18,19,19,19,19,20,21,21,22
                [/move_unit_fake]

                {NAMED_GENERIC_UNIT 4 (Dwarvish Thunderguard) 32 22 () ( _ "Dwarf Grenadier")}

#ifdef HARD

                [move_unit_fake]
                    type=Dwarvish Thunderguard
                    side=4
                    x=26,27,28,29,30,30,31,32,33,34
                    y=18,19,19,19,19,20,21,21,22,22
                [/move_unit_fake]

                {NAMED_GENERIC_UNIT 4 (Dwarvish Thunderguard) 34 22 () ( _ "Dwarf Grenadier")}
#endif

                #east side

                [move_unit_fake]
                    type=Dwarvish Thunderguard
                    side=5
                    x=45,44,44,43,42,41,41,40
                    y=18,18,19,20,20,21,22,22
                [/move_unit_fake]

                {NAMED_GENERIC_UNIT 5 (Dwarvish Thunderguard) 40 22 () ( _ "Dwarf Grenadier")}

#ifndef EASY

                [move_unit_fake]
                    type=Dwarvish Thunderguard
                    side=5
                    x=45,44,44,43,43,42,42
                    y=18,18,19,20,21,21,22
                [/move_unit_fake]

                {NAMED_GENERIC_UNIT 5 (Dwarvish Thunderguard) 42 22 () ( _ "Dwarf Grenadier")}
#endif

                [message]
                    speaker=Dwarf Grenadier
                    message= _ "Let’s blast those monsters back to the pits they spawned from! Fire in the hole!"
                [/message]

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

                [redraw]
                [/redraw]

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

                [delay]
                    time=100
                [/delay]

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

                [redraw]
                [/redraw]

                [message]
                    speaker=Troll Leader
                    message= _ "More dwarven trickery! Fall back!"
                [/message]

                [message]
                    speaker=Kaleh
                    message= _ "Those new dwarves are lobbing explosives at the trolls with devastating effect! I don’t think the trolls can take this much longer."
                [/message]

                [store_unit]
                    [filter]
                        role="Troll Defender"
                        x=24-46
                        y=19-35
                    [/filter]
                    variable=victims
                    kill=no
                [/store_unit]

                #if easy kill 50%
                #if medium kill 60%
                #if hard kill 75%

                {VARIABLE deaths $victims.length}
                {VARIABLE_OP deaths multiply {ON_DIFFICULTY 0.5 0.6 0.75}}
                {VARIABLE_OP deaths round 0}

                #{DEBUG_MSG "Killing $deaths defenders out of $victims.length"}

                {VARIABLE i 1}
                {REPEAT $deaths (
                    {RANDOM "1..$victims.length"}
                    {VARIABLE_OP random sub 1}

                    {VARIABLE casualty $victims[$random].id}

                    #have some trolls scream as they die
                    [switch]
                        variable=i

                        [case]
                            value=1

                            [message]
                                id=$casualty
                                message= _ "Aaughh!"
                            [/message]
                        [/case]

                        [case]
                            value=2

                            [message]
                                id=$casualty
                                message= _ "No...!"
                            [/message]
                        [/case]

                        [case]
                            value=3

                            [message]
                                id=$casualty
                                message= _ "Gaaghh!"  # wmllint: no spellcheck
                            [/message]
                        [/case]
                    [/switch]

                    [kill]
                        id=$casualty
                        animate=yes
                        fire_event=no
                    [/kill]

                    {VARIABLE_OP i add 1}

                    # Update the victims array
                    [store_unit]
                        [filter]
                            find_in=victims
                            [not]
                                id=$casualty
                            [/not]
                        [/filter]

                        variable=victims
                        kill=no
                    [/store_unit]
                )}

                [message]
                    speaker=Troll Leader
                    message= _ "I must go back and find more trolls to fight. You must hold them back, Kaleh. Be strong like rock. Griknagh will be with you."
                [/message]

                [kill]
                    id=Troll Leader
                    animate=no
                [/kill]
            [/else]
        [/if]

        {CLEAR_VARIABLE casualty,deaths,victims,i}
    [/event]

    # Event 4: Ally reinforcements
    #   message: ally leader sent us to help you

#define ALLY_REINFORCEMENTS

    [if]
        [variable]
            name=ally_race
            equals="troll"
        [/variable]

        [then]
            # Troll
            # Easy: 3 whelps 1 troll shaman, 1 rock lobber
            # Medium: 3 whelps 1 troll shaman, 1 rock lobber
            # Hard: 2 whelps 1 troll shaman, 1 rock lobber

            [unit]
                type=Troll Shaman
                id="Thu'lok"
                name= _ "Thu’lok"
                x=34
                y=32
                side=1
                unrenamable=yes
                [modifications]
                    {TRAIT_LOYAL}
                    {TRAIT_RESILIENT}
                [/modifications]
                {IS_LOYAL}
                role=ally
            [/unit]

            [unit]
                type=Troll Whelp
                id=Harpo
                name= _ "Harpo"
                x=33
                y=33
                side=1
                unrenamable=yes
                [modifications]
                    {TRAIT_LOYAL}
                    {TRAIT_STRONG}
                [/modifications]
                {IS_LOYAL}
                role=ally
            [/unit]

            [unit]
                type=Troll Whelp
                id=Groucho
                name= _ "Groucho"
                x=34
                y=33
                side=1
                unrenamable=yes
                [modifications]
                    {TRAIT_LOYAL}
                    {TRAIT_INTELLIGENT}
                [/modifications]
                {IS_LOYAL}
                role=ally
            [/unit]

#ifndef HARD
            [unit]
                type=Troll Whelp
                name= _ "Chico"
                id=Chico
                x=35
                y=33
                side=1
                unrenamable=yes
                [modifications]
                    {TRAIT_LOYAL}
                    {TRAIT_RESILIENT}
                [/modifications]
                {IS_LOYAL}
                role=ally
            [/unit]
#endif

            [unit]
                type=Troll Rocklobber
                name= _ "Groo"
                id=Groo
                x=33
                y=34
                side=1
                unrenamable=yes
                [modifications]
                    {TRAIT_LOYAL}
                    {TRAIT_QUICK}
                [/modifications]
                {IS_LOYAL}
                role=ally
            [/unit]

            [message]
                speaker="Thu'lok"
                message= _ "Our leader sent us to help you. We fight for you until all the dwarves are dead. We will avenge the deaths of our people!"
            [/message]

            #dwarf cairns
            [event]
                name=moveto
                [filter]
                    x=30,38,42
                    y=20,20,21
                    side=1
                [/filter]

                [message]
                    role=ally
                    message= _ "The dwarves use stone cairns to mark their territory. What a waste of good throwing stones."
                [/message]

                [allow_undo]
                [/allow_undo]
            [/event]
        [/then]

        [else]
            # Dwarves
            # Easy: 2 dwarvish fighters, 1 thunderer, 1 berserker,
            # 1 dwarvish scout
            # Medium: 2 dwarvish fighters, 1 thunderer, 1 berserker,
            # 1 dwarvish scout
            # Hard: 1 dwarvish fighter, 1 thunderer, 1 berserker,
            # 1 dwarvish scout

            [unit]
                type=Dwarvish Fighter
                id=Dwalim
                name= _ "Dwalim"
                x=36
                y=21
                side=1
                unrenamable=yes
                [modifications]
                    {TRAIT_LOYAL}
                    {TRAIT_STRONG}
                [/modifications]
                {IS_LOYAL}
                role=ally
                placement=map
                passable=yes
            [/unit]

            [unit]
                type=Dwarvish Pathfinder
                id=Moin
                name= _ "Moin"
                x=35
                y=21
                side=1
                unrenamable=yes
                [modifications]
                    {TRAIT_LOYAL}
                    {TRAIT_QUICK}
                [/modifications]
                {IS_LOYAL}
                role=ally
                placement=map
                passable=yes
            [/unit]

            [unit]
                type=Dwarvish Thunderer
                id=Nordi
                name= _ "Nordi"
                x=37
                y=21
                side=1
                unrenamable=yes
                [modifications]
                    {TRAIT_LOYAL}
                    {TRAIT_INTELLIGENT}
                [/modifications]
                {IS_LOYAL}
                role=ally
                placement=map
                passable=yes
            [/unit]

            [unit]
                type=Dwarvish Berserker
                id=Byorn
                name= _ "Byorn"
                x=38
                y=20
                side=1
                unrenamable=yes
                [modifications]
                    {TRAIT_LOYAL}
                    {TRAIT_RESILIENT}
                [/modifications]
                {IS_LOYAL}
                role=ally
                placement=map
                passable=yes
            [/unit]

#ifndef HARD

            [unit]
                type=Dwarvish Fighter
                id=Runin
                name= _ "Runin"
                x=35
                y=20
                side=1
                unrenamable=yes
                [modifications]
                    {TRAIT_LOYAL}
                    {TRAIT_QUICK}
                [/modifications]
                {IS_LOYAL}
                role=ally
                placement=map
                passable=yes
            [/unit]
#endif

            [message]
                speaker=Dwalim
                message= _ "Looks like we came just in time. Our chief told us we’re to fight with you until all the trolls are dead. Tell us where to go — I want to kill me some troll!"
            [/message]

            #troll totems
            [event]
                name=moveto

                [filter]
                    x=27,35,43
                    y=33,34,33
                    side=1
                [/filter]

                [message]
                    role=ally
                    message= _ "The trolls display the skulls of their enemies as a way of marking their territory. How barbaric."
                [/message]

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

#enddef

    # Event 29: Return of the Assassin/Cloaked Figure (same guy, two names)

    # Find the location of Kaleh and then find an adjacent
    # hex the cloaked figure can pop up in. (any hex that is not impassable)

    [event]
        name=call_assassin

        [store_locations]
            [filter]
                id=Kaleh
            [/filter]

            variable=dark_assassin_location
        [/store_locations]

        [unit]
            type=Dark Assassin2
            id=Cloaked Figure
            name= _ "Cloaked Figure"
            side=7
            x,y=$dark_assassin_location.x,$dark_assassin_location.y
            placement=map
            passable=yes
            [modifications]
                {TRAIT_INTELLIGENT}
                {TRAIT_RESILIENT}
            [/modifications]
        [/unit]

        [micro_ai]
            side=7
            ai_type=assassin
            action=add

            [filter]
                id=Cloaked Figure
            [/filter]
            [filter_second]
                id=Kaleh
            [/filter_second]
        [/micro_ai]

        [message]
            speaker=Cloaked Figure
            image=portraits/cloaked.png
            message= _ "Did you think you had escaped me, Kaleh? I am your shadow, I will always be there until you pay for what you have done."
        [/message]

        [message]
            speaker=Cloaked Figure
            image=portraits/cloaked.png
            message= _ "You want to flee, don’t you? But you cannot. They couldn’t escape her either. Even death could not save them. She will devour us all. But first I shall have my revenge. Do the dance of death for me, Kaleh! Dance! Dance!"
        [/message]
        {CLEAR_VARIABLE dark_assassin_location}
    [/event]

    [event]
        name=queue_battle_events

        [event]
            name="turn $($turn_number+3)"
            delayed_variable_substitution=no

            [fire_event]
                name=enemy_attack
            [/fire_event]
        [/event]

        [event]
            name="turn $($turn_number+6)"
            delayed_variable_substitution=no
            {ALLY_REINFORCEMENTS}
        [/event]

        [set_variable]
            name=assassin_turn
            rand=13..18
        [/set_variable]
        [event]
            name="turn $($turn_number|+$assassin_turn|)"
            delayed_variable_substitution=no
            [fire_event]
                name=call_assassin
            [/fire_event]
        [/event]
        {CLEAR_VARIABLE assassin_turn}
    [/event]

    # Event 6: death events for each leader

    #for each death check to see if other leader is dead, if so
    #also send in dwarf/troll messenger with victory congratulations

    [event]
        name=last breath

        [filter]
            id=Fundin
        [/filter]

        [message]
            speaker=Fundin
            message= _ "The rest is silence..."
        [/message]

        [kill]
            id=Fundin
            animate=yes
        [/kill]

        [fire_event]
            name=troll_victory_test
        [/fire_event]
    [/event]

    [event]
        name=last breath

        [filter]
            id=Nori
        [/filter]

        [message]
            speaker=Nori
            message= _ "I go to my ancestors..."
        [/message]

        [kill]
            id=Nori
            animate=yes
        [/kill]

        [fire_event]
            name=troll_victory_test
        [/fire_event]
    [/event]

    [event]
        name=last breath

        [filter]
            id=Thungar
        [/filter]

        [message]
            speaker=Thungar
            message= _ "Arrghh!!"
        [/message]

        [fire_event]
            name=dwarf_victory_test
        [/fire_event]
    [/event]

    [event]
        name=last breath

        [filter]
            id=Gnarl
        [/filter]

        [message]
            speaker=Gnarl
            message= _ "I will be avenged..."
        [/message]

        [kill]
            id=Gnarl
            animate=yes
        [/kill]

        [fire_event]
            name=dwarf_victory_test
        [/fire_event]
    [/event]

    # When cloaked figure dies and if player has already killed both enemy
    # leaders, then go to victory

    [event]
        name=die

        [filter]
            id=Cloaked Figure
        [/filter]

        [kill]
            id=Cloaked Figure
            animate=no
            fire_event=no
        [/kill]

        [micro_ai]
            side=7
            ai_type=assassin
            action=delete
        [/micro_ai]

        [message]
            speaker=Kaleh
            message= _ "Where did he go? How does he disappear like that? And what in Uria’s name was he ranting about? Whoever that is is starting to make me get edgy."
        [/message]

        # if cloaked figure was delaying victory event from firing

        [if]
            [have_unit]
                race=dwarf
                canrecruit=yes
                count=0
            [/have_unit]

            [then]
                [message]
                    speaker=Kaleh
                    message= _ "The cloaked figure is gone. Still, our work is not yet done. Gather yourselves together; we must follow Zurg."
                [/message]

                [endlevel]
                    result=victory
                    next_scenario=06b_In_the_Domain_of_the_Dwarves
                    bonus=yes
                    {NEW_GOLD_CARRYOVER 40}
                [/endlevel]
            [/then]
        [/if]

        [if]
            [have_unit]
                race=troll
                canrecruit=yes
                count=0
            [/have_unit]

            [then]
                [message]
                    speaker=Kaleh
                    message= _ "The cloaked figure is gone. Still, our work is not yet done. Gather yourselves together; we must follow Grimnir."
                [/message]

                [endlevel]
                    result=victory
                    next_scenario=06a_In_the_Tunnels_of_the_Trolls
                    bonus=yes
                    {NEW_GOLD_CARRYOVER 40}
                [/endlevel]
            [/then]
        [/if]
    [/event]

    #at victory, clear variables:
    [event]
        name=victory
        {CLEAR_VARIABLE Fundin_approach_message}
        {CLEAR_VARIABLE Nori_approach_message}
        {CLEAR_VARIABLE Thungar_approach_message}
        {CLEAR_VARIABLE Gnarl_approach_message}
    [/event]

    # if player runs out of time, display time over message
    [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]
