#textdomain wesnoth-httt
[scenario]
    id=17_Sceptre_of_Fire
    name= _ "The Sceptre of Fire"
    next_scenario=18_A_Choice_Must_Be_Made
    map_generation=lua
    [generator]
        id="cavegen"
        config_name="Lua Cave Generator"
        create_map = << return wesnoth.require("cave_map_generator").generate_map(...) >>

        map_width=50
        map_height=70
        village_density=20
        terrain_wall = "Xu"
        terrain_clear = "Uu"
        terrain_keep = "Kud"
        terrain_castle = "Cud"
        terrain_village = "Uu^Vud"

        transform=flip_x
        transform_chance=50

        #
        # The chamber with the player. Somewhere in the far south
        #
        [chamber]
            id=player
            x=15-35
            y=65
            size=8
            jagged=50
            [item_location]
                id = 1
                place_castle = yes
            [/item_location]
        [/chamber]

        #
        # The antechambers
        #
        [chamber]
            id=antechamber_1
            x=10-25
            y=50-60
            size=7
            jagged=5
            {PASSAGE_NORMAL player 2 10 10}
            [item_location]
                id = 2
                place_castle = yes
            [/item_location]
        [/chamber]
        [chamber]
            id=antechamber_2
            x=25-40
            y=50-60
            size=7
            jagged=2
            {PASSAGE_NORMAL player 2 3 1}
            {PASSAGE_CHANCE 40 antechamber_1 1 9 9}
            [item_location]
                id = 3
                place_castle = yes
            [/item_location]
        [/chamber]

        #
        # The central volcano
        #
        [chamber]
            id=center
            x=25-26
            y=35-36
            size=2
            jagged=1
            {PASSAGE_NORMAL antechamber_1 1 20 3}
            {PASSAGE_NORMAL antechamber_2 1 20 3}
        [/chamber]

        #
        # The 5 minichambers encircling the central volcano and a 6th chamber
        # to serve as the exit
        #
        [chamber]
            id=mini_1
            x=10-16
            y=36-40
            size=5
            jagged=2
            {PASSAGE_NORMAL center 1 5 2}
            {PASSAGE_NORMAL antechamber_1 2 5 2}
        [/chamber]
        [chamber]
            id=mini_2
            x=8-20
            y=17-26
            size=5
            jagged=3
            {PASSAGE_NORMAL center 1 5 2}
            {PASSAGE_NORMAL mini_1 1 5 2}
        [/chamber]
        [chamber]
            id=mini_3
            x=6-44
            y=14-30
            size=3
            jagged=4
            {PASSAGE_NORMAL center 1 5 2}
            {PASSAGE_NORMAL mini_2 2 5 2}
            [item_location]
                id = sceptre
            [/item_location]
        [/chamber]
        [chamber]
            id=mini_4
            x=30-42
            y=17-26
            size=5
            jagged=5
            {PASSAGE_NORMAL center 1 5 2}
            {PASSAGE_NORMAL mini_3 2 5 2}
        [/chamber]
        [chamber]
            id=mini_5
            x=34-40
            y=36-40
            size=5
            jagged=5
            {PASSAGE_NORMAL mini_4 2 5 2}
            {PASSAGE_NORMAL center 1 5 2}
            {PASSAGE_NORMAL antechamber_2 2 5 2}
        [/chamber]
        [chamber]
            id=exit
            x=25
            y=1
            size=2
            jagged=1
            {PASSAGE_NORMAL mini_2 1 5 2}
            {PASSAGE_NORMAL mini_3 1 5 2}
            {PASSAGE_NORMAL mini_4 1 5 2}
        [/chamber]

        #
        # There are 4 chambers for enemies randomly scattered on the map
        #
        [chamber]
            id=enemy_1
            x=6-15
            y=45-55
            size=5
            jagged=3
            {PASSAGE_NORMAL mini_1 2 5 2}
            {PASSAGE_CHANCE 60 mini_2 1 5 2}
            {PASSAGE_CHANCE 40 antechamber_2 1 5 2}
            [item_location]
                id = 4
                place_castle = yes
            [/item_location]
        [/chamber]
        [chamber]
            id=enemy_2
            x=6-15
            y=1-35
            size=5
            jagged=3
            {PASSAGE_NORMAL mini_2 2 5 2}
            {PASSAGE_NORMAL enemy_1 2 5 5}
            {PASSAGE_CHANCE 60 mini_1 1 5 2}
            {PASSAGE_CHANCE 40 mini_3 1 5 2}
            [item_location]
                id = 5
                place_castle = yes
            [/item_location]
        [/chamber]
        [chamber]
            id=enemy_3
            x=35-45
            y=1-35
            size=5
            jagged=3
            {PASSAGE_NORMAL mini_4 2 5 2}
            {PASSAGE_CHANCE 60 mini_5 1 5 2}
            {PASSAGE_CHANCE 40 mini_3 1 5 2}
            [item_location]
                id = 6
                place_castle = yes
            [/item_location]
        [/chamber]
        [chamber]
            id=enemy_4
            x=35-45
            y=45-55
            size=5
            jagged=3
            {PASSAGE_NORMAL mini_5 2 5 2}
            {PASSAGE_NORMAL enemy_3 2 5 5}
            {PASSAGE_CHANCE 60 mini_4 1 5 2}
            {PASSAGE_CHANCE 40 antechamber_2 1 5 2}
            [item_location]
                id = 7
                place_castle = yes
            [/item_location]
        [/chamber]
    [/generator]

    #
    # If you think this is laying it on a bit thick, I'm open to
    # suggestions for better wording or something less
    # melodramatic.
    #
    [story]
        [part]
            story= _ "Across the chasm, the air almost crackled with magical energy. It also became steadily warmer. The floor was smooth and glassy in places, and a faint glow provided a small reprieve from the thick blackness. Volcanic fumes drifted up from cracks in the floor."
            show_title=no
        [/part]
        [part]
            story= _ "Distant rumbles and earthquakes made it difficult to keep steady footing. The very earth had come alive, heaving, struggling to be relieved of its centuries-old burden..."
            show_title=no
        [/part]
    [/story]
    {HTTT_TRACK {JOURNEY_17_NEW} }

    {TURNS 50 45 35}
    victory_when_enemies_defeated=no

    #
    # This is a custom schedule that gives everything a slight
    # reddish hue and gently pulsates. -30, -40, -40 are default
    # values that look good
    #
    {UNDERGROUND_VOLCANO -27 -37 -37}
    {UNDERGROUND_VOLCANO -28 -38 -38}
    {UNDERGROUND_VOLCANO -32 -42 -42}
    {UNDERGROUND_VOLCANO -36 -46 -46}
    {UNDERGROUND_VOLCANO -37 -47 -47}
    {UNDERGROUND_VOLCANO -36 -46 -46}
    {UNDERGROUND_VOLCANO -32 -42 -42}
    {UNDERGROUND_VOLCANO -28 -38 -38}

    # wmllint: recognize Konrad

    {SCENARIO_MUSIC "the_deep_path.ogg"}
    {EXTRA_SCENARIO_MUSIC "knalgan_theme.ogg"}
    {EXTRA_SCENARIO_MUSIC "heroes_rite.ogg"}
    {EXTRA_SCENARIO_MUSIC "underground.ogg"}
    {EXTRA_SCENARIO_MUSIC "into_the_shadows.ogg"}

    [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
        shroud=yes
        gold=200
        {FLAG_VARIANT long}
    [/side]
    [side]
        type=Goblin Rouser
        side=2
        canrecruit=yes
        controller=ai
        recruit=Goblin Spearman
        team_name=orcs
        user_team_name=_"Orcs"
        gold=70
        [ai]
            aggression=1.0
            village_value=0.0
            leader_value=50.0
            villages_per_scout=0
        [/ai]
    [/side]
    [side]
        type=Goblin Rouser
        side=3
        canrecruit=yes
        controller=ai
        recruit=Goblin Spearman
        team_name=orcs
        user_team_name=_"Orcs"
        gold=70
        [ai]
            aggression=1.0
            village_value=0.0
            leader_value=50.0
            villages_per_scout=0
        [/ai]
        {FLAG_VARIANT6 ragged}
    [/side]
    [side]
        type=Orcish Warlord
        side=4
        canrecruit=yes
        controller=ai
        [ai]
            leader_value=10
            {ATTACK_DEPTH 5 5 6}
        [/ai]
        recruit=Wolf Rider,Orcish Grunt,Orcish Archer
        team_name=orcs
        user_team_name=_"Orcs"

        {GOLD 30 40 50}
        {INCOME 2 4 6}
        {FLAG_VARIANT6 ragged}
    [/side]
    [side]
        type=Troll Warrior
        side=5
        canrecruit=yes
        controller=ai
        [ai]
            {ATTACK_DEPTH 5 5 6}
            leader_value=10
            recruitment_pattern=fighter
        [/ai]
#ifdef EASY
        recruit=Troll Whelp,Ogre
#else
        recruit=Troll Whelp,Troll,Troll Warrior,Ogre
#endif
        team_name=orcs
        user_team_name=_"Orcs"
        {GOLD 30 40 50}
        {INCOME 2 4 6}
    [/side]
    [side]
        type=Orcish Warlord
        side=6
        canrecruit=yes
        controller=ai
        [ai]
            {ATTACK_DEPTH 5 5 6}
            leader_value=10
            recruitment_pattern=fighter,fighter,scout
        [/ai]
#ifdef EASY
        recruit=Ogre,Goblin Knight,Wolf Rider,Troll Whelp
#else
        recruit=Troll,Ogre,Goblin Knight,Wolf Rider,Troll Whelp,Saurian Skirmisher
#endif
        team_name=orcs
        user_team_name=_"Orcs"

        {GOLD 10 20 30}
        {INCOME 2 4 6}
    [/side]
    [side]
        type=Orcish Warlord
        side=7
        canrecruit=yes
        controller=ai
        [ai]
            {ATTACK_DEPTH 5 5 6}
            leader_value=10
            recruitment_pattern=fighter,fighter,scout
        [/ai]
#ifdef EASY
        recruit=Wolf Rider,Goblin Impaler
#else
        recruit=Goblin Knight,Wolf Rider,Goblin Spearman,Goblin Impaler,Saurian Skirmisher
#endif
        team_name=orcs
        user_team_name=_"Orcs"
        {GOLD 20 30 40}
        {INCOME 2 4 6}
        {FLAG_VARIANT6 ragged}
    [/side]

    [event]
        name=prestart

        {VARIABLE moved_too_close no}
        [objectives]
            side=1
            [objective]
                description= _ "Capture the Sceptre of Fire with Konrad or Li’sar"
                condition=win
            [/objective]
            [objective]
                description= _ "Death of Konrad"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Delfador"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Li’sar"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Kalenz"
                condition=lose
            [/objective]

            {TURNS_RUN_OUT}

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

            [note]
                [show_if]
                    {VARIABLE_CONDITIONAL moved_too_close boolean_equals no}
                [/show_if]
                description= _ "If Delfador rests, he can concentrate on the location of the Sceptre of Fire."
            [/note]
        [/objectives]

        {SOF_TERRAIN_MASK}
        #
        # Except on hard, you only have to face 4 enemies
        # instead of 5. This part randomly kills 1 or 2 of the
        # 6 leaders.
        #
#ifdef HARD
#else
        {VARIABLE_OP side_kill rand "4..7"}
        {ERASE_CASTLE $side_kill Uu}
        [kill]
            side=$side_kill
        [/kill]
        [modify_side]
            side=$side_kill
            hidden=yes
        [/modify_side]
#endif
        {VARIABLE_OP side_kill rand "2..3"}
        {ERASE_CASTLE $side_kill Uu}
        [kill]
            side=$side_kill
        [/kill]
        [modify_side]
            side=$side_kill
            hidden=yes
        [/modify_side]
        {CLEAR_VARIABLE side_kill}

        # just making sure that the starting castle is big enough that
        # no recalled units can end up inside walls
        [store_locations]
            [and]
                [filter]
                    id=Konrad
                [/filter]

                radius=1
            [/and]

            [not]
                terrain=C*,K*
            [/not]

            variable=adjacent_to_starting_loc
        [/store_locations]

        [foreach]
            array=adjacent_to_starting_loc
            [do]
                [terrain]
                    x,y=$this_item.x,$this_item.y
                    terrain=Cud
                [/terrain]
            [/do]
        [/foreach]

        {CLEAR_VARIABLE adjacent_to_starting_loc}
    [/event]

    [event]
        name=prestart
        #
        # Initialize the lava growing effect
        #
        {NEXT_LAVA}
        {CLEAR_VARIABLE sceptre}
        {NEED_DELFADOR placement=leader}
        {NEED_KALENZ placement=leader}
        {NEED_LISAR placement=leader}
    [/event]

    [event]
        name=start
        [message]
            speaker=Konrad
            message= _ "The Sceptre must be getting close now! Where shall we go?"
        [/message]
        [message]
            speaker=Kalenz
            message= _ "We cannot explore this much area by ourselves, especially if we are beset by the denizens of these caves. We must ask our dwarven allies to help search the caves with us."
        [/message]
        [message]
            speaker=Delfador
            message= _ "Yes, I feel it is near here! We must search for it carefully."
        [/message]
        {QUAKE "rumble.ogg"}
        {QUAKE "rumble.ogg"}
        [message]
            speaker="Li'sar"
            message= _ "An earthquake! We’ll be trapped!"
        [/message]
        [message]
            speaker=Kalenz
            message= _ "No, not an earthquake, but I feel it too. Something is... aware... of us."
        [/message]
        [message]
            speaker="Li'sar"
            message= _ "Aware? Who? Delfador, what does he mean?"
        [/message]
        [message]
            speaker=Delfador
            message= _ "The Sceptre... the earth... the molten lava... the fire... the air around us... everything. It calls out to the heir — I can barely hold it back in my mind. It was forged here, not far north of where we now stand. The sheer power of it!"
        [/message]
        {QUAKE "rumble.ogg"}
        [message]
            speaker=Konrad
            message= _ "Where? Where is it?"
        [/message]
        [message]
            speaker=Delfador
            message= _ "North. More than that, I cannot tell."
        [/message]
        [message]
            speaker="Li'sar"
            message= _ "Konrad, as promised, here is my purse, full of gold."
        [/message]
        [sound]
            name=gold.ogg
        [/sound]
        [message]
            speaker=narrator
            image="wesnoth-icon.png"
            message= _ "You receive 300 pieces of gold!"
        [/message]
        [gold]
            side=1
            amount=300
        [/gold]
        [store_locations]
            terrain=Xu,Qxu,Ql
            [and]
                location_id="sceptre"
                radius=3
            [/and]
            [or]
                location_id="sceptre"
                radius=1
            [/or]
            variable=sceptre_vicinity
        [/store_locations]
        [foreach]
            array=sceptre_vicinity
            [do]
                [terrain]
                    x=$this_item.x
                    y=$this_item.y
                    terrain=Uu
                [/terrain]
            [/do]
        [/foreach]
        {CLEAR_VARIABLE sceptre_vicinity}
        [terrain]
            location_id="sceptre"
            terrain=Uu^Ii
        [/terrain]

        [message]
            speaker=Delfador
            message= _ "I think... I think if I were to concentrate a bit I could tell you where it is."
        [/message]
        [message]
            speaker=narrator
            image="wesnoth-icon.png"
            message= _ "If Delfador rests, he can concentrate on the location of the Sceptre of Fire."
        [/message]
    [/event]

    [event]
        name=side 1 turn end
        first_time_only=no

        [store_unit]
            [filter]
                id=Delfador
            [/filter]
            kill=no
            variable=delfador_moves
        [/store_unit]

        [if]
            {VARIABLE_CONDITIONAL delfador_moves.moves numerical_equals $delfador_moves.max_moves}
            {VARIABLE_CONDITIONAL moved_too_close boolean_equals no}
            [then]
                [floating_text]
                    [filter]
                        id=Delfador
                    [/filter]
                    text= _ "Concentrating"
                [/floating_text]
                {VARIABLE concentrating yes}
            [/then]
            [else]
                {VARIABLE concentrating no}
            [/else]
        [/if]

        {CLEAR_VARIABLE delfador_moves}
    [/event]

    # Introductory dialog to give the player a hint that she may lose units to
    # {EXPAND_LAVA}.
    [event]
        name=turn 2
        # The noise was played by {EXPAND_LAVA}.
        [message]
            id=Konrad
            {KONRAD_VARIATION concerned}
            message=_ "That noise was made by no troll! What manner of beast makes its den in these forsaken caves?"
        [/message]
        [message]
            race=merman
            # po: Comic relief
            message=_ "I wouldn't know, milord. Without a river running though it, such hot, dry places are anathema to my kind. Whatever monsters may abide in these depths, are known not to my kin."
        [/message]
        [message]
            race=merman
            # po: Comic relief
            message=_ "Nor do I know, my most eccentric lord, what I am doing in a cave. No Mer has yet entered the caves under the mountain and lived to tell the tale. I would gladly die for you, milord, but I would that you didn't spend my life for nothing, looking for another dwarf to rescue when there is none."
        [/message]
        [if]
            [have_unit]
                race=dwarf
            [/have_unit]
            [then]
                # A denizen of caves answers Konrad's question.
                [message]
                    race=dwarf
                    # po: "sowl" is a rare word synonymous with "sully", meaning "contaminate".
                    # po: "toiled" means "difficult, requires effort".
                    message=_ "Lord Konrad, yon rumble was made by nae living creature. ’Tis the echoes o’ the very bedrock witherin’ and crumbling beneath us that we ’eard. Are ye nae feeling the heat redoubling? Is yer breath nae toiled for the ashes sowling the air? I beseech that we not tarry ’ere any more than we ha’ to, lest the forming abysses claim us."
                [/message]
            [/then]
            [elseif]
                # At this point, the lava is shrouded. Let's assume that among
                # mages, only high-level ones know the rumble was caused by
                # lava, either because they know the Sceptre's lore or because
                # they can sense the fire.
                #
                # Level 1 Mages are left out because they are apprentices,
                # according to lore.
                #
                # Red or White Mages are left out because there is no
                # particular reason to include them. They could be added if
                # lore is created that justifies that.
                [have_unit]
                    type=Arch Mage,Great Mage,Silver Mage
                [/have_unit]
                [then]
                    [message]
                        type=Arch Mage,Great Mage,Silver Mage
                        # po: "din" means "loud noise". It refers to an ambient sound of collapsing cave walls.
                        message= _ "Prince Konrad, that din was made by no living thing. It was the sound of the very earth that’s boiling around us, of the fire and lava that are breaching this cave even now. We must tread with caution, for the very ground we stand on may turn against us."
                    [/message]
                [/then]
            [/elseif]
        [/if]
        # Li'sar now spells it out: the floor is collapsing. This is partly for fallback in case the player has neither dwarves nor magi, and partly for storyline reasons, to show Li'sar's attitude toward Konrad changing.
        [message]
            id="Li'sar"
            {LISAR_VARIATION mad} # the default portrait doesn't have her sword
            message= _ "Konrad, that must have been the floor collapsing. I was warned of such dangers when I prepared to journey here. We mustn’t tarry here; let us fetch the Sceptre and return to the surface with haste, before the lava under our feet consumes us."
        [/message]
    [/event]

    [event]
        name=moveto
        [filter]
            [filter_location]
                location_id="sceptre"
                radius=6
            [/filter_location]
            side=1
        [/filter]

        [message]
            speaker=Delfador
            message= _ "I sense we are quite near the Sceptre; in fact, we should be able to see it right about now..."
        [/message]

        {VARIABLE moved_too_close yes}
    [/event]

    [event]
        name=victory

        {CLEAR_VARIABLE concentrating,moved_too_close,lava_body,lava_count,sceptre_x,sceptre_y}
    [/event]

    [event]
        name=side 1 turn refresh
        first_time_only=no
        [filter_condition]
            {VARIABLE_CONDITIONAL concentrating boolean_equals yes}
        [/filter_condition]

        # The revealing of the path is much easier done in lua. Thanks mattsc for the code!

        [lua]
            # wmllint: markcheck off
            code= <<
                local ai_helper = wesnoth.require "ai/lua/ai_helper.lua"
                local delf = wesnoth.get_units { id = 'Delfador' }[1]
                local sceptre_loc= wesnoth.special_locations.sceptre
                local path = wesnoth.find_path(delf, sceptre_loc[1], sceptre_loc[2], {ignore_units = true, viewing_side = 0}) -- # wmllint: noconvert
                _ = wesnoth.textdomain 'wesnoth-httt'

                local dirs = {  _"I sense the path to the Sceptre is to the east of me.",
                                _"I sense the path to the Sceptre is to the south-east of me.",
                                _"I sense the path to the Sceptre is to the south of me.",
                                _"I sense the path to the Sceptre is to the south-west of me.",
                                _"I sense the path to the Sceptre is to the west of me.",
                                _"I sense the path to the Sceptre is to the north-west of me.",
                                _"I sense the path to the Sceptre is to the north of me.",
                                _"I sense the path to the Sceptre is to the north-east of me." }

                local goal = { x = path[6][1], y = path[6][2] }

                wesnoth.wml_actions.message{ speaker = 'Delfador', message= dirs[ai_helper.get_direction_index(delf, goal, 8, true)]}
            >>
            # wmllint: markcheck on
        [/lua]
    [/event]

    #
    # Some volcanic ambiance
    #
    [event]
        name=turn end
        first_time_only=no

        {QUAKE "rumble.ogg"}
        {VARIABLE_OP lava_count add 1}
        {EXPAND_LAVA}

        # Reset the counter if it gets too large (it slows things down)
        [if]
            [variable]
                name=lava_count
                numerical_equals=3
            [/variable]
            [then]
                {NEXT_LAVA}
            [/then]
        [/if]
    [/event]

    {campaigns/Heir_To_The_Throne/utils/deaths.cfg}

    {PLACE_SCEPTRE}
    {KONRAD_GETS_SCEPTRE}
    {LISAR_GETS_SCEPTRE}
[/scenario]
