#textdomain wesnoth-Invasion_from_the_Unknown

[scenario]
    id=23Bx_Do_Us_Part
    name= _ "Do Us Part"
    {MAP 23Bx_Do_Us_Part.map}
    turns=-1
    victory_when_enemies_defeated=no
    next_scenario=23C_Broken_Heart
    {NO_RECALLS}

    {SCENARIO_MUSIC "snowfall.ogg"}

    {FULLSCREEN_CUTSCENE_UI}

    {DEEP_UNDERGROUND} {SCHEDULE_LIGHTING -20 -40 -10}

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

        shroud=yes

        {NO_ECONOMY}

        # wmllint: recognize Galas
        {CHARACTER_STATS_GALAS}
    [/side]
    # wmllint: validate-on

    [side]
        side=2
        team_name=enemies
        user_team_name= _ "team_name^Chaos Emperor"
        {CHAOS_FLAG}
        color=gold

        {NO_ECONOMY}
        hidden=yes

        type=Dead Shadow Master
        id=Argan
        name= _ "Argan"
        hitpoints=1
    [/side]

    {HIVE_NOISE_2_SOUND_SOURCE}
    [+sound_source]
        id=noise # wmllint: ignore
    [/sound_source]

    {PLACE_IMAGE units/shaxthal/master-drone-dead.png 19 16}

    [event]
        name=prestart

        # wmllint: recognize Elynia
        {RECALL_ELYNIA_AT     27 12}
        # wmllint: recognize Mal Keshar
        {RECALL_MAL_KESHAR_AT 26 14}
        # wmllint: recognize Lédinor
        {RECALL_LEDINOR_AT    23 12}
        # wmllint: recognize Igor
        {RECALL_IGOR_AT       25 11}

        {FACE_UNIT side=1 id=Argan}

        [hide_unit][/hide_unit]

        {BLACK_SCREEN}

        {LOCK_VIEW}
    [/event]

    [event]
        name=start

        [fire_event]
            name=cutscene 1
        [/fire_event]

        [fire_event]
            name=cutscene 2
        [/fire_event]

        [fire_event]
            name=cutscene 3
        [/fire_event]

        {ENDLEVEL_QUIET} {NO_REPLAY_SAVE}
    [/event]

    [event]
        name=cutscene 1

        [store_shroud]
            side=1
            variable=s23bx_shroud_mask
        [/store_shroud]

        [place_shroud]
            side=1
        [/place_shroud]

        [delay]
            time=750
        [/delay]

        [message]
            speaker=Argan
            scroll=no
            highlight=no
            message= _ "Elynia..."
        [/message]

        [delay]
            time=750
        [/delay]

        [set_shroud]
            side=1
            shroud_data=$s23bx_shroud_mask
        [/set_shroud]

        {FADE_IN}

        [unhide_unit][/unhide_unit]

        [delay]
            time=750
        [/delay]

        {MOVE_UNIT (id=Elynia) 21 15}

        [message]
            speaker=Argan
            message= _ "... you betrayed... me..."
        [/message]

        [message]
            speaker=Elynia
            message= _ "No, Argan. You are the one who betrayed me... me and the peoples of Irdya as well. The peoples we swore to defend until the end of time. You betrayed everything we stood for..."
        [/message]

        [message]
            speaker=Argan
            message= _ "I did nothing but choose the right path for your sake. And this is how you repay me... Then again, I guess I had this coming ever since... then..."
        [/message]

        [message]
            speaker=Elynia
            message= _ "No, you never deserved any of this. If... None of this... none of this would have happened if I..."
        [/message]

        {MOVE_UNIT (id=Mal Keshar) 24 14}
        {MOVE_UNIT (id=Galas)      24 13}

        [message]
            speaker=Argan
            message= _ "In the end... it is not your fault..."
        [/message]

        [message]
            speaker=Mal Keshar
            message= _ "You said that Uria turned you into this. What does she really want? What’s her goal in enslaving and desecrating Irdya?"
        [/message]

        [message]
            speaker=Argan
            message= _ "Well... she will... she will avenge me... that is for sure. And now without the Union, you cannot— (<i>cough</i>) you cannot defeat... her... Even if you manage to find her... in the heart of... Urvatha... it would be suicide..."
        [/message]

        #
        # TODO visual effects
        #

        [message]
            speaker=narrator
            image=wesnoth-icon.png
            message= _ "Just as he said those words, Argan’s broken body suddenly convulsed, releasing a steaming mixture of blood and a thick yellow ooze from his mouth and chest, and splashing everything around him. The bright yellow glow that constantly lit his eyes until moments before began to vanish, little by little, until there was nothing to see but tears."
        [/message]

        {MOVE_UNIT id=Elynia 20 15}

        [message]
            speaker=narrator
            image=wesnoth-icon.png
            message= _ "... And once Elynia moved closer within his grasp, he violently clenched at her arm. And with hurried breath, he asked:"
        [/message]

        [message]
            speaker=Argan
            message= _ "Elynia... I beg of you... please forgive me. I allowed this to happen. I allowed her... to become who she is now... and control me... Promise me that you will never... you will never... become..."
        [/message]

        [kill]
            id=Argan
        [/kill]

        [message]
            speaker=Elynia
            message= _ "Become... become what? Argan?"
        [/message]

        [delay]
            time=750
        [/delay]

        [message]
            speaker=Elynia
            message= _ "Argan? What do you mean? What am I supposed to do? Argan?!"
        [/message]

        [message]
            speaker=Elynia
            message= _ "Argan! No, no, this can’t be happening, you can’t be doing this, I am trying to heal you, don’t you see? I..."
        [/message]

        [fade_out_music][/fade_out_music]

        [delay]
            time=2000
        [/delay]

        [message]
            speaker=Elynia
            message= _ "Why don’t you say anything? Argan!"
        [/message]

        [delay]
            time=750
        [/delay]

        [message]
            speaker=Elynia
            message= _ "Mal Keshar, don’t you just stand there and watch, help me! Help him!"
        [/message]

        [message]
            speaker=Mal Keshar
            message= _ "There is nothing I can do. His spirit left us too quickly, and to pull him back into the physical realm after that... You do remember why that is the greatest taboo of all, right?"
        [/message]

        [delay]
            time=1000
        [/delay]

        [message]
            speaker=Mal Keshar
            message= _ "At least he may finally rest now."
        [/message]

        [delay]
            time=3000
        [/delay]

        {MOVE_UNIT id=Galas 23 14}

        [hide_unit][/hide_unit]

        {FADE_TO_BLACK}

        [store_shroud]
            side=1
            variable=s23bx_shroud_mask
        [/store_shroud]

        [place_shroud]
            side=1
        [/place_shroud]

        [fade_out_sound_effects]
            time=2000
        [/fade_out_sound_effects]
    [/event]

    [event]
        name=cutscene 2

        [delay]
            time=5000
        [/delay]

        {STORYTXT_DO_US_PART}
        [+story]
            # Shut up Wesnoth 1.14, I don't need a stupid title here
            title=""
        [/story]
    [/event]

    [event]
        name=cutscene 3

        [delay]
            time=2000
        [/delay]

        {PLACE_IMAGE items/book1.png 19 16}

        [teleport]
            [filter]
                id=Galas
            [/filter]
            x,y=22,14
        [/teleport]

        [teleport]
            [filter]
                id=Mal Keshar
            [/filter]
            x,y=20,16
        [/teleport]

        [teleport]
            [filter]
                id=Lédinor
            [/filter]
            x,y=19,15
        [/teleport]

        [teleport]
            [filter]
                id=Igor
            [/filter]
            x,y=23,15
        [/teleport]

        {FACE_LOCATION side=1 x,y=19,16}

        # Center view on Galas again, Wesnoth is lazy about this

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

        [set_shroud]
            side=1
            shroud_data=$s23bx_shroud_mask
        [/set_shroud]

        {CLEAR_VARIABLE s23bx_shroud_mask}

        {FADE_IN}

        [unhide_unit][/unhide_unit]

        [fade_in_sound_effects]
            duration=1000
        [/fade_in_sound_effects]

        [message]
            speaker=Lédinor
            message= _ "Argan’s journal — it was hidden beneath his robes. Elynia, my lady, if you allow me to speak my opinion: we should keep it, it may contain information that could prove useful later."
        [/message]

        [delay]
            time=750
        [/delay]

        [message]
            speaker=Elynia
            message= _ "Very well... But..."
        [/message]

        [delay]
            time=750
        [/delay]

        [message]
            speaker=Elynia
            message= _ "... I should be the one to carry this with me, no-one else."
        [/message]

        [remove_item]
            image="items/book1.png"
        [/remove_item]

        [redraw][/redraw]

        [delay]
            time=750
        [/delay]

        {QUAKE rumble.ogg}

        [delay]
            time=120
        [/delay]

        {QUAKE ()}

        [delay]
            time=120
        [/delay]

        {QUAKE cave-in.ogg}

        [delay]
            time=120
        [/delay]

        {QUAKE ()}

        [delay]
            time=120
        [/delay]

        {QUAKE explosion.ogg}

        {QUAKE ()}

        [message]
            speaker=Mal Keshar
            message= _ "Uh-oh."
        [/message]

        {QUAKE explosion.ogg}

        [delay]
            time=120
        [/delay]

        {QUAKE ()}

        [delay]
            time=120
        [/delay]

        {QUAKE explosion.ogg}

        [delay]
            time=120
        [/delay]

        {QUAKE ()}

        [delay]
            time=120
        [/delay]

        {QUAKE rumble.ogg}

        [delay]
            time=120
        [/delay]

        [sound]
            name=cave-in.ogg
        [/sound]

        [delay]
            time=120
        [/delay]

        {QUAKE explosion.ogg}

        [delay]
            time=120
        [/delay]

        {QUAKE ()}

        [delay]
            time=120
        [/delay]

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

        [delay]
            time=120
        [/delay]

        {QUAKE rumble.ogg}

        [message]
            speaker=Galas
            message= _ "The stronghold is crumbling down! We must leave immediately!"
        [/message]

        [message]
            speaker=Mal Keshar
            message= _ "At least the Master of Darkness will have a proper burial."
        [/message]

        {QUAKE_HEAVY rumble.ogg}

        #
        # Everyone moves to the map border and their units are
        # put back to recall list (including Galas).
        #

        [store_unit]
            [filter]
                side=1
                x=1-100
                y=1-100
            [/filter]
            variable=heroes_store
            kill=no
        [/store_unit]

        [foreach]
            array=heroes_store
            [do]
                [kill]
                    id=$this_item.id
                [/kill]

                {RANDOM 35..37}

                [move_unit_fake]
                    x=$this_item.x,$random
                    y=$this_item.y,7
                    type=$this_item.type
                    gender=$this_item.gender
                    side=$this_item.side
                    variation=$this_item.variation
                    force_scroll=no
                [/move_unit_fake]

                [unstore_unit]
                    variable=this_item
                    x,y=recall,recall
                [/unstore_unit]
            [/do]
        [/foreach]

        {CLEAR_VARIABLE heroes_store,random}

        {QUAKE_HEAVY (rumble.ogg)}
        {QUAKE_HEAVY ()}
        {QUAKE_HEAVY ()}
        {QUAKE_HEAVY ()}

        {BLACK_SCREEN}

        [place_shroud]
            side=1
        [/place_shroud]

        [delay]
            time=2000
        [/delay]

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

        [delay]
            time=2000
        [/delay]
    [/event]
[/scenario]

# NOTE: jump scenario for compatibility with existing IftU 2.1.4.1 and earlier
#       saves for S23B. Remove in IftU 2.2.0.

[scenario]
    id=23Bx_Do_Us_Part_1
    name= _ "Do Us Part"
    {NO_MAP_DATA}
    turns=-1
    victory_when_enemies_defeated=no
    next_scenario=23Bx_Do_Us_Part
    {NO_RECALLS}

    {SCENARIO_MUSIC "silence.ogg"}

    {FULLSCREEN_CUTSCENE_UI}

    {NEUTRAL_TOD}

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

        shroud=yes

        {NO_ECONOMY}

        # wmllint: recognize Galas
        {CHARACTER_STATS_GALAS}
    [/side]
    # wmllint: validate-on

    [event]
        name=prestart

        # Skip to real scenario
        {ENDLEVEL_QUIET} {NO_REPLAY_SAVE} {NO_START_OF_SCENARIO_SAVE}
    [/event]
[/scenario]

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


#textdomain wesnoth-Invasion_from_the_Unknown

# It's not "Till Death" so as to not make it 100% obvious. That said, people
# playing for the first time will not really see the title until after the
# opening cutscene. Still, by the time I started working on Reconstruction,
# the fact that the big bad is Elynia's SO had already become common knowledge,
# so I'm not too bothered about the potential for spoilers (that is, until
# somebody with a loose screw decides to allow players to see a campaign's list
# of scenarios in advance or something).

[scenario]
    id=23B_Until_Death
    name= _ "Until Death"
    {MAP 23B_Until_Death.map}
    turns=-1
    victory_when_enemies_defeated=no
    next_scenario=23Bx_Do_Us_Part
    {NO_RECALLS}

    {FULLSCREEN_CUTSCENE_UI}

    {SCENARIO_MUSIC "silence.ogg"}

    {DEEP_UNDERGROUND} {SCHEDULE_LIGHTING -40 -50 -15}

    {DEATHS_ACT_4}

    {SPAWN_CONTROLLER}

    {UNION_CAST_EVENTS}

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

        shroud=yes

        {NO_ECONOMY}

        {GOLD 180 160 150}

        # wmllint: recognize Galas
        {CHARACTER_STATS_GALAS}
    [/side]
    # wmllint: validate-on

    [side]
        side=2
        team_name=enemies
        user_team_name= _ "team_name^Chaos Emperor"
        {CHAOS_FLAG}
        color=gold

        {GOLD 220 240 260}
        {INCOME 6 9 11}
        recruit=Shadow Spawn,Shaxthal Drone,Ghost,Wraith,Spectre,Goliath,Automaton

        # Real leader spawned during the initial cutscene.
        no_leader=yes
        # The side does not initially have a leader (but it controls villages)
        # and we define victory for the human player based on the boss' death.
        defeat_condition=never

        {ai/aliases/stable_singleplayer.cfg}
        [ai]
            {AI_SIMPLE_ALWAYS_ASPECT village_value          0.00}
            {AI_SIMPLE_ALWAYS_ASPECT caution                0.00}
            {AI_SIMPLE_ALWAYS_ASPECT aggression             1.0}
            # Aggression turn ranges are not predetermined, use boss_stage as
            # a hint instead.
            {AI_ASPECT leader_aggression (
                engine=lua
                code=<<
                    local aggr = { 0.4, 1.0, 2.0 }
                    return aggr[in_range(wml.variables["boss_stage"], 1, #aggr)]
                >>
            )}

            {AI_ASPECT passive_leader (
                engine=lua
                code=<<
                    return wml.variables["boss_stage"] < 2
                >>
            )}

            # FIXME: doesn't work, Argan is reluctant to recruit Cockatrices.
            {AI_ASPECT recruitment_pattern (
                [facet]
                    id=initial_turns
                    turns=1-5
                    value=fighter,mixed fighter,fighter,scout,fighter,mixed fighter,fighter,scout
                [/facet]
                [facet]
                    id=stonegazer_turns
                    turns=6-10
                    value=stonegazer,fighter,mixed fighter,fighter,fighter,scout,mixed fighter
                [/facet]
                [facet]
                    id=no_scout_turns
                    turns=11-1000
                    value=stonegazer,fighter,fighter,mixed fighter,mixed fighter,fighter,fighter
                [/facet]
            )}

            # FIXME: disabled due to IftU issue #5
            #{AI_BOSS_TARGETING_ENGINE <<{"Elynia"}>>} # Don't target Galas specially!

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

            {AI_RECRUITMENT_ALWAYS_ASPECT (
                {AI_RECRUIT_LIMIT "Cockatrice"      {DIFF 1 2 2}}
                {AI_RECRUIT_LIMIT "Spectre"         {DIFF 1 2 2}}
                {AI_RECRUIT_LIMIT "Goliath"                    1}
                {AI_RECRUIT_LIMIT "Wraith"          {DIFF 2 3 3}}
                {AI_RECRUIT_LIMIT "Automaton"                  2} # Prefer Shadow Spawns.
            )}
        [/ai]
    [/side]

    [side]
        side=3
        team_name=enemies
        user_team_name= _ "team_name^Lair Guards"
        {CHAOS_FLAG}
        color=blue

        {GOLD 120 140 160}
        {INCOME 3 4 5}
        recruit=Shaxthal Rayblade,Shaxthal Runner Drone,Shaxthal Protector Drone

        {ai/aliases/stable_singleplayer.cfg}
        [ai]
            {AI_SIMPLE_ALWAYS_ASPECT aggression             1.00}
            {AI_SIMPLE_ALWAYS_ASPECT caution                0.00}
            {AI_SIMPLE_ALWAYS_ASPECT village_value          0.00}
        [/ai]

        canrecruit=yes
        type=Hell Overlord
        id=Yivril
        name= _ "Yivril"
        [modifications]
            {TRAIT_STRONG}
            {TRAIT_RESILIENT}
        [/modifications]
    [/side]

    [side]
        side=4
        team_name=enemies
        user_team_name= _ "team_name^Lair Hive"
        {CHAOS_FLAG}

        no_leader=yes
        hidden=yes
        {NO_ECONOMY}

        {ai/aliases/stable_singleplayer.cfg}
        [ai]
            {AI_SIMPLE_ALWAYS_ASPECT aggression             9.00}
            {AI_SIMPLE_ALWAYS_ASPECT caution                0.00}
            {AI_SIMPLE_ALWAYS_ASPECT village_value          0.00}
            {AI_SIMPLE_ALWAYS_ASPECT simple_targeting        yes}
            {AI_SIMPLE_ALWAYS_ASPECT grouping                 no}
        [/ai]
    [/side]

#define F_MATRIX _X _Y
    [unit]
        type=Verlissh Matrix Flow System
        x={_X}
        y={_Y}
        upkeep=free
        random_traits=yes
    [/unit]
#enddef

    [side]
        side=5
        team_name=enemies
        user_team_name= _ "team_name^Uria"
        {CHAOS_FLAG}

        {GOLD 120 130 140}
        {INCOME 2 3 4}
        recruit=Demon,Imp,Chaos Invoker,Chaos Invader,Chaos Headhunter

        {ai/aliases/stable_singleplayer.cfg}
        [ai]
            {AI_SIMPLE_ALWAYS_ASPECT aggression             1.00}
            {AI_SIMPLE_ALWAYS_ASPECT caution                0.00}
            {AI_SIMPLE_ALWAYS_ASPECT village_value          0.00}
        [/ai]

        canrecruit=yes
        type=Chaos Lorekeeper
        id=Mal Myaramor
        name= _ "Mal Myaramor"
        facing=se
        [modifications]
            {TRAIT_DEXTROUS}
            {TRAIT_QUICK}

            [object]
                silent=yes
                [effect]
                    apply_to=halo
                    halo="halo/obscures-aura.png"
                [/effect]
                [effect]
                    apply_to=new_ability
                    [abilities]
                        {ABILITY_OBSCURE}
                    [/abilities]
                [/effect]
            [/object]
        [/modifications]

        {F_MATRIX 28 20}
        {F_MATRIX 31 21}
        {F_MATRIX 34 21}
        {F_MATRIX 42 13}
        {F_MATRIX 43 15}
        {F_MATRIX 44 16}

        {F_MATRIX 36 24}
        {F_MATRIX 22 23}
        {F_MATRIX 24 25}
        {F_MATRIX 34 28}

        {F_MATRIX 50 20}
    [/side]

    [side]
        side=6
        team_name=enemies
        user_team_name= _ "team_name^Uria"
        {CHAOS_FLAG}

        {GOLD 140 150 160}
        {INCOME 3 5 6}
        recruit=Demon Zephyr,Chaos Hound,Shaxthal Runner Drone,Psy Crawler

        {ai/aliases/stable_singleplayer.cfg}
        [ai]
            {AI_SIMPLE_ALWAYS_ASPECT aggression             1.00}
            {AI_SIMPLE_ALWAYS_ASPECT caution                0.00}
            {AI_SIMPLE_ALWAYS_ASPECT village_value          0.00}
        [/ai]

        canrecruit=yes
        type=Shadow Courier
        id=An-Laila
        name= _ "An-Laila"
        facing=sw
        [modifications]
            {TRAIT_UNDEAD}
            {TRAIT_SLOW}
        [/modifications]
    [/side]

    [side]
        side=7
        team_name=enemies
        user_team_name= _ "team_name^Lair Hive"
        {CHAOS_FLAG}

        no_leader=yes
        hidden=yes
        {NO_ECONOMY}

        {ai/aliases/stable_singleplayer.cfg}
        [ai]
            {AI_SIMPLE_ALWAYS_ASPECT aggression             9.00}
            {AI_SIMPLE_ALWAYS_ASPECT caution                0.00}
            {AI_SIMPLE_ALWAYS_ASPECT village_value          0.00}
            {AI_SIMPLE_ALWAYS_ASPECT simple_targeting        yes}
            {AI_SIMPLE_ALWAYS_ASPECT grouping                 no}
        [/ai]
    [/side]

    {STARTING_VILLAGES_ALL 2}

    {HIVE_NOISE_2_SOUND_SOURCE}

    # No water drip sound sources, too much of a hassle to deal with them
    # during cutscenes (of which there are quite a lot here).

    {PLACE_IMAGE "items/dragonstatue.png" 14  8}
    {PLACE_IMAGE "items/dragonstatue.png" 18  6}

#define F_DRONES_EASY
    "Shaxthal Drone,Shaxthal Drone,Shaxthal Drone,Shaxthal Drone,Shaxthal Drone,Shaxthal Sentry Drone" #enddef
#define F_DRONES_MEDIUM
    "Shaxthal Drone,Shaxthal Drone,Shaxthal Drone,Shaxthal Drone,Shaxthal Sentry Drone,Shaxthal Sentry Drone" #enddef
#define F_DRONES_HARD
    "Shaxthal Drone,Shaxthal Sentry Drone,Shaxthal Sentry Drone" #enddef

#define F_DRONE _TURNS _DIFFICULTY_LEVEL _X _Y
    {TIMED_DRONE_SPAWNER {_TURNS} (type={F_DRONES_{_DIFFICULTY_LEVEL}}) 6 {_X} {_Y} }
#enddef

    {F_DRONE {DIFF 14 12 10} EASY   24 22}
    {F_DRONE {DIFF 9 8 7}    EASY   26 21}
    {F_DRONE {DIFF 9 8 7}    EASY   22 17}
    {F_DRONE {DIFF 14 12 10} HARD   17 22}
    {F_DRONE {DIFF 14 12 10} MEDIUM 19 25}
    {F_DRONE {DIFF 9 8 7}    EASY   26 26}
    {F_DRONE {DIFF 14 12 10} HARD   31 28}
    {F_DRONE {DIFF 14 12 10} MEDIUM  8 19}
    {F_DRONE {DIFF 8 7 6}    EASY   12 19}
    {F_DRONE {DIFF 8 7 6}    EASY    4 24}
    {F_DRONE {DIFF 8 7 6}    EASY    7 26}

    {F_DRONE {DIFF 8 7 6}    EASY   46 28}
    {F_DRONE {DIFF 11 10 9}  HARD   44 30}
    {F_DRONE {DIFF 11 10 9}  MEDIUM 42 32}
    {F_DRONE {DIFF 10 9 8}   HARD   40 30}
    {F_DRONE {DIFF 14 13 12} HARD   35 31}
    {F_DRONE {DIFF 9 8 7}    MEDIUM 29 32}
    {F_DRONE {DIFF 10 9 8}   HARD   38 27}
    {F_DRONE {DIFF 9 8 7}    EASY   48 24}
    {F_DRONE {DIFF 10 9 8}   EASY   47 23}
    {F_DRONE {DIFF 12 11 10} HARD   47 18}
    {F_DRONE {DIFF 9 8 7}    EASY   45 20}

    {F_DRONE {DIFF 9 8 7}    EASY   48 15}
    {F_DRONE {DIFF 9 8 7}    EASY   47 15}
    {F_DRONE {DIFF 8 7 6}    HARD   46 12}

    {F_DRONE {DIFF 9 8 7}    EASY   40 11}
    {F_DRONE {DIFF 10 9 8}   EASY   35  8}
    {F_DRONE {DIFF 9 8 7}    HARD   40  7}
    {F_DRONE {DIFF 10 9 8}   MEDIUM 35  5}
    {F_DRONE {DIFF 7 6 5}    EASY   31  5}
    {F_DRONE {DIFF 9 8 7}    EASY   30  2}
    {F_DRONE {DIFF 9 8 7}    EASY   32  1}

    {F_DRONE {DIFF 8 7 6}    HARD   44  3}
    {F_DRONE {DIFF 9 8 7}    HARD   48  4}

    {F_DRONE {DIFF 7 6 5}    EASY   47  7}
    {F_DRONE {DIFF 10 9 8}   HARD   50  9}
    {F_DRONE {DIFF 9 8 7}    MEDIUM 53  8}
    {F_DRONE {DIFF 9 8 7}    EASY   50  3}
    {F_DRONE {DIFF 9 8 7}    EASY   52  4}

    #
    # Set-up healing spots.
    #

    {OBJ_HEALING_GLYPH 51 30}
    {OBJ_HEALING_GLYPH  7 21}
    {OBJ_HEALING_GLYPH 32 24}
    {OBJ_HEALING_GLYPH 27 23}
    {OBJ_HEALING_GLYPH 20 17}
    {OBJ_HEALING_GLYPH 27 28}
    {OBJ_HEALING_GLYPH 38 23}
    {OBJ_HEALING_GLYPH 47 20}
    {OBJ_HEALING_GLYPH 52 18}
    {OBJ_HEALING_GLYPH 44 11}
    {OBJ_HEALING_GLYPH 47 14}
    {OBJ_HEALING_GLYPH 37  9}
    {OBJ_HEALING_GLYPH 32  3}

#define F_PLAYER_CAN_RECRUIT_UNDEAD
    [modify_side]
        side=1
        recruit=Skeleton,Skeleton Archer,Skeleton Rider,Walking Corpse,Vampire Bat,Ghost
    [/modify_side]
#enddef

    {FINAL_SCENARIO_PLAYER_RECRUITMENT}

    [event]
        name=prestart

        {VARIABLE boss_stage 1}

        {F_PLAYER_CAN_RECRUIT_UNDEAD}

        [store_unit]
            [filter]
                x=1-100
                y=1-100
                [not]
                    side=1
                [/not]
                [not]
                    # These count as set pieces, so don't hide them.
                    type=Verlissh Matrix Flow System
                [/not]
            [/filter]
            variable=initial_onmap_units
            kill=yes
        [/store_unit]

        #
        # Clear shroud from all reachable locations (two passes to account for
        # impassable hexes in range).
        #

        [remove_shroud]
            side=1
            [filter]
                id=Galas
            [/filter]
            radius=50
            [filter_radius]
                terrain=!,X*,X*^*,*^X*
            [/filter_radius]
        [/remove_shroud]

        [remove_shroud]
            side=1
            terrain=X*,X*^*,*^X*
            [filter_adjacent_location]
                [filter_vision]
                    side=1
                [/filter_vision]
            [/filter_adjacent_location]
        [/remove_shroud]

        #
        # Remove shroud from throne room area.
        #

        {CLEAR_CAVE_SHROUD (
            x,y,radius=30,14,10
        )}

        {CLEAR_CAVE_SHROUD (
            x,y,radius=35,17,8
        )}

        [hide_unit]
            side=1
        [/hide_unit]

        {BLACK_SCREEN}

        {LOCK_VIEW}
    [/event]

    [event]
        name=DEBUG93

        {VARIABLE boss_stage 3}

        [modify_turns]
            current=100
        [/modify_turns]

        {VARIABLE turn_number 100}

        [modify_unit]
            [filter]
                # wmllint: recognize Argan
                id=Argan
            [/filter]
            hitpoints=1
        [/modify_unit]

        [unit]
            x,y=37,19
            type=Ancient Lich
            side=1
        [/unit]
    [/event]

    ############################################################################
    #                                                                          #
    #                               INTRO CUTSCENE                             #
    #                                                                          #
    ############################################################################

    [event]
        name=start

        {FADE_IN}

        [unhide_unit]
            id=Galas
        [/unhide_unit]

        {MOVE_UNIT id=Galas 27 13}

        [scroll_to_unit]
            id=Galas
        [/scroll_to_unit]

        [delay]
            time=750
        [/delay]

        # wmllint: recognize Elynia
        {RECALL_ELYNIA_AT     26 13}
        # wmllint: recognize Mal Keshar
        {RECALL_MAL_KESHAR_AT 28 12}
        # wmllint: recognize Lédinor
        {RECALL_LEDINOR_AT    26 12}
        # wmllint: recognize Erathan
        {RECALL_ERATHAN2_AT   27 12}
        # wmllint: recognize Igor
        {RECALL_IGOR_AT       25 13}
        [scroll_to]
            x,y=37,18
        [/scroll_to]

        [unit]
            canrecruit=yes
            side=2
            placement=leader
            type=Master of Darkness
            variation=unknown_unit_type_label
            id=Chaos Emperor
            name= _ "Chaos Emperor"
            animate=yes
            facing=se
            {IS_BOSS}
            [modifications]
                {TRAIT_BIOMECHANICAL}
                {TRAIT_RESILIENT}
            [/modifications]
        [/unit]

        [delay]
            time=750
        [/delay]

        [message]
            speaker=Galas
            message= _ "There he is. There doesn’t appear to be anyone else around. Would it be possible for you two, perhaps, to strike him down from afar while he has his back turned?"
        [/message]

        [message]
            speaker=Chaos Emperor
            message= _ "I was certain that you would not fail your word, Galas. At long last you and your loyal companions have made it to my lair, overcoming every obstacle in your way. Well done."
        [/message]

        [delay]
            time=250
        [/delay]

        {FACE_UNIT (id=Chaos Emperor) (side=1)}

        [delay]
            time=500
        [/delay]

        [message]
            speaker=Galas
            message= _ "So we have. Now it’s down to you and us."
        [/message]

        [message]
            speaker=Chaos Emperor
            message= _ "Not exactly. You allow yourselves to be deceived by appearances."
        [/message]

        [remove_terrain_overlays]
            x=23-45
            y=11-23
            terrain=*^Xo
        [/remove_terrain_overlays]

        [foreach]
            array=initial_onmap_units
            [do]
                [unstore_unit]
                    variable=this_item
                [/unstore_unit]
            [/do]
        [/foreach]

        {CLEAR_VARIABLE initial_onmap_units}

        [delay]
            time=750
        [/delay]

        [scroll_to_unit]
            id=An-Laila
        [/scroll_to_unit]

        [delay]
            time=250
        [/delay]

        [scroll_to_unit]
            id=Mal Myaramor
        [/scroll_to_unit]

        [delay]
            time=250
        [/delay]

        [message]
            speaker=Mal Keshar
            message= _ "Who are you in truth? Reveal your identity, scum! We should at least be allowed to know the name of the wretched creature hiding behind that mask!"
        [/message]

        [message]
            speaker=An-Laila
            message= _ "Hehehehehehehehehe..." # wmllint: no spellcheck
        [/message]

        [message]
            speaker=Mal Myaramor
            message= _ "<b>AH HA HA HA HA HA HA HA HA!</b>"
        [/message]

        [message]
            speaker=Chaos Emperor
            message= _ "Have you not told them yet, Elynia? What do you expect to accomplish by withholding the truth from your allies?"
        [/message]

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

        [message]
            speaker=Galas
            message= _ "Told us what, exactly?"
        [/message]

        {MOVE_UNIT id=Elynia 28 14}

        [delay]
            time=250
        [/delay]

        {FACE_UNIT id=Elynia id=Galas}

        [delay]
            time=250
        [/delay]

        [message]
            speaker=Elynia
            message= _ "Oh, Galas. I am so sorry. All this time, I wanted to believe otherwise... I wanted..."
        [/message]

        [message]
            speaker=Chaos Emperor
            message= _ "To escape the truth is what you wanted."
        [/message]

        [delay]
            time=250
        [/delay]

        {FACE_UNIT id=Elynia (id=Chaos Emperor)}

        [scroll_to_unit]
            id=Chaos Emperor
        [/scroll_to_unit]

        [delay]
            time=750
        [/delay]

        [message]
            speaker=narrator
            image=wesnoth-icon.png
            message= _ "Once he had pronounced those words, the Chaos Emperor reached for his facemask and disconnected the wires that held it in place, one by one. The Lady of Light just stood there, trembling ever so slightly, her eyes concealed by her right hand. Completely ignorant of the thoughts and feelings raging inside her, Galas could only watch as the dark truth unraveled before his eyes."
        [/message]

        [store_direction]
            [from]
                [filter]
                    id=Chaos Emperor
                [/filter]
            [/from]
            [to]
                [filter]
                    id=Elynia
                [/filter]
            [/to]
        [/store_direction]

        [kill]
            id=Chaos Emperor
        [/kill]

        [unit]
            canrecruit=yes
            side=2
            placement=leader
            type=Master of Darkness
            id=Argan
            name= _ "Argan"
            facing=$direction
            variation=unmasked
            {IS_BOSS}
            [modifications]
                {TRAIT_BIOMECHANICAL}
                {TRAIT_RESILIENT}
            [/modifications]
        [/unit]

        [modify_side]
            side=2,8
            user_team_name= _ "team_name^Argan"
        [/modify_side]

        {CLEAR_VARIABLE direction}

        {REPLACE_SCENARIO_MUSIC silence.ogg}

        # 2 seconds total pause

        [fade_out_sound_effects][/fade_out_sound_effects]

        [delay]
            time=1000
        [/delay]

        [message]
            speaker=narrator
            image=wesnoth-icon.png
            sound=break.ogg
            message= _ "And although he quite obviously would have never recognized the distorted visage of the monster otherwise, the silence that followed allowed him to clearly hear Elynia utter his name in her faintest voice."
        [/message]

        [delay]
            time=750
        [/delay]

        {REPLACE_SCENARIO_MUSIC siege_of_laurelmor.ogg}
        {APPEND_MUSIC           the_city_falls.ogg}

        {INCIDENTAL_MUSIC the_dangerous_symphony.ogg}

        [message]
            speaker=Elynia
            message= _ "Why... did this happen... When... Why could I never find you...?"
        [/message]

        [message]
            speaker=Mal Keshar
            message= _ "I cannot... believe this. So my suspicions were true. And you... Elynia... you knew this! You led us here when you knew his identity and never saw fit to reveal it! Why?"
        [/message]

        [message]
            speaker=Elynia
            message= _ "Having heard about Yechnagoth, I tried my hardest to believe that the power that enshrouded the lands of Uria was not his doing, and— and perhaps..."
        [/message]

        {MOVE_UNIT id=Galas 28 13}

        [message]
            speaker=Galas
            message= _ "You are but a foul impostor created by Uria to lead our Lady astray! There is no chance I will ever accept that the Master of Darkness who once protected Irdya has turned to evil! You shall pay for each and every one of your misdeeds!"
        [/message]

        [message]
            speaker=Argan
            message= _ "Oh, but I am very real. While I cannot deny that she made me who I am now, you are deeply mistaken if you think I am not the true Master of Darkness. Even now in these dark times, I continue to look out for Irdya; but instead of wandering the earth as an aimless soul, I follow the path that was laid before us by the Guardian of Life herself. I am her Voice, and I carry out her will for the sake of our future."
        [/message]

        [message]
            speaker=Argan
            message= _ "Elynia, have you not wondered yourself countless times whether it was destiny or a true coincidence that made our paths cross on Irdya? Did you not ask me once why we, mere mortals, had been blessed with an ultimate power beyond our comprehension? Why, even now that you have witnessed how by Uria’s grace I overcame the limits of my frail vessel, you continue to renounce her will? We were born to be together, to fight together, to save this doomed reality together! What should I do to persuade you to embrace our glorious destiny under her command?"
        [/message]

        [delay]
            time=750
        [/delay]

        [message]
            speaker=Argan
            message= _ "Or perhaps, you truly believe I am an impostor like this elf claims."
        [/message]

        [delay]
            time=750
        [/delay]

        [message]
            speaker=Elynia
            message= _ "No, Argan. I believe you. I have always believed you. I believe you even now, at the end. I loved you, and I still love you."
        [/message]

        [delay]
            time=1500
        [/delay]

        [message]
            speaker=Elynia
            message= _ "Which is why I must put an end to your suffering."
        [/message]

        [fade_in_sound_effects][/fade_in_sound_effects]

        [message]
            speaker=Argan
            message= _ "I see. If there is nothing I can do to make you accept the truth, then you will have to die as well. As much as it pains me to even consider the possibility... Uria’s will reigns supreme."
        [/message]

        [message]
            speaker=Mal Myaramor
            message= _ "One last chance to retract your words, unbelievers!"
        [/message]

        [delay]
            time=750
        [/delay]

        [message]
            speaker=An-Laila
            message= _ "O Voice that speaks the will of the Mother of Urvatha, what is your command?"
        [/message]

        [message]
            speaker=Argan
            message= _ "Kill them."
        [/message]

        [message]
            speaker=Argan
            message= _ "Kill them all."
        [/message]

        {GENERIC_UNIT 2 (Blood Imp) 33 18} {NO_UPKEEP_NO_OVERLAY} {FACING nw}
        {GENERIC_UNIT 2 (Blood Imp) 37 16} {NO_UPKEEP_NO_OVERLAY} {FACING nw}

        {GENERIC_UNIT 2 (Shaxthal Rayblade) 35 18} {NO_UPKEEP_NO_OVERLAY} {FACING nw}
        {GENERIC_UNIT 2 (Shaxthal Rayblade) 37 17} {NO_UPKEEP_NO_OVERLAY} {FACING nw}

        {GENERIC_UNIT 2 (Gutwrencher Imp) 37 20} {NO_UPKEEP_NO_OVERLAY} {FACING nw}
        {GENERIC_UNIT 2 (Gutwrencher Imp) 41 18} {NO_UPKEEP_NO_OVERLAY} {FACING nw}

#ifdef HARD
        {GENERIC_UNIT 2 (Hell Overlord) 39 19} {NO_UPKEEP_NO_OVERLAY} {FACING nw}
#endif

        {GENERIC_UNIT 5 (Demon Zephyr) 25 15} {NO_UPKEEP_NO_OVERLAY} {FACING ne}
        {GENERIC_UNIT 5 (Demon Zephyr) 29 17} {NO_UPKEEP_NO_OVERLAY} {FACING nw}

        {GENERIC_UNIT 6 (Necrophage) 31 12} {NO_UPKEEP_NO_OVERLAY} {FACING sw}
        {GENERIC_UNIT 6 (Necrophage) 35 14} {NO_UPKEEP_NO_OVERLAY} {FACING sw}

        [recall_all]
            side=1
        [/recall_all]

        {OBJECTIVES (
            {OBJECTIVE_VICTORY ( _ "Kill Argan, the Master of Darkness, and put an end to his sinister rule over Irdya")}

            {OBJECTIVE_DEFEAT ( _ "Death of Galas")}
            {OBJECTIVE_DEFEAT ( _ "Death of Elynia")}
            {OBJECTIVE_DEFEAT ( _ "Death of Mal Keshar")}
            {OBJECTIVE_DEFEAT ( _ "Death of Lédinor")}

            {TURNS_RUN_OUT}

            {OBJECTIVE_FULL_CARRYOVER}

            {OBJECTIVE_NOTE ( _ "Mal Keshar’s presence should allow you to recruit undead once again if you can capture an enemy keep")}
        )}

        {RESET_THEME}

        {UNLOCK_VIEW}
    [/event]

    [event]
        name=last breath
        [filter]
            id=Mal Myaramor
        [/filter]

        [message]
            speaker=Mal Myaramor
            message= _ "Thus you have sealed your fate! Ack—"
        [/message]
    [/event]

    [event]
        name=last breath
        [filter]
            id=An-Laila
        [/filter]

        [message]
            speaker=An-Laila
            message= _ "Go on, destroy the container of my soul if you must. As long as Uria exists, I can be brought back..."
        [/message]
    [/event]

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

    [event]
        name=turn 2

        [message]
            speaker=Argan
            message= _ "Why did it have to come to this, my beloved Elynia? Why must we face each other as enemies now, after spending so many thousands of years apart?"
        [/message]

        [message]
            speaker=Elynia
            message= _ "Oh, right! Are you saying I shouldn’t take all the attempts on my life by your subjects as a sign of hostility? Or that I should allow you to continue this new pitiful existence of yours?"
        [/message]

        [message]
            speaker=Argan
            message= _ "You say that as though you have not let that mediocre necromancer become an ally and friend of yours. Is his existence in undeath not far more pitiful than mine? My body was not just rebuilt, but also improved! I can understand your distrust of this new form of technology, but no-one will force you to change your beautiful body if you don’t wish to transcend its limitations — as long as you devote your life and soul to our one true goddess."
        [/message]

        [message]
            speaker=Mal Keshar
            message= _ "Exactly who are you calling mediocre, you miserable piece of—"
        [/message]

        [message]
            speaker=Elynia
            message= _ "If this goddess of yours truly cared for all life on Irdya, she would not be using you to destroy it! Are you not the Chaos Emperor? Did you not command your legions to raze and desecrate the earth?! How can you... how can you speak as if all this destruction were for the greater good!?"
        [/message]

        [message]
            speaker=Argan
            # po: He's calling Elynia "my love", in case it's not obvious.
            message= _ "If only you could understand the ineffable designs of our goddess, my love. If only you allowed me to show you the way..."
        [/message]
    [/event]

    [event]
        name=side 1 turn 2 end

        [message]
            speaker=Argan
            message= _ "Every day and every night without you has been a nightmare..."
        [/message]
    [/event]

    [event]
        name=side 2 turn 2 end

        [message]
            speaker=Argan
            message= _ "I hoped that the nightmare would end tonight. But no, you had to choose the path of the infidels. And on top of that, you had to choose <i>him</i>, of all creatures — a lowly mundane elf of the forest with no remarkable qualities whatsoever."
        [/message]

        [message]
            speaker=Elynia
            message= _ "How dare <i>you</i>, of all people, appeal to jealousy now after all we’ve been through?! And in any case, he is nothing but my friend!"
        [/message]
    [/event]

    #
    # Mal Keshar can bring the player's fallen in this scenario back as undead.
    #
    {PLAYER_RISE_UP}

    #
    # Can't hit Argan before he disables Union on Mal Keshar (and then he
    # becomes invulnerable).
    #
    {FORCE_CHANCE_TO_HIT () id=Argan 0 ({VARIABLE_NUMERICAL_LESS_THAN turn_number 4})}

    #
    # This isn't a general unpetrify action macro, this is only intended to
    # help with displaying floating text and inserting a delay for unpetrifying
    # units about to be possessed by Argan. (TODO: actually implement this.)
    #
#define L_UNPETRIFY_VICTIM _SUF
    [unpetrify]
        {_SUF}
    [/unpetrify]
#enddef

#define L_POSSESS_PLAYER_UNIT _SUF
    [fire_event]
        name=possess unit helper
        [primary_unit]
            {_SUF}
        [/primary_unit]
    [/fire_event]
#enddef

    [event]
        name=possess unit helper
        first_time_only=no

        [floating_text]
            [filter]
                x,y=$x1,$y1
            [/filter]
            text="<span color='#f00'>!</span>" # wmllint: ignore
        [/floating_text]

        [repeat]
            times=5
            [do]
                [delay]
                    time=250
                [/delay]

                [modify_unit]
                    [filter]
                        x,y=$x1,$y1
                    [/filter]
                    side=1
                [/modify_unit]

                [delay]
                    time=250
                [/delay]

                [modify_unit]
                    [filter]
                        x,y=$x1,$y1
                    [/filter]
                    side=2
                [/modify_unit]
            [/do]
        [/repeat]

        [modify_unit]
            [filter]
                x,y=$x1,$y1
            [/filter]
            # If the unit isn't at full HP, regenerate up to 30%/45%/60% HP.
            # NOTE: second argument is rounded because min()/max() get
            #       completely confused by decimal values.
            hitpoints="$(max($this_unit.hitpoints, round({DIFF 0.30 0.45 0.60}*$this_unit.max_hitpoints)))"
        [/modify_unit]
    [/event]

    [event]
        name=turn 4

        {LOCK_VIEW}

        [fade_out_music][/fade_out_music]

        {REPLACE_SCENARIO_MUSIC frantic.ogg}

        [message]
            speaker=Argan
            message= _ "Your ploy to draw power from me and into the lich to gain control of the Union was quite ingenious. I would have never expected any less from you, my love. Still, I fear that I must revoke your magic privileges before anyone else gets hurt."
        [/message]

        [delay]
            time=750
        [/delay]

        {L_UNPETRIFY_VICTIM (id=Mal Keshar)}

        [message]
            speaker=Mal Keshar
            message= _ "ARGH! He’s reaching into my mind!"
        [/message]

        [message]
            speaker=Galas
            message= _ "Malin!"
        [/message]

        [object]
            silent=yes
            duration=forever
            [filter]
                id=Mal Keshar
            [/filter]
            # Don't let Elynia use the Union with him...
            [effect]
                apply_to=remove_ability
                [abilities]
                    {ABILITY_UNION_ID}
                [/abilities]
            [/effect]
            # Or him with Elynia... (but we want to allow HER to use it later!)
            [effect]
                apply_to=remove_attacks
                name=union
            [/effect]
        [/object]

        {L_POSSESS_PLAYER_UNIT (id=Mal Keshar)}

        {REMOVE_RECRUIT_LIST 1}

        [delay]
            time=750
        [/delay]

        [message]
            speaker=Mal Keshar
            message= _ "I cannot... control my body..."
        [/message]

#ifndef EASY
        [message]
            speaker=Argan
            message= _ "You people surely would not murder your own allies, right?"
        [/message]

        [if]
            [have_unit]
                id=Erathan
            [/have_unit]
            [then]
                {L_UNPETRIFY_VICTIM id=Erathan}

                [message]
                    speaker=Erathan
                    message= _ "Argh!!! Get out of... my head!!!"
                [/message]

                {L_POSSESS_PLAYER_UNIT id=Erathan}
            [/then]
            [else]
                {L_UNPETRIFY_VICTIM id=Galas}

                [message]
                    speaker=Galas
                    message= _ "Elynia! He... he’s doing it again... NO!!!"
                [/message]

                [modify_unit]
                    [filter]
                        id=Elynia
                    [/filter]
                    canrecruit=yes
                    {NO_OVERLAYS_NO_ELLIPSE}
                [/modify_unit]

                [modify_unit]
                    [filter]
                        id=Galas
                    [/filter]
                    canrecruit=no
                    {IS_HERO}
                [/modify_unit]

                {L_POSSESS_PLAYER_UNIT id=Galas}

                [message]
                    speaker=Lédinor
                    message= _ "Galas! It cannot end like this — you must fight back!"
                [/message]
            [/else]
        [/if]
#endif

        [message]
            speaker=Argan
            message= _ "I really do not want to do this to you, Elynia, and I am sure you would be unable to live with yourself if your friends were to perish because of one single misguided choice. I offer you one more chance to accept Uria as our true protector—"
        [/message]

        [message]
            speaker=Elynia
            message= _ "Never! Do you really believe I would choose two people over the fate of Irdya? Who do you take me for?!"
        [/message]

        {OBJECTIVES (
            {OBJECTIVE_VICTORY ( _ "Use Elynia to destroy Mal Keshar")}

            {OBJECTIVE_DEFEAT ( _ "Death of Galas")}
            {OBJECTIVE_DEFEAT ( _ "Death of Elynia")}
            {OBJECTIVE_DEFEAT ( _ "Death of Mal Keshar at the hands of any other unit")}
            {OBJECTIVE_DEFEAT ( _ "Death of Lédinor")}

            {TURNS_RUN_OUT}

            {OBJECTIVE_FULL_CARRYOVER}
        )}

        {UNLOCK_VIEW}
    [/event]

    #
    # Argan can recruit Cockatrices from turn 6 onwards.
    #

    [event]
        name=turn 6

        [allow_recruit]
            side=2
            type=Cockatrice
        [/allow_recruit]

        [disallow_recruit]
            side=2
            type=Goliath,Automaton
        [/disallow_recruit]
    [/event]

    [event]
        name=petrified
        [filter]
            id=Elynia
        [/filter]

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

        [delay]
            time=250
        [/delay]
    [/event]

    [event]
        name=petrified
        first_time_only=no
        [filter]
            id=Elynia
        [/filter]

        [unpetrify]
            id=Elynia
        [/unpetrify]

        [floating_text]
            [filter]
                id=Elynia
            [/filter]
            text="<span color='#0f0'>"+_"female^unpetrified"+"</span>" # wmllint: ignore
        [/floating_text]
    [/event]

    [event]
        name=petrified
        [filter]
            id=Elynia
        [/filter]

        [message]
            speaker=Elynia
            message= _ "— I <b>don’t</b> have time for <b>THIS!</b>"
        [/message]

        [message]
            speaker=Argan
            message= _ "Oh... Oh. You learned to block their spell. That is such a pity — I had hoped Uria would at least allow me to keep you as a garden ornament."
        [/message]
    [/event]

    {UNPETRIFY_UNITS_AT_TURN_START 1 id=Elynia}

    #
    # On Normal and higher difficulties, while Argan controls Mal Keshar he can
    # recall undead found among the unused recalls from S21.
    #

#ifndef EASY
    [event]
        name=side 2 turn refresh
        first_time_only=no
        [filter_condition]
            [have_unit]
                id=Mal Keshar
                side=2
            [/have_unit]
        [/filter_condition]

        [for]
            array=recall_list
            variable=k
            [do]
                [if]
                    [true][/true]
                    [and]
                        {VARIABLE_NUMERICAL_EQUALS recall_list[$k].level 2}
#ifdef HARD
                        [or]
                            {VARIABLE_NUMERICAL_EQUALS recall_list[$k].level 3}
                        [/or]
#endif
                    [/and]
                    [and]
                        {VARIABLE_LEXICAL_EQUALS recall_list[$k].race undead}
                        [or]
                            {VARIABLE_LEXICAL_EQUALS recall_list[$k].race bats}
                        [/or]
                    [/and]
                    [then]
                        [fire_event]
                            name=argan recalls undead
                        [/fire_event]

                        [unit]
                            animate=yes
                            random_traits=no
                            generate_name=no
                            random_gender=no

                            side=2
                            placement=leader

                            id=$recall_list[$k].id
                            name=$recall_list[$k].name
                            type=$recall_list[$k].type
                            gender=$recall_list[$k].gender
                            variation=$recall_list[$k].variation
                            experience=$recall_list[$k].experience
                            max_experience=$recall_list[$k].max_experience

                            # FIXME: Yeah, this will also include items and AMLAs.
                            #        Do I want to change that? Not sure. It's a
                            #        reasonable punishment.
                            [insert_tag]
                                name=modifications
                                variable=recall_list[$k].modifications
                            [/insert_tag]
                        [/unit]

                        {CLEAR_VARIABLE recall_list[$k]}

                        [break][/break]
                    [/then]
                [/if]
            [/do]
        [/for]
    [/event]
#endif

    [event]
        name=argan recalls undead

        [message]
            speaker=Argan
            message= _ "Perhaps you will learn to not regard the undead as companions now that you realize how easy it is to turn them against their masters."
        [/message]
    [/event]

    # Mal Keshar and Galas are fighting back.
    {FORCE_CHANCE_TO_HIT (id=Galas,Mal Keshar) side=1 {DIFF 10 20 30} ()}

    #
    # Enemy Galas and Erathan’s death events.
    #

    [event]
        name=last breath
        [filter]
            id=Galas
            side=2
        [/filter]

        [message]
            speaker=Galas
            message= _ "Why... did it have to end like this..."
        [/message]

        [message]
            speaker=Elynia
            message= _ "No, Galas!"
        [/message]
    [/event]

    [event]
        name=die
        [filter]
            id=Galas
            side=2
        [/filter]

        {HERO_DEFEAT_ENDLEVEL}
    [/event]

    [event]
        name=last breath
        [filter]
            id=Erathan
            side=2
        [/filter]

        [message]
            speaker=Erathan
            message= _ "So this is the end of the road for me..."
        [/message]
    [/event]

    #
    # Enemy Mal Keshar's death events. He's supposed to be killed by Elynia
    # only rather than anyone else to prevent player defeat.
    #

    [event]
        name=last breath
        [filter]
            id=Mal Keshar
            side=2
        [/filter]

        [message]
            speaker=Mal Keshar
            message= _ "Oh, the sweet, cold embrace of death... it has been far too long..."
        [/message]
    [/event]

    [event]
        name=die
        [filter]
            id=Mal Keshar
            side=2
        [/filter]
        [filter_second]
            [not]
                id=Elynia
            [/not]
        [/filter_second]

        {HERO_DEFEAT_ENDLEVEL}
    [/event]

    [event]
        name=last breath
        [filter]
            id=Mal Keshar
        [/filter]
        [filter_second]
            id=Elynia
        [/filter_second]

        {LOCK_VIEW}

        {FACE_UNIT id=Elynia id=Argan}

        [message]
            speaker=Elynia
            message= _ "Our foolishness allowed the Warlord to take one of our friends away from us. But I will not let you take advantage of us again, Argan. Not today!"
        [/message]

        {FACE_UNIT id=Argan id=Elynia}

        # Go bright.

        {FADE_STEP 32 25}

        {FADE_STEP 64 25}

        {FADE_STEP 96 25}

        {FADE_STEP 128 25}

        [harm_unit]
            [filter]
                id=Argan
            [/filter]
            fire_event=no
            kill=no
            amount=16
            animate=yes
        [/harm_unit]

        [message]
            speaker=Argan
            message= _ "No! This light... I cannot—"
        [/message]

        {FADE_STEP 160 25}

        {FADE_STEP 192 25}

        {FADE_STEP 224 25}

        {FADE_STEP 256 25}

        [hide_unit][/hide_unit]

        {FADE_STEP 280 25}

        {FADE_STEP 340 25}

        {FADE_STEP 500 25}

        # The "Voice of Uria" isn't just a fancy title, you know.

        [message]
            speaker=Argan
            caption= _ "caption^Argan?"
            # po: Every Argan line with the female^ prefix is actually spoken
            # po: by Uria herself. The prefix is a hint for translations were
            # po: gendered speaker syntax is required (e.g. personal pronouns).
            message="<span color='#fd5'>"+_ "female^<b>STOP HER!</b>"+"</span>"
        [/message]

        [delay]
            time=1000
        [/delay]

        {RESET_SCREEN}

        [kill]
            [not]
                id=Argan
            [/not]
            [not]
                side=1
            [/not]
            [not]
                canrecruit=yes
            [/not]
            [filter_adjacent]
                id=Elynia
            [/filter_adjacent]

            animate=no
            fire_event=no
        [/kill]

        [unhide_unit][/unhide_unit]

        {QUAKE explosion-big.ogg}

        [message]
            speaker=Argan
            caption= _ "caption^Argan?"
            message="<span color='#fd5'>"+_ "female^No... more... fooling around."+"</span>"
        [/message]

        [message]
            speaker=Lédinor
            message= _ "I... I recognize that voice..."
        [/message]

        [if]
            [have_unit]
                id=Galas
                side=2
            [/have_unit]
            [then]
                [modify_unit]
                    [filter]
                        id=Elynia
                    [/filter]
                    canrecruit=no
                    {IS_HERO}
                [/modify_unit]

                [modify_unit]
                    [filter]
                        id=Galas
                    [/filter]
                    side=1
                    canrecruit=yes
                    {NO_OVERLAYS_NO_ELLIPSE}
                [/modify_unit]

                [message]
                    speaker=Galas
                    message= _ "Elynia... You saved me again..."
                [/message]

                [message]
                    speaker=Elynia
                    message= _ "I’m sorry it took me so long, Galas, but with Mal Keshar under his control..."
                [/message]
            [/then]
        [/if]

        [message]
            speaker=Galas
            message= _ "Mal Keshar! Is he... gone?"
        [/message]

        [message]
            speaker=Elynia
            message= _ "If he truly is as powerful as he likes to make himself out to be, there might be a chance his soul is still intact, but whether it will return to this body..."
        [/message]

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

        {UNLOCK_VIEW}
    [/event]

    #
    # Argan absorbs all damage throughout stage 1 once it's possible to land
    # hits on him (i.e. once the player can no longer cast the Union on him).
    #

    {BOSS_ABSORB_FULL_DAMAGE Argan ({VARIABLE_NUMERICAL_EQUALS boss_stage 1})}

    #
    # Can't die on stages 1 or 2!
    #
    [event]
        name=die
        [filter]
            id=Argan
        [/filter]
        [filter_condition]
            {VARIABLE_NUMERICAL_LESS_THAN boss_stage 3}
        [/filter_condition]

        {BUG ("Boss died unexpectedly on stage $boss_stage!")}
    [/event]

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

    [event]
        name=boss stage 2

        {LOCK_VIEW}

        {VARIABLE boss_stage 2}

        {REPLACE_SCENARIO_MUSIC orc_theme.ogg}

        [object]
            silent=yes
            duration=forever
            [filter]
                id=Argan
            [/filter]

            {UNION_EFFECTS}

            {UNION_ATTACK_STATS {DIFF 19 20 22} 2}
        [/object]

        [floating_text]
            [filter]
                id=Argan
            [/filter]
            # Yep, red this time!
            text="<span color='#f00'>"+_"union"+"</span>" # wmllint: ignore
        [/floating_text]

        [message]
            speaker=Argan
            message= _ "You made a fatal mistake in coming to Inferno, Elynia. As long as you are in the same realm as I, I too can use the Union against you."
        [/message]

        [message]
            speaker=Elynia
            message= _ "(<i>defiant</i>) Then one of us must die first."
        [/message]

        [message]
            speaker=Argan
            caption= _ "caption^Argan?"
            message="<span color='#fd5'>"+_ "female^Truly, it is a magnificent power. It is a shame that you will not surrender it to me willingly, Lady of Light."+"</span>"
        [/message]

        [object]
            silent=yes
            [filter]
                id=Elynia
            [/filter]

            [effect]
                apply_to=max_hitpoints
                increase_total=10%
                heal_full=yes
            [/effect]

            # 28-3 -> 34-4
            [effect]
                apply_to=attack
                name=union
                increase_attacks=1
                increase_damage=6
            [/effect]
        [/object]

        {OBJECTIVES (
            {OBJECTIVE_VICTORY ( _ "Kill Argan, the Master of Darkness, and put an end to his sinister rule over Irdya")}

            {OBJECTIVE_DEFEAT ( _ "Death of Galas")}
            {OBJECTIVE_DEFEAT ( _ "Death of Elynia")}
            {OBJECTIVE_DEFEAT ( _ "Death of Mal Keshar")}
            {OBJECTIVE_DEFEAT ( _ "Death of Lédinor")}

            {TURNS_RUN_OUT}

            {OBJECTIVE_FULL_CARRYOVER}

            # TODO: Perhaps only include this note on Easy/Normal difficulty?
            #       I don't know.
            {OBJECTIVE_NOTE ( _ "Elynia is able to use the Union without Mal Keshar if she is adjacent to Argan, although doing so puts her at risk")}
        )}

        {UNLOCK_VIEW}
    [/event]

    #
    # In this stage Argan can only take full damage from the Union. Other forms
    # of damage will be absorbed. However, because he has a lot more HP than
    # Elyssa and we need to bring him down to 0%, we only want him to
    # regenerate a percentage of the inflicted damage rather than always go
    # back to 100% on each hit (the most common gameplay complaint about legacy
    # IftU).
    #
    # Some code in these three event handlers is supposed to match the
    # equivalent in BOSS_ABSORB_DAMAGE_EX.
    #

    [event]
        name=argan absorb damage
        first_time_only=no
        [filter_second_attack]
            [not]
                special=union
            [/not]
        [/filter_second_attack]

        [set_variables]
            name=unit
            mode=merge
            [value]
                # HACK: cross-event variable!
                hitpoints={BOSS_REGEN_ATTACK_DAMAGE {DIFF 0.60 0.85 0.90}}
            [/value]
        [/set_variables]

        # Argan can't remove negative statuses. The justification is that he is
        # not as competent a fighter as Elyssa or Elynia.

        [unstore_unit]
            find_vacant=no
            variable=unit
            {COLOR_HEAL}
            text= _ "absorbed damage" # wmllint: ignore
        [/unstore_unit]
    [/event]

    [event]
        name=attacker hits
        first_time_only=no
        [filter_second]
            id=Argan
        [/filter_second]
        [filter_condition]
            {VARIABLE_NUMERICAL_EQUALS boss_stage 2}
        [/filter_condition]

        [fire_event]
            name=argan absorb damage
            {PROPAGATE_FULL_EVENT_CONTEXT_INVERTED}
        [/fire_event]
    [/event]

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

        [fire_event]
            name=argan absorb damage
            {PROPAGATE_FULL_EVENT_CONTEXT}
        [/fire_event]
    [/event]

    #
    # Periodically spawn Psy Crawlers from spires.
    #

    [event]
        name=side 5 turn refresh
        first_time_only=no
        [filter_condition]
            {VARIABLE_NUMERICAL_GREATER_THAN_OR_EQUAL boss_stage 2}
        [/filter_condition]

        {VARIABLE     temp_should_spawn turn_number}
        {VARIABLE_MOD temp_should_spawn {DIFF 8 6 4}}

        [if]
            {VARIABLE_NUMERICAL_EQUALS temp_should_spawn 0}
            [then]
                [count_units]
                    [filter_wml]
                        [variables]
                            auto_spawned=yes
                        [/variables]
                    [/filter_wml]
                [/count_units]

                [store_locations]
                    [filter]
                        type=Verlissh Matrix Flow System
                    [/filter]
                    variable=spire_locs
                [/store_locations]

                [foreach]
                    array=spire_locs
                    variable=spire_loc
                    [do]
                        # Chance of not spawning on lower difficulties.
                        {VARIABLE_RANDOM temp_should_spawn {DIFF 0..1 0..2 1..2}}

                        [repeat]
                            times=$temp_should_spawn
                            [do]
                                [unit]
                                    animate=yes
                                    side=$side_number
                                    type=Psy Crawler

                                    random_traits=yes
                                    upkeep=free
                                    moves=0
                                    attacks_left=0

                                    x,y=$spire_loc.x,$spire_loc.y
                                    passable=yes

                                    [variables]
                                        auto_spawned=yes
                                    [/variables]
                                [/unit]

                                {VARIABLE_INC unit_count}
                            [/do]
                        [/repeat]

                        # Population cap according to difficulty.
                        [if]
                            {VARIABLE_NUMERICAL_GREATER_THAN_OR_EQUAL unit_count {DIFF 4 6 9} }
                            [then]
                                [break][/break]
                            [/then]
                        [/if]
                    [/do]
                [/foreach]
            [/then]
        [/if]

        {CLEAR_VARIABLE temp_should_spawn,spire_locs,unit_count}
    [/event]

    #
    # Advance to stage 3 once Argan goes down to 0 HP from the Union.
    #

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

        # Save Argan from certain death before proceeding. This is really just
        # a safe-guard against potential engine changes to cross-event victory
        # check semantics (and also because I don't want to test this).
        {VARIABLE unit.hitpoints $unit.max_hitpoints}

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

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

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

    [event]
        name=boss stage 3

        {LOCK_VIEW}

        {VARIABLE boss_stage 3}

        # Lend the player a hand for what follows next.
        [object]
            silent=yes
            [filter]
                id=Elynia
            [/filter]

            # 34-4 -> 36-5
            [effect]
                apply_to=attack
                name=union
                increase_attacks=1
                increase_damage=2
            [/effect]

            [effect]
                apply_to=hitpoints
                increase_total=10%
                heal_full=yes
            [/effect]
        [/object]

        # YOU ARE TEARING ME APART ELYNIA!
        [message]
            speaker=Argan
            message= _ "You are... hurting me..."
        [/message]

        [delay]
            time=750
        [/delay]

        [message]
            speaker=Argan
            message= _ "Elynia... Why do you do this?"
        [/message]

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

        [message]
            speaker=Argan
            message= _ "We were meant to be together... for the rest of eternity... Do you not love me anymore, Elynia?"
        [/message]

        [message]
            speaker=Elynia
            message= _ "You... You’ve become a monster! An abomination! Do you really not realize what you’ve done, what... what Uria has done to you?"
        [/message]

        {BUG_ON (
            [have_unit]
                id=Mal Keshar
            [/have_unit]
        ) ("Unexpected Mal Keshar found on map!")}

        #
        # Mal Keshar is back! Without the Union and any items he might've had
        # before, anyway. Intentionally. Mostly.
        #

        [scroll_to_unit]
            id=Galas
        [/scroll_to_unit]

        {RECALL_MAL_KESHAR}
        [+unit]
            placement=leader
            passable=yes
            animate=yes
            [+modifications]
                {TRAIT_DETERIORATED_IFTU}
            [/modifications]
        [/unit]

        {F_PLAYER_CAN_RECRUIT_UNDEAD}

        [delay]
            time=750
        [/delay]

        {FACE_UNIT (id=Mal Keshar) id=Argan}

        [message]
            speaker=Mal Keshar
            message= _ "Excuse me, did I miss anything? ... Oh! I guess you haven’t managed to strike down that pompous fool yet. Good, good."
        [/message]

        [message]
            speaker=Galas
            message= _ "Mal Keshar! Thank the Lords of Light — I thought I would never hear you speak again. Do you think you can still help us somehow?"
        [/message]

        [message]
            speaker=Mal Keshar
            message= _ "Help you? HA! Of course I can help you! It will take much more than some unfathomable ancient power to put me out of commission, boy."
        [/message]

        [fade_out_music][/fade_out_music]

        [message]
            speaker=Argan
            caption= _ "caption^Argan?"
            message="<span color='#fd5'>"+_ "female^Put an end... to this... <b>NOW!</b>"+"</span>"
        [/message]

        # Supercharge Argan!
        [object]
            silent=yes
            duration=forever
            [filter]
                id=Argan
            [/filter]

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

            [effect]
                apply_to=hitpoints
                increase_total=40%
                heal_full=yes
            [/effect]

            [effect]
                apply_to=attack
                name=noctum
                # Final base stats: 14-6
                increase_attacks=1
                increase_damage=1
            [/effect]

            [effect]
                apply_to=attack
                name=infernal chill
                # Final base stats: 11-5
                increase_attacks=1
                [set_specials]
                    mode=append
                    {WEAPON_SPECIAL_SLOW}
                [/set_specials]
            [/effect]

            [effect]
                apply_to=variation
                name=bloated
            [/effect]
        [/object]

        [floating_text]
            [filter]
                id=Argan
            [/filter]
            text="<span color='#f00'>!</span>" # wmllint: ignore
        [/floating_text]

        [delay]
            time=1000
        [/delay]

        {REPLACE_SCENARIO_MUSIC vengeful.ogg}

        {UNLOCK_VIEW}
    [/event]

    #
    # Argan has a chance to spawn drones and worms every time he takes damage.
    #

    [event]
        name=attacker hits
        first_time_only=no
        [filter_second]
            id=Argan
        [/filter_second]
        [filter_condition]
            {VARIABLE_NUMERICAL_GREATER_THAN_OR_EQUAL boss_stage 3}
        [/filter_condition]

        [fire_event]
            name=argan took damage
            {PROPAGATE_FULL_EVENT_CONTEXT_INVERTED}
        [/fire_event]
    [/event]

    [event]
        name=defender hits
        first_time_only=no
        [filter]
            id=Argan
        [/filter]
        [filter_condition]
            {VARIABLE_NUMERICAL_GREATER_THAN_OR_EQUAL boss_stage 3}
        [/filter_condition]

        [fire_event]
            name=argan took damage
            {PROPAGATE_FULL_EVENT_CONTEXT}
        [/fire_event]
    [/event]

    [event]
        name=argan took damage
        first_time_only=no

        {VARIABLE_RANDOM temp_should_spawn 0..100}
        {VARIABLE_MOD    temp_should_spawn {DIFF 25 10 5} }

        [if]
            {VARIABLE_NUMERICAL_EQUALS temp_should_spawn 0}
            {VARIABLE_NUMERICAL_GREATER_THAN unit.hitpoints 0}
            [then]
                [fire_event]
                    name=argan damage spawn
                [/fire_event]
            [/then]
        [/if]

        {CLEAR_VARIABLE temp_should_spawn}
    [/event]

    [event]
        name=argan damage spawn
        first_time_only=no

#ifndef EASY
        {VARIABLE_RANDOM temp_spawn_type "Shaxthal Drone,Shaxthal Worm,Shaxthal Worm,Shaxthal Worm,Shaxthal Worm"}
#else
        {VARIABLE        temp_spawn_type "Shaxthal Worm"}
#endif
        {VARIABLE_RANDOM temp_spawn_count {DIFF 1 1..2 2} }

        [repeat]
            times=$temp_spawn_count
            [do]
                [if]
                    {VARIABLE_LEXICAL_EQUALS temp_spawn_type "Shaxthal Worm"}
                    [then]
                        {VARIABLE_RANDOM temp_spawn_variation a,b,c}
                    [/then]
                    [else]
                        {VARIABLE temp_spawn_variation ()}
                    [/else]
                [/if]

                [unit]
                    side=2
                    animate=yes
                    type=$temp_spawn_type
                    variation=$temp_spawn_variation
                    placement=leader
                    random_traits=yes
                    [modifications]
                        [object]
                            silent=yes
                            [effect]
                                apply_to=image_mod
                                add="BLEND(255,0,0,25%)"
                            [/effect]
                        [/object]
                    [/modifications]
                [/unit]
            [/do]
        [/repeat]

        {CLEAR_VARIABLE temp_spawn_type,temp_spawn_count,temp_spawn_variation}
    [/event]

    [event]
        name=argan damage spawn

        [message]
            speaker=Lédinor
            message= _ "Look out! Those creatures emerge from his body with each hit!"
        [/message]
    [/event]

    #
    # Argan can take hits from everyone for this stage but Elynia must be the
    # one to inflict the killing blow.
    #

    {BOSS_IMMORTALITY Argan ({VARIABLE_NUMERICAL_EQUALS boss_stage 3}) {DIFF 25 40 50}}

    [event]
        name=boss revived

        [message]
            speaker=Argan
            message= _ "Energy... Yes... Feed me more energy..."
        [/message]
    [/event]

    [event]
        name=last breath
        [filter]
            id=Argan
        [/filter]
        [filter_condition]
            {VARIABLE_NUMERICAL_EQUALS boss_stage 3}
            # HACK: Prevent chaining along with the immortality handler.
            {VARIABLE_NUMERICAL_LESS_THAN unit.hitpoints 1}
        [/filter_condition]

        {LOCK_VIEW}

        {CLEAR_LABELS}

        # Precondition checks.

        {BUG_ON ({VARIABLE_LEXICAL_NOT_EQUALS second_unit.id Elynia}) ("Boss died to an unexpected unit with id $second_unit.id (cheating?)")}

        {BUG_ON (
            [not]
                [have_unit]
                    id=Elynia
                    [filter_adjacent]
                        id=Argan
                    [/filter_adjacent]
                [/have_unit]
            [/not]
        ) ("Boss and Elynia not adjacent")}

        [message]
            speaker=Argan
            message= _ "No... Noooooo!!! My power... falters..."
        [/message]

        [fade_out_music]
            duration=500
        [/fade_out_music]

        {QUAKE ()}

        {QUAKE (cave-in.ogg)}

        {QUAKE ()}

        {QUAKE (explosion.ogg)}

        {QUAKE ()}

        {QUAKE (explosion-big.ogg)}

        [message]
            speaker=Elynia
            message= _ "It’s— it’s tearing me apart—"
        [/message]

        {QUAKE (cave-in.ogg)}

        {QUAKE ()}

        [kill]
            side=2
            [not]
                id=Argan
            [/not]
            [filter_adjacent]
                id=Argan,Elynia
            [/filter_adjacent]

            animate=yes
            fire_event=yes
        [/kill]

        # Erathan exit stage left.

        [kill]
            id=Erathan
            animate=yes
            fire_event=yes
        [/kill]

        {QUAKE_HEAVY (rumble.ogg)}

        #
        # Orb splitting animation.
        #
        # A precondition we don't actually verify here is that Elynia and Argan
        # *must* be facing each other. This will always happen if this event is
        # triggered normally (Elynia deals the killing blow), but bugs and
        # cheating can break things.
        #
        # NOTE 2015-10-16: okay now we check this at the start of the EH.
        #

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

        [store_unit]
            [filter]
                id=Argan
            [/filter]
            kill=no
            variable=argan_store
        [/store_unit]

        [store_relative_location]
            [from]
                x,y=$argan_store.x,$argan_store.y
            [/from]
            distance=3
            direction=$elynia_store.facing
            variable=orbd_loc
        [/store_relative_location]

        [store_relative_location]
            [from]
                x,y=$elynia_store.x,$elynia_store.y
            [/from]
            distance=3
            direction=$argan_store.facing
            variable=orbl_loc
        [/store_relative_location]

        [floating_text]
            [filter]
                id=Argan
            [/filter]
            text="<span color='#f00'>!</span>" # wmllint: ignore
        [/floating_text]

        [sound]
            name=union-powerdown.ogg
        [/sound]

        [move_unit_fake]
            side=1
            type=Union Orb
            variation=tenebrae
            x=$argan_store.x,$orbd_loc.x
            y=$argan_store.y,$orbd_loc.y
            force_scroll=no
        [/move_unit_fake]

        #
        # Downgrade Elynia to her AtS E1/E2 unit type. She loses all her items
        # in the process like Mal Keshar above, but meh. Probably for the best.
        #

        [kill]
            id=Elynia
        [/kill]

        {RECALL_ELYNIA_AT $elynia_store.x $elynia_store.y}
        [+unit]
            facing=$elynia_store.facing
            type=Sylvan Warden
            moves,attacks_left=0,0 # She just attacked Argan this turn, right?
        [/unit]

        [floating_text]
            [filter]
                id=Elynia
            [/filter]
            text="<span color='#f00'>!</span>" # wmllint: ignore
        [/floating_text]

        [sound]
            name=union-powerdown.ogg
        [/sound]

        [move_unit_fake]
            side=1
            type=Union Orb
            variation=lux
            x=$elynia_store.x,$orbl_loc.x
            y=$elynia_store.y,$orbl_loc.y
        [/move_unit_fake]

        [kill]
            id=Argan
            animate=yes
            # We're handling it already.
            fire_event=no
        [/kill]

        {PLACE_IMAGE units/shaxthal/master-drone-dead.png $argan_store.x $argan_store.y}

        {CLEAR_VARIABLE elynia_store,argan_store,orbd_loc,orbl_loc}

        {QUAKE_HEAVY (cave-in.ogg)}

        {QUAKE_HEAVY (rumble.ogg)}

        {QUAKE_HEAVY (cave-in.ogg)}

        [delay]
            time=750
        [/delay]

        [message]
            speaker=Lédinor
            message= _ "What just happened?"
        [/message]

        [delay]
            time=750
        [/delay]

        [message]
            speaker=Elynia
            message= _ "The prophecy... has been broken."
        [/message]

        {UNLOCK_VIEW}

        {ENDLEVEL_QUIET}
    [/event]

    [event]
        name=victory

        {REMOVE_RECRUIT_LIST 1}

        {CLEAR_VARIABLE boss_stage}
    [/event]
[/scenario]

#undef F_MATRIX
#undef F_DRONES_EASY
#undef F_DRONES_MEDIUM
#undef F_DRONES_HARD
#undef F_DRONE
#undef F_PLAYER_CAN_RECRUIT_UNDEAD
#undef L_UNPETRIFY_VICTIM
#undef L_POSSESS_PLAYER_UNIT

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


#textdomain wesnoth-Invasion_from_the_Unknown

[scenario]
    id=23Cx_Finale
    name= _ "Cutscene"
    {MAP 23Cx_Finale.map}
    turns=-1
    next_scenario=24_Epilogue
    victory_when_enemies_defeated=no
    {NO_RECALLS}

    {FULLSCREEN_CUTSCENE_UI}

    {SCENARIO_MUSIC "battle-epic.ogg"}

    {LONGDARK4}

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

        {NO_ECONOMY}

        # Used partly to disable the map preview and also to deal with shroud
        # on map edges.
        shroud=yes

        # wmllint: recognize Galas
        {CHARACTER_STATS_GALAS}
    [/side]
    # wmllint: validate-on

    [side]
        side=2
        team_name=enemies
        user_team_name= _ "team_name^Chaos Empire"
        {CHAOS_FLAG}
        color=black

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

    [event]
        name=prestart

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

        [store_unit]
            [filter]
                id=Galas
            [/filter]
            kill=yes
            variable=galas_store
        [/store_unit]

        [unstore_unit]
            variable=galas_store
            x,y=recall,recall
        [/unstore_unit]

        {CLEAR_VARIABLE galas_store}

        [store_starting_location]
            side=1
            variable=map_center
        [/store_starting_location]

        {BLACK_SCREEN}

        {LOCK_VIEW}
    [/event]

#define F_FLEEING_DRONE _PATH_X _PATH_Y
    {VARIABLE_RANDOM drone_type "Shaxthal Drone,Shaxthal Assault Drone,Shaxthal Sentry Drone"}

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

    [move_unit_fake]
        type=$drone_type
        side=2
        variation=surface
        x={_PATH_X}
        y={_PATH_Y}
        force_scroll=no
    [/move_unit_fake]
#enddef

    [event]
        name=start

        {FADE_IN}

        # wmlindent: start ignoring

        {F_FLEEING_DRONE
            16,20,22
            14,16,20
        }

        {F_FLEEING_DRONE
            25,22,19
            19,18,21
        }

        {QUAKE ()}

        {F_FLEEING_DRONE
            14,18,18
            18,20,23
        }

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

        {F_FLEEING_DRONE
            16,19,21,22,24,21
            14,16,13,14,12,8
        }

        {QUAKE ()}

        {F_FLEEING_DRONE
            16,20
            14,16
        }

        # wmlindent: stop ignoring

        [terrain]
            x,y=20,16
            terrain=Qxu
        [/terrain]

        [redraw][/redraw]

        {QUAKE (explosion.ogg)}

        {QUAKE ()}

        {QUAKE ()}

        {QUAKE ()}

        {QUAKE_HEAVY (rumble.ogg)}

        {QUAKE ()}

        {QUAKE (cave-in.ogg)}

        [store_locations]
            variable=damage_locs
            x,y=$map_center.x,$map_center.y
            radius=5
        [/store_locations]

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

                [terrain]
                    x,y=$damage_locs[$r].x,$damage_locs[$r].y
                    radius=1
                    terrain=Qxu
                [/terrain]

                [redraw][/redraw]

                [unit]
                    side=2
                    x,y=$damage_locs[$r].x,$damage_locs[$r].y
                    type=Fake Explosion
                [/unit]

                {VARIABLE_RANDOM n 0..4}

                [while]
                    {VARIABLE_NUMERICAL_GREATER_THAN_OR_EQUAL n 0}
                    [do]
                        {QUAKE ()}

                        {VARIABLE_DEC n}
                    [/do]
                [/while]

                [kill]
                    x,y=$damage_locs[$r].x,$damage_locs[$r].y
                    animate=yes
                [/kill]
            [/do]
        [/repeat]

        [delay]
            time=750
        [/delay]

        [store_locations]
            variable=damage_locs
            # All solid tiles.
            terrain=!,Q*,Q*^
        [/store_locations]

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

        {REPLACE_SCENARIO_MUSIC silence.ogg}

        # Give music controller a chance to run and play silence.ogg.
        [delay]
            time=10
        [/delay]

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

                {VARIABLE_RANDOM play_sound yes,no,no,no,no,no,no,no,no,no,no,no,no}

                [if]
                    {VARIABLE_BOOLEAN_EQUALS play_sound yes}
                    [then]
                        [sound]
                            name=explosion.ogg,explosion-big.ogg,dragonstick.ogg,thunderstick.ogg
                        [/sound]
                    [/then]
                [/if]

                [terrain]
                    x,y=$damage_locs[$r].x,$damage_locs[$r].y
                    terrain=Qxu
                [/terrain]

                [redraw][/redraw]

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

        {QUAKE ()}

        {QUAKE ()}

        {QUAKE ()}

        {QUAKE ()}

        {QUAKE ()}

        {QUAKE ()}

        {QUAKE ()}

        {QUAKE_HEAVY ()}

        {QUAKE_HEAVY ()}

        {QUAKE_HEAVY (cave-in.ogg)}

        {QUAKE_HEAVY ()}

        {QUAKE_HEAVY ()}

        {QUAKE_HEAVY (rumble.ogg)}

        {QUAKE_HEAVY ()}

        {QUAKE_HEAVY ()}

        {QUAKE_HEAVY ()}

        [terrain]
            [and]
                terrain=X*,X*^*
            [/and]
            [or]
                x,y=15,14
                radius=8
            [/or]
            terrain=Qxu
        [/terrain]

        [redraw][/redraw]

        {QUAKE_HEAVY ()}

        {QUAKE_HEAVY ()}

        {QUAKE_HEAVY (explosion-big.ogg)}

        {QUAKE_HEAVY ()}

        {QUAKE_HEAVY ()}

        {QUAKE_HEAVIER ()}

        {QUAKE_HEAVIER ()}

        {QUAKE_HEAVIER ()}

        {QUAKE_HEAVIER ()}

        {QUAKE_HEAVIER ()}

        {QUAKE_HEAVIER (dragonstick.ogg)}

        {QUAKE_HEAVIER ()}

        {QUAKE_HEAVIER ()}

        {QUAKE_HEAVIER ()}

        {QUAKE_HEAVIER ()}

        {QUAKE_HEAVIER ()}

        {QUAKE_HEAVIER ()}

        {QUAKE_HEAVIER ()}

        {QUAKE_HEAVIER ()}

        {QUAKE_HEAVIER ()}

        {QUAKE_HEAVIER ()}

        {QUAKE_HEAVIER ()}

        {QUAKE_HEAVIER ()}

        {QUAKE_HEAVIER ()}

        [terrain]
            terrain=Qxu
        [/terrain]

        [redraw][/redraw]

        {QUAKE_HEAVIER (explosion-big.ogg)}

        {QUAKE_HEAVIER ()}

        {QUAKE_HEAVIER (explosion-big.ogg)}

        {QUAKE_HEAVIER (rumble.ogg)}

        {FADE_TO_BLACK}

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

        [place_shroud]
            side=1
        [/place_shroud]

        [redraw][/redraw]

        [delay]
            time=4000
        [/delay]

        {ENDLEVEL_QUIET} {NO_REPLAY_SAVE}
    [/event]

    [event]
        name=victory

        {CLEAR_VARIABLE damage_locs,map_center,play_sound,drone_type,r}
    [/event]
[/scenario]

#undef F_FLEEING_DRONE

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