#textdomain wesnoth-sota

# There is a lot of terrain dedicated to this map. There is a twinkling overlay, two
# versions of a stone gate, and a passable hex that looks very small to represent a
# narrow passage. The twinkling overlay is pretty generic, but for the others, no attempt
# has been made to make them work anywhere other than where they are drawn.

[scenario]
    name= _ "North Knalga"
    map_data="{campaigns/Secrets_of_the_Ancients/maps/20_North_Knalga.map}"

    id=20_North_Knalga
    next_scenario=21_Against_the_World

    [story]
        [part]
            story= _ "27 VI, 23 YW

We left the fires of the mountain behind and plunged once more into darkness. After a long trek, we were brought up short by a dead end in the tunnel... but the wall was not solid."
        [/part]
        {JOURNEY_PART 20}
    [/story]

    {UNDERGROUND}
    {DEFAULT_MUSIC_PLAYLIST}
    {TURNS 32 31 30}

    [side]
        {SIDE_1_BOTH}
        # The player should have a lot of gold after the last scenario, so:
        {GOLD 100 40 20}
        shroud=yes
    [/side]

    [side]
        side=2
        controller=ai
        user_team_name= _ "Dwarves"
        team_name=bad
        type=Dwarvish Lord
        name= _ "Golbanduth"
        id=Golbanduth
        {GOLD 600 800 1000}
        recruit=Dwarvish Fighter, Dwarvish Steelclad, Dwarvish Lord, Dwarvish Thunderguard, Dwarvish Pathfinder, Dwarvish Runesmith
        # The castle tiles are connected through two posts (stone walls), but that
        # means the AI could recruit on the posts. These two units sit on those
        # locations in order to prevent that. In prestart, they will be hidden and
        # petrified so they don't affect the gameplay.
        [unit]
            x,y=18, 26
            type=Dummy
        [/unit]
        [unit]
            x,y=22, 26
            type=Dummy
        [/unit]
        {FLAG_VARIANT knalgan}
        shroud=yes  # This is necessary for some sighted events.
    [/side]
    {STARTING_VILLAGES 2 10}

    {ANIMATED_MAUSOLEUM 33 4}
    {ANIMATED_MAUSOLEUM 33 6}
    {ANIMATED_MAUSOLEUM 33 8}

    # *************************** PRESTART ***************************
    [event]
        name=prestart

        [objectives]
            side=1
            [objective]
                description= _ "Defeat the enemy leader"
                condition=win
            [/objective]

            {HOW_TO_LOSE_WITH_BOTH}

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

            [note]
                description= _ "You can recruit from anywhere in the Hall of Heroes."
            [/note]
        [/objectives]

        [hide_unit]
            id=Ardonna
        [/hide_unit]

        [petrify]
            type=Dummy
        [/petrify]
        [hide_unit]
            type=Dummy
        [/hide_unit]

        # Add a few miners to the Crystal Caves for flavor. They will run away as the
        # player approaches, but could be used to level up a unit.
        [store_locations]
            x=3-25
            y=1-11
            terrain=!,X*
            variable=possible_dwarf_locations
        [/store_locations]

        [repeat]
            times=4
            [do]
                # Choose a location by selecting an array index:
                [set_variable]
                    name=location_index
                    rand="0..$($possible_dwarf_locations.length-1)"
                [/set_variable]

                [unit]
                    type=Dwarvish Miner
                    x=$possible_dwarf_locations[$location_index].x
                    y=$possible_dwarf_locations[$location_index].y
                    side=2
                [/unit]
            [/do]
        [/repeat]

        [clear_variable]
            name=possible_dwarf_locations, location_index, dwarf_index
        [/clear_variable]

        # Add one more miner who will have some dialog:
        [unit]
            type=Dwarvish Miner
            placement=map
            passable=yes
            x,y=25,12
            side=2
            id=spotter
            max_moves=4
        [/unit]

        # All miners run away from side 1 because they are not soldiers.
        [micro_ai]
            side=2
            action=add
            ai_type=coward
            distance=4
            attack_if_trapped=yes  # In case running away doesn't work.
            [filter]
                type=Dwarvish Miner
            [/filter]
        [/micro_ai]

        # Unit that will go get more gold for the leader.
        [unit]
            type=Dwarvish Berserker
            x,y=19,27
            id=porter
            side=2
        [/unit]

        # These two guys shouldn't move until the player has seen them.
        {FREEZE_UNIT porter}
        {FREEZE_UNIT Golbanduth}

        # We want the dwarves to reach the caves ahead of the player's units, but
        # player 1 gets a head start. To compensate for that, we will give side 2 an
        # initial castle-full of recruits. In effect, side 2 goes first. The player
        # doesn't see that because of the shroud, so it doesn't need to be explained.
        [repeat]
            times=11
            [do]
                {FIND_CASTLE_HEX 2 location}
                [set_variable]
                    name=dwarf_type
                    rand=Dwarvish Steelclad, Dwarvish Lord, Dwarvish Thunderguard, Dwarvish Pathfinder, Dwarvish Runesmith
                [/set_variable]
                [unit]
                    type=$dwarf_type
                    side=2
                    x=$location.x
                    y=$location.y
                [/unit]
            [/do]
        [/repeat]
        [clear_variable]
            name=dwarf_type, location
        [/clear_variable]

        [item]
            image=scenery/rune4.png
            x,y=32,15
        [/item]

        # ----- Hall of Heroes -----
        [item]
            image=items/coffin-dwarf.png~FL()
            x,y=36,2
        [/item]
        [item]
            image=items/coffin-dwarf.png
            x,y=30,2
        [/item]
        [item]
            image=items/coffin-dwarf.png~FL()
            x,y=36,4
        [/item]
        [item]
            image=items/coffin-dwarf.png
            x,y=30,4
        [/item]
        [item]
            image=items/coffin-dwarf.png~FL()
            x,y=36,6
        [/item]
        [item]
            image=items/coffin-dwarf.png
            x,y=30,6
        [/item]
        [item]
            image=items/coffin-dwarf.png~FL()
            x,y=36,8
        [/item]
        [item]
            image=items/coffin-dwarf.png
            x,y=30,8
        [/item]
        # These images are statues. They are scaled so they are larger than life.
        [item]
            image=units/dwarves/dragonguard/dragonguard-se-blade-defend1.png~GS()~SCALE(90,90)~CROP(0,20,60,60)
            x,y=31,3
        [/item]
        [item]
            image=units/dwarves/explorer-ranged-1.png~FL()~GS()~SCALE(90,90)~CROP(0,15,70,70)
            x,y=35,3
        [/item]
        [item]
            image=units/dwarves/lord.png~GS()~SCALE(90,90)~CROP(0,20,70,60)
            x,y=31,5
        [/item]
        [item]
            image=units/dwarves/dragonguard/dragonguard-s-ranged6.png~FL()~GS()~SCALE(90,90)~CROP(0,30,65,60)
            x,y=35,5
        [/item]
        [item]
            image=units/dwarves/arcanister.png~GS()~SCALE(90,90)~CROP(0,20,70,60)
            x,y=31,7
        [/item]
        [item]
            image=units/dwarves/sentinel.png~FL()~GS()~SCALE(90,90)~CROP(0,20,66,60)
            x,y=35,7
        [/item]
        [item]
            image=units/dwarves/explorer-melee-3.png~FL()~GS()~SCALE(90,90)~CROP(0,20,85,60)
            x,y=35,9
        [/item]
        [item]
            image=units/dwarves/lord-se-axe2.png~GS()~SCALE(90,90)~CROP(0,10,60,60)
            x,y=31,9
        [/item]

        # The statues are not passable, but we want the player to be able to *see*
        # behind them, so we'll remove the shroud in those hexes.
        [remove_shroud]
            side=1
            radius=1
            x,y=36,2
        [/remove_shroud]
        [remove_shroud]
            side=1
            radius=1
            x,y=30,2
        [/remove_shroud]
        [remove_shroud]
            side=1
            radius=1
            x,y=36,4
        [/remove_shroud]
        [remove_shroud]
            side=1
            radius=1
            x,y=30,4
        [/remove_shroud]
        [remove_shroud]
            side=1
            radius=1
            x,y=36,6
        [/remove_shroud]
        [remove_shroud]
            side=1
            radius=1
            x,y=30,6
        [/remove_shroud]
        [remove_shroud]
            side=1
            radius=1
            x,y=30,8
        [/remove_shroud]
        [remove_shroud]
            side=1
            radius=1
            x,y=36,8
        [/remove_shroud]

        # ----- Water Barrels -----
        [item]
            image=items/barrel.png
            x,y=39,23
        [/item]
        [item]
            image=items/barrel.png
            x,y=39,24
        [/item]
        [item]
            image=items/barrel.png
            x,y=38,23
        [/item]
        [item]
            image=items/barrel.png
            x,y=36,23
        [/item]

        # ----- Workshop -----
        [item]
            image=items/anvil.png
            x,y=4,33
        [/item]
        [item]
            image=items/anvil.png
            x,y=7,35
        [/item]

        [item]
            image=items/axe.png
            x,y=6,34
        [/item]
        [item]
            image=items/axe.png
            x,y=7,34
        [/item]
        [item]
            image=items/gold.png
            x,y=8,32
        [/item]
        [item]
            image=items/gold.png
            x,y=9,33
        [/item]
        [item]
            image=items/coal.png
            x,y=7,32
        [/item]

        # ----- Treasury -----
        [item]
            image=items/chest-open.png
            x,y=13,34
        [/item]
        [item]
            image=items/chest-open.png
            x,y=14,34
        [/item]
        [item]
            image=items/chest-open.png
            x,y=14,35
        [/item]
        [item]
            image=items/chest-open.png
            x,y=16,35
        [/item]
        [item]
            image=items/chest.png
            x,y=17,36
        [/item]
    [/event]

    # *************************** PLAY ***************************
    [event]
        # This event fires when the king sees a unit approaching the castle, and he
        # is alerted that he needs more units. We want the player to be able to see
        # the king while he talks about this, so that's what we're actually going to
        # check: That a player's unit can see the side-2 leader. We can't use
        # "sighted" for this because that only works when a unit is revealed for the
        # first time, and the king may have already been revealed by a shadow or other
        # hidden unit. (But, of course, the king wouldn't have been alerted in that
        # case.) We fire a custom event to have the king open another chest of gold.
        name=moveto
        first_time_only=no
        id=king_alerted_1

        [filter]
            side=1
        [/filter]
        [store_reachable_locations]
            [filter]
                id=$unit.id
                # These three units are hidden, so they don't count:
                [not]
                    type=Shadow, Nightgaunt
                [/not]
                [and]
                    [not]
                        type=Fugitive
                        [filter_location]
                            terrain=*^V*  # Hidden only in villages
                        [/filter_location]
                    [/not]
                [/and]
            [/filter]
            range=vision
            [filter_location]
                x,y=20,26
            [/filter_location]
            variable=can_see_the_king
        [/store_reachable_locations]

        [if]
            [variable]
                name=can_see_the_king.length
                greater_than=0
            [/variable]
            [then]
                [store_unit]
                    [filter]
                        id=$unit.id
                    [/filter]
                    variable=alerting_unit
                [/store_unit]
                [clear_variable]
                    name=can_see_the_king
                [/clear_variable]
                [fire_event]
                    name=get_more_gold
                [/fire_event]
            [/then]
            [else]
                # If a unit can see the king, this else won't be reached, so undo
                # won't work for that unit. To solve that, this whole event will
                # be removed by the get_more_gold event, and undo will only be
                # blocked on the one move that causes get_more_gold to fire.
                [allow_undo][/allow_undo]
            [/else]
        [/if]
    [/event]

    [event]
        # If a previously hidden unit becomes visible near the leader of side 2, we
        # want that to have the same effect as moving a unit close to him. This event
        # fires the same custom event as the event above.
        name=sighted
        id=king_alerted_2

        [filter]
            type=Shadow, Nightgaunt, Fugitive
            # If the fugitive is not keeping to villages, the event above will
            # fire first with the same effect.
            [filter_location]
                x,y=20,26
                # This is the sight distance of all the units involved:
                radius=8
            [/filter_location]
        [/filter]

        [store_unit]
            [filter]
                id=$unit.id
            [/filter]
            variable=alerting_unit
        [/store_unit]

        [fire_event]
            name=get_more_gold
        [/fire_event]
    [/event]

    [event]
        name=get_more_gold
        [remove_event]
            # We have to remove the first event to preserve the undo ability. It
            # shouldn't actually be necessary to remove the second event, but
            # we might as well.
            id=king_alerted_1, king_alerted_2
        [/remove_event]

        # Removing the shroud is necessary here because otherwise, it won't be
        # cleared until after the event is over, and the king may not be visible
        # while he talks.
        [store_reachable_locations]
            [filter]
                id=$alerting_unit.id
            [/filter]
            range=vision
            variable=clear_shroud
        [/store_reachable_locations]
        [clear_variable]
            name=alerting_unit
        [/clear_variable]
        [remove_shroud]
            side=1
            find_in=clear_shroud
        [/remove_shroud]
        [redraw][/redraw]
        [clear_variable]
            name=clear_shroud
        [/clear_variable]

        [message]
            speaker=Golbanduth
            message= _ "The invaders approach! Fetch the last chest o’ gold. We’re equippin’ more soldiers."
        [/message]

        [message]
            speaker=porter
            message= _ "Yes, sire."
        [/message]

        {THAW_UNIT porter}
        {THAW_UNIT Golbanduth}

        [move_unit]
            id=porter
            to_x=16
            to_y=33
        [/move_unit]
        [terrain]
            terrain=^Pr\o
            x,y=16,34
            layer=overlay
        [/terrain]

        # Now that the gate to the dwarves' treasury is open, the player might be
        # able to see into it. We will remove the shroud for all visible locations
        # in case that's true.
        [store_reachable_locations]
            [filter]
                side=1
            [/filter]
            range=vision
            variable=clear_shroud
        [/store_reachable_locations]
        [remove_shroud]
            side=1
            find_in=clear_shroud
        [/remove_shroud]
        [redraw][/redraw]
        [clear_variable]
            name=clear_shroud
        [/clear_variable]

        [move_unit]
            id=porter
            to_x=17
            to_y=36
        [/move_unit]
        [remove_item]
            x,y=17,36
        [/remove_item]
        [move_unit]
            id=porter
            to_x=18
            to_y=27
        [/move_unit]
        [item]
            image=items/chest-open.png
            x,y=19,27
        [/item]

        [gold]
            side=2
#ifdef EASY
            amount=300
#endif
#ifdef NORMAL
            amount=450
#endif
#ifdef HARD
            amount=600
#endif
        [/gold]
    [/event]
    # *************************** ENDING / OPENING ***************************

#define SHAKE X Y
    [scroll]
        x,y={X},{Y}
    [/scroll]
    [delay]
        time=150
    [/delay]
#enddef

#define BREAK_STATUE
    [animate_unit]
        [filter]
            id=Ras-Tabahn
        [/filter]
        flag=attack
        [primary_attack]
            range=ranged
        [/primary_attack]
        hits=yes
        [facing]
            x,y=31,9
        [/facing]
    [/animate_unit]
    [remove_item]
        x,y=31,9
    [/remove_item]
#enddef

    [event]
        name=start

        [teleport]
            [filter]
                id=Ardonna
            [/filter]
            x,y=33,1
            animate=no
            clear_shroud=yes
            check_passability=no
        [/teleport]
        [message]
            speaker=Ardonna
            message= _ "Actually, I don’t think this is the end. There is air coming through here. Help me shift these stones."
        [/message]
        [sound]
            name=cave-in.ogg
        [/sound]
        [delay]
            time=1000
        [/delay]
        [terrain]
            x,y=33,1
            terrain=Uu
        [/terrain]
        [unhide_unit]
            id=Ardonna
        [/unhide_unit]
        [delay]
            time=1000
        [/delay]
        [move_unit]
            id=Ardonna
            to_x=33
            to_y=9
        [/move_unit]

        [recall]
            id=Ras-Tabahn
            x,y=32,8  # We want him on a specific hex...
            show=no
        [/recall]
        [modify_unit]
            [filter]
                id=Ras-Tabahn
            [/filter]
            # ...facing a specific way.
            facing=nw
        [/modify_unit]
        {RECALL_LOYAL_UNITS}

        [terrain]
            x,y=31,9
            terrain=^Kov
            layer=overlay
        [/terrain]
        [message]
            speaker=Ardonna
            message= _ "Now, where are we?"
        [/message]
        [message]
            speaker=Ras-Tabahn
            message= _ "We are in a Dwarvish burial chamber it seems. That is good. We could always use some more soldiers."
        [/message]
        [move_unit_fake]
            type=Dwarvish Sentinel
            side=2
            x=31, 33
            y=17, 12
        [/move_unit_fake]
        [unit]
            x,y=33,12
            type=Dwarvish Sentinel
            side=2
            name= _ "Aigondur"
            id=Aigondur
        [/unit]
        [message]
            speaker=Aigondur
            message= _ "Human invaders be comin’ through the Hall o’ Heroes! I’m closin’ th’ Troll Gate!"
        [/message]
        [move_unit]
            id=Aigondur
            to_x=32
            to_y=15
        [/move_unit]

        [remove_item]
            image=scenery/rune4.png
            x,y=32,15
        [/remove_item]
        [item]
            image=scenery/rune4-glow.png
            x,y=32,15
        [/item]
        [redraw][/redraw]
        [delay]
            time=100
        [/delay]
        [terrain]
            x,y=33,15
            terrain=^Gytc
            layer=overlay
        [/terrain]
        [redraw][/redraw]
        [sound]
            name=explosion.ogg
        [/sound]
        {SHAKE 2 1}
        {SHAKE -1 -3}
        {SHAKE -3 1}
        {SHAKE 1 3}
        {SHAKE 1 -2}
        [delay]
            time=500
        [/delay]

        [message]
            speaker=Ardonna
            message= _ "Well, we are not going that way."
        [/message]
        [message]
            speaker=Ras-Tabahn
            message= _ "But look. There is a large crack in the wall to our left! I’m certain we could squeeze through."
        [/message]
        [message]
            speaker=Ras-Tabahn
            message= _ "We’ll just remove the statue..."
        [/message]

        {BREAK_STATUE}
        [item]
            image=items/debris1.png
            x,y=31,9
        [/item]
        [item]
            image=misc/blank-hex.png~BLIT(units/dwarves/lord-se-axe2.png~GS()~SCALE(90,90)~CROP(0,30,60,40),0,20)
            x,y=31,9
        [/item]

        {BREAK_STATUE}
        [item]
            image=items/debris2.png
            x,y=31,9
        [/item]
        [item]
            image=misc/blank-hex.png~BLIT(units/dwarves/lord-se-axe2.png~GS()~SCALE(90,90)~CROP(0,45,60,25),0,35)
            x,y=31,9
        [/item]

        {BREAK_STATUE}
        [item]
            image=items/debris3.png
            x,y=31,9
        [/item]
        [move_unit]
            id=Ras-Tabahn
            to_x=30
            to_y=8
        [/move_unit]
        [message]
            speaker=Ras-Tabahn
            message= _ "... and push the coffin out of the way."
        [/message]

        [remove_item]
            x,y=30,8
        [/remove_item]
        [item]
            image=items/coffin-dwarf-back.png
            x,y=30,8
        [/item]
        [terrain]
            x,y=29,9
            terrain=^
            layer=overlay
        [/terrain]

        # We want a "sighted" event for the first dwarf other than the guard who sees
        # a unit from side 1. Now that the guard can't see them anymore, we can add
        # this event. The next dwarf who sees a unit from side 1 will almost
        # certainly be the closest miner, so he has the dialog.
        [event]
            name=sighted
            [filter]
                side=1
            [/filter]
            # The other side will always be 2, so we don't need to filter for it.
            [message]
                speaker=spotter
                message= _ "Ach! Th’ invaders have made it inna the Crystal Caves!"
            [/message]
        [/event]
    [/event]

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

    {TURN_INTO_A_LICH}
    {HERO_DEATHS}
    {MANAGE_PROFILE}
    {KIDS_ADVANCE}
[/scenario]
