#textdomain wesnoth-utbs
# Level 12: The Final Confrontation

[scenario]
    id="12_The_Final_Confrontation"
    name= _ "The Final Confrontation"
    label= _ "The Final Confrontation"

    next_scenario=13_Epilogue

    map_data="{campaigns/Under_the_Burning_Suns/maps/12_The_Final_Confrontation.map}"

    {STORY_THE_FINAL_CONFRONTATION}

    {SCENARIO_MUSIC "the_deep_path.ogg"}
    {EXTRA_SCENARIO_MUSIC "revelation.ogg"}
    {EXTRA_SCENARIO_MUSIC "the_city_falls.ogg"}

    #don't display snapshot of map in saved games
    snapshot="no"
    victory_when_enemies_defeated=no

    #there is no turn limit for this scenario
    turns="-1"

    {UNDERGROUND}

    # Side 1: elves
    [side]
        side=1
        id=Kaleh
        type=Desert Fighter
        canrecruit=yes
        gold=0
        {INCOME 0 0 0}
        controller=human
        shroud=no
        fog=no
    [/side]

    # Side 2: Eloh
    [side]
        side=2
        no_leader=yes
        gold=0
        income=0
        controller=ai
        shroud=no
        fog=no
        team_name=eloh
        user_team_name=_"Eloh"
        {FLAG_VARIANT undead}
        [ai]
            aggression=0.95
            caution=0.1

            # change AI so that enemies target Kaleh's friends
            # more than Kaleh himself. I want to threaten
            # other units more, since Kaleh's death leads to
            # immediate defeat

            [target]
                id=Kaleh
                value=1
            [/target]

            [target]
                id=Nym
                value=3
            [/target]

            [target]
                id=Zhul
                value=3
            [/target]

            [target]
                id=$ally_name
                value=3
            [/target]
        [/ai]
    [/side]

    # Side 3: Brainwashed Elves
    [side]
        side=3
        no_leader=yes
        gold=0
        income=0
        controller=ai
        shroud=no
        fog=no
        team_name=eloh
        user_team_name=_"Eloh"

        [ai]
            aggression=0.95
            caution=0.1

            [target]
                id=Kaleh
                value=1
            [/target]

            [target]
                id=Nym
                value=3
            [/target]

            [target]
                id=Zhul
                value=3
            [/target]

            [target]
                id=$ally_name
                value=3
            [/target]
        [/ai]
    [/side]

    # Side 4: Pulsing Spires
    [side]
        side=4
        no_leader=yes
        gold=0
        income=0
        controller=ai
        shroud=no
        fog=no
        team_name=eloh
        user_team_name=_"Eloh"
        {FLAG_VARIANT undead}

        [ai]
            aggression=-1.0
            caution=1.0

            [target]
                id=Kaleh
                value=1
            [/target]

            [target]
                id=Nym
                value=3
            [/target]

            [target]
                id=Zhul
                value=3
            [/target]

            [target]
                id=$ally_name
                value=3
            [/target]
        [/ai]
    [/side]

    # Prestart functions:
    # insert items onto map
    # place item images on map
    # recall main heroes
    # store/remove kaleh
    # initialize starting variables
    # set starting scenario objectives

    [event]
        name=prestart

        # add items to map

        {PLACE_IMAGE items/kaleh-dead.png 10 13}

        # recall heroes

        [recall]
            id=Zhul
            x,y=11,17
        [/recall]
        [recall]
            id=Nym
            x,y=10,16
        [/recall]
        # recall dwarf/troll
        [recall]
            id=$ally_name
            x,y=9,17
        [/recall]

        # store/remove kaleh

        [store_unit]
            [filter]
                id=Kaleh
            [/filter]
            kill=yes
            variable=stored_kaleh
        [/store_unit]

        #initialize starting variables

        {VARIABLE in_final_fight true}
        {VARIABLE spires_destroyed 0}
        {VARIABLE allies_killed 0}
        {VARIABLE nym_dead false}
        {VARIABLE zhul_dead false}

        #if ally survived to final battle then save this info
        [if]
            [have_unit]
                id=$ally_name
            [/have_unit]

            [then]
                {VARIABLE ally_died_in_final_fight no}
            [/then]
        [/if]

        {VARIABLE minion_turn_counter 0}
        {VARIABLE attacked_yechnagoth 0}
        {VARIABLE immortal_hero 0}

        # at each increase 1 extra alien minion is produced each turn

#ifdef EASY
        {VARIABLE first_increase 2}
        {VARIABLE second_increase 4}
        {VARIABLE third_increase 6}
#endif
#ifdef NORMAL
        {VARIABLE first_increase 2}
        {VARIABLE second_increase 3}
        {VARIABLE third_increase 5}
#endif
#ifdef HARD
        {VARIABLE first_increase 1}
        {VARIABLE second_increase 3}
        {VARIABLE third_increase 5}
#endif

        # set starting scenario objectives

        [objectives]
            summary= _ "Starting Objectives:"
            [objective]
                description= _ "Defeat the false Eloh"
                condition=win
            [/objective]
            [objective]
                description= _ "Death of Kaleh"
                condition=lose
            [/objective]
        [/objectives]
    [/event]

    # Event 1: Starting dialogue

    [event]
        name=start

        [scroll_to]
            x,y=10,13
        [/scroll_to]

        [redraw]
        [/redraw]

        [delay]
            time=750
        [/delay]

        [message]
            speaker=Nym
            message= _ "Kaleh! No!"
        [/message]

        [redraw]
        [/redraw]

        [delay]
            time=500
        [/delay]

        # Nym runs to Kaleh's side

        {MOVE_UNIT id=Nym 10 14}

        [delay]
            time=300
        [/delay]

        # Zhul runs to Kaleh's side

        {MOVE_UNIT id=Zhul 11 14}

        [message]
            speaker=Zhul
            message= _ "He's still breathing. Eloh, what grace I have, give unto him."
        [/message]

        [delay]
            time=300
        [/delay]

        [message]
            speaker=Nym
            message= _ "He's stirring."
        [/message]

        # Ally runs to Kaleh's side

        {MOVE_UNIT id=$ally_name 9 14}

        {CLEAR_VARIABLE stored_unit}

        [delay]
            time=300
        [/delay]

        [if]
            [variable]
                name=ally_race
                equals=Dwarf
            [/variable]

            [then]
                [message]
                    speaker=$ally_name
                    message= _ "Aye, there's still life in the boy. But where is the foul creature that did this to him?"
                [/message]
            [/then]

            [else]
                [message]
                    speaker=$ally_name
                    message= _ "The little one is not dead yet. But where is evil lady that did this to him?"
                [/message]
            [/else]
        [/if]

        [redraw]
        [/redraw]

        [delay]
            time=300
        [/delay]

        [unit]
            type=Divine Incarnation
            id=Eloh
            name= _ "Eloh"
            profile=portraits/eloh.png
            x,y=10,11
            upkeep=free
            side=2
            [modifications]
                {TRAIT_INTELLIGENT}
                {TRAIT_QUICK}
            [/modifications]
        [/unit]

        [delay]
            time=500
        [/delay]

        [message]
            speaker=Eloh
            message= _ "So, the elf's puny friends think they can save him. But you are too late. He is already mine!"
        [/message]

        [removeitem]
            x,y=10,13
        [/removeitem]

        {VARIABLE_OP stored_kaleh.hitpoints add -10}
        {VARIABLE stored_kaleh.facing n}

        [unstore_unit]
            variable=stored_kaleh
            find_vacant=yes
        [/unstore_unit]

        {CLEAR_VARIABLE stored_kaleh}

        [if]
            [have_unit]
                id=$ally_name
            [/have_unit]

            [then]
                [message]
                    speaker=Kaleh
                    message= _ "Nym, Zhul, $intl_ally_name|, you shouldn't have."
                [/message]
            [/then]

            [else]
                [message]
                    speaker=Kaleh
                    message= _ "Nym, Zhul, you shouldn't have."
                [/message]
            [/else]
        [/if]

        [message]
            speaker=Eloh
            message= _ "Your struggles were mildly entertaining, but futile in the end. For I am powerful beyond your imagining, and this is the seat of my power!"
        [/message]

        [message]
            speaker=Zhul
            message= _ "You are not Eloh. You are but a pitiful mockery of her power and glory!"
        [/message]

        [message]
            speaker=Eloh
            message= _ "Is that what you think? I shall prove you wrong. Look out upon your people and despair!"
        [/message]

        [move_unit_fake]
            type=Desert Fighter
            side=3
            x=9,8
            y=20,16
        [/move_unit_fake]

        [unit]
            type=Desert Fighter
            id=Anarion
            name= _ "Anarion"
            upkeep=full
            x=8
            y=16
            side=3
            [modifications]
                {TRAIT_QUICK}
                {TRAIT_STRONG}
            [/modifications]
        [/unit]

        [redraw]
        [/redraw]

        [delay]
            time=100
        [/delay]

        [move_unit_fake]
            type=Desert Archer
            side=3
            x=11,12
            y=20,16
        [/move_unit_fake]

        [unit]
            type=Desert Archer
            id=Zylea
            name= _ "Zylea"
            upkeep=full
            x=12
            y=16
            side=3
            [modifications]
                {TRAIT_DEXTROUS}
                {TRAIT_RESILIENT}
            [/modifications]
        [/unit]

        [message]
            speaker=Anarion
            message= _ "All hail Eloh!"
        [/message]

        [message]
            speaker=Zylea
            message= _ "Death to the heretics!"
        [/message]

        [message]
            speaker=Eloh
            message= _ "They worship their true god."
        [/message]

        [message]
            speaker=Eloh
            message= _ "Come and bow down before your true master, boy."
        [/message]

        [message]
            speaker=Kaleh
            message= _ "Your wish is my command."
        [/message]

        [redraw]
        [/redraw]

        [delay]
            time=300
        [/delay]

        # Kaleh moves over to Eloh

        {MOVE_UNIT id=Kaleh 10 12}

        [delay]
            time=500
        [/delay]

        [animate_unit]
            flag=attack

            [filter]
                id=Kaleh
            [/filter]

            [primary_attack]
                name=sword
            [/primary_attack]

            hit=yes
        [/animate_unit]

        # damage Eloh slightly

        [store_unit]
            [filter]
                id=Eloh
            [/filter]

            variable=stored_eloh
        [/store_unit]

        {VARIABLE_OP stored_eloh add -6}

        [unstore_unit]
            variable=stored_eloh
            find_vacant=no
            text="-6"	# wmllint: ignore
            {COLOR_HARM}
        [/unstore_unit]

        {CLEAR_VARIABLE stored_eloh}

        [redraw]
        [/redraw]

        [delay]
            time=500
        [/delay]

        [message]
            speaker=Eloh
            image=portraits/eloh_rage.png
            message= _ "Argh, you stabbed me!"
        [/message]

        [message]
            speaker=Kaleh
            # wmllint: local spelling Kalehssar
            message= _ "I have crossed deserts, mountains, and oceans and watched my people bleed every step of the way. I did not come all this way to give up now. I am tired of being called boy; I am Kalehssar, the leader of my people and I will fight you until my dying breath!"
        [/message]

        [message]
            speaker=Eloh
            image=portraits/eloh_rage.png
            message= _ "I command you to stop this foolishness!"
        [/message]

        [message]
            speaker=Kaleh
            message= _ "Never!"
        [/message]

        [message]
            speaker=Nym
            message= _ "The shell brooches Melusand gave us, they're glowing!"
        [/message]

        [message]
            speaker=Kaleh
            message= _ "You will never dominate us, not while hope survives!"
        [/message]

        [message]
            speaker=Eloh
            image=portraits/eloh_rage.png
            message= _ "So be it. You choose death? Then you shall receive it from those you hold most dear. Kill the unbelievers, let none survive!"
        [/message]

        [message]
            speaker=Anarion
            message= _ "Yes mistress."
        [/message]

        [message]
            speaker=Kaleh
            message= _ "Ignore our brethren, we must destroy her!"
        [/message]
    [/event]

    # MACRO 1: Create an alien minion

#define CREATE_MINION
    [store_locations]
        x=6-14
        y=7-14
        terrain=Rr,Uu

        [not]
            [filter]
            [/filter]
        [/not]

        variable=possible_spawn_locations
    [/store_locations]

    [if]
        [variable]
            name=possible_spawn_locations.length
            greater_than_equal_to=1
        [/variable]

        [then]
            {VARIABLE_OP new_minion_loc_i random "1..$possible_spawn_locations.length"}
            {VARIABLE_OP new_minion_loc_i add -1}

            {UNIT 2 (Crawling Horror) $possible_spawn_locations[$new_minion_loc_i].x $possible_spawn_locations[$new_minion_loc_i].y (upkeep=free)}

            {CLEAR_VARIABLE possible_spawn_locations}
        [/then]
    [/if]
#enddef

    # Event 1: The Death of Eloh and the revealing of the true monster

    [event]
        name=die

        [filter]
            id=Eloh
        [/filter]

        [message]
            speaker=Eloh
            image=portraits/eloh_rage.png
            message= _ "You think you killed me? You have no idea what you are facing."
        [/message]

        [message]
            speaker=Nym
            message= _ "Who is she really? Could it be Yanqui, back to avenge his imprisonment?"
        [/message]

        [message]
            speaker=Eloh
            image=portraits/eloh_rage.png
            message= _ "Nonsense. Unlike that fool, I do not care about petty things like revenge. Nor do I depend on mortals to enact my will. No, I shall destroy you myself!"
        [/message]

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

        {MODIFY_UNIT side=3 side 1}

        [message]
            type=Desert Fighter, Desert Archer
            message= _ "Huh? What happened?"
        [/message]

        [message]
            speaker=Zhul
            message= _ "Her spell has been broken. But I don't think we have won yet. Come aid us!"
        [/message]

        [message]
            type=Desert Fighter, Desert Archer
            message= _ "Yes priestess."
        [/message]

        [scroll]
            x=25
        [/scroll]
        [scroll]
            x=-25
        [/scroll]
        [scroll]
            x=25
        [/scroll]
        [scroll]
            x=-25
        [/scroll]
        [scroll]
            x=25
        [/scroll]
        [scroll]
            x=-25
        [/scroll]

        [redraw]
        [/redraw]

        [delay]
            time=400
        [/delay]

        [colour_adjust]
            red,green,blue=40,0,100
        [/colour_adjust]

        [redraw]
        [/redraw]

        [delay]
            time=250
        [/delay]

        [colour_adjust]
            red,green,blue=0,0,0
        [/colour_adjust]

        [redraw]
        [/redraw]

        # main body appears

        [terrain]
            terrain=Ryd	# wmllint: ignore
            x,y=10,10
        [/terrain]

        # create main body unit here

        [unit]
            type=Central Body
            id=Yechnagoth
            name= _ "Yechnagoth"
            side=2
            x,y=10,10
        [/unit]

        # add roots around central body

        #{PLACE_IMAGE scenery/castle-ruins.png 9 11}
        #{PLACE_IMAGE scenery/castle-ruins.png 10 11}
        #{PLACE_IMAGE scenery/castle-ruins.png 11 11}
        #{PLACE_IMAGE scenery/castle-ruins.png 9 10}
        #{PLACE_IMAGE scenery/castle-ruins.png 10 9}
        #{PLACE_IMAGE scenery/castle-ruins.png 11 10}

        # Pulsing Spires appear

        {RANDOM 0..1}

        [if]
            [variable]
                name=random
                numerical_equals=1
            [/variable]

            [then]
                [terrain]
                    terrain=Uu
                    x=8,8,14
                    y=7,13,10
                [/terrain]

                # create Pulsing Spires at:
                # (8,7) (8,13) (14,10)

                [unit]
                    type=Pulsing Spire
                    side=4
                    x,y=8,7
                [/unit]

                [unit]
                    type=Pulsing Spire
                    side=4
                    x,y=8,13
                [/unit]

                [unit]
                    type=Pulsing Spire
                    side=4
                    x,y=14,10
                [/unit]
            [/then]

            [else]
                [terrain]
                    terrain=Uu
                    x=6,12,12
                    y=10,7,13
                [/terrain]

                # create Pulsing Spires at:
                # (6,10) (12,7) (12,13)

                [unit]
                    type=Pulsing Spire
                    side=4
                    x,y=6,10
                [/unit]

                [unit]
                    type=Pulsing Spire
                    side=4
                    x,y=12,7
                [/unit]

                [unit]
                    type=Pulsing Spire
                    side=4
                    x,y=12,13
                [/unit]
            [/else]
        [/if]

        [redraw]
        [/redraw]

        [message]
            speaker=Nym
            message= _ "What is that thing!?!"
        [/message]

        [message]
            speaker=Zhul
            message= _ "Eloh protect us!"
        [/message]

        [colour_adjust]
            red,green,blue=33,181,140
        [/colour_adjust]

        [redraw]
        [/redraw]

        [delay]
            time=250
        [/delay]

        [colour_adjust]
            red,green,blue=0,0,0
        [/colour_adjust]

        [redraw]
        [/redraw]

        # alien bugs appear

        # create 3 bugs using a macro

        {CREATE_MINION}
        {CREATE_MINION}
        {CREATE_MINION}

        [redraw]
        [/redraw]

        [delay]
            time=500
        [/delay]

        [message]
            speaker=Zhul
            message= _ "Curse Uria, more abominations!"
        [/message]

        [message]
            speaker=Nym
            message= _ "Is it even possible to kill this thing? It's huge!"
        [/message]

        [message]
            speaker=Kaleh
            message= _ "It must have some sort of weak point. Look at those pulsing spires, I thought they were stone, but they seem to be alive. Maybe if we destroy them it will weaken the creature. No matter what horrors appear, we must keep attacking it. We can't stop now!"
        [/message]

        [objectives]
            summary= _ "New Objectives:"
            [objective]
                description= _ "Defeat Yechnagoth"
                condition=win
            [/objective]
            [objective]
                description= _ "Death of Kaleh"
                condition=lose
            [/objective]
        [/objectives]
    [/event]

    [event]
        name=new turn
        first_time_only=no

        [if]
            [have_unit]
                id=Yechnagoth
            [/have_unit]

            [then]
                [store_unit]
                    [filter]
                        side=1
                        level=4
                        x,y=recall,recall
                    [/filter]

                    kill=no
                    variable=lvl4_recalls
                [/store_unit]

                [if]
                    [variable]
                        name=lvl4_recalls.length
                        greater_than=0
                    [/variable]

                    [then]
                        {VARIABLE helper_level 4}
                    [/then]

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

                            kill=no
                            variable=lvl3_recalls
                        [/store_unit]

                        [if]
                            [variable]
                                name=lvl3_recalls.length
                                greater_than=0
                            [/variable]

                            [then]
                                {VARIABLE helper_level 3}
                            [/then]

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

                                    kill=no
                                    variable=lvl2_recalls
                                [/store_unit]

                                [if]
                                    [variable]
                                        name=lvl2_recalls.length
                                        greater_than=0
                                    [/variable]

                                    [then]
                                        {VARIABLE helper_level 2}
                                    [/then]

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

                                            kill=no
                                            variable=lvl1_recalls
                                        [/store_unit]

                                        [if]
                                            [variable]
                                                name=lvl1_recalls.length
                                                greater_than=0
                                            [/variable]

                                            [then]
                                                {VARIABLE helper_level 1}
                                            [/then]

                                            [else]
                                                {VARIABLE no_possible_helpers_left yes}
                                            [/else]
                                        [/if]
                                    [/else]
                                [/if]
                            [/else]
                        [/if]
                    [/else]
                [/if]

                {CLEAR_VARIABLE lvl1_recalls,lvl2_recalls,lvl3_recalls,lvl4_recalls}

                [if]
                    [variable]
                        name=no_possible_helpers_left
                        not_equals=yes
                    [/variable]

                    [then]
                        [store_unit]
                            [filter]
                                side=1
                                level=$helper_level
                                x,y=recall,recall
                            [/filter]

                            kill=no
                            variable=possible_helpers
                        [/store_unit]

                        {VARIABLE random_helper_i "1..$possible_helpers.length"}
                        {VARIABLE_OP random_helper_i add -1}

                        {VARIABLE possible_helpers[$random_helper_i].x 10}
                        {VARIABLE possible_helpers[$random_helper_i].y 18}

                        [move_unit_fake]
                            type=$possible_helpers[$random_helper_i].type
                            side=1
                            x=10,10
                            y=20,18
                        [/move_unit_fake]

                        [recall]
                            id=$possible_helpers[$random_helper_i].id
                            x,y=10,18
                            show=no
                        [/recall]

                        {CLEAR_VARIABLE possible_helpers}
                    [/then]
                [/if]
            [/then]
        [/if]
    [/event]

    # Event 2: New minions are created each turn

    [event]
        name=new turn
        first_time_only=no

        [if]
            [have_unit]
                id=Yechnagoth
            [/have_unit]

            [then]
                {CREATE_MINION}

                [set_variable]
                    name=minion_turn_counter
                    add=1
                [/set_variable]

                #[message]
                #speaker=narrator
                #message= _ "minion turn counter: $minion_turn_counter"
                #[/message]
            [/then]
        [/if]

        [if]
            [variable]
                name=minion_turn_counter
                greater_than=$first_increase
            [/variable]

            [then]
                {CREATE_MINION}
            [/then]
        [/if]

        [if]
            [variable]
                name=minion_turn_counter
                greater_than=$second_increase
            [/variable]

            [then]
                {CREATE_MINION}
            [/then]
        [/if]

        [if]
            [variable]
                name=minion_turn_counter
                greater_than=$third_increase
            [/variable]

            [then]
                {CREATE_MINION}
            [/then]
        [/if]
    [/event]

    # Event 3: Central Body heals fully each turn

    [event]
        name=new turn
        first_time_only=no

        [if]
            [have_unit]
                type=Central Body
            [/have_unit]

            [then]
                [store_unit]
                    [filter]
                        type=Central Body
                    [/filter]
                    variable=stored_alien
                [/store_unit]

                [set_variable]
                    name=temp
                    value=$stored_alien.hitpoints
                [/set_variable]

                [object]
                    [filter]
                        type=Central Body
                    [/filter]

                    silent=yes
                    duration="level"

                    [effect]
                        apply_to="hitpoints"
                        heal_full="yes"
                    [/effect]
                [/object]

                [if]
                    [variable]
                        name=temp
                        less_than=100
                    [/variable]

                    [variable]
                        name=attacked_yechnagoth
                        numerical_equals=0
                    [/variable]

                    [then]
                        [set_variable]
                            name=attacked_yechnagoth
                            value=1
                        [/set_variable]

                        [message]
                            speaker=Kaleh
                            message= _ "That central body is healing faster than we can damage it. It's almost as if our attacks are doing no damage at all. We got to try another tactic, and fast!"
                        [/message]
                    [/then]
                [/if]

                {CLEAR_VARIABLE stored_alien}
            [/then]
        [/if]
    [/event]

    # Event 4: Special attack my Central Body each turn

    [event]
        name=new turn
        first_time_only=no

        [if]
            [have_unit]
                id=Yechnagoth
            [/have_unit]

            [then]
                [store_unit]
                    [filter]
                        side=1
                        {EVERYWHERE}
                    [/filter]

                    kill=no
                    variable=elf_list
                [/store_unit]

                {VARIABLE_OP victim_i random "1..$elf_list.length"}
                {VARIABLE_OP victim_i add -1}

                #used to be 140,255,247

                [colour_adjust]
                    red,green,blue=40,0,100
                [/colour_adjust]

                [redraw]
                [/redraw]

                [delay]
                    time=250
                [/delay]

                [colour_adjust]
                    red,green,blue=0,0,0
                [/colour_adjust]

                [redraw]
                [/redraw]

                [delay]
                    time=100
                [/delay]

                # Add 2 effects slow unit and damage it
                # 1: slow unit
                # 2: do damage to unit (8/9/10 based on difficulty?-stops healing)

                {VARIABLE elf_list[$victim_i].status.slowed on}

                [if]
                    [variable]
                        name="elf_list[$victim_i].gender"
                        equals="female"
                    [/variable]
                    [then]
                        #textdomain wesnoth
                        {VARIABLE slowed_text ( _ "female^slowed")}
                    [/then]
                    [else]
                        {VARIABLE slowed_text ( _ "slowed")}
                    [/else]
                [/if]
                #textdomain wesnoth-utbs

                [unstore_unit]
                    variable=elf_list[$victim_i]
                    find_vacant=no
                    text=$slowed_text
                    {COLOR_HARM}
                [/unstore_unit]

                {CLEAR_VARIABLE slowed_text}

#ifdef EASY
                {VARIABLE special_attack_damage 10}
#endif
#ifdef NORMAL
                {VARIABLE special_attack_damage 11}
#endif
#ifdef HARD
                {VARIABLE special_attack_damage 12}
#endif

                [if]
                    [variable]
                        name=elf_list[$victim_i].hitpoints
                        less_than_equal_to=$special_attack_damage
                    [/variable]

                    [then]
                        {VARIABLE special_attack_damage $elf_list[$victim_i].hitpoints}
                        {VARIABLE_OP special_attack_damage add -1}
                    [/then]
                [/if]

                {VARIABLE_OP elf_list[$victim_i].hitpoints add -$special_attack_damage}

                [unstore_unit]
                    variable=elf_list[$victim_i]
                    find_vacant=no
                    text="-$special_attack_damage"	# wmllint: ignore no spellcheck
                    {COLOR_HARM}
                [/unstore_unit]

                [if]
                    [variable]
                        name=hit_by_special_attack
                        not_equals=yes
                    [/variable]

                    [then]
                        {VARIABLE hit_by_special_attack yes}

                        [message]
                            x,y=$elf_list[$victim_i].x,$elf_list[$victim_i].y
                            message= _ "What the heck? That central creature just hit me with some sort of slime. It hurts and I, I'm stuck!"
                        [/message]
                    [/then]

                    [else]
                        [message]
                            x,y=$elf_list[$victim_i].x,$elf_list[$victim_i].y
                            message= _ "Ow, I'm stuck!"
                        [/message]
                    [/else]
                [/if]

                {CLEAR_VARIABLE elf_list}
            [/then]
        [/if]
    [/event]

    # Event 5: When player destroys all 3 Pulsing Spires, Central Body is weakened

    [event]
        name=die
        first_time_only=no

        [filter]
            type=Pulsing Spire
        [/filter]

        [if]
            [not]
                [have_unit]
                    type=Pulsing Spire
                [/have_unit]
            [/not]

            [then]
                # when all 3 spires are destroyed, weaken main body
                [kill]
                    type=Pulsing Spire
                    fire_event=no
                    animate=no
                [/kill]

                [scroll]
                    x=20
                [/scroll]
                [scroll]
                    x=-20
                [/scroll]
                [scroll]
                    x=20
                [/scroll]
                [scroll]
                    x=-20
                [/scroll]
                [scroll]
                    x=20
                [/scroll]
                [scroll]
                    x=-20
                [/scroll]

                [message]
                    speaker=narrator
                    message= _ "Aaaauuurrrggghhhh!!!"
                    image= # wmllint: no-icon
                [/message]

                [kill]
                    type=Central Body
                    animate=no
                    fire_event=no
                [/kill]

                [unit]
                    type=Central Body2
                    id=Yechnagoth
                    name= _ "Yechnagoth"
                    side=2
                    x,y=10,10
                [/unit]

                [redraw]
                [/redraw]

                [message]
                    speaker=Kaleh
                    message= _ "I think we're finally doing some damage. We must attack the central body, while it remains vulnerable!"
                [/message]
            [/then]
        [/if]
    [/event]

    # Event 6: Death of weakened Central Body

    [event]
        name=last breath

        [filter]
            type=Central Body2
        [/filter]

        # shake cavern

        [scroll]
            x=40
        [/scroll]
        [scroll]
            x=-40
        [/scroll]
        [scroll]
            x=40
        [/scroll]
        [scroll]
            x=-40
        [/scroll]
        [scroll]
            x=40
        [/scroll]
        [scroll]
            x=-40
        [/scroll]

        # flash dark blue, light blue, very light blue,

        # dark blue

        [colour_adjust]
            red,green,blue=40,0,100
        [/colour_adjust]

        [redraw]
        [/redraw]

        [delay]
            time=300
        [/delay]

        #light blue

        [colour_adjust]
            red,green,blue=33,181,140
        [/colour_adjust]

        [redraw]
        [/redraw]

        [delay]
            time=300
        [/delay]

        # very light blue

        [colour_adjust]
            red,green,blue=140,255,247
        [/colour_adjust]

        [redraw]
        [/redraw]

        [delay]
            time=300
        [/delay]

        # back to normal

        [colour_adjust]
            red,green,blue=0,0,0
        [/colour_adjust]

        [redraw]
        [/redraw]
    [/event]

    [event]
        name=die

        [filter]
            type=Central Body2
        [/filter]

        # have a minion scream

        [message]
            type=Crawling Horror
            message= _ "Aiiieeee!!!"	# wmllint: no spellcheck
        [/message]

        [redraw]
        [/redraw]

        # kill minions

        [kill]
            type=Crawling Horror,Pulsing Spire
            animate=yes
            fire_event=no
        [/kill]

        [redraw]
        [/redraw]

        # wait
        [delay]
            time=2500
        [/delay]

        #ending conversation

        [message]
            speaker=Kaleh
            message= _ "At last. It is finished."
        [/message]

        [if]
            [have_unit]
                id=Nym
            [/have_unit]

            [then]
                [message]
                    speaker=Nym
                    message= _ "Is Yechnagoth really dead?"
                [/message]

                [message]
                    speaker=Kaleh
                    message= _ "Yes, she's dead Nym."
                [/message]

                [message]
                    speaker=Nym
                    message= _ "I almost can't believe it."
                [/message]
            [/then]
        [/if]

        [message]
            speaker=Zhul
            message= _ "Behold, the pretender has been defeated. Eloh's might has prevailed."
        [/message]

        [if]
            [variable]
                name=ally_race
                equals=Dwarf
            [/variable]

            [then]
                [message]
                    speaker=$ally_name
                    message= _ "Ugh. I'm covered in blood and guts, and this nasty blue stuff. I don't know what in the nine hells we were fighting, but she doesn't smell any better dead than she did alive."
                [/message]
            [/then]

            [else]
                [message]
                    speaker=$ally_name
                    message= _ "Ugh. $intl_ally_name is covered in blood and guts and nasty blue goo. Whatever creature was, she doesn't smell any better dead than she did alive."
                [/message]
            [/else]
        [/if]

        [if]
            [have_unit]
                id=Nym
            [/have_unit]

            [or]
                [have_unit]
                    id=Zhul
                [/have_unit]
            [/or]

            [or]
                [have_unit]
                    id=$ally_name
                [/have_unit]
            [/or]

            [then]
                [message]
                    speaker=Kaleh
                    message= _ "Let's get out of here."
                [/message]
            [/then]
        [/if]

        {CLEAR_VARIABLE minion_turn_counter}
        {CLEAR_VARIABLE attacked_yechnagoth}
        {CLEAR_VARIABLE hit_by_special_attack}

        {CLEAR_VARIABLE random}
        {CLEAR_VARIABLE counter}
        {CLEAR_VARIABLE temp}
        {CLEAR_VARIABLE temp_x}
        {CLEAR_VARIABLE temp_y}

        [endlevel]
            result=victory
            bonus=no
            carryover_report=no
            linger_mode=no
            {NEW_GOLD_CARRYOVER 0}
        [/endlevel]
    [/event]

    # testing event to skip to end of battle

    #[event]
    #name=turn 2

    #[kill]
    #type=Pulsing Spire
    #animate=yes
    #fire_event=yes
    #[/kill]

    #[/event]

    {@campaigns/Under_the_Burning_Suns/utils/kaleh-abilities.cfg}
    {@campaigns/Under_the_Burning_Suns/utils/deaths.cfg}
[/scenario]
