#textdomain wesnoth-sof
[scenario]
    name= _ "Closing the Gates"
    id=2_Closing_the_Gates
    turns=15
    map_data="{campaigns/Sceptre_of_Fire/maps/2_Closing_the_Gates.map}"
    next_scenario=2t_In_the_Dwarven_City
    victory_when_enemies_defeated=no

    {SCENARIO_MUSIC battle.ogg}
    {EXTRA_SCENARIO_MUSIC the_city_falls.ogg}

    {DEFAULT_SCHEDULE}

    [side]
        type=Dwarvish Fighter
        id=Rugnur
        side=1
        canrecruit=yes
        shroud=yes
        controller=human
        recruit=Dwarvish Fighter,Dwarvish Thunderer,Dwarvish Guardsman,Dwarvish Scout
        team_name=dwarves
        user_team_name= _ "Dwarves"
        {FLAG_VARIANT knalgan}
    [/side]

    # Give Rugnur control of all the non-boneyard villages inside the cave.
    # Trolls are going to take a couple of these fairly quickly.
    {STARTING_VILLAGES_AREA 1 14 11 3}
    {STARTING_VILLAGES_AREA 1 26 11 6}

    [side]
        type=Elvish Captain
        id=Glindur
        name= _ "Glindur"
        side=2
        canrecruit=yes
        {RECRUIT (Elvish Fighter,Elvish Scout,Elvish Archer,Elvish Shaman) (Elvish Fighter,Elvish Hero,Elvish Scout,Elvish Archer,Elvish Shaman) (Elvish Fighter,Elvish Hero,Elvish Scout,Elvish Archer,Elvish Ranger,Elvish Shaman)}
        {GOLD 200 250 300}
        team_name=elves
        user_team_name= _ "Elves"
        [ai]
            grouping=offensive
        [/ai]
        {FLAG_VARIANT long}
    [/side]

    [side]
        no_leader=yes
        side=3
        team_name=trolls
        user_team_name= _ "Trolls"
    [/side]

    [story]
        [part]
            story= _ "Next in our story, Rugnur retreated into the caves of Knalga. But the elves pressed on, and would enter the caves shortly."
        [/part]
        [part]
            show_title=yes
            {TO_CLOSING_THE_GATES}
        [/part]
    [/story]

    {SOF_DEATHS}

    [event]
        name=prestart

        # {SCATTER_IMAGE (terrain=Gg) 1 scenery/snowbits.png}

        [objectives]
            side=1
            [objective]
                description= _ "Place someone on each of the six magic glyphs in order to close the dwarvish gates"
                condition=win
            [/objective]
            [objective]
                description= _ "Kill all elves in the caves after the gates are closed"
                condition=win
            [/objective]
            [objective]
                description= _ "Death of Rugnur"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Baglur"
                condition=lose
            [/objective]

            {TURNS_RUN_OUT}

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

        [kill]
            side=1
            type=Caravan
        [/kill]

        # stores all the locations outside the gate into an array, so checks for
        # whether there are units still outside don't require maintaining a
        # static set of coordinates
        [store_locations]
            x,y=10,20
            radius=20

            [filter_radius]
                [not]
                    terrain=X*
                [/not]

                [not]
                    x=12,13,14
                    y=12,13,13
                [/not]

                [not]
                    x=2,21
                    y=6,24
                [/not]
            [/filter_radius]

            variable=locations_outside
        [/store_locations]

        [remove_shroud]
            find_in=locations_outside
            side=1
        [/remove_shroud]

        {GENERIC_UNIT 3 "Troll Whelp" 6 3}
        {GENERIC_UNIT 3 "Troll Whelp" 6 3}
        {GENERIC_UNIT 3 "Troll Whelp" 6 3}

        {GENERIC_UNIT 3 "Troll Whelp" 30 25}
        {GENERIC_UNIT 3 "Troll Whelp" 30 25}
        {GENERIC_UNIT 3 "Troll Whelp" 30 25}

#ifdef HARD
        {GENERIC_UNIT 3 "Troll Whelp" 30 25}
#endif
#ifdef NORMAL
        {GENERIC_UNIT 3 "Troll Whelp" 30 25}
#endif
    [/event]

    [event]
        name=start
        {MOVE_UNIT id=Rugnur 22 12}
        [recall]
            id=Alanin
        [/recall]
        [redraw]
            side=1
        [/redraw]
        [message]
            speaker=Rugnur
            message= _ "These elves are right behind me! We have to go warn the council of this attack...!"
        [/message]
        [message]
            speaker=Alanin
            message= _ "Here — I’ll go warn the council. You stay here and fight."
        [/message]
        [message]
            speaker=Rugnur
            message= _ "Yes, that sounds good. I’ll stay here and try to defend the gates..."
        [/message]
        {MOVE_UNIT id=Alanin 32 12}
        [store_unit]
            [filter]
                id=Alanin
            [/filter]
            variable=changealanin
            kill=yes
        [/store_unit]
        [message]
            speaker=Glindur
            message= _ "I see you have sent a messenger to the city. Good idea, but will it really help? We have more troops and more provisions, and we are going to enter those caves and kill you!"
        [/message]
        [message]
            speaker=Rugnur
            message= _ "(<i>Gulp</i>)"
        [/message]
        [message]
            speaker=Glindur
            message= _ "Ha! I thought as much. Well then, surrender!"
        [/message]
        [message]
            speaker=Rugnur
            message= _ "Um... on what terms?"
        [/message]
        [message]
            speaker=Glindur
            message= _ "Unconditional."
        [/message]

        {GENERIC_UNIT 1 "Dwarvish Guardsman" 10 13}
        {GENERIC_UNIT 1 "Dwarvish Guardsman" 14 15}
        {GENERIC_UNIT 1 "Dwarvish Guardsman" 12 11}
        {GENERIC_UNIT 1 "Dwarvish Guardsman" 15 13}

#ifdef EASY
        {GENERIC_UNIT 1 "Dwarvish Guardsman" 12 14}
#endif
        [unit]
            x,y=22,11
            type=Dwarvish Stalwart
            side=1
            id=Baglur
            name= _ "Baglur"
            {IS_HERO}
            profile=portraits/baglur.png
            facing=sw
            [modifications]
                {TRAIT_LOYAL}
                {TRAIT_RESILIENT}
            [/modifications]
        [/unit]

        [redraw]
            side=1
        [/redraw]

        [message]
            speaker=Baglur
            message= _ "Wait! Rugnur, dinna’ they tell you <i>anything</i> when you took command here?!"
        [/message]
        [message]
            speaker=Rugnur
            message= _ "Not really. This was supposedly a time of peace. Who are you?"
        [/message]
        [message]
            speaker=Baglur
            message= _ "Argh! I’m a retired warrior... it looks like I’ll ha’ to come out and help you, else we’ll be overrun by the damn elves... argh again. Well, listen here. This entrance to the city can be closed. There’s a gate. If we can close it, the elves will no’ be able to enter. And you elves; begone from here!"
        [/message]
        [message]
            speaker=Glindur
            message= _ "Cease your meddling, old dwarf! Rugnur is in charge here, and it is he who will bargain with us."
        [/message]
        [message]
            speaker=Rugnur
            message= _ "Baglur, how do you close the gates?"
        [/message]
        [message]
            speaker=Baglur
            message= _ "Och, well that’s the difficulty. It’s a magic gate. Indestructible. But that means it can only be closed by magic. Ye need to position a warrior on each of the six glyphs. When all are occupied, the gates wi’ close. Then the elves will be shut out of the caves forever, at least through this entrance."
        [/message]
        [message]
            speaker=Rugnur
            message= _ "Don’t we have troops positioned near these glyphs, to unlock them?"
        [/message]
        [message]
            speaker=Baglur
            message= _ "Och, well, no. Lately trolls have been more in charge of those caves where the glyphs are than we dwarves have."
        [/message]
        [message]
            speaker=Rugnur
            message= _ "Well, where are they located?"
        [/message]
        [message]
            speaker=Baglur
            message= _ "Two are down a side passage in the northwest, another two are in a similar location in the southeast, and two are right next to the front gate."
        [/message]
        [message]
            speaker=Glindur
            message= _ "Have you made up your mind yet about surrendering? If you will not give up, prepare for battle!"
        [/message]
        [message]
            speaker=Rugnur
            # wmllint: local spelling stonecraft
            message= _ "You may be more powerful than us, but I doubt even you can blast through dwarf-made stonecraft. We refuse — now let’s close these gates!"
        [/message]

        [set_variables]
            name=glyphs

            [value]
                x,y=9,11
                image_off=scenery/rune1.png
                image_on=scenery/rune1-glow.png
            [/value]
            [value]
                x,y=14,3
                image_off=scenery/rune2.png
                image_on=scenery/rune2-glow.png
            [/value]
            [value]
                x,y=30,19
                image_off=scenery/rune3.png
                image_on=scenery/rune3-glow.png
            [/value]
            [value]
                x,y=17,15
                image_off=scenery/rune4.png
                image_on=scenery/rune4-glow.png
            [/value]
            [value]
                x,y=3,5
                image_off=scenery/rune1.png
                image_on=scenery/rune1-glow.png
                toggles_x=2
                toggles_y=6
            [/value]
            [value]
                x,y=23,24
                image_off=scenery/rune2.png
                image_on=scenery/rune2-glow.png
                toggles_x=21
                toggles_y=24
            [/value]
        [/set_variables]

        {FOREACH glyphs i}
            [item]
                x,y=$glyphs[$i].x,$glyphs[$i].y
                image=$glyphs[$i].image_off
            [/item]
        {NEXT i}
    [/event]

    # These macros toggle a glyph on the given location on or off
#define GLYPH_ON X Y
    {FOREACH glyphs glyph_i}
        [if]
            [variable]
                name=glyphs[$glyph_i].x
                numerical_equals={X}
            [/variable]
            [variable]
                name=glyphs[$glyph_i].y
                numerical_equals={Y}
            [/variable]

            [then]
                [remove_item]
                    x,y={X},{Y}
                    image=$glyphs[$glyph_i].image_off
                [/remove_item]

                [item]
                    x,y={X},{Y}
                    image=$glyphs[$glyph_i].image_on
                [/item]

                [if]
                    [variable]
                        name=glyphs[$glyph_i].toggles_x
                        not_equals=$empty
                    [/variable]

                    [then]
                        [sound]
                            name=rumble.ogg
                        [/sound]

                        [terrain]
                            x,y=$glyphs[$glyph_i].toggles_x,$glyphs[$glyph_i].toggles_y
                            terrain=Xu
                        [/terrain]
                    [/then]
                [/if]
            [/then]
        [/if]
    {NEXT glyph_i}
#enddef
#define GLYPH_OFF X Y
    {FOREACH glyphs glyph_i}
        [if]
            [variable]
                name=glyphs[$glyph_i].x
                numerical_equals={X}
            [/variable]
            [variable]
                name=glyphs[$glyph_i].y
                numerical_equals={Y}
            [/variable]

            [then]
                [remove_item]
                    x,y={X},{Y}
                    image=$glyphs[$glyph_i].image_on
                [/remove_item]

                [item]
                    x,y={X},{Y}
                    image=$glyphs[$glyph_i].image_off
                [/item]

                [if]
                    [variable]
                        name=glyphs[$glyph_i].toggles_x
                        not_equals=$empty
                    [/variable]

                    [then]
                        [sound]
                            name=rumble.ogg
                        [/sound]

                        [terrain]
                            x,y=$glyphs[$glyph_i].toggles_x,$glyphs[$glyph_i].toggles_y
                            terrain=Cud
                        [/terrain]
                    [/then]
                [/if]
            [/then]
        [/if]
    {NEXT glyph_i}
#enddef

    # We reach the northern side entrance

    [event]
        name=moveto
        [filter]
            x,y=3,5
            side=1
        [/filter]
        [message]
            speaker=unit
            message= _ "But what about this entrance? The elves can come through it just as easily as the main gate, and it cannot be closed!"
        [/message]
        [message]
            speaker=Baglur
            message= _ "I think it can be sealed up somehow... Yes, look, activating that glyph seems to have closed up the gap."
        [/message]
    [/event]

    # We reach the southern side entrance

    [event]
        name=moveto
        [filter]
            x,y=23,24
            side=1
        [/filter]
        [message]
            speaker=unit
            message= _ "But what about this entrance? The elves can come through it just as easily as the main gate, and it cannot be closed!"
        [/message]
        [message]
            speaker=Baglur
            message= _ "I think it can be sealed up somehow... Yes, look, activating that glyph seems to have closed up the gap."
        [/message]
    [/event]

    # Check for all glyph activators in place

    [event]
        name=moveto
        first_time_only=no

        [filter]
            side=1

            [filter_location]
                find_in=glyphs
            [/filter_location]
        [/filter]

        [filter_condition]
            [variable]
                name=gate_closed
                boolean_equals=no
            [/variable]
        [/filter_condition]

        {GLYPH_ON $x1 $y1}

        [if]
            [have_unit]
                side=1

                [filter_location]
                    find_in=glyphs
                [/filter_location]

                count=6
            [/have_unit]

            [then]
                {VARIABLE gate_closed yes}

                [message]
                    speaker=Rugnur
                    message= _ "We have everyone positioned on the glyphs! What do we do now?"
                [/message]
                [message]
                    speaker=Baglur
                    message= _ "Just watch. The gates wi’ close very soon. Then the elves outside — and, unfortunately, our dwarves who are still out there — wi’ become irrelevant."
                [/message]

                [scroll_to]
                    x,y=13,13
                [/scroll_to]

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

                [terrain]
                    x=12,13,14
                    y=12,13,13
                    terrain=^Xo
                    layer=overlay
                [/terrain]

                [kill]
                    x=12,13,14
                    y=12,13,13
                    animate=yes
                    fire_event=yes
                [/kill]

                [delay]
                    time=2000
                [/delay]

                [message]
                    speaker=Glindur
                    message= _ "Agh! Well, you have defeated me for now, but eventually you will have to exit these caves, to give Haldric back his jewel. And when you do, we will be ready for you."
                [/message]
                [message]
                    speaker=Baglur
                    message= _ "If that is what ye choose to do, fine, but be prepared to wait for many years."
                [/message]

                [kill]
                    [filter_location]
                        find_in=locations_outside
                    [/filter_location]
                    side=1
                    fire_event=yes
                    animate=yes
                [/kill]

                [kill]
                    [filter_location]
                        find_in=locations_outside
                    [/filter_location]
                    side=2,3
                    fire_event=no
                [/kill]

                [place_shroud]
                    find_in=locations_outside
                    [not]
                        x=10,   11,   12,   13,   14
                        y=13,13-14,13-14,14-15,14-15
                    [/not]
                    side=1
                [/place_shroud]
                [if]
                    [have_unit]
                        side=2
                    [/have_unit]
                    [then]
                        [message]
                            speaker=Rugnur
                            message= _ "There are still some elves left in here. We have to eliminate them, too, unless they surrender."
                        [/message]
                        [message]
                            side=2
                            message= _ "You would have us be cowards! We do not surrender."
                        [/message]
                        [objectives]
                            side=1
                            [objective]
                                description= _ "Kill all elves in the caves"
                                condition=win
                            [/objective]
                            [objective]
                                description= _ "Death of Rugnur"
                                condition=lose
                            [/objective]
                            [objective]
                                description= _ "Death of Baglur"
                                condition=lose
                            [/objective]

                            {TURNS_RUN_OUT}

                            [gold_carryover]
                                bonus=yes
                                carryover_percentage=40
                            [/gold_carryover]
                        [/objectives]
                    [/then]
                    [else]
                        [message]
                            speaker=Rugnur
                            message= _ "Well, now I should go down to the city and report. I’m late already..."
                        [/message]
                        [endlevel]
                            result=victory
                            bonus=yes
                            {NEW_GOLD_CARRYOVER 40}
                        [/endlevel]
                    [/else]
                [/if]
            [/then]
            [else]
                [message]
                    speaker=unit
                    message= _ "My glyph is on."
                [/message]

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

    # Toggles a glyph off when the player steps off it
    [event]
        name=moveto
        first_time_only=no

        [filter]
            side=1

            [filter_location]
                [not]
                    find_in=glyphs
                [/not]
            [/filter_location]
        [/filter]

        [filter_condition]
            [variable]
                name=gate_closed
                boolean_equals=no
            [/variable]

            [have_location]
                x,y=$x2,$y2
                find_in=glyphs
            [/have_location]
        [/filter_condition]

        {GLYPH_OFF $x2 $y2}
    [/event]

    # Toggles a glyph off when the player unit on it dies
    [event]
        name=die
        first_time_only=no

        [filter]
            side=1

            [filter_location]
                find_in=glyphs
            [/filter_location]
        [/filter]

        [filter_condition]
            [variable]
                name=gate_closed
                boolean_equals=no
            [/variable]
        [/filter_condition]

        {GLYPH_OFF $x1 $y1}
    [/event]

    # This triggers either when the remaining elves in the caves after the gate
    # is closed are killed or when all elves are killed before the gate is
    # closed (unlikely)
    [event]
        name=die
        first_time_only=no

        [filter]
            side=2
        [/filter]

        [if]
            [not]
                [have_unit]
                    side=2

                    [not]
                        x,y=$x1,$y1
                    [/not]
                [/have_unit]
            [/not]

            [then]
                [message]
                    speaker=Rugnur
                    message= _ "That’s all of the elves. Now I should go down to the city and report. I’m late already..."
                [/message]

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

    [event]
        name=victory

        {CLEAR_VARIABLE glyphs,glyph_i,locations_outside,gate_closed}
    [/event]

    [event]
        name=die
        [filter]
            id=Baglur
        [/filter]
        [endlevel]
            result=defeat
        [/endlevel]
    [/event]

    # These handle the graphics for the gate. Normally, the gate is drawn in the
    # "down" position, and after the impassable overlay ^Xo has been placed on
    # the gate, an animated version will be drawn, displaying the gate closing.
    # The map= and images are drawn relative to 11,11.
    [terrain_graphics]
        x,y=11,11

        map="
*,  *,  *
, *,  *,  *
*,  *,  *
, 1,  *,  *
*,  2,  *
, *,  3,  *
*,  *,  *
, *,  *,  *"

        [tile]
            pos=1
            x,y=1,1
            type=!,*^Xo
        [/tile]
        [tile]
            pos=2
            x,y=2,2
            type=!,*^Xo
        [/tile]
        [tile]
            pos=3
            x,y=3,2
            type=!,*^Xo
        [/tile]

        [image]
            name=gate-left-down.png
            base=90,144
        [/image]
        [image]
            name=gate-middle-down.png
            base=144,180
        [/image]
        [image]
            name=gate-right-down.png
            base=196,216
        [/image]
    [/terrain_graphics]

    [terrain_graphics]
        x,y=11,11

        map="
*,  *,  *
, *,  *,  *
*,  *,  *
, 1,  *,  *
*,  2,  *
, *,  3,  *
*,  *,  *
, *,  *,  *"

        [tile]
            pos=1
            x,y=1,1
            type=*^Xo
        [/tile]
        [tile]
            pos=2
            x,y=2,2
            type=*^Xo
        [/tile]
        [tile]
            pos=3
            x,y=3,2
            type=*^Xo
        [/tile]

        # Technically, these animations loop, but only about once per 15 minutes
        # so the user should see the loop very rarely.
        [image]
            name=gate-left-rising-1.png:500,gate-left-rising-2.png:500,gate-left-rising-3.png:500,gate-left-up.png:50000,gate-left-up.png:50000,gate-left-up.png:50000,gate-left-up.png:50000
            base=90,144
        [/image]
        [image]
            name=gate-middle-rising-1.png:500,gate-middle-rising-2.png:500,gate-middle-rising-3.png:500,gate-middle-up.png:50000,gate-middle-up.png:50000,gate-middle-up.png:50000,gate-middle-up.png:50000
            base=144,180
        [/image]
        [image]
            name=gate-right-rising-1.png:500,gate-right-rising-2.png:500,gate-right-rising-3.png:500,gate-right-up.png:50000,gate-right-up.png:50000,gate-right-up.png:50000,gate-right-up.png:50000
            base=196,216
        [/image]
    [/terrain_graphics]
[/scenario]
