#textdomain wesnoth-After_the_Storm

[scenario]
    id=11_A_Final_Confrontation
    name= _ "A Final Confrontation"
    {MAP 11_A_Final_Confrontation.map}
    {TURNS 36 34 32}
    next_scenario=12_Fate
    victory_when_enemies_defeated=no
    {DO_NOT_REMOVE_FROM_CARRYOVER_ON_DEFEAT}

    {E_DEATHS}

    {SCENARIO_MUSIC       "suspense.ogg"} {CONTINUE_PLAYING_STORY_MUSIC_FIRST}
    {EXTRA_SCENARIO_MUSIC "the_deep_path.ogg"}

    {STORYTXT_A_FINAL_CONFRONTATION}

    {INDOORS_HIVE}

    {SPAWN_CONTROLLER}

    # wmllint: validate-off
    [side]
        side=1
        controller=human
        save_id=player
        team_name=good
        user_team_name= _ "team_name^Elynia"

        {GOLD 250 225 200}
        {INCOME 4 3 2}

        shroud=yes

        # wmllint: recognize Elynia
        {CHARACTER_STATS_ELYNIA}
    [/side]
    # wmllint: validate-on

    #
    # Boss controller
    #
    [side]
        side=2
        team_name=evil
        user_team_name= _ "team_name^Mal Hekuba"
        color=gold
        {CHAOS_FLAG}

        hidden=yes
        no_leader=yes

        {NO_ECONOMY}

        {ai/aliases/stable_singleplayer.cfg}
        [ai]
            {AI_SIMPLE_ALWAYS_ASPECT aggression          5.00}
            {AI_SIMPLE_ALWAYS_ASPECT caution             0.00}
            {AI_SIMPLE_ALWAYS_ASPECT recruitment_pattern "scout,mixed fighter,scout,scout"}
        [/ai]

        [unit]
#ifdef EASY
            type=Necrophage
#else
            type=Ghast
#endif
            ai_special=guardian
            upkeep=free
            x,y=43,38
            facing=se
            [modifications]
                {TRAIT_UNDEAD}
            [/modifications]
        [/unit]

        [unit]
#ifdef EASY
            type=Necrophage
#else
            type=Ghast
#endif
            ai_special=guardian
            upkeep=free
            x,y=39,38
            facing=ne
            [modifications]
                {TRAIT_UNDEAD}
            [/modifications]
        [/unit]
    [/side]

    [side]
        side=3
        team_name=evil
        user_team_name= _ "team_name^Mal Hekuba"
        color=blue
        {CHAOS_FLAG}
        controller=null

        hidden=yes
        no_leader=yes

        {NO_ECONOMY}
    [/side]

    #
    # Guards
    #
    [side]
        side=4
        team_name=evil
        user_team_name= _ "team_name^Chamber Guards"
        {CHAOS_FLAG}

        no_leader=yes
        hidden=yes

        {NO_ECONOMY}
        recruit=Demon,Imp,Chaos Headhunter

        {ai/aliases/stable_singleplayer.cfg}
        [ai]
            {AI_SIMPLE_ALWAYS_ASPECT aggression          1.00}
            {AI_SIMPLE_ALWAYS_ASPECT caution             0.00}
            {AI_SIMPLE_ALWAYS_ASPECT recruitment_pattern "mixed fighter,fighter,mixed fighter,scout,fighter"}
        [/ai]

        [unit]
            type=Demon Zephyr
            gender=female
            id=Azalia
            name= _ "Azalia"
            ai_special=guardian
            upkeep=free
            x,y=36,40
            facing=ne
            [modifications]
                {TRAIT_INTELLIGENT}
                {TRAIT_RESILIENT}
            [/modifications]
        [/unit]
        [unit]
            type=Demon Zephyr
            gender=male
            id=Rhadiarch
            name= _ "Rhadiarch"
            ai_special=guardian
            upkeep=free
            x,y=44,33
            facing=s
            [modifications]
                {TRAIT_STRONG}
                {TRAIT_QUICK}
            [/modifications]
        [/unit]
#ifndef EASY
        [unit]
            type=Demon Zephyr
            gender=female
            id=Galaviel
            name= _ "Galaviel"
            ai_special=guardian
            upkeep=free
            x,y=47,37
            facing=sw
            [modifications]
                {TRAIT_QUICK}
                {TRAIT_DEXTROUS}
            [/modifications]
        [/unit]
#endif
    [/side]

    [side]
        side=5
        team_name=evil
        user_team_name= _ "team_name^Chamber Guards"
        {CHAOS_FLAG}

        no_leader=yes
        hidden=yes

        {NO_ECONOMY}
        recruit=Automaton,Shaxthal Drone,Chaos Invoker

        {ai/aliases/stable_singleplayer.cfg}
        [ai]
            {AI_SIMPLE_ALWAYS_ASPECT aggression          1.00}
            {AI_SIMPLE_ALWAYS_ASPECT caution             0.00}
            {AI_SIMPLE_ALWAYS_ASPECT recruitment_pattern "fighter,mixed fighter,fighter,archer"}
        [/ai]

        [unit]
            type=Shaxthal Enforcer Drone
            ai_special=guardian
            upkeep=free
            x,y=37,32
            facing=sw
            [modifications]
                {TRAIT_BIOMECHANICAL}
                {TRAIT_RESILIENT}
                {UNIT_PALETTE_SWITCH () shaxthal_drone_base shaxthal_drone_purple}
            [/modifications]
        [/unit]

        [unit]
            type=Shaxthal Enforcer Drone
            ai_special=guardian
            upkeep=free
            x,y=33,34
            facing=se
            [modifications]
                {TRAIT_BIOMECHANICAL}
                {TRAIT_ARMORED}
                {UNIT_PALETTE_SWITCH () shaxthal_drone_base shaxthal_drone_purple}
            [/modifications]
        [/unit]

        [unit]
            type=Shaxthal Enforcer Drone
            ai_special=guardian
            upkeep=free
            x,y=48,21
            facing=sw
            [modifications]
                {TRAIT_BIOMECHANICAL}
                {TRAIT_QUICK}
                {UNIT_PALETTE_SWITCH () shaxthal_drone_base shaxthal_drone_purple}
            [/modifications]
        [/unit]
    [/side]

    #
    # Hive spawns
    #
    [side]
        side=6
        team_name=evil
        user_team_name= _ "team_name^Dark Hive"
        {CHAOS_FLAG}

        no_leader=yes
        hidden=yes
        {NO_ECONOMY}

        {ai/aliases/stable_singleplayer.cfg}
        [ai]
            {AI_SIMPLE_ALWAYS_ASPECT aggression 5.00}
            {AI_SIMPLE_ALWAYS_ASPECT caution    0.00}
            {AI_SIMPLE_ALWAYS_ASPECT grouping     no}
        [/ai]
    [/side]

#define I_DOOR _X _Y
    {GENERIC_UNIT () Door ({_X}) ({_Y})} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN}
#enddef

    #
    # Door controller
    #
    [side]
        side=7
        team_name=evil
        user_team_name= _ "team_name^Dark Hive"
        {CHAOS_FLAG}
        color=black

        no_leader=yes
        hidden=yes
        {NO_ECONOMY}

        {I_DOOR 35 36}
        {I_DOOR 36 35}
        {I_DOOR 37 35}
    [/side]

    #
    # Dummy stand-in for side 1
    #
    [side]
        side=8
        team_name=good,evil
        user_team_name= _ "team_name^Elynia"
        color=red
        controller=null

        no_leader=yes
        hidden=yes
        {NO_ECONOMY}
    [/side]

#undef I_DOOR

#define I_DRONE _RESPAWN_TURNS _TYPES _X _Y
    {TIMED_DRONE_SPAWNER ({_RESPAWN_TURNS}) (type={_TYPES}) 6 ({_X}) ({_Y})}
#enddef

    {I_DRONE 4 (Shaxthal Drone) 35 45}
    {I_DRONE 8 (Shaxthal Drone) 33 42}
#ifndef EASY
    {I_DRONE 8 (Shaxthal Drone) 33 43}
#endif

    {I_DRONE 1 (Shaxthal Drone) 51 37}
    {I_DRONE 1 (Shaxthal Drone) 53 35}

    {I_DRONE 3 (Shaxthal Drone,Shaxthal Sentry Drone,Shaxthal Sentry Drone) 12 19}
    {I_DRONE 4 (Shaxthal Drone,Shaxthal Sentry Drone,Shaxthal Sentry Drone) 22 10}
    {I_DRONE 5 (Shaxthal Drone,Shaxthal Sentry Drone,Shaxthal Sentry Drone) 20 10}
    {I_DRONE 4 (Shaxthal Drone,Shaxthal Sentry Drone,Shaxthal Sentry Drone) 34 13}
    {I_DRONE 3 (Shaxthal Drone,Shaxthal Sentry Drone,Shaxthal Sentry Drone) 36 10}
    {I_DRONE 5 (Shaxthal Drone,Shaxthal Sentry Drone,Shaxthal Sentry Drone) 38  9}
    {I_DRONE 4 (Shaxthal Drone,Shaxthal Sentry Drone,Shaxthal Sentry Drone) 26 10}

    {I_DRONE 1 (Shaxthal Drone,Shaxthal Sentry Drone,Shaxthal Sentry Drone)  8 32}
    {I_DRONE 1 (Shaxthal Drone,Shaxthal Sentry Drone,Shaxthal Sentry Drone)  7 35}
    {I_DRONE 1 (Shaxthal Drone,Shaxthal Sentry Drone,Shaxthal Sentry Drone)  6 37}

    {I_DRONE 1 (Shaxthal Drone,Shaxthal Sentry Drone,Shaxthal Sentry Drone)  3 23}
    {I_DRONE 1 (Shaxthal Drone,Shaxthal Sentry Drone,Shaxthal Sentry Drone)  2 21}
    {I_DRONE 1 (Shaxthal Drone,Shaxthal Sentry Drone,Shaxthal Sentry Drone)  4 20}
    {I_DRONE 1 (Shaxthal Drone,Shaxthal Sentry Drone,Shaxthal Sentry Drone)  6 15}
    {I_DRONE 1 (Shaxthal Drone,Shaxthal Sentry Drone,Shaxthal Sentry Drone) 11 10}
    {I_DRONE 1 (Shaxthal Drone,Shaxthal Sentry Drone,Shaxthal Sentry Drone) 11 12}
    {I_DRONE 1 (Shaxthal Drone,Shaxthal Sentry Drone,Shaxthal Sentry Drone) 19  8}
    {I_DRONE 1 (Shaxthal Drone,Shaxthal Sentry Drone,Shaxthal Sentry Drone) 24  6}
    {I_DRONE 1 (Shaxthal Drone,Shaxthal Sentry Drone,Shaxthal Sentry Drone) 27  8}
    {I_DRONE 1 (Shaxthal Drone,Shaxthal Sentry Drone,Shaxthal Sentry Drone) 28  4}
    {I_DRONE 1 (Shaxthal Drone,Shaxthal Sentry Drone,Shaxthal Sentry Drone) 32  7}
    {I_DRONE 1 (Shaxthal Drone,Shaxthal Sentry Drone,Shaxthal Sentry Drone) 32  5}
    {I_DRONE 1 (Shaxthal Drone,Shaxthal Sentry Drone,Shaxthal Sentry Drone) 34  6}

#undef I_DRONE

    {STARTING_VILLAGES 1 6}

    {CAVE_NOISE_SOUND_SOURCE}
    [+sound_source]
        id=cave_sound_source # wmllint: ignore
    [/sound_source]

    {CAVE_WATER_SOUND_SOURCE 24 36}
    [+sound_source]
        id=water_sound_source # wmllint: ignore
    [/sound_source]

    [event]
        name=prestart

        {VARIABLE boss_stage 0}

        #
        # Set-up boss unit spawning
        #
        # The population cap for a given spawn set must never exceed the number
        # of flags on the map for it.
        #

        [set_variables]
            name=boss_spawn
            [literal]
                # Next turn for spawning units
                next_turn=0
                # How many turns to wait before spawning units again
                turn_delay={DIFF 4 3 3}
                # Population cap and types for mobile Shaxthal units (flag A)
                shaxthal_cap={DIFF 3 3 4}
#ifndef EASY
                shaxthal_types=Shaxthal Drone,Shaxthal Drone,Shaxthal Sentry Drone,Shaxthal Sentry Drone,Shaxthal Sentry Drone,Shaxthal Enforcer Drone
#else
                shaxthal_types=Shaxthal Drone,Shaxthal Drone,Shaxthal Drone,Shaxthal Sentry Drone,Shaxthal Sentry Drone
#endif
                # Population cap and types for mobile demons (flag B)
                demon_cap={DIFF 2 3 4}
#ifndef EASY
                demon_types=Demon Zephyr,Demon Zephyr,Demon Zephyr,Demon Zephyr,Demon Spelldancer,Spectre,Wraith
#else
                demon_types=Demon Zephyr,Demon Zephyr,Demon Zephyr,Demon Zephyr,Demon Zephyr,Wraith,Wraith,Spectre
#endif
            [/literal]
        [/set_variables]

        [store_locations]
            terrain=*^&A
            variable=boss_spawn.shaxthal_locs
        [/store_locations]

        [store_locations]
            terrain=*^&B
            variable=boss_spawn.demon_locs
        [/store_locations]

        {DIE_ON (
            [not]
                {VARIABLE_NUMERICAL_GREATER_THAN_OR_EQUAL boss_spawn.shaxthal_locs.length $boss_spawn.shaxthal_cap}
                {VARIABLE_NUMERICAL_GREATER_THAN_OR_EQUAL boss_spawn.demon_locs.length    $boss_spawn.demon_cap}
            [/not]
        ) ("E2S11 boss spawn setup failed (map/config mismatch)")}

        [remove_terrain_overlays]
            find_in=boss_spawn.shaxthal_locs
            [or]
                find_in=boss_spawn.demon_locs
            [/or]
        [/remove_terrain_overlays]

        {LOG_ATS_DBG ("E2 boss: shaxthal_locs = $boss_spawn.shaxthal_locs.length; demon_locs = $boss_spawn.demon_locs.length")}

        # wmllint: recognize Anya
        {RECALL_ANYA_AT_LOCATION   47 49}
        # wmllint: recognize Durvan
        {RECALL_DURVAN_AT_LOCATION 49 48}

        {FACE_DIRECTION (side=1) nw}

        {OBJECTIVES (
            victory_string= _ "Current Objectives:"
            {OBJECTIVE_VICTORY ( _ "Proceed further underground with Elynia to find Mal Hekuba")}

            {OBJECTIVE_DEFEAT  ( _ "Death of Elynia")}
            {OBJECTIVE_DEFEAT  ( _ "Death of Anya")}
            {OBJECTIVE_DEFEAT  ( _ "Death of Durvan")}

            {TURNS_RUN_OUT}
        )}
    [/event]

    [event]
        name=start
        [message]
            speaker=Durvan
            message= _ "This goes deeper and deeper—"
        [/message]

        {QUAKE "rumble.ogg"}

        {QUAKE "cave-in.ogg"}

        [message]
            speaker=Elynia
            message=  _ "... I fear we don’t have much more time. Just where in this accursed place are you, Hekuba?"
        [/message]
    [/event]

    [event]
        name=DEBUG0

        [teleport]
            [filter]
                id=Elynia
            [/filter]
            x,y=28,24
        [/teleport]

        [teleport]
            [filter]
                id=Anya
            [/filter]
            x,y=28,23
        [/teleport]

        [teleport]
            [filter]
                id=Durvan
            [/filter]
            x,y=27,25
        [/teleport]

        #[fire_event]
        #    name=lift central barriers
        #[/fire_event]

        [kill]
            type=Goliath
        [/kill]

        [fire_event]
            name=moveto
            [primary_unit]
                id=Elynia
            [/primary_unit]
        [/fire_event]
    [/event]

    [event]
        name=DEBUG1

        [fire_event]
            name=DEBUG0
        [/fire_event]

        [fire_event]
            name=boss stage 1
        [/fire_event]
    [/event]

    [event]
        name=DEBUG2

        [fire_event]
            name=DEBUG1
        [/fire_event]

        [fire_event]
            name=boss stage 2
        [/fire_event]

        [kill]
            x,y=25,20
        [/kill]

        [teleport]
            [filter]
                id=Elynia
            [/filter]
            x,y=25,20
        [/teleport]

        # Nothing important takes place here.
        #[fire_event]
        #    name=moveto
        #    [primary_unit]
        #        id=Elynia
        #    [/primary_unit]
        #[/fire_event]

        [fire_event]
            name=boss stage 3
        [/fire_event]

        [fire_event]
            name=boss stage 4
        [/fire_event]
    [/event]

    [event]
        name=DEBUG3

        [fire_event]
            name=DEBUG2
        [/fire_event]

        [kill]
            animate=yes
            fire_event=yes
            id=Ivyel
        [/kill]
    [/event]

    [event]
        name=die
        [filter]
            type=Door
        [/filter]

#define I_CAPTURE_STARTING_VILLAGES _SIDE _RADIUS
    [store_starting_location]
        side={_SIDE}
        variable=temp_I_CAPTURE_STARTING_VILLAGES
    [/store_starting_location]

    {CAPTURE_VILLAGES_OF_TYPE (*^V*) {_SIDE} $temp_I_CAPTURE_STARTING_VILLAGES.x $temp_I_CAPTURE_STARTING_VILLAGES.y {_RADIUS}}

    {CLEAR_VARIABLE temp_I_CAPTURE_STARTING_VILLAGES}
#enddef

        [modify_side]
            side=4
            hidden=no
            income=0
            {GOLD 100 120 140}
            village_gold=1
        [/modify_side]

        [unit]
            canrecruit=yes
            type=Chaos Lorekeeper
            id=Jarlean
            name= _ "Jarlean"
            side=4
            x,y=46,25
            [modifications]
                {TRAIT_INTELLIGENT}
                {TRAIT_QUICK}
                {BOSS_BOOST 60% 1 0 0 0}
            [/modifications]
        [/unit]

        {I_CAPTURE_STARTING_VILLAGES 4 8}

        [modify_side]
            side=5
            hidden=no
            income=0
            {GOLD 100 120 140}
            village_gold=1
        [/modify_side]

        [unit]
            canrecruit=yes
            type=Chaos Arbalestier
            id=Terryl
            name= _ "Terryl"
            side=5
            x,y=49,18
            [modifications]
                {TRAIT_STRONG}
                {TRAIT_RESILIENT}
                {BOSS_BOOST 15% 0 0 1 0}
            [/modifications]
        [/unit]

        {I_CAPTURE_STARTING_VILLAGES 5 4}

#undef I_CAPTURE_STARTING_VILLAGES
    [/event]

    [event]
        name=die
        [filter]
            side=4
            canrecruit=yes
        [/filter]
        [filter_condition]
            [have_unit]
                side=5
                canrecruit=yes
            [/have_unit]
        [/filter_condition]

        [gold]
            side=5
            {QUANTITY amount 110 120 140}
        [/gold]
    [/event]

    [event]
        name=die
        [filter]
            side=5
            canrecruit=yes
        [/filter]
        [filter_condition]
            [have_unit]
                side=4
                canrecruit=yes
            [/have_unit]
        [/filter_condition]

        [gold]
            side=4
            {QUANTITY amount 110 120 140}
        [/gold]
    [/event]

    [event]
        name=lift central barriers

        {LOCK_VIEW}

        [unit]
            side=2
            x,y=22,13
            facing=se
            type=Goliath
            upkeep=free
            ai_special=guardian
        [/unit]

        [unit]
            side=2
            x,y=36,20
            facing=se
            type=Goliath
            upkeep=free
            ai_special=guardian
        [/unit]

        [unit]
            side=2
            x,y=35,26
            facing=s
            type=Goliath
            upkeep=free
            ai_special=guardian
        [/unit]

        [unit]
            side=2
            x,y=17,26
            facing=ne
            type=Goliath
            upkeep=free
            ai_special=guardian
        [/unit]

        {QUAKE "rumble.ogg"}

#define I_BARRIER_LOCS_NORTH
    x=40,40,40,40,40,40,41,41,41,41,41,41
    y=18,19,20,21,22,23,18,19,20,21,22,23
#enddef
#define I_BARRIER_LOCS_SOUTH
    x=28,29,29,30,30,31,31,32,32,33,33,34,34,35,35,36,36,37
    y=31,31,32,30,31,30,31,29,30,29,30,28,29,28,29,27,28,28
#enddef
        [message]
            speaker=narrator
            image=wesnoth-icon.png
            message= _ "The earth shakes."
        [/message]

        [terrain]
            terrain="Ur^Es"
            {I_BARRIER_LOCS_NORTH}
            [or]
                {I_BARRIER_LOCS_SOUTH}
            [/or]
        [/terrain]

        [remove_shroud]
            side=1
            {I_BARRIER_LOCS_NORTH}
            [or]
                {I_BARRIER_LOCS_SOUTH}
            [/or]
        [/remove_shroud]

        [redraw]
            side=1
        [/redraw]

        [scroll_to]
            x,y=41,21
        [/scroll_to]

        {HIGHLIGHT_GOAL ({I_BARRIER_LOCS_NORTH})}

        [scroll_to]
            x,y=33,30
        [/scroll_to]

        {HIGHLIGHT_GOAL ({I_BARRIER_LOCS_SOUTH})}

        [scroll_to]
            x,y=$x1,$y1
        [/scroll_to]

#undef I_BARRIER_LOCS_NORTH
#undef I_BARRIER_LOCS_SOUTH

        {OBJECTIVES (
            victory_string= _ "Current Objectives:"
            {OBJECTIVE_VICTORY ( _ "Move Elynia to the central chamber")}

            {OBJECTIVE_DEFEAT  ( _ "Death of Elynia")}
            {OBJECTIVE_DEFEAT  ( _ "Death of Anya")}
            {OBJECTIVE_DEFEAT  ( _ "Death of Durvan")}

            {TURNS_RUN_OUT}
        )}

        {UNLOCK_VIEW}
    [/event]

    {ITEM_CRYSTAL_GLYPH_GATE 53 17}

    [event]
        name=moveto
        [filter]
            side=1
            x,y=53,17
        [/filter]

        [redraw][/redraw]

        [message]
            speaker=narrator
            image="items/crystal-glyph.png"
            sound="magic-3.ogg"
            message= _ "Access granted."
        [/message]

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

        [fire_event]
            name=lift central barriers
            [primary_unit]
                x,y=$x1,$y1
            [/primary_unit]
        [/fire_event]
    [/event]

#define I_BOSS_LOC
    x=24
    y=19
#enddef

#define I_CORE_LOCS
    x=24,28,20,20,28,24
    y=15,17,17,21,21,23
#enddef

    [event]
        name=side 2 turn refresh
        first_time_only=no

        {LOCK_VIEW}

        [animate_control_spires]
            type=Verlissh Control Spire
        [/animate_control_spires]

        # wmllint: recognize Mal Hekuba
        [if]
            [have_unit]
                id=Mal Hekuba
            [/have_unit]
            [then]
                # We are in the boss sequence.
                {QUAKE ({SOUND_LIST:COLLAPSING_FACILITY})}
            [/then]
        [/if]

        {UNLOCK_VIEW}
    [/event]

    #
    # Boss sequence trigger.
    #

    [event]
        name=moveto
        [filter]
            id=Elynia
            x=13-32
            y=12-25
        [/filter]

        {LOCK_VIEW}

        [allow_undo][/allow_undo]

        [event]
            delayed_variable_substitution=no
            name="turn $($turn_number + 1)"
            [fire_event]
                name=boss stage 1
            [/fire_event]
        [/event]

        [message]
            speaker=Elynia
            message= _ "Be careful. I’m sure he is in this chamber, somewhere..."
        [/message]

        {FACE_LOCATION (id=Elynia) ({I_BOSS_LOC})}

        [remove_shroud]
            side=1
            {I_BOSS_LOC}
            radius=7
            # The central block is Xu, can't use this without an exception!
            #[filter_radius]
            #    [not]
            #        terrain=X*,X*^*,*^X*
            #    [/not]
            #[/filter_radius]
        [/remove_shroud]

        [redraw]
            side=1
        [/redraw]

        [scroll_to]
            {I_BOSS_LOC}
        [/scroll_to]

        [delay]
            time=750
        [/delay]

        [message]
            speaker=Elynia
            message= _ "Most likely on that tall platform at the center. We need to get up there somehow."
        [/message]

        {FACE_LOCATION (side=1) ({I_BOSS_LOC})}

        {QUAKE "rumble.ogg"}

        [message]
            speaker=Anya
            message= _ "What’s that noise?"
        [/message]

        {QUAKE "cave-in.ogg"}
        {QUAKE "rumble.ogg"}

        [store_locations]
            {I_CORE_LOCS}
            variable=core_locs
        [/store_locations]

        [foreach]
            array=core_locs
            variable=core
            [do]
                [terrain]
                    x,y=$core.x,$core.y
                    terrain=Cud^Xo
                [/terrain]

                [redraw][/redraw]

                [unit]
                    type=Verlissh Control Spire
                    side=2
                    x,y=$core.x,$core.y
                    upkeep=free
                    ai_special=guardian
                [/unit]
            [/do]
        [/foreach]

        [redraw][/redraw]

        {CLEAR_VARIABLE core_locs}

        [scroll_to]
            {I_BOSS_LOC}
        [/scroll_to]

        [delay]
            time=250
        [/delay]

        [animate_control_spires]
            type=Verlissh Control Spire
        [/animate_control_spires]

        [delay]
            time=250
        [/delay]

        [message]
            speaker=Elynia
            message= _ "... That energy... It’s the same..."
        [/message]

        [message]
            speaker=Durvan
            message= _ "Elynia? What’s happening?"
        [/message]

        [delay]
            time=750
        [/delay]

        [message]
            speaker=Elynia
            message= _ "... A dark energy emanates from those spires... It’s the same energy that was released in Wesmere when we destroyed the creature within the hive. "
        [/message]

        [message]
            speaker=Anya
            message= _ "How should we destroy them?"
        [/message]

        [message]
            speaker=Elynia
            message= _ "Search the chamber — there must be some mechanism controlling them."
        [/message]

        # Increase turn limit.

        [modify_turns]
            {QUANTITY add 24 22 20}
        [/modify_turns]

        {OBJECTIVES (
            victory_string= _ "Current Objectives:"
            {OBJECTIVE_VICTORY ( _ "Explore the central chamber")}

            {OBJECTIVE_DEFEAT  ( _ "Death of Elynia")}
            {OBJECTIVE_DEFEAT  ( _ "Death of Anya")}
            {OBJECTIVE_DEFEAT  ( _ "Death of Durvan")}

            {TURNS_RUN_OUT}
        )}

        {UNLOCK_VIEW}
    [/event]

    ############################################################################
    #
    # BOSS FIGHT STAGE 1
    #
    ############################################################################

    [event]
        name=boss stage 1
        first_time_only=yes

        {VARIABLE boss_stage 1}

        {LOCK_VIEW}

        {QUAKE "rumble.ogg"}

        {FACE_LOCATION (side=1) ({I_BOSS_LOC})}

        [terrain]
            {I_BOSS_LOC}
            radius=1
            terrain=Yhl^Cov
        [/terrain]

        [terrain]
            {I_BOSS_LOC}
            terrain=Yhl^Kov
        [/terrain]

        [item]
            {I_BOSS_LOC}
            image="items/ball-magenta.png"
            halo="halo/heart-aura.png"
        [/item]

        [modify_side]
            side=2
            hidden=no
            recruit=Demon Zephyr,Shaxthal Sentry Drone
            gold=50
            income=25
        [/modify_side]

        [store_direction]
            [from]
                {I_BOSS_LOC}
            [/from]
            [to]
                [filter]
                    id=Elynia
                [/filter]
            [/to]
        [/store_direction]

#define I_EFFECT_CANNOT_MOVE
    [effect]
        apply_to="movement"
        set=1
    [/effect]
    [effect]
        apply_to="movement_costs"
        replace=yes
        [movement_costs]
            shallow_water={UNREACHABLE}
            reef={UNREACHABLE}
            swamp_water={UNREACHABLE}
            flat={UNREACHABLE}
            sand={UNREACHABLE}
            forest={UNREACHABLE}
            hills={UNREACHABLE}
            mountains={UNREACHABLE}
            village={UNREACHABLE}
            castle={UNREACHABLE}
            cave={UNREACHABLE}
            frozen={UNREACHABLE}
            fungus={UNREACHABLE}
        [/movement_costs]
    [/effect]
#enddef

        [unit]
            {CHARACTER_STATS_MAL_HEKUBA}
            type=Chaos Cardinal Fused
            canrecruit=yes
            {IS_BOSS}
            side=2
            {I_BOSS_LOC}
            facing=$direction
        [/unit]

        {CLEAR_VARIABLE direction}

        [redraw]
            side=1
        [/redraw]

        [scroll_to_unit]
            id=Mal Hekuba
        [/scroll_to_unit]

        {REPLACE_SCENARIO_MUSIC "in_the_land_of_madness.ogg"}
        {APPEND_MUSIC           "the_city_falls.ogg"}
        {APPEND_MUSIC           "casualties_of_war.ogg"}

        {BOSS_POPUP}

        [delay]
            time=750
        [/delay]

        [message]
            speaker=Mal Hekuba
            message= _ "Welcome to your end, heathens!"
        [/message]

        {QUAKE (cave-in.ogg)}

        {FLASH_RED (
            [store_unit]
                [filter]
                    type=Verlissh Control Spire
                [/filter]
                variable=spires_store
            [/store_unit]

            [foreach]
                array=spires_store
                variable=spire
                [do]
                    [object]
                        silent=yes
                        [filter]
                            x,y=$spire.x,$spire.y
                        [/filter]
                        [effect]
                            apply_to="image_mod"
                            add="R(128)"
                        [/effect]
                    [/object]
                [/do]
            [/foreach]

            [redraw][/redraw]

            [sound]
                name=gun-activate-2.ogg
            [/sound]

            [delay]
                time=700
            [/delay]

            # Flash redder momentarily
            {COLOR_ADJUST 500 0 0}

            [sound]
                name=explosion-big.ogg
            [/sound]

            [delay]
                time=50
            [/delay]

            [sound]
                name=rumble.ogg
            [/sound]

            [terrain]
                x=19-30
                y=16-24
                [and]
                    terrain=Q*
                [/and]
                terrain=Qxua
            [/terrain]

            [redraw][/redraw]

            [foreach]
                array=spires_store
                variable=spire
                [do]
                    [unstore_unit]
                        find_vacant=no
                        variable=spire
                    [/unstore_unit]
                [/do]
            [/foreach]

            {CLEAR_VARIABLE spires_store}
        )}

        [redraw][/redraw]

        [delay]
            time=1000
        [/delay]

        [message]
            speaker=Durvan
            message= _ "What kind of evil magic is this?"
        [/message]

        [message]
            speaker=Anya
            message= _ "Is he... fused with one of the spires?"
        [/message]

        [message]
            speaker=Elynia
            # po: "Your leader" refers to Elyssa.
            message= _ "What do you expect to achieve by creating a new portal to Inferno with your new-found powers? Your troops are falling one by one under the might of the northern peoples’ alliance, while you decided to rebel against your leader in this dead land?"
        [/message]

        [message]
            speaker=Mal Hekuba
            # po: "her" refers to Elyssa, again.
            message= _ "Ha, ha, ha, ha! Don’t forget that your shortsightedness is the chief cause of your friends’ demise, foolish creature. The power I control now is too great, even for her."
        [/message]

        [message]
            speaker=Anya
            message= _ "What if you don’t truly control it? You said you cannot speak to Uria directly — what if her will is <i>your</i> destruction?"
        [/message]

        [message]
            speaker=Mal Hekuba
            message= _ "Insolent girl! You shall all die!"
        [/message]

        {QUAKE (rumble.ogg)}
        {QUAKE (cave-in.ogg)}

        [message]
            speaker=Mal Hekuba
            message= _ "YOU SHALL ALL DIE NOW!"
        [/message]

        {QUAKE (explosion-big.ogg)}
        {QUAKE (rumble.ogg)}

#define I_RECRUIT_DEMON _X _Y
    [animate_unit]
        [filter]
            id=Mal Hekuba
        [/filter]
        flag=recruiting
        with_bars=yes
        [facing]
            x={_X}
            y={_Y}
        [/facing]
    [/animate_unit]

    [unit]
        animate=yes
        type=Demon Zephyr
        side=2
        x={_X}
        y={_Y}
        random_traits=yes
        random_gender=yes
        generate_name=yes
        upkeep=loyal
    [/unit]
#enddef
        [store_locations]
            [filter_adjacent_location]
                {I_BOSS_LOC}
            [/filter_adjacent_location]
            variable=recruit_locs
        [/store_locations]

        [foreach]
            array=recruit_locs
            [do]
                {I_RECRUIT_DEMON $this_item.x $this_item.y}
            [/do]
        [/foreach]

        {CLEAR_VARIABLE recruit_locs}

#undef I_RECRUIT_DEMON

        [message]
            speaker=Durvan
            message= _ "What are we going to do now?"
        [/message]

        [message]
            speaker=Elynia
            message= _ "Everyone, stay away from the spires and the portal! I must think of something..."
        [/message]

#define I_PORTAL_NOTES
    {OBJECTIVE_NOTE ( _ "The portal grows in size every turn, taking adjacent ground hexes with it")}
    {OBJECTIVE_NOTE ( _ "Unless adjacent to a spire, road hexes cannot disappear")}
    {OBJECTIVE_NOTE ( _ "Units incapable of flight over chasms will die should the ground below them disappear")}
#enddef

        {OBJECTIVES (
            victory_string= _ "Current Objectives:"
            {OBJECTIVE_VICTORY ( _ "Survive until the end of turns")}

            {OBJECTIVE_DEFEAT  ( _ "Death of Elynia")}
            {OBJECTIVE_DEFEAT  ( _ "Death of Anya")}
            {OBJECTIVE_DEFEAT  ( _ "Death of Durvan")}

            {I_PORTAL_NOTES}
        )}

        {UNLOCK_VIEW}

        #
        # On subsquent turns, increase the portal opening's size.
        # (FIXME: is it really necessary to check the turn number so it
        # doesn't fire immediately at the end of the current new turn event?)
        #

        [event]
            delayed_variable_substitution=no
            name=new turn
            first_time_only=no
            # Only fire on subsequent turns
            [filter_condition]
                {VARIABLE_NUMERICAL_GREATER_THAN turn_number "$($turn_number + 1)"}
            [/filter_condition]
            [fire_event]
                name=portal crack step
            [/fire_event]
        [/event]

        #
        # Two turns later, stage 2.
        #

        [event]
            delayed_variable_substitution=no
            name="turn $($turn_number + 2)"
            [fire_event]
                name=boss stage 2
            [/fire_event]
        [/event]
    [/event]

#define I_EXTEND_PORTAL_TO _X _Y
    [sound]
        name="rumble.ogg"
    [/sound]

    [terrain]
        x={_X}
        y={_Y}
        terrain=Qxua
    [/terrain]

    [redraw][/redraw]

    #
    # Kill present unit if it cannot normally move/fly
    # on Unwalkable hexes.
    #

    [if]
        [not]
            [unit_location_is_passable]
                x={_X}
                y={_Y}
            [/unit_location_is_passable]
        [/not]
        [then]
            [kill]
                x={_X}
                y={_Y}
                animate=yes
                fire_event=yes
            [/kill]

            [redraw][/redraw]
        [/then]
    [/if]
#enddef

    #
    # Portal logic!
    #
    [event]
        name=portal crack step
        first_time_only=no

        # Each destroyed hex will cause the death of the unit on it if it's
        # not capable of flight on unwalkable hexes.

        # First, destroy one adjacent non-portal hex for each core unit.

        [store_locations]
            {I_CORE_LOCS}
            variable=core_locs
        [/store_locations]

        [foreach]
            array=core_locs
            variable=core
            [do]
                [store_locations]
                    [filter_adjacent_location]
                        x,y=$core.x,$core.y
                    [/filter_adjacent_location]
                    terrain=!,Qxua
                    variable=core_adjacent_locs
                [/store_locations]

                [if]
                    {VARIABLE_NUMERICAL_GREATER_THAN core_adjacent_locs.length 0}
                    [then]
                        # Randomize choice if there are more than 1 remaining locations
                        {VARIABLE_RANDOM r "0..$($core_adjacent_locs.length - 1)"}
                        {I_EXTEND_PORTAL_TO $core_adjacent_locs[$r].x $core_adjacent_locs[$r].y}
                        {CLEAR_VARIABLE r}
                    [/then]
                [/if]

                {CLEAR_VARIABLE core_adjacent_locs}
            [/do]
        [/foreach]

        {CLEAR_VARIABLE core_locs}

        # Then, destroy one non-portal/castle/keep/road/wall hex that is adjacent
        # to a portal hex that is *not* adjacent to a core unit nor is a core
        # unit's location. Don't destroy Dark Hive tiles either.

        [store_locations]
            [filter_adjacent_location]
                terrain=Qxua
                [not]
                    {I_CORE_LOCS}
                [/not]
            [/filter_adjacent_location]
            terrain=!,Qxua,C*,C*^*,*^C*,K*,K*^*,*^K*,Ur*,Ur*^*,X*,X*^*,Y*,Y*^*
            [not]
                {I_CORE_LOCS}
            [/not]
            variable=portal_adjacent_locs
        [/store_locations]

        [if]
            {VARIABLE_NUMERICAL_GREATER_THAN portal_adjacent_locs.length 0}
            [then]
                # Randomize choice if there are more than 1 remaining locations
                {VARIABLE_RANDOM r "0..$($portal_adjacent_locs.length - 1)"}
                {I_EXTEND_PORTAL_TO $portal_adjacent_locs[$r].x $portal_adjacent_locs[$r].y}
                {CLEAR_VARIABLE r}
            [/then]
        [/if]

        {CLEAR_VARIABLE portal_adjacent_locs}
    [/event]

#undef I_EXTEND_PORTAL_TO

    #
    # Damage absorption logic for control spires.
    #

    [event]
        name=attacker hits
        first_time_only=no
        [filter]
            side=1
        [/filter]
        [filter_second]
            type=Verlissh Control Spire
        [/filter_second]

        {BOSS_ABSORB_FULL_DAMAGE (x,y=$x2,$y2)}

        [fire_event]
            name=spire warning
        [/fire_event]
    [/event]

    [event]
        name=defender hits
        first_time_only=no
        [filter_second]
            side=1
        [/filter_second]
        [filter]
            type=Verlissh Control Spire
        [/filter]

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

        [fire_event]
            name=spire warning
        [/fire_event]
    [/event]

    [event]
        name=spire warning
        first_time_only=yes
        [message]
            speaker=Elynia
            message= _ "There’s no use in attacking those structures — the energy they release in retaliation could kill us all!"
        [/message]
    [/event]

    #
    # Side 2 recruits become loyal every time.
    #

    [event]
        name=prerecruit
        first_time_only=no
        [filter]
            side=2
        [/filter]

        {VARIABLE unit.upkeep free}

        [unstore_unit]
            variable=unit
            find_vacant=no
        [/unstore_unit]
    [/event]

    #
    # Hekuba's infinite army of evil minions from the abyss
    #

    [event]
        name=side 2 turn end
        first_time_only=no
        [filter_condition]
            {VARIABLE_NUMERICAL_GREATER_THAN boss_stage 0}
            [and]
                {VARIABLE_NUMERICAL_EQUALS boss_spawn.next_turn $turn_number}
                [or]
                    {VARIABLE_NUMERICAL_EQUALS boss_spawn.next_turn 0}
                [/or]
            [/and]
        [/filter_condition]

        # Reset the event timer
        {VARIABLE boss_spawn.next_turn "$($turn_number + $boss_spawn.turn_delay)"}

        {VARIABLE boss_spawn_sequence demon}

        [fire_event]
            name=boss spawn controller
        [/fire_event]

        {VARIABLE boss_spawn_sequence shaxthal}

        [fire_event]
            name=boss spawn controller
        [/fire_event]

        {CLEAR_VARIABLE boss_spawn_sequence}
    [/event]

    [event]
        name=boss spawn controller
        first_time_only=no

        {VARIABLE_DEFAULT boss_spawn_sequence demon} # failsafe

        [count_units]
            side=2
            [filter_wml]
                [variables]
                    boss_spawn_sequence=$boss_spawn_sequence
                [/variables]
            [/filter_wml]
            variable=spawns_current
        [/count_units]

        {VARIABLE spawns_needed "$($boss_spawn.$boss_spawn_sequence|_cap - $spawns_current)"}

        # Copy spawn location set into a new variable so we can remove any
        # locations used during one sequence in order to avoid spawning
        # more than one unit per location.

        [set_variables]
            name=spawn_locs
            mode=replace
            to_variable=boss_spawn.$boss_spawn_sequence|_locs
        [/set_variables]

        {BUG_ON ({VARIABLE_NUMERICAL_EQUALS spawn_locs.length 0}) ()}

        [repeat]
            times=$spawns_needed
            [do]
                {VARIABLE_RANDOM r "0..$($spawn_locs.length - 1)"}
                {VARIABLE_RANDOM t $boss_spawn.$boss_spawn_sequence|_types}

                {VARIABLE sx $spawn_locs[$r].x}
                {VARIABLE sy $spawn_locs[$r].y}

                [store_direction]
                    from_x,from_y=$sx,$sy
                    [to]
                        {I_BOSS_LOC}
                    [/to]
                    variable=d
                [/store_direction]

                [unit]
                    side=2
                    animate=yes
                    type=$t
                    facing=$d
                    x,y=$spawn_locs[$r].x,$spawn_locs[$r].y
                    generate_name=yes
                    random_traits=yes
                    random_gender=yes
                    [variables]
                        boss_spawn_sequence=$boss_spawn_sequence
                    [/variables]
                [/unit]
                {NO_UPKEEP_NO_OVERLAY}

                {CLEAR_VARIABLE spawn_locs[$r]}

                {CLEAR_VARIABLE d,r,sx,sy,t}
            [/do]
        [/repeat]

        {CLEAR_VARIABLE spawn_locs,spawns_needed,spawns_current}
    [/event]

    #
    # Damage absorption logic for Mal Hekuba.
    #

    [event]
        name=attacker hits
        first_time_only=no
        [filter]
            side=1
        [/filter]
        [filter_second]
            id=Mal Hekuba
        [/filter_second]

        {BOSS_ABSORB_FULL_DAMAGE (id=Mal Hekuba)}

        [fire_event]
            name=invincibility taunt
        [/fire_event]
    [/event]

    [event]
        name=defender hits
        first_time_only=no
        [filter_second]
            side=1
        [/filter_second]
        [filter]
            id=Mal Hekuba
        [/filter]

        {BOSS_ABSORB_FULL_DAMAGE (id=Mal Hekuba)}

        [fire_event]
            name=invincibility taunt
        [/fire_event]
    [/event]

    [event]
        name=invincibility taunt
        first_time_only=yes
        [message]
            speaker=Mal Hekuba
            message= _ "Attacking me is futile!"
        [/message]
    [/event]

    ############################################################################
    #
    # BOSS FIGHT STAGE 2
    #
    ############################################################################

    [event]
        name=boss stage 2
        first_time_only=yes

        {VARIABLE boss_stage 2}

        {LOCK_VIEW}

        {CLEAR_LABELS}

        [message]
            speaker=Elynia
            message= _ "The more time passes, the more energy fills the place and binds our worlds together. I must go and try to destroy that lich by myself."
        [/message]

        {FACE_UNIT (id=Anya) (id=Elynia)}

        [message]
            speaker=Anya
            message= _ "Elynia? No! If you go he will—"
        [/message]

        [message]
            speaker=Elynia
            message= _ "I am well aware, but I’m also the only one who can wield this staff and not become corrupted by the power of the Ruby. Using it to vanquish this evil... if my own sacrifice is necessary, I will gladly accept my fate. There’s nothing left in Irdya for me now, and there will certainly be nothing for you all if this fiend succeeds."
        [/message]

        [delay]
            time=750
        [/delay]

        [message]
            speaker=Anya
            message= _ "I— I—"
        [/message]

        [delay]
            time=250
        [/delay]

        {FACE_UNIT (id=Elynia) (id=Anya)}

        [delay]
            time=250
        [/delay]

        [message]
            speaker=Elynia
            message= _ "You cannot go with me, for someone must survive the ordeal to pass the news on to our allies. You are a very smart girl, and I’m sure you all will be able to escape together with your power."
        [/message]

        [delay]
            time=750
        [/delay]

        {FACE_LOCATION (id=Elynia) ({I_BOSS_LOC})}

        [message]
            speaker=Elynia
            message= _ "I must go now."
        [/message]

        {FACE_UNIT (id=Durvan) (id=Elynia)}

        [message]
            speaker=Durvan
            message= _ "Elynia, my lady... Good luck!"
        [/message]

        [message]
            speaker=Anya
            message= _ "(<i>tearful</i>) Elynia! Don’t die up there! Please!"
        [/message]

        [delay]
            time=1000
        [/delay]

        [message]
            speaker=Elynia
            message= _ "(<i>muttering to herself</i>) I can only try."
        [/message]

        [object]
            name= _ "Darkness Flight"
            description= _ "Elynia is now able to fly over chasms."
            image="icons/jewelry_butterfly_pin.png"
            duration=scenario
            [filter]
                id=Elynia
            [/filter]
            [effect]
                apply_to=movement_costs
                replace=true
                [movement_costs]
                    unwalkable=1
                [/movement_costs]
            [/effect]
        [/object]

        # Switch leaders

        [store_unit]
            [filter]
                id=Elynia
            [/filter]
            variable=elynia_store
        [/store_unit]
        [store_unit]
            [filter]
                id=Anya
            [/filter]
            variable=anya_store
        [/store_unit]

        [set_variables]
            name=elynia_store
            mode=merge
            [literal]
                canrecruit=no
                {IS_HERO}
            [/literal]
        [/set_variables]

        {VARIABLE anya_store.canrecruit yes}
        {CLEAR_VARIABLE anya_store.overlays}
        {CLEAR_VARIABLE anya_store.ellipse}

        [unstore_unit]
            variable=elynia_store
            find_vacant=no
        [/unstore_unit]
        [unstore_unit]
            variable=anya_store
            find_vacant=no
        [/unstore_unit]

        [object]
            silent=yes
            duration=scenario
            [filter]
                id=Elynia
            [/filter]
            [effect]
                apply_to=loyal
            [/effect]
        [/object]

        {CLEAR_VARIABLE anya_store,elynia_store}

        {OBJECTIVES (
            victory_string= _ "Current Objectives:"
            {OBJECTIVE_VICTORY ( _ "Attack Mal Hekuba with Elynia")}

            {OBJECTIVE_DEFEAT  ( _ "Death of Elynia")}
            {OBJECTIVE_DEFEAT  ( _ "Death of Anya")}
            {OBJECTIVE_DEFEAT  ( _ "Death of Durvan")}

            {TURNS_RUN_OUT}

            {I_PORTAL_NOTES}
        )}

        {UNLOCK_VIEW}

        [event]
            name=moveto
            [filter]
                id=Elynia
                [filter_adjacent]
                    id=Mal Hekuba
                [/filter_adjacent]
            [/filter]

            {LOCK_VIEW}

            {FACE_UNIT (id=Elynia) (id=Mal Hekuba)}

            {FACE_UNIT (id=Mal Hekuba) (id=Elynia)}

            [message]
                speaker=Mal Hekuba
                message= _ "And just what are you doing now?"
            [/message]

            [message]
                speaker=Elynia
                message= _ "I’m going to destroy you!"
            [/message]

            {UNLOCK_VIEW}

            #
            # Needed so Elynia can't be hit and killed before the next sequence.
            #

            {FORCE_CHANCE_TO_HIT () (id=Elynia) 0 (
                {VARIABLE_NUMERICAL_EQUALS boss_stage 2}
                [or]
                    {VARIABLE_NUMERICAL_EQUALS boss_stage 3}
                [/or]
            )}

            [event]
                name=attack end
                [filter]
                    id=Elynia
                [/filter]
                [filter_second]
                    id=Mal Hekuba
                [/filter_second]

                [fire_event]
                    name=boss stage 3
                [/fire_event]
            [/event]

            [event]
                name=attack end
                [filter_second]
                    id=Elynia
                [/filter_second]
                [filter]
                    id=Mal Hekuba
                [/filter]

                [fire_event]
                    name=boss stage 3
                [/fire_event]
            [/event]
        [/event]
    [/event]

    ############################################################################
    #
    # BOSS FIGHT STAGE 3
    #
    ############################################################################

    [event]
        name=boss stage 3
        first_time_only=yes

        {VARIABLE boss_stage 3}

        {LOCK_VIEW}

        {CLEAR_LABELS}

        {FACE_UNIT (id=Elynia) (id=Mal Hekuba)}

        {FACE_UNIT (id=Mal Hekuba) (id=Elynia)}

        [message]
            speaker=Mal Hekuba
            message= _ "How pathetic."
        [/message]

        [animate_attack]
            [filter]
                id=Elynia
            [/filter]
            [filter_second]
                id=Mal Hekuba
            [/filter_second]
            [filter_attack]
                range=ranged
            [/filter_attack]
            kill=no
            amount="$($this_unit.hitpoints * 0.75)"
            animate,fire_event=yes,no
            #experience=no
        [/animate_attack]

        [message]
            speaker=Mal Hekuba
            message= _ "Have you not learned anything after your friends’ vain sacrifices, Elynia? Have you not learned to GIVE UP?"
        [/message]

        [store_unit]
            [filter]
                id=Elynia
            [/filter]
            kill=no
            variable=elynia_store
        [/store_unit]

        {COLOR_ADJUST 63 0 0}
        {COLOR_ADJUST 127 0 0}
        {COLOR_ADJUST 255 0 0}

#define I_HEKUBA_MUST_FACE_ELYNIA
    {FACE_UNIT (id=Mal Hekuba) (id=Elynia)}
#enddef

#define I_ATTACK_ELYNIA
    {I_HEKUBA_MUST_FACE_ELYNIA}

    [animate_attack]
        [filter]
            id=Elynia
        [/filter]
        [filter_second]
            id=Mal Hekuba
        [/filter_second]
        [filter_attack]
            range=ranged
        [/filter_attack]
        kill=no
        amount=$this_unit.hitpoints
        animate,fire_event=yes,no
        #experience=no
    [/animate_attack]
#enddef

        {I_ATTACK_ELYNIA}

        [store_unit]
            [filter]
                id=Elynia
            [/filter]
            kill=no
            variable=elynia_original_store
        [/store_unit]

        [object]
            silent=yes
            [filter]
                id=Elynia
            [/filter]
            {I_EFFECT_CANNOT_MOVE}
            [effect]
                apply_to=variation
                name=injured
            [/effect]
        [/object]

        {COLOR_ADJUST 127 0 0}
        {COLOR_ADJUST 63 0 0}
        {COLOR_ADJUST 0 0 0}

        {FACE_UNIT (id=Anya) (id=Elynia)}

        [delay]
            time=750
        [/delay]

        [message]
            speaker=Anya
            message= _ "ELYNIA!"
        [/message]

        [message]
            speaker=Mal Hekuba
            message= _ "Why won’t you just accept your fate and <b>DIE</b>?!"
        [/message]

        [repeat]
            times=3
            [do]
                {I_ATTACK_ELYNIA}
            [/do]
        [/repeat]

        [message]
            speaker=Anya
            message= _ "We must do something!"
        [/message]

        [message]
            speaker=Durvan
            message= _ "If Elynia herself doesn’t have the power to destroy that lich, I don’t think there’s anything we can do about it..."
        [/message]

        {OBJECTIVES (
            victory_string= _ "Current Objectives:"
            {OBJECTIVE_VICTORY ( _ "Survive until the opportunity to destroy Mal Hekuba presents itself")}

            {OBJECTIVE_DEFEAT  ( _ "Death of Elynia")}
            {OBJECTIVE_DEFEAT  ( _ "Death of Anya")}
            {OBJECTIVE_DEFEAT  ( _ "Death of Durvan")}

            {I_PORTAL_NOTES}
        )}

        {VARIABLE boss_stage 3}

        {UNLOCK_VIEW}

        [event]
            delayed_variable_substitution=no
            name="side 2 turn $($turn_number + 1)"
            {I_HEKUBA_MUST_FACE_ELYNIA}
            [message]
                speaker=Mal Hekuba
                message= _ "It’s too late to stop me!"
            [/message]
        [/event]

        [event]
            delayed_variable_substitution=no
            name="side 2 turn $($turn_number + 2)"
            {I_HEKUBA_MUST_FACE_ELYNIA}
            [message]
                speaker=Mal Hekuba
                message= _ "There’s absolutely <i>nothing</i> you can do!"
            [/message]
        [/event]

        [event]
            delayed_variable_substitution=no
            name="turn $($turn_number + 3)"
            {I_HEKUBA_MUST_FACE_ELYNIA}
            [fire_event]
                name=boss stage 4
            [/fire_event]
        [/event]

        # Continue torturing Elynia every turn.
        #
        # It's not currently possible to stop unit deaths in "attacker/defender
        # hits" event handlers by any obvious means (FIXME: bug?), so we change
        # her side for the duration of side 2 turns to a dual ally side (8) in
        # order for the AI to not attack her; then Mal Hekuba's attacks are
        # animated every time in WML. Her unit is switched back to side 1's
        # control in player turns.
        #
        # This mechanism is rather unelegant, but at least permits safely
        # forcing all AI sides to ignore her for target evaluation.
        #

        [event]
            name=side 2 turn
            first_time_only=no
            [filter_condition]
                {VARIABLE_NUMERICAL_EQUALS boss_stage 3}
            [/filter_condition]

            {MODIFY_UNIT (id=Elynia) side 8}

            {I_ATTACK_ELYNIA}
        [/event]

        [event]
            name=side 1 turn refresh
            first_time_only=no
            [filter_condition]
                {VARIABLE_NUMERICAL_EQUALS boss_stage 3}
            [/filter_condition]

            [store_unit]
                [filter]
                    id=Elynia
                [/filter]
                variable=elynia_store
            [/store_unit]

            [set_variables]
                name=elynia_store
                mode=merge
                [literal]
                    attacks_left=0
                    moves=0
                    resting=no
                    side=1
                [/literal]
            [/set_variables]

            [unstore_unit]
                variable=elynia_store
                find_vacant=no
            [/unstore_unit]

            {CLEAR_VARIABLE elynia_store}
        [/event]
    [/event]

    ############################################################################
    #
    # BOSS FIGHT STAGE 4
    #
    ############################################################################

    [event]
        name=boss stage 4
        first_time_only=no

        {VARIABLE boss_stage 4}

        {LOCK_VIEW}

        {CLEAR_LABELS}

        {FACE_UNIT (id=Mal Hekuba) (id=Elynia)}

        [fade_out_music]
            duration=500
        [/fade_out_music]

        [fade_out_sound_effects]
            duration=500
        [/fade_out_sound_effects]

        [store_locations]
            [filter]
                id=Elynia
            [/filter]
            variable=elynia_current_loc
        [/store_locations]

        {FORCE_CHANCE_TO_HIT (
            [not]
                id=Mal Hekuba
            [/not]
        ) (id=Elynia) 20 ()}

        {FORCE_CHANCE_TO_HIT (id=Mal Hekuba) (id=Elynia) 0 ()}

        [message]
            speaker=Mal Hekuba
            message= _ "Have you had enough already?"
        [/message]

        [message]
            speaker=Mal Hekuba
            message= _ "No?"
        [/message]

        {REPLACE_SCENARIO_MUSIC "the_high_kings_march.ogg"}

        [message]
            speaker=narrator
            image="misc/blank-hex.png"
            message= _ "<i>... Don’t forget that you are here fighting for a greater cause...</i>"
        [/message]

        [message]
            speaker=Elynia
            message= _ "... I..."
        [/message]

        [delay]
            time=250
        [/delay]

        [message]
            speaker=Mal Hekuba
            message= _ "Stupid little vermin..."
        [/message]

        [remove_shroud]
            side=1
            x= 8-20, 9-11
            y=21-27,28
            terrain=!,X*,X*^*,*^X*
        [/remove_shroud]

        [redraw]
            side=1
        [/redraw]

        [modify_side]
            side=3
            controller=ai
            hidden=no
        [/modify_side]

        [unit]
            side=3
            #canrecruit=yes
            type=Shaxthal Infiltrator
            id=Ivyel
            name= _ "Ivyel"
            facing=ne
            x,y=9,28
            [modifications]
                {TRAIT_BIOMECHANICAL}
                {TRAIT_RESILIENT}
                {TRAIT_DEXTROUS}
            [/modifications]
        [/unit]

        [store_locations]
            [filter_adjacent_location]
                {I_BOSS_LOC}
            [/filter_adjacent_location]
            [not]
                [filter][/filter]
            [/not]
            variable=vacant_locs
        [/store_locations]

        [if]
            {VARIABLE_NUMERICAL_GREATER_THAN vacant_locs.length 0}
            [then]
                {VARIABLE_RANDOM r "0..$($vacant_locs.length - 1)"}
                {MOVE_UNIT (id=Ivyel) "10,12,17,23,$vacant_locs[$r].x" "27,26,24,21,$vacant_locs[$r].y"}
                {CLEAR_VARIABLE r}
            [/then]
            [else]
                {MOVE_UNIT (id=Ivyel) "10,12,17,23" "27,26,24,21"}
            [/else]
        [/if]

        {CLEAR_VARIABLE vacant_locs}

        # Force re-center
        [scroll_to_unit]
            id=Mal Hekuba
        [/scroll_to_unit]

        {FACE_UNIT (id=Mal Hekuba) (id=Ivyel)}

        [message]
            speaker=Mal Hekuba
            message= _ "Ah, Ivyel, at long last! It’s good to see you again... This pitiful creature is hindering me. Could you sweep her away for me?"
        [/message]

        [fade_in_sound_effects]
            duration=250
        [/fade_in_sound_effects]

        {VARIABLE elynia_original_store.x $elynia_current_loc.x}
        {VARIABLE elynia_original_store.y $elynia_current_loc.y}

        [unstore_unit]
            # Not removed now, we still need to restore her stats
            # before the end of the scenario!
            variable=elynia_original_store
            find_vacant=no
        [/unstore_unit]

        {CLEAR_VARIABLE elynia_current_loc}

        [heal_unit]
            [filter]
                id=Elynia
            [/filter]
            amount="full"
            animate=yes
            restore_statuses=yes
        [/heal_unit]

        [modify_unit]
            [filter]
                id=Elynia
            [/filter]
            attacks_left=$this_unit.max_attacks
            moves=$this_unit.max_moves
            level="$($this_unit.level + 1)"
        [/modify_unit]

        {I_HEKUBA_MUST_FACE_ELYNIA}

        [message]
            speaker=Mal Hekuba
            message= _ "... What’s this?"
        [/message]

        {FACE_UNIT (id=Anya) (id=Elynia)}

        [delay]
            time=250
        [/delay]

        [message]
            speaker=Elynia
            message= _ "... I cannot be defeated by the likes of you..."
        [/message]

        [object]
            silent=yes
            [filter]
                id=Elynia
            [/filter]
            [effect]
                apply_to="attack"
                name=faerie fire
                increase_damage=1
                increase_attacks=1
            [/effect]
            [effect]
                apply_to="hitpoints"
                heal_full=yes
                increase_total=20%
            [/effect]
            [effect]
                apply_to="image_mod"
                add="CS(32,32,32)"
            [/effect]
        [/object]

        [delay]
            time=500
        [/delay]

        [message]
            speaker=Elynia
            message= _ "... I am the Lady of Light..."
        [/message]

        [delay]
            time=500
        [/delay]

        [message]
            speaker=Elynia
            message= _ "... even without my former power, I shall destroy you..."
        [/message]

        [message]
            speaker=Mal Hekuba
            message= _ "Of course not. TAKE HER DOWN!"
        [/message]

        {FACE_UNIT (id=Elynia) (id=Ivyel)}

        {FACE_UNIT (id=Ivyel) (id=Elynia)}

        [message]
            speaker=Durvan
            message= _ "What’s she going to do? Does she have a plan?"
        [/message]

        [message]
            speaker=Anya
            message= _ "I am not sure. It appears she wants to attack the infiltrator creature..."
        [/message]

        {OBJECTIVES (
            {OBJECTIVE_VICTORY ( _ "Destroy Ivyel")}

            {OBJECTIVE_DEFEAT  ( _ "Death of Elynia")}
            {OBJECTIVE_DEFEAT  ( _ "Death of Anya")}
            {OBJECTIVE_DEFEAT  ( _ "Death of Durvan")}

            {TURNS_RUN_OUT}

            {I_PORTAL_NOTES}
        )}

        {UNLOCK_VIEW}

        [event]
            name=last breath
            [filter]
                id=Ivyel
            [/filter]

            {LOCK_VIEW}

            {CLEAR_LABELS}

            {FACE_UNIT (id=Elynia) (id=Ivyel)}

            [message]
                speaker=Elynia
                message= _ "This is <i>your</i> end, Mal Hekuba..."
            [/message]

            {REPLACE_SCENARIO_MUSIC "silence.ogg"}

            {REMOVE_SOUND_SOURCE cave_sound_source,water_sound_source}

            #
            # TODO: ensure Elynia and Ivyel are in adjacent hexes (?)
            #

            [if]
                [have_unit]
                    id=Elynia
                    [filter_adjacent]
                        id=Ivyel
                    [/filter_adjacent]
                [/have_unit]
                [then]
                    [animate_attack]
                        [filter]
                            id=Ivyel
                        [/filter]
                        [filter_second]
                            id=Elynia
                        [/filter_second]
                        [filter_attack]
                            range=melee
                        [/filter_attack]
                        kill=yes
                        animate=yes
                        fire_event=no
                        amount=1
                        #experience=no
                    [/animate_attack]
                [/then]
                [else]
                    [kill]
                        id=Ivyel
                        animate=yes
                        fire_event=no
                    [/kill]
                [/else]
            [/if]

            [message]
                speaker=Elynia
                message= _ "... Irdya is not yours to destroy!"
            [/message]

            {QUAKE ({SOUND_LIST:COLLAPSING_FACILITY})}

            {FLASH_RED (
                [harm_unit]
                    [filter]
                        id=Mal Hekuba
                    [/filter]
                    kill=no
                    amount=$this_unit.hitpoints
                    animate,fire_event=yes,no
                [/harm_unit]
            )}

            [message]
                speaker=Mal Hekuba
                message= _ "AAAAAAAARRGH!"
            [/message]

            [store_unit]
                [filter]
                    type=Verlissh Control Spire
                [/filter]
                kill=no
                variable=spires_store
            [/store_unit]

            {QUAKE ({SOUND_LIST:COLLAPSING_FACILITY})}

            {QUAKE ({SOUND_LIST:COLLAPSING_FACILITY})}

            [foreach]
                array=spires_store
                variable=spire
                [do]
                    {FLASH_RED (
                        {VARIABLE spire.hitpoints 1}

                        [unstore_unit]
                            variable=spire
                            find_vacant=no
                            text="$spire.max_hitpoints" # wmllint: ignore
                            {COLOR_HARM}
                        [/unstore_unit]

                        [object]
                            [filter]
                                x,y=$spire.x,$spire.y
                            [/filter]
                            [effect]
                                apply_to=variation
                                name=low
                            [/effect]
                            silent=yes
                        [/object]
                    )}
                [/do]
            [/foreach]

            {FLASH_RED ({QUAKE ({SOUND_LIST:COLLAPSING_FACILITY})})}

            [message]
                speaker=Mal Hekuba
                message= _ "WHAT HAVE YOU DONE?"
            [/message]

            {FLASH_RED ()}

            {QUAKE ({SOUND_LIST:COLLAPSING_FACILITY})}

            {QUAKE ({SOUND_LIST:COLLAPSING_FACILITY})}

            [store_unit]
                [filter]
                    id=Anya
                [/filter]
                variable=anya_store
                kill=yes
            [/store_unit]

            [set_variables]
                name=anya_store
                mode=merge
                [literal]
                    canrecruit=no
                    {IS_HERO}
                    x,y=recall,recall
                [/literal]
            [/set_variables]

            {CLEAR_VARIABLE elynia_original_store.overlays}
            {CLEAR_VARIABLE elynia_original_store.ellipse}

            [set_variables]
                name=elynia_original_store
                mode=merge
                [literal]
                    canrecruit=yes
                    x,y=recall,recall
                [/literal]
            [/set_variables]

            [unstore_unit]
                variable=anya_store
                find_vacant=no
            [/unstore_unit]

            [kill]
                id=Elynia
            [/kill]

            [unstore_unit]
                variable=elynia_original_store
                find_vacant=no
            [/unstore_unit]

            {CLEAR_VARIABLE elynia_original_store,anya_store}

            {PUT_TO_RECALL_LIST (side=1)}

            [scroll_to_unit]
                id=Mal Hekuba
            [/scroll_to_unit]

            {QUAKE ({SOUND_LIST:COLLAPSING_FACILITY})}

            [repeat]
                times=3
                [do]
                    {FLASH_RED (
                        [animate_unit]
                            [filter]
                                id=Mal Hekuba
                            [/filter]
                            flag=defend
                            hits=hit
                            with_bars=yes
                            [primary_attack]
                                range=ranged
                            [/primary_attack]
                            [secondary_attack]
                                range=ranged
                            [/secondary_attack]
                        [/animate_unit]
                    )}
                [/do]
            [/repeat]

            [kill]
                id=Mal Hekuba
                animate=yes
                fire_event=no
            [/kill]

            [remove_item]
                {I_BOSS_LOC}
            [/remove_item]

            [item]
                {I_BOSS_LOC}
                image="items/ball-magenta.png~CS(25,25,25)"
                halo="halo/heart-aura.png"
            [/item]

            {COLOR_ADJUST 96 0 0}

            [redraw][/redraw]

            [delay]
                time=250
            [/delay]

            [remove_item]
                {I_BOSS_LOC}
            [/remove_item]

            [item]
                {I_BOSS_LOC}
                image="items/ball-magenta.png~CS(50,50,50)"
                halo="halo/heart-aura.png~SCALE(450,450)~BL(1)"
            [/item]

            {COLOR_ADJUST 196 0 0}

            [redraw][/redraw]

            [delay]
                time=250
            [/delay]

            [remove_item]
                {I_BOSS_LOC}
            [/remove_item]

            [item]
                {I_BOSS_LOC}
                image="items/ball-magenta.png~CS(120,120,120)"
                halo="halo/heart-aura.png~SCALE(675,675)~BL(2)"
            [/item]

            [redraw][/redraw]

            [delay]
                time=750
            [/delay]

            {COLOR_ADJUST 500 0 0}

            [sound]
                name=break.ogg
            [/sound]

            [delay]
                time=500
            [/delay]

            [sound]
                name={SOUND_LIST:EXPLOSION}
            [/sound]

            [remove_item]
                {I_BOSS_LOC}
            [/remove_item]

            [kill]
                {NOT_ON_RECALL_LIST}
                fire_event=no
                animate=no
            [/kill]

            [terrain]
                {I_BOSS_LOC}
                radius=1
                [or]
                    terrain=Qxua
                [/or]
                terrain=Qxu
            [/terrain]

            [sound]
                name={SOUND_LIST:EXPLOSION}
            [/sound]

            {COLOR_ADJUST 0 0 0}

            [redraw][/redraw]

            [sound]
                name={SOUND_LIST:COLLAPSING_FACILITY}
            [/sound]

            [delay]
                time=250
            [/delay]

            #
            # Fade to black
            #

#define I_NOISY_FADE_STEP _VALUE _DELAY
    {FADE_STEP ({_VALUE}) ({_DELAY})}

    [sound]
        name={SOUND_LIST:COLLAPSING_FACILITY}
    [/sound]
#enddef

            {I_NOISY_FADE_STEP -32 250}

            {I_NOISY_FADE_STEP -64 250}
            {I_NOISY_FADE_STEP -96 250}
            {I_NOISY_FADE_STEP -128 250}
            {I_NOISY_FADE_STEP -160 250}
            {I_NOISY_FADE_STEP -192 250}
            {I_NOISY_FADE_STEP -224 250}

            [remove_item][/remove_item]

            [replace_schedule]
                {NEUTRAL_TOD} {SCHEDULE_LIGHTING 0 -20 -20}
            [/replace_schedule]

            [terrain]
                # Everywhere
                terrain=Xv
            [/terrain]

            [modify_side]
                side=1
                shroud=no
            [/modify_side]

            [redraw][/redraw]

            {I_NOISY_FADE_STEP -500 750}

            {UNLOCK_VIEW}

            {ENDLEVEL_QUIET}
        [/event]
    [/event]

#undef I_BOSS_LOC

    [event]
        name=victory
        {CLEAR_VARIABLE boss_spawn,boss_stage,elynia_original_store}
    [/event]

#undef I_ATTACK_ELYNIA
#undef I_CORE_LOCS
#undef I_PORTAL_NOTES
#undef I_NOISY_FADE_STEP
#undef I_HEKUBA_MUST_FACE_ELYNIA
#undef I_EFFECT_CANNOT_MOVE
[/scenario]

# kate: indent-mode normal; encoding utf-8; space-indent on;


#textdomain wesnoth-After_the_Storm

[scenario]
    id=12_Fate
    name= _ "Fate"
    {MAP 12_Fate.map}
    turns=-1
    next_scenario=null
    victory_when_enemies_defeated=no

    {FULLSCREEN_CUTSCENE_UI}

    {SCENARIO_MUSIC "underground.ogg"} {CONTINUE_PLAYING_STORY_MUSIC_FIRST}

    {LONGDARK4}

    # wmllint: validate-off
    [side]
        side=1
        controller=human
        save_id=player
        team_name=good
        user_team_name= _ "team_name^Elynia"

        village_gold,gold=0,0
        {NO_INCOME}

        fog=yes
        # This is solely to prevent a map snapshot from being used in the
        # Load Game menu when all terrain grahics are loaded. The shroud
        # is disabled later during prestart.
        shroud=yes

        # wmllint: recognize Elynia
        {CHARACTER_STATS_ELYNIA}
    [/side]
    # wmllint: validate-on

    [side]
        side=2
        team_name=evil
        user_team_name= _ "team_name^Elyssa"
        color=gold
        controller=null
        {CHAOS_FLAG}

        village_gold,gold=0,0
        {NO_INCOME}

        hidden=yes
        no_leader=yes
    [/side]

    [side]
        side=3
        team_name=evil
        user_team_name= _ "team_name^Elyssa"
        color=orange
        controller=null
        {CHAOS_FLAG}

        village_gold,gold=0,0
        {NO_INCOME}

        hidden=yes
        no_leader=yes
    [/side]

    [event]
        name=prestart

        {VARIABLE_DEFAULT anya_has_mermaid_pendant no}

        {GLOBAL_TABLE:OPEN NAMESPACE={PERSISTENT_NS_ATS}}
        {GLOBAL_TABLE:WRITE anya_has_mermaid_pendant}
        {GLOBAL_TABLE:CLOSE}

        #
        # Store the heroes’ stats for use in E3.
        # TODO: only store the crap we actually need, i.e. HP, XP, AMLA hints
        #

        {UNIT_TO_GLOBAL_STORE Elynia episode2_stats_elynia}

        # wmllint: recognize Anya
        {UNIT_TO_GLOBAL_STORE Anya episode2_stats_anya}

        # wmllint: recognize Durvan
        {UNIT_TO_GLOBAL_STORE Durvan episode2_stats_durvan}

        [modify_side]
            side=1
            gold=0
        [/modify_side]

        {BLACK_SCREEN}

        {APPLY_INJURED_VARIATION id=Elynia ne}

        [hide_unit]
            id=Elynia
        [/hide_unit]

        {LOCK_VIEW}

        [lua]
            code= <<
            function music_fade_in_fade_out(do_fadein)
                local function set_music_volume(percentage)
                    wesnoth.fire("volume", { music = percentage })
                end

                for k = 0, 100 do
                    local v
                    if do_fadein then v = k else v = 100 - k end
                    set_music_volume(v)
                    wesnoth.interface.delay(10)
                end
            end

            function music_fade_in() music_fade_in_fade_out(true) end

            function music_fade_out() music_fade_in_fade_out(false) end
            >>
        [/lua]

#define F_CLEAR_SHROUD
    [remove_shroud]
        side=1
        # empty SLF matches all locations
    [/remove_shroud]
#enddef

#define F_FILL_SHROUD
    [place_shroud]
        side=1
        # empty SLF matches all locations
    [/place_shroud]
#enddef
    [/event]

#define F_SPIRIT _X _Y
    [delay]
        time=250
    [/delay]

    [scroll_to]
        x={_X}
        y={_Y}
    [/scroll_to]

    [store_direction]
        [from]
            x={_X}
            y={_Y}
        [/from]
        [to]
            [filter]
                id=Elynia
            [/filter]
        [/to]
    [/store_direction]

    [unit]
        side=3
        type=Fire Wraith
        x={_X}
        y={_Y}
        random_traits=yes
        generate_name=yes
        random_gender=yes
        upkeep=free
        facing=$direction
        animate=yes
    [/unit]

    [redraw][/redraw]

    {CLEAR_VARIABLE direction}
#enddef

    [event]
        name=start

        {F_FILL_SHROUD}

        [message]
            speaker=narrator
            image=wesnoth-icon.png
            message= _ "Severely injured, Elynia manages to escape from the underground base as the energy destroys Mal Hekuba and his minions."
        [/message]

        [sound]
            name={SOUND_LIST:COLLAPSING_FACILITY}
        [/sound]

        [delay]
            time=1000
        [/delay]

        [sound]
            name={SOUND_LIST:ELF_FEMALE_HIT}
        [/sound]

        [delay]
            time=2000
        [/delay]

        {F_CLEAR_SHROUD}

        {FADE_IN}

        [unhide_unit]
            id=Elynia
        [/unhide_unit]

        [item]
            # SE from Elynia
            x,y=18,9
            image="items/staff-elynia-niryone.png"
        [/item]

        [delay]
            time=1000
        [/delay]

        [message]
            speaker=Elynia
            message= _ "Ungh..." # wmllint: no spellcheck
        [/message]

        [delay]
            time=1500
        [/delay]

        [message]
            speaker=narrator
            image=wesnoth-icon.png
            message= _ "Just as she tries, to no avail, to stand up, Elynia senses the presence of creatures of some sort amidst the dense night fog."
        [/message]

        [delay]
            time=750
        [/delay]

        {F_SPIRIT 17 12}

        {F_SPIRIT 14  7}

        {F_SPIRIT 20  7}

        {F_SPIRIT 14 10}

        {F_SPIRIT 20 10}

        {F_SPIRIT 17  6}

        [delay]
            time=1500
        [/delay]

        [message]
            speaker=narrator
            image=wesnoth-icon.png
            message= _ "And then..."
        [/message]

        [delay]
            time=250
        [/delay]

        #
        # Screen fade to black
        #

        [kill]
            # We won't need these again.
            type=Fire Wraith
        [/kill]

        [hide_unit][/hide_unit]

        {FADE_TO_BLACK}

        {F_FILL_SHROUD}

        #
        # End of fade to black.
        #

        [scroll_to_unit]
            id=Elynia
            {WARP}
        [/scroll_to_unit]

        [lua]
            code= << music_fade_out() >> # 1000 ms delay
        [/lua]

        [message]
            speaker=narrator
            image=wesnoth-icon.png
            message= _ "... darkness."
        [/message]

        [delay]
            time=2000
        [/delay]

        [message]
            speaker=narrator
            image=misc/blank-hex.png
            message= _ "I cannot bring myself to kill someone so much weaker than I."
        [/message]

        [delay]
            time=500
        [/delay]

        [message]
            speaker=narrator
            caption= _ "Elynia"
            image=misc/blank-hex.png
            # po: The implication is not only that Elynia is hearing words she
            # po: herself uttered once (near the end of IftU), but also that
            # po: she is hearing them in her voice, possibly as a result of a
            # po: head trauma-induced hallucination.
            message= _ "Are those my words?"
        [/message]

        [delay]
            time=500
        [/delay]

        [message]
            speaker=narrator
            image=misc/blank-hex.png
            message= _ "(in a seductive tone) It would be unfair... and cruel..."
        [/message]

        [delay]
            time=500
        [/delay]

        [message]
            speaker=narrator
            caption= _ "Elynia"
            image=misc/blank-hex.png
            # po: See the previous implication. This is why the line is "who
            # po: are you?" and not "who is there?".
            message= _ "Who are you?"
        [/message]

        [delay]
            time=2000
        [/delay]

        # wmllint: recognize Elyssa
        [hidden_unit]
            {CHARACTER_STATS_ELYSSA}
            side=2
            canrecruit=yes
            #x,y=1,1
            x,y=18,8
            facing=sw
        [/hidden_unit]

        [lua]
            code= << music_fade_in() >> # 1000 ms delay
        [/lua]

        [message]
            speaker=narrator
            image=wesnoth-icon.png
            # po: Take note of the description. She continues to do this for
            # po: the remainder of the scene.
            message= _ "The demoness stared directly into her eyes, with a deeply unsettling, emotionless expression."
        [/message]

        [delay]
            time=500
        [/delay]

        [message]
            speaker=narrator
            image=wesnoth-icon.png
            message= _ "Elynia was still unable to stand up and defend herself."
        [/message]

        [message]
            speaker=narrator
            image=wesnoth-icon.png
            message= _ "As Elyssa bent down to reach her head, Elynia prepared for the inevitable."
        [/message]

        [delay]
            time=750
        [/delay]

        [message]
            speaker=narrator
            image=wesnoth-icon.png
            message= _ "But instead of dealing the killing blow, Elyssa stroked the elf’s hair — the expression on her face unchanged — and continued to speak in a soft tone of voice."
        [/message]

        [delay]
            time=500
        [/delay]

        [message]
            speaker=Elyssa
            message= _ "I don’t want to kill you."
        [/message]

        [delay]
            time=500
        [/delay]

        [message]
            speaker=Elyssa
            # po: "He" refers to Argan.
            message= _ "You spared my life, just like he did."
        [/message]

        [delay]
            time=500
        [/delay]

        [message]
            speaker=Elyssa
            # po: No, she isn't sorry. Unfortunately there's pretty much no
            # po: way to convey this in direct written speech, so we only have
            # po: the previous description of an emotionless expression. You
            # po: could say she is being sardonic about it all; after all, she
            # po: also knows for a fact that Elynia won't actually step aside.
            # po: Finally, the person who "didn't give [her] a choice" is
            # po: Uria.
            message= _ "I’m truly sorry about your friends. She didn’t give me a choice. Now, just let me take what I need, and die in peace if you need to — otherwise, don’t inconvenience us anymore and I’ll never cause you any harm again."
        [/message]

        [remove_item]
            x,y=18,9
        [/remove_item]

        [delay]
            time=500
        [/delay]

        [message]
            speaker=narrator
            image=wesnoth-icon.png
            message= _ "Elynia felt the exchange could have taken place at a better time, and not during such a terrible display of weakness."
        [/message]

        [delay]
            time=500
        [/delay]

        #
        # NOTE: This line uses the internal monologue convention even though
        #       she is addressing Elyssa. This is on purpose.
        #
        [message]
            speaker=narrator
            caption= _ "Elynia"
            image=misc/blank-hex.png
            message= _ "... Why... why do you do this..."
        [/message]

        [delay]
            time=250
        [/delay]

        [message]
            speaker=Elyssa
            message= _ "... Because it’s all I have left, my dear."
        [/message]

        [delay]
            time=1000
        [/delay]

        #
        # (Sudden) screen restore, unhiding all units.
        #

        {F_CLEAR_SHROUD}

        {RESET_SCREEN}

        [unhide_unit][/unhide_unit]

        [object]
            [filter]
                id=Elyssa
            [/filter]
            [effect]
                apply_to=variation
                name=e2s12_cutscene
            [/effect]
            silent=yes
        [/object]

        #
        # End of screen restore.
        #

        [move_unit]
            id=Elyssa
            to_x,to_y=20,9
            fire_event=no
        [/move_unit]

        [delay]
            time=500
        [/delay]

        [message]
            speaker=Elyssa
            # po: Still sardonic.
            message= _ "... And thank you for getting rid of that bothersome upstart for me."
        [/message]

        [move_unit]
            id=Elyssa
            # NOTE: just a bit into the fog, out of the player vision range
            to_x,to_y=25,12
            fire_event=no
        [/move_unit]

        [kill]
            fire_event=no
            animate=no
        [/kill]

        {FADE_TO_BLACK}

        {F_FILL_SHROUD}

        [delay]
            time=2000
        [/delay]

        [message]
            speaker=narrator
            image=misc/blank-hex.png
            message= _ "... Elynia?"
        [/message]

        [delay]
            time=1000
        [/delay]

        {APPEND_MUSIC "into_the_shadows.ogg"}

        {ENDLEVEL_QUIET} {NO_START_OF_SCENARIO_SAVE} {NO_REPLAY_SAVE}
    [/event]

#undef F_CLEAR_SHROUD
#undef F_FILL_SHROUD
#undef F_SPIRIT
[/scenario]

# kate: indent-mode normal; encoding utf-8; space-indent on;


#textdomain wesnoth-After_the_Storm

#define CHARACTER_STATS_ELYNIA
    id=Elynia
    name= _ "Elynia"
    type=Sylvan Warden
    profile="portraits/elynia.png"
    canrecruit=yes
    unrenamable=yes
    [modifications]
        {TRAIT_RESILIENT}
        {TRAIT_DEXTROUS}
    [/modifications]
#enddef

#define CHARACTER_STATS_DURVAN
    id=Durvan
    name= _ "Durvan"
    type=Trapper
    #profile="portraits/durvan.png"
    {IS_HERO}
    unrenamable=yes
    [modifications]
        {TRAIT_RESILIENT}
        {TRAIT_STRONG}
        {MOD_LOYAL_HERO}
    [/modifications]
#enddef

#define CHARACTER_STATS_ANYA
    id=Anya
    name= _ "Anya"
    type=Night Nymph
    gender=female
    #profile="portraits/anya.png"
    {IS_HERO}
    unrenamable=yes
    [modifications]
        {TRAIT_QUICK}
        {TRAIT_RESILIENT}
        {MOD_LOYAL_HERO}
    [/modifications]
#enddef

#define CHARACTER_STATS_IRYLEAN
    id=Irylean
    name= _ "Irylean"
    type=Necromancer
    gender=female
    #profile="portraits/irylean.png"
    {IS_HERO}
    unrenamable=yes
    [modifications]
        {TRAIT_STRONG}
        {TRAIT_INTELLIGENT}
        {TEAM_COLOR_OVERRIDE () white}
        {MOD_LOYAL_HERO}
    [/modifications]
#enddef

#define CHARACTER_STATS_UNARYE
    id=Unarye
    name= _ "Unarye"
    type=Elvish Avatar
    #profile="portraits/unarye.png"
    profile="portraits/elves/sylph.png~RIGHT()"
    unrenamable=yes
    [modifications]
        {TRAIT_DEXTROUS}
        {TRAIT_INTELLIGENT}
    [/modifications]
#enddef

#define CHARACTER_STATS_GENERAL_BARDYL
    id=General Bardyl
    name= _ "General Bardyl"
    type=Aragwaith Ancient Banner
    #profile="portraits/bardyl.png"
    canrecruit=yes
    unrenamable=yes
    [modifications]
        {TRAIT_STRONG}
        {TRAIT_RESILIENT}
    [/modifications]
#enddef

#define CHARACTER_STATS_KYARA
    id=Kyara
    name= _ "Kyara"
    type=Quenoth Marksman
    gender=female
    #profile="portraits/kyara.png"
    {IS_HERO}
    unrenamable=yes
    [modifications]
        {TRAIT_STRONG}
        {TRAIT_DEXTROUS}
        {MOD_LOYAL_HERO}
    [/modifications]
#enddef

#define CHARACTER_STATS_HORO
    id=Horo
    name= _ "Horo"
    type=Orcish Warlord
    #profile="portraits/horo.png"
    profile="portraits/orcs/grunt-5.png"
    {IS_HERO}
    unrenamable=yes
    [modifications]
        {TRAIT_QUICK}
        {TRAIT_RESILIENT}
        {MOD_LOYAL_HERO}
    [/modifications]
#enddef

#define CHARACTER_STATS_CRON
    id=Cron
    name= _ "Cron"
    type=Troll Warrior
    profile="portraits/trolls/troll-hero-alt.png"
    #profile="portraits/cron.png"
    {IS_HERO}
    unrenamable=yes
    [modifications]
        {TRAIT_INTELLIGENT}
        {TRAIT_RESILIENT}
        {MOD_LOYAL_HERO}
    [/modifications]
#enddef

#define CHARACTER_STATS_ERGEA
    id=Ergea
    name= _ "Ergea"
    type=Demon Hellbent Tide
    #profile="portraits/ergea.png"
    {IS_HERO}
    unrenamable=yes
    [modifications]
        {TRAIT_STRONG}
        {TRAIT_RESILIENT}
        {MOD_LOYAL_HERO}
    [/modifications]
#enddef

#define CHARACTER_STATS_DAEIRA
    id=Daeira
    name= _ "Naiad"
    type=Naiad
    #profile="portraits/daeira.png"
    {IS_HERO}
    unrenamable=yes
    [modifications]
        {TRAIT_DEXTROUS}
        {TRAIT_INTELLIGENT}
        {MOD_LOYAL_HERO}

        [object]
            [effect]
                apply_to=image_mod
                add="RC(magenta>lightblue)"
            [/effect]
        [/object]
    [/modifications]
#enddef

#define RECALL_DURVAN
    {RECALL_CHARACTER ({CHARACTER_STATS_DURVAN})}
#enddef
#define RECALL_DURVAN_AT_LOCATION _X _Y
    {RECALL_CHARACTER_AT ({CHARACTER_STATS_DURVAN}) ({_X}) ({_Y})}
#enddef

#define RECALL_ANYA
    {RECALL_CHARACTER ({CHARACTER_STATS_ANYA})}
#enddef
#define RECALL_ANYA_AT_LOCATION _X _Y
    {RECALL_CHARACTER_AT ({CHARACTER_STATS_ANYA}) ({_X}) ({_Y})}
#enddef

#define RECALL_IRYLEAN
    {RECALL_CHARACTER ({CHARACTER_STATS_IRYLEAN})}
#enddef
#define RECALL_IRYLEAN_AT_LOCATION _X _Y
    {RECALL_CHARACTER_AT ({CHARACTER_STATS_IRYLEAN}) ({_X}) ({_Y})}
#enddef

#define RECALL_KYARA
    {RECALL_CHARACTER ({CHARACTER_STATS_KYARA})}
    [+unit]
        side=2
    [/unit]
#enddef
#define RECALL_KYARA_AT_LOCATION _X _Y
    {RECALL_CHARACTER_AT ({CHARACTER_STATS_KYARA}) ({_X}) ({_Y})}
    [+unit]
        side=2
    [/unit]
#enddef

#define RECALL_HORO
    {RECALL_CHARACTER ({CHARACTER_STATS_HORO})}
    [+unit]
        side=2
    [/unit]
#enddef
#define RECALL_HORO_AT_LOCATION _X _Y
    {RECALL_CHARACTER_AT ({CHARACTER_STATS_HORO}) ({_X}) ({_Y})}
    [+unit]
        side=2
    [/unit]
#enddef

#define RECALL_CRON
    {RECALL_CHARACTER ({CHARACTER_STATS_CRON})}
#enddef
#define RECALL_CRON_AT_LOCATION _X _Y
    {RECALL_CHARACTER_AT ({CHARACTER_STATS_CRON}) ({_X}) ({_Y})}
#enddef

#define RECALL_ERGEA
    {RECALL_CHARACTER ({CHARACTER_STATS_ERGEA})}
#enddef
#define RECALL_ERGEA_AT_LOCATION _X _Y
    {RECALL_CHARACTER_AT ({CHARACTER_STATS_ERGEA}) ({_X}) ({_Y})}
#enddef

#define RECALL_ELYSSA
    {RECALL_CHARACTER (
        {CHARACTER_STATS_ELYSSA}
        canrecruit=yes
        type=Guardian of Darkness Elyssa
    )}
#enddef
#define RECALL_ELYSSA_AT_LOCATION _X _Y
    {RECALL_CHARACTER_AT (
        {CHARACTER_STATS_ELYSSA}
        canrecruit=yes
        type=Guardian of Darkness Elyssa
    ) ({_X}) ({_Y})}
#enddef

#define RECALL_DAEIRA
    [recall]
        id=Daeira
        show=no
    [/recall]
#enddef

#define RECALL_DAEIRA_AT_LOCATION _X _Y
    [recall]
        id=Daeira
        x={_X}
        y={_Y}
        show=no
    [/recall]
#enddef

#define AMLA_MENU_HINT
    {AMLA_MENU_HINT:BASE ( _ "Certain high-level units in this campaign under your control — such as Elynia and Anya — have special After Maximum Level Advancements (AMLAs). You can learn more about those by right-clicking applicable units and choosing the Unit Advancements option.")}
#enddef

# kate: indent-mode normal; encoding utf-8; space-indent on;


#textdomain wesnoth-After_the_Storm

#define F_DEATHS
    {F_DEATH_ELYNIA}
    {F_DEATH_ANYA}
    {F_DEATH_DURVAN}
    {F_DEATH_IRYLEAN}
    {F_DEATH_IGOR}
#enddef

#define G_DEATHS
    {F_DEATH_ELYNIA}
    {F_DEATH_ANYA}
    {F_DEATH_DURVAN}
    {F_DEATH_IRYLEAN}
    {F_DEATH_IGOR}
    {G_DEATH_HORO}
    {G_DEATH_KYARA}
    {G_DEATH_BARDYL}
#enddef

#define H_DEATHS
    {H_DEATH_ANYA}
    {H_DEATH_DURVAN}
    {H_DEATH_IRYLEAN}
    {H_DEATH_IGOR}
    {H_DEATH_ERGEA}
    {H_DEATH_CRON}
    {H_DEATH_DAEIRA}
#enddef

#define I_DEATHS
    {I_DEATH_ELYNIA}
    {I_DEATH_ELYSSA}
#enddef

#define J_DEATHS
    {H_DEATH_DURVAN}
    {H_DEATH_IRYLEAN}
    {H_DEATH_IGOR}
    {H_DEATH_ERGEA}
    {H_DEATH_CRON}
    {H_DEATH_DAEIRA}
#enddef

#define K_DEATHS
    {K_DEATH_ELYNIA}
    {I_DEATH_ELYSSA}
#enddef

# Anya can fire last breath events prematurely when using her abscond ability
# in this episode. Let's avoid that, shall we?
#define ANYA_IS_NOT_ABSCONDING
    [+filter]
        [not]
            status=absconding
        [/not]
    [/filter]
#enddef

#define F_DEATH_ELYNIA
    {HERO_DEATH_BASE
    (Elynia)
    (
        [message]
            speaker=unit
            message= _ "I... fall to the darkness."
        [/message]
    ) (
        [message]
            speaker=Anya
            message= _ "No! Elynia, please!"
        [/message]
    )}
#enddef

#define F_DEATH_ANYA
    {HERO_DEATH_BASE
    (Anya)
    (
        {ANYA_IS_NOT_ABSCONDING}
        [message]
            speaker=unit
            message= _ "No... Elynia... save me..."
        [/message]
    ) (
        [message]
            speaker=Elynia
            message= _ "No... NO!"
        [/message]
    )}
#enddef

#define F_DEATH_DURVAN
    {HERO_DEATH_BASE
    (Durvan)
    (
        [message]
            speaker=unit
            message= _ "I guess... I will never see my home again..."
        [/message]
    ) ()}
#enddef

#define F_DEATH_IRYLEAN
    {HERO_DEATH_BASE
    (Irylean)
    (
        [message]
            speaker=unit
            message= _ "It is over for me..."
        [/message]
    ) ()}
#enddef

#define F_DEATH_IGOR
    {MINOR_DEATH_EVENT
    (Igor)
    (
        [message]
            speaker=unit
            # po: He's referring to Galas and Mal Keshar.
            message= _ "I couldn’t... I couldn’t avenge the elf friend and the scary lich..."
        [/message]
    )
    (
        [message]
            speaker=Elynia
            message= _ "I should have seen this coming... I’m sorry we failed to protect you, friend. I’m sorry..."
        [/message]
    )}
#enddef

#define G_DEATH_KYARA
    {HERO_DEATH_BASE
    (Kyara)
    (
        [message]
            speaker=unit
            message= _ "I don’t think I— ARGH!"
        [/message]
    ) ()}
#enddef

#define G_DEATH_HORO
    {HERO_DEATH_BASE
    (Horo)
    (
        [message]
            speaker=unit
            message= _ "This couldn’t be more untimely... ugh..."
        [/message]
    ) ()}
#enddef

#define G_DEATH_BARDYL
    {HERO_DEATH_BASE
    (General Bardyl)
    (
        [message]
            speaker=unit
            message= _ "Alas, I thought I’d... recover Aran-Balgur... before... this..."
        [/message]
    ) ()}
#enddef

#define H_DEATH_ANYA
    {HERO_DEATH_BASE
    (Anya)
    (
        {ANYA_IS_NOT_ABSCONDING}
        [message]
            speaker=unit
            message= _ "Oh, Elynia... I wish... you could be... with me now..."
        [/message]
    ) ()}
#enddef

#define H_DEATH_DURVAN
    {HERO_DEATH_BASE
    (Durvan)
    (
        [message]
            speaker=unit
            message= _ "... I knew this was... a terrible idea... heh...."
        [/message]
    ) (
        [message]
            speaker=Anya
            message= _ "No! It didn’t have to end like this!"
        [/message]
    )}
#enddef

#define H_DEATH_IRYLEAN
    {HERO_DEATH_BASE
    (Irylean)
    (
        [message]
            speaker=unit
            message= _ "Alas... nobody will mourn my passing..."
        [/message]
    ) ()}
#enddef

#define H_DEATH_IGOR
    {MINOR_DEATH_EVENT
    (Igor)
    (
        [message]
            speaker=unit
            message= _ "No... we were so close to the end..."
        [/message]
    )
    (
        [message]
            speaker=Anya
            message= _ "I cannot believe it... I’m sorry, Elynia. I could’ve done better, I could’ve saved him..."
        [/message]

        [message]
            speaker=Durvan
            message= _ "It’s not your fault, Anya. At least he died a hero — better than can be said for most goblins who just end up as cannon fodder for— Irylean what are you doing??"
        [/message]

        [message]
            speaker=Irylean
            message= _ "Master Mal Keshar once told me that the goblin’s life-long wish was to rise again as a fearsome undead champion."
        [/message]

        [message]
            speaker=Anya
            message= _ "No!!! I am <b>not</b> allowing you to do this! I never had the chance to meet the guy but I have a feeling he wasn’t being serious when he said that."
        [/message]

        [message]
            speaker=Irylean
            message= _ "Very well, if you say so... I’ve got to admit I am not very good at raising the recently deceased anyway."
        [/message]
    )}
#enddef

#define H_DEATH_ERGEA
    {HERO_DEATH_BASE
    (Ergea)
    (
        [message]
            speaker=unit
            message= _ "I failed you all..."
        [/message]
    ) ()}
#enddef

#define H_DEATH_DAEIRA
    [event]
        name=last breath
        [filter]
            id=Daeira
        [/filter]

        [message]
            speaker=unit
            message= _ "This battle is not mine to fight. Farewell, Anya."
        [/message]
    [/event]
#enddef

#define H_DEATH_CRON
    {HERO_DEATH_BASE
    (Cron)
    (
        [message]
            speaker=unit
            message= _ "Cron falls... and bids the storm lady... farewell..."
        [/message]
    ) (
        [message]
            speaker=Ergea
            message= _ "No! This... this was not supposed to happen! Cron!"
        [/message]
    )}
#enddef

#define I_DEATH_ELYNIA
    {HERO_DEATH_SIMPLE Elynia (
        [message]
            speaker=Elyssa
            message= _ "Such an incompetent fool. I should have known she would be useless in the end."
        [/message]
    )}
#enddef

#define I_DEATH_ELYSSA
    {HERO_DEATH_SIMPLE Elyssa (
        [message]
            speaker=Elynia
            message= _ "Even with her greater experience... This is hopeless..."
        [/message]
    )}
#enddef

#define K_DEATH_ELYNIA
    {HERO_DEATH_SIMPLE Elynia (
        [message]
            speaker=Elyssa
            message= _ "I should have known this would fail so horribly in the end..."
        [/message]
    )}
#enddef

# kate: indent-mode normal; encoding utf-8; space-indent on;


#textdomain wesnoth-After_the_Storm

#ifhave ~add-ons/After_the_Storm/Naia
#define ATS_MENU_DRYAD_PATH
data/add-ons/After_the_Storm/Naia/images/units/faeries/dryad.png#enddef
#else
#define ATS_MENU_DRYAD_PATH
data/add-ons/Naia/images/units/faeries/dryad.png#enddef
#endif

[campaign]
    id=After_the_Storm_III
    {ATS_SHARED_ATTRIBUTES III}

    abbrev= _ "AtS-3"
    name=_"After the Storm"+"
"+_"Final"
    icon="data/add-ons/After_the_Storm/images/campaign-icon-episode3.png"
    image="data/add-ons/After_the_Storm/images/campaign-logo-episode3.png"
    background="story/landscape-hills-01.jpg"
    first_scenario=00_Opening

    description= _ "Though at the end of the day darkness may claim our homes and lives and trap the world beneath its impenetrable veil, the night is not eternal, and even amidst chaos, hope shines forth as a beacon which can help us vanquish our worst fears and reunite us with our destiny."+"

"+_"(Intermediate level, 13 scenarios.)"+{ATS_SHARED_DESCRIPTION_FOOTER}

    # PAL: faerie_sylvan_wings>faerie_neutral_wings
    {ATS_DIFFICULTY_MENU
    (_"difficulty_menu^Dancer")  ("data/add-ons/After_the_Storm/images/units/faeries/eventide-dancer.png")
    (_"difficulty_menu^Dryad")   ("{ATS_MENU_DRYAD_PATH}~PAL(bee0f6,77cba7,61a280>d0f4f1,87d5cf,47b8af)")
    (_"difficulty_menu^Warden")  ("data/add-ons/After_the_Storm/images/units/faeries/elynia-attack-melee-5.png~CROP(4,4,72,72)")
    }

    end_text=" " # wmllint: ignore

    {ATS_CREDITS_BACKGROUND "misc/blank-hex.png~BG()"}
[/campaign]

#undef ATS_MENU_DRYAD_PATH

# kate: indent-mode normal; encoding utf-8; space-indent on;