#textdomain wesnoth-sota

# A large number of bats spawn in the cave under these two conditions:
# (1) Six non-bat units are in the cave (Bats don't stomp on the floor.)
# (2) Ardonna has been in the cave for two turns in a row. (That gets her solidly into
#     the cave, but not close to the exit. If she is alone, it's the player's fault when
#     the bats kill her.)

[scenario]
    name= _ "Following the Shadow"
    map_data="{campaigns/Secrets_of_the_Ancients/maps/06_Following_the_Shadow.map}"

    id=06_Following_the_Shadow
    next_scenario=07_Meeting_of_the_Minds

    victory_when_enemies_defeated=no

    [story]
        [part]
            [background_layer]
                image=story/book.png
                scale=no
            [/background_layer]
            [if]
                [have_unit]
                    id=Bone Captain
                    search_recall_list=yes
                [/have_unit]
                [then]
                    story=_ "3 IV, 23 YW

I was so sure I had the answer! I was wrong. I wanted to believe that I was imagining it, but it’s now clear that the captain’s memories are fading. He still retains speech, and the others fight better when he is watching (many used to be his crew after all — it would be habit for them), but he remembers precious little else now. The same is true of my other recently-raised friends. I am disappointed, but the existence of my strange new companion keeps me from being depressed."
                [/then]
                [else]
                    story=_ "3 IV, 23 YW

I was so sure I had the answer! I was wrong. I wanted to believe that I was imagining it, but my recently-raised friends are losing their memories. They retain the power of speech, but precious little else. I am disappointed, but the existence of my strange new companion keeps me from being depressed."
                [/else]
            [/if]
        [/part]

        [part]
            [background_layer]
                image=portraits/undead/shadow.png
                scale=no
                base_layer=yes
            [/background_layer]
            story=_ "The phantom leads us north. It has identified itself as the ghost of Vash-Gorn, an orc. So it remembers! It seems that the problem has been solved, but the ghost ignores all my questions. I am burning with curiosity, so I hope we reach our destination soon! We travel only by day because at night the spirit is practically invisible. We have not had any trouble on the road yet, but we <i>are</i> a rather large company now, so I have not expected it."
        [/part]
        {JOURNEY_PART 6}
    [/story]

    {DEFAULT_SCHEDULE}
    {DEFAULT_MUSIC_PLAYLIST}
    turns=38

#define CAVE_TERRAINS
    U*,U*^*,Rb,Rb^*,*^Dr,Qxe,Wwf,Wwg
#enddef

    [side]
        # wmllint: unwho SIDE_1_ARDONNA_EARLY
        # wmllint: who SIDE_1_ARDONNA is Ardonna
        {SIDE_1_ARDONNA}
        {GOLD 200 175 150}
        shroud=yes
    [/side]

    [side]
        side=2
        controller=ai
        user_team_name= _ "Goblin Raiders"
        team_name=bad
        type=Direwolf Rider
        name= _ "Bogdush"
        id=Bogdush
        facing=sw
        recruit=Wolf Rider, Goblin Knight, Goblin Pillager
        {GOLD 250 325 400}
        [unit]
            x,y=46,3
            type=Goblin Pillager
            moves=0
            name= _ "Blaust"
            id=Blaust
            facing=se
        [/unit]
        {FLAG_VARIANT6 ragged}
    [/side]

    [side]
        side=3
        controller=ai
        user_team_name= _ "Bats"
        hidden=yes
        team_name=bats
        no_leader=yes
        [ai]
            [avoid]
                # Bats stay in the cave.
                terrain=!,{CAVE_TERRAINS}
            [/avoid]
        [/ai]
    [/side]

    [side]
        side=4
        team_name=good
        controller=null
        hidden=yes
        color=darkred
    [/side]
    {STARTING_VILLAGES  1 6}
    {STARTING_VILLAGES  2 6}

#ifdef HARD
    {LIMIT_CONTEMPORANEOUS_RECRUITS 2 (Goblin Knight) 2}
#endif

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

    {SIDE_1_ARDONNA:TRANSFROM_BATS_TO_NORMAL_EVENT}

    [event]
        name=prestart
        {RECALL_LOYAL_UNITS}

        # Using a shroud_data file would work too, but this is easier to modify if
        # necessary:
        [remove_shroud]
            side=1
        [/remove_shroud]
        [place_shroud]
            side=1
            terrain={CAVE_TERRAINS}
        [/place_shroud]
        [place_shroud]
            # Shrouded floor next to cave wall leaves part of the cave wall
            # unshrouded, showing part of the shape of the cave before it is
            # discovered. This tag covers up most of the offending walls. Some of
            # them can't be covered without covering the *outside* of the cave
            # walls as well, so we are leaving those as they are.
            x=41-44, 46, 47, 47-50, 50-54
            y=10-14,  8 , 18,    8    , 9-19
        [/place_shroud]
        [remove_shroud]
            # So the player can see the three entrances:
            side=1
            x=36, 37, 43
            y=11, 11, 15
        [/remove_shroud]

        [objectives]
            side=1
            [objective]
                description= _ "Find out where the ghost went and follow it"
                condition=win
            [/objective]

            {HOW_TO_LOSE_WITH_ARDONNA}

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

        [time_area]
            {UNDERGROUND}
            terrain={CAVE_TERRAINS}
        [/time_area]
    [/event]

    # *************************** PLAY ***************************

    # Animates bats coming from the chasm in the center of the cave (at 47,13)
#define MOVE_AND_PLACE_BAT X Y TYPE
    # X and Y values are passed to the macro, but that location may be occupied. This
    # macro finds the nearest unoccupied location in case it is. That is the location
    # that will actually be used in case it is different than X and Y.
    {NEAREST_HEX {X} {Y} 99 (
        [not]
            [filter]
            [/filter]
            [or]
                x,y=47,13
            [/or]
        [/not]) location}

        [move_unit_fake]
            type={TYPE}
            side=3
            x=47,$location.x
            y=13,$location.y
        [/move_unit_fake]
        [unit]
            type={TYPE}
            side=3
            x=$location.x
            y=$location.y
        [/unit]
        [clear_variable]
            name=location
        [/clear_variable]
#enddef

    [event]
        name=moveto
        [filter_condition]
            [have_location]
                x,y=47,13
                [filter_vision]
                    side=1
                [/filter_vision]
            [/have_location]
        [/filter_condition]
        [scroll_to]
            x,y=47,13
        [/scroll_to]
        [delay]
            time=1000
        [/delay]
        [message]
            speaker=Ardonna
            message= _ "Look, there is a cavern below the cave floor. What might we rouse if we go tromping about in the cave? I hope it is nothing worse than bats."
        [/message]
        [message]
            speaker=Bone Captain
            message= _ "Ghost?"
        [/message]
        [message]
            speaker=Ardonna
            message= _ "Surely the ghost would not have gone that way, where I cannot follow."
        [/message]
        # Have a bat fly out of the chasm as a warning.
        [delay]
            time=1000
        [/delay]
#ifdef EASY
        {MOVE_AND_PLACE_BAT 43 12 (Vampire Bat)}
#endif
#ifdef NORMAL
        {MOVE_AND_PLACE_BAT 43 12 (Blood Bat)}
#endif
#ifdef HARD
        {MOVE_AND_PLACE_BAT 43 12 (Dread Bat)}
#endif
        [delay]
            time=1000
        [/delay]
        [message]
            speaker=Ardonna
            message= _ "If there is one bat, there are certainly more. We should be careful."
        [/message]
    [/event]

    [event]
        name=moveto
        [filter_condition]
            [have_location]
                x,y=53,20
                [filter_vision]
                    side=1
                [/filter_vision]
            [/have_location]
        [/filter_condition]
        [scroll_to]
            x,y=53,20
        [/scroll_to]
        [delay]
            time=500
        [/delay]
        [message]
            speaker=Ardonna
            message= _ "There is a tunnel at the back of the cave! The ghost must have gone through there."
        [/message]
        {HIGHLIGHT_IMAGE 53 20 "items/gohere.png" ()}
        [remove_item]
            x,y=53,20
        [/remove_item]
        [objectives]
            side=1
            [objective]
                description= _ "Move Ardonna to the tunnel"
                condition=win
            [/objective]

            {HOW_TO_LOSE_WITH_ARDONNA}

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

    # If we don't do this, the first wolf attacks by himself, leaving him little
    # chance of surviving or doing much damage.
    [event]
        name=side 2 turn 1 refresh
        [modify_unit]
            [filter]
                id=Blaust
            [/filter]
            moves=0
            attacks_left=0
        [/modify_unit]
    [/event]

    # This checks to see if the conditions are correct for activating the bats. The
    # bats will spawn as their turn ends, so they can't spawn and attack at the same
    # time. (Which would be a bad surprise.) This way, the player gets one turn to
    # arrange their defenses.
    [event]
        name=side 3 turn end
        first_time_only=no
        id=check_for_bat_activation

        # If Ardonna is not in the cave, reset (or set) a counter.
        [if]
            [have_unit]
                id=Ardonna
                [filter_location]
                    terrain=!,{CAVE_TERRAINS}
                [/filter_location]
            [/have_unit]
            [then]
                [set_variable]
                    name=turns_in_cave
                    value=0
                [/set_variable]
            [/then]
        [/if]

        # If Ardonna *is* in the cave, increment the counter.
        [if]
            [have_unit]
                id=Ardonna
                [filter_location]
                    terrain={CAVE_TERRAINS}
                [/filter_location]
            [/have_unit]
            [then]
                [set_variable]
                    name=turns_in_cave
                    add=1
                [/set_variable]
            [/then]
        [/if]

        # If Ardonna's counter gets to 2, activate the bats.
        [if]
            [variable]
                name=turns_in_cave
                equals=2
            [/variable]
            [then]
                [fire_event]
                    name=activate bats
                [/fire_event]
            [/then]
        [/if]

        # Activate the bats if there are six or more non-flying units in the cave.
        [if]
            [have_unit]
                side=1
                [filter_location]
                    terrain={CAVE_TERRAINS}
                [/filter_location]
                [not]
                    race=bats
                [/not]
                [not]
                    variation=bat  # undead bat
                [/not]
                count=6-99  # six or more
            [/have_unit]
            [then]
                [fire_event]
                    name=activate bats
                [/fire_event]
            [/then]
        [/if]
    [/event]

    # Four hard-coded, animated bats appear every time, the number of others, which are
    # randomly placed, is based on difficulty.
#ifdef EASY
#define NUMBER_OF_EXTRA_BATS
    5
#enddef
#endif

#ifdef NORMAL
#define NUMBER_OF_EXTRA_BATS
    7
#enddef
#endif

#ifdef HARD
#define NUMBER_OF_EXTRA_BATS
    10
#enddef
#endif

    [event]
        name=activate bats

        [remove_event]
            # We don't want the bats activated twice:
            id=check_for_bat_activation
        [/remove_event]
        [clear_variable]
            name=turns_in_cave
        [/clear_variable]

        {MOVE_AND_PLACE_BAT 45 11 (Vampire Bat)}
        {MOVE_AND_PLACE_BAT 45 14 (Blood Bat)}
        {MOVE_AND_PLACE_BAT 47 9 (Blood Bat)}
        {MOVE_AND_PLACE_BAT 48 10 (Dread Bat)}

        [store_locations]
            terrain={CAVE_TERRAINS}
            variable=possible_bat_locations
        [/store_locations]

        [set_variable]
            name=bat_index
            value=1
        [/set_variable]
        [while]
            [variable]
                name=bat_index
                less_than_equal_to={NUMBER_OF_EXTRA_BATS}
            [/variable]

            [do]
                # Choose a location by selecting an array index:
                [set_variable]
                    name=location_index
                    rand="0..$($possible_bat_locations.length-1)"
                [/set_variable]

                # [while] the location is no good, [do] choose another one:
                [while]
                    [have_location]
                        x=$possible_bat_locations[$location_index].x
                        y=$possible_bat_locations[$location_index].y
                        [filter]
                            # It's no good because there is already a unit here.
                        [/filter]
                    [/have_location]
                    [or]
                        [variable]
                            # It's no good because it's off the edge of the map.
                            # (It can only go off in the positive x direction.)
                            name=possible_bat_locations[$location_index].x
                            greater_than=53
                        [/variable]
                    [/or]
                    [do]
                        [set_variable]
                            name=location_index
                            rand=0.."$($possible_bat_locations.length-1)"
                        [/set_variable]
                    [/do]
                [/while]

                # The bat_index reaches higher with each difficulty level. The
                # types of bats are hard-coded instead of random because it can
                # make a large difference in the difficulty.
                [switch]
                    variable=bat_index
                    [case]
                        value=1, 5, 8
                        [set_variable]
                            name=bat_type
                            value=Vampire Bat
                        [/set_variable]
                    [/case]
                    [case]
                        value=2, 4, 6, 9
                        [set_variable]
                            name=bat_type
                            value=Blood Bat
                        [/set_variable]
                    [/case]
                    [case]
                        value=3, 7, 10
                        [set_variable]
                            name=bat_type
                            value=Dread Bat
                        [/set_variable]
                    [/case]
                [/switch]
                # Put a bat on the map at the selected location:
                [unit]
                    type=$bat_type
                    x=$possible_bat_locations[$location_index].x
                    y=$possible_bat_locations[$location_index].y
                    side=3
                [/unit]

                [set_variable]
                    name=bat_index
                    add=1
                [/set_variable]
            [/do]
        [/while]
        [message]
            speaker=Ardonna
            message= _ "So it’s only bats, but rather a lot of them."
        [/message]

        [clear_variable]
            name=possible_bat_locations, bat_index, bat_type, location_index
        [/clear_variable]
    [/event]

    # *************************** OPENING / ENDING ***************************
    [event]
        name=moveto

        [filter]
            x,y=53,20
            id=Ardonna
        [/filter]

        [message]
            speaker=Ardonna
            message= _ "Now let’s see where that ghost went."
        [/message]
        [endlevel]
            result=victory
            bonus=yes
            {NEW_GOLD_CARRYOVER 40}
        [/endlevel]
    [/event]

    [event]
        name=start

        [unit]
            type=Shadow
            side=4
            id=Shadow
            x,y=27,15
            facing=sw
        [/unit]
        [message]
            speaker=Shadow
            message= _ "Come."
        [/message]
        [kill]
            id=Shadow
        [/kill]
        [move_unit_fake]
            x=27,38
            y=15,12
            type=Shadow
            side=4
        [/move_unit_fake]
        [message]
            speaker=Ardonna
            message= _ "Let’s follow it into the cave."
        [/message]
        [message]
            speaker=Blaust
            message= _ "I don’t know, Bogdush. The chief said he wanted stuff that was helpful for the war."
        [/message]
        [message]
            speaker=Bogdush
            message= _ "It’s a <i>mage</i>! That <i>would</i> be helpful for the war, you idiot! It’s only a pup, and unless I miss my guess, it’s a female too. It will be easy to capture."
        [/message]
        [message]
            speaker=Blaust
            message= _ "What about the others? They look like the dead ones back on the Green Isle."
        [/message]
        [message]
            speaker=Bogdush
            message= _ "They are. It makes our job of killing them that much easier!"
        [/message]
        [modify_unit]
            [filter]
                id=Blaust
            [/filter]
            facing=sw
        [/modify_unit]
        [message]
            speaker=Ardonna
            message= _ "You must be Goblins. You will stand out of my way if you wish to live."
        [/message]
        [message]
            speaker=Bogdush
            message= _ "Oh, it’s a mage that makes jokes too! Let’s go."
        [/message]
    [/event]

    [event]
        name=time over

        [message]
            speaker=narrator
            image=logo.png
            message= _ "You have no more time."
        [/message]
    [/event]

    {HERO_DEATHS}
    {MANAGE_PROFILE}

#undef MOVE_AND_PLACE_BAT
#undef CAVE_TERRAINS
#undef NUMBER_OF_EXTRA_BATS
[/scenario]
