#textdomain wesnoth-did

[scenario]
    id=04_Beginning_of_the_Revenge
    name= _ "Outset of Vengeance"
    map_data="{campaigns/Descent_Into_Darkness/maps/04_Beginning_of_the_Revenge.map}"
    {TURNS 30 27 24}
    next_scenario=05_Orc_War
    victory_when_enemies_defeated=yes

    {DEFAULT_SCHEDULE}

    {SCENARIO_MUSIC       the_city_falls.ogg}
    {EXTRA_SCENARIO_MUSIC casualties_of_war.ogg}
    {EXTRA_SCENARIO_MUSIC vengeful.ogg}

    [story]
        [part]
            music=underground.ogg
            story= _ "Weeks pass in the cold, bare caverns. There is little light and little food, but Malin’s burning desire for vengeance sustains him through his training. As the grip of black magic grows stronger, so does his control of the undead. He finds it to be a mastery of a different sort than what he was taught at the Academy. The magic reaches out to him as it never has before; he listens and it speaks back — he senses its will and lets it guide him — he embraces the dark magic as an extension of himself."
            {STORYTXT_BACKGROUND travel.jpg}
        [/part]
        [part]
            story= _ "When spring comes, Darken Volk leads Malin toward the jagged mountain peaks in the north. In the higher reaches, snow begins to melt into rivulets of water, streaming downward and fracturing the surface of the ice that blankets the river Listra. Though the unstable terrain is hazardous to traverse, the elder necromancer insists that they press on, saying that he hopes to catch the orcish fighters unprepared by arriving early in the year."
            {STORYTXT_BACKGROUND travel.jpg}
        [/part]
    [/story]

    {DID_TRACK {JOURNEY_04_NEW}}

    # wmllint: validate-off
    [side]
        side=1
        controller=human
        recruit=Walking Corpse,Vampire Bat,Ghost,Ghoul,Skeleton Archer,Skeleton
        {GOLD 170 140 110}
        team_name=good
        user_team_name= _ "Malin Keshar"
        {FLAG_VARIANT undead}

        # wmllint: recognize Malin Keshar
        {CHARACTER_STATS_MALIN_KESHAR}

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

    {STARTING_VILLAGES 1 5}

    [side]
        side=2
        controller=ai
        recruit=Orcish Archer,Orcish Crossbowman,Wolf Rider,Orcish Grunt
        {GOLD 20 40 60}
        {INCOME 2 4 6}
        team_name=orcs
        user_team_name= _ "Orcs"
        {FLAG_VARIANT6 ragged}

        type=Orcish Warlord
        id="Gron'r Hronk"
        name= _ "Gron’r Hronk"
        canrecruit=yes

        facing=nw

        [ai]
            aggression=0.5
            caution=0.1
            villages_per_scout=8
            village_value=0.5
            recruitment_pattern=scout,archer,archer,fighter
        [/ai]
    [/side]

    {STARTING_VILLAGES 2 5}

    [side]
        side=3
        controller=ai
        recruit=Orcish Archer,Orcish Crossbowman,Wolf Rider,Goblin Pillager,Orcish Grunt
        {GOLD 40 65 90}
        {INCOME 3 4 5}
        team_name=orcs
        user_team_name= _ "Orcs"
        {FLAG_VARIANT6 ragged}

        type=Orcish Warlord
        id="K'rrlar Oban"
        name=_ "K’rrlar Oban"
        canrecruit=yes

        facing=sw

        [ai]
            aggression=0.5
            caution=0.2
            villages_per_scout=6
            recruitment_pattern=archer,scout,fighter
        [/ai]
    [/side]

    {STARTING_VILLAGES 3 5}

    # Signpost at the end of the pass
    {PLACE_IMAGE scenery/signpost.png 17 1}

    [event]
        name=prestart

        {CLEAR_CORPSE_HORDE}

        [objectives]
            side=1
            [objective]
                condition=win
                description= _ "Move Malin to the end of the mountain pass"
            [/objective]
            [objective]
                {ALTERNATIVE_OBJECTIVE_CAPTION}
                condition=win
                description= _ "Kill the orc leaders"
            [/objective]
            [objective]
                condition=lose
                description= _ "Death of Malin Keshar"
            [/objective]
            [objective]
                condition=lose
                description= _ "Death of Darken Volk"
            [/objective]

            {TURNS_RUN_OUT}

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

        [lua]
            code=<<
-- 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 = 'Gd,Gd^Es,Gd^Em,Gd^Fmw,Gd^Fdw,Hh,Hh^Fdw,Hh^Fmw,Mm,Mm^Xm'}

local coverage = 0.25
-- Constant 25% random coverage.
-- Valid range is (0.0 .. 1.0)
-- Do not raise too high or player may not be able to make it in the time allotted.

local snowNeeded = (#locations * coverage)

local convert = {
    ['Gd'] = 'Aa',
    ['Gd^Es'] = 'Aa',
    ['Gd^Em'] = 'Aa^Vca',
    ['Gd^Fmw'] = 'Aa^Fma',
    ['Gd^Fdw'] = 'Aa^Fpa',
    ['Hh'] = 'Ha',
    ['Hh^Fdw'] = 'Ha^Fdw',
    ['Hh^Fmw'] = 'Ha^Fmw',
    ['Mm'] = 'Ms',
    ['Mm^Xm'] = 'Ms^Xm'
}

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]

        [recall]
            id=Darken Volk
        [/recall]

        {MODIFY_UNIT (id=Malin Keshar) profile (portraits/malin_old.png)}
        {MODIFY_UNIT (side=1)          facing   nw}
    [/event]

    [event]
        name=start

        [message]
            speaker=Darken Volk
            message= _ "Across this pass lies the eastern edge of the Heart Mountains proper. For their distinctive shape, the orcs call these the ‘Whitefang’ Mountains, which is also the namesake of the dominant clan in this region. They are the very same who killed your father."
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "The Whitefang orcs... so that is their name."
        [/message]

        [message]
            speaker=Darken Volk
            message= _ "To reach them, we must first destroy the orcs who block our way out of this mountain pass. Then, we can move on to our main target."
        [/message]

        [message]
            speaker=Malin Keshar
            message= _ "I have anticipated this day for a long time. At last, we shall have revenge."
        [/message]
    [/event]

    # In-scenario events
    [event]
        name=die
        first_time_only=yes
        [filter]
            race=orc
        [/filter]

        [filter_second]
            side=1
        [/filter_second]

        [message]
            speaker=Malin Keshar
            message=_ "Feel my wrath, vile creature!"
        [/message]
    [/event]

    [event]
        name=die
        [filter]
            id="Gron'r Hronk"
        [/filter]

        [filter_second]
            side=1
        [/filter_second]

        [message]
            speaker=Malin Keshar
            message=_ "These orcs aren’t so strong after all!"
        [/message]
    [/event]

    [event]
        name=last breath
        first_time_only=yes
        [filter]
            id="K'rrlar Oban"
        [/filter]

        [filter_second]
            side=1
        [/filter_second]

        [message]
            speaker="K'rrlar Oban"
            message=_ "Wait, human! I may be of use to you! What is it that you desire? Slaves? Gold? Land?"
        [/message]
        [message]
            speaker=Malin Keshar
            message=_ "What I desire is your death."
        [/message]

        [kill]
            id="K'rrlar Oban"
            animate=yes
            fire_event=yes
        [/kill]
    [/event]

    [event]
        name=moveto
        [filter]
            side=1

            [not]
                type_adv_tree=Ghost,Vampire Bat
            [/not]

            [filter_location]
                terrain=Ai
            [/filter_location]
        [/filter]

        [message]
            speaker=Malin Keshar
            message= _ "The ice doesn’t look very thick..."
        [/message]

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

        [allow_undo][/allow_undo]
    [/event]

    [event]
        name=new turn
        first_time_only=no

        [store_locations]
            [filter]
                [not]
                    type_adv_tree=Ghost,Vampire Bat
                [/not]
            [/filter]

            find_in=weakened_ice

            variable=breaking_ice
        [/store_locations]

        [foreach]
            array=breaking_ice
            [do]
                [scroll_to]
                    x,y=$this_item.x,$this_item.y
                [/scroll_to]

                [terrain]
                    x,y=$this_item.x,$this_item.y
                    terrain=Wo
                [/terrain]

                [remove_item]
                    x,y=$this_item.x,$this_item.y
                    image=misc/weakened-ice.png
                [/remove_item]

                [redraw][/redraw]

                [if]
                    [have_unit]
                        x,y=$this_item.x,$this_item.y
                        race=undead
                    [/have_unit]
                    [then]
                        [message]
                            speaker=narrator
                            image=wesnoth-icon.png
                            message= _ "The weak ice gives way beneath the undead creature, who becomes mired in the thick mud at the lake’s bottom."
                        [/message]
                    [/then]
                    [else]
                        [message]
                            speaker=narrator
                            image=wesnoth-icon.png
                            message=_ "The weak ice gives way beneath the heavy warrior, who drowns in the frigid mountain waters."
                        [/message]
                    [/else]
                [/if]

                [sound]
                    name=water-blast.wav
                [/sound]

                [kill]
                    x,y=$this_item.x,$this_item.y
                    animate=yes
                    fire_event=yes
                [/kill]
            [/do]
        [/foreach]

        [store_locations]
            terrain=Ai
            [filter]
                [not]
                    type_adv_tree=Ghost,Vampire Bat
                [/not]
            [/filter]

            [or]
                terrain=Ai

                find_in=weakened_ice
            [/or]

            variable=weakened_ice
        [/store_locations]

        [foreach]
            array=weakened_ice
            [do]
                [item]
                    x,y=$this_item.x,$this_item.y
                    image=misc/weakened-ice.png
                [/item]
            [/do]
        [/foreach]

        {CLEAR_VARIABLE breaking_ice}
    [/event]

    # Victory Condition
    [event]
        name=moveto
        [filter]
            id=Malin Keshar
            x,y=17,1
        [/filter]

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

    [event]
        name=enemies defeated

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

    [event]
        name=victory

        [message]
            speaker=Malin Keshar
            message=_ "It’s time to hunt down the Whitefangs."
        [/message]

        {CLEAR_VARIABLE weakened_ice}
    [/event]

    {HERODEATH_MALIN}
    {HERODEATH_VOLK}

    {TIME_OVER_DEFEAT}
[/scenario]
