#textdomain wesnoth-ei
[scenario]
    id=Reject
    name= _ "Weldyn Besieged"
    map_data="{campaigns/Eastern_Invasion/maps/Weldyn_Besieged.map}"
    turns=30
    next_scenario=Epilog

    {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}

    [side]
        type=Lieutenant
        id=Gweddry
        name= _ "Gweddry"
        side=1
        gold=200
        canrecruit=yes
        controller=human
        team_name=wesnothians
        user_team_name=_"Wesnothians"
    [/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=Mal-Drakanal
        name= _ "Mal-Drakanal"
        side=2
        canrecruit=yes
        controller=ai
        recruit=Bone Shooter,Revenant,Wraith,Shadow
        {GOLD 150 200 250}
        {FLAG_VARIANT undead}
        team_name=bad
        user_team_name=_"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
        {GOLD 150 200 250}
        {FLAG_VARIANT undead}
        team_name=bad
        user_team_name=_"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
        {GOLD 150 200 250}
        {FLAG_VARIANT undead}
        team_name=bad
        user_team_name=_"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
        {GOLD 150 200 250}
        {FLAG_VARIANT undead}
        team_name=bad
        user_team_name=_"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
        {GOLD 150 200 250}
        {FLAG_VARIANT undead}
        team_name=bad
        user_team_name=_"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
        {GOLD 150 200 250}
        {FLAG_VARIANT undead}
        team_name=bad
        user_team_name=_"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
        {GOLD 150 200 250}
        {FLAG_VARIANT undead}
        team_name=bad
        user_team_name=_"Undead"
    [/side]

    # wmllint: recognize Mal-Ravanal
    [event]
        name=prestart
        [objectives]
            side=1
            [objective]
                description= _ "Defeat Mal-Ravanal"
                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= _ "Letting any enemy unit on to your keep"
                condition=lose
            [/objective]
        [/objectives]
    [/event]

    [event]
        name=start
        [recall]
            id=Dacyn
        [/recall]
        [recall]
            id=Owaec
        [/recall]
        [recall]
            id=Grug
        [/recall]
        [message]
            speaker=Mal-Drakanal
            message= _ "You people are fools! You rejected my master's offer to duel... very well, you will ALL die! And know this; my master is here to enjoy your demise. For he does not fear you at all, and knows that you will never be able to find him and kill him! There are seven of us, and only ONE of us is the real leader! Ha ha ha ha! To be fair about it, we will all tell you our names if you can reach us."
        [/message]
        [message]
            speaker=Dacyn
            message= _ "Remember, we have to kill Mal-Ravanal. He's here somewhere..."
        [/message]
        {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-Ravanal"}
    [/event]

    # The odds of Mal-Ravanal turning up are
    # 0%, 4%, 10%, 20%, 40%, 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..4}

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

            [do]
                {RANDOM 0..5}
            [/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. I figured the
        # odds were bad enough that it wasn't necessary
        # to put in some sort of fallback check here...

        {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]

            [then]
                {VARIABLE second_unit.profile portraits/mal-ravanal.png}
                {VARIABLE second_unit.id "Mal-Ravanal"}

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

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

                [message]
                    speaker=Dacyn
                    message= _ "(whispered) There he is!"
                [/message]
            [/then]

            [else]
                [message]
                    speaker=Dacyn
                    message= _ "(whispered) That isn't him..."
                [/message]
            [/else]
        [/if]

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

        [message]
            speaker=second_unit
            message=_"You dare to attack me? Now you will pay!"
        [/message]

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

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

    [event]
        name=last breath
        [filter]
            id=Mal-Ravanal
        [/filter]
        [message]
            speaker=Mal-Ravanal
            message= _ "How did you know...?"
        [/message]
        [endlevel]
            result=victory
            carryover_report=no
            save=no
        [/endlevel]
    [/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 the king!"
        [/message]
        [message]
            speaker=Gweddry
            message= _ "They killed Konrad... now, even if we save Weldyn, Wesnoth has fallen."
        [/message]
        [endlevel]
            result=defeat
        [/endlevel]
    [/event]

    {LATE_DEFEAT}
[/scenario]
