#textdomain wesnoth-did
[scenario]
    id=Endless_Night
    next_scenario=Endless_Night

    name=_ "Endless Night"
    map_data="{campaigns/Descent_Into_Darkness/maps/Endless_night.map}"
    turns=-1

    victory_when_enemies_defeated=yes

    [story]
        [part]
            story=_ "Years pass. Every summer when the mountain passes become clear, the lich sends his soldiers to attack the orcs, removing any human, elven, or dwarvish patrols that get in the way. Every year the undead inflict some damage before being repelled and retreating to the caves."
            background=story/end.jpg
            scale_background=no
        [/part]
        [part]
            story=_ "Rumors circulate of a lich that preys on scouting patrols. A hero, gathering his loyal troops to him, decides to put an end to the evil."
            background=story/end.jpg
            scale_background=no
        [/part]
    [/story]

    {UNDERGROUND}

    [side]
        side=1
        controller=human
        type=Lich
        id=Mal Keshar
        name=_ "Mal Keshar"
        unrenamable=yes
        canrecruit=yes
        recruit=Walking Corpse,Vampire Bat,Ghost,Ghoul,Skeleton Archer,Skeleton,Dark Adept
        gold=200
        save_id=Malin Keshar
        profile=portraits/malinlich.png
        {FLAG_VARIANT undead}
    [/side]

    {STARTING_VILLAGES 1 6}

    [side]
        # Foolish Hero
        side=2
        controller=ai
        # place leader, grant gold and recruits later
        no_leader=yes
        gold=0
        recruit=
        [ai]
            villages_per_scout=10
            village_value=0.5
        [/ai]
        fog=no
        shroud=no
        #After I have greatly reduced the number of villages in this scenario
        #it might be a good idea to give our foolish hero some income
        {INCOME 5 7 9}
    [/side]

    [event]
        name=prestart

        # {SCATTER_IMAGE (terrain=Uu) 3 scenery/rubble.png}

        [objectives]
            side=1
            [objective]
                condition=win
                description=_ "Defeat the foolish hero"
            [/objective]
            [objective]
                condition=lose
                description=_ "Destruction of Mal Keshar"
            [/objective]

            note={EARLY_FINISH_BONUS_NOTE}+{NEW_GOLD_CARRYOVER_NOTE_40}
        [/objectives]

        [if]
            [variable]
                name=timesForever
                greater_than=1
            [/variable]

            [then]
                {VARIABLE previous_previous_randomHero $previous_randomHero}
                {VARIABLE previous_randomHero $randomHero}
            [/then]

            [else]
                {VARIABLE previous_randomHero -1}
            [/else]
        [/if]

        # let's pick a random hero type...
        {VARIABLE_OP randomHero random (human,bandit,elf,dwarf,orc)}

        # and make sure it's not the same as last time
        [while]
            [variable]
                name=randomHero
                equals=$previous_randomHero
            [/variable]

            [or]
                [variable]
                    name=randomHero
                    equals=$previous_previous_randomHero
                [/variable]
            [/or]

            [do]
                {VARIABLE_OP randomHero random (human,bandit,elf,dwarf,orc)}
            [/do]
        [/while]

        [if]
            [variable]
                name=randomHero
                equals=human
            [/variable]
            [then]
                [unit]
                    side=2
                    type=Royal Guard
                    id=Foolish Hero
                    name=_ "Foolish Hero"
                    x,y=20,3
                    canrecruit=yes
                [/unit]
                {ALLOW_RECRUIT (Spearman,Swordsman,Pikeman,Bowman,Longbowman,Heavy Infantryman,Shock Trooper,Mage,Red Mage,White Mage,Horseman,Knight,Lancer)}

                {INCIDENTAL_MUSIC loyalists.ogg}
                [music]
                    name=the_city_falls.ogg
                    append=yes
                [/music]
            [/then]
        [/if]
        [if]
            [variable]
                name=randomHero
                equals=bandit
            [/variable]
            [then]
                [unit]
                    side=2
                    type=Assassin
                    id=Foolish Hero
                    name=_ "Foolish Hero"
                    x,y=20,3
                    canrecruit=yes
                [/unit]
                {ALLOW_RECRUIT (Thug,Bandit,Footpad,Outlaw,Poacher,Trapper,Thief,Rogue)}
                {INCIDENTAL_MUSIC battle.ogg}
            [/then]
        [/if]
        [if]
            [variable]
                name=randomHero
                equals=elf
            [/variable]
            [then]
                [unit]
                    side=2
                    type=Elvish Marshal
                    id=Foolish Hero
                    name=_ "Foolish Hero"
                    x,y=20,3
                    canrecruit=yes
                [/unit]
                {ALLOW_RECRUIT (Elvish Fighter,Elvish Hero,Elvish Captain,Elvish Archer,Elvish Ranger,Elvish Marksman,Elvish Shaman,Elvish Sorceress,Elvish Druid,Elvish Scout,Elvish Rider,Wose)}

                {INCIDENTAL_MUSIC knolls.ogg}
                [music]
                    name=elf-land.ogg
                    append=yes
                [/music]
            [/then]
        [/if]
        [if]
            [variable]
                name=randomHero
                equals=dwarf
            [/variable]
            [then]
                [unit]
                    side=2
                    type=Dwarvish Lord
                    id=Foolish Hero
                    name=_ "Foolish Hero"
                    x,y=20,3
                    canrecruit=yes
                [/unit]
                {ALLOW_RECRUIT (Dwarvish Fighter,Dwarvish Steelclad,Dwarvish Thunderer,Dwarvish Thunderguard,Dwarvish Stalwart,Dwarvish Guardsman,Dwarvish Ulfserker,Dwarvish Berserker,Dwarvish Scout,Dwarvish Pathfinder,Gryphon Rider,Gryphon Master)}

                {INCIDENTAL_MUSIC knalgan_theme.ogg}
                [music]
                    name=legends_of_the_north.ogg
                    append=yes
                [/music]
            [/then]
        [/if]
        [if]
            [variable]
                name=randomHero
                equals=orc
            [/variable]
            [then]
                [unit]
                    side=2
                    type=Orcish Warlord
                    id=Foolish Hero
                    name=_ "Foolish Hero"
                    x,y=20,3
                    canrecruit=yes
                [/unit]
                {ALLOW_RECRUIT (Orcish Warrior,Orcish Slayer,Orcish Crossbowman,Troll,Troll Rocklobber)}

                [music]
                    name=northerners.ogg
                    immediate=yes
                    append=no
                [/music]
                [music]
                    name=battle.ogg
                    append=yes
                [/music]
            [/then]
        [/if]

        # Give the foolish hero gold = 200*timesForever
        {VARIABLE heroGold 200}
        {VARIABLE_OP heroGold multiply $timesForever}

        [gold]
            side=2
            amount=$heroGold
        [/gold]
        {CLEAR_VARIABLE heroGold}

        # play only sad music after a few repetitions
        [if]
            [variable]
                name=timesForever
                greater_than=4
            [/variable]
            [then]
                {INCIDENTAL_MUSIC the_king_is_dead.ogg}
                [music]
                    name=elvish-theme.ogg
                    append=yes
                [/music]
                [music]
                    name=nunc_dimittis.ogg
                    append=yes
                [/music]
            [/then]
        [/if]
    [/event]

    [event]
        name=start
        [if]
            [variable]
                name=randomHero
                equals=human
            [/variable]
            [then]
                [message]
                    speaker=Foolish Hero
                    message=_ "Your doom is at hand, foul lich! I shall bring you down by my own hand."
                [/message]
                [message]
                    speaker=Mal Keshar
                    message=_ "The only doom at hand is yours, if you do not leave now. Go back to your manors and taverns, and do not trouble me further."
                [/message]
            [/then]
        [/if]
        [if]
            [variable]
                name=randomHero
                equals=bandit
            [/variable]
            [then]
                [message]
                    speaker=Foolish Hero
                    message=_ "You have preyed on too many of my boys. I have come here to end this, once and for all."
                [/message]
                [message]
                    speaker=Mal Keshar
                    message=_ "Not even real soldiers. I will sweep you aside."
                [/message]
            [/then]
        [/if]
        [if]
            [variable]
                name=randomHero
                equals=elf
            [/variable]
            [then]
                [message]
                    speaker=Foolish Hero
                    message=_ "Your kind is an abomination. I am here to see that you inflict no more harm on the spirits that have passed on."
                [/message]
                [message]
                    speaker=Mal Keshar
                    message=_ "Fool! By leaving your forests and coming into my domain, you have sacrificed your own spirits."
                [/message]
            [/then]
        [/if]
        [if]
            [variable]
                name=randomHero
                equals=dwarf
            [/variable]
            [then]
                [message]
                    speaker=Foolish Hero
                    message=_ "I've been itching for a chance to break some bones with my hammer, and now you've given me cause. Goodness knows you've got plenty of bones over there."
                [/message]
                [message]
                    speaker=Mal Keshar
                    message=_ "Go back to your tunnels and your mines, and do not trouble me unless you want your hammers to work for me once you are dead."
                [/message]
                [message]
                    speaker=Foolish Hero
                    message=_ "Hear that boys, he's threatening us! It's time he learned what real dwarves are made of."
                [/message]
            [/then]
        [/if]
        [if]
            [variable]
                name=randomHero
                equals=orc
            [/variable]
            [then]
                [message]
                    speaker=Foolish Hero
                    message=_ "So, I've finally found your lair, lich. You, who have been attacking the orcs for years and have destroyed many of our tribes."
                [/message]
                [message]
                    speaker=Mal Keshar
                    message=_ "Orcs? It is you who have forced me to choose the dark path, and it is you who ended my mortal life and made me turn into a lich. I am actually grateful to you for coming here, foolish hero. My hatred toward your race makes me stronger and I again get a chance to kill orcs!"
                [/message]
                [message]
                    speaker=Foolish Hero
                    message=_ "Nothing will stop me from taking revenge for my father who died at your hand and for the rest of my people you and your minions have killed, lich. Orcs, attack! The one who brings me his skull gets a bag of gold."
                [/message]
            [/then]
        [/if]

        # Some more harassment once the scanario's been played a few times
        [if]
            [variable]
                name=timesForever
                numerical_equals=4
            [/variable]
            [then]
                [message]
                    speaker=Mal Keshar
                    message=_ "Is there an endless supply of foolish heroes with death wishes? Honestly, where do you all come from?"
                [/message]
            [/then]
        [/if]
    [/event]

    [event]
        name=die

        [filter]
            id="Mal Keshar"
        [/filter]

        [if]
            [variable]
                name=timesForever
                greater_than=1
            [/variable]

            # after the first repetition, we'll let the campaign really end
            # when Malin dies
            [then]
                [role]
                    side=2
                    [not]
                        canrecruit=yes
                    [/not]
                    role=second
                [/role]
                [music]
                    name=elvish-theme.ogg
                    immediate=yes
                    append=no
                [/music]
                [message]
                    speaker=Foolish Hero
                    message=_ "Well, that is the end of his evil ways."
                [/message]
                [message]
                    role=second
                    message=_ "I wonder who he was in life, before falling into the evil ways that led him to today."
                [/message]
                [message]
                    speaker=Foolish Hero
                    message=_ "Once he chose this unlife for himself and his soldiers, he forfeited any claim to our compassion. Does it even matter who he was?"
                [/message]
                [message]
                    role=second
                    message=_ "No, I suppose it does not."
                [/message]

                [endlevel]
                    next_scenario=null
                    carryover_report=no
                    save=no
                    linger_mode=no
                [/endlevel]
            [/then]

            # Normal defeat condition on the first repeat
            [else]
                {MAL_DEATH}
            [/else]
        [/if]
    [/event]

    # In-scenario events defined within the prestart event

    [event]
        name=enemies defeated

        {VARIABLE_OP timesForever add 1}

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