#textdomain wesnoth-did

[scenario]
    id=07b_A_Small_Favor2
    name= _ "A Small Favor — II"
    map_file=07b_A_Small_Favor2.map
    {TURNS 25 22 19}
    next_scenario=07c_A_Small_Favor3
    victory_when_enemies_defeated=no

    [time]
        id=indoors
        name= _ "Indoors"
        image=misc/time-schedules/schedule-indoors.png
        lawful_bonus=-10
    [/time]

    {SCENARIO_MUSIC       casualties_of_war.ogg}
    {EXTRA_SCENARIO_MUSIC battle.ogg}
    {EXTRA_SCENARIO_MUSIC siege_of_laurelmor.ogg}

    # No story; this occurs immediately after the previous scenario

    # wmllint: validate-off
    [side]
        side=1
        controller=human
        gold=0
        team_name=intruders
        user_team_name= _ "Intruders"
        {FLAG_VARIANT undead}
        color=darkblue

        income=-2

        # wmllint: recognize Malin Keshar
        {CHARACTER_STATS_MALIN_KESHAR}

        shroud=yes
        facing=nw
    [/side]
    # wmllint: validate-on

    # Mages
    [side]
        side=2
        controller=ai
        team_name=defenders
        user_team_name= _ "Defenders"
        no_leader=yes
        hidden=yes
        color=purple

        [ai]
            village_value=0
            aggression=0.8
        [/ai]
    [/side]

    # City guard
    [side]
        side=3
        controller=ai
        team_name=defenders
        user_team_name= _ "Defenders"
        no_leader=yes
        hidden=yes
        {FLAG_VARIANT loyalist}
        color=gold
        [ai]
            village_value=0
            aggression=0.8
        [/ai]
    [/side]

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

    # Scenery
    {PLACE_IMAGE items/ankh-necklace.png 3 15}
    {PLACE_IMAGE items/ball-blue.png 2 10}
    {PLACE_IMAGE items/ball-magenta.png 25 2}
    {PLACE_IMAGE items/ball-green.png 8 2}
    {PLACE_IMAGE items/stone-tablet.png 6 16}
    {PLACE_IMAGE items/stone-tablet.png 15 13}
    {PLACE_IMAGE items/ornate2.png 17 1}
    {PLACE_IMAGE items/potion-blue.png 26 4}
    {PLACE_IMAGE items/potion-grey.png 27 5}
    {PLACE_IMAGE items/potion-green.png 27 6}
    {PLACE_IMAGE items/potion-red.png 25 6}
    {PLACE_IMAGE items/potion-yellow.png 26 6}
    {PLACE_IMAGE items/altar-evil.png 26 5}
    {PLACE_IMAGE items/barrel.png 24 10}
    {PLACE_IMAGE items/barrel.png 25 10}
    {PLACE_IMAGE items/barrel.png 22 9}
    {PLACE_IMAGE items/box.png 25 9}
    {PLACE_IMAGE items/box.png 22 11}
    {PLACE_IMAGE items/box.png 21 12}
    {PLACE_IMAGE items/box.png 9 7}
    {PLACE_IMAGE items/chest-open.png 8 7}
    {PLACE_IMAGE items/chest-plain-open.png 9 9}
    {PLACE_IMAGE items/barrel.png 8 9}
    {PLACE_IMAGE items/altar.png 15 2}
    {PLACE_IMAGE items/holy-water.png 17 1}
    {PLACE_IMAGE items/holy-water.png 18 1}
    {PLACE_IMAGE items/ring-white.png 13 3}
    {PLACE_IMAGE items/ring-red.png 24 1}

    {PLACE_IMAGE items/dragonstatue.png 11 12}
    {PLACE_IMAGE items/dragonstatue.png 10 12}
    {PLACE_IMAGE items/dragonstatue.png 21 3}
    {PLACE_IMAGE scenery/monolith4.png 14 12}
    {PLACE_IMAGE scenery/monolith3.png 13 15}
    {PLACE_IMAGE scenery/monolith1.png 17 14}

    [event]
        name=prestart

        {COLOR_ADJUST -17 -17 -17}

        [objectives]
            side=1
            [objective]
                condition=win
                description= _ "Find Lady Karae"
            [/objective]
            [objective]
                condition=lose
                description= _ "Death of Malin Keshar"
            [/objective]
            [objective]
                condition=lose
                description= _ "Death of Darken Volk"
            [/objective]

            {TURNS_RUN_OUT}

            [gold_carryover]
                bonus=no
                carryover_percentage=100
            [/gold_carryover]
        [/objectives]

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

        [switch]
            variable=random
            [case]
                value=0
                {VARIABLE karae_x 1}
                {VARIABLE karae_y 11}
            [/case]

            [case]
                value=1
                {VARIABLE karae_x 8}
                {VARIABLE karae_y 1}
            [/case]

            [case]
                value=2
                {VARIABLE karae_x 13}
                {VARIABLE karae_y 3}
            [/case]

            [case]
                value=3
                {VARIABLE karae_x 24}
                {VARIABLE karae_y 1}
            [/case]
        [/switch]

        [unit]
            type=Silver Mage
            id=Lady Karae
            name= _ "Lady Karae"
            profile=portraits/karae.webp
            gender=female
            side=2
            x,y=$karae_x,$karae_y
            canrecruit=yes
            random_traits=no
        [/unit]

        {CLEAR_VARIABLE karae_x,karae_y}

        [set_variables]
            name=guardtypetable
            [value]
                type=Shock Trooper
            [/value]
            [value]
                type=Spearman
            [/value]
#ifndef HARD
            [value]
                type=Fencer
            [/value]
#endif
            [value]
                type=Duelist
            [/value]
            [value]
                type=Pikeman
            [/value]
            [value]
                type=Swordsman
            [/value]
#ifndef EASY
            [value]
                type=Swordsman
            [/value]
            [value]
                type=Duelist
            [/value]
#endif
#ifdef HARD
            [value]
                type=Swordsman
            [/value]
            [value]
                type=Pikeman
            [/value]
            [value]
                type=Duelist
            [/value]
#endif
            [value]
                type=Heavy Infantryman
            [/value]
            [value]
                type=Heavy Infantryman
            [/value]
            [value]
                type=Spearman
            [/value]
            [value]
                type=Fencer
            [/value]
            [value]
                type=Fencer
            [/value]
        [/set_variables]

        [random_placement]
            num_items={ON_DIFFICULTY 5 6 7}
            variable=loc
            min_distance=3
            [filter_location]
                terrain=Iwr,Iwc,Iwo
                [not]
                    x=1-5
                    y=1-5
                [/not]
                [not]
                    [filter][/filter]
                [/not]
            [/filter_location]
            [command]
                {NAMED_NOTRAIT_UNIT 2 "$guardtypetable[$($loc.n % $guardtypetable.length)].type"
                # po: Unit name
                $loc.x $loc.y () (_ "Guard")
                # po: Unit name
                FEMALE_NAME=_"female^Guard"} {GUARDIAN}
            [/command]
        [/random_placement]

        [set_variables]
            name=magetypetable
            [value]
                type=Mage
            [/value]
            [value]
                type=White Mage
            [/value]
            [value]
                type=Mage
            [/value]
            [value]
                type=Red Mage
            [/value]
            [value]
                type=Red Mage
            [/value]
#ifdef EASY
            [value]
                type=Mage
            [/value]
#endif
#ifndef EASY
            [value]
                type=White Mage
            [/value]
            [value]
                type=Red Mage
            [/value]
#endif
#ifdef HARD
            [value]
                type=Red Mage
            [/value]
            [value]
                type=White Mage
            [/value]
#endif
            [value]
                type=Mage
            [/value]
            [value]
                type=Mage
            [/value]
            [value]
                type=Mage
            [/value]
        [/set_variables]

        [random_placement]
            num_items={ON_DIFFICULTY 6 7 8}
            variable=loc
            min_distance=2
            [filter_location]
                terrain=Iwr,Iwc,Iwo
                [not]
                    x=1-5
                    y=1-5
                [/not]
                [not]
                    [filter][/filter]
                [/not]
            [/filter_location]
            [command]
                {GENERIC_UNIT 2 "$magetypetable[$($loc.n % $magetypetable.length)].type" $loc.x $loc.y}
            [/command]
        [/random_placement]

        # this code is a handler in case this scenario is reached via debug or an old save which did not include this variable
        [lua]
            code = << if wml.variables["S7_turns"] == nil then wml.variables["S7_turns"] = 5 end >>
        [/lua]

        [set_variable]
            name=S7_turns
            value=$(min({ON_DIFFICULTY 7 6 5},($S7_turns)))
        [/set_variable]
        [set_variable]
            name=spawn1
            value=$("{ON_DIFFICULTY 7 6 5} + ($S7_turns)")
        [/set_variable]
        [set_variable]
            name=spawn2
            value=$("{ON_DIFFICULTY 14 12 10} + ($S7_turns)")
        [/set_variable]

        [recall]
            id=Darken Volk
            x,y=18,15
        [/recall]

        # We want Volk facing another direction
        {MODIFY_UNIT (id=Darken Volk) facing ne}
    [/event]

    # le Volk is very chatty in this scenario because he's excited to get his book
    # Malin is still pretty quiet here
    [event]
        name=start

        [remove_shroud]
            side=1
            x,y=24,19
            radius=3
        [/remove_shroud]

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

            variable=units_inside_manor
        [/store_unit]

        [foreach]
            array=units_inside_manor
            [do]
                [recall]
                    id=$this_item.id
                [/recall]

                [modify_unit]
                    [filter]
                        id=$this_item.id
                    [/filter]

                    [object]
                        duration=scenario
                        [effect]
                            apply_to=loyal
                        [/effect]
                    [/object]
                [/modify_unit]

                [redraw]
                    clear_shroud=yes
                    side=1
                [/redraw]
            [/do]
        [/foreach]

        {CLEAR_VARIABLE units_inside_manor}

        [message]
            speaker=narrator
            image=wesnoth-icon.png
            message= _ "Malin and Darken Volk enter the mage lady’s manor, closing the heavy gates behind them."
        [/message]

        [message]
            speaker=Darken Volk
            message= _ "That gate will not hold back the soldiers for long. We must hurry."
        [/message]
    [/event]

    [event]
        name=sighted
        [filter]
            side=2
        [/filter]
        [filter_second]
            side=1
        [/filter_second]

        [message]
            speaker=unit
            message= _ "Undead have entered the manor! Defend yourselves!"
        [/message]
    [/event]

    # City guard spawns
#define SPAWN_GUARD
    [random_placement]
        num_items=1
        variable=loc
        min_distance=1
        [filter_location]
            x=22,23,21-26
            y=18,18,19-20
            [not]
                [filter][/filter]
            [/not]
        [/filter_location]
        [command]
            [set_variable]
                name=ind
                rand=1..$guardtypetable.length
            [/set_variable]
            {VARIABLE_OP ind sub 1}
            {GENERIC_UNIT 3 "$guardtypetable[$ind].type" $loc.x $loc.y}
            {CLEAR_VARIABLE ind}
        [/command]
    [/random_placement]
#enddef
#define SPAWN_MAGE
    [random_placement]
        num_items=1
        variable=loc
        min_distance=1
        [filter_location]
            x=22,23,21-26
            y=18,18,19-20
            [not]
                [filter][/filter]
            [/not]
        [/filter_location]
        [command]
            [set_variable]
                name=ind
                rand=1..$magetypetable.length
            [/set_variable]
            {VARIABLE_OP ind sub 1}
            {GENERIC_UNIT 3 "$magetypetable[$ind].type" $loc.x $loc.y}
            {CLEAR_VARIABLE ind}
        [/command]
    [/random_placement]
#enddef

    [event]
        name=new turn
        first_time_only=no

        [if]
            [variable]
                name=turn_number
                greater_than_equal_to=$spawn2
            [/variable]
            [then]
                {RANDOM 0..2}
                [switch]
                    variable=random
                    [case]
                        value=0
                        {SPAWN_GUARD}
#ifdef HARD
                        {SPAWN_GUARD}
#endif
                    [/case]
                    [case]
                        value=1
                        {SPAWN_MAGE}
                    [/case]
                    [case]
                        value=2
                        {SPAWN_GUARD}
                        {SPAWN_MAGE}
                    [/case]
                [/switch]
            [/then]
            [elseif]
                [variable]
                    name=turn_number
                    greater_than_equal_to=$spawn1
                [/variable]
                [then]
                    [set_variable]
                        name=turn_mod
                        value=$("($turn_number - $spawn1) % 2")
                    [/set_variable]
                    [if]
                        [variable]
                            name=turn_mod
                            numerical_equals=0
                        [/variable]
                        [then]
                            {RANDOM 0..3}
                            [switch]
                                variable=random
                                [case]
                                    value=0
                                    {SPAWN_GUARD}
#ifdef HARD
                                    {SPAWN_GUARD}
#endif
                                [/case]
                                [case]
                                    value=1
                                    {SPAWN_GUARD}
                                [/case]
                                [case]
                                    value=2
                                    {SPAWN_MAGE}
                                [/case]
                                [case]
                                    value=3
                                    {SPAWN_GUARD}
                                    {SPAWN_MAGE}
                                [/case]
                            [/switch]
                        [/then]
                    [/if]
                    {CLEAR_VARIABLE turn_mod}
                [/then]
            [/elseif]
            [elseif]
                [variable]
                    name=turn_number
                    greater_than_equal_to=$S7_turns
                [/variable]
                [then]
                    [set_variable]
                        name=turn_mod
                        value=$("($turn_number - $S7_turns) % 3")
                    [/set_variable]
                    [if]
                        [variable]
                            name=turn_mod
                            numerical_equals=0
                        [/variable]
                        [then]
                            {RANDOM 0..3}
                            [switch]
                                variable=random
                                [case]
                                    value=0
                                    {SPAWN_GUARD}
                                [/case]
                                [case]
                                    value=1
                                    {SPAWN_GUARD}
                                [/case]
                                [case]
                                    value=2
                                    {SPAWN_MAGE}
                                [/case]
                                [case]
                                    value=3
#ifndef EASY
                                    {SPAWN_GUARD}
#endif
                                    {SPAWN_MAGE}
                                [/case]
                            [/switch]
                        [/then]
                    [/if]
                    {CLEAR_VARIABLE turn_mod}
                [/then]
            [/elseif]
        [/if]

        [fire_event]
            name=city guard dialogue
        [/fire_event]
        [fire_event]
            name=city guard dialogue2
        [/fire_event]
        [fire_event]
            name=city guard dialogue3
        [/fire_event]
    [/event]

    [event]
        name=city guard dialogue
        [filter_condition]
            [variable]
                name=turn_number
                equals=$S7_turns
            [/variable]
        [/filter_condition]

        [message]
            side=3
            [filter_location]
                x=22,23,21-26
                y=18,18,19-20
            [/filter_location]
            message= _ "Hunt down the undead! Destroy them all!"
        [/message]
        [message]
            speaker=Darken Volk
            message= _ "The city guard has entered the manor. We will have to hold them off while we search."
        [/message]
    [/event]
    [event]
        name=city guard dialogue2
        [filter_condition]
            [variable]
                name=turn_number
                equals=$spawn1
            [/variable]
        [/filter_condition]

        [message]
            speaker=Darken Volk
            message= _ "More guards are coming. We must hasten our search!"
        [/message]
    [/event]
    [event]
        name=city guard dialogue3
        [filter_condition]
            [variable]
                name=turn_number
                equals=$spawn2
            [/variable]
        [/filter_condition]

        [message]
            speaker=Malin Keshar
            message= _ "It does not look like we have much time left..."
        [/message]
    [/event]

    # Flavor text

    [event]
        name=moveto
        [filter]
            x,y=15,13
            side=1
        [/filter]
        # from The Alchemist
        [message]
            speaker=narrator
            message= _ "There is only one thing that makes a dream impossible to achieve."
        [/message]
        [delay]
            time=350
        [/delay]
        [sound]
            name=wail-long.wav
        [/sound]
        [delay]
            time=350
        [/delay]
        [message]
            speaker=narrator
            message= _ "... the fear of failure."
        [/message]
    [/event]
    [event]
        name=moveto
        [filter]
            x,y=6,16
            side=1
        [/filter]
        [message]
            speaker=narrator
            message= _ "<i>There is nothing good or bad, but thinking makes it so.</i>"
        [/message]
    [/event]
    [event]
        name=moveto
        [filter]
            x,y=2,10
            side=1
        [/filter]
        [message]
            speaker=narrator
            message= _ "<i>Mirrors of shallow blue, cold and opaque, they stare,
Piercing, unblinking, a pitiless reflection,
Shattering our brittle, fancied reality.</i>"
        [/message]
    [/event]
    [event]
        name=moveto
        [filter]
            x,y=8,2
            side=1
        [/filter]
        [message]
            speaker=narrator
            message= _ "<i>Emerald, verdant green gives way,
Touched by shadow, dark dye of black,
An abhorrent stain of pestilence.</i>"
        [/message]
    [/event]
    [event]
        name=moveto
        [filter]
            x,y=25,2
            side=1
        [/filter]
        [message]
            speaker=narrator
            message= _ "<i>Crimson pool, essence of life,
Flesh and blood flow away to rot,
Leaving only bones to remain.</i>"
        [/message]
    [/event]

    # someone's marriage had some issues at the last moment...
    [event]
        name=moveto
        [filter]
            x,y=13,3
            side=1
        [/filter]
        [message]
            speaker=Darken Volk
            message= _ "This appears to be a wedding ring."
        [/message]
    [/event]

    [event]
        name=moveto
        [filter]
            x=17,18
            y=1,1
            side=1
        [/filter]
        [message]
            speaker=Darken Volk
            message= _ "This vial of holy water is inscribed with a cross and two rings."
        [/message]
    [/event]

    [event]
        name=moveto
        [filter]
            x,y=15,2
            side=1
        [/filter]
        [message]
            speaker=Darken Volk
            message= _ "There is a torn up dress on this altar."
        [/message]
    [/event]

    # Malin gets some extra cold resistance
    [event]
        name=moveto
        [filter]
            x,y=3,15
            id=Malin Keshar
        [/filter]
        [message]
            speaker=Malin Keshar
            message= _ "This amulet feels strangely cold."
        [/message]
        [object]
            silent=yes
            duration=forever
            [filter]
                id=Malin Keshar
            [/filter]
            [effect]
                apply_to=resistance
                [resistance]
                    cold=-10
                [/resistance]
            [/effect]
        [/object]

        {REMOVE_IMAGE 3 15}
    [/event]

    [event]
        name=moveto
        [filter]
            x=8,9
            y=7,9
            side=1
        [/filter]
        [message]
            speaker=Malin Keshar
            message= _ "The mages have hidden their gold somewhere else."
        [/message]
    [/event]

    # Lady Karae

    [event]
        name=sighted
        [filter]
            id=Lady Karae
        [/filter]
        [filter_second]
            side=1
        [/filter_second]

        [message]
            speaker=Lady Karae
            message= _ "Darken Volk! I should have known it was you! What are you doing here?"
        [/message]
        [message]
            speaker=Darken Volk
            message= _ "Taking back what is rightfully mine."
        [/message]
        [message]
            speaker=Lady Karae
            message= _ "As impudent as ever. What lies in that book is not meant for human eyes — yours or mine."
        [/message]
        [message]
            speaker=Darken Volk
            message= _ "I am not here to argue."
        [/message]
        [message]
            speaker=Lady Karae
            message= _ "Of course you are not. I assume your arrogance has only grown over the years."
        [/message]
        [message]
            speaker=Darken Volk
            message= _ "If you wish to continue lecturing me, by all means, do so. It will only make you easier to kill."
        [/message]
        [message]
            speaker=Lady Karae
            message= _ "I regret that it has come to this."
        [/message]
    [/event]

    [event]
        name=moveto
        [filter]
            x,y=26,5
            side=1
        [/filter]
        [message]
            speaker=Darken Volk
            message= _ "Someone was... messing... with some potions here."
        [/message]
    [/event]

    [event]
        name=last breath
        [filter]
            id=Lady Karae
        [/filter]

        [message]
            speaker=Lady Karae
            message= _ "Darken Volk... you... wouldn’t dare..."
        [/message]
        [message]
            speaker=Darken Volk
            message= _ "Enough with your decrepit prattling."
        [/message]

        [kill]
            id=Lady Karae
            animate=yes
            fire_event=yes
        [/kill]

        {QUAKE rumble.ogg}

        [terrain]
            x=6,7
            y=4,4
            terrain=Iwr
        [/terrain]
        [redraw][/redraw]

        [message]
            speaker=Darken Volk
            # po: a vault is a big underground room, sort of like a gigantic basement
            message= _ "I feel the breaking of an illusion spell. The book is in the vault in the northwest corner of the manor. We must find the passageway leading there!"
        [/message]

        [objectives]
            side=1
            [objective]
                condition=win
                description= _ "Move Malin Keshar or Darken Volk through the passageway in the northwest leading to the vault"
            [/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-3,1-3
            id=Malin Keshar,Darken Volk
        [/filter]

        [message]
            speaker=unit
            message=_ "Here is the passage to the vault!"
        [/message]

        [endlevel]
            result=victory
            bonus=no
            linger_mode=no
            {NEW_GOLD_CARRYOVER 100}
            carryover_report=no
        [/endlevel]
    [/event]

    # The player may have created new units using the plague ability. They
    # should carry over to the next scenario.
    [event]
        name=victory

        {SET_GOT_INSIDE_MANOR}
        {CLEAR_VARIABLE guardtypetable}
        {CLEAR_VARIABLE magetypetable}
        {CLEAR_VARIABLE random}
        {CLEAR_VARIABLE S7_turns}
        {CLEAR_VARIABLE spawn1}
        {CLEAR_VARIABLE spawn2}
    [/event]

    [event]
        name=time over

        [if]
            [have_unit]
                id=Lady Karae
            [/have_unit]
            [then]
                [unit]
                    type=Royal Guard
                    side=3
                    x,y=22,18
                    id=RGuard
                    canrecruit=yes
                    animate=yes
                    facing=nw
                [/unit]
                {GENERIC_UNIT 3 (Pikeman) 23 18} {FACING nw}
                {GENERIC_UNIT 3 (Longbowman) 22 19} {FACING nw}
                {GENERIC_UNIT 3 (Swordsman) 21 19} {FACING nw}
                {GENERIC_UNIT 3 (Mage of Light) 23 19} {FACING nw}

                [message]
                    speaker=RGuard
                    message= _ "Surround those necromancers! They cannot escape now!"
                [/message]
                [message]
                    speaker=Darken Volk
                    message= _ "Fool boy! You took too long to defeat Karae! Now we are at the mercy of the city guards!"
                [/message]
            [/then]
            [else]
                [remove_shroud]
                    side=1
                    x=0-3,3,4,5
                    y=0-3,3-4,1-4,4-5
                [/remove_shroud]
                [unit]
                    type=Royal Guard
                    side=3
                    x,y=2,3
                    id=RGuard
                    canrecruit=yes
                    animate=yes
                    facing=se
                [/unit]
                {GENERIC_UNIT 3 (Pikeman) 2 2} {FACING se}
                {GENERIC_UNIT 3 (Longbowman) 3 2} {FACING se}
                {GENERIC_UNIT 3 (Swordsman) 1 3} {FACING se}
                {GENERIC_UNIT 3 (Mage of Light) 2 1} {FACING se}

                [message]
                    speaker=RGuard
                    message= _ "Surround those necromancers! They cannot escape now!"
                [/message]
                [message]
                    speaker=Darken Volk
                    message= _ "Fool boy! You took too long to find the path to the vault! Now we are at the mercy of the city guards!"
                [/message]
            [/else]
        [/if]

        [endlevel]
            result=defeat
        [/endlevel]
    [/event]

    {HERODEATH_MALIN}
    {HERODEATH_VOLK_B}
[/scenario]

#undef SPAWN_GUARD
#undef SPAWN_MAGE
