#textdomain wesnoth-tsg
[scenario]
    id=06b_The_Long_March

    name= _ "The Long March"
    next_scenario=07b_Pebbles_in_the_Flood

    {SCENARIO_MUSIC casualties_of_war.ogg}
    {EXTRA_SCENARIO_MUSIC underground.ogg}
    {EXTRA_SCENARIO_MUSIC weight_of_revenge.ogg}

    # Note: the map contains a large grid of roads but only one path through the
    # forest will be picked when the scenario starts. When editing the map, make
    # sure not to create any dead ends in the roads, and preferably also make
    # sure to not have sets of three dirt hexes all adjacent to each other.
    map_data="{campaigns/The_South_Guard/maps/06b_The_Long_March.map}"

    {campaigns/The_South_Guard/utils/sg_deaths.cfg}

    {DEFAULT_SCHEDULE_DUSK}

    victory_when_enemies_defeated=no
    {TURNS 25 22 20}

    {TSG_BIGMAP {JOURNEY_06B_NEW} }

    # The South Guard

    [side]
        side=1
        type=Horseman Commander
        id=Deoran
        name= _ "Deoran"
        profile=portraits/deoran.png
        unrenamable=yes

        team_name=South_Guard
        user_team_name=_"South Guard"
        controller=human
        {CUSTOM_SG_FLAG}

        fog=yes
        shroud=yes
        share_view=yes
        share_maps=yes

        {GOLD 60 50 40}
        {INCOME 6 4 2}

        canrecruit=yes
        recruit=Bowman, Spearman, Cavalryman, Thug, Footpad
    [/side]

    # We're not using an extra side for the ambusher elves
    # - they will be on the undead side by default (so the undead
    # don't trigger ambushes), then change to the active elven side
    # and are being petrified to prevent any action
    #
    # note: this means that elves can trigger ambushes, which should
    # be treated as if they "ran for reinforcements" - thus these elves
    # switch to their side just as if the player had triggered the ambush
    # (this is a better solution than undead triggering ambushes, and
    # should occur less often)

    # The Undead Hordes (Part II)

    [side]
        side=2
        no_leader=yes

        team_name=undead
        user_team_name=_"Undead"
        controller=ai

        fog=no
        shroud=no

        [ai]
            aggression=1.0
            grouping=no
            attack_depth=1
            village_value=0
            [goal]
                name=target
                [criteria]
                    side=1
                [/criteria]
                value=20
            [/goal]
        [/ai]
        {FLAG_VARIANT undead}
    [/side]

    # When discovering an ambusher, the ambusher will be moved to this side so
    # it may move and attack normally

    [side]
        side=3
        no_leader=yes
        team_name=elves
        user_team_name=_"Elves"
        controller=ai

        [ai]
            aggression=0.7
            caution=0.4
            [goal]
                name=target
                [criteria]
                    side=1
                [/criteria]
                value=5
            [/goal]
        [/ai]
        {FLAG_VARIANT long}
    [/side]

    # This prestart event iterates through the labyrinth and picks a random
    # route through it, stores the path in $secret_path, covers everything with
    # forest and then places the ford on the endpoint of the chosen path.
    [event]
        name=prestart

        {VARIABLE previous_path_hex.x 18}
        {VARIABLE previous_path_hex.y 36}
        {VARIABLE new_path_hex.x 18}
        {VARIABLE new_path_hex.y 35}
        {VARIABLE secret_path.x 18}
        {VARIABLE secret_path.y 35}

        # the first undead will spawn on this turn+1 (i.e. turn {6 5 4})
#ifdef EASY
        {VARIABLE first_undead_spawn_turn 5}
#endif
#ifdef NORMAL
        {VARIABLE first_undead_spawn_turn 4}
#endif
#ifdef HARD
        {VARIABLE first_undead_spawn_turn 3}
#endif

        [while]
            [variable]
                name=new_path_hex.y
                greater_than=10
            [/variable]

            [do]
                [if]
                    [have_location]
                        terrain=R*^*

                        [filter_adjacent_location]
                            x,y=$new_path_hex.x,$new_path_hex.y
                            adjacent=sw,s,se
                        [/filter_adjacent_location]

                        [not]
                            x,y=$previous_path_hex.x,$previous_path_hex.y
                        [/not]
                    [/have_location]

                    [then]
                        [store_locations]
                            terrain=R*^*

                            [filter_adjacent_location]
                                x,y=$new_path_hex.x,$new_path_hex.y
                                adjacent=sw,s,se
                            [/filter_adjacent_location]

                            [not]
                                x,y=$previous_path_hex.x,$previous_path_hex.y
                            [/not]

                            variable=new_road_branches
                        [/store_locations]
                    [/then]

                    [else]
                        [store_locations]
                            terrain=R*^*

                            [filter_adjacent_location]
                                x,y=$new_path_hex.x,$new_path_hex.y
                                adjacent=nw,n,ne
                            [/filter_adjacent_location]

                            [not]
                                x,y=$previous_path_hex.x,$previous_path_hex.y
                            [/not]

                            variable=new_road_branches
                        [/store_locations]
                    [/else]
                [/if]

                {VARIABLE previous_path_hex.x $new_path_hex.x}
                {VARIABLE previous_path_hex.y $new_path_hex.y}

                [if]
                    [variable]
                        name=new_road_branches.length
                        numerical_equals=1
                    [/variable]

                    [then]
                        {VARIABLE new_path_hex.x $new_road_branches.x}
                        {VARIABLE new_path_hex.y $new_road_branches.y}
                    [/then]

                    [elseif]
                        [variable]
                            name=new_road_branches.length
                            greater_than=1
                        [/variable]

                        [then]
                            {RANDOM 1..$new_road_branches.length}
                            {VARIABLE_OP random sub 1}

                            {VARIABLE new_path_hex.x $new_road_branches[$random].x}
                            {VARIABLE new_path_hex.y $new_road_branches[$random].y}
                        [/then]
                    [/elseif]
                [/if]

                #[item]
                #    x,y=$new_path_hex.x,$new_path_hex.y
                #    image=misc/capture.png
                #[/item]

                [store_locations]
                    x,y=$new_path_hex.x,$new_path_hex.y

                    [or]
                        find_in=secret_path
                    [/or]

                    variable=secret_path
                [/store_locations]
            [/do]
        [/while]

        [store_locations]
            find_in=secret_path

            variable=secret_path_unrevealed
        [/store_locations]

        [store_locations]
            terrain=R*^*
            y=10-35

            variable=secret_path_to_hide
        [/store_locations]

        [foreach]
            array=secret_path_to_hide
            [do]
                [store_locations]
                    terrain=*^F*

                    [and]
                        x,y=$this_item.x,$this_item.y
                        radius=1
                    [/and]

                    variable=adjacent_forests
                [/store_locations]

                {RANDOM "1..$adjacent_forests.length"}
                {VARIABLE_OP random sub 1}

                [terrain]
                    x,y=$this_item.x,$this_item.y
                    terrain=$adjacent_forests[$random].terrain
                [/terrain]
            [/do]
        [/foreach]

        {VARIABLE ford_mask_x $new_path_hex.x}
        {VARIABLE_OP ford_mask_x sub 1}

        # This mask creates the ford at the end of the road
        [terrain_mask]
            x=$ford_mask_x
            y=3
            border=no
            mask="Ww^Ewl, Wwf, Ww^Ewl
Ww, Wwf, Ww^Ewl
Ww^Ewl, Wwf, Ww^Ewl
Ww, Wwf, Ww
Ww, Wwf, Ww^Ewl
Ww, Wwf, Ww^Ewl
Ww^Ewl, Wwf, Ww
Ww^Ewl, Wwf, Ww^Ewl
"

            [rule]
                old=!,W*
                use_old=yes
            [/rule]
        [/terrain_mask]

        # This mask creates a straight road leading north of the ford
        [terrain_mask]
            x=$ford_mask_x
            y=0
            mask="_f, _f, Re, _f
_f, _f, Re, _f
_f, _f, Re, _f
_f, _f, Re, _f
_f, _f, Re, _f
_f, _f, Re, _f
_f, _f, Re, _f
_f, _f, Re, _f
"

            [rule]
                old=W*,D*
                use_old=yes
            [/rule]
        [/terrain_mask]

        {CLEAR_VARIABLE new_road_branches,secret_path_to_hide,adjacent_forests}

        {VARIABLE urza_dialogue 0}
    [/event]

    [event]
        name=prestart

#ifdef EASY
        {VARIABLE num_of_ambushers 8}
#endif

#ifdef NORMAL
        {VARIABLE num_of_ambushers 12}
#endif

#ifdef HARD
        {VARIABLE num_of_ambushers 16}
#endif

        # This places a bunch of lvl1 elves along the secret path, the player
        # will probably bump into all of these.
        {SCATTER_UNITS $num_of_ambushers "Elvish Fighter,Elvish Archer,Elvish Scout,Elvish Fighter" 3 (
            terrain=*^F*
            x=3-34
            y=13-31

            [filter_adjacent_location]
                find_in=secret_path
            [/filter_adjacent_location]

            [not]
                find_in=secret_path
            [/not]

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

            [not]
                [filter_adjacent_location]
                    [filter]
                    [/filter]
                [/filter_adjacent_location]
            [/not]
        ) (
            side=2
            generate_name=yes
            random_traits=yes
            upkeep=loyal

            [modifications]
                [object]
                    [effect]
                        apply_to=new_ability

                        [abilities]
                            [hides]
                                id=elvish_ambush
                                affect_self=yes

                                [filter_self]
                                    # only effective as long as the unit
                                    # is on the ambusher side
                                    side=2

                                    [filter_location]
                                        terrain=*^F*
                                    [/filter_location]
                                [/filter_self]
                            [/hides]
                        [/abilities]
                    [/effect]
                [/object]
            [/modifications]
        )}

        # This places lvl2 elves in the forest, away from the secret path. The
        # player will mostly only bump into these if he wanders away from the
        # path, although a few might occasionally be close enough to join an
        # ambush by the lvl1 elves next to the path.
        {SCATTER_UNITS 30 "Elvish Hero,Elvish Captain,Elvish Ranger" 2 (
            terrain=*^F*
            x=3-34
            y=13-31

            [not]
                radius=3
                find_in=secret_path
            [/not]

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

            [not]
                [filter_adjacent_location]
                    [filter]
                    [/filter]
                [/filter_adjacent_location]
            [/not]
        ) (
            side=2
            generate_name=yes
            random_traits=yes
            upkeep=loyal

            [modifications]
                [object]
                    [effect]
                        apply_to=new_ability

                        [abilities]
                            [hides]
                                id=elvish_ambush
                                affect_self=yes

                                [filter_self]
                                    # only effective as long as the unit
                                    # is on the ambusher side
                                    side=2

                                    [filter_location]
                                        terrain=*^F*
                                    [/filter_location]
                                [/filter_self]
                            [/hides]
                        [/abilities]
                    [/effect]
                [/object]
            [/modifications]
        )}

        # if the player used debug to get here, create afalas for them
        [if]
            [not]
                [have_unit]
                    id=Urza Afalas
                [/have_unit]
            [/not]
            [then]
                [unit]
                    side=1
                    type=Outlaw
                    id=Urza Afalas
                    name= _ "Urza Afalas"
                    unrenamable=yes
                    profile=portraits/urza-afalas.png
                    {IS_HERO}
                    x,y=recall,recall
                    random_traits=no
                    [modifications]
                        {TRAIT_LOYAL}
                    [/modifications]
                [/unit]
            [/then]
        [/if]

        # also get rid of elves
        [kill]
            side=1
            race=elf
            animate=no
            fire_event=no
        [/kill]
    [/event]

    [event]
        name=start

        [message]
            speaker=narrator
            {NARRATOR: _"Deoran and Urza Afalas retreated quickly from Mebrin's fortress..."}
            image=wesnoth-icon.png
        [/message]

        [recall]
            id=Urza Afalas
        [/recall]
        [recall]
            id=Sir Gerrick
        [/recall]
        [recall]
            id=Minister Hylas
        [/recall]
        [recall]
            id=Jarek
        [/recall]

#ifdef HARD
        [recall]
            type=Royal Guard, Pikeman, Swordsman, Javelineer, Master Bowman, Longbowman, Spearman, Bowman
        [/recall]
        [recall]
            type=Royal Guard, Pikeman, Swordsman, Javelineer, Master Bowman, Longbowman, Spearman, Bowman
        [/recall]
#else
        [recall]
            type=Royal Guard, Pikeman, Swordsman, Javelineer, Master Bowman, Longbowman, Spearman, Bowman
        [/recall]
        [recall]
            type=Royal Guard, Pikeman, Swordsman, Javelineer, Master Bowman, Longbowman, Spearman, Bowman
        [/recall]
        [recall]
            type=Royal Guard, Pikeman, Swordsman, Javelineer, Master Bowman, Longbowman, Spearman, Bowman
        [/recall]
#endif

        [message]
            speaker=Deoran
            message= _ "Forest, forest, forest! As far as the eye can see, it is all forest! I am getting sick of endlessly trudging through these woods. Are you sure you are taking us in the right direction?"
        [/message]
        [message]
            speaker=Urza Afalas
            message= _ "Trust me. I have often traveled these woods with my comrades and I know a secret path through it. I am taking you there so we can avoid the elves on the way back."
        [/message]
        [message]
            speaker=Deoran
            message= _ "Would that I had listened to Ethiliel... we would be battling the undead by now, instead of crawling around in these woods like rats!"
        [/message]
        [message]
            speaker=Sir Gerrick
            message= _ "What’s done is done. We must worry about the task at hand. Winter is almost upon us and it will be much more difficult to forage for food then."
        [/message]
        [message]
            speaker=Minister Hylas
            message= _ "That is not our only problem. I sense that many undead creatures are approaching us rapidly. It seems as if they are chasing us, though I know not why."
        [/message]
        [message]
            speaker=Urza Afalas
            message= _ "It must be because we destroyed that phantom lich. The corrupted sage is sending them to hunt us in retaliation. Let’s get out of here quickly so we won’t have to fight them."
        [/message]

        {MOVE_UNIT (id=Urza Afalas) 18 35}

        [message]
            speaker=Urza Afalas
            message= _ "Also, remember that these woods are crawling with elves. Stay on the path and perhaps we can avoid at least most of them. If we do get seen, well, let’s hope they won’t attack us."
        [/message]

        [objectives]
            side=1
            [objective]
                description= _ "Move Deoran through the woods and over the river"
                condition=win
            [/objective]
            [objective]
                description= _ "Death of Deoran"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Sir Gerrick"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Minister Hylas"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Urza Afalas"
                condition=lose
            [/objective]

            {TURNS_RUN_OUT}

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

            [note]
                description= _ "At the start of each turn, Urza Afalas will reveal as much of the secret path as is within his vision range."
            [/note]
        [/objectives]
    [/event]

    # This event stores the vision range of Urza Afalas into
    # $urza_movement_area and then reveals any parts of the secret path within
    # it.
    #
    # Note: the mechanism inside the loop is very fragile and the ordering of
    # the commands is important. Be sure to test thoroughly after making any
    # changes to it.
    [event]
        name=new turn
        first_time_only=no

        [store_unit]
            [filter]
                id=Urza Afalas
            [/filter]

            kill=no
            variable=stored_Urza_Afalas
        [/store_unit]

        {VARIABLE urza_movement_area.x $stored_Urza_Afalas.x}
        {VARIABLE urza_movement_area.y $stored_Urza_Afalas.y}

        {VARIABLE urza_moves $stored_Urza_Afalas.max_moves}

        [while]
            [variable]
                name=urza_moves
                greater_than=0
            [/variable]

            [do]
                [store_locations]
                    find_in=delayed_2_mp_hexes

                    variable=previous_2_mp_hexes
                [/store_locations]

                [store_locations]
                    terrain=*^F*,Hh,Ss,Ww

                    [filter_adjacent_location]
                        find_in=urza_movement_area
                    [/filter_adjacent_location]

                    [not]
                        find_in=urza_movement_area
                    [/not]

                    [not]
                        find_in=secret_path
                    [/not]

                    [not]
                        find_in=previous_2_mp_hexes
                    [/not]

                    variable=delayed_2_mp_hexes
                [/store_locations]

                [store_locations]
                    find_in=urza_movement_area
                    radius=1

                    [filter_radius]
                        terrain=R*^*,Gg,C*,K*,*^V*

                        [or]
                            terrain=*^F*
                            find_in=secret_path
                        [/or]
                    [/filter_radius]

                    variable=urza_movement_area
                [/store_locations]

                [store_locations]
                    find_in=urza_movement_area

                    [or]
                        find_in=previous_2_mp_hexes
                    [/or]

                    variable=urza_movement_area
                [/store_locations]

                {CLEAR_VARIABLE previous_2_mp_hexes}

                {VARIABLE_OP urza_moves sub 1}
            [/do]
        [/while]

        # extend the movement area by one to expand the area to represent the
        # vision range instead
        [store_locations]
            find_in=urza_movement_area
            radius=1

            variable=urza_movement_area
        [/store_locations]

        # marks the area visibly on the map, useful for debugging
        #[foreach]
        #    array=urza_movement_area
        #    [do]
        #        [label]
        #            x,y=$this_item.x,$this_item.y
        #            text=$turn_number
        #        [/label]
        #    [/do]
        #[/foreach]

        # store all yet unrevealed hidden road hexes within the area and reveal
        # them
        [store_locations]
            [not]
                terrain=R*^*
            [/not]
            y=10-35
            find_in=urza_movement_area

            [and]
                find_in=secret_path
            [/and]

            variable=secret_path_to_reveal
        [/store_locations]

        [store_locations]
            find_in=secret_path_unrevealed

            [not]
                find_in=secret_path_to_reveal
            [/not]

            variable=secret_path_unrevealed
        [/store_locations]

        [if]
            [variable]
                name=secret_path_unrevealed.length
                greater_than=0
            [/variable]
            [then]
                [scroll_to_unit]
                    id=Urza Afalas
                [/scroll_to_unit]
            [/then]
        [/if]
        [foreach]
            array=secret_path_to_reveal
            [do]
                [terrain]
                    x,y=$this_item.x,$this_item.y
                    terrain=Re
                [/terrain]
            [/do]
        [/foreach]

        [redraw]
        [/redraw]

        # This makes Urza Afalas say different things based on how much of the
        # path is still unrevealed
        [if]
            [variable]
                name=secret_path_to_reveal.length
                greater_than=0
            [/variable]

            [variable]
                name=turn_number
                greater_than=1
            [/variable]

            [variable]
                name=urza_dialogue
                less_than=4
            [/variable]

            [then]
                [if]
                    [variable]
                        name=secret_path_unrevealed.length
                        less_than=10
                    [/variable]

                    [variable]
                        name=urza_dialogue
                        equals=3
                    [/variable]

                    [then]
                        [message]
                            speaker=Urza Afalas
                            message= _ "Just a little further!"
                        [/message]
                    [/then]

                    [elseif]
                        [variable]
                            name=secret_path_unrevealed.length
                            less_than=20
                        [/variable]

                        [variable]
                            name=urza_dialogue
                            equals=2
                        [/variable]

                        [then]
                            [message]
                                speaker=Urza Afalas
                                message= _ "We’re almost there!"
                            [/message]
                        [/then]
                    [/elseif]
                    [elseif]
                        [variable]
                            name=secret_path_unrevealed.length
                            less_than=30
                        [/variable]

                        [variable]
                            name=urza_dialogue
                            equals=1
                        [/variable]

                        [then]
                            [message]
                                speaker=Urza Afalas
                                message= _ "Come on! Hurry!"
                            [/message]
                        [/then]
                    [/elseif]
                    [elseif]
                        [variable]
                            name=secret_path_unrevealed.length
                            less_than=40
                        [/variable]

                        [variable]
                            name=urza_dialogue
                            equals=0
                        [/variable]

                        [then]
                            [message]
                                speaker=Urza Afalas
                                message= _ "Keep moving!"
                            [/message]
                        [/then]
                    [/elseif]
                [/if]

                {VARIABLE_OP urza_dialogue add 1}
            [/then]
        [/if]

        [if]
            [have_location]
                y=10
                find_in=secret_path_to_reveal
            [/have_location]

            [then]
                [message]
                    speaker=Urza Afalas
                    message= _ "You can see the banks of the river from here!"
                [/message]
            [/then]
        [/if]

        {CLEAR_VARIABLE urza_movement_area,delayed_2_mp_hexes,previous_2_mp_hexes,secret_path_to_reveal,stored_Urza_Afalas}
    [/event]

    [event]
        name=turn refresh
        first_time_only=no

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

            [then]
                {MODIFY_UNIT side,race=2,elf moves 0}
            [/then]
        [/if]
    [/event]

    [event]
        name=moveto

        [filter]
            side=1

            [filter_adjacent]
                side=2
                race=elf
            [/filter_adjacent]
        [/filter]

        {MODIFY_UNIT (
            side=2
            race=elf
            [filter_location]
                x,y=$x1,$y1
                radius=4
            [/filter_location]
        ) side 3}

        [redraw][/redraw]

        [message]
            race=elf
            [filter_location]
                [filter_adjacent_location]
                    x,y=$x1,$y1
                [/filter_adjacent_location]
            [/filter_location]
            message= _"Humans! You are the curs who kidnapped the Sage Mebrin! Tell us where you have hidden him and perhaps we will grant you a quick death!"
        [/message]
        [message]
            id=Deoran
            message= _"We have already told you of his whereabouts! Your wise sage now practices necromancy and commands an army of undead. He means to march on our land and slay all living souls, human and elf alike!"
        [/message]
        [message]
            race=elf
            [filter_location]
                [filter_adjacent_location]
                    x,y=$x1,$y1
                [/filter_adjacent_location]
            [/filter_location]
            message= _"How dare you speak of Mebrin with such lies?! Though he has knowledge of the undead, he is no human, so easily corrupted by the temptation of such power. Did you think that you could fool us with such a fictitious story?"
        [/message]
        [message]
            id=Deoran
            message= _"We are not lying! If you travel south and enter the undead lair, you see that we speak the truth!"
        [/message]
        [message]
            race=elf
            [filter_location]
                [filter_adjacent_location]
                    x,y=$x1,$y1
                [/filter_adjacent_location]
            [/filter_location]
            message= _"Enough! I should have known that the stupidity of humans knows no bounds. We have nothing more to say here."
        [/message]

        [event]
            name=moveto
            delayed_variable_substitution=yes

            [filter]
                side=1

                [filter_adjacent]
                    side=2
                    race=elf
                [/filter_adjacent]
            [/filter]

            {MODIFY_UNIT (
                side=2
                race=elf
                [filter_location]
                    x,y=$x1,$y1
                    radius=4
                [/filter_location]
            ) side 3}

            [redraw][/redraw]

            [message]
                race=elf
                [filter_location]
                    [filter_adjacent_location]
                        x,y=$x1,$y1
                    [/filter_adjacent_location]
                [/filter_location]
                message= _ "Humans, bandits and abductors! Prepare to be slain!"
            [/message]
            [message]
                speaker=Sir Gerrick
                message= _ "We don’t want to fight you! We wish only to return to our homes!"
            [/message]
            [message]
                race=elf
                [filter_location]
                    [filter_adjacent_location]
                        x,y=$x1,$y1
                    [/filter_adjacent_location]
                [/filter_location]
                message= _ "And so does our Sage Mebrin, yet you have not returned him to us! Even if you did, we would still dispatch of you for your heinous transgressions, you treacherous scum!"
            [/message]
            [message]
                speaker=Urza Afalas
                message= _ "There is no reasoning with the elves. We will either have to fight or run."
            [/message]

            [event]
                name=moveto
                delayed_variable_substitution=yes

                [filter]
                    side=1

                    [filter_adjacent]
                        side=2
                        race=elf
                    [/filter_adjacent]
                [/filter]

                {MODIFY_UNIT (
                    side=2
                    race=elf
                    [filter_location]
                        x,y=$x1,$y1
                        radius=4
                    [/filter_location]
                ) side 3}

                [redraw][/redraw]

                [message]
                    race=elf
                    [filter_location]
                        [filter_adjacent_location]
                            x,y=$x1,$y1
                        [/filter_adjacent_location]
                    [/filter_location]
                    message= _ "Get them!"
                [/message]

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

                    [filter]
                        side=1

                        [filter_adjacent]
                            side=2
                            race=elf
                        [/filter_adjacent]
                    [/filter]

                    {MODIFY_UNIT (
                        side=2
                        race=elf
                        [filter_location]
                            x,y=$x1,$y1
                            radius=4
                        [/filter_location]
                    ) side 3}

                    [redraw][/redraw]
                [/event]
            [/event]
        [/event]
    [/event]

    [event]
        name=moveto
        first_time_only=no

        [filter]
            side=3

            [filter_adjacent]
                side=2
                race=elf
            [/filter_adjacent]
        [/filter]

        {MODIFY_UNIT (
            side=2
            race=elf
            [filter_adjacent]
                x,y=$x1,$y1
            [/filter_adjacent]
        ) side 3}

        [redraw][/redraw]
    [/event]

    [event]
        name=new turn
        first_time_only=no
        [if]
            [variable]
                name=turn_number
                greater_than=$first_undead_spawn_turn
            [/variable]

            # Don't spawn more than is really needed, for performance reasons
            [have_unit]
                side=2
                race=bats,undead
                count=0-30
            [/have_unit]

            [then]
                {VARIABLE got_a_quick_one_this_turn no}

                [repeat]
                    times="$(min($turn_number - $first_undead_spawn_turn, 5))"

                    [do]
                        {VARIABLE_OP undead_spawn_x rand "11..22"}

                        # If there's already been a quick unit (bat)
                        # spawned this turn, don't make any more
                        [if]
                            [variable]
                                name=got_a_quick_one_this_turn
                                equals=yes
                            [/variable]

                            [then]
#ifdef EASY
                                {VARIABLE_OP undead_spawn_type rand "Bone Shooter,Revenant,Necrophage"}
#else
                                {VARIABLE_OP undead_spawn_type rand "Bone Shooter,Revenant,Deathblade,Necrophage"}
#endif
                            [/then]

                            [else]
#ifdef EASY
                                {VARIABLE_OP undead_spawn_type rand "Bone Shooter,Revenant,Blood Bat,Necrophage"}
#else
                                {VARIABLE_OP undead_spawn_type rand "Bone Shooter,Revenant,Deathblade,Blood Bat,Dread Bat,Necrophage"}
#endif
                            [/else]
                        [/if]

                        # This checks whether the current spawn is going to be a
                        # "quick" one, so on the next repeat the above [if] will
                        # know whether we've already gotten a quick one this turn
                        {VARIABLE quick_spawn_types "Blood Bat,Dread Bat"}
                        [if]
                            [variable]
                                name=quick_spawn_types
                                contains=$undead_spawn_type
                            [/variable]

                            [then]
                                {VARIABLE got_a_quick_one_this_turn yes}
                            [/then]
                        [/if]

                        [unit]
                            side=2
                            type=$undead_spawn_type
                            x=$undead_spawn_x
                            y=45
                        [/unit]
                    [/do]
                [/repeat]

                {CLEAR_VARIABLE got_a_quick_one_this_turn,undead_spawn_type,undead_spawn_x}
            [/then]
        [/if]
    [/event]

    [event]
        name=last breath
        [filter]
            id=Urza Afalas
        [/filter]
        [message]
            speaker=unit
            message= _ "I pay for my crimes, but without me, you will never make it back to Kerlath..."
        [/message]
        [kill]
            x,y=$x1,$y1
            animate=yes
        [/kill]
        [endlevel]
            result=defeat
        [/endlevel]
    [/event]

    [event]
        name=time over

        [remove_shroud]
            [filter_side]
                side=1
            [/filter_side]
            x=16-23
            y=0-5
        [/remove_shroud]
        [lift_fog]
            [filter_side]
                side=1
            [/filter_side]
            x=16-23
            y=0-5
            multiturn=yes
        [/lift_fog]
        [redraw]
            side=1
            clear_shroud=yes
        [/redraw]

        [unit]
            type=Elvish Marshal
            side=3
            id=Ithelden
            name= _ "Ithelden"
            animate=yes
            x,y=20,1
        [/unit]
        {GENERIC_UNIT 3 (Elvish Hero) 19 2}
        {GENERIC_UNIT 3 (Elvish Captain) 21 2}
        {GENERIC_UNIT 3 (Elvish Marksman) 19 1}
        {GENERIC_UNIT 3 (Elvish Druid) 21 1}

        [message]
            speaker=Ithelden
            message= _ "So, these pitiful humans are still alive? Good. I will have the pleasure of crushing them myself."
        [/message]
        [message]
            speaker=Deoran
            message= _ "We took too long to escape these forests..."
        [/message]
    [/event]

    # Deoran exits the forest.

    [event]
        name=moveto
        [filter]
            id=Deoran
            y=1-6

            [filter_location]
                [not]
                    terrain=W*
                [/not]
            [/filter_location]
        [/filter]
        [message]
            speaker=Deoran
            message= _ "At last, we have escaped that awful forest! It seems that you actually kept your word about something, Afalas."
        [/message]
        [message]
            speaker=Urza Afalas
            message= _ "Good knight, I am not evil as you think I am. I really do mean to aid in your battle against the undead. I am sorry for my deception before, but that was truly only because I thought you would kill me!"
        [/message]
        [message]
            speaker=Sir Gerrick
            message= _ "Your past crimes are not easy to overlook, but we will see how you aid us in the coming battle. As Sir Deoran said, you have kept your word about leading us back to Wesnoth, and that is a good start."
        [/message]
        [message]
            speaker=Urza Afalas
            message= _ "Thank you. I will help you as much as I can when we return to Westin."
        [/message]

        {CLEAR_VARIABLE secret_path}
        {CLEAR_VARIABLE first_undead_spawn_turn,ford_mask_x,num_of_ambushers,quick_spawn_types,random,urza_moves,urza_dialogue,previous_path_hex,new_path_hex,secret_path_unrevealed}

        [endlevel]
            result=victory
            bonus=yes
            {NEW_GOLD_CARRYOVER 40}
        [/endlevel]
    [/event]
[/scenario]
