#textdomain wesnoth-did

[scenario]
    id=07c_A_Small_Favor3
    name= _ "A Small Favor — III"
    map_file=07c_A_Small_Favor3.map
    {TURNS 21 18 15}
    next_scenario=08_Alone_at_Last
    victory_when_enemies_defeated=no

    {UNDERGROUND}

    {SCENARIO_MUSIC       the_city_falls.ogg}
    {EXTRA_SCENARIO_MUSIC the_king_is_dead.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
        share_vision=shroud
        facing=nw
    [/side]
    # wmllint: validate-on

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

        # Book guards
        {NAMED_NOTRAIT_UNIT 2 (Silver Mage) 11 9 ("") ( _ "Guardian")} {GUARDIAN}
        {NAMED_NOTRAIT_UNIT 2 (Silver Mage) 13 7 ("") ( _ "Guardian")} {GUARDIAN}

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

    [side]
        side=3
        controller=ai
        team_name=intruders
        user_team_name= _ "Intruders"
        no_leader=yes
        color=black
        hidden=yes
        shroud=yes
        share_vision=none

        [ai]
            village_value=0
            aggression=0.8
            [goal]
                name=protect_unit
                [criteria]
                    id=Darken Volk
                [/criteria]
                value=20
            [/goal]
            [goal]
                name=protect_unit
                [criteria]
                    id=Darken Volk
                [/criteria]
                protect_radius=6
                value=20
            [/goal]
        [/ai]
    [/side]
    # City guard
    [side]
        side=4
        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,3}

    # scenery

    {PLACE_IMAGE items/dragonstatue.png 23 16}
    {PLACE_IMAGE items/dragonstatue.png 24 15}
    {PLACE_IMAGE items/dragonstatue.png 31 12}
    {PLACE_IMAGE scenery/monolith4.png 34 11}
    {PLACE_IMAGE scenery/monolith3.png 29 14}
    {PLACE_IMAGE scenery/monolith1.png 29 16}
    {PLACE_IMAGE scenery/monolith3.png 26 15}
    {PLACE_IMAGE scenery/monolith4.png 26 18}
    {PLACE_IMAGE scenery/castle-ruins2.png 32 15}
    {PLACE_IMAGE scenery/castle-ruins3.png 32 14}
    {PLACE_IMAGE scenery/castle-ruins3.png 25 15}
    {PLACE_IMAGE scenery/castle-ruins2.png 24 18}
    {PLACE_IMAGE scenery/castle-ruins.png 33 4}
    {PLACE_IMAGE scenery/castle-ruins3.png 33 3}
    {PLACE_IMAGE scenery/castle-ruins2.png 31 5}
    {PLACE_IMAGE scenery/castle-ruins2.png 9 11}
    {PLACE_IMAGE scenery/castle-ruins.png 8 7}

    {PLACE_IMAGE items/ornate2.png 23 11}
    {PLACE_IMAGE items/ornate2.png 25 12}
    {PLACE_IMAGE items/ornate1.png 20 13}
    {PLACE_IMAGE items/coffin-open.png 22 12}

    {PLACE_IMAGE items/cloak-green.png 11 17}
    {PLACE_IMAGE items/armor.png 14 16}
    {PLACE_IMAGE items/armor.png 16 16}
    {PLACE_IMAGE items/bow.png 30 9}
    {PLACE_IMAGE items/bow-crystal.png 29 9}
    {PLACE_IMAGE items/spear-fancy.png 27 10}
    {PLACE_IMAGE items/axe.png 27 9}

    {PLACE_IMAGE items/potion-blue.png 18 3}
    {PLACE_IMAGE items/potion-blue.png 17 6}
    {PLACE_IMAGE items/potion-grey.png 17 3}
    {PLACE_IMAGE items/potion-grey.png 19 4}
    {PLACE_IMAGE items/potion-grey.png 21 5}
    {PLACE_IMAGE items/potion-green.png 19 5}
    {PLACE_IMAGE items/potion-green.png 22 3}
    {PLACE_IMAGE items/potion-red.png 19 3}
    {PLACE_IMAGE items/potion-red.png 14 3}
    {PLACE_IMAGE items/potion-yellow.png 17 4}
    {PLACE_IMAGE items/altar-evil.png 18 4}
    {PLACE_IMAGE items/altar.png 14 8}
    {PLACE_IMAGE items/holy-water.png 14 9}
    {PLACE_IMAGE items/holy-water.png 14 7}
    {PLACE_IMAGE items/holy-water.png 15 8}
    {PLACE_IMAGE items/holy-water.png 10 8}
    {PLACE_IMAGE items/stone-tablet.png 7 11}
    {PLACE_IMAGE items/stone-tablet.png 15 10}

    {PLACE_IMAGE items/dragonstatue.png 19 7}
    {PLACE_IMAGE scenery/monolith3.png 16 11}
    {PLACE_IMAGE scenery/monolith1.png 21 7}
    {PLACE_IMAGE scenery/monolith3.png 19 9}
    {PLACE_IMAGE scenery/monolith1.png 12 4}
    {PLACE_IMAGE scenery/monolith1.png 9 12}
    {PLACE_IMAGE scenery/monolith3.png 10 7}
    {PLACE_IMAGE scenery/monolith4.png 10 11}
    {PLACE_IMAGE scenery/monolith4.png 4 10}

    {PLACE_IMAGE items/barrel.png 29 17}
    {PLACE_IMAGE items/barrel.png 28 17}
    {PLACE_IMAGE items/barrel.png 28 13}
    {PLACE_IMAGE items/barrel.png 22 19}
    {PLACE_IMAGE items/barrel.png 21 19}
    {PLACE_IMAGE items/barrel.png 29 8}
    {PLACE_IMAGE items/barrel.png 30 7}
    {PLACE_IMAGE items/barrel.png 14 17}
    {PLACE_IMAGE items/barrel.png 34 4}
    {PLACE_IMAGE items/barrel.png 23 3}
    {PLACE_IMAGE items/box.png 29 13}
    {PLACE_IMAGE items/box.png 22 18}
    {PLACE_IMAGE items/box.png 13 18}
    {PLACE_IMAGE items/box.png 13 17}
    {PLACE_IMAGE items/box.png 28 4}
    {PLACE_IMAGE items/box.png 27 5}
    {PLACE_IMAGE items/box.png 26 5}

    [event]
        name=prestart

        # yes this value is not as adjusted as the previous scenario, but that's because we're underground
        {COLOR_ADJUST -10 -10 -10}

        # place the book's image
        [item]
            location_id=book_start
            image="items/book5.png"
        [/item]

        [objectives]
            side=1
            [objective]
                condition=win
                description= _ "Retrieve the book and escape"
            [/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]

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

        [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 5 6 7}
            variable=loc
            min_distance=3
            [filter_location]
                terrain=Urb,Urb^E*,Ur,Ur^E*
                [not]
                    x=27-35
                    y=1-7
                [/not]
                [not]
                    x=20-35,24
                    y=16-20,15
                [/not]
                [not]
                    x=25-29
                    y=13-20
                [/not]
                [not]
                    x=30-35
                    y=11-20
                [/not]
                [not]
                    [filter][/filter]
                [/not]
            [/filter_location]
            [command]
                {NAMED_NOTRAIT_UNIT 2 "$guardtypetable[$($loc.n % $guardtypetable.length)].type" $loc.x $loc.y () (_ "Guard")} {GUARDIAN}
            [/command]
        [/random_placement]
        [random_placement]
            num_items={ON_DIFFICULTY 6 6 7}
            variable=loc
            min_distance=3
            [filter_location]
                terrain=Urb,Urb^E*,Ur,Ur^E*,Irs,Irs^*
                [not]
                    x=27-35
                    y=1-7
                [/not]
                [not]
                    x=20-35,24
                    y=16-20,15
                [/not]
                [not]
                    x=25-29
                    y=13-20
                [/not]
                [not]
                    x=30-35
                    y=11-20
                [/not]
                [not]
                    [filter][/filter]
                [/not]
            [/filter_location]
            [command]
                {NAMED_NOTRAIT_UNIT 2 "$magetypetable[$($loc.n % $magetypetable.length)].type" $loc.x $loc.y () (_ "Mage")} {GUARDIAN}
            [/command]
        [/random_placement]

        # Who has the book?
        {VARIABLE hasBook none}

        # Switch Volk's side
        [recall]
            id=Darken Volk
            x,y=26,16
        [/recall]

        [modify_unit]
            [filter]
                id=Darken Volk
            [/filter]
            side=3
        [/modify_unit]

        [lock_view][/lock_view]
    [/event]

    [event]
        name=start

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

        # Create DV's units
        {GENERIC_UNIT 3 (Draug)    26 16}
        [+unit]
            animate=yes
        [/unit]

        {GENERIC_UNIT 3 (Deathblade) 26 16}
        [+unit]
            animate=yes
        [/unit]

        {GENERIC_UNIT 3 (Necrophage) 26 16}
        [+unit]
            animate=yes
        [/unit]

        {GENERIC_UNIT 3 (Banebow)    26 16}
        [+unit]
            animate=yes
        [/unit]

        [message]
            speaker=Darken Volk
            message= _ "The book is very close, somewhere to the northwest."
        [/message]

        [unlock_view][/unlock_view]
    [/event]

    [event]
        name=side 3 turn refresh
        first_time_only=no

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

        [if]
            [variable]
                name=hasBook
                equals=none
            [/variable]
            [then]
                [if]
                    [variable]
                        name=turn_number
                        greater_than=4
                    [/variable]
                    [then]
                        [store_locations]
                            location_id=book_start
                            variable=dest
                        [/store_locations]
                    [/then]
                    [else]
                        {VARIABLE dest.x 16}
                        {VARIABLE dest.y 14}
                    [/else]
                [/if]
            [/then]
            [else]
                {VARIABLE dest.x 31}
                {VARIABLE dest.y 1}
            [/else]
        [/if]

        [find_path]
            [traveler]
                id=Darken Volk
            [/traveler]
            [destination]
                x=$dest.x
                y=$dest.y
            [/destination]
            variable=darken_goto
            allow_multiple_turns=yes
        [/find_path]

        [if]
            [variable]
                name=darken_goto.step.length
                greater_than=0
            [/variable]
            [then]
                {VARIABLE index "$(min($stored_Darken.max_moves,$darken_goto.step.length-1))"}
                {VARIABLE stored_Darken.goto_x $darken_goto.step[$index].x}
                {VARIABLE stored_Darken.goto_y $darken_goto.step[$index].y}
            [/then]
            [else]
                {VARIABLE stored_Darken.goto_x $dest.x}
                {VARIABLE stored_Darken.goto_y $dest.y}
            [/else]
        [/if]

        [unstore_unit]
            variable=stored_Darken
        [/unstore_unit]

        {CLEAR_VARIABLE stored_Darken,dest,darken_goto,index}
    [/event]

    # If there are no enemies, then DV's troops won't move; this event unblocks the book if necessary.
    [event]
        name=side 3 turn refresh
        first_time_only=no
        [filter_condition]
            [variable]
                name=hasBook
                equals=none
            [/variable]
            [have_unit]
                # This won't be DV himself, otherwise hasBook is not none.
                side=3
                [filter_location]
                    location_id=book_start
                [/filter_location]
            [/have_unit]
            [not]
                [have_unit]
                    # Ignore side=4, the city guard is (hopefully for the player) not near the book.
                    side=2
                    [filter_vision]
                        # There are 3 entrance rooms, one of them might still have all of its doors shut.
                        side=3
                    [/filter_vision]
                [/have_unit]
            [/not]
        [/filter_condition]
        [modify_unit]
            [filter]
                [filter_location]
                    location_id=book_start
                [/filter_location]
            [/filter]
            # Somewhere inside the tunnel, but out-of-the-way for the hexes that Malin must move to.
            goto_x=29
            goto_y=2
        [/modify_unit]
    [/event]

    # pursuer spawns

#define SPAWN_GUARD
    [random_placement]
        num_items=1
        variable=loc
        min_distance=1
        [filter_location]
            x=33-34,35
            y=17-18,18-19
            [not]
                [filter][/filter]
            [/not]
        [/filter_location]
        [command]
            [set_variable]
                name=ind
                rand=1..$guardtypetable.length
            [/set_variable]
            {VARIABLE_OP ind sub 1}
            {GENERIC_UNIT 4 "$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=33-34,35
            y=17-18,18-19
            [not]
                [filter][/filter]
            [/not]
        [/filter_location]
        [command]
            [set_variable]
                name=ind
                rand=1..$magetypetable.length
            [/set_variable]
            {VARIABLE_OP ind sub 1}
            {GENERIC_UNIT 4 "$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={ON_DIFFICULTY 14 12 10}
            [/variable]
            [then]
                {RANDOM 0..2}
                [switch]
                    variable=random
                    [case]
                        value=0
                        {SPAWN_GUARD}
                        {SPAWN_GUARD}
                    [/case]
                    [case]
                        value=1
                        {SPAWN_MAGE}
#ifdef HARD
                        {SPAWN_MAGE}
#else
                        {SPAWN_GUARD}
#endif
                    [/case]
                    [case]
                        value=2
                        {SPAWN_GUARD}
                        {SPAWN_MAGE}
                    [/case]
                [/switch]
            [/then]
            [elseif]
                [variable]
                    name=turn_number
                    greater_than_equal_to={ON_DIFFICULTY 8 7 6}
                [/variable]
                [then]
                    [set_variable]
                        name=turn_mod
                        value=$("($turn_number - {ON_DIFFICULTY 8 7 6}) % 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}
                                    {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=2
                [/variable]
                [then]
                    [set_variable]
                        name=turn_mod
                        value=$("($turn_number - 2) % 3")
                    [/set_variable]
                    [if]
                        [variable]
                            name=turn_mod
                            numerical_equals=0
                        [/variable]
                        [then]
                            {RANDOM 0..3}
                            [switch]
                                variable=random
                                [case]
                                    value=0
                                    {SPAWN_GUARD}
                                    {SPAWN_GUARD}
                                [/case]
                                [case]
                                    value=1
                                    {SPAWN_GUARD}
#ifdef HARD
                                    {SPAWN_GUARD}
#endif
                                [/case]
                                [case]
                                    value=2
                                    {SPAWN_MAGE}
                                [/case]
                                [case]
                                    value=3
                                    {SPAWN_GUARD}
                                    {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]
    [/event]

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

        [message]
            speaker=Darken Volk
            message= _ "The guards continue to pursue us."
        [/message]
    [/event]
    [event]
        name=city guard dialogue2
        [filter_condition]
            [variable]
                name=turn_number
                equals={ON_DIFFICULTY 14 12 10}
            [/variable]
        [/filter_condition]

        [message]
            speaker=Malin Keshar
            message= _ "The guards are catching up to us."
        [/message]
    [/event]

    # Flavor text

    [event]
        name=moveto
        [filter]
            x,y=15,10
            side=1
        [/filter]
        [message]
            speaker=narrator
            message= _ "<i>Beware that, when fighting monsters, you yourself do not become a monster...</i>"
        [/message]
    [/event]
    [event]
        name=moveto
        [filter]
            x,y=7,11
            side=1
        [/filter]
        [message]
            speaker=narrator
            message= _ "<i>Death is the twilight that follows the light of day.
Life fades with the setting sun into darkness.
Existence churns and the soul is reborn into endless night.</i>"
        [/message]
    [/event]
    [event]
        name=moveto
        [filter]
            x,y=18,4
            side=1
        [/filter]
        [message]
            speaker=Malin Keshar
            message= _ "There are quite a few failed potions around here."
        [/message]
    [/event]
    [event]
        name=moveto
        [filter]
            x,y=22,12
            side=1
        [/filter]
        [message]
            speaker=Malin Keshar
            message= _ "This coffin is empty."
        [/message]
    [/event]

    [event]
        name=moveto
        [filter]
            x=20,23,25
            y=13,11,12
            race=undead
            side=1
        [/filter]

        # throwback to S7 when you knock over the holy water
        [message]
            speaker=Malin Keshar
            message= _ "Be careful around those urns."
        [/message]
    [/event]

    [event]
        name=moveto
        [filter]
            x,y=11,17
            id=Malin Keshar
        [/filter]

        [message]
            speaker=Malin Keshar
            message= _ "Fancy, but I’ve no use for a new cloak right now."
        [/message]
    [/event]

    # the armory
    # only allow level 3s and your fancy chocobone (if you got it) to take items
    # also make it silent besides some small sound effects
    [event]
        name=moveto
        [filter]
            x,y=27,9
            side=1
            type=Draug
        [/filter]

        [sound]
            name=skeleton-big-hit-1.ogg
        [/sound]

        [object]
            silent=yes
            duration=forever
            [filter]
                x,y=27,9
            [/filter]
            [effect]
                apply_to=attack
                range=melee
                increase_damage=1
            [/effect]
        [/object]

        {REMOVE_IMAGE 27 9}
    [/event]
    [event]
        name=moveto
        [filter]
            x,y=29,9
            side=1
            type=Banebow
        [/filter]

        [sound]
            name=skeleton-big-hit-2.ogg
        [/sound]

        [object]
            silent=yes
            duration=forever
            [filter]
                x,y=29,9
            [/filter]
            [effect]
                apply_to=attack
                range=ranged
                increase_damage=1
            [/effect]
        [/object]

        {REMOVE_IMAGE 29 9}
    [/event]
    [event]
        name=moveto
        [filter]
            x,y=30,9
            side=1
            type=Banebow
        [/filter]

        [sound]
            name=skeleton-big-hit-1.ogg
        [/sound]

        [object]
            silent=yes
            duration=forever
            [filter]
                x,y=30,9
            [/filter]
            [effect]
                apply_to=attack
                range=ranged
                increase_damage=1
            [/effect]
        [/object]

        {REMOVE_IMAGE 30 9}
    [/event]
    [event]
        name=moveto
        [filter]
            x,y=27,10
            side=1
            type=Chocobone
        [/filter]

        [sound]
            name=skeleton-big-hit-3.ogg
        [/sound]

        [object]
            silent=yes
            duration=forever
            [filter]
                x,y=27,10
            [/filter]
            [effect]
                apply_to=attack
                range=melee
                increase_damage=2
            [/effect]
        [/object]

        {REMOVE_IMAGE 27 10}
    [/event]

    [event]
        # a side 1 unit finds the book
        name=moveto
        [filter]
            side=1
            canrecruit=no
            [filter_location]
                location_id=book_start
            [/filter_location]
        [/filter]
        [filter_condition]
            [variable]
                name=hasBook
                equals=none
            [/variable]
        [/filter_condition]

        [if]
            [have_unit]
                [filter_location]
                    location_id=book_start
                [/filter_location]
                race=undead
            [/have_unit]
            [then]
                [message]
                    id=Darken Volk
                    # po: The speaker addresses an undead minion.
                    message=_ "The book! Guard it with your unlife while I come to fetch it."
                [/message]
            [/then]
            [else]
                [message]
                    id=Darken Volk
                    # po: The speaker addresses a human or a bat.
                    message=_ "The book! Guard it with your life while I come to fetch it."
                [/message]
            [/else]
        [/if]
    [/event]

    [event]
        # Malin gets the book
        name=moveto
        [filter]
            id=Malin Keshar
            [filter_location]
                location_id=book_start
            [/filter_location]
        [/filter]
        [filter_condition]
            [variable]
                name=hasBook
                equals=none
            [/variable]
        [/filter_condition]

        [remove_item]
            location_id=book_start
        [/remove_item]

        {VARIABLE hasBook Malin}

        [message]
            speaker=Darken Volk
            message= _ "Hand that over."
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "Can’t it wait until we’re away from all these guards?"
        [/message]

        [message]
            speaker=Darken Volk
            message= _ "... Fine. There is 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]

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

            {TURNS_RUN_OUT}

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

        [event]
            name=moveto
            [filter]
                id=Malin Keshar
                x,y=29-32,1-3
            [/filter]

            [message]
                speaker=Malin Keshar
                message= _ "Let’s get out of here."
            [/message]

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

    [event]
        # DV gets the book
        name=moveto
        [filter]
            id=Darken Volk
            [filter_location]
                location_id=book_start
            [/filter_location]
        [/filter]
        [filter_condition]
            [variable]
                name=hasBook
                equals=none
            [/variable]
        [/filter_condition]

        [remove_item]
            location_id=book_start
        [/remove_item]

        {VARIABLE hasBook Darken}

        # A second, non-silent, update triggers when DV moves again (next turn, he's still set to go to the book_start hex this turn).
        [objectives]
            side=1
            silent=yes
            [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]

            {TURNS_RUN_OUT}
        [/objectives]

        [event]
            name=moveto
            [filter]
                id=Darken Volk
                side=3
            [/filter]

            [message]
                speaker=Malin Keshar
                message= _ "We’ve got the book, but how do we get out of here?"
            [/message]

            [scroll_to_unit]
                id=Darken Volk
            [/scroll_to_unit]

            [delay]
                time=500
            [/delay]

            [message]
                speaker=Malin Keshar
                message= _ "Wait, you aren’t leaving me behind, are you?"
            [/message]

            [objectives]
                side=1
                [objective]
                    condition=win
                    description= _ "Escape the manor (Malin reaches the tunnel in the northeast cellar before Darken Volk)"
                [/objective]
                [objective]
                    condition=lose
                    description= _ "Darken Volk reaches the tunnel before Malin does"
                [/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]
                id=Darken Volk
                x,y=30-31,1
            [/filter]

            [message]
                speaker=Darken Volk
                message= _ "You have served me well, boy."
            [/message]

            [message]
                speaker=narrator
                image=wesnoth-icon.png
                message= _ "As he heads into the tunnel, Darken Volk collapses the entrance, leaving Malin to be captured and executed by the city guards."
            [/message]

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

        [event]
            name=moveto
            [filter]
                id=Malin Keshar
                x,y=29-32,1-3
            [/filter]

            [message]
                speaker=Malin Keshar
                message= _ "Let’s get out of here."
            [/message]

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

    [event]
        name=time over

        [remove_shroud]
            side=1
            x=29-33
            y=0-4
        [/remove_shroud]

        [unit]
            type=Royal Guard
            side=4
            x,y=32,2
            id=RGuard
            canrecruit=yes
            animate=yes
            facing=sw
        [/unit]
        {GENERIC_UNIT 4 (Pikeman) 30 2} {FACING sw}
        {GENERIC_UNIT 4 (Longbowman) 30 1} {FACING sw}
        {GENERIC_UNIT 4 (Swordsman) 31 2} {FACING sw}
        {GENERIC_UNIT 4 (Mage of Light) 31 1} {FACING sw}

        [message]
            speaker=RGuard
            message= _ "Surround those necromancers! They cannot escape now!"
        [/message]

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

    [event]
        name=victory

        {CLEAR_VARIABLE guardtypetable}
        {CLEAR_VARIABLE magetypetable}
        {CLEAR_VARIABLE random}
        {MODIFY_UNIT (id=Darken Volk) side 1}
    [/event]

    {HERODEATH_MALIN}
    {HERODEATH_VOLK_B}
[/scenario]

#undef SPAWN_GUARD
#undef SPAWN_MAGE


#textdomain wesnoth-did

[scenario]
    id=08_Alone_at_Last
    name= _ "Alone at Last"
    map_file=08_Alone_at_Last.map
    {TURNS 35 32 29}
    next_scenario=09_Descent_into_Darkness
    victory_when_enemies_defeated=no

    {DEFAULT_SCHEDULE_DUSK}

    {SCENARIO_MUSIC       knolls.ogg}
    {EXTRA_SCENARIO_MUSIC siege_of_laurelmor.ogg}
    {EXTRA_SCENARIO_MUSIC nunc_dimittis.ogg}

    [story]
        [part]
            story= _ "As he enters the tunnel underneath Lady Karae’s manor, Darken Volk gestures, causing the entrance to collapse. The two necromancers turn and begin walking down the pitch-black tunnel, temporarily safe from pursuit."
            {STORYTXT_BACKGROUND travel.jpg}
        [/part]
        [part]
            story= _ "Silence permeates the darkness, broken only by the irregular dripping of water from the musty cave walls. Malin looks toward Darken Volk occasionally, expecting something from the elder necromancer, but is met with stone-cold dispassion. There is no acknowledgment... no word of gratitude — only a sense of unease that has as much to do with the book as it does with Darken Volk."
            {STORYTXT_BACKGROUND travel.jpg}
        [/part]
        [part]
            story= _ "They continue walking through the damp, cold air all through the morning and day, finally emerging as the last rays of sunlight sink below the horizon."
            {STORYTXT_BACKGROUND travel.jpg}
        [/part]
    [/story]

    {DID_TRACK {JOURNEY_08_NEW}}

    # wmllint: validate-off
    [side]
        side=1
        controller=human
        recruit=Walking Corpse,Vampire Bat,Ghost,Ghoul,Skeleton Archer,Skeleton
        {GOLD 260 220 180}
        team_name=good
        user_team_name= _ "Malin Keshar"
        {FLAG_VARIANT undead}
        color=darkblue
        village_gold=2
        village_support=1

        # wmllint: recognize Malin Keshar
        {CHARACTER_STATS_MALIN_KESHAR}

        facing=ne
    [/side]
    # wmllint: validate-on

    # Darken Volk
    [side]
        side=2
        controller=ai
        recruit=Blood Bat,Ghost,Wraith,Necrophage,Skeleton Archer,Bone Shooter,Skeleton,Revenant,Deathblade
        {GOLD 170 220 270}
        {INCOME 20 25 30}
        team_name=bad
        user_team_name= _ "Darken Volk"
        no_leader=yes

        {FLAG_VARIANT undead}
        color=black

        [ai]
            village_value=0.2
            villages_per_scout=10
            [goal]
                name=target
                [criteria]
                    id=Malin Keshar
                [/criteria]
                value=2
            [/goal]

            [goal]
                name=protect_unit
                [criteria]
                    side=2
                    canrecruit=yes
                [/criteria]
                value=4
            [/goal]
        [/ai]
    [/side]

    # Paladins and co
    [side]
        side=3
        controller=ai
        recruit=Spearman,Heavy Infantryman,White Mage,Mage,Paladin
        gold=0
        team_name=wesnothians
        user_team_name= _ "Wesnothians"
        {FLAG_VARIANT loyalist}
        color=gold

        income=-2
        no_leader=yes
        hidden=yes

        [ai]
            village_value=0.1
            villages_per_scout=10
            #recruitment_pattern=fighter,healer
            [goal]
                name=target
                [criteria]
                    side=2
                [/criteria]
                value=3
            [/goal]

            [goal]
                name=target
                [criteria]
                    side=1
                [/criteria]
                value=2
            [/goal]
        [/ai]
    [/side]

    # Dela Keshar
    [side]
        side=4
        controller=ai
        gold=0
        recruit=Spearman,Bowman,Swordsman,Longbowman,Thug,Poacher,Footpad
        team_name=wesnothians
        user_team_name= _ "Wesnothians"
        {FLAG_VARIANT loyalist}
        color=green

        income=-2
        no_leader=yes
        hidden=yes

        [ai]
            passive_leader=yes
            [goal]
                name=target
                [criteria]
                    id=Malin Keshar
                [/criteria]
                value=2
            [/goal]
        [/ai]
    [/side]

    {PLACE_IMAGE scenery/wreck-2.png 16 4}
    {PLACE_IMAGE scenery/dark-monolith.png 5 18}
    {PLACE_IMAGE scenery/lighthouse.png 31 3}

    [event]
        name=prestart

        {CLEAR_CORPSE_HORDE}

        # leave some room for it to get darker later
        {COLOR_ADJUST -12 -12 -12}

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

            {TURNS_RUN_OUT}

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

        # Scenario variables
        {VARIABLE bookX -1}
        {VARIABLE bookY -1}

        {MODIFY_UNIT (id=Malin Keshar) profile (portraits/malin_old-decay.png)}

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

        [remove_trait]
            id=Darken Volk
            trait_id="loyal"
        [/remove_trait]

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

            side=2
            canrecruit=yes
            role=book_carrier
        [/modify_unit]
    [/event]

    [event]
        name=start

        {MOVE_UNIT (id=Malin Keshar) 6 14}
        {MOVE_UNIT (id=Darken Volk) 9 15}

        [message]
            speaker=Malin Keshar
            message= _ "Well, we got your precious book and made it out of Tath in one piece. What do we do now?"
        [/message]

        [message]
            speaker=Darken Volk
            message= _ "‘We’? <i>We</i> do nothing."
        [/message]

        [if]
            [variable]
                name=hasBook
                equals=Malin
            [/variable]
            [then]
                # Malin has the book

                {MOVE_UNIT (id=Darken Volk) 7 14}

                [message]
                    speaker=Darken Volk
                    message= _ "Now give the book to me."
                [/message]

                [message]
                    speaker=narrator
                    image=wesnoth-icon.png
                    message=_ "Darken Volk grabs the mysterious book from Malin’s hands and stalks away."
                [/message]

                {MODIFY_UNIT (id=Darken Volk) facing se}
            [/then]
            [else]
                # DV already has book
                [message]
                    speaker=Darken Volk
                    message= _ "Now go your own way, and I will go mine."
                [/message]
            [/else]
        [/if]

        {COLOR_ADJUST -13 -13 -13}

        {CLEAR_VARIABLE hasBook}

        [unit_overlay]
            id=Darken Volk
            image="misc/book-icon.png"
            object_id="book_icon"
        [/unit_overlay]

        {MODIFY_UNIT (id=Darken Volk) facing ne}

        {MOVE_UNIT (id=Darken Volk) 9 15}
        {MOVE_UNIT (id=Darken Volk) 11 15}

        [delay]
            time=500
        [/delay]

        [message]
            speaker=Malin Keshar
            message= _ "You can’t just leave me here!"
        [/message]

        {MOVE_UNIT (id=Malin Keshar) 7 15}
        {COLOR_ADJUST -14 -14 -14}
        {MOVE_UNIT (id=Malin Keshar) 8 14}

        {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!"
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "Wait!"
        [/message]

        {MOVE_UNIT (id=Darken Volk) 12 15}
        {COLOR_ADJUST -15 -15 -15}

        [message]
            speaker=Malin Keshar
            message= _ "You used me! You knew the people of Parthyn would cast me out — you knew I had nobody else to turn to! You knew I would do whatever you said..."
        [/message]

        {MOVE_UNIT (id=Darken Volk) 13 16}
        {COLOR_ADJUST -16 -16 -16}

        [message]
            speaker=Malin Keshar
            message= _ "After all this time... I haven’t just been a pawn to you... have I?"
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "Why don’t you say anything?! Don’t you care at all?!"
        [/message]

        {MOVE_UNIT (id=Darken Volk) 16 17}
        {COLOR_ADJUST -17 -17 -17}

        [message]
            speaker=Malin Keshar
            message= _ "... you can’t abandon me too, not the way they did... Father, Drogan, Dela..."
        [/message]

        {MOVE_UNIT (id=Darken Volk) 17 17}
        {MOVE_UNIT (id=Darken Volk) 20 18}
        # darker and darker we go as Malin's mental state degenerates
        {COLOR_ADJUST -18 -18 -18}

        [message]
            role=advisor
            message= _ "He will not help you, but perhaps the book might. You were the one who helped him retrieve it. It is rightfully yours!"
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "Yes, I suppose that is right..."
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "Darken Volk! That book is as much mine as it is yours! Give it back, or I will take it by force!"
        [/message]

        [delay]
            time=1000
        [/delay]

        {MODIFY_UNIT (id=Darken Volk) facing nw}

        [delay]
            time=1000
        [/delay]

        [message]
            speaker=Darken Volk
            message= _ "You are even more of a fool than I thought."
        [/message]

        {MOVE_UNIT (id=Darken Volk) 29 14}

        # Make camp
        [terrain]
            x,y=29,14
            radius=1
            terrain=Cer
        [/terrain]

        [terrain]
            x,y=29,14
            terrain=Ker
        [/terrain]
        [redraw][/redraw]

        {MODIFY_UNIT (id=Darken Volk) facing sw}

        {MOVE_UNIT (id=Malin Keshar) 5 13}

        [terrain]
            x,y=5,13
            radius=1
            terrain=Cer
        [/terrain]

        [terrain]
            x,y=5,13
            terrain=Ker
        [/terrain]
        [redraw][/redraw]

        {MODIFY_UNIT (id=Malin Keshar) facing ne}

        {VARIABLE dela_arrived 0}
    [/event]

    # This helps prevent Darken from getting himself killed easily by preventing
    # him from leaving his castle
    [event]
        name=side 2 turn refresh
        first_time_only=no

        [object]
            silent=yes
            duration=turn end
            take_only_once=no
            [filter]
                id=Darken Volk
            [/filter]

            [effect]
                apply_to=movement_costs
                replace=yes
                [movement_costs]
                    flat={UNREACHABLE}
                [/movement_costs]
            [/effect]
        [/object]
    [/event]

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

        # Let's check for whether he's still alive, just in case the
        # paladins somehow killed him already
        [filter_condition]
            [have_unit]
                id=Darken Volk
            [/have_unit]
        [/filter_condition]

        [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. 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= _ "You took my home away from me. You took Drogan away. You turned my sister against me! You have ruined my life!"
        [/message]

        [message]
            speaker=Darken Volk
            message= _ "The only thing I have done is given you the power to achieve your own goals. The rest, you have done yourself."
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "That’s not true! You can’t abandon me like this!"
        [/message]

        [message]
            speaker=Darken Volk
            message= _ "Fighting the orcs is your ambition, not mine. You cannot expect me to help you in an endeavor that does not concern me at all."
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "The very same to you! I helped you retrieve that book! If you will not help me with my mission, I will at least keep the fruits of my labor."
        [/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. What you choose to do with your life is not my business. Leave me alone!"
        [/message]
    [/event]

    # easter eggies

    [event]
        name=moveto
        [filter]
            id=Malin Keshar
            x,y=5,18
        [/filter]
        [filter_condition]
            [have_unit]
                id=Darken Volk
            [/have_unit]
        [/filter_condition]

        [message]
            speaker=narrator
            message= _ "A monolith of dark stone, marred by cracks running throughout."
        [/message]

#ifdef EASY
        {GIVE_MALIN_EXPERIENCE 12}
#endif
#ifdef NORMAL
        {GIVE_MALIN_EXPERIENCE 10}
#endif
#ifdef HARD
        {GIVE_MALIN_EXPERIENCE 8}
#endif
    [/event]

    [event]
        name=moveto
        [filter]
            side=1
            x,y=31,3
        [/filter]

        # a lighthouse makes light and is a navigational aid
        [message]
            speaker=Malin Keshar
            message= _ "This lighthouse has been dark for quite some time. I wonder if I could relight it?"
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "... no, that would be silly..."
        [/message]
    [/event]

    # wmllint checks that every unit referred to in [event], [message] etc is first introduced by [unit], [recall] etc.
    # It does this to catch errors where a scenario author forgets to recall a unit.
    # Jaime is spawned by an easter egg in 07a_A_Small_Favor.
    # Jaime may be on the recall list for this scenario if the egg was triggered and if Jaime survives 07a, 07b and 07c.
    # I assume that in this case the scenario author intentionally counts on the player manually recalling Jaime instead of using [recall].
    # wmllint: recognize Jaime
    [event]
        name=moveto
        [filter]
            id=Jaime
            x,y=16,4
        [/filter]

        [sound]
            name=zombie-attack.wav
        [/sound]

        [delay]
            time=1000
        [/delay]

        [sound]
            name=zombie-hit-3.ogg
        [/sound]

        [delay]
            time=500
        [/delay]

        [message]
            speaker=Malin Keshar
            message= _ "He seems to be drinking the remains of the wine in the cargo hold."
        [/message]

        # now Jaime becomes a super zombie...
        [object]
            silent=yes
            duration=forever
            [filter]
                id=Jaime
            [/filter]
            [effect]
                apply_to=attack
                range=melee
                increase_damage=2
            [/effect]
            [effect]
                apply_to=hitpoints
                increase=8
                increase_total=8
            [/effect]
            [effect]
                apply_to=movement
                increase=2
            [/effect]
            [effect]
                apply_to=attack
                range=melee
                [set_specials]
                    {WEAPON_SPECIAL_DRUNKEN_RAGE}
                    mode=append
                [/set_specials]
            [/effect]
        [/object]
    [/event]

    # Paladins!
    [event]
        name=turn 6

        # 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=40,40,39,38,37
            y=23,22,22,21,21
            side=3
        [/move_unit_fake]

        [unit]
            type=Paladin
            side=3
            x,y=37,21
            id=Sir Cadaeus
            name= _ "Sir Cadaeus"
            canrecruit=yes
            animate=no
            facing=nw
        [/unit]

        {GENERIC_UNIT 3 (Paladin)       38 20} {FACING nw}
        {GENERIC_UNIT 3 (Mage of Light) 38 21} {FACING nw}

        # Make camp
        [terrain]
            x,y=37,21
            radius=1
            terrain=Ce
        [/terrain]

        [terrain]
            x,y=37,21
            terrain=Ke
        [/terrain]

        [redraw][/redraw]

        [modify_side]
            side=3
            {QUANTITY gold 50 75 100}
            {QUANTITY income 12 16 20}
            hidden=no
        [/modify_side]

        [message]
            speaker=Sir Cadaeus
            message= _ "Your travels in this region have not gone unnoticed, necromancers! The disaster you wreaked upon the good city of Tath and the noble Lady Karae can only be paid back in turn with your blood. Yes, on this day, you shall have to answer for your innumerable crimes."
        [/message]

        [message]
            speaker=Darken Volk
            message= _ "Pompous words from an arrogant fool. We shall see how well you fare once I have finished this upstart."
        [/message]
    [/event]

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

        {INCIDENTAL_MUSIC northern_mountains.ogg}

        [move_unit_fake]
            type=Frontier Baroness
            x=41,40,39,38,37,36
            y= 6, 6, 6, 6, 7, 7
            side=4
        [/move_unit_fake]

        [unit]
            side=4
            x,y=36,7

            animate=no
            moves=0
            facing=sw

            # wmllint: recognize Dela Keshar
            {CHARACTER_STATS_DELA_KESHAR}
        [/unit]

        # Make camp
        [terrain]
            x,y=36,7
            radius=1
            terrain=Ce
        [/terrain]

        [terrain]
            x,y=36,7
            terrain=Ke
        [/terrain]

        [redraw][/redraw]

        [modify_side]
            side=4
            {QUANTITY gold 160 200 240}
            {QUANTITY income 16 20 24}
            hidden=no
        [/modify_side]

        {MODIFY_UNIT (id=Dela Keshar) profile (portraits/dela-mad.png)}

        [message]
            speaker=Dela Keshar
            message= _ "I finally found you, Malin! At last, you’ll pay fer what you’ve done to my people and my home. I won’t let you live ta see another sunset."
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "... I suppose you did vow to kill me..."
        [/message]

        [message]
            speaker=Dela Keshar
            message= _ "You didn’t think I’d keep my word? You thought I’d forgive you fer killing Drogan and tearing apart our town? You don’t know how badly I want ta kill ya, Malin."
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "..."
        [/message]

        [message]
            speaker=Dela Keshar
            message= _ "Nothing to say this time? No quip ’bout how we don’t understand ya? No comment about yer moral superiority? I can only hope you’ll be so quiet when I kill you."
        [/message]

        {VARIABLE dela_arrived 1}
    [/event]

    [event]
        name=last breath
        [filter]
            id=Dela Keshar
        [/filter]

        [message]
            speaker=Dela Keshar
            message= _ "Vile fiend... I regret that I was... not strong enough... to destroy you..."
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "You brought this on yourself..."
        [/message]

        [if]
            [not]
                [have_unit]
                    id=Sir Cadaeus,Darken Volk
                [/have_unit]
            [/not]
            [then]
                [kill]
                    id=Dela Keshar
                    animate=yes
                [/kill]
                [fire_event]
                    name=alternate victory
                [/fire_event]
            [/then]
        [/if]
    [/event]

    [event]
        name=last breath
        [filter]
            id=Sir Cadaeus
        [/filter]

        [message]
            speaker=Sir Cadaeus
            message= _ "For the Light I fall..."
        [/message]

        [if]
            [not]
                [have_unit]
                    id=Dela Keshar,Darken Volk
                [/have_unit]
            [/not]
            [and]
                [variable]
                    name=dela_arrived
                    numerical_equals=1
                [/variable]
            [/and]
            [then]
                [kill]
                    id=Sir Cadaeus
                    animate=yes
                [/kill]
                [fire_event]
                    name=alternate victory
                [/fire_event]
            [/then]
        [/if]
    [/event]

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

        [message]
            speaker=Malin Keshar
            message= _ "See? This is what happens when you abandon me!"
        [/message]

        [message]
            speaker=Darken Volk
            message= _ "Ungrateful wretch, this is how you repay me? Curse you, Malin the Damned!"
        [/message]

        [kill]
            id=Darken Volk
            animate=yes
        [/kill]

        {PLACE_IMAGE items/book5.png $x1 $y1}

        {VARIABLE bookX $x1}
        {VARIABLE bookY $y1}

        [message]
            speaker=Malin Keshar
            message= _ "I have already been cursed many times over. One more time is nothing..."
        [/message]

        {COLOR_ADJUST -20 -20 -20}
        [delay]
            time=1500
        [/delay]

        [message]
            speaker=Malin Keshar
            message= _ "Well, I suppose I should get that book off the battlefield."
        [/message]

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

            {TURNS_RUN_OUT}

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

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

        {REMOVE_IMAGE 5 18}
        {PLACE_IMAGE scenery/black-monolith.png 23 14}

        [event]
            name=moveto
            [filter]
                id=Malin Keshar
                x,y=23,14
            [/filter]
            [message]
                speaker=narrator
                message= _ "A monolith of black stone, broken shards inexplicably held together by nothingness."
            [/message]

#ifdef EASY
            {GIVE_MALIN_EXPERIENCE 12}
#endif
#ifdef NORMAL
            {GIVE_MALIN_EXPERIENCE 10}
#endif
#ifdef HARD
            {GIVE_MALIN_EXPERIENCE 8}
#endif
        [/event]
    [/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,y=$bookX,$bookY
            side=1
            [not]
                type_adv_tree=Ghost,Vampire Bat
            [/not]
            [not]
                # this unit does not advance normally so each type has to be listed individually
                type=Spectral Servant,Phantom,Eidolon
            [/not]
        [/filter]

        [remove_item]
            x,y=$x1,$y1
            image="items/book5.png"
        [/remove_item]

        [role]
            x,y=$x1,$y1
            role=book_carrier
        [/role]

        [unit_overlay]
            x,y=$x1,$y1
            image="misc/book-icon.png"
            object_id="book_icon"
        [/unit_overlay]

        [if]
            [have_unit]
                x,y=$x1,$y1
                id=Malin Keshar
            [/have_unit]
            [then]
                [message]
                    speaker=Malin Keshar
                    message= _ "I have the book."
                [/message]
            [/then]
            [else]
                [message]
                    speaker=Malin Keshar
                    message= _ "My minions have captured the book."
                [/message]
            [/else]
        [/if]

        {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,Spectral Servant,Phantom,Eidolon
        [/filter]

        [message]
            speaker=unit
            message= _ "I am sorry, master, but this book is too heavy for me."
        [/message]
    [/event]

    [event]
        name=moveto
        first_time_only=no
        [filter]
            x=$bookX
            y=$bookY
            side=1
            type=Vampire Bat,Blood Bat,Dread Bat
        [/filter]

        [message]
            speaker=unit
            # po: this is just a squeaking sound a bat might make
            message= _ "Neep! Neep!"
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "It’s impossible for a bat to carry a book that heavy."
        [/message]
    [/event]

    [event]
        name=time over

        [unit]
            type=Paladin
            side=3
            x,y=37,21
            id=Telemon
            name= _ "Telemon the Slayer"
            canrecruit=yes
            animate=yes
            facing=nw
        [/unit]
        {GENERIC_UNIT 3 (Paladin)       36 20} {FACING nw}
        {GENERIC_UNIT 3 (Paladin)       37 20} {FACING nw}
        {GENERIC_UNIT 3 (Paladin)       36 21} {FACING nw}
        {GENERIC_UNIT 3 (Mage of Light) 37 22} {FACING nw}
        {GENERIC_UNIT 3 (Mage of Light) 38 20} {FACING nw}
        {GENERIC_UNIT 3 (Mage of Light) 38 21} {FACING nw}

        [message]
            speaker=Telemon
            message= _ "Foul necromancer! Your time has come!"
        [/message]
        [message]
            speaker=Malin Keshar
            message= _ "Reinforcements! I took too long to retrieve that book and now I have no way to win this battle..."
        [/message]
    [/event]

    # Victory Conditions
    [event]
        name=moveto
        [filter]
            role=book_carrier
            side=1
            [filter_location]
                terrain=C*,K*
                [and]
                    x,y=5,13
                    radius=1
                [/and]
            [/filter_location]
        [/filter]

        {MOVE_UNIT (id=Malin Keshar) 1 8}

        [message]
            speaker=Malin Keshar
            message= _ "The terrain is hazardous in these mountains. Neither the paladins nor Dela will be able to pursue me."
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "... then again, is there a point? Even should I escape, then what? All that I have fought for, everyone who I have tried to protect, everything is gone to me."
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "Life, death... unlife — at this point, I could accept any of them and nothing would change."
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "... but, I suppose giving up now would be a waste..."
        [/message]

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

    [event]
        name=alternate victory

        [message]
            speaker=Malin Keshar
            message= _ "So, they are all dead. The paladin, Darken Volk, Dela..."
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "All that I have fought for, everyone who I have tried to protect, everything is gone. Everyone is dead. Is this truly all that I am left with?"
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "... nothing at all..."
        [/message]

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

    [event]
        name=victory

        [remove_object]
            role=book_carrier
            object_id="book_icon"
        [/remove_object]
        {CLEAR_VARIABLE bookX,bookY,dela_arrived}
    [/event]

    {HERODEATH_MALIN}
[/scenario]