#textdomain wesnoth-After_the_Storm

[scenario]
    id=10_Blood
    name= _ "Blood"
    {MAP 10_Blood.map}
    turns=-1
    next_scenario=11_After_the_Storm
    victory_when_enemies_defeated=no
    disallow_recall=yes
    # We do not need this because the player 'Elynia' is never used again
    # after this point.
    #{DO_NOT_REMOVE_FROM_CARRYOVER_ON_DEFEAT}

    {K_DEATHS}

    {FINALE_AMLA_EVENTS}

    {SCENARIO_MUSIC       "gameplay06.ogg"}
    {EXTRA_SCENARIO_MUSIC "nunc_dimittis.ogg"}
    {EXTRA_SCENARIO_MUSIC "heroes_rite.ogg"}
    {EXTRA_SCENARIO_MUSIC "weight_of_revenge.ogg"}

    {STORYTXT_BLOOD}

    {SPAWN_CONTROLLER}

    {UNDERGROUND} {SCHEDULE_LIGHTING 20 -20 -20}

    {SHAXTHAL_SET_SURFACE_VARIATIONS_FLAG}

#define FINALE_B_SHARED_AI_PARAMS
    {AI_SIMPLE_ALWAYS_ASPECT aggression        9.00}
    {AI_SIMPLE_ALWAYS_ASPECT leader_aggression 9.00}
    {AI_SIMPLE_ALWAYS_ASPECT caution           0.00}
    {AI_SIMPLE_ALWAYS_ASPECT grouping            no}
#enddef

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

        fog=yes
        shroud=yes

        {NO_ECONOMY}

        share_vision=none

        # wmllint: recognize Elynia
        {CHARACTER_STATS_ELYNIA}
        type=Guardian of Earth Elynia
        canrecruit=yes
    [/side]
    # wmllint: validate-on

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

        hidden=yes
        no_leader=yes

        {NO_ECONOMY}

        {ai/aliases/stable_singleplayer.cfg}
        [ai]
            {FINALE_B_SHARED_AI_PARAMS}

            {AI_BOSS_TARGETING_ENGINE <<{"Elyssa","Elynia"}>>}

            [goal]
                [criteria]
                    id=Elyssa
                [/criteria]
                value=200
            [/goal]

            [goal]
                [criteria]
                    id=Elynia
                [/criteria]
                value=200
            [/goal]

            [goal]
                [criteria]
                    type=Fire Wraith
                [/criteria]
                value=1
            [/goal]
        [/ai]
    [/side]

    [side]
        # Demons
        side=3
        team_name=evil
        user_team_name= _ "team_name^Zhangor"
        {CHAOS_FLAG}

        hidden=yes
        no_leader=yes

        {NO_ECONOMY}

        {ai/aliases/stable_singleplayer.cfg}
        [ai]
            {FINALE_B_SHARED_AI_PARAMS}
        [/ai]

        {GENERIC_UNIT () (Psy Mindraider) 33 30} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {FACING sw}

        {GENERIC_UNIT () (Psy Crawler) 32 29} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {FACING sw}
        {GENERIC_UNIT () (Psy Crawler) 32 30} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {FACING sw}
        {GENERIC_UNIT () (Psy Crawler) 33 31} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {FACING sw}

        {GENERIC_UNIT () (Psy Crawler) 23 32} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN}
        {GENERIC_UNIT () (Psy Crawler) 30 25} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN}
        {GENERIC_UNIT () (Psy Crawler) 24 52} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN}
        {GENERIC_UNIT () (Psy Crawler) 33 56} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN}
    [/side]

    [side]
        # Shaxthal
        side=4
        team_name=evil
        user_team_name= _ "team_name^Zhangor"
        {CHAOS_FLAG}

        hidden=yes
        no_leader=yes

        {NO_ECONOMY}

        {ai/aliases/stable_singleplayer.cfg}
        [ai]
            {FINALE_B_SHARED_AI_PARAMS}
        [/ai]

        {GENERIC_UNIT () (Shaxthal Custodian Drone) 25 36} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {FACING se}
        {GENERIC_UNIT () (Shaxthal Custodian Drone) 32 37} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {FACING sw}
        {GENERIC_UNIT () (Shaxthal Custodian Drone) 27 26} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {FACING se}
        {GENERIC_UNIT () (Shaxthal Custodian Drone) 21 24} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {FACING se}
        {GENERIC_UNIT () (Shaxthal Custodian Drone) 32 19} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {FACING sw}
    [/side]

    [side]
        # Undead
        side=5
        team_name=evil
        user_team_name= _ "team_name^Zhangor"
        {CHAOS_FLAG}

        hidden=yes
        no_leader=yes

        {NO_ECONOMY}

        {ai/aliases/stable_singleplayer.cfg}
        [ai]
            {FINALE_B_SHARED_AI_PARAMS}
        [/ai]

        {GENERIC_UNIT () (Fallen Faerie) 15 51} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {FACING se}
        {GENERIC_UNIT () (Fallen Faerie) 28 48} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {FACING se}
        {GENERIC_UNIT () (Fallen Faerie) 33 50} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {FACING sw}
        {GENERIC_UNIT () (Fallen Faerie) 43 49} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {FACING sw}

        {GENERIC_UNIT () (Fallen Faerie) 29 43} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {FACING se}
        {GENERIC_UNIT () (Fallen Faerie) 32 42} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {FACING sw}

        {GENERIC_UNIT () (Fallen Faerie) 24 28} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN}
        {GENERIC_UNIT () (Fallen Faerie) 29 29} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN}
        {GENERIC_UNIT () (Fallen Faerie) 31 17} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN}
        {GENERIC_UNIT () (Fallen Faerie) 39 47} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN}
        {GENERIC_UNIT () (Fallen Faerie) 21 48} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN}

        {GENERIC_UNIT () (Revenant) 26 58} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {FACING se}
        {GENERIC_UNIT () (Revenant) 32 45} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {FACING sw}

        {GENERIC_UNIT () (Death Knight) 36 57} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {FACING sw}

        {GENERIC_UNIT () (Soulless) 32 58} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {VARIATION swimmer}
        {GENERIC_UNIT () (Soulless) 34 61} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {VARIATION swimmer}
        {GENERIC_UNIT () (Soulless) 26 61} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {VARIATION swimmer}
        {GENERIC_UNIT () (Soulless) 24 57} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {VARIATION swimmer}
        {GENERIC_UNIT () (Soulless) 22 55} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {VARIATION swimmer}
        {GENERIC_UNIT () (Soulless) 21 61} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {VARIATION swimmer}
        {GENERIC_UNIT () (Soulless) 36 55} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {VARIATION swimmer}
        {GENERIC_UNIT () (Soulless) 40 52} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {VARIATION swimmer}
        {GENERIC_UNIT () (Soulless) 41 55} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {VARIATION swimmer}
        {GENERIC_UNIT () (Soulless) 45 54} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {VARIATION swimmer}
        {GENERIC_UNIT () (Soulless) 30 46} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {VARIATION swimmer}
        {GENERIC_UNIT () (Soulless) 33 48} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {VARIATION swimmer}
        {GENERIC_UNIT () (Soulless) 18 49} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {VARIATION swimmer}

        {GENERIC_UNIT () (Soulless) 26 54} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {VARIATION wose}
        {GENERIC_UNIT () (Soulless) 41 51} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {VARIATION wose}
        {GENERIC_UNIT () (Soulless) 33 53} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {VARIATION wose}
        {GENERIC_UNIT () (Soulless) 12 54} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {VARIATION wose}
        {GENERIC_UNIT () (Soulless) 18 61} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {VARIATION wose}
        {GENERIC_UNIT () (Soulless) 39 61} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {VARIATION wose}
        {GENERIC_UNIT () (Soulless) 46 49} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {VARIATION wose}

        {GENERIC_UNIT () (Soulless) 30 32} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {VARIATION wose}
        {GENERIC_UNIT () (Soulless) 23 26} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {VARIATION wose}
        {GENERIC_UNIT () (Soulless) 11 25} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {VARIATION wose}
        {GENERIC_UNIT () (Soulless) 13 41} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {VARIATION wose}
        {GENERIC_UNIT () (Soulless) 39 39} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {VARIATION wose}
        {GENERIC_UNIT () (Soulless) 49 27} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {VARIATION wose}
        {GENERIC_UNIT () (Soulless) 49 21} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {VARIATION wose}

        {GENERIC_UNIT () (Soulless) 23 50} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {VARIATION wose}
        {GENERIC_UNIT () (Soulless) 38 51} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {VARIATION wose}
        {GENERIC_UNIT () (Soulless) 31 28} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {VARIATION wose}
        {GENERIC_UNIT () (Soulless) 23 33} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {VARIATION wose}

        {GENERIC_UNIT () (Spectre) 40 30} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN}
        {GENERIC_UNIT () (Spectre) 27 31} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN}
        {GENERIC_UNIT () (Spectre) 17 29} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN}

        {GENERIC_UNIT () (Spectre) 28 17} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN}

        {GENERIC_UNIT () (Spectre)  6 41} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN}
        {GENERIC_UNIT () (Spectre) 11 35} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN}
        {GENERIC_UNIT () (Spectre) 19 33} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN}

        {GENERIC_UNIT () (Spectre) 41 36} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN}
        {GENERIC_UNIT () (Spectre) 48 33} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN}
        {GENERIC_UNIT () (Spectre) 41 24} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN}

        {GENERIC_UNIT () (Ghost) 16 14} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN}
        {GENERIC_UNIT () (Ghost)  8 26} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN}
        {GENERIC_UNIT () (Ghost) 47 53} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN}
        {GENERIC_UNIT () (Ghost) 39 62} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN}

        {GENERIC_UNIT () (Draug) 25 22} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {FACING se}
        {GENERIC_UNIT () (Draug) 31 22} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {FACING sw}

        {GENERIC_UNIT () (Spectre) 28 46} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {FACING se}
        {GENERIC_UNIT () (Spectre) 35 50} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {FACING sw}
        {GENERIC_UNIT () (Spectre) 27 52} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN} {FACING se}

        {GENERIC_UNIT () (Nightgaunt) 21 52} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN}
        {GENERIC_UNIT () (Nightgaunt) 43 51} {NO_UPKEEP_NO_OVERLAY} {GUARDIAN}

        # These aren't guardians. This is intentional.
        {GENERIC_UNIT () (Nightgaunt) 31 40} {NO_UPKEEP_NO_OVERLAY}
        {GENERIC_UNIT () (Nightgaunt) 26 33} {NO_UPKEEP_NO_OVERLAY}
        {GENERIC_UNIT () (Nightgaunt) 28 22} {NO_UPKEEP_NO_OVERLAY}
    [/side]

    {TIMED_DRONE_SPAWNER 6 (type,variation="Shaxthal Enforcer Drone",surface)  4 26 38}
    {TIMED_DRONE_SPAWNER 6 (type,variation="Shaxthal Enforcer Drone",surface)  4 34 45}

    {TIMED_DRONE_SPAWNER 3 (type,variation="Shaxthal Sentry Drone",surface)  4 10 38}
    {TIMED_DRONE_SPAWNER 3 (type,variation="Shaxthal Sentry Drone",surface)  4 11 40}
    {TIMED_DRONE_SPAWNER 3 (type,variation="Shaxthal Sentry Drone",surface)  4 14 43}
    {TIMED_DRONE_SPAWNER 3 (type,variation="Shaxthal Sentry Drone",surface)  4  2 36}
    {TIMED_DRONE_SPAWNER 3 (type,variation="Shaxthal Sentry Drone",surface)  4  1 31}
    {TIMED_DRONE_SPAWNER 3 (type,variation="Shaxthal Sentry Drone",surface)  4  8 23}
    {TIMED_DRONE_SPAWNER 3 (type,variation="Shaxthal Sentry Drone",surface)  4 45 32}
    {TIMED_DRONE_SPAWNER 3 (type,variation="Shaxthal Sentry Drone",surface)  4 46 35}
    {TIMED_DRONE_SPAWNER 3 (type,variation="Shaxthal Sentry Drone",surface)  4 42 38}
    {TIMED_DRONE_SPAWNER 3 (type,variation="Shaxthal Sentry Drone",surface)  4 49 35}
    {TIMED_DRONE_SPAWNER 3 (type,variation="Shaxthal Sentry Drone",surface)  4 54 34}

#ifndef EASY
    {TIMED_DRONE_SPAWNER 6 (type,variation="Shaxthal Sentry Drone",surface)  4 19 25}
    {TIMED_DRONE_SPAWNER 6 (type,variation="Shaxthal Sentry Drone",surface)  4 21 19}
    {TIMED_DRONE_SPAWNER 6 (type,variation="Shaxthal Sentry Drone",surface)  4 35 17}
    {TIMED_DRONE_SPAWNER 6 (type,variation="Shaxthal Sentry Drone",surface)  4 35 24}
#endif

    {FIRE_GUARDIAN_SUMMONING}

    [event]
        name=DEBUG1

        [kill]
            [filter_location]
                x,y=28,20
                radius=10
            [/filter_location]
        [/kill]

        [teleport]
            [filter]
                id=Elyssa
            [/filter]
            x,y=25,23
        [/teleport]

        [teleport]
            [filter]
                id=Elynia
            [/filter]
            x,y=26,23
        [/teleport]

        [fire_event]
            name=reveal keep
        [/fire_event]
    [/event]

#define FINALE_B_SIGIL_TILE_FRAGMENT _REL_X _REL_Y
    [tile]
        x={_REL_X}
        y={_REL_Y}
        type=!,W* # needed to void this rule on boss stage 4
    [/tile]
#enddef

    [terrain_graphics]
        x,y=27,19
        {FINALE_B_SIGIL_TILE_FRAGMENT 0 0}
        {FINALE_B_SIGIL_TILE_FRAGMENT 1 0}
        {FINALE_B_SIGIL_TILE_FRAGMENT 2 0}
        {FINALE_B_SIGIL_TILE_FRAGMENT 0 1}
        {FINALE_B_SIGIL_TILE_FRAGMENT 1 1} # keep at 28,20
        {FINALE_B_SIGIL_TILE_FRAGMENT 2 1}
        {FINALE_B_SIGIL_TILE_FRAGMENT 0 2}
        {FINALE_B_SIGIL_TILE_FRAGMENT 1 2}
        {FINALE_B_SIGIL_TILE_FRAGMENT 2 2}
        probability=100
        [image]
            center=94,144
            layer=0
            name="../scenery/sigil-4.png"
        [/image]
    [/terrain_graphics]

#undef FINALE_B_SIGIL_TILE_FRAGMENT

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

    [event]
        name=prestart

        {VARIABLE boss_stage      0}
        {VARIABLE boss_start_turn 0}

        {VARIABLE boss_skip_spawns_on_turn   0}
        {VARIABLE boss_spawn_sequence_count  0}
        {VARIABLE boss_status_flag           0}
        {VARIABLE boss_flood_active          no}

        {VARIABLE banshee_spawn_1st_run      yes}
        {VARIABLE satellite_spawn_1st_run    yes}
        {VARIABLE shapeshifter_spawn_1st_run yes}

        # Terrain information variables used by the boss fight proper later.

        [store_starting_location]
            side=2
            variable=boss_keep
        [/store_starting_location]

        [store_locations]
            terrain=Xos
            [and]
                x,y=$boss_keep.x,$boss_keep.y
                radius=7
            [/and]
            variable=core_locs
        [/store_locations]

        [store_locations]
            terrain=*^Fet*
            [and]
                x,y=$boss_keep.x,$boss_keep.y
                radius=7
            [/and]
            #
            # NOTE: this variable persists until the end of the
            # boss battle!
            #
            variable=banshee_locs
        [/store_locations]

        # Satellite cores will be spawned at locations that are exactly 6 tiles
        # away from the boss keep.

        [store_area_edge]
            x,y=$boss_keep.x,$boss_keep.y
            radius=6
            variable=satellite_locs
        [/store_area_edge]

        {BUG_ON ({VARIABLE_NUMERICAL_EQUALS core_locs.length 0}) ("core_locs.length == 0")}
        {BUG_ON ({VARIABLE_NUMERICAL_EQUALS banshee_locs.length 0}) ("banshee_locs.length == 0")}
        {BUG_ON ({VARIABLE_NUMERICAL_EQUALS satellite_locs.length 0}) ("satellite_locs.length == 0")}

        # Aspect combos shit is too complicated to setup here,
        # so it's done later on.

        # wmllint: recognize Elyssa
        {RECALL_ELYSSA_AT_LOCATION 29 65}

        {FACE_DIRECTION id=Elyssa se}
        {FACE_DIRECTION id=Elynia nw}

        {OBJECTIVES (
            victory_string= _ "Current Objective:"
            {OBJECTIVE_VICTORY ( _ "Locate Zhangor")}

            {OBJECTIVE_DEFEAT  ( _ "Death of Elyssa")}
            {OBJECTIVE_DEFEAT  ( _ "Death of Elynia")}

            {OBJECTIVE_NO_CARRYOVER}

            {FIRE_GUARDIAN_SUMMONING_NOTE}
        )}

        [set_menu_item]
            # NOTE: the z prefix is intended to force this item
            # to go below the Fire Wraith summoning command.
            id=wmi_z_combo_attacks_info
            description= _ "Attack Combinations"
            image="icons/menu-book.png"
            synced=no
            [command]
                [combo_info_dialog][/combo_info_dialog]
            [/command]
        [/set_menu_item]

        # Allow Elyssa to summon Fire Wraiths next to Elynia
        [modify_unit]
            [filter]
                id=Elynia
            [/filter]
            [variables]
                is_summoning_target=yes
            [/variables]
        [/modify_unit]
    [/event]

    [event]
        name=start

        [message]
            speaker=Elynia
            message= _ "Blood?"
        [/message]

        [message]
            speaker=Elyssa
            message= _ "Yes, more blood. You might want to get used to it — there is a lot of it in the place we are headed towards."
        [/message]

        [transient_message]
            image=units/monsters/firewraith/firewraith.png
            message= _ "Elyssa can now summon Fire Wraiths next to Elynia regardless of the distance between both."
        [/transient_message]
    [/event]

    [event]
        name=turn 2

        [message]
            speaker=Elyssa
            message= _ "If we want to have any chance of killing that bastard for good, we must stick together and try to combine our powers and attacks in different ways. Your aspect is undeniably affiliated to the path of Light; mine is Darkness. Perhaps, even without the Union, we may be able to achieve something in some form."
        [/message]

        [message]
            speaker=Elynia
            message= _ "That is why you wanted me by your side, right?"
        [/message]

        [message]
            speaker=Elyssa
            # po: "Us" = Argan and Elyssa.
            message= _ "Yes. It is my hope that you... will not let us down."
        [/message]

        [transient_message]
            image=icons/book.png
            message= _ "Right click on the map and choose <b>Attack Combinations</b> for information on the possible attack combinations you can use and their effects."
        [/transient_message]
    [/event]

#define FINALE_B_GLYPH_CONDITIONS
    {VARIABLE_NUMERICAL_NOT_EQUALS boss_stage 5}
#enddef

#ifdef EASY
    {OBJ_HEALING_GLYPH 19 21 CONDITIONS={FINALE_B_GLYPH_CONDITIONS}}
#else
    [event]
        name=prestart
        [remove_terrain_overlays]
            x,y=19,21
        [/remove_terrain_overlays]
    [/event]
#endif

    {OBJ_HEALING_GLYPH 20 16 CONDITIONS={FINALE_B_GLYPH_CONDITIONS}}

    {OBJ_HEALING_GLYPH 33 15 CONDITIONS={FINALE_B_GLYPH_CONDITIONS}}

    {OBJ_HEALING_GLYPH 36 23 CONDITIONS={FINALE_B_GLYPH_CONDITIONS}}

#ifndef HARD
    {OBJ_HEALING_GLYPH 25 27 CONDITIONS={FINALE_B_GLYPH_CONDITIONS}}
#else
    [event]
        name=prestart
        [remove_terrain_overlays]
            x,y=25,27
        [/remove_terrain_overlays]
    [/event]
#endif

    ############################################################################
    #                                                                          #
    #                               ASPECT COMBOS                              #
    #                                                                          #
    ############################################################################

#define COMBO_DBG _MSG
    {LOG_ATS_DBG ("COMBO: "+{_MSG})}
#enddef

#define COMBO_SIGIL _COL _ROW
    "icons/original-ten-sigils.png~CROP($(60*"+{_COL}+"), $(60*"+{_ROW}+"), 60, 60)"
#enddef

#define COMBO_SIGIL:EARTH
    {COMBO_SIGIL 3 0}
#enddef

#define COMBO_SIGIL:DARKNESS
    {COMBO_SIGIL 0 1}
#enddef

#define COMBO_EFFECT:DMG_MULTIPLY _FACTOR
    [effect]
        apply_to=damage
        multiply={_FACTOR}
    [/effect]
#enddef

#define COMBO_DEFINITION _NAME _SIDE_A _SIDE_B
#arg SYMMETRIC
    yes
#endarg
#arg EFFECTS
#endarg
    [+set_variables]
        [literal]
            name={_NAME}
            symmetric={SYMMETRIC}

            {EFFECTS}

            [side_a]
                {_SIDE_A}
            [/side_a]
            [side_b]
                {_SIDE_B}
            [/side_b]
        [/literal]
    [/set_variables]
#enddef

    [event]
        name=reset combo status
        first_time_only=no

        [set_variables]
            name=combo_status
            mode=replace
            [value]
                [side_a]
                    attacker,attack=$null,$null
                    [target]
                        x,y=0,0
                    [/target]
                [/side_a]
                [side_b]
                    attacker,attack=$null,$null
                    [target]
                        x,y=0,0
                    [/target]
                [/side_b]
            [/value]
        [/set_variables]
    [/event]

    [event]
        name=prestart

        {COMBO_DBG "initializing"}

        [fire_event]
            name=reset combo status
        [/fire_event]

        [set_variables]
            name=combo_attacks
            mode=replace
        [/set_variables]

        {COMBO_DEFINITION ( _ "inferno grasp")
            (
                attack_id=claw of urvatha
                attack_name= _ "claw of urvatha" # wmllint: ignore
                attack_icon=attacks/staff-elven-star.png # FIXME: placeholder
                aspect_icon={COMBO_SIGIL:DARKNESS}
            )
            (
                attack_id=ensnare
                attack_name= _ "ensnare" # wmllint: ignore
                attack_icon=attacks/entangle.png
                aspect_icon={COMBO_SIGIL:EARTH}
            )
            SYMMETRIC=no
            EFFECTS={COMBO_EFFECT:DMG_MULTIPLY 3.0}
        }

        {COMBO_DEFINITION ( _ "primal nightmare")
            (
                attack_id=claw of urvatha
                attack_name= _ "claw of urvatha" # wmllint: ignore
                attack_icon=attacks/staff-elven-star.png # FIXME: placeholder
                aspect_icon={COMBO_SIGIL:DARKNESS}
            )
            (
                attack_id=faerie touch
                attack_name= _"faerie touch" # wmllint: ignore
                attack_icon=attacks/touch-faerie.png
                aspect_icon={COMBO_SIGIL:EARTH}
            )
            SYMMETRIC=no
            EFFECTS={COMBO_EFFECT:DMG_MULTIPLY 4.0}
        }

        {COMBO_DEFINITION ( _ "flames of xia’el")
            (
                attack_id=pyranoctum
                attack_name= _"pyranoctum" # wmllint: ignore
                attack_icon=attacks/fireball.png
                aspect_icon={COMBO_SIGIL:DARKNESS}
            )
            (
                attack_id=arcane rage
                attack_name= _"arcane rage" # wmllint: ignore
                attack_icon=attacks/arcane-rage.png
                aspect_icon={COMBO_SIGIL:EARTH}
            )
            SYMMETRIC=yes
            EFFECTS={COMBO_EFFECT:DMG_MULTIPLY 2.0}
        }

        {COMBO_DEFINITION ( _ "lunar wrath")
            (
                attack_id=noctum
                attack_name= _"noctum" # wmllint: ignore
                attack_icon=attacks/noctum.png
                aspect_icon={COMBO_SIGIL:DARKNESS}
            )
            (
                attack_id=arcane rage
                attack_name= _"arcane rage" # wmllint: ignore
                attack_icon=attacks/arcane-rage.png
                aspect_icon={COMBO_SIGIL:EARTH}
            )
            SYMMETRIC=yes
            EFFECTS={COMBO_EFFECT:DMG_MULTIPLY 4.0}
        }

        {COMBO_DBG "$combo_attacks.length combos"}
    [/event]

    [event]
        name=victory

        {CLEAR_VARIABLE combo_attacks,combo_status}
    [/event]

    #
    # NOTE: Only during offense.
    #

    [event]
        name=attack
        first_time_only=no
        [filter]
            id=Elynia,Elyssa
        [/filter]
        [filter_condition]
            {VARIABLE_NUMERICAL_EQUALS side_number 1}
        [/filter_condition]

        # Did we already start an attack combo? If not then this is side A,
        # otherwise it's side B

        {VARIABLE_IF_ELSE combo_side a b ({VARIABLE_LEXICAL_EQUALS combo_status.side_a.attacker $null})}

        [set_variables]
            name=combo_status.side_$combo_side
            mode=replace
            [value]
                attacker=$unit.id
                attack=$weapon.name
                [target]
                    x,y=$x2,$y2
                [/target]
            [/value]
        [/set_variables]

        {COMBO_DBG "side $combo_side status set (attacker=$unit.id attack=$weapon.name [target] x,y=$x2,$y2)"}

        # Can we confirm a potential attack combo now?

        [if]
            {VARIABLE_LEXICAL_EQUALS combo_side b}

            {VARIABLE_POS_EQUALS combo_status.side_a.target $combo_status.side_b.target.x $combo_status.side_b.target.y}

            [then]
                {COMBO_DBG "side a and side b match, running controller"}

                {EVENT_FORWARD_DISPATCH "combo controller"}
            [/then]
        [/if]

        {CLEAR_VARIABLE combo_side}
    [/event]

    [event]
        name=combo controller
        first_time_only=no

        # Pseudo-references to keep code tidy.

        {VARIABLE REF_side_a combo_status.side_a}
        {VARIABLE REF_side_b combo_status.side_b}

        {BUG_ON (
            [not]
                {VARIABLE_POS_EQUALS $REF_side_a|.target $x2 $y2}
                {VARIABLE_POS_EQUALS $REF_side_b|.target $x2 $y2}
            [/not]
        ) ("Combo controller invoked with bad target")}

        [foreach]
            array=combo_attacks
            variable=combo
            index_var=i
            [do]
                {BUG_ON ({VARIABLE_NUMERICAL_NOT_EQUALS combo.effect.length 1}) ("Combo effect count must be exactly 1 ($rc_combo.effect.length)")}

                # Check if the current fight matches the configured combo
                #
                # a, b = current fight's side A and side B
                # A, B = configured combo's side A and side B

                [if]
                    # Condition 1: symmetric combos (a == A || a == B || b == A || b == B)
                    [not]
                        {VARIABLE_BOOLEAN_EQUALS combo.symmetric yes}
                        {VARIABLE_LEXICAL_IN combo.side_a.attack_id $$REF_side_a|.attack|,$$REF_side_b|.attack|}
                    [/not]

                    # Condition 2: asymmetric combos (a == A && b == B)
                    [not]
                        {VARIABLE_BOOLEAN_EQUALS combo.symmetric no}
                        {VARIABLE_LEXICAL_EQUALS combo.side_a.attack_id $$REF_side_a|.attack}
                        {VARIABLE_LEXICAL_EQUALS combo.side_b.attack_id $$REF_side_b|.attack}
                    [/not]

                    [then]
                        [continue][/continue]
                    [/then]
                [/if]

                # Combo confirmed

                {COMBO_DBG "selected combo [$i]: [ side A = $$REF_side_a|.attack; side B = $$REF_side_b|.attack ]"}

                # Find out the subscript for the {attack] entry we're modifying

                {VARIABLE n -1}

                [for]
                    array=unit.attack
                    variable=j
                    [do]
                        [if]
                            {VARIABLE_LEXICAL_EQUALS unit.attack[$j].name $$REF_side_b|.attack}
                            [then]
                                {VARIABLE n $j}
                                [break][/break]
                            [/then]
                        [/if]
                    [/do]
                [/for]

                [if]
                    {VARIABLE_NUMERICAL_EQUALS n -1}
                    [then]
                        {BUG ("Attack sequence using a nonexistent [attack]")}
                        [break][/break]
                    [/then]
                [/if]

                [if]
                    {VARIABLE_LEXICAL_NOT_EQUALS combo.effect.apply_to damage}
                    [then]
                        {BUG ("Combo effect not implemented yet: $combo.effect.apply_to")}
                        [continue][/continue]
                    [/then]
                [/if]

                {COMBO_DBG "applying combo effects to attack[$n] '$weapon.name' from $unit.id"}

                # This can only be the multiply effect at the moment, which
                # translates directly into a [damage] multiply= weapon special.

                # Damage effects are temporarily appended at the end of the
                # [attack][specials][damage] array. Structuring this using WML
                # variable interpolation is a little awkward but it works.
                {VARIABLE REF_atk_damage_ary "unit.attack[$n].specials.damage"}

                #    REF_atk_combo_effect = "unit.attack[0].specials.damage[$unit.attack[0].specials.damage.length]"
                # -> REF_atk_combo_effect = "unit.attack[0].specials.damage[9999]"
                {VARIABLE REF_atk_combo_effect "$REF_atk_damage_ary|[$$REF_atk_damage_ary|.length]"}

                # I feel so sorry for the sanity of whoever has to read this code in the future. <3
                # (Except not really.)

                # This is getting meta. We're generating a new [damage] special
                # by copying the attributes directly from the combo's [effect]
                # WML node, and the target is an interpolated variable
                # reference that contained a nested interpolated reference.
                [set_variables]
                    name=$REF_atk_combo_effect
                    mode=insert

                    [insert_tag]
                        name=value
                        variable=combo.effect
                    [/insert_tag]
                [/set_variables]

                # Also need to add some fluff to have the new [damage] sepecial
                # work as intended.
                [set_variables]
                    name=$REF_atk_combo_effect
                    mode=merge
                    [value]
                        id=combo_damage_effect # wmllint: ignore
                        apply_to=self
                        active_on=offense
                    [/value]
                [/set_variables]

                # Clobber the attack unit with the result and make sure to
                # restore it to its original state at the end of the attack
                # sequence.

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

                # This event is not guaranteed to execute if the RNG causes the
                # unit to miss all strikes, so remove it later.

                [event]
                    id=combo_hits_handler # wmllint: ignore
                    name=attacker hits
                    delayed_variable_substitution=no
                    first_time_only=yes

                    {UNIT_SPELL_POPUP x,y=$|x1,$|y1 $rc_combo.name}
                [/event]

                # Clean-up sequence.

                [event]
                    name=attack end
                    delayed_variable_substitution=no
                    first_time_only=yes

                    {COMBO_DBG "removing combo effects"}

                    {CLEAR_VARIABLE $REF_atk_combo_effect}

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

                    [fire_event]
                        name=reset combo status
                    [/fire_event]

                    [remove_event]
                        id=combo_hits_handler # wmllint: ignore
                    [/remove_event]
                [/event]

                # We're done here.

                {CLEAR_VARIABLE REF_atk_combo_effect,REF_atk_damage_ary,n}

                [break][/break]
            [/do]
        [/foreach]

        {CLEAR_VARIABLE REF_side_a,REF_side_b}
    [/event]

    [event]
        name=side 1 turn end
        first_time_only=no

        {COMBO_DBG "resetting state on side 1 turn end"}

        [fire_event]
            name=reset combo status
        [/fire_event]
    [/event]

#undef COMBO_EFFECT:DMG_MULTIPLY
#undef COMBO_DEFINITION
#undef COMBO_SIGIL:EARTH
#undef COMBO_SIGIL:DARKNESS
#undef COMBO_SIGIL
#undef COMBO_DBG

    [event]
        name=moveto
        [filter]
            x,y=31,42
            side=1
        [/filter]

        [allow_undo][/allow_undo]

        [message]
            speaker=Elynia
            message= _ "Is that..."
        [/message]

        [message]
            speaker=Elyssa
            message= _ "Xia’el, the Guardian of Earth. Her First Cycle self, to be precise. Don’t feel too intimidated by her stature, though — I very much doubt this is to scale."
        [/message]

        [message]
            speaker=Elynia
            message= _ "She is... she was a faerie? I am not sure what I was expecting."
        [/message]

        [message]
            speaker=Elyssa
            message= _ "Big shoes to fill? Then again, it looks like neither of you have ever worn shoes for reasons that are surely beyond my ability to comprehend. And again, most likely not to scale."
        [/message]

        [message]
            speaker=Elynia
            message= _ "No, I mean... She looks so beautiful and dignified... but at the same time, she is... so..."
        [/message]

        [message]
            speaker=Elyssa
            message= _ "Mundane? By now you should know gods come in all shapes and sizes. Although anyone can play god to a sufficiently ignorant crowd."
        [/message]

        [message]
            speaker=Elynia
            message= _ "I suppose you would know about that kind of thing."
        [/message]

        [message]
            speaker=Elyssa
            message= _ "You don’t need miracles, prophecies or mind control to rule over uncivilized brutes. You just need to establish clear boundaries and show your subjects the consequences for stepping out of line. Sooner or later someone will test your power regardless, so there is little point to claiming godhood unless you want to paint an even larger target on your back."
        [/message]

        [message]
            speaker=Elyssa
            message= _ "Ignoring the entire Uria situation for a moment, what this world needs right now is not another false god. It just needs someone able to purge all the scum and build a new order from the ground up. If that someone happens to have god-like powers, then the job should be certainly much easier. But it does not have to be like that."
        [/message]

        [event]
            name=new turn

            [message]
                speaker=Elyssa
                message= _ "You don’t need to play the role of the Guardian of Earth if that’s not what you want."
            [/message]

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

            [message]
                speaker=Elyssa
                message= _ "I can feel the question lingering in the back of your head."
            [/message]

            [delay]
                time=500
            [/delay]

            [message]
                speaker=Elynia
                message= _ "When my mentor realized the true nature of the Sceptre of Fire, she asked me to wield it against the enemies of Irdya... with her dying breath."
            [/message]

            [message]
                speaker=Elyssa
                message= _ "This cursed artifact is bad for your health. You must not use it ever again."
            [/message]

            [message]
                speaker=Elynia
                message= _ "I just said—"
            [/message]

            [message]
                speaker=Elyssa
                message= _ "You can say anything you want, but it does nothing to change the fact that you are a creature of Light trying to wield a power that is harmful to your mind and soul."
            [/message]

            [message]
                speaker=Elynia
                message= _ "Niryone’s staff rightfully belongs to me—"
            [/message]

            [message]
                speaker=Elyssa
                message= _ "I assure you, the <i>thing</i> lurking beneath the Ruby’s lustrous surface does not care for any sentimental attachment you may have to it. Your mentor’s intentions may have been noble, but if you want to save Irdya you are going to have to learn to let go of the past and not waste your potential living an eternal life of regret."
            [/message]

            [message]
                speaker=Elyssa
                message= _ "If you truly want to honor her memory... if you want to make your friends’ sacrifices worth it, then it is time for you to become the Guardian of Earth we need instead of the Lady of Light who lost to her own Darkness. But if you don’t feel up to the task then you are free to stay behind, drowning in an endless ocean of sorrow for what little is left of eternity."
            [/message]
        [/event]
    [/event]

    ############################################################################
    #                                                                          #
    #                            REACHING BOSS AREA                            #
    #                                                                          #
    ############################################################################

    [event]
        name=moveto
        [filter]
            side=1
            [filter_location]
                x,y=28,20
                radius=6
            [/filter_location]
        [/filter]

        [fire_event]
            name=reveal keep
        [/fire_event]
    [/event]

#define FINALE_B_ZHANGORS_KEEP_SLF
    x,y=28,20
    radius=1
#enddef

    [event]
        name=reveal keep

        {LOCK_VIEW}

        [remove_shroud]
            side=1
            x,y=28,20
            radius=7
        [/remove_shroud]

        [remove_terrain_overlays]
            x,y=28,20
            radius=1
            [and]
                terrain=C*^*
            [/and]
        [/remove_terrain_overlays]

        [terrain]
            x,y=28,20
            terrain=^Kov
            layer=overlay
        [/terrain]

        {CLEAR_FOG 1 28 20 8}

        [redraw]
            side=1
        [/redraw]

        [scroll_to]
            x,y=28,20
        [/scroll_to]

        {HIGHLIGHT_GOAL (x,y=28,20)}

        {UNCLEAR_FOG}

        [message]
            speaker=Elyssa
            message= _ "Hm. He is not here like I expected. I suppose his intention is to make some kind of dramatic entrance later."
        [/message]

        [message]
            speaker=Elyssa
            message= _ "Elynia, there is something here you should see."
        [/message]

        {OBJECTIVES (
            victory_string= _ "Current Objective:"
            {OBJECTIVE_VICTORY ( _ "Move Elynia and Elyssa to (28, 20) and the immediately adjacent locations")}

            {OBJECTIVE_DEFEAT  ( _ "Death of Elyssa")}
            {OBJECTIVE_DEFEAT  ( _ "Death of Elynia")}

            {OBJECTIVE_NO_CARRYOVER}

            {FIRE_GUARDIAN_SUMMONING_NOTE}
        )}

        {UNLOCK_VIEW}

        [event]
            id=ee_moveto_handler # wmllint: ignore
            name=moveto
            first_time_only=no
            [filter]
                [filter_location]
                    {FINALE_B_ZHANGORS_KEEP_SLF}
                [/filter_location]
                [and]
                    id=Elynia
                    [or]
                        id=Elyssa
                    [/or]
                [/and]
            [/filter]

            [if]
                {VARIABLE_LEXICAL_EQUALS unit.id Elynia}
                [have_unit]
                    id=Elyssa
                    [filter_location]
                        {FINALE_B_ZHANGORS_KEEP_SLF}
                    [/filter_location]
                [/have_unit]
                [or]
                    {VARIABLE_LEXICAL_EQUALS unit.id Elyssa}
                    [have_unit]
                        id=Elynia
                        [filter_location]
                            {FINALE_B_ZHANGORS_KEEP_SLF}
                        [/filter_location]
                    [/have_unit]
                [/or]

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

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

#define FINALE_B_SET_BOSS_STAGE _NUMBER
    {VARIABLE boss_stage {_NUMBER}}
    # Used to stagger periodic respawns so that they don't trigger too soon
    # after transitioning into a new boss fight phase.
    {VARIABLE boss_skip_spawns_on_turn $turn_number}
#enddef

    [event]
        name=boss stage 1

        [remove_event]
            id=ee_moveto_handler # wmllint: ignore
        [/remove_event]

        {BUG_ON (
            [not]
                [have_unit]
                    id=Elynia
                    [filter_location]
                        {FINALE_B_ZHANGORS_KEEP_SLF}
                    [/filter_location]
                [/have_unit]
                [have_unit]
                    id=Elyssa
                    [filter_location]
                        {FINALE_B_ZHANGORS_KEEP_SLF}
                    [/filter_location]
                [/have_unit]
            [/not]
        ) ("stage 1 fired early")}

        {FINALE_B_SET_BOSS_STAGE 1}

        {VARIABLE boss_start_turn $turn_number}

        {LOCK_VIEW}

        {CLEAR_LABELS}

        {RESET_UNIT_STATUSES id=Elyssa,Elynia}

        [kill]
            [filter_location]
                x,y=28,20
                radius=7
            [/filter_location]
            [not]
                id=Elynia
                [or]
                    id=Elyssa
                [/or]
            [/not]
        [/kill]

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

        [remove_shroud]
            side=1
            x=0-55
            y=0-39
        [/remove_shroud]

        [place_shroud]
            side=1
            x= 0-55
            y=40-69
        [/place_shroud]

        [redraw]
            side=1
        [/redraw]

        [fade_out_music][/fade_out_music]

        [fade_out_sound_effects][/fade_out_sound_effects]

        {REMOVE_SOUND_SOURCE cave_noise}

        [reset_sound_effects][/reset_sound_effects]

        {REPLACE_SCENARIO_MUSIC "transience.ogg"}

        [delay]
            time=1000
        [/delay]

        [message]
            speaker=Elynia
            message= _ "What is that in the middle of the blood lake?"
        [/message]

        [scroll_to]
            x,y=28,4
        [/scroll_to]

        [delay]
            time=750
        [/delay]

        [message]
            speaker=Elyssa
            scroll=no
            message= _ "The Seed of Earth, the place from which all life on Irdya originally sprang."
        [/message]

        [message]
            speaker=Elyssa
            scroll=no
            message= _ "It is currently sealed in its chamber. There is not much that can be done with it by anyone other than the Guardian of Earth. Generally speaking, nobody is supposed to see or tamper with the thing that lays within; not even the controlling Guardian herself. That is to say, not even you."
        [/message]

        [message]
            speaker=Elyssa
            scroll=no
            message= _ "... Unless, of course, you wanted to reset life on Irdya back to the beginning. That is how the Second Cycle was started. But if you did that, then every living or non-living creature currently standing on this world would be erased from existence — even you."
        [/message]

        [message]
            speaker=Elyssa
            message= _ "Doing that would destroy Zhangor for sure, but Uria is not on Irdya, and the loss would be little more than a minor inconvenience to her in the long term. But if you feel particularly selfish about your pitiful existence, this is an option for you to consider."
        [/message]

        [delay]
            time=750
        [/delay]

        [message]
            speaker=Elynia
            message= _ "That would be a coward’s choice, don’t you think?"
        [/message]

        [message]
            speaker=Elyssa
            message= _ "Possibly. But if the guardian in question feared the absolute end of their own existence, then one could say that making the choice would take some degree of courage."
        [/message]

        [delay]
            time=750
        [/delay]

        [message]
            speaker=Elyssa
            message= _ "What is your choice?"
        [/message]

        [delay]
            time=750
        [/delay]

        [message]
            speaker=Elyssa
            message= _ "Do not worry, it is not a binding decision, yet."
        [/message]

        [message]
            speaker=Elynia
            message= _ "Is that the only possible use for the power stored in this place? To destroy all life in our world so it can start anew?"
        [/message]

        [message]
            speaker=Elyssa
            message= _ "If Merthiaal’s cautionary story is any indication, yes. But Uria believes otherwise. She believes that the seeds can be used for other things if the aspect you control is either Life or Existence, based on the efforts of her predecessor — or the very unreliable information we have about them, anyway. But her attempts to find the seed on Urvatha have proven fruitless so far, and her patience is wearing thin. As ludicrous as it sounds, she no longer wants to find <i>just</i> the Body of the Union on Ethea."
        [/message]

        [message]
            speaker=Elynia
            message= _ "I see. Then, there is no point in doing anything with ours."
        [/message]

        [message]
            speaker=Elyssa
            message= _ "I suppose. There is so much we still don’t know about our own creation..."
        [/message]

        [message]
            speaker=Elyssa
            message= _ "Come to think of it, we should be thankful that Delethia’s actions ensured we would not get a proper Guardian of Existence for this cycle who could turn out to be an even more powerful deranged psychopath than Uria. Well, probably. That girl, Anya... her sole existence raises many questions we thought we had already answered."
        [/message]

        [message]
            speaker=Elyssa
            sound=laugh.ogg
            message= _ "...!"
        [/message]

        [fade_out_music]
            duration=500
        [/fade_out_music]

        {REPLACE_SCENARIO_MUSIC "siege_of_laurelmor.ogg"}

        [mute_sound_effects][/mute_sound_effects]

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

        [fade_in_sound_effects]
            duration=750
        [/fade_in_sound_effects]

        [message]
            speaker=Elyssa
            message= _ "... But of course. The bastard’s plans aren’t as aligned with Uria’s own as I originally thought."
        [/message]

        [store_unit]
            [filter]
                id=Elyssa
            [/filter]
            variable=elyssa_store
            kill=yes
        [/store_unit]

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

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

        [hide_unit][/hide_unit]

        {RED_SCREEN}

        {VARIABLE_ADD elyssa_store.x  4}
        {VARIABLE elyssa_store.facing se}
        {VARIABLE_MIN elynia_store.x  4}
        {VARIABLE elynia_store.facing sw}

        [hidden_unit]
            {CHARACTER_STATS_ELYNIA}
            type=Guardian of Earth Elynia
            canrecruit=no
            facing=$elynia_store.facing
            side=1
            x,y=$elynia_store.x,$elynia_store.y
            max_hitpoints=1
            variation=injured
            upkeep=free
        [/hidden_unit]

        [hidden_unit]
            {CHARACTER_STATS_ELYSSA}
            type=Guardian of Darkness Elyssa
            canrecruit=no
            facing=$elyssa_store.facing
            side=1
            x,y=$elyssa_store.x,$elyssa_store.y
            max_hitpoints=1
            variation=injured
            upkeep=free
        [/hidden_unit]

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

        [delay]
            time=100
        [/delay]

        {RESET_SCREEN}

        [unhide_unit][/unhide_unit]

        [redraw]
            side=1
        [/redraw]

        [delay]
            time=100
        [/delay]

        [kill]
            x,y=28,20
            [not]
                side=1
            [/not]
        [/kill]

        [unit]
            canrecruit=yes
            animate=yes
            side=2
            id=Zhangor
            name= _ "Zhangor"
            type=Angel of Blood
            {IS_BOSS}
            x,y=28,20
            facing=se
            [modifications]
                {TRAIT_BIOMECHANICAL}
                {TRAIT_DEXTROUS}
            [/modifications]
        [/unit]

        [scroll_to_unit]
            id=Zhangor
        [/scroll_to_unit]

        {BOSS_POPUP}

        #{REPLACE_SCENARIO_MUSIC "the_dangerous_symphony.ogg"}

        [delay]
            time=1000
        [/delay]

        [message]
            speaker=Zhangor
            message= _ "What a glorious day for Irdya! So, at long last, Argan’s little girl solved the puzzle!"
        [/message]

        [kill]
            id=Elyssa
        [/kill]

        [unstore_unit]
            variable=elyssa_store
            find_vacant=yes
            check_passability=no
        [/unstore_unit]

        [redraw][/redraw]

        [delay]
            time=500
        [/delay]

        {FACE_DIRECTION id=Elyssa nw}

        [redraw][/redraw]

        [delay]
            time=250
        [/delay]

        [message]
            speaker=Elyssa
            message= _ "You are a disgusting liar and a traitor, Zhangor."
        [/message]

        [kill]
            id=Elynia
        [/kill]

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

        [redraw][/redraw]

        [delay]
            time=750
        [/delay]

        {FACE_DIRECTION id=Elynia ne}

        [redraw][/redraw]

        [delay]
            time=250
        [/delay]

        [message]
            speaker=Zhangor
            message= _ "Excellent. That makes us equals in the eyes of the Lady of Light. Or should I now call her the Guardian of Earth on Irdya?"
        [/message]

        {FACE_DIRECTION id=Zhangor sw}

        [redraw][/redraw]

        [message]
            speaker=Zhangor
            message= _ "Ah yes, Elynia... I have not forgotten what you and Argan did to me. Do you have any idea what it feels like to live for hundreds of years in agonizing pain with your limbs scattered across the land and buried deep in the ground? Do you?"
        [/message]

        [message]
            speaker=Elynia
            message= _ "You’ve got nothing to fear; this time I shall make sure <i>nothing</i> remains of your wretched body for Uria to bring back."
        [/message]

        [message]
            speaker=Elyssa
            message= _ "And I shall make sure your soul is destroyed forever as well."
        [/message]

        [message]
            speaker=Zhangor
            message= _ "Without the Union? Go ahead, try your best!"
        [/message]

        {REPLACE_SCENARIO_MUSIC "gathering_storm.ogg"}

        [sound]
            name="dragonstick.ogg"
        [/sound]

        [kill]
            x=29-33
            y=43-45
        [/kill]

        [terrain]
            x=29-33
            y=43-45
            terrain=Xos
        [/terrain]

        [sound]
            name="dragonstick.ogg"
        [/sound]

        [terrain_mask]
            x,y=1,1
            {MASK 10_Blood_1.mask}
            border=yes
        [/terrain_mask]

        [redraw][/redraw]

        {QUAKE (explosion-big.ogg)}

        [delay]
            time=250
        [/delay]

        {OBJECTIVES (
            {OBJECTIVE_VICTORY ( _ "Vanquish Demon Lord Zhangor")}

            {OBJECTIVE_DEFEAT  ( _ "Death of Elyssa")}
            {OBJECTIVE_DEFEAT  ( _ "Death of Elynia")}

            {OBJECTIVE_NO_CARRYOVER}

            {OBJECTIVE_NOTE ( _ "Zhangor’s Gatekeeper powers allow him to debilitate and destroy the Energy Heart of a Guardian")}
            {FIRE_GUARDIAN_SUMMONING_NOTE}
        )}

        {UNLOCK_VIEW}
    [/event]

    ############################################################################
    #                                                                          #
    #                       SHARED CODE FOR ALL BOSS STAGES                    #
    #                                                                          #
    ############################################################################

    #
    # See 'boss spawns controller' and the associated trigger event handler for
    # stuff specific to boss phases 2 through 4.
    #

    [event]
        name=side 2 turn
        first_time_only=no
        [filter_condition]
            {VARIABLE_NUMERICAL_NOT_EQUALS boss_stage 0}
        [/filter_condition]

        {BUG_ON ({VARIABLE_NUMERICAL_LESS_THAN boss_start_turn 1}) ("boss_start_turn < 1")}

        #
        # Let him recruit a castleful of units every n turns,
        # depending on the current difficulty setting.
        #

        {VARIABLE boss_recruitment_rate {DIFF 4 3 2} }

        # First see if we just started the first boss turn.
        {VARIABLE boss_recruitment_factor "$($turn_number - $boss_start_turn)"}

        [if]
            {VARIABLE_NUMERICAL_NOT_EQUALS boss_recruitment_factor 0}
            [then]
                {VARIABLE_MOD boss_recruitment_factor $boss_recruitment_rate}
            [/then]
        [/if]

        [if]
            {VARIABLE_NUMERICAL_EQUALS boss_recruitment_factor 0}
            [then]
                [fire_event]
                    name=boss recruitment controller
                [/fire_event]
            [/then]
        [/if]

        {CLEAR_VARIABLE boss_recruitment_rate,boss_recruitment_factor}
    [/event]

    [event]
        name=boss recruitment controller
        first_time_only=no

        {BUG_ON ({VARIABLE_NUMERICAL_NOT_EQUALS side_number 2}) ("boss recruitment controller called when side_number != 2")}

        [if]
            {VARIABLE_NUMERICAL_LESS_THAN boss_stage 3}
            [then]
                {VARIABLE side_2_recruits "Psy Crawler,Shaxthal Drone"}
            [/then]
            [else]
                {VARIABLE side_2_recruits "Psy Crawler,Shaxthal Drone,Shaxthal Rayblade,Ghost"}
            [/else]
        [/if]

        [modify_side]
            side=2
            recruit=$side_2_recruits
            gold={DIFF 60 70 80}
        [/modify_side]

        [event]
            name=side 2 turn end
            delayed_variable_substitution=no

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

            {DISALLOW_RECRUIT 2 $side_2_recruits}
        [/event]

        {CLEAR_VARIABLE side_2_recruits}
    [/event]

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

    [event]
        name=last breath
        [filter]
            id=Zhangor
        [/filter]
        [filter_condition]
            {VARIABLE_NUMERICAL_EQUALS boss_stage 1}
        [/filter_condition]

        [fire_event]
            name=boss stage 2
            [primary_unit]
                id=Zhangor
            [/primary_unit]
        [/fire_event]
    [/event]

#define FINALE_B_SET_SPAWN_LIMIT _1ST_RUN_VAR _SPAWN_LIMIT
#arg INITIAL_LIMIT
999999 #endarg
    {VARIABLE_IF_ELSE spawn_limit {INITIAL_LIMIT} {_SPAWN_LIMIT} (
        {VARIABLE_BOOLEAN_EQUALS {_1ST_RUN_VAR} yes}
    )}

    {VARIABLE {_1ST_RUN_VAR} no}
#enddef

#define FINALE_B_STOP_AT_SPAWN_LIMIT
#arg VAR
i #endarg
    [if]
        {VARIABLE_NUMERICAL_GREATER_THAN_OR_EQUAL {VAR} $spawn_limit}
        [then]
            [break][/break]
        [/then]
    [/if]
#enddef

#define FINALE_B_CLEAR_SPAWN_LIMIT
    {CLEAR_VARIABLE spawn_limit}
#enddef

    [event]
        name=boss stage 2

        {FINALE_B_SET_BOSS_STAGE 2}

        {VARIABLE ofacing $unit.facing}

        {LOCK_VIEW}

        {RESET_UNIT_STATUSES id=Elyssa,Elynia}

        [scroll_to_unit]
            id=Zhangor
        [/scroll_to_unit]

        [fade_out_music]
            duration=500
        [/fade_out_music]

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

        {PLAY_LEVELOUT_ANIM id=Zhangor}

        [kill]
            id=Zhangor
        [/kill]

        [hide_unit][/hide_unit]

        {RED_SCREEN}

        #
        # Try to get any outstanding actions out of the queue now to mitigate
        # [delay] timing issues below.
        #

        [redraw]
            side=1
        [/redraw]

        # Zhangor in his Gatekeeper form cannot move, therefore we need to
        # place him on his keep, pushing away any units that might've been
        # already there.

        [if]
            [have_unit]
                x,y=$boss_keep.x,$boss_keep.y
            [/have_unit]
            [then]
                [store_unit]
                    [filter]
                        x,y=$boss_keep.x,$boss_keep.y
                    [/filter]
                    kill=yes
                    variable=zhangors_keep_unit
                [/store_unit]
            [/then]
        [/if]

        [hidden_unit]
            canrecruit=yes
            side=2
            id=Zhangor
            name= _ "Zhangor"
            type=Angel of Blood
            {IS_BOSS}
            variation=gatekeeper_phase2
            x,y=$boss_keep.x,$boss_keep.y
            facing=$ofacing
            [modifications]
                {TRAIT_UNDEAD}
                {TRAIT_DEXTROUS}
            [/modifications]
        [/hidden_unit]

        [if]
            {VARIABLE_NUMERICAL_NOT_EQUALS zhangors_keep_unit.length 0}
            [then]
                [unstore_unit]
                    variable=zhangors_keep_unit
                    find_vacant=yes
                [/unstore_unit]
            [/then]
        [/if]

        {CLEAR_VARIABLE ofacing,zhangors_keep_unit}

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

        [delay]
            # Approx. the length of gun-activate-2.ogg (actually around 920 ms,
            # but [delay] is too inaccurate due to the calls to the display drawing
            # code involved.
            time=900
        [/delay]

        {RESET_SCREEN}

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

        {REPLACE_SCENARIO_MUSIC "ambuscade-loop.ogg"}
        {INCIDENTAL_MUSIC       "moleman.ogg"}

        [unhide_unit][/unhide_unit]

        {PLAY_LEVELIN_ANIM id=Zhangor}

        [redraw]
            side=1
        [/redraw]

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

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

        [foreach]
            array=core_locs
            variable=core_loc
            [do]
                [store_direction]
                    from_x,from_y=$core_loc.x,$core_loc.y
                    to_x,to_y=$boss_keep.x,$boss_keep.y
                [/store_direction]

                [terrain]
                    terrain=Kte^Xo
                    x,y=$core_loc.x,$core_loc.y
                [/terrain]

                [hidden_unit]
                    side=2
                    type=Blood Core
                    x,y=$core_loc.x,$core_loc.y
                    facing=$direction
                    random_traits=no
                    generate_name=no
                    {IS_HERO}
                    [variables]
                        is_zhangors_body=yes
                    [/variables]
                    [modifications]
                        [object]
                            silent=yes
                            [effect]
                                apply_to=loyal
                            [/effect]
                        [/object]
                    [/modifications]
                [/hidden_unit]

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

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

        [unhide_unit][/unhide_unit]

        [redraw][/redraw]

        {QUAKE ()}

        [message]
            speaker=Elynia
            message= _ "What are you trying to do now?"
        [/message]

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

        [unhide_unit][/unhide_unit]

        [redraw][/redraw]

        [delay]
            time=750
        [/delay]

        [message]
            speaker=Elyssa
            message= _ "His body is affixed to the ground, just avoid getting hit by his lightning!"
        [/message]

        {UNLOCK_VIEW}

        #
        # Any time all Blood Cores are destroyed, every attack from the
        # guardians has 100% chance to hit Zhangor.
        #
        # Mobile Blood Cores begin to be periodically spawned in later phases
        # to compensate.
        #

        {FORCE_CHANCE_TO_HIT (
            id=Elynia
            [or]
                id=Elyssa
            [/or]
        ) (id=Zhangor) 100 (
            [not]
                [have_unit]
                    type=Blood Core
                [/have_unit]
            [/not]
        )}
    [/event]

    #
    # The banshee spawn logic is used throughout phases 2, 3 and 4.
    #

    [event]
        name=banshee spawn controller
        first_time_only=no

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

        {FINALE_B_SET_SPAWN_LIMIT banshee_spawn_1st_run {DIFF 2 4 4}}

        [foreach]
            array=banshee_locs
            variable=banshee_loc
            [do]
                {FINALE_B_STOP_AT_SPAWN_LIMIT}

                [store_direction]
                    from_x,from_y=$banshee_loc.x,$banshee_loc.y
                    to_x,to_y=$boss_keep.x,$boss_keep.y
                [/store_direction]

                [remove_terrain_overlays]
                    x,y=$banshee_loc.x,$banshee_loc.y
                [/remove_terrain_overlays]

                [hidden_unit]
                    side=2
                    type=Fallen Faerie
                    random_gender=yes
                    random_traits=yes
                    generate_name=yes
                    x,y=$banshee_loc.x,$banshee_loc.y
                    facing=$direction
                    [modifications]
                        [object]
                            silent=yes
                            [effect]
                                apply_to=loyal
                            [/effect]
                        [/object]
                    [/modifications]
                [/hidden_unit]

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

        [unhide_unit][/unhide_unit]

        {FINALE_B_CLEAR_SPAWN_LIMIT}
    [/event]

    [event]
        name=attacker hits
        first_time_only=no
        [filter]
            side=1
        [/filter]
        [filter_second]
            type=Blood Core
        [/filter_second]

        {EVENT_FORWARD_DISPATCH_SIMPLE "player hits blood core"}
    [/event]

    [event]
        name=defender hits
        first_time_only=no
        [filter_second]
            side=1
        [/filter_second]
        [filter]
            type=Blood Core
        [/filter]

        {EVENT_INVERSE_DISPATCH_SIMPLE "player hits blood core"}
    [/event]

    [event]
        name=player hits blood core
        first_time_only=yes

        [message]
            # Try speaking as the primary unit if she's Elynia or Elyssa, or Elyssa
            # otherwise.
            x,y=$x1,$y1
            [and]
                id=Elynia
                [or]
                    id=Elyssa
                [/or]
            [/and]
            [or]
                id=Elyssa
            [/or]
            message= _ "What the—"
        [/message]
    [/event]

    [event]
        name=player hits blood core
        first_time_only=no

        #
        # PRECONDITIONS:
        # * primary unit is side = 1
        # * secondary unit is side = 2
        #

        {BUG_ON (
            [not]
                {VARIABLE_NUMERICAL_EQUALS unit.side 1}
                {VARIABLE_NUMERICAL_EQUALS second_unit.side 2}
            [/not]
        ) ("Invalid Blood Core fight sequence")}

        {BUG_ON ({VARIABLE_NUMERICAL_LESS_THAN boss_stage 2}) ("Blood Core handler running before boss stage 2 ($boss_stage|)")}

        #
        # Shuffle Blood Cores' HP bars around whenever possible.
        #

        [store_unit]
            [filter]
                type=Blood Core
                [filter_location]
                    find_in=core_locs
                [/filter_location]
            [/filter]
            variable=shuffle_stack
        [/store_unit]

        #
        # This shuffling algorithm should work in every case regardless of the amount
        # of units stored. If there's a single unit stored, it is unstored back with
        # minimal overhead later (*). If there are no units stored, the loop never
        # runs, and the bugcheck path is not hit.
        #

        [for]
            array=shuffle_stack
            variable=k
            reverse=yes
            [do]
                # k is the position of the primary unit to swap, set by the foreach cycle.
                # r is the position of teh secondary unit to swap, determined by the RNG.
                {VARIABLE r -1}

                [if]
                    {VARIABLE_NUMERICAL_GREATER_THAN k 0}
                    [then]
                        {VARIABLE_RANDOM r "0..$($k - 1)"}

                        {LOG_ATS_DBG ("SWAP: $k <-> $r <$shuffle_stack.length>")}

                        {VARIABLE swap.hitpoints     $shuffle_stack[$r].hitpoints}
                        {VARIABLE swap.max_hitpoints $shuffle_stack[$r].max_hitpoints}

                        {VARIABLE shuffle_stack[$r].hitpoints     $shuffle_stack[$k].hitpoints}
                        {VARIABLE shuffle_stack[$r].max_hitpoints $shuffle_stack[$k].max_hitpoints}

                        {VARIABLE shuffle_stack[$k].hitpoints     $swap.hitpoints}
                        {VARIABLE shuffle_stack[$k].max_hitpoints $swap.max_hitpoints}

                        [unstore_unit]
                            variable="shuffle_stack[$r]"
                            find_vacant=no
                            check_passability=no
                        [/unstore_unit]

                        {CLEAR_VARIABLE swap}
                    [/then]
                [/if]

                # (*)

                [unstore_unit]
                    variable="shuffle_stack[$k]"
                    find_vacant=no
                    check_passability=no
                [/unstore_unit]

                [if]
                    {VARIABLE_NUMERICAL_GREATER_THAN r -1}
                    [then]
                        [floating_text]
                            x=$shuffle_stack[$k].x,$shuffle_stack[$r].x
                            y=$shuffle_stack[$k].y,$shuffle_stack[$r].y
                            text="<span color='#FF7F00'>!</span>" # wmllint: ignore
                        [/floating_text]

                        {CLEAR_VARIABLE shuffle_stack[$r]}

                        {VARIABLE_DEC k}
                    [/then]
                [/if]

                {CLEAR_VARIABLE r,shuffle_stack[$k]}
            [/do]
        [/for]

        # We shouldn't hit this unless the shuffling algorithm is broken.
        {BUG_ON ({VARIABLE_NUMERICAL_GREATER_THAN shuffle_stack.length 0}) ("Unit stats shuffling algorithm postcondition broken")}

        # FIXME: because I don't want to see what happens if we fire death events in the
        #        middle of the sequence above, we need to check now what Blood Cores are
        #        left with 0 HP or less. The game does not automatically kill them when
        #        this happens unless it's the actual target unit.

        [store_unit]
            [filter]
                type=Blood Core
                [filter_location]
                    find_in=core_locs
                [/filter_location]
            [/filter]
            variable=shuffle_stack
            kill=no
        [/store_unit]

        [foreach]
            array=shuffle_stack
            variable=shuffled_core
            [do]
                [if]
                    {VARIABLE_NUMERICAL_LESS_THAN shuffled_core.hitpoints 1}
                    [not]
                        # If it's the event's target it will be killed when
                        # returning to the engine anyway.
                        {VARIABLE_LEXICAL_EQUALS second_unit.id shuffled_core.id}
                        #{VARIABLE_NUMERICAL_EQUALS x2 shuffled_core.x}
                        #{VARIABLE_NUMERICAL_EQUALS y2 shuffled_core.y}
                    [/not]
                    [then]
                        [kill]
                            x,y=$shuffled_core.x,$shuffled_core.y
                            fire_event=yes
                            animate=yes
                        [/kill]
                    [/then]
                [/if]
            [/do]
        [/foreach]

        {CLEAR_VARIABLE shuffle_stack}
    [/event]

    [event]
        name=player hits blood core
        first_time_only=yes

        [message]
            speaker=Elyssa
            message= _ "Oh, is that what we are doing now, Zhangor? I expected you to be more creative than this in a real fight."
        [/message]
    [/event]

    [event]
        name=die
        first_time_only=no
        [filter]
            type=Blood Core
        [/filter]

        #
        # Boost ALL guardians HP. This includes Zhangor unless he's
        # already on stage 4 or 5.
        #

        {VARIABLE guardian_player_hp_boost {DIFF 60 50 40} }
        {VARIABLE guardian_enemy_hp_boost  {DIFF 40 50 60} }

        [store_unit]
            [filter]
                id=Elyssa
                [or]
                    id=Elynia
                [/or]
            [/filter]
            variable=guardians_store
            kill=no
        [/store_unit]

        [sound]
            name="heal.wav"
        [/sound]

        [foreach]
            array=guardians_store
            variable=guardian
            [do]
                # Remove poison only
                [clear_unit_status]
                    [filter]
                        id=$guardian.id
                    [/filter]
                    status=poisoned
                [/clear_unit_status]

                [floating_text]
                    [filter]
                        id=$guardian.id
                    [/filter]
                    text="+$guardian_enemy_hp_boost" # wmllint: ignore
                [/floating_text]

                [object]
                    silent=yes
                    [filter]
                        x,y=$guardian.x,$guardian.y
                    [/filter]
                    [effect]
                        apply_to=hitpoints
                        increase=$guardian_player_hp_boost
                        increase_total=$guardian_player_hp_boost
                    [/effect]
                [/object]
            [/do]
        [/foreach]

        {CLEAR_VARIABLE guardians_store}

        [if]
            {VARIABLE_NUMERICAL_LESS_THAN boss_stage 4}
            [then]
                [floating_text]
                    [filter]
                        id=Zhangor
                    [/filter]
                    text="+$guardian_enemy_hp_boost" # wmllint: ignore
                [/floating_text]

                [object]
                    silent=yes
                    [filter]
                        id=Zhangor
                    [/filter]
                    [effect]
                        apply_to=hitpoints
                        increase=$guardian_player_hp_boost
                        increase_total=$guardian_player_hp_boost
                    [/effect]
                [/object]
            [/then]
        [/if]

        {CLEAR_VARIABLE guardian_player_hp_boost,guardian_enemy_hp_boost}
    [/event]

    [event]
        name=die
        [filter]
            type=Blood Core
        [/filter]

        [delay]
            time=250
        [/delay]

        [message]
            speaker=Elynia
            message= _ "Destroying these things is increasing my power somehow..."
        [/message]

        [message]
            speaker=Elyssa
            message= _ "There is something about the energy released by these cores, although I have a feeling Zhangor benefits from it as well."
        [/message]
    [/event]

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

    [event]
        name=last breath
        [filter]
            id=Zhangor
        [/filter]
        [filter_condition]
            {VARIABLE_NUMERICAL_EQUALS boss_stage 2}
        [/filter_condition]

        [fire_event]
            name=boss stage 3
            [primary_unit]
                id=Zhangor
            [/primary_unit]
        [/fire_event]
    [/event]

#define FINALE_B_SHAPESHIFTER_NEEDS_TC_OVERRIDE
    [set_variables]
        name=shapeshifter_mods
        mode=insert
        [literal]
            [object]
                silent=yes
                [effect]
                    apply_to=image_mod
                    add="PAL(magenta_to_red_tc>magenta_to_gold_tc)"
                [/effect]
            [/object]
        [/literal]
    [/set_variables]
#enddef

    [event]
        name=boss stage 3

        {FINALE_B_SET_BOSS_STAGE 3}

        {VARIABLE ofacing $unit.facing}
        {VARIABLE ox      $x1}
        {VARIABLE oy      $y1}

        {LOCK_VIEW}

        {RESET_UNIT_STATUSES id=Elyssa,Elynia,Zhangor}

        [scroll_to_unit]
            id=Zhangor
        [/scroll_to_unit]

        [delay]
            time=250
        [/delay]

        {FLASH_RED (
            [sound]
                name="dragonstick.ogg"
            [/sound]

            [kill]
                id=Zhangor
            [/kill]

            [unit]
                canrecruit=yes
                side=2
                id=Zhangor
                name= _ "Zhangor"
                type=Angel of Blood
                {IS_BOSS}
                variation=gatekeeper_phase3
                x,y=$ox,$oy
                facing=$ofacing
                [modifications]
                    {TRAIT_UNDEAD}
                    {TRAIT_DEXTROUS}
                [/modifications]
            [/unit]

            {REPLACE_SCENARIO_MUSIC "frantic.ogg"}
        )}

        {CLEAR_VARIABLE ox,oy,ofacing}

        [redraw][/redraw]

        [delay]
            time=500
        [/delay]

        [sound]
            name="laugh.ogg"
        [/sound]

        [delay]
            time=500
        [/delay]

        [message]
            speaker=Elynia
            message= _ "Oh dear gods, will this ever stop?"
        [/message]

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

        [message]
            speaker=Zhangor
            message= _ "What is the matter, Guardians? Do you fear killing those of your own kin now? Have you not become accustomed to it over the course of your pathetic lives?"
        [/message]

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

        {UNLOCK_VIEW}
    [/event]

    #
    # The shapeshifter spawn logic is used throughout phases 3 and 4 equally.
    #

    [event]
        name=shapeshifter spawn controller
        first_time_only=no

        [sound]
            name=mud-glob-miss.ogg
        [/sound]

        {FINALE_B_SET_SPAWN_LIMIT shapeshifter_spawn_1st_run {DIFF 2 3 4}}

        [foreach]
            array=banshee_locs
            variable=banshee_loc
            [do]
                {FINALE_B_STOP_AT_SPAWN_LIMIT}

                {VARIABLE shapeshifter_type      "Demon Shapeshifter"}
                {VARIABLE shapeshifter_id        $null}
                {VARIABLE shapeshifter_name      $null}
                {VARIABLE shapeshifter_variation $null}
                {VARIABLE shapeshifter_ellipse   "misc/ellipse-hero"}
                {VARIABLE shapeshifter_overlays  "misc/hero-icon.png"}

                # Greater chance to spawn a generic shapeshifter rather than a
                # mimic for any named character.
                {RANDOM 99,99,99,99,99,99,0..8}

                [set_variables]
                    name=shapeshifter_mods
                    mode=replace
                    [literal]
                        [object]
                            silent=yes
                            [effect]
                                apply_to=loyal
                            [/effect]
                            [effect]
                                apply_to=image_mod
                                add="CS(16,0,16)" # Add a little purpleish tint.
                            [/effect]
                            [effect]
                                apply_to=profile
                                portrait=misc/blank-hex.png
                            [/effect]
                        [/object]
                    [/literal]
                [/set_variables]

                [switch]
                    variable=random
                    [case]
                        value=0
                        {VARIABLE shapeshifter_type "Master of Darkness"}
                        {VARIABLE shapeshifter_id   "fakeArgan"}
                        {VARIABLE shapeshifter_name _"Argan"}
                    [/case]
                    [case]
                        value=1
                        {VARIABLE shapeshifter_type "Elvish Spellbinder"}
                        {VARIABLE shapeshifter_id   "fakeNiryone"}
                        {VARIABLE shapeshifter_name _"Niryone"}
                        {FINALE_B_SHAPESHIFTER_NEEDS_TC_OVERRIDE}
                    [/case]
                    [case]
                        value=2
                        {VARIABLE shapeshifter_type "Nightshade Fire"}
                        {VARIABLE shapeshifter_id   "fakeAnya"}
                        {VARIABLE shapeshifter_name _"Anya"}
                        {FINALE_B_SHAPESHIFTER_NEEDS_TC_OVERRIDE}
                    [/case]
                    [case]
                        value=3
                        {VARIABLE shapeshifter_type "Anlinde Elvish Avatar"}
                        {VARIABLE shapeshifter_id   "fakeAnlinde"}
                        {VARIABLE shapeshifter_name _"Anlindë"}
                        {FINALE_B_SHAPESHIFTER_NEEDS_TC_OVERRIDE}
                    [/case]
                    [case]
                        value=4
                        {VARIABLE shapeshifter_type "Elvish Wayfarer"}
                        {VARIABLE shapeshifter_id   "fakeGalas"}
                        {VARIABLE shapeshifter_name _"Galas"}
                    [/case]
                    [case]
                        value=5
                        {VARIABLE shapeshifter_type "Ancient Lich"}
                        {VARIABLE shapeshifter_id   "fakeMalin"}
                        {VARIABLE shapeshifter_name _"Mal Keshar"}
                    [/case]
                    [case]
                        value=6
                        {VARIABLE shapeshifter_type "Quenoth Champion"}
                        {VARIABLE shapeshifter_id   "fakeGarak"}
                        #textdomain wesnoth-utbs
                        {VARIABLE shapeshifter_name _"Garak"}
                        #textdomain wesnoth-After_the_Storm
                    [/case]
                    [case]
                        value=7
                        {VARIABLE shapeshifter_type "Faerie Dryad"}
                        {VARIABLE shapeshifter_id   "fakeIsawen"}
                        {VARIABLE shapeshifter_name _"Delanith-Isáwen"}
                    [/case]
                    [case]
                        value=8
                        {VARIABLE shapeshifter_type "Chaos Lorekeeper"}
                        {VARIABLE shapeshifter_id   "fakeReghara"}
                        {VARIABLE shapeshifter_name _"Reghara"}
                    [/case]
                    [else]
                        {VARIABLE shapeshifter_ellipse  $null}
                        {VARIABLE shapeshifter_overlays $null}
                        {RANDOM 1..100}
                        [if]
                            {VARIABLE_NUMERICAL_GREATER_THAN random 50}
                            [then]
                                {VARIABLE shapeshifter_variation gendered}
                            [/then]
                        [/if]
                    [/else]
                [/switch]

                [store_direction]
                    from_x,from_y=$banshee_loc.x,$banshee_loc.y
                    to_x,to_y=28,20
                [/store_direction]

                [scroll_to]
                    x,y=$banshee_loc.x,$banshee_loc.y
                [/scroll_to]

                [unit]
                    side=2
                    animate=yes
                    type=$shapeshifter_type
                    variation=$shapeshifter_variation
                    facing=$direction
                    x,y=$banshee_loc.x,$banshee_loc.y
                    id=$shapeshifter_id
                    name=$shapeshifter_name
                    ellipse=$shapeshifter_ellipse # wmllint: no ellipsecheck
                    overlays=$shapeshifter_overlays
                    generate_name=no
                    random_traits=no
                    random_gender=yes
                    [insert_tag]
                        name=modifications
                        variable=shapeshifter_mods
                    [/insert_tag]
                    [variables]
                        is_zhangors_body=yes
                    [/variables]
                [/unit]

                {CLEAR_VARIABLE shapeshifter_id,shapeshifter_type,shapeshifter_mods,shapeshifter_ellipse,shapeshifter_overlays,direction,random}
            [/do]
        [/foreach]

        {FINALE_B_CLEAR_SPAWN_LIMIT}
    [/event]

    # BIG FAT TODO:
    # Figure out a way to attribute shapeshifter kills from Fire Wraiths to
    # Elyssa without making the EH code even more bloated than it already is.

#define FINALE_B_SHAPESHIFTER_CONFRONT_EH _ID _SECOND_ID _WML
    [event]
        name=attack
        [filter]
            id="fake"+{_ID} # wmllint: ignore
        [/filter]
        [filter_second]
            id={_SECOND_ID}
        [/filter_second]

        [fire_event]
            name="shapeshifter "+{_ID}+" confront "+{_SECOND_ID}
            [primary_unit]
                x,y=$x1,$y1
            [/primary_unit]
        [/fire_event]
    [/event]

    [event]
        name=attack
        [filter_second]
            id="fake"+{_ID} # wmllint: ignore
        [/filter_second]
        [filter]
            id={_SECOND_ID}
        [/filter]

        [fire_event]
            name="shapeshifter "+{_ID}+" confront "+{_SECOND_ID}
            [primary_unit]
                x,y=$x2,$y2
            [/primary_unit]
        [/fire_event]
    [/event]

    [event]
        name="shapeshifter "+{_ID}+" confront "+{_SECOND_ID}

        {_WML}
    [/event]
#enddef

#define FINALE_B_SHAPESHIFTER_KILL_EH _ID _SECOND_ID _WML
    [event]
        name=die
        [filter]
            id="fake"+{_ID} # wmllint: ignore
        [/filter]
        [filter_second]
            id={_SECOND_ID}
        [/filter_second]

        {_WML}
    [/event]
#enddef

    #
    # Anya doppelganger
    #

    # Nothing for Elyssa<->Anya.
    #{FINALE_B_SHAPESHIFTER_CONFRONT_EH Anya Elyssa ()}

    {FINALE_B_SHAPESHIFTER_KILL_EH Anya Elyssa (
        [message]
            speaker=Elyssa
            # po: This sentence could be interpreted in two different ways; either Elyssa
            # po: is stating that she wouldn't want to kill the real Anya, or that she
            # po: does not necessarily consider the real Anya to be the real Guardian of
            # po: Darkness, and hence she might not hesitate in killing her. This
            # po: ambiguity is intentional.
            message= _ "I would hate to have to kill you... if you were the real Guardian of Darkness."
        [/message]
    )}

    {FINALE_B_SHAPESHIFTER_CONFRONT_EH Anya Elynia (
        [message]
            speaker=unit
            message= _ "You abandoned me..."
        [/message]
    )}

    {FINALE_B_SHAPESHIFTER_KILL_EH Anya Elynia (
        [message]
            speaker=Elynia
            # po: ‘Demon’ here refers to Zhangor.
            message= _ "Posing as my dearest friend will not save you from my wrath, demon!"
        [/message]
    )}

    #
    # Argan doppelganger
    #

    {FINALE_B_SHAPESHIFTER_CONFRONT_EH Argan Elyssa (
        [message]
            speaker=unit
            message= _ "You left me to die..."
        [/message]
    )}

    {FINALE_B_SHAPESHIFTER_KILL_EH Argan Elyssa (
        [message]
            speaker=Elyssa
            message= _ "Your imitation of Argan is once again absolutely pathetic."
        [/message]
    )}

    {FINALE_B_SHAPESHIFTER_CONFRONT_EH Argan Elynia (
        [message]
            speaker=unit
            message= _ "You betrayed me..."
        [/message]
    )}

    {FINALE_B_SHAPESHIFTER_KILL_EH Argan Elynia (
        [message]
            speaker=Elynia
            message= _ "This cheap trick will not break me down this time."
        [/message]
    )}

    #
    # Galas doppelganger
    #

    {FINALE_B_SHAPESHIFTER_CONFRONT_EH Galas Elyssa (
        [message]
            speaker=unit
            message= _ "You killed me..."
        [/message]
    )}

    {FINALE_B_SHAPESHIFTER_KILL_EH Galas Elyssa (
        [message]
            speaker=Elyssa
            message= _ "I could slaughter a whole host of these stupid elves a thousand times, Zhangor. Is this all you’ve got?"
        [/message]
    )}

    {FINALE_B_SHAPESHIFTER_CONFRONT_EH Galas Elynia (
        [message]
            speaker=unit
            message= _ "You failed to protect me..."
        [/message]
    )}

    {FINALE_B_SHAPESHIFTER_KILL_EH Galas Elynia (
        [message]
            speaker=Elynia
            message= _ "I no longer care for any of this, Zhangor. I no longer care."
        [/message]
    )}

    #
    # Mal Keshar doppelganger
    #

    {FINALE_B_SHAPESHIFTER_CONFRONT_EH Malin Elyssa (
        [message]
            speaker=unit
            message= _ "You are no small part of the cause of my departure from this world, you know."
        [/message]
    )}

    {FINALE_B_SHAPESHIFTER_KILL_EH Malin Elyssa (
        [message]
            speaker=Elyssa
            message= _ "I could not care less about a walking joke like you."
        [/message]
    )}

    {FINALE_B_SHAPESHIFTER_CONFRONT_EH Malin Elynia (
        [message]
            speaker=unit
            message= _ "You could have prevented my death, Elynia. You could have—"
        [/message]
    )}

    {FINALE_B_SHAPESHIFTER_KILL_EH Malin Elynia (
        [message]
            speaker=Elynia
            message= _ "No... there was nothing I could do. Stop this!"
        [/message]
    )}

    #
    # Niryone doppelganger
    #

    # Nothing for Elyssa<->Niryone.
    #{FINALE_B_SHAPESHIFTER_CONFRONT_EH Niryone Elyssa ()}

    {FINALE_B_SHAPESHIFTER_KILL_EH Niryone Elyssa (
        [message]
            speaker=Elyssa
            # po: 'Her' refers to Elynia.
            message= _ "I assume you are important to her in some way?"
        [/message]
    )}

    {FINALE_B_SHAPESHIFTER_CONFRONT_EH Niryone Elynia (
        [message]
            speaker=unit
            message= _ "... You were too late to save me, Elynia. If only you had not allowed him into your heart—"
        [/message]
    )}

    {FINALE_B_SHAPESHIFTER_KILL_EH Niryone Elynia (
        [message]
            speaker=Elynia
            message= _ "NO! I don’t want to remember this! I’LL KILL YOU!"
        [/message]
    )}

    #
    # Garak doppelganger
    #

    {FINALE_B_SHAPESHIFTER_CONFRONT_EH Garak Elyssa (
        [message]
            speaker=unit
            message= _ "With your power you surely could have banished those foul creatures to whence they came. Useless child."
        [/message]
    )}

    {FINALE_B_SHAPESHIFTER_KILL_EH Garak Elyssa (
        [message]
            speaker=Elyssa
            message= _ "... You peered into places you shouldn’t have, Zhangor."
        [/message]
    )}

    {FINALE_B_SHAPESHIFTER_CONFRONT_EH Garak Elynia (
        [message]
            speaker=unit
            message= _ "I wonder what became of my kind under your command..."
        [/message]
    )}

    {FINALE_B_SHAPESHIFTER_KILL_EH Garak Elynia (
        [message]
            speaker=Elynia
            message= _ "..."
        [/message]
    )}

    #
    # Reghara doppelganger
    #

    {FINALE_B_SHAPESHIFTER_CONFRONT_EH Reghara Elyssa (
        [message]
            speaker=unit
            message= _ "HA! I hope you enjoyed your stay with us, sweetheart."
        [/message]
    )}

    {FINALE_B_SHAPESHIFTER_KILL_EH Reghara Elyssa (
        [message]
            speaker=Elyssa
            message= _ "I’LL KILL YOU!"
        [/message]
    )}

    {FINALE_B_SHAPESHIFTER_CONFRONT_EH Reghara Elynia (
        [message]
            speaker=unit
            # po: Use your imagination here. See also E3S7A.2's intro text.
            message= _ "You must feel proud of yourself, allying with a mass-murderer like this c—"
        [/message]
    )}

    {FINALE_B_SHAPESHIFTER_KILL_EH Reghara Elynia (
        [message]
            speaker=Elynia
            message= _ "That couldn’t possibly be my own memory... I don’t think..."
        [/message]
    )}

    #
    # Anlindë doppelganger
    #

    {FINALE_B_SHAPESHIFTER_CONFRONT_EH Anlinde Elyssa (
        [message]
            speaker=unit
            message= _ "For someone who wants so much to stop Uria, you are hardly any different from her kind."
        [/message]
    )}

    {FINALE_B_SHAPESHIFTER_KILL_EH Anlinde Elyssa (
        [message]
            speaker=Elyssa
            message= _ "... You are not wrong."
        [/message]
    )}

    {FINALE_B_SHAPESHIFTER_CONFRONT_EH Anlinde Elynia (
        [message]
            speaker=unit
            message= _ "Taking advantage of my protégé’s heart so soon after my death... You truly are a shameless creature."
        [/message]
    )}

    {FINALE_B_SHAPESHIFTER_KILL_EH Anlinde Elynia (
        [message]
            speaker=Elynia
            message= _ "I’ve had enough. You will not break me again."
        [/message]
    )}

    #
    # Delanith-Isáwen doppelganger
    #

    # Nothing for Elyssa<->Delanith-Isáwen.
    #{FINALE_B_SHAPESHIFTER_CONFRONT_EH Isawen Elyssa ()}

    {FINALE_B_SHAPESHIFTER_KILL_EH Isawen Elyssa (
        [message]
            speaker=Elyssa
            message= _ "... Is this..."
        [/message]
    )}

    {FINALE_B_SHAPESHIFTER_CONFRONT_EH Isawen Elynia (
        [message]
            speaker=unit
            message= _ "Child..."
        [/message]
    )}

    {FINALE_B_SHAPESHIFTER_KILL_EH Isawen Elynia (
        [message]
            speaker=Elynia
            message= _ "M— mother...? No... NO! STOP!"
        [/message]
    )}

    [event]
        name=die
        [filter]
            type=Demon Shapeshifter
        [/filter]

        # Whoever isn't the unit who killed the shapeshifter gets to speak the
        # first time around.
        [if]
            [have_unit]
                id=Elynia
                x,y=$x2,$y2
            [/have_unit]
            [then]
                [message]
                    speaker=Elyssa
                    message= _ "They are manifestations of Zhangor’s will — not even real shapeshifters!"
                [/message]
            [/then]
            [else]
                [message]
                    speaker=Elynia
                    message= _ "They are manifestations of Zhangor’s will — not even real shapeshifters!"
                [/message]
            [/else]
        [/if]
    [/event]

    ############################################################################
    #                                                                          #
    #               COMMON SPAWN LOGIC FOR BOSS PHASES 2 THROUGH 4             #
    #                                                                          #
    ############################################################################

    #
    # Unlike the boss recruitment controller EH, this code is executed only at
    # the end of boss side turns (and only if the boss stage wasn't advanced
    # on the same turn!) since we're spawning high-impact units that start with
    # full moves, rather than giving the AI the option to recruit whatever it
    # deems best for the occasion.
    #

    [event]
        name=side 2 turn end
        first_time_only=no
        [filter_condition]
            {VARIABLE_NUMERICAL_GREATER_THAN boss_stage 1}
            {VARIABLE_NUMERICAL_NOT_EQUALS boss_skip_spawns_on_turn $turn_number}
        [/filter_condition]

        [fire_event]
            name=boss spawns controller
        [/fire_event]
    [/event]

    [event]
        name=boss spawns controller
        first_time_only=no

        [if]
            {VARIABLE_NUMERICAL_GREATER_THAN boss_stage 3}
            [then]
                # On phases 4 and 5 satellite cores start being spawned every
                # single turn regardless of anything else. This event is
                # defined later.
                [fire_event]
                    name=satellite spawn controller
                [/fire_event]
            [/then]
        [/if]

        {VARIABLE can_do_boss_spawns "$(if($turn_number % {DIFF 4 3 3} = 0, 'yes', 'no'))"}
        {VARIABLE_RANDOM boss_spawns_type {DIFF
            banshee,banshee,shapeshifter
            banshee,banshee,shapeshifter,shapeshifter
            banshee,banshee,shapeshifter,shapeshifter,shapeshifter
        }}

        [if]
            {VARIABLE_BOOLEAN_EQUALS can_do_boss_spawns yes}
            [then]
                {VARIABLE_INC boss_spawn_sequence_count}

                {LOG_ATS_DBG ("E3S10_BOSS: run spawn sequence: $boss_spawns_type")}

                [fire_event]
                    name="$boss_spawns_type spawn controller"
                [/fire_event]
            [/then]
        [/if]

        #
        # Every other spawn sequence, on Normal there's a 6% chance the other
        # type of spawn will be triggered as well, while on Hard it's a 14%
        # chance. This does not increment boss_spawn_sequence_count.
        #

#ifndef EASY
        {VARIABLE_RANDOM can_do_boss_spawns 1..100}
        {VARIABLE        can_do_boss_spawns "$(if($can_do_boss_spawns % {DIFF 0 17 7} = 0, 'yes', 'no'))"}

        [if]
            {VARIABLE_BOOLEAN_EQUALS can_do_boss_spawns yes}
            [then]
                {VARIABLE boss_spawns_type "$(if($boss_spawns_type = 'banshee', 'shapeshifter', 'banshee'))"}

                {LOG_ATS_DBG ("E3S10_BOSS: run extra spawn sequence: $boss_spawns_type")}

                [fire_event]
                    name="$boss_spawns_type spawn controller"
                [/fire_event]
            [/then]
        [/if]
#endif

        {CLEAR_VARIABLE can_do_boss_spawns,boss_spawns_type}
    [/event]

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

    [event]
        name=last breath
        [filter]
            id=Zhangor
        [/filter]
        [filter_condition]
            {VARIABLE_NUMERICAL_EQUALS boss_stage 3}
        [/filter_condition]

        [fire_event]
            name=boss stage 4
            [primary_unit]
                id=Zhangor
            [/primary_unit]
        [/fire_event]
    [/event]

    [event]
        name=boss stage 4

        {FINALE_B_SET_BOSS_STAGE 4}

        # This will be used by the stage 4 'new turn' EH.
        {VARIABLE boss_status_flag 0}

        {VARIABLE ofacing $unit.facing}
        {VARIABLE ox      $x1}
        {VARIABLE oy      $y1}

        {LOCK_VIEW}

        [message]
            speaker=Elynia
            message= _ "Brace yourself!"
        [/message]

        {RESET_UNIT_STATUSES id=Elyssa,Elynia,Zhangor}

        [scroll_to_unit]
            id=Zhangor
        [/scroll_to_unit]

        [delay]
            time=250
        [/delay]

        {FLASH_RED (
            [sound]
                name="dragonstick.ogg"
            [/sound]

            [kill]
                id=Zhangor
            [/kill]

            [unit]
                canrecruit=yes
                side=2
                id=Zhangor
                name= _ "Zhangor"
                type=Angel of Blood
                {IS_BOSS}
                variation=nightmare_phase4a
                x,y=$ox,$oy
                facing=$ofacing
                [modifications]
                    {TRAIT_UNDEAD}
                    {TRAIT_DEXTROUS}
                [/modifications]
            [/unit]

            [push_units_away_from]
                from_x,from_y=$boss_keep.x,$boss_keep.y
                [filter]
                    [not]
                        id=Zhangor
                    [/not]
                    [filter_location]
                        x,y=$boss_keep.x,$boss_keep.y
                        radius=8
                    [/filter_location]
                [/filter]
            [/push_units_away_from]

            {REPLACE_SCENARIO_MUSIC "vengeful.ogg"}
        )}

        {CLEAR_VARIABLE ox,oy,facing}

        [redraw][/redraw]

        [delay]
            time=500
        [/delay]

        # FIXME: placeholder

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

        [delay]
            time=500
        [/delay]

        [message]
            speaker=Elyssa
            message= _ "Wh— what is... happening to him..."
        [/message]

        [message]
            speaker=Elynia
            message= _ "This is getting out of hand. We need to defeat him here and now!"
        [/message]

        [message]
            speaker=Elyssa
            message= _ "... to his body..."
        [/message]

        {UNLOCK_VIEW}

        [event]
            delayed_variable_substitution=no
            name="side 2 turn $($turn_number + 1) end"

            [message]
                speaker=Elynia
                message= _ "Elyssa? Are you still with me?"
            [/message]

            [delay]
                time=500
            [/delay]

            [message]
                speaker=Elyssa
                message= _ "Is this... Is this the fate of our kind should we allow emotions to overpower our minds?"
            [/message]
        [/event]

        [event]
            delayed_variable_substitution=no
            name="turn $($turn_number + 2)"

            [message]
                speaker=Elyssa
                message= _ "No... Zhangor devoured the heart of a Gatekeeper. He became like this of his own volition. Focus, Elyssa."
            [/message]

            [event]
                name=attack
                [filter]
                    id=Elyssa
                [/filter]

                [message]
                    speaker=Elyssa
                    message= _ "FOCUS!"
                [/message]
            [/event]
        [/event]
    [/event]

    #
    # At the start of each player turn during boss stage 4, Zhangor changes
    # shape and unleashes devastating attacks during his own turn. The player
    # has only their own side turn to move away from his path.
    #

    [event]
        name=new turn
        first_time_only=no
        [filter_condition]
            {VARIABLE_NUMERICAL_EQUALS boss_stage 4}
        [/filter_condition]

        # Charge up every 4/3/3 turns depending on the difficulty level.

        {VARIABLE boss_should_attack "$(if($turn_number % {DIFF 4 3 3} = 0, 'yes', 'no'))"}

        {VARIABLE new_variation $null}

        [if]
            {VARIABLE_BOOLEAN_EQUALS boss_should_attack yes}
            [then]
                {VARIABLE_INC boss_status_flag}

                # Use the more devastating 4c variation once every 3 charges.

                {VARIABLE new_variation "$(if($boss_status_flag % 3 = 0, 'nightmare_phase4c', 'nightmare_phase4b'))"}

                [fire_event]
                    name=supercharge warning
                [/fire_event]

                # And reset to 4a afterwards.

                [if]
                    {VARIABLE_NUMERICAL_GREATER_THAN boss_status_flag 3}
                    [then]
                        {VARIABLE boss_status_flag 0}
                    [/then]
                [/if]
            [/then]
            [else]
                {VARIABLE new_variation "nightmare_phase4a"}
            [/else]
        [/if]

        [remove_object]
            object_id=boss_variation
            id=Zhangor
        [/remove_object]

        #
        # HACK: Quite annoyingly, [remove_object] does not allow us to ignore
        # the once-per-scenario limit on using objects with a particular id.
        # (The id is there to make the object trivial to remove.)
        #
        # gfgtdf suggested using [modify_unit] to insert the object into the
        # unit directly, which works as of version 1.14.13 at least. But it's
        # worth keeping in mind that this could randomly break in the future
        # since it's obviously not an intentional feature. We probably should
        # use an id-less [object] and implement our own logic to remove it from
        # "Bob".
        #
        # See also zhangor5 health controller.
        #

        [modify_unit]
            [filter]
                id=Zhangor
            [/filter]

            [object]
                id=boss_variation
                silent=yes

                [effect]
                    apply_to=variation
                    name=$new_variation
                [/effect]
            [/object]
        [/modify_unit]

        [redraw][/redraw]

        {CLEAR_VARIABLE new_variation,boss_should_attack}
    [/event]

    [event]
        name=supercharge warning

        [message]
            speaker=Elynia
            message= _ "He’s preparing to unleash a devastating attack, watch out!"
        [/message]
    [/event]

    #
    # The core spawn logic is triggered by the 'boss spawns controller' EH
    # defined earlier if the current boss stage is at least 4.
    #
    [event]
        name=satellite spawn controller
        first_time_only=no
        [filter_condition]
            [not]
                [have_unit]
                    side=2
                    type=Blood Core
                    # Spawn these even if the static cores are around, but not
                    # if there's even a single mobile core on the map.
                    variation=alpha,beta,gamma
                [/have_unit]
            [/not]
        [/filter_condition]

        [sound]
            name=magic-dark-big.ogg
        [/sound]

        {VARIABLE_RANDOM core_limit {DIFF 1..2 2..3 2..4}}

        [repeat]
            times=$core_limit
            [do]
                {VARIABLE_RANDOM n "0..$($satellite_locs.length - 1)"}
                {VARIABLE_RANDOM v alpha,beta,gamma}

                [set_variables]
                    name=satellite_loc
                    mode=replace
                    to_variable=satellite_locs[$n]
                [/set_variables]

                [store_direction]
                    from_x,from_y=$satellite_loc.x,$satellite_loc.y
                    to_x,to_y=28,20
                [/store_direction]

                [unit]
                    side=2
                    animate=yes
                    type=Blood Core
                    variation=$v
                    facing=$direction
                    x,y=$satellite_loc.x,$satellite_loc.y
                    random_traits=no
                    generate_name=no
                    {IS_HERO}
                    [variables]
                        is_zhangors_body=yes
                    [/variables]
                    [modifications]
                        [object]
                            silent=yes
                            [effect]
                                apply_to=loyal
                            [/effect]
                        [/object]
                    [/modifications]
                [/unit]
            [/do]
        [/repeat]

        {CLEAR_VARIABLE core_limit,satellite_loc,n,v}
    [/event]

    ############################################################################
    #                                                                          #
    #                           BOSS STAGE 5 (FINAL)                           #
    #                                                                          #
    ############################################################################

    [event]
        name=last breath
        [filter]
            id=Zhangor
        [/filter]
        [filter_condition]
            {VARIABLE_NUMERICAL_EQUALS boss_stage 4}
        [/filter_condition]

        [fire_event]
            name=boss stage 5
            [primary_unit]
                id=Zhangor
            [/primary_unit]
        [/fire_event]
    [/event]

#define FINALE_B_RED_AURA _ID
    [item]
        [filter]
            id={_ID}
        [/filter]
        halo="halo/heart-aura.png"
        image="misc/blank-hex.png"
    [/item]
#enddef

#define FINALE_B_GREEN_AURA _ID
    [item]
        [filter]
            id={_ID}
        [/filter]
        halo="halo/heart-aura.png~CS(-255,128,16)"
        image="misc/blank-hex.png"
    [/item]
#enddef

#define FINALE_B_FADE_STEP NUMBER DELAY_TIME
    {FADE_STEP_RGB {NUMBER} -500 -500 {DELAY_TIME}}
#enddef

    [event]
        name=boss stage 5

        {FINALE_B_SET_BOSS_STAGE 5}

        # This will be used by 'zhangor5 health controller'.
        {VARIABLE boss_status_flag 1}

        {VARIABLE ofacing $unit.facing}
        {VARIABLE ox      $x1}
        {VARIABLE oy      $y1}

        {LOCK_VIEW}

        {REPLACE_SCENARIO_MUSIC "vengeful.ogg"}
        {INCIDENTAL_MUSIC       "in_the_land_of_madness.ogg"}

        [message]
            speaker=Zhangor
            message= _ "... You... CANNOT..."
        [/message]

        {RESET_UNIT_STATUSES id=Elyssa,Elynia,Zhangor}

        [scroll_to_unit]
            id=Zhangor
        [/scroll_to_unit]

        [delay]
            time=250
        [/delay]

        {FLASH_RED (
            [sound]
                name="dragonstick.ogg"
            [/sound]

            [kill]
                id=Zhangor
            [/kill]

            [unit]
                canrecruit=yes
                side=2
                id=Zhangor
                name= _ "Zhangor"
                type=Angel of Blood
                {IS_BOSS}
                variation=nightmare_phase5a
                x,y=$ox,$oy
                facing=$ofacing
                [modifications]
                    {TRAIT_UNDEAD}
                    {TRAIT_DEXTROUS}
                [/modifications]
            [/unit]
        )}

        [redraw][/redraw]

        [delay]
            time=500
        [/delay]

        [message]
            speaker=Zhangor
            message= _ "... destroy ME..."
        [/message]

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

        # Remove healing glyphs
        [remove_item][/remove_item]

        # And debris in the main atrium
        [remove_terrain_overlays]
            terrain=*^Ii,*^Gvs,*^Dr
        [/remove_terrain_overlays]

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

        [fire_event]
            name=blood flood begin
        [/fire_event]

        [delay]
            time=500
        [/delay]

        [message]
            speaker=Elynia
            message= _ "Do you think we can do it? He should not be able to focus us anymore!"
        [/message]

        [message]
            speaker=Elyssa
            message= _ "No... I’m not ready yet! Just keep striking at him with all you have!"
        [/message]

        [message]
            speaker=Zhangor
            message= _ "(<i>feminine voice</i>) ... I am... the Angel of Blood..."
        [/message]

        {UNLOCK_VIEW}
    [/event]

    # TODO FIXME: can we trust the engine not to interrupt the attack sequence
    # if we set Zhangor's variation on attacker/defender hits instead?

    [event]
        name=attack end
        first_time_only=no
        [filter_second]
            id=Zhangor
        [/filter_second]
        [filter_condition]
            {VARIABLE_NUMERICAL_EQUALS boss_stage 5}
        [/filter_condition]

        {EVENT_INVERSE_DISPATCH_SIMPLE "zhangor5 health controller"}
    [/event]

    [event]
        name=attack end
        first_time_only=no
        [filter]
            id=Zhangor
        [/filter]
        [filter_condition]
            {VARIABLE_NUMERICAL_EQUALS boss_stage 5}
        [/filter_condition]

        {EVENT_FORWARD_DISPATCH_SIMPLE "zhangor5 health controller"}
    [/event]

    #
    # Advance Zhangor's unit variation during phase 5 depending on his health
    # remaining. Alternatively, if his HP drops below a difficulty-specific
    # value, trigger the final cutscene.
    #

    [event]
        name=zhangor5 health controller
        first_time_only=no
        [filter_condition]
            {VARIABLE_NUMERICAL_LESS_THAN boss_status_flag 3}
        [/filter_condition]

        {VARIABLE new_variation    none}
        {VARIABLE zhangor_hp_level "$(round(100.0 * as_decimal($unit.hitpoints) / $unit.max_hitpoints))"}

        [if]
            {VARIABLE_NUMERICAL_LESS_THAN_OR_EQUAL zhangor_hp_level 33}
            {VARIABLE_NUMERICAL_LESS_THAN boss_status_flag 3}
            [then]
                {VARIABLE boss_status_flag 3}
                {VARIABLE new_variation nightmare_phase5c}
            [/then]
            [elseif]
                {VARIABLE_NUMERICAL_LESS_THAN_OR_EQUAL zhangor_hp_level 66}
                {VARIABLE_NUMERICAL_LESS_THAN boss_status_flag 2}
                [then]
                    {VARIABLE boss_status_flag 2}
                    {VARIABLE new_variation nightmare_phase5b}
                [/then]
            [/elseif]
        [/if]

        [if]
            [not]
                {VARIABLE_LEXICAL_IN new_variation none,$unit.variation}
            [/not]
            [then]
                [remove_object]
                    object_id=boss_variation
                    id=Zhangor
                [/remove_object]

                #
                # HACK:
                # See new turn event for phase 4.
                #

                [modify_unit]
                    [filter]
                        id=Zhangor
                    [/filter]

                    [object]
                        id=boss_variation
                        silent=yes

                        [effect]
                            apply_to=variation
                            name=$new_variation
                        [/effect]
                    [/object]
                [/modify_unit]

                [redraw][/redraw]

                # Elyssa may have something to say. The fact that we don't
                # attempt to set the same variation more than once guarantees
                # that her messages only get displayed once.

                [delay]
                    time=500
                [/delay]

                # NOTE: if Zhangor takes an EXTREME amount of damage, Elyssa
                # will not speak the first line. This should be such an
                # unlikely occurrence we shouldn't need to worry about it,
                # however.

                [switch]
                    variable=boss_status_flag
                    [case]
                        value=2
                        [message]
                            speaker=Elyssa
                            message= _ "You are nothing but a sad memory of who you once were. It seems Uria did not do such a good job in bringing you back from your grave."
                        [/message]
                    [/case]
                    [case]
                        value=3
                        [message]
                            speaker=Elyssa
                            message= _ "Perhaps she was aware that you would eventually betray her. Or perhaps she did not have the same infatuation she had with Argan."
                        [/message]
                    [/case]
                [/switch]
            [/then]
        [/if]

        {CLEAR_VARIABLE new_variation,zhangor_hp_level}
    [/event]

    [event]
        name=zhangor5 health controller
        [filter_condition]
            {VARIABLE_NUMERICAL_LESS_THAN unit.hitpoints {DIFF 50 25 10}}
        [/filter_condition]

        [fire_event]
            name=boss end
        [/fire_event]
    [/event]

    #
    # Redden the screen as in E3S9
    #

    [event]
        name=attacker hits
        first_time_only=no
        [filter]
            id=Zhangor
        [/filter]
        [filter_condition]
            {VARIABLE_NUMERICAL_EQUALS boss_stage 5}
        [/filter_condition]

        [fire_event]
            name="zhangor5 lighting controller"
        [/fire_event]
    [/event]

    [event]
        name=defender hits
        first_time_only=no
        [filter]
            id=Zhangor
        [/filter]
        [filter_condition]
            {VARIABLE_NUMERICAL_EQUALS boss_stage 5}
        [/filter_condition]

        [fire_event]
            name="zhangor5 lighting controller"
        [/fire_event]
    [/event]

    [event]
        name=zhangor5 lighting controller
        first_time_only=no

        [final_boss_hp_tint]
            id=Zhangor
        [/final_boss_hp_tint]
    [/event]

    #
    # While in stage 5, Zhangor's body starts flooding the keep in the main
    # atrium with blood. There's two phases for this:
    #
    # 1. We generate random pools within a 6 hex radius of Zhangor's main body.
    #
    # 2. Every turn, those pools grow in size by 1 in a random number of
    #    directions.
    #
    # 3. In all cases we leave impassable and unwalkable terrain alone since
    #    we don't want to accidentally allow the player to escape the boss
    #    arena.
    #

    # NOTE: We use event flag F to mark flooded tiles so we can tell them apart
    #       from (Shallow) Blood/Blood Ford tiles which are part of the map's
    #       initial state.

#define FINALE_B_FLOODED_LOCS_SLF
    terrain=*^&F
#enddef

    # Not all *^X*, only the Impassable Overlay. Dark Hive has an unfortunate
    # implementation detail matching that pattern.

#define FINALE_B_FLOODABLE_LOCS_SLF
    terrain=!,Wwb,*^&F,X*,X*^*,*^Xo,Q*,Q*^*,*^Q*,*^Zok
#enddef

    # Muddy Quagmire needs to become (Shallow) Blood instead of Blood Ford so
    # the terrain transition issues aren't too evident.
    #
    # NOTE: Please move this to a new WML action implemented in Lua if the
    #       substitution rules become messier.
#define FINALE_B_FLOOD_TERRAIN _TERRAIN_AT_POS_VARIABLE
    terrain="$(concatenate(if(find_string('${_TERRAIN_AT_POS_VARIABLE}', 'S') = 0, 'Wwb', 'Wwbf'), '^&F'))"
#enddef

    # Call me during stage 5 setup.

    [event]
        name=blood flood begin

        {VARIABLE boss_flood_active yes}

        [store_locations]
            [filter]
                id=Zhangor
            [/filter]
            variable=zhangor_loc
        [/store_locations]

        # NOTE: fallback for debugging of the flooding logic outside of the
        #       boss fight sequence.
        [if]
            {VARIABLE_ARRAY_EMPTY zhangor_loc}
            [then]
                [set_variables]
                    name=zhangor_loc
                    mode=replace
                    to_variable=boss_keep
                [/set_variables]
            [/then]
        [/if]

        {BUG_ON ({VARIABLE_NUMERICAL_NOT_EQUALS zhangor_loc.length 1}) ()}

        [store_locations]
            variable=flood_initial_area
            {FINALE_B_FLOODABLE_LOCS_SLF}
            [and]
                x,y=$zhangor_loc.x,$zhangor_loc.y
                radius=6
            [/and]
        [/store_locations]

        {VARIABLE_RANDOM puddle_count 4..10}

        [repeat]
            times=$puddle_count
            [do]
                {VARIABLE_RANDOM r "0..$($flood_initial_area.length - 1)"}

                [terrain]
                    x,y=$flood_initial_area[$r].x,$flood_initial_area[$r].y
                    {FINALE_B_FLOOD_TERRAIN flood_initial_area[$r].terrain}
                [/terrain]

                {CLEAR_VARIABLE flood_initial_area[$r]}
            [/do]
        [/repeat]

        {CLEAR_VARIABLE flood_initial_area,puddle_count,r,zhangor_loc}

        [redraw][/redraw]
    [/event]

#define FINALE_B_BLOOD_FLOOD_STEP
    [fire_event]
        name=blood flood step
    [/fire_event]
#enddef

    [event]
        name=side 2 turn
        first_time_only=no
        [filter_condition]
            {VARIABLE_BOOLEAN_EQUALS boss_flood_active yes}
        [/filter_condition]

        {FINALE_B_BLOOD_FLOOD_STEP}
    [/event]

    [event]
        name=blood flood step
        first_time_only=no

        # NOTE: This does nothing if fired early since the number of flood
        #       candidate locations will be zero.

        [store_locations]
            variable=flood_candidates
            {FINALE_B_FLOODABLE_LOCS_SLF}
            [filter_adjacent_location]
                {FINALE_B_FLOODED_LOCS_SLF}
            [/filter_adjacent_location]
        [/store_locations]

        # We don't want to flood everything all at the same time but we also
        # don't want to take forever since Elynia and Elyssa dispose of Mr. Z
        # pretty quickly during phase 5. Flood only half of the found locations
        # (remember that expontential growth is taking place here).

        {VARIABLE flood_cap "$(round($flood_candidates.length / 2))"}

        [repeat]
            times=$flood_cap
            [do]
                {VARIABLE_RANDOM r "0..$($flood_candidates.length - 1)"}

                [terrain]
                    x,y=$flood_candidates[$r].x,$flood_candidates[$r].y
                    {FINALE_B_FLOOD_TERRAIN flood_candidates[$r].terrain}
                [/terrain]

                {CLEAR_VARIABLE flood_candidates[$r]}
            [/do]
        [/repeat]

        {CLEAR_VARIABLE flood_cap,r,flood_candidates}

        [redraw][/redraw]
    [/event]

    [event]
        name=boss end

        {BUG_ON (
            [not]
                {VARIABLE_NUMERICAL_EQUALS boss_stage 5}
                # If he is below 1 HP he counts as not being on the map as far
                # as [have_unit] is concerned so we can't use this criterion
                # here.
                #[have_unit]
                #    id=Zhangor
                #[/have_unit]
            [/not]
        ) ("are you trying to break the campaign, love? why u do this")}

        {LOCK_VIEW}

        {CLEAR_LABELS}

        [scroll_to_unit]
            id=Zhangor
        [/scroll_to_unit]

        # Force the screen color shift to what it should be with Zhangor
        # at 0 HP, in case it somehow didn't happen already. (Debug mode?)

        {COLOR_ADJUST 0 -60 -60}

        {REPLACE_SCENARIO_MUSIC "silence.ogg"}
        {INCIDENTAL_MUSIC       "one_thousand_suns.ogg"}

        {FINALE_B_BLOOD_FLOOD_STEP}

        [redraw][/redraw]

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

        [message]
            speaker=Elyssa
            message= _ "What I do know for sure..."
        [/message]

        {FINALE_B_BLOOD_FLOOD_STEP}

        [kill]
            [filter_location]
                x,y=28,20
                radius=8
            [/filter_location]
            [not]
                id=Zhangor,Elynia,Elyssa
            [/not]
            [not]
                # Zhangor's component parts get killed in a second step if
                # there are any left still.
                [filter_wml]
                    [variables]
                        is_zhangors_body=yes
                    [/variables]
                [/filter_wml]
            [/not]
            animate=yes
            fire_event=no
        [/kill]

        # Recenter in case the game scrolled away
        [scroll_to_unit]
            id=Zhangor
        [/scroll_to_unit]

        [message]
            speaker=Elyssa
            message= _ "... is that you are done. Elynia!"
        [/message]

        {RESET_UNIT_STATUSES id=Elyssa,Elynia,Zhangor}

        [store_unit]
            [filter]
                id=Zhangor
            [/filter]
            kill=no
            variable=zhangor_store
        [/store_unit]

        {VARIABLE zhangor_store.name (_"unit_name^???")}

        [unstore_unit]
            variable=zhangor_store
            find_vacant=no
            check_passability=no
        [/unstore_unit]

        {FINALE_B_BLOOD_FLOOD_STEP}

        {MOVE_UNIT id=Elyssa "$($zhangor_store.x - 2)" $zhangor_store.y}
        {FACE_DIRECTION id=Elyssa se}

        [redraw][/redraw]

        {FINALE_B_BLOOD_FLOOD_STEP}

        [delay]
            time=250
        [/delay]

        [message]
            speaker=Zhangor
            # po: 'He' is intended to be used in the sense of a deity here.
            message= _ "(<i>feminine voice</i>) ... He will come for you... at the end..."
        [/message]

        {FINALE_B_BLOOD_FLOOD_STEP}

        {MOVE_UNIT id=Elynia "$($zhangor_store.x + 2)" $zhangor_store.y}
        {FACE_DIRECTION id=Elynia sw}

        {CLEAR_VARIABLE zhangor_store}

        [redraw][/redraw]

        {FINALE_B_BLOOD_FLOOD_STEP}

        [message]
            speaker=Zhangor
            # po: 'Him' also as a deity here.
            message= _ "(<i>feminine voice</i>) ... the Four will not suffice to defeat Him..."
        [/message]

        {FINALE_B_RED_AURA Elyssa}

        {FINALE_B_BLOOD_FLOOD_STEP}

        [message]
            speaker=Elyssa
            message= _ "Pierce into the heart of stone..."
        [/message]

        {COLOR_ADJUST 30 -40 -40}

        {QUAKE ({SOUND_LIST:LICH_HIT})}

        {FINALE_B_BLOOD_FLOOD_STEP}

        [kill]
            [filter_wml]
                [variables]
                    is_zhangors_body=yes
                [/variables]
            [/filter_wml]
            animate=yes
            fire_event=no
        [/kill]

        # Recenter in case the game scrolled away... again
        [scroll_to_unit]
            id=Zhangor
        [/scroll_to_unit]

        {FINALE_B_GREEN_AURA Elynia}

        {FINALE_B_BLOOD_FLOOD_STEP}

        [message]
            speaker=Elynia
            message= _ "... release the soul back to the void..."
        [/message]

        {FINALE_B_BLOOD_FLOOD_STEP}

        {COLOR_ADJUST 60 -30 -30}

        {QUAKE ({SOUND_LIST:LICH_HIT})}

        {MODIFY_UNIT id=Elyssa level 7}

        {FINALE_B_BLOOD_FLOOD_STEP}

        [message]
            speaker=Elyssa
            message= _ "... break the curse of Yarae..."
        [/message]

        {FINALE_B_BLOOD_FLOOD_STEP}

        {COLOR_ADJUST 90 -10 -10}

        {QUAKE ({SOUND_LIST:LICH_HIT})}

        {MODIFY_UNIT id=Elynia level 7}

        [redraw][/redraw]

        {REMOVE_SOUND_SOURCE cave_noise}

        {FINALE_B_BLOOD_FLOOD_STEP}

        [fade_out_music]
            duration=500
        [/fade_out_music]

        [message]
            speaker=Elynia
            message= _ "... dispel the spirit from this world."
        [/message]

        {FINALE_B_BLOOD_FLOOD_STEP}

        {COLOR_ADJUST 120 0 0}

        {QUAKE ({SOUND_LIST:LICH_HIT})}

        {FINALE_B_BLOOD_FLOOD_STEP}

        {COLOR_ADJUST 150 0 0}

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

        {FINALE_B_BLOOD_FLOOD_STEP}

        {COLOR_ADJUST 180 0 0}

        #
        # We need these two stored for the next scenario since we will be
        # working with Anya and Durvan's side information instead.
        #

        [store_unit]
            [filter]
                id=Elyssa
            [/filter]
            variable=elyssa_store
            kill=yes
        [/store_unit]

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

        {VARIABLE coming_from_e3s10 yes}

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

        {QUAKE ("dragonstick.ogg")}

        {COLOR_ADJUST 210 0 0}

        [remove_item][/remove_item]

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

        {RED_SCREEN}

        [delay]
            time=1900
        [/delay]

        #
        # Cross-fade from red screen to black screen.
        #

        {FINALE_B_FADE_STEP  224 5}
        {FINALE_B_FADE_STEP  192 5}
        {FINALE_B_FADE_STEP  160 5}
        {FINALE_B_FADE_STEP  128 5}
        {FINALE_B_FADE_STEP   96 5}
        {FINALE_B_FADE_STEP   64 5}
        {FINALE_B_FADE_STEP   32 5}
        {FINALE_B_FADE_STEP    0 5}
        {FINALE_B_FADE_STEP  -32 5}
        {FINALE_B_FADE_STEP  -64 5}
        {FINALE_B_FADE_STEP  -96 5}
        {FINALE_B_FADE_STEP -128 5}
        {FINALE_B_FADE_STEP -160 5}
        {FINALE_B_FADE_STEP -192 5}
        {FINALE_B_FADE_STEP -224 5}

        {BLACK_SCREEN}

        [place_shroud]
            side=1
            # EVERYWHERE
        [/place_shroud]

        [delay]
            time=10000
        [/delay]

        {REPLACE_SCENARIO_MUSIC "journeys_end.ogg"}

        [delay]
            time=4000
        [/delay]

        [message]
            speaker=narrator
            caption= _ "Aradellys"
            image="misc/blank-hex.png"
            message= _ "<i>It is all well, children of Irdya. You may rest now.</i>"
        [/message]

        [delay]
            time=4000
        [/delay]

        [fade_out_music]
            duration=2000
        [/fade_out_music]

        {UNLOCK_VIEW}

        {ENDLEVEL_QUIET}
    [/event]

    [event]
        name=victory

        {REMOVE_MENU_ITEM wmi_z_combo_attacks_info}

        {CLEAR_VARIABLE boss_stage,boss_start_turn}
        {CLEAR_VARIABLE boss_skip_spawns_on_turn,boss_spawn_sequence_count,boss_status_flag,boss_flood_active}
        {CLEAR_VARIABLE banshee_spawn_1st_run,satellite_spawn_1st_run,shapeshifter_spawn_1st_run,spawn_limit}
        {CLEAR_VARIABLE boss_keep,core_locs,banshee_locs,satellite_locs}
    [/event]
[/scenario]

#undef FINALE_B_SHARED_AI_PARAMS
#undef FINALE_B_FADE_STEP
#undef FINALE_B_RED_AURA
#undef FINALE_B_GREEN_AURA
#undef FINALE_B_ZHANGORS_KEEP_SLF
#undef FINALE_B_STOP_AT_SPAWN_LIMIT
#undef FINALE_B_SET_SPAWN_LIMIT
#undef FINALE_B_CLEAR_SPAWN_LIMIT
#undef FINALE_B_SHAPESHIFTER_NEEDS_TC_OVERRIDE
#undef FINALE_B_SHAPESHIFTER_KILL_EH
#undef FINALE_B_SHAPESHIFTER_CONFRONT_EH
#undef FINALE_B_SET_BOSS_STAGE
#undef FINALE_B_FLOODABLE_LOCS_SLF
#undef FINALE_B_FLOODED_LOCS_SLF
#undef FINALE_B_BLOOD_FLOOD_STEP
#undef FINALE_B_GLYPH_CONDITIONS
#undef FINALE_B_FLOOD_TERRAIN

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