#textdomain wesnoth-sof
[scenario]
    name= _ "Caverns of Flame"
    id=9_Caverns_of_Flame
    map_data="{campaigns/Sceptre_of_Fire/maps/9_Caverns_of_Flame.map}"
    next_scenario=Epilogue
    turns=unlimited
    victory_when_enemies_defeated=no

    {SCENARIO_MUSIC "the_dangerous_symphony.ogg"}
    {EXTRA_SCENARIO_MUSIC "heroes_rite.ogg"}

    {UNDERGROUND}

    [side]
        type=Dwarvish Fighter
        id=Rugnur
        side=1
        canrecruit=yes
        recruit=
        controller=human
        shroud=yes
        gold=0
        income=-2
        village_gold=0
        team_name=dwarves
        user_team_name= _ "Dwarves"
        {FLAG_VARIANT knalgan}
    [/side]

    [side]
        no_leader=yes
        side=2
        team_name=elves
        user_team_name= _ "Elves"
        [ai]
            [goal]
                name=target
                [criteria]
                    side=1
                [/criteria]
                value=5
            [/goal]
            [goal]
                name=target
                [criteria]
                    side=3,4,5,6
                [/criteria]
                value=0.5
            [/goal]
            grouping=no
            aggression=1.0
        [/ai]
        {FLAG_VARIANT wood-elvish}
    [/side]

    [side]
        type=Orcish Warlord
        side=3
        canrecruit=yes
        recruit=Orcish Warrior,Troll Warrior
        {GOLD 200 220 240}
        {INCOME 20 25 30}
        [ai]
            recruitment_pattern=fighter
            [goal]
                name=target
                [criteria]
                    side=1
                [/criteria]
                value=10
            [/goal]
        [/ai]
        team_name=orcs
        user_team_name= _ "Orcs"
        {FLAG_VARIANT6 ragged}
    [/side]

    [side]
        type=Orcish Warlord
        side=4
        canrecruit=yes
        recruit=Orcish Warrior,Troll Warrior
        {GOLD 200 220 240}
        {INCOME 20 25 30}
        [ai]
            recruitment_pattern=fighter
            [goal]
                name=target
                [criteria]
                    side=1
                [/criteria]
                value=10
            [/goal]
        [/ai]
        team_name=orcs
        user_team_name= _ "Orcs"
        {FLAG_VARIANT6 ragged}
    [/side]

    [side]
        type=Troll Warrior
        side=5
        canrecruit=yes
        recruit=Orcish Warrior,Troll
        {GOLD 150 200 250}
        {INCOME 5 6 7}
        [ai]
            recruitment_pattern=fighter
            [goal]
                name=target
                [criteria]
                    side=1
                [/criteria]
                value=10
            [/goal]
            [goal]
                name=target
                [criteria]
                    side=2
                [/criteria]
                value=1
            [/goal]
        [/ai]
        team_name=orcs
        user_team_name= _ "Orcs"
    [/side]

    [side]
        type=Troll Warrior
        side=6
        canrecruit=yes
        controller=ai
        recruit=Orcish Warrior,Troll
        {GOLD 150 200 250}
        {INCOME 5 6 7}
        [ai]
            recruitment_pattern=fighter
            [goal]
                name=target
                [criteria]
                    side=1
                [/criteria]
                value=10
            [/goal]
            [goal]
                name=target
                [criteria]
                    side=2
                [/criteria]
                value=1
            [/goal]
        [/ai]
        team_name=orcs
        user_team_name= _ "Orcs"
    [/side]

    [story]
        [part]
            # po: The text in 1.14.4 mentioned Knalga. Geographically, these caves are in Knalga, but the text was confusing if the reader thought of the Knalgan Alliance.
            story= _ "There was no exit for the dwarves. They had reached the realms of the orcs, with the elves hot on their trail. There was truly no exit from these caves."
        [/part]
        [part]
            story= _ "So the dwarves had no way out. They could not leave the caverns that they had entered."
        [/part]
    [/story]

    {SOF_TRACK {JOURNEY_09_NEW} }

    {SOF_DEATHS}

    [event]
        name=prestart

        #the glyph
        [item]
            x,y=18,23
            image=scenery/rune1.png
        [/item]

        [objectives]
            side=1
            [objective]
                description= _ "Explore as much of the cave as possible"
                condition=win
            [/objective]
            [objective]
                description= _ "Death of Rugnur"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Krawg"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Thursagan"
                condition=lose
            [/objective]

            {IS_LAST_SCENARIO}
        [/objectives]
    [/event]

    [event]
        name=start

        [recall]
            id=Thursagan
        [/recall]
        [recall]
            id=Baglur
        [/recall]
        [recall]
            id=Krawg
        [/recall]

        [store_unit]
            [filter]
                side=1
                x,y=recall,recall
            [/filter]

            kill=no
            variable=to_be_recalled
        [/store_unit]

        [foreach]
            array=to_be_recalled
            [do]
                [recall]
                    id=$this_item.id
                [/recall]
            [/do]
        [/foreach]

        {CLEAR_VARIABLE to_be_recalled}

        #orcs/trolls start out
        [unit]
            x,y=17,21
            side=5
            type=Troll
            ai_special=guardian
            role=Guardian
            name= _ "Guardian"
        [/unit]
        [unit]
            x,y=15,24
            side=5
            type=Orcish Warrior
            ai_special=guardian
            role=Guardian
            name= _ "Guardian"
        [/unit]
        [unit]
            x,y=16,25
            side=5
            type=Orcish Warrior
            ai_special=guardian
            role=Guardian
            name= _ "Guardian"
        [/unit]
        [unit]
            x,y=19,21
            side=6
            type=Troll
            ai_special=guardian
            role=Guardian
            name= _ "Guardian"
        [/unit]
        [unit]
            x,y=21,24
            side=6
            type=Orcish Warrior
            ai_special=guardian
            role=Guardian
            name= _ "Guardian"
        [/unit]
        [unit]
            x,y=20,25
            side=6
            type=Orcish Warrior
            ai_special=guardian
            role=Guardian
            name= _ "Guardian"
        [/unit]
        #messages
        [message]
            speaker=Rugnur
            message= _ "Well, I think we’ve gotten a good distance ahead of the elves. What do we do now?"
        [/message]
        [message]
            speaker=Thursagan
            # wmllint: local spelling Khrakrahs
            message= _ "We should keep going. Remember what Khrakrahs said, about this being a volcano? I think we should try to cause it to erupt. It will kill all the elves, and we might be able to find a safe place so the lava doesn’t kill us."
        [/message]
        [message]
            speaker=Rugnur
            message= _ "This sounds... plausible."
        [/message]
        [message]
            speaker=Baglur
            message= _ "Yes, an interesting plan. How do you propose we do this?"
        [/message]
        [message]
            speaker=Thursagan
            message= _ "I have a suspicion that there is an opening somewhere near here than leads to the surface. Somewhere near there, there is probably a magic object of some kind that will cause the lava to flow."
        [/message]
        [message]
            speaker=Rugnur
            message= _ "Very well. We will continue to explore until we find it."
        [/message]
        [message]
            speaker=Krawg
            #po: "Krawg smells orcs in this cave!"
            message= _ "Krawg sme’ o’cz in iz ’ave!" # wmllint: no spellcheck
        [/message]
        [message]
            speaker=Thursagan
            message= _ "Indeed, there may be orcs lurking here. We should beware of them."
        [/message]
        [set_variable]
            name=overlayturn
            value=not_yet
        [/set_variable]
    [/event]

#define RANDOM_MERCENARY_2 TYPE
    {GENERIC_UNIT 2 {TYPE} 15 4}
#enddef

    #elves come
    [event]
        name=turn 4
        [move_unit_fake]
            type=Elvish Marshal
            side=2
            x=15
            y=1,4
        [/move_unit_fake]
        [unit]
            type=Elvish Marshal
            side=2
            id=Aendan
            name= _ "Aendan"
            canrecruit=yes
            x,y=15,4
        [/unit]
        [message]
            speaker=Aendan
            message= _ "We have caught up to the dwarves. Now, prepare to destroy them!"
        [/message]
        {RANDOM_MERCENARY_2 "Dwarvish Lord"}
        {RANDOM_MERCENARY_2 "Dwarvish Lord"}
        {RANDOM_MERCENARY_2 "Dwarvish Lord"}
        {RANDOM_MERCENARY_2 "Dwarvish Lord"}
#ifdef HARD
        {RANDOM_MERCENARY_2 "Dwarvish Lord"}
#endif
        {RANDOM_MERCENARY_2 "Dwarvish Dragonguard"}
        {RANDOM_MERCENARY_2 "Dwarvish Dragonguard"}
        {RANDOM_MERCENARY_2 "Dwarvish Dragonguard"}
        {RANDOM_MERCENARY_2 "Dwarvish Dragonguard"}
#ifdef HARD
        {RANDOM_MERCENARY_2 "Dwarvish Dragonguard"}
#endif
        {RANDOM_MERCENARY_2 "Elvish Champion"}
        {RANDOM_MERCENARY_2 "Elvish Champion"}
        {RANDOM_MERCENARY_2 "Elvish Champion"}
        {RANDOM_MERCENARY_2 "Elvish Champion"}
#ifdef HARD
        {RANDOM_MERCENARY_2 "Elvish Champion"}
#endif
        {RANDOM_MERCENARY_2 "Elvish Avenger"}
        {RANDOM_MERCENARY_2 "Elvish Avenger"}
        {RANDOM_MERCENARY_2 "Elvish Avenger"}
        {RANDOM_MERCENARY_2 "Elvish Avenger"}
#ifdef HARD
        {RANDOM_MERCENARY_2 "Elvish Avenger"}
#endif
        {RANDOM_MERCENARY_2 "Elvish Outrider"}
        {RANDOM_MERCENARY_2 "Elvish Outrider"}
        {RANDOM_MERCENARY_2 "Elvish Outrider"}
        {RANDOM_MERCENARY_2 "Elvish Outrider"}
#ifdef HARD
        {RANDOM_MERCENARY_2 "Elvish Outrider"}
#endif
        [message]
            speaker=Rugnur
            message= _ "Thursagan! The elves are right on our tail; what should we do?"
        [/message]
        [message]
            speaker=Thursagan
            message= _ "We could try to set off the volcano as soon as possible. Meanwhile, we should draw the elves further into the caves, so they can’t escape when it does erupt."
        [/message]
    [/event]

    #when the elves lose troops, the orcs gain them
    [event]
        name=die
        first_time_only=no
        [filter]
            side=2
        [/filter]
        [gold]
            side=3
            amount=50
        [/gold]
        [gold]
            side=4
            amount=50
        [/gold]
    [/event]

    #seeing the guards of the glyph
    [event]
        name=sighted
        [filter]
            role=Guardian
            side=5,6
            [filter_vision]
                side=1
            [/filter_vision]
        [/filter]
        [message]
            speaker=Rugnur
            message= _ "These orcs appear to be guarding something."
        [/message]
        [message]
            speaker=Thursagan
            message= _ "It is probably the magical object. Attack them!"
        [/message]
        [objectives]
            side=1
            [objective]
                description= _ "Move onto the glyph in the center of the volcano"
                condition=win
            [/objective]
            [objective]
                description= _ "Death of Rugnur"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Krawg"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Thursagan"
                condition=lose
            [/objective]

            {IS_LAST_SCENARIO}
        [/objectives]
    [/event]

#define CHANGE_MAP TURN
    [if]
        [variable]
            name=overlayturn
            numerical_equals={TURN}
        [/variable]
        [then]
            [terrain_mask]
                x,y=1,1
                border=yes
                mask="{campaigns/Sceptre_of_Fire/maps/9_overlay/9_o{TURN}.mask}"
                [rule]
                    old=Ql,Uu,Cud,Vu,Rr,Uf,Uh
                    new=Ql
                    terrain=Qlf
                [/rule]
                [rule]
                    use_old=yes
                [/rule]
            [/terrain_mask]
            [redraw][/redraw]
        [/then]
    [/if]
#enddef

    #triggering the volcano
    [event]
        name=moveto
        [filter]
            x,y=18,23
            side=1
        [/filter]
        {QUAKE "rumble.ogg"}
        {QUAKE "rumble.ogg"}
        [message]
            speaker=Thursagan
            message= _ "That’s it! The volcano will erupt soon now that you’ve triggered it."
        [/message]
        [terrain]
            x=11
            y=4-5
            terrain=Uu
        [/terrain]
        [terrain]
            x=20
            y=3-4
            terrain=Uu
        [/terrain]
        {QUAKE "rumble.ogg"}
        {QUAKE "rumble.ogg"}
        [message]
            speaker=Rugnur
            message= _ "Look, it has knocked down some of the walls! Those orcs have a way out now!"
        [/message]
        [scroll_to]
            x,y=14,1
        [/scroll_to]
        {QUAKE "rumble.ogg"}
        {QUAKE "rumble.ogg"}
        [message]
            speaker=Baglur
            message= _ "Well, we’ve succeeded in stopping the elves from getting the Sceptre, I would say. But what do we do now?!"
        [/message]
        {QUAKE "rumble.ogg"}
        {QUAKE "rumble.ogg"}
        [message]
            speaker=Thursagan
            message= _ "Now, we must fight the orcs until we die, it’s our only option."
        [/message]
        {QUAKE "rumble.ogg"}
        {QUAKE "rumble.ogg"}
#ifdef __UNUSED__
        [message]
            speaker=Rugnur
            message= _ "We cannot let any of the elves or their mercenaries escape; they would tell where the Sceptre of Fire is. Make sure they all die!"
        [/message]
        {QUAKE "rumble.ogg"}
        {QUAKE "rumble.ogg"}
        [message]
            speaker=Thursagan
            message= _ "We have to kill them quickly, before they realize what is happening. And remember, the lava spreads quickly, suddenly, and dangerously. The only place safe from it is the area near the glyph."
        [/message]
        {QUAKE "rumble.ogg"}
        {QUAKE "rumble.ogg"}
#endif

        #a turn counter that starts when you set off the volcano
        [set_variable]
            name=overlayturn
            value=0
        [/set_variable]

        #the objectives, they are a-changin'
        [objectives]
            side=1
            [objective]
                description= _ "Survive as long as possible"
                condition=win
            [/objective]
            [objective]
                description= _ "Death of Rugnur"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Krawg"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Thursagan"
                condition=lose
            [/objective]

            {IS_LAST_SCENARIO}
        [/objectives]
    [/event]

    [event]
        name=new turn
        first_time_only=no

        [filter_condition]
            [variable]
                name=overlayturn
                not_equals=not_yet
            [/variable]
        [/filter_condition]

        [set_variable]
            name=overlayturn
            add=1
        [/set_variable]
        #the elves try to escape 5 turns after you set off the volcano
        [if]
            [variable]
                name=overlayturn
                numerical_equals=5
            [/variable]
            [then]
                [role]
                    role=smartelf
                    side=2
                    type=Elvish Marshal,Elvish Avenger,Elvish Champion,Elvish Outrider,Dwarvish Lord,Dwarvish Dragonguard
                [/role]
                {QUAKE "rumble.ogg"}
                {QUAKE "rumble.ogg"}
                [message]
                    role=smartelf
                    message= _ "Wait, what’s going on? The volcano is going to erupt! Everyone, try to get out of these caves!"
                [/message]

                [micro_ai]
                    side=2
                    action=add
                    ai_type=coward

                    [filter]
                        side=2
                    [/filter]
                    distance=15
                    seek_x,seek_y=15,1
                    avoid_x,avoid_y=18,23
                [/micro_ai]

                # once they try to escape, if they move to the top 2 rows you lose
                # deactivated out since there's no way for the player to
                # stop them from moving to those locations
#ifdef __UNUSED__
                [event]
                    name=moveto
                    [filter]
                        x=14-16
                        y=1-2
                        side=2
                    [/filter]
                    [message]
                        speaker=unit
                        message= _ "I must go report to my superiors!"
                    [/message]
                    [message]
                        speaker=Thursagan
                        message= _ "We have allowed an enemy to escape; we have failed."
                    [/message]
                    [endlevel]
                        result=defeat
                    [/endlevel]
                [/event]
#endif
            [/then]
        [/if]
        #the lava is spreading and killing things every turn
        {QUAKE "rumble.ogg"}
        {QUAKE "rumble.ogg"}
        {CHANGE_MAP 1}
        {CHANGE_MAP 2}
        {CHANGE_MAP 3}
        {CHANGE_MAP 4}
        {CHANGE_MAP 5}
        {CHANGE_MAP 6}
        [store_locations]
            [filter]
                [not]
                    type=Gryphon,Gryphon Rider,Gryphon Master
                [/not]
                side=1,2,3,4,5,6
            [/filter]
            x=1-32
            y=1-32
            terrain=Ql,Qlf
            variable=lavadead
        [/store_locations]
        [foreach]
            array=lavadead
            [do]
                [kill]
                    x=$this_item.x
                    y=$this_item.y
                    animate=yes
                    fire_event=yes
                [/kill]
            [/do]
        [/foreach]
        [clear_variable]
            name=lavadead
        [/clear_variable]
        #after 7 turns you win
        [if]
            [variable]
                name=overlayturn
                numerical_equals=7
            [/variable]
            [then]
                {QUAKE "rumble.ogg"}
                {QUAKE "rumble.ogg"}
                [role]
                    role=dumbelf
                    side=2
                    type=Elvish Marshal,Elvish Avenger,Elvish Champion,Elvish Outrider,Dwarvish Lord,Dwarvish Dragonguard
                [/role]
                [message]
                    role=dumbelf
                    message= _ "Aghh!! The volcano is erupting, and now our exit has been blocked off!!"
                [/message]

                [fire_event]
                    name=lava kills all
                [/fire_event]
            [/then]
        [/if]
    [/event]

    #when all the elves die, you win
    [event]
        name=die
        first_time_only=no
        [filter]
            side=2
        [/filter]
        [filter_condition]
            [not]
                [have_unit]
                    side=2
                [/have_unit]
            [/not]
        [/filter_condition]
        [message]
            speaker=Rugnur
            message= _ "We have hidden the Sceptre well, but now the volcano is going to erupt and kill us all."
        [/message]
        [fire_event]
            name=lava kills all
        [/fire_event]
    [/event]

    #when you win, Krawg flies out of the mountain
    [event]
        name=lava kills all
        #kill everyone in a sequence that makes it look like the cave is being flooded by lava

        [while]
            [have_location]
                terrain=!,Xu,Qlf,Qlf^*
                include_borders=no
                [filter_adjacent_location]
                    terrain=Ql,Qlf
                [/filter_adjacent_location]
            [/have_location]

            [do]
                [store_locations]
                    terrain=!,Xu,Qlf,Qlf^*
                    include_borders=no
                    [filter_adjacent_location]
                        terrain=Ql,Qlf
                    [/filter_adjacent_location]
                    variable=new_lava
                [/store_locations]

                [terrain]
                    find_in=new_lava
                    include_borders=no
                    [and]
                        terrain=*^Ii
                    [/and]
                    terrain=Qlf^Ii
                [/terrain]

                [terrain]
                    find_in=new_lava
                    include_borders=no
                    [not]
                        terrain=Qlf^Ii
                    [/not]
                    terrain=Qlf
                [/terrain]

                [redraw][/redraw]

                [kill]
                    [filter_location]
                        find_in=new_lava
                    [/filter_location]
                    [not]
                        type=Gryphon,Gryphon Rider,Gryphon Master
                    [/not]
                    animate=yes
                    fire_event=no
                [/kill]
            [/do]
        [/while]

        {CLEAR_VARIABLE new_lava}

        [terrain]
            [and]
                terrain=!,Xu,Qlf,Qlf^*
            [/and]
            terrain=Qlf
        [/terrain]

        #krawg leaves
        [kill]
            [not]
                id=Krawg
            [/not]
            animate=yes
        [/kill]
        [message]
            speaker=Krawg
            #po: "Hmmm, me leave now!"
            message= _ "Hm... Me le’ no’w!" # wmllint: no spellcheck
        [/message]
        [store_unit]
            [filter]
                id=Krawg
            [/filter]
            variable=krawg
            kill=yes
        [/store_unit]
        {VARIABLE move_x ($krawg.x|,18)}
        {VARIABLE move_y ($krawg.y|,23)}
        [move_unit_fake]
            type=$krawg.type
            side=$krawg.side
            x=$move_x
            y=$move_y
        [/move_unit_fake]
        [unstore_unit]
            variable=alanin
        [/unstore_unit]
        {CLEAR_VARIABLE move_x,move_y}
        {CLEAR_VARIABLE alanin}
        [endlevel]
            result=victory
            carryover_report=no
            save=no
            linger_mode=no
        [/endlevel]
    [/event]
[/scenario]

#undef RANDOM_MERCENARY_2
#undef CHANGE_MAP
