#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

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

    {UNDERGROUND}

    [time_area]
        # The great hall is lit up
        {DUSK}
        x=1-12
        y=1-10
    [/time_area]

    [side]
        side=1
        controller=human
        team_name=intruders
        type=Apprentice Mage
        description=Malin Keshar
        user_description=_ "Malin Keshar"
        unrenamable=yes
        canrecruit=yes
        recruit=
        gold=0
        shroud=yes
        fog=no
        share_maps=yes
        share_view=yes
    [/side]

    [side]
        # Mages
        side=2
        controller=ai
        team_name=defenders
        no_leader=yes
        [ai]
            village_value=0
            agression=0.8
        [/ai]
    [/side]

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

    [event]
        name=prestart
        [music]
            name=underground.ogg
        [/music]
        [music]
            name=frantic.ogg
            append=yes
            ms_before=2000
            ms_after=2000
        [/music]

        [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]
        [/objectives]

        # Place doors
        {PLACE_DOOR 12 11 se 11 11 13 12}
        {PLACE_DOOR 10 13 se 9 13 11 14}
        {PLACE_DOOR 17 11 sw 16 11 18 10}
        {PLACE_DOOR 22 10 sw 21 11 23 10}

        #TODO: Create some scenery (fire, torches, ...)
        {PLACE_IMAGE items/book5.png 5 5}
        {PLACE_IMAGE scenery/signpost.png 25 1}

        # Place manor guards
        {GEN_GUARDIAN (Pikeman) (Guard) (_ "Guard") 2 14 12}
        {GEN_GUARDIAN (Duelist) (Guard) (_ "Guard") 2 16 12}
        {GEN_GUARDIAN (Pikeman) (Guard) (_ "Guard") 2 23 5}
        {GEN_GUARDIAN (Spearman) (Guard) (_ "Guard") 2 11 9}
        {GEN_GUARDIAN (Pikeman) (Guard) (_ "Guard") 2 4 9}
        {GEN_GUARDIAN (Swordsman) (Guard) (_ "Guard") 2 5 10}

        # Place mages in great hall
        {GEN_GUARDIAN (Silver Mage) (Guardian) (_ "Guardian") 2 8 4}
        {GEN_GUARDIAN (Red Mage) (Guardian) (_ "Guardian") 2 8 4}
        {GEN_GUARDIAN (White Mage) (Guardian) (_ "Guardian") 2 8 4}

        # Place mages in rooms
        {GEN_UNIT (White Mage) 2 8 12}
        {GEN_UNIT (Red Mage) 2 24 9}

        # More guards, depending on the difficulty?
#ifdef NORMAL
#ifdef HARD
        {GEN_GUARDIAN (Swordsman) (Guard) (_ "Guard") 2 14 6}
        {GEN_GUARDIAN (Spearman) (Guard) (_ "Guard") 2 19 4}
#endif
#endif

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

    [event]
        name=start
        {FOREACH ASFUnit_store i}
        {VARIABLE ASFUnit_store[$i].x 13}
        {VARIABLE ASFUnit_store[$i].y 17}
        [unstore_unit]
            variable=ASFUnit_store[$i]
            find_vacant=yes
        [/unstore_unit]
        {NEXT i}
        {CLEAR_VARIABLE ASFUnit_store}
        # All undead at this point are Malin's
        {MODIFY_UNIT (race=undead) upkeep loyal}

        {VARIABLE darkenVolkStore.x 5}
        {VARIABLE darkenVolkStore.y 18}
        {VARIABLE darkenVolkStore.side 3}
        [unstore_unit]
            variable=darkenVolkStore
            find_vacant=yes
        [/unstore_unit]
        {CLEAR_VARIABLE darkenVolkStore}

        # Create DV's units
        {GEN_UNIT (Revenant) 3 5 18}
        {GEN_UNIT (Spectre) 3 5 18}
        {GEN_UNIT (Deathblade) 3 5 18}
        {GEN_UNIT (Ghast) 3 5 18}
        {GEN_UNIT (Banebow) 3 5 18}
        {GEN_UNIT (Nightgaunt) 3 5 18}

        {MODIFY_UNIT (description=Darken Volk) canrecruit yes}
        #Go for the book
        {MODIFY_UNIT (description=Darken Volk) goto_x 5}
        {MODIFY_UNIT (description=Darken Volk) goto_y 5}

        {DIALOGUE_ASF3_START}
    [/event]

    [event]
        # Malin gets the book
        name=moveto
        [filter]
            description=Malin Keshar
            x,y=5,5
        [/filter]
        [if]
            [variable]
                name=hasBook
                equals=none
            [/variable]
            [then]
                {VARIABLE hasBook Malin}
                {DIALOGUE_ASF3_TOEXIT1}
                [removeitem]
                    x,y=5,5
                [/removeitem]
                # new goal for DV
                {MODIFY_UNIT (description=Darken Volk) goto_x 25}
                {MODIFY_UNIT (description=Darken Volk) goto_y 1}
                [objectives]
                    side=1
                    [objective]
                        condition=win
                        description=_ "Escape via the tunnel in the northeast cellar"
                    [/objective]
                [/objectives]
                [event]
                    name=moveto
                    [filter]
                        description=Darken Volk
                        x,y=25,1
                    [/filter]
                    [store_unit]
                        [filter]
                            description=Darken Volk
                        [/filter]
                        variable=darkenVolkStore
                        kill=yes
                    [/store_unit]
                [/event]
                [event]
                    name=moveto
                    [filter]
                        description=Malin Keshar
                        x,y=25,1
                    [/filter]
                    {DIALOGUE_ASF3_END}
                    [endlevel]
                        bonus=no
                        result=continue
                    [/endlevel]
                [/event]
            [/then]
        [/if]
    [/event]
    [event]
        # DV gets the book
        name=moveto
        [filter]
            description=Darken Volk
            x,y=5,5
        [/filter]
        [if]
            [variable]
                name=hasBook
                equals=none
            [/variable]
            [then]
                {VARIABLE hasBook Darken}
                [removeitem]
                    x,y=5,5
                [/removeitem]
                # new goal for DV
                {MODIFY_UNIT (description=Darken Volk) goto_x 25}
                {MODIFY_UNIT (description=Darken Volk) goto_y 1}
                [objectives]
                    side=1
                    [objective]
                        condition=win
                        description=_ "Escape the manor"
                    [/objective]
                [/objectives]
                [event]
                    name=moveto
                    [filter]
                        description=Darken Volk
                        side=3
                    [/filter]
                    {DIALOGUE_ASF3_TOEXIT2}
                [/event]
                [event]
                    name=moveto
                    [filter]
                        description=Darken Volk
                        x,y=25,1
                    [/filter]
                    {DIALOGUE_ASF3_LEFT}
                    [endlevel]
                        result=defeat
                    [/endlevel]
                [/event]
                [event]
                    name=moveto
                    [filter]
                        description=Malin Keshar
                        x,y=25,1
                    [/filter]
                    {DIALOGUE_ASF3_END}
                    [endlevel]
                        bonus=no
                        result=continue
                    [/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]

    # Victory Conditions are in the massive event on the mage lord's death

    [event]
        name=victory
        {MODIFY_UNIT (race=undead
        side=1) upkeep full}
        [if]
            [have_unit]
                description=Darken Volk
            [/have_unit]
            [then]
                [store_unit]
                    [filter]
                        description=Darken Volk
                    [/filter]
                    variable=darkenVolkStore
                    kill=yes
                [/store_unit]
                # Full heal for DV since I had some reports DV would
                # refuse to recruit if heavily injured
                {VARIABLE (darkenVolkStore.hitpoints) ($darkenVolkStore.max_hitpoints)}
            [/then]
        [/if]
    [/event]

    {@campaigns/Descent_Into_Darkness/utils/global-events.cfg}
[/scenario]
