#textdomain wesnoth-utbs
# Level 5: A Subterranean Struggle

# 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

    #don't display snapshot of map in saved games
    #TODO why?
    snapshot="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=Desert Fighter
        canrecruit=yes
        gold=200
        {INCOME 7 5 3}
        controller=human
        shroud=yes
        fog=no
        {FLAG_VARIANT long}
    [/side]

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

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

            # AI will attack a weak unit with a max of 3,4,5 units
            # depending on the difficulty (default=5)
            {ATTACK_DEPTH 3 4 5}

            [target]
                side=1
                value=10
            [/target]

            [target]
                side=4
                value=9
            [/target]

            [target]
                side=5
                value=9
            [/target]

            #[protect_location]
            #x,y=35,37
            #radius=7
            #target=20
            #[/protect_location]
        [/ai]
    [/side]

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

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

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

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

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

#ifdef NORMAL
            recruitment_pattern=fighter,fighter,mixed fighter
#endif

#ifdef HARD
            recruitment_pattern=fighter,fighter,mixed fighter
#endif

            # AI will attack a weak unit with a max of 3,4,5 units
            # depending on the difficulty (default=5)
            {ATTACK_DEPTH 3 4 5}

            aggression=0.8
            caution=0.05
            grouping=offensive

            [target]
                side=1
                value=10
            [/target]

            [target]
                side=4
                value=9
            [/target]

            [target]
                side=5
                value=9
            [/target]

            #[protect_location]
            #x,y=35,37
            #radius=7
            #target=20
            #[/protect_location]
        [/ai]
    [/side]

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

#ifdef EASY
        recruit=Dwarvish Fighter, Dwarvish Steelclad, Dwarvish Thunderer, Dwarvish Guardsman
#endif

#ifdef NORMAL
        recruit=Dwarvish Fighter, Dwarvish Steelclad, Dwarvish Thunderer, Dwarvish Guardsman
#endif

#ifdef HARD
        recruit=Dwarvish Fighter, Dwarvish Steelclad, Dwarvish Thunderer, Dwarvish Guardsman
#endif

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

#ifdef NORMAL
            #recruitment_pattern=scout,fighter,archer,fighter
#endif

#ifdef HARD
            #recruitment_pattern=scout,fighter,archer,fighter
#endif

            # AI will attack a weak unit with a max of 3,4,5 units
            # depending on the difficulty (default=5)
            {ATTACK_DEPTH 3 4 5}

            aggression=0.8
            caution=0.05
            grouping=offensive

            [target]
                side=1
                value=10
            [/target]

            [target]
                side=2
                value=9
            [/target]

            [target]
                side=3
                value=9
            [/target]

            #[protect_location]
            #x,y=35,37
            #radius=7
            #target=20
            #[/protect_location]
        [/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
        team_name=dwarf
        user_team_name=_"Dwarves"

#ifdef EASY
        recruit=Dwarvish Fighter, Dwarvish Steelclad, Dwarvish Thunderer, Dwarvish Guardsman
#endif

#ifdef NORMAL
        recruit=Dwarvish Fighter, Dwarvish Steelclad, Dwarvish Thunderer, Dwarvish Guardsman
#endif

#ifdef HARD
        recruit=Dwarvish Fighter, Dwarvish Steelclad, Dwarvish Thunderer, Dwarvish Guardsman
#endif

        [ai]
            #TODO clean up this mess!
#ifdef EASY
            #recruitment_pattern=scout,fighter,archer,fighter
#endif

#ifdef NORMAL
            #recruitment_pattern=scout,fighter,archer,fighter
#endif

#ifdef HARD
            #recruitment_pattern=scout,fighter,archer,fighter
#endif

            # AI will attack a weak unit with a max of 3,4,5 units
            # depending on the difficulty (default=5)
            {ATTACK_DEPTH 3 4 5}

            aggression=0.8
            caution=0.05
            grouping=offensive

            [target]
                side=1
                value=10
            [/target]

            [target]
                side=2
                value=9
            [/target]

            [target]
                side=3
                value=9
            [/target]

            #TODO clean up.
            #[protect_location]
            #x,y=35,37
            #radius=7
            #target=20
            #[/protect_location]
        [/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

            # AI will attack a weak unit with a max of 3,4,5 units
            # depending on the difficulty (default=5)
            {ATTACK_DEPTH 3 4 5}

            recruitment_pattern=fighter,fighter,fighter,fighter
            recruitment_ignore_bad_combat=yes

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

            [target]
                side=1
                value=100
            [/target]

            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]
            # AI will attack a weak unit with a max of 4,5,6 units
            # depending on the difficulty (default=5)
            {ATTACK_DEPTH 3 4 5}

            aggression=0.90
            caution=0.10
        [/ai]

        #causes assassin to attack other units more than Kaleh
        #TODO but isn't Kaleh the target of the Assassin?
        [target]
            id=Kaleh
            value=20
        [/target]
    [/side]

    {UTBS_INCLUDE utils/dialog-macros.cfg}

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

    [event]
        name=prestart

        #set starting scenario objectives

        [objectives]
            summary= _ "Starting Objectives:"
            [objective]
                description= _ "Explore Underground"
                condition=win
            [/objective]
            [objective]
                description= _ "Defeat all Enemies"
                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]

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

        #increase the cost of all units by 1
        {INCREASE_RECRUIT_COSTS_BY_ONE}

        #fires lighting central cavern
        {PLACE_IMAGE scenery/fire1.png 31 32}
        {PLACE_IMAGE scenery/fire2.png 33 21}

        #central cavern furnishings

        {PLACE_IMAGE scenery/rock-cairn.png 42 21}
        {PLACE_IMAGE scenery/rock-cairn.png 38 20}
        {PLACE_IMAGE scenery/rock-cairn.png 30 20}

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

        #recall heroes
        [unit]
            {NYM}
            placement=leader
        [/unit]

        [unit]
            {ZHUL}
            placement=leader
        [/unit]

        [recall]
            id=Elyssa
        [/recall]

        #initialize starting variables

        #TODO get rid of this variable
        [set_variable]
            name=battle_turn_counter
            value=0
        [/set_variable]

        #TODO get rid of this variable
        [set_variable]
            name=elvish_ally
            value=0
        [/set_variable]

        #TODO get rid of this variable
        [set_variable]
            name=saw_cairn_or_totem
            value=0
        [/set_variable]

        #TODO get rid of this variable
        [set_variable]
            name=dead_dwarf_leaders
            value=0
        [/set_variable]

        #TODO get rid of this variable
        [set_variable]
            name=dead_troll_leaders
            value=0
        [/set_variable]

        #assassin_turn must be 2 or greater to fire
        #TODO 23 turns after the player entered the big cave?
        #isn't that a little too long?
        {RANDOM 13..23}

        #TODO get rid of this variable
        [set_variable]
            name=assassin_turn
            value=$random
        [/set_variable]

        #TODO get rid of this variable
        [set_variable]
            name=call_assassin
            value=0
        [/set_variable]

        #TODO get rid of this variable
        [set_variable]
            name=unslow_kaleh
            value=0
        [/set_variable]

        #if play kills both enemy leaders while assassin is alive, then
        #delay victory

        #TODO get rid of this variable
        [set_variable]
            name=delay_victory
            value=0
        [/set_variable]

        #create starting elves

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

        [unit]
            type=Desert Archer
            id=Sylestria
            name= _ "Sylestria"
            upkeep=full
            x=5
            y=25
            side=1
            [modifications]
                {TRAIT_INTELLIGENT}
                {TRAIT_DEXTROUS}
            [/modifications]
        [/unit]

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

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

            [then]
                # goody for you! ;-)
            [/then]
            [else]
                [unit]
                    type=Desert Ranger
                    id=Rygar
                    name= _ "Rygar"
                    x=6
                    y=24
                    side=1
                    [modifications]
                        {TRAIT_LOYAL}
                        {TRAIT_DEXTROUS}
                    [/modifications]
                    {IS_LOYAL}
                [/unit]
            [/else]
        [/if]
    [/event]

    # starting events

    # 1. create some villages manually, so that early finish bonus counts
    # only the number of villages that a player can control

    # 2. starting dialogue

    [event]
        name=start

        # there are two sets of villages in the tunnels around the great
        # central cave. One set will be erased when the player chooses
        # a side. To avoid duplication, I should make one set manually, so
        # only one set is added to the early finish bonus

        # tunnel villages in southern tunnels
        # village locations: (25,36) (34,37) (47,36) (37,47)

        #TODO Why are those villages not on the map from beginning?
        [terrain]
            x,y=25,36
            terrain=Uu^Vu
        [/terrain]

        [terrain]
            x,y=34,37
            terrain=Uu^Vu
        [/terrain]

        [terrain]
            x,y=47,36
            terrain=Uu^Vu
        [/terrain]

        [terrain]
            x,y=37,47
            terrain=Uu^Vu
        [/terrain]

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

        [move_unit_fake]
            type=Dwarvish Fighter
            side=4
            x=25,24,23,22,21
            y=28,28,29,28,29
        [/move_unit_fake]

        #TODO maybe the wounded dwarf should really be wounded?
        {NAMED_NOTRAIT_UNIT 4 (Dwarvish Fighter) 21 29 (Wounded Dwarf) ( _ "Wounded Dwarf")}
        # wmllint: recognize Wounded Dwarf

        [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

    # NW dwarf
    [event]
        name=moveto

        # wmllint: recognize Fundin
        [filter]
            x=23-31
            y=8-17
            side=1
        [/filter]

        [remove_shroud]
            x=22-31
            y=7-17
            side=1
        [/remove_shroud]

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

            [then]
                [message]
                    speaker=Fundin
                    message= _ "What are you doing back here? The trolls hide in the southern tunnels, not this way."
                [/message]
            [/then]

            [else]
                # open other exit from cave, closed off to try to keep AI
                # from guarding it

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

                [message]
                    speaker=Fundin
                    message= _ "Treacherous elves, how can you fight with such horrid creatures as trolls? I will cleave all in two with my axe!"
                [/message]
            [/else]
        [/if]
    [/event]

    # NE dwarf
    [event]
        name=moveto

        # wmllint: recognize Nori
        [filter]
            x=41-51
            y=10-16
            side=1
        [/filter]

        [remove_shroud]
            x=40-52
            y=9-17
            side=1
        [/remove_shroud]

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

            [then]
                [message]
                    speaker=Nori
                    message= _ "What are you doing back here? The trolls hide in the southern tunnels, not this way."
                [/message]
            [/then]

            [else]
                # open other exit from cave, closed off to try to keep AI
                # from guarding it

                [terrain]
                    x,y=44,6
                    terrain=Uu
                [/terrain]

                [message]
                    speaker=Nori
                    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!"
                [/message]
            [/else]
        [/if]
    [/event]

    # SW troll
    [event]
        name=moveto

        # wmllint: recognize Thungar
        [filter]
            x=23-31
            y=37-44
            side=1
        [/filter]

        [remove_shroud]
            x=21-32
            y=37-44
            side=1
        [/remove_shroud]

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

            [then]
                [message]
                    speaker=Thungar
                    # wmllint: local spelling stinkin'
                    message= _ "Nasty dwarves and stinkin’ elves, we will smash you all!"
                [/message]
            [/then]

            [else]
                [message]
                    speaker=Thungar
                    message= _ "What you doing back here? Nasty dwarves are to the north, no dwarves this way. Go back and fight bravely."
                [/message]
            [/else]
        [/if]
    [/event]

    # SE troll
    [event]
        name=moveto

        # wmllint: recognize Gnarl
        [filter]
            x=42-51
            y=37-45
            side=1
        [/filter]

        [remove_shroud]
            x=42-51
            y=37-46
            side=1
        [/remove_shroud]

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

            [then]
                [message]
                    speaker=Gnarl
                    message= _ "Kill the elves! We must stop them here. This is our land, crush the intruders!"
                [/message]
            [/then]

            [else]
                [message]
                    speaker=Gnarl
                    message= _ "What you doing back here? Nasty dwarves are to the north, no dwarves this way. Go back and fight bravely."
                [/message]
            [/else]
        [/if]
    [/event]

    # victory events for trolls and dwarves here to make the ally event easier to follow
#define TROLL_ALLY_VICTORY
    [event]
        name=troll_victory_test
        first_time_only=no

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

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

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

                        [objectives]
                            summary= _ "New Objectives:"
                            show=yes
                            [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]

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

                        [set_variable]
                            name=delay_victory
                            value=1
                        [/set_variable]
                    [/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]
            [/then]
        [/if]
    [/event]
#enddef

#define DWARF_ALLY_VICTORY
    [event]
        name=dwarf_victory_test
        first_time_only=no

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

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

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

                # wmllint: recognize Grimnir
                [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]

                        [objectives]
                            summary= _ "New Objectives:"
                            show=yes
                            [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]

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

                        [set_variable]
                            name=delay_victory
                            value=2
                        [/set_variable]
                    [/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
#ifdef UTBSE_NEW
                            next_scenario=06a_In_the_Tunnels_of_the_Trolls_new
#else
                            next_scenario=06a_In_the_Tunnels_of_the_Trolls
#endif
                            bonus=yes
                            {NEW_GOLD_CARRYOVER 40}
                        [/endlevel]
                    [/else]
                [/if]
            [/then]
        [/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
    #set battle turn counter

    [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
        upkeep=full
        role={ROLE_STRING}
    [/unit]
#enddef

        {DEFENDER 4 (Dwarvish Fighter) 30 27 (Dwarf Defender) ( _ "Dwarf Defender")}
        {DEFENDER 4 (Dwarvish Thunderer) 30 25 (Dwarf Defender) ( _ "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)

#ifdef EASY
#else
        {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 Leader
        # wmllint: recognize Troll Leader
        # 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]
            #TODO get rid of $explorer
            speaker=$explorer.id
            message= _ "Whoa."
        [/message]

        [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 women and children, 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 in Moradin’s name are they?"
        [/message]

        [message]
            #TODO coordinates are speaking? Hell!
            x,y=30,27
            message= _ "Wait a minute... Blond hair, pointy ears — they must be elves."
        [/message]

        [message]
            #TODO coordinates are speaking? Hell!
            x,y=30,25
            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?"
        [/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]
                message= _ "Let’s aid the dwarves."

                [command]
                    [set_variable]
                        name=elvish_ally
                        value=1
                    [/set_variable]

                    [modify_side]
                        side=1
                        team_name=dwarf
                        user_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_VICTORY}
                    [event]
                        name=troll_victory_test
                        [endlevel]
                            result=defeat
                        [/endlevel]
                    [/event]

                    #set new scenario objectives

                    [objectives]
                        summary= _ "New Objectives:"
                        show=yes
                        [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]

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

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

                [command]
                    [set_variable]
                        name=elvish_ally
                        value=2
                    [/set_variable]

                    [modify_side]
                        side=1
                        team_name=troll
                        user_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_VICTORY}
                    [event]
                        name=dwarf_victory_test
                        [endlevel]
                            result=defeat
                        [/endlevel]
                    [/event]

                    #set new scenario objectives
                    [objectives]
                        summary= _ "New Objectives:"
                        show=yes
                        [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]

                        [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=elvish_ally
                numerical_equals=1
            [/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 37 () ( _ "Troll Skirmisher")}
#endif
                {NAMED_GENERIC_UNIT 3 (Troll Whelp) 33 38 () ( _ "Troll Skirmisher")}
                {NAMED_GENERIC_UNIT 3 (Troll Whelp) 42 36 () ( _ "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")}

#ifdef EASY
#else
                {NAMED_GENERIC_UNIT 5 (Dwarvish Thunderguard) 43 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=elvish_ally
                numerical_equals=1
            [/variable]

            [then]
                #ally with dwarves

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

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

                #allies
                [modify_side]
                    side=4

#ifdef EASY
                    income=-2
                    gold=50
#endif
#ifdef NORMAL
                    income=-3
                    gold=50
#endif
#ifdef HARD
                    income=-4
                    gold=25
#endif
                [/modify_side]

                [modify_side]
                    side=5

#ifdef EASY
                    income=-2
                    gold=50
#endif
#ifdef NORMAL
                    income=-3
                    gold=50
#endif
#ifdef HARD
                    income=-4
                    gold=25
#endif
                [/modify_side]

                #enemies
                [modify_side]
                    side=2

#ifdef EASY
                    income=3
                    gold=100
#endif
#ifdef NORMAL
                    income=5
                    gold=125
#endif
#ifdef HARD
                    income=7
                    gold=150
#endif
                [/modify_side]

                [modify_side]
                    side=3

#ifdef EASY
                    income=3
                    gold=100
#endif
#ifdef NORMAL
                    income=5
                    gold=125
#endif
#ifdef HARD
                    income=7
                    gold=150
#endif
                [/modify_side]
            [/then]

            [else]
                #ally with trolls

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

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

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

                #allies
                [modify_side]
                    side=2

#ifdef EASY
                    income=-2
                    gold=50
#endif
#ifdef NORMAL
                    income=-3
                    gold=50
#endif
#ifdef HARD
                    income=-4
                    gold=25
#endif
                [/modify_side]

                [modify_side]
                    side=3

#ifdef EASY
                    income=-2
                    gold=50
#endif
#ifdef NORMAL
                    income=-3
                    gold=50
#endif
#ifdef HARD
                    income=-4
                    gold=25
#endif
                [/modify_side]

                #enemies
                [modify_side]
                    side=4

#ifdef EASY
                    income=4
                    gold=125
#endif
#ifdef NORMAL
                    income=6
                    gold=150
#endif
#ifdef HARD
                    income=8
                    gold=150
#endif
                [/modify_side]

                [modify_side]
                    side=5

#ifdef EASY
                    income=4
                    gold=125
#endif
#ifdef NORMAL
                    income=6
                    gold=150
#endif
#ifdef HARD
                    income=8
                    gold=150
#endif
                [/modify_side]
            [/else]
        [/if]

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

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

        #create 2 troll leaders (32,70) (52,72)
        [unit]
            type=Troll Warrior
            id=Thungar
            name= _ "Thungar"
            canrecruit=yes
            x=26
            y=43
            side=2
            [modifications]
                {TRAIT_LOYAL}
                {TRAIT_RESILIENT}
            [/modifications]
            {IS_LOYAL}
        [/unit]

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

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

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

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

        #destroy ally's villages in the tunnels
        [if]
            [variable]
                name=elvish_ally
                numerical_equals=1
            [/variable]

            [then]
                # if allied with dwarves
                # destroy dwarvish villages in tunnels

                [terrain]
                    x=28,35
                    y=19,14
                    terrain=Uu
                [/terrain]

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

            [else]
                # if allied with trolls
                # destroy troll villages in tunnels

                [terrain]
                    x=25
                    y=36
                    terrain=Uu^Uf
                [/terrain]

                [terrain]
                    x=34
                    y=37
                    terrain=Xu
                [/terrain]

                [terrain]
                    x=47,37
                    y=36,47
                    terrain=Uu
                [/terrain]
            [/else]
        [/if]

        # 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=31,30
            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=36,32
            side=3
        [/capture_village]

        [capture_village]
            x,y=40,26
            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]

        # 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=35,27
            side=5
        [/capture_village]

        #count number of turns since central cave battle started

        [set_variable]
            name=battle_turn_counter
            value=1
        [/set_variable]
    [/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)

    # they destroy (Easy:33%  Medium: 50% Hard: 50%) of your allies

#define ENEMY_ATTACK

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

        [then]
            #if allied with dwarves

            #west side

            [move_unit_fake]
                type=Troll Shaman
                side=2
                x=22,23,24,25,26,27,27,27,28
                y=37,37,36,36,35,35,34,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=32,33,34,34,34,34,33,32,31,30
                y=38,38,37,36,35,34,34,33,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=47,47,47,46,45,44,43,42,42,42,42,41
                y=37,36,35,34,34,33,33,32,31,30,29,29
            [/move_unit_fake]

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

#ifdef EASY
#else

            [move_unit_fake]
                type=Troll Shaman
                side=3
                x=47,47,47,46,45,44,43,43,43,43,43
                y=27,26,25,24,24,23,23,22,21,20,19
            [/move_unit_fake]

            {NAMED_GENERIC_UNIT 3 (Troll Shaman) 43 19 () ( _ "Troll Flamecaster")}
            # wmllint: recognize 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]

            [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 dwarves in main cave in array
            #main cave = x 32-51 y 21-33
            #store length of array as X
            #divide X in half = Y
            #loop Y times:
            #   (store dwarves in main cave in array)
            #   (store length of array as Z)
            #   (Random 0 to Z-1)
            #   (Kill random dwarf)
            #   [set_variable]
            #   name=kill_x
            #   to_variable=locs[$i].x
            #   [/set_variable]
            #
            #   [set_variable]
            #   name=kill_y
            #   to_variable=locs[$i].y
            #   [/set_variable]

            [store_locations]
                x=24-46
                y=19-35
                [filter]
                    role="Dwarf Defender"
                [/filter]
                variable=dwarf_list
            [/store_locations]

            [set_variable]
                name=number_of_dwarves
                value=$dwarf_list.length
            [/set_variable]

            [set_variable]
                name=number_to_kill
                value=$number_of_dwarves
            [/set_variable]

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

#ifdef EASY
            [set_variable]
                name=number_to_kill
                multiply=0.59
            [/set_variable]
#endif

#ifdef NORMAL
            [set_variable]
                name=number_to_kill
                multiply=0.69
            [/set_variable]
#endif

#ifdef HARD
            [set_variable]
                name=number_to_kill
                multiply=0.79
            [/set_variable]
#endif

            [set_variable]
                name=i
                value=$number_to_kill
            [/set_variable]

            [if]
                [variable]
                    name=number_to_kill
                    greater_than=0
                [/variable]
                [then]
                    {STARTLOOP i}

                    [store_locations]
                        x=24-46
                        y=19-35
                        [filter]
                            role="Dwarf Defender"
                        [/filter]
                        variable=dwarf_victims
                    [/store_locations]

                    [set_variable]
                        name=number_of_victims
                        value=$dwarf_victims.length
                    [/set_variable]

                    #subtract 1 because arrays go from 0 to length-1
                    [set_variable]
                        name=number_of_victims
                        sub=1
                    [/set_variable]

                    [set_variable]
                        name=random_string
                        value=0..$number_of_victims
                    [/set_variable]

                    [set_variable]
                        name=death
                        rand=$random_string
                    [/set_variable]

                    [set_variable]
                        name=kill_x
                        to_variable=dwarf_victims[$death].x
                    [/set_variable]

                    [set_variable]
                        name=kill_y
                        to_variable=dwarf_victims[$death].y
                    [/set_variable]

                    #have some dwarves scream as they die

                    [if]
                        [variable]
                            name=i
                            numerical_equals=-1
                        [/variable]

                        [then]
                            [message]
                                x,y=$kill_x,$kill_y
                                message= _ "Aauughh!"
                            [/message]
                        [/then]
                    [/if]

                    [if]
                        [variable]
                            name=i
                            numerical_equals=-2
                        [/variable]

                        [then]
                            [message]
                                x,y=$kill_x,$kill_y
                                message= _ "No...!"
                            [/message]
                        [/then]
                    [/if]

                    [if]
                        [variable]
                            name=i
                            numerical_equals=-3
                        [/variable]

                        [then]
                            [message]
                                x,y=$kill_x,$kill_y
                                message= _ "Help me!!"
                            [/message]
                        [/then]
                    [/if]

                    [kill]
                        x,y=$kill_x,$kill_y
                        animate=yes
                        fire_event=no
                    [/kill]

                    {ENDLOOP i}
                [/then]
            [/if]

            [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 you best, boy, and may Moradin 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")}

#ifdef EASY
#else

            [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")}
            # wmllint: recognize 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]

            [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_locations]
                x=24-46
                y=19-35
                [filter]
                    role="Troll Defender"
                [/filter]
                variable=troll_list
            [/store_locations]

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

            [set_variable]
                name=number_to_kill
                value=$number_of_trolls
            [/set_variable]

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

#ifdef EASY
            [set_variable]
                name=number_to_kill
                multiply=0.49
            [/set_variable]
#endif

#ifdef NORMAL
            [set_variable]
                name=number_to_kill
                multiply=0.59
            [/set_variable]
#endif

#ifdef HARD
            [set_variable]
                name=number_to_kill
                multiply=0.74
            [/set_variable]
#endif

            [set_variable]
                name=i
                value=$number_to_kill
            [/set_variable]

            [if]
                [variable]
                    name=number_to_kill
                    greater_than=0
                [/variable]
                [then]
                    {STARTLOOP i}

                    [store_locations]
                        x=24-46
                        y=19-35
                        [filter]
                            role="Troll Defender"
                        [/filter]
                        variable=troll_victims
                    [/store_locations]

                    [set_variable]
                        name=number_of_victims
                        value=$troll_victims.length
                    [/set_variable]

                    #subtract 1 because arrays go from 0 to length-1
                    [set_variable]
                        name=number_of_victims
                        sub=1
                    [/set_variable]

                    [set_variable]
                        name=random_string
                        value=0..$number_of_victims
                    [/set_variable]

                    [set_variable]
                        name=death
                        rand=$random_string
                    [/set_variable]

                    [set_variable]
                        name=kill_x
                        to_variable=troll_victims[$death].x
                    [/set_variable]

                    [set_variable]
                        name=kill_y
                        to_variable=troll_victims[$death].y
                    [/set_variable]

                    #have some trolls scream as they die

                    [if]
                        [variable]
                            name=i
                            numerical_equals=-1
                        [/variable]

                        [then]
                            [message]
                                x,y=$kill_x,$kill_y
                                message= _ "Aaughh!"
                            [/message]
                        [/then]
                    [/if]

                    [if]
                        [variable]
                            name=i
                            numerical_equals=-2
                        [/variable]

                        [then]
                            [message]
                                x,y=$kill_x,$kill_y
                                message= _ "No...!"
                            [/message]
                        [/then]
                    [/if]

                    [if]
                        [variable]
                            name=i
                            numerical_equals=-3
                        [/variable]

                        [then]
                            [message]
                                x,y=$kill_x,$kill_y
                                message= _ "Gaaghh!"  # wmllint: no spellcheck
                            [/message]
                        [/then]
                    [/if]

                    [kill]
                        x,y=$kill_x,$kill_y
                        animate=yes
                        fire_event=no
                    [/kill]

                    {ENDLOOP i}
                [/then]
            [/if]

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

#enddef

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

#define ALLY_REINFORCEMENTS

    [if]
        [variable]
            name=elvish_ally
            numerical_equals=2
        [/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
                [modifications]
                    {TRAIT_LOYAL}
                    {TRAIT_RESILIENT}
                [/modifications]
                {IS_LOYAL}
            [/unit]

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

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

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

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

            [role]
                role=ally
                type=Troll Whelp
            [/role]

            [role]
                role=ally
                type=Troll Rocklobber
            [/role]

            [role]
                role=ally
                type=Troll Shaman
            [/role]

            [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]
        [/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
                [modifications]
                    {TRAIT_LOYAL}
                    {TRAIT_STRONG}
                [/modifications]
                {IS_LOYAL}
            [/unit]

            [unit]
                type=Dwarvish Pathfinder
                id=Moin
                name= _ "Moin"
                x=35
                y=21
                side=1
                [modifications]
                    {TRAIT_LOYAL}
                    {TRAIT_QUICK}
                [/modifications]
                {IS_LOYAL}
            [/unit]

            [unit]
                type=Dwarvish Thunderer
                id=Nordi
                name= _ "Nordi"
                x=37
                y=21
                side=1
                [modifications]
                    {TRAIT_LOYAL}
                    {TRAIT_INTELLIGENT}
                [/modifications]
                {IS_LOYAL}
            [/unit]

            [unit]
                type=Dwarvish Berserker
                id=Byorn
                name= _ "Byorn"
                x=38
                y=20
                side=1
                [modifications]
                    {TRAIT_LOYAL}
                    {TRAIT_RESILIENT}
                [/modifications]
                {IS_LOYAL}
            [/unit]

#ifdef HARD
#else

            [unit]
                type=Dwarvish Fighter
                id=Runin
                name= _ "Runin"
                x=35
                y=20
                side=1
                [modifications]
                    {TRAIT_LOYAL}
                    {TRAIT_QUICK}
                [/modifications]
                {IS_LOYAL}
            [/unit]

            [role]
                role=ally
                type=Dwarvish Fighter
            [/role]
#endif

            [role]
                role=ally
                type=Dwarvish Fighter
            [/role]

            [role]
                role=ally
                type=Dwarvish Berserker
            [/role]

            [role]
                role=ally
                type=Dwarvish Thunderer
            [/role]

            [role]
                role=ally
                type=Dwarvish Pathfinder
            [/role]

            [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]
        [/else]
    [/if]

#enddef

    # Battle Turn Counter
    # Each turn increment counter, to keep track of when battle
    # events should happen. 0 = off
    # 1 = player just entered main cave, start incrementing counter each turn

    # Turn after player enters main cave it is 2, next turn 3, 4, 5 and so on

    # also at start of each turn, erase gold/income for sides that are dead
    # or who haven't been activated yet #TODO where is that done?
    # TODO recode using nested events and get rid of the battle_turn_counter

    [event]
        name=new turn
        first_time_only=no

        [if]
            [variable]
                name=battle_turn_counter
                greater_than=0
            [/variable]

            [then]
                [set_variable]
                    name=battle_turn_counter
                    add=1
                [/set_variable]
            [/then]
        [/if]

        # on turn 3 have winning enemy launch special attack
        [if]
            [variable]
                name=battle_turn_counter
                numerical_equals=4
            [/variable]

            [then]
                {ENEMY_ATTACK}
            [/then]
        [/if]

        # on turn 6 have losing enemy bring reinforcements
        [if]
            [variable]
                name=battle_turn_counter
                numerical_equals=7
            [/variable]

            [then]
                {ALLY_REINFORCEMENTS}
            [/then]
        [/if]

        # at a random time later, the assassin should appear and challenge Kaleh

        [if]
            [variable]
                name=battle_turn_counter
                numerical_equals=$assassin_turn
            [/variable]

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

    # Event 4.5: when player moves onto dwarf cairn or troll totems,
    # his ally comments about it

    #troll totems
    [event]
        name=moveto
        first_time_only=no

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

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

            [variable]
                name=saw_cairn_or_totem
                numerical_equals=0
            [/variable]

            [have_unit]
                role=ally
            [/have_unit]

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

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

        [allow_undo]
        [/allow_undo]
    [/event]

    #dwarf cairns
    [event]
        name=moveto
        first_time_only=no

        [filter]
            x=30,38,42
            y=20,20,21
            side=1
        [/filter]

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

            [variable]
                name=saw_cairn_or_totem
                numerical_equals=0
            [/variable]

            [have_unit]
                role=ally
            [/have_unit]

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

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

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

    #send in troll messenger to tell player to continue north

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

    #send in dwarf messenger to tell player to continue south

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

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

    # Every turn check to see if the call_assassin flag was fired
    # When it does, 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=new turn
        first_time_only=no

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

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

                {FIND_LOCATION_FOR_DARK_ASSASSIN}

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

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

#ifdef __UNUSED__
                # commented out like this to preserve the string for now...
                [message]
                    speaker=Kaleh
                    message= _ "Ow!"
                [/message]
#endif

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

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

            [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]
            [variable]
                name=delay_victory
                numerical_equals=2
            [/variable]

            [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 i}
        {CLEAR_VARIABLE number_enemies}
        {CLEAR_VARIABLE battle_turn_counter}
        {CLEAR_VARIABLE saw_cairn_or_totem}
        {CLEAR_VARIABLE dead_dwarf_leaders}
        {CLEAR_VARIABLE dead_troll_leaders}
        {CLEAR_VARIABLE first_tunnel}
        {CLEAR_VARIABLE bridge_blown}
        {CLEAR_VARIABLE temp}
        {CLEAR_VARIABLE tempx}
        {CLEAR_VARIABLE tempy}
        {CLEAR_VARIABLE tentacle_count}
        {CLEAR_VARIABLE tentacle_deaths}
        {CLEAR_VARIABLE tripped_bats}
        {CLEAR_VARIABLE x_left}
        {CLEAR_VARIABLE x_right}
        {CLEAR_VARIABLE y_up}
        {CLEAR_VARIABLE y_down}
        {CLEAR_VARIABLE dwarf_door}
        {CLEAR_VARIABLE dwarf_amulet}
        {CLEAR_VARIABLE took_belt}

        {CLEAR_VARIABLE lava_damage}
        {CLEAR_VARIABLE heat_damage}
        {CLEAR_VARIABLE summon_flame}
        {CLEAR_VARIABLE flame_counter}
        {CLEAR_VARIABLE troll_undead_arose}
        {CLEAR_VARIABLE met_ghost}
        {CLEAR_VARIABLE buried_dwarf}
        {CLEAR_VARIABLE took_wand}
        {CLEAR_VARIABLE entered_dwarf_lair}
        {CLEAR_VARIABLE entered_troll_lair}

        {CLEAR_VARIABLE assassin_turn}
        {CLEAR_VARIABLE call_assassin}

        {CLEAR_VARIABLE saw_interrogators}
        {CLEAR_VARIABLE saw_sergeant}

        {CLEAR_VARIABLE unslow_kaleh}
        {CLEAR_VARIABLE delay_victory}
        {CLEAR_VARIABLE unitstats}
    [/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/kaleh-abilities.cfg}
    {UTBS_INCLUDE utils/deaths.cfg}
[/scenario]
