#textdomain wesnoth-sof
[scenario]
    name= _ "Gathering Materials"
    id=4_Gathering_Materials
    map_generation=lua
    next_scenario=4t_The_Jeweler

    [story]
        [part]
            story= _ "Those who went to the eastern mines were brave indeed. They were infested with trolls and other vile creatures, who thrived in the dark and gloom of the caves."
        [/part]
        [part]
            story= _ "And braver still were Rugnur and his companions, who had to spend two years in those tunnels. For mining is a lengthy business. But they could for the most part avoid the enemy. They only once had to venture into the very heart of the trolls’ territory."
        [/part]
    [/story]

    {SOF_TRACK {JOURNEY_04_NEW} }

    [generator]
        id="cavegen"
        config_name="Lua Cave Generator"
        create_map = << return wesnoth.require("cave_map_generator").generate_map(...) >>

        map_width=45
        map_height=45
        village_density=60

        terrain_wall = "Xu"
        terrain_clear = "Uu"
        terrain_keep = "Kud"
        terrain_castle = "Cud"
        terrain_village = "Uu^Vud"

        #the chamber with the player. Near the south-west corner
        [chamber]
            id=player
            x=10-15
            y=32-38
            size=7
            jagged=40
            [item_location]
                id = 1
                place_castle = yes
            [/item_location]
        [/chamber]

        #the chamber with the coal. Somewhere in the southeast.
        [chamber]
            id=coal1
            x=28-35
            y=28-35
            size=8
            [passage]
                chance=70
                width=1
                windiness=5
                destination=gold
            [/passage]
            [passage]
                chance=50
                width=2
                windiness=5
                destination=coal2
            [/passage]
            [passage]
                chance=50
                width=2
                windiness=5
                destination=gold
            [/passage]
            [passage]
                chance=50
                width=2
                windiness=10
                destination=coal2
            [/passage]
            [item_location]
                id = 2
                place_castle = yes
            [/item_location]
        [/chamber]

        #the northern chamber with only bad guys. Somewhere in the north-east.
        [chamber]
            id=gold
            x=28-35
            y=5-12
            size=8
            jagged=50
            [passage]
                chance=100
                width=2
                windiness=10
                destination=coal1
            [/passage]
            [passage]
                chance=100
                width=2
                windiness=10
                destination=coal2
            [/passage]
            [passage]
                chance=50
                width=1
                windiness=5
                destination=coal1
            [/passage]
            [passage]
                chance=50
                width=1
                windiness=5
                destination=coal2
            [/passage]
            [item_location]
                id = 3
                place_castle = yes
            [/item_location]
        [/chamber]

        #chamber with the second coal mine. Somewhere in the north-west.
        [chamber]
            id=coal2
            x=8-12
            y=8-12
            size=7
            [passage]
                chance=70
                width=1
                windiness=5
                destination=gold
            [/passage]
            [passage]
                chance=50
                width=2
                windiness=5
                destination=coal1
            [/passage]
            [passage]
                chance=50
                width=2
                windiness=5
                destination=gold
            [/passage]
            [passage]
                chance=50
                width=1
                windiness=5
                destination=coal1
            [/passage]
            [item_location]
                id = 4
                place_castle = yes
            [/item_location]
        [/chamber]

        #chamber with gold in it that connects to all the others
        [chamber]
            id=connector
            x=15-20
            y=15-20
            size=4
            #passages to both the troll's chamber and the player’s chamber
            [passage]
                destination=player
                width=2
                windiness=10
            [/passage]
            [passage]
                destination=coal1
                width=2
                windiness=10
            [/passage]
            [passage]
                destination=coal2
                width=2
                windiness=10
            [/passage]
            [passage]
                destination=gold
                width=2
                windiness=10
            [/passage]
        [/chamber]

        [chamber]
            id=exit_tunnel
            x=1
            y=38-43
            size=2
            [passage]
                destination=player
                width=1
                windiness=10
            [/passage]
        [/chamber]
    [/generator]

    [side]
        type=Dwarvish Fighter
        id=Rugnur
        side=1
        canrecruit=yes
        recruit=Dwarvish Fighter,Dwarvish Thunderer,Dwarvish Guardsman,Dwarvish Scout,Gryphon Rider,Dwarvish Miner
        controller=human
        shroud=yes
        {FLAG_VARIANT knalgan}
        team_name=dwarves
        user_team_name= _ "Dwarves"
    [/side]
    [side]
#ifdef HARD
        type="Troll Warrior"
#else
        type="Troll"
#endif
        side=2
        canrecruit=yes
        controller=ai
        [ai]
            leader_value=2
            village_value=0.2
            recruitment_pattern=fighter,fighter,mixed fighter

            [goal]
                name=target
                [criteria]
                    side=1
                [/criteria]
                value=2
            [/goal]
        [/ai]
        recruit=Troll Whelp,Goblin Spearman,Troll Rocklobber
        team_name=trolls1
        user_team_name=_"Trolls"
        {GOLD 75 125 175}
    [/side]
    [side]
#ifdef HARD
        type="Troll Warrior"
#else
        type="Troll"
#endif
        side=3
        canrecruit=yes
        controller=ai
        [ai]
            leader_value=2
            village_value=0.2
            recruitment_pattern=fighter

            [goal]
                name=target
                [criteria]
                    side=1
                [/criteria]
                value=2
            [/goal]
        [/ai]
        recruit=Troll Whelp,Goblin Spearman,Young Ogre,Ogre
        team_name=trolls2
        user_team_name=_"Trolls"
        {GOLD 75 125 175}
    [/side]
    [side]
#ifdef HARD
        type="Troll Warrior"
#else
        type="Troll"
#endif
        side=4
        canrecruit=yes
        controller=ai
        [ai]
            leader_value=2
            village_value=0.2
            recruitment_pattern=fighter

            [goal]
                name=target
                [criteria]
                    side=1
                [/criteria]
                value=2.0
            [/goal]
        [/ai]
        recruit=Goblin Spearman,Goblin Rouser,Goblin Impaler
        team_name=trolls3
        user_team_name=_"Trolls"
        {GOLD 75 125 175}
    [/side]

    {TURNS 40 37 34}
    victory_when_enemies_defeated=no

    {SCENARIO_MUSIC underground.ogg}
    {EXTRA_SCENARIO_MUSIC suspense.ogg}

    {UNDERGROUND}

    {SOF_DEATHS}

    # wmllint: recognize Rugnur

    {STARTING_VILLAGES 1 5}
    {STARTING_VILLAGES 2 8}
    {STARTING_VILLAGES 3 8}
    {STARTING_VILLAGES 4 8}

    {LIMIT_CONTEMPORANEOUS_RECRUITS 2 (Troll Rocklobber) 1}
    {LIMIT_CONTEMPORANEOUS_RECRUITS 3 (Ogre) 1}
    {LIMIT_CONTEMPORANEOUS_RECRUITS 4 (Goblin Rouser) 2}
    {LIMIT_CONTEMPORANEOUS_RECRUITS 4 (Goblin Impaler) 2}

    [event]
        name=prestart

        # Alanin and Krawg are taking a break
        # wmllint: recognize Alanin
        # wmllint: recognize Krawg

        [store_unit]
            [filter]
                id=Alanin
            [/filter]

            variable=alanin
            kill=yes
        [/store_unit]
        [store_unit]
            [filter]
                id=Krawg
            [/filter]

            variable=krawg
            kill=yes
        [/store_unit]

        [store_locations]
            [and]
                [filter]
                    side=1
                    canrecruit=yes
                [/filter]

                radius=1

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

            [not]
                [filter]
                    side=1
                    canrecruit=yes
                [/filter]
            [/not]

            variable=adjacent_cavewall
        [/store_locations]

        [foreach]
            array=adjacent_cavewall
            [do]
                [terrain]
                    x,y=$this_item.x,$this_item.y
                    terrain=Uu
                [/terrain]
            [/do]
        [/foreach]

        {CLEAR_VARIABLE adjacent_cavewall}

        [store_locations]
            [filter]
                side=1
                canrecruit=yes
            [/filter]

            radius=4

            [filter_radius]
                terrain=Cud,Kud
            [/filter_radius]

            variable=resource_return_locations
        [/store_locations]

        # Here we need to randomize the coal and gold locations by
        # hand, because the map generator can otherwise sometimes place
        # them inside cavewall.

        # The first coal pile is somewhere near where side 2 starts

        [store_locations]
            [filter]
                side=2
                canrecruit=yes
            [/filter]

            radius=5

            [filter_radius]
                terrain=!,Xu,Qxu
            [/filter_radius]

            variable=possible_coal_1_locations
        [/store_locations]

        {RANDOM 1..$possible_coal_1_locations.length}
        {VARIABLE_OP random sub 1}

        {VARIABLE coal_1.x $possible_coal_1_locations[$random].x}
        {VARIABLE coal_1.y $possible_coal_1_locations[$random].y}

        # The second coal pile is somewhere near where side 4 starts

        [store_locations]
            [filter]
                side=4
                canrecruit=yes
            [/filter]

            radius=5

            [filter_radius]
                terrain=!,Xu,Qxu
            [/filter_radius]

            variable=possible_coal_2_locations
        [/store_locations]

        {RANDOM 1..$possible_coal_2_locations.length}
        {VARIABLE_OP random sub 1}

        {VARIABLE coal_2.x $possible_coal_2_locations[$random].x}
        {VARIABLE coal_2.y $possible_coal_2_locations[$random].y}

        # And the gold pile is 8-12 hexes away from where side 3 starts

        [store_locations]
            [and]
                [filter]
                    side=3
                    canrecruit=yes
                [/filter]

                radius=12

                [filter_radius]
                    terrain=!,Xu,Qxu
                [/filter_radius]
            [/and]

            [not]
                [filter]
                    side=3
                    canrecruit=yes
                [/filter]

                radius=8

                [filter_radius]
                    terrain=!,Xu,Qxu
                [/filter_radius]
            [/not]

            variable=possible_gold_locations
        [/store_locations]

        {RANDOM 1..$possible_gold_locations.length}
        {VARIABLE_OP random sub 1}

        {VARIABLE gold_1.x $possible_gold_locations[$random].x}
        {VARIABLE gold_1.y $possible_gold_locations[$random].y}

        [item]
            image=items/gold.png
            x,y=$gold_1.x,$gold_1.y
        [/item]

        [item]
            image=items/coal.png
            x,y=$coal_1.x,$coal_1.y
        [/item]

        [item]
            image=items/coal.png
            x,y=$coal_2.x,$coal_2.y
        [/item]

        {VARIABLE coalin 0}
        {VARIABLE goldin 0}

        {CLEAR_VARIABLE possible_coal_1_locations}
        {CLEAR_VARIABLE possible_coal_2_locations}
        {CLEAR_VARIABLE possible_gold_locations}

        # Here we overlay a mask containing a rather random pattern of
        # suitable terrains on the map, because the map generator itself
        # only places the very basic terrains (floor, walls, etc)

        [terrain_mask]
            x,y=1,1
            mask="{campaigns/Sceptre_of_Fire/maps/4_Gathering_Materials-random.mask}"

            [rule]
                old=Uu
                new=Uu^Ii,Uu^Uf,Uh,Ww,Cud
            [/rule]

            [rule]
                old=Xu,Cud,Kud
                use_old=yes
            [/rule]
        [/terrain_mask]

        [objectives]
            side=1
            [objective]
                description= _ "Bring the necessary gold and coal to create the sceptre to the starting castle"
                condition=win
            [/objective]
            [objective]
                description= _ "1 load of gold and 2 loads of coal are needed"
                condition=win
            [/objective]
            [objective]
                description= _ "Death of Rugnur"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Thursagan"
                condition=lose
            [/objective]

            {TURNS_RUN_OUT}

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

    [event]
        name=start
        [recall]
            id=Baglur
        [/recall]
        [recall]
            id=Thursagan
        [/recall]
        [recall]
            id=Kinan
        [/recall]
        [recall]
            id=Rynan
        [/recall]
        [message]
            speaker=Rugnur
            message= _ "Well, these are the eastern mines. Trolls and ogres live here, be prepared to fight them; also be prepared to spend quite some time here — mining can take a while."
        [/message]
        [message]
            speaker=Thursagan
            message= _ "For me to make the artifact Haldric wants, I need a special type of gold. I do not know where it was found, but Baglur said these mines were the source of it."
        [/message]
        [message]
            speaker=Baglur
            message= _ "Also, the only coal that wi’ melt this gold is here."
        [/message]
        [message]
            speaker=Rugnur
            message= _ "So we’re down here to, what, mine this gold and coal? That should be easy enough."
        [/message]
        [message]
            speaker=Thursagan
            message= _ "Yes, although we will have to hire the miners — they don’t work for free. But beware, there are trolls and such down here..."
        [/message]
    [/event]

    # This reminder will be triggered either on the third turn, or when the
    # player is running out of gold for recruitment, whichever happens first.
    # The message will only be displayed once, because it's first_time_only=yes.
    [event]
        name=reminder_to_recruit_miners

        [filter_condition]
            [not]
                [have_unit]
                    type=Dwarvish Miner
                [/have_unit]
            [/not]
        [/filter_condition]

        [message]
            speaker=Thursagan
            message= _ "Don’t forget to recruit the miners. Our warriors can clear the tunnels of trolls and worse, but once we find the gold and coal we need the miners to acquire it."
        [/message]
    [/event]

    [event]
        name=turn 3

        [fire_event]
            name=reminder_to_recruit_miners
        [/fire_event]
    [/event]
    [event]
        name=recruit,recall
        first_time_only=no

        [filter]
            side=1
        [/filter]

        [store_gold]
            side=1
            variable=money_for_recruitment
        [/store_gold]
        [if]
            [variable]
                name=money_for_recruitment
                less_than=65
                # Two miners cost 38, the most expensive recruit is 24, and
                # upkeep will cost a bit, hence warn around about 65.
            [/variable]
            [then]
                [fire_event]
                    name=reminder_to_recruit_miners
                [/fire_event]
            [/then]
        [/if]
        {CLEAR_VARIABLE money_for_recruitment}
        [allow_undo][/allow_undo]
    [/event]

    [event]
        name=moveto

        [filter]
            x,y=$coal_1.x,$coal_1.y

            [or]
                x,y=$coal_2.x,$coal_2.y
            [/or]

            [and]
                side=1

                [not]
                    type=Dwarvish Miner
                [/not]
            [/and]
        [/filter]

        [message]
            speaker=unit
            message= _ "Here is some of the coal that we need! Bring the miners to take it!"
        [/message]

        [allow_undo][/allow_undo]
    [/event]

    [event]
        name=moveto

        [filter]
            x,y=$coal_1.x,$coal_1.y
            side=1
            type=Dwarvish Miner

            [not]
                role=has_coal
            [/not]

            [not]
                role=has_gold
            [/not]
        [/filter]

        [message]
            speaker=unit
            message= _ "I have all the coal I can carry..."
        [/message]
        {VARIABLE unit.role has_coal}
        [unstore_unit]
            variable=unit
            find_vacant=no
        [/unstore_unit]
        [unit_overlay]
            x,y=$x1,$y1
            image=misc/coal-icon.png
        [/unit_overlay]
        [remove_item]
            x,y=$x1,$y1
        [/remove_item]
        {VARIABLE coal_1.x 0}
        {VARIABLE coal_1.y 0}
    [/event]

    [event]
        name=moveto

        [filter]
            x,y=$coal_2.x,$coal_2.y
            side=1
            type=Dwarvish Miner

            [not]
                role=has_coal
            [/not]

            [not]
                role=has_gold
            [/not]
        [/filter]

        [message]
            speaker=unit
            message= _ "I have all the coal I can carry..."
        [/message]
        {VARIABLE unit.role has_coal}
        [unstore_unit]
            variable=unit
            find_vacant=no
        [/unstore_unit]
        [unit_overlay]
            x,y=$x1,$y1
            image=misc/coal-icon.png
        [/unit_overlay]
        [remove_item]
            x,y=$x1,$y1
        [/remove_item]
        {VARIABLE coal_2.x 0}
        {VARIABLE coal_2.y 0}
    [/event]

    [event]
        name=moveto

        [filter]
            x,y=$gold_1.x,$gold_1.y
            side=1

            [not]
                type=Dwarvish Miner
            [/not]
        [/filter]

        [message]
            speaker=unit
            message= _ "Here is the mine of precious gold! Send the miners this way."
        [/message]

        [allow_undo][/allow_undo]
    [/event]

    [event]
        name=moveto

        [filter]
            x,y=$gold_1.x,$gold_1.y
            side=1
            type=Dwarvish Miner

            [not]
                role=has_coal
            [/not]

            [not]
                role=has_gold
            [/not]
        [/filter]

        [message]
            speaker=unit
            message= _ "I have all the gold I can carry..."
        [/message]
        {VARIABLE unit.role has_gold}
        [unstore_unit]
            variable=unit
            find_vacant=no
        [/unstore_unit]
        [unit_overlay]
            x,y=$x1,$y1
            image=misc/gold-icon.png
        [/unit_overlay]
        [remove_item]
            x,y=$x1,$y1
        [/remove_item]
        {VARIABLE gold_1.x 0}
        {VARIABLE gold_1.y 0}
    [/event]

    # If a miner carrying coal dies, we let others pick it up

    [event]
        name=die
        first_time_only=no

        [filter]
            side=1
            type=Dwarvish Miner
            role=has_coal
        [/filter]

        [item]
            x,y=$x1,$y1
            image=items/coal.png
        [/item]

        {VARIABLE hex_$x1|_$y1|_coal yes}
    [/event]

    [event]
        name=moveto
        first_time_only=no

        [filter]
            side=1
            type=Dwarvish Miner

            [not]
                role=has_coal
            [/not]

            [not]
                role=has_gold
            [/not]
        [/filter]

        [if]
            [variable]
                name=hex_$x1|_$y1|_coal
                equals=yes
            [/variable]

            [then]
                [message]
                    speaker=unit
                    message= _ "I have all the coal I can carry..."
                [/message]

                {VARIABLE unit.role has_coal}

                [unstore_unit]
                    variable=unit
                    find_vacant=no
                [/unstore_unit]

                [unit_overlay]
                    x,y=$x1,$y1
                    image=misc/coal-icon.png
                [/unit_overlay]

                [remove_item]
                    x,y=$x1,$y1
                [/remove_item]

                {CLEAR_VARIABLE hex_$x1|_$y1|_coal}
            [/then]

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

    # Exactly same as above, but for gold

    [event]
        name=die
        first_time_only=no

        [filter]
            side=1
            type=Dwarvish Miner
            role=has_gold
        [/filter]

        [item]
            x,y=$x1,$y1
            image=items/gold.png
        [/item]

        {VARIABLE hex_$x1|_$y1|_gold yes}
    [/event]

    [event]
        name=moveto
        first_time_only=no

        [filter]
            side=1
            type=Dwarvish Miner

            [not]
                role=has_coal
            [/not]

            [not]
                role=has_gold
            [/not]
        [/filter]

        [if]
            [variable]
                name=hex_$x1|_$y1|_gold
                equals=yes
            [/variable]

            [then]
                [message]
                    speaker=unit
                    message= _ "I have all the gold I can carry..."
                [/message]

                {VARIABLE unit.role has_gold}

                [unstore_unit]
                    variable=unit
                    find_vacant=no
                [/unstore_unit]

                [unit_overlay]
                    x,y=$x1,$y1
                    image=misc/gold-icon.png
                [/unit_overlay]

                [remove_item]
                    x,y=$x1,$y1
                [/remove_item]

                {CLEAR_VARIABLE hex_$x1|_$y1|_gold}
            [/then]

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

    [event]
        name=moveto
        first_time_only=no
        [filter]
            type=Dwarvish Miner
            role=has_coal

            [filter_location]
                find_in=resource_return_locations
            [/filter_location]
        [/filter]
        [message]
            speaker=unit
            message= _ "My load of coal is delivered!"
        [/message]

        {CLEAR_VARIABLE unit.role}

        [unstore_unit]
            variable=unit
            find_vacant=no
        [/unstore_unit]

        [remove_unit_overlay]
            x,y=$x1,$y1
            image=misc/coal-icon.png
        [/remove_unit_overlay]

        [set_variable]
            name=coalin
            add=1
        [/set_variable]

        [if]
            [variable]
                name=coalin
                greater_than_equal_to=2
            [/variable]
            [then]
                [message]
                    speaker=unit
                    message= _ "That’s the last load of coal we need."
                [/message]
                [if]
                    [variable]
                        name=goldin
                        greater_than_equal_to=1
                    [/variable]
                    [then]
                        [message]
                            speaker=Thursagan
                            message= _ "This is all we need from these mines. Now we should go back further west, where there are no trolls and ogres, and mine there."
                        [/message]

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

    [event]
        name=moveto
        first_time_only=no
        [filter]
            type=Dwarvish Miner
            role=has_gold

            [filter_location]
                find_in=resource_return_locations
            [/filter_location]
        [/filter]
        [message]
            speaker=unit
            message= _ "Here’s the gold."
        [/message]

        {CLEAR_VARIABLE unit.role}

        [unstore_unit]
            variable=unit
            find_vacant=no
        [/unstore_unit]

        [remove_unit_overlay]
            x,y=$x1,$y1
            image=misc/gold-icon.png
        [/remove_unit_overlay]

        [set_variable]
            name=goldin
            add=1
        [/set_variable]

        [if]
            [variable]
                name=goldin
                greater_than_equal_to=1
            [/variable]
            [then]
                [if]
                    [variable]
                        name=coalin
                        greater_than_equal_to=2
                    [/variable]
                    [then]
                        [message]
                            speaker=Thursagan
                            message= _ "This is all we need from these mines. Now we should go back further west, where there are no trolls and ogres, and mine there."
                        [/message]

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

    [event]
        name=victory

        [unstore_unit]
            variable=alanin
        [/unstore_unit]

        [unstore_unit]
            variable=krawg
        [/unstore_unit]

        {CLEAR_VARIABLE alanin}
        {CLEAR_VARIABLE krawg}

        {CLEAR_VARIABLE coalin}
        {CLEAR_VARIABLE goldin}
        {CLEAR_VARIABLE coal_1}
        {CLEAR_VARIABLE coal_2}
        {CLEAR_VARIABLE gold_1}
        {CLEAR_VARIABLE resource_return_locations}

        [kill]
            type=Dwarvish Miner
        [/kill]

        [disallow_recruit]
            side=1
            type=Dwarvish Miner
        [/disallow_recruit]
    [/event]
[/scenario]
