#textdomain wesnoth-httt
[scenario]
    id=07_Crossroads
    name= _ "Crossroads"
    map_data="{campaigns/Heir_To_The_Throne/maps/07_Crossroads.map}"
    {SCENARIO_MUSIC "battle.ogg"}
    {VICTORY_AND_DEFEAT_MUSIC}
    {TURNS 37 34 31}

    next_scenario=08_The_Princess_of_Wesnoth

    [event]
        name=prestart
        [objectives]
            side=1
            [objective]
                description= _ "Defeat Kojun Herolm"
                condition=win
            [/objective]
            [objective]
                description= _ "Death of Konrad"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Delfador"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Kalenz"
                condition=lose
            [/objective]
        [/objectives]
    [/event]

    [story]
        [part]
            story= _ "Konrad, Delfador, and Kalenz swiftly rode east through the wilderlands of Wesnoth."
            background="maps/wesnoth-httt.jpg"
            show_title=no
        [/part]
        [part]
            story= _ "The queen's agents were still scouring the coastal regions, for news of Elensefar's rescue had not yet reached her."
            background="maps/wesnoth-httt.jpg"
            show_title=no
        [/part]
        [part]
            story= _ "Small as it was, Konrad's army avoided the notice of hostile eyes in the sparsely populated western countryside for a time. However, their luck did not hold."
            background="maps/wesnoth-httt.jpg"
            show_title=no
        [/part]
    [/story]

    {BIGMAP_CROSSROADS}

    {DEFAULT_SCHEDULE}

    [side]
        type=Commander
        description=Konrad
        user_description= _ "Konrad"
        unrenamable=yes
        side=1
        canrecruit=yes
        controller=human
        {GOLD 300 200 200}
#ifdef EASY
        {INCOME 10 0 0}
#endif
        [ai]
            passive_leader="yes"
            grouping="offensive"
            aggression="0.0"
            caution="-0.75"
            [protect_location]
                x=20
                y=28
                radius="12"
                value="20.0"
            [/protect_location]
            [protect_location]
                x=16
                y=17
                radius="12"
                value="10.0"
            [/protect_location]
            [protect_location]
                x=25
                y=10
                radius="10"
                value="10.0"
            [/protect_location]
            [target]
                description=Delfador
                value=3.0
            [/target]
            [protect_unit]
                description=Delfador
                radius=6
                value=20.0
            [/protect_unit]
            [protect_unit]
                description=Konrad
                radius=10
                value=20.0
            [/protect_unit]
            [target]
                description=Kalenz
                value=3.0
            [/target]
            [protect_unit]
                description=Kalenz
                radius=4
                value=20.0
            [/protect_unit]

            [avoid]
                x="9-23"
                y="1-9"
            [/avoid]
            [avoid]
                x="14-19"
                y="10-14"
            [/avoid]
            [avoid]
                x="20-40"
                y="16-24"
            [/avoid]
            {ATTACK_DEPTH 5 5 5}
        [/ai]

        [village]
            x,y=3,5
        [/village]
    [/side]

#define CROSSROADS_AI_PARAMS
    [ai]
        caution=1.0
        aggression=0.3
        passive_leader=yes
        {ATTACK_DEPTH 1 3 5}
    [/ai]
    [ai]
        time_of_day=dusk
        turns=8-100
        caution=0.25
        aggression=0.6
    [/ai]
    [ai]
        time_of_day=first_watch,second_watch
        caution=0.0
        aggression=1.0
        grouping=no
    [/ai]
#enddef

    [side]
        type=Orcish Warlord
        description=Kojun Herolm
        user_description= _ "Kojun Herolm"
        experience=0
        side=2
        controller=ai
        canrecruit=yes
        recruit=Orcish Grunt,Wolf Rider,Orcish Archer,Troll Whelp
        {GOLD 70 130 190}
        team_name=orcs
        {CROSSROADS_AI_PARAMS}
    [/side]

    {STARTING_VILLAGES 2 18}

    [side]
        type=Orcish Warrior
        description=Mokho Kimer
        user_description= _ "Mokho Kimer"
        experience=0
        side=3
        controller=ai
        canrecruit=yes
        recruit=Orcish Grunt,Wolf Rider,Orcish Archer,Troll Whelp
        {GOLD 40 90 150}
        team_name=orcs
        {CROSSROADS_AI_PARAMS}

        [village]
            x,y=39,27
        [/village]
    [/side]

    {STARTING_VILLAGES 3 17}

    #signs at the crossroads for decoration
    {PLACE_IMAGE scenery/signpost.png 16 16}

    [event]
        name=start
        [recall]
            description=Delfador
        [/recall]
        [recall]
            description=Kalenz
        [/recall]
        [message]
            description=Delfador
            message= _ "Here we come to the great cross-roads. We should go northeast."
        [/message]
        [message]
            description=Kalenz
            message= _ "Beware! These hills are not safe! The roads are important to Asheviere's strategy, and she has hired orcs to guard them. We shall have to fight to travel through."
        [/message]
        [message]
            description=Konrad
            message= _ "Then fight we shall. Fortunately, now that we have liberated Elensefar, they are providing what gold they can spare to help us swell our ranks with new recruits. Let it begin!"
        [/message]
    [/event]

    [event]
        name=prestart

        # Here we set up an array that holds the possible ambusher unit types.
        # We do this instead of just using a random type when creating the
        # ambusher so that the even distribution of different types is
        # guaranteed. For now, Orcish Archer is listed twice, so they appear
        # more often than the other types.

        {VARIABLE random_ambusher_type_table[0].type "Orcish Grunt"}
        {VARIABLE random_ambusher_type_table[1].type "Orcish Archer"}
        {VARIABLE random_ambusher_type_table[2].type "Orcish Archer"}
        {VARIABLE random_ambusher_type_table[3].type "Orcish Assassin"}
        {VARIABLE random_ambusher_type_table[4].type "Goblin Spearman"}

        {VARIABLE random_ambusher_type_i 0}

        # With this macro we place a set number of ambushers (from the above list) to a
        # given rectangular area. We do this in blocks so that we can guarantee that
        # every major area has at least some ambushers: if we randomized the ambush
        # locations across all possible locations at once, the RNG could theoretically
        # clump all the ambushers into a single corner of the map.

#define ORC_AMBUSH_AREA SIDE X_SPAN Y_SPAN NUMBER
    # Any hill that's completely surrounded by other hills, mountains or
    # forest is eligible as an ambush location.

    [store_locations]
        x={X_SPAN}
        y={Y_SPAN}
        terrain=Hh

        [not]
            [filter_adjacent_location]
                [not]
                    terrain=Hh*,Mm*,Gs^Fp
                [/not]
            [/filter_adjacent_location]
        [/not]

        variable=possible_ambush_locations
    [/store_locations]

    {VARIABLE ambusher 0}

    [while]
        [variable]
            name=ambusher
            less_than={NUMBER}
        [/variable]

        [do]
            {RANDOM 0..$possible_ambush_locations.length}

            # Check if the random location picked already contains an ambusher
            # and if it does, we loop again to pick another one until an empty
            # one is found.

            [if]
                [have_unit]
                    x,y=$possible_ambush_locations[$random].x,$possible_ambush_locations[$random].y
                [/have_unit]

                [else]
                    # random_ambusher_type_i is just a counter variable with
                    # which we point to a given element of the
                    # random_ambusher_type_table array. Here we make it loop
                    # over the array continuously.

                    [if]
                        [variable]
                            name=random_ambusher_type_i
                            less_than=4
                        [/variable]

                        [then]
                            {VARIABLE_OP random_ambusher_type_i add 1}
                        [/then]

                        [else]
                            {VARIABLE random_ambusher_type_i 0}
                        [/else]
                    [/if]

                    # And here we place an ambusher of a type specified by the
                    # random_ambusher_type_table and random_ambusher_type_i
                    # variables, thus always having the same ratio of different
                    # types.

                    [unit]
                        x,y=$possible_ambush_locations[$random].x,$possible_ambush_locations[$random].y
                        type=$random_ambusher_type_table[$random_ambusher_type_i].type
                        side={SIDE}
                        facing=sw
                        role=ambusher
                        random_traits=yes

                        [modifications]
                            {TRAIT_LOYAL}
                        [/modifications]

                        [status]
                            hides=yes
                        [/status]
                    [/unit]

                    [object]
                        silent=yes

                        [filter]
                            x,y=$possible_ambush_locations[$random].x,$possible_ambush_locations[$random].y
                        [/filter]

                        [effect]
                            apply_to=new_ability

                            [abilities]
                                [hides]
                                    id=orc_ambush
                                    affect_self=yes

                                    [filter_self]
                                        role=ambusher

                                        [filter_location]
                                            terrain=Hh,Mm
                                        [/filter_location]
                                    [/filter_self]
                                [/hides]
                            [/abilities]
                        [/effect]
                    [/object]

                    {VARIABLE_OP ambusher add 1}
                [/else]
            [/if]
        [/do]
    [/while]
#enddef

        # Only the number of ambushers differs by difficulty here.

#ifdef EASY
        {ORC_AMBUSH_AREA 2 5-35 1-7 5}
        {ORC_AMBUSH_AREA 2 9-24 8-15 5}
        {ORC_AMBUSH_AREA 3 1-17 17-24 9}
        {ORC_AMBUSH_AREA 3 18-26 16-23 5}
        {ORC_AMBUSH_AREA 3 26-40 16-23 5}
#endif
#ifdef NORMAL
        {ORC_AMBUSH_AREA 2 5-35 1-7 6}
        {ORC_AMBUSH_AREA 2 9-24 8-15 6}
        {ORC_AMBUSH_AREA 3 1-17 17-24 10}
        {ORC_AMBUSH_AREA 3 18-26 16-23 6}
        {ORC_AMBUSH_AREA 3 26-40 16-23 6}
#endif
#ifdef HARD
        {ORC_AMBUSH_AREA 2 5-35 1-7 7}
        {ORC_AMBUSH_AREA 2 9-24 8-15 7}
        {ORC_AMBUSH_AREA 3 1-17 17-24 11}
        {ORC_AMBUSH_AREA 3 18-26 16-23 7}
        {ORC_AMBUSH_AREA 3 26-40 16-23 7}
#endif

        {CLEAR_VARIABLE possible_ambush_locations}
        {CLEAR_VARIABLE random_ambusher_type_table}
        {CLEAR_VARIABLE random_ambusher_type_i}
        {CLEAR_VARIABLE ambusher}
    [/event]

    [event]
        name=ai turn
        first_time_only=no

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

            [then]
                {MODIFY_UNIT side,role=2,ambusher moves 0}
            [/then]
        [/if]

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

            [then]
                {MODIFY_UNIT side,role=3,ambusher moves 0}
            [/then]
        [/if]
    [/event]

    [event]
        name=moveto
        first_time_only=no

        [filter]
            side=1

            [filter_adjacent]
                side=2,3
                role=ambusher
            [/filter_adjacent]
        [/filter]

        {MODIFY_UNIT (
        side=2,3
        role=ambusher

        [filter_adjacent]
            x,y=$x1,$y1
        [/filter_adjacent]
        ) role not_ambusher}

        [redraw][/redraw]
    [/event]

    [event]
        name=moveto
        [filter]
            side=1
            x,y=12,11
        [/filter]
        [move_unit_fake]
            type=Elvish Archer
            side=1
            x=12,12,11
            y=14,13,13
        [/move_unit_fake]

        {LOYAL_UNIT 1 (Elvish Archer) 11 13 (Niodien) ( _ "Niodien")}
        # wmllint: recognize Niodien

        [message]
            description=Niodien
            message= _ "Stay on the path! The hills here are not safe!"
        [/message]
    [/event]

    [event]
        name=moveto
        [filter]
            side=1
            x,y=3,15
        [/filter]
        [move_unit_fake]
            type=Elvish Fighter
            side=1
            x=4,5,5
            y=16,16,15
        [/move_unit_fake]

        {LOYAL_UNIT 1 (Elvish Fighter) 5 15 (Loflar) ( _ "Loflar")}
        # wmllint: recognize Loflar

        [message]
            description=Loflar
            message= _ "Beware the hills! There are many orcs in hiding, preparing to ambush you!"
        [/message]
    [/event]

    [event]
        name=moveto
        [allow_undo]
        [/allow_undo]
        [filter]
            side=1
            x=16
            y=16
        [/filter]
        [redraw]
        [/redraw]
        [message]
            speaker=narrator
            image="scenery/signpost.png"
            message= _ "NE - Dan'Tonk
SE - Fort Tahn"
        [/message]
        [message]
            description=Konrad
            message= _ "Dan'Tonk, we are so close to Weldyn."
        [/message]
        [message]
            description=Delfador
            message= _ "We dare not confront Asheviere yet. We must retrieve the Scepter of Fire and gather more allies in the north."
        [/message]
    [/event]

    [event]
        name=die
        [filter]
            description=Kojun Herolm
        [/filter]
        [endlevel]
            result=victory
            bonus=yes
        [/endlevel]
    [/event]

    [event]
        name=victory
        [message]
            description=Konrad
            message= _ "Victory is ours, men. Let us proceed northeast!"
        [/message]
    [/event]

    {campaigns/Heir_To_The_Throne/utils/deaths.cfg}
[/scenario]
