#textdomain wesnoth-l
[scenario]
    id=hide_and_seek
    next_scenario=the_gray_woods
    victory_when_enemies_defeated=no
    name= _ "Hide and Seek"
    map_data="{campaigns/Liberty/maps/hide_and_seek.map}"
    {TURNS 34 30 26}

    [time]
        id=midnight
        name= _ "Midnight"
        image=schedule-midnight.png
        lawful_bonus=-25
    [/time]

    {INTRO_AND_SCENARIO_MUSIC wanderer.ogg underground.ogg}
    {EXTRA_SCENARIO_MUSIC revelation.ogg}
    {EXTRA_SCENARIO_MUSIC nunc_dimittis.ogg}
    # the music will change if the guards see you

    [story]
        [part]
            show_title=yes
            {TO_HIDE_AND_SEEK}
        [/part]
    [/story]

    [side]
        type=Bandit
        id=Baldras
        side=1
        canrecruit=yes
        controller=human
        recruit=Thug,Footpad,Poacher
        gold=0
        income=-2
        village_gold=0
        team_name=good_guys
        user_team_name=_"Rebels"
        shroud=no
        fog=yes
        share_maps=no
        share_view=no
    [/side]

    [side]
        type=Master Bowman
        id=Quentin
        name= _ "Quentin"
        side=2
        canrecruit=yes
        controller=ai
        ai_algorithm=idle_ai
        user_team_name=_"Asheviere"
        team_name=bad_guys
        [ai]
            passive_leader=yes
        [/ai]
        shroud=no
        fog=yes
    [/side]

    [side]
        no_leader=yes
        side=3
        colour=blue
        controller=ai
        team_name=bad_guys
        user_team_name=_"Asheviere"
        [ai]
            aggression=2.0
            caution=0.0
            grouping=no
            simple_targeting=yes
        [/ai]
        shroud=no
        fog=yes
    [/side]

    {STARTING_VILLAGES_ALL 2}

    [side]
        type=Rogue
        id=Hans
        name= _ "Hans"
        side=4
        colour=green
        canrecruit=yes
        controller=ai
        facing=sw
        [ai]
            passive_leader=yes
        [/ai]
        team_name=good_guys
        user_team_name=_"Rebels"
        shroud=no
        fog=yes
        share_maps=no
        share_view=no
    [/side]

    #
    # Prestart events
    #
    [event]
        name=prestart

        # {SCATTER_IMAGE (terrain,y=Gg,1-25) 14 scenery/pine1.png}
        {PLACE_IMAGE "scenery/oak-leaning.png~FL(horiz)" 12 37}

        [store_side]
            side=1
            variable=stored_side_1
        [/store_side]

        [objectives]
            side=1
            [objective]
                description= _ "Take Baldras to meet the leader of the insurgency"
                condition=win
            [/objective]
            [objective]
                description= _ "Death of Baldras"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Harper"
                condition=lose
            [/objective]
            [objective]
                description= _ "Turns run out"
                condition=lose
            [/objective]
        [/objectives]

        #some various city props
        {PLACE_IMAGE scenery/well.png 11 36}

        {PLACE_IMAGE items/scarecrow.png 15 22}

        {PLACE_IMAGE items/straw-bale1.png 1 22}
        {PLACE_IMAGE "items/straw-bale2.png~FL(horiz)" 2 22}
        {PLACE_IMAGE "items/straw-bale1.png~FL(horiz)" 1 23}
        {PLACE_IMAGE items/straw-bale1.png 31 27}

        {PLACE_IMAGE "items/archery-target-right.png~FL(horiz)" 4 44}
        {PLACE_IMAGE "items/archery-target-right.png~FL(horiz)" 6 45}
        {PLACE_IMAGE "items/archery-target-right.png~FL(horiz)" 5 47}

        # Here we preplace a few guards

#define SEEKER X Y TYPE
    [unit]
        type={TYPE}
        generate_name=yes
        x,y={X},{Y}
        side=2
        random_traits=yes
    [/unit]
#enddef

        {SEEKER 3 42 (Iron Mauler)}
        {SEEKER 15 37 (Shock Trooper)}
        {SEEKER 17 38 (Shock Trooper)}
        {SEEKER 2 45 (Bowman)}
        {SEEKER 3 47 (Bowman)}

        # Here we approximate the vision area extending around all units and
        # initialize the loop variables, both of which we need when randomly
        # placing the guards below

        [store_locations]
            [filter]
            [/filter]

            radius=6

            [filter_radius]
                terrain=Gg,R*,Re^Gvs,*^V*,C*
            [/filter_radius]

            variable=total_vision_area
        [/store_locations]

        {VARIABLE guards 1}
        {VARIABLE loops 0}

        # This loop which places most of the guards works like this:
        #
        # - generate a new guard (at 1,1, no real location for it yet)
        # - figure out the vision range of this unit (dependant on unit type and
        #   traits) and a few other helper variables
        # - store all the locations on which the vision range of the new guard
        #   wouldn't touch the vision range of any other unit.
        # - pick one of those locations randomly, and place the new guard there.
        # - pick a random location which satisfies the same condition (but
        #   ignoring the vision range of the new guard) around the new guard,
        #   and place an assistant guard there, so guards are almost always
        #   paired.
        # - update the total vision range with those of the new guards.
        # - repeat until 20 guards are placed or we've repeated 25 times.
        #
        # Note that when calculating vision ranges of units, the vision range is
        # only approximated, by only allowing the simulated vision range to
        # penetrate terrain=Gg,R*,Re^Gvs,*^V*,C*. However, this is a good enough
        # estimate when the map doesn't contain thin walls of other terrains.
        #
        # The result is the map filled with randomly placed pairs of guards,
        # which all have a hex or two of space between their vision ranges, thus
        # allowing the player to sneak through the map without being spotted.
        # It might not be perfect, but it seems to work perfectly most of the
        # time.

        [while]
            [variable]
                name=guards
                less_than=20
            [/variable]

            [variable]
                name=loops
                less_than=25
            [/variable]

            [do]
                {VARIABLE_OP guard_type random "Shock Trooper"}

                [unit]
                    x,y=1,1
                    side=2
                    type=$guard_type
                    random_traits=yes
                    generate_name=yes
                [/unit]

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

                    kill=yes
                    variable=stored_guard
                [/store_unit]

                [if]
                    [variable]
                        name=stored_guard.modifications.trait[0].id
                        not_equals=quick
                    [/variable]

                    [variable]
                        name=stored_guard.modifications.trait[1].id
                        not_equals=quick
                    [/variable]

                    [then]
                        {VARIABLE guard_MP $stored_guard.max_moves}
                    [/then]

                    [else]
                        {VARIABLE guard_MP $stored_guard.max_moves}
                        {VARIABLE_OP guard_MP add 1}
                    [/else]
                [/if]

                {VARIABLE guard_vision_range $guard_MP}
                {VARIABLE_OP guard_vision_range add 1}

                {VARIABLE guard_vision_range_plus_1 $guard_vision_range}
                {VARIABLE_OP guard_vision_range_plus_1 add 1}

                {VARIABLE guard_vision_range_plus_3 $guard_vision_range}
                {VARIABLE_OP guard_vision_range_plus_3 add 3}

                [store_locations]
                    [and]
                        find_in=total_vision_area
                        radius=$guard_vision_range_plus_3

                        [filter_radius]
                            terrain=Gg,R*,Re^Gvs,*^V*,C*
                        [/filter_radius]
                    [/and]

                    [not]
                        find_in=total_vision_area
                        radius=$guard_vision_range_plus_1

                        [filter_radius]
                            terrain=Gg,R*,Re^Gvs,*^V*,C*
                        [/filter_radius]
                    [/not]

                    [and]
                        terrain=Gg,R*,Re^Gvs,*^V*,C*
                    [/and]

                    variable=new_guard_locations
                [/store_locations]

                {IF_VAR new_guard_locations.length greater_than 0 (
                    [then]
                        {VARIABLE_OP random_i random "1..$new_guard_locations.length"}
                        {VARIABLE_OP random_i add -1}

                        {VARIABLE stored_guard.x $new_guard_locations[$random_i].x}
                        {VARIABLE stored_guard.y $new_guard_locations[$random_i].y}

                        [unstore_unit]
                            variable=stored_guard
                        [/unstore_unit]

                        [store_locations]
                            [and]
                                x,y=$stored_guard.x,$stored_guard.y
                                radius=2
                            [/and]

                            [not]
                                find_in=total_vision_area
                                radius=7

                                [filter_radius]
                                    terrain=Gg,R*,Re^Gvs,*^V*,C*
                                [/filter_radius]
                            [/not]

                            [not]
                                terrain=*^F*,Hh,Ww,Wo,Ss,Ds
                            [/not]

                            variable=new_assistant_guard_locations
                        [/store_locations]

                        {IF_VAR new_assistant_guard_locations.length greater_than 0 (
                            [then]
                                {VARIABLE_OP random_i random "1..$new_assistant_guard_locations.length"}
                                {VARIABLE_OP random_i add -1}

                                {VARIABLE_OP assistant_guard_type random "Shock Trooper,Shock Trooper,Iron Mauler,Javelineer,Swordsman,Pikeman"}

                                [unit]
                                    type=$assistant_guard_type
                                    side=2
                                    x,y=$new_assistant_guard_locations[$random_i].x,$new_assistant_guard_locations[$random_i].y
                                    generate_name=yes
                                    random_traits=yes
                                [/unit]

                                [store_locations]
                                    [and]
                                        x,y=$new_assistant_guard_locations[$random_i].x,$new_assistant_guard_locations[$random_i].y
                                        radius=6

                                        [filter_radius]
                                            terrain=Gg,R*,Re^Gvs,*^V*,C*
                                        [/filter_radius]
                                    [/and]

                                    [or]
                                        find_in=total_vision_area
                                    [/or]

                                    variable=total_vision_area
                                [/store_locations]
                            [/then]
                        )}

                        [store_locations]
                            [and]
                                x,y=$stored_guard.x,$stored_guard.y
                                radius=$guard_vision_range

                                [filter_radius]
                                    terrain=Gg,R*,Re^Gvs,*^V*,C*
                                [/filter_radius]
                            [/and]

                            [or]
                                find_in=total_vision_area
                            [/or]

                            variable=total_vision_area
                        [/store_locations]

                        {VARIABLE_OP guards add 1}
                    [/then]
                )}

                {VARIABLE_OP loops add 1}
            [/do]
        [/while]

        {CLEAR_VARIABLE total_vision_area}
        {CLEAR_VARIABLE new_guard_locations}
        {CLEAR_VARIABLE new_assistant_guard_locations}
        {CLEAR_VARIABLE guard_type}
        {CLEAR_VARIABLE assistant_guard_type}
        {CLEAR_VARIABLE stored_guard}
        {CLEAR_VARIABLE guard_MP}
        {CLEAR_VARIABLE guard_vision_range}
        {CLEAR_VARIABLE guard_vision_range_plus_1}
        {CLEAR_VARIABLE guard_vision_range_plus_3}
        {CLEAR_VARIABLE guards}
        {CLEAR_VARIABLE loops}

        [store_unit]
            [filter]
                id=Baldras
            [/filter]

            kill=yes
            variable=stored_Baldras
        [/store_unit]

        [unit]
            side=1
            type=Boat
            id=Boat
            x,y=1,4
        [/unit]

        [hide_unit]
            x,y=1,4
        [/hide_unit]

        [item]
            x,y=1,4
            image=units/transport/boat.png
        [/item]
    [/event]

    #
    # Starting conversation and actions
    #
    [event]
        name=start

        [message]
            speaker=narrator
            message= _ "Relentlessly pursued by riders patrolling the road to Elensefar, Baldras and his men traveled unseen along the swampy banks of the Great River to the city of Carcyn."
            image="wesnoth-icon.png"
        [/message]
        [message]
            speaker=narrator
            message= _ "They hid until nightfall, then crept out to find the help Lord Maddock hinted was here."
            image="wesnoth-icon.png"
        [/message]

        [kill]
            x,y=1,4
            animate=no
            fire_event=no
        [/kill]

        [removeitem]
            x,y=1,4
        [/removeitem]

        [move_unit_fake]
            side=1
            type=Boat
            x=1,7
            y=4,5
        [/move_unit_fake]

        [item]
            x,y=7,5
            image=units/transport/boat.png
        [/item]

        [move_unit_fake]
            side=1
            type=$stored_Baldras.type
            x=7,7
            y=5,6
        [/move_unit_fake]

        [unstore_unit]
            variable=stored_Baldras
        [/unstore_unit]

        [redraw]
            side=1
        [/redraw]

        [recall]
            id=Harper
            x,y=8,5
        [/recall]

        [message]
            speaker=Baldras
            message= _ "I'm not sure where we're supposed to go now. If we go into town we can start asking around for information."
        [/message]

        [move_unit_fake]
            type=Thief
            side=4
            x=7,6
            y=9,6
        [/move_unit_fake]

        [unit]
            type=Thief
            side=4
            x,y=6,6
            random_traits=yes
            id=Link
            name= _ "Link"
        [/unit]

        [message]
            speaker=Baldras
            message= _ "Who are you?"
        [/message]
        [message]
            speaker=Link
            message= _ "I have come to find you. You must make it through the city without being seen. The soldiers are normally out hunting for my kind, but a company of heavy infantry arrived three days ago and took over the patrols."
        [/message]
        [message]
            speaker=Harper
            message= _ "They're looking for us."
        [/message]
        [message]
            speaker=Link
            message= _ "Well, good thing I found you first. Now hurry, you must make it past the city into the Grey Woods. There is a small path in the southeast that leads into the forest; look for the signpost. My brother Hans is waiting for you there and will take you to our master. Try to avoid the soldiers if you can."
        [/message]

        {HIGHLIGHT_IMAGE 26 48 scenery/signpost.png ()}

        [scroll_to_unit]
            id=Baldras
        [/scroll_to_unit]

        {MOVE_UNIT id=Link 5 8}

        [kill]
            id=Link
            animate=no
            fire_event=no
        [/kill]

        [redraw]
        [/redraw]

        {CLEAR_VARIABLE stored_Baldras}
    [/event]

    [event]
        name=sighted
        [filter]
            side=2
        [/filter]
        [filter_second]
            side=1
        [/filter_second]

        [redraw]
            side=1
        [/redraw]

        [message]
            speaker=second_unit
            message= _ "Looks like these guards hold stationary posts, so if we're careful enough we should be able to slip our way around them unnoticed."
        [/message]
        [message]
            speaker=Baldras
            message= _ "Harper, you younger and faster folks need to be our eyes now. Scout ahead and try to find a way for us to get past the guards, but make sure to not stay where they can see you or we're all in big trouble..."
        [/message]
    [/event]

    [event]
        name=side turn
        first_time_only=no

        [if]
            [variable]
                name=side_number
                equals=2
            [/variable]

            [then]
                [if]
                    [have_unit]
                        side=1

                        [filter_vision]
                            visible=yes
                            viewing_side=2
                        [/filter_vision]

                        [or]
                            side=1

                            [filter_vision]
                                visible=yes
                                viewing_side=3
                            [/filter_vision]
                        [/or]
                    [/have_unit]

                    [then]
                        # if player is visible to enemies, flip all nearby enemies to side 3,
                        # which is the aggressive side and thus the nearby enemies will begin
                        # to pursue the player

                        [store_unit]
                            [filter]
                                side=2

                                [filter_location]
                                    [filter]
                                        side=1
                                    [/filter]

                                    {QUANTITY radius 8 11 13}
                                [/filter_location]
                            [/filter]

                            kill=yes
                            variable=side_2_units_nearby
                        [/store_unit]

                        {FOREACH side_2_units_nearby i}
                            {VARIABLE side_2_units_nearby[$i].side 3}

                            [unstore_unit]
                                variable=side_2_units_nearby[$i]
                                find_vacant=no
                            [/unstore_unit]
                        {NEXT i}

                        {CLEAR_VARIABLE side_2_units_nearby}
                    [/then]

                    [else]
                        # if player isn't seen by enemies anymore, make all enemies idle again

                        {MODIFY_UNIT side=3 side 2}
                    [/else]
                [/if]
            [/then]
        [/if]
    [/event]

    [event]
        name=moveto
        [filter]
            side=3
        [/filter]

        {INCIDENTAL_MUSIC vengeful.ogg}

        [message]
            speaker=Harper
            message= _ "They've seen us, RUN!"
        [/message]
    [/event]

    [event]
        name=attack_end
        [filter]
            side=2
        [/filter]
        [filter_second]
            side=1
        [/filter_second]

        {IF_VAR $second_unit.hitpoints greater_than 0 (
            [then]
                [message]
                    speaker=Baldras
                    message= _ "Fall back, quickly! Maybe we can yet lose them!"
                [/message]
            [/then]
        )}
    [/event]

    [event]
        name=sighted
        [filter]
            id=Hans
        [/filter]
        [filter_second]
            side=1
        [/filter_second]
        [message]
            speaker=unit
            message= _ "Gentlemen, please come this way and follow the path into the forest. Our leader is anxious to meet you."
        [/message]
        [message]
            speaker=second_unit
            message= _ "As soon as we escape the Wesnoth death squad, we will be happy to oblige."
        [/message]
        [message]
            speaker=Baldras
            message= _ "I haven't run this much in years. If you were to ask me to infiltrate a heavily guarded city relying on nothing but the cover of darkness and my wits, I would say you were crazy."
        [/message]
        [message]
            speaker=Baldras
            message= _ "As it turns out, I'm the crazy one. Let's finish this folly."
        [/message]
    [/event]

    #
    # Special event - if someone besides Baldras goes to the end, you get a
    # message
    #
    [event]
        name=moveto

        [filter]
            side=1

            [filter_adjacent]
                id=Hans
            [/filter_adjacent]

            [not]
                id=Baldras
            [/not]
        [/filter]

        [message]
            speaker=Hans
            message= _ "You can come if you wish, but the leader is only interested in speaking to Baldras."
        [/message]

        [allow_undo][/allow_undo]
    [/event]

    #
    # Victory
    #
    [event]
        name=moveto

        [filter]
            x=26
            y=48
            id=Baldras
        [/filter]

        [message]
            speaker=Hans
            message= _ "Fly, and we may yet evade them in the Grey Woods."
        [/message]

        [message]
            speaker=Baldras
            message= _ "Lead the way..."
        [/message]

        [modify_side]
            side=1
            gold=$stored_side_1.gold
        [/modify_side]

        {CLEAR_VARIABLE stored_side_1}

        [endlevel]
            result=victory
            bonus=no
            {NEW_GOLD_CARRYOVER 100}
        [/endlevel]
    [/event]

    #
    # Loss conditions - turns run out
    #
    [event]
        name=time over
        [message]
            speaker=Baldras
            message= _ "We have spent too much time here. Surely the Queen's forces have returned to Dallben. Our mission is unfinished, but we must return to fight a suicide battle."
        [/message]
        [endlevel]
            result=defeat
        [/endlevel]
    [/event]

    {LIBERTY_DEATHS}

    {VILLAGE_BURNED}
[/scenario]
