#textdomain wesnoth-httt
[scenario]
    id=07_Crossroads
    name= _ "Crossroads"
    map_file=07_Crossroads.map
    {TURNS 36 32 28}

    next_scenario=08_The_Princess_of_Wesnoth

    {SCENARIO_MUSIC "knolls.ogg"}
    {EXTRA_SCENARIO_MUSIC "wanderer.ogg"}
    {EXTRA_SCENARIO_MUSIC "journeys_end.ogg"}
    {EXTRA_SCENARIO_MUSIC "breaking_the_chains.ogg"}

    #wmllint: local spelling Kojun Herolm NE SE
    [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]

            {TURNS_RUN_OUT}

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

    [story]
        [part]
            story= _ "Konrad, Delfador, and Kalenz swiftly rode east through the wilderlands of Wesnoth."
            {HTTT_BIGMAP}
            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."
            {HTTT_BIGMAP}
            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."
            {HTTT_BIGMAP}
            show_title=no
        [/part]
    [/story]

    {HTTT_TRACK {JOURNEY_07_NEW} }

    {DEFAULT_SCHEDULE}

    [side]
        type=Commander
        id=Konrad
        name= _ "Konrad"
        unrenamable=yes
        profile=portraits/konrad-human.webp
        side=1
        canrecruit=yes
        controller=human
        team_name=rebels
        user_team_name=_"Rebels"
        {GOLD 300 250 200}
        {INCOME 6 4 2}
        [ai]
            passive_leader="yes"
            grouping="offensive"
            aggression="0.0"
            caution="-0.75"
            [goal]
                name=protect_location
                [criteria]
                    x,y=20,28
                [/criteria]
                protect_radius=12
                value=20.0
            [/goal]
            [goal]
                name=protect_location
                [criteria]
                    x,y=16,17
                [/criteria]
                protect_radius=12
                value=10.0
            [/goal]
            [goal]
                name=protect_location
                [criteria]
                    x,y=25,10
                [/criteria]
                protect_radius=10
                value=10.0
            [/goal]
            [goal]
                name=target
                [criteria]
                    id=Delfador
                [/criteria]
                value=3.0
            [/goal]
            [goal]
                name=protect_unit
                [criteria]
                    id=Delfador
                [/criteria]
                protect_radius=6
                value=20.0
            [/goal]
            [goal]
                name=protect_unit
                [criteria]
                    id=Konrad
                [/criteria]
                protect_radius=10
                value=20.0
            [/goal]
            [goal]
                name=target
                [criteria]
                    id=Kalenz
                [/criteria]
                value=3.0
            [/goal]
            [goal]
                name=protect_unit
                [criteria]
                    id=Kalenz
                [/criteria]
                protect_radius=4
                value=20.0
            [/goal]

            [avoid]
                x=9-23,14-19,20-40
                y= 1-9,10-14,16-24
            [/avoid]
        [/ai]

        [village]
            x,y=3,5
        [/village]
        {FLAG_VARIANT long}
    [/side]

#define CROSSROADS_AI_PARAMS
    [ai]
        caution=1.0
        aggression=0.3
        passive_leader=yes
    [/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
        id=Kojun Herolm
        name= _ "Kojun Herolm"
        experience=0
        side=2
        controller=ai
        canrecruit=yes
        recruit=Orcish Grunt,Wolf Rider,Orcish Archer,Troll Whelp
        {GOLD 100 140 180}
        team_name=orcs
        user_team_name=_"Orcs"
        {CROSSROADS_AI_PARAMS}
        {FLAG_VARIANT6 ragged}
    [/side]

    {STARTING_VILLAGES 2 18}

    [side]
        type=Orcish Warrior
        id=Mokho Kimer
        name= _ "Mokho Kimer"
        experience=0
        side=3
        controller=ai
        canrecruit=yes
        recruit=Orcish Grunt,Wolf Rider,Orcish Archer,Troll Whelp
        {GOLD 70 110 150}
        team_name=orcs
        user_team_name=_"Orcs"
        {CROSSROADS_AI_PARAMS}

        [village]
            x,y=39,27
        [/village]
        {FLAG_VARIANT6 ragged}
    [/side]

    {STARTING_VILLAGES 3 17}

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

    [event]
        name=start

        [message]
            speaker=Delfador
            message= _ "Here we come to the great cross-roads. We should go northeast."
        [/message]
        [message]
            speaker=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]
            speaker=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

        {MODIFY_UNIT id=Konrad profile "portraits/konrad-human.webp"}

        {NEED_DELFADOR placement=leader}
        {NEED_KALENZ placement=leader}

        # 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.
    [random_placement]
        variable=ambush_location
        num_items={NUMBER}
        [filter_location]
            x={X_SPAN}
            y={Y_SPAN}
            terrain=Hh

            [not]
                [filter_adjacent_location]
                    [not]
                        terrain=Hh*,Mm*,*^F*
                    [/not]
                [/filter_adjacent_location]
            [/not]
        [/filter_location]
        [command]
            [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.

            [if]
                [not]
                    [have_unit]
                        x,y=$ambush_location.x,$ambush_location.y
                    [/have_unit]
                [/not]
                [then]
                    [unit]
                        x,y=$ambush_location.x,$ambush_location.y
                        type=$random_ambusher_type_table[$random_ambusher_type_i].type
                        side={SIDE}
                        facing=sw
                        random_traits=yes

                        [modifications]
                            {TRAIT_LOYAL}
                        [/modifications]
                    [/unit]

                    [object]
                        id=one_time_ambush
                        take_only_once=no

                        [filter]
                            x,y=$ambush_location.x,$ambush_location.y
                        [/filter]

                        [effect]
                            apply_to=new_ability

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

                                    [filter_self]
                                        [filter_location]
                                            terrain=Hh,Mm
                                        [/filter_location]
                                    [/filter_self]
                                [/hides]
                            [/abilities]
                        [/effect]
                    [/object]
                [/then]
            [/if]
        [/command]
    [/random_placement]
    {CLEAR_VARIABLE ambush_location}

#enddef

        # Only the number of ambushers differs by difficulty here.

        # Adjust for leaders killed in S01 ('The Elves Besieged')
        # Reduce 25% for each. Note: implicit floor() due to loop
        # GL-2016Aug
        #
        #             3  2  1  0 # killed
        #
        #      5 -->  1  2  3  5
        #      6 -->  1  3  4  6
        #      7 -->  1  3  5  7
        #
        #      9 -->  2  4  6  9
        #     10 -->  2  5  7 10
        #     11 -->  2  5  8 11
        #
        # Net result
        #
        #   Easy -->  6 12 18 29
        # Normal -->  6 17 23 34
        #   Hard -->  6 17 28 39

        {VARIABLE small_number "$(((4 - $besieged_enemies_killed?0|) * {ON_DIFFICULTY 5 6 7}) / 4)"}
        {VARIABLE large_number "$(((4 - $besieged_enemies_killed?0|) * {ON_DIFFICULTY 9 10 11}) / 4)"}

        {ORC_AMBUSH_AREA 2 5-35 1-7 $small_number}
        {ORC_AMBUSH_AREA 2 9-24 8-15 $small_number}
        {ORC_AMBUSH_AREA 3 1-17 17-24 $large_number}
        {ORC_AMBUSH_AREA 3 18-25 16-23 $small_number}
        {ORC_AMBUSH_AREA 3 26-40 16-23 $small_number}

        {CLEAR_VARIABLE random_ambusher_type_table}
        {CLEAR_VARIABLE random_ambusher_type_i,small_number,large_number}
    [/event]

    [event]
        name=turn refresh
        first_time_only=no

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

            [or]
                [variable]
                    name=side_number
                    numerical_equals=3
                [/variable]
            [/or]

            [then]
                {MODIFY_UNIT side,ability=$side_number,orc_ambush moves 0}
            [/then]
        [/if]
    [/event]

    [event]
        name=moveto
        first_time_only=no

        [filter]
            side=1

            [filter_adjacent]
                side=2,3
                ability=orc_ambush
            [/filter_adjacent]
        [/filter]

        [remove_object]
            [filter_location]
                [filter_adjacent_location]
                    x,y=$x1,$y1
                [/filter_adjacent_location]
            [/filter_location]
            object_id=one_time_ambush
        [/remove_object]
    [/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]

        {NAMED_LOYAL_UNIT 1 (Elvish Archer) 11 13 (Niodien) ( _ "Niodien")}

        [message]
            speaker=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]

        {NAMED_LOYAL_UNIT 1 (Elvish Fighter) 5 15 (Loflar) ( _ "Loflar")}

        [message]
            speaker=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]
            speaker=Konrad
            message= _ "Dan’Tonk, we are so close to Weldyn."
        [/message]
        [message]
            speaker=Delfador
            message= _ "We dare not confront Asheviere yet. We must retrieve the Sceptre of Fire and gather more allies in the north."
        [/message]
    [/event]

    [event]
        name=die
        [filter]
            id=Kojun Herolm
        [/filter]
        [message]
            speaker=Konrad
            message= _ "Victory is ours, men. Let us proceed northeast!"
        [/message]
        [endlevel]
            result=victory
            bonus=yes
            {NEW_GOLD_CARRYOVER 40}
        [/endlevel]
    [/event]

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

#undef CROSSROADS_AI_PARAMS
#undef ORC_AMBUSH_AREA
