#textdomain wesnoth-tsg
[scenario]
    id=04_Vale_of_Tears

    name= _ "Vale of Tears"
    next_scenario=05_Choice_in_the_Fog

    {SCENARIO_MUSIC elvish-theme.ogg}
    {EXTRA_SCENARIO_MUSIC knolls.ogg}
    {EXTRA_SCENARIO_MUSIC nunc_dimittis.ogg}

    map_data="{campaigns/The_South_Guard/maps/04_Vale_of_Tears.map}"

    {campaigns/The_South_Guard/utils/sg_deaths.cfg}

    {DEFAULT_SCHEDULE}

    {TURNS 32 28 24}
    victory_when_enemies_defeated=no

    {TSG_BIGMAP {JOURNEY_04_NEW} }

    # The South Guard

    [side]
        side=1
        type=Horseman Commander
        id=Deoran
        name= _ "Deoran"
        profile=portraits/deoran.png
        unrenamable=yes

        team_name=South_Guard
        user_team_name=_"South Guard"
        controller=human
        {CUSTOM_SG_FLAG}

        {GOLD 120 110 100}

        canrecruit=yes
        recruit=Bowman, Spearman, Cavalryman
    [/side]

    # The Undead Hordes

    [side]
        side=2
#ifdef EASY
        type=Dark Adept
#endif
#ifdef NORMAL
        type=Dark Sorcerer
        advances_to=Necromancer
#endif
#ifdef HARD
        type=Dark Sorcerer
        advances_to=Necromancer
#endif
        id="Mal A'kai"
        name= _ "Mal A’kai"
        facing=sw

        {GOLD 70 85 100}

        team_name=undead
        user_team_name=_"Undead"
        controller=ai

        canrecruit=yes
        [ai]
#ifdef EASY
            recruitment_pattern=fighter
#else
            recruitment_pattern=fighter,archer
#endif
            {QUANTITY aggression 0.5 0.6 0.7}
            {ATTACK_DEPTH 3 4 5}
            [goal]
                name=target
                [criteria]
                    id=Ethiliel
                [/criteria]
                value=3
            [/goal]
        [/ai]
#ifdef EASY
        recruit=Walking Corpse,Skeleton
#endif
#ifdef NORMAL
        recruit=Walking Corpse,Skeleton, Skeleton Archer,Ghoul
#endif
#ifdef HARD
        recruit=Walking Corpse,Skeleton, Revenant, Skeleton Archer,Bone Shooter,Ghoul
#endif
        {FLAG_VARIANT undead}
    [/side]

    {STARTING_VILLAGES 2 14}

    # Do this here since these circles overlap, we want side 2 to have most of the villages and side 1 to have fewer.
    {STARTING_VILLAGES 1 5}

    # The Bandit Army

    [side]
        side=3
        type=Outlaw
        id=Jera Ilras
        name= _ "Jera Ilras"
        facing=sw

        {GOLD 40 55 70}

        team_name=undead
        user_team_name=_"Bandits"
        controller=ai

        canrecruit=yes
        [ai]
#ifdef EASY
            recruitment_pattern=scout,archer
#else
            recruitment_pattern=fighter,scout,archer
#endif
            {QUANTITY aggression 0.2 0.3 0.4}

            [goal]
                name=target
                [criteria]
                    id=Ethiliel
                [/criteria]
                value=2
            [/goal]
            [avoid]
                x,y=21-29,25-32
            [/avoid]
        [/ai]
#ifdef EASY
        recruit=Footpad,Poacher
#endif
#ifdef NORMAL
        recruit=Footpad,Thug,Poacher
#endif
#ifdef HARD
        recruit=Footpad,Thug,Poacher,Thief
#endif
        {FLAG_VARIANT6 ragged}
    [/side]

    # This prevents the magican from getting himself killed easily by not
    # leaving him off his castle.
    [event]
        name=side 2 turn refresh
        first_time_only=no
        # Exception to avoid the leader being passive even after his army is defeated.
        # If the leader recruited one unit last turn, they can leave the castle together.
        [filter_condition]
            [not]
                [have_unit]
                    side=2
                    canrecruit=no
                    count=1
                [/have_unit]
                [have_unit]
                    side=2
                    canrecruit=no
                    [filter_location]
                        terrain=C*^*
                    [/filter_location]
                    count=1
                [/have_unit]
            [/not]
        [/filter_condition]

        [object]
            silent=yes
            duration=turn end
            take_only_once=no
            [filter]
                id="Mal A'kai"
                # Apply the movement restriction only if he is in the keep or castle,
                # so he can still go back if he left due to the exception above.
                [filter_location]
                    terrain=K*^*,C*^*
                [/filter_location]
            [/filter]
            [effect]
                apply_to=movement_costs
                replace=yes
                [movement_costs]
                    flat={UNREACHABLE}
                    swamp_water={UNREACHABLE}
                [/movement_costs]
            [/effect]
        [/object]
    [/event]

    {STARTING_VILLAGES 3 9}

    # Start

    [event]
        name=start
        [recall]
            id=Sir Gerrick
        [/recall]
        [recall]
            id=Ethiliel
        [/recall]
        [recall]
            id=Minister Hylas
        [/recall]
        [recall]
            id=Jarek
        [/recall]

        [message]
            speaker=Ethiliel
            message= _ "This is the Vale of Blossoming Trees. Our great sages come here to find peace and enlightenment. Some other elves also live nearby — mostly artisans inspired by the tranquility of this valley."
        [/message]

        {HIGHLIGHT_IMAGE 25 28 items/gohere.png ()}

        [message]
            speaker=Sir Gerrick
            message= _ "It does seem peaceful, but why are there humans camped to the south of us? Are they friends of the elves?"
        [/message]

        [message]
            speaker=Ethiliel
            image=portraits/ethiliel-mad.png
            message= _ "What?! Those marauders are no friends of ours! We must destroy them at once!"
        [/message]

        [message]
            speaker=Deoran
            message= _ "Perhaps they are allied with the bandits who have been sacking Westin. They have grown quite bold if they are attacking the elves!"
        [/message]

        [message]
            speaker=Ethiliel
            message= _ "They would not dare without the aid of the dark ones, but we will vanquish them all nonetheless. Now, listen well to me, humans. The Great Sage Mebrin is ancient and remembers well Haldric’s betrayal of the elves. Should one of you set foot in his village first, he will certainly drive us away!"
        [/message]

        [message]
            speaker=Sir Gerrick
            message= _ "Sounds like a charming fellow."
        [/message]

        [message]
            speaker=Ethiliel
            message= _ "Mebrin can be... difficult, yes. However, should we cleanse the valley and restore peace to his home, I am sure he will accept your request for his aid. That is, so long as I am the one who makes the request to him."
        [/message]

        [message]
            speaker=Deoran
            message= _ "Very well, we will put our trust in you, Ethiliel. But first, we have a battle to attend to."
        [/message]

        [message]
            speaker=Jera Ilras
            message= _ "Ha, you blabbering buffoons! Even should you defeat us here, it does not matter! We’ve already taken what we wanted from this valley."
        [/message]

        [message]
            speaker="Mal A'kai"
            message= _ "Weep, filthy flesh bags, weep and know that your tears will become the blood that stains the trees and feeds our powers!"
        [/message]

        [objectives]
            side=1
            [objective]
                [show_if]
                    [not]
                        [variable]
                            name=undead_defeated
                            boolean_equals=yes
                        [/variable]
                        [variable]
                            name=bandits_defeated
                            boolean_equals=yes
                        [/variable]
                    [/not]
                [/show_if]
                description= _ "Cleanse the Vale of Tears"
                condition=win
            [/objective]
            [objective]
                [show_if]
                    [variable]
                        name=mebrin_found
                        boolean_not_equals=yes
                    [/variable]
                [/show_if]
                description= _ "Move Ethiliel to Mebrin’s village"
                condition=win
            [/objective]
            [objective]
                description= _ "Death of Deoran"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Sir Gerrick"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Minister Hylas"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Ethiliel"
                condition=lose
            [/objective]

            {TURNS_RUN_OUT}

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

    [event]
        name=attack_end

        [filter]
            side=1
        [/filter]

        [filter_attack]
            type=pierce
        [/filter_attack]

        [filter_second]
            type_adv_tree=Skeleton,Skeleton Archer
        [/filter_second]

        # Only comment on pierce damage being useless if the player didn't
        # actually kill the skeleton with it.
        [filter_condition]
            [variable]
                name=second_unit.hitpoints
                greater_than=0
            [/variable]
        [/filter_condition]

        [message]
            speaker=unit
            message= _ "Our weapons are useless against these skeletons! They have nothing for our shafts to pierce but air!"
        [/message]
    [/event]

    [event]
        name=attack_end

        [filter]
            side=1
        [/filter]

        [filter_second]
            side=2
        [/filter_second]

        [filter_condition]
            [have_unit]
                side=2
            [/have_unit]
        [/filter_condition]

        [message]
            speaker=Deoran
            message= _ "We must clear out these undead if you are to reach Mebrin’s village, but our weapons are ineffective against them! What should we do?"
        [/message]

        [message]
            speaker=Ethiliel
            message= _ "For now, I can slow them down. But where are the elves who used to live in this valley? They should have come to our aid by now."
        [/message]

        [message]
            speaker=Deoran
            message= _ "I do hope that nothing terrible has happened to them..."
        [/message]
    [/event]

    [event]
        name=last breath

        [filter]
            side=1
            [not]
                id=Deoran,Sir Gerrick,Minister Hylas,Ethiliel
            [/not]
        [/filter]

        [message]
            speaker=unit
            message= _ "My friends, do not let me join these accursed undead! Please, bury me deeply..."
        [/message]

        [kill]
            x,y=$x1,$y1
            animate=yes
        [/kill]

        [message]
            speaker=Ethiliel
            message= _ "You shall not die in vain! My bodyguards will avenge you!"
        [/message]

        [fire_event]
            name=bodyguard
        [/fire_event]
    [/event]

    [event]
        name=bodyguard
        # Catching an endless loop if Ethiliel is not on the map. Can only happen using debug mode.
        [filter_condition]
            [have_unit]
                id=Ethiliel
            [/have_unit]
        [/filter_condition]

        {VARIABLE have_bodyguard yes}

        [message]
            speaker=Sir Gerrick
            message= _ "You have bodyguards?!"
        [/message]

        [message]
            speaker=Deoran
            message= _ "You have bodyguards?!"
        [/message]

#ifdef EASY
        {VARIABLE hexes_needed 4}
#endif
#ifdef NORMAL
        {VARIABLE hexes_needed 3}
#endif
#ifdef HARD
        {VARIABLE hexes_needed 2}
#endif

        {VARIABLE extending 3}

        # We assume that there are always at least $hexes_needed free
        # forest hexes with no units adjacent on the whole map, otherwise
        # this will be and endless loop.
        [while]
            [variable]
                name=bodyguard_loc.length
                less_than=$hexes_needed
            [/variable]
            [do]
                [store_locations]
                    terrain=*^F*
                    [not]
                        [filter]
                            # only hexes where no unit is to be matched
                        [/filter]
                        radius=1 # nor adjacent, as with normal ambush ability
                    [/not]
                    [and]
                        [filter]
                            id=Ethiliel
                        [/filter]
                        radius=$extending
                        include_borders=no
                    [/and]
                    variable=bodyguard_loc
                    include_borders=no
                [/store_locations]
                {VARIABLE_OP extending add 1}
            [/do]
        [/while]

        [for]
            start=0
            end=$($hexes_needed-1)
            variable=i
            [do]
                [unit]
                    role=Elvish Bodyguard
                    name= _ "Elvish Bodyguard"
                    type=Elvish Ranger
                    side=1
                    x=$bodyguard_loc[$i].x
                    y=$bodyguard_loc[$i].y
                    random_traits=yes
                    [modifications]
                        {TRAIT_LOYAL}
                    [/modifications]
                    {IS_LOYAL}
                    to_variable=ranger
                [/unit]
                [if]
                    [variable]
                        name=ranger.gender
                        equals=female
                    [/variable]
                    [then]
                        {VARIABLE ranger.name  _"female^Elvish Bodyguard"}
                    [/then]
                [/if]
                [unstore_unit]
                    variable=ranger
                [/unstore_unit]
            [/do]
        [/for]

        {CLEAR_VARIABLE bodyguard_loc,hexes_needed,extending,ranger}

        [message]
            role=Elvish Bodyguard
            message= _ "We stand ready to protect you, my lady! We shall lead these men of Wesnoth into the fray!"
        [/message]
    [/event]

    [event]
        name=last breath
        [filter]
            role=Elvish Bodyguard
        [/filter]

        [message]
            speaker=unit
            message= _ "For my lady I fall..."
        [/message]
    [/event]

    [event]
        name=last breath
        [filter]
            id="Mal A'kai"
        [/filter]

        [message]
            speaker="Mal A'kai"
            message= _ "The darkness beckons..."
        [/message]

        [kill]
            x,y=$x1,$y1
            animate=yes
        [/kill]

        {VARIABLE undead_defeated yes}

        [if]
            [variable]
                name=mebrin_found
                boolean_equals=yes
            [/variable]
            [variable]
                name=undead_defeated
                boolean_equals=yes
            [/variable]
            [variable]
                name=bandits_defeated
                boolean_equals=yes
            [/variable]
            [then]
                [endlevel]
                    result=victory
                    bonus=yes
                    {NEW_GOLD_CARRYOVER 40}
                [/endlevel]
            [/then]
        [/if]
    [/event]

    [event]
        name=last breath
        [filter]
            id=Jera Ilras
        [/filter]

        [message]
            speaker=Jera Ilras
            message= _ "Fools, my death here means nothing! Even if you could find our stronghold, you will never defeat our master!"
        [/message]

        [message]
            speaker=second_unit
            message= _ "Perhaps, but you, at least, will trouble us no more!"
        [/message]

        [kill]
            x,y=$x1,$y1
            animate=yes
        [/kill]

        {VARIABLE bandits_defeated yes}

        [if]
            [variable]
                name=mebrin_found
                boolean_equals=yes
            [/variable]
            [variable]
                name=undead_defeated
                boolean_equals=yes
            [/variable]
            [variable]
                name=bandits_defeated
                boolean_equals=yes
            [/variable]
            [then]
                [endlevel]
                    result=victory
                    bonus=yes
                    {NEW_GOLD_CARRYOVER 40}
                [/endlevel]
            [/then]
        [/if]
    [/event]

    # Ethiliel arrives at Mebrin’s village.

    [event]
        name=moveto
        [filter]
            id=Ethiliel
            x,y=25,28
        [/filter]

        [remove_item]
            x,y=25,28
        [/remove_item]

        [message]
            speaker=Ethiliel
            message= _ "Mebrin! Are you there? We have come to seek your aid!"
        [/message]

        [message]
            speaker=narrator
            message= _ "..."
            image=wesnoth-icon.png
        [/message]

        [message]
            speaker=Ethiliel
            message= _ "Mebrin! Where are you?!"
        [/message]

        [message]
            speaker=narrator
            message= _ "..."
            image=wesnoth-icon.png
        [/message]

        [message]
            speaker=Ethiliel
            message= _ "His home is empty... There are several sets of footprints leading away to the south..."
        [/message]

        [message]
            speaker=Sir Gerrick
            message= _ "Perhaps he was taken by the undead?"
        [/message]

        [message]
            speaker=Deoran
            message= _ "I am not sure. There is no blood here and very little sign of struggle. If it had been the undead, I suspect they would have slain him and made him one of them."
        [/message]

        [message]
            speaker=Sir Gerrick
            message= _ "Then maybe the bandits who have been summoning the undead took him?"
        [/message]

        [message]
            speaker=Deoran
            message= _ "We cannot be sure. I should think that he would have put up a fight, but if he were ambushed or taken by surprise, he may not have had the chance. Ethiliel, what do you think?"
        [/message]

        [message]
            speaker=Deoran
            message= _ "... Ethiliel? I know you must be distraught, but—"
        [/message]

        [message]
            speaker=Ethiliel
            image=portraits/ethiliel-mad.png
            message= _ "Distraught? <i>Distraught?</i> I am angry! We must follow these footprints into the woods! Whoever took our greatest sage must pay! Nothing will stop the vengeance of the elves!"
        [/message]

        [message]
            speaker=Deoran
            message= _ "Let us go quickly. <i>And avoid doing anything that might make her angrier than she already is...</i>"
        [/message]

        {VARIABLE mebrin_found yes}

        [if]
            [variable]
                name=mebrin_found
                boolean_equals=yes
            [/variable]
            [variable]
                name=undead_defeated
                boolean_equals=yes
            [/variable]
            [variable]
                name=bandits_defeated
                boolean_equals=yes
            [/variable]
            [then]
                [endlevel]
                    result=victory
                    bonus=yes
                    {NEW_GOLD_CARRYOVER 40}
                [/endlevel]
            [/then]
        [/if]
    [/event]

    [event]
        name=time over

        [unit]
            type=Necromancer
            side=2
            x,y=24,28
            canrecruit=yes
            id=Necro
            animate=yes
            facing=nw
        [/unit]
        {GENERIC_UNIT 2 (Revenant) 26 27}
        {GENERIC_UNIT 2 (Deathblade) 25 29}
        [message]
            speaker=Necro
            message= _ "So the elves finally come to defend their valley. Pity, they showed up too late. We will stop them here and prevent them from reclaiming the knowledge that was once theirs!"
        [/message]
        [message]
            speaker=Deoran
            message= _ "They have reached Mebrin’s village before us! We have no hope of obtaining his aid now..."
        [/message]
    [/event]

    [event]
        name=victory

        [if]
            [variable]
                name=have_bodyguard
                boolean_not_equals=yes
            [/variable]
            [then]
                [message]
                    speaker=Ethiliel
                    message= _ "Come, my bodyguards! We must make haste to follow this trail and find Mebrin."
                [/message]

                [fire_event]
                    name=bodyguard
                [/fire_event]
            [/then]
        [/if]

        {CLEAR_VARIABLE mebrin_found,undead_defeated,bandits_defeated,have_bodyguard}
    [/event]
[/scenario]
