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

    name=_ "A Small Favor - Part 3"
    map_data="{campaigns/Descent_Into_Darkness/maps/A_small_favor3.map}"
    {TURNS 30 27 25}
    victory_when_enemies_defeated=no
    {UNDERGROUND}

    {SCENARIO_MUSIC underground.ogg}
    {EXTRA_SCENARIO_MUSIC loyalists.ogg}
    {EXTRA_SCENARIO_MUSIC suspense.ogg}

    # no story -- this occurs immediately after the previous scenario

    [side]
        side=1
        controller=human
        team_name=intruders
        user_team_name=_"Intruders"
        type=Apprentice Mage
        id=Malin Keshar
        name=_ "Malin Keshar"
        unrenamable=yes
        canrecruit=yes
        recruit=
        gold=0
        income=-2
        shroud=yes
        fog=no
        share_maps=yes
        share_view=yes
        {FLAG_VARIANT undead}
    [/side]

    [side]
        # Mages
        side=2
        controller=ai
        team_name=defenders
        user_team_name=_"Defenders"
        no_leader=yes
        [ai]
            village_value=0
            aggression=0.8
        [/ai]
    [/side]

    [side]
        side=3
        controller=ai
        team_name=intruders
        user_team_name=_"Intruders"
        no_leader=yes
        [ai]
            village_value=0
            aggression=0.8
            protect_leader=5
            [protect_unit]
                id=Darken Volk
                radius=6
                value=20
            [/protect_unit]
        [/ai]
        shroud=yes
        fog=no
        share_maps=no
        share_view=no
    [/side]

    [event]
        name=prestart

        [objectives]
            side=1
            [objective]
                condition=win
                description=_ "Find the book"
            [/objective]
            [objective]
                condition=lose
                description=_ "Death of Malin Keshar"
            [/objective]
            [objective]
                condition=lose
                description=_ "Death of Darken Volk"
            [/objective]
            [objective]
                condition=lose
                description=_ "Time runs out"
            [/objective]
        [/objectives]

        # Place doors

        {DOOR_OPENING_EVENT 1,3}

        {PLACE_DOOR 20 16 se}
        {PLACE_DOOR 25 14 se}
        {PLACE_DOOR 30 11 se}

        {PLACE_DOOR 15 14 se}
        {PLACE_DOOR 20 11 se}
        {PLACE_DOOR 25 9 se}

        #TODO: Create some scenery (fire, torches, ...)
        {PLACE_IMAGE items/book5.png 11 7}

        # Place mages in great hall
        {NAMED_NOTRAIT_UNIT 2 (Silver Mage) 13 8 ("") (_"Guardian")} {GUARDIAN}
        {NAMED_NOTRAIT_UNIT 2 (Red Mage)    14 9 ("") (_"Guardian")} {GUARDIAN}
        {NAMED_NOTRAIT_UNIT 2 (White Mage)  16 8 ("") (_"Guardian")} {GUARDIAN}

        # Place manor guards
        {SCATTER_UNITS 6 Pikeman,Duelist,Pikeman,Spearman,Pikeman,Swordsman 3 (
            terrain=Rr
            [not]
                x,y=25,18
                radius=4
            [/not]
            [not]
                x,y=31,15
                radius=2
            [/not]
            [not]
                x,y=11,7
            [/not]
            [not]
                [filter]
                [/filter]
            [/not]
        ) (
            side=2
            name= _ "Guard"
            random_traits=yes
        )}

        # Place mages in rooms
        {NAMED_NOTRAIT_UNIT 2 Mage 16 15 () (_"Guardian")} {GUARDIAN}
        {NAMED_NOTRAIT_UNIT 2 Mage 18 15 () (_"Guardian")} {GUARDIAN}
        {NAMED_NOTRAIT_UNIT 2 Mage 22 13 () (_"Guardian")} {GUARDIAN}
        {NAMED_NOTRAIT_UNIT 2 Mage 24 12 () (_"Guardian")} {GUARDIAN}
        {NAMED_NOTRAIT_UNIT 2 Mage 27 10 () (_"Guardian")} {GUARDIAN}
        {NAMED_NOTRAIT_UNIT 2 Mage 29 10 () (_"Guardian")} {GUARDIAN}

        # Who has the book?
        {VARIABLE hasBook none}
    [/event]

    [event]
        name=start

        [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 loyal}

            [unstore_unit]
                variable=units_inside_manor[$i]
                find_vacant=no
            [/unstore_unit]

            [recall]
                id=$units_inside_manor[$i].id
            [/recall]
        {NEXT i}

        {CLEAR_VARIABLE units_inside_manor}

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

        [redraw]
            side=1
        [/redraw]

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

            kill=yes
            variable=stored_Darken
        [/store_unit]

        {VARIABLE stored_Darken.side 3}
        {VARIABLE stored_Darken.canrecruit yes}

        [unstore_unit]
            variable=stored_Darken
        [/unstore_unit]

        {CLEAR_VARIABLE stored_Darken}

        # Create DV's units
        {GENERIC_UNIT 3 (Revenant)   26 16}
        {GENERIC_UNIT 3 (Spectre)    26 16}
        {GENERIC_UNIT 3 (Deathblade) 26 16}
        {GENERIC_UNIT 3 (Necrophage) 26 16}
        {GENERIC_UNIT 3 (Banebow)    26 16}
        {GENERIC_UNIT 3 (Nightgaunt) 26 16}

        [message]
            speaker=Malin Keshar
            message=_ "I feel the book is very close, somewhere to the northwest."
        [/message]
    [/event]

    [event]
        name=turn refresh
        first_time_only=no

        [if]
            [variable]
                name=side_number
                equals=3
            [/variable]

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

                    kill=yes
                    variable=stored_Darken
                [/store_unit]

                [if]
                    [variable]
                        name=hasBook
                        equals=none
                    [/variable]

                    [then]
                        {VARIABLE stored_Darken.goto_x 11}
                        {VARIABLE stored_Darken.goto_y 7}
                    [/then]

                    [else]
                        {VARIABLE stored_Darken.goto_x 31}
                        {VARIABLE stored_Darken.goto_y 1}
                    [/else]
                [/if]

                [unstore_unit]
                    variable=stored_Darken
                [/unstore_unit]

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

    [event]
        # Malin gets the book
        name=moveto
        [filter]
            id=Malin Keshar
            x,y=11,7
        [/filter]
        [if]
            [variable]
                name=hasBook
                equals=none
            [/variable]
            [then]
                {VARIABLE hasBook Malin}
                [message]
                    speaker=Malin Keshar
                    message=_ "We've got the book, but now how do we get out of here?"
                [/message]
                [message]
                    speaker=Darken Volk
                    message=_ "There's a small tunnel in the cellar to the northeast. It will lead us far enough out of the city that we can escape pursuit."
                [/message]
                [removeitem]
                    x,y=11,7
                [/removeitem]
                [objectives]
                    side=1
                    [objective]
                        condition=win
                        description=_ "Escape via the tunnel in the northeast cellar"
                    [/objective]
                    [objective]
                        condition=lose
                        description=_ "Death of Malin Keshar"
                    [/objective]
                    [objective]
                        condition=lose
                        description=_ "Death of Darken Volk"
                    [/objective]
                [/objectives]
                [event]
                    name=moveto
                    [filter]
                        id=Malin Keshar
                        x,y=30-31,1
                    [/filter]
                    [message]
                        speaker=Malin Keshar
                        message=_ "Phew! Let's get out of here."
                    [/message]
                    [endlevel]
                        result=victory
                        bonus=no
                        {NEW_GOLD_CARRYOVER 100}
                        carryover_report=no
                    [/endlevel]
                [/event]
            [/then]
        [/if]
    [/event]
    [event]
        # DV gets the book
        name=moveto
        [filter]
            id=Darken Volk
            x,y=11,7
        [/filter]
        [if]
            [variable]
                name=hasBook
                equals=none
            [/variable]
            [then]
                {VARIABLE hasBook Darken}
                [removeitem]
                    x,y=11,7
                [/removeitem]
                [objectives]
                    side=1
                    [objective]
                        condition=win
                        description=_ "Escape the manor"
                    [/objective]
                    [objective]
                        condition=lose
                        description=_ "Death of Malin Keshar"
                    [/objective]
                    [objective]
                        condition=lose
                        description=_ "Death of Darken Volk"
                    [/objective]
                [/objectives]
                [event]
                    name=moveto
                    [filter]
                        id=Darken Volk
                        side=3
                    [/filter]
                    [message]
                        speaker=Malin Keshar
                        message=_ "We've got the book, but now how do we get out of here?"
                    [/message]
                    [scroll_to_unit]
                        id=Darken Volk
                    [/scroll_to_unit]
                    [message]
                        speaker=Malin Keshar
                        message=_ "Wait, you aren't leaving me behind, are you?"
                    [/message]
                [/event]
                [event]
                    name=moveto
                    [filter]
                        id=Darken Volk
                        x,y=30-31,1
                    [/filter]
                    [message]
                        speaker=Darken Volk
                        message=_ "You have served me well, my apprentice."
                    [/message]
                    [message]
                        speaker=narrator
                        message=_ "As he heads into the tunnel Darken Volk collapses its entrance, leaving Malin to be captured and executed by the city guards."
                        image=wesnoth-icon.png
                    [/message]
                    [endlevel]
                        result=defeat
                    [/endlevel]
                [/event]
                [event]
                    name=moveto
                    [filter]
                        id=Malin Keshar
                        x,y=30-31,1
                    [/filter]
                    [message]
                        speaker=Malin Keshar
                        message=_ "Phew! Let's get out of here."
                    [/message]
                    [endlevel]
                        result=victory
                        bonus=no
                        {NEW_GOLD_CARRYOVER 100}
                        carryover_report=no
                    [/endlevel]
                [/event]
            [/then]
        [/if]
    [/event]

    # Defeat Conditions
    {MALIN_DEATH}
    {VOLK_DEATH2}

    [event]
        name=time over
        [message]
            speaker=narrator
            message=_ "As dawn breaks, the city guards force their way into the manor and capture the two necromancers."
            image=wesnoth-icon.png
        [/message]
        [endlevel]
            result=defeat
        [/endlevel]
    [/event]

    [event]
        name=victory

        {MODIFY_UNIT (id=Darken Volk) side 1}
    [/event]
[/scenario]
