#textdomain wesnoth-did

# Attention when tweaking the first map used for this scenario:
# Cave (Uu^* & Uh^*) and canyon (Qxu) hexes have a special meaning,
# adding more of them can lead to units not being reachable.

[scenario]
    id=09_Descent_into_Darkness
    name= _ "Descent into Darkness"
    map_file=09a_Descent_into_Darkness.map
    turns=unlimited
    next_scenario=10_Endless_Night
    victory_when_enemies_defeated=no

    {UNDERGROUND}

    {INTRO_AND_SCENARIO_MUSIC heroes_rite.ogg underground.ogg}

    # This is a 3 part scenario that goes as follows (SPOILERS HERE)
    # Part 1 - Malin completes the ritual that is his declension into lichdom
    #       Gameplay - fight against ghouls+corpses, skeletons, then ghosts in that order
    #       Stand on runes that will give you buffs that help you beat the ritual
    # Part 2 - explore the new lair and complete some puzzles
    #       Puzzles give some small dmg bonuses
    #       You can regain health and get some exp from creeps, but be wary of the giant spider lurking somewhere
    # Part 3 - Boss battle with fire dragon
    #       Fight against respawning fire elementals, step onto runes to gain some free units (differs depending on rune)
    #       You get 3 ghost-type units (from recall list) that will help you
    #       When you defeat the dragon, you will get a special skeletal dragon with feeding

    [story]
        [part]
            story= _ "“To become a lich, one must first die.”"
            {STORYTXT_BACKGROUND book.jpg}
        [/part]
        [part]
            story= _ "So reads the book that Malin has reclaimed. “The spells of necromancy can free the spirit from the limitations of the flesh, but only once the soul has been unbound from the body. The necromancer must make the necessary incantations with his dying breaths, then conquer his own spirit in much the same way he binds the spirits of others. Because he retains his own will, however, the lich can call upon the awesome powers of the spirit world.”"
            {STORYTXT_BACKGROUND book.jpg}
        [/part]
        [part]
            story= _ "Malin closes the book. After all this time and all that he has struggled for, could he just surrender to despair and perish? He has never been one to simply give up, but then, is there anything left for him to struggle for? Still, the words, that terrifying idea, stick in the back of his mind. Life has offered him nothing; could death be any worse? Death... and not rebirth, but undeath."
            {STORYTXT_BACKGROUND book.jpg}
        [/part]
        [part]
            story= _ "With all of Wesnoth forbidden to him, Malin finds refuge in a secluded frontier village where few questions are asked. When word eventually filters to the town that Parthyn has been overrun by orcs, Malin rouses himself from his listless state without really knowing why, and heads northward."
            {STORYTXT_BACKGROUND travel.jpg}
        [/part]
        [part]
            story= _ "Malin soon finds the track of an enormous orc army and follows them until he reaches their camps. As he sees the number of orcs, he begins to realize the futility of his quest — harassing and harrying one clan of orcs is a far different task than waging a truly meaningful war against them as a whole. Then again, perhaps he is beyond the point of ordinary reason."
            {STORYTXT_BACKGROUND end.jpg}
        [/part]
        [part]
            story= _ "There is nothing else left for me to do anyway, Malin thinks, and raises his undead to assault the camp. Within mere minutes, nearly his entire army is destroyed and he is badly injured by a poisoned dagger. Malin retreats and finds refuge in a small cave."
            {STORYTXT_BACKGROUND end.jpg}
        [/part]
    [/story]

    {DID_TRACK {JOURNEY_09_NEW}}

    # wmllint: validate-off
    [side]
        side=1
        controller=human
        recruit=
        gold=0
        team_name=good
        village_gold=0
        village_support=4
        user_team_name= _ "Malin Keshar"
        {FLAG_VARIANT undead}
        color=darkblue

        income=-2

        # wmllint: recognize Malin Keshar
        {CHARACTER_STATS_MALIN_KESHAR}

        facing=ne
        fog=yes
        shroud=yes
    [/side]

    # wmllint: validate-on

    [side]
        side=2
        controller=ai
        no_leader=yes
        hidden=yes
        gold=0
        income=-2
        team_name=monsters
        user_team_name= _ "teamname^Opposers"
        color=black
        [ai]
            aggression=1.0
            caution=0.0
            grouping=no
            simple_targeting=yes
            village_value=0
            [goal]
                name=target
                [criteria]
                    side=1
                [/criteria]
                value=100
            [/goal]
        [/ai]
    [/side]

    # Monsters
    [side]
        side=3
        team_name=monsters
        user_team_name= _ "teamname^Monsters"
        gold=0
        controller=ai
        no_leader=yes
        hidden=yes
        income=-2
        color=orange

        fog=yes
        shroud=yes
        [ai]
            aggression=0.9
            caution=0.1
            grouping=no
            simple_targeting=yes
            village_value=0
        [/ai]
    [/side]

    [side]
        side=4
        controller=ai
        no_leader=yes
        hidden=yes
        team_name=fake
        user_team_name= _ "teamname^Illusion"
        color=darkblue
        # Using the same flag and color as the next side, one can't spot it in the top bar
        {FLAG_VARIANT undead}
    [/side]

    [event]
        name=prestart

        {CLEAR_CORPSE_HORDE}

        {COLOR_ADJUST -15 -15 -15}

        [object]
            duration=scenario
            [filter]
                id=Malin Keshar
            [/filter]
            [effect]
                apply_to=movement
                set=4
            [/effect]
        [/object]

        [store_unit]
            [filter]
                id=Malin Keshar
            [/filter]
            variable=malin
            kill=yes
        [/store_unit]

        [unit]
            id=Malin Keshar
            name= _ "Malin Keshar"
            type=Dark Mage
            side=1
            x,y=5,17
            profile=portraits/malin_old-decay.png
            canrecruit=yes
            unrenamable=yes

            hitpoints=11
            max_moves=4
            facing=ne

            [status]
                poisoned=yes
            [/status]
        [/unit]

        [disallow_recruit]
            side=1
        [/disallow_recruit]

        [objectives]
            side=1
            [objective]
                condition=win
                description= _ "Complete the ritual"
            [/objective]
            [objective]
                condition=lose
                description= _ "Death of Malin Keshar"
            [/objective]
            [note]
                description= _ "While Malin is standing on a runestone, he receives additional abilities"
            [/note]
        [/objectives]

        {VARIABLE scenario_stage 1}
    [/event]

#define GENERATE_VOID
    [store_locations]
        variable=hex
        terrain=Uu^*,Uh^*
        [filter_adjacent_location]
            terrain=Qxu
        [/filter_adjacent_location]
        [not]
            [filter][/filter]
        [/not]
    [/store_locations]
    [for]
        array=hex
        [do]
            [set_variable]
                name=change_terrain
                rand=1..4
            [/set_variable]
            [if]
                [variable]
                    name=change_terrain
                    numerical_equals=1
                [/variable]
                [then]
                    [terrain]
                        x,y=$hex[$i].x,$hex[$i].y
                        terrain=Qxu
                    [/terrain]
                [/then]
            [/if]
            {CLEAR_VARIABLE change_terrain}
        [/do]
    [/for]
    {CLEAR_VARIABLE hex}
    [redraw][/redraw]

    [store_locations]
        variable=hex
        terrain=Uh^*
        [not]
            [filter][/filter]
        [/not]
    [/store_locations]
    [for]
        array=hex
        [do]
            [set_variable]
                name=change_terrain
                rand=1..3
            [/set_variable]
            [if]
                [variable]
                    name=change_terrain
                    numerical_equals=1
                [/variable]
                [then]
                    [terrain]
                        x,y=$hex[$i].x,$hex[$i].y
                        terrain=Qxu
                    [/terrain]
                [/then]
            [/if]
            {CLEAR_VARIABLE change_terrain}
        [/do]
    [/for]
    {CLEAR_VARIABLE hex}
    [redraw][/redraw]
#enddef

    [event]
        name=start

        [message]
            speaker=Malin Keshar
            message= _ "Cursed beasts!"
        [/message]

        {MOVE_UNIT (id=Malin Keshar) 8 14}

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

        [message]
            speaker=Malin Keshar
            message= _ "I won’t go down like this, felled by an orc’s blade!"
        [/message]

        {MOVE_UNIT (id=Malin Keshar) 11 13}

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

        [delay]
            time=300
        [/delay]
        [scroll_to]
            x,y=11,12
        [/scroll_to]
        {MOVE_UNIT (id=Malin Keshar) 11 12}
        [delay]
            time=100
        [/delay]
        [harm_unit]
            [filter]
                id=Malin Keshar
            [/filter]
            amount=1
            animate=no
        [/harm_unit]

        [delay]
            time=400
        [/delay]
        [scroll_to]
            x,y=11,11
        [/scroll_to]
        {MOVE_UNIT (id=Malin Keshar) 11 11}
        [delay]
            time=100
        [/delay]
        [harm_unit]
            [filter]
                id=Malin Keshar
            [/filter]
            amount=1
            animate=no
        [/harm_unit]

        [delay]
            time=700
        [/delay]
        [scroll_to]
            x,y=12,10
        [/scroll_to]
        {MOVE_UNIT (id=Malin Keshar) 12 10}
        [delay]
            time=100
        [/delay]
        [harm_unit]
            [filter]
                id=Malin Keshar
            [/filter]
            amount=1
            animate=no
        [/harm_unit]

        [delay]
            time=1000
        [/delay]
        [scroll_to]
            x,y=12,9
        [/scroll_to]
        {MOVE_UNIT (id=Malin Keshar) 12 9}
        [delay]
            time=100
        [/delay]
        [harm_unit]
            [filter]
                id=Malin Keshar
            [/filter]
            amount=2
            animate=no
        [/harm_unit]
        [delay]
            time=300
        [/delay]
        [harm_unit]
            [filter]
                id=Malin Keshar
            [/filter]
            amount=2
            animate=no
        [/harm_unit]

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

        [delay]
            time=500
        [/delay]

        {CREATE_ADVISOR}

        [message]
            role=advisor
            message= _ "Master, you are gravely injured!"
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "So it seems..."
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "Perhaps it would be a suitable end. The orcs were the ones who provoked this journey of mine. Would it not be fitting for them to end my quest as well?"
        [/message]

        [message]
            role=advisor
            message= _ "There is another way, master. Remember the book!"
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "Yes... yes. The book. That half-rotten, tattered tome which I gave my blood and soul for. I forsook my people, my family, my home... to follow that damned necromancer! And in the end, he betrayed me too, just like Drogan, just like Dela..."
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "Everyone has betrayed me. I am alone. All that is left..."
        [/message]

        [delay]
            time=700
        [/delay]
        {MOVE_UNIT (id=Malin Keshar) 12 8}
        [delay]
            time=300
        [/delay]
        [harm_unit]
            [filter]
                id=Malin Keshar
            [/filter]
            amount=3
            animate=no
        [/harm_unit]

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

        [delay]
            time=1500
        [/delay]

        [message]
            speaker=Malin Keshar
            message= _ "Leave me while I prepare."
        [/message]

        [message]
            role=advisor
            message= _ "Yes, master."
        [/message]

        {PUT_TO_RECALL_LIST (role=advisor)}

        [scroll_to]
            x,y=12,7
        [/scroll_to]
        [terrain]
            x=12
            y=7
            terrain=Urb
        [/terrain]
        [redraw][/redraw]
        [item]
            x,y=12,7
            image=misc/makeshift-altar.png
        [/item]

        [delay]
            time=500
        [/delay]

        [terrain]
            x=11,13,11,13,10,14
            y= 6, 6, 9, 9, 7, 7
            terrain=Urb
        [/terrain]
        [redraw][/redraw]

        [delay]
            time=1000
        [/delay]

        [message]
            speaker=Malin Keshar
            message= _ "... I suppose I have never been one for regrets..."
        [/message]

        [delay]
            time=1000
        [/delay]

        [recall]
            role=advisor
            x,y=11,8
        [/recall]

        {MODIFY_UNIT role=advisor facing se}

        [delay]
            time=1500
        [/delay]

        [animate_unit]
            flag=attack
            hits=yes

            [filter]
                role=advisor
            [/filter]

            [primary_attack]
                range=melee
            [/primary_attack]

            [facing]
                [filter]
                    id=Malin Keshar
                [/filter]
            [/facing]

            [animate]
                flag=defend

                [filter]
                    id=Malin Keshar
                [/filter]

                hits=no

                [facing]
                    [filter]
                        role=advisor
                    [/filter]
                [/facing]
            [/animate]
        [/animate_unit]

        [kill]
            id=Malin Keshar
            fire_event=no
            animate=yes
        [/kill]

        {PUT_TO_RECALL_LIST (role=advisor)}

        {REPLACE_SCENARIO_MUSIC silence.ogg}

        {FADE_TO_BLACK}

        [place_shroud]
            side=1
            x,y=0-23,0-20
        [/place_shroud]

        [replace_schedule]
            [time]
                id=limbo1
                name= _ "Limbo"
                image=misc/time-schedules/after-the-fall/6shortdark.png
                lawful_bonus=-30
                red=-50
                green=-50
                blue=-15
            [/time]
        [/replace_schedule]

        [terrain]
            x=0-8
            y=13-20
            terrain=Xu
        [/terrain]
        [redraw][/redraw]

        {GENERATE_VOID}

        [random_placement]
            num_items=1
            variable=loc
            min_distance=5
            [filter_location]
                terrain=Uu^*,Uh^*
                # 3 spawn points:
                # (some hexes are on impassable, but the player gets no into vision in time to find out)
                # 1st: North-chamber       9/34 = ~26%
                # 2nd: Northeast-chamber   9/34 = ~26%
                # 3rd: south-east chamber 16/34 = ~47%
                x=7-9,3-5,16-19
                y=1-3,4-6,11-14
                [not]
                    [filter][/filter]
                [/not]
            [/filter_location]
            [command]
                [unit]
                    id=MKFlesh
                    name= _ "Flesh of Malin Keshar"
                    type=Necrophage
                    moves=0
                    max_moves=2
                    side=2
                    x,y=$loc.x,$loc.y
                    unrenamable=yes
                [/unit]
            [/command]
        [/random_placement]

        [delay]
            time=1500
        [/delay]

        [unstore_unit]
            variable=malin
            x,y=12,7
        [/unstore_unit]
        {CLEAR_VARIABLE malin}

        {MODIFY_UNIT (id=Malin Keshar) profile (portraits/malin_young.png)}
        {MODIFY_UNIT (id=Malin Keshar) moves 4}

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

        {PLACE_IMAGE scenery/rune4.png 13 6}
        {PLACE_IMAGE scenery/rune4.png 11 9}
        {PLACE_IMAGE scenery/rune3.png 14 7}
        {PLACE_IMAGE scenery/rune3.png 10 7}
        {PLACE_IMAGE scenery/rune1.png 11 6}
        {PLACE_IMAGE scenery/rune1.png 13 9}

        {FADE_IN}
        {REPLACE_SCENARIO_MUSIC into_the_shadows.ogg}
        {APPEND_MUSIC           weight_of_revenge.ogg}
        {APPEND_MUSIC           underground.ogg}

        [delay]
            time=1000
        [/delay]

        [message]
            speaker=Malin Keshar
            message= _ "I feel... different. Where am I?"
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "Why is my body still intact? Am I still alive? Or..."
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "... something isn’t quite right."
        [/message]

        [animate_unit]
            [filter]
                id=Malin Keshar
            [/filter]
            [primary_attack]
                range=ranged
            [/primary_attack]
            flag=attack
        [/animate_unit]

        [delay]
            time=400
        [/delay]

        [unit]
            type=Ghoul
            x,y=15,8
            side=2
            animate=yes
            max_moves=4
        [/unit]

        [message]
            speaker=Malin Keshar
            message= _ "I see, accepting death was not enough. To become a lich, I must master it."
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "First comes the flesh, the physical form of life. Blood is the crux of life for mortal creatures, but if I wish to ascend beyond that..."
        [/message]

        [random_placement]
            num_items={ON_DIFFICULTY 2 3 4}
            variable=loc
            min_distance=3
            [filter_location]
                terrain=Uu^*,Uh^*
                [not]
                    x=18-23
                    y=1-5
                [/not]
                [not]
                    x,y=12,7
                    radius=3
                [/not]
                [not]
                    [filter][/filter]
                [/not]
            [/filter_location]
            [command]
                [unit]
                    type=Ghoul
                    side=2
                    x,y=$loc.x,$loc.y
                    max_moves=4
                    ai_special=guardian
                [/unit]
            [/command]
        [/random_placement]
        [random_placement]
            num_items={ON_DIFFICULTY 4 5 6}
            variable=loc
            min_distance=1
            [filter_location]
                terrain=Uu^*,Uh^*
                [not]
                    x=18-23
                    y=1-5
                [/not]
                [not]
                    x,y=12,7
                    radius=3
                [/not]
                [not]
                    [filter][/filter]
                [/not]
            [/filter_location]
            [command]
                [unit]
                    type=Walking Corpse
                    side=2
                    x,y=$loc.x,$loc.y
                    max_moves=3
                [/unit]
            [/command]
        [/random_placement]
    [/event]

#define RESET_RUNES
    {REMOVE_IMAGE 13 6}
    {REMOVE_IMAGE 11 9}
    {REMOVE_IMAGE 14 7}
    {REMOVE_IMAGE 10 7}
    {REMOVE_IMAGE 11 6}
    {REMOVE_IMAGE 13 9}
    {PLACE_IMAGE scenery/rune4.png 13 6}
    {PLACE_IMAGE scenery/rune4.png 11 9}
    {PLACE_IMAGE scenery/rune3.png 14 7}
    {PLACE_IMAGE scenery/rune3.png 10 7}
    {PLACE_IMAGE scenery/rune1.png 11 6}
    {PLACE_IMAGE scenery/rune1.png 13 9}
#enddef

    # Ritual part 2

    [event]
        name=die
        first_time_only=no
        [filter]
            id=MKFlesh
        [/filter]

        [kill]
            side=2
        [/kill]

        [message]
            speaker=Malin Keshar
            message= _ "The flesh dies, and then..."
        [/message]

        [delay]
            time=1000
        [/delay]
        {FADE_TO_BLACK}

        {RESET_RUNES}

        [store_unit]
            [filter]
                id=Malin Keshar
            [/filter]
            variable=malin
            kill=yes
        [/store_unit]

        [place_shroud]
            side=1
            x,y=0-23,0-20
        [/place_shroud]

        [replace_schedule]
            [time]
                id=limbo2
                name= _ "Limbo"
                image=misc/time-schedules/after-the-fall/6shortdark.png
                lawful_bonus=-35
                red=-60
                green=-60
                blue=-20
            [/time]
        [/replace_schedule]

        {GENERATE_VOID}

        [delay]
            time=1500
        [/delay]

        [unstore_unit]
            variable=malin
            x,y=12,7
        [/unstore_unit]
        {CLEAR_VARIABLE malin}
        {FULL_HEAL (id=Malin Keshar)}
        {MODIFY_UNIT (id=Malin Keshar) attacks_left 1}
        {MODIFY_UNIT (id=Malin Keshar) moves 4}

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

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

        {FADE_IN}

        [message]
            speaker=Malin Keshar
            message= _ "Drain the blood, strip away the meat, and all that is left is the bone."
        [/message]

        [animate_unit]
            [filter]
                id=Malin Keshar
            [/filter]
            [primary_attack]
                range=ranged
            [/primary_attack]
            flag=attack
        [/animate_unit]

        [delay]
            time=400
        [/delay]

        [unit]
            type=Skeleton
            x,y=11,7
            side=2
            max_moves=4
            animate=yes
        [/unit]

        [message]
            speaker=Malin Keshar
            message= _ "I remember a time when I thought skeletons were repulsive, sickening things. I suppose it’s only natural for the living to fear the most prominent figure of death. Even if I grew out of such a naive thought... I suppose I couldn’t have expected Drogan and Dela to follow suit so easily. My family and friends, yes, but they were nothing but simpletons. If only they had tried to understand me instead of shunning me outright."
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "I do not really regret following my own path that much."
        [/message]

        [random_placement]
            num_items={ON_DIFFICULTY 3 4 5}
            variable=loc
            min_distance=3
            [filter_location]
                terrain=Uu^*,Uh^*
                [not]
                    x=18-23
                    y=1-5
                [/not]
                [not]
                    x,y=12,7
                    radius=3
                [/not]
                [not]
                    [filter][/filter]
                [/not]
            [/filter_location]
            [command]
                [unit]
                    type=Skeleton
                    side=2
                    x,y=$loc.x,$loc.y
                    max_moves=4
                    ai_special=guardian
                [/unit]
            [/command]
        [/random_placement]
        [random_placement]
            num_items={ON_DIFFICULTY 3 4 4}
            variable=loc
            min_distance=1
            [filter_location]
                terrain=Uu^*,Uh^*
                [not]
                    x=18-23
                    y=1-5
                [/not]
                [not]
                    x,y=12,7
                    radius=3
                [/not]
                [not]
                    [filter][/filter]
                [/not]
            [/filter_location]
            [command]
                [unit]
                    type=Skeleton Archer
                    side=2
                    x,y=$loc.x,$loc.y
                    max_moves=3
                [/unit]
            [/command]
        [/random_placement]

        [random_placement]
            num_items=1
            variable=loc
            min_distance=3
            # Spawn chances:
            # 1st: 2/12 = ~17% (north)
            # 2nd: 2/12 = ~17% (west)
            # 3rd: 2/12 = ~17% (west)
            # 4th: 2/12 = ~17% (southeast)
            # 5th: 4/12 = ~33% (south)
            [filter_location]
                x=8-9,6,5,5-6,16-17,11-14
                y=  4,6,7,  8,   11,   12
                [not]
                    [filter][/filter]
                [/not]
            [/filter_location]
            [command]
                [unit]
                    id=MKBone
                    name= _ "Bones of Malin Keshar"
                    type=Revenant
#ifdef EASY
                    hitpoints=60
                    max_hitpoints=60
#endif
#ifdef NORMAL
                    hitpoints=75
                    max_hitpoints=75
#endif
#ifdef HARD
                    hitpoints=90
                    max_hitpoints=90
#endif
                    moves=0
                    max_moves=2
                    side=2
                    x,y=$loc.x,$loc.y
                    unrenamable=yes
                [/unit]
            [/command]
        [/random_placement]

        {MODIFY_UNIT (side=2) attacks_left 0}
        {MODIFY_UNIT (side=2) moves 0}
    [/event]

    # Ritual part 3

    [event]
        name=die
        first_time_only=no
        [filter]
            id=MKBone
        [/filter]

        [kill]
            side=2
        [/kill]

        [message]
            speaker=Malin Keshar
            message= _ "The physical form collapses altogether. What remains?"
        [/message]

        [delay]
            time=1000
        [/delay]
        {FADE_TO_BLACK}

        {RESET_RUNES}

        [store_unit]
            [filter]
                id=Malin Keshar
            [/filter]
            variable=malin
            kill=yes
        [/store_unit]

        [place_shroud]
            side=1
            x,y=0-23,0-20
        [/place_shroud]

        [replace_schedule]
            [time]
                id=limbo3
                name= _ "Limbo"
                image=misc/time-schedules/after-the-fall/6shortdark.png
                lawful_bonus=-40
                red=-70
                green=-70
                blue=-30
            [/time]
        [/replace_schedule]

        {GENERATE_VOID}
        {GENERATE_VOID}
        {GENERATE_VOID}

        [delay]
            time=1500
        [/delay]

        [unstore_unit]
            variable=malin
            x,y=12,7
        [/unstore_unit]
        {CLEAR_VARIABLE malin}
        {FULL_HEAL (id=Malin Keshar)}
        {MODIFY_UNIT (id=Malin Keshar) attacks_left 1}
        {MODIFY_UNIT (id=Malin Keshar) moves 4}

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

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

        {FADE_IN}

        [animate_unit]
            [filter]
                id=Malin Keshar
            [/filter]
            [primary_attack]
                range=ranged
            [/primary_attack]
            flag=attack
        [/animate_unit]

        [delay]
            time=400
        [/delay]

        [unit]
            type=Ghost
            x,y=14,5
            side=2
            animate=yes
        [/unit]

        [message]
            speaker=Malin Keshar
            message= _ "The soul. Dela always said I was a creature without a soul. I suppose she was jesting at the time, but maybe not after I killed Drogan."
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "Bah! I don’t deserve this, and I certainly don’t answer to someone like her. I have only ever fought to protect my people and my family, but the only thing I received in return was scorn and exile. My own sister thought I was a monster for using my power to save our town. My people would rather feed themselves to orcs than follow my ‘sorcerous ways’. Drogan, Dela... all of them! They are the ones without souls, not me!"
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "I should not really care anymore. I don’t care anymore. According to them, I have forsaken my soul already. So what does it matter?"
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "I should not regret anything. I’m not really one for regrets..."
        [/message]

        [random_placement]
            num_items={ON_DIFFICULTY 3 3 4}
            variable=loc
            min_distance=3
            [filter_location]
                terrain=Uu^*,Uh^*,Qxu
                [not]
                    x=18-23
                    y=1-5
                [/not]
                [not]
                    x,y=12,7
                    radius=4
                [/not]
                [not]
                    [filter][/filter]
                [/not]
            [/filter_location]
            [command]
                {NOTRAIT_UNIT 2 "Ghost" $loc.x $loc.y}
            [/command]
        [/random_placement]

        [random_placement]
            num_items={ON_DIFFICULTY 1 2 2}
            variable=loc
            min_distance=3
            [filter_location]
                terrain=Uu^*,Uh^*,Qxu
                [not]
                    x=18-23
                    y=1-5
                [/not]
                [not]
                    x,y=12,7
                    radius=4
                [/not]
                [not]
                    [filter][/filter]
                [/not]
            [/filter_location]
            [command]
                {NOTRAIT_UNIT 2 "Wraith" $loc.x $loc.y}
            [/command]
        [/random_placement]

        {MODIFY_UNIT (side=2) attacks_left 0}
        {MODIFY_UNIT (side=2) moves 0}
        {MODIFY_UNIT (side=2) max_moves 2}

        [random_placement]
            num_items=1
            variable=loc
            min_distance=3
            # Same spawn points as MKFlesh
            [filter_location]
                x=7-9,3-5,16-19
                y=1-3,4-6,11-14
                [not]
                    [filter][/filter]
                [/not]
            [/filter_location]
            [command]
                [unit]
                    id=MKSpirit
                    name= _ "Soul of Malin Keshar"
                    type=Spectre
#ifdef EASY
                    hitpoints=50
                    max_hitpoints=50
#endif
#ifdef NORMAL
                    hitpoints=60
                    max_hitpoints=60
#endif
#ifdef HARD
                    hitpoints=70
                    max_hitpoints=70
#endif
                    moves=0
                    max_moves=2
                    side=2
                    x,y=$loc.x,$loc.y
                    unrenamable=yes
                [/unit]
            [/command]
        [/random_placement]
    [/event]

    # Malin gets to heal upon killing a unit
    [event]
        name=die
        first_time_only=no
        [filter]
            side=2
            [not]
                id=MKFlesh,MKBone,MKSpirit
            [/not]
        [/filter]
        [filter_second]
            side=1
        [/filter_second]

        [heal_unit]
            [filter]
                id=Malin Keshar
            [/filter]
            amount=8
            restore_statuses=yes
            animate=yes
        [/heal_unit]
        {CLEAR_VARIABLE heal_amount}

        [fire_event]
            name=heal_on_kill_dialogue
        [/fire_event]
    [/event]

    [event]
        name=heal_on_kill_dialogue

        [message]
            speaker=Malin Keshar
            message= _ "These thralls contain only a meager amount of energy, but enough to sustain me."
        [/message]
    [/event]

    # Sighting the bosses
    [event]
        name=sighted
        [filter]
            id=MKFlesh
        [/filter]
        [filter_second]
            side=1
        [/filter_second]

        [message]
            speaker=Malin Keshar
            message= _ "The flesh appears, distorted and warped, but somehow strangely familiar."
        [/message]
    [/event]
    [event]
        name=sighted
        [filter]
            id=MKBone
        [/filter]
        [filter_second]
            side=1
        [/filter_second]

        [message]
            speaker=Malin Keshar
            message= _ "I feel an aching in my bones, as if they are crumbling inside me."
        [/message]
    [/event]
    [event]
        name=sighted
        [filter]
            id=MKSpirit
        [/filter]
        [filter_second]
            side=1
        [/filter_second]

        [message]
            speaker=Malin Keshar
            message= _ "Cold... it’s so very cold. Not a biting chill, but a numbness, an emptiness that eats at me from within..."
        [/message]
    [/event]

    # Ritual runes

    [event]
        name=moveto
        first_time_only=no
        [filter]
            id=Malin Keshar
            x=13,11
            y=6,9
        [/filter]

        {REMOVE_IMAGE $x1 $y1}
        {PLACE_IMAGE scenery/rune4-glow.png $x1 $y1}

        [sound]
            name=magic-dark.ogg
        [/sound]

        [object]
            id=precision_rune
            take_only_once=no
            silent=yes
            duration=scenario
            [filter]
                id=Malin Keshar
            [/filter]
            [effect]
                apply_to=new_ability
                [abilities]
                    [leadership]
                        id=conscience
                        name= _ "conscience"
                        name_inactive= _ "conscience"
                        description= _ "This unit has its damage increased by 50%"
                        description_inactive= _ "This unit has its damage increased by 50%"
                        value=50
                        affect_self=yes
                        affect_enemies=no
                        affect_allies=no
                        [filter]
                            id=Malin Keshar
                            x=13,11
                            y=6,9
                        [/filter]
                    [/leadership]
                [/abilities]
            [/effect]
        [/object]

        [fire_event]
            name=rune dialogue
        [/fire_event]
    [/event]

    [event]
        name=moveto
        first_time_only=no
        [filter]
            id=Malin Keshar
            x=13,11
            y=9,6
        [/filter]

        {REMOVE_IMAGE $x1 $y1}
        {PLACE_IMAGE scenery/rune1-glow.png $x1 $y1}

        [sound]
            name=petrified.ogg
        [/sound]

        [object]
            id=carapace_rune
            take_only_once=no
            silent=yes
            duration=scenario
            [filter]
                id=Malin Keshar
            [/filter]
            [effect]
                apply_to=new_ability
                [abilities]
                    [resistance]
                        id=carapace
                        max_value=90
                        add=40
                        name= _ "carapace"
                        name_inactive= _ "carapace"
                        description= _ "This unit gains 40% to all resistances."
                        description_inactive= _ "This unit gains 40% to all resistances."
                        affect_self=yes
                        [filter]
                            id=Malin Keshar
                            x=13,11
                            y=9,6
                        [/filter]
                    [/resistance]
                [/abilities]
            [/effect]
        [/object]

        [fire_event]
            name=rune dialogue
        [/fire_event]
    [/event]

    [event]
        name=moveto
        first_time_only=no
        [filter]
            id=Malin Keshar
            x=14,10
            y=7,7
        [/filter]

        {REMOVE_IMAGE $x1 $y1}
        {PLACE_IMAGE scenery/rune3-glow.png $x1 $y1}

        [sound]
            name=magic-missile-1.ogg
        [/sound]

        [object]
            id=lifeblood_rune
            take_only_once=no
            silent=yes
            duration=scenario
            [filter]
                id=Malin Keshar
            [/filter]
            [effect]
                apply_to=new_ability
                [abilities]
                    [regenerate]
                        id=lifeblood
                        value=40
                        name= _ "lifeblood"
                        name_inactive= _ "lifeblood"
                        description= _ "This unit will heal 40 HP every turn. If it is poisoned, it will remove the poison instead of healing."
                        description_inactive= _ "This unit will heal 40 HP every turn. If it is poisoned, it will remove the poison instead of healing."
                        affect_self=yes
                        poison=cured
                        [filter]
                            id=Malin Keshar
                            x=14,10
                            y=7,7
                        [/filter]
                    [/regenerate]
                [/abilities]
            [/effect]
        [/object]

        [fire_event]
            name=rune dialogue
        [/fire_event]
    [/event]

    [event]
        name=moveto
        first_time_only=no
        [filter]
            id=Malin Keshar
            x,y=1-23,1-20
        [/filter]

        [if]
            [variable]
                name=x2
                numerical_equals=13
            [/variable]
            [then]
                [if]
                    [variable]
                        name=y2
                        numerical_equals=6
                    [/variable]
                    [then]
                        {REMOVE_IMAGE $x2 $y2}
                        {PLACE_IMAGE scenery/rune4.png $x2 $y2}
                    [/then]
                    [elseif]
                        [variable]
                            name=y2
                            numerical_equals=9
                        [/variable]
                        [then]
                            {REMOVE_IMAGE $x2 $y2}
                            {PLACE_IMAGE scenery/rune1.png $x2 $y2}
                        [/then]
                    [/elseif]
                [/if]
            [/then]
            [elseif]
                [variable]
                    name=x2
                    numerical_equals=11
                [/variable]
                [then]
                    [if]
                        [variable]
                            name=y2
                            numerical_equals=6
                        [/variable]
                        [then]
                            {REMOVE_IMAGE $x2 $y2}
                            {PLACE_IMAGE scenery/rune1.png $x2 $y2}
                        [/then]
                        [elseif]
                            [variable]
                                name=y2
                                numerical_equals=9
                            [/variable]
                            [then]
                                {REMOVE_IMAGE $x2 $y2}
                                {PLACE_IMAGE scenery/rune4.png $x2 $y2}
                            [/then]
                        [/elseif]
                    [/if]
                [/then]
            [/elseif]
            [elseif]
                [variable]
                    name=x2
                    numerical_equals=10
                [/variable]
                [then]
                    [if]
                        [variable]
                            name=y2
                            numerical_equals=7
                        [/variable]
                        [then]
                            {REMOVE_IMAGE $x2 $y2}
                            {PLACE_IMAGE scenery/rune3.png $x2 $y2}
                        [/then]
                    [/if]
                [/then]
            [/elseif]
            [elseif]
                [variable]
                    name=x2
                    numerical_equals=14
                [/variable]
                [then]
                    [if]
                        [variable]
                            name=y2
                            numerical_equals=7
                        [/variable]
                        [then]
                            {REMOVE_IMAGE $x2 $y2}
                            {PLACE_IMAGE scenery/rune3.png $x2 $y2}
                        [/then]
                    [/if]
                [/then]
            [/elseif]
        [/if]
    [/event]

    [event]
        name=rune dialogue

        [message]
            speaker=Malin Keshar
            message= _ "These runes seem to be useful."
        [/message]
    [/event]

    # Ritual completion

    [event]
        name=die
        [filter]
            id=MKSpirit
        [/filter]

        {REPLACE_SCENARIO_MUSIC silence.ogg}
        [kill]
            side=2
        [/kill]
        [delay]
            time=1000
        [/delay]

        [harm_unit]
            [filter]
                id=Malin Keshar
            [/filter]
            amount=999
            kill=no
        [/harm_unit]
        [delay]
            time=1000
        [/delay]

        [kill]
            id=Malin Keshar
            animate=yes
        [/kill]
        [delay]
            time=1000
        [/delay]

        {FADE_TO_BLACK}

        {REMOVE_IMAGE 13 6}
        {REMOVE_IMAGE 11 9}
        {REMOVE_IMAGE 14 7}
        {REMOVE_IMAGE 10 7}
        {REMOVE_IMAGE 11 6}
        {REMOVE_IMAGE 13 9}
        {REMOVE_IMAGE 12 7}

        [place_shroud]
            side=1
            x,y=0-23,0-20
        [/place_shroud]

        [message]
            speaker=narrator
            message= _ "There is darkness..."
        [/message]

        [delay]
            time=1000
        [/delay]

        [message]
            speaker=narrator
            message= _ "and peace..."
        [/message]

        [delay]
            time=1000
        [/delay]

        [message]
            speaker=narrator
            message= _ "for a moment."
        [/message]

        [delay]
            time=1000
        [/delay]

        [message]
            speaker=narrator
            message= _ "Then, they are replaced by a pulling, a pain too strong to resist, and then..."
        [/message]

        [message]
            speaker=narrator
            message= _ "... by emptiness."
        [/message]

        [replace_map]
            map_file=09_Descent_into_Darkness.map
            expand=yes
            shrink=yes
        [/replace_map]

        [place_shroud]
            side=1
            x,y=0-30,0-36
        [/place_shroud]

        {PLACE_IMAGE scenery/signpost.png 13 27}

        {PLACE_IMAGE items/altar-evil.png 6 15}
        {PLACE_IMAGE items/potion-clear.png 15 22}
        {PLACE_IMAGE items/stone-tablet.png 16 17}

        {PLACE_IMAGE items/coffin-closed.png 3 2}
        {PLACE_IMAGE items/bones.png 1 3}
        {PLACE_IMAGE items/key.png 2 1}
        {PLACE_IMAGE items/stone-tablet.png 2 4}

        # treasure boxes
        {PLACE_IMAGE items/chest-plain-closed.png 4 19}
        {PLACE_IMAGE items/chest-plain-closed.png 28 23}
        {PLACE_IMAGE items/chest-plain-closed.png 27 6}
        {PLACE_IMAGE items/chest-plain-closed.png 4 26}

        [replace_schedule]
            {UNDERGROUND}
        [/replace_schedule]

        [delay]
            time=1000
        [/delay]

        [remove_shroud]
            side=1
            x,y=3,34
            radius=3
        [/remove_shroud]
        [lift_fog]
            [filter_side]
                side=1
            [/filter_side]
            x,y=3,34
            radius=3
            multiturn=yes
        [/lift_fog]

        {FADE_IN}

        {COLOR_ADJUST -15 -15 -15}

        # And is reborn as a lich
        [unit]
            side=1
            x,y=3,34
#ifdef EASY
            hitpoints=33
#else
            hitpoints=28
#endif
            facing=se
            animate=yes

            # wmllint: recognize Mal Keshar
            {CHARACTER_STATS_MAL_KESHAR}
        [/unit]

        [modify_side]
            side=1
            user_team_name= _ "Mal Keshar"
        [/modify_side]

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

        {INCIDENTAL_MUSIC the_dangerous_symphony.ogg}
        [music]
            name=into_the_shadows.ogg
            append=yes
        [/music]
        [music]
            name=underground.ogg
            append=yes
        [/music]
        [music]
            name=weight_of_revenge.ogg
            append=yes
        [/music]

        # He's not necessarily totally happy about this
        [message]
            speaker=Mal Keshar
            sound=lich-die.ogg
            message= _ "<big>AAAaaiiigghh!!</big>" # wmllint: no spellcheck
        [/message]

        [message]
            speaker=Mal Keshar
            message= _ "The cold, it burns!"
        [/message]

        [message]
            speaker=Mal Keshar
            message= _ "I need warmth... life..."
        [/message]

        [remove_shroud]
            side=1
            x=8-9,10
            y=29-31,29-30
        [/remove_shroud]
        [lift_fog]
            [filter_side]
                side=1
            [/filter_side]
            x=8-9,10
            y=29-31,29-30
            multiturn=yes
        [/lift_fog]

        [animate_unit]
            flag=attack
            hits=yes

            [filter]
                id=Mal Keshar
            [/filter]

            [primary_attack]
                name=shadow wave
            [/primary_attack]
        [/animate_unit]

        [scroll_to]
            x,y=9,30
        [/scroll_to]

        {QUAKE cave-in.ogg}

        [item]
            x,y=10,29
            image=misc/blank-hex.png
            # HACK: An item can't be removed after only one cycle, so set the blank hex at the end
            halo="halo/undead/dark-magic-[1~6].png,misc/blank-hex.png:100000"
        [/item]

        [delay]
            time=1000
        [/delay]

        [terrain]
            x,y=10-11,29
            terrain=Qxu^Bcx/
        [/terrain]

        [remove_item]
            x,y=10,29
        [/remove_item]

        [message]
            speaker=Mal Keshar
            message= _ "I sense some primitive life in that direction. Perhaps I can sustain myself upon them."
        [/message]

        [reset_fog]
            [filter_side]
                side=1
            [/filter_side]
        [/reset_fog]

        [random_placement]
            num_items=5
            variable=loc
            min_distance=3
            [filter_location]
                x,y=1-29,1-24
                terrain=Uu^*
                [not]
                    [filter][/filter]
                [/not]
            [/filter_location]
            [command]
                {NOTRAIT_UNIT 3 "Vampire Bat" $loc.x $loc.y} {GUARDIAN}
            [/command]
        [/random_placement]

        [random_placement]
            num_items={ON_DIFFICULTY 1 2 2}
            variable=loc
            min_distance=3
            [filter_location]
                x,y=1-29,1-24
                terrain=Uu^*
                [not]
                    [filter][/filter]
                [/not]
            [/filter_location]
            [command]
                [unit]
                    type=Blood Bat
                    side=3
                    x,y=$loc.x,$loc.y
                    max_moves=4
                [/unit]
            [/command]
        [/random_placement]

        [random_placement]
            num_items={ON_DIFFICULTY 2 3 3}
            variable=loc
            min_distance=3
            [filter_location]
                x,y=1-29,1-24
                terrain=Uu^*
                [not]
                    [filter][/filter]
                [/not]
            [/filter_location]
            [command]
                {NOTRAIT_UNIT 3 "Giant Rat" $loc.x $loc.y}
            [/command]
        [/random_placement]

        {MODIFY_UNIT (type=Giant Rat) max_moves 3}

        [random_placement]
            num_items=3
            variable=loc
            min_distance=3
            [filter_location]
                x,y=1-29,1-24
                terrain=Uu^*
                [not]
                    [filter][/filter]
                [/not]
            [/filter_location]
            [command]
                {NOTRAIT_UNIT 3 "Giant Rat" $loc.x $loc.y} {GUARDIAN}
            [/command]
        [/random_placement]

        [random_placement]
            num_items=10
            variable=loc
            min_distance=3
            [filter_location]
                x,y=1-29,1-24
                terrain=Uu^*
                [not]
                    [filter][/filter]
                [/not]
            [/filter_location]
            [command]
                {NOTRAIT_UNIT 3 "Giant Scorpling" $loc.x $loc.y} {GUARDIAN}
            [/command]
        [/random_placement]

        [random_placement]
            num_items=1
            variable=loc
            min_distance=11
            [filter_location]
                x,y=1-29,1-24
                terrain=Uu^*
                [not]
                    [filter][/filter]
                [/not]
            [/filter_location]
            [command]
                {NOTRAIT_UNIT 3 "Giant Spider" $loc.x $loc.y} {GUARDIAN}
            [/command]
        [/random_placement]

        [unit]
            type=Dread Bat
            side=3
            x,y=2,23
            max_hitpoints=60
            hitpoints=60
            max_moves=5
            ai_special=guardian
            moves=5
            level=3
        [/unit]

        {MODIFY_UNIT (type=Giant Spider) max_moves 3}

        {VARIABLE scenario_stage 2}
        {VARIABLE puzzle_coffin 0}
        {VARIABLE puzzle_potion 0}

        {PLACE_IMAGE scenery/black-monolith.png 7 35}

        [label]
            x,y=29,33
            text= _ "Reflection pool"
        [/label]

        [objectives]
            side=1
            [objective]
                condition=win
                description= _ "Regain your strength"
            [/objective]
            [objective]
                condition=lose
                description= _ "Destruction of Mal Keshar"
            [/objective]

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

    # text at lair entrance
    [event]
        name=moveto
        first_time_only=no
        [filter]
            id=Mal Keshar
            x,y=13,27
        [/filter]
        [filter_condition]
            [variable]
                name=scenario_stage
                numerical_equals=2
            [/variable]
        [/filter_condition]

        [message]
            speaker=narrator
            # wmllint: local spelling Zephryn
            message= _ "<s>Welcome to Zephryn’s laboratory. Please be mindful of ongoing experiments during your visit.</s>
<big>This territory is the domain of the mighty Xanthric. All unwelcome intruders shall die.</big>"
            image=wesnoth-icon.png
        [/message]

        [fire_event]
            name=lair text
        [/fire_event]
    [/event]

    [event]
        name=lair text

        [message]
            speaker=Mal Keshar
            message= _ "That mage’s name is suspiciously... familiar."
        [/message]
    [/event]

    # monolith
    [event]
        name=moveto
        [filter]
            id=Mal Keshar
            x,y=7,35
        [/filter]
        [filter_condition]
            [variable]
                name=scenario_stage
                numerical_equals=2
            [/variable]
        [/filter_condition]

        [message]
            speaker=narrator
            message= _ "A monolith of black stone, a fractured surface shrouded by darkened shadow."
        [/message]
    [/event]

    # small flavor events
    [event]
        name=attack
        [filter]
            type=Blood Bat
        [/filter]
        [filter_second]
            side=1
        [/filter_second]
        [filter_condition]
            [variable]
                name=scenario_stage
                numerical_equals=2
            [/variable]
        [/filter_condition]

        [message]
            speaker=Mal Keshar
            message= _ "Why are these bats chasing me?"
        [/message]
    [/event]

    [event]
        name=attack
        [filter]
            type=Giant Rat
        [/filter]
        [filter_second]
            side=1
        [/filter_second]
        [filter_condition]
            [variable]
                name=scenario_stage
                numerical_equals=2
            [/variable]
        [/filter_condition]

        [message]
            speaker=Mal Keshar
            message= _ "Blasted rats!"
        [/message]
    [/event]

    [event]
        name=sighted
        [filter]
            type=Giant Spider
        [/filter]
        [filter_second]
            side=1
        [/filter_second]
        [filter_condition]
            [variable]
                name=scenario_stage
                numerical_equals=2
            [/variable]
        [/filter_condition]

        [message]
            speaker=Mal Keshar
            message= _ "Dela used to be afraid of spiders. Imagine if she saw something like this..."
        [/message]
    [/event]

    [event]
        name=die
        [filter]
            type=Giant Spider
        [/filter]

        # a little bit of breaking the fourth wall
        [message]
            speaker=Mal Keshar
            message= _ "For a moment, I was a little worried there. To have come so far and conquered death... only to be slain by an oversized arachnid! What a tale that would be."
        [/message]
    [/event]

    [event]
        name=sighted
        [filter]
            type=Dread Bat
        [/filter]
        [filter_second]
            side=1
        [/filter_second]
        [filter_condition]
            [variable]
                name=scenario_stage
                numerical_equals=2
            [/variable]
        [/filter_condition]

        [modify_unit]
            [filter]
                type=Dread Bat
            [/filter]
            [effect]
                apply_to=status
                remove=guardian
            [/effect]
        [/modify_unit]

        [message]
            speaker=Mal Keshar
            message= _ "What an unusual bat."
        [/message]
    [/event]

    [event]
        name=attacker_hits
        id=lich_attacker
        first_time_only=yes
        [filter]
            id=Mal Keshar
        [/filter]
        [filter_attack]
            name=touch
        [/filter_attack]
        [filter_condition]
            [variable]
                name=scenario_stage
                numerical_equals=2
            [/variable]
        [/filter_condition]

        [message]
            speaker=Mal Keshar
            message= _ "Yes! I can draw energy from even these insignificant creatures."
        [/message]
        [remove_event]
            id=lich_defender
        [/remove_event]
    [/event]

    [event]
        name=defender_hits
        id=lich_defender
        first_time_only=yes
        [filter_second]
            id=Mal Keshar
        [/filter_second]
        [filter_second_attack]
            name=touch
        [/filter_second_attack]
        [filter_condition]
            [variable]
                name=scenario_stage
                numerical_equals=2
            [/variable]
        [/filter_condition]

        [message]
            speaker=Mal Keshar
            message= _ "Yes! I can draw energy from even these insignificant creatures."
        [/message]
        [remove_event]
            id=lich_attacker
        [/remove_event]
    [/event]

    # treasure boxes
    [event]
        name=moveto
        [filter]
            id=Mal Keshar
            x,y=4,19
        [/filter]
        [filter_condition]
            [variable]
                name=scenario_stage
                numerical_equals=2
            [/variable]
        [/filter_condition]

        [message]
            speaker=narrator
            message= _ "You found some gold!"
            image=wesnoth-icon.png
        [/message]

        [sound]
            name=gold.ogg
        [/sound]

        {PLACE_IMAGE items/chest-plain-open.png 4 19}

        [gold]
            side=1
            {QUANTITY amount 25 20 15}
        [/gold]
    [/event]

    [event]
        name=moveto
        [filter]
            id=Mal Keshar
            x,y=28,23
        [/filter]
        [filter_condition]
            [variable]
                name=scenario_stage
                numerical_equals=2
            [/variable]
        [/filter_condition]

        [message]
            speaker=narrator
            message= _ "You found some gold!"
            image=wesnoth-icon.png
        [/message]

        [sound]
            name=gold.ogg
        [/sound]

        {PLACE_IMAGE items/chest-plain-open.png 28 23}

        [gold]
            side=1
            {QUANTITY amount 25 20 15}
        [/gold]
    [/event]

    [event]
        name=moveto
        [filter]
            id=Mal Keshar
            x,y=27,6
        [/filter]
        [filter_condition]
            [variable]
                name=scenario_stage
                numerical_equals=2
            [/variable]
        [/filter_condition]

        [message]
            speaker=narrator
            message= _ "You found some gold!"
            image=wesnoth-icon.png
        [/message]

        [sound]
            name=gold.ogg
        [/sound]

        {PLACE_IMAGE items/chest-plain-open.png 27 6}

        [gold]
            side=1
            {QUANTITY amount 50 40 30}
        [/gold]
    [/event]

    [event]
        name=moveto
        [filter]
            id=Mal Keshar
            x,y=4,26
        [/filter]
        [filter_condition]
            [variable]
                name=scenario_stage
                numerical_equals=2
            [/variable]
        [/filter_condition]

        [message]
            speaker=narrator
            message= _ "You found some gold!"
            image=wesnoth-icon.png
        [/message]

        [sound]
            name=gold.ogg
        [/sound]

        {PLACE_IMAGE items/chest-plain-open.png 4 26}

        [gold]
            side=1
            {QUANTITY amount 25 20 15}
        [/gold]
    [/event]

    # potion puzzle
    # Solution: Pick up the brazier and melt the ice, creating water
    # pick up the empty flask and move to the water, then pick up the filled flask
    # go to the altar, which puts the filled flask there and creates a potion

    [event]
        name=moveto
        first_time_only=no
        [filter]
            id=Mal Keshar
            x,y=21,18
        [/filter]
        [filter_condition]
            [variable]
                name=scenario_stage
                numerical_equals=2
            [/variable]
            [and]
                [variable]
                    name=puzzle_potion
                    numerical_equals=0
                [/variable]
            [/and]
        [/filter_condition]

        [message]
            speaker=narrator
            message= _ "You picked up a brazier!"
            image=wesnoth-icon.png
        [/message]

        [unit_overlay]
            id=Mal Keshar
            image="misc/fire-icon.png"
            object_id="fire_icon"
        [/unit_overlay]

        [terrain]
            x,y=21,18
            terrain=Urb
        [/terrain]
        [redraw][/redraw]
        {VARIABLE puzzle_potion 1}
    [/event]

    [event]
        name=moveto
        first_time_only=no
        [filter]
            id=Mal Keshar
            x,y=15,22
        [/filter]
        [filter_condition]
            [variable]
                name=scenario_stage
                numerical_equals=2
            [/variable]
        [/filter_condition]
        [if]
            [variable]
                name=puzzle_potion
                numerical_equals=2
            [/variable]
            [then]
                [message]
                    speaker=narrator
                    message= _ "You picked up an empty flask."
                    image=wesnoth-icon.png
                [/message]

                [unit_overlay]
                    id=Mal Keshar
                    image="misc/potion-icon.png"
                    object_id="potion_icon"
                [/unit_overlay]

                {REMOVE_IMAGE 15 22}
                {VARIABLE puzzle_potion 3}
            [/then]

            [elseif]
                [variable]
                    name=puzzle_potion
                    less_than=2
                [/variable]
                [then]
                    [sound]
                        name=magic-dark-big-miss.ogg
                    [/sound]

                    [message]
                        speaker=narrator
                        message= _ "You should do something else first."
                        image=wesnoth-icon.png
                    [/message]
                [/then]
            [/elseif]
        [/if]
    [/event]

    [event]
        name=moveto
        first_time_only=no
        [filter]
            id=Mal Keshar
            x,y=15,18
        [/filter]
        [filter_condition]
            [variable]
                name=scenario_stage
                numerical_equals=2
            [/variable]
        [/filter_condition]
        [if]
            [variable]
                name=puzzle_potion
                numerical_equals=1
            [/variable]
            [then]
                [sound]
                    name=flame-big.ogg
                [/sound]

                [message]
                    speaker=narrator
                    message= _ "You melted the ice."
                    image=wesnoth-icon.png
                [/message]

                [remove_object]
                    id=Mal Keshar
                    object_id="fire_icon"
                [/remove_object]

                [terrain]
                    x,y=15,18
                    terrain=Wo
                [/terrain]
                [redraw][/redraw]
                {VARIABLE puzzle_potion 2}
            [/then]

            [elseif]
                [variable]
                    name=puzzle_potion
                    numerical_equals=3
                [/variable]
                [then]
                    [sound]
                        name=water-blast.wav
                    [/sound]

                    [message]
                        speaker=narrator
                        message= _ "You filled the flask."
                        image=wesnoth-icon.png
                    [/message]

                    [remove_object]
                        id=Mal Keshar
                        object_id="potion_icon"
                    [/remove_object]

                    {PLACE_IMAGE items/potion-blue.png 14 18}
                    {VARIABLE puzzle_potion 4}
                [/then]
            [/elseif]
        [/if]
    [/event]

    [event]
        name=moveto
        first_time_only=no
        [filter]
            id=Mal Keshar
            x,y=14,18
        [/filter]
        [filter_condition]
            [variable]
                name=scenario_stage
                numerical_equals=2
            [/variable]
        [/filter_condition]
        [if]
            [variable]
                name=puzzle_potion
                numerical_equals=4
            [/variable]
            [then]
                [message]
                    speaker=narrator
                    message= _ "You picked up the flask of water."
                    image=wesnoth-icon.png
                [/message]

                [unit_overlay]
                    id=Mal Keshar
                    image="misc/potion-blue-icon.png"
                    object_id="potion_blue_icon"
                [/unit_overlay]

                {REMOVE_IMAGE 14 18}
                {VARIABLE puzzle_potion 5}
            [/then]
        [/if]
    [/event]

    [event]
        name=moveto
        first_time_only=no
        [filter]
            id=Mal Keshar
            x,y=6,15
        [/filter]
        [filter_condition]
            [variable]
                name=scenario_stage
                numerical_equals=2
            [/variable]
        [/filter_condition]
        [if]
            [variable]
                name=puzzle_potion
                numerical_equals=5
            [/variable]
            [then]
                [sound]
                    name=magic-dark-big.ogg
                [/sound]

                [message]
                    speaker=narrator
                    message= _ "You created a new potion!"
                    image=wesnoth-icon.png
                [/message]

                [message]
                    speaker=narrator
                    message= _ "Mal Keshar drinks the potion and feels energy flow through his skeletal form. The power of death surges in his vacant body, threatening to overflow, to burst free, but at the last moment, he reins it in. The stream of magic trickles down to drops, diminished, tempered, but manageable."
                [/message]

                [remove_object]
                    id=Mal Keshar
                    object_id="potion_blue_icon"
                [/remove_object]

                # reward: +1 RANGED DMG
                [object]
                    silent=yes
                    duration=forever
                    [filter]
                        id=Mal Keshar
                    [/filter]
                    [effect]
                        apply_to=attack
                        range=ranged
                        increase_damage=1
                    [/effect]
                [/object]

                {REMOVE_IMAGE 16 17}
                {REMOVE_IMAGE 6 15}
                {VARIABLE puzzle_potion 6}
            [/then]

            [elseif]
                [variable]
                    name=puzzle_potion
                    less_than=6
                [/variable]
                [then]
                    [sound]
                        name=magic-dark-big-miss.ogg
                    [/sound]

                    [message]
                        speaker=narrator
                        message= _ "You should do something else first."
                        image=wesnoth-icon.png
                    [/message]
                [/then]
            [/elseif]
        [/if]
    [/event]

    [event]
        name=moveto
        first_time_only=no
        [filter]
            id=Mal Keshar
            x,y=16,17
        [/filter]
        [filter_condition]
            [variable]
                name=scenario_stage
                numerical_equals=2
            [/variable]
            [and]
                [variable]
                    name=puzzle_potion
                    less_than=6
                [/variable]
            [/and]
        [/filter_condition]

        [message]
            speaker=narrator
            message= _ "Dark water beneath crystal ice,
Unmasked by a lick of flame,
Transmuted by the altar of darkness
Into pitch black shadow."
        [/message]
        [fire_event]
            name=potion text
        [/fire_event]
    [/event]

    [event]
        name=potion text
        [message]
            speaker=Mal Keshar
            message= _ "Someone left their experiment unfinished here."
        [/message]
    [/event]

    # coffin puzzle
    # Solution: pick up the key, move to the coffin to open it
    # then, pick up the bones, put them in the coffin
    # finally, pick up the brazier and cremate the bones

    [event]
        name=moveto
        first_time_only=no
        [filter]
            id=Mal Keshar
            x,y=2,1
        [/filter]
        [filter_condition]
            [variable]
                name=scenario_stage
                numerical_equals=2
            [/variable]
            [and]
                [variable]
                    name=puzzle_coffin
                    numerical_equals=0
                [/variable]
            [/and]
        [/filter_condition]

        [message]
            speaker=narrator
            message= _ "You picked up a key!"
            image=wesnoth-icon.png
        [/message]

        [unit_overlay]
            id=Mal Keshar
            image="misc/key-icon.png"
            object_id="key_icon"
        [/unit_overlay]

        {REMOVE_IMAGE 2 1}
        {VARIABLE puzzle_coffin 1}
    [/event]

    [event]
        name=moveto
        first_time_only=no
        [filter]
            id=Mal Keshar
            x,y=1,3
        [/filter]
        [filter_condition]
            [variable]
                name=scenario_stage
                numerical_equals=2
            [/variable]
        [/filter_condition]
        [if]
            [variable]
                name=puzzle_coffin
                numerical_equals=2
            [/variable]
            [then]
                [message]
                    speaker=narrator
                    message= _ "You found a skeleton."
                    image=wesnoth-icon.png
                [/message]

                # TODO: add a bones icon?

                [message]
                    speaker=Mal Keshar
                    message= _ "There are some robe scraps stuck to this skeleton. This must have once been a mage."
                [/message]

                {REMOVE_IMAGE 1 3}
                {VARIABLE puzzle_coffin 3}
            [/then]

            [elseif]
                [variable]
                    name=puzzle_coffin
                    less_than=2
                [/variable]
                [then]
                    [sound]
                        name=magic-dark-big-miss.ogg
                    [/sound]

                    [message]
                        speaker=narrator
                        message= _ "You should do something else first."
                        image=wesnoth-icon.png
                    [/message]
                [/then]
            [/elseif]
        [/if]
    [/event]

    [event]
        name=moveto
        first_time_only=no
        [filter]
            id=Mal Keshar
            x,y=5,3
        [/filter]
        [filter_condition]
            [variable]
                name=scenario_stage
                numerical_equals=2
            [/variable]
        [/filter_condition]
        [if]
            [variable]
                name=puzzle_coffin
                numerical_equals=4
            [/variable]
            [then]
                [message]
                    speaker=narrator
                    message= _ "You picked up a brazier."
                    image=wesnoth-icon.png
                [/message]

                [unit_overlay]
                    id=Mal Keshar
                    image="misc/fire-icon.png"
                    object_id="brazier_icon"
                [/unit_overlay]

                [terrain]
                    x,y=5,3
                    terrain=Irs
                [/terrain]
                [redraw][/redraw]
                {VARIABLE puzzle_coffin 5}
            [/then]

            [elseif]
                [variable]
                    name=puzzle_coffin
                    less_than=4
                [/variable]
                [then]
                    [sound]
                        name=magic-dark-big-miss.ogg
                    [/sound]

                    [message]
                        speaker=narrator
                        message= _ "You should do something else first."
                        image=wesnoth-icon.png
                    [/message]
                [/then]
            [/elseif]
        [/if]
    [/event]

    [event]
        name=moveto
        first_time_only=no
        [filter]
            id=Mal Keshar
            x,y=3,2
        [/filter]
        [filter_condition]
            [variable]
                name=scenario_stage
                numerical_equals=2
            [/variable]
        [/filter_condition]
        [if]
            [variable]
                name=puzzle_coffin
                numerical_equals=1
            [/variable]
            [then]
                [sound]
                    name=open-chest.wav
                [/sound]

                [message]
                    speaker=narrator
                    message= _ "You opened the coffin."
                    image=wesnoth-icon.png
                [/message]

                [remove_object]
                    id=Mal Keshar
                    object_id="key_icon"
                [/remove_object]

                {PLACE_IMAGE items/coffin-open.png 3 2}
                {VARIABLE puzzle_coffin 2}
            [/then]

            [elseif]
                [variable]
                    name=puzzle_coffin
                    numerical_equals=3
                [/variable]
                [then]
                    [sound]
                        name=skeleton-hit-1.ogg
                    [/sound]

                    [message]
                        speaker=narrator
                        message= _ "You placed the skeleton in the coffin."
                        image=wesnoth-icon.png
                    [/message]

                    {VARIABLE puzzle_coffin 4}
                [/then]
            [/elseif]

            [elseif]
                [variable]
                    name=puzzle_coffin
                    numerical_equals=5
                [/variable]
                [then]
                    [sound]
                        name=flame-big.ogg
                    [/sound]

                    [message]
                        speaker=narrator
                        message= _ "You cremated the bones!"
                        image=wesnoth-icon.png
                    [/message]

                    [message]
                        speaker=narrator
                        message= _ "Ash swirls upward, charred remnants of the once living. Charcoal from bone, embers from flesh, he consumes the pallid cinders, siphoning away the energy with a draining touch."
                    [/message]

                    [remove_object]
                        id=Mal Keshar
                        object_id="brazier_icon"
                    [/remove_object]

                    # reward: +2 MELEE DMG
                    [object]
                        silent=yes
                        duration=forever
                        [filter]
                            id=Mal Keshar
                        [/filter]
                        [effect]
                            apply_to=attack
                            range=melee
                            increase_damage=2
                        [/effect]
                    [/object]

                    {REMOVE_IMAGE 3 2}
                    {REMOVE_IMAGE 2 4}
                    [terrain]
                        x,y=3,2
                        terrain=Irs
                    [/terrain]
                    [redraw][/redraw]
                    {VARIABLE puzzle_coffin 6}
                [/then]
            [/elseif]
        [/if]
    [/event]

    [event]
        name=moveto
        first_time_only=no
        [filter]
            id=Mal Keshar
            x,y=2,4
        [/filter]
        [filter_condition]
            [variable]
                name=scenario_stage
                numerical_equals=2
            [/variable]
            [and]
                [variable]
                    name=puzzle_coffin
                    less_than=6
                [/variable]
            [/and]
        [/filter_condition]

        [message]
            speaker=narrator
            message= _ "Bones, scattered carelessly onto the floor,
Remains laid without rest,
Reside outside their coffin, locked away,
In eternal wakefulness.
Then, the key is found and the lid opened,
And the tongue of fire begets ashen repose."
        [/message]
    [/event]

    # reflection pool scene
    [event]
        name=moveto
        [filter]
            id=Mal Keshar
            [filter_location]
                terrain=Wog,Wog^*
            [/filter_location]
        [/filter]
        [filter_condition]
            [variable]
                name=scenario_stage
                numerical_equals=2
            [/variable]
        [/filter_condition]

        [delay]
            time=1000
        [/delay]

        [sound]
            name=wail-long.wav
        [/sound]

        [delay]
            time=1500
        [/delay]

        {FADE_TO_BLACK}

        [remove_shroud]
            side=1
            x,y=29,32
            radius=2
        [/remove_shroud]
        [lift_fog]
            [filter_side]
                side=1
            [/filter_side]
            x,y=29,32
            radius=2
        [/lift_fog]

        [store_unit]
            [filter]
                id=Mal Keshar
            [/filter]
            variable=malkeshar
            kill=yes
        [/store_unit]

        [place_shroud]
            side=1
            x,y=17,25
            radius=8
        [/place_shroud]

        [delay]
            time=1500
        [/delay]

        [scroll_to]
            x,y=29,32
        [/scroll_to]

        [unstore_unit]
            x,y=29,32
            variable=malkeshar
        [/unstore_unit]

        {MODIFY_UNIT (id=Mal Keshar) facing nw}

        {CLEAR_VARIABLE malkeshar}

        [lock_view][/lock_view]

        {FADE_IN}

        [message]
            speaker=Mal Keshar
            message= _ "This place seems familiar."
        [/message]

        [delay]
            time=1500
        [/delay]

        [animate_unit]
            [filter]
                id=Mal Keshar
            [/filter]
            [primary_attack]
                range=ranged
            [/primary_attack]
            flag=attack
        [/animate_unit]

        [unit]
            type=Lich
            id=Fake Mal Keshar
            name= _ "Mal Keshar"
            profile=
            x,y=28,31
            side=4
            facing=se
            animate=yes
            generate_name=no
            random_traits=no
        [/unit]

        [message]
            speaker=Mal Keshar
            message= _ "The blood is drained and the flesh is stripped. The bone remains, waiting to be reanimated. A necromancer’s most basic weapon is that, the puppet body of a skeleton."
        [/message]

        [message]
            speaker=Mal Keshar
            message= _ "... but that is not what I am looking at, is it? This is... my face. This is what I have become..."
        [/message]

        [message]
            speaker=Fake Mal Keshar
            message= _ "A skeleton, yourself."
        [/message]

        [message]
            speaker=Mal Keshar
            message= _ "A skeleton — a puppet for Darken Volk’s manipulation, yes."
        [/message]

        [message]
            speaker=Fake Mal Keshar
            message= _ "A puppet of your own machinations."
        [/message]

        [message]
            speaker=Mal Keshar
            message= _ "I..."
        [/message]

        [message]
            speaker=Fake Mal Keshar
            message= _ "From the outset, deep down, you knew. If you followed this path, they would never accept you. Yet, you manipulated yourself into believing the opposite. You could not face the truth."
        [/message]

        [message]
            speaker=Mal Keshar
            message= _ "But I was right. My logic was sound. My reasoning was flawless. They should have accepted me..."
        [/message]

        [message]
            speaker=Fake Mal Keshar
            message= _ "A puppet shackled by the limitations of your own feelings. No matter its faultlessness, logic could not have displaced the reality of your world. A culture of preconception supplanting even the instinct for survival."
        [/message]

        [message]
            speaker=Mal Keshar
            message= _ "Enough of this."
        [/message]

        [animate_unit]
            [filter]
                id=Mal Keshar
            [/filter]
            [primary_attack]
                range=ranged
            [/primary_attack]
            flag=attack
        [/animate_unit]

        [kill]
            id=Fake Mal Keshar
            animate=no
        [/kill]

        [terrain]
            x,y=28,31
            terrain=Wog
        [/terrain]
        [redraw][/redraw]

        [delay]
            time=1500
        [/delay]

        {MODIFY_UNIT (id=Mal Keshar) facing se}

        [delay]
            time=1000
        [/delay]

        [terrain]
            x,y=30,32
            terrain=Urb
        [/terrain]
        [redraw][/redraw]

        [unit]
            type=Lich
            id=Fake Mal Keshar
            name= _ "Mal Keshar"
            profile=
            x,y=30,32
            side=4
            facing=nw
            animate=yes
            generate_name=no
            random_traits=no
        [/unit]

        [message]
            speaker=Fake Mal Keshar
            message= _ "This is what you have become. A decaying soul chained within the puppet form of a skeleton."
        [/message]

        [message]
            speaker=Mal Keshar
            message= _ "A rotten soul? I was never concerned with such things to begin with. When you are slain by orcs and decompose into nothing but a maggot-eaten carcass, no ‘soul’ is going to be of any use."
        [/message]

        [message]
            speaker=Fake Mal Keshar
            message= _ "Better to sacrifice the spirit and live, than to die with dignity?"
        [/message]

        [message]
            speaker=Mal Keshar
            message= _ "Dignity in death is a notion contrived of by fools. It is an assertion of lies, told by humans to comfort themselves in the face of an immutable force. But not I. By sacrificing my soul, I have conquered death and will live on past all those who tried to destroy me."
        [/message]

        [message]
            speaker=Fake Mal Keshar
            message= _ "For what purpose? You will persist on, bitter and alone, until the end of time?"
        [/message]

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

        [delay]
            time=1000
        [/delay]

        [animate_unit]
            [filter]
                id=Mal Keshar
            [/filter]
            [primary_attack]
                range=ranged
            [/primary_attack]
            flag=attack
        [/animate_unit]

        [kill]
            id=Fake Mal Keshar
            animate=no
        [/kill]

        [terrain]
            x,y=30,32
            terrain=Wog
        [/terrain]
        [redraw][/redraw]

        [delay]
            time=1500
        [/delay]

        {FADE_TO_BLACK}

        [terrain]
            [and]
                terrain=Wog,Wog^*
            [/and]
            terrain=Qxu
        [/terrain]
        [terrain]
            x=21,22
            y=28,28
            terrain=Qxu^Bcx\
        [/terrain]
        [redraw][/redraw]

        [store_unit]
            [filter]
                id=Mal Keshar
            [/filter]
            variable=malkeshar
            kill=yes
        [/store_unit]

        [place_shroud]
            side=1
            x,y=29,32
            radius=3
        [/place_shroud]

        [delay]
            time=1500
        [/delay]

        [remove_shroud]
            side=1
            x,y=22,28
            radius=2
        [/remove_shroud]

        [scroll_to]
            x,y=22,28
        [/scroll_to]

        {FADE_IN}

        [unstore_unit]
            x,y=22,28
            find_vacant=yes
            variable=malkeshar
        [/unstore_unit]

        {CLEAR_VARIABLE malkeshar}

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

        [delay]
            time=1500
        [/delay]

        [message]
            speaker=Mal Keshar
            message= _ "I regret nothing."
        [/message]

        [unlock_view][/unlock_view]
    [/event]

    # Sighting the enemy lair
    [event]
        name=moveto
        [filter]
            side=1
        [/filter]
        [filter_condition]
            [variable]
                name=scenario_stage
                numerical_equals=2
            [/variable]
            [and]
                [have_location]
                    terrain=Ql
                    [filter_vision]
                        side=1
                    [/filter_vision]
                [/have_location]
            [/and]
        [/filter_condition]

        [remove_shroud]
            side=1
            x,y=17,7
            radius=3
        [/remove_shroud]

        [redraw]
            side=1
        [/redraw]

        [scroll_to]
            x,y=17,7
        [/scroll_to]

        [delay]
            time=1500
        [/delay]

        [message]
            speaker=Mal Keshar
            message= _ "I sense the lair of something powerful nearby. Capturing that castle may be a worthwhile endeavor, but I shall have to approach carefully."
        [/message]

        [cancel_action][/cancel_action]
    [/event]

    # Initiate boss battle
    [event]
        name=enter hex
        [filter]
            side=1
            # just to make it not trigger with debug mode vision too early:
            x=7-27
            y=1-18
        [/filter]
        [filter_condition]
            [variable]
                name=scenario_stage
                numerical_equals=2
            [/variable]
            [and]
                [have_location]
                    x,y=17,7
                    radius=2
                    [filter_vision]
                        side=1
                    [/filter_vision]
                [/have_location]
            [/and]
        [/filter_condition]

        [remove_shroud]
            side=1
            x,y=17,7
            radius=6
        [/remove_shroud]
        [lift_fog]
            [filter_side]
                side=1
            [/filter_side]
            x,y=17,7
            radius=6
            multiturn=yes
        [/lift_fog]

        {MODIFY_UNIT (id=Mal Keshar) moves 6}
        {MODIFY_UNIT (id=Mal Keshar) attacks_left 1}

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

        [remove_object]
            id=Mal Keshar
            object_id="fire_icon"
        [/remove_object]

        [remove_object]
            id=Mal Keshar
            object_id="potion_icon"
        [/remove_object]

        [remove_object]
            id=Mal Keshar
            object_id="potion_blue_icon"
        [/remove_object]

        [remove_object]
            id=Mal Keshar
            object_id="brazier_icon"
        [/remove_object]

        [remove_object]
            id=Mal Keshar
            object_id="key_icon"
        [/remove_object]

        {REPLACE_SCENARIO_MUSIC silence.ogg}

        [scroll_to]
            x,y=17,7
        [/scroll_to]

        [delay]
            time=2000
        [/delay]

        {QUAKE cave-in.ogg}

        [delay]
            time=500
        [/delay]

        [sound]
            name=flame-big.ogg
        [/sound]

        [delay]
            time=500
        [/delay]

        {QUAKE cave-in.ogg}

        [delay]
            time=500
        [/delay]

        [sound]
            name=flame-big.ogg
        [/sound]

        [delay]
            time=250
        [/delay]

        [sound]
            name=flame-big.ogg
        [/sound]

        [scroll_to]
            x,y=17,7
        [/scroll_to]

        [unit]
            id=Xanthric
            name= _ "Xanthric"
            type=Fire Dragon
            side=2
            x,y=17,7
#ifdef EASY
            hitpoints=150
            max_hitpoints=150
#endif
#ifdef NORMAL
            hitpoints=190
            max_hitpoints=190
#endif
#ifdef HARD
            hitpoints=230
            max_hitpoints=230
#endif
            unrenamable=yes
            canrecruit=yes
            [modifications]
#ifdef EASY
                [object]
                    [effect]
                        apply_to=movement
                        increase=-1
                    [/effect]
                    [effect]
                        apply_to=attack
                        range=melee
                        increase_damage=-3
                    [/effect]
                    [effect]
                        apply_to=attack
                        range=ranged
                        increase_damage=-2
                    [/effect]
                [/object]
#endif
#ifdef NORMAL
                [object]
                    [effect]
                        apply_to=movement
                        increase=-1
                    [/effect]
                    [effect]
                        apply_to=attack
                        range=melee
                        increase_damage=-2
                    [/effect]
                    [effect]
                        apply_to=attack
                        range=ranged
                        increase_damage=-1
                    [/effect]
                [/object]
#endif
            [/modifications]
        [/unit]

        [capture_village]
            [not]
                owner_side=1
            [/not]
            side=2
        [/capture_village]

        {REPLACE_SCENARIO_MUSIC frantic.ogg}
        {APPEND_MUSIC           vengeful.ogg}

        [message]
            speaker=Xanthric
            message= _ "<big>What manner of vile creature intrudes upon my domain?!</big>"
        [/message]

        [message]
            speaker=Mal Keshar
            message= _ "I suppose there is some irony in this."
        [/message]

        [message]
            speaker=Mal Keshar
            message= _ "I was not aware that dragons still existed. This was most unexpected. Nevertheless, this shall be a worthwhile test of my new powers."
        [/message]

        [message]
            speaker=Xanthric
            message= _ "<big>A derelict corpse like you has no business challenging a dragon! Even my lowly elementals will be more than a match for you. Rise, my thralls!</big>"
        [/message]

        [random_placement]
            num_items={ON_DIFFICULTY 2 3 5}
            variable=loc
            min_distance=1
            [filter_location]
                terrain=Ql
                [not]
                    [filter][/filter]
                [/not]
            [/filter_location]
            [command]
                [unit]
                    side=2
                    type=Fire Guardian
                    x,y=$loc.x,$loc.y
                    animate=yes
                [/unit]
            [/command]
        [/random_placement]

        [message]
            speaker=Mal Keshar
            message= _ "Fire magic. Annoying, but you are not the only one who can summon minions to your aid."
        [/message]

        {CREATE_ADVISOR}

        [if]
            [have_unit]
                side=1
                type=Spectre,Nightgaunt,Wraith,Shadow
                search_recall_list=yes
                x,y=recall,recall
            [/have_unit]
            [then]
                [recall]
                    type=Spectre,Nightgaunt,Wraith,Shadow
                [/recall]
            [/then]
            [else]
                [unit]
                    type=Wraith
                    side=1
                    animate=yes
                    placement=leader
                [/unit]
            [/else]
        [/if]
        [if]
            [have_unit]
                side=1
                type=Spectre,Nightgaunt,Wraith,Shadow,Ghost
                search_recall_list=yes
                x,y=recall,recall
            [/have_unit]
            [then]
                [recall]
                    type=Spectre,Nightgaunt,Wraith,Shadow,Ghost
                [/recall]
            [/then]
            [else]
                [unit]
                    type=Ghost
                    side=1
                    animate=yes
                    placement=leader
                [/unit]
            [/else]
        [/if]
#ifdef NORMAL
        [if]
            [have_unit]
                side=1
                type=Spectre,Nightgaunt,Wraith,Shadow,Ghost
                search_recall_list=yes
                x,y=recall,recall
            [/have_unit]
            [then]
                [recall]
                    type=Spectre,Nightgaunt,Wraith,Shadow,Ghost
                [/recall]
            [/then]
            [else]
                [unit]
                    type=Ghost
                    side=1
                    animate=yes
                    placement=leader
                [/unit]
            [/else]
        [/if]
#endif
#ifdef EASY
        [if]
            [have_unit]
                side=1
                type=Spectre,Nightgaunt,Wraith,Shadow,Ghost
                search_recall_list=yes
                x,y=recall,recall
            [/have_unit]
            [then]
                [recall]
                    type=Spectre,Nightgaunt,Wraith,Shadow,Ghost
                [/recall]
            [/then]
            [else]
                [unit]
                    type=Ghost
                    side=1
                    animate=yes
                    placement=leader
                [/unit]
            [/else]
        [/if]
        [if]
            [have_unit]
                side=1
                type=Spectre,Nightgaunt,Wraith,Shadow,Ghost
                search_recall_list=yes
                x,y=recall,recall
            [/have_unit]
            [then]
                [recall]
                    type=Spectre,Nightgaunt,Wraith,Shadow,Ghost
                [/recall]
            [/then]
            [else]
                [unit]
                    type=Ghost
                    side=1
                    animate=yes
                    placement=leader
                [/unit]
            [/else]
        [/if]
#endif

        [message]
            speaker=Xanthric
            message= _ "<big>A few decrepit souls will do you no good, necromancer. Your bones shall soon join my garden of carcasses!</big>"
        [/message]

        [reset_fog]
            [filter_side]
                side=1
            [/filter_side]
        [/reset_fog]

        {VARIABLE scenario_stage 3}
        {VARIABLE runes_activated 0}

        {PLACE_IMAGE scenery/summoning-circle1.png 18 3}
        {PLACE_IMAGE scenery/summoning-circle5.png 23 7}
        {PLACE_IMAGE scenery/summoning-circle3.png 10 6}
        {PLACE_IMAGE scenery/summoning-circle6.png 13 3}

        [objectives]
            side=1
            [objective]
                condition=win
                description= _ "Defeat Xanthric"
            [/objective]
            [objective]
                condition=lose
                description= _ "Destruction of Mal Keshar"
            [/objective]
            [gold_carryover]
                bonus=no
                carryover_percentage=100
            [/gold_carryover]
            [note]
                description= _ "At most one rune may be activated each turn"
            [/note]
        [/objectives]

        [cancel_action][/cancel_action]
    [/event]

    # flavor text if you capture the dragon's keep
    [event]
        name=moveto
        [filter]
            x,y=17,7
            side=1
        [/filter]
        [filter_condition]
            [variable]
                name=scenario_stage
                numerical_equals=3
            [/variable]
        [/filter_condition]

        [message]
            speaker=Xanthric
            message= _ "<big>How dare you?! That stone is my throne! I am the king of this domain!</big>"
        [/message]

        [message]
            speaker=Mal Keshar
            message= _ "Peasant, warrior, king, and emperor, all living things are the subjects of death. Eternity will claim your soul as its own if I do not."
        [/message]
    [/event]

    # spawns
    [event]
        name=new turn
        first_time_only=no
        [filter_condition]
            [variable]
                name=scenario_stage
                numerical_equals=3
            [/variable]
        [/filter_condition]

        [if]
            [have_unit]
                side=2
                type=Fire Guardian
                count="0-5"
            [/have_unit]
            [then]
                [random_placement]
                    num_items={ON_DIFFICULTY 1 2 2}
                    variable=loc
                    min_distance=1
                    [filter_location]
                        terrain=Ql
                        [not]
                            [filter][/filter]
                        [/not]
                    [/filter_location]
                    [command]
                        {NOTRAIT_UNIT 2 "Fire Guardian" $loc.x $loc.y}
                    [/command]
                [/random_placement]
            [/then]
        [/if]

        {VARIABLE runes_activated 0}
    [/event]

    [event]
        name=moveto
        first_time_only=no
        [filter]
            id=Mal Keshar
        [/filter]
        [filter_condition]
            [variable]
                name=scenario_stage
                numerical_equals=3
            [/variable]
            [and]
                [variable]
                    name=runes_activated
                    numerical_equals=0
                [/variable]
            [/and]
        [/filter_condition]

        [if]
            [have_unit]
                id=Mal Keshar
                x,y=10,6
            [/have_unit]
            [then]
                [sound]
                    name=skeleton-big-hit-1.wav
                [/sound]

                [fire_event]
                    name=raise undead
                [/fire_event]

                [animate_unit]
                    [filter]
                        id=Mal Keshar
                    [/filter]
                    [primary_attack]
                        range=ranged
                    [/primary_attack]
                    flag=attack
                [/animate_unit]

                [unit]
                    side=1
                    type=Ghoul
                    x,y=10,7
                    animate=yes
                    passable=yes
                    overwrite=no
                [/unit]
                [unit]
                    side=1
#ifdef HARD
                    type=Walking Corpse
#else
                    type=Soulless
#endif
                    x,y=11,7
                    animate=yes
                    passable=yes
                    overwrite=no
                [/unit]
                [unit]
                    side=1
#ifdef EASY
                    type=Soulless
#else
                    type=Walking Corpse
#endif
                    x,y=10,5
                    animate=yes
                    passable=yes
                    overwrite=no
                [/unit]

                {VARIABLE runes_activated 1}
            [/then]

            [elseif]
                [have_unit]
                    id=Mal Keshar
                    x,y=18,3
                [/have_unit]
                [then]
                    [sound]
                        name=skeleton-big-hit-1.ogg
                    [/sound]

                    [fire_event]
                        name=raise undead
                    [/fire_event]

                    [animate_unit]
                        [filter]
                            id=Mal Keshar
                        [/filter]
                        [primary_attack]
                            range=ranged
                        [/primary_attack]
                        flag=attack
                    [/animate_unit]

                    [unit]
                        side=1
                        type=Skeleton
                        x,y=19,4
                        animate=yes
                        passable=yes
                        overwrite=no
                    [/unit]
                    [unit]
                        side=1
#ifdef HARD
                        type=Walking Corpse
#else
                        type=Soulless
#endif
                        x,y=19,3
                        animate=yes
                        passable=yes
                        overwrite=no
                    [/unit]
                    [unit]
                        side=1
#ifdef EASY
                        type=Soulless
#else
                        type=Walking Corpse
#endif
                        x,y=17,4
                        animate=yes
                        passable=yes
                        overwrite=no
                    [/unit]

                    {VARIABLE runes_activated 1}
                [/then]
            [/elseif]

            [elseif]
                [have_unit]
                    id=Mal Keshar
                    x,y=23,7
                [/have_unit]
                [then]
                    [sound]
                        name=skeleton-big-hit-1.ogg
                    [/sound]

                    [fire_event]
                        name=raise undead
                    [/fire_event]

                    [animate_unit]
                        [filter]
                            id=Mal Keshar
                        [/filter]
                        [primary_attack]
                            range=ranged
                        [/primary_attack]
                        flag=attack
                    [/animate_unit]

                    [unit]
                        side=1
                        type=Ghost
                        x,y=23,8
                        animate=yes
                        passable=yes
                        overwrite=no
                    [/unit]
                    [unit]
                        side=1
#ifdef HARD
                        type=Walking Corpse
#else
                        type=Soulless
#endif
                        x,y=22,7
                        animate=yes
                        passable=yes
                        overwrite=no
                    [/unit]
                    [unit]
                        side=1
#ifdef EASY
                        type=Soulless
#else
                        type=Walking Corpse
#endif
                        x,y=22,6
                        animate=yes
                        passable=yes
                        overwrite=no
                    [/unit]

                    {VARIABLE runes_activated 1}
                [/then]
            [/elseif]

            [elseif]
                [have_unit]
                    id=Mal Keshar
                    x,y=13,3
                [/have_unit]
                [then]
                    [sound]
                        name=skeleton-big-hit-1.ogg
                    [/sound]

                    [fire_event]
                        name=raise undead
                    [/fire_event]

                    [animate_unit]
                        [filter]
                            id=Mal Keshar
                        [/filter]
                        [primary_attack]
                            range=ranged
                        [/primary_attack]
                        flag=attack
                    [/animate_unit]

                    [unit]
                        side=1
                        type=Skeleton Archer
                        x,y=14,2
                        animate=yes
                        passable=yes
                        overwrite=no
                    [/unit]
                    [unit]
                        side=1
#ifdef HARD
                        type=Walking Corpse
#else
                        type=Soulless
#endif
                        x,y=14,3
                        animate=yes
                        passable=yes
                        overwrite=no
                    [/unit]
                    [unit]
                        side=1
#ifdef EASY
                        type=Soulless
#else
                        type=Walking Corpse
#endif
                        x,y=12,3
                        animate=yes
                        passable=yes
                        overwrite=no
                    [/unit]

                    {VARIABLE runes_activated 1}
                [/then]
            [/elseif]
        [/if]
    [/event]

    [event]
        name=raise undead

        [message]
            speaker=Mal Keshar
            message= _ "There are many corpses around here. I can put them to use."
        [/message]
    [/event]

    # Victory
    [event]
        name=last breath
        [filter]
            id=Xanthric
        [/filter]

        [delay]
            time=500
        [/delay]

        [kill]
            side=2
            [not]
                id=Xanthric
            [/not]
        [/kill]

        [delay]
            time=500
        [/delay]

        [message]
            speaker=Xanthric
            message= _ "How could this happen..."
        [/message]

        [message]
            speaker=Mal Keshar
            message= _ "You were not as powerful as you thought."
        [/message]

        [animate_unit]
            [filter]
                id=Mal Keshar
            [/filter]
            [primary_attack]
                range=ranged
            [/primary_attack]
            flag=attack
        [/animate_unit]

        [message]
            speaker=Xanthric
            message= _ "It’s... so... cold..."
        [/message]

        [kill]
            id=Xanthric
            animate=yes
        [/kill]

        [message]
            speaker=Mal Keshar
            message= _ "So, the beast dies. I will have to make a few modifications to this lair later. First, I shall have to test the extent to which I can push these new powers."
        [/message]

        [animate_unit]
            [filter]
                id=Mal Keshar
            [/filter]
            [primary_attack]
                range=ranged
            [/primary_attack]
            flag=attack
        [/animate_unit]

        [delay]
            time=1000
        [/delay]

        [sound]
            name=magic-dark.ogg
        [/sound]

        [delay]
            time=250
        [/delay]

        {QUAKE cave-in.ogg}

        [delay]
            time=1000
        [/delay]

        [sound]
            name=magic-dark.ogg
        [/sound]

        [delay]
            time=250
        [/delay]

        {QUAKE cave-in.ogg}

        [delay]
            time=1000
        [/delay]

        [sound]
            name=magic-dark.ogg
        [/sound]

        [delay]
            time=250
        [/delay]

        [sound]
            name=magic-dark.ogg
        [/sound]

        [delay]
            time=250
        [/delay]

        [sound]
            name=magic-dark-big.ogg
        [/sound]

        [delay]
            time=250
        [/delay]

        [unit]
            id=Xanthric
            name= _ "Xanthric"
            type=Skeletal Dragon
            side=1
            x,y=$x1,$y1
#ifdef EASY
            max_experience=80
#endif
#ifdef NORMAL
            max_experience=90
#endif
#ifdef HARD
            max_experience=100
#endif
            max_hitpoints=73
            unrenamable=yes
            [modifications]
                {TRAIT_LOYAL}
                [object]
                    take_only_once=yes
                    silent=yes
                    duration=forever
                    [filter]
                        id=Xanthric
                    [/filter]
                    [effect]
                        apply_to=new_ability
                        [abilities]
                            {ABILITY_FEEDING}
                        [/abilities]
                    [/effect]
                [/object]
            [/modifications]
        [/unit]

        [object]
            take_only_once=yes
            silent=yes
            duration=forever
            [filter]
                id=Xanthric
            [/filter]
            [effect]
                apply_to=attack
                name=jaw
                defense_weight=0.8
            [/effect]
            [effect]
                apply_to=attack
                name=claws
                defense_weight=0.2
            [/effect]
            [effect]
                apply_to=new_advancement
                replace=no

                {AMLA_OPTION_DRAGON_MELEE}
                {AMLA_OPTION_DRAGON_CARAPACE}
                {AMLA_OPTION_DRAGON_FLIGHT}
                {AMLA_OPTION_DRAGON_SOUL_EATER1}
                {AMLA_OPTION_DRAGON_SOUL_EATER2}
                {AMLA_OPTION_DRAGON_SOUL_EATER3}
            [/effect]
        [/object]

        [delay]
            time=500
        [/delay]

        [message]
            speaker=Mal Keshar
            message= _ "Excellent! My will is even strong enough to conquer a dragon. None can stand against me any longer! To those who cursed me prior, I spit at thee! For I am Mal Keshar the Damned, he who is spurned by all who live and draw breath! I laugh, for now, you are all naught but remains and dust, and I live on, alone..."
        [/message]

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

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

        [message]
            speaker=unit
            message= _ "So this is how it ends..."
        [/message]

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

    [event]
        name=victory

        {CLEAR_VARIABLE scenario_stage}
        {CLEAR_VARIABLE puzzle_coffin}
        {CLEAR_VARIABLE puzzle_potion}
        {CLEAR_VARIABLE runes_activated}

        {VARIABLE timesForever 0}
        {VARIABLE metInky 0}
        {VARIABLE reflectionPool 0}
    [/event]

    {HERODEATH_MALIN_LICH}
[/scenario]