#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

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

    {TWO_SUNS_DEFAULT_SCHEDULE}

    {INTRO_AND_SCENARIO_MUSIC "knolls.ogg" "wanderer.ogg"}
    {EXTRA_SCENARIO_MUSIC "battle.ogg"}
    {EXTRA_SCENARIO_MUSIC "traveling_minstrels.ogg"}

    {STORY_ACROSS_THE_HARSH_SANDS}

    #elves
    [side]
        side=1
        id=Kaleh
        type=Desert Fighter
        canrecruit=yes
        gold=200
        {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
        user_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,scout,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
        user_team_name=_"Monsters"
        [ai]
            aggression=1.0
            caution=0.00
#ifdef EASY
            # monsters are extra dumb
            simple_targeting=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
        user_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

        {VARIABLE bandits_mobilized 0}

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

            note={EARLY_FINISH_BONUS_NOTE}+{NEW_GOLD_CARRYOVER_NOTE_40}
        [/objectives]

        # Recall Nym, Zhul and Garak

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

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

        [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]
            speaker=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]
            speaker=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]
            speaker=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]
            speaker=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]
            speaker=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]
            speaker=Nym
            message= _ "Unfortunately, because of our hasty flight from our village, we are short on water-skins 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 damage. Only by being cared for by a healer or refreshing at an oasis (any shallow water hex) at the start of your turn will your units regain full attack strength."
            image=wesnoth-icon.png
        [/message]

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

#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
            name=$enemy[$i].name
            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
            overlays=misc/dehydration-icon.png
            [status]
                slowed=$enemy[$i].status.slowed
                poisoned=$enemy[$i].status.poisoned
            [/status]
            [variables]
                dehydrated=$enemy[$i].variables.dehydrated
            [/variables]
        [/unit]
    {NEXT i}
#enddef

    # This dehydrates the given units. However, if the unit is adjacent to a
    # friendly healer, the dehydration will be delayed instead: the unit suffers no
    # ill effects, but it cannot receive any healing on this turn either. Basically,
    # dehydration works just like poison.
    #
    # Each turn of dehydration lowers the unit's attack damage by 1 point and
    # decreases hitpoints by -$dehydration_loss.
#define APPLY_DEHYDRATION_EFFECT FILTER
    [store_unit]
        [filter]
            {FILTER}
        [/filter]

        kill=no
        variable=dehydrating_units
    [/store_unit]

    {FOREACH dehydrating_units i}
        {VARIABLE dehydrating_units[$i].resting no}

        [if]
            [have_unit]
                x,y=$dehydrating_units[$i].x,$dehydrating_units[$i].y

                [filter_adjacent]
                    is_enemy=no
                    [and]
                        ability=healing
                        [or]
                            ability=curing
                        [/or]
                    [/and]
                [/filter_adjacent]
            [/have_unit]

            [then]
                {VARIABLE dehydrating_units[$i].status.dehydration_slowed_by_healer yes}

                [unstore_unit]
                    variable=dehydrating_units[$i]
                    find_vacant=no
                [/unstore_unit]
            [/then]

            [else]
                {FOREACH dehydrating_units[$i].attack j}
                    # if dehydrating for the first time, store the original attack stats
                    [if]
                        [variable]
                            name=dehydrating_units[$i].status.dehydrated
                            not_equals=yes
                        [/variable]

                        [then]
                            {VARIABLE dehydrating_units[$i].variables.original_attack[$j].damage $dehydrating_units[$i].attack[$j].damage}
                        [/then]
                    [/if]

                    # the attack's damage drops by 1 point, but cannot drop to zero
                    [if]
                        [variable]
                            name=dehydrating_units[$i].attack[$j].damage
                            greater_than=1
                        [/variable]

                        [then]
                            {VARIABLE_OP dehydrating_units[$i].attack[$j].damage add -1}
                        [/then]
                    [/if]
                {NEXT j}

                # unit's hitpoints drop by $dehydration_loss, but cannot kill it
                [if]
                    [variable]
                        name=dehydrating_units[$i].hitpoints
                        greater_than=$dehydration_loss
                    [/variable]

                    [then]
                        {VARIABLE_OP dehydrating_units[$i].hitpoints add -$dehydration_loss}
                    [/then]

                    [else]
                        {VARIABLE dehydrating_units[$i].hitpoints 1}
                    [/else]
                [/if]

                {VARIABLE dehydrating_units[$i].status.dehydrated yes}

                [unstore_unit]
                    variable=dehydrating_units[$i]
                    find_vacant=no
                    text= _ "thirst"
                    {COLOR_HARM}
                [/unstore_unit]

                [unit_overlay]
                    x,y=$dehydrating_units[$i].x,$dehydrating_units[$i].y
                    image=misc/dehydration-icon.png
                [/unit_overlay]
            [/else]
        [/if]
    {NEXT i}

    {CLEAR_VARIABLE dehydrating_units}
#enddef

    # This rehydrates the given units. The units gain back their full attack damage.
#define APPLY_HYDRATION_EFFECT FILTER
    [store_unit]
        [filter]
            {FILTER}
        [/filter]

        kill=no
        variable=hydrating_units
    [/store_unit]

    {FOREACH hydrating_units i}
        [if]
            [variable]
                name=hydrating_units[$i].status.dehydrated
                equals=yes
            [/variable]

            [then]
                {FOREACH hydrating_units[$i].attack j}
                    {VARIABLE hydrating_units[$i].attack[$j].damage $hydrating_units[$i].variables.original_attack[$j].damage}
                {NEXT j}
            [/then]
        [/if]

        {VARIABLE hydrating_units[$i].status.dehydrated no}

        [unstore_unit]
            variable=hydrating_units[$i]
            find_vacant=no
            text= _ "refreshed"
            {COLOR_HEAL}
        [/unstore_unit]

        [remove_unit_overlay]
            x,y=$hydrating_units[$i].x,$hydrating_units[$i].y
            image=misc/dehydration-icon.png
        [/remove_unit_overlay]
    {NEXT i}

    {CLEAR_VARIABLE hydrating_units}
#enddef

    [event]
        name=side turn
        first_time_only=no

        # dehydration:
        # only at dawn, morning, mid-day, afternoon and dusk
        {APPLY_DEHYDRATION_EFFECT (
            side=$side_number
            [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]
        )}
    [/event]

    [event]
        name=side turn
        first_time_only=no

        # rehydration in water, villages or when next to healers
        {APPLY_HYDRATION_EFFECT (
            side=$side_number
            [filter_wml]
                [status]
                    dehydrated=yes
                [/status]
            [/filter_wml]
            [not]
                race=undead
            [/not]
            [not]
                type=Giant Scorpion
            [/not]
            [not]
                type=Dust Devil
            [/not]
            [and]
                [filter_location]
                    terrain=W*,*^V*
                [/filter_location]

                # adjacent healers will remove dehydration
                [or]
                    [filter_adjacent]
                        is_enemy=no
                        [and]
                            ability=healing
                            [or]
                                ability=curing
                            [/or]
                        [/and]
                    [/filter_adjacent]
                [/or]
            [/and]
        )}
    [/event]

    [event]
        name=turn refresh
        first_time_only=no

        [if]
            [variable]
                name=side_number
                equals=1
            [/variable]

            [then]
                {MODIFY_UNIT (
                    side=1
                    [filter_wml]
                        [status]
                            dehydration_slowed_by_healer=yes
                        [/status]
                    [/filter_wml]
                ) status.dehydration_slowed_by_healer no}
            [/then]
        [/if]
    [/event]

    [event]
        name=advance
        first_time_only=no

        [filter]
            [filter_wml]
                [status]
                    dehydrated=yes
                [/status]
            [/filter_wml]
        [/filter]

        {VARIABLE unit.status.dehydrated no}

        [unstore_unit]
            variable=unit
            find_vacant=no
            text= _ "refreshed"
            {COLOR_HEAL}
            advance=no
        [/unstore_unit]

        [remove_unit_overlay]
            x,y=$unit.x,$unit.y
            image=misc/dehydration-icon.png
        [/remove_unit_overlay]
    [/event]

    # 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

        #!***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]
                    speaker=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]
                    speaker=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]
                id=Scorpion
            [/filter]

            {VARIABLE tempx $x1}
            {VARIABLE tempy $y1}
            [if]
                [have_unit]
                    id=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]

                    [event]
                        name=moveto
                        delayed_variable_substitution=no

                        [filter]
                            x=$x1
                            y=$y1
                            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=$x1
                                y=$y1
                                side=1
                            [/filter]
                        [/object]

                        {APPLY_HYDRATION_EFFECT (
                            x,y=$x1,$y1
                        )}

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

                        [role]
                            role=immune
                            x=$x1
                            y=$y1
                        [/role]
                    [/event]
                [/else]
            [/if]
        [/event]

#undef SCORPION_PLACEMENT
    [/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 3}

    {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

    # also need to apply thirst right away
    {APPLY_DEHYDRATION_EFFECT (
        side=3
        x=13-22
        y=44-54
    )}

    {VARIABLE ogres_killed 0}

#enddef

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

            [then]
                {OGRE_PLACEMENT}

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

                # 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]
                        speaker=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]
                        speaker=Nym
                        message=_"Hey! Look there, they dropped something... A stone bottle, sealed. I wonder what's inside..."
                    [/message]

                    {MOVE_UNIT id=Nym $x1 $y1}

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

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

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

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

                    [redraw]
                    [/redraw]

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

                    [store_locations]
                        [not]
                            [filter]
                            [/filter]
                        [/not]

                        [and]
                            x,y=$x1,$y1
                            radius=1
                        [/and]

                        variable=locs_around_nym
                    [/store_locations]

                    [store_unit]
                        [filter]
                            type=Dust Devil
                        [/filter]

                        kill=yes
                        variable=stored_dust_devil
                    [/store_unit]

                    {VARIABLE dust_devil_path.x    $stored_dust_devil.x}
                    {VARIABLE dust_devil_path.y    $stored_dust_devil.y}

                    {VARIABLE dust_devil_retpath.x $stored_dust_devil.x}
                    {VARIABLE dust_devil_retpath.y $stored_dust_devil.y}

                    {FOREACH locs_around_nym i}
                        {VARIABLE dust_devil_path.x    "$dust_devil_path.x|,$locs_around_nym[$i].x"}
                        {VARIABLE dust_devil_path.y    "$dust_devil_path.y|,$locs_around_nym[$i].y"}

                        {VARIABLE dust_devil_retpath.x "$locs_around_nym[$i].x|,$dust_devil_retpath.x|"}
                        {VARIABLE dust_devil_retpath.y "$locs_around_nym[$i].y|,$dust_devil_retpath.y|"}
                    {NEXT i}

                    # TODO: instead of returning back to its original position all of sudden,
                    # go through dust_devil_path in inverse direction.
                    [move_unit_fake]
                        type=Dust Devil
                        side=1
                        x=$dust_devil_path.x|,$dust_devil_retpath.x
                        y=$dust_devil_path.y|,$dust_devil_retpath.y
                    [/move_unit_fake]

                    [unstore_unit]
                        variable=stored_dust_devil
                    [/unstore_unit]

                    {CLEAR_VARIABLE locs_around_nym,stored_dust_devil,dust_devil_path,dust_devil_retpath}

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

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

    # These are related to the ogre ambush. A player unit being killed by an ogre,
    # or an ogre being killed by a player unit, changes the behavior of the bandits
    # whenever they are encountered.
    [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]
    [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]

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

        [filter]
            side=1
            x=4,20
            y=40-54
        [/filter]

        {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}

        [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]
            [/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
            id=Elyssa
            name= _ "Elyssa"
            unrenamable=yes
            profile=portraits/elyssa.png
            gender=female
            experience=15
            [modifications]
                {TRAIT_LOYAL}
                {TRAIT_QUICK}
                [object]
                    [effect]
                        apply_to=profile
                        unit_type=Silver Mage
                        portrait=portraits/elyssa_silver.png
                    [/effect]
                [/object]
                # boost Elyssa's movement on sand
                # - she'll be dragging her feet, anyway
                # drop her quick trait instead
                [object]
                    [effect]
                        apply_to=movement_costs
                        replace="true"
                        [movement_costs]
                            sand=1
                        [/movement_costs]
                    [/effect]
                [/object]
            [/modifications]
            x=$x_coord
            y=$y_coord
            side=1
        [/unit]

        {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]
            speaker=Elyssa
            message= _ "Back, you fiends! Or I'll kill you a second time!"
        [/message]

#ifdef HARD

        [message]
            speaker="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]
            speaker="Go'hag"
            message= _ "You have defied our master for the last time. Now you shall die!"
        [/message]

#endif

        [message]
            speaker=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]
                            id=Elyssa
                        [/have_unit]

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

                            [message]
                                speaker=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]
                                speaker=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]
                                speaker=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]
                                speaker=$speaking_unit.id
                                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
                    id=Vengeful Lord
                    name= _ "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]
                    speaker=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]
                    speaker=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]
                    speaker=Garak
                    message= _ "Still singing the same tune. We defeated you once before, we can do so again!"
                [/message]

                {UNCLEAR_FOG}

                [event]
                    name=die

                    [filter]
                        id=Vengeful Lord
                    [/filter]

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

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

                    {CHECK_SPEAKER}
                    [message]
                        speaker=$speaking_unit.id
                        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]

                [message]
                    speaker=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]
                        id=Ogre Nomad
                    [/filter]

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

                        [then]
                        [/then]

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

                            [message]
                                speaker=Garak
                                message= _ "This ruin looks oddly familiar."
                            [/message]

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

            note={EARLY_FINISH_BONUS_NOTE}+{NEW_GOLD_CARRYOVER_NOTE_40}
        [/objectives]

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

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

        [if]
            [have_unit]
                id=Thorn
            [/have_unit]

            [then]
                [message]
                    speaker=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]
                    speaker=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
                    {NEW_GOLD_CARRYOVER 40}
                [/endlevel]
            [/else]
        [/if]
    [/event]

    [event]
        name=victory

        # Rehydrate all units for the next scenario
        {APPLY_HYDRATION_EFFECT (
            side=1
            {EVERYWHERE}
        )}

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

            [then]
                [message]
                    speaker=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]
                    speaker=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]
                    speaker=Nym
                    message= _ "It's hard to imagine. There's barely enough rain here now to support these tiny cacti, let alone crops."
                [/message]

                [message]
                    speaker=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]
                    speaker=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]
                    speaker=Kaleh
                    message= _ "No, I haven't. What is it?"
                [/message]

                [message]
                    speaker=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]
                    speaker=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 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]
            speaker=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]
                                    speaker=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]
                                    speaker=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]
                                            speaker=Kaleh
                                            message= _ "You come and rise again, spirits? Have we not defeated you before?"
                                        [/message]

                                        [message]
                                            speaker=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]
                            speaker=Kaleh
                            message= _ "What happened? The undead spirits all just disappeared."
                        [/message]

                        [message]
                            speaker=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]
                            speaker=Kaleh
                            message= _ "Flee, you craven spirits, and leave us in peace!"
                        [/message]
                    [/else]
                [/if]
            [/else]
        [/if]
    [/event]

    {@campaigns/Under_the_Burning_Suns/utils/kaleh-abilities.cfg}
    {@campaigns/Under_the_Burning_Suns/utils/deaths.cfg}
[/scenario]
