#textdomain wesnoth-did
[scenario]
    id=Alone_at_Last
    next_scenario=Descent_into_Darkness

    name=_ "Alone at Last"
    map_data="{campaigns/Descent_Into_Darkness/maps/Alone_at_last.map}"
    turns=-1
    victory_when_enemies_defeated=no
    {DUSK}
    {FIRST_WATCH}
    {SECOND_WATCH}
    {DAWN}
    {MORNING}
    {AFTERNOON}

    {SCENARIO_MUSIC breaking_the_chains.ogg}
    {EXTRA_SCENARIO_MUSIC vengeful.ogg}
    {EXTRA_SCENARIO_MUSIC battle.ogg}

    [story]
        [part]
            story=_ "As he enters the tunnel underneath Lord Karres's manor, Darken Volk turns and, with a word and gesture causes the entrance to collapse. The two necromancers turn and begin walking down the pitch-black tunnel, temporarily safe from pursuit."
            background=story/travel.jpg
            scale_background=no
        [/part]
        [part]
            story=_ "They continue walking through the damp, cold air all through the morning and day, finally emerging as the last rays of sun sink below the horizon."
            background=story/travel.jpg
            scale_background=no
        [/part]
        [part]
            show_title=yes
            {TO_ALONE_AT_LAST}
        [/part]
    [/story]

    [side]
        side=1
        controller=human
        type=Apprentice Mage
        id=Malin Keshar
        name=_ "Malin Keshar"
        unrenamable=yes
        canrecruit=yes
        recruit=Walking Corpse,Vampire Bat,Ghost,Ghoul,Skeleton Archer,Skeleton,Dark Adept DiD
        fog=no
        shroud=no
        gold=200
        {FLAG_VARIANT undead}
    [/side]

    [side]
        # Darken Volk
        side=2
        controller=ai
        no_leader=yes
        {GOLD 300 340 380}
        recruit=Blood Bat,Ghost,Wraith,Necrophage,Skeleton Archer,Bone Shooter,Skeleton,Revenant,Deathblade,Dark Adept DiD,Dark Sorcerer DiD
        {FLAG_VARIANT undead}
        [ai]
            passive_leader=yes
            village_value=0.2
            villages_per_scout=10
            [target]
                id=Malin Keshar
                value=2
            [/target]
            protect_leader=4
            number_of_possible_recruits_to_force_recruit=1
        [/ai]
    [/side]

    {LIMIT_CONTEMPORANEOUS_RECRUITS 2 "Blood Bat" 2}
    {LIMIT_CONTEMPORANEOUS_RECRUITS 2 "Wraith" 2}
    {LIMIT_CONTEMPORANEOUS_RECRUITS 2 "Necrophage" 2}
    {LIMIT_CONTEMPORANEOUS_RECRUITS 2 "Bone Shooter" 2}
    {LIMIT_CONTEMPORANEOUS_RECRUITS 2 "Revenant" 2}
    {LIMIT_CONTEMPORANEOUS_RECRUITS 2 "Deathblade" 2}
    {LIMIT_CONTEMPORANEOUS_RECRUITS 2 "Dark Sorcerer DiD" 1}

    [side]
        # Paladins and co.
        side=3
        team_name=wesnothians
        user_team_name=_"Wesnothians"
        controller=ai
        no_leader=yes
        gold=0
        income=-2
        recruit=Spearman,Heavy Infantryman,White Mage,Mage,Paladin
        [ai]
            village_value=0.1
            villages_per_scout=10
            [target]
                side=2
                value=3
            [/target]
            [target]
                side=1
                value=2
            [/target]
            #recruitment_pattern=fighter,healer
        [/ai]
    [/side]

#ifdef EASY
    {LIMIT_CONTEMPORANEOUS_RECRUITS 3 "Paladin" 2}
#else
    {LIMIT_CONTEMPORANEOUS_RECRUITS 3 "Paladin" 3}
#endif

    [side]
        # Dela Keshar
        side=4
        team_name=wesnothians
        user_team_name=_"Wesnothians"
        controller=ai
        no_leader=yes
        gold=0
        income=-2
        recruit=Spearman,Bowman,Swordsman,Longbowman,Thug,Poacher,Footpad
        [ai]
            passive_leader=yes
            [target]
                id=Malin Keshar
                value=2
            [/target]
        [/ai]
    [/side]

    [event]
        name=prestart

        [objectives]
            side=1
            [objective]
                condition=win
                description=_ "Take the book back from Darken Volk"
            [/objective]
            [objective]
                condition=lose
                description=_ "Death of Malin Keshar"
            [/objective]

            note={NO_EARLY_FINISH_BONUS_NOTE}+{NEW_GOLD_CARRYOVER_NOTE_40}
        [/objectives]

        # {SCATTER_IMAGE (terrain=Gg) 3 scenery/oak-leaning.png}

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

            kill=yes
            variable=units_inside_manor
        [/store_unit]

        {FOREACH units_inside_manor i}
            {VARIABLE units_inside_manor[$i].upkeep full}

            [unstore_unit]
                variable=units_inside_manor[$i]
                find_vacant=no
            [/unstore_unit]
        {NEXT i}

        {CLEAR_VARIABLE units_inside_manor}

        # Scenario variables
        {VARIABLE bookX -1}
        {VARIABLE bookY -1}
    [/event]

    [event]
        name=start

        [recall]
            id=Darken Volk
            x,y=2,26
        [/recall]

        [store_unit]
            [filter]
                id=Darken Volk
            [/filter]

            kill=yes
            variable=stored_Darken
        [/store_unit]

        {VARIABLE stored_Darken.side 2}
        {VARIABLE stored_Darken.canrecruit yes}
        {VARIABLE stored_Darken.role book_carrier}

        # removes the loyal trait
        {CLEAR_VARIABLE stored_Darken.modifications.trait[0]}
        {CLEAR_VARIABLE stored_Darken.overlays}

        [unstore_unit]
            variable=stored_Darken
        [/unstore_unit]

        {CLEAR_VARIABLE stored_Darken}

        {MOVE_UNIT (id=Malin Keshar) 7 22}

        [message]
            speaker=Malin Keshar
            message=_ "Well, we made it out. We got your precious book. What's in it, anyway? And what do we do now?"
        [/message]
        [message]
            speaker=Darken Volk
            message=_ "We? WE do nothing. You are no longer my apprentice."
        [/message]

        {MOVE_UNIT (id=Darken Volk) 8 21}

        [if]
            [variable]
                name=hasBook
                equals=Malin
            [/variable]
            [then]
                # Malin has the book
                [message]
                    speaker=Darken Volk
                    message=_ "Now give the book to me."
                [/message]
                [message]
                    speaker=narrator
                    message=_ "Darken Volk grabs the mysterious book from Malin's hands and stalks away."
                    image=wesnoth-icon.png
                [/message]
                [message]
                    speaker=Malin Keshar
                    message=_ "You can't just leave me here!"
                [/message]
            [/then]
            [else]
                # DV already has book
                [message]
                    speaker=Darken Volk
                    message=_ "Now go your own way, and I will go mine."
                [/message]
                [message]
                    speaker=Malin Keshar
                    message=_ "You can't just leave me here!"
                [/message]
            [/else]
        [/if]
        {CLEAR_VARIABLE hasBook}

        [unit_overlay]
            x,y=8,21
            image="misc/book-icon.png"
        [/unit_overlay]

        {MOVE_UNIT (id=Darken Volk) 19 19}
        {DELAY 500}
        {MOVE_UNIT (id=Malin Keshar) 7 21}
        {DELAY 500}
        {MOVE_UNIT (id=Malin Keshar) 8 20}
        {DELAY 500}
        {MOVE_UNIT (id=Malin Keshar) 9 20}
        {DELAY 500}

        {CREATE_ADVISOR}

        [message]
            role=advisor
            message=_ "Master, do you not see? He has merely been using you to get that book!"
        [/message]
        [message]
            speaker=Malin Keshar
            message=_ "What do you mean?"
        [/message]
        [message]
            role=advisor
            message=_ "He cares not about your noble quest to push back the orcs. He needed your help to fight the mages! The book is rightfully yours!"
        [/message]
        [message]
            speaker=Malin Keshar
            message=_ "Wait!"
        [/message]
        [message]
            speaker=Malin Keshar
            message=_ "All of the training you gave me, that was just so I would help you get that book! Return it to me now, or I will take it by force."
        [/message]
        [message]
            speaker=Darken Volk
            message=_ "You are even more of a fool than I thought."
        [/message]

        {MOVE_UNIT (id=Malin Keshar) 5 17}
        {MOVE_UNIT (id=Darken Volk) 26 10}

        # Make camp
        [terrain]
            x=25-27,26
            y=10-11,9
            terrain=Ce
        [/terrain]
        [terrain]
            x,y=26,10
            terrain=Ke
        [/terrain]
    [/event]

    # In-scenario events
    [event]
        name=attack
        [filter]
            side=1
        [/filter]
        [filter_second]
            side=2
        [/filter_second]

        [message]
            speaker=Malin Keshar
            message=_ "You let me believe Parthyn would accept me back so when they didn't I would be willing to attack my own countrymen!"
        [/message]
        [message]
            speaker=Darken Volk
            message=_ "What you let yourself believe is no fault of mine. Cease your foolish prattle. I gave you more power than you could have possibly dreamed. Use it to fight the orcs that annoy you so, not me."
        [/message]
        [message]
            speaker=Malin Keshar
            message=_ "So it is true! You took my home away from me. You took Drogan away. Now that all of that is gone, I will at least keep the fruits of my labors."
        [/message]
        [message]
            speaker=Darken Volk
            message=_ "Hardly. I will crush you easily."
        [/message]
        [message]
            speaker=Malin Keshar
            message=_ "Perhaps, but I have nothing left to lose."
        [/message]
    [/event]

    [event]
        name=attack
        [filter]
            side=1
        [/filter]
        [filter_second]
            id=Darken Volk
        [/filter_second]

        [message]
            speaker=Malin Keshar
            message=_ "Now you shall pay for what you have done to me."
        [/message]
        [message]
            speaker=Darken Volk
            message=_ "Done? I have done nothing but give you an opportunity. You have chosen your own path. Now leave me alone!"
        [/message]
    [/event]

    # Paladins!
    [event]
        name=turn 8

        # Fake move the paladin in, set up a camp, have some
        # dialog, and give him another paladin and a mage of
        # light
        {INCIDENTAL_MUSIC loyalists.ogg}
        [move_unit_fake]
            type=Paladin
            x=34,33
            y=27,26
            side=3
        [/move_unit_fake]
        [unit]
            type=Paladin
            side=3
            x,y=33,26
            id=Sir Cadaeus
            name=_ "Sir Cadaeus"
            canrecruit=yes
            animate=no
        [/unit]
        {GENERIC_UNIT 3 (Paladin)       34 25}
        {GENERIC_UNIT 3 (Paladin)       33 27}
        {GENERIC_UNIT 3 (Mage of Light) 32 25}
        # Make camp
        [terrain]
            x=32-34,33
            y=25-26,27
            terrain=Ce
        [/terrain]
        [terrain]
            x,y=33,26
            terrain=Ke
        [/terrain]

        [modify_side]
            side=3
            {QUANTITY gold 100 150 200}
            income=20
        [/modify_side]

        [message]
            speaker=Sir Cadaeus
            message=_ "Your travels in this region have not gone unnoticed, foul necromancers! We have been tracking you for weeks, with our purpose renewed this morning upon hearing of the disaster you wreaked upon the goodly city of Tath."
        [/message]
        [message]
            speaker=Sir Cadaeus
            message=_ "Do not even think of running away - my horsemen can easily catch you. Yes, on this day you shall have to answer for your innumerable crimes."
        [/message]
        [message]
            speaker=Darken Volk
            message=_ "We'll see how well your pompous words protect you from the blades of the undead once I have finished this upstart."
        [/message]
    [/event]

    # Dela joins the party
    [event]
        name=side 4 turn 11

        # Find a good location with no adjacent enemies for Dela to set up camp
        # at
        [store_locations]
            x=20-25
            y=2-3

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

            variable=possible_camp_locs
        [/store_locations]

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

            # If such locations were found, pick one randomly...
            [then]
                {VARIABLE_OP camp_loc_i rand "0..$($possible_camp_locs.length - 1)"}
            [/then]

            # ...but if not, then just pick any non-occupied location
            [else]
                [store_locations]
                    x=20-25
                    y=2-3

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

                    variable=possible_camp_locs
                [/store_locations]

                {VARIABLE_OP camp_loc_i rand "0..$($possible_camp_locs.length - 1)"}
            [/else]
        [/if]

        # Fake move Dela in, set up camp, have some
        # dialog. She doesn't bring anyone with her.
        {INCIDENTAL_MUSIC legends_of_the_north.ogg}
        [move_unit_fake]
            type=Frontier Baroness
            x=23,$possible_camp_locs[$camp_loc_i].x
            y=1,$possible_camp_locs[$camp_loc_i].y
            side=4
        [/move_unit_fake]
        [unit]
            type=Frontier Baroness
            side=4
            x=$possible_camp_locs[$camp_loc_i].x
            y=$possible_camp_locs[$camp_loc_i].y
            id=Dela Keshar
            name=_ "Dela Keshar"
            canrecruit=yes
            animate=no
            profile=portraits/dela.png
            moves=0
        [/unit]

        # Make camp
        [store_locations]
            x=$possible_camp_locs[$camp_loc_i].x
            y=$possible_camp_locs[$camp_loc_i].y
            radius=1
            variable=camp_hexes
        [/store_locations]

        {FOREACH camp_hexes i}
            [terrain]
                x=$camp_hexes[$i].x
                y=$camp_hexes[$i].y
                terrain=Ce
            [/terrain]
        {NEXT i}

        [terrain]
            x=$possible_camp_locs[$camp_loc_i].x
            y=$possible_camp_locs[$camp_loc_i].y
            terrain=Ke
        [/terrain]

        {CLEAR_VARIABLE possible_camp_locs,camp_loc_i,camp_hexes}

        [modify_side]
            side=4
            {QUANTITY gold 150 200 250}
            income=20
        [/modify_side]

        [message]
            speaker=Dela Keshar
            message=_ "I told you, Malin, that I would find you again, and I have kept my word. I will not suffer you to bring any further harm to my people, and so you will not live to see another sunset."
        [/message]
        [message]
            speaker=Malin Keshar
            message=_ "You wrong me, sister. I have only tried to help. Have you not seen fewer orcs this past summer?"
        [/message]
        [message]
            speaker=Dela Keshar
            message=_ "I will not listen to any more of your lies. Men, attack!"
        [/message]
    [/event]

    [event]
        name=last breath
        [filter]
            id=Darken Volk
        [/filter]

        [message]
            speaker=Darken Volk
            message=_ "With my dying breath I curse you, Malin the Damned!"
        [/message]
        [message]
            speaker=Malin Keshar
            message=_ "Now I've just got to get that book out of the battleground!"
        [/message]

        [objectives]
            side=1
            [objective]
                condition=win
                description=_ "Bring the book back to Malin's castle"
            [/objective]
            [objective]
                condition=lose
                description=_ "Death of Malin Keshar"
            [/objective]

            note={NO_EARLY_FINISH_BONUS_NOTE}+{NEW_GOLD_CARRYOVER_NOTE_40}
        [/objectives]

        [message]
            speaker=narrator
            message=_ "Pick up the book by moving a unit onto it. Bats and ghosts cannot carry the book."
            image=wesnoth-icon.png
        [/message]
    [/event]

    [event]
        name=die
        first_time_only=no
        [filter]
            role=book_carrier
        [/filter]
        {VARIABLE bookX $x1}
        {VARIABLE bookY $y1}
        {PLACE_IMAGE (items/book5.png) ($bookX) ($bookY)}
    [/event]

    [event]
        name=moveto
        first_time_only=no
        [filter]
            x=$bookX
            y=$bookY
            side=1
            [not]
                type=Ghost,Wraith,Spectre,Shadow,Nightgaunt,Vampire Bat,Blood Bat,Dread Bat
            [/not]
        [/filter]
        [removeitem]
            x=$x1
            y=$y1
            image="items/book5.png"
        [/removeitem]
        [role]
            x=$x1
            y=$y1
            role=book_carrier
        [/role]
        [unit_overlay]
            x=$x1
            y=$y1
            image="misc/book-icon.png"
        [/unit_overlay]

        [message]
            speaker=unit
            message=_ "I have the book."
        [/message]

        {VARIABLE bookX -1}
        {VARIABLE bookY -1}
    [/event]

    #Inform the player if a unit cannot pick up the book
    [event]
        name=moveto
        first_time_only=no
        [filter]
            x=$bookX
            y=$bookY
            side=1
            type=Ghost,Wraith,Spectre,Shadow,Nightgaunt,Vampire Bat,Blood Bat,Dread Bat
        [/filter]

        [message]
            speaker=unit
            #Possibly bad English!
            message=_ "I am sorry master, that is too heavy for me."
        [/message]

        [allow_undo]
        [/allow_undo]
    [/event]

    # Defeat Conditions
    {MALIN_DEATH}

    # Victory Condition
    [event]
        name=moveto
        [filter]
            role=book_carrier
            side=1
            [filter_location]
                terrain=C*,K*
                [and]
                    x,y=5,17
                    radius=2
                [/and]
            [/filter_location]
        [/filter]
        {MOVE_UNIT (id=Malin Keshar) 1 12}

        [message]
            speaker=Malin Keshar
            message=_ "The paladins will not be able to pursue me through the mountains, and Dela will not dare take the soldiers too far from Parthyn."
        [/message]

        [endlevel]
            result=victory
            bonus=no
            {NEW_GOLD_CARRYOVER 40}
        [/endlevel]
    [/event]

    [event]
        name=victory

        [store_unit]
            [filter]
                role=book_carrier
            [/filter]

            kill=no
            variable=book_carrier
        [/store_unit]

        [remove_unit_overlay]
            x,y=$book_carrier.x,$book_carrier.y
            image="misc/book-icon.png"
        [/remove_unit_overlay]

        {CLEAR_VARIABLE book_carrier}
        {CLEAR_VARIABLE bookX}
        {CLEAR_VARIABLE bookY}
    [/event]
[/scenario]
