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

    {DEFAULT_SCHEDULE}

    {DEFAULT_MUSIC_PLAYLIST}

    {campaigns/Eastern_Invasion/utils/deaths.cfg}

    [side]
        type=Lieutenant
        description=Gweddry
        user_description= _ "Gweddry"
        side=1
        canrecruit=yes
        controller=human
        fog=yes
        {GOLD 200 150 100}
    [/side]

    [side]
        no_leader=yes
        side=2
        colour=blue
        controller=ai
        team_name=baddies
    [/side]

    [side]
        side=3
        colour=green
        controller=ai
        team_name=baddies
        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
            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]
        [unit]
            type="Skeletal Dragon"
            description="Khrakrahs"
            user_description= _ "Khrakrahs"
            x=28
            y=23
            canrecruit=yes
        [/unit]
    [/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]
        [/objectives]

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

                        [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=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=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]
    [/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]
            description=Dacyn
        [/recall]
        [recall]
            description=Owaec
        [/recall]
        [recall]
            description=Grug
        [/recall]
        [recall]
            description=Engineer
        [/recall]
        [message]
            description=Owaec
            message= _ "Now, we have reached the fair homeland of my people. They may be able to help us in - "
        [/message]
        [message]
            description=Owaec
            message= _ "What?! Swamps?! Those undead have destroyed the grasslands of my House, and they shall pay for it!"
        [/message]
        [message]
            description=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]
            description=Engineer
            message= _ "I s'ppose we'll hav' to wade across it anyway... ugh."
        [/message]
        [scroll_to]
            x,y=28,23
        [/scroll_to]
        [message]
            caption= _ "Khrakrahs"
            image=units/random-enemy.png
            message= _ "Findshhhh... slayhhhh... killhhhh..."
        [/message]
        [message]
            description=Gweddry
            message= _ "What is that sound?!?"
        [/message]
        [message]
            description=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]
            description=Khrakrahs
        [/filter]
        [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 flame... this is strange magic indeed."
        [/message]
    [/event]
[/scenario]
