#textdomain wesnoth-sota

# Several enemies are placed on the map at the start. Three others are placed later on
# specific turns. It is possible for the player to occupy their castles before they
# appear, or wait nearby to assassinate them before they can recruit. This strategy will
# backfire as the leader will get at least a castle's worth of units a turn earlier in
# this case. Several events and macros control this. (About half of this file actually.)

# This is slightly hackish since the closer contains VAR_NAME...
# wmlindent: opener "{NEAREST_HEX "
# wmlindent: closer "location}"

[scenario]
    name= _ "Against the World"
    map_data="{campaigns/Secrets_of_the_Ancients/maps/21_Against_the_World.map}"

    id=21_Against_the_World
    next_scenario=22_Epilogue
    victory_when_enemies_defeated=no

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

From the northern part of the kingdom of Knalga, the tunnel split into many passageways. Being able to feel the life force of the dwarves allowed us to take mostly unoccupied tunnels, which we did."
        [/part]
        [part]
            [background_layer]
                image=story/ford-of-abez-2.jpg
                scale=no
            [/background_layer]
            story= _ "3 VII, 23 YW

We finally made it out of the mountains. We crossed the Ford of Abez late this morning, and hiked the rest of the day south-east, making for the town of Tath. We planned to split up there. I was going to go south to Dan’Tonk and Weldyn to spread our news, and Ras-Tabahn was to head toward Elensefar to carry out his plan of carving out a special place for us in the world. (And becoming some sort of king as well I suppose.)"
        [/part]
        [part]
            [background_layer]
                image=story/ford-of-abez-2.jpg
                scale=no
            [/background_layer]
            story= _ "It was not to be. We were about to start on our separate journeys when the plains suddenly became more crowded."
        [/part]
    [/story]

    {MORNING}
    {AFTERNOON}
    {DUSK}
    {FIRST_WATCH}
    {SECOND_WATCH}
    {DAWN}
    {DEFAULT_MUSIC_PLAYLIST}
    turns=unlimited

    [side]
        {SIDE_1_BOTH}
        {GOLD 500 450 400}
    [/side]

    [side]
        # wmllint: recognize Gwendir
        side=2
        controller=ai
        user_team_name= _ "Cavalry"
        team_name=bad
        {GOLD 200 300 400}
        recruit=Knight, Dragoon
        no_leader=yes
        {INCOME 10 12 15}
        {FLAG_VARIANT loyalist}
    [/side]

    [side]
        # wmllint: recognize Fizztsars
        side=3
        controller=ai
        user_team_name= _ "Saurians"
        team_name=bad
        {GOLD 350 450 550}
        recruit=Saurian Ambusher
        no_leader=yes
        hidden=yes
        income=-2  # Until they arrive.
        [ai]
            [avoid]
                x,y=48,19 # Naga keep
            [/avoid]
        [/ai]
    [/side]

    [side]
        # wmllint: recognize Thrigalurd
        side=4
        controller=ai
        user_team_name= _ "Dwarves"
        team_name=bad
        {GOLD 350 450 550}
        recruit=Dwarvish Fighter, Dwarvish Steelclad, Dwarvish Lord, Dwarvish Thunderguard, Dwarvish Pathfinder, Dwarvish Runesmith
        no_leader=yes
        hidden=yes
        income=-2  # Until they arrive.
        {FLAG_VARIANT knalgan}
    [/side]

    [side]
        # wmllint: recognize Carmyna
        side=5
        controller=ai
        user_team_name= _ "Magi"
        team_name=bad
        {GOLD 300 400 500}
        recruit=Mage, Red Mage, White Mage
        no_leader=yes
        hidden=yes
        income=-2  # Until they arrive.
        {FLAG_VARIANT loyalist}
    [/side]

    [side]
        # wmllint: recognize Isthiniel
        side=6
        controller=ai
        user_team_name= _ "Elves"
        team_name=bad
        {GOLD 250 320 400}
        recruit=Elvish Hero, Elvish Sharpshooter, Elvish Sorceress, Elvish Druid
        no_leader=yes
        hidden=yes
        income=-2  # Until they arrive.
        {FLAG_VARIANT wood-elvish}
    [/side]

    [side]
        # wmllint: recognize Mauapan
        side=7
        controller=ai
        user_team_name= _ "Merfolk"
        team_name=bad
        {GOLD 150 250 300}
        recruit=Mermaid Priestess, Mermaid Enchantress, Mermaid Initiate, Merman Warrior, Merman Netcaster, Merman Spearman
        no_leader=yes
        {INCOME 4 6 8}
        [ai]
            # We want the merfolk to pick up villages, but not to start slithering
            # over land towards the player's keep, which they would normally do.
            # To prevent it, we make them avoid everything except water and
            # villages (and their own castle). This is only active through turn 3
            # because a micro-ai is going to take over on turn 4 and send them
            # down the river.
            turns=1-3
            [avoid]
                terrain=!,C*^*,K*^*,W*^*,*^V*
            [/avoid]
        [/ai]
        {FLAG_VARIANT long}
    [/side]

    [side]
        # wmllint: recognize Taxtrimon
        side=8
        controller=ai
        user_team_name= _ "Nagas"
        team_name=bad
        recruit=Naga Warrior
        no_leader=yes
        gold=0
        [ai]
            [avoid]
                x,y=47,19 # Saurian keep
            [/avoid]
        [/ai]
    [/side]

    # This grabs an extra unit from a list for this side. (See the next macro for more
    # info.)
#define EXTRA_STARTING_UNIT SIDE INDEX
    # This variable name is used a lot, and it would be a problem if it were set:
    [clear_variable]
        name=location
    [/clear_variable]
    # We need a variable for the next if statement:
    [set_variable]
        name=side
        value={SIDE}
    [/set_variable]
    #The dwarf leader arrives early enough that he is guaranteed to be able to reach
    # his keep, so we will put *his* units on castle hexes.
    [if]
        [variable]
            name=side
            equals=4
        [/variable]
        [then]
            {FIND_CASTLE_HEX 4 location}
        [/then]
    [/if]
    # The other leaders need more flexible placement.
    [if]
        [variable]
            name=location.length
            less_than=1
        [/variable]
        [then]
            [set_variables]
                name=location
                to_variable=leader_location
            [/set_variables]
        [/then]
    [/if]
    [unit]
        x=$location.x
        y=$location.y
        side={SIDE}
        type=$starting_units_list[{INDEX}].type
    [/unit]
    [gold]
        # It costs about as much as a level-2 unit whether it is or not. This way it
        # has little impact on the total number of units that will be fielded:
        side={SIDE}
        amount=-35
    [/gold]
#enddef

    # When an enemy leader of a new side arrives on the map, it could find the player's
    # units sitting on its castle tiles so it can't recruit, or waiting to assassinate it.
    # This would make it too easy for the player. In this case, the AI will get a castle-full
    # of units without having to recruit them. Many of them will be more powerful than what
    # it would have been able to recruit in the first place, making this strategy backfire
    # on the player.
#define ADD_EXTRA_STARTING_UNITS SIDE
    # close_units will be units from side 1 that can reach the castle this turn (before
    # the leader can recruit), prevent the leader from getting to its keep, or attack
    # the leader.
    [clear_variable]
        name=close_units
    [/clear_variable]

    [store_locations]
        [filter]
            side={SIDE}
            canrecruit=yes
        [/filter]
        variable=leader_location
    [/store_locations]
    [store_locations]
        # If the leader managed to make it to its keep, these will be castle tiles:
        x=$leader_location.x
        y=$leader_location.y
        radius=1
        variable=adjacent_to_leader
    [/store_locations]

    [store_unit]
        [filter]
            side=1
            [not]
                x,y="recall","recall"
            [/not]
        [/filter]
        variable=side1_units
    [/store_unit]

    # For each unit on side-1, store its reachable locations. Then, check each hex
    # adjacent to the enemy leader to see if one matches one of those locations.
    [for]
        array=side1_units
        variable=u
        [do]
            [store_reachable_locations]
                [filter]
                    id=$side1_units[$u].id
                [/filter]
                moves=max
                variable=reachable_locations
            [/store_reachable_locations]
            [for]
                array=adjacent_to_leader
                variable=a
                [do]
                    [if]
                        [have_location]
                            # If an enemy unit can reach this hex...
                            x=$adjacent_to_leader[$a].x
                            y=$adjacent_to_leader[$a].y
                            find_in=reachable_locations
                        [/have_location]
                        [then]
                            # ...then add it to the close_units list:
                            [set_variables]
                                name=close_units
                                mode=append
                                to_variable=side1_units[$u]
                            [/set_variables]
                            [break]
                                # So we don't store the unit again.
                            [/break]
                        [/then]
                    [/if]
                [/do]
            [/for]
        [/do]
    [/for]

    # We need a variable to use in the switch statement:
    [set_variable]
        name=side
        value={SIDE}
    [/set_variable]
    # This statement creates an array of types for the units that will be added to the
    # incoming leader's side. The first five are added in order. The sixth is an extra
    # high-level unit used in special circumstances, and the last is a generic kind of
    # unit that will be used to finish filling up the keep. (Or the hexes around the
    # leader if it couldn't reach its keep.) We also have a different message from
    # each of the leaders depending on whether there are side-1 units nearby.
    [switch]
        variable=side
        [case]
            value=4
            [if]
                [variable]
                    name=close_units.length
                    greater_than=0
                [/variable]
                [then]
                    [set_variables]
                        name=starting_units_list
                        [split]
                            list="Dwarvish Explorer,Dwarvish Thunderguard,Dwarvish Explorer,Dwarvish Runemaster,Dwarvish Runesmith,Dwarvish Arcanister,Dwarvish Thunderguard"
                            key=type
                            separator=","
                        [/split]
                    [/set_variables]
                    [message]
                        speaker=Thrigalurd
                        message= _ "The enemy already attempts to breach our castle walls! Up arms lads, look lively!"
                    [/message]
                [/then]
                [else]
                    [message]
                        speaker=Thrigalurd
                        message= _ "Now we avenge the fallen of Knalga!"
                    [/message]
                [/else]
            [/if]
        [/case]
        [case]
            value=5
            [if]
                [variable]
                    name=close_units.length
                    greater_than=0
                [/variable]
                [then]
                    [set_variables]
                        name=starting_units_list
                        [split]
                            list="White Mage,Silver Mage,Mage of Light,Red Mage,Mage of Light,Great Mage,Red Mage"
                            key=type
                            separator=","
                        [/split]
                    [/set_variables]
                    [message]
                        speaker=Carmyna
                        message= _ "The enemy is already poised to attack! They must have anticipated our arrival. No rest my friends; we go to work immediately."
                    [/message]
                [/then]
                [else]
                    [message]
                        speaker=Carmyna
                        message= _ "The battle is already underway. So many of our comrades have fallen! We must incinerate these undead and rid the world of this evil."
                    [/message]
                [/else]
            [/if]
        [/case]
        [case]
            value=6
            [if]
                [variable]
                    name=close_units.length
                    greater_than=0
                [/variable]
                [then]
                    [set_variables]
                        name=starting_units_list
                        [split]
                            list="Elvish Sorceress,Elvish Sharpshooter,Elvish Enchantress,Elvish Druid,Elvish Enchantress,Elvish Sylph,Elvish Sorceress"
                            key=type
                            separator=","
                        [/split]
                    [/set_variables]
                    [message]
                        speaker=Isthiniel
                        message= _ "What is this? The enemy lurking in wait in our forests? To arms! Let us immediately rid our land of this foulness!"
                    [/message]
                [/then]
                [else]
                    [message]
                        speaker=Isthiniel
                        message= _ "Never again will these monsters use the spirits of our ancestors against us. We will burn them down!"
                    [/message]
                [/else]
            [/if]
        [/case]
    [/switch]

    [for]
        array=close_units
        variable=i
        [do]
            # For each side 1 unit in range, add a unit from the list:
            [if]
                [variable]
                    name=i
                    less_than=7
                [/variable]
                [then]
                    {EXTRA_STARTING_UNIT {SIDE} $i}
                [/then]
            [/if]

            # If the close unit is an ancient lich waiting to assassinate the leader, it
            # is going to find it protected by an equally-powerful unit. This doesn't
            # completely prevent an assassination, but it makes the loss of the lich
            # almost certain if the attempt fails (which is the most likely outcome).
            [if]
                [have_unit]
                    id=$close_units[$i].id
                    type=Ancient Lich, Ancient Lich SotA
                [/have_unit]
                [then]
                    # Using "5" as the index chooses a level-4 unit:
                    {EXTRA_STARTING_UNIT {SIDE} 5}
                [/then]
            [/if]
        [/do]
    [/for]

    # If the AI got just a couple units, they would go off to die one at a time. We
    # finish filling up the keep so that doesn't happen. Also, this will make leader
    # assassination by backstabbing shadows or nightgaunts very difficult. We will
    # do nothing if side 1 wasn't nearby, and no extra units were added. In that case,
    # the leader will just recruit as normal.
    [store_unit]
        [filter]
            side={SIDE}
        [/filter]
        variable=extra_starting_units
    [/store_unit]
    [if]
        [variable]
            name=extra_starting_units.length
            greater_than=1  # *One* unit is the leader.
        [/variable]
        [then]
            [repeat]
                times="$(7 - $extra_starting_units.length)"
                [do]
                    {EXTRA_STARTING_UNIT {SIDE} 6}
                [/do]
            [/repeat]
        [/then]
        [else]
            [set_variable]
                name=extra_units_message
                value="empty"
            [/set_variable]
        [/else]
    [/if]

    # Having a unit sitting directly on the keep is rude of the player, so the AI gets
    # an extra, powerful unit to kick it off.
    [store_starting_location]
        side={SIDE}
        variable=keep_location
    [/store_starting_location]
    [if]
        [have_unit]
            side=1
            x=$keep_location.x
            y=$keep_location.y
        [/have_unit]
        [then]
            # Using "5" chooses a level-4 unit:
            {EXTRA_STARTING_UNIT {SIDE} 5}
        [/then]
    [/if]

    [clear_variable]
        name=adjacent_to_leader, side1_units, side, reachable_locations, starting_units_list, keep_location, leader_location, location, close_units, extra_starting_units
    [/clear_variable]
#enddef

#define LEADER_JOINS_BATTLE SIDE
    # The dwarf, magi, & elf sides are far away, so have the leaders
    # join the battle when recruiting is finished. This saves the player
    # from spending time to chase them down at the end of the battle.
    [event]
        name=side {SIDE} turn
        first_time_only=no
        [store_gold]
            side={SIDE}
        [/store_gold]
        [if]
            [variable]
                name=gold
                less_than_equal_to=15 # Doesn't have to be 0, just less than the cheapest recruit
            [/variable]
            [then]
                [modify_side]
                    side={SIDE}
                    [ai]
                        leader_ignores_keep=yes
                    [/ai]
                [/modify_side]
            [/then]
        [/if]
        [clear_variable]
            name=gold
        [/clear_variable]
    [/event]
#enddef

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

        [objectives]
            side=1
            [objective]
                description= _ "Defeat all enemy leaders"
                condition=win
            [/objective]
            [objective]
                description= _ "Death of Ardryn-Na"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Ras-Tabahn"
                condition=lose
            [/objective]

            {HAS_NO_TURN_LIMIT}
            {IS_LAST_SCENARIO}
        [/objectives]

        {RECALL_LOYAL_UNITS}

        [item]
            x,y=33,42
            image=scenery/tent-fancy-red.png
        [/item]
        [item]
            x,y=7,39
            image=scenery/mine-abandoned.png
        [/item]
    [/event]

    # *************************** PLAY ***************************
    [event]
        name=turn 2

        [move_unit_fake]
            type=Saurian Flanker
            side=3
            x=58, 47
            y=16, 19
        [/move_unit_fake]
        [unit]
            side=3
            x,y=47,19
            type=Saurian Flanker
            name= _ "Fizztsars"
            id=Fizztsars
            canrecruit=yes
            facing=sw
        [/unit]
        [message]
            speaker=Fizztsars
            message= _ "We have arrived, Taxsstrimon. We will fight bessside you. They will pay for killing our malesss!"  # no spellcheck
        [/message]
        [message]
            speaker=Taxtrimon
            message= _ "Well met Fizztsars. Now these creatures will certainly fall."
        [/message]
        [modify_side]
            side=3
            hidden=no
            {INCOME 10 15 20}
        [/modify_side]
    [/event]

    [event]
#ifdef EASY
        name=turn 3
#else
        name=turn 4
#endif
        [move_unit_fake]
            type=Dwarvish Dragonguard
            side=4
            x=7,  8,  8
            y=39, 39, 37
        [/move_unit_fake]
        [unit]
            side=4
            x,y=8,37
            type=Dwarvish Dragonguard
            name= _ "Thrigalurd"
            id=Thrigalurd
            canrecruit=yes
        [/unit]
        [modify_side]
            side=4
            {INCOME 5 7 10}
            hidden=no
        [/modify_side]

        {ADD_EXTRA_STARTING_UNITS 4}
        {LEADER_JOINS_BATTLE 4}

        [message]
            speaker=Ras-Tabahn
            message= _ "Look! The dwarves circled around in front of us."
        [/message]
        [message]
            speaker=Ardonna
            message= _ "That’s why getting through Knalga was so easy."
        [/message]

        # The Merfolk leader will have recruited all he can for now. We turn him
        # into a "messenger" and send him down the river in the east towards the
        # player's keep. He will be escorted by all of his soldiers.
        [micro_ai]
            side=7
            ai_type=messenger_escort
            action=add
            [filter]
                id=Mauapan
            [/filter]
            waypoint_x=56, 45
            waypoint_y=9, 24
        [/micro_ai]
    [/event]

    # When the Merfolk leader arrives near the player's keep, his "escorts" (all his
    # soldiers) will be turned loose.
    [event]
        name=moveto
        [filter]
            id=Mauapan
            x,y=45,24
        [/filter]
        [micro_ai]
            side=7
            ai_type=messenger_escort
            action=delete
        [/micro_ai]
    [/event]

    [event]
        name=turn 12, enemies defeated

        # Find the nearest hext to (4,13) that doesn't have unit on it:
        {NEAREST_HEX 4 13 30 (
            [not]
                [filter]
                [/filter]
            [/not])
        location}

        [move_unit_fake]
            type=Great Mage
            gender=female
            side=5
            x=1,  $location.x
            y=14, $location.y
        [/move_unit_fake]

        [unit]
            side=5
            x=$location.x
            y=$location.y
            type=Great Mage
            gender=female
            name= _ "Carmyna"
            id=Carmyna
            # This is so she can't be assassinated by a single, lucky Ancient Lich:
            [modifications]
                {TRAIT_RESILIENT}
            [/modifications]
            canrecruit=yes
        [/unit]
        [modify_side]
            side=5
            {INCOME 5 7 10}
            hidden=no
        [/modify_side]

        {ADD_EXTRA_STARTING_UNITS 5}
        {LEADER_JOINS_BATTLE 5}

        [message]
            speaker=Ardonna
            message= _ "They just keep coming! Has nobody even considered what we offer?"
        [/message]

        # Now that almost everybody is here, killing the leaders will end it. The
        # player could *theoretically* avoid meeting the elves, but this is awfully
        # unlikely.
        [event]
            name=enemies defeated
            [message]
                speaker=Ras-Tabahn
                message= _ "Well. That was intense."
            [/message]
            [message]
                speaker=Ardonna
                message= _ "At last we are victorious!"
            [/message]
            [endlevel]
                result=victory
                bonus=no
                carryover_report=no
            [/endlevel]
        [/event]
    [/event]

    [event]
        name=turn 13

        {NEAREST_HEX 53 34 30 (
            [not]
                [filter]
                [/filter]
            [/not])
        location}

        [move_unit_fake]
            type=Elvish Enchantress
            side=6
            x=58, $location.x
            y=36, $location.y
        [/move_unit_fake]

        [unit]
            side=6
            x=$location.x
            y=$location.y
            type=Elvish Enchantress
            name= _ "Isthiniel"
            id=Isthiniel
            # This is so she can't be assassinated by a single, lucky Ancient Lich:
            [modifications]
                {TRAIT_RESILIENT}
            [/modifications]
            canrecruit=yes
        [/unit]
        [modify_side]
            side=6
            {INCOME 5 7 10}
            hidden=no
        [/modify_side]

        {ADD_EXTRA_STARTING_UNITS 6}
        {LEADER_JOINS_BATTLE 6}

        [message]
            speaker=Ardonna
            message= _ "The whole world is against us."
        [/message]
    [/event]

    # *************************** ENDING / OPENING ***************************
#define PLACE_NAGA
    [unit]
        side=8
        x,y=51,20
        type=Naga Warrior
        random_gender=yes
        facing=sw
    [/unit]
#enddef

#define PLACE_RIDER TYPE
    {FIND_CASTLE_HEX 2 location}
    [unit]
        x=$location.x
        y=$location.y
        type={TYPE}
        side=2
    [/unit]
#enddef

    [event]
        name=start

        [move_unit_fake]
            type=Paladin
            side=2
            x=40, 33
            y=43, 42
        [/move_unit_fake]
        [unit]
            side=2
            x,y=33,42
            type=Paladin
            name= _ "Gwendir"
            id=Gwendir
            canrecruit=yes
        [/unit]
        [unit]
            side=2
            x,y=33,42
            type=Knight
            id=adviser
        [/unit]
#ifdef EASY
        {PLACE_RIDER Paladin}
        {REPEAT 4 {PLACE_RIDER Knight}}
#endif
#ifdef NORMAL
        {REPEAT 2 {PLACE_RIDER Paladin}}
        {REPEAT 3 {PLACE_RIDER Knight}}
#endif
#ifdef HARD
        {REPEAT 3 {PLACE_RIDER Paladin}}
        {REPEAT 2 {PLACE_RIDER Knight}}
#endif
        [message]
            speaker=adviser
            message= _ "This is the place Crelanu’s gryphon told us to come, and yon party has an ill-favored look."
        [/message]
        [message]
            speaker=Gwendir
            message= _ "Yes, I believe that is our quarry."
        [/message]
        [message]
            speaker=Ras-Tabahn
            message= _ "Crelanu! Still he haunts us. Remind me again why we didn’t kill him."
        [/message]
        [message]
            speaker=Gwendir
            message= _ "Ardonna of Tarrynth and Rassin Tabin, you stand convicted of necromancy, and are sentenced to death!"
        [/message]
        [message]
            speaker=Ras-Tabahn
            message= _ "Is that all? Death holds no terror for us as we have died already. You will follow shortly."
        [/message]
        [message]
            speaker=Gwendir
            message= _ "The blessed swords of our order will soon give you cause to fear again."
        [/message]
        [message]
            speaker=adviser
            message= _ "Sir, the magi have not yet arrived."
        [/message]
        [message]
            speaker=Gwendir
            message= _ "No matter. We need not fear this darkness, and we need not wait on the schoolteachers."
        [/message]

        [move_unit_fake]
            type=Naga Myrmidon
            side=8
            x=58, 51
            y=15, 20
        [/move_unit_fake]
        [unit]
            side=8
            x,y=51,20
            type=Naga Myrmidon
            name= _ "Taxtrimon"
            id=Taxtrimon
            facing=sw
            canrecruit=yes
            goto_x,goto_y=48,19 # So he will be on the keep for dialog when the Saurians arrive
        [/unit]
#ifdef EASY
        {REPEAT 10 {PLACE_NAGA}}
#endif
#ifdef NORMAL
        {REPEAT 13 {PLACE_NAGA}}
#endif
#ifdef HARD
        {REPEAT 16 {PLACE_NAGA}}
#endif
        [message]
            speaker=Taxtrimon
            message= _ "We have found them! These magi destroyed a profitable business venture. We will take vengeance. You horse humans can assist us."
        [/message]

        [move_unit_fake]
            type=Merman Hoplite
            side=7
            x=31, 36
            y=1, 5
        [/move_unit_fake]
        [unit]
            side=7
            x,y=36,5
            type=Merman Hoplite
            name= _ "Mauapan"
            id=Mauapan
            canrecruit=yes
        [/unit]
        [message]
            speaker=Mauapan
            message= _ "I will also assist. Too many innocents have died. This ends today."
        [/message]
        [message]
            speaker=Ardonna
            message= _ "We are surrounded. We must defeat this horde."
        [/message]
    [/event]

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