#textdomain wesnoth-utbs

[scenario]
    id=02_Across_the_Harsh_Sands
    name= _ "Across the Harsh Sands"
    {UTBS_MAP 02_Across_the_Harsh_Sands.map}
    next_scenario=03_Stirring_in_the_Night
    {TURNS 62 58 54}
    victory_when_enemies_defeated=no

    {TWO_SUNS_DEFAULT_SCHEDULE}

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

    {STORY_ACROSS_THE_HARSH_SANDS}

    # Load dehydration stuff
    {DEHYDRATION_EVENTS}

    #elves
    [side]
        side=1
        id=Kaleh
        type=Quenoth Youth
        canrecruit=yes
        {GOLD 250 200 150}
        {INCOME 19 15 11}
        controller=human
        recruit=Quenoth Fighter,Tauroch Rider,Quenoth Scout,Quenoth Mystic
        shroud=yes
        fog=yes
        {FLAG_VARIANT long}
        user_team_name= _ "team_name^Quenoth Elves"
    [/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]
            {AI_SIMPLE_ALWAYS_ASPECT aggression                      0.8}
            {AI_SIMPLE_ALWAYS_ASPECT caution                         0.2}
            {AI_SIMPLE_ALWAYS_ASPECT recruitment_pattern {ON_DIFFICULTY (scout,fighter,archer,fighter) (fighter,mixed fighter,archer,scout,fighter) (fighter,mixed fighter,archer,scout,fighter)}}

            [goal]
                value=3
                [criteria]
                    side=1
                [/criteria]
            [/goal]

            [goal]
                name=protect_location
                value=10
                [criteria]
                    x,y=21,9
                [/criteria]
            [/goal]
        [/ai]
        # limit recruitment to one bandit and trapper
        [ai]
            [aspect]
                id=recruitment_instructions
                [facet]
                    [value]
                        [limit]
                            type=Bandit
                            max=1
                        [/limit]
                        [limit]
                            type=Trapper
                            max=1
                        [/limit]
                    [/value]
                [/facet]
            [/aspect]
        [/ai]
        {FLAG_VARIANT6 ragged}
    [/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]
            {AI_SIMPLE_ALWAYS_ASPECT aggression  1.0}
            {AI_SIMPLE_ALWAYS_ASPECT caution    0.00}
#ifdef EASY
            # monsters are extra dumb
            {AI_SIMPLE_ALWAYS_ASPECT simple_targeting yes} # TODO: do research about simple_targeting and the new AI
            {AI_SIMPLE_ALWAYS_ASPECT grouping          no}
#endif
#ifdef NORMAL
            {AI_SIMPLE_ALWAYS_ASPECT grouping          no}
#endif

            [goal]
                value=40
                [criteria]
                    side=1
                [/criteria]
            [/goal]

            [goal]
                value=2
                [criteria]
                    side=2
                [/criteria]
            [/goal]
        [/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]
            {AI_SIMPLE_ALWAYS_ASPECT aggression 1.0}
            {AI_SIMPLE_ALWAYS_ASPECT caution    0.0}
            {AI_SIMPLE_ALWAYS_ASPECT recruitment_pattern scout,fighter,fighter,archer}
            {AI_SIMPLE_ALWAYS_ASPECT grouping no}

            [goal]
                value=3
                [criteria]
                    side=1
                [/criteria]
            [/goal]

            [goal]
                value=1
                [criteria]
                    side=2
                [/criteria]
            [/goal]

            [goal]
                value=1
                [criteria]
                    side=3
                [/criteria]
            [/goal]

            [goal]
                name=protect_location
                [criteria]
                    x,y=34,27
                [/criteria]
            [/goal]
        [/ai]
        {FLAG_VARIANT undead}
    [/side]

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

    [event]
        name=prestart

        # Set starting scenario objectives

        [objectives]
            [objective]
                description= _ "Kaleh must reach the northern edge of the desert"
                condition=win
            [/objective]
            [objective]
                description= _ "Defeat outlaw leader"
                condition=win
                [show_if]
                    [have_unit]
                        # wmllint: recognize Thorn
                        id=Thorn
                    [/have_unit]
                [/show_if]
            [/objective]
            [objective]
                description= _ "Death of Kaleh"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Nym"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Garak"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Zhul"
                condition=lose
            [/objective]

            {TURNS_RUN_OUT}

            [note]
                description= _ "Your units become dehydrated during the day, losing hitpoints and damage when they are not on a water hex"
            [/note]
            [note]
                description= _ "Units are refreshed at the start of your turn when they are on a water or oasis hex"
            [/note]
            [note]
                description= _ "Healers can prevent dehydration from getting worse, but cannot heal it"
            [/note]
            [note]
                description= _ "Healers are unable to heal adjacent units during the day"
            [/note]

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

        # Recall Nym, Zhul and Garak
        [recall]
            id=Nym
        [/recall]

        [recall]
            id=Garak
        [/recall]

        [recall]
            id=Zhul
        [/recall]

        [label]
            x,y=22,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-23,20-22,21
            y= 9-10,    8,11
        [/remove_shroud]

        [remove_shroud]
            side=1
            x=0-8
            y=66-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,y=22,9
        [/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-23,20-22,21
            y= 9-10,    8,11
        [/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]

        # Rewritten message -> explains new dehydration rules
        [message]
            speaker=narrator
            message= _ "During the daytime (Dawn, Morning, Midday, 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. Healers can prevent dehydration from becoming worse, but cannot heal it. Only by refreshing at an oasis (any 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]

        # setup ghost difficulty
        {VARIABLE max_ghosts {ON_DIFFICULTY 6 7 8} }
        {VARIABLE ghosts_to_kill "$(ceil($($max_ghosts / 3.0)))"}
    [/event]

    # Special Encounter events

    # Encounter 1: Scorpions attack

#define SCORPION_PLACEMENT
    {SCATTER_UNITS {ON_DIFFICULTY 6 7 8} "Giant Scorpion" 0 x,y,radius=29,62,2 (
        side=3
        name= _ "Scuttler"
        variation=scuttler
        ai_special=guardian
        animate=yes
    )}
#enddef

    [event]
        name=moveto
        id=scorpions_spotted

        [filter]
            side=1

            [filter_location]
                x,y=29,62
                radius=8
            [/filter_location]

            [filter_wml]
                usage=scout
            [/filter_wml]
        [/filter]

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

        [remove_event]
            id=scorpion_ambush
        [/remove_event]
    [/event]

    [event]
        name=moveto
        id=scorpion_ambush

        [filter]
            side=1

            [filter_location]
                x,y=29,62
                radius=3
            [/filter_location]

            [not]
                [filter_wml]
                    usage=scout
                [/filter_wml]
            [/not]
        [/filter]

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

        {SCORPION_PLACEMENT}

        [remove_event]
            id=scorpions_spotted
        [/remove_event]
    [/event]

    [event]
        name=die

        [filter]
            type=Giant Scorpion
        [/filter]

        [filter_condition]
            [not]
                [have_unit]
                    type=Giant Scorpion
                [/have_unit]
            [/not]
        [/filter_condition]

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

        [store_locations]
            x,y=$x1,$y1
            variable=ring_loc
        [/store_locations]

        [event]
            name=moveto
            id=take_traveler_ring
            delayed_variable_substitution=yes
            first_time_only=no

            [filter]
                side=1
                [not]
                    type=Dust Devil
                [/not]
                [filter_location]
                    find_in=ring_loc
                [/filter_location]
            [/filter]

            [message]
                # wmllint: deathcheck off
                speaker=unit
                # wmllint: deathcheck on

                message= _ "Should I take this ring?"
                [option]
                    label= _ "Yes, I’ll take it."

                    [command]
                        [object]
                            id=Travelring
                            name= _ "Traveler’s Ring"
                            image=items/ring-white.png
                            description= _ "The unit wearing this ring will not become dehydrated by the desert."
                            [filter]
                                x=$x1
                                y=$y1
                                side=1
                            [/filter]
                            [effect]
                                apply_to=overlay
                                add="misc/silver-ring-icon.png"
                            [/effect]
                        [/object]

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

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

                        [role]
                            role=immune
                            x=$x1
                            y=$y1
                        [/role]

                        [remove_event]
                            id=take_traveler_ring
                        [/remove_event]

                        {CLEAR_VARIABLE ring_loc}
                    [/command]
                [/option]

                [option]
                    label= _ "No, I think someone else should wear it."

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

#undef SCORPION_PLACEMENT

    #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

#define OGRE_PLACEMENT
    {SCATTER_UNITS 5 "Ogre,Ogre,Young Ogre,Young Ogre,Young Ogre" 0 x,y,radius=17,49,3 (
        side=3
        name= _ "Hunting Ogre"
        role=Hunting Ogre
        random_traits=no
        ai_special=guardian
        animate=yes
    )}

    {VARIABLE ogres_killed 0}
#enddef

    [event]
        name=moveto
        id=ogres_spotted

        [filter]
            side=1

            [filter_location]
                x,y=17,49
                radius=8
            [/filter_location]

            [filter_wml]
                usage=scout
            [/filter_wml]
        [/filter]

        [remove_event]
            id=ogre_ambush
        [/remove_event]

        {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]
                side=1

                [filter_location]
                    [filter]
                        role=Hunting Ogre
                    [/filter]

                    [and]
                        x,y=17,49
                        radius=3
                    [/and]
                [/filter_location]
            [/filter]

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

    [event]
        name=moveto
        id=ogre_ambush

        [filter]
            side=1

            [filter_location]
                x,y=17,49
                radius=3
            [/filter_location]

            [not]
                [filter_wml]
                    usage=scout
                [/filter_wml]
            [/not]
        [/filter]

        [remove_event]
            id=ogres_spotted
        [/remove_event]

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

    [event]
        name=die

        [filter]
            role="Hunting Ogre"
        [/filter]

        [filter_condition]
            [have_unit]
                role="Hunting Ogre"
                count=0
            [/have_unit]
        [/filter_condition]

        [kill]
            role="Hunting Ogre"
        [/kill]

        [redraw]
        [/redraw]

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

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

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

        # Move Nym to the bottle, but don't move her if she's already adjacent to it
        # if she landed the killing blow, moving her now means that she doesn't get the XP.

        [if]
            [not]
                [have_unit]
                    id=Nym
                    [filter_location]
                        x,y=$x1,$y1
                        radius=1
                    [/filter_location]
                [/have_unit]
            [/not]
            [then]
                {MOVE_UNIT id=Nym $x1 $y1}
            [/then]
        [/if]

        [message]
            speaker=Zhul
            message=_"Nym! No! Don’t open—"
        [/message]

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

        [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"
            side=1
            [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]

        [hide_unit]
            id=Dust Devil
        [/hide_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]
            array=locs_around_nym
            [do]
                {VARIABLE dust_devil_path.x    "$dust_devil_path.x|,$this_item.x"}
                {VARIABLE dust_devil_path.y    "$dust_devil_path.y|,$this_item.y"}

                {VARIABLE dust_devil_retpath.x "$this_item.x|,$dust_devil_retpath.x|"}
                {VARIABLE dust_devil_retpath.y "$this_item.y|,$dust_devil_retpath.y|"}
            [/do]
        [/foreach]

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

        [unhide_unit]
            id=Dust Devil
        [/unhide_unit]

        {CLEAR_VARIABLE locs_around_nym,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}
    [/event]

#undef OGRE_PLACEMENT

    # 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=mobilize_black_hand_bandits
        [filter_condition]
            [variable]
                name=bandits_mobilized
                boolean_equals=no
            [/variable]
            [have_unit]
                role="Black Lieutenant"
                [or]
                    role="Black Hand Bandit"
                [/or]
            [/have_unit]
        [/filter_condition]

        [store_unit]
            variable=bandits
            kill=yes
            [filter]
                role="Black Lieutenant"
                [or]
                    role="Black Hand Bandit"
                [/or]
            [/filter]
        [/store_unit]
        [foreach]
            array=bandits
            [do]
                {CLEAR_VARIABLE this_item.ai_special,this_item.status.guardian}
                [unstore_unit]
                    variable=this_item
                [/unstore_unit]
            [/do]
        [/foreach]
        {CLEAR_VARIABLE bandits}

        {VARIABLE bandits_mobilized yes}
    [/event]

    [event]
        name=die
        [filter]
            side=1
        [/filter]
        [filter_second]
            side=3
            race=ogre
        [/filter_second]

        [fire_event]
            name=mobilize_black_hand_bandits
        [/fire_event]
    [/event]

    [event]
        name=die
        first_time_only=no
        [filter]
            side=3
            race=ogre
        [/filter]

        [if]
            [variable]
                name=ogres_killed
                equals=3
            [/variable]
            [then]
                [fire_event]
                    name=mobilize_black_hand_bandits
                [/fire_event]
            [/then]
            [else]
                {VARIABLE_OP ogres_killed add 1}
            [/else]
        [/if]
    [/event]

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

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

        {RANDOM_PLACEMENT_AREA 6 43 3}

        {VARIABLE trappers {ON_DIFFICULTY 1 1 2} }
        {VARIABLE thugs    {ON_DIFFICULTY 3 3 3} }
        {VARIABLE poachers {ON_DIFFICULTY 3 3 3} }
        {VARIABLE bandits  {ON_DIFFICULTY 0 1 2} }

        {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 "Trapper" "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]

                {UNCLEAR_FOG}

                [fire_event]
                    name=mobilize_black_hand_bandits
                [/fire_event]
            [/then]

            [else]
                [event]
                    name=sighted
                    [filter]
                        side=2
                    [/filter]
                    [filter_second]
                        side=1
                    [/filter_second]

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

                    {UNCLEAR_FOG}

                    [fire_event]
                        name=mobilize_black_hand_bandits
                    [/fire_event]
                [/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=23-36,14-21
            side=1
        [/filter]

        [unit]
            type=Red Mage
            id=Elyssa
            name= _ "Elyssa"
            unrenamable=yes
            profile=portraits/elyssa.png
            gender=female
            experience=15
            [modifications]
                {TRAIT_LOYAL}
                {TRAIT_QUICK}
                # 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=yes
                        [movement_costs]
                            sand=1
                        [/movement_costs]
                    [/effect]
                [/object]
            [/modifications]
            x="$($x1 - 2)"
            y="$($y1 - 4)"
            side=1
        [/unit]

        {RANDOM_PLACEMENT_AREA "$($x1 + 7)" "$($y1 - 1)" 3}

        {VARIABLE skeletons {ON_DIFFICULTY 2 2 2} }
        {VARIABLE archers   {ON_DIFFICULTY 2 2 1} }
        {VARIABLE shooters  {ON_DIFFICULTY 1 1 2} }
        {VARIABLE revenants {ON_DIFFICULTY 0 1 2} }

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

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

        {CLEAR_PLACEMENT_AREA}

        [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
            [filter]
                role="ElyssaUndead"
            [/filter]

            [filter_condition]
                [have_unit]
                    role="ElyssaUndead"
                    count=0
                [/have_unit]
            [/filter_condition]

            [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]
        [/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-39
                y=22-33
                [filter_location]
                    [not]
                        time_of_day=lawful
                    [/not]
                [/filter_location]
            [/have_unit]

            #!***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
                [/unit]

                {CLEAR_FOG 1 34 26 3}

                {NAMED_UNIT 4 "Ghost" 33 25 () ( _ "Honor Guard") (animate=yes)}
                {NAMED_UNIT 4 "Ghost" 35 25 () ( _ "Honor Guard") (animate=yes)}

#ifndef EASY
                {NAMED_UNIT 4 "Ghost" 33 26 () ( _ "Honor Guard") (animate=yes)}
                {NAMED_UNIT 4 "Ghost" 35 26 () ( _ "Honor Guard") (animate=yes)}
#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 of the sands, 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=last breath

                    [filter]
                        id=Vengeful Lord
                    [/filter]

                    [message]
                        speaker=Vengeful Lord
                        message= _ "Noo... (<i>Fades</i>)"
                    [/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]
                        amount={ON_DIFFICULTY 100 75 50}
                        side=1
                    [/gold]
                [/event]
            [/then]

            #!***Do ogre event***
            [else]
#ifdef EASY
                {VARIABLE ogres "Ogre,Young Ogre,Young Ogre,Ogre"}
#endif
#ifdef NORMAL
                {VARIABLE ogres "Ogre,Ogre,Young Ogre,Ogre"}
#endif
#ifdef HARD
                {VARIABLE ogres "Ogre,Ogre,Ogre,Ogre"}
#endif
                {SCATTER_UNITS 4 $ogres 1 x,y,radius=34,27,3 (
                    side=3
                    name= _ "Ogre Nomad"
                    role=Ogre Nomad
                    random_traits=no
                    animate=yes
                )}

                {CLEAR_VARIABLE ogres}

                {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

                    [filter]
                        role=Ogre Nomad
                    [/filter]

                    [filter_condition]
                        [have_unit]
                            role=Ogre Nomad
                            count=0
                        [/have_unit]
                    [/filter_condition]

                    [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."
                        image=portraits/nym_moody.png
                    [/message]
                [/event]
            [/else]
        [/if]
    [/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}

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

        [modify_side]
            side=2
            {INCOME 12 16 20}
            {GOLD 150 200 250}
        [/modify_side]

        [show_objectives]
        [/show_objectives]
    [/event]

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

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

        [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

    [event]
        name=last breath

        [filter]
            id=Thorn
        [/filter]

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

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

#ifdef HARD

        [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

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

        # Holy Water item event
        # If unit moves to hex after outlaw leader dies, it gets the holy water
        [event]
            name=moveto
            id=take_holy_water
            delayed_variable_substitution=no
            first_time_only=no

            [filter]
                x=$x1
                y=$y1
                side=1
                [not]
                    type=Dust Devil
                [/not]
            [/filter]

            [message]
                speaker=unit

                message= _ "Should I take the holy water?"
                [option]
                    label= _ "Yes, I’ll take it."

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

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

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

                        [remove_event]
                            id=take_holy_water
                        [/remove_event]
                    [/command]
                [/option]

                [option]
                    label= _ "No, I think someone else should take it."

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

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

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

        [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 all this used to 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 Sceptre 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 ghosts}
        {CLEAR_VARIABLE ghosts_already_spawned}
        {CLEAR_VARIABLE ghosts_killed}
        {CLEAR_VARIABLE max_ghosts}
        {CLEAR_VARIABLE ghosts_to_kill}
    [/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.

    # LOST SOUL DISAPPEARANCE at first daylight
    [event]
        name=new turn
        first_time_only=no
        [filter_condition]
            [have_location]
                time_of_day_id=dawn1,dawn2
            [/have_location]
        [/filter_condition]

        # reset the ghosts' counters
        {VARIABLE ghosts_already_spawned 0}
        {VARIABLE ghosts_killed 0}
        [kill]
            role=LostSoul
            animate=no
            fire_event=no
        [/kill]
    [/event]

    [event]
        name=new turn
        first_time_only=no
        [filter_condition]
            [have_location]
                time_of_day_id=dusk1,dusk2
                [or]
                    time_of_day=chaotic
                [/or]
            [/have_location]
            [variable]
                name=ghosts_already_spawned
                less_than=$max_ghosts
            [/variable]
        [/filter_condition]

        # store "exposed" units - those surrounded by a lot of desert
        [store_locations]
            variable=ghost_spawn_locs
            [filter]
                side=1
            [/filter]
            terrain=Hd^*, Dd^*, Ds^*, Rr^*
            [filter_adjacent_location]
                count="5,6"
                adjacent="n,ne,se,s,sw,nw"
                terrain=Hd^*, Dd^*, Ds^*, Rr^*
            [/filter_adjacent_location]
        [/store_locations]

        [if]
            [variable]
                name=ghost_spawn_locs.length
                greater_than=0
            [/variable]
            [then]
                # 1 ghost per 4/3/2 exposed units (roughly) per turn
                {VARIABLE ghosts_per_unit "$($ghost_spawn_locs.length / {ON_DIFFICULTY 4 3 2})"}

                # spawn more rapidly as time goes by
                {VARIABLE ghosts_per_turn "$(($turn_number / 21) + {ON_DIFFICULTY 1 2 3})"}

                {VARIABLE ghost_limit "$($max_ghosts - $ghosts_already_spawned)"}

                {VARIABLE ghosts "$(min([$ghosts_per_unit, $ghosts_per_turn, $ghost_limit]))"}

                {CLEAR_VARIABLE ghosts_per_unit,ghosts_per_turn,ghost_limit}

                # store all possible spawn locations in range
                # pick one random ghost_spawn from which
                # to store all locations in range
                {RANDOM "0..$($ghost_spawn_locs.length - 1)"}

                [store_locations]
                    variable=spawn
                    terrain=Hd^*, Dd^*, Ds^*, Rr^*
                    [filter_adjacent_location]
                        [filter]
                            side=1
                        [/filter]
                    [/filter_adjacent_location]
                    [and]
                        x=$ghost_spawn_locs[$random].x
                        y=$ghost_spawn_locs[$random].y
                        radius=8
                    [/and]
                [/store_locations]

                {CLEAR_VARIABLE ghost_spawn_locs}

                [repeat]
                    times=$ghosts

                    [do]
                        {RANDOM "0..$($spawn.length - 1)"}

                        # wmlindent: start ignoring
                        {NAMED_UNIT 4 "Ghost" $spawn[$random].x $spawn[$random].y () ( _ "Lost Soul") (
                            animate=yes
                            role="LostSoul")}
                        # wmlindent: stop ignoring

                        {VARIABLE_OP ghosts_already_spawned add 1}
                    [/do]
                [/repeat]

                {CLEAR_VARIABLE spawn,random}

                # some ghosts dialogue
                [fire_event]
                    name=ghosts_talk
                [/fire_event]
            [/then]
        [/if]
    [/event]

    [event]
        name=ghosts_talk

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

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

            [then]
                {VARIABLE_OP ghosts_killed add 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}

                [fire_event]
                    name=ghosts_leave
                [/fire_event]
            [/else]
        [/if]
    [/event]

    [event]
        name=ghosts_leave

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

        [event]
            name=ghosts_talk

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

        [event]
            name=ghosts_leave

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

#define UTBS_GARAK_MUST_LIVE
#enddef
    {UTBS_INCLUDE utils/deaths.cfg}
#undef UTBS_GARAK_MUST_LIVE
[/scenario]