#textdomain wesnoth-ei
[scenario]
    id=13_The_Drowned_Plains
    name= _ "The Drowned Plains"
    map_data="{campaigns/Eastern_Invasion/maps/13_The_Drowned_Plains.map}"
    {TURNS 28 26 24}
    next_scenario=14_Approaching_Weldyn

    {DEFAULT_SCHEDULE}

    {INTRO_AND_SCENARIO_MUSIC heroes_rite.ogg underground.ogg}
    {EXTRA_SCENARIO_MUSIC knolls.ogg}
    {EXTRA_SCENARIO_MUSIC the_king_is_dead.ogg}

    {campaigns/Eastern_Invasion/utils/deaths.cfg}

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

    [side]
        type=Lieutenant
        id=Gweddry
        name= _ "Gweddry"
        side=1
        canrecruit=yes
        controller=human
        team_name=wesnothians
        user_team_name=_"Wesnothians"
        fog=yes
        gold=200
        {FLAG_VARIANT loyalist}
    [/side]

    [side]
        no_leader=yes
        side=2
        color=blue
        controller=ai
        team_name=baddies
        user_team_name=_"Evil"
    [/side]

    [side]
        side=3
        color=green
        controller=ai
        team_name=baddies
        user_team_name=_"Evil"
        gold=0
        no_leader=yes
        [ai]
            #don't target any of the default things
            village_value=0
            leader_value=0
            protect_leader=0
            #stay in the middle of the map
            [avoid]
                [not]
                    x=14-32
                    y=10-45
                [/not]
            [/avoid]
            #and target anyone in it (otherwise it will have nothing to do when
            #it can't attack in the current turn)
            [target]
                #who knows what would happen if it targetted itself
                [not]
                    type="Skeletal Dragon"
                [/not]
                x=14-32
                y=10-45
                value=100.0
            [/target]
            #I don't know if this is needed, but it sure would be stupid to use
            #any grouping behavior when there's only one unit on this side!
            grouping=no
            #not sure if this really does anything; it was copied from IP's code
            aggression=1.0
            leader_aggression=0.4
            caution=0.0
        [/ai]

        #give the skeletal dragon something to do when there are no
        #enemies available let it look like it's wandering around
        #semi-randomly
        [ai]
            [leader_goal]
                x,y=14,10
            [/leader_goal]
            time_of_day=dawn
        [/ai]
        [ai]
            [leader_goal]
                x,y=23,26
            [/leader_goal]
            time_of_day=morning
        [/ai]
        [ai]
            [leader_goal]
                x,y=23,26
            [/leader_goal]
            time_of_day=afternoon
        [/ai]
        [ai]
            [leader_goal]
                x,y=32,32
            [/leader_goal]
            time_of_day=dusk
        [/ai]
        [ai]
            [leader_goal]
                x,y=32,10
            [/leader_goal]
            time_of_day=first_watch
        [/ai]
        [ai]
            [leader_goal]
                x,y=14,32
            [/leader_goal]
            time_of_day=second_watch
        [/ai]
    [/side]

    [event]
        name=prestart
        [objectives]
            side=1
            [objective]
                description= _ "Defeat Khrakrahs"
                condition=win
            [/objective]
            [objective]
                description= _ "Death of Gweddry"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Dacyn"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Owaec"
                condition=lose
            [/objective]

            {TURNS_RUN_OUT}

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

        [unit]
            type="Skeletal Dragon"
            id="Khrakrahs"
            name= _ "Khrakrahs"
            side=3
            x=28
            y=23
            canrecruit=yes
        [/unit]

        # Note: the code for the ambushers is mostly identical to that of
        # Crossroads in HttT.

        # 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.

        {VARIABLE random_ambusher_type_table[0].type "Bone Shooter"}
        {VARIABLE random_ambusher_type_table[1].type "Revenant"}
        {VARIABLE random_ambusher_type_table[2].type "Shadow"}
        {VARIABLE random_ambusher_type_table[3].type "Wraith"}
        {VARIABLE random_ambusher_type_table[4].type "Necrophage"}

#ifdef NORMAL
        {VARIABLE random_ambusher_type_table[5].type "Banebow"}
#endif

#ifdef HARD
        {VARIABLE random_ambusher_type_table[5].type "Banebow"}
        {VARIABLE random_ambusher_type_table[6].type "Spectre"}
        {VARIABLE random_ambusher_type_table[7].type "Nightgaunt"}
#endif

        {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 UNDEAD_AMBUSH_AREA SIDE X_SPAN Y_SPAN NUMBER
    [store_locations]
        x={X_SPAN}
        y={Y_SPAN}
        terrain=Ss

        [filter_adjacent_location]
            [not]
                terrain=Ss
            [/not]

            [not]
                [filter]
                [/filter]
            [/not]
        [/filter_adjacent_location]

        variable=possible_ambush_locations
    [/store_locations]

    {VARIABLE ambusher 0}

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

        [do]
            {RANDOM 1..$possible_ambush_locations.length}
            {VARIABLE_OP random sub 1}

            # 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}
                        role=ambusher
                        random_traits=yes
                        upkeep=loyal
                    [/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=undead_ambush
                                    affect_self=yes

                                    [filter_self]
                                        role=ambusher

                                        [filter_location]
                                            terrain=Ss
                                        [/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
        {UNDEAD_AMBUSH_AREA 2 1-27 1-32 25}
        {UNDEAD_AMBUSH_AREA 2 28-38 1-21 15}
        {UNDEAD_AMBUSH_AREA 2 38-45 5-28 10}
#endif
#ifdef NORMAL
        {UNDEAD_AMBUSH_AREA 2 1-27 1-32 27}
        {UNDEAD_AMBUSH_AREA 2 28-38 1-21 19}
        {UNDEAD_AMBUSH_AREA 2 38-45 5-28 13}
#endif
#ifdef HARD
        {UNDEAD_AMBUSH_AREA 2 1-27 1-32 29}
        {UNDEAD_AMBUSH_AREA 2 28-38 1-21 21}
        {UNDEAD_AMBUSH_AREA 2 38-45 5-28 16}
#endif

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

    [event]
        name=turn refresh
        first_time_only=no

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

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

    [event]
        name=moveto
        first_time_only=no

        [filter]
            side=1

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

        {MODIFY_UNIT (
            side=2
            role=ambusher

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

        [redraw][/redraw]
    [/event]

    [event]
        name=start
        [recall]
            id=Dacyn
        [/recall]
        [recall]
            id=Owaec
        [/recall]
        [recall]
            id=Grug
        [/recall]
        [recall]
            id=Engineer
        [/recall]
        [message]
            speaker=Owaec
            message= _ "Now, we have reached the Horse Plains, the fair homeland of my people. They may be able to help us in—"
        [/message]
        [message]
            speaker=Owaec
            message= _ "What?! Swamps?! Those undead have destroyed the grasslands of my House, and they shall pay for it!"
        [/message]
        [message]
            speaker=Dacyn
            message= _ "The lich-lord must have flooded the valley. The undead like the swamp. It is death, decay, everything that they are themselves. I can sense them lurking in the swamps, waiting to ambush us."
        [/message]
        [message]
            speaker=Engineer
            message= _ "I s’ppose we’ll hav’ to wade across it anyway... ugh."  # wmllint: no spellcheck
        [/message]
        [scroll_to]
            x,y=28,23
        [/scroll_to]
        [message]
            caption= _ "Khrakrahs"
            image=units/unknown-unit.png
            message= _ "Findshhhh... slayhhhh... killhhhh..."   # wmllint: no spellcheck
        [/message]
        [message]
            speaker=Gweddry
            message= _ "What is that sound?!"
        [/message]
        [message]
            speaker=Dacyn
            message= _ "I do not know, but my guess is the dragon Khrakrahs. He was a powerful beast indeed, in the time of Haldric, living in the Northern Mountains. But he does not sound alive. I know not what happened, but we must surely destroy him."
        [/message]
    [/event]

    [event]
        name=sighted
        [filter]
            id=Khrakrahs
        [/filter]
        [fire_event]
            name=KK_spotted
        [/fire_event]
    [/event]
    [event]
        name=moveto
        [filter]
            id=Khrakrahs
            [filter_vision]
                viewing_side=1
            [/filter_vision]
        [/filter]
        [fire_event]
            name=KK_spotted
        [/fire_event]
    [/event]

    [event]
        name=KK_spotted
        [redraw]
            side=1
        [/redraw]
        [scroll_to]
            x,y=$x1,$y1
        [/scroll_to]
        [message]
            caption= _ "Dacyn"
            image=portraits/dacyn.png
            message= _ "Aha! I see what they have done. They have raised him as an undead dragon. But he does not appear to be weak to my arcane senses... this is strange magic indeed."
        [/message]
    [/event]

    [event]
        name=enemies defeated

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

    {WESNOTH_DEFEAT}
[/scenario]
