#textdomain wesnoth-ei
#
# NOTE: This level is intended as a challenging final scenario.
#       Players without sufficient 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=unlimited
    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/final_battle.cfg}

    {EI_TRACK {JOURNEY_17_NEW} }

    [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
        facing=sw
        [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
        facing=sw
        [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
        facing=nw
        [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
        facing=nw
        [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
        facing=ne
        [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
        facing=se
        [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
        facing=se
        [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

        {VARIABLE liches_to_kill {ON_DIFFICULTY 2 3 4}}

        [objectives]
            side=1
            [objective]
                description= _ "Kill $liches_to_kill liches to reveal Mal-Ravanal"
                condition=win
            [/objective]
            [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]

        [scroll_to]
            x,y=51,1
        [/scroll_to]
        [delay]
            time=100
        [/delay]
        [scroll_to]
            x,y=51,25
        [/scroll_to]
        [delay]
            time=100
        [/delay]
        [scroll_to]
            x,y=1,25
        [/scroll_to]
        [delay]
            time=100
        [/delay]
        [scroll_to]
            x,y=1,1
        [/scroll_to]
        [delay]
            time=100
        [/delay]

        [message]
            speaker=Gweddry
            message= _ "But how? We don't know which one of them he is."
        [/message]

        [message]
            speaker=Dacyn
            message= _ "They are channeling some dark magic to hide themselves. We have to kill some of them to break the spell."
        [/message]

        [set_variables]
            name=random_liches

            [value]
                name= _ "Mal-Hadanak"
                quote= _ "You dare to attack me? Now you will pay!"
            [/value]
            [value]
                name= _ "Mal-Katklagad"
                quote= _ "I will enjoy watching you suffer!"
            [/value]
            [value]
                name= _ "Mal-Xaskanat"
                quote= _ "Death will only be the beginning of your torment!"
            [/value]
            [value]
                name= _ "Mal-Akranbral"
                quote= _ "You will serve me in death!"
            [/value]
            [value]
                name= _ "Mal-Larakan"
                quote= _ "Allow me to free you from your wretched existence!"
            [/value]
            [value]
                name= _ "Mal-Drakanal"
                quote= _ "My master is merely toying with you!"
            [/value]
        [/set_variables]

        [store_unit]
            [filter]
                type=Lich
                canrecruit=yes
            [/filter]

            kill=no
            variable=liches
        [/store_unit]

        [foreach]
            array=liches

            [do]
                [object]
                    silent=yes

                    [filter]
                        x,y=$this_item.x,$this_item.y
                    [/filter]

                    [effect]
                        apply_to=halo
                        halo="halo/holy/light-beam-5.png~CROP(0,0,150,500)~SCALE(108,144)~CS(-159,-255,-159)~MASK(terrain/misc/smoke-A[01~12].png~SCALE(108,144)):100"
                    [/effect]
                [/object]
            [/do]
        [/foreach]

        {CLEAR_VARIABLE liches}
    [/event]

    # Reveals the primary unit as Mal-Ravanal
    [event]
        name=reveal ravanal

        [modify_unit]
            [filter]
                find_in=unit
            [/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}

        [object]
            silent=yes

            [filter]
                id=Mal-Ravanal
            [/filter]

            [effect]
                apply_to=halo
                halo=""
            [/effect]
        [/object]

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

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

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

    # When attacking a random lich, reveal it (the last one will be Mal-Ravanal)
    [event]
        name=attack
        first_time_only=no

        [filter]
            side=1
        [/filter]

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

        [filter_condition]
            [have_unit]
                [filter_wml]
                    [variables]
                        random_lich=yes
                    [/variables]
                [/filter_wml]

                [not]
                    x,y=$x2,$y2
                [/not]
            [/have_unit]
        [/filter_condition]

        {RANDOM "0..$($random_liches.length - 1)"}

        {VARIABLE second_unit.name $random_liches[$random].name}
        {VARIABLE second_unit.variables.random_lich no}

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

        [object]
            silent=yes

            [filter]
                find_in=second_unit
            [/filter]

            [effect]
                apply_to=halo
                halo=""
            [/effect]
        [/object]

        [message]
            speaker=second_unit
            message=$random_liches[$random].quote
        [/message]

        {CLEAR_VARIABLE random_liches[$random]}
    [/event]

    # If only one random lich remains (all others have been attacked but not
    # enough of them killed to break the spell), then just reveal Mal-Ravanal
    # since the player already knows where he is
    [event]
        name=attack end

        [filter_condition]
            [have_unit]
                [filter_wml]
                    [variables]
                        random_lich=yes
                    [/variables]
                [/filter_wml]

                count=1
            [/have_unit]
        [/filter_condition]

        [fire_event]
            name=reveal ravanal

            [primary_unit]
                [filter_wml]
                    [variables]
                        random_lich=yes
                    [/variables]
                [/filter_wml]
            [/primary_unit]
        [/fire_event]
    [/event]

    # Reveal remaining liches when X of them have been killed
    [event]
        name=die
        first_time_only=no

        [filter]
            canrecruit=yes
            type_adv_tree=Lich
        [/filter]

        {VARIABLE_OP liches_killed add 1}

        [if]
            [variable]
                name=liches_killed
                greater_than_equal_to=$liches_to_kill
            [/variable]
            [not]
                [have_unit]
                    id=Mal-Ravanal
                [/have_unit]
            [/not]

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

                [color_adjust]
                    red,green,blue=127,64,127
                [/color_adjust]
                [delay]
                    time=100
                [/delay]
                [color_adjust]
                    red,green,blue=-127,-191,-127
                [/color_adjust]
                [delay]
                    time=100
                [/delay]
                [color_adjust]
                    red,green,blue=127,64,127
                [/color_adjust]
                [delay]
                    time=100
                [/delay]
                [color_adjust]
                    red,green,blue=0,0,0
                [/color_adjust]

                [message]
                    speaker=Owaec
                    message= _ "Their spell is broken! There he is!"
                [/message]

                [store_unit]
                    [filter]
                        [filter_wml]
                            [variables]
                                random_lich=yes
                            [/variables]
                        [/filter_wml]

                        [not]
                            x,y=$x2,$y2
                        [/not]
                    [/filter]

                    kill=no
                    variable=remaining_random_liches
                [/store_unit]

                {VARIABLE longest_path_to_dacyn 0}
                {VARIABLE farthest_lich_side ""}

                [foreach]
                    array=remaining_random_liches

                    [do]
                        [find_path]
                            [traveler]
                                canrecruit=yes
                                side=$this_item.side
                            [/traveler]

                            [destination]
                                [filter]
                                    id=Dacyn
                                [/filter]
                            [/destination]

                            allow_multiple_turns=yes
                            check_zoc=no
                            variable=path_to_dacyn
                        [/find_path]

                        [if]
                            [variable]
                                name=longest_path_to_dacyn
                                less_than=$path_to_dacyn.hexes
                            [/variable]

                            [then]
                                {VARIABLE longest_path_to_dacyn $path_to_dacyn.hexes}
                                {VARIABLE farthest_lich_side $this_item.side}
                            [/then]
                        [/if]
                    [/do]
                [/foreach]

                [scroll_to_unit]
                    canrecruit=yes
                    side=$farthest_lich_side
                [/scroll_to_unit]

                [fire_event]
                    name=reveal ravanal

                    [primary_unit]
                        canrecruit=yes
                        side=$farthest_lich_side
                    [/primary_unit]
                [/fire_event]

                [store_unit]
                    [filter]
                        find_in=remaining_random_liches

                        [not]
                            side=$farthest_lich_side
                        [/not]
                    [/filter]

                    kill=no
                    variable=remaining_random_liches
                [/store_unit]

                [foreach]
                    array=remaining_random_liches

                    [do]
                        {RANDOM "0..$($random_liches.length - 1)"}

                        {VARIABLE this_item.name $random_liches[$random].name}
                        {VARIABLE this_item.variables.random_lich no}

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

                        [object]
                            silent=yes

                            [filter]
                                x,y=$this_item.x,$this_item.y
                            [/filter]

                            [effect]
                                apply_to=halo
                                halo=""
                            [/effect]
                        [/object]

                        {CLEAR_VARIABLE random_liches[$random]}
                    [/do]
                [/foreach]

                {CLEAR_VARIABLE longest_path_to_dacyn,farthest_lich_side,path_to_dacyn,liches_killed,remaining_random_liches}
            [/then]
        [/if]
    [/event]

    # Don't allow the liches to be baited out of their castles
    [event]
        name=turn refresh
        first_time_only=no

        [filter_condition]
            [variable]
                name=side_number
                greater_than=1
            [/variable]
        [/filter_condition]

        [modify_unit]
            [filter]
                side=$side_number
                canrecruit=yes
            [/filter]

            moves=0
        [/modify_unit]
    [/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}

    {HERODEATH_GWEDDRY}
    {HERODEATH_DACYN}
    {HERODEATH_OWAEC}
    {HERODEATH_GRUG}
    {HERODEATH_ENGINEER}
[/scenario]
