#textdomain wesnoth-ei

#define SCENARIO_TURN_LIMIT
45 #enddef

[scenario]
    id=13_Spoils_of_War
    name= _ "Spoils of War"
    map_file=13_Spoils_of_War.map
    turns=45
    next_scenario=14_The_Drowned_Plains

    #
    # note that Mal-Yrna (or Mal-Mana)'s castle is positioned so that the bats could easily fly either north or west.
    # The AI sends them towards whichever player units are closest, giving the player a lot of control
    #

    {DEFAULT_SCHEDULE_DUSK} # so you fight the first deathblade during the day, making it very hard to lose to RNG

    {SCENARIO_MUSIC       casualties_of_war.ogg}
    {EXTRA_SCENARIO_MUSIC battle.ogg}

    [story]
        [part]
            title= _ "<b><span color='#ff0000'>Part III: Wesnoth at War</span></b>"
            music=silence.ogg
        [/part]
        [if]
            [variable]
                name=drakes_rebelling
                equals=yes
            [/variable]
            [then]
                [part]
                    title= _ "<b><span color='#ff0000'>Part III: Wesnoth at War</span></b>"
                    story= _ "As Gweddry, Owaec, and Dacyn fled across the Listra, a frenzy of shouts, screams, and roars broke out from behind. Neither the orcs nor the drakes attempted to give pursuit."
                [/part]
            [/then]
        [/if]
        [part]
            title= _ "<b><span color='#ff0000'>Part III: Wesnoth at War</span></b>"
            story= _ "Leaving the tumultuous wildlands far behind, the soldiers of Wesnoth prepared to cross the Great River once more and return to their homeland..."
            {EI_BIGMAP}
        [/part]
    [/story]
    {EI_TRACK {JOURNEY_13_NEW} }

    # tell the AIs to avoid blocking the prisoners
#define AVOID_NORTH_PATH
    [avoid]
        x=6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,21,22,22,22,23,24,25,26,26,27,28,28,28,28,28,29,29,29,29,29,30,31,32
        y=1,2,1,2, 2, 3, 3, 4, 4, 4, 3, 4, 4, 5, 5, 6, 7, 7, 8, 9,10,10,11,11,12,13,13,14,15,16,17,18,19,20,21,22,22,23,22
    [/avoid]
#enddef
#define AVOID_SOUTH_PATH
    [avoid]
        x= 1, 2, 3, 3, 4, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32
        y=23,22,22,21,20,19,19,18,19,18,18,18,19,19,20,20,21,21,21,21,21,21,21,21,21,21,21,21,22,22,22,22,23,22
    [/avoid]
#enddef
#define MARCH_PRISONER FROM_X FROM_Y TO_X TO_Y
    {VARIABLE unit_to_swap "none"}
    #     {MODIFY_UNIT (x,y={FROM_X},{FROM_Y}) resting no} # no rest healing; they're supposed to be marching
    [if] # if the destination is occupied, try to clear it
        [have_unit]
            [not] # don't overwrite the gaoler. Can't use name because that might get translated (I think?)
                type=Deathblade
            [/not]
            side=2,3,4 # don't overwrite player units or prisoners
            x,y={TO_X},{TO_Y}
        [/have_unit]
        [then]
            # we try to avoid the paths, but sometimes AI units end up there anyway for some reason
            # so be prepared to swap those units out of the way if needed
            [store_unit]
                [filter]
                    x,y={TO_X},{TO_Y}
                [/filter]
                variable=unit_to_swap
                kill=yes
            [/store_unit]
        [/then]
    [/if]
    [if] # only move if the destination is vacant
        [have_unit]
            x,y={TO_X},{TO_Y}
        [/have_unit]
        [then]
        [/then]
        [else]
            [move_unit]
                side=5
                x,y={FROM_X},{FROM_Y}
                [or]
                    side=4
                    type=Deathblade
                    x,y={FROM_X},{FROM_Y}
                [/or]
                to_x,to_y={TO_X},{TO_Y}
                check_passability=no #ignore the impassable overlays
            [/move_unit]
        [/else]
    [/if]

    # unstore the unit we removed earlier
    [if]
        {VARIABLE_CONDITIONAL unit_to_swap not_equals none}
        [then]
            [unstore_unit]
                variable=unit_to_swap
                find_vacant=yes
            [/unstore_unit]
        [/then]
    [/if]
#enddef
#define MARCH_NORTH
    {MARCH_PRISONER 31 23 32 22}
    {MARCH_PRISONER 30 22 31 23}
    {MARCH_PRISONER 29 22 30 22}
    {MARCH_PRISONER 29 21 29 22}
    {MARCH_PRISONER 29 20 29 21}
    {MARCH_PRISONER 29 19 29 20}
    {MARCH_PRISONER 29 18 29 19}
    {MARCH_PRISONER 28 17 29 18}
    {MARCH_PRISONER 28 16 28 17}
    {MARCH_PRISONER 28 15 28 16}
    {MARCH_PRISONER 28 14 28 15}
    {MARCH_PRISONER 28 13 28 14}
    {MARCH_PRISONER 27 13 28 13}
    {MARCH_PRISONER 26 12 27 13}
    {MARCH_PRISONER 26 11 26 12}
    {MARCH_PRISONER 25 11 26 11}
    {MARCH_PRISONER 24 10 25 11}
    {MARCH_PRISONER 23 10 24 10}
    {MARCH_PRISONER 22 9 23 10}
    {MARCH_PRISONER 22 8 22 9}
    {MARCH_PRISONER 22 7 22 8}
    {MARCH_PRISONER 21 7 22 7}
    {MARCH_PRISONER 21 6 21 7}
    {MARCH_PRISONER 20 5 21 6}
    {MARCH_PRISONER 19 5 20 5}
    {MARCH_PRISONER 18 4 19 5}
    {MARCH_PRISONER 17 4 18 4}
    {MARCH_PRISONER 16 3 17 4}
    {MARCH_PRISONER 15 4 16 3}
    {MARCH_PRISONER 14 4 15 4}
    {MARCH_PRISONER 13 4 14 4}
    {MARCH_PRISONER 12 3 13 4}
    {MARCH_PRISONER 11 3 12 3}
    {MARCH_PRISONER 10 2 11 3}
    {MARCH_PRISONER 9 2 10 2}
    {MARCH_PRISONER 8 1 9 2}
    {MARCH_PRISONER 7 2 8 1}
    {MARCH_PRISONER 6 1 7 2}
    {MARCH_PRISONER 5 1 6 1}
#enddef
#define MARCH_SOUTH
    {MARCH_PRISONER 31 23 32 22}
    {MARCH_PRISONER 30 22 31 23}
    {MARCH_PRISONER 29 22 30 22}
    {MARCH_PRISONER 28 22 29 22}
    {MARCH_PRISONER 27 22 28 22}
    {MARCH_PRISONER 26 21 27 22}
    {MARCH_PRISONER 25 21 26 21}
    {MARCH_PRISONER 24 21 25 21}
    {MARCH_PRISONER 23 21 24 21}
    {MARCH_PRISONER 22 21 23 21}
    {MARCH_PRISONER 21 21 22 21}
    {MARCH_PRISONER 20 21 21 21}
    {MARCH_PRISONER 19 21 20 21}
    {MARCH_PRISONER 18 21 19 21}
    {MARCH_PRISONER 17 21 18 21}
    {MARCH_PRISONER 16 21 17 21}
    {MARCH_PRISONER 15 21 16 21}
    {MARCH_PRISONER 14 20 15 21}
    {MARCH_PRISONER 13 20 14 20}
    {MARCH_PRISONER 12 19 13 20}
    {MARCH_PRISONER 11 19 12 19}
    {MARCH_PRISONER 10 18 11 19}
    {MARCH_PRISONER 9 18 10 18}
    {MARCH_PRISONER 8 18 9 18}
    {MARCH_PRISONER 7 19 8 18}
    {MARCH_PRISONER 6 18 7 19}
    {MARCH_PRISONER 5 19 6 18}
    {MARCH_PRISONER 4 19 5 19}
    {MARCH_PRISONER 4 20 4 19}
    {MARCH_PRISONER 3 21 4 20}
    {MARCH_PRISONER 3 22 3 21}
    {MARCH_PRISONER 2 22 3 22}
    {MARCH_PRISONER 1 23 2 22}
    {MARCH_PRISONER 1 24 1 23}
#enddef
#define EXECUTE_PRISONER X Y TYPE
    [animate_unit]
        flag=attack

        [filter]
            id=Mal-Yrna
        [/filter]

        [primary_attack]
            name=shadow wave
        [/primary_attack]
        hits=yes
        [facing]
            x,y={X},{Y}
        [/facing]
    [/animate_unit]
    {STORE_UNIT_VAR x,y={X},{Y} name corpse_name}
    {KILL x,y={X},{Y} ANIMATE=yes}
    [sound]
        name=zombie-hit-1.ogg
    [/sound]

    {NAMED_UNIT 4 {TYPE} {X} {Y} "" "$corpse_name" ()} {ANIMATE}
#enddef
#define EXECUTE_PRISONERS
    [kill] # kill instead of releasing the deathblade, to make things easier
        x,y=32,22
        side=4
        type=Deathblade
        animate=yes
    [/kill]
    {EXECUTE_PRISONER 31 23 {ON_DIFFICULTY "Ghoul"          "Ghoul" "Necrophage"}}
    {EXECUTE_PRISONER 30 22 {ON_DIFFICULTY "Walking Corpse" "Ghoul" "Ghoul"     }}
    {VARIABLE_OP prisoners_remaining sub 1}
#enddef

    #--------------------
    # WESNOTHIANS
    #--------------------
    # wmllint: validate-off
    [side]
        side=1
        controller=human
        gold=0
        team_name=good
        user_team_name= _ "Wesnothians"
        defeat_condition=never # since Terraent isn't a leader
        {FLAG_VARIANT loyalist}
        {CHARACTER_STATS_GWEDDRY}
    [/side]
    # wmllint: validate-on

    #--------------------
    # DEATH KNIGHT
    #--------------------
    [side]
        type=Death Knight
        id=Ducatithil
        name= _ "Ducatithil" # a dwarvish name, implying this is a death knight from Mal-Ravanal's knalgan campaign
        side=2
        canrecruit=yes
        controller=ai
        recruit=Skeleton,Revenant,Skeleton Archer,Bone Shooter
        team_name=undead
        user_team_name=_"Undead"
        {GOLD 120 120 120} # he doesn't attack; no point varying with difficulty
        income=8
        {FLAG_VARIANT undead}
        [ai]
            leader_value=0
        [/ai]
    [/side]
    {STARTING_VILLAGES 2 5}
    {SILENTLY_LIMIT_LEADER_MOVES 2 2}

    #--------------------
    # NECROMANCER (no recruiting)
    #--------------------
    [side]
        side=3
        color=teal
        type=Necromancer
        id=Mal-Yrna
        #po: female necromancer, will be renamed to Mal-Mana if Mal-Mana wasn't killed in a previous scenario
        name= _ "Mal-Yrna"
        canrecruit=yes
        controller=ai
        gender=female
        [modifications]
            {TRAIT_STRONG}
            {TRAIT_RESILIENT}
        [/modifications]
        team_name=undead
        user_team_name=_"Undead"
        gold=0
        income=-2
        {FLAG_VARIANT undead}
        [ai]
            leader_value=0
        [/ai]
        {GENERIC_UNIT 3 (Dark Adept) 25 23}
        {GENERIC_UNIT 3 (Dark Adept) 34 17}
#ifndef EASY
        {GENERIC_UNIT 3 (Dark Adept) 38 21}
#endif
        {GENERIC_UNIT 3 (Dark Adept) 29 25}
#ifdef HARD
        {GENERIC_UNIT 3 (Dark Adept) 37 23}
#endif
        {GENERIC_UNIT 3 ({ON_DIFFICULTY (Ghoul) (Ghoul)      (Ghoul)     }) 33 25}
        {GENERIC_UNIT 3 ({ON_DIFFICULTY (Ghoul) (Ghoul)      (Necrophage)}) 33 17}
        {GENERIC_UNIT 3 ({ON_DIFFICULTY (Ghoul) (Necrophage) (Necrophage)}) 27 21}
    [/side]
    {STARTING_VILLAGES 3 5}
    {SILENTLY_LIMIT_LEADER_MOVES 3 1}
    [event]
        name=prestart
        [modify_unit]
            [filter]
                side=3
                [filter_wml]
                    upkeep=full
                [/filter_wml]
            [/filter]
            [object]
                duration=scenario
                [effect]
                    apply_to=loyal
                [/effect]
            [/object]
        [/modify_unit]
    [/event]
    [event] # keep gold at 0
        name=side 3 turn refresh
        first_time_only=no
        [modify_side]
            side=3
            gold=0
        [/modify_side]
    [/event]

    #--------------------
    # ATTACK WAVES
    #--------------------
    [side] # undead attacker side
        side=4
        color=teal
        no_leader=yes
        controller=ai
        team_name=undead
        user_team_name= _ "Undead"
        hidden=yes
        [ai]
            leader_value=0
        [/ai]
    [/side]
    [event] # keep gold at 0
        name=side 4 turn refresh
        first_time_only=no
        [modify_side]
            side=4
            gold=0
        [/modify_side]
    [/event]

    [event] # vary AI over time
        name=side 2 turn
        first_time_only=no
        {RESET_SIDE_AI 2,3 no -2 0.25} # grouping will interfere with predefined defense position
        {RESET_SIDE_AI 4 defensive 0.9 0.1} # defensive grouping, but aggressive attacks

        {MODIFY_SIDE_AI (2,3) ({AVOID_NORTH_PATH})}
        {MODIFY_SIDE_AI (2,3) ({AVOID_SOUTH_PATH})} # for some reason, south path escorts mess up when I give the avoids to side 4

        # side 2 (death knight) is permanently defensive
        {MODIFY_SIDE_AI (2) ({GOAL_LOCATION 99 (x,y=13,3)})}
        {MODIFY_SIDE_AI (2) ({GOAL_LOCATION 99 (x,y=11,4)})}
        {MODIFY_SIDE_AI (2) ({GOAL_LOCATION 99 (x,y=12,2)})}
        {MODIFY_SIDE_AI (2) ({GOAL_LOCATION 99 (x,y=13,2)})}
        {MODIFY_SIDE_AI (2) ({GOAL_LOCATION 99 (x,y=10,4)})}
        {MODIFY_SIDE_AI (2) ({GOAL_LOCATION 99 (x,y= 9,3)})}
        {MODIFY_SIDE_AI (2) ({GOAL_LOCATION 99 (x,y=21,1)})}

        # side 3 (necromancer) is defensive, but will rush to Mal-Yrna's defense if enemies get close
        [if]
            [have_unit]
                side=1
                x=24,   25,   26,   27,   28,   29,   30,   31,   32-99
                y=23-99,23-99,21-99,21-99,20-99,18-99,17-99,18-99,17-99
            [/have_unit]

            [then]
            [/then]
            [else]
                {MODIFY_SIDE_AI (3) ({GOAL_LOCATION 99 (x,y=30,17)})}
                {MODIFY_SIDE_AI (3) ({GOAL_LOCATION 88 (x,y=33,17)})}
                {MODIFY_SIDE_AI (3) ({GOAL_LOCATION 88 (x,y=34,17)})}
                {MODIFY_SIDE_AI (3) ({GOAL_LOCATION 99 (x,y=25,23)})}
                {MODIFY_SIDE_AI (3) ({GOAL_LOCATION 99 (x,y=27,21)})}
                {MODIFY_SIDE_AI (3) ({GOAL_LOCATION 11 (x,y=35,22)})}
                {MODIFY_SIDE_AI (3) ({GOAL_LOCATION 11 (x,y=38,21)})}
            [/else]
        [/if]
        {MODIFY_SIDE_AI (3) (
            [avoid] # always stay out of the water (note that it's possible to move to-and-from the sandbar island without stopping in water)
                terrain=Ww,Wo
            [/avoid]
        )}
    [/event]

    #--------------------
    # PRISONERS
    #--------------------
    [side]
        side=5
        no_leader=yes
        controller=null
        team_name=good,undead
        user_team_name= _ "Prisoners"
        color=white
        hidden=yes
    [/side]

    [event]
        name=prestart
        {CLEAR_VARIABLE drakes_rebelling}

        [if]
            {VARIABLE_CONDITIONAL mana_dead not_equals yes}
            [then]
                {MODIFY_UNIT (id=Mal-Yrna) name Mal-Mana}
            [/then]
        [/if]

        {PLACE_IMAGE scenery/village-human-burned1.png 39 5}
        {PLACE_IMAGE scenery/village-human-burned2.png 42 8}
        {PLACE_IMAGE scenery/village-human-burned3.png 47 9}
        {PLACE_IMAGE scenery/village-human-burned4.png 28 8}

        {PLACE_IMAGE scenery/village-human-burned2.png 17 22}
        {PLACE_IMAGE scenery/village-human-burned3.png 7 23}

        # remove Gweddry, since we won't be using him
        {PUT_TO_RECALL_LIST id=Gweddry}

        # prisoners and gaolers
        {NAMED_NOTRAIT_UNIT 5 (Spearman) 16 21 "Vaenyc" (_"Vaenyc")}
        [+unit]
            [modifications]
                {TRAIT_SURVIVOR}
            [/modifications]
        [/unit]
        [+unit]
            [modifications]
                {TRAIT_RESILIENT}
            [/modifications]
        [/unit]

        {NAMED_NOTRAIT_UNIT 5 (Javelineer) 17 21 "Vovan" (_"Vovan")}
        [+unit]
            [modifications]
                {TRAIT_SURVIVOR}
            [/modifications]
        [/unit]
        [+unit]
            [modifications]
                {TRAIT_INTELLIGENT}
            [/modifications]
        [/unit]

        {NAMED_UNIT       4 (Deathblade) 18 21 "Gaoler_Initial" (_"Gaoler") ()}
        [+unit]
            max_moves=0
        [/unit]

        {MODIFY_UNIT (side=5) status.slowed yes}

        {SET_LABEL 30 23 ( _ "Parthyn Guardpost")}

        {VARIABLE prisoners_remaining 6}
        [objectives]
            side=1
            [objective]
                description= _ "Rescue as many prisoners as possible (6 groups arrive soon)"
                condition=win
                [show_if]
                    {VARIABLE_CONDITIONAL prisoners_remaining equals 6}
                [/show_if]
            [/objective]
            [objective]
                description= _ "Rescue as many prisoners as possible (5 groups arrive soon)"
                condition=win
                [show_if]
                    {VARIABLE_CONDITIONAL prisoners_remaining equals 5}
                [/show_if]
            [/objective]
            [objective]
                description= _ "Rescue as many prisoners as possible (4 groups arrive soon)"
                condition=win
                [show_if]
                    {VARIABLE_CONDITIONAL prisoners_remaining equals 4}
                [/show_if]
            [/objective]
            [objective]
                description= _ "Rescue as many prisoners as possible (3 groups arrive soon)"
                condition=win
                [show_if]
                    {VARIABLE_CONDITIONAL prisoners_remaining equals 3}
                [/show_if]
            [/objective]
            [objective]
                description= _ "Rescue as many prisoners as possible (2 groups arrive soon)"
                condition=win
                [show_if]
                    {VARIABLE_CONDITIONAL prisoners_remaining equals 2}
                [/show_if]
            [/objective]
            [objective]
                description= _ "Rescue as many prisoners as possible (1 group arrives soon)"
                condition=win
                [show_if]
                    {VARIABLE_CONDITIONAL prisoners_remaining equals 1}
                [/show_if]
            [/objective]
            [objective]
                #po: Mal-Yrna, a female necromancer
                description= _ "Defeat the southern necromancer"
                condition=win
            [/objective]
            [objective]
                description= _ "Death of Owaec"
                condition=lose
                [show_if]
                    [have_unit]
                        id=Owaec
                    [/have_unit]
                [/show_if]
            [/objective]
            [objective]
                description= _ "Death of Terraent"
                condition=lose
                [show_if]
                    [have_unit]
                        id=Terraent
                    [/have_unit]
                [/show_if]
            [/objective]

            {TURNS_RUN_OUT}
            [gold_carryover]
                bonus=yes
                carryover_percentage=40
            [/gold_carryover]
            [note]
                #po: two prisoners and one jailer per gang
                description= _ "Prisoner gangs will alternate arriving on north and south paths."
            [/note]
            [note]
                #po: "enemy" in this case meaning "player's unit"
                description= _ "Bats prefer to fly towards the nearest enemy."
                [show_if]
                    [have_unit]
                        type=Vampire Bat,Blood Bat,Dread Bat
                    [/have_unit]
                [/show_if]
            [/note]
        [/objectives]
    [/event]

    [event]
        name=start
        [move_unit_fake]
            type=Blood Bat
            side=4
            x=36,31
            y=10,23
        [/move_unit_fake]
        [delay]
            time=500
        [/delay]

        {RECALL_XY Terraent 47 3}
        [if]
            [not]
                [have_unit]
                    id=Terraent
                [/have_unit]
            [/not]

            [then]
                {RECALL_XY Owaec 47 3}
            [/then]
        [/if]

        {MOVE_UNIT side=1 46 2}
        {MOVE_UNIT side=1 42 4}
        [message]
            id=Owaec,Terraent
            #po: TODO for 1.19? There's a cut-scene running up to this, but it's unclear what's happened until I read the WML and found that player's full starting roster is exactly one *horseman* for this scenario.
            #po: The fastest-moving of the player's units has been trying to catch up with a bat, but the bat has flow over the river to the Mal-Yrna / Mal-Mana.
            message= _ "Alas, I have failed in my mission! The undead scout has already reported back to its mistress."
        [/message]

        [delay]
            time=500
        [/delay]

        [message]
            speaker=Mal-Yrna
            #po: speaking to a bat
            message= _ "Minion, what’s this? Soldiers of Wesnoth approach from the north? That area should have been cleansed over a month ago."
        [/message]
        [message]
            speaker=Mal-Yrna
            message= _ "No matter. Bring in the next batch of prisoners. I have need of them a little sooner than expected."
        [/message]

        {REPEAT 50 ({MARCH_SOUTH})}
        {KILL x,y,side,type=32,22,4,Deathblade ANIMATE=yes} # kill before dialogue
        {PLACE_IMAGE items/bones.png 32 22}
        [message]
            speaker=Vovan
            message= _ "Wait a moment, can we talk about thi-"
        [/message]
        {EXECUTE_PRISONERS}
        [message]
            x,y=31,23
            #po: Vovan has been turned into a ghoul, and the other prisoner has been too.
            message= _ "I... Hunger..."
        [/message]

        [message]
            speaker=Owaec
            message= _ "By the Clans, she wields slaves as weapons! This injustice will not stand!"
        [/message]
        [message]
            speaker=Terraent
            message= _ "By all that is holy, she wields slaves as weapons! This evil will not stand!"
        [/message]

        [delay]
            time=1000
        [/delay]

        [fire_event]
            name=dwarvish_prisoners
        [/fire_event]

        [message]
            id=Owaec,Terraent
            #po: two dwarves, with a Deathblade as the jailer
            message= _ "More prisoners approach from the northwest! I have not the time to depart and return with Gweddry, or those dwarves will surely be slain."
        [/message]
        [message]
            speaker=Owaec
            message= _ "I may have failed my original mission, but in the name of the Clans, I will not fail these prisoners now!"
        [/message]
        [message]
            speaker=Terraent
            message= _ "I may have failed my original mission, but by the holy Light, I will not fail these prisoners now!"
        [/message]
    [/event]

    [event]
        name=turn 3

        [filter_condition]
            {VARIABLE_CONDITIONAL fork_06b  equals     yes}
            [and]
                {VARIABLE_CONDITIONAL mana_dead not_equals yes}
            [/and]
        [/filter_condition]

        [message]
            speaker=Mal-Yrna
            #po: the speaker was renamed to Mal-Mana at the start
            message= _ "Wait — I remember you! You’re one of the ones who fled from me last autumn near Soradoc!"
        [/message]
        [message]
            speaker=Mal-Yrna
            message= _ "This time I’ve traded my bats for ghouls. There’s no way you’ll escape now!"
        [/message]
        {VARIABLE mana_said_no_bats yes}
    [/event]

    #--------------------
    # FORCE OWAEC ACCURACY
    #--------------------
    # Owaec's hammer is powerful but unreliable, and missing several consecutive attacks can end the mission.
    # If he misses one hammer attack, force him to hit the next hammer attack.
    [event]
        name=attacker misses
        first_time_only=no

        [filter]
            id=Owaec
        [/filter]

        [filter_condition]
            {VARIABLE_CONDITIONAL weapon.type equals impact}
        [/filter_condition]

        [event]
            name=pre attack

            [filter]
                id=Owaec
            [/filter]

            [filter_condition]
                {VARIABLE_CONDITIONAL weapon.type equals impact}
            [/filter_condition]

            [modify_unit]
                [filter]
                    id=Owaec
                [/filter]

                [object]
                    id=forced_cth
                    [effect]
                        apply_to=attack
                        set_accuracy=100
                    [/effect]
                [/object]
            [/modify_unit]
            [event]
                name=attack end
                [remove_object]
                    object_id=forced_cth
                [/remove_object]
            [/event]
        [/event]
    [/event]

    #--------------------
    # MARCH PRISONERS
    #--------------------
    [event]
        name=side 4 turn
        first_time_only=no
        {REPEAT 3 ({MARCH_NORTH})}
        {REPEAT 3 ({MARCH_SOUTH})}
        [if]
            [have_unit]
                x,y=31,23
                side=5
            [/have_unit]
            [then]
                {EXECUTE_PRISONERS}
            [/then]
        [/if]
    [/event]

    # note that these guys can attack immediately, to deter spawncamping
#define GAOLER_ESCORT TYPE CORPSE_VARIATION X Y GAOLER_ID ESCORT_ID
    {VARIABLE deleteme {TYPE}}
    [if]
        {VARIABLE_CONDITIONAL deleteme not_equals none}
        [then]
            {NAMED_UNIT 4 ({TYPE}) {X} {Y} "{ESCORT_ID}" "" ()} {FACING se} {VARIATION {CORPSE_VARIATION}}
            [micro_ai]
                side=4
                ai_type=zone_guardian
                ca_id=mai_{ESCORT_ID}
                action=add
                id={ESCORT_ID}
                [filter_location]
                    radius=2
                    [filter]
                        id={GAOLER_ID}
                    [/filter]
                [/filter_location]
                [filter_location_enemy]
                    radius=6
                    [filter]
                        id={GAOLER_ID}
                    [/filter]
                [/filter_location_enemy]
            [/micro_ai]
            [event]
                name=die

                [filter]
                    id={GAOLER_ID}
                [/filter]

                [micro_ai]
                    side=4
                    action=delete
                    ai_type=zone_guardian
                    ca_id=mai_{ESCORT_ID}
                [/micro_ai]
            [/event]
        [/then]
    [/if]
    {CLEAR_VARIABLE deleteme}
#enddef

    #--------------------
    # NORTH - DWARVISH PRISONERS
    #--------------------
    [event]
        name=dwarvish_prisoners
        {NAMED_NOTRAIT_UNIT 5 (Dwarvish Thunderguard) 5 1 "Nargaril" (_"Nargaril")}
        [+unit]
            [status]
                slowed=yes
            [/status]
        [/unit]
        [+unit]
            [modifications]
                {TRAIT_SURVIVOR}
            [/modifications]
        [/unit]
        [+unit]
            [modifications]
                {TRAIT_HEALTHY}
            [/modifications]
        [/unit]

        {NAMED_NOTRAIT_UNIT 5 (Dwarvish Runesmith)    6 1 "Urmarol"  (_"Urmarol")}
        [+unit]
            [status]
                slowed=yes
            [/status]
        [/unit]
        [+unit]
            [modifications]
                {TRAIT_SURVIVOR}
            [/modifications]
        [/unit]
        [+unit]
            [modifications]
                {TRAIT_STRONG}
            [/modifications]
        [/unit]

        {NAMED_UNIT         4 (Deathblade)            7 2 "Gaoler_Dwarves" _"Gaoler" ()}
        [+unit]
            max_moves=0
        [/unit]

        [message]
            speaker=Nargaril
            #po: to a Deathblade
            message= _ "Get yer filthy chains off my wrists, ye pile o’ bones!"
        [/message]
    [/event]
    [event]
        name=die
        [filter]
            id=Gaoler_Dwarves
        [/filter]
        [filter_second]
            side=1
        [/filter_second]
        [message]
            speaker=Urmarol
            #po: "Thank you kindly for freeing us! My runesmithing arts are not meant to be used outside of knalga, but I think now is an exception. I just hope the East Gate didn't fare too bad after I was captured..."
            message= _ "Thank ye kindly fer freein’ us! Me runesmithin’ arts are not meant ta be used outside o’ Knalga, but methinks now is an exception. I just hope tha East Gate dinnae fare’n too bad after I were captured..."
        [/message]
        {MODIFY_UNIT id=Nargaril side 1}
        {MODIFY_UNIT id=Urmarol side 1}
        {VARIABLE_OP prisoners_remaining sub 1}
        [fire_event]
            name=summon_bats
        [/fire_event]
        [show_objectives]
        [/show_objectives]
    [/event]

    #--------------------
    # SOUTH - LOYALIST PRISONERS
    #--------------------
    [event]
        name=side 4 turn 8
        {NAMED_NOTRAIT_UNIT 5 (Mage)       1 24 "Vinreddoc" (_"Vinreddoc")}
        [+unit]
            [status]
                slowed=yes
            [/status]
        [/unit]
        [+unit]
            [modifications]
                {TRAIT_SURVIVOR}
            [/modifications]
        [/unit]
        [+unit]
            [modifications]
                {TRAIT_INTELLIGENT}
            [/modifications]
        [/unit]

        {NAMED_NOTRAIT_UNIT 5 (Longbowman) 1 23 "Tunreoryr" (_"Tunreoryr")}
        [+unit]
            [status]
                slowed=yes
            [/status]
        [/unit]
        [+unit]
            [modifications]
                {TRAIT_SURVIVOR}
            [/modifications]
        [/unit]
        [+unit]
            [modifications]
                {TRAIT_QUICK}
            [/modifications]
        [/unit]

        {NAMED_UNIT         4 (Deathblade) 2 22 "Gaoler_Loyalists" _"Gaoler" ()}
        [+unit]
            max_moves=0
        [/unit]

        [message]
            speaker=Vinreddoc
            #po: spoken between two prisoners, "them" being the undead forces
            message= _ "How are there so many of them? Is this the end of all Wesnoth?"
        [/message]
        [message]
            speaker=Tunreoryr
            #po: spoken between two prisoners
            message= _ "Don’t worry. I’m sure the Crown has a plan..."
        [/message]
    [/event]
    [event]
        name=die
        [filter]
            id=Gaoler_Loyalists
        [/filter]
        [filter_second]
            side=1
        [/filter_second]
        [message]
            speaker=Vinreddoc
            #po: Vinreddoc is a mage, but with random gender
            message= _ "Thank you! When the undead overran the academy I thought I was dead for sure. I just wonder what has happened to my friends..."
        [/message]
        [message]
            speaker=Terraent
            message= _ "Take heart! As long as there is Light, hope always remains."
        [/message]
        {MODIFY_UNIT id=Vinreddoc side 1}
        {MODIFY_UNIT id=Tunreoryr side 1}
        {VARIABLE_OP prisoners_remaining sub 1}
        [fire_event]
            name=summon_bats
        [/fire_event]
        [show_objectives]
        [/show_objectives]
    [/event]

    #--------------------
    # NORTH - ORCISH PRISONERS
    #--------------------
    [event]
        name=side 4 turn 13
        {NAMED_NOTRAIT_UNIT 5 (Orcish Archer) 5 1 "Ugzush" (_"Ugzush")}
        [+unit]
            [status]
                slowed=yes
            [/status]
        [/unit]
        [+unit]
            [modifications]
                {TRAIT_SURVIVOR}
            [/modifications]
        [/unit]
        [+unit]
            [modifications]
                {TRAIT_INTELLIGENT}
            [/modifications]
        [/unit]

        {NAMED_NOTRAIT_UNIT 5 (Orcish Slayer) 6 1 "Hibro"  (_"Hibro")}
        [+unit]
            [status]
                slowed=yes
            [/status]
        [/unit]
        [+unit]
            [modifications]
                {TRAIT_SURVIVOR}
            [/modifications]
        [/unit]
        [+unit]
            [modifications]
                {TRAIT_RESILIENT}
            [/modifications]
        [/unit]

        {NAMED_UNIT         4 (Deathblade)    7 2 "Gaoler_Orcs" _"Gaoler" ()}
        [+unit]
            max_moves=0
        [/unit]

        {GAOLER_ESCORT ({ON_DIFFICULTY "none" "Walking Corpse" "Soulless"}) gryphon 7 1 "Gaoler_Orcs" "Escort1_Orcs"}
        {GAOLER_ESCORT ({ON_DIFFICULTY "none" "Walking Corpse" "Soulless"}) saurian 7 1 "Gaoler_Orcs" "Escort2_Orcs"}

        [message]
            speaker=Hibro
            message= _ "I’ll take you all on! As soon as I get out of these chains..."
        [/message]
        [message]
            speaker=Gaoler_Orcs
            message= _ "Silence. Orcs. March."
        [/message]
    [/event]
    [event]
        name=die
        [filter]
            id=Gaoler_Orcs
        [/filter]
        [filter_second]
            side=1
        [/filter_second]
        [message]
            speaker=Ugzush
            #po: he's just been rescued
            message= _ "Cruddy undead think they can chain me up! I’ll grind their bones into dust!"
        [/message]
        {MODIFY_UNIT id=Ugzush side 1}
        {MODIFY_UNIT id=Hibro side 1}
        {VARIABLE_OP prisoners_remaining sub 1}
        [fire_event]
            name=summon_bats
        [/fire_event]
        [show_objectives]
        [/show_objectives]
    [/event]

    #--------------------
    # SOUTH - DARK ADEPT PRISONERS
    #--------------------
    [event]
        name=side 4 turn 18
        {NAMED_NOTRAIT_UNIT 5 (Pikeman)    1 23 "Syner"    (_"Syner")}
        [+unit]
            [status]
                slowed=yes
            [/status]
        [/unit]
        [+unit]
            [modifications]
                {TRAIT_SURVIVOR}
            [/modifications]
        [/unit]
        [+unit]
            [modifications]
                {TRAIT_QUICK}
            [/modifications]
        [/unit]

        [unit]
            side=5
            type=Dark Adept
            id=Gaennell
            #po: A female Dark Adept with portrait, can be converted to the player's side.
            #po: Appears in S01, where she's an apprentice of Mel Guthrak, and sometimes copies his motions (plays her animation) when he recruits.
            #po: She reappears in S13, where Terraent can recruit her to the player's side.
            name= _ "Gaennell"
            profile=portraits/gaennell-adept.webp
            x,y=1,24
            random_traits=no
            gender=female
            [status]
                slowed=yes
            [/status]
            [modifications]
                {TRAIT_SURVIVOR}
            [/modifications]
        [/unit]

        {NAMED_UNIT 4 (Deathblade) 2 22 "Gaoler_DarkAdept" _"Gaoler" ()}
        [+unit]
            max_moves=0
        [/unit]

        {GAOLER_ESCORT ({ON_DIFFICULTY "none" "Skeleton Archer" "Bone Shooter"   }) none 1 22 "Gaoler_DarkAdept" "Escort1_DarkAdept"}
        {GAOLER_ESCORT ({ON_DIFFICULTY "none" "Skeleton Archer" "Skeleton Archer"}) none 2 23 "Gaoler_DarkAdept" "Escort2_DarkAdept"}

        [message]
            speaker=Gaennell
            #po: this Dark Adept is a prisoner too
            message= _ "I swear, it was an accident! Please, if you’ll just let me talk to Mel Guthrak, I’m sure this can all be straightened out."
        [/message]
        [message]
            speaker=Syner
            #po: to Gaennell
            message= _ "Your time is done, necromancer. If I have one small comfort, it’s that I’ll get to watch you die alongside me."
        [/message]
    [/event]
    [event]
        name=die
        [filter]
            id=Gaoler_DarkAdept
        [/filter]
        [filter_second]
            side=1
        [/filter_second]
        [message]
            speaker=Syner
            #po: speaking of Gaennell
            message= _ "I’m free! Now I’ll have the pleasure of killing this dark adept myself."
        [/message]

        [if]
            [have_unit]
                id=Terraent
            [/have_unit]

            # Terraent converts and recruits the dark adept
            [then]
                [message]
                    speaker=Terraent
                    #po: speaking to Syner about Gaennell
                    message= _ "Stay your hand, man of Wesnoth! A killing in cold blood is no justice and revenge will bring you no peace."
                [/message]
                [message]
                    speaker=Syner
                    #po: speaking of Gaennell
                    message= _ "Are you mad? That woman is a necromancer!"
                [/message]
                [message]
                    speaker=Gaennell
                    message= _ "...you might as well. They think I’m a traitor. I’m as good as dead already."
                [/message]
                [message]
                    speaker=Terraent
                    message= _ "Then the choice is yours. Flee and face the world alone, or forswear your dark arts and join me to protect all that yet remains holy in this good world."
                [/message]
                [message]
                    speaker=Gaennell
                    message= _ "..."
                [/message]
                [delay]
                    time=1000
                [/delay]

                [message]
                    speaker=Gaennell
                    message= _ "Okay. No more necromancy. I’ll join you."
                [/message]
                [modify_unit]
                    [filter]
                        id=Gaennell
                    [/filter]

                    side=1
                    alignment=liminal
                    advances_to=Shadow Mage
                    upkeep=loyal
                    [modifications]
                        {TRAIT_LOYAL}
                        {TRAIT_SURVIVOR}
                    [/modifications]
                [/modify_unit]
                [set_achievement]
                    content_for=eastern_invasion
                    id=ei_S13
                [/set_achievement]
            [/then]

            # if no Terraent, Syner kills her
            [else]
                [animate_unit]
                    [filter]
                        id=Syner
                    [/filter]
                    flag=attack
                    hits=yes
                    [primary_attack]
                        range=melee
                    [/primary_attack]
                    [facing]
                        [filter]
                            id=Gaennell
                        [/filter]
                    [/facing]
                [/animate_unit]

                {KILL id=Gaennell ANIMATE=yes}
                {MODIFY_UNIT id=Syner experience 8}
                [message]
                    speaker=Owaec
                    #po: speaking to Syner, who has just killed Gaennell
                    message= _ "And so justice has been served! Such is the fate of all evildoers."
                [/message]
            [/else]
        [/if]

        {MODIFY_UNIT id=Syner side 1}
        {VARIABLE_OP prisoners_remaining sub 1}
        [fire_event]
            name=summon_bats
        [/fire_event]
        [show_objectives]
        [/show_objectives]
    [/event]

    #--------------------
    # NORTH - MERFOLK PRISONERS
    #--------------------
    [event]
        name=side 4 turn 22

        {NAMED_NOTRAIT_UNIT 5 (Merman Citizen)    5 1 "Scyla" (_"Scyla")}
        [+unit]
            [status]
                slowed=yes
            [/status]
        [/unit]
        [+unit]
            [modifications]
                {TRAIT_SURVIVOR}
            [/modifications]
        [/unit]
        [+unit]
            [modifications]
                {TRAIT_STRONG}
            [/modifications]
        [/unit]

        {NAMED_NOTRAIT_UNIT 5 (Mermaid Priestess) 6 1 "Mame"  (_"Mame")}
        [+unit]
            [status]
                slowed=yes
            [/status]
        [/unit]
        [+unit]
            [modifications]
                {TRAIT_SURVIVOR}
            [/modifications]
        [/unit]
        [+unit]
            [modifications]
                {TRAIT_RESILIENT}
            [/modifications]
        [/unit]

        {NAMED_UNIT       4 (Deathblade) 7 2 "Gaoler_Merfolk" _"Gaoler" ()}
        [+unit]
            max_moves=0
        [/unit]

        {GAOLER_ESCORT ({ON_DIFFICULTY "none" "Soulless"       "Soulless"      }) swimmer 4 1 "Gaoler_Merfolk" "Escort1_Merfolk"}
        {GAOLER_ESCORT ({ON_DIFFICULTY "none" "Walking Corpse" "Soulless"      }) swimmer 4 1 "Gaoler_Merfolk" "Escort2_Merfolk"}
        {GAOLER_ESCORT ({ON_DIFFICULTY "none" "Walking Corpse" "Soulless"      }) swimmer 6 2 "Gaoler_Merfolk" "Escort3_Merfolk"}
        {GAOLER_ESCORT ({ON_DIFFICULTY "none" "Walking Corpse" "Walking Corpse"}) swimmer 6 2 "Gaoler_Merfolk" "Escort4_Merfolk"}

        [message]
            speaker=Gaoler_Merfolk
            message= _ "March. Faster."
        [/message]
        [message]
            speaker=Mame
            #po: a Mermaid Priestess as prisoner, the line ends with the jailer hitting her
            message= _ "We’re going as fast as we can! Maybe if you’d let us swim-"
        [/message]
        [animate_unit]
            flag=attack
            [filter]
                id=Gaoler_Merfolk
            [/filter]
            [primary_attack]
                name=axe
            [/primary_attack]
            hits=yes
            [facing]
                x,y=6,1
            [/facing]
        [/animate_unit]
        [message]
            speaker=Gaoler_Merfolk
            message= _ "March. Faster."
        [/message]
    [/event]
    [event]
        name=die
        [filter]
            id=Gaoler_Merfolk
        [/filter]
        [filter_second]
            side=1
        [/filter_second]
        [message]
            speaker=Mame
            message= _ "Thanks stranger! I do not recognize these lands, but I do recognize a kind heart when I meet one. Should I ever find my way home to the coast, I will be sure to tell my kinsmen of your actions here."
        [/message]
        {MODIFY_UNIT id=Scyla side 1}
        {MODIFY_UNIT id=Mame side 1}
        {VARIABLE_OP prisoners_remaining sub 1}
        [fire_event]
            name=summon_bats
        [/fire_event]
        [message]
            id=Owaec,Terraent
            #po: "we" being the speaker and the prisoners that they've rescued
            message= _ "Now we come for you, necromancer!"
        [/message]
        [show_objectives]
        [/show_objectives]
    [/event]

    #--------------------
    # ESCALATING ATTACK WAVES
    #--------------------
#define RECRUIT_UNIT SIDE TYPE X Y
    {VARIABLE deleteme {TYPE}}
    [if]
        {VARIABLE_CONDITIONAL deleteme not_equals none}
        [then]
            [animate_unit]
                [filter]
                    id=Mal-Yrna
                [/filter]
                flag=attack
                hits=yes
                [primary_attack]
                    name=shadow wave
                [/primary_attack]
                [facing]
                    x,y={X},{Y}
                [/facing]
            [/animate_unit]

            {GENERIC_UNIT {SIDE} ({TYPE}) {X} {Y}} {ANIMATE}
        [/then]
    [/if]
    {CLEAR_VARIABLE deleteme}
#enddef

    [event]
        name=summon_bats

        [filter_condition]
            [have_unit]
                id=Mal-Yrna
            [/have_unit]
        [/filter_condition]

        [if]
            {VARIABLE_CONDITIONAL mana_said_no_bats equals yes}
            [then]
                [message]
                    speaker=Mal-Yrna
                    #po: each bat-summoning event is triggered by the player rescuing prisoners
                    message= _ "Of course, just because I said I wouldn’t summon bats doesn’t mean I can’t..."
                [/message]
            [/then]

            [else]
                [message]
                    speaker=Mal-Yrna
                    #po: each bat-summoning event is triggered by the player rescuing prisoners
                    message= _ "My prisoners!! You’ll pay for that..."
                [/message]
            [/else]
        [/if]
        {RECRUIT_UNIT 4 ({ON_DIFFICULTY "Vampire Bat" "Vampire Bat" "Vampire Bat"}) 31 23}
        {RECRUIT_UNIT 4 ({ON_DIFFICULTY "none"        "Vampire Bat" "Vampire Bat"}) 30 22}
        {RECRUIT_UNIT 4 ({ON_DIFFICULTY "none"        "none"        "Vampire Bat"}) 29 23}
        [message]
            speaker=Mal-Yrna
            #po: this is a common misquote from the wizard of oz, she's talking to her bats
            message= _ "Fly, my pretties, fly!"
        [/message]
        [show_objectives]
        [/show_objectives]

        [event]
            name=summon_bats

            [filter_condition]
                [have_unit]
                    id=Mal-Yrna
                [/have_unit]
            [/filter_condition]

            [delay]
                time=500
            [/delay]

            [if]
                [have_unit]
                    id=Terraent
                [/have_unit]

                [then]
                    [message]
                        speaker=Mal-Yrna
                        #po: to Terraent. She's summoning bats.
                        message= _ "Not again! Do not test me, paladin of Wesnoth..."
                    [/message]
                [/then]

                [else]
                    [message]
                        speaker=Mal-Yrna
                        #po: to Owaec. She's summoning bats.
                        #po: The text is an allusion to drowned plains, which the next scenario will reveal to be the current state of the Horse Clans' beloved plains.
                        message= _ "Not again! Do not test me, lord of the swamps..."
                    [/message]
                [/else]
            [/if]
            {RECRUIT_UNIT 4 ({ON_DIFFICULTY "Blood Bat" "Blood Bat"   "Blood Bat"  }) 31 23}
            {RECRUIT_UNIT 4 ({ON_DIFFICULTY "none"      "Vampire Bat" "Vampire Bat"}) 30 22}
            {RECRUIT_UNIT 4 ({ON_DIFFICULTY "none"      "none"        "Vampire Bat"}) 29 23}

            [event]
                name=summon_bats

                [filter_condition]
                    [have_unit]
                        id=Mal-Yrna
                    [/have_unit]
                [/filter_condition]

                [delay]
                    time=500
                [/delay]

                [message]
                    speaker=Mal-Yrna
                    #po: More prisoners rescued, more bats get summoned.
                    message= _ "You are starting to become rather irritating."
                [/message]
                {RECRUIT_UNIT 4 ({ON_DIFFICULTY "none"      "Vampire Bat" "Vampire Bat"}) 31 23}
                {RECRUIT_UNIT 4 ({ON_DIFFICULTY "Blood Bat" "Blood Bat"   "Blood Bat"  }) 30 22}
                {RECRUIT_UNIT 4 ({ON_DIFFICULTY "none"      "Vampire Bat" "Vampire Bat"}) 29 23}
                {RECRUIT_UNIT 4 ({ON_DIFFICULTY "none"      "none"        "Vampire Bat"}) 31 22}

                [event]
                    name=summon_bats

                    [filter_condition]
                        [have_unit]
                            id=Mal-Yrna
                        [/have_unit]
                    [/filter_condition]

                    [delay]
                        time=500
                    [/delay]

                    [message]
                        speaker=Mal-Yrna
                        message= _ "Enough! My bats will feast on your blood!"
                    [/message]
                    {RECRUIT_UNIT 4 ({ON_DIFFICULTY "Vampire Bat" "Blood Bat"   "Blood Bat"  }) 31 23}
                    {RECRUIT_UNIT 4 ({ON_DIFFICULTY "Vampire Bat" "Vampire Bat" "Vampire Bat"}) 30 22}
                    {RECRUIT_UNIT 4 ({ON_DIFFICULTY "none"        "Vampire Bat" "Vampire Bat"}) 29 23}
                    {RECRUIT_UNIT 4 ({ON_DIFFICULTY "none"        "Vampire Bat" "Vampire Bat"}) 31 24}
                    {RECRUIT_UNIT 4 ({ON_DIFFICULTY "none"        "none"        "Blood Bat"  }) 31 22}

                    [event]
                        name=summon_bats

                        [filter_condition]
                            [have_unit]
                                id=Mal-Yrna
                            [/have_unit]
                        [/filter_condition]

                        [delay]
                            time=500
                        [/delay]

                        [message]
                            speaker=Mal-Yrna
                            message= _ "Why won’t you just die?!"
                        [/message]
                        {RECRUIT_UNIT 4 ({ON_DIFFICULTY "Vampire Bat" "Vampire Bat" "Vampire Bat"}) 31 23}
                        {RECRUIT_UNIT 4 ({ON_DIFFICULTY "Blood Bat"   "Blood Bat"   "Blood Bat"  }) 30 22}
                        {RECRUIT_UNIT 4 ({ON_DIFFICULTY "none"        "Vampire Bat" "Vampire Bat"}) 29 23}
                        {RECRUIT_UNIT 4 ({ON_DIFFICULTY "none"        "Vampire Bat" "Vampire Bat"}) 31 24}
                        {RECRUIT_UNIT 4 ({ON_DIFFICULTY "none"        "none"        "Blood Bat"  }) 31 22}
                        {RECRUIT_UNIT 4 ({ON_DIFFICULTY "none"        "none"        "Vampire Bat"}) 29 22}
                    [/event]
                [/event]
            [/event]
        [/event]
    [/event]

    # end the scenario if Terraent dies, since he's more-or-less your leader
    [event]
        name=die
        [filter]
            id=Terraent
        [/filter]
        [endlevel]
            result=defeat
        [/endlevel]
    [/event]

    #
    # victory if Mal-Yrna dies
    #
#define RECALL_LEVELED_UNIT X Y
    [store_unit]
        [filter]
            side=1
            x,y=recall,recall
            level=2-99 # prefer leveled units, since the player is more likely to recognize those
        [/filter]
        kill=no
        variable=to_recall
    [/store_unit]
    [if]
        {VARIABLE_CONDITIONAL to_recall.length greater_than 0}
        [then]
        [/then]

        [else]
            [store_unit]
                [filter]
                    side=1
                    x,y=recall,recall
                [/filter]

                kill=no
                variable=to_recall
            [/store_unit]
        [/else]
    [/if]
    {RECALL_XY $to_recall[0].id {X} {Y}}
    {CLEAR_VARIABLE to_recall}
#enddef
    [event]
        name=last breath

        [filter]
            id=Mal-Yrna
        [/filter]

        {RECALL_XY Gweddry 47 3}
        {RECALL_XY Owaec 47 4}
        {RECALL_XY Dacyn 45 2}
        #         {MODIFY_UNIT id=Dacyn halo ""}

        {RECALL_LEVELED_UNIT 44 1}
        {RECALL_LEVELED_UNIT 44 2}
        {RECALL_LEVELED_UNIT 46 3}
        {RECALL_LEVELED_UNIT 44 4}

        [if]
            [have_unit]
                id=Terraent
            [/have_unit]

            [then]
                [message]
                    speaker=Gweddry
                    #po: victory event, and the rest of the player's units catch up
                    message= _ "Hail, Terraent! We feared the worst when you did not return, yet here I find you alive and well!"
                [/message]
            [/then]

            [else]
                [message]
                    speaker=Gweddry
                    #po: victory event, and the rest of the player's units catch up
                    message= _ "Hail, Owaec! We feared the worst when you did not return, yet here I find you alive and well!"
                [/message]
            [/else]
        [/if]

        [if]
            [have_unit]
                id=Terraent
            [/have_unit]

            [then]
                [message]
                    speaker=Terraent
                    message= _ "Indeed! The Light blessed me with an opportunity to save many souls and I could not decline! Now the crossing is clear, and we may return to see what has become of our homeland."
                [/message]
            [/then]

            [else]
                [message]
                    speaker=Owaec
                    message= _ "Huzzah! Today has been a great victory! I have rescued a great many prisoners and defeated a foul necromancer! Now the crossing is clear, and we may return to see what has become of our homeland."
                [/message]
            [/else]
        [/if]

        [message]
            speaker=Mal-Yrna
            message= _ "You’re too late! Wesnoth is... already..."
        [/message]
    [/event]
    [event]
        name=die

        [filter]
            id=Mal-Yrna
        [/filter]

        {KILL id=Gaoler_Dwarves   FIRE_EVENT=yes}
        {KILL id=Gaoler_Loyalists FIRE_EVENT=yes}
        {KILL id=Gaoler_Orcs      FIRE_EVENT=yes}
        {KILL id=Gaoler_DarkAdept FIRE_EVENT=yes}
        {KILL id=Gaoler_Merfolk   FIRE_EVENT=yes}

        {CLEAR_VARIABLE unit_to_swap,prisoners_remaining,corpse_name,}
        {CLEAR_VARIABLE mana_dead,mana_said_no_bats}
        [endlevel]
            result=victory
            bonus=yes
            {NEW_GOLD_CARRYOVER 40}
        [/endlevel]
    [/event]

    [event]
        name=side 1 turn {SCENARIO_TURN_LIMIT} end
        [fire_event]
            name=time over
        [/fire_event]
    [/event]
    [event]
        name=time over
        {RECALL_XY Gweddry 47 3}
        {RECALL_XY Owaec 47 4}
        {RECALL_XY Dacyn 45 2}
        {MODIFY_UNIT id=Dacyn halo ""}

        {RECALL_LEVELED_UNIT 44 1}
        {RECALL_LEVELED_UNIT 44 2}
        {RECALL_LEVELED_UNIT 46 3}
        {RECALL_LEVELED_UNIT 44 4}

        [if]
            [have_unit]
                id=Terraent
            [/have_unit]

            [then]
                [message]
                    speaker=Dacyn
                    message= _ "Hail, Terraent! We feared the worst when you did not return, yet here I find you alive and well! But who is this you are fighting?"
                [/message]
            [/then]

            [else]
                [message]
                    speaker=Dacyn
                    message= _ "Hail, Owaec! We feared the worst when you did not return, yet here I find you alive and well! But who is this you are fighting?"
                [/message]
            [/else]
        [/if]
        [message]
            speaker=Mal-Yrna
            #po: she dispatches a messenger bat after this, leading to Dacyn saying all is lost
            message= _ "Can it be? Dacyn the mage? We thought you perished in the wild northlands, but now I find you alive and isolated from Wesnoth! I must inform Mal-Ravanal!"
        [/message]
        [animate_unit]
            [filter]
                id=Mal-Yrna
            [/filter]
            flag=attack
            hits=no
            [primary_attack]
                range=ranged
            [/primary_attack]
            [facing]
                [filter]
                    x,y=31,23
                [/filter]
            [/facing]
        [/animate_unit]

        [move_unit_fake]
            type=Vampire Bat
            side=4
            x=30,32,32
            y=23,25,26
        [/move_unit_fake]
        [message]
            speaker=Dacyn
            message= _ "Even if we triumph at this crossing, Mal-Ravanal will surely find and destroy us before we can reunite with Wesnoth..."
        [/message]
        [endlevel]
            result=defeat
        [/endlevel]
    [/event]

    {WESNOTH_DEFEAT}

    {HERODEATH_GWEDDRY}
    {HERODEATH_DACYN}
    {HERODEATH_OWAEC}
    {HERODEATH_TERRAENT}
    {HERODEATH_DOLBURRAS}
    {HERODEATH_GRUG}
    {HERODEATH_HAHID}
    {HERODEATH_GAENNELL}
[/scenario]

#undef SCENARIO_TURN_LIMIT
