#textdomain wesnoth-Saving_Elensefar

[scenario]
    id=sea_voyage_04
    next_scenario=sea_voyage_fake3
    name= _ "Saving Elensefar"
    map_data="{~add-ons/Saving_Elensefar/maps/04-Saving_Elensefar.map}"
    random_start_time=no

    {QUANTITY turns 42 36 32}

    {DAWN}
    {DAWN}
    {MORNING}
    {MORNING}
    {AFTERNOON}
    {AFTERNOON}
    {DUSK}
    {DUSK}
    {FIRST_WATCH}
    {FIRST_WATCH}
    {SECOND_WATCH}
    {SECOND_WATCH}

    {MOOD_EPIC}

    {DEATHS_SEA}

#define GUARD_ELENSEFAR SIDE TYPE X Y
    [unit]
        side={SIDE}
        x,y={X},{Y}
        type={TYPE}
        moves=0
        max_moves=0
        upkeep=loyal
        ai_special=guardian
        random_traits=no
        [modifications]
            [object]
                silent=yes
                [effect]
                    apply_to=hitpoints
                    increase_total=40%
                    increase=40%
                [/effect]
                [effect]
                    apply_to=defense
                    replace=yes
                    [defense]
                        castle=50
                    [/defense]
                [/effect]
            [/object]
        [/modifications]
    [/unit]
#enddef

    [side]
        side=1
        [leader]
            {MENELDUR}
        [/leader]

        {HUMAN_SIDE}

        recruit=Peasant,Skeleton,Skeleton Archer
    [/side]

    [side]
        side=2
        [leader]
            id=Pakch-Glun
            name= _"Pakch-Glun"
            type=Orcish Sovereign
            #type=Orcish Warlord
        [/leader]

        controller=ai
        gold=700
        recruit=Orcish Warrior,Troll,Troll Whelp,Orcish Slayer,Orcish Assassin,Orcish Crossbowman,Orcish Grunt
        team_name=orcs
        user_team_name= _"Besieged Orcs"

        {FLAG_VARIANT undead}

        {GENERIC_AI}
        [+ai]
            grouping=offensive

            # avoid the harbour making the third choice less suicidal
            {AI_SIMPLE_ALWAYS_ASPECT_VALUE avoid (
                x=1-23
                y=15-26
            )}
        [/ai]
        {PROTECT unit (type=Troll Warrior)}
    [/side]

    {LIMIT_RECRUITS 2 ("Orcish Assassin") 3}
    {LIMIT_RECRUITS 2 ("Orcish Slayer") 3}
    {LIMIT_CONTEMPORANEOUS_RECRUITS 2 ("Troll") 3}

    [side]
        side=3
        [leader]
            id=Mathein
            name= _"Mathein"
            type=Great Mage
            #type=General
        [/leader]

        controller=ai
        gold=500
        recruit=Swordsman,Pikeman,Longbowman,Dragoon,Red Mage,Spearman
        team_name=humans
        user_team_name= _"Wesnoth Army"

        {FLAG_VARIANT loyalist}

        {GENERIC_AI}
        {FOCUS_ON_SIDE 2}
    [/side]

    {LIMIT_CONTEMPORANEOUS_RECRUITS 3 ("Dragoon") 2}

    [side]
        side=4
        [leader]
            id=Galiyonia
            name= _"Galiyonia"
            type=Elvish Sylph
            #type=Elvish Marshal
        [/leader]
        [unit]
            type=Merman Netcaster
            x,y=29,5
        [/unit]
        [unit]
            type=Merman Netcaster
            x,y=30,7
        [/unit]

        controller=ai
        gold=520
        recruit=Elvish Ranger,Elvish Hero,Elvish Rider,Elvish Druid,Elvish Fighter
        team_name=elves
        user_team_name= _"Angry Elves"

        {FLAG_VARIANT wood-elvish}

        {GENERIC_AI}
        {FOCUS_ON_SIDE 2}
    [/side]

    {LIMIT_RECRUITS 4 ("Elvish Rider") 3}
    {LIMIT_CONTEMPORANEOUS_RECRUITS 4 ("Elvish Druid") 2}

    [event]
        name=prestart

        [objectives]
            [objective]
                description= _ "Retake Elensefar by defeating all enemy leaders"
                condition=win
            [/objective]
            {DEATH_OF_LEADERS}
            {TURNS_RUN_OUT}
            {IS_LAST_SCENARIO}
        [/objectives]

        {VARIABLE last_scenario yes}
        {VARIABLE started no}

        # change map, recruits and leader position based on previous choice
        [switch]
            variable=whichwaytogo
            [case]
                value=north

                {VARIABLE sx 7}
                {VARIABLE sy 15}
                {VARIABLE kx 6}
                {VARIABLE ky 12}
                {TELEPORT_UNIT (id="Meneldur") 7 12}
            [/case]
            [case]
                value=south

                {VARIABLE sx 14}
                {VARIABLE sy 29}
                {VARIABLE kx 16}
                {VARIABLE ky 31}
                {TELEPORT_UNIT (id="Meneldur") 17 31}
            [/case]
            [case]
                value=harbour

                {VARIABLE sx 12}
                {VARIABLE sy 23}
                {VARIABLE kx 15}
                {VARIABLE ky 23}
                {TELEPORT_UNIT (id="Meneldur") 15 22}
            [/case]
        [/switch]

        ##set the guardians on the bridges
        {GUARD_ELENSEFAR 2 (Troll Warrior) 23 12}
        {GUARD_ELENSEFAR 2 (Troll Rocklobber) 25 12}
        {GUARD_ELENSEFAR 2 (Troll Warrior) 26 12}
        {GUARD_ELENSEFAR 2 (Troll Rocklobber) 28 13}
        {GUARD_ELENSEFAR 2 (Troll Warrior) 28 24}
        {GUARD_ELENSEFAR 2 (Troll Warrior) 30 24}
        {GUARD_ELENSEFAR 2 (Troll Rocklobber) 35 22}
        {GUARD_ELENSEFAR 2 (Troll Rocklobber) 36 20}
        {GUARD_ELENSEFAR 2 (Troll Rocklobber) 22 18}
        {GUARD_ELENSEFAR 2 (Troll Rocklobber) 23 20}

        {GUARD_ELENSEFAR 3 (Master Bowman) 41 25}
        {GUARD_ELENSEFAR 3 (Royal Guard) 28 29}
        {GUARD_ELENSEFAR 3 (Royal Guard) 30 29}

        {GUARD_ELENSEFAR 4 (Elvish Champion) 23 6}
        {GUARD_ELENSEFAR 4 (Elvish Champion) 25 6}
        {GUARD_ELENSEFAR 4 (Elvish Enchantress) 35 10}
    [/event]

    {STARTING_VILLAGES 2 12}
    {STARTING_VILLAGES 3 6}
    {STARTING_VILLAGES 4 6}

    {START_A_SCENE 1 22 $sx $sy $kx $ky 04 RETREAT=no}

    [event]
        name=side turn

        # recall merman (in water)
        {SEARCH_NEAREST id=Meneldur (W*) 8}
        [recall]
            race=merman
            side=1
            x,y=$location.x,$location.y
        [/recall]

        {SEARCH_NEAREST id=Meneldur (W*) 8}
        [recall]
            race=merman
            side=1
            x,y=$location.x,$location.y
        [/recall]

        {SEARCH_NEAREST id=Meneldur (W*) 8}
        [recall]
            race=merman
            side=1
            x,y=$location.x,$location.y
        [/recall]

        #and Monsters
        {SEARCH_NEAREST id=Meneldur (Wo*) 9}
        [recall]
            type=Cuttle Fish
            side=1
            x,y=$location.x,$location.y
        [/recall]

        {SEARCH_NEAREST id=Meneldur (Hh) 9}
        [recall]
            type=Giant Spider
            side=1
            x,y=$location.x,$location.y
        [/recall]
        {CLEAR_VARIABLE location}
        {CLEAR_VARIABLE sx,sy,kx,ky}

        {VARIABLE started yes}

        [scroll_to_unit]
            id=Meneldur
        [/scroll_to_unit]

        [message]
            speaker=narrator
            message= _ "As the warriors landed the Explorer, they overheard the elves, men and orcs talking..."
            image=wesnoth-icon.png
        [/message]
        [message]
            id=Galiyonia
            message= _ "Orc, you promised that, if we allied with you, you would free all the prisoners you took when you captured Elensefar. You have broken our pact!"
        [/message]
        [message]
            id=Pakch-Glun
            message= _ "Ag, elf-dog, you shuttup! I aint takin' orders fr'm you!"
        [/message]
        [message]
            id=Mathein
            message= _ "Listen, Pakch-Glun, if you don't fufill your side of the treaty, we will attack you again! And this time, we will probably win, since our reinforcements have arrived, and there are two armies to attack you."
        [/message]
        [message]
            id=Pakch-Glun
            message= _ "Heh, shur. I'd like t' see you try!"
        [/message]
        [message]
            id=Galiyonia
            message= _ "Mathein, shall we attack?"
        [/message]
        [message]
            id=Mathein
            message= _ "Yes. Pakch-Glun, prepare to die!"
        [/message]
        [message]
            speaker=narrator
            message= _ "This was welcome news to the crew of the Explorer. However, although the elves, men and orcs would fight each other, Meneldur still had to fight all of them."
            image=wesnoth-icon.png
        [/message]

        [switch]
            variable=whichwaytogo
            [case]
                value=north
                [if]
                    [variable]
                        name=fought_elves_yet
                        equals=yes
                    [/variable]
                    [then]
                        [message]
                            id=Meneldur
                            message= _ "The first enemy we will encounter will be the elves. Show them no mercy!"
                        [/message]
                        [message]
                            id=Galiyonia
                            message= _ "And we will show you none. Prepare to meet your doom!"
                        [/message]
                    [/then]
                    [else]
                        [message]
                            id=Meneldur
                            message= _ "What? There appears to be elves blocking our path! Elves, why are you in our way?"
                        [/message]
                        [message]
                            id=Galiyonia
                            message= _ "We are here to kill you! After that, we will kill the orcs, but we must kill you first."
                        [/message]
                        [message]
                            id=Meneldur
                            message= _ "Are you mad?! Well, if you are, I have no time to reason with you. Prepare to die!"
                        [/message]
                    [/else]
                [/if]
            [/case]
            [case]
                value=south
                [if]
                    [variable]
                        name=fought_men_yet
                        equals=yes
                    [/variable]
                    [then]
                        [message]
                            id=Meneldur
                            message= _ "We have come to take back Elensefar from the orcs! But remember, the men of Wesnoth will no doubt try to stop us, as will the elves. We must fight them too."
                        [/message]
                        [message]
                            id=Mathein
                            message= _ "Indeed we will. You are a traitor to your country, and must die as one!"
                        [/message]
                    [/then]
                    [else]
                        [message]
                            id=Meneldur
                            message= _ "This looks like the area the men of Wesnoth have claimed to siege Elensefar from. Unfortunately, we must get past them to free the city."
                        [/message]
                        [message]
                            id=Mathein
                            message= _ "You will not get past us while I am alive. And even if you do, the elves will stop you. If you must be a traitor, pick one nation to betray, not two!"
                        [/message]
                        [message]
                            id=Meneldur
                            message= _ "What are you talking about? Oh, I see... my northern brethren have decided to attack me... Well, we will get past these Wesnothians first, and see what we can do then."
                        [/message]
                        [message]
                            id=Pakch-Glun
                            message= _ "Prepare to die, Elensian scum!"
                        [/message]
                    [/else]
                [/if]
            [/case]
            [else]
                #value=harbour
                [if]
                    [variable]
                        name=fought_elves_yet
                        equals=yes
                    [/variable]
                    [then]
                        [message]
                            id=Meneldur
                            message= _ "We are in Elensefar's harbor, from where we will drive the orcs out into the faces of the elves. Although they are my brethren, we must not hold back from killing them because of it. Onward!"
                        [/message]
                        [message]
                            id=Pakch-Glun
                            message= _ "Listen t' that! He really thinks 'e can kill us! We'll show 'im! Kill the invader!"
                        [/message]
                    [/then]
                    [else]
                        [message]
                            id=Meneldur
                            message= _ "We are now in Elensefar's harbor. From now, we fight until the orcs are all out of our city! Then, it appears, we will have to deal with those Wesnothians who believe us to be traitors."
                        [/message]
                        [message]
                            id=Galiyonia
                            message= _ "Yes, please, do that. Then we will not have to deal with the orcs, and only with you!"
                        [/message]
                        [message]
                            id=Meneldur
                            message= _ "What?... Oh, it's elves, from the northlands. What do you want?"
                        [/message]
                        [message]
                            id=Galiyonia
                            message= _ "You dead, traitor!"
                        [/message]
                        [message]
                            id=Madru
                            message= _ "Whatever you say. We'll deal with you after we kill the orcs."
                        [/message]
                    [/else]
                [/if]
            [/else]
        [/switch]
    [/event]

    [event]
        name=die

        [filter]
            id=Pakch-Glun
        [/filter]

        [if]
            [have_unit]
                id=Mathein
            [/have_unit]
            [then]
                [message]
                    id=Meneldur
                    message= _ "Elensefar may be ours, but there are still enemies to defeat. Onwards!"
                [/message]
            [/then]
            [else]
                [if]
                    [have_unit]
                        id=Galiyonia
                    [/have_unit]
                    [then]
                        [message]
                            id=Meneldur
                            message= _ "We have taken Elensefar and defeated the Wesnothians, but these Elves need attending to."
                        [/message]
                    [/then]
                    [else]
                        [message]
                            id=Madru
                            message= _ "There are no enemies left - we have succeeded in retaking Elensefar."
                        [/message]
                        {SE_ENDLEVEL fake3}
                    [/else]
                [/if]
            [/else]
        [/if]

        [message]
            speaker=second_unit
            message= _ "The orcs had ransacked the city's coffers - now it's time for us to return the favor."
        [/message]

        [message]
            speaker=narrator
            side_for="$second_unit.side"
            message= _ "You find 250 pieces of gold!"
            image=wesnoth-icon.png
        [/message]
        [gold]
            side="$second_unit.side"
            amount=250
        [/gold]
    [/event]

    [event]
        name=die

        [filter]
            id=Mathein
        [/filter]

        [if]
            [have_unit]
                id=Pakch-Glun
            [/have_unit]
            [then]
                [message]
                    id=Madru
                    message= _ "Good work, men. Onwards, we have to retake the city also!"
                [/message]
            [/then]
            [else]
                [if]
                    [have_unit]
                        id=Galiyonia
                    [/have_unit]
                    [then]
                        [message]
                            id=Madru
                            message= _ "Come on, we should go North and see if we can help defeat the elves."
                        [/message]
                    [/then]
                    [else]
                        [message]
                            id=Madru
                            message= _ "There are no enemies left - we have succeeded in retaking Elensefar."
                        [/message]
                        {SE_ENDLEVEL fake3}
                    [/else]
                [/if]
            [/else]
        [/if]

        [message]
            speaker=second_unit
            message= _ "The Wesnothians had much gold in their camp. It is ours now!"
        [/message]

        [message]
            speaker=narrator
            side_for="$second_unit.side"
            message= _ "You find 175 pieces of gold!"
            image=wesnoth-icon.png
        [/message]

        [gold]
            side="$second_unit.side"
            amount=175
        [/gold]
    [/event]

    [event]
        name=die

        [filter]
            id=Galiyonia
        [/filter]

        [if]
            [have_unit]
                id=Pakch-Glun
            [/have_unit]
            [then]
                [message]
                    id="Black the Red"
                    message= _ "Orcs still reside in the city! Come one, let us kill them too!"
                [/message]
            [/then]
            [else]
                [if]
                    [have_unit]
                        id=Mathein
                    [/have_unit]
                    [then]
                        [message]
                            id="Black the Red"
                            message= _ "Come on, we should go South and see if we can help defeat the Wesnothians."
                        [/message]
                    [/then]
                    [else]
                        [message]
                            id=Madru
                            message= _ "There are no enemies left - we have succeeded in retaking Elensefar."
                        [/message]
                        {SE_ENDLEVEL fake3}
                    [/else]
                [/if]
            [/else]
        [/if]

        [message]
            speaker=second_unit
            message= _ "It seems the elves brought some of their treasure out of the northlands. More for us!"
        [/message]

        [message]
            speaker=narrator
            message= _ "You find 175 pieces of gold!"
            image=wesnoth-icon.png
        [/message]
        [gold]
            side="$second_unit.side"
            amount=175
        [/gold]
    [/event]

    [event]
        name=enemies defeated

        [message]
            id=Meneldur
            message= _ "Yes, Elensefar is ours, finally. Well, Black the Red, we thank you for your help."
        [/message]
        [message]
            id="Black the Red"
            message= _ "It was an honor to fight for you. I wonder where my people will go now... the orcs have surely taken our home by now."
        [/message]
        [message]
            id=Meneldur
            message= _ "I'm sure we will have time to discuss that later. But for now, we must help those conquered by the orcs rebuild their city."
        [/message]
    [/event]

    # elves and humans receive some reinforcements
    [event]
        name=turn 6, turn 10, turn 12,turn 16,turn 20
        first_time_only=no

        [switch]
            variable=whichwaytogo
            [case]
                value=north
                [gold]
                    side=3
                    amount=80
                [/gold]
                [gold]
                    side=4
                    amount=140
                [/gold]
            [/case]
            [case]
                value=south
                [gold]
                    side=4
                    amount=80
                [/gold]
                [gold]
                    side=3
                    amount=140
                [/gold]
            [/case]
            [else]
                [gold]
                    side=3,4
                    amount=120
                [/gold]
            [/else]
        [/switch]
    [/event]

    # if a leader or loyal unit captures a village some random level 0s spawn
    [event]
        id=spawn_villagers
        name=capture
        first_time_only=no

        [filter_condition]
            [variable]
                name=started
                boolean_equals=yes
            [/variable]
        [/filter_condition]
        [filter]
            side=1
            canrecruit=yes
        [/filter]

        [if]
            [variable]
                name=village.at_$x1|_$y1|.side_$unit.side
                not_equals=cleared
            [/variable]
            [then]
                [set_variable]
                    name=village.at_$x1|_$y1|.side_$unit.side
                    value=cleared
                [/set_variable]
                [switch]
                    variable=unit.id
                    [case]
                        value="Black the Red",Madru
                        {VARIABLE_OP villagers rand "-1..2"}
                    [/case]
                    [case]
                        value=SirDrake,Thamen,Raenna
                        {VARIABLE_OP villagers rand "-2..2"}
                    [/case]
                    [case]
                        value=Meneldur
                        {VARIABLE_OP villagers rand "0..3"}
                    [/case]
                    [case]
                        value=Ah-Reblak
                        {VARIABLE_OP villagers rand "-3..1"}
                    [/case]
                    [else]
                        {VARIABLE_OP villagers rand "-3..2"}
                    [/else]
                [/switch]

                {VARIABLE countdown $villagers}
                [while]
                    [variable]
                        name=countdown
                        greater_than=0
                    [/variable]
                    [do]
                        {RANDOM (Ruffian,Woodsman,Peasant)}
                        [unit]
                            type=$random
                            side=$unit.side
                            x,y=$x1,$y1
                            random_traits=yes
                            moves=0
                        [/unit]
                        {VARIABLE_OP countdown sub 1}
                    [/do]
                [/while]
                {CLEAR_VARIABLE countdown}
                {IF_VAR villagers greater_than 0 (
                    [then]
                        [message]
                            speaker=unit
                            message= _ "The villagers are joining us!"
                        [/message]
                    [/then]
                )}
                {CLEAR_VARIABLE villagers,random}
            [/then]
        [/if]
    [/event]

    # unstore 1 of the mages from Alduin (if exist)
    [event]
        name=side 3 turn refresh
        first_time_only=no

        [filter_condition]
            [have_unit]
                side=3
                canrecruit=yes
            [/have_unit]
        [/filter_condition]

        # Only the first one
        {IF_VAR mages_2.length greater_than 0 (
            [then]
                {VARIABLE mages_2[0].side 3}
                [unstore_unit]
                    variable=mages_2[0]
                    x,y=40,33
                [/unstore_unit]
                {CLEAR_VARIABLE mages_2[0]}
            [/then]
        )}
    [/event]

    # scatter goblins on orcish villages
    [event]
        name=prestart

        [store_villages]
            owner_side=2
            variable=orcish_villages
        [/store_villages]

        [foreach]
            array=orcish_villages
            [do]
                {VARIABLE_OP random rand ("Goblin Spearman,Goblin Spearman,Goblin Impaler")}
                [unit]
                    id=rg_goblin_$i
                    side=2
                    type=$random
                    x,y=$this_item.x,$this_item.y
                    moves,max_moves=0,2
                [/unit]
            [/do]
        [/foreach]
        {CLEAR_VARIABLE random,orcish_villages}
    [/event]

    # do not let the elves or humans kill the orc leader
    {FORCE_CHANCE_TO_HIT side=3,4 id=Pakch-Glun 10 ()}
[/scenario]
