#textdomain wesnoth-dw

# I think the key to this scenario is "Control the villages!" You get
# a bunch of free citizens, so send them all directions and take as
# many villages as possible right away. Recruit two level ones
# (fighters or hunters) and the rest citizens this turn. Send those
# two and two or three citizens straight south to get in a battle over
# the group of villages there. Try not to lose the level one units,
# but more importantly, don't let any bats get by you! Another level
# one unit may be necessary in the lower middle of the map to keep the
# bats from getting by. Initiates are very effective at bat disposal,
# so use them. Send one more fighter or hunter and some citizens to
# hold the north-east corner villages. Don't worry about the land
# villages too much, but hold the ones in the water. Use Cylanna,
# Gwabbo, and a few citizens to just hold off the skeletons and
# soulless south east of your keep until the village situation is
# under control. When it is, bring in more units to help them. Attack
# during the day, but retreat and heal at night: You can't afford to
# have very many units zombified. Also, rotate your units and use the
# ZOC of Gwabbo and your level ones to sheild your wounded. Fighters
# are the best for this because they can take some punishment. When
# the map starts to thin out, circle most units around to the enemy
# leader. Try to make some brawlers, and take Gwabbo's advice to get
# Krellis as much experience as possible.

# You can also just charge, and if you time it right, the leader should be poorly
# defended because all his units will be off taking villages. Maybe.

# The most complicated event here pops zombies out of villages taken by enemy
# units. There is also a fair bit of animation and story events. One
# event makes a black ghost rise from the corpse of the enemy leader
# and disappear under the mountain. There is also some action around a
# southern village.

[scenario]
    name= _ "Invasion!"
    map_data="{campaigns/Dead_Water/maps/Home_1.map}"

    id=01_Invasion
    next_scenario=02_Flight

    [story]
        [part]
            background=dead-water-map.png
            story= _ "In the Far North, where the Mountains of Dorth approach the coast, lies a bay with a narrow mouth. In the calm waters of this bay lies the merfolk city of Jotha. The natural harbor and the prosperity of the dwellers within drew many envious eyes, but none could match the prowess of the merfolk in their aquatic domain. In most years, orcs from the port city of Tirigaz, further north, would raid against Jotha as predictably as the spring rains; always, they were driven back to dry land with heavy losses, the salt water of the bay stinging their wounds."
        [/part]

        [part]
            story= _ "In 626 YW, Jotha was attacked by a larger raiding party than ever before. The king Kai Laudiss led his troops to repel the raiders. They were successful, but merfolk casualties were greater than usual. And numbered with the slain was the wife of Kai Laudiss. The kai’s grief was great, but his wrath was also fearsome to behold. He readied his army to follow the orcs. His stated purpose was not revenge, but the desire to secure the safety of Jotha with a demonstration of force that would teach the orcs to steer well clear of the merfolk. The merfolk army caught up with the orc band as it reached Tirigaz."
        [/part]

        [part]
            story= _ "Under the cover of darkness, the mermen slid into the bay, intending to launch a surprise attack at dawn. But the orcs were prepared for this, and had hastily devised a surprise of their own. Ships at the mouth of the bay that had seemed but derelict hulks disgorged hordes of orcs. More of them poured forth from the city. The merfolk were trapped, and found themselves vastly outnumbered."
        [/part]

        [part]
            story= _ "Kai Laudiss’s army fought fiercely, slaying five orcs for every warrior they lost. The kai himself fought with reckless abandon; foes fell with each swing of his great mace. The orcs discovered that trapped mermen were tougher than they had imagined, and found themselves fleeing into the forest to escape their vengeful spears. As the kai had intended, it was a defeat the orcs would not soon forget; but the cost was heavy. Kai Laudiss was felled by a poisoned orcish dart, and the greater part of his army was destroyed. When the remaining soldiers returned to Jotha, Krellis, the young son of Kai Laudiss, learned that he had become kai."
        [/part]

        [part]
            story= _ "Kai Krellis felt too young for the responsibilities of a kingdom, so he relied on the wisdom of a priestess named Cylanna. She had been a friend of his father, and he had known her as long as he could remember. Cylanna mourned the former kai, but believed that his sacrifice would allow Krellis to preside over an age of peace. Unfortunately, that was not to be. A new enemy appeared from under the mountains."
        [/part]

        [part]
            background=dead-water-map.png
            show_title=yes
            {NEW_BATTLE 746 673}
        [/part]
    [/story]

    {DEFAULT_SCHEDULE}
    {TURNS4 30 28 26 26}

    [side]
        # wmllint: recognize Kai Krellis
        {SIDE_1}
        {GOLD4 110 110 130 130}
    [/side]

    [side]
        side=2
        controller=ai
        user_team_name= _ "Undead Forces"
        team_name="bad guys"
        color=teal

        type="Dark Sorcerer"
        id="Mal-Kevek"
        name= _ "Mal-Kevek"
        canrecruit=yes
        {FLAG_VARIANT undead}

#ifdef EASY
        recruit="Walking Corpse, Vampire Bat, Skeleton Archer, Skeleton"
        [ai]
            recruitment_pattern=fighter,fighter,archer,scout
        [/ai]
#endif

#ifdef NORMAL
        recruit="Walking Corpse, Soulless, Vampire Bat, Skeleton Archer, Skeleton"
        [ai]
            recruitment_pattern=fighter,fighter,archer,scout
        [/ai]
#endif

#ifdef HARD
        recruit="Walking Corpse, Soulless, Vampire Bat, Blood Bat, Skeleton Archer, Skeleton"
        [ai]
            recruitment_pattern=fighter,fighter,archer,scout,scout
        [/ai]
#endif

#ifdef NIGHTMARE
        recruit="Walking Corpse, Soulless, Vampire Bat, Blood Bat, Skeleton Archer, Skeleton"
        [ai]
            recruitment_pattern=fighter,fighter,archer,scout,scout,scout
        [/ai]
#endif

        {GOLD4 120 160 210 250}
        {INCOME4 0 2 4 6}

        # It's not fair if the enemy leader goes into the mountains
        # where the merfolk can't follow--even slowly We'll have the
        # leader avoid the mountains.
        [ai]
            [avoid]
                x=43,43,43,43,43,42,42,41,41,41,40,39,38,37,36,35,33,32
                y=13,14,15,16,17,17,18,19,20,21,22,24,25,26,26,27,27,27
            [/avoid]
        [/ai]
    [/side]

    {RECRUIT_UNIT_VARIATIONS 2 "Walking Corpse" swimmer}
    {RECRUIT_UNIT_VARIATIONS 2 "Soulless" swimmer}

    {LIMIT_RECRUITS 1 (Merman Hunter) 3}
    {LIMIT_RECRUITS 1 (Merman Fighter) 3}
    {LIMIT_RECRUITS 1 (Mermaid Initiate) 3}

#define PUT_CITIZEN X Y TRAIT_1_NAME TRAIT_2_NAME
    [unit]
        type=Merman Citizen
        side=1
        x={X}
        y={Y}
        # wmlscope: start ignoring
        [modifications]
            {TRAIT_{TRAIT_1_NAME}}
            {TRAIT_{TRAIT_2_NAME}}
        [/modifications]
        # wmlscope: stop ignoring
        generate_name=yes
    [/unit]
#enddef

#define SET_VILLAGE_ZOMBIE_PATTERN N1_AMOUNT N2_AMOUNT N3_AMOUNT N4_AMOUNT N5_AMOUNT TYPE
    [set_variables]
        name=zombie_number_pattern
        mode=replace
        [value]
            number={N1_AMOUNT}
        [/value]
        [value]
            number={N2_AMOUNT}
        [/value]
        [value]
            number={N3_AMOUNT}
        [/value]
        [value]
            number={N4_AMOUNT}
        [/value]
        [value]
            number={N5_AMOUNT}
        [/value]
    [/set_variables]
    [set_variable]
        name=zombie_type
        value={TYPE}
    [/set_variable]
#enddef

    [event]
        name=prestart

        [music]
            name=the_king_is_dead.ogg
        [/music]
        [music]
            name=vengeful.ogg
            append=yes
        [/music]
        [music]
            name=legends_of_the_north.ogg
            append=yes
        [/music]

        [objectives]
            side=1
            [objective]
                description= _ "Defeat enemy leader"
                condition=win
            [/objective]

            {HOW_TO_LOSE}

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

        {PUT_CITIZEN 19 6 INTELLIGENT STRONG}
        {PUT_CITIZEN 21 6 STRONG RESILIENT}
        {PUT_CITIZEN 14 9 QUICK INTELLIGENT}
        {PUT_CITIZEN 26 9 RESILIENT QUICK}
        {PUT_CITIZEN 19 13 STRONG QUICK}
        {PUT_CITIZEN 21 13 RESILIENT INTELLIGENT}

        [unit]
            type=Mermaid Priestess
            [modifications]
                {TRAIT_LOYAL}
                {TRAIT_RESILIENT}
            [/modifications]
            id=Cylanna
            name= _ "Cylanna"
            profile=portraits/transparent/cylanna.png
            side=1
            x=21
            y=9
            {IS_HERO}
            unrenamable=yes
        [/unit]

        [set_variable]
            name=number_of_captured_villages
            value=0
        [/set_variable]
#ifdef EASY
        {SET_VILLAGE_ZOMBIE_PATTERN 1 1 1 2 1 (Walking Corpse)}
#endif

#ifdef NORMAL
        {SET_VILLAGE_ZOMBIE_PATTERN 1 2 1 2 2 (Walking Corpse)}
#endif

#ifdef HARD
        {SET_VILLAGE_ZOMBIE_PATTERN 2 2 2 2 2 (Soulless)}
#endif

#ifdef NIGHTMARE
        {SET_VILLAGE_ZOMBIE_PATTERN 2 2 1 2 3 (Soulless)}
#endif
    [/event]

#undef PUT_CITIZEN
#undef SET_VILLAGE_ZOMBIE_PATTERN

    [event]
        name=start

        [message]
            speaker=Kai Krellis
            message= _ "Is something wrong, priestess?"
        [/message]

        [message]
            speaker=Cylanna
            message= _ "Maybe. I smell death and decay."
        [/message]

        [scroll_to]
            x=34
            y=23
        [/scroll_to]

        [unit]
            type=Skeleton
            side=2
            x=34
            y=23
            animate=yes
            id=fiend
        [/unit]

        [hide_unit]
            x=34
            y=23
        [/hide_unit]
        [teleport]
            [filter]
                id=fiend
            [/filter]
            x=35
            y=20
        [/teleport]
        [move_unit_fake]
            type=Skeleton
            x=34, 35
            y=23, 20
            side=2
        [/move_unit_fake]
        [unhide_unit]
        [/unhide_unit]
        [delay]
            time=300
        [/delay]

        [unit]
            type=Merman Netcaster
            id=Gwabbo
            name= _ "Gwabbo"
            unrenamable=yes
            side=1
            x=34
            y=20
            hitpoints=4
            [modifications]
                {TRAIT_LOYAL}
                {TRAIT_FEARLESS}
            [/modifications]
            facing=se
            animate=yes
            overlays=overlays/loyal-icon.png
            profile=portraits/transparent/gwabbo.png
        [/unit]

        [message]
            speaker=Gwabbo
            message= _ "Back, you fiend! You have not won...I will return to finish this."
        [/message]

        {MOVE_UNIT id=Gwabbo 20 10}

        [delay]
            time=500
        [/delay]

        [message]
            speaker=Gwabbo
            message= _ "Kai! We are under attack! Fell creatures invaded our village and slew without mercy. I managed to fight my way clear, and have come to seek your aid."
        [/message]

        [message]
            speaker=Kai Krellis
            message= _ "What manner of creatures were they?"
        [/message]

        [message]
            speaker=Gwabbo
            message= _ "I have never seen their like before in all my years. They were like men, but they had no skin. Our spears went right through them; they <i>laughed</i> at our weapons. And they smelled like nothing in the sea, of dank earth and rottenness."
        [/message]

        [message]
            speaker=Cylanna
            message= _ "Undead. This will be your first real test as a leader, Kai Krellis. You must defeat these creatures."
        [/message]

        [message]
            speaker=Gwabbo
            message= _ "Kai, please lead us against them yourself, it will hearten the folk. And — if you will permit — you reach the age now where a kai must learn battle skills."
        [/message]

        [message]
            speaker=Cylanna
            message= _ "Gwabbo speaks wisely. You must fight; I will protect you as much as I can...but when war looms, a kai must lead. It is our way."
        [/message]

        [message]
            speaker=narrator
            message= _ "You have few trained fighters, so you can only recruit three of each level 1 unit. There is no limit on citizens."
            image=wesnoth-icon.png
            duration=60
        [/message]
    [/event]

#define PUT_CORPSE_SWIMMER X Y
    [unit]
#ifndef NIGHTMARE
        type=Walking Corpse
#else
        type=Soulless
#endif
        side=2
        variation=swimmer
        x={X}
        y={Y}
        animate=yes
    [/unit]
    [delay]
        time=200
    [/delay]
#enddef

    [event]
        name=ai turn
        first_time_only=yes

        [message]
            speaker=Mal-Kevek
            message= _ "Rise, my slaves! We will create an army for Lord Ravanal."
        [/message]

        {PUT_CORPSE_SWIMMER 34 20}
        {PUT_CORPSE_SWIMMER 34 21}
        {PUT_CORPSE_SWIMMER 35 21}

        [message]
            speaker=Cylanna
            message= _ "He is raising our dead to fight against us! Even though they were your own citizens, Krellis, show no mercy. Destroy them if you can."
        [/message]
    [/event]

#undef PUT_CORPSE_SWIMMER

    [event]
        name=last breath
        [filter]
            id=Mal-Kevek
        [/filter]

        [store_unit]
            [filter]
                id=Mal-Kevek
            [/filter]
            variable=MK
        [/store_unit]

        [message]
            id=Mal-Kevek
            message= _ "Foolish fish-men. You cannot kill <i>me</i> that easily."
        [/message]

        [message]
            speaker=narrator
            message= _ "The merfolk saw a shadowy shape rise from the dead body of the necromancer and disappear under the mountain."
            image=wesnoth-icon.png
        [/message]
    [/event]

    [event]
        name=die
        [filter]
            id=Mal-Kevek
        [/filter]

        [move_unit_fake]
            type=Dark Shape
            x=$MK.x, 37, 37, 36, 35, 34, 33, 34
            y=$MK.y, 25, 26, 26, 27, 26, 26, 24
            side=2
        [/move_unit_fake]

        [sound]
            name=wail-long.wav
        [/sound]

        {CLEAR_VARIABLE MK}
    [/event]

    # Zombies are spawned manually during the starting sequence, so we
    # won't zombify villages until after turn 1 is over:
    [event]
        name=turn 2

        # Pop up a couple zombies when the bad guys capture a village
        # for the first time. (Recapturing doesn't do anything.)
        [event]
            name=capture
            first_time_only=no

            [filter]
                side=2
            [/filter]

            # Check to see if the village has been taken over before.
            # First, get the location:
            [set_variable]
                name=village_location
                value=$x1|_$y1
            [/set_variable]

            # Loop through the zombified villages to see if that
            # location has already been stored:
            {FOREACH zombified_villages loop}
                [if]
                    [variable]
                        name=village_location
                        equals=$zombified_villages[$loop].location
                    [/variable]

                    [then]
                        [set_variable]
                            name=already_zombified
                            value=yes
                        [/set_variable]
                    [/then]
                [/if]
            {NEXT loop}

            # If the village's location was not found in the
            # array, it hasn't been taken over yet, so zombify
            # it:
            [if]
                [variable]
                    name=already_zombified
                    not_equals=yes
                [/variable]
                [then]
                    # Note that it *is* taken over now by
                    # saving the location in the array:
                    [set_variable]
                        name=zombified_villages[$number_of_captured_villages].location
                        value=$x1|_$y1
                    [/set_variable]

                    # If it is a merman village, set
                    # zombie_variation to swimmer.
                    # Otherwise, make normal zombies:
                    [if]
                        [have_location]
                            terrain=Ww^Vm,Ss^Vm
                            x=$x1
                            y=$y1
                        [/have_location]
                        [then]
                            [set_variable]
                                name=zombie_variation
                                value=swimmer
                            [/set_variable]
                        [/then]
                        [else]
                            [set_variable]
                                name=zombie_variation
                                value=none
                            [/set_variable]
                        [/else]
                    [/if]

                    # Get the number of zombies from the list of numbers:
                    [set_variable]
                        name=index
                        value=$($number_of_captured_villages % 5)
                    [/set_variable]
                    [set_variable]
                        name=number_of_zombies
                        value=$zombie_number_pattern[ $($number_of_captured_villages % 5) ].number
                    [/set_variable]
                    #{DEBUG_MSG zombie_number_pattern[$index]

                    # Now place the zombies. For each one,
                    # decrease the number until it reaches 0
                    [while]
                        [variable]
                            name=number_of_zombies
                            greater_than=0
                        [/variable]
                        [do]
#ifdef HARD
                            [if]
                                [variable]
                                    name=zombie_type
                                    equals=Soulless
                                [/variable]
                                [then]
                                    [set_variable]
                                        name=zombie_type
                                        value=Walking Corpse
                                    [/set_variable]
                                [/then]
                                [else]
                                    [set_variable]
                                        name=zombie_type
                                        value=Soulless
                                    [/set_variable]
                                [/else]
                            [/if]
#endif
                            [unit]
                                type=$zombie_type
                                side=$unit.side
                                variation=$zombie_variation
                                x=$x1
                                y=$y1
                                moves=0
                                attacks_left=0
                                animate=yes
                            [/unit]

                            [set_variable]
                                name=number_of_zombies
                                sub=1
                            [/set_variable]
                        [/do]
                    [/while]

                    [set_variable]
                        name=number_of_captured_villages
                        add=1
                    [/set_variable]

                    # The second time this happens, warn the player that this will
                    # keep happening (in case it's not obvious yet):
                    [if]
                        [variable]
                            name=number_of_captured_villages
                            equals=2
                        [/variable]
                        [then]
                            [redraw][/redraw]
                            [message]
                                speaker=Kai Krellis
                                message= _ "He is raising the dead from every village!"
                            [/message]
                            [message]
                                speaker=Gwabbo
                                message= _ "This will be a dark day, but we <i>will</i> prevail."
                            [/message]
                        [/then]
                    [/if]

                    #{CLEAR_VARIABLE number_of_zombies}
                    {CLEAR_VARIABLE zombie_variation}
                    {CLEAR_VARIABLE already_zombified}
                    {CLEAR_VARIABLE village_location}
                [/then]
            [/if]
        [/event]
    [/event]

    [event]
        name=die
        [filter_second]
            id=Kai Krellis
        [/filter_second]

        [message]
            speaker=Kai Krellis
            message= _ "Hey, I did it!"
        [/message]
        [message]
            speaker=Gwabbo
            message= _ "Good work Kai, if I may say so."
        [/message]
    [/event]

    [event]
        name=enemies defeated

        {CLEAR_VARIABLE side_1_limited_recruits}
        {CLEAR_VARIABLE zombified_villages}
        {CLEAR_VARIABLE number_of_captured_villages}
        {CLEAR_VARIABLE zombie_type}

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

    {HERO_DEATHS}
[/scenario]
