#textdomain wesnoth-ei

#
# this scenario is filled with "sleeping" undead
# when you stumble upon 1, it awakens some nearby buddies
# when you sight an enemy leader, they recruit, awaken sleepers, and charge at you
#
# the turn limit is very generous. A player with many veterans can finish quickly and have a large bonus for S16
# alternatively, maybe the player only has a few veterans (maybe they lost everything in S12 Evacuation)
#    then they can take their time, cap villages, farm XP off the swamp, etc.
#    and wait as long as possible to activate enemy leaders
#
# note that the map is based off of the penultimate HttT scenario, "Test of the Clans"
#

#define SCENARIO_TURN_LIMIT
75 #enddef
#define TURNS_LOW_WARNING
60 #enddef

[scenario]
    id=14_The_Drowned_Plains
    name= _ "The Drowned Plains"
    map_file=14_The_Drowned_Plains.map
    victory_when_enemies_defeated=no # leaders don't count as leaders once they run out of gold
    turns={SCENARIO_TURN_LIMIT}
    next_scenario=15_Return_to_Wesnoth

    {DEFAULT_SCHEDULE_DUSK}

    {SCENARIO_MUSIC into_the_shadows.ogg}
    {EXTRA_SCENARIO_MUSIC heroes_rite.ogg}
    {EXTRA_SCENARIO_MUSIC underground.ogg}
    {EXTRA_SCENARIO_MUSIC knolls.ogg}
    {EXTRA_SCENARIO_MUSIC the_deep_path.ogg}
    {EXTRA_SCENARIO_MUSIC nunc_dimittis.ogg}

    [story]
        [part]
            #po: In this scenario, there's circa 18 ghosts, each with a single line of speech, you'll recognise them because most start and end with ellipsis. The WML would need to be restructured to put po hints on those lines themselves.
            story= _ "Anxious to rendezvous with the Wesnothian army, Gweddry, Owaec, and Dacyn pressed hurriedly westward. Their hearts sank as they passed battlefield after battlefield, in the ruins of what used to be Wesnoth’s thriving eastern provinces."
            music=silence.ogg
            {EI_BIGMAP}
        [/part]
    [/story]
    {EI_TRACK {JOURNEY_14_NEW} }

    # replace most of the villages with ruined villages
    #    1) this emphasizes the destruction, and
    #    2) this makes the early-finish bonus less swingy
    {PLACE_IMAGE scenery/village-human-burned1.png 66 46}
    {PLACE_IMAGE scenery/village-human-burned2.png 62 53}
    {PLACE_IMAGE scenery/village-human-burned3.png 1 36}
    {PLACE_IMAGE scenery/village-human-burned4.png 2 41}
    {PLACE_IMAGE scenery/village-human-burned1.png 11 54}
    {PLACE_IMAGE scenery/village-human-burned2.png 2 20}
    {PLACE_IMAGE scenery/village-human-burned3.png 40 2}
    {PLACE_IMAGE scenery/village-human-burned4.png 21 9}
    {PLACE_IMAGE scenery/village-human-burned1.png 56 6}
    {PLACE_IMAGE scenery/village-human-burned2.png 53 3}
    {PLACE_IMAGE scenery/village-human-burned3.png 12 5}
    {PLACE_IMAGE scenery/village-human-burned4.png 14 4}
    {PLACE_IMAGE scenery/village-human-burned1.png 9 8}
    {PLACE_IMAGE scenery/village-human-burned2.png 15 54}
    {PLACE_IMAGE scenery/village-human-burned3.png 29 56}
    {PLACE_IMAGE scenery/village-human-burned4.png 2 5}
    {PLACE_IMAGE scenery/village-human-burned1.png 4 8}
    {PLACE_IMAGE scenery/village-human-burned3.png 34 5}
    {PLACE_IMAGE scenery/village-human-burned4.png 13 53}
    {PLACE_IMAGE scenery/village-human-burned1.png 34 46}
    {PLACE_IMAGE scenery/village-human-burned2.png 71 39}
    {PLACE_IMAGE scenery/village-human-burned3.png 68 38}
    {PLACE_IMAGE scenery/village-human-burned4.png 62 45}
    {PLACE_IMAGE scenery/village-human-burned1.png 49 50}
    {PLACE_IMAGE scenery/village-human-burned2.png 38 33}
    {PLACE_IMAGE scenery/village-human-burned3.png 35 33}
    {PLACE_IMAGE scenery/village-human-burned4.png 47 17}
    {PLACE_IMAGE scenery/village-human-burned1.png 44 18}
    {PLACE_IMAGE scenery/village-human-burned2.png 54 36}
    {PLACE_IMAGE scenery/village-human-burned3.png 18 30}
    {PLACE_IMAGE scenery/village-human-burned4.png 10 23}
    {PLACE_IMAGE scenery/village-human-burned1.png 37 19}
    {PLACE_IMAGE scenery/village-human-burned2.png 38 13}
    {PLACE_IMAGE scenery/village-human-burned3.png 15 19}
    {PLACE_IMAGE scenery/village-human-burned4.png 27 39}

    #---------------------------
    # GWEDDRY
    #---------------------------
    [side]
        side=1
        controller=human
        {GOLD 180 170 160} # even if you recall a full keep (2 hexes) both turns with gweddry, you'll still have gold left over for Owaec
        team_name=good
        user_team_name= _ "Wesnothians"
        {FLAG_VARIANT loyalist}
        fog=yes
        shroud=yes
        {CHARACTER_STATS_GWEDDRY}
    [/side]
    {STARTING_VILLAGES_AREA 1 69 22 8}

    #---------------------------
    # UNDEAD LEADERS
    #---------------------------
    [side]
        hidden=yes
        color=blue
        [leader] # since controller is null
            type=Barrow Wight
            facing=nw
            id=Sir Seoraery
            name=_"Sir Seoraery"
        [/leader]
        side=2
        controller=null # no controller until spotted
        income=-2
        gold=0  #recruits are scripted
        team_name=undead
        user_team_name=_"Undead"
        {FLAG_VARIANT undead}
    [/side]
    {SILENTLY_LIMIT_LEADER_MOVES 2 1} # stops applying once canrecruit=no

    [side]
        hidden=yes
        color=red
        [leader] # since controller is null
            type=Pyre Wight
            facing=se
            id=Sir Efran
            name=_"Sir Efran"
        [/leader]
        side=3
        controller=null # no controller until spotted
        income=-2
        gold=0  #recruits are scripted
        team_name=undead
        user_team_name=_"Undead"
        {FLAG_VARIANT undead}
    [/side]
    {SILENTLY_LIMIT_LEADER_MOVES 3 1} # stops applying once canrecruit=no

    [side]
        hidden=yes
        color=brown
        [leader] # since controller is null
            type=Skeletal Dragon
            facing=ne
            id=Khrakrahs
            #po: Date of death unknown, seen alive in 40 YW during the Sceptre of Fire campaign.
            #po: Male; he's been canon in many versions of Wesnoth that only had male drakes, thus will surely have been male in any translation that needed to know his gender.
            name=_"Khrakrahs"
        [/leader]
        side=4
        controller=null # no controller until spotted
        income=-2
        gold=0  #recruits are scripted
        team_name=undead
        user_team_name=_"Undead"
        {FLAG_VARIANT undead}
    [/side]
    {SILENTLY_LIMIT_LEADER_MOVES 4 1} # stops applying once canrecruit=no
#define MAI_ESCORT SIDE CA_ID ID SCORE
    [micro_ai]
        side={SIDE}
        ai_type=zone_guardian
        action=change
        ca_id={CA_ID}
        ca_score={SCORE} # used to disable all of these during the day
        [filter]
            side={SIDE}
            canrecruit=no
            [filter_location]    # only units near this leader should group up,
                radius=15        # otherwise we might steal escorts from other leaders

                [filter]
                    id={ID}
                [/filter]
            [/filter_location]
        [/filter]
        [filter_location]
            [filter]
                id={ID}
            [/filter]
            # stay near the leader
            radius=1
        [/filter_location]
        [filter_location_enemy]
            [filter]
                id={ID}
            [/filter]
            # attack enemies near the leader
            radius=8
        [/filter_location_enemy]
    [/micro_ai]
#enddef
    [event] # vary AI over time
        name=side 1 turn end
        first_time_only=no
        {RESET_SIDE_AI 2,3,4 no -1 1}
        {RESET_SIDE_AI     8 offensive 0.8 0.25}
        {MODIFY_SIDE_AI 2,3,4,8 (village_value=0)}
        {MODIFY_SIDE_AI 2,3,4,8 (retreat_factor=0)}
        {VARY_AI_BY_SCHEDULE 2,3,4,8}

        # disable the escort MAI if it's daytime, or else we won't retreat properly
        {VARIABLE ca_score 300000}
        [store_time_of_day]
        [/store_time_of_day]
        [if]
            [variable]
                name=time_of_day.id
                equals=morning
            [/variable]

            [then]
                {VARIABLE ca_score 0}
            [/then]
        [/if]

        [store_time_of_day]
        [/store_time_of_day]
        [if]
            [variable]
                name=time_of_day.id
                equals=afternoon
            [/variable]

            [then]
                {VARIABLE ca_score 0}
            [/then]
        [/if]

        {MAI_ESCORT 8 mai_seoraery  (Sir Seoraery) $ca_score}
        {MAI_ESCORT 8 mai_efran     (Sir Efran)    $ca_score}
        {MAI_ESCORT 8 mai_khrakrahs (Khrakrahs)    $ca_score}
        {CLEAR_VARIABLE ca_score}
    [/event]

    #---------------------------
    # GHOSTS
    #---------------------------
    # these guys are supposed to avoid the player and steal undefended villages
    [side]
        color=white
        side=5
        controller=ai
        hidden=yes
        no_leader=yes
        team_name=undead
        user_team_name=_"Undead"
    [/side]
    [event]
        name=prestart
        {RESET_SIDE_AI 5 no -9 9} # they can't attack anyway, but this doesn't hurt
        {MODIFY_AI_DELETE_CANDIDATE_ACTION 5 main_loop goto}
        {MODIFY_AI_DELETE_CANDIDATE_ACTION 5 main_loop spread_poison}
        {MODIFY_AI_DELETE_CANDIDATE_ACTION 5 main_loop recruitment}
        {MODIFY_AI_DELETE_CANDIDATE_ACTION 5 main_loop move_leader_to_goals}
        {MODIFY_AI_DELETE_CANDIDATE_ACTION 5 main_loop move_leader_to_keep}
        {MODIFY_AI_DELETE_CANDIDATE_ACTION 5 main_loop high_xp_attack}
        {MODIFY_AI_DELETE_CANDIDATE_ACTION 5 main_loop combat}
        {MODIFY_AI_DELETE_CANDIDATE_ACTION 5 main_loop place_healers}
        {MODIFY_AI_DELETE_CANDIDATE_ACTION 5 main_loop move_to_targets}
        {MODIFY_AI_DELETE_CANDIDATE_ACTION 5 main_loop leader_shares_keep}
        {MODIFY_AI_DELETE_CANDIDATE_ACTION 5 main_loop move_to_any_enemy}
        [micro_ai]
            side=5
            ai_type=forest_animals
            action=add
            ca_score=7500 # village max_score is 60k. I've found 7.5k MAI score provides a good balance
            deer_type=Ghost,Wraith,Spectre,Shadow,Nightgaunt
        [/micro_ai]
    [/event]

    #---------------------------
    # UNDEAD AMBUSHERS
    #---------------------------
    [side] # active ambushers
        color=teal
        side=6
        controller=ai
        hidden=yes
        no_leader=yes
        team_name=undead
        user_team_name=_"Undead"
        [ai]
            aggression=1
            caution=0
            retreat_factor=0
            village_value=0
            leader_value=0
        [/ai]
    [/side]
    [side] # idle ambushers
        color=teal
        side=7
        controller=null
        hidden=yes
        no_leader=yes
        team_name=undead
        user_team_name=_"Undead"
    [/side]

    #---------------------------
    # LEADER ESCORTS
    #---------------------------
    [side]
        color=white
        side=8
        controller=ai
        hidden=yes
        no_leader=yes
        team_name=undead
        user_team_name=_"Undead"
    [/side]

    #---------------------------
    # NEUTRAL HORSES
    #---------------------------
    [side]
        color=black
        hidden=yes
        side=9
        controller=null
        team_name=good,undead
        #po: neutral side, is only horses (although the user_team_name is vaguer than that)
        user_team_name=_"Animals"
        gold=0
        no_leader=yes

        {GENERIC_UNIT 9 (Bay Horse) 68 12}
        {GENERIC_UNIT 9 (Great Horse) 70 14}

        {GENERIC_UNIT 9 (Bay Horse) 27 54}
        {GENERIC_UNIT 9 (Great Horse) 34 52}
        {GENERIC_UNIT 9 (Bay Horse) 35 54}
    [/side]

    #---------------------------
    # PLACE INITIAL ENEMIES
    #---------------------------
    [event]
        name=prestart

        #---------------------------
        # SPAWN AMBUSHERS
        #---------------------------
        [store_locations]
            x,y=1-74,1-55 # not any unreachable border hexes
            terrain=Ss,Ss^Qhu,Sm,Sm^Qhu
            variable=swamp_hexes
        [/store_locations]
        [foreach]
            array=swamp_hexes
            [do]
                # ambushers scale in both number and power
                # only numbers-scaling leaves Easy feeling empty,
                # and only power-scaling feeds too much XP on Hard
                {VARIABLE_OP yesno rand {ON_DIFFICULTY
                ("no,no,no,no,no,no,no,no,no,no,no,no,no,no,yes,yes")   #2/16 on easy
                ("no,no,no,no,no,no,no,no,no,no,no,no,no,yes,yes,yes")  #3/16 on normal
                ("no,no,no,no,no,no,no,no,no,no,no,no,yes,yes,yes,yes") #4/16 on hard
                }}
                [if]
                    {VARIABLE_CONDITIONAL yesno equals yes}
                    [then]
                        {VARIABLE_OP ambusher_variation rand "none,mounted,mounted,horse"}
                        {VARIABLE_OP ambusher_type rand {ON_DIFFICULTY
                        ("Walking Corpse")
                        ("Walking Corpse,Walking Corpse,Soulless,Ghoul")
                        ("Walking Corpse,Soulless,Ghoul")
                        }}
                        {GENERIC_UNIT 7 ($ambusher_type) $this_item.x $this_item.y} {VARIATION $ambusher_variation}
                        [modify_unit]
                            [filter]
                                x,y=$this_item.x,$this_item.y
                            [/filter]

                            [object]
                                id=ambusher_invisibility
                                [effect]
                                    apply_to=new_ability
                                    [abilities]
                                        [hides]
                                            affect_self=yes
                                        [/hides]
                                    [/abilities]
                                [/effect]
                            [/object]
                            [object] # buff ambushers slightly in swamp. Otherwise corpses can only move 1 hex
                                [effect]
                                    apply_to=movement_costs
                                    replace=yes
                                    [movement_costs]
                                        swamp_water=2
                                    [/movement_costs]
                                [/effect]
                                [effect]
                                    apply_to=defense
                                    replace=yes
                                    [defense]
                                        swamp_water=70
                                    [/defense]
                                [/effect]
                            [/object]
                        [/modify_unit]
                    [/then]
                [/if]
            [/do]
        [/foreach]
        {CLEAR_VARIABLE swamp_hexes,yesno,ambusher_variation,ambusher_type}

        #---------------------------
        # SPAWN GHOSTS
        #---------------------------
        # needs to be after ambushers, in case we overlap (which breaks ambushers but not ghosts)
#define GHOST_WANDERER YESNO X Y DEATH_MESSAGE
    {VARIABLE yesno {YESNO}}
    [if]
        {VARIABLE_CONDITIONAL yesno equals yes}
        [then]
            {GENERIC_UNIT 5 (Ruffian) 1 1} # wesnothian name "generator"
            {STORE_UNIT_VAR (type=Ruffian) name ghost_name}
            {KILL type=Ruffian}
            {VARIABLE_OP ghost_i add 1}
            {VARIABLE_OP ghost_type rand "Ghost","Ghost","Wraith"} # ghosts rarely attack, mostly just wander
            {NAMED_UNIT 5 ($ghost_type) {X} {Y} "ghost_$ghost_i" $ghost_name ()}
            [event]
                name=last breath
                delayed_variable_substitution=no

                [filter]
                    id=ghost_$ghost_i
                [/filter]

                [message]
                    speaker=unit
                    message={DEATH_MESSAGE}
                [/message]
            [/event]
        [/then]
    [/if]
    {CLEAR_VARIABLE yesno,ghost_type,ghost_name}
#enddef

        {VARIABLE ghost_i 0}
        {GHOST_WANDERER {ON_DIFFICULTY (yes)(yes)(yes)}   1  1 _"... I failed you all..."}
        {GHOST_WANDERER {ON_DIFFICULTY (no )(yes)(yes)}  10  1 _"... my daughter... what has happened to my daughter?"}
        {GHOST_WANDERER {ON_DIFFICULTY (no )(no )(yes)}  20  1 _"... they don’t know pain... they don’t know fatigue... there was nothing we could have done..."}
        {GHOST_WANDERER {ON_DIFFICULTY (no )(yes)(yes)}  30  1 _"Lords of Light... what affront did I commit to deserve this fate..."}
        {GHOST_WANDERER {ON_DIFFICULTY (no )(no )(yes)}  40  1 _"I fought the undead with everything... I gave everything..."}
        {GHOST_WANDERER {ON_DIFFICULTY (yes)(yes)(yes)}  50  1 _"... it came from the shadows. A gust of wind, then a claw through my chest..."}
        {GHOST_WANDERER {ON_DIFFICULTY (no )(yes)(yes)}  60  1 _"... please, find my wife... tell her my story."}
        {GHOST_WANDERER {ON_DIFFICULTY (no )(no )(yes)}   1 10 _"... honor, glory, wealth... none of it saved us in the end."}
        {GHOST_WANDERER {ON_DIFFICULTY (no )(yes)(yes)}   1 20 _"... they won’t let me die... why won’t they let me die?"}
        {GHOST_WANDERER {ON_DIFFICULTY (no )(no )(yes)}   1 30 _"... they tore the ribs out from my friends... then fashioned them into arrows."}
        {GHOST_WANDERER {ON_DIFFICULTY (yes)(yes)(yes)}   1 40 _"... I could not hurt him... my own father... his corpse risen to fight me."}
        {GHOST_WANDERER {ON_DIFFICULTY (no )(yes)(yes)}   1 50 _"... Lord Gaelyc, I have failed you."}
        {GHOST_WANDERER {ON_DIFFICULTY (no )(no )(yes)}  10 55 _"... the Clans were not prepared... I was not prepared."}
        {GHOST_WANDERER {ON_DIFFICULTY (no )(yes)(yes)}  20 55 _"... how could we have known? What could we have done?"}
        {GHOST_WANDERER {ON_DIFFICULTY (no )(no )(yes)}  30 55 _"... my family, where is my family?"}
        {GHOST_WANDERER {ON_DIFFICULTY (yes)(yes)(yes)}  40 55 _"... what good was my lance... against a creature... without flesh?"}
        {GHOST_WANDERER {ON_DIFFICULTY (no )(yes)(yes)}  50 55 _"At first I thought he was one of ours... then he waved his hand and my blood ran cold..."}
        {GHOST_WANDERER {ON_DIFFICULTY (no )(no )(yes)}  60 55 _"... so cold... so dark..."}
    [/event]

    #---------------------------
    # AMBUSHERS RANDOMLY ACTIVATE
    #---------------------------
    # to make the map a little more chaotic, and to grab the occasional village
    [event]
        name=new turn
        first_time_only=no

        [filter_condition]
            [have_unit]
                side=7
            [/have_unit]
        [/filter_condition]

        [store_unit]
            [filter]
                side=7
            [/filter]

            variable=side_7_units
        [/store_unit]
        {VARIABLE_OP yesno rand {ON_DIFFICULTY ("no,no,yes") ("no,yes") ("no,yes,yes")}}
        [if]
            {VARIABLE_CONDITIONAL yesno equals yes}
            [then]
                {RANDOM "0..$($side_7_units.length-1)"}
                {MODIFY_UNIT id=$side_7_units[$random].id side 6}
            [/then]
        [/if]
        {CLEAR_VARIABLE yesno,side_7_units}
    [/event]

    #---------------------------
    # AMBUSHER IS SPOTTED
    #---------------------------
    [event]
        name=moveto
        first_time_only=no
        [filter]
            side=1

            [filter_adjacent]
                side=7
            [/filter_adjacent]
        [/filter]
        {MODIFY_UNIT (
            side=7
            [filter_location]
                x,y=$unit.x,$unit.y
                radius=1
            [/filter_location]
        ) side 6
        }    # wake the visible ambushers
        {REPEAT 4 (
            {FIND_NEARBY (side=7) $unit.x $unit.y 4}
            [if]
                [variable]
                    name=found_unit.length
                    greater_than=0
                [/variable]

                [then]
                    {MODIFY_UNIT id=$found_unit.id side 6}
                [/then]
            [/if]
            # wake some nearby invisible ambushers
        )}
        [fire_event]
            name=explain_ambushers
            [primary_unit]
                id=$unit.id
            [/primary_unit]
        [/fire_event]
    [/event]
    [event]
        name=explain_ambushers
        [message]
            speaker=unit
            #po: Player's unit has just been ambushed. Some other nearby corpses have awoken too, but the player will find that out during the enemy turn.
            message= _ "Gah, undead! This one was mired face-first in the swamp, dormant until I stumbled upon it. They must be lying everywhere in this muck."
        [/message]
        [message]
            speaker=Gweddry
            message= _ "Perhaps their leaders are dormant as well. I suspect we will not have to contend with them until we sight them. We can afford some time to capture villages and build our forces."
        [/message]
        [event]
            name=new turn
            [message]
                speaker=Gweddry
                #po: Player's unit trigger an ambush last turn. During the enemy turn, some other nearby corpses started moving too.
                message= _ "Whenever we stumble upon a corpse, it awakens its nearby allies! What will happen when we wake their leaders?"
            [/message]
            [message]
                speaker=Dacyn
                #po: The keeps are visible, so the player can guess where the leaders themselves are.
                message= _ "It may be prudent to clear the swamp near the undead leaders before we sight them."
            [/message]
        [/event]
    [/event]

    #---------------------------
    # REMOVE INVISIBILITY
    #---------------------------
    # only remove invisibility when an ambusher moves, not when they're first woken. Makes them much more sneaky
    [event]
        name=exit hex
        first_time_only=no

        [filter]
            side=6
        [/filter]

        [remove_object]
            id=$unit.id
            object_id=ambusher_invisibility
        [/remove_object]
    [/event]

    #---------------------------
    # LEADER ACTIVATES AMBUSHERS
    #---------------------------
#define ACTIVATE_AMBUSHERS_NEAR_LEADER LEADER_ID
    [if]
        [have_unit]
            id={LEADER_ID}
        [/have_unit]

        [then]
            {STORE_UNIT_VAR (id={LEADER_ID}) x leaderX}
            {STORE_UNIT_VAR (id={LEADER_ID}) y leaderY}
            {REPEAT 10 (
                [store_unit]
                    variable=found_unit
                    [filter]
                        side=7
                        [filter_location] # radius is really slow, but this is drastically faster
                            x="$($leaderX-6)-$($leaderX+6)"
                            y="$($leaderY-5)-$($leaderY+5)" # Y feels larger than X on Wesnoth's hex grid
                        [/filter_location]
                    [/filter]
                [/store_unit]
                [if]
                    [variable]
                        name=found_unit.length
                        greater_than=0
                    [/variable]

                    [then]
                        {MODIFY_UNIT id=$found_unit.id side 6}
                        [fire_event]
                            name=explain_leader_ambushers
                        [/fire_event]
                    [/then]
                [/if]
            )}
            {CLEAR_VARIABLE leaderX,leaderY}
        [/then]
    [/if]
#enddef
    # replace ambushers with impassable terrain during the AI turns, otherwise the AI is way too slow
#define REMOVE_AMBUSHERS_ON_TERRAIN TERRAIN
    [store_unit]
        [filter]
            side=7

            [filter_location]
                terrain={TERRAIN}
            [/filter_location]
        [/filter]

        variable=ambushers_{TERRAIN}
        kill=yes
    [/store_unit]
    [foreach]
        array=ambushers_{TERRAIN}
        variable=unit
        [do]
            {MODIFY_TERRAIN {TERRAIN}^Xo $unit.x $unit.y}
        [/do]
    [/foreach]
    [event]
        name=new turn
        [foreach]
            array=ambushers_{TERRAIN}
            variable=unit
            [do]
                {MODIFY_TERRAIN {TERRAIN} $unit.x $unit.y}
                [unstore_unit]
                    variable=unit
                    animate=no
                [/unstore_unit]
            [/do]
        [/foreach]
        {CLEAR_VARIABLE ambushers_{TERRAIN}}
    [/event]
#enddef
    [event]
        name=side 1 turn end
        first_time_only=no
        [if]
            {VARIABLE_CONDITIONAL seoraery_active  equals yes}
            [then]
                {ACTIVATE_AMBUSHERS_NEAR_LEADER (Sir Seoraery)}
            [/then]
        [/if]
        [if]
            {VARIABLE_CONDITIONAL efran_active     equals yes}
            [then]
                {ACTIVATE_AMBUSHERS_NEAR_LEADER (Sir Efran)}
            [/then]
        [/if]
        [if]
            {VARIABLE_CONDITIONAL khrakrahs_active equals yes}
            [then]
                {ACTIVATE_AMBUSHERS_NEAR_LEADER (Khrakrahs)}
            [/then]
        [/if]
    [/event]
    [event]
        name=explain_leader_ambushers
        [event]
            name=new turn
            [message]
                speaker=Gweddry
                message= _ "Their leader is waking nearby allies out from the swamp!"
            [/message]
            [message]
                speaker=Owaec
                message= _ "Just more abominations for us to slay!!"
            [/message]
        [/event]
    [/event]

    #---------------------------
    # START GAME
    #---------------------------
#define GATE X Y
    {MODIFY_TERRAIN Qxua^Xo {X} {Y}}
    [sound]
        name=lightning-miss.ogg
    [/sound]
    [delay]
        time=300
    [/delay]
    [redraw]
        clear_shroud=yes
        side=1
    [/redraw]
#enddef
#define SPIRIT FLT TYPE X Y TO_X TO_Y
    [if]
        [have_unit]
            {FLT}
        [/have_unit]

        [then]
            [sound]
                name=wail-sml.wav
            [/sound]

            {GENERIC_UNIT 6 ({TYPE}) {X} {Y}} {ANIMATE}
            {MOVE_UNIT (x,y={X},{Y}) {TO_X} {TO_Y}}
        [/then]
    [/if]
#enddef
#define UNGATE X Y
    {MODIFY_TERRAIN Ds {X} {Y}}
    [sound]
        name=net.wav
    [/sound]
    [delay]
        time=200
    [/delay]

    [redraw]
        clear_shroud=yes
        side=1
    [/redraw]
#enddef
    [event]
        name=prestart

        {SET_LABEL 16 24 _"Lord Alric’s Palace"  }
        {SET_LABEL 17 5  _"Sir Efran’s Castle"   }
        {SET_LABEL 7 50  _"Lord Gaelyc’s Citadel"}
        {SET_LABEL 68 42 _"Sir Seoraery’s Keep"  }
        {SET_LABEL 49 9  _"Clearwater Lake"      }

        {PLACE_IMAGE "data/core/images/misc/blank-hex.png~BLIT(data/core/images/items/bones.png~CHAN(red*1.2),-4,-30)"     16 24} # lord alric
        [item]
            x,y,image=7,50,"data/core/images/misc/blank-hex.png~BLIT(data/core/images/items/bones.png~CHAN(red*1.2)~FL(),3,-30)" # lord gaelyc
            visible_in_fog=no
        [/item]

        # the path we came from
        [remove_shroud]
            x,y=73,23
            radius=4
        [/remove_shroud]
        [remove_shroud]
            x,y=69,25
            radius=4
        [/remove_shroud]
        [remove_shroud]
            x,y=65,28
            radius=4
        [/remove_shroud]
        [remove_shroud]
            x,y=70,18
            radius=4
        [/remove_shroud]

        # Owaec's memories. To make the map less mysterious
        [remove_shroud] # a few reveals spread around the lake, to make it look more natural
            x,y=50,8
            radius=6
        [/remove_shroud]
        [remove_shroud]
            x,y=47,9
            radius=6
        [/remove_shroud]
        [remove_shroud]
            x,y=48,6
            radius=6
        [/remove_shroud]
        [remove_shroud]
            x,y=50,6
            radius=6
        [/remove_shroud]

        # holy amulet, so players are less likely to miss it
        [remove_shroud]
            x,y=43,12
            radius=1
        [/remove_shroud]

        [objectives]
            side=1
            [objective]
                description= _ "Defeat the spirits"
                condition=win
            [/objective]
            [objective]
                description= _ "Death of Gweddry or Dacyn"
                condition=lose
            [/objective]
        [/objectives]
    [/event]
    [event]
        name=start

        {RECALL_XY Dacyn 56 28}
        [redraw]
        [/redraw]

        {RECALL_XY Owaec 55 28}
        {RECALL_XY Addogin 57 27}
        {RECALL_XY (Hahid al-Ali) 57 27}
        {RECALL_XY Terraent 57 27}
        {RECALL_XY Dolburras 57 28} # choose tiles that won't get in the way of the gate
        {RECALL_XY Grug 57 28}
        {RECALL_XY Gaennell 56 26}
        {MODIFY_UNIT side=1 facing sw}
        [redraw]
            clear_shroud=yes
            side=1
        [/redraw]

        [message]
            speaker=Owaec
            #po: The player hasn't recruited yet, so listening are Gweddry, Dacyn, and the auto-recalled optional loyals (at most 8 people in total).
            message= _ "At last we have reached the Horse Plains, the fair homeland of my people! Here we can finally reunite with our comrades-in-arms and vanquish the undead once and for all."
        [/message]
        {MOVE_UNIT (id=Owaec) 48 26}
        [redraw]
            clear_shroud=yes
            side=1
        [/redraw]
        [message]
            speaker=Owaec
            #po: Shouted to the landscape, expecting to find living comrades. What will answer is walking corpses and ghosts.
            message= _ "Hail, Clansmen! Come and greet one of your own!"
        [/message]

        # Owaec finds out the clans have been wiped out
        {NAMED_UNIT 7 (Walking Corpse) 45 27 "Fallen Clansman" (_"Fallen Clansman") ()} {VARIATION mounted}
        [sound]
            name=zombie-hit-1.ogg
        [/sound]

        [delay]
            time=1000
        [/delay]

        {MOVE_UNIT (id=Fallen Clansman) 47 27}
        [message]
            speaker=Fallen Clansman
            message= _ "..."
        [/message]
        [message]
            speaker=Owaec
            message= _ "..."
        [/message]
        [message]
            speaker=Owaec
            message= _ "No."
        [/message]
        [message]
            speaker=Owaec
            message= _ "No, NO! Why are there swamps here? Why are there undead here? How can this be?! How can Mal-Ravanal have already advanced so far into the heartland of Wesnoth?"
        [/message]
        [message]
            speaker=Gweddry
            message= _ "I’m sorry Owaec. The undead armies must have defeated the Clansmen and dammed the river to drown the plains. They must like the swamp, filled with death and decay..."
        [/message]
        [message]
            speaker=Owaec
            message= _ "My home... my people... how could this happen..."
        [/message]

        [delay]
            time=2000
        [/delay]

        {MOVE_UNIT (id=Owaec) 50 27}
        [message]
            speaker=Owaec
            message= _ "YOU! MAGE!!! Had you not insisted we travel north, we might have arrived in time to save these people! My friends, my family, dead from your neglect. And all for what? Some necromancer’s trinket more likely to curse us than save us?"
        [/message]
        [message]
            speaker=Owaec
            message= _ "We trusted you. I trusted you! Instead, you sent us away from the war like cowards, led our men to slaughter at the hand of orcs and drakes, and deprived Wesnoth of her eastern guard when she needed it most sorely. And now my homeland has paid the price for your misdeeds!!"
        [/message]

        [delay]
            time=2000
        [/delay]

        [message]
            speaker=Owaec
            #po: Dacyn is bleeding and about to take damage from an unknown source
            message= _ "Are you even listening?!?"
        [/message]
        [harm_unit]
            [filter]
                id=Dacyn
            [/filter]
            amount=3
            animate=yes # also deal this damage again after advancing
        [/harm_unit]
        {PLACE_IMAGE scenery/blood-1.png 56 28}
        [delay]
            time=500
        [/delay]

        [message]
            speaker=Gweddry
            #po: Dacyn is bleeding and taking damage, from an unknown source
            message= _ "Dacyn, what’s wrong? Are you all right?"
        [/message]

        # Dacyn loses control of the amulet
        {MOVE_UNIT (id=Dacyn) 55 29}
        [delay]
            time=500
        [/delay]

        [message]
            speaker=Dacyn
            #po: Dacyn steps away from the initial encampment, and is about take a lot of damage
            message= _ "...I can’t-"
        [/message]

        [harm_unit]
            [filter]
                id=Dacyn
            [/filter]
            amount=5
            animate=yes # also deal this damage again after advancing
        [/harm_unit]
        [sound]
            name=magic-dark-big-miss.ogg
        [/sound]

        {COLOR_ADJUST  127   64  127}
        [delay]
            time=100
        [/delay]

        {COLOR_ADJUST -127 -191 -127}
        [delay]
            time=100
        [/delay]

        {COLOR_ADJUST -127 -191 -127}
        [delay]
            time=100
        [/delay]

        {COLOR_ADJUST    0    0    0}

        [sound]
            name=magic-twilight.ogg
        [/sound]

        {MODIFY_TERRAIN Dd^Dc 55 29}

        {REMOVE_IMAGE 56 28}
        {GATE 55 28} {MODIFY_UNIT id=Dacyn hitpoints 40} # overwrite a castle tile. Having only 3 castles stops Gweddry spending all his money before Owaec reappears
        {GATE 56 28} {MODIFY_UNIT id=Dacyn hitpoints 35} # overwrite a castle tile. Gate right next to Gweddry also gives a barrier to the ghosts on turn 1
        {GATE 56 29} {MODIFY_UNIT id=Dacyn hitpoints 30}
        {GATE 55 30} {MODIFY_UNIT id=Dacyn hitpoints 25}
        {GATE 54 29} {MODIFY_UNIT id=Dacyn hitpoints 20}
        {GATE 54 28} {MODIFY_UNIT id=Dacyn hitpoints 15}
        [message]
            speaker=Dacyn
            #po: Dacyn stepped away from the castle, the six hexes around him have changed to chasms and he's down to 15 hp.
            message= _ "I’ve lost control of the Amulet! I must-"
        [/message]

        [harm_unit]
            [filter]
                id=Dacyn
            [/filter]
            amount=10
            animate=yes
        [/harm_unit]
        # if the player lost their auto-recall loyals, spawn fewer spirits
#ifndef EASY
        {SPIRIT side,count=1,5-99 {ON_DIFFICULTY Ghost Wraith Wraith} 54 28 46 28}
        {SPIRIT side,count=1,4-99 {ON_DIFFICULTY Ghost Wraith Wraith} 55 28 51 24}
#endif
        {SPIRIT side,count=1,0-99 {ON_DIFFICULTY Ghost Ghost  Wraith} 56 28 59 24}
        {SPIRIT side,count=1,4-99 {ON_DIFFICULTY Ghost Ghost  Wraith} 56 29 60 30}
        {SPIRIT side,count=1,0-99 {ON_DIFFICULTY Ghost Wraith Wraith} 55 30 55 33}
        {SPIRIT side,count=1,4-99 {ON_DIFFICULTY Ghost Wraith Wraith} 54 29 47 31}

        [message]
            caption= _ "Unknown"
            image=units/unknown-unit1.png
            #po: up to 6 ghosts or wraiths come out of the chasms, depending on difficulty and how many auto-recalled loyals the player has
            message= _ "<span size='small' font-style='italic'>And most importantly...</span>"
        [/message]

        [sound]
            name=wail.wav
        [/sound]

        {NAMED_UNIT 6 ({ON_DIFFICULTY Wraith Spectre Spectre}) 54 28 "Lord Alric" (_"Lord Alric") ()} {ANIMATE}
        {MOVE_UNIT (id=Lord Alric) 51 27}

        [message]
            speaker=Lord Alric
            #po: speaker is a wraith, he's emerged from the chasms around Dacyn
            message= _ "Owaec, my son..."
        [/message]
        [message]
            speaker=Lord Alric
            message= _ "Why did you abandon us? I am dead... thanks to you..."
        [/message]
        [message]
            speaker=Owaec
            message= _ "No, it’s not true! You’re not dead! I don’t believe you!"
        [/message]
        [message]
            speaker=Gweddry
            #po: Owaec rides off to the south, into the shroud, and disappears
            message= _ "Owaec, wait!"
        [/message]

        {MOVE_UNIT (id=Owaec) 50 34}
        {MOVE_UNIT (id=Owaec) 46 37}
        [store_unit]
            [filter]
                id=Owaec
            [/filter]
            kill=yes
            variable=saved_Owaec
        [/store_unit]

        [delay]
            time=2000
        [/delay]

        [message]
            speaker=Gweddry
            message= _ "What do I do now?"
        [/message]
        {KILL (id=Fallen Clansman)} # he looks weird fighting alongside the ghosts
    [/event]

    #---------------------------
    # DACYN CLOSES THE PORTAL
    #---------------------------
    [event]
        name=side 1 turn 2

        {SCROLL_TO 55 29}
        {UNGATE 55 28} {MODIFY_UNIT id=Dacyn hitpoints 20} # restore HP in case the player lost his recall list at S12, and needs Dacyn to fight the ghosts
        {UNGATE 56 28} {MODIFY_UNIT id=Dacyn hitpoints 25}
        {UNGATE 56 29} {MODIFY_UNIT id=Dacyn hitpoints 30}
        {UNGATE 55 30} {MODIFY_UNIT id=Dacyn hitpoints 35}
        {UNGATE 54 29} {MODIFY_UNIT id=Dacyn hitpoints 40}
        {UNGATE 54 28} {FULL_HEAL   id=Dacyn}

        [message]
            speaker=Dacyn
            #po: The chasms are filled with sand, so all terrain near the keep is walkable again.
            #po: Dacyn has healed back to full HP, although that's for gameplay reasons because he needs to fight the ghosts, there isn't a story reason for it.
            message= _ "(gasping) I have regained control!"
        [/message]
        [message]
            speaker=Gweddry
            message= _ "What happened? I did not know this Amulet could act of its own accord."
        [/message]
        [message]
            speaker=Dacyn
            message= _ "Neither did I. I should have sensed it, but the darkness is nearly overwhelming. Even with my inner light, the best I can do is cancel out this dark energy. Such is the price of power..."
        [/message]
        [message]
            speaker=Dacyn
            #po: loses his halo and advances to an L4 Twilight Mage (liminal, heals +4, does not illuminate, melee attack gains "drain")
            message= _ "... but it is a necessary price to pay. I will not let up until Mal-Ravanal is defeated, whatever the cost may be."
        [/message]
        {STORE_UNIT_VAR id=Dacyn experience dacyn_xp}
        {ADVANCE_UNIT id=Dacyn (Twilight Mage)}
        {MODIFY_UNIT id=Dacyn experience $dacyn_xp}
        {CLEAR_VARIABLE dacyn_xp}
    [/event]

    #---------------------------
    # OWAEC RETURNS HOME
    #---------------------------
    [event]
        name=side 1 turn 3

        # where Owaec walked on his journey
        [remove_shroud]
            x,y=50,35
            radius=4
        [/remove_shroud]
        [remove_shroud]
            x,y=46,37
            radius=4
        [/remove_shroud]
        [remove_shroud]
            x,y=39,37
            radius=4
        [/remove_shroud]
        [remove_shroud]
            x,y=32,38
            radius=4
        [/remove_shroud]
        [remove_shroud]
            x,y=27,41
            radius=4
        [/remove_shroud]
        [remove_shroud]
            x,y=21,43
            radius=4
        [/remove_shroud]
        [remove_shroud]
            x,y=16,42
            radius=4
        [/remove_shroud]
        [remove_shroud]
            x,y=11,40
            radius=4
        [/remove_shroud]
        [remove_shroud]
            x,y=10,35
            radius=4
        [/remove_shroud]
        [remove_shroud]
            x,y=10,30
            radius=4
        [/remove_shroud]

        {SCROLL_TO 15 27}
        [unstore_unit]
            variable=saved_Owaec
            x,y=10,30
        [/unstore_unit]
        [redraw]
            clear_shroud=yes
        [/redraw]
        {MOVE_UNIT id=Owaec 15 27}
        [redraw]
            clear_shroud=yes
        [/redraw]

        [message]
            speaker=Owaec
            message= _ "..."
        [/message]

        [delay]
            time=2000
        [/delay]

        [message]
            speaker=Owaec
            #po: Owaec is near his father's castle, and there's a dead (not undead) skeleton on the keep
            message= _ "...so it is true. My lord and father is dead."
        [/message]
        [delay]
            time=2000
        [/delay]

        [message]
            speaker=Owaec
            #po: today is when he learns the news of that, not when it happened
            message= _ "Today truly is a day of ill fortune. My family is slain, my kinsmen scattered, my homeland defiled."
        [/message]
        [message]
            speaker=Owaec
            message= _ "But I will not despair! This damning destruction will not be the lasting legacy of the Clans!! These undead will pay for the death they have brought!!!"
        [/message]

#define UNSHROUD_UNFOG X Y FOGX FOGY
    [remove_shroud]
        x,y={X},{Y}
        radius=3
    [/remove_shroud]
    #     [lift_fog]
    #         [filter_side]
    #             side=1
    #         [/filter_side]
    #         x={FOGX} # so the player can see which units were recruited (corpses vs skellies), but not the leader
    #         y={FOGY} # (I couldn't get this working with radius and
    #  [not])
    #     [/lift_fog]
#enddef
        {SCROLL_TO 68 42}
        {UNSHROUD_UNFOG 68 42  68,67,67,68,69  41,42,43,43,43}
        [delay]
            time=700
        [/delay]

        {SCROLL_TO 17 5}
        {UNSHROUD_UNFOG 17 5  17,18,18,17,16,16  4,4,5,6,5,4}
        [delay]
            time=700
        [/delay]

        {SCROLL_TO 7 50}
        {UNSHROUD_UNFOG 7 50  7,8,8,7,6,6  49,49,50,51,50,49}
        [delay]
            time=700
        [/delay]

        [message]
            speaker=Dacyn
            message= _ "Owaec, do not be so hasty. Much of the horde that conquered this place now marches on Weldyn, but even what little lingers is surely more than a match for our weary band."
        [/message]
        [message]
            speaker=Gweddry
            message= _ "I agree, yet all is strangely quiet. Perhaps the remaining undead lie dormant? If we take time to capture villages and rebuild our forces, we may yet be able to reclaim this place."
        [/message]

        [objectives]
            side=1
            [objective]
                description= _ "Defeat all enemy leaders"
                condition=win
            [/objective]
            [objective]
                {BONUS_OBJECTIVE_CAPTION}
                description= _ "Loot Addogin’s smugglers’ caches."
                condition=win
                [show_if]
                    {VARIABLE_CONDITIONAL caches_active equals yes}
                [/show_if]
            [/objective]
            [objective]
                description= _ "Death of Gweddry, Dacyn, or Owaec"
                condition=lose
            [/objective]
            {TURNS_RUN_OUT}
            [gold_carryover]
                bonus=yes
                carryover_percentage=40
            [/gold_carryover]
            [note]
                description= _ "Enemy leaders are idle until first sighted, but will then abandon their keep and attack you."
            [/note]
            [note]
                description= _ "It may be wise to capture villages and build up an army before attacking."
            [/note]
        [/objectives]

        [event]
            name=side turn end,moveto,attack,recruit,recall,select # as soon as you do anything at all
            [message]
                speaker=Hahid al-Ali
                message= _ "On a quest of searing vengeance? Owaec my friend, what you need is my elixir of ulfserker fury! One taste of this bitter red ambrosia and you’ll fight until you drop, with <b><i>unlimited melee attacks</i></b> and the <b><i>berserk</i></b> melee special!"
            [/message]
            [event]
                name=buy_elixir
                [gold]
                    side=1
                    amount=-30
                [/gold]
                [sound]
                    name=gold.ogg
                [/sound]
                [message]
                    speaker=Hahid al-Ali
                    #po: player bought the elixir of ulfserker fury
                    message= _ "Try not to die too quickly!"
                [/message]
                {ELIXIR_OF_FURY 13 26} # by Owaec. He doesn't have to drink it, but he's the best choice
            [/event]
            [event]
                name=ignore_elixir
                [message]
                    speaker=Hahid al-Ali
                    #po: player declined the elixir of ulfserker fury
                    message= _ "Suit yourself! Don’t expect me to drink that though, I’m not suicidal!"
                [/message]
            [/event]
            [message]
                speaker=Hahid al-Ali
                message= _ "This offer expires as soon as your anger does! <i>(elixirs only last for 1 scenario)</i>"
                [option]
                    label= _ "Buy the elixir for Owaec (30 gold)."
                    [command]
                        [do_command]
                            [fire_event]
                                raise=buy_elixir
                            [/fire_event]
                        [/do_command]
                    [/command]
                [/option]
                [option]
                    label= _ "Leave the elixir."
                    [command]
                        [do_command]
                            [fire_event]
                                raise=ignore_elixir
                            [/fire_event]
                        [/do_command]
                    [/command]
                [/option]
            [/message]
        [/event]
    [/event]

    #---------------------------
    # ADDOGIN SMUGGLERS' CACHES
    #---------------------------
    [event]
        name=side 1 turn 5

        [filter_condition]
            [have_unit]
                id=Addogin
            [/have_unit]
        [/filter_condition]

        [message]
            speaker=Addogin
            #po: Wait a moment... this landscape looks familiar.
            message= _ "Hol’ up... some of this place is lookin’ mighty familiar. Yeah, few years back I used to work for a smuggler ’round these parts."
        [/message]
        [message]
            speaker=Addogin
            #po: hint about the smuggler's hidden gold, will be followed by adding three "go here" markers to the map
            message= _ "I’m sure the undead have burned down his storehouse, but there might still be a cache or three hidden ’round here."
        [/message]

        [remove_shroud]
            side=1
            x,y=51,48
            radius=1
        [/remove_shroud]
        {HIGHLIGHT_IMAGE 51 48 items/gohere.png ()}
        [remove_shroud]
            side=1
            x,y=26,49
            radius=1
        [/remove_shroud]
        {HIGHLIGHT_IMAGE 26 49 items/gohere.png ()}
        [remove_shroud]
            side=1
            x,y=7,14
            radius=1
        [/remove_shroud]
        {HIGHLIGHT_IMAGE 7 14 items/gohere.png ()}

        [message]
            speaker=Gweddry
            message= _ "We should retrieve as many as we can; I’m sure the extra gold will be useful."
        [/message]
        [message]
            speaker=Addogin
            message= _ "Just remember, I ain’t been here in years. Some of them caches might be empty now."
        [/message]
        {VARIABLE caches_active yes}
        [show_objectives]
        [/show_objectives]

        [event]
            name=moveto
            [filter]
                side=1
                x,y=51,48
            [/filter]

            {REMOVE_IMAGE $x1 $y1}
            [fire_event]
                name=cache_found
            [/fire_event]
        [/event]
        [event]
            name=moveto
            [filter]
                side=1
                x,y=26,49
            [/filter]

            {REMOVE_IMAGE $x1 $y1}
            [fire_event]
                name=cache_found
            [/fire_event]
        [/event]
        [event]
            name=moveto
            [filter]
                side=1
                x,y=7,14
            [/filter]

            {REMOVE_IMAGE $x1 $y1}
            [fire_event]
                name=cache_found
            [/fire_event]
        [/event]
        [event]
            name=cache_found
            [message]
                speaker=Addogin
                #po: although there are three map markers to explore, the first one that the player explores always has this gold, and the second is always empty
                message= _ "That’s the first cache. Lookee here, my smuggler friend left us 45 gold pieces."
            [/message]
            [sound]
                name=gold.ogg
            [/sound]

            [gold]
                side=1
                amount=45
            [/gold]
            [event]
                name=cache_found
                [message]
                    speaker=Addogin
                    message= _ "This be the second cache, but looks like some’un found it before us. Empty."
                [/message]
                [event]
                    name=cache_found
                    [message]
                        speaker=Addogin
                        message= _ "Third ’n last cache, and it’s loaded! A clean 100 gold pieces."
                    [/message]
                    [sound]
                        name=gold.ogg
                    [/sound]

                    [gold]
                        side=1
                        amount=100
                    [/gold]
                    [message]
                        speaker=Gweddry
                        message= _ "Excellent! The gold from these caches will be very helpful."
                    [/message]
                    {CLEAR_VARIABLE caches_active}
                [/event]
            [/event]
        [/event]
    [/event]

    #---------------------------
    # REVEAL VILLAGES
    #---------------------------
    # remove shroud so the player has some idea where they should be trying to go, instead of wandering aimlessly
    [event]
        name=prestart
        [remove_shroud]
            terrain=*^V*
            radius=1
        [/remove_shroud]
    [/event]
    # remove fog so they player can more easily see when their villages get captured, even through fog
    # otherwise it's easy for the player not to realize what's happening
    [event]
        name=new turn
        first_time_only=no
        [reset_fog]
        [/reset_fog]
        [lift_fog]
            terrain=*^V*
            radius=1
            owner_side=1 # only if the village is owned
            multiturn=yes # so we can see what happens on enemies' turns
        [/lift_fog]
    [/event]

    #---------------------------
    # FLAVOR MESSAGES
    #---------------------------
    [event]
        name=attack

        [filter]
            id=Grug
        [/filter]

        [filter_second]
            type_adv_tree=Walking Corpse,Soulless,Ghoul
        [/filter_second]

        [message]
            speaker=Grug
            #po: this is probably grug's first time fighting squishy undead (though there were a few in S10)
            message= _ "Squishy fun to squash! Squash squish squash!"
        [/message]
    [/event]

    #--------------------
    # HOLY AMULET
    #--------------------
    {PLACE_ITEM_HOLY_AMULET {ID_HOLY_AMULET_3} 43 13}
    {PLACE_IMAGE items/rider-corpse.png 43 14}
    [event]
        name=prestart
        {GENERIC_UNIT 8 (Soulless) 43 14} {VARIATION horse} {GUARDIAN} # side 8 is the least scripted side
    [/event]
    [event]
        name=moveto
        [filter]
            side=1
            x,y=43,14
        [/filter]
        [message]
            speaker=unit
            #po: "odd that his body didn't become undead"
            message= _ "This man looks to have been dead for about a month. Odd that his body yet lies still."
        [/message]
    [/event]

    {PLACE_IMAGE items/book2.png 42 13}
    [event]
        name=moveto
        [filter]
            side=1
            x,y=42,13
        [/filter]
        [message]
            speaker=unit
            message= _ "There is a journal here, still intact despite the swamp."
        [/message]
        [message]
            speaker=unit
            #po: Reading from a dead horseman's journal. This is a warning to the player that there's a Skeletal Dragon in the scenario.
            message= _ "<i>... we thought the enemy had erred, that the battle could yet be won. Taking advantage of the gap, our swiftest riders drove toward the exposed heart of the skeletal formation. But even as they wrought destruction within the necromancers’ ranks, from out of the water came a great tremor, as if Irdya herself shivered in morbid anticipation.

For a moment, I espied a great bony figure, a shambling mass of tooth and claw and wing. Then it fell upon us from behind, and the enemy closed their ranks, and we were encircled by death.</i>"
        [/message]
        [message]
            speaker=unit
            #po: Reading more from the dead horseman's journal, whose corpse prompted the "odd that his body didn't become undead" comment.
            message= _ "<i>I fled, coward that I am, riding breakneck through the night before my injuries overcame my strength. And so it comes that I have earned no honorable death in battle, only the slow fatigue and fever borne by rot and poison.

I offer one final prayer to the Light, that at least my family may be spared and that my soul may be kept from undeath...</i>"
        [/message]
        {REMOVE_IMAGE 42 12}
        [set_achievement]
            content_for=eastern_invasion
            id=ei_S14
        [/set_achievement]
    [/event]

    #--------------------
    # ENEMY LEADERS SPOTTED
    #--------------------
#define REVEAL X Y
    [remove_shroud]
        x,y={X},{Y}
        radius=1
    [/remove_shroud]
    [lift_fog]
        [filter_side]
            side=1
        [/filter_side]
        x,y={X},{Y}
        radius=1
    [/lift_fog]
#enddef
#define RECRUIT SIDE TYPE X Y
    {VARIABLE recruit_type {TYPE}}
    [if]
        {VARIABLE_CONDITIONAL recruit_type not_equals none}
        [then]
            {GENERIC_UNIT {SIDE} ({TYPE}) {X} {Y}} {ANIMATE}
        [/then]
    [/if]
    {CLEAR_VARIABLE recruit_type}
#enddef
    [event]
        name=sighted

        [filter]
            id=Sir Seoraery
        [/filter]

        {INCIDENTAL_MUSIC knalgan_theme.ogg}
        {REVEAL $unit.x $unit.y}
        [message]
            speaker=Sir Seoraery
            #po: no-one sends a messenger, he's talking about memories from before he died
            message= _ "... we need... reinforcements... send a messenger..."
        [/message]
        [message]
            speaker=second_unit
            message= _ "I’m not your enemy!"
        [/message]
        [message]
            speaker=Sir Seoraery
            #po: he's talking of a memory, not the current situation
            message= _ "...must... hold... the line."
        [/message]
        {VARIABLE seoraery_active yes}
        [modify_side]
            side=2
            controller=ai
        [/modify_side]
        {RECRUIT 2 ({ON_DIFFICULTY "Skeleton Rider" "Bone Knight" "Bone Knight"}) 68 41}
        {RECRUIT 2 ({ON_DIFFICULTY "Skeleton Rider" "Bone Knight" "Bone Knight"}) 67 42}
        {RECRUIT 2 ({ON_DIFFICULTY "Skeleton Rider" "Chocobone"   "Chocobone"  }) 67 43}
        {RECRUIT 2 ({ON_DIFFICULTY "Skeleton Rider" "Bone Knight" "Bone Knight"}) 68 43}
        {RECRUIT 2 ({ON_DIFFICULTY "none"           "Chocobone"   "Chocobone"  }) 69 43}
        [event]
            name=side 2 turn end
            {RECRUIT 2 ({ON_DIFFICULTY "none" "Skeleton Rider" "Skeleton Rider"}) 68 41}
            {RECRUIT 2 ({ON_DIFFICULTY "none" "Skeleton Rider" "Bone Knight"   }) 67 42}
            {RECRUIT 2 ({ON_DIFFICULTY "none" "none"           "Bone Knight"   }) 67 43}
            {RECRUIT 2 ({ON_DIFFICULTY "none" "none"           "Skeleton Rider"}) 68 43}
            {RECRUIT 2 ({ON_DIFFICULTY "none" "none"           "Skeleton Rider"}) 69 43}
        [/event]
        [event]
            # turn end; don't let the second wave of recruits move on the same turn they're recruited
            name=side 8 turn end
            {MODIFY_UNIT side,canrecruit=2,no side 8}
            [modify_unit]
                [filter]
                    id=Sir Seoraery
                [/filter]

                canrecruit=no
                [object]
                    [effect]
                        apply_to=overlay
                        add=misc/leader-crown.png
                    [/effect]
                [/object]
            [/modify_unit]
        [/event]
    [/event]

    [event]
        name=sighted

        [filter]
            id=Sir Efran
        [/filter]

        {INCIDENTAL_MUSIC knalgan_theme.ogg}
        {REVEAL $unit.x $unit.y}
        [message]
            speaker=Sir Efran
            #po: he's talking of a memory, not the current situation
            message= _ "... there’s... too many."
        [/message]
        [message]
            speaker=second_unit
            message= _ "You and your men have been turned to undeath! Let us put you out of your misery."
        [/message]
        [message]
            speaker=Sir Efran
            #po: he's talking to a memory, not the unit that triggered the event
            message= _ "... I would sooner... die... than yield to you... necromancer."
        [/message]
        {VARIABLE efran_active yes}
        [modify_side]
            side=3
            controller=ai
        [/modify_side]
        {RECRUIT 3 ({ON_DIFFICULTY "Skeleton Rider" "Chocobone"   "Chocobone"  }) 17 6} # chobobones are thematic for a fire wight, plus they do well in the water
        {RECRUIT 3 ({ON_DIFFICULTY "Skeleton Rider" "Chocobone"   "Chocobone"  }) 18 5}
        {RECRUIT 3 ({ON_DIFFICULTY "Skeleton Rider" "Bone Knight" "Bone Knight"}) 18 4}
        {RECRUIT 3 ({ON_DIFFICULTY "Skeleton Rider" "Chocobone"   "Chocobone"  }) 17 4}
        {RECRUIT 3 ({ON_DIFFICULTY "none"           "Bone Knight" "Bone Knight"}) 16 4}
        [event]
            name=side 3 turn end
            {RECRUIT 3 ({ON_DIFFICULTY "none" "Skeleton Rider" "Skeleton Rider"}) 17 6}
            {RECRUIT 3 ({ON_DIFFICULTY "none" "Skeleton Rider" "Chocobone"     }) 18 5}
            {RECRUIT 3 ({ON_DIFFICULTY "none" "none"           "Chocobone"     }) 18 4}
            {RECRUIT 3 ({ON_DIFFICULTY "none" "none"           "Skeleton Rider"}) 17 4}
            {RECRUIT 3 ({ON_DIFFICULTY "none" "none"           "Skeleton Rider"}) 16 4}
        [/event]
        [event]
            # turn end; don't let the second wave of recruits move on the same turn they're recruited
            name=side 8 turn end
            {MODIFY_UNIT side,canrecruit=3,no side 8}
            [modify_unit]
                [filter]
                    id=Sir Efran
                [/filter]

                canrecruit=no
                [object]
                    [effect]
                        apply_to=overlay
                        add=misc/leader-crown.png
                    [/effect]
                [/object]
            [/modify_unit]
        [/event]
    [/event]

    [event]
        name=sighted

        [filter]
            id=Khrakrahs
        [/filter]

        {INCIDENTAL_MUSIC knalgan_theme.ogg}
        [object]
            [filter]
                id=Khrakrahs
            [/filter]

            [effect]
                apply_to=new_ability
                [abilities]
                    {ABILITY_TERROR}
                [/abilities]
            [/effect]

            [effect]
                apply_to=movement
                set=3
            [/effect]
            # needs to be slow, or else his escort can't keep up
        [/object]
        [fire_event]
            name=moveto
            [primary_unit]
                id=Khrakrahs # create the terror visual. Do it now, or else it's visible through the fog earlier
            [/primary_unit]
        [/fire_event]
        {REVEAL $unit.x $unit.y}
        [message]
            speaker=unit
            #po: undead dragon, talking to a memory rather than the unit that triggered him
            message= _ "... Lich, you do not belong here... leave, or I shall... rend you..."
        [/message]
        [message]
            speaker=Gweddry
            message= _ "Is that... an undead dragon? Dear Light! Raising such a powerful thrall must have been no small feat, even for a necromancer such as Mal-Ravanal."
        [/message]
        [message]
            speaker=Dacyn
            message= _ "Even a living dragon is nothing but flesh. This feeble imitation lacks even that. Smash it to pieces."
        [/message]
        [message]
            speaker=unit
            #po: undead dragon, again talking to a memory rather than anyone currently on the map
            message= _ "... I warned you... Ravanal... now <b>burn</b>."
        [/message]
        {VARIABLE khrakrahs_active yes}
        [modify_side]
            side=4
            controller=ai
        [/modify_side]
        {RECRUIT 4 ({ON_DIFFICULTY "Skeleton"        "Revenant"     "Revenant"    }) 7 49}
        {RECRUIT 4 ({ON_DIFFICULTY "Skeleton Archer" "Banebow"      "Banebow"     }) 8 49}
        {RECRUIT 4 ({ON_DIFFICULTY "Skeleton Archer" "Bone Shooter" "Bone Shooter"}) 8 50}
        {RECRUIT 4 ({ON_DIFFICULTY "Skeleton"        "Deathblade"   "Deathblade"  }) 7 51}
        {RECRUIT 4 ({ON_DIFFICULTY "none"            "Bone Shooter" "Bone Shooter"}) 6 50}
        {RECRUIT 4 ({ON_DIFFICULTY "none"            "Draug"        "Draug"       }) 6 49}
        [event]
            name=side 4 turn end
            {RECRUIT 4 ({ON_DIFFICULTY "none" "Skeleton"        "Revenant"       }) 7 49}
            {RECRUIT 4 ({ON_DIFFICULTY "none" "Skeleton Archer" "Bone Shooter"   }) 8 49}
            {RECRUIT 4 ({ON_DIFFICULTY "none" "none"            "Deathblade"     }) 8 50}
            {RECRUIT 4 ({ON_DIFFICULTY "none" "none"            "Skeleton"       }) 7 51}
            {RECRUIT 4 ({ON_DIFFICULTY "none" "none"            "Skeleton Archer"}) 6 50}
            {RECRUIT 4 ({ON_DIFFICULTY "none" "none"            "Skeleton"       }) 6 49}
        [/event]
        [event]
            # turn end; don't let the second wave of recruits move on the same turn they're recruited
            name=side 8 turn end
            {MODIFY_UNIT side,canrecruit=4,no side 8}
            [modify_unit]
                [filter]
                    id=Khrakrahs
                [/filter]

                canrecruit=no
                [object]
                    [effect]
                        apply_to=overlay
                        add=misc/leader-crown.png
                    [/effect]
                [/object]
            [/modify_unit]
        [/event]
    [/event]

    # keep recruits all a consistent color, even when later swapped to side 8
    [event]
        name=unit placed
        first_time_only=no

        [filter]
            side=2
        [/filter]

        {TEAM_COLOR_OVERRIDE id=$unit.id blue}
    [/event]
    [event]
        name=unit placed
        first_time_only=no

        [filter]
            side=3
        [/filter]

        {TEAM_COLOR_OVERRIDE id=$unit.id red}
    [/event]
    [event]
        name=unit placed
        first_time_only=no

        [filter]
            side=4
        [/filter]

        {TEAM_COLOR_OVERRIDE id=$unit.id brown}
    [/event]

    #--------------------
    # ATTACK VOICELINES
    #--------------------
    [event]
        name=attack

        [filter]
            id=Owaec
        [/filter]

        [filter_second]
            id=Sir Seoraery,Sir Efran
        [/filter_second]

        [message]
            speaker=second_unit
            #po: speaker is one of the undead leaders, in an [event]name=attack
            message= _ "... Owaec... why do you fight me? Join us... ride alongside your father... defend the plains..."
        [/message]
        [message]
            speaker=Owaec
            #po: speaking to one of the undead leaders, in an [event]name=attack
            message= _ "Abomination!! Clansmen though you once were, I have no choice but to grind you into dust now."
        [/message]
    [/event]
    [event]
        name=attack

        [filter]
            id=Owaec,Khrakrahs
        [/filter]

        [filter_second]
            id=Khrakrahs,Owaec
        [/filter_second]

        [message]
            speaker=Owaec
            #po: speaking to Khrakrahs in an [event]name=attack, although the anger seems misdirected
            message= _ "You! You are the cause of this destruction!! Vengeance for my fallen people!"
        [/message]
    [/event]
    [event]
        name=attack

        [filter]
            id=Terraent,Sir Seoraery,Sir Efran
        [/filter]

        [filter_second]
            id=Terraent,Sir Seoraery,Sir Efran
        [/filter_second]

        [message]
            speaker=Terraent
            #po: speaking to one of the undead leaders (horseman, not dragon) in an [event]name=attack
            message= _ "All things deserve redemption, but I fear the undead will never find it. May this place be cleansed in holy fire!"
        [/message]
    [/event]
    [event]
        name=attack

        [filter]
            id=Terraent,Khrakrahs
        [/filter]

        [filter_second]
            id=Khrakrahs,Terraent
        [/filter_second]

        [message]
            speaker=Terraent
            #po: speaking to Khrakrahs in an [event]name=attack
            message= _ "I have faced worse terrors than you, lizard! Through piety and the holy Light, this place will be sanctified once more."
        [/message]
    [/event]
    [event]
        name=attack

        [filter]
            id=Gaennell
        [/filter]

        [filter_second]
            id=Sir Seoraery,Sir Efran
        [/filter_second]

        [message]
            speaker=second_unit
            #po: speaker is an undead horseman, who's fighting Gaennell. Unclear whether this is to a memory, or because Gaennell is a Shadow Mage
            message= _ "... Necromancer... you are a blight..."
        [/message]
        [message]
            speaker=Gaennell
            #po: to an undead horseman in an [event]name=attack
            message= _ "Not sure what you’d call me anymore, but definitely not more of a blight than you."
        [/message]
    [/event]
    [event]
        name=attack

        [filter]
            type=Red Mage,Silver Mage,Arch Mage,Great Mage
        [/filter]

        [filter_second]
            id=Khrakrahs
        [/filter_second]

        [message]
            speaker=second_unit
            #po: Attacked with a fireball from an L2 or higher mage.
            #po: Shek’kahan was Khrakrahs' brother, but died during TRoW, 6 centuries ago.
            #po: The date of Khrakrahs' own death isn't canon, he was still alive in 40 YW (he appears in SoF).
            message= _ "... Shek’kahan, your fire grows weak... you are hardly... a rival."
        [/message]
    [/event]
    [event]
        name=attack

        [filter]
            race=dwarf
        [/filter]

        [filter_second]
            id=Khrakrahs
        [/filter_second]

        [message]
            speaker=second_unit
            #po: speaker is Khrakrahs when attacked by a dwarf, but remembering one he met in 40 YW.
            message= _ "... Thursagan... I warned you not... to enter my lair."
        [/message]
    [/event]

    #--------------------
    # LEADER LAST BREATHS
    #--------------------
    [event]
        name=last breath

        [filter]
            id=Lord Alric
        [/filter]

        [message]
            speaker=Lord Alric
            #po: last breath request from the ghost of Lord Alric
            message= _ "... please, watch over my son for me."
        [/message]
    [/event]
    [event]
        name=last breath

        [filter]
            id=Sir Seoraery
        [/filter]

        [message]
            speaker=Sir Seoraery
            message= _ "... I may die... but the Clans... will never fall."
        [/message]
    [/event]
    [event]
        name=last breath

        [filter]
            id=Sir Efran
        [/filter]

        [message]
            speaker=Sir Efran
            #po: last breath, but still talking to a memory rather than the unit currently fighting him
            message= _ "... Necromancer... Wesnoth will defeat you... in the end."
        [/message]
    [/event]
    [event]
        name=last breath

        [filter]
            id=Khrakrahs
        [/filter]

        [message]
            speaker=Khrakrahs
            #po: last breath
            message= _ "I am... ...<i>cold</i>."
        [/message]
    [/event]

    #--------------------
    # FLAVOR
    #--------------------
    [event]
        name=last breath

        [filter]
            id=Sir Seoraery,Sir Efran,Khrakrahs
        [/filter]

        [event]
            name=new turn
            [event]
                name=new turn
                [message]
                    speaker=Gweddry
                    #po: spoken at the start of a new turn after killing the first undead leader
                    message= _ "Dacyn, I’ve been thinking. This artifact has had such a draining effect on you. Perhaps you should rest and take a moment to regain your strength? I could carry the Amulet for a while, or we could store it away."
                [/message]
                [message]
                    speaker=Dacyn
                    message= _ "Hmm... I suppose there would be no harm in locking it up somewhere safe."
                [/message]
                [message]
                    caption= _ "Unknown"
                    image=units/unknown-unit1.png
                    #po: voices from the amulet talking to Dacyn
                    message= _ "<span size='small' font-style='italic'>You have a great weapon... and you would simply lock it away?</span>"
                [/message]
                [message]
                    caption= _ "Unknown 2"
                    image=units/unknown-unit2.png
                    #po: voices from the amulet talking to Dacyn, speaking of Gweddry
                    message= _ "<span size='small' font-style='italic'>His intent is good, but his spirit is weak... he would be corrupted...</span>"
                [/message]
                [message]
                    caption= _ "Unknown"
                    image=units/unknown-unit1.png
                    #po: voices from the amulet talking to Dacyn
                    message= _ "<span size='small' font-style='italic'>Only you can be trusted with this responsibility.</span>"
                [/message]
                [message]
                    speaker=Dacyn
                    message= _ "But on the other hand... this is a dangerous artifact. I fear that only I can withstand its energies."
                [/message]
                [message]
                    speaker=Gweddry
                    message= _ "Well, you’re the magic expert."
                [/message]
            [/event]
        [/event]
    [/event]

    #--------------------
    # VICTORY / DEFEAT
    #--------------------
    # time running out warning
    [event]
        name=side 1 turn {TURNS_LOW_WARNING}
        [message]
            speaker=Gweddry
            message= _ "We must finish here and hurry on from this dead place! If the Horse Clans have fallen so completely, I fear for Weldyn and the rest of Wesnoth."
        [/message]
        [message]
            speaker=Dacyn
            message= _ "Why are we here at all? We should be advancing towards Weldyn, not humoring Owaec’s lust for vengeance. The Plains are already drowned."
        [/message]
    [/event]
    # time over
    [event]
        name=side 1 turn {SCENARIO_TURN_LIMIT} end
        [message]
            speaker=Dacyn
            #po: time over, talking to Owaec
            message= _ "Enough of this. We have wasted too much time here already; give up your foolish quest of vengeance and travel on."
        [/message]
        [message]
            speaker=Owaec
            #po: time over, talking to Dacyn
            message= _ "Your search for foul magic has caused enough harm already, and now you would leave my countrymen’s bodies defiled by undeath? I will slay everything that moves in this swamp, and once I am done, perhaps my hammer will find you next!"
        [/message]
        [message]
            speaker=Dacyn
            message= _ "You fools are welcome to linger here clearing this swamp, but I intend to travel on alone and defeat Mal-Ravanal. Go as you wish on your own. I have a task to complete."
        [/message]
        [endlevel]
            result=defeat
        [/endlevel]
    [/event]

    # are all enemy leaders dead?
    [event]
        name=die
        first_time_only=no

        [filter]
            id=Sir Seoraery,Sir Efran,Khrakrahs
        [/filter]

        [if]
            [not]
                [have_unit]
                    id=Sir Seoraery,Sir Efran,Khrakrahs
                [/have_unit]
            [/not]

            [then]
                [endlevel]
                    result=victory
                    bonus=yes
                    {NEW_GOLD_CARRYOVER 40}
                [/endlevel]
            [/then]
        [/if]
    [/event]
    [event]
        name=victory
        {KILL_COUNT 4 side=2,3,4,6,8 ANIMATE=yes}
        {KILL side=2,3,4,5,6,7,8}
        [message]
            speaker=Owaec
            message= _ "It is done. My kinsmen have been avenged."
        [/message]

        [delay]
            time=1000
        [/delay]

        [message]
            speaker=Owaec
            message= _ "Mage... I owe you an apology. This destruction... even had we journeyed here instead of questing north, we would most assuredly have made no difference. Of that I am now certain.

The fates may yet reveal that our journey north was necessary, even at such a steep cost. I will stand by you in this fight."
        [/message]
        [message]
            speaker=Dacyn
            message= _ "Apology accepted."
        [/message]

        [delay]
            time=2000
        [/delay]

        [message]
            speaker=Dacyn
            message= _ "And I too must admit, I underestimated the power in this Amulet. Even now, voices gnaw at the edges of my mind..."
        [/message]
        [message]
            speaker=Owaec
            #po: to Dacyn
            message= _ "Make no mistake, I still do not approve of your plan! It will lead you only to ruin and death! Yet, at least now I believe that your intentions are good despite your questionable methods."
        [/message]
        [message]
            speaker=Gweddry
            message= _ "I’m glad we have prevailed here, but I fear greatly for Wesnoth if the Clans have been defeated so thoroughly so quickly. It has scant been a year since their invasion first began. Owaec, Dacyn, if you are prepared to travel, we must press toward Weldyn at once."
        [/message]
        {CLEAR_VARIABLE seoraery_active,efran_active,khrakrahs_active,caches_active}
    [/event]

    {WESNOTH_DEFEAT}

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

#undef SCENARIO_TURN_LIMIT
#undef TURNS_LOW_WARNING
