#textdomain wesnoth-httt
[scenario]
    id=13_The_Dwarven_Doors
    name= _ "The Dwarven Doors"
    map_data="{campaigns/Heir_To_The_Throne/maps/13_The_Dwarven_Doors.map}"
    {TURNS 26 20 15}
    victory_when_enemies_defeated=no

    {DEFAULT_SCHEDULE_DUSK}

    next_scenario=14_Plunging_into_the_Darkness

    {SCENARIO_MUSIC "battle.ogg"}
    {EXTRA_SCENARIO_MUSIC "the_dangerous_symphony.ogg"}
    {EXTRA_SCENARIO_MUSIC "legends_of_the_north.ogg"}
    {EXTRA_SCENARIO_MUSIC "northern_mountains.ogg"}
    {EXTRA_SCENARIO_MUSIC "casualties_of_war.ogg"}
    {EXTRA_SCENARIO_MUSIC "battle-epic.ogg"}
    {EXTRA_SCENARIO_MUSIC "the_city_falls.ogg"}

    {HTTT_TRACK {JOURNEY_13_NEW} }

    [event]
        name=prestart
        [objectives]
            side=1
            [objective]
                description= _ "Move Konrad to the entrance of the dwarven tunnels"
                condition=win
            [/objective]
            [objective]
                description= _ "Death of Konrad"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Delfador"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Kalenz"
                condition=lose
            [/objective]

            {TURNS_RUN_OUT}

            [gold_carryover]
                bonus=yes
                carryover_percentage=40
            [/gold_carryover]
        [/objectives]
    [/event]

    {PLACE_IMAGE scenery/dwarven-doors-closed.png 14 3}
    {PLACE_IMAGE scenery/mine-abandoned.png 25 2}

    [side]
        type=Commander
        id=Konrad
        name= _ "Konrad"
        unrenamable=yes
        profile=portraits/konrad-human.png
        side=1
        canrecruit=yes
        team_name=elves
        user_team_name=_"Rebels"
        controller=human
        gold=200
        {FLAG_VARIANT long}
    [/side]

    [side]
        type=Orcish Slurbow
        id=Knafa-Telfar
        name= _ "Knafa-Telfar"
        side=2
        canrecruit=yes
        recruit=Troll Whelp,Orcish Grunt,Orcish Crossbowman
        {GOLD 100 120 140}
        {INCOME 14 18 22}
        [ai]
            {ATTACK_DEPTH 4 5 5}
            aggression=1.0
            grouping=no
            caution=-5.0
        [/ai]
        team_name=orcs
        user_team_name=_"Orcs"
        {FLAG_VARIANT6 ragged}
    [/side]

    [side]
        type=Orcish Warlord
        id=Urug-Tan
        name= _ "Urug-Tan"
        side=3
        canrecruit=yes
        recruit=Troll Whelp,Orcish Grunt,Orcish Crossbowman
        {GOLD 75 95 115}
        {INCOME 12 14 18}
        [ai]
            {ATTACK_DEPTH 4 5 5}
            aggression=1.0
            grouping=no
            caution=-5.0
        [/ai]
        team_name=orcs
        user_team_name=_"Orcs"
        {FLAG_VARIANT6 ragged}
    [/side]

    [side]
        type=Orcish Sovereign
        id=Shuuga-Mool
        name= _ "Shuuga-Mool"
        side=4
        canrecruit=yes
        recruit=Orcish Grunt,Wolf Rider,Orcish Archer,Troll
        {GOLD 30 50 70}
        {INCOME 12 14 18}
        team_name=orcs
        user_team_name=_"Orcs"
        [ai]
            {ATTACK_DEPTH 4 5 5}
            aggression=1.0
            grouping=no
            caution=-5.0
        [/ai]
        {FLAG_VARIANT6 ragged}
    [/side]

    [side]
        side=5
        no_leader=yes
        controller=ai
        hidden=yes
    [/side]

    {STARTING_VILLAGES 2 20}
    {STARTING_VILLAGES 3 20}
    {STARTING_VILLAGES 4 20}
    [label]
        x=15
        y=21
        text= _ "Pillars of Thunedain"
    [/label]
    [label]
        x=14
        y=4
        text= _ "The Great Doors"
    [/label]

#define LISAR_GUARD_DOORS
    {NAMED_LOYAL_UNIT 2 (Royal Guard) 8 44 () ( _ "Royal Guard")}
#enddef

    [event]
        name=prestart
        #
        # Randomly set the exit location - 1=the main door and 2=the mine
        #
        {VARIABLE_OP true_entrance_location rand "1..2"}

        [lua]
            code=<<
-- The only parameter right now is the coverage (real 0.0 to 1.0, incl.)
-- For prime-time use the filter terrain needs to come in and the convert
-- table needs to be VERY complete. Or, rename this to morph_terrain and
-- have the conversion table come in as an argument, too.

local locations = wesnoth.get_locations{terrain = 'Ce,Ch,Chr,Co,Gd,Gd^Vc,Gd^Vh,Gd^Vo,Gll^Fp,Hhd,Hhd^Fp,Hhd^Vo,Ke,Khr,Ko,Md,Mm,Rd,Re,Re^Vo,Rr,Ww'}

local coverage = wml.variables.snowCoverage
if coverage == nil or coverage < 0 then
    coverage = 0
elseif coverage > 1 then
    coverage = 1
end
local snowNeeded = (#locations * coverage) / 3
-- Use 1/3rd of the coverage from Northern Winter. Spring is approaching. We need to watch
-- this; if it adds too much snow, the player may not be able to make it to the exit in time.

local convert = {
    ['Ce'] = 'Cea',
    ['Ch'] = 'Cha',
    ['Chr'] = 'Cha',
    ['Co'] = 'Coa',
    ['Gd'] = 'Aa',
    ['Gd^Vc'] = 'Aa^Vca',
    ['Gd^Vh'] = 'Aa^Vha',
    ['Gd^Vo'] = 'Aa^Voa',
    ['Gll^Fp'] = 'Aa^Fpa',
    ['Hhd'] = 'Ha',
    ['Hhd^Fp'] = 'Ha^Fpa',
    ['Hhd^Vo'] = 'Ha^Voa',
    ['Ke'] = 'Kea',
    ['Khr'] = 'Kha',
    ['Ko'] = 'Koa',
    ['Md'] = 'Ms',
    ['Mm'] = 'Ms',
    ['Rd'] = 'Aa',
    ['Re'] = 'Aa',
    ['Re^Vo'] = 'Aa^Voa',
    ['Rr'] = 'Aa',
    ['Ww'] = 'Ai'
}

local loopCounter
for loopCounter = 1, snowNeeded do
    local locationsIndex = wesnoth.random(#locations)
    local coordinate = locations[locationsIndex]
    local terrainCode = wesnoth.get_terrain(coordinate[1], coordinate[2])
    wesnoth.set_terrain(coordinate[1], coordinate[2], (convert[terrainCode] or terrainCode))
    table.remove(locations, locationsIndex)
end
            >>
        [/lua]
    [/event]

    [event]
        name=prestart

        {NEED_DELFADOR placement=leader}
        {NEED_KALENZ placement=leader}
        {STORE_LISAR}
        [recall]
            id=Haldiel
        [/recall]
        [role] #If Haldiel was just recalled, this will select him.
            role=Rider
            [auto_recall][/auto_recall]

            type="Elvish Outrider," +
                "Paladin,Grand Knight," +
                "Elvish Rider," +
                "Knight,Lancer," +
                "Elvish Scout," +
                "Horseman"
            [else]
                [unit]
                    side=1
                    type=Horseman
                    role=Rider
                    placement=leader
                [/unit]
            [/else]
        [/role]
        [recall]
            id=Delurin
        [/recall]
        [role] # If Delurin was just recalled, this will select him.
            role=Outlaw_Advisor
            [auto_recall][/auto_recall]

            type="Highwayman,Fugitive,Huntsman,Ranger," +
                "Bandit,Outlaw,Trapper," +
                "Thug,Footpad,Poacher"
        [/role]
    [/event]

    [event]
        name=start

        [message]
            speaker=Delfador
            message= _ "At last, this is the entrance to the dwarven tunnels."
        [/message]
        [message]
            speaker=Konrad
            message= _ "All I can see are ruins and poor villages."
        [/message]
        [message]
            speaker=Kalenz
            message= _ "The poor villagers that once lived here and traded with the dwarves are now held in slavery by the orcs."
        [/message]
        [message]
            speaker=Konrad
            message= _ "Slaves to the evil orcs? We must liberate them!"
        [/message]
        [message]
            speaker=Delfador
            {DELFADOR_MENTORING}
            message= _ "That would not be a wise choice, for our mission is to retrieve the Sceptre of Fire. If we tarry in this place, hordes of orcs will surround us."
        [/message]
        [message]
            speaker=Kalenz
            message= _ "Konrad, heed the words of Delfador. We shall return to wrest the grip of the orcs from these lands. Look — orcs are already gathering. More are surely on their way."
        [/message]
        [message]
            speaker=Konrad
            message= _ "This does not please me, but I will listen to your advice."
        [/message]
        [message]
            role=Rider
            message= _ "Sir, I should like to scout out the path ahead. It would be disaster for us to find these doors sealed, leaving us as hunting fodder for the gathering horde."
        [/message]
        [message]
            speaker=Delfador
            message= _ "Excellent. We must find a way underground before we are overwhelmed."
        [/message]
        #
        # If you have outlaws in your party, you have the chance to
        # find a special unit
        #
        [if]
            [have_unit]
                role=Outlaw_Advisor
            [/have_unit]

            [then]
                [store_unit]
                    [filter]
                        role=Outlaw_Advisor
                    [/filter]

                    kill=no
                    variable=outlaw_advisor_store
                [/store_unit]

                {VARIABLE outlaw_name $outlaw_advisor_store.name}

                [message]
                    role=Outlaw_Advisor
                    message= _ "My uncle used to smuggle... err... I mean... trade food for the dwarves. He could get grain carts in under the very noses of those ugly orcs."
                [/message]
                [message]
                    role=Outlaw_Advisor
                    message= _ "He must be hiding in one of those villages."
                [/message]
            [/then]
        [/if]
    [/event]

    #
    # Some colorful talking events to make the scenario more interesting
    #

    [event]
        name=turn 2
        [message]
            speaker=Konrad
            message= _ "The defense of the dwarves must have been strong. Look at those ruined towers!"
        [/message]
        [message]
            speaker=Delfador
            message= _ "The battle outside was fierce and lasted a full half-year. But, the battles inside the tunnels were worse."
        [/message]
    [/event]

    [event]
        name=moveto
        [filter]
            id=Delfador
            x=11-19
            y=19-22
        [/filter]
        [message]
            speaker=Delfador
            {DELFADOR_MENTORING}
            # wmllint: local spelling Thunedain
            message= _ "The pillars of Thunedain. He was a legendary dwarvish lord who made his last stand here. May we triumph where he fell."
        [/message]
        [message]
            speaker=Kalenz
            message= _ "I would settle for escape, though I know not which I dread more: foul orcs or fetid caves."
        [/message]

        [allow_undo][/allow_undo]
    [/event]

    [event]
        name=die
        [filter]
            side=2,3,4
        [/filter]
        [filter_second]
            side=1
            id=Haldiel,Simyr
            [or]
                type=Paladin
            [/or]
        [/filter_second]
        [message]
            speaker=second_unit
            message= _ "Back to the abyss, spawn of filth!"
        [/message]
    [/event]

    #
    # Finding the bandit uncle who gives you a big hint about the true exit
    #
    [event]
        name=moveto
        [filter]
            side=1
            x=18
            y=24
        [/filter]
        [if]
            [have_unit]
                role=Outlaw_Advisor
            [/have_unit]
            [then]
                {NAMED_LOYAL_UNIT 1 (Bandit) 18 24 (Uncle Somf) ( _ "Uncle Somf")}

                [message]
                    speaker=unit
                    message= _ "Who... who’s here?"
                [/message]

                #
                # The bandit's speech depends on who finds him
                #
                [if]
                    [have_unit]
                        role=Outlaw_Advisor
                        x,y=18,24
                    [/have_unit]
                    # the outlaw advisor finds Uncle Somf
                    [then]
                        [message]
                            speaker=Uncle Somf
                            # po: The addressed person is the speaker's niece or nephew.
                            message= _ "$outlaw_name|! How have you been? I haven’t seen you in years."
                        [/message]
                    [/then]
                    # ...or someone else finds him
                    [else]
                        [message]
                            speaker=Uncle Somf
                            # po: The mentioned person, $outlaw_name, is the speaker's niece or nephew.
                            message= _ "Haw! Any friend of $outlaw_name is a friend of mine too."
                        [/message]
                    [/else]
                [/if]

                [message]
                    speaker=Konrad
                    message= _ "We need to make it into the caves of the dwarves."
                [/message]

                #
                # The bandit divulges the true location of the entrance
                #
                [if]
                    [variable]
                        name=true_entrance_location
                        numerical_equals=1
                    [/variable]
                    [then]
                        [message]
                            speaker=Uncle Somf
                            message= _ "The mine entrances were all collapsed intentionally during the fighting. The doors, while heavily defended, remain accessible. The orcish hordes that assault them are repulsed, but you may be able to sneak in unnoticed."
                        [/message]
                    [/then]
                    [else]
                        [message]
                            speaker=Uncle Somf
                            message= _ "The best way is through the mine tunnels. The orcs have never found all the mine entrances, and many still lead deep underground."
                        [/message]
                    [/else]
                [/if]
            [/then]

            [else]
                [allow_undo][/allow_undo]
            [/else]
        [/if]
    [/event]

    [event]
        name=moveto

        [filter]
            id=Uncle Somf

            [filter_adjacent]
                role=Outlaw_Advisor
            [/filter_adjacent]

            [or]
                role=Outlaw_Advisor

                [filter_adjacent]
                    id=Uncle Somf
                [/filter_adjacent]
            [/or]
        [/filter]

        [message]
            speaker=Uncle Somf
            # po: The addressed person is the speaker's niece or nephew.
            message= _ "How are we doing, $outlaw_name|?"
        [/message]
        [message]
            speaker=$outlaw_advisor_store.id
            message= _ "The same as always, Uncle!"
        [/message]
        [message]
            speaker=Uncle Somf
            message= _ "That bad, huh?"
        [/message]

        [allow_undo][/allow_undo]
    [/event]

    #
    # When any side 1 unit reaches a possible exit, we find out if it
    # is the correct one. We don't know who will reach the exit,
    # so the dialog must be general for anyone.
    # If the unit is Konrad, the victory event is handled separately.
    #
    [event]
        name=moveto
        first_time_only=yes
        [filter]
            side=1
            x=14
            y=3
        [/filter]
        #
        # If true_entrance_location = 1 then this is the true entrance
        #
        [if]
            [variable]
                name=true_entrance_location
                numerical_equals=1
            [/variable]
            [then]
                [message]
                    speaker=unit
                    message= _ "The doors... they can be moved!"
                [/message]
                [message]
                    speaker=Delfador
                    message= _ "Quickly, now, let us slip inside and hope the dwarves do not object..."
                [/message]
                [message]
                    speaker=Kalenz
                    message= _ "... and that the orcs do not follow."
                [/message]
            [/then]
            [else]
                [message]
                    speaker=unit
                    message= _ "The doors are closed and barred from the inside!"
                [/message]
                [message]
                    speaker=Konrad
                    {KONRAD_VARIATION concerned}
                    message= _ "We can’t get in! What should we do now?"
                [/message]
                [message]
                    speaker=Kalenz
                    message= _ "It is said that the orcs used old mine tunnels to surprise the dwarves. There appears to be one nearby, to the north-east."
                [/message]
                [message]
                    speaker=Konrad
                    message= _ "Then we must make it to that tunnel!"
                [/message]
            [/else]
        [/if]
    [/event]

    [event]
        name=moveto
        [filter]
            side=1
            x=25
            y=2
        [/filter]
        #
        # If true_entrance_location = 2 then this is the true entrance
        #
        [if]
            [variable]
                name=true_entrance_location
                numerical_equals=2
            [/variable]
            [then]
                [message]
                    speaker=unit
                    message= _ "This old mine seems to be connected to the main tunnels."
                [/message]
                [message]
                    speaker=Kalenz
                    message= _ "I am hesitant to enter. It will be so difficult in the darkness!"
                [/message]
                [message]
                    speaker=Delfador
                    {DELFADOR_VARIATION mad}
                    message= _ "There is no time for idle chatter or delay of any kind. Onward!"
                [/message]
            [/then]
            [else]
                [message]
                    speaker=unit
                    message= _ "I have reached the mine entrance, but there is no tunnel here. Rock and rubble completely block the way."
                [/message]
                [message]
                    speaker=Konrad
                    message= _ "I hope we can make it through the main doors, then."
                [/message]
                [message]
                    speaker=Delfador
                    message= _ "It is our only choice now. Hurry!"
                [/message]
            [/else]
        [/if]
    [/event]

    #
    # These events end the level when Konrad makes it to the correct location.
    # This has to be a separate event from when any side 1 unit first reaches
    # a possible exit (the above events).
    #
    [event]
        name=moveto
        first_time_only=no
        [filter]
            id=Konrad
            x=14
            y=3
        [/filter]
        [if]
            [variable]
                name=true_entrance_location
                numerical_equals=1
            [/variable]
            [then]
                [message]
                    speaker=Konrad
                    message= _ "Pray that we live to see sunlight again."
                [/message]
                [fire_event]
                    name=victory dance
                [/fire_event]
                [endlevel]
                    result=victory
                    bonus=yes
                    {NEW_GOLD_CARRYOVER 40}
                [/endlevel]
            [/then]
        [/if]
    [/event]
    [event]
        name=moveto
        first_time_only=no
        [filter]
            id=Konrad
            x=25
            y=2
        [/filter]
        [if]
            [variable]
                name=true_entrance_location
                numerical_equals=2
            [/variable]
            [then]
                [message]
                    speaker=Konrad
                    message= _ "Pray that we live to see sunlight again."
                [/message]
                [fire_event]
                    name=victory dance
                [/fire_event]
                [endlevel]
                    result=victory
                    bonus=yes
                    {NEW_GOLD_CARRYOVER 40}
                [/endlevel]
            [/then]
        [/if]
    [/event]

    [event]
        name=enemies defeated
        [message]
            speaker=Kalenz
            message= _ "We have defeated the foul orcs guarding this land, but we must continue without rest. More will surely come!"
        [/message]
        [if]
            [variable]
                name=true_entrance_location
                numerical_equals=1
            [/variable]
            [then]
                [message]
                    speaker=Delfador
                    message= _ "Indeed we must not delay. Let us breach the great doors to the dwarven kingdom."
                [/message]
            [/then]
            [else]
                [message]
                    speaker=Delfador
                    message= _ "Indeed we must not delay. The mines in the northeast are the best way to enter."
                [/message]
            [/else]
        [/if]
    [/event]

    [event]
        name=moveto
        [filter]
            side=1
            x=10-15
            y=10-15
        [/filter]

        {NAMED_LOYAL_UNIT 5 (Cuttle Fish) 13 13 (Cuttle Fish) ( _ "Cuttle Fish")}

        [message]
            speaker=Cuttle Fish
            message= _ "Ruarrrrr!!!"    # wmllint: no spellcheck
        [/message]
        [message]
            speaker=unit
            message= _ "A monster was hiding in that lake!"
        [/message]
        [role]
            type="Thief," +
                "Poacher,Footpad,Thug," +
                "Horseman," +
                "Elvish Scout,Elvish Archer,Elvish Fighter," +
                "Fighter," +
                "Rogue," +
                "Trapper,Outlaw,Bandit," +
                "Lancer,Knight," +
                "Elvish Rider,Elvish Marksman,Elvish Ranger,Elvish Hero,Elvish Captain," +
                "Elvish Lord," +
                "Commander," +
                "Assassin," +
                "Ranger,Huntsman,Fugitive,Highwayman," +
                "Grand Knight,Paladin," +
                "Elvish Outrider,Elvish Sharpshooter,Elvish Avenger,Elvish Champion,Elvish Marshal," +
                "Elvish High Lord," +
                "Lord"
            role=whiner
        [/role]
        [message]
            role=whiner
            message= _ "The legend was true! There are always tentacled creatures hiding in the lakes near the dwarven tunnels."
        [/message]
    [/event]

    [event]
        name=victory dance

        [message]
            speaker=narrator
            image="wesnoth-icon.png"
            message= _ "But Konrad’s party was not alone in entering the caves..."
        [/message]

        {NAMED_LOYAL_UNIT 2 (Princess) 8 44 (Li'sar) ( _ "Li’sar")}
        [+unit]
            profile=portraits/lisar.png
        [/unit]
        {LISAR_GUARD_DOORS}
        {LISAR_GUARD_DOORS}
        {LISAR_GUARD_DOORS}
        {LISAR_GUARD_DOORS}
        {LISAR_GUARD_DOORS}
        {LISAR_GUARD_DOORS}
        [message]
            speaker="Li'sar"
            message= _ "Whew! We made our way through the dangerous fog of the mountains, and now there is all this chaos before us! Come on, men! We must make it to the caves that lie ahead of us!"
        [/message]
    [/event]

    [event]
        name=victory

        {CLEAR_VARIABLE true_entrance_location}
        {CLEAR_VARIABLE outlaw_advisor_store}
        {CLEAR_VARIABLE outlaw_name}

        {RESTORE_LISAR}
    [/event]

    {campaigns/Heir_To_The_Throne/utils/deaths.cfg}
[/scenario]

#undef LISAR_GUARD_DOORS
