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

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

    {SCENARIO_MUSIC heroes_rite.ogg}
    {EXTRA_SCENARIO_MUSIC the_dangerous_symphony.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}

    {DUSK}
    {FIRST_WATCH}
    {SECOND_WATCH}
    {DAWN}
    {MORNING}
    {AFTERNOON}

    victory_when_enemies_defeated=no
    {TURNS 30 25 20}

    # The South Guard

    [side]
        side=1
        type=Horseman Commander
        id=Deoran
        name= _ "Deoran"
        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 125 100 80}
        {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

        canrecruit=yes
        [ai]
            aggression=2.0
            grouping=no
            attack_depth=1
            village_value=0
            [target]
                side=1
                value=20
            [/target]
        [/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
            [target]
                side=1
                value=5.0
            [/target]
        [/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}

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

                    [else]
                        [if]
                            [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]
                        [/if]
                    [/else]
                [/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 secret_path_to_hide i}
            [store_locations]
                terrain=*^F*

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

                variable=adjacent_forests
            [/store_locations]

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

            [terrain]
                x,y=$secret_path_to_hide[$i].x,$secret_path_to_hide[$i].y
                terrain=$adjacent_forests[$random].terrain
            [/terrain]
        {NEXT i}

        {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
            mask="usage=mask
border_size=0

Ww, Wwf, Ww
Ww, Wwf, Ww
Ww, Wwf, Ww
Ww, Wwf, Ww
Ww, Wwf, Ww
Ww, Wwf, Ww
Ww, Wwf, Ww
Ww, Wwf, Ww
"

            [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
            border=yes
            mask="usage=map
border_size=1

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

        {NAMED_GENERIC_UNIT 3 (Naga Warrior) $new_path_hex.x 6 (Fssth) (_"Fssth")} {GUARDIAN}

#ifdef NORMAL
        {NAMED_GENERIC_UNIT 3 (Naga Fighter) $new_path_hex.x 6 (Wizzi) (_"Wizzi")} {GUARDIAN}
#endif

#ifdef HARD
        {NAMED_GENERIC_UNIT 3 (Naga Warrior) $new_path_hex.x 6 (Wizzi) (_"Wizzi")} {GUARDIAN}
#endif

        {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 12}
#endif

#ifdef NORMAL
        {VARIABLE num_of_ambushers 16}
#endif

#ifdef HARD
        {VARIABLE num_of_ambushers 20}
#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 Shaman,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 Marksman,Elvish Ranger,Elvish Druid,Elvish Sorceress" 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]
        )}
    [/event]

    [event]
        name=start

        [message]
            speaker=narrator
            message= _ "Deoran and Urza Afalas led their men 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]

        [message]
            speaker=Deoran
            message= _ "Will this forest ever end? How can we know what path to take?!"
        [/message]
        [message]
            speaker=Urza Afalas
            message= _ "You have to follow me! I have often traveled these woods avoiding the elves with my comrades, and I know a secret path through it."
        [/message]
        [message]
            speaker=Sir Gerrick
            message= _ "Can we trust him? His people are murderers and thieves!"
        [/message]
        [message]
            speaker=Minister Hylas
            message= _ "Do we have any choice? Our food is running out, and it will be winter soon..."
        [/message]
        [message]
            speaker=Minister Hylas
            message= _ "Beware! I sense that there are many undead creatures chasing us. We should move quickly."
        [/message]
        [message]
            speaker=Urza Afalas
            message= _ "Let us go quickly, then. I will show you the way."
        [/message]

        {MOVE_UNIT (id=Urza Afalas) 18 35}

        [message]
            speaker=Urza Afalas
            message= _ "Remember that these woods are crawling with elves, and I don’t think they’ll receive us too kindly. Stay on the path and perhaps we can avoid at least most of them."
        [/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= _ "At the start of each turn, Urza Afalas will reveal as much of the secret path as is within his vision range."
        [/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 urza_movement_area i}
        #    [label]
        #        x,y=$urza_movement_area[$i].x,$urza_movement_area[$i].y
        #        text=$turn_number
        #    [/label]
        #{NEXT i}

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

        [scroll_to_unit]
            id=Urza Afalas
        [/scroll_to_unit]

        {FOREACH secret_path_to_reveal i}
            [terrain]
                x,y=$secret_path_to_reveal[$i].x,$secret_path_to_reveal[$i].y
                terrain=Re
            [/terrain]
        {NEXT i}

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

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

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

                                    [else]
                                        [if]
                                            [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]
                                        [/if]
                                    [/else]
                                [/if]
                            [/else]
                        [/if]
                    [/else]
                [/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= _ "From here you can see the banks of the river! Across it we will find the sun again, and an end to these cursed trees!"
                [/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= _"You humans have kidnapped Mebrin the Sage. You will pay for your crimes."
        [/message]
        [message]
            id=Deoran
            message= _"Your sage Mebrin is the criminal. He practices necromancy and now commands an army of undead. By the laws of Wesnoth, the punishment for his crimes is death!"
        [/message]
        [message]
            race=elf
            [filter_location]
                [filter_adjacent_location]
                    x,y=$x1,$y1
                [/filter_adjacent_location]
            [/filter_location]
            message= _"It is you who is a criminal and who is in a company of criminals. We will not let you kill Mebrin!"
        [/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= _ "We cannot allow you bandits and criminals to pass through our lands!"
            [/message]
            [message]
                speaker=Urza Afalas
                message= _ "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= _ "Nevertheless, we will have vengeance for Mebrin!!"
            [/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=1
            [/variable]

            [then]
                {VARIABLE max_undead_spawns $turn_number}
                {VARIABLE_OP max_undead_spawns sub 2}

                [if]
                    [variable]
                        name=max_undead_spawns
                        greater_than=4
                    [/variable]

                    [then]
                        {VARIABLE max_undead_spawns 5}
                    [/then]

                    [else]
                        {VARIABLE max_undead_spawns $turn_number}
                    [/else]
                [/if]

                {VARIABLE got_a_quick_one_this_turn no}

                {VARIABLE i 0}
                [while]
                    [variable]
                        name=i
                        less_than=$max_undead_spawns
                    [/variable]

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

                        # If there's already been a quick unit (ghost or 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 "Ghost,Bone Shooter,Revenant,Blood Bat,Necrophage"}
#else
                                {VARIABLE_OP undead_spawn_type rand "Wraith,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 "Ghost,Wraith,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]

                        {VARIABLE_OP i add 1}
                    [/do]
                [/while]

                {CLEAR_VARIABLE i,max_undead_spawns,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= _ "Now I pay for my crimes. But how many others will also suffer for them?"
        [/message]
        [kill]
            x,y=$x1,$y1
            animate=yes
        [/kill]
        [endlevel]
            result=defeat
        [/endlevel]
    [/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= _ "Thank heaven we have escaped that awful forest!"
        [/message]
        [message]
            speaker=Sir Gerrick
            message= _ "Indeed, Afalas, you’ve earned my trust today."
        [/message]
        [message]
            speaker=Urza Afalas
            message= _ "I can only hope for mercy when I return to Westin."
        [/message]

        [message]
            speaker=narrator
            message= _ "But there was a dark cloud looming behind Deoran and his men..."
            image=wesnoth-icon.png
        [/message]

        {CLEAR_VARIABLE secret_path}

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