#textdomain wesnoth-did
[scenario]
    id=08_A_Small_Favor2
    next_scenario=09_A_Small_Favor3

    name=_ "A Small Favor — Part 2"
    map_data="{campaigns/Descent_Into_Darkness/maps/08_A_Small_Favor2.map}"
    {TURNS 30 27 25}
    victory_when_enemies_defeated=no

    {SCENARIO_MUSIC loyalists.ogg}
    {EXTRA_SCENARIO_MUSIC the_king_is_dead.ogg}
    {EXTRA_SCENARIO_MUSIC siege_of_laurelmor.ogg}

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

    {UNDERGROUND}

    [side]
        side=1
        controller=human
        team_name=intruders
        user_team_name=_"Intruders"
        type=Apprentice Mage
        id=Malin Keshar
        name=_ "Malin Keshar"
        profile=portraits/malin_old.png
        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]

    # wmllint: recognize Darken Volk

    # This macro places the doors and adds the door-opening event
    {MANOR_DOORS 1}

    [event]
        name=prestart

        [objectives]
            side=1
            [objective]
                condition=win
                description=_ "Find the mage Lord Karres"
            [/objective]
            [objective]
                condition=lose
                description=_ "Death of Malin Keshar"
            [/objective]
            [objective]
                condition=lose
                description=_ "Death of Darken Volk"
            [/objective]

            {TURNS_RUN_OUT}
        [/objectives]

        # Randomize Karres’s location
        {RANDOM 0..3}

        [switch]
            variable=random

            [case]
                value=0

                {VARIABLE karres_x 10}
                {VARIABLE karres_y 18}
            [/case]

            [case]
                value=1

                {VARIABLE karres_x 3}
                {VARIABLE karres_y 11}
            [/case]

            [case]
                value=2

                {VARIABLE karres_x 14}
                {VARIABLE karres_y 3}
            [/case]

            [case]
                value=3

                {VARIABLE karres_x 28}
                {VARIABLE karres_y 3}
            [/case]
        [/switch]

        [unit]
#ifdef EASY
            type=Arch Mage
#else
            type=Great Mage
#endif
            id=Lord Karres
            name= _ "Lord Karres"
            side=2
            x,y=$karres_x,$karres_y
            canrecruit=yes
            random_traits=no
        [/unit]

        # wmllint: recognize Lord Karres

        # Place manor guards in hallways
        {NAMED_NOTRAIT_UNIT 2 (Halberdier) 10 4 () (_"Guard")} {GUARDIAN}

        [store_locations]
            terrain=Iwr
            [not]
                x,y=20,20
                radius=6
            [/not]
            [not]
                x=1-5
                y=1-5
            [/not]
            [not]
                [filter]
                [/filter]
            [/not]
            variable=possible_guard_locations
        [/store_locations]

        [set_variables]
            name=guardtypetable

            [value]
                type=Heavy Infantryman
            [/value]
            [value]
                type=Heavy Infantryman
            [/value]
            [value]
                type=Spearman
            [/value]
            [value]
                type=Spearman
            [/value]
            [value]
                type=Fencer
            [/value]
            [value]
                type=Pikeman
            [/value]
            [value]
                type=Swordsman
            [/value]
        [/set_variables]

        {VARIABLE guardtypetable_i 0}
        {VARIABLE guards_to_place 8}

        [while]
            [variable]
                name=guards_to_place
                greater_than=0
            [/variable]

            [do]
                {RANDOM 1..$possible_guard_locations.length}
                {VARIABLE_OP random sub 1}

                {NAMED_NOTRAIT_UNIT 2 $guardtypetable[$guardtypetable_i].type $possible_guard_locations[$random].x $possible_guard_locations[$random].y () (_ "Guard")} {GUARDIAN}

                [store_locations]
                    find_in=possible_guard_locations
                    [not]
                        x,y=$possible_guard_locations[$random].x,$possible_guard_locations[$random].y
                        radius=4
                    [/not]
                    variable=possible_guard_locations
                [/store_locations]

                {VARIABLE_OP guardtypetable_i add 1}

                [if]
                    [variable]
                        name=guardtypetable_i
                        numerical_equals=$guardtypetable.length
                    [/variable]

                    [then]
                        {VARIABLE guardtypetable_i 0}
                    [/then]
                [/if]

                {VARIABLE_OP guards_to_place sub 1}
            [/do]
        [/while]

        {CLEAR_VARIABLE guardtypetable,guardtypetable_i,possible_guard_locations}

        [store_locations]
            terrain=Iwr
            [not]
                x,y=20,20
                radius=6
            [/not]
            [not]
                x=1-5
                y=1-5
            [/not]
            [not]
                [filter_adjacent_location]
                    terrain=!,Iwr
                [/filter_adjacent_location]
            [/not]
            variable=possible_mage_locations
        [/store_locations]

        [set_variables]
            name=magetypetable

            [value]
                type=Mage
            [/value]
            [value]
                type=Mage
            [/value]
            [value]
                type=Mage
            [/value]
            [value]
                type=White Mage
            [/value]
            [value]
                type=Red Mage
            [/value]
            [value]
                type=Red Mage
            [/value]
#ifndef EASY
            [value]
                type=Silver Mage
            [/value]
#endif
        [/set_variables]

        {VARIABLE magetypetable_i 0}

#ifdef EASY
        {VARIABLE mages_to_place 12}
#endif
#ifdef NORMAL
        {VARIABLE mages_to_place 14}
#endif
#ifdef HARD
        {VARIABLE mages_to_place 16}
#endif

        [while]
            [variable]
                name=mages_to_place
                greater_than=0
            [/variable]

            [do]
                {RANDOM 1..$possible_mage_locations.length}
                {VARIABLE_OP random sub 1}

                {GENERIC_UNIT 2 $magetypetable[$magetypetable_i].type $possible_mage_locations[$random].x $possible_mage_locations[$random].y}

                [store_locations]
                    find_in=possible_mage_locations
                    [not]
                        x,y=$possible_mage_locations[$random].x,$possible_mage_locations[$random].y
                        radius=2
                    [/not]
                    variable=possible_mage_locations
                [/store_locations]

                {VARIABLE_OP magetypetable_i add 1}

                [if]
                    [variable]
                        name=magetypetable_i
                        numerical_equals=$magetypetable.length
                    [/variable]

                    [then]
                        {VARIABLE magetypetable_i 0}
                    [/then]
                [/if]

                {VARIABLE_OP mages_to_place sub 1}
            [/do]
        [/while]

        {CLEAR_VARIABLE magetypetable,magetypetable_i,possible_mage_locations}

        #TODO: Create some scenery (fire, torches, ...)
    [/event]

    [event]
        name=start

        [recall]
            id=Darken Volk
            x,y=20,17
        [/recall]

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

        [message]
            speaker=narrator
            message=_ "Malin and Darken Volk enter the mage lord’s manor, quickly closing the heavy gates behind them."
            image=wesnoth-icon.png
        [/message]
        [message]
            speaker=Malin Keshar
            message=_ "That gate won’t hold back the soldiers for more than a few hours. We’d better hurry."
        [/message]
    [/event]

    [event]
        name=die

        [filter]
            id=Lord Karres
        [/filter]

        {QUAKE rumble.ogg}

        [terrain]
            x,y=5,4
            terrain=Iwr
        [/terrain]

        [message]
            speaker=Darken Volk
            message=_ "I feel the breaking of an illusion spell. The book must be in the great hall, in the northwest corner of the manor. Let us move quickly now!"
        [/message]

        [objectives]
            side=1
            [objective]
                condition=win
                description=_ "Move through the passageway in the northwest leading to the great hall"
            [/objective]
            [objective]
                condition=lose
                description=_ "Death of Malin Keshar"
            [/objective]
            [objective]
                condition=lose
                description=_ "Death of Darken Volk"
            [/objective]

            {TURNS_RUN_OUT}
        [/objectives]
    [/event]

    [event]
        name=moveto
        [filter]
            x,y=1,1
            id=Malin Keshar
            [or]
                x,y=1,1
                id=Darken Volk
            [/or]
        [/filter]
        [message]
            speaker=unit
            #Possibly bad English!
            message=_ "I think I have found the passage to the great hall. Follow me quickly!"
        [/message]
        [endlevel]
            result=victory
            bonus=no
            {NEW_GOLD_CARRYOVER 100}
            carryover_report=no
        [/endlevel]
    [/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]
[/scenario]
