#textdomain wesnoth-utbs
# Scenario 2: Across the Harsh Sands

[scenario]

    id="2_Across_the_Harsh_Sands"
    name= _ "Across the Harsh Sands"

    map_data="{campaigns/Under_the_Burning_Suns/maps/02_Across_the_Harsh_Sands.map}"

    next_scenario=3_Stirring_in_the_Night_alt

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

    {DAWN1}
    {MORNING1}
    {MIDDAY1}
    {AFTERNOON1}
    {DUSK1}
    {SHORTDARK}
    {DAWN2}
    {MORNING2}
    {MIDDAY2}
    {AFTERNOON2}
    {DUSK2}
    {LONGDARK1}
    {LONGDARK2}
    {LONGDARK3}
    {LONGDARK4}

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

    [story]
        [part]
            story= _ "Chapter 2: When I was fifteen I went on my first raid, against an orcish incursion to the west. A large band of orcs under some new banner had come out of the northern foothills and were rampaging across the sands, killing anything they could get their filthy hands on."
        [/part]

        [part]
            story= _ "Sneaking amidst the dunes we crept up to their camp and ambushed them at dawn. To a young boy the fighting was overwhelming: crashing blades, blood, battle cries, friend and foe struggling back and forth. The orcs rallied around their leader, their greater numbers countering our superior skill at combat. It was my father who finally fought his way to the foul orc leader and slew him on the ochre sand, stained with blood. The surviving orcs, showing their true colors, fled from the battlefield. It seemed a glorious victory, and I hardly noticed our elven brethren lying dead in the sand."
        [/part]

        [part]
            story= _ "I was giddy on the journey back home, my heart pumping with elation and pride. I had fought my first battle; now I was a man like my father. Then one night during the long dark a harsh wind came up, moaning and howling around our tents. By dawn it had only gotten worse; I wondered if some dark god was trying to avenge the massacre of the orcs. I'd seen sandstorms before, but I had never experienced one like this. I hid in my tent praying to Eloh as if my life depended on it. The air grew thick with sand, and everything grew dark and hazy..."
        [/part]

        [part]
            story= _ "The next thing I remember, someone was bending over me shaking me awake. I was half buried in sand, and I felt weak but alive. Our equipment was scattered across the dunes or buried in the sand. Looking around I only saw a few of my companions, who were digging in the sand, fervently hoping to find other survivors. I dug furiously at the sand with my bare hands and yelled until I was hoarse, but try as I might, I could not find my father. They told me he had been swallowed by the sand, but I would not be consoled. In one instant my world crumbled; I never looked at the sands quite the same way again. I learned that day that the desert can be fickle and fierce, and death is always lurking just over the horizon."
        [/part]

        [part]
            story= _ "Now I journeyed out again across the sands, but this time it was not just myself, but my entire people that I had to protect. They were depending on my judgment, and I was only too aware of the weight on my narrow shoulders. Thinking of the last time we had gone out in force, I made a silent prayer to my father to watch over us. There was nowhere to go but north."
        [/part]
    [/story]

    #elves
    [side]
        side=1
        description=Kaleh
        type=Desert Fighter
        canrecruit=yes
        {INCOME 17 14 12}
        controller=human
        recruit=Desert Fighter, Desert Archer, Desert Hunter, Desert Shaman, Desert Scout
        shroud=yes
        fog=yes
    [/side]

    #Outlaw side
    [side]
        side=2
        no_leader=yes
        income=-2
        gold=0
        controller=ai
        shroud=yes
        fog=yes
        team_name=bandits
#ifdef EASY
        recruit=Thug, Bandit, Poacher, Trapper, Footpad
#endif

#ifdef NORMAL
        recruit=Bandit, Thug, Poacher, Footpad, Outlaw
#endif

#ifdef HARD
        recruit=Bandit, Thug, Trapper, Poacher, Footpad, Outlaw
#endif
        [ai]
            aggression=0.8
            caution=0.2
            recruitment_ignore_bad_movement="yes"

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

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

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

            {ATTACK_DEPTH 3 4 5}

            [target]
                side=1
                value=3
            [/target]
            [protect_location]
                x=1-39
                y=1-35
                value=10
            [/protect_location]
        [/ai]
    [/side]

    #Random monster side: ogres, scorpions
    # they target side 1 much more than side 2
    [side]
        no_leader=yes
        side=3
        controller=ai
        shroud=no
        fog=no
        team_name=monsters
        [ai]
            aggression=1.0
            caution=0.00
#ifdef EASY
            # monsters are extra dumb
            simple_targetting=yes
            grouping=no
#endif
#ifdef NORMAL
            grouping=no
#endif
            {ATTACK_DEPTH 2 3 4}

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

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

    #undead side, used to control wandering ghosts and walking dead
    [side]
        no_leader=yes
        side=4
        gold=0
        income=0
        controller=ai
        shroud=no
        fog=no
        team_name=undead
        recruit=Skeleton,Skeleton Archer,Ghost,Walking Corpse,Ghoul
        [ai]
            aggression=1.0
            caution=0.0
            recruitment_pattern=scout,fighter,fighter,archer

            grouping=no

            {ATTACK_DEPTH 2 3 4}

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

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

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

            [protect_location]
                x=32-36
                y=25-28
            [/protect_location]
        [/ai]
    [/side]

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

    # prestart events:
    # Set starting scenario objectives
    # Recall Nym, Zhul and Garak
    # Prestart variable initiation

    [event]
        name=prestart

        # Set starting scenario objectives

        [objectives]
            summary= _ "Starting Objectives:"
            [objective]
                description= _ "Kaleh Must Reach the Northern Edge of the Desert"
                condition=win
            [/objective]
            [objective]
                description= _ "Death of Kaleh, Nym, Garak or Zhul"
                condition=lose
            [/objective]
        [/objectives]

        # Recall Nym, Zhul and Garak

        [recall]
            description=Nym
        [/recall]
        [recall]
            description=Zhul
        [/recall]
        [recall]
            description=Garak
        [/recall]

        # dehydration loss - a variable for the dialogue
        {VARIABLE dehydration_loss 2}

        [label]
            x,y=21,9
            text= _ "Pinnacle Rock"
        [/label]

        # shroud MUST be removed in prestart for the label
        # info to get updated correctly before the player's
        # turn
        [remove_shroud]
            side=1
            x=19,19,20,21,22,21,20,21,22,20,22,21,23,23
            y= 9,10, 8, 8, 8, 9, 9,10, 9,10,10,11, 9,10
        [/remove_shroud]

        [remove_shroud]
            side=1
            x=1-8
            y=65-74
        [/remove_shroud]
    [/event]

    [event]
        name=start

        [message]
            description=Garak
            message= _ "I have crossed these sands long ago, when I was a youth, and we went on a great raid against a foul necromancer who was hiding out in one of the ruins, creating an army of undead. It was a nasty battle, especially after nightfall, but all his magics couldn't save him when we cut off his head. Ah, those were the days..."
        [/message]

        [message]
            description=Kaleh
            message= _ "Do you remember anything about these sands?"
        [/message]

        [scroll_to]
            x=19
            y=6
        [/scroll_to]

        [message]
            speaker=narrator
            caption=_ "Garak"
            image=portraits/garak.png
            message= _ "Do you see that brown spot sticking up on the northern horizon? That's Pinnacle Rock. It's the highest land for miles around, and has a spring at its base, or did the last time I camped there. If we make it to Pinnacle Rock we will be just a few miles from the edge of the mountains."
        [/message]

        [message]
            description=Garak
            message= _ "But between here and there is a particularly barren stretch of the sands, with only a few oases and watering holes. Luckily I think there used to be an old caravan route leading north, which went from oasis to oasis. The oases aren't easy to find but occasionally the sands blow away, revealing old stone roads that lead from oasis to oasis. Once the path must have formed a great thoroughfare for commerce. "
        [/message]

        [place_shroud]
            side=1
            x=19,19,20,21,22,21,20,21,22,20,22,21,23,23
            y= 9,10, 8, 8, 8, 9, 9,10, 9,10,10,11, 9,10
        [/place_shroud]

        [redraw]
            side=1
        [/redraw]

        [message]
            description=Zhul
            message= _ "I believe that there was once a great empire in these lands, long ago, before the sands came."
        [/message]

        #!***Extended message -> Hints about new ambush logic and the need of scouting***
        [message]
            description=Garak
            message= _ "I've seen the ancient remains of stone castles and markers in the sands. The paths of the ancients may serve us yet again. If we follow the paths from oasis to oasis, we may be able to survive the thirst and heat of the desert. But there are worse dangers in these sands than thirst, we must be wary and scout our way carefully."
        [/message]

        [message]
            description=Nym
            message= _ "Unfortunately, because of our hasty flight from our village, we are short on waterskins and rations. We'll have enough if we move quickly and eat as little as possible, but we won't last long in the wastes if we can't find more sources of water. As it is, between the heat of the day and the cold of the night, this journey will be hard on our people."
        [/message]

        # Rewriten message -> explains new dehydration rules
        [message]
            speaker=narrator
            message= _ "During the daytime (Dawn, Morning, Mid-day, Afternoon, and Dusk) at the beginning of each your turns, every unit in a sand, road, rubble or sand dune hex will suffer from thirst. Each turn of thirst reduces a unit's attack damage and causes $dehydration_loss hitpoints of dehydration damage that cannot be healed by your shamans or druids. Only by refreshing at an oasis (any shallow water hex) at the start of your turn will your units regain full attack strength and some of the lost hitpoints."
            image=wesnoth-icon.png
        [/message]

        [message]
            description=Nym
            message= _ "Kaleh, be careful you don't recruit too many guards to go with us. I doubt we're going to find any other villages out in the sands, so the income that we have right now is all we're going to get. Remember that as our people get more experienced they often require more support, so we don't want to run out of supplies halfway across the desert."
        [/message]

        [message]
            description=Kaleh
            message= _ "Well, the sooner we reach Pinnacle Rock, the better. Onwards!"
        [/message]

        {VARIABLE ghosts_talk 0}
        # setup ghost difficulty
#ifdef EASY
        {VARIABLE max_ghosts 6}
        {VARIABLE max_per_turn 1}
#endif
#ifdef NORMAL
        {VARIABLE max_ghosts 7}
        {VARIABLE max_per_turn 2}
#endif
#ifdef HARD
        {VARIABLE max_ghosts 8}
        {VARIABLE max_per_turn 3}
#endif
    [/event]

    # This sets up some unit variables for sides that
    # will suffer thirst
#define SETUP_ENEMY_THIRST SIDE THIRST_NUMBER
    [store_unit]
        variable=enemy
        kill=yes
        [filter]
            side={SIDE}
        [/filter]
    [/store_unit]
    {FOREACH enemy i}
    {VARIABLE enemy[$i].variables.dehydration {THIRST_NUMBER}}
    {VARIABLE enemy[$i].variables.full_hitpoints $enemy[$i].hitpoints}
    {VARIABLE enemy[$i].variables.max_hitpoints $enemy[$i].max_hitpoints}
    [unstore_unit]
        variable=enemy[$i]
        find_vacant=no
    [/unstore_unit]
    {NEXT i}
#enddef

#define MOBILIZE_SIDE SIDE
    [store_unit]
        variable=enemy
        kill=yes
        [filter]
            side={SIDE}
        [/filter]
    [/store_unit]
    {FOREACH enemy i}
    {CLEAR_VARIABLE enemy[$i].ai_special}
    [unit]
        side={SIDE}
        type=$enemy[$i].type
        user_description=$enemy[$i].user_description
        facing=$enemy[$i].facing
        gender=$enemy[$i].gender
        x=$enemy[$i].x
        y=$enemy[$i].y
        hitpoints=$enemy[$i].hitpoints
        random_traits=no
        canrecruit=$enemy[$i].canrecruit
        experience=$enemy[$i].experience
        role=$enemy[$i].role
        moves=$enemy[$i].moves
        attacks_left=$enemy[$i].attacks_left
        [status]
            slowed=$enemy[$i].status.slowed
            poisoned=$enemy[$i].status.poisoned
        [/status]
        [variables]
            dehydrated=$enemy[$i].variables.dehydrated
            full_hitpoints=$enemy[$i].variables.full_hitpoints
            max_hitpoints=$enemy[$i].variables.max_hitpoints
        [/variables]
    [/unit]
    {NEXT i}
#enddef

#define INIT_HYDRATION_FOR_PLAYER_UNIT
    {VARIABLE player_unit.variables.full_hitpoints $player_unit.hitpoints}
    {VARIABLE player_unit.variables.max_hitpoints $player_unit.max_hitpoints}
    {VARIABLE player_unit.variables.dehydration 0}
    [unstore_unit]
        variable=player_unit
        find_vacant=no
    [/unstore_unit]
#enddef

    # macro for all hydration effects to all units stored in
    # a variable called "affected_unit"
    # EFFECT_NUMBER > 0 is dehydration
    # EFFECT_NUMBER = 0 is silent rehydration
    # EFFECT_NUMBER < 0 is rehydration with notification

#define APPLY_HYDRATION_EFFECT EFFECT_NUMBER
    {FOREACH affected_unit i}
    {VARIABLE effect {EFFECT_NUMBER}}
    {VARIABLE thirst $affected_unit[$i].variables.dehydration}
    {VARIABLE hp_to_regain 0}
    # are we dehydrating or rehydrating?
    [if]
        [variable]
            name=effect
            greater_than=0
        [/variable]
        [then]
            {VARIABLE_OP thirst add 1}
            {VARIABLE green -30}
            {VARIABLE_OP green multiply $thirst}
            {VARIABLE_OP green add 280}
            [if]
                [variable]
                    name=green
                    less_than=0
                [/variable]
                [then]
                    {VARIABLE green 0}
                [/then]
            [/if]
            {VARIABLE red 250}
            {VARIABLE blue 52}
        [/then]
        [else]
            # rehydrate
            {VARIABLE hp_to_regain $thirst}
            {VARIABLE_OP hp_to_regain multiply 2}
            {VARIABLE red 170}
            {VARIABLE green 230}
            {VARIABLE blue 245}
            [if]
                [variable]
                    name=hp_to_regain
#ifdef EASY
                    greater_than=8
#else
                    greater_than=4
#endif
                [/variable]
                [then]
#ifdef EASY
                    {VARIABLE hp_to_regain 8}
#else
                    {VARIABLE hp_to_regain 4}
#endif
                [/then]
            [/if]
            # make rehydrate silent if the unit isn't
            # suffering dehydration
            [if]
                [variable]
                    name=thirst
                    less_than=1
                [/variable]
                [then]
                    {VARIABLE effect 0}
                [/then]
            [/if]
            {VARIABLE thirst 0}
        [/else]
    [/if]
    # modify attacks
    {FOREACH affected_unit[$i].attack j}
    [if]
        [variable]
            name=affected_unit[$i].attack[$j].range
            equals=melee
        [/variable]
        [then]
            {VARIABLE damage_multiplier 10}
        [/then]
        [else]
            {VARIABLE damage_multiplier 20}
        [/else]
    [/if]
    {VARIABLE damage_divisor $damage_multiplier}
    {VARIABLE_OP damage_multiplier add -$thirst}
    # store real_damage
    [if]
        [variable]
            name=affected_unit[$i].attack[$j].real_damage
            greater_than=0
        [/variable]
        [else]
            {VARIABLE affected_unit[$i].attack[$j].real_damage $affected_unit[$i].attack[$j].damage}
        [/else]
    [/if]
    {VARIABLE new_damage $affected_unit[$i].attack[$j].real_damage}
    {VARIABLE_OP new_damage multiply $damage_multiplier}
    {VARIABLE_OP new_damage divide $damage_divisor}
    [if]
        [variable]
            name=new_damage
            less_than=1
        [/variable]
        [then]
            {VARIABLE new_damage 1}
        [/then]
    [/if]
    {VARIABLE affected_unit[$i].attack[$j].damage $new_damage}
    {NEXT j}
    # adjust current hp
    [if]
        [variable]
            name=thirst
            greater_than=0
        [/variable]
        [then]
            {VARIABLE_OP affected_unit[$i].hitpoints add -$dehydration_loss}
            {VARIABLE_OP affected_unit[$i].max_hitpoints add -$dehydration_loss}
            {VARIABLE affected_unit[$i].variables.dehydration $thirst}
        [/then]
        [else]
#ifdef HARD
#else
            {VARIABLE_OP affected_unit[$i].hitpoints add $hp_to_regain}
#endif
            {VARIABLE affected_unit[$i].max_hitpoints $affected_unit[$i].variables.max_hitpoints}
            {VARIABLE affected_unit[$i].variables.dehydration 0}
        [/else]
    [/if]
    {VARIABLE hp_check $affected_unit[$i].hitpoints}
    # check hp
    [if]
        [variable]
            name=hp_check
            less_than=1
        [/variable]
        [then]
            {VARIABLE affected_unit[$i].hitpoints 1}
        [/then]
    [/if]
    [if]
        [variable]
            name=effect
            greater_than=0
        [/variable]
        [then]
            [unstore_unit]
                variable=affected_unit[$i]
                find_vacant=no
                red,green,blue=$red,$green,$blue
                text= _ "thirst"
            [/unstore_unit]
        [/then]
        [else]
            [if]
                [variable]
                    name=effect
                    not_equals=0
                [/variable]
                [then]
                    [unstore_unit]
                        variable=affected_unit[$i]
                        find_vacant=no
                        red,green,blue=$red,$green,$blue
                        text= _ "refreshed"
                    [/unstore_unit]
                [/then]
                [else]
                    # silent unstore
                    [unstore_unit]
                        variable=affected_unit[$i]
                        find_vacant=no
                    [/unstore_unit]
                [/else]
            [/if]
        [/else]
    [/if]
    {NEXT i}
    {CLEAR_VARIABLE hp_check}
    {CLEAR_VARIABLE thirst}
    {CLEAR_VARIABLE hp_to_regain}
    {CLEAR_VARIABLE damage_multiplier}
    {CLEAR_VARIABLE damage_divisor}
    {CLEAR_VARIABLE new_damage}
    {CLEAR_VARIABLE green}
    {CLEAR_VARIABLE red}
    {CLEAR_VARIABLE blue}
#enddef

    # Special Encounter events

    # Encounter 1: Scorpions attack

    [event]
        name=moveto

        [filter]
            side=1
            x=21-32
            y=59-70
        [/filter]

#define SCORPION_PLACEMENT

    {RANDOM_PLACEMENT_AREA 29 62 2}
#ifdef EASY
    {VARIABLE number_of_scorpions 4}
#endif
#ifdef NORMAL
    {VARIABLE number_of_scorpions 6}
#endif
#ifdef HARD
    {VARIABLE number_of_scorpions 8}
#endif

    {PLACE_UNITS_RANDOMLY $number_of_scorpions 3 "Giant Scorpion" "Scorpion" ( _ "Scorpion") (ai_special="guardian")}

    {CLEAR_VARIABLE number_of_scorpions}

    {CLEAR_PLACEMENT_AREA}

#enddef

#define RING_PICKUP X Y
    [event]
        name=moveto

        [filter]
            x={X}
            y={Y}
            side=1
        [/filter]

        [object]
            id=Travelring
            name= _ "Traveler's Ring"
            image=items/ring-white.png
            description= _ "At the end of each turn, this unit takes no damage from the desert."
            [filter]
                x={X}
                y={Y}
                side=1
            [/filter]
        [/object]

        [removeitem]
            x={X}
            y={Y}
        [/removeitem]

        [role]
            role=immune
            x={X}
            y={Y}
        [/role]
        {CLEAR_VARIABLE tempx}
        {CLEAR_VARIABLE tempy}
    [/event]
#enddef

        #!***Check if unit is scout type***
        [if]
            [variable]
                name=unit.usage
                equals=scout
            [/variable]

            #!***Spot scorpions, give player chance to avoid fight***
            [then]
                [message]
                    speaker=unit
                    message=_"Wait. I think I spotted something ahead of us..."
                [/message]
                [message]
                    description=Kaleh
                    message=_"What? I don't see a thing."
                [/message]
                [message]
                    speaker=unit
                    message=_"See that shining speckles ahead? Scorpions. Large. Whole nest of them."
                [/message]

                {SCORPION_PLACEMENT}

                [message]
                    description=Garak
                    message=_"Do you want us to fight? We can still avoid them, but they are between us and the next oasis."
                [/message]
            [/then]

            #!***Set ambush***

            [else]
                [event]
                    name=moveto
                    [filter]
                        side=1
                        x=26-32
                        y=59-65
                    [/filter]

                    [message]
                        speaker=unit
                        message= _ "Scorpions! We must have stumbled across a nest of them."
                    [/message]

                    {SCORPION_PLACEMENT}
                [/event]
            [/else]
        [/if]

        #!***Set scorpion death event when needed only***
        # wmllint: recognize Scorpion

        [event]
            name=die
            first_time_only=no
            [filter]
                description=Scorpion
            [/filter]

            {VARIABLE tempx $x1}
            {VARIABLE tempy $y1}
            [if]
                [have_unit]
                    description=Scorpion
                [/have_unit]

                [else]
                    [item]
                        x=$x1
                        y=$y1
                        image=items/ring-white.png
                    [/item]

                    [message]
                        speaker=Kaleh
                        message= _ "The scorpions were devouring some poor person's body. There doesn't seem to be much of him or her left, but wait...what's this? It looks like a tiny gold ring. I think I see elvish runes on the inside, but I can barely make them out. This seems to be a ring of travel! Those who wear it will not suffer from thirst or hunger, nor cold, nor heat. I've heard tales of such magical items, and we can certainly use it now."
                    [/message]

                    [message]
                        speaker=narrator
                        message= _ "One unit wearing this ring is immune to any damage caused during the day by the heat of the desert."
                        image=wesnoth-icon.png
                    [/message]

                    {RING_PICKUP $tempx $tempy}
                [/else]
            [/if]
        [/event]

        #undef SCORPION_PLACEMENT
        #undef RING_PICKUP
    [/event]

    #Encounter 1.5: Second Oasis, remind player of healing properties
    [event]
        name=moveto

        [filter]
            x=27-38
            y=55-65
            side=1
        [/filter]

        [message]
            speaker=unit
            message= _ "Look, an oasis! Its refreshing water will allow our people to regain strength and rest safely on the grass during the heat of the day."
        [/message]
    [/event]

    #Encounter 2 Ogre Ambush
    [event]
        name=moveto

        [filter]
            x=8-25
            y=42-56
            side=1
        [/filter]

#define OGRE_PLACEMENT

    {RANDOM_PLACEMENT_AREA 17 49 2}

    {PLACE_UNITS_RANDOMLY 3 3 "Young Ogre" "Hunting Ogre" ( _ "Hunting Ogre") (ai_special="guardian"
    random_traits="no")}
    {PLACE_UNITS_RANDOMLY 2 3 "Ogre"       "Hunting Ogre" ( _ "Hunting Ogre") (ai_special="guardian"
    random_traits="no")}

    {CLEAR_PLACEMENT_AREA}

#ifdef EASY
    {VARIABLE ogre_thirst 3}
#endif
#ifdef NORMAL
    {VARIABLE ogre_thirst 1}
#endif
#ifdef HARD
    {VARIABLE ogre_thirst -1}
#endif

    {SETUP_ENEMY_THIRST 3 $ogre_thirst}

    # also need to apply thirst right away
    [store_unit]
        variable=affected_unit
        kill=no
        [filter]
            side=3
            x=13-22
            y=44-54
        [/filter]
    [/store_unit]
    {APPLY_HYDRATION_EFFECT 1}

    {VARIABLE ogres_killed 0}

#enddef

        [if]
            [variable]
                name=unit.usage
                equals=scout
            [/variable]

            [then]
                [message]
                    speaker=unit
                    message=_ "Shh... Ogres ahead. Behind that dune there, we have to pass them if we want to reach the next oasis."
                [/message]
                [message]
                    description=Kaleh
                    message=_ "No chance to sneak around?"
                [/message]
                [message]
                    speaker=unit
                    message=_ "We would have to cover extra distance in difficult terrain. If you want to bypass them I suggest going straight north in the hopes of finding an oasis there."
                [/message]

                {OGRE_PLACEMENT}

                # move into the inner area to alert ogres
                [event]
                    name=moveto
                    [filter]
                        x=14-20
                        y=46-52
                        side=1
                    [/filter]
                    [message]
                        type=Ogre
                        message= _ "Fresh meat!"
                    [/message]
                [/event]
            [/then]

            [else]
                [event]
                    name=moveto

                    [filter]
                        x=14-20
                        y=46-52
                        side=1
                    [/filter]

                    {OGRE_PLACEMENT}

                    [message]
                        description=Kaleh
                        message= _ "Ogre ambush!"
                    [/message]

                    [message]
                        type=Ogre
                        message= _ "Fresh meat!"
                    [/message]

                    [message]
                        speaker=unit
                        message= _ "Ugh, they smell as bad as they look. And they're huge! Well, the bigger they are, the harder they fall."
                    [/message]
                [/event]
            [/else]
        [/if]

        [event]
            name=die
            first_time_only=no
            [filter]
                role="Hunting Ogre"
            [/filter]

            [if]
                [not]
                    [have_unit]
                        role="Hunting Ogre"
                    [/have_unit]
                [/not]

                [then]
                    [message]
                        speaker=Kaleh
                        message= _ "That's the last of them. This looks like a hunting party, they must have a camp around here somewhere."
                    [/message]

                    [item]
                        x=$x1
                        y=$y1
                        image=items/potion-grey.png
                    [/item]

                    [redraw]
                    [/redraw]

                    [message]
                        description=Nym
                        message=_"Hey! Look there, they dropped something... A stone bottle, sealed. I wonder what's inside..."
                    [/message]

                    [teleport]
                        [filter]
                            description=Nym
                        [/filter]
                        x=$x1
                        y=$y1
                    [/teleport]

                    [message]
                        description=Zhul
                        message=_"Nym! No! Don't open..."
                    [/message]

                    [removeitem]
                        x=$x1
                        y=$y1
                    [/removeitem]

                    [message]
                        description=Nym
                        message=_"Too late. And it's just sand inside. Not worth... Whoa! What's happening?!"
                    [/message]

                    [unit]
                        type=Dust Devil
                        description=Dust Devil
                        user_description=_"Dust Devil"
                        [modifications]
                            {TRAIT_LOYAL}
                        [/modifications]
                        x=$x1
                        y=$y1
                    [/unit]

                    [redraw]
                    [/redraw]

                    [message]
                        description=Garak
                        message=_"It's a dust devil, but I have never seen one so small before."
                    [/message]

                    # this isn't working yet - but takes ages to filter
                    # so I'm commenting it out for 1.3.8
                    #[store_locations]
                    #  variable=move
                    #  x=$x1
                    #  y=$y1
                    #  radius=1
                    #  [filter_radius]
                    #    [not]
                    #      [filter]
                    #        side=1,2,3,4
                    #      [/filter]
                    #    [/not]
                    #  [/filter_radius]
                    #[/store_locations]

                    #{VARIABLE i 0}
                    #[while]
                    #  [variable]
                    #    name=i
                    #    less_than=4
                    #  [/variable]
                    #  [do]
                    #    {RANDOM 1..move.length}
                    #    {VARIABLE moves[$i].x $move[$random].x}
                    #    {VARIABLE moves[$i].y $move[$random].y}
                    #  [/do]
                    #[/while]
                    #{CLEAR_VARIABLE move}

                    #[move_unit_fake]
                    #    type=Dust Devil
                    #    side=1
                    #    x=$moves[0].x,$x1,$moves[1].x,$x1,$moves[2].x,$x1,$moves[3].x,$x1
                    #    y=$moves[0].y,$y1,$moves[1].y,$y1,$moves[2].y,$y1,$moves[3].y,$y1
                    #[/move_unit_fake]

                    #{CLEAR_VARIABLE moves}

                    [message]
                        description=Kaleh
                        message=_"It seems to like you, looks like you just got yourself a pet."
                    [/message]

                    [message]
                        description=Zhul
                        message=_"Girl, I told you not to open it. Let's go, I'd really like to get to an oasis soon."
                    [/message]

                    {CLEAR_VARIABLE ogres_killed}
                [/then]
            [/if]
        [/event]

        #undef OGRE_PLACEMENT
    [/event]

    #Encounter 3: Black Hand Ambush
    [event]
        name=turn 5

        {RANDOM_PLACEMENT_AREA 6 43 3}

#ifdef EASY
        {VARIABLE trappers 1}
        {VARIABLE thugs    3}
        {VARIABLE poachers 3}
        {VARIABLE bandits  0}
#endif
#ifdef NORMAL
        {VARIABLE trappers 1}
        {VARIABLE thugs    3}
        {VARIABLE poachers 3}
        {VARIABLE bandits  1}
#endif
#ifdef HARD
        {VARIABLE trappers 2}
        {VARIABLE thugs    2}
        {VARIABLE poachers 2}
        {VARIABLE bandits  2}
#endif

        {PLACE_UNITS_RANDOMLY $trappers 2 "Trapper" "Black Lieutenant"  ( _ "Black Lieutenant")  (ai_special="guardian")}
        {PLACE_UNITS_RANDOMLY $thugs    2 "Thug"    "Black Hand Bandit" ( _ "Black Hand Bandit") (ai_special="guardian")}
        {PLACE_UNITS_RANDOMLY $poachers 2 "Poacher" "Black Hand Bandit" ( _ "Black Hand Bandit") (ai_special="guardian")}
        {PLACE_UNITS_RANDOMLY $bandits  2 "Bandit"  "Black Hand Bandit" ( _ "Black Hand Bandit") (ai_special="guardian")}

        {CLEAR_VARIABLE trappers}
        {CLEAR_VARIABLE thugs}
        {CLEAR_VARIABLE poachers}
        {CLEAR_VARIABLE bandits}

        {CLEAR_PLACEMENT_AREA}

        {SETUP_ENEMY_THIRST 2 0}

        {VARIABLE bandits_mobilized 0}

        [if]
            [have_unit]
                side=1
                x=1-17
                y=37-51
            [/have_unit]

            [then]
                [scroll_to_unit]
                    type=Thug
                [/scroll_to_unit]

                {CLEAR_FOG 1 8 44 2}

                [message]
                    type=Thug
                    message= _ "And I thought it was going to be another boring patrol. You there! Elf! This is our oasis, and we will water it with your blood!"
                [/message]

                [message]
                    speaker=Kaleh
                    message= _ "We need that water, and if we have to go through you to get it, so be it."
                [/message]
                {MOBILIZE_SIDE 2}
                {UNCLEAR_FOG}
            [/then]

            [else]
                {VARIABLE black_patrol_dialogue 0}
                [event]
                    name=sighted

                    [filter]
                        side=1
                    [/filter]

                    [filter_second]
                        role=Black Lieutenant
                    [/filter_second]

                    [if]
                        [variable]
                            name=black_patrol_dialogue
                            equals=0
                        [/variable]
                        [then]
                            [scroll_to_unit]
                                role=Black Lieutenant
                            [/scroll_to_unit]

                            {CLEAR_FOG 1 8 44 2}

                            [message]
                                role=Black Lieutenant
                                message= _ "We don't know who you are, and we don't much care. Tremble before the might of the Black Hand!"
                            [/message]

                            [message]
                                speaker=Kaleh
                                message= _ "We need that water, and if we have to go through you to get it, so be it."
                            [/message]

                            {VARIABLE black_patrol_dialogue 1}
                            [if]
                                [variable]
                                    name=bandits_mobilized
                                    less_than=1
                                [/variable]
                                [then]
                                    {MOBILIZE_SIDE 2}
                                    {VARIABLE bandits_mobilized 1}
                                [/then]
                            [/if]
                        [/then]
                    [/if]
                    {UNCLEAR_FOG}
                [/event]
                # events that spark off Bandit mobility:
                # - a fight between one of the bandits
                # - and a player unit dying
                [event]
                    name=attack
                    [filter]
                        side=1
                    [/filter]
                    [filter_second]
                        side=2
                    [/filter_second]
                    [if]
                        [variable]
                            name=bandits_mobilized
                            less_than=1
                        [/variable]
                        [then]
                            {MOBILIZE_SIDE 2}
                            {VARIABLE bandits_mobilized 1}
                        [/then]
                    [/if]
                [/event]
                [event]
                    name=sighted
                    [filter]
                        side=2
                    [/filter]
                    [filter_second]
                        side=1
                    [/filter_second]
                    [if]
                        [variable]
                            name=black_patrol_dialogue
                            equals=0
                        [/variable]
                        [then]
                            [scroll_to_unit]
                                role=Black Lieutenant
                            [/scroll_to_unit]

                            {CLEAR_FOG 1 8 44 2}

                            [message]
                                role=Black Lieutenant
                                message= _ "We don't know who you are, and we don't much care. Tremble before the might of the Black Hand!"
                            [/message]

                            [message]
                                speaker=Kaleh
                                message= _ "We need that water, and if we have to go through you to get it, so be it."
                            [/message]

                            {VARIABLE black_hand_dialogue 1}
                            [if]
                                [variable]
                                    name=bandits_mobilized
                                    less_than=1
                                [/variable]
                                [then]
                                    {MOBILIZE_SIDE 2}
                                    {VARIABLE bandits_mobilized 1}
                                [/then]
                            [/if]
                            {UNCLEAR_FOG}
                        [/then]
                    [/if]
                [/event]
                # a player unit being killed by an ogre
                [event]
                    name=die
                    first_time_only=no
                    [filter]
                        side=1
                    [/filter]
                    [filter_second]
                        side=3
                        [not]
                            type=Giant Scorpion
                        [/not]
                    [/filter_second]
                    [if]
                        [variable]
                            name=bandits_mobilized
                            less_than=1
                        [/variable]
                        [then]
                            {MOBILIZE_SIDE 2}
                            {VARIABLE bandits_mobilized 1}
                        [/then]
                    [/if]
                [/event]
                # an ogre being killed by a player unit
                [event]
                    name=die
                    [filter]
                        side=3
                        [not]
                            type=Giant Scorpion
                        [/not]
                    [/filter]
                    [if]
                        [variable]
                            name=ogres_killed
                            equals=3
                        [/variable]
                        [variable]
                            name=bandits_mobilized
                            less_than=1
                        [/variable]
                        [then]
                            {MOBILIZE_SIDE 2}
                            {VARIABLE bandits_mobilized 1}
                        [/then]
                        [else]
                            {VARIABLE_OP ogres_killed add 1}
                        [/else]
                    [/if]
                [/event]
            [/else]
        [/if]
    [/event]

    #Encounter 4 Undead Fire Mage combat
    #y coor: 1 to (35-39)

    #Encounter happens right after player moves to the location

    [event]
        name=moveto

        [filter]
            x=1-28,29-39
            y=21-34,14-21
            side=1
        [/filter]

        {VARIABLE x_coord $x1}
        {VARIABLE y_coord $y1}

        {ADD x_coord -2}
        {ADD y_coord -4}

        [unit]
            # Elyssa used to have her own unit type. This was
            # unnecessary except as a hook to hang her portrait on.
            # This makes her a stock Red Mage.
            type=Red Mage
            description=Elyssa
            user_description= _ "Elyssa"
            profile=portraits/elyssa.png
            gender=female
            upkeep=full
            experience=15
            [modifications]
                {TRAIT_QUICK}
                {TRAIT_INTELLIGENT}
            [/modifications]
            x=$x_coord
            y=$y_coord
            side=1
        [/unit]

        [store_unit]
            [filter]
                description=Elyssa
            [/filter]
            variable=player_unit
            kill=yes
        [/store_unit]
        {INIT_HYDRATION_FOR_PLAYER_UNIT}

        # boost Elyssa's movement on sand
        # - she'll be dragging her feet, anyway
        # drop her quick trait instead
        [object]
            silent="yes"
            [filter]
                description=Elyssa
            [/filter]
            [effect]
                apply_to=movement_costs
                replace="true"
                [movement_costs]
                    sand=1
                [/movement_costs]
            [/effect]
        [/object]

        {ADD x_coord 7}
        {ADD y_coord -1}

        {RANDOM_PLACEMENT_AREA $x_coord $y_coord 3}

#ifdef EASY
        {VARIABLE skeletons 2}
        {VARIABLE archers   2}
        {VARIABLE shooters  1}
        {VARIABLE revenants 0}
#endif
#ifdef NORMAL
        {VARIABLE skeletons 1}
        {VARIABLE archers   2}
        {VARIABLE shooters  1}
        {VARIABLE revenants 1}
#endif
#ifdef HARD
        {VARIABLE skeletons 1}
        {VARIABLE archers   1}
        {VARIABLE shooters  2}
        {VARIABLE revenants 1}
#endif

        {PLACE_UNITS_RANDOMLY 1          4 "Revenant"         "Go'hag"        ( _ "Go'hag")        (role="ElyssaUndead")}
        {PLACE_UNITS_RANDOMLY $skeletons 4 "Skeleton"         "Undead Raider" ( _ "Undead Raider") (role="ElyssaUndead")}
        {PLACE_UNITS_RANDOMLY $archers   4 "Skeleton Archer"  "Undead Raider" ( _ "Undead Raider") (role="ElyssaUndead")}
        {PLACE_UNITS_RANDOMLY $revenants 4 "Revenant"         "Undead Raider" ( _ "Undead Raider") (role="ElyssaUndead")}
        {PLACE_UNITS_RANDOMLY $shooters  4 "Bone Shooter"     "Undead Raider" ( _ "Undead Raider") (role="ElyssaUndead")}
        # wmllint: recognize Go'hag

        {CLEAR_VARIABLE skeletons}
        {CLEAR_VARIABLE archers}
        {CLEAR_VARIABLE shooters}
        {CLEAR_VARIABLE revenants}

        {CLEAR_PLACEMENT_AREA}

        {CLEAR_VARIABLE x_coord}
        {CLEAR_VARIABLE y_coord}

        [message]
            description=Elyssa
            message= _ "Back, you fiends! Or I'll kill you a second time!"
        [/message]

#ifdef HARD

        [message]
            description="Go'hag"
            message= _ "You have defied our master for the last time. Now you shall die! And I shall personally make it slow and painful, to thank you for that scorching you gave me. "
        [/message]

#else

        [message]
            description="Go'hag"
            message= _ "You have defied our master for the last time. Now you shall die!"
        [/message]

#endif

        [message]
            description=Elyssa
            message= _ "Stupid undead, they never listen. Then have a taste of my flame!"
        [/message]

        [message]
            speaker=Kaleh
            message= _ "She looks like she's in trouble. We should help her."
        [/message]

        [event]
            name=die
            first_time_only=no
            [filter]
                role="ElyssaUndead"
            [/filter]

            [if]
                [have_unit]
                    role="ElyssaUndead"
                [/have_unit]

                [then]
                [/then]

                [else]
                    [if]
                        [have_unit]
                            description=Elyssa
                        [/have_unit]

                        [then]
                            [message]
                                speaker=Nym
                                message= _ "Whew! Looks like that's the last of them."
                            [/message]

                            [message]
                                description=Elyssa
                                message= _ "Thanks for the help! That was a little too close for comfort."
                            [/message]

                            [message]
                                speaker=Kaleh
                                message= _ "That was a lot of undead. Why were they coming after you?"
                            [/message]

                            [message]
                                description=Elyssa
                                message= _ "I accidentally managed to anger a powerful necromancer a while ago...it's a long story. But who are you? You almost look like elves."
                            [/message]

                            [message]
                                speaker=Kaleh
                                message= _ "We are, we're the Quenoth Elves and we are traveling north. You look like you're a mage, but I thought your kind were all gone."
                            [/message]

                            [message]
                                description=Elyssa
                                message= _ "I am, I'm a fire mage. I've been traveling for quite a while now, exploring and learning. But these sands seem particularly inhospitable! I've been meaning to check out the northern mountains; mind if I join you for a while in your travels?"
                            [/message]

                            [message]
                                speaker=Zhul
                                message= _ "We'd be glad to have the help of someone with the mastery of fire."
                            [/message]
                        [/then]

                        [else]
                            {CHECK_SPEAKER}
                            [message]
                                description=$speaking_unit.description
                                message= _ "The undead are defeated, but we lost her in the fight as well. Darn, if only we could have saved her."
                            [/message]
                            {CLEAR_VARIABLE speaking_unit}
                        [/else]
                    [/if]
                [/else]
            [/if]
        [/event]
    [/event]

    #Encounter 5 Castle/Ogre Camp
    #Encounter happens right after player moves near castle

    [event]
        name=moveto

        [filter]
            side=1
            x=25-40
            y=22-33
        [/filter]

        [if]
            [have_unit]
                side=1
                x=28-40
                y=22-33
                [filter_location]
                    [not]
                        time_of_day=lawful
                    [/not]
                [/filter_location]
            [/have_unit]
            [or]
                [have_unit]
                    x=27-37
                    y=30-33
                    side=1
                [/have_unit]
            [/or]

            #!***Do wraith event***
            [then]
                [modify_side]
                    {INCOME 16 23 30}
                    {GOLD 55 70 80}
                    side=4
                [/modify_side]

                [unit]
                    type=Wraith
                    description=Vengeful Lord
                    user_description= _ "Vengeful Lord"
                    side=4
                    x=34
                    y=25
                    canrecruit=yes
                    role=wraith
                [/unit]

                {CLEAR_FOG 1 34 26 3}

                {RANDOM_TRAIT_UNIT 4 "Ghost" 33 25 "Honor Guard" ( _ "Honor Guard") (role=Wraith)}
                {RANDOM_TRAIT_UNIT 4 "Ghost" 35 25 "Honor Guard" ( _ "Honor Guard") (role=Wraith)}

#ifdef EASY
#else
                {RANDOM_TRAIT_UNIT 4 "Ghost" 33 26 "Honor Guard" ( _ "Honor Guard") (role=Wraith)}
                {RANDOM_TRAIT_UNIT 4 "Ghost" 35 26 "Honor Guard" ( _ "Honor Guard") (role=Wraith)}
#endif

                [message]
                    description=Vengeful Lord
                    message= _ "The most hated living in my castle! To me! To me stray souls of the deserts! Let's cleanse this pollution!"
                [/message]

                [message]
                    description=Vengeful Lord
                    message=_"Elf with a bow, I remember you. You thought me defeated, but I am back even more powerful than before. Death reigns eternal, your scorched bones and lifeless body will join my host."
                [/message]

                [message]
                    description=Garak
                    message= _ "Still singing the same tune. We defeated you once before, we can do so again!"
                [/message]

                {UNCLEAR_FOG}

                [event]
                    name=die

                    [filter]
                        description=Vengeful Lord
                    [/filter]

                    [message]
                        description=Vengeful Lord
                        message= _ "Nooo... (fades)"
                    [/message]

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

                    {CHECK_SPEAKER}
                    [message]
                        description=$speaking_unit.description
                        message= _ "Searching his castle, we found a chest filled with gold."
                    [/message]
                    {CLEAR_VARIABLE speaking_unit}

                    [gold]
#ifdef EASY
                        amount=250
#endif

#ifdef NORMAL
                        amount=200
#endif

#ifdef HARD
                        amount=150
#endif
                    [/gold]
                [/event]
            [/then]

            #!***Do ogre event***
            [else]
                {RANDOM_PLACEMENT_AREA 34 27 3}
#ifdef EASY
                {VARIABLE ogres 1}
                {VARIABLE young_ogres 3}
#endif
#ifdef NORMAL
                {VARIABLE ogres 2}
                {VARIABLE young_ogres 2}
#endif
#ifdef HARD
                {VARIABLE ogres 3}
                {VARIABLE young_ogres 1}
#endif
                {PLACE_UNITS_RANDOMLY $ogres        3 "Ogre"        "Ogre Nomad" ( _ "Ogre Nomad") (random_traits="no")}
                {PLACE_UNITS_RANDOMLY $young_ogres  3 "Young Ogre"  "Ogre Nomad" ( _ "Ogre Nomad") (random_traits="no")}
                # wmllint: recognize Ogre Nomad

                {CLEAR_VARIABLE ogres}
                {CLEAR_VARIABLE young_ogres}

                {CLEAR_PLACEMENT_AREA}

                {CLEAR_FOG 1 34 27 3}

                [message]
                    type=Ogre
                    message= _ "Elves! Kill them all!"
                [/message]

                {SETUP_ENEMY_THIRST 3 0}

                [message]
                    description=Garak
                    message= _ "This ruined castle must be where the ogres make their home. Well, if it's a fight they want, it's a fight they'll get. "
                [/message]

                {UNCLEAR_FOG}

                [event]
                    name=die
                    first_time_only=no

                    [filter]
                        description=Ogre Nomad
                    [/filter]

                    [if]
                        [have_unit]
                            descritpion=Ogre Nomad
                        [/have_unit]

                        [then]
                        [/then]

                        [else]
                            [message]
                                description=Kaleh
                                message= _ "That's the last of those stinking beasts. They should trouble us no more."
                            [/message]

                            [message]
                                description=Garak
                                message= _ "Y'know, this ruin looks oddly familiar."
                            [/message]

                            [message]
                                description=Nym
                                message= _ "Yuck, there's still dried blood on the stones. It's kind of spooky."
                            [/message]
                        [/else]
                    [/if]
                [/event]
            [/else]
        [/if]

        {CLEAR_VARIABLE turn_temp}
    [/event]

    #Encounter #6 Outlaw Sign
    #y coor: 1 to (20-21)
    #Sign appears as soon as player moves to the location
    #Outlaw leader is given money and units
    [event]
        name=moveto

        [filter]
            y=1-20
            side=1
        [/filter]

        [item]
            x=$x1
            y=$y1
            image=items/orcish-flag2.png
        [/item]

        [message]
            speaker=narrator
            message= _ "This land belongs to the Black Hand. Trespass and you will die."
            image=scenery/signpost.png
        [/message]

        {CHECK_EXPLORER}
        [message]
            description=$explorer.description
            message= _ "Bandits. If they get in our way, they're going to be sorry."
        [/message]

        [message]
            speaker=Kaleh
            message= _ "There's no way we can get all our people safely across the desert with outlaws harassing us. We must defeat them before we can continue."
        [/message]

        [message]
            description=$explorer.description
            message= _ "Did you see that? I think I just saw someone disappear behind that dune over there. I think we're being watched. I suspect there are more of these bandits lurking in these dunes than we originally thought."
        [/message]
        {CLEAR_VARIABLE explorer}

        [objectives]
            summary= _ "New Objectives:"
            show=yes
            [objective]
                description= _ "Kaleh Must Reach the Northern Edge of the Desert"
                condition=win
            [/objective]
            [objective]
                description= _ "Defeat Outlaw Leader"
                condition=win
            [/objective]
            [objective]
                description= _ "Death of Kaleh, Nym, Garak or Zhul"
                condition=lose
            [/objective]
        [/objectives]

        [unit]
            side=2
            description=Thorn
            user_description= _ "Thorn"
            type=Outlaw
            gender=female
            upkeep=loyal
            [modifications]
                {TRAIT_INTELLIGENT}
                {TRAIT_STRONG}
            [/modifications]
            canrecruit=yes
            x=22
            y=6
        [/unit]

        {SETUP_ENEMY_THIRST 2 0}

        [modify_side]
            side=2
            {INCOME 9 11 13}
            {GOLD 100 125 150}
        [/modify_side]
    [/event]

    # limit recruitment to one bandit and trapper
    [event]
        name=recruit
        first_time_only="yes"
        [filter]
            side=2
            type=Bandit
        [/filter]
        [disallow_recruit]
            side=2
            type=Bandit
        [/disallow_recruit]
    [/event]
    [event]
        name=recruit
        first_time_only="yes"
        [filter]
            side=2
            type=Trapper
        [/filter]
        [disallow_recruit]
            side=2
            type=Trapper
        [/disallow_recruit]
    [/event]

    #Encounter 7 Mirage
    #Oasis disappears as soon as player moves to the location
    [event]
        name=moveto

        [filter]
            side=1
            x=9-13
            y=18-19
        [/filter]

        #x coor: 9 to 13
        #y coor: 19 to 18

        [terrain]
            terrain=Dd
            x=10,10,11,11,11,12,12
            y=14,15,15,16,17,15,16
        [/terrain]

        {CHECK_EXPLORER}
        [message]
            description=$explorer.description
            message= _ "Dang. I was sure I saw an oasis here. Must have been a mirage. I've been out in the sand for too long."
        [/message]
        {CLEAR_VARIABLE explorer}
    [/event]

    # Encounter 7.5 Pinnacle Rock
    # first unit to see the rock comments on the outlaws there
    [event]
        name=moveto

        [filter]
            x=14-29
            y=1-14
            side=1
        [/filter]

        [allow_undo]
        [/allow_undo]

        {CLEAR_FOG 1 21 8 3}

        {CHECK_EXPLORER}
        [message]
            description=$explorer.description
            message= _ "So, the outlaws have made a base around Pinnacle Rock. It's a good location, but we will drive them from it all the same."
        [/message]
        {CLEAR_VARIABLE explorer}

        {UNCLEAR_FOG}
    [/event]

    #Encounter 8: Death of outlaw leader

    # Holy Water item event
    # If unit moves to hex after outlaw leader it dead, it gets the holy water

#define HOLY_WATER X Y
    [event]
        name=moveto

        [filter]
            x={X}
            y={Y}
            side=1
        [/filter]

        [object]
            id=PureWater
            name= _ "Pure Water"
            image=items/holy-water.png
            description= _ "This water will make your melee weapons arcane, and thus especially powerful against the undead."

            [effect]
                apply_to=attack
                range=melee
                set_type=arcane
            [/effect]
        [/object]

        [removeitem]
            x={X}
            y={Y}
        [/removeitem]

        {CLEAR_VARIABLE bottle_x}
        {CLEAR_VARIABLE bottle_y}
    [/event]

#enddef

    [event]
        name=die

        [filter]
            description=Thorn
        [/filter]

        [message]
            speaker=unit
            message= _ "I surrender! I mean we surrender. Just please don't kill me."
        [/message]

#ifdef HARD

        [message]
            speaker=unit
            message= _ "Please, have mercy on us. We're just trying to survive in this horrible land."
        [/message]

        [message]
            speaker=Kaleh
            message= _ "I will not kill you in cold blood. But leave these lands, and never darken our path again. Or I will show you no mercy."
        [/message]

#else

        [message]
            speaker=unit
            message= _ "Please, have mercy on us. We're just trying to survive in this horrible land."
        [/message]

        [item]
            x=$x1
            y=$y1
            image=items/holy-water.png
        [/item]

        [message]
            speaker=unit
            message= _ "Here, take this vial of holy water. You'll need it with all the undead around."
        [/message]

        [message]
            speaker=Kaleh
            message= _ "I will take this vial, in exchange for your life. But leave these lands, and never darken our path again. Or I will show you no mercy."
        [/message]

        [message]
            description=Zhul
            message= _ "It's Holy Water. When anointed on a weapon it makes it deadly to magical creatures. Vials such as these are rare and valuable. We should choose carefully who will use it."
        [/message]

        {VARIABLE bottle_x $x1}
        {VARIABLE bottle_y $y1}

        {HOLY_WATER $bottle_x $bottle_y}
#endif

        #all surviving outlaws flee, so kill all units
        [kill]
            side=2
            animate=no
            fire_event=no
        [/kill]

        #undef HOLY_WATER
    [/event]

    #if Kaleh moves to north edge of map but outlaw leader isn't defeated
    #then this warns the player that he must go back and kill outlaw leader
    #first to complete the scenario

    [event]
        name=moveto
        first_time_only=no

        [filter]
            y=1
            side=1
            description=Kaleh
        [/filter]

        [if]
            [have_unit]
                description=Thorn
            [/have_unit]

            [then]
                [message]
                    description=Kaleh
                    message= _ "The Black Hand outlaws still threaten the others. While their leader still fights we won't be able to get all our people across the sands safely. We must deal with the Black Hand before we can progress further."
                [/message]

                [allow_undo][/allow_undo]
            [/then]

            [else]
                [message]
                    description=Kaleh
                    message= _ "The outlaws are defeated and we've made it across these blasted sands. The hills are just a few miles to the north. We should be able to find water and rest there."
                [/message]

                [endlevel]
                    result=victory
                    bonus=yes
                [/endlevel]
            [/else]
        [/if]
    [/event]

    # store full_hitpoints at start, recruit and on advance
    [event]
        name=start
        [store_unit]
            variable=player_unit
            kill=no
            [filter]
                x=1-39
                y=1-74
            [/filter]
        [/store_unit]
        {FOREACH player_unit i}
        {VARIABLE player_unit[$i].variables.full_hitpoints $player_unit[$i].hitpoints}
        {VARIABLE player_unit[$i].variables.max_hitpoints $player_unit[$i].max_hitpoints}
        {VARIABLE player_unit[$i].variables.dehydration 0}
        [unstore_unit]
            variable=player_unit[$i]
            find_vacant=no
        [/unstore_unit]
        {NEXT i}
    [/event]

    [event]
        name=recruit
        first_time_only=no
        [store_unit]
            variable=player_unit
            kill=no
            [filter]
                x=$x1
                y=$y1
            [/filter]
        [/store_unit]
        {INIT_HYDRATION_FOR_PLAYER_UNIT}
    [/event]

    [event]
        name=post_advance
        first_time_only=no
        [store_unit]
            [filter]
                x=$x1
                y=$y1
            [/filter]
            variable=player_unit
            kill=yes
        [/store_unit]
        {INIT_HYDRATION_FOR_PLAYER_UNIT}
    [/event]

    # Adjust max_hitpoints just before healing
    # to make sure the dehydration damage
    # doesn't heal, then restore max_hitpoints
    # on the next select event to show the
    # dehydration damage
    [event]
        name=side turn
        first_time_only=no
        [if]
            [variable]
                name=side_number
                equals=1
            [/variable]
            [then]
                [store_unit]
                    variable=player_unit
                    kill=no
                    [filter]
                        side=1
                        x=1-39
                        y=1-74
                        [not]
                            type=Dust Devil
                        [/not]
                    [/filter]
                [/store_unit]
                {FOREACH player_unit i}
                {VARIABLE thirst $player_unit[$i].variables.dehydration}
                [if]
                    [variable]
                        name=thirst
                        greater_than=0
                    [/variable]
                    [then]
                        {VARIABLE_OP thirst multiply -$dehydration_loss}
                        {VARIABLE_OP thirst add $player_unit[$i].variables.max_hitpoints}
                        {VARIABLE player_unit[$i].max_hitpoints $thirst}
                    [/then]
                [/if]
                [unstore_unit]
                    variable=player_unit[$i]
                    find_vacant=no
                [/unstore_unit]
                {NEXT i}
            [/then]
        [/if]
        # event on next turn refresh
        # that reapplies max hitpoints - for representational purposes
        [event]
            name=turn refresh
            [store_unit]
                variable=player_unit
                kill=no
                [filter]
                    side=1
                    x=1-39
                    y=1-74
                    [not]
                        type=Dust Devil
                    [/not]
                [/filter]
            [/store_unit]
            {FOREACH player_unit i}
            {VARIABLE hp_check $player_unit[$i].variables.max_hitpoints}
            [if]
                [variable]
                    name=hp_check
                    greater_than=0
                [/variable]
                [then]
                    {VARIABLE player_unit[$i].max_hitpoints $hp_check}
                [/then]
            [/if]
            [unstore_unit]
                variable=player_unit[$i]
                find_vacant=no
            [/unstore_unit]
            {NEXT i}
        [/event]
    [/event]

    [event]
        name=victory

        # Rehydrate all units for the next scenario
        [store_unit]
            variable=affected_unit
            kill=no
            [filter]
                side=1
                x=1-39
                y=1-74
            [/filter]
        [/store_unit]
        # silent rehydration
        {APPLY_HYDRATION_EFFECT 0}
        {FOREACH affected_unit i}
        {CLEAR_VARIABLE affected_unit[$i].variables.full_hitpoints}
        {CLEAR_VARIABLE affected_unit[$i].variables.max_hitpoints}
        {CLEAR_VARIABLE affected_unit[$i].variables.dehydration}
        [unstore_unit]
            find_vacant=no
            variable=affected_unit[$i]
        [/unstore_unit]
        {NEXT i}

        [if]
            [have_unit]
                description=Elyssa
            [/have_unit]

            [then]
                [message]
                    description=Kaleh
                    message= _ "Now that we have a moment of peace, I have to ask you, Elyssa, what were you really doing out in the middle of the desert? It's a rather barren place to be traveling."
                [/message]

                [message]
                    description=Elyssa
                    message= _ "I've been searching for secrets from the past. Did you know that this entire land used to be a huge empire? Apparently this used to all be great plains and farmland, before the sands came."
                [/message]

                [message]
                    description=Nym
                    message= _ "It's hard to imagine. There's barely enough rain here now to support these tiny cacti, let alone crops."
                [/message]

                [message]
                    description=Garak
                    message= _ "And yet the great ruins that I have seen scattered across these sands are a testament that something was here before us. Perhaps in a time long ago when the land was more forgiving."
                [/message]

                [message]
                    description=Elyssa
                    message= _ "Before the great fall, there were huge cities, with great schools of magery, libraries of books, vast repositories of knowledge. Most of it was destroyed in the ensuing chaos and years of decay, but I'm searching for the little fragments that are left. For example, have you ever heard of The Scepter of Fire?"
                [/message]

                [message]
                    description=Kaleh
                    message= _ "No, I haven't. What is it?"
                [/message]

                [message]
                    description=Elyssa
                    message= _ "I'm not sure. I've read various references to it, but nothing specific. I've been searching for it for a long time. All I know is that it was a very powerful magical wand and that it was some sort of symbol of royalty in the old empire, but I have no idea where it might be. So I scour the land, learning all I can about the olden days. I'm sure it must be somewhere."
                [/message]

                [message]
                    description=Kaleh
                    message= _ "Perhaps. But it has been so long, and so much has been lost. Who can say?"
                [/message]
            [/then]
        [/if]
        # clearing variables
        {CLEAR_VARIABLE affected_unit}
        {CLEAR_VARIABLE bandits_mobilized}
        {CLEAR_VARIABLE black_hand_dialogue}
        {CLEAR_VARIABLE black_patrol_dialogue}
        {CLEAR_VARIABLE dehydration_loss}
        {CLEAR_VARIABLE ghosts}
        {CLEAR_VARIABLE ghosts_already_spawned}
        {CLEAR_VARIABLE ghosts_killed}
        {CLEAR_VARIABLE ghosts_talk}
        {CLEAR_VARIABLE max_ghosts}
        {CLEAR_VARIABLE max_per_turn}
        {CLEAR_VARIABLE ogre_thirst}
        {CLEAR_VARIABLE enemy}
        {CLEAR_VARIABLE new_fog_clearer}
    [/event]

    #time over defeat event

    [event]
        name=time over

        [message]
            description=Kaleh
            message= _ "We've run out of provisions and our people are exhausted. We've taken too long crossing the desert, I fear we shall never reach the other side. "
        [/message]
    [/event]

    # These events happen continually throughout the scenario

    # LOST SOULS:
    # changed spawning of lost soul to happen in the vicinity of player units
    # spawn a number of ghosts each turn depending on how "exposed"
    # the player units are. Only units in the middle of the desert attract
    # ghosts.
    [event]
        name=new turn
        first_time_only=no
        {VARIABLE turn_temp $turn_number}
        {VARIABLE_OP turn_temp modulo 15}
        # LOST SOUL DISAPPEARANCE at first daylight
        [if]
            [variable]
                name=turn_temp
                numerical_equals=1
            [/variable]
            [or]
                [variable]
                    name=turn_temp
                    numerical_equals=7
                [/variable]
            [/or]
            [then]
                # reset the ghosts' counters
                {VARIABLE ghosts_already_spawned 0}
                {VARIABLE ghosts_killed 0}
                [kill]
                    role=LostSoul
                    animate=no
                    fire_event=no
                [/kill]
            [/then]
        [/if]
        [if]
            [variable]
                name=ghosts_already_spawned
                less_than=$max_ghosts
            [/variable]
            [variable]
                name=turn_temp
                not_equals=1
            [/variable]
            [variable]
                name=turn_temp
                not_equals=7
            [/variable]
            [then]
                # store "exposed" units - those surrounded by a lot of desert
                [store_locations]
                    variable=ghost_spawn
                    [filter]
                        side=1
                    [/filter]
                    [not]
                        time_of_day=lawful
                    [/not]
                    terrain=Hd, Dd, Rr
                    [filter_adjacent_location]
                        count="5,6"
                        direction="n,ne,se,s,sw,nw"
                        terrain=Hd, Dd, Rr
                    [/filter_adjacent_location]
                [/store_locations]

                {VARIABLE ghosts $ghost_spawn.length}

                [if]
                    [variable]
                        name=ghosts
                        greater_than=0
                    [/variable]
                    [then]
                        # spawn more rapidly as time goes by
                        {VARIABLE ghosts_per_turn $turn_number}
                        {VARIABLE_OP ghosts_per_turn divide 20}
                        {VARIABLE_OP ghosts_per_turn add $max_per_turn}
                        #! -- Amount of ghost generation borders on insane bearing in mind that they now do arcane damage
                        #! -- and payer won't have any damage type efective against them.
                        [store_locations]
                            variable=head_count
                            [filter]
                                side=1
                            [/filter]
                        [/store_locations]
                        {VARIABLE ear_count $head_count.length}
                        {CLEAR_VARIABLE head_count}
#ifdef EASY
                        # 1 ghost per 4 exposed units (roughly) per turn
                        {VARIABLE_OP ghosts divide 4}
                        {VARIABLE_OP ear_count divide 4}
#endif
#ifdef NORMAL
                        # 1 ghost per 3 exposed units per turn
                        {VARIABLE_OP ghosts divide 3}
                        {VARIABLE_OP ear_count divide 3}
#endif
#ifdef HARD
                        # 1 ghost per 2 exposed units per turn
                        {VARIABLE_OP ghosts divide 2}
                        {VARIABLE_OP ear_count divide 2}
#endif
                        [if]
                            [variable]
                                name=max_ghosts
                                greater_than=$ear_count
                            [/variable]
                            [then]
                                {VARIABLE max_ghosts $ear_count}
                            [/then]
                        [/if]
                        {CLEAR_VARIABLE ear_count}
                        {CLEAR_VARIABLE added_ghosts}
                        # limit ghost appearance rate
                        [if]
                            [variable]
                                name=ghosts
                                greater_than=$ghosts_per_turn
                            [/variable]
                            [then]
                                {VARIABLE ghosts $ghosts_per_turn}
                            [/then]
                        [/if]
                        {CLEAR_VARIABLE ghosts_per_turn}
                        # deduct already spawned ghosts - whether still alive or not
                        {VARIABLE ghost_limit $max_ghosts}
                        {VARIABLE_OP ghost_limit add -$ghosts_already_spawned}
                        [if]
                            [variable]
                                name=ghost_limit
                                less_than=0
                            [/variable]
                            [then]
                                {VARIABLE ghost_limit 0}
                            [/then]
                        [/if]
                        [if]
                            [variable]
                                name=ghosts
                                greater_than=$ghost_limit
                            [/variable]
                            [then]
                                {VARIABLE ghosts $ghost_limit}
                            [/then]
                        [/if]
                        {CLEAR_VARIABLE ghost_limit}

                        # store all possible spawn locations in range
                        # pick one random ghost_spawn from which
                        # to store all locations in range
                        {RANDOM 1..$ghost_spawn.length}
                        {VARIABLE ghost_x $ghost_spawn[$random].x}
                        {VARIABLE ghost_y $ghost_spawn[$random].y}
                        {CLEAR_VARIABLE ghost_spawn}
                        [store_locations]
                            variable=spawn
                            x=$ghost_x
                            y=$ghost_y
                            radius=8
                            terrain=Hd, Dd, Rr
                        [/store_locations]

                        {CLEAR_VARIABLE ghost_x}
                        {CLEAR_VARIABLE ghost_y}

                        {VARIABLE i 0}
                        [while]
                            [variable]
                                name=i
                                less_than=$ghosts
                            [/variable]
                            [do]
                                {RANDOM 1..$spawn.length}
                                # don't spawn right adjacent to a player unit
                                [if]
                                    [have_location]
                                        x=$spawn[$random].x
                                        y=$spawn[$random].y
                                        [filter_adjacent_location]
                                            adjacent="n,ne,se,s,sw,nw"
                                            count="1-6"
                                            [filter]
                                                side=1
                                            [/filter]
                                        [/filter_adjacent_location]
                                    [/have_location]
                                    [else]
                                        {RANDOM_TRAIT_UNIT 4 "Ghost" $spawn[$random].x $spawn[$random].y "Lost Soul" ( _ "Lost Soul") (role="LostSoul")}
                                        {VARIABLE_OP ghosts_already_spawned add 1}
                                    [/else]
                                [/if]
                                {ADD i 1}
                            [/do]
                        [/while]
                        {CLEAR_VARIABLE spawn}
                        # some ghosts dialogue
                        [if]
                            [variable]
                                name=ghosts_talk
                                less_than=1
                            [/variable]
                            [then]
                                {VARIABLE ghosts_talk 1}

                                [message]
                                    description=Garak
                                    message= _ "The sands are haunted with the spirits of tortured souls long since dead. At dusk they rise again, and during the Long Dark they can be particularly nasty. They disappear again at dawn, but personally I prefer the heat of the day to the horrors of the night."
                                [/message]

                                [message]
                                    description=Zhul
                                    message= _ "The combination of the heat of the day and the rising undead make dusk a particularly dangerous time of day."
                                [/message]
                            [/then]
                            [else]
                                [if]
                                    [variable]
                                        name=ghosts_talk
                                        equals=2
                                    [/variable]
                                    [then]
                                        {VARIABLE ghosts_talk 3}

                                        [message]
                                            description=Kaleh
                                            message= _ "You come and rise again, spirits? Have we not defeated you before?"
                                        [/message]

                                        [message]
                                            description=Zhul
                                            message= _ "These lost souls cannot hold onto anything but memories of lives long gone. They will rise to plague us again, night after night."
                                        [/message]
                                    [/then]
                                [/if]
                            [/else]
                        [/if]
                    [/then]
                [/if]
            [/then]
        [/if]
        {CLEAR_VARIABLE turn_temp}
    [/event]

    # If player kills more than 33% of the maximum number of lost souls, the rest flee

    [event]
        name=die
        first_time_only=no

        [filter]
            role=LostSoul
        [/filter]

        {VARIABLE to_kill $max_ghosts}
        {VARIABLE_OP to_kill divide 3}

        [if]
            [variable]
                name=ghosts_killed
                numerical_not_equals=$to_kill
            [/variable]

            [then]
                {ADD ghosts_killed 1}
            [/then]

            [else]
                [kill]
                    role=LostSoul
                    animate=no
                    fire_event=no
                [/kill]

                # don't respawn ghosts this night
                {VARIABLE ghosts_already_spawned $max_ghosts}
                {VARIABLE ghosts_killed 0}

                [if]
                    [variable]
                        name=ghosts_talk
                        less_than=2
                    [/variable]

                    [then]
                        {VARIABLE ghosts_talk 2}

                        [message]
                            description=Kaleh
                            message= _ "What happened? The undead spirits all just disappeared."
                        [/message]

                        [message]
                            description=Garak
                            message= _ "These spirits are weak. They seek to prey on the helpless, but when they encounter strong resistance they flee."
                        [/message]
                    [/then]

                    [else]
                        [message]
                            description=Kaleh
                            message= _ "Flee, you craven spirits, and leave us in peace!"
                        [/message]
                    [/else]
                [/if]
            [/else]
        [/if]
    [/event]

    [event]
        name=new turn
        first_time_only=no
        # dehydration:
        # only at dawn, morning, mid-day, afternoon and dusk
        [store_unit]
            [filter]
                [not]
                    race=undead
                [/not]
                [not]
                    type=Giant Scorpion
                [/not]
                [not]
                    type=Dust Devil
                [/not]
                [not]
                    # traveller's ring
                    role=immune
                [/not]
                [not]
                    # prevent AI units from being depleted
                    # by hydration before the player shows up
                    ai_special=guardian
                [/not]
                [filter_location]
                    terrain=Hd, Dd, Rr, Dd^Dr
                    [not]
                        time_of_day=chaotic
                    [/not]
                [/filter_location]
            [/filter]
            kill=no
            variable=affected_unit
        [/store_unit]
        {APPLY_HYDRATION_EFFECT 1}
    [/event]

    [event]
        name=new turn
        first_time_only=no
        [store_unit]
            [filter]
                [not]
                    race=undead
                [/not]
                [not]
                    type=Giant Scorpion
                [/not]
                [not]
                    type=Dust Devil
                [/not]
                [filter_location]
                    terrain=Ww
                [/filter_location]
            [/filter]
            kill=no
            variable=affected_unit
        [/store_unit]
        {APPLY_HYDRATION_EFFECT -1}
    [/event]

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

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