#textdomain wesnoth-ei
#
# NOTE: This level is intended as a challenging final scenario.
#       Players without sufficent high-level troops to recall
#       are advised to select "The Duel" instead.
#
[scenario]
    id=17b_Weldyn_Besieged
    name= _ "Weldyn Besieged"
    map_data="{campaigns/Eastern_Invasion/maps/17b_Weldyn_Besieged.map}"
    turns=-1
    next_scenario=18_Epilogue

    {DEFAULT_SCHEDULE}

    {INTRO_AND_SCENARIO_MUSIC loyalists.ogg the_city_falls.ogg}
    {EXTRA_SCENARIO_MUSIC the_deep_path.ogg}
    {EXTRA_SCENARIO_MUSIC battle.ogg}

    {campaigns/Eastern_Invasion/utils/deaths.cfg}
    {campaigns/Eastern_Invasion/utils/final_battle.cfg}

    {BIGMAP_17}

    [side]
        type=Lieutenant
        id=Gweddry
        name= _ "Gweddry"
        side=1
        gold=300
        canrecruit=yes
        controller=human
        team_name=wesnothians
        user_team_name=_"Wesnothians"
        {FLAG_VARIANT loyalist}
    [/side]

    # Gweddry gets the villages within the moat.
    # The ones outside he'll actually have to take.
    {STARTING_VILLAGES 1 3}

    [side]
        type=Lich
        id=Lich2
        name= _ "?"
        side=2
        canrecruit=yes
        [variables]
            random_lich=yes
        [/variables]
        controller=ai
        recruit=Bone Shooter,Revenant,Wraith,Shadow,Soulless,Walking Corpse
        {GOLD 130 150 180}
        team_name=bad
        user_team_name=_"Undead"
        {FLAG_VARIANT undead}
    [/side]

    [side]
        type=Lich
        id=Lich3
        name= _ "?"
        side=3
        canrecruit=yes
        [variables]
            random_lich=yes
        [/variables]
        controller=ai
        recruit=Bone Shooter,Revenant,Wraith,Shadow,Deathblade
        {GOLD 130 150 180}
        team_name=bad
        user_team_name=_"Undead"
        {FLAG_VARIANT undead}
    [/side]

    [side]
        type=Lich
        id=Lich4
        name= _ "?"
        side=4
        canrecruit=yes
        [variables]
            random_lich=yes
        [/variables]
        controller=ai
        recruit=Bone Shooter,Revenant,Wraith,Shadow,Banebow
        {GOLD 130 150 180}
        team_name=bad
        user_team_name=_"Undead"
        {FLAG_VARIANT undead}
    [/side]

    [side]
        type=Lich
        id=Lich5
        name= _ "?"
        side=5
        canrecruit=yes
        [variables]
            random_lich=yes
        [/variables]
        controller=ai
        recruit=Bone Shooter,Revenant,Wraith,Shadow,Necrophage,Ghast
        {GOLD 130 150 180}
        team_name=bad
        user_team_name=_"Undead"
        {FLAG_VARIANT undead}
    [/side]

    [side]
        type=Lich
        id=Lich6
        name= _ "?"
        side=6
        canrecruit=yes
        [variables]
            random_lich=yes
        [/variables]
        controller=ai
        recruit=Bone Shooter,Revenant,Wraith,Shadow,Draug
        {GOLD 130 150 180}
        team_name=bad
        user_team_name=_"Undead"
        {FLAG_VARIANT undead}
    [/side]

    [side]
        type=Lich
        id=Lich7
        name= _ "?"
        side=7
        canrecruit=yes
        [variables]
            random_lich=yes
        [/variables]
        controller=ai
        recruit=Bone Shooter,Revenant,Wraith,Shadow,Spectre
        {GOLD 130 150 180}
        team_name=bad
        user_team_name=_"Undead"
        {FLAG_VARIANT undead}
    [/side]

    [side]
        type=Lich
        id=Lich8
        name= _ "?"
        side=8
        canrecruit=yes
        [variables]
            random_lich=yes
        [/variables]
        controller=ai
        recruit=Bone Shooter,Revenant,Wraith,Shadow,Dark Adept,Dark Sorcerer
        {GOLD 130 150 180}
        team_name=bad
        user_team_name=_"Undead"
        {FLAG_VARIANT undead}
    [/side]

    # wmllint: recognize Mal-Ravanal
    [event]
        name=prestart
        [objectives]
            side=1
            [objective]
                description= _ "Defeat Mal-Ravanal while Dacyn is nearby"
                condition=win
            [/objective]
            [objective]
                description= _ "Death of Gweddry"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Dacyn"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Owaec"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Konrad"
                condition=lose
            [/objective]
            [objective]
                description= _ "Letting any enemy unit onto your keep"
                condition=lose
            [/objective]
            {IS_LAST_SCENARIO}
        [/objectives]
    [/event]

    [event]
        name=start
        [recall]
            id=Dacyn
        [/recall]
        [recall]
            id=Owaec
        [/recall]
        [recall]
            id=Grug
        [/recall]
        [recall]
            id=Konrad
        [/recall]
        [recall]
            id=Engineer
        [/recall]
        [recall]
            id=Terraent
        [/recall]

        [message]
            speaker=Dacyn
            message= _ "I can sense Mal-Ravanal is here somewhere... When we attack him I will need to be nearby with the Null Stone to prevent his teleportation. We cannot afford to let him escape."
        [/message]
        {VARIABLE lich_death_bonus 25}

        {VARIABLE lich_name_0 _"Mal-Hadanak"}
        {VARIABLE lich_name_1 _"Mal-Katklagad"}
        {VARIABLE lich_name_2 _"Mal-Xaskanat"}
        {VARIABLE lich_name_3 _"Mal-Akranbral"}
        {VARIABLE lich_name_4 _"Mal-Larakan"}
        {VARIABLE lich_name_5 _"Mal-Drakanal"}
        {VARIABLE lich_name_6 _"Mal-Ravanal"}

        {VARIABLE lich_quote_0 _"You dare to attack me? Now you will pay!"}
        {VARIABLE lich_quote_1 _"I will enjoy watching you suffer!"}
        {VARIABLE lich_quote_2 _"Death will only be the beginning of your torment!"}
        {VARIABLE lich_quote_3 _"You will serve me in death!"}
        {VARIABLE lich_quote_4 _"Allow me to free you from your wretched existence!"}
        {VARIABLE lich_quote_5 _"My master is merely toying with you!"}
    [/event]

    # The odds of Mal-Ravanal turning up are roughly
    # 0%, 3%, 7%, 13%, 22%, 42%, 100% for each respective
    # lich revealed (assuming he hasn't already).

    [event]
        name=attack
        first_time_only=no

        [filter]
            side=1
        [/filter]

        [filter_second]
            [filter_wml]
                [variables]
                    random_lich=yes
                [/variables]
            [/filter_wml]
        [/filter_second]

        {RANDOM 0..5}

        [while]
            [variable]
                name=lich_name_$random|
                equals="used"
            [/variable]

            [do]
                {RANDOM 0..6}
            [/do]
        [/while]

        # According to the WML guide, there is a chance
        # (Roughly 1e-81 at best) that we will exit the
        # while loop without a lich name. In that very
        # unlikely event, go ahead and spawn Mal-Ravanal

        {VARIABLE second_unit.name $lich_name_$random||}
        {VARIABLE second_unit.variables.random_lich no}

        [if]
            [variable]
                name=lich_name_$random
                equals= _ "Mal-Ravanal" # wmllint: ignore
            [/variable]
            [or]
                [variable]
                    name=lich_name_$random
                    equals="used" # wmllint: ignore
                [/variable]
            [/or]
            [then]
                [modify_unit]
                    [filter]
                        x,y=$second_unit.x, $second_unit.y
                    [/filter]
                    type=Lich Lord
                    id=Mal-Ravanal
                    name= _ "Mal-Ravanal"
                    profile=portraits/mal-ravanal.png
                    hitpoints=80
                    [variables]
                        random_lich=no
                    [/variables]
                [/modify_unit]

                {TEAM_COLOR_OVERRIDE id=Mal-Ravanal blue}

                [allow_recruit]
                    side=$second_unit.side
                    type="Bone Shooter,Revenant,Wraith,Shadow,Nightgaunt,Spectre,Draug,Banebow"
                [/allow_recruit]

                [gold]
                    amount=100
                    side=$second_unit.side
                [/gold]

                [message]
                    speaker=Mal-Ravanal
                    message=_"I shall destroy you personally!"
                [/message]
            [/then]

            [else]
                [unstore_unit]
                    variable=second_unit
                    find_vacant=no
                [/unstore_unit]

                [message]
                    speaker=second_unit
                    message=$lich_quote_$random||
                [/message]
            [/else]
        [/if]

        [gold]
            amount=50
            side=$second_unit.side
        [/gold]

        {VARIABLE lich_name_$random| "used"}
    [/event]

    #make sure the undead leaders don't run out of troops too easily...
    [event]
        name=die
        first_time_only=no

        [filter]
            side=2,3,4,5,6,7,8
        [/filter]

        [gold]
            side=$unit.side
            amount=$unit.cost
        [/gold]
    [/event]

    #discourage players from just killing all the liches...
    [event]
        name=die
        first_time_only=no

        [filter]
            side=2,3,4,5,6,7,8
            type=Lich
        [/filter]

        [gold]
            side=2,3,4,5,6,7,8
            amount=$lich_death_bonus
        [/gold]
        {VARIABLE_OP lich_death_bonus add 25}
    [/event]

    # Enemy unit reaches the Weldyn keep
    [event]
        name=moveto
        [filter]
            x=24
            y=14
            [not]
                side=1
            [/not]
        [/filter]
        [message]
            speaker=unit
            message= _ "Kill them all!"
        [/message]
        [message]
            speaker=Gweddry
            message= _ "They killed the royal family... now, even if we save Weldyn, Wesnoth has fallen."
        [/message]
        [endlevel]
            result=defeat
        [/endlevel]
    [/event]

    # King gets killed
    [event]
        name=last breath
        [filter]
            id=Konrad
        [/filter]
        [message]
            speaker=Konrad
            message= _ "The Kingdom of Wesnoth dies with me!"
        [/message]
        [endlevel]
            result=defeat
            save=no
        [/endlevel]
    [/event]

    {LATE_DEFEAT}
[/scenario]
