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

    {EI_TRACK {JOURNEY_13_NEW} }

    [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
            [goal]
                name=protect_unit
                [criteria]
                    side=3
                    canrecruit=yes
                [/criteria]
                value=0
            [/goal]
            #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)
            [goal]
                name=target
                [criteria]
                    #who knows what would happen if it targetted itself
                    [not]
                        type="Skeletal Dragon"
                    [/not]
                    x=14-32
                    y=10-45
                [/criteria]
                value=100
            [/goal]
            #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]

        #buff the dragon with some magic resistances
        [object]
            name= _ "Null Stone"
            duration=forever
            silent=yes
            [filter]
                id=Khrakrahs
            [/filter]

            [effect]
                apply_to=resistance
                replace=yes
                [resistance]
                    arcane=60
                    cold=30
                    fire=60
                [/resistance]
            [/effect]
        [/object]

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

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

                [not]
                    [filter]
                    [/filter]
                [/not]
            [/filter_adjacent_location]
        [/filter_location]
        [command]
            {VARIABLE random_ambusher_type_i ("$(($random_ambusher_type_i + 1) % $random_ambusher_type_table.length)")}
            [unit]
                x,y=$ambush_location.x,$ambush_location.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=$ambush_location.x,$ambush_location.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]
        [/command]
    [/random_placement]
#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]
        [recall]
            id=Terraent
        [/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 find—"
        [/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 was that?!"
        [/message]
        [message]
            speaker=Dacyn
            message= _ "That was the dragon Khrakrahs. He was a powerful beast indeed in the time of Haldric, living in the Northern Mountains. But he radiates a dark power these days, and I know not why he has taken up residence this far south. We must find him, and pray that he still has the Null Stone."
        [/message]
    [/event]

    [event]
        name=sighted
        [filter]
            id=Khrakrahs
        [/filter]
        [filter_second]
            side=1
        [/filter_second]
        [fire_event]
            name=KK_spotted
        [/fire_event]
    [/event]
    [event]
        name=moveto
        [filter]
            id=Khrakrahs
            [filter_vision]
                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]
            speaker=Dacyn
            message= _ "Ah! It appears that the mighty Khrakrahs is no longer among the living. But this undead abomination does not appear weak to my arcane powers... he must still have the Null Stone!"
        [/message]
    [/event]

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

    [event]
        name=victory

        [message]
            speaker=Dacyn
            message= _ "The Null Stone! With it we may finally defeat Mal-Ravanal!"
        [/message]

        #not really necessary, but it's nice for a quest item to actually *do* something
        #besides, this makes forcing Dacyn's recall during "The Duel" less annoying...
        [object]
            name= _ "Null Stone"
            duration=forever
            silent=yes
            [filter]
                id=Dacyn
            [/filter]

            [effect]
                apply_to=resistance
                replace=yes
                [resistance]
                    arcane=30
                    cold=70
                    fire=50
                [/resistance]
            [/effect]
        [/object]
    [/event]

    {WESNOTH_DEFEAT}

    {HERODEATH_GWEDDRY}
    {HERODEATH_DACYN}
    {HERODEATH_OWAEC}
    {HERODEATH_GRUG}
    {HERODEATH_ENGINEER}
[/scenario]
