#textdomain wesnoth-trow
[scenario]
    id=19_The_Vanguard
    name= _ "The Vanguard"
    next_scenario=20_Return_of_the_Fleet
    map_data="{campaigns/The_Rise_Of_Wesnoth/maps/19_The_Vanguard.map}"

    {TURNS 41 38 35}
    {DEFAULT_SCHEDULE}

    {SCENARIO_MUSIC loyalists.ogg}
    {EXTRA_SCENARIO_MUSIC northerners.ogg}
    {EXTRA_SCENARIO_MUSIC breaking_the_chains.ogg}
    {EXTRA_SCENARIO_MUSIC siege_of_laurelmor.ogg}

    # No story
    {TROW_GC_TRACK {JOURNEY_19_NEW} }

    {TROW_DEATHS}

    [side]
        id=Prince Haldric
        name= _ "Prince Haldric"
        side=1
        type=Noble Commander
        unrenamable=yes
        canrecruit=yes
        gold=200
        controller=human
        team_name=Haldric
        user_team_name=_"Refugees"
        fog=yes
        {FLAG_VARIANT loyalist}
    [/side]

    [side]
        type=Orcish Warlord
        id=Tan-Erirt
        name= _ "Tan-Erirt"
        side=2
        canrecruit=yes
#ifdef EASY
        recruit=Orcish Archer, Orcish Assassin, Orcish Grunt, Wolf Rider, Orcish Crossbowman, Goblin Pillager, Goblin Knight, Goblin Spearman
#endif

#ifdef NORMAL
        recruit=Orcish Archer, Orcish Assassin, Orcish Grunt, Wolf Rider, Orcish Crossbowman, Goblin Pillager, Goblin Knight, Orcish Slayer, Orcish Warrior, Goblin Spearman
#endif

#ifdef HARD
        recruit=Orcish Archer, Orcish Assassin, Orcish Grunt, Wolf Rider, Orcish Crossbowman, Orcish Warrior, Goblin Knight, Goblin Pillager, Orcish Slayer, Goblin Spearman, Goblin Impaler
#endif
        {GOLD 150 190 230}
        team_name=orcs
        user_team_name=_"Orcs"
        [ai]
            #{NO_SCOUTS}
            recruitment_pattern=scout,fighter,fighter,mixed fighter,archer
            {ATTACK_DEPTH 5 5 6}
        [/ai]
        [ai]
            time_of_day=dusk,first_watch,second_watch
            aggression=0.75
            caution=0.0
            grouping=no
        [/ai]
        {INCOME 2 4 8}
    [/side]

    {STARTING_VILLAGES 2 8}

    [side]
        type=Orcish Warlord
        id=Tan-Gagar
        name= _ "Tan-Gagar"
        profile=portraits/orcs/grunt-2.png
        side=3
        canrecruit=yes
#ifdef EASY
        recruit=Orcish Archer, Orcish Assassin, Orcish Grunt, Wolf Rider, Orcish Crossbowman, Goblin Pillager, Goblin Knight, Goblin Spearman
#endif

#ifdef NORMAL
        recruit=Orcish Archer, Orcish Assassin, Orcish Grunt, Wolf Rider, Orcish Crossbowman, Goblin Knight, Orcish Slayer, Orcish Warrior, Goblin Spearman
#endif

#ifdef HARD
        recruit=Orcish Archer, Orcish Assassin, Orcish Grunt, Wolf Rider, Orcish Crossbowman, Orcish Warrior, Goblin Knight, Goblin Pillager, Orcish Slayer, Goblin Spearman, Goblin Impaler
#endif
        {GOLD 150 190 230}
        team_name=orcs
        user_team_name=_"Orcs"
        [ai]
            #{NO_SCOUTS}
            recruitment_pattern=scout,fighter,fighter,mixed fighter,archer
            {ATTACK_DEPTH 5 5 6}
        [/ai]
        [ai]
            time_of_day=dusk,first_watch,second_watch
            aggression=0.75
            caution=0.0
            grouping=no
        [/ai]
        {INCOME 2 4 8}
    [/side]

    {STARTING_VILLAGES 3 8}

    [side]
        type=Troll Warrior
        id=Thruf
        name= _ "Thruf"
        side=4
        canrecruit=yes
#ifdef EASY
        recruit=Troll Whelp
#endif

#ifdef NORMAL
        recruit=Troll Whelp,Troll,Troll Rocklobber
#endif

#ifdef HARD
        recruit=Troll Whelp,Troll,Troll Warrior,Troll Rocklobber
#endif

        {GOLD 110 150 190}
        team_name=orcs
        user_team_name=_"Orcs"
        [ai]
            {NO_SCOUTS}
#ifdef EASY
            recruitment_pattern=fighter
#else
            recruitment_pattern=fighter,fighter,mixed fighter
#endif
            grouping=no
            {ATTACK_DEPTH 5 5 6}
        [/ai]

        {INCOME 2 4 8}
    [/side]

    [side]
        side=5
        no_leader=yes
        team_name=orcs
        hidden=yes
        [ai]
            village_value=0
        [/ai]
    [/side]

    {STARTING_VILLAGES 4 8}

    [event]
        name=prestart

        [store_locations]
            terrain=Gg
            x=11-18
            y=2-6
            variable=possible_chest_locs
        [/store_locations]
        {RANDOM "0..$($possible_chest_locs.length - 1)"}

        {VARIABLE xx1 $possible_chest_locs[$random].x}
        {VARIABLE yy1 $possible_chest_locs[$random].y}

        {CLEAR_VARIABLE possible_chest_locs,random}

        {PLACE_IMAGE (items/chest-plain-closed.png) $xx1 $yy1}

        [recall]
            id=Lady Jessene
        [/recall]
        [recall]
            id=Commander Aethyr
        [/recall]

        [remove_object]
            id=Prince Haldric
            object_id=ruby_of_fire
        [/remove_object]

        [objectives]
            side=1
            [objective]
                description= _ "Defeat all enemy leaders"
                condition=win
            [/objective]
            [objective]
                description= _ "Death of Prince Haldric"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Commander Aethyr"
                condition=lose
            [/objective]

            {TURNS_RUN_OUT}

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

    [event]
        name=start
        [message]
            speaker=narrator
            message= _ "Haldric and his companions race across the plains to confront the resurgent orcish threat. On the eve of battle, Lady Jessene catches up with Haldric on a fog-covered plain."
            image=wesnoth-icon.png
        [/message]
        [message]
            speaker=Lady Jessene
            message= _ "Haldric, the elves, we can’t trust them!"
        [/message]

        [message]
            speaker=Prince Haldric
            message= _ "I could have told you that."
        [/message]
        [message]
            speaker=Lady Jessene
            message= _ "Haldric, it’s bad. The elves are having second thoughts. They think we might be more trouble than we’re worth. We have to defeat these orcs decisively."
        [/message]
        [message]
            speaker=Prince Haldric
            message= _ "That does not sound unreasonable."
        [/message]
        [message]
            speaker=Lady Jessene
            message= _ "More orcs will follow after these. That ruby guarantees it. If more orcs follow us here, the elves seem content to let us fight it out. Then they said they would ‘deal with the survivors’."
        [/message]
        [message]
            speaker=Lady Jessene
            message= _ "Jevyan is here... His familiar, that skull. He won’t let such a lucrative prize as the Ruby of Fire just slip away. Especially when he sees that he has the advantage."
        [/message]
        [message]
            speaker=Prince Haldric
            message= _ "I have a plan. Jessene, remember that troll-hole? Here, take the Ruby of Fire, hide it in the hole."
        [/message]
        [message]
            speaker=Lady Jessene
            message= _ "Haldric! What! Why?"
        [/message]
        [message]
            speaker=Prince Haldric
            message= _ "Just do it."
        [/message]
        [message]
            speaker=Prince Haldric
            message= _ "Commander Aethyr, did these orcs come on our ships? Did they capture the fleet we sent out?"
        [/message]
        [message]
            speaker=Commander Aethyr
            message= _ "No, sir! They must have seized all remaining boats on the Green Isle. Our fleet should be returning any day now."
        [/message]
        [message]
            speaker=Prince Haldric
            message= _ "We are a refugee people. We must push back their vanguard, and secure our beachhead. If they capture our ships all is lost. We will be crushed under a tide of orcs."
            image=portraits/haldric-mad.png
        [/message]

        [recall]
            id=Burin the Lost
        [/recall]
        [recall]
            id=Sir Ruddry
        [/recall]
        [recall]
            id=Sir Ladoc
        [/recall]
        [recall]
            id=Minister Edren
        [/recall]

        [store_unit]
            variable=jessica_store
            kill=yes
            [filter]
                id=Lady Jessene
            [/filter]
        [/store_unit]

        [message]
            speaker=narrator
            message= _ "Lady Jessene soon departs. In the distance a voice booms:"
            image=wesnoth-icon.png
        [/message]

        [message]
            speaker=Tan-Gagar
            message= _ "Drop the gold where it is! We’ve paid our friends enough. There are humans about, I can smell ’em!"
        [/message]
    [/event]

    [event]
        name=time over
        [message]
            speaker=Prince Haldric
            message= _ "We have run out of time... We’ll never beat the orcs to the beach."
            image=portraits/haldric-surprised.png
        [/message]
    [/event]

    [event]
        name=moveto
        [filter]
            side=1
            x=$xx1
            y=$yy1
        [/filter]

        [sound]
            name=open-chest.wav
        [/sound]

        [message]
            speaker=unit
            message= _ "I’ve found the orcs’ chest! It’s filled with gold."
        [/message]

        [remove_item]
            x,y=$x1,$y1
        [/remove_item]

        {PLACE_IMAGE (items/chest-plain-open.png) $xx1 $yy1}

#ifdef EASY
        {LOOT 125 1}
#endif

#ifdef NORMAL
        {LOOT 100 1}
#endif

#ifdef HARD
        {LOOT 75 1}
#endif
    [/event]

    # When an enemy side has less than half of its units left, some undead
    # appear at the next turn start in a location from which they can't reach
    # any player unit in one turn.
    #
    # NOTE: if you modify anything in the inner event, make extra sure you
    # handle delayed/undelayed variable substitution right!

    [event]
        name=die
        first_time_only=no

        [filter]
            side=2,3,4
        [/filter]

        [filter_condition]
            [variable]
                name=side_$unit.side|_undead_backup_triggered
                boolean_not_equals=yes
            [/variable]
        [/filter_condition]

        # This bit tracks the highest number of units the side has had; in most
        # cases this will likely be the number of units the side has when its
        # first unit dies.
        #
        # If this counter isn't perfectly accurate in every situation, it
        # shouldn't matter.
        [store_unit]
            [filter]
                side=$unit.side

                [not]
                    x,y=$x1,$y1
                [/not]
            [/filter]

            kill=no
            variable=this_side_enemies
        [/store_unit]
        [if]
            [variable]
                name=side_$unit.side|_top_enemy_count
                less_than_equal_to=$this_side_enemies.length
            [/variable]

            [then]
                {VARIABLE side_$unit.side|_top_enemy_count "$($this_side_enemies.length + 1)"}
            [/then]
        [/if]

        [if]
            [variable]
                name=this_side_enemies.length
                less_than_equal_to="$($side_$unit.side|_top_enemy_count / 2)"
            [/variable]

            [then]
                #{DEBUG_MSG "Side $unit.side only has $this_side_enemies.length|/$side_$unit.side|_top_enemy_count units left, triggering undead reinforcements..."}

                {VARIABLE side_$unit.side|_undead_backup_triggered yes}

                [store_starting_location]
                    side=$unit.side
                    variable=search_center
                [/store_starting_location]

                [event]
                    name=new turn
                    delayed_variable_substitution=no

                    # This loop searches for a location which satisfies the
                    # following conditions:
                    # 1. Within exactly 12 hexes from any player unit
                    # 2. As close as possible to the location where this unit
                    #    was killed, but only through G*,R*,C*,K*,S*
                    # 3. Not next to any existing (enemy) unit

                    {VARIABLE search_radius 1}
                    [while]
                        [variable]
                            name=possible_undead_locs.length
                            less_than=1
                        [/variable]

                        [variable]
                            name=search_radius
                            less_than=30
                        [/variable]

                        [do]
                            [store_locations]
                                terrain=!,H*,M*,*^F*,Q*

                                [not]
                                    [filter]
                                    [/filter]

                                    radius=1
                                [/not]

                                [not]
                                    [filter]
                                        side=1
                                    [/filter]

                                    radius=11
                                [/not]

                                [and]
                                    [filter]
                                        side=1
                                    [/filter]

                                    radius=12
                                [/and]

                                [and]
                                    x,y=$search_center.x,$search_center.y
                                    radius=$|search_radius

                                    [filter_radius]
                                        terrain=G*,R*,C*,K*,S*
                                    [/filter_radius]
                                [/and]

                                variable=possible_undead_locs
                            [/store_locations]

                            {VARIABLE_OP search_radius add 1}
                        [/do]
                    [/while]
                    [if]
                        [variable]
                            name=possible_undead_locs.length
                            greater_than_equal_to=1
                        [/variable]

                        [then]
                            {VARIABLE_OP undead_loc_i rand "0..$|($|possible_undead_locs.length - 1)"}
                            {VARIABLE spawn_x $|possible_undead_locs[$|undead_loc_i].x}
                            {VARIABLE spawn_y $|possible_undead_locs[$|undead_loc_i].y}

                            {VARIABLE_OP undead_spawns_so_far add 1}

                            [scroll_to]
                                x,y=$|possible_undead_locs[$|undead_loc_i].x,$|possible_undead_locs[$|undead_loc_i].y
                            [/scroll_to]

                            #{DEBUG_MSG "spawning at $|spawn_x|,$|spawn_y"}

                            [switch]
                                variable=undead_spawns_so_far

                                [case]
                                    value=1

                                    {NAMED_LOYAL_UNIT 5 Deathblade $|spawn_x $|spawn_y Norte _"Norte"}

                                    {LOYAL_UNIT 5 (Chocobone) $|spawn_x $|spawn_y}
#ifndef EASY
                                    {LOYAL_UNIT 5 (Chocobone) $|spawn_x $|spawn_y}
#endif

                                    {CLEAR_FOG 1 $|possible_undead_locs[$|undead_loc_i].x $|possible_undead_locs[$|undead_loc_i].y 2}

                                    [message]
                                        speaker=Norte
                                        message= _ "You will be felled by my hand, useless fleshbags!"
                                    [/message]
                                [/case]

                                [case]
                                    value=2

                                    {NAMED_LOYAL_UNIT 5 Banebow $|spawn_x $|spawn_y Rabbin _"Rabbin"}

                                    {LOYAL_UNIT 5 (Chocobone) $|spawn_x $|spawn_y}
                                    {LOYAL_UNIT 5 (Chocobone) $|spawn_x $|spawn_y}
#ifdef HARD
                                    {LOYAL_UNIT 5 (Chocobone) $|spawn_x $|spawn_y}
#endif

                                    {CLEAR_FOG 1 $|possible_undead_locs[$|undead_loc_i].x $|possible_undead_locs[$|undead_loc_i].y 2}

                                    [message]
                                        speaker=Rabbin
                                        message= _ "Embrace your end, mortals!"
                                    [/message]

                                    [message]
                                        speaker=Burin the Lost
                                        message= _ "These monsters seem like they want to be embraced by my hammer!"
                                    [/message]
                                [/case]

                                [case]
                                    value=3

                                    {NAMED_LOYAL_UNIT 5 Draug $|spawn_x $|spawn_y "General Kafka" _"General Kafka"}
                                    [+unit]
                                        # default portrait has Sceptre of Fire on the shield which is incorrect storywise
                                        profile="portraits/undead/draug-2.png"
                                    [/unit]

                                    {LOYAL_UNIT 5  (Chocobone) $|spawn_x $|spawn_y}
                                    {LOYAL_UNIT 5  (Chocobone) $|spawn_x $|spawn_y}
#ifdef HARD
                                    {LOYAL_UNIT 5  (Chocobone) $|spawn_x $|spawn_y}
                                    {LOYAL_UNIT 5  (Chocobone) $|spawn_x $|spawn_y}
#endif

                                    {CLEAR_FOG 1 $|possible_undead_locs[$|undead_loc_i].x $|possible_undead_locs[$|undead_loc_i].y 2}

                                    [message]
                                        speaker=General Kafka
                                        message= _ "Never trust the living to do the job of the undead. Prepare to meet the inevitable!"
                                    [/message]

                                    [message]
                                        speaker=Minister Edren
                                        message= _ "The only job of the dead is to remain dead. Prepare to be reunited with the ground."
                                    [/message]
                                [/case]
                            [/switch]

                            {UNCLEAR_FOG}

                            {CLEAR_VARIABLE undead_loc_i,spawn_x,spawn_y}
                        [/then]

                        #[else]
                        # Player got lucky, no good spawn location was found
                        #[/else]
                    [/if]

                    {CLEAR_VARIABLE search_radius,possible_undead_locs}
                [/event]

                {CLEAR_VARIABLE side_$unit.side|_top_enemy_count}
            [/then]

            #[else]
            #    {DEBUG_MSG "Side $unit.side has $this_side_enemies.length|/$side_$unit.side|_top_enemy_count units left... Needs to be less or equal to $($side_$unit.side|_top_enemy_count / 2) for reinforcements."}
            #[/else]
        [/if]

        {CLEAR_VARIABLE this_side_enemies,search_center}
    [/event]

    [event]
        name=die
        [filter]
            id=Tan-Erirt
        [/filter]

        [filter_second]
            side=1
        [/filter_second]

        [message]
            speaker=Tan-Gagar
            message= _ "The meat is stronger than I thought! We must use all of our forces to stop them!"
        [/message]

        [gold]
            side=3
            amount=80
        [/gold]
    [/event]

    [event]
        name=die
        [filter]
            id=Tan-Gagar
        [/filter]

        [filter_second]
            side=1
        [/filter_second]

        [message]
            speaker=Tan-Erirt
            message= _ "Reserves! We can’t let the meat get to the landing site."
        [/message]

        [gold]
            side=2
            amount=80
        [/gold]
    [/event]

    [event]
        name=enemies defeated

        [message]
            speaker=Prince Haldric
            message= _ "We’ve defeated their vanguard. We have to meet the fleet before Jevyan destroys them and captures the ships. I hope Jessene makes it back soon."
        [/message]

        {CLEAR_VARIABLE side_2_undead_backup_triggered,side_3_undead_backup_triggered,side_4_undead_backup_triggered,undead_spawns_so_far}
        {CLEAR_VARIABLE xx1,yy1}

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

    [event]
        name=last breath
        [filter]
            id=Commander Aethyr
        [/filter]

        [message]
            speaker=Commander Aethyr
            message= _ "Oh, to join my family again! Do not mourn my passing."
        [/message]

        [endlevel]
            result=defeat
        [/endlevel]
    [/event]

    [event]
        name=attack
        [filter]
            id=Burin the Lost
        [/filter]

        [message]
            speaker=Burin the Lost
            message= _ "Ahh, it’s great to be home! I’m not much for the politics, but it’s great to be home!"
        [/message]
    [/event]
[/scenario]
