#textdomain wesnoth-sota

# There is no leader for side 2, but the side starts with a canrecruit unit who leaves
# at the beginning. Woses spawn on many turns, but they are invisible on their forest
# hexes. So, in their first turn, they move one hex and tear up the dirt on their
# starting location. That way the player isn't totally surprised.

[scenario]
    name= _ "Walking Trees"
    map_data="{campaigns/Secrets_of_the_Ancients/maps/12_Walking_Trees.map}"

    id=12_Walking_Trees
    next_scenario=13_Together_Again

    [story]
        [part]
            [background_layer]
                image=story/elf-road.jpg
                scale=no
            [/background_layer]
            story= _ "17 IV, 23 YW

My primary objective was met, and I had been able to press a number of ghosts into service. Also, I had a new companion with a talent for healing. I doubted that Ras-Tabahn was already waiting at the appointed place, but I still wanted to hurry, so we followed the elf road north as quickly as we could. Fortunately, it was not in bad repair, though in many places it was overgrown, leaving only a few smooth cobbles to mark the way. The second day, we were still deep in the woods when we encountered an elf sorceress."
        [/part]
        {JOURNEY_PART 12}
    [/story]

    {DEFAULT_SCHEDULE}
    {DEFAULT_MUSIC_PLAYLIST}
    {TURNS 18 18 18}
    victory_when_enemies_defeated=no

    [side]
        {SIDE_1_ARDONNA}
        {GOLD 200 190 180}
    [/side]

    [side]
        side=2
        no_leader=yes
        controller=ai
        user_team_name= _ "Forest"
        team_name=bad
        gold=0
        income=-2
        color=teal
        [unit]
            type=Elvish Enchantress
            x,y=24,2
            name=_ "Isthiniel"
            id=Isthiniel
            # To match her appearance in S21:
            [modifications]
                {TRAIT_RESILIENT}
            [/modifications]
            canrecruit=yes
            facing=sw
        [/unit]
        hidden=yes
        {FLAG_VARIANT wood-elvish}
    [/side]

    # For each difficulty level, the first 16 woses are level-1, the rest are level-2.
#ifdef EASY
#define MAX_NUMBER_OF_WOSES
    18
#enddef
#define WOSES_PER_TURN
    4
#enddef
#endif

#ifdef NORMAL
#define MAX_NUMBER_OF_WOSES
    22
#enddef
#define WOSES_PER_TURN
    4
#enddef
#endif

#ifdef HARD
#define MAX_NUMBER_OF_WOSES
    25
#enddef
#define WOSES_PER_TURN
    5
#enddef
#endif

    # *************************** PRESTART ***************************

    {SIDE_1_ARDONNA:TRANSFROM_BATS_TO_NORMAL_EVENT}

    [event]
        name=prestart

        [objectives]
            side=1
            [objective]
                description= _ "Move Ardonna to the northeast corner of the map"
                condition=win
            [/objective]

            {HOW_TO_LOSE_WITH_ARDONNA}

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

        {RECALL_LOYAL_UNITS}

        [set_variable]
            name=wose_x
            value=15
        [/set_variable]
        [set_variable]
            name=wose_y
            value=13
        [/set_variable]
        [set_variable]
            name=number_of_woses
            value=1
        [/set_variable]
    [/event]

    # *************************** PLAY ***************************
    [event]
        name=start,side 2 turn 3 refresh,side 2 turn 6 refresh,side 2 turn 9 refresh,side 2 turn 11 refresh
        first_time_only=no

        [set_variable]
            name=woses_this_turn
            value=1
        [/set_variable]
        [while]
            [and]
                [variable]
                    name=number_of_woses
                    less_than_equal_to={MAX_NUMBER_OF_WOSES}
                [/variable]
                [variable]
                    name=woses_this_turn
                    less_than_equal_to={WOSES_PER_TURN}
                [/variable]
            [/and]

            # Put a wose in a random location with some constraints:
            [do]
                # [do] keep choosing a location [while] the location is no good.
                [while]
                    [have_location]
                        x=$wose_x
                        y=$wose_y
                        terrain=!,*^F*  # No good. Not a forest hex.

                        [or]
                            terrain=Ke
                            [and]
                                x=$wose_x
                                y=$wose_y
                                radius=7  # Too close to the player’s keep.
                            [/and]
                        [/or]

                        [or]
                            [filter]
                                side=1
                            [/filter]
                            [and]
                                x=$wose_x
                                y=$wose_y
                                radius=2
                                # The wose would be adjacent to one of the
                                # player's units after it moved one hex.
                            [/and]
                        [/or]

                        [or]
                            x=$wose_x
                            y=$wose_y
                            [filter]
                            [/filter]  # Another unit is there already.
                        [/or]
                    [/have_location]

                    [do]
                        # Choose a random location, which gets closer to the
                        # top corner after turn 8.
                        [if]
                            [variable]
                                name=turn_number
                                greater_than=8
                            [/variable]
                            [then]
                                [set_variable]
                                    name=wose_x
                                    rand=13..27
                                [/set_variable]
                                [set_variable]
                                    name=wose_y
                                    rand=1..13
                                [/set_variable]
                            [/then]
                            [else]
                                [set_variable]
                                    name=wose_x
                                    rand=1..27
                                [/set_variable]
                                [set_variable]
                                    name=wose_y
                                    rand=1..21
                                [/set_variable]
                            [/else]
                        [/if]
                    [/do]
                [/while]

                [if]
                    [variable]
                        name=number_of_woses
                        greater_than=16
                    [/variable]
                    [then]
                        [set_variable]
                            name=wose_type
                            value=Elder Wose
                        [/set_variable]
                    [/then]
                    [else]
                        [set_variable]
                            name=wose_type
                            value=Wose
                        [/set_variable]
                    [/else]
                [/if]

                [unit]
                    type=$wose_type
                    x=$wose_x
                    y=$wose_y
                    side=2
                [/unit]

                [set_variable]
                    name=number_of_woses
                    add=1
                [/set_variable]
                [set_variable]
                    name=woses_this_turn
                    add=1
                [/set_variable]
            [/do]
        [/while]

        [store_locations]
            variable=new_wose_locations
            terrain=*^F*
            [filter]
                side=2
            [/filter]
        [/store_locations]

        [if]
            [variable]
                name=turn_number
                greater_than=1
            [/variable]
            [then]
                [modify_unit]
                    [filter]
                        [filter_location]
                            find_in=new_wose_locations
                        [/filter_location]
                    [/filter]
                    moves=1
                [/modify_unit]
            [/then]
        [/if]
    [/event]

    # If a newly spawned wose is moving from its starting location, change the location
    # to dark dirt. (The wose will probably be invisible in the forest, but the dirt
    # will tell where it spawned.)
    [event]
        name=moveto
        first_time_only=no
        [filter]
            side=2
        [/filter]
        [filter_condition]
            [have_location]
                x,y=$x2,$y2
                find_in=new_wose_locations
            [/have_location]
        [/filter_condition]

        [terrain]
            terrain=Rb
            x,y=$x2,$y2
        [/terrain]
        [item]
            x,y=$x2,$y2
            image=items/dirt1.png
        [/item]
    [/event]

    # *************************** ENDING / OPENING ***************************
    [event]
        name=start
        [message]
            speaker=Isthiniel
            message= _ "I felt our dead stirring and corruption descending on the forest. When I left to investigate, I expected to find grim, veiled forces at work. Instead I find a human mage boldly marching down our road! I will not allow you to continue to molest our peace, and you must release the dead you have enslaved."
        [/message]
        [message]
            speaker=Ardonna
            message= _ "Well, that I won’t do. However, if you do not attack, you will be rid of me soon, as I only wish to leave these woods."
        [/message]
        [message]
            speaker=Isthiniel
            message= _ "Attack? No, I cannot. Most of my people won’t come here; it is only duty that drives me. However, the woods themselves are disturbed, and they will cast you out on their own. I can feel them awaken."
        [/message]
        [terrain]
            terrain=Rb
            x,y=24,4
        [/terrain]
        [item]
            x,y=24,4
            image=items/dirt1.png
        [/item]
        [move_unit_fake]
            type=Ancient Wose
            x=24, 22
            y=4,   3
        [/move_unit_fake]
        [unit]
            type=Ancient Wose
            name= _ "Dolmathengalin"
            id=Dolmathengalin
            x,y=22,3
            side=2
        [/unit]
        [message]
            speaker=Dolmathengalin
            message= _ "You are greeted daughter of elves. Why does the forest moan in despair?"
        [/message]
        [message]
            speaker=Vendraxis
            message= _ "That tree can talk!"
        [/message]
        [message]
            speaker=Isthiniel
            message= _ "That dark mage has woken our dead and stifles the air with her very presence. Please wake your people, Elder, and destroy her."
        [/message]
        [message]
            speaker=Dolmathengalin
            message= _ "This shall be done."
        [/message]
        [message]
            speaker=Isthiniel
            message= _ "I shall leave you. I have no wish to witness this violence."
        [/message]
        [kill]
            id=Isthiniel
        [/kill]
        [move_unit_fake]
            type=Elvish Enchantress
            x=24, 27
            y=2,   1
        [/move_unit_fake]
    [/event]

    [event]
        name=moveto
        [filter]
            id=Ardonna
            x,y=27,1
        [/filter]

        [clear_variable]
            name=wose_x, wose_y, wose_type, random, number_of_woses, woses_this_turn, new_wose_locations
        [/clear_variable]

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

    {HERO_DEATHS}
    {MANAGE_PROFILE}

#undef MAX_NUMBER_OF_WOSES
#undef WOSES_PER_TURN
[/scenario]
