#textdomain wesnoth-tsg
[scenario]
    id=8b_The_Tides_of_War

    name= _ "The Tides of War"
    next_scenario=9b_Bandit_Epilogue

    {SCENARIO_MUSIC knalgan_theme.ogg}
    {EXTRA_SCENARIO_MUSIC battle.ogg}
    {EXTRA_SCENARIO_MUSIC siege_of_laurelmor.ogg}

    map_data="{campaigns/The_South_Guard/maps/8b_The_Tides_of_War.map}"

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

    victory_when_enemies_defeated=yes
    turns=60

    # The South Guard

    [side]
        side=1
        type=Horseman Commander
        id=Deoran
        name= _ "Deoran"
        unrenamable=yes

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

        gold=200
        {INCOME 4 2 0}

        fog=no
        shroud=no

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

    # The Undead Hordes

    [side]
        side=2
        type=Ancient Lich
        id="Mal M'Brin"
        name= _ "Mal M'Brin"

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

        {GOLD 400 700 900}
        {INCOME 20 30 40}

        canrecruit=yes
        [ai]
            recruitment_pattern=scout,fighter,fighter,archer,scout,fighter,archer,mixed fighter
            aggression=1.0
            caution=0.0
            [target]
                id=Deoran
                value=6.0
            [/target]
        [/ai]
        recruit=Wraith,Shadow,Revenant,Draug,Bone Shooter,Blood Bat,Chocobone,Dark Sorcerer,Necrophage
    [/side]

    {STARTING_VILLAGES 2 5}

    [event]
        name=prestart

#define ILLUMINATING_CAMPFIRE X Y
    # {ANIMATED_CAMPFIRE {X} {Y}}

    [item]
        x,y={X},{Y}
        image=scenery/fire1.png
    [/item]

    [item]
        x,y={X},{Y}
        halo=halo/fire-aura.png
    [/item]

    [store_locations]
        x,y={X},{Y}
        radius=2

        variable=illuminated_locations
    [/store_locations]

    [time_area]
        find_in=illuminated_locations

        {MORNING}
        {MORNING}
        {AFTERNOON}
        {AFTERNOON}
        {DUSK}
        {DAWN}
    [/time_area]
#enddef

        [set_variables]
            name=pebbles_defense_benefits

            [value]
                turn=6
                {MODIFY_TERRAIN Ce 30 17}
                {VARIABLE defenses some}
            [/value]
            [value]
                turn=7
                {NAMED_GENERIC_UNIT 1 (Heavy Infantryman) 30 17 () (_"Provincial Guard")} {GUARDIAN}
                {VARIABLE provincial_guard some}
            [/value]
            [value]
                turn=8
                {MODIFY_TERRAIN Ce 27 17}
            [/value]
            [value]
                turn=9
                {NAMED_GENERIC_UNIT 1 (Heavy Infantryman) 27 17 () (_"Provincial Guard")} {GUARDIAN}
            [/value]
            [value]
                turn=10
                {MODIFY_TERRAIN Re^Vct 16 9}
                {MODIFY_TERRAIN Re^Vct 16 11}
            [/value]
            [value]
                turn=11
                {MODIFY_TERRAIN Re^Vct 17 14}
                {MODIFY_TERRAIN Re^Vct 19 15}
            [/value]
            [value]
                turn=12
                {MODIFY_TERRAIN Ch 20 11}
                {MODIFY_TERRAIN Ch 22 10}
            [/value]
            [value]
                turn=13
                {MODIFY_TERRAIN Ce 25 17}
                {MODIFY_TERRAIN Gg 26 16}
                {NAMED_GENERIC_UNIT 1 (Lieutenant) 25 17 (Lt. Nilaf) (_"Lt. Nilaf")} {GUARDIAN}
            [/value]
            [value]
                turn=14
                {MODIFY_TERRAIN Ce 23 17}
                {MODIFY_TERRAIN Gg 24 16}
                {NAMED_GENERIC_UNIT 1 (Spearman) 23 17 () (_"Provincial Guard")} {GUARDIAN}
            [/value]
            [value]
                turn=15
                {MODIFY_TERRAIN Ce 14 17}
                {NAMED_GENERIC_UNIT 1 (Bowman) 14 17 () (_"Provincial Guard")} {GUARDIAN}
            [/value]
            [value]
                turn=16
                {MODIFY_TERRAIN Ce 34 17}
                {NAMED_GENERIC_UNIT 1 (Heavy Infantryman) 34 17 () (_"Provincial Guard")} {GUARDIAN}
                {VARIABLE provincial_guard full}
            [/value]
            [value]
                turn=17
                {ILLUMINATING_CAMPFIRE 20 16}
            [/value]
            [value]
                turn=18
                {ILLUMINATING_CAMPFIRE 11 14}
            [/value]
            [value]
                turn=19
                {ILLUMINATING_CAMPFIRE 26 16}
            [/value]
            [value]
                turn=20
                {MODIFY_TERRAIN Gg 35 17}
                {ILLUMINATING_CAMPFIRE 35 17}
            [/value]
            [value]
                turn=21
                {MODIFY_TERRAIN Ce 23 21}
            [/value]
            [value]
                turn=22
                {MODIFY_TERRAIN Ce 26 21}
                {VARIABLE defenses full}
            [/value]
        [/set_variables]

        {FOREACH pebbles_defense_benefits i}
            [if]
                [variable]
                    name=pebbles_defense_length
                    greater_than_equal_to=$pebbles_defense_benefits[$i].turn
                [/variable]

                [then]
                    [insert_tag]
                        name=command
                        variable=pebbles_defense_benefits[$i]
                    [/insert_tag]
                [/then]
            [/if]
        {NEXT i}

        # the player gets 20 extra starting gold for each turn past 22
        [if]
            [variable]
                name=pebbles_defense_length
                greater_than=22
            [/variable]

            [then]
                {VARIABLE turns_past_22 $pebbles_defense_length}
                {VARIABLE_OP turns_past_22 add -22}

                {VARIABLE extra_starting_gold 20}
                {VARIABLE_OP extra_starting_gold multiply $turns_past_22}

                [gold]
                    side=1
                    amount=$extra_starting_gold
                [/gold]
            [/then]
        [/if]

        {CLEAR_VARIABLE pebbles_defense_benefits,illuminated_locations}
    [/event]

    [event]
        name=prestart

        {VARIABLE village_radius $pebbles_defense_length}
        {VARIABLE_OP village_radius add -5}

        [if]
            [variable]
                name=village_radius
                less_than=2
            [/variable]

            [then]
                {VARIABLE village_radius 2}
            [/then]
        [/if]

        [if]
            [variable]
                name=village_radius
                greater_than=20
            [/variable]

            [then]
                {VARIABLE village_radius 20}
            [/then]
        [/if]
    [/event]

    {STARTING_VILLAGES 1 $village_radius}

    #start
    [event]
        name=start
        [recall]
            id=Minister Hylas
            x,y=21,11
        [/recall]
        [recall]
            id=Jarek
            x,y=21,12
        [/recall]
        [recall]
            id=Moreth
            x,y=22,12
        [/recall]

        [message]
            speaker=Deoran
            message= _ "Look! The undead army has broken through Sir Gerrick's lines! Alas, he is lost!"
        [/message]

        [message]
            speaker=Moreth
            message= _ "Since I was a boy, Gerrick was my leader, and there was no one braver. Today we shall defend our homes, and avenge him!"
        [/message]

        [if]
            [variable]
                name=provincial_guard
                equals=some
            [/variable]
            [variable]
                name=defenses
                equals=some
            [/variable]
            [then]
                [message]
                    speaker=Deoran
                    message= _ "But in the narrow window his valiant stand gave us, we have roused some of the provincial guard and reinforced our defenses!"
                [/message]
            [/then]
            [else]
                [if]
                    [variable]
                        name=provincial_guard
                        equals=full
                    [/variable]
                    [variable]
                        name=defenses
                        equals=some
                    [/variable]
                    [then]
                        [message]
                            speaker=Deoran
                            message= _ "But in the narrow window his valiant stand gave us, we have roused the whole provincial guard and reinforced our defenses!"
                        [/message]
                    [/then]
                    [else]
                        [if]
                            [variable]
                                name=provincial_guard
                                equals=full
                            [/variable]
                            [variable]
                                name=defenses
                                equals=full
                            [/variable]
                            [then]
                                [message]
                                    speaker=Deoran
                                    message= _ "But in the narrow window his valiant stand gave us, we have roused the whole provincial guard and fully embattled the city!"
                                [/message]
                            [/then]
                        [/if]
                    [/else]
                [/if]
            [/else]
        [/if]

        [message]
            speaker=Lt. Nilaf
            message= _ "Indeed, Lord, we are ready to defend our homes!"
        [/message]

        [if]
            [variable]
                name=pebbles_defense_length
                less_than=24
            [/variable]
            [then]
                [message]
                    speaker=Minister Hylas
                    message= _ "I have summoned the great Council of Westin!"
                [/message]
                # wmllint: local spelling Mefel
                {NAMED_LOYAL_UNIT 1 (Silver Mage) 11 1 (Minister Mefel) (_"Minister Mefel")}
                [message]
                    speaker=Minister Mefel
                    message= _ "Hylas, I have made all possible haste, but my Council brethren are not as swift as I. It will take more time for them to arrive."
                [/message]
                {VARIABLE council_arrive_turn 25}
                {VARIABLE_OP council_arrive_turn add -$pebbles_defense_length}
            [/then]

            [else]
                [message]
                    speaker=Minister Hylas
                    message= _ "And I have summoned the great Council of Westin! They will battle with all their might!"
                [/message]
                {NAMED_LOYAL_UNIT 1 (Silver Mage) 11 1 (Minister Mefel) (_"Minister Mefel")}
                {NAMED_LOYAL_UNIT 1 (Arch Mage) 10 1 (Minister Romand) (_"Minister Romand")}
                {NAMED_LOYAL_UNIT 1 (Red Mage) 12 1 (Minister Alanafel) (_"Minister Alanafel")}
                {GENERIC_UNIT 1 (White Mage) 9 1}
                {GENERIC_UNIT 1 (White Mage) 13 1}
                [message]
                    speaker=Minister Mefel
                    message= _ "We have heard your summons, and we shall send these dark foes back to the abyss that spawned them!"
                [/message]
                [message]
                    speaker=Deoran
                    message= _ "Council of Westin, we are cheered by your presence. Your aid will be most welcome."
                [/message]
            [/else]
        [/if]

        [message]
            speaker=Deoran
            message= _ "We have rallied all the forces that we will be able to! Let us not make Sir Gerrick's sacrifice a vain one. For Wesnoth!"
        [/message]

        [if]
            [variable]
                name=pebbles_defense_length
                less_than=24
            [/variable]
            [then]
                [objectives]
                    side=1
                    [objective]
                        description= _ "Defeat Mal M'Brin"
                        condition=win
                    [/objective]
                    [objective]
                        description= _ "Death of Deoran"
                        condition=lose
                    [/objective]
                    [objective]
                        description= _ "Time runs out"
                        condition=lose
                    [/objective]
                    note= _ "The Council of Westin will arrive on turn $council_arrive_turn|."
                [/objectives]
            [/then]
            [else]
                [objectives]
                    side=1
                    [objective]
                        description= _ "Defeat Mal M'Brin"
                        condition=win
                    [/objective]
                    [objective]
                        description= _ "Death of Deoran"
                        condition=lose
                    [/objective]
                    [objective]
                        description= _ "Time runs out"
                        condition=lose
                    [/objective]
                [/objectives]
            [/else]
        [/if]
    [/event]

    [event]
        name=new turn
        first_time_only=no

        [if]
            [variable]
                name=council_arrive_turn
                equals=$turn_number
            [/variable]
            [variable]
                name=council_arrive_turn
                not_equals=$empty
            [/variable]
            [then]
                {NAMED_LOYAL_UNIT 1 (Arch Mage) 10 1 (Minister Romand) (_"Minister Romand")}
                {NAMED_LOYAL_UNIT 1 (Red Mage) 12 1 (Minister Alanafel) (_"Minister Alanafel")}
                {GENERIC_UNIT 1 (White Mage) 9 1}
                {GENERIC_UNIT 1 (White Mage) 13 1}
                [message]
                    speaker=Minister Romand
                    message= _ "We have heard your summons, and we shall send these dark foes back to the abyss that spawned them!"
                [/message]
                [message]
                    speaker=Deoran
                    message= _ "Council of Westin, we are cheered by your presence. Your aid will be most welcome."
                [/message]

                [objectives]
                    side=1
                    silent=yes
                    [objective]
                        description= _ "Defeat Mal M'Brin"
                        condition=win
                    [/objective]
                    [objective]
                        description= _ "Death of Deoran"
                        condition=lose
                    [/objective]
                    [objective]
                        description= _ "Time runs out"
                        condition=lose
                    [/objective]
                [/objectives]
            [/then]
        [/if]
    [/event]

    [event]
        name=die

        [filter]
            id="Mal M'Brin"
        [/filter]

        [endlevel]
            result=victory
            carryover_report=no
            save=no
        [/endlevel]
    [/event]

    # in order to allow Hylas to die, the death events aren't included from the
    # standard location here
    [event]
        name=last breath
        [filter]
            id=Deoran
        [/filter]
        [message]
            speaker=unit
            image=portraits/deoran-sad.png
            message= _ "I have failed my kingdom and duty..."
        [/message]
        [endlevel]
            result=defeat
        [/endlevel]
    [/event]

    [event]
        name=last breath
        [filter]
            id=Minister Hylas
        [/filter]
        [message]
            speaker=unit
            image=portraits/hylas.png
            message= _ "Now I will never see Westin free again..."
        [/message]
    [/event]

    [event]
        name=last breath
        [filter]
            id=Jarek
        [/filter]
        [message]
            speaker=unit
            message= _ "Far from home I fall - but not in vain! Guard our people, Deoran!"
        [/message]
    [/event]
[/scenario]
