#textdomain wesnoth-did

[scenario]
    id=03_A_Haunting_in_Winter
    name= _ "A Haunting in Winter"
    map_file=03_A_Haunting_in_Winter.map
    victory_when_enemies_defeated=no
    turns=unlimited
    next_scenario=04_Spring_of_Reprisal

    [time_area]
        x=15-16
        y=20-21
        {DEFAULT_SCHEDULE}
    [/time_area]
    {UNDERGROUND}

    {SCENARIO_MUSIC       the_deep_path.ogg}
    {EXTRA_SCENARIO_MUSIC underground.ogg}
    {EXTRA_SCENARIO_MUSIC nunc_dimittis.ogg}

    [story]
        [part]
            music=transience.ogg
            story=_ "Malin and Darken Volk spend the summer and fall traveling across the lower Northlands. They attack goblin villages and small orcish camps when they find them. Although Darken Volk is occasionally demanding and irritable, he proves to be an effective mentor. Malin’s power grows considerably under the necromancer’s tutelage."
            {STORYTXT_BACKGROUND travel.webp}
        [/part]
        [part]
            story=_ "The colored patchwork of fall eventually fades to wintry snowfall drifting over barren lands. The nights grow longer as the sun disappears behind a seemingly perpetual mist of pallid clouds. Hounded by the elements, the two necromancers retreat into the hills to seek shelter. By a stroke of good fortune, they stumble across a mostly abandoned cave just as the winter sets in."
            {STORYTXT_BACKGROUND travel.webp}
        [/part]
    [/story]

    {DID_TRACK {JOURNEY_03_NEW}}

    # wmllint: validate-off
    [side]
        side=1
        controller=human
        recruit=Walking Corpse,Vampire Bat,Ghoul
        income=-2
        village_gold=0
        {GOLD 60 50 40}
        village_support=2
        team_name=good
        user_team_name= _ "Malin Keshar"
        {FLAG_VARIANT undead}
        color=darkblue

        # wmllint: recognize Malin Keshar
        {CHARACTER_STATS_MALIN_KESHAR}

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

    [side]
        side=2
        controller=ai
        no_leader=yes
        hidden=yes
        team_name=monsters
        user_team_name= _ "teamname^Monsters"
        color=orange
        [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]
            [goal]
                name=target
                [criteria]
                    side=3
                [/criteria]
                value=-100
            [/goal]
        [/ai]
    [/side]

    [side]
        side=3
        controller=ai
        no_leader=yes
        hidden=yes
        team_name=good
        user_team_name= _ "Darken Volk"
        color=black
    [/side]

    [side]
        side=4
        controller=ai
        no_leader=yes
        hidden=yes
        team_name=spirits
        user_team_name= _ "teamname^Illusion"
        color=darkblue
        # Using the same flag icon and color as the next side, one can't spot it in the top bar
        flag_icon=flags/undead-flag-icon.png
        [ai]
            aggression=1.0
            caution=0.0
        [/ai]
    [/side]

#define OBJECTIVE_EXPLORE
    [objectives]
        side=1
        [objective]
            condition=win
            description= _ "Explore"
        [/objective]
        [objective]
            condition=lose
            description= _ "Death of Malin Keshar"
        [/objective]
        [gold_carryover]
            carryover_percentage=100
        [/gold_carryover]
    [/objectives]
#enddef

#define FULL_HEAL_RESTORE FILTER
    [heal_unit]
        [filter]
            {FILTER}
        [/filter]

        amount=full
        moves=full
        restore_statuses=yes
        restore_attacks=yes
    [/heal_unit]
#enddef

    {PLACE_IMAGE scenery/summoning-circle3.png 26 5}
    {PLACE_IMAGE items/storm-trident.png 29 10}
    {PLACE_IMAGE items/potion-clear.png 2 2}
    {PLACE_IMAGE scenery/nest-empty.png 9 7}

    {PLACE_IMAGE scenery/castle-ruins.png 5 6}
    {PLACE_IMAGE scenery/castle-ruins2.png 2 3}
    {PLACE_IMAGE scenery/orcish-flag-flat2.png 2 5}
    {PLACE_IMAGE scenery/orcish-flag-flat2.png 4 5}
    {PLACE_IMAGE scenery/orcish-flag-flat2.png 11 3}
    {PLACE_IMAGE items/bonestack.png 5 5}
    {PLACE_IMAGE scenery/whitefang-flag.png 4 6}

    [event]
        name=prestart

        {CLEAR_CORPSE_HORDE}

        [label]
            x,y=5,17
            text= _ "Reflection pool"
        [/label]

        [objectives]
            side=1
            [objective]
                condition=win
                description= _ "Kill both young ogres"
            [/objective]
            [objective]
                condition=lose
                description= _ "Death of Malin Keshar"
            [/objective]
            [gold_carryover]
                carryover_percentage=100
            [/gold_carryover]
        [/objectives]

        [recall]
            id=Darken Volk
        [/recall]

        [unit]
            type=Young Ogre
            x,y=25,17
            side=2
            id=YOgre1
            hitpoints=38
            facing=sw
            ai_special=guardian
            undead_variation=null
            [modifications]
                {TRAIT_STRONG}
            [/modifications]
        [/unit]

        # Non-strong one:
        # wmllint: recognize YOgre2
        [while]
            [not]
                [have_unit]
                    id=YOgre2
                    [not]
                        trait=strong
                    [/not]
                [/have_unit]
            [/not]
            [do]
                [unit]
                    type=Young Ogre
                    x,y=24,17
                    side=2
                    id=YOgre2
                    hitpoints=33
                    facing=ne
                    ai_special=guardian
                    undead_variation=null
                    overwrite=yes
                [/unit]
            [/do]
        [/while]

        {PLACE_IMAGE "items/bow.png" 17 8}
        [unit]
            type=Ogre
            x,y=18,6
            side=2
            id=Ogre3
            ai_special=guardian
            undead_variation=null
        [/unit]

        [unit]
            type=Wolf
            x,y=1,5
            side=2
            ai_special=guardian
            random_traits=no
            [modifications]
                {TRAIT_MAIMED}
            [/modifications]
        [/unit]

        [unit]
            type=Wolf
            x,y=6,8
            side=2
            ai_special=guardian
            random_traits=no
            [modifications]
                {TRAIT_MAIMED}
            [/modifications]
        [/unit]

        [unit]
            type=Wolf
            x,y=5,5
            side=2
            ai_special=guardian
            random_traits=no
            [modifications]
                {TRAIT_MAIMED}
            [/modifications]
        [/unit]

        [unit]
            type=Wolf
            x,y=4,2
            side=2
            ai_special=guardian
            random_traits=no
            [modifications]
                {TRAIT_MAIMED}
            [/modifications]
        [/unit]

#ifdef EASY
        [unit]
            type=Great Wolf
            x,y=3,7
            side=2
            ai_special=guardian
            random_traits=no
            [modifications]
                {TRAIT_MAIMED}
            [/modifications]
        [/unit]
#else
        [unit]
            type=Direwolf
            x,y=3,7
            side=2
            ai_special=guardian
            random_traits=no
            [modifications]
                {TRAIT_MAIMED}
            [/modifications]
        [/unit]
#endif
        [remove_shroud]
            x=13-16,12,12
            y=11-21,13-14,17-19
        [/remove_shroud]

        [unit]
            type=Giant Scorpion
            x,y=8,9
            side=2
            level=2
            id=Big Scorpion
            ai_special=guardian
            max_moves=1
            max_hitpoints=66
        [/unit]

        {MODIFY_UNIT (id=Darken Volk) max_moves 0}
        {MODIFY_UNIT (id=Darken Volk) moves 0}
        {MODIFY_UNIT (id=Darken Volk) side 3}

        {VARIABLE mud_puzzle 0}
        {VARIABLE rat_puzzle 0}
    [/event]

    [event]
        name=start

        [message]
            speaker=Darken Volk
            message= _ "I sense many lifeforms in this cavern, but none are very powerful. Most likely they are merely nothing more than beasts. Still, we may be able to use them to continue your training."
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "Better than nothing. Which way do these creatures lie?"
        [/message]

        [message]
            speaker=Darken Volk
            message= _ "East."
        [/message]

        [move_unit]
            id=Malin Keshar
            to_x,to_y=20,13
        [/move_unit]
        [redraw]
            side=1
            clear_shroud=yes
        [/redraw]
        [scroll_to]
            x,y=24,17
        [/scroll_to]

        [message]
            speaker=YOgre1
            message= _ "Me kill you!"
        [/message]

        [harm_unit]
            [filter]
                id=YOgre2
            [/filter]
            [filter_second]
                id=YOgre1
            [/filter_second]
            [primary_attack]
                name=cleaver
            [/primary_attack]
            [secondary_attack]
                name=cleaver
            [/secondary_attack]
            animate=yes
            amount=6
        [/harm_unit]

        [message]
            speaker=YOgre2
            message= _ "No, me kill you!"
        [/message]

        [harm_unit]
            [filter]
                id=YOgre1
            [/filter]
            [filter_second]
                id=YOgre2
            [/filter_second]
            [primary_attack]
                name=cleaver
            [/primary_attack]
            [secondary_attack]
                name=cleaver
            [/secondary_attack]
            animate=yes
            amount=5
        [/harm_unit]

        [message]
            speaker=YOgre1
            message= _ "No, me kill— I see human!"
        [/message]

        [message]
            speaker=YOgre2
            message= _ "Human! We smash human!"
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "Simple-minded, obtuse creatures."
        [/message]

        [message]
            speaker=Darken Volk
            message= _ "This should be an easy task for you. Once you have slain them both, we shall turn them into skeletons."
        [/message]
    [/event]

    # First area (two young ogres)
    [event]
        name=last breath
        [filter]
            id=YOgre1
        [/filter]

        [message]
            speaker=YOgre1
            # po: this line is just a death scream
            message= _ "Arrghhaaugghhh..." # wmllint: no spellcheck
        [/message]

        {PLACE_IMAGE "items/bones.png" $x1 $y1}

        {VARIABLE loc1.x $x1}
        {VARIABLE loc1.y $y1}
    [/event]
    [event]
        name=last breath
        [filter]
            id=YOgre2
        [/filter]

        [message]
            speaker=YOgre2
            message= _ "Me... die..."
        [/message]

        {PLACE_IMAGE "items/bones.png" $x1 $y1}

        {VARIABLE loc2.x $x1}
        {VARIABLE loc2.y $y1}
    [/event]

    [event]
        name=die
        first_time_only=no

        [filter]
            id=YOgre1,YOgre2
        [/filter]

        [if]
            [not]
                [have_unit]
                    id=YOgre1,YOgre2
                [/have_unit]
            [/not]
            [then]
                # if Malin is the one to kill the last ogre, he doesn't get the exp because he gets moved off the hex
                # so this will give it to him manually
                [if]
                    [have_unit]
                        x,y=$x2,$y2
                        id=Malin Keshar
                    [/have_unit]
                    [then]
                        [store_unit]
                            [filter]
                                id=Malin Keshar
                            [/filter]
                            variable=malin
                        [/store_unit]

                        {VARIABLE_OP malin.experience add 8}
                        [unstore_unit]
                            variable=malin
                        [/unstore_unit]

                        {CLEAR_VARIABLE malin}
                    [/then]
                [/if]

                # this is necessary to stop the units from blocking their hexes
                [kill]
                    id=YOgre1,YOgre2
                [/kill]
                [fire_event]
                    name=cutscene1
                [/fire_event]
            [/then]
        [/if]
    [/event]
    [event]
        name=cutscene1

        {FULL_HEAL_RESTORE (side=1)}

        [message]
            speaker=Malin Keshar
            message= _ "The ogres are dead."
        [/message]

        [message]
            speaker=Darken Volk
            message= _ "Good... I have already showed you how to raise a skeleton before, but perhaps it would be best if we... went over it once again. Other than for ghouls, flesh and blood are useless for undead creatures. First, let out the blood and innards and dispose of them."
        [/message]

        [message]
            speaker=Malin Keshar
            # po: In the sense of "Understood, I will do that", not "very good".
            message= _ "Very well."
        [/message]

        [move_unit]
            id=Malin Keshar
            to_x,to_y=$loc1.x,$loc1.y
        [/move_unit]

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

        [delay]
            time=750
        [/delay]

        [message]
            speaker=Malin Keshar
            message= _ "What a mess."
        [/message]

        [message]
            speaker=Darken Volk
            message= _ "Leave it. The ghouls will take care of it in due course. Next, strip away the flesh. A little remaining is fine; too much will hamper your skeletons’ movement."
        [/message]

        [delay]
            time=350
        [/delay]

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

        [delay]
            time=500
        [/delay]

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

        [delay]
            time=750
        [/delay]

        [message]
            speaker=Malin Keshar
            message= _ "And then?"
        [/message]

        [message]
            speaker=Darken Volk
            message= _ "Bind the strings of energy to the bone. Skeletons are no more than puppets. Novices will directly control their movement with strands of dark magic, but you should have no need for such crude methods. Instead, impart your will upon the skeleton, spread the threads over the bones, then let loose the twine from your hand. If your will is strong enough, the skeleton will be able to move on its own."
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "I will try."
        [/message]

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

        [delay]
            time=400
        [/delay]

        {REMOVE_IMAGE $loc1.x $loc1.y}

        [unit]
            type=Skeleton
            x,y=$loc1.x,$loc1.y
            passable=yes
            side=1
            animate=yes
        [/unit]

        [message]
            speaker=Darken Volk
            message= _ "Do it again."
        [/message]

        [delay]
            time=750
        [/delay]

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

        [delay]
            time=400
        [/delay]

        {REMOVE_IMAGE $loc2.x $loc2.y}

        [unit]
            type=Skeleton
            x,y=$loc2.x,$loc2.y
            passable=yes
            side=1
            animate=yes
        [/unit]

        [terrain]
            x=19,20,20
            y=9,9,8
            terrain=Ur
        [/terrain]
        [redraw][/redraw]

        {CLEAR_VARIABLE loc1}
        {CLEAR_VARIABLE loc2}

        [message]
            speaker=Darken Volk
            message= _ "Good. Now head north. I sense another ogre there."
        [/message]

        {OBJECTIVE_EXPLORE}
    [/event]

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

        [message]
            speaker=Malin Keshar
            message= _ "I can turn this one into a skeleton as well."
        [/message]
    [/event]

    # Second mini area (third young ogre)
    [event]
        name=die
        first_time_only=yes
        [filter]
            id=Ogre3
        [/filter]

        {FULL_HEAL_RESTORE (side=1)}

        {PLACE_IMAGE "items/bones.png" $x1 $y1}

        [kill]
            id=Ogre3
        [/kill]

        [remove_shroud]
            x,y=17,8
            radius=1
        [/remove_shroud]

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

        [delay]
            time=1000
        [/delay]

        [message]
            speaker=Darken Volk
            message= _ "The ogre seems to have looted a bow. Use it to create a Skeleton Archer."
        [/message]

        [message]
            speaker=Malin Keshar
            # po: That's the case for both usages of this string.
            message= _ "Very well."
        [/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 Archer
            x,y=$x1,$y1
            side=1
            animate=yes
            passable=yes
        [/unit]

        {REMOVE_IMAGE $x1 $y1}
        {REMOVE_IMAGE 17 8}

        [message]
            speaker=Darken Volk
            message= _ "It seems that raising skeletons is already a trivial matter for you now. You are indeed... talented."
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "Perhaps, but I will need more than a few skeletons to fight the orcs."
        [/message]

        [message]
            speaker=Darken Volk
            message= _ "They will come in time, when you learn to control more of them at once. For now, continue exploring these caves. We may yet find something else of use."
        [/message]

        [terrain]
            x=24,25,24,25,21,22
            y=10,10,11,11,6,6
            terrain=Rb
        [/terrain]
        [terrain]
            x=21,15,17
            y=7,5,5
            terrain=Uu
        [/terrain]
        [terrain]
            x=16,16
            y=4,5
            terrain=Ur
        [/terrain]
        [redraw][/redraw]

        {GENERIC_UNIT 2 (Giant Mudcrawler) 24 5}
        {GENERIC_UNIT 2 (Giant Mudcrawler) 26 6}
        {GENERIC_UNIT 2 (Giant Mudcrawler) 27 7}
        {GENERIC_UNIT 2 (Giant Mudcrawler) 26 4}

        [unit]
            type=Giant Rat
            x,y=8,2
            side=2
            level=1
            id=Big Rat
            ai_special=guardian
            max_moves=3
            max_hitpoints=40
        [/unit]

        {VARIABLE mud_puzzle 1}
        {VARIABLE rat_puzzle 1}

        [objectives]
            side=1
            [objective]
                condition=win
                description= _ "Reach the end of the cave"
            [/objective]
            [objective]
                condition=lose
                description= _ "Death of Malin Keshar"
            [/objective]
            [gold_carryover]
                carryover_percentage=100
            [/gold_carryover]
        [/objectives]
    [/event]

    # Third area (mudcrawler puzzle)

    # not part of the puzzle, but justifies why there's a storm trident here
    [event]
        name=moveto
        [filter]
            x,y=29,11
            side=1
        [/filter]

        {PLACE_IMAGE "items/bones.png" $x1 $y1}

        [message]
            speaker=Malin Keshar
            message= _ "It seems like a merman used to live here."
        [/message]
    [/event]
    [event]
        name=sighted
        [filter]
            type=Giant Mudcrawler
        [/filter]
        [filter_second]
            side=1
        [/filter_second]

        [message]
            speaker=Malin Keshar
            message= _ "Ick. Mudcrawlers. How did these things end up here?"
        [/message]

        [message]
            speaker=Darken Volk
            message= _ "The magic of our world is unpredictable and random. Even without the interference of a trained mage, it sometimes coagulates by itself, giving rise to physical anomalies or breathing life into the inanimate. See there?"
        [/message]

        [remove_shroud]
            x,y=26,5
            radius=3
        [/remove_shroud]

        [scroll_to]
            x,y=26,5
        [/scroll_to]

        [delay]
            time=1000
        [/delay]

        [message]
            speaker=Darken Volk
            message= _ "It is merely a small eddy of energy, but enough to animate the swamp water."
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "I see. Well, even if these mudcrawlers are not much of a threat, they’re still bothersome. I wonder if I could be rid of them. What is magic mud even weak to, anyway? Fire?"
        [/message]

        [message]
            speaker=Darken Volk
            message= _ "You should be able to figure it out on your own."
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "... I suppose you’re right."
        [/message]
    [/event]
    [event]
        name=moveto
        [filter]
            x,y=29,10
            side=1
            [not]
                id=Malin Keshar
            [/not]
        [/filter]
        [filter_condition]
            [variable]
                name=mud_puzzle
                numerical_equals=1
            [/variable]
        [/filter_condition]

        [message]
            speaker=Malin Keshar
            message= _ "It looks like there is something mired in the mud over there. I should have a look."
        [/message]
    [/event]
    [event]
        name=moveto
        [filter]
            x,y=29,10
            id=Malin Keshar
        [/filter]
        [filter_condition]
            [variable]
                name=mud_puzzle
                numerical_equals=1
            [/variable]
        [/filter_condition]

        [message]
            speaker=Malin Keshar
            message= _ "Hmm, a storm trident. I wonder if it could be useful."
        [/message]

        {REMOVE_IMAGE 29 10}

        {VARIABLE mud_puzzle 2}
    [/event]
    [event]
        name=moveto
        [filter]
            x,y=23,6
            id=Malin Keshar
        [/filter]
        [filter_condition]
            [variable]
                name=mud_puzzle
                numerical_equals=2
            [/variable]
        [/filter_condition]

        [message]
            speaker=Malin Keshar
            message= _ "I can use the trident to set these trees aflame."
        [/message]

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

        [delay]
            time=1000
        [/delay]

        [sound]
            name=fire.wav
        [/sound]

        [delay]
            time=750
        [/delay]

        [terrain]
            x,y=23,6
            terrain=Rb
        [/terrain]
        [redraw][/redraw]

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

        {VARIABLE mud_puzzle 3}
    [/event]
    [event]
        name=moveto
        first_time_only=no
        [filter]
            x,y=26,5
            id=Malin Keshar
        [/filter]
        [filter_condition]
            [variable]
                name=mud_puzzle
                less_than=4
            [/variable]
        [/filter_condition]

        [if]
            [variable]
                name=mud_puzzle
                numerical_equals=3
            [/variable]
            [then]
                [message]
                    speaker=Malin Keshar
                    message= _ "Let’s see if this fire does anything."
                [/message]

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

                [sound]
                    name=fire.wav
                [/sound]

                [delay]
                    time=1000
                [/delay]

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

                [delay]
                    time=500
                [/delay]

                {REMOVE_IMAGE 26 5}

                [delay]
                    time=500
                [/delay]

                [message]
                    speaker=Malin Keshar
                    message= _ "I think that did it. No more pesky crawlers."
                [/message]

#ifdef EASY
                {GIVE_MALIN_EXPERIENCE 30}
#endif
#ifdef NORMAL
                {GIVE_MALIN_EXPERIENCE 25}
#endif
#ifdef HARD
                {GIVE_MALIN_EXPERIENCE 20}
#endif

                {VARIABLE mud_puzzle 4}
            [/then]
            [else]
                [message]
                    speaker=narrator
                    message= _ "Nothing happens."
                [/message]
            [/else]
        [/if]
    [/event]

    # only give the player 1 exp on killing mudcrawlers and giant rats, otherwise it's too easy to farm exp
    # since the filter is level=0, it doesn't affect the big rat you encounter
    [event]
        name=die
        first_time_only=no
        [filter]
            level=0
            [not]
                side=1
            [/not]
        [/filter]

        [store_unit]
            [filter]
                x,y=$x2,$y2
            [/filter]
            variable=s_unit
        [/store_unit]

        {VARIABLE_OP s_unit.experience sub 3}

        [unstore_unit]
            variable=s_unit
        [/unstore_unit]

        [if]
            [variable]
                name=unit.type
                equals=Mudcrawler
            [/variable]
            [then]
                [fire_event]
                    name=crawler hint
                [/fire_event]
            [/then]
            [else]
                [fire_event]
                    name=rat hint
                [/fire_event]

                # I swear if you just sit here eating rats all day...
                [if]
                    [variable]
                        name=s_unit.type
                        equals=Necrophage
                    [/variable]
                    [or]
                        [variable]
                            name=s_unit.type
                            equals=Ghast
                        [/variable]
                    [/or]
                    [and]
                        [variable]
                            name=s_unit.max_hitpoints
                            greater_than=111
                        [/variable]
                    [/and]
                    [then]
                        [fire_event]
                            name=rat eating1
                        [/fire_event]
                    [/then]
                [/if]
                [if]
                    [variable]
                        name=s_unit.type
                        equals=Necrophage
                    [/variable]
                    [or]
                        [variable]
                            name=s_unit.type
                            equals=Ghast
                        [/variable]
                    [/or]
                    [and]
                        [variable]
                            name=s_unit.max_hitpoints
                            greater_than=419
                        [/variable]
                    [/and]
                    [then]
                        [fire_event]
                            name=rat eating2
                        [/fire_event]
                    [/then]
                [/if]
                [if]
                    [variable]
                        name=s_unit.type
                        equals=Necrophage
                    [/variable]
                    [or]
                        [variable]
                            name=s_unit.type
                            equals=Ghast
                        [/variable]
                    [/or]
                    [and]
                        [variable]
                            name=s_unit.max_hitpoints
                            greater_than=999
                        [/variable]
                    [/and]
                    [then]
                        [fire_event]
                            name=rat eating3
                        [/fire_event]
                    [/then]
                [/if]

                # there's no way you do this without debug, right?
                [if]
                    [variable]
                        name=s_unit.type
                        equals=Necrophage
                    [/variable]
                    [or]
                        [variable]
                            name=s_unit.type
                            equals=Ghast
                        [/variable]
                    [/or]
                    [and]
                        [variable]
                            name=s_unit.max_hitpoints
                            greater_than=9999
                        [/variable]
                    [/and]
                    [then]
                        [fire_event]
                            name=rat eating4
                        [/fire_event]
                    [/then]
                [/if]
            [/else]
        [/if]

        {CLEAR_VARIABLE s_unit}
    [/event]
    [event]
        name=crawler hint

        [message]
            speaker=Malin Keshar
            message= _ "Somehow, I feel that killing these mudcrawlers is a less than meaningful use of my time."
        [/message]
    [/event]
    [event]
        name=rat hint

        [message]
            speaker=Malin Keshar
            message= _ "I don’t think there’s much experience to be gained by fighting a few rats."
        [/message]
    [/event]
    [event]
        name=rat eating1

        [message]
            speaker=Malin Keshar
            message= _ "I must be absolutely mad to be spending hours here just feeding rats to this rotten pile of flesh..."
        [/message]

        [set_achievement]
            content_for=descent_into_darkness
            id="did_rat_eater"
        [/set_achievement]
    [/event]
    [event]
        name=rat eating2

        [message]
            speaker=Darken Volk
            message= _ "Has this boy been smoking again?"
        [/message]
    [/event]
    [event]
        name=rat eating3

        [message]
            speaker=Malin Keshar
            message= _ "Day 1337:

All sense of time and being are entombed within darkness. How long has passed since I came here? My senses are a blur. My past life, my prior goals, my very self is all but a distant memory. All I know now is the constant squealing of rats and the ever growing mountain of flesh that feasts before me."
        [/message]
    [/event]
    [event]
        name=rat eating4

        [message]
            speaker=Malin Keshar
            #po: Malin Keshar breaks the fourth wall here and addresses the player, whom the 'you' refers to.
            message= _ "You. You there behind the screen. You are the one controlling me. Why are you doing this? What did I do to deserve this torment? Please, I beg of you. End me now. Release me from my misery!"
        [/message]
    [/event]
    # spawn crawlers
    [event]
        name=new turn
        first_time_only=no
        [filter_condition]
            [not]
                [have_unit]
                    type=Mudcrawler
                [/have_unit]
            [/not]
            [and]
                [variable]
                    name=mud_puzzle
                    greater_than=0
                [/variable]
            [/and]
            [and]
                [variable]
                    name=mud_puzzle
                    less_than=4
                [/variable]
            [/and]
        [/filter_condition]

        {GENERIC_UNIT 2 (Mudcrawler) 26 5}
    [/event]
    # spawn rats
    [event]
        name=new turn
        first_time_only=no
        [filter_condition]
            [not]
                [have_unit]
                    type=Giant Rat
                    level=0
                [/have_unit]
            [/not]
            [and]
                [variable]
                    name=rat_puzzle
                    greater_than=0
                [/variable]
            [/and]
            [and]
                [variable]
                    name=rat_puzzle
                    less_than=4
                [/variable]
            [/and]
        [/filter_condition]

        [unit]
            type=Giant Rat
            side=2
            x,y=8,7
            passable=yes
        [/unit]
    [/event]
    # Fourth area (wolves)

    # first some scenery events
    [event]
        name=sighted
        [filter]
            id=Big Rat
        [/filter]
        [filter_second]
            side=1
        [/filter_second]

        [message]
            speaker=Malin Keshar
            message= _ "What a fat rat. I wonder where it came from?"
        [/message]
    [/event]

    [event]
        name=sighted
        [filter]
            id=Big Scorpion
        [/filter]
        [filter_second]
            side=1
        [/filter_second]

        [message]
            speaker=Malin Keshar
            message= _ "This scorpion seems unusually large... and poisonous."
        [/message]
    [/event]

    [event]
        name=moveto
        [filter]
            x=10-11,12
            y=2-4,1-3
            side=1
        [/filter]

        [message]
            speaker=Malin Keshar
            message= _ "A tattered, red flag. Some orcs used to live here, but it seems like they were slain in a fierce battle."
        [/message]
    [/event]

    [event]
        name=moveto
        [filter]
            x,y=4,6
            side=1
        [/filter]

        # this is a whitefang banner, but Malin only gets introduced to them in the next scenario
        [message]
            speaker=Malin Keshar
            message= _ "This banner is different than the other ones. It looks familiar."
        [/message]
    [/event]

    [event]
        name=sighted
        [filter]
            type=Wolf
        [/filter]
        [filter_second]
            side=1
        [/filter_second]

        [remove_shroud]
            x,y=4,4
            radius=3
        [/remove_shroud]

        # implication is that the whitefangs abandoned them because they were maimed in the battle
        [message]
            speaker=Malin Keshar
            message= _ "A pack of wolves, the same that the goblins use as mounts. They were abandoned here for some reason."
        [/message]

        [modify_unit]
            [filter]
                type_adv_tree=Wolf,Great Wolf,Direwolf
            [/filter]
            [effect]
                apply_to=status
                remove=guardian
            [/effect]
        [/modify_unit]
    [/event]

    # rat puzzle
    [event]
        name=moveto
        [filter]
            x,y=2,2
            id=Malin Keshar
        [/filter]
        [filter_condition]
            [variable]
                name=rat_puzzle
                numerical_equals=1
            [/variable]
        [/filter_condition]

        [message]
            speaker=Malin Keshar
            message= _ "An empty vial. I wonder if this could be useful."
        [/message]

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

        {REMOVE_IMAGE 2 2}

        {VARIABLE rat_puzzle 2}
    [/event]
    [event]
        name=die
        [filter]
            id=Big Scorpion
        [/filter]

        {PLACE_IMAGE "items/bones.png" 8 9}
    [/event]
    [event]
        name=moveto
        [filter]
            x,y=8,9
            id=Malin Keshar
        [/filter]
        [filter_condition]
            [variable]
                name=rat_puzzle
                numerical_equals=2
            [/variable]
        [/filter_condition]

        [message]
            speaker=Malin Keshar
            message= _ "This scorpion’s poison gland is still intact. I bet I can make a potion out of it."
        [/message]

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

        [unit_overlay]
            id=Malin Keshar
            image="misc/potion-poison-icon.png"
            object_id="potion_poison_icon"
        [/unit_overlay]

        {VARIABLE rat_puzzle 3}
    [/event]
    [event]
        name=moveto
        first_time_only=no
        [filter]
            x,y=8,7
            id=Malin Keshar
        [/filter]
        [filter_condition]
            [variable]
                name=rat_puzzle
                less_than=4
            [/variable]
        [/filter_condition]

        [if]
            [variable]
                name=rat_puzzle
                numerical_equals=3
            [/variable]
            [then]
                [message]
                    speaker=Malin Keshar
                    message= _ "I think I can pour in the poison from here."
                [/message]

                [remove_object]
                    id=Malin Keshar
                    object_id="potion_poison_icon"
                [/remove_object]

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

                [delay]
                    time=500
                [/delay]

                {REMOVE_IMAGE 9 7}

                [delay]
                    time=500
                [/delay]

                [message]
                    speaker=Malin Keshar
                    message= _ "These rats will no longer bother us."
                [/message]

#ifdef EASY
                {GIVE_MALIN_EXPERIENCE 30}
#endif
#ifdef NORMAL
                {GIVE_MALIN_EXPERIENCE 25}
#endif
#ifdef HARD
                {GIVE_MALIN_EXPERIENCE 20}
#endif

                {VARIABLE rat_puzzle 4}
            [/then]
            [else]
                [fire_event]
                    name=rat nest
                [/fire_event]
            [/else]
        [/if]
    [/event]

    [event]
        name=rat nest

        [message]
            speaker=Malin Keshar
            message= _ "It appears as if there are some rats living here, but the hole in the wall is too small to reach them."
        [/message]
    [/event]

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

        [fire_event]
            name=water dialogue
        [/fire_event]

        [message]
            speaker=Malin Keshar
            [option]
                label= _ "I’m ready to learn about controlling spirits."
                [command]
                    [fire_event]
                        name=cutscene2
                    [/fire_event]
                [/command]
            [/option]
            [option]
                label= _ "I’m not ready yet."
            [/option]
        [/message]
    [/event]

    [event]
        name=water dialogue

        [message]
            speaker=Darken Volk
            message= _ "You have done well so far to master the reanimation of skeletons. Now, I shall teach you to control spirits. Are you ready?"
        [/message]
    [/event]

    [event]
        name=cutscene2

        [message]
            speaker=Darken Volk
            message= _ "Mastery over ghosts, especially the more powerful ones, is something even experienced necromancers may struggle with. While other undead are nothing more than mindless slaves upon reanimation, spirits are the souls of their original owners and thus retain some degree of consciousness. If the spirit is stronger than you, it will break free of your hold. To master it, your own soul must be unwavering."
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "I doubt I shall be overcome by the soul of an orc."
        [/message]

        [message]
            speaker=Darken Volk
            message= _ "Perhaps, but that is not what I meant. Before you can begin to control other souls, you must first master your own."
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "What do you mean? I am proud. I am strong. If it is a matter of conviction—"
        [/message]

        [message]
            speaker=Darken Volk
            message= _ "It is not I that you must convince, but yourself. Hold thy tongue and listen. After a great slaughter, even after the corpses are all rotted to nothing but bones, the bridge between the land of death and life is weakened. By placing yourself closer to death, you will better understand who it is that you really are. But you must not speak. Be silent and embrace the darkness."
        [/message]

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

        [message]
            speaker=Darken Volk
            message= _ "Concentrate."
        [/message]

        [delay]
            time=1500
        [/delay]

        [terrain]
            x=5,5,6
            y=11,12,12
            terrain=Wwg
        [/terrain]
        [redraw][/redraw]

        [delay]
            time=1000
        [/delay]

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

        [delay]
            time=750
        [/delay]

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

        [delay]
            time=1250
        [/delay]

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

        [delay]
            time=1500
        [/delay]

        {SCREEN_FADE_OUT}

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

        [place_shroud]
            side=1
            x=1-32
            y=1-21
        [/place_shroud]

        [remove_shroud]
            side=1
            x,y=5,16
            radius=2
        [/remove_shroud]

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

        [unstore_unit]
            x,y=5,16
            variable=malin
        [/unstore_unit]

        {MODIFY_UNIT (id=Malin Keshar) facing nw}

        {CLEAR_VARIABLE malin}

        [delay]
            time=1500
        [/delay]

        [scroll_to]
            x,y=5,16
        [/scroll_to]

        {SCREEN_FADE_IN}

        [delay]
            time=1500
        [/delay]

        [message]
            speaker=Malin Keshar
            message= _ "What sorcery is this? Darken Volk, what’s going... on..?"
        [/message]

        [unit]
            type=Apprentice Mage
            id=Fake Malin
            name= _ "Malin Keshar"
            profile=portraits/malin_young.webp
            x,y=4,15
            side=4
            facing=se
            animate=yes
            generate_name=no
            random_traits=no
        [/unit]

        [delay]
            time=1000
        [/delay]

        [message]
            speaker=Malin Keshar
            message= _ "Where is everything? Who are you?"
        [/message]

        [message]
            speaker=Fake Malin
            message= _ "I am Malin Keshar."
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "No, <i>I</i> am Malin Keshar. Tell me who you are, and this time, make it believable."
        [/message]

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

        [unit]
            type=Apprentice Necromancer
            id=Fake Malin
            name= _ "Malin Keshar"
            profile=portraits/malin_old.webp
            x,y=4,15
            side=4
            facing=se
            animate=no
            overwrite=yes
            generate_name=no
            random_traits=no
        [/unit]

        [message]
            speaker=Fake Malin
            message= _ "I am Malin Keshar, but no longer the naive, uncertain boy I once was."
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "I am no boy, nor am I uncertain. And I certainly won’t be questioned by a mere reflection like you."
        [/message]

        [message]
            speaker=Fake Malin
            message= _ "The distorted reflection you see mirrors the reality within. An ever morphing perspective, warped by a profusion of uncertain ambitions."
        [/message]

        # doesn't sound a lot like one goal to me... but he's just a boy, so he can't be expected to recognize that he's contradicting himself
        [message]
            speaker=Malin Keshar
            message= _ "Nonsense. My convictions are strong and my mind is set. I have but one goal. I will gain power. I will master death, and I will use it to protect my home and slaughter these orcs. And I’ll make my family and my people accept me back."
        [/message]

        [message]
            speaker=Fake Malin
            message= _ "When you are drawn in many directions at once, which one will you choose? If you try for them all, you shall be torn apart."
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "Protecting my home and slaying the orcs are the same thing. If you mean choosing between my family and gaining more power... they will come around and see reason. I will make sure of it."
        [/message]

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

        [unit]
            type=Dark Mage
            id=Fake Malin
            name= _ "Malin Keshar"
            profile=portraits/malin_old-decay.webp
            x,y=4,15
            side=4
            facing=se
            animate=no
            overwrite=yes
            generate_name=no
            random_traits=no
        [/unit]

        [message]
            speaker=Fake Malin
            message= _ "When you do not have the wisdom to see through the veil of lies you have told yourself, you must not seek the advice of one who only spins the web of deception further."
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "Enough with this. No proverbs are going to help me achieve my goals. This is just like back on Alduin. My teachers never understood what I needed. I don’t need to think and I don’t need to philosophize about “the grand nature of my life and my place in the universe.” Right now, I must train my skills with necromancy. That’s it."
        [/message]

        [message]
            speaker=Fake Malin
            message= _ "When you are sad and alone, even all the power in the world will not help you. In the barren void of endless night, will rage and hatred alone be enough to sustain you?"
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "... well, my choice is already made. Rethinking it now is pointless. I have never been one for regrets."
        [/message]

        [delay]
            time=1000
        [/delay]

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

        [kill]
            id=Fake Malin
        [/kill]
        [terrain]
            x,y=4,15
            terrain=Wog
        [/terrain]
        [redraw][/redraw]

        [delay]
            time=500
        [/delay]

        [message]
            speaker=Malin Keshar
            message= _ "... Damn it all. As if I would pay any heed to a twisted doppelganger of myself. Anyway..."
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "What did Darken Volk say about it again? “After a great slaughter, even after the corpses are all rotted to nothing but bones, the bridge between the land of death and life is weakened.” If I use that knowledge..."
        [/message]

        [delay]
            time=250
        [/delay]

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

        [delay]
            time=250
        [/delay]

        [message]
            speaker=Malin Keshar
            message= _ "... I should be able to sense the soul past the barrier of life and death..."
        [/message]

        [delay]
            time=500
        [/delay]

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

        [delay]
            time=1500
        [/delay]

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

        [delay]
            time=1000
        [/delay]

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

        [unit]
            type=Ghost
            x,y=4,15
            side=1
            hitpoints=10
            animate=yes
            passable=yes
            id=S3_Rebel_Soul
        [/unit]

        [delay]
            time=500
        [/delay]

        [message]
            speaker=Malin Keshar
            message= _ "I did it! I—"
        [/message]

        # aaand Malin loses control because he's not as confident as he says he is

        [delay]
            time=300
        [/delay]
        {MODIFY_UNIT (id=S3_Rebel_Soul) side 4}
        [delay]
            time=400
        [/delay]
        {MODIFY_UNIT (id=S3_Rebel_Soul) side 1}
        [delay]
            time=400
        [/delay]
        {MODIFY_UNIT (id=S3_Rebel_Soul) side 4}
        [delay]
            time=400
        [/delay]
        {MODIFY_UNIT (id=S3_Rebel_Soul) side 1}
        [delay]
            time=400
        [/delay]
        {MODIFY_UNIT (id=S3_Rebel_Soul) side 4}
        [delay]
            time=400
        [/delay]

        [message]
            speaker=S3_Rebel_Soul
            message=_ "Freedom! Your sorceries cannot shackle me, Malin the Damned!"
        [/message]

        [message]
            speaker=Malin Keshar
            message=_ "I failed? No... it must have been because the spell was too tricky to master on the first attempt. No matter, no matter. It’s only a single, weak spirit. It should be simple to crush it and exert my will back over it."
        [/message]

        [objectives]
            side=1
            [objective]
                condition=win
                description= _ "Defeat the rebel ghost"
            [/objective]
            [objective]
                condition=lose
                description= _ "Death of Malin Keshar"
            [/objective]
            [gold_carryover]
                carryover_percentage=100
            [/gold_carryover]
        [/objectives]
    [/event]

    [event]
        name=last breath
        [filter]
            id=S3_Rebel_Soul
        [/filter]

        [message]
            speaker=Malin Keshar
            message= _ "I’ve got you!"
        [/message]

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

        [delay]
            time=400
        [/delay]
        {MODIFY_UNIT (id=S3_Rebel_Soul) side 1}
        [delay]
            time=400
        [/delay]
        {MODIFY_UNIT (id=S3_Rebel_Soul) side 4}
        [delay]
            time=400
        [/delay]
        {MODIFY_UNIT (id=S3_Rebel_Soul) side 1}
        [delay]
            time=400
        [/delay]
        {MODIFY_UNIT (id=S3_Rebel_Soul) side 4}
        [delay]
            time=400
        [/delay]
        {MODIFY_UNIT (id=S3_Rebel_Soul) side 1}
        [delay]
            time=400
        [/delay]

        [unit]
            x,y=$x1,$y1
            type=Spectral Servant
            variation=SS1
            overwrite=yes
            [modifications]
                {TRAIT_LOYAL}
            [/modifications]
        [/unit]

        [delay]
            time=900
        [/delay]

        {SCREEN_FADE_OUT}

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

        [unstore_unit]
            x,y=5,11
            find_vacant=yes
            variable=malin
        [/unstore_unit]

        [store_unit]
            [filter]
                type=Spectral Servant
            [/filter]
            variable=ghost
            kill=yes
        [/store_unit]

        [unstore_unit]
            x,y=6,10
            find_vacant=yes
            variable=ghost
        [/unstore_unit]

        [place_shroud]
            side=1
            x,y=5,16
            radius=3
        [/place_shroud]

        {CLEAR_VARIABLE malin,ghost}

        [delay]
            time=1500
        [/delay]

        [foreach]
            array=units
            [do]
                [unstore_unit]
                    find_vacant=yes
                    variable=units[$i]
                [/unstore_unit]
            [/do]
        [/foreach]

        {CLEAR_VARIABLE units}

        # Restore Darken Volk's MP so that he isn't stuck behind the shroud after the redraw
        {MODIFY_UNIT (id=Darken Volk) max_moves 6}
        {MODIFY_UNIT (id=Darken Volk) moves 6}
        {MODIFY_UNIT (id=Darken Volk) side 1}

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

        [scroll_to]
            x,y=5,11
        [/scroll_to]

        {SCREEN_FADE_IN}

        [message]
            speaker=Malin Keshar
            message= _ "I’ve done it! I’ve learned to summon the souls of the dead!"
        [/message]

        [message]
            speaker=Darken Volk
            message= _ "So you have, and you even managed to regain control of it when it slipped out of your grasp. Well done. Now pay heed. A necromancer’s first ghost is often special — use it carefully, and it will serve you well. Let it escape your control again, and it will be lost to you forever."
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "I see. I won’t make the same mistake twice."
        [/message]

        [message]
            speaker=Darken Volk
            message= _ "We shall see. You have done well enough so far, but I still have much more to teach you. Come, we have work to do."
        [/message]

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

    [event]
        name=victory

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

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

        [remove_object]
            id=Malin Keshar
            object_id="potion_poison_icon"
        [/remove_object]

        # Duplicated from the rebel soul event for debugging purposes
        {MODIFY_UNIT (id=Darken Volk) max_moves 6}
        {MODIFY_UNIT (id=Darken Volk) moves 6}
        {MODIFY_UNIT (id=Darken Volk) side 1}

        {CLEAR_VARIABLE mud_puzzle,rat_puzzle}
    [/event]

    {HERODEATH_MALIN}
    {HERODEATH_VOLK}
[/scenario]

#undef OBJECTIVE_EXPLORE
#undef FULL_HEAL_RESTORE
