#textdomain wesnoth-sota

# Liches are now available to the player. All the stored units from the DA line need to
# have that option added to their unit trees. Since all dark sorcerers have been leveling
# into necromancers until this point, a menu option is included to turn them into liches
# too.

# The spiders nearest to the player start with no movement points, and get one added
# each turn for the first few turns. That keeps the spiders from swarming the player
# immediately, and lets the battle happen in the middle of the map. For difficulty
# levels other than hard, a few changes are made to the map that make the scenario
# a bit easier.

# The spiderweb terrain can be applied anywhere, but will be invisible if it is not used
# with specific shapes of corners or chasms. This limited the amount of artwork needed.

[scenario]
    name= _ "Abandoned Outpost"
    map_data="{campaigns/Secrets_of_the_Ancients/maps/18_Abandoned_Outpost.map}"

    id=18_Abandoned_Outpost
    next_scenario=19_Lava_and_Stone
    victory_when_enemies_defeated=no

    [story]
        [part]
            [background_layer]
                image=story/book.png
                scale=no
            [/background_layer]
            story= _ "9 VI, 23 YW

To become a lich, one must first die. The spells of necromancy bind the spirit, but only once it has been unbound from the body. To become a lich, the mage must make the necessary incantations with his dying breaths. He thus binds his own spirit in much the same way he binds the spirits of others... An altar serves to hold his soul in the physical plane long enough to make the transition, and a suitable object must be found to hold the consciousness for one crucial moment when the body expires."
        [/part]
        [part]
            [background_layer]
                image=portraits/undead/lich.png
                scale=no
            [/background_layer]
            story= _ "13 VI, 23 YW

So, Ras-Tabahn is now undead. In the beginning, his health was not much improved, and he complained of being cold. His life force was weak, but he was able to absorb life from the rats infesting the ancient castle. Now he is more strong and vigorous than I have ever seen him. This is despite the fact that his flesh is starting to rot away, a process that causes him no discomfort. Nor can he smell it any more than I can. If all continues to go well, I will make the transition soon myself."
        [/part]
        [part]
            [background_layer]
                image=portraits/ardonna-lich.png
                scale=no
            [/background_layer]
            story= _ "17 VI, 23 YW

Now the process is complete for both of us. I posted a guard so nothing could happen to me in my weakened state, and nothing did. I am restored. I don’t feel much different, but I suppose I look a fright. Ah well, it can’t be helped. But my primary goal is achieved! I cannot die! I suppose I could be broken apart like a skeleton or maybe burned, but I do not have to fear these unlikely events: I have the awesome powers of the spirit realm flowing through me, and can protect myself quite adequately! My next goal is to share my knowledge with the world so that <i>no one</i> need die."
        [/part]
    [/story]

    {UNDERGROUND}
    {DEFAULT_MUSIC_PLAYLIST}
    {TURNS 32 29 25}

    [side]
        {SIDE_1_BOTH}
        {GOLD 230 190 110}
        shroud=yes
    [/side]

    [side]
        side=2
        controller=ai
        user_team_name= _ "Spiders"
        team_name=bad
        no_leader=yes
        gold=0
        hidden=yes
    [/side]

    # *************************** PRESTART ***************************
    [event]
        name=prestart

        [objectives]
            side=1
            [objective]
                description= _ "Move Ardryn-Na and Ras-Tabahn to the west end of the cave"
                condition=win
            [/objective]
            {ALTERNATIVE_OBJECTIVE ( _ "Kill every enemy unit")}

            {HOW_TO_LOSE_WITH_BOTH}

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

            [note]
                description= _ "To turn a necromancer into a lich, right-click on it while it is in a castle."
            [/note]
        [/objectives]

        # There is now a menu option to turn necromancers into liches.
        [set_menu_item]
            id=lichify
            description= _ "Make Into a Lich"
            image=units/icons/icon-lich.png
            [show_if]
                [have_unit]
                    x=$x1
                    y=$y1
                    side=1
                    type=Necromancer, Necromancer SotA
                [/have_unit]
            [/show_if]
            [command]
                [if]
                    [have_location]
                        x=$x1
                        y=$y1
                        terrain=C*^*,K*^*,*^K*,*^C*  # castles and keeps
                    [/have_location]
                    [then]
                        [fire_event]
                            name=lichify  # See: sota-utils.cfg
                            [primary_unit]
                                x=$x1
                                y=$y1
                            [/primary_unit]
                        [/fire_event]
                    [/then]
                    [else]
                        [message]
                            speaker=narrator
                            image=logo.png
                            message= _ "You can only do this in a castle."
                        [/message]
                    [/else]
                [/if]
            [/command]
        [/set_menu_item]

        # From now on, newly recruited DAs need to be able to eventually advance to
        # liches like normal. We will stop using the custom units in the recruit list.
        [disallow_recruit]
            side=1
            type=Dark Adept SotA
        [/disallow_recruit]
        [allow_recruit]
            side=1
            type=Dark Adept
        [/allow_recruit]

        # This section changes all *existing* units so that they have liches in their
        # advancement trees. As of 1.14 at least, [modify_unit] doesn't apply to
        # the recall list, same for [transform_unit].
        [lua]
            code = <<
                for i, u in ipairs(wesnoth.get_recall_units { type = 'Dark Adept SotA, Dark Sorcerer SotA' }) do
                    if u.type == 'Dark Adept SotA' then
                        u:transform('Dark Adept')
                    elseif u.type == 'Dark Sorcerer SotA' then
                        u:transform('Dark Sorcerer')
                    end
                end
            >>
        [/lua]

        # We need to change all existing bats to normal types instead of SotA types
        # so they are drawn above terrains. SotA bats were necessary for the ship
        # where they were drawn behind the rigging, but for the spiderwebs in this
        # scenario, normal bats look a lot better. First, we'll make sure newly
        # recruited ones will be correct:
        [disallow_recruit]
            side=1
            type=Vampire Bat SotA
        [/disallow_recruit]
        [allow_recruit]
            side=1
            type=Vampire Bat
        [/allow_recruit]

        # Now the type of all bats on the recall list is changed
        [lua]
            code = <<
                for i, u in ipairs(wesnoth.get_recall_units { type = 'Vampire Bat SotA, Blood Bat SotA, Dread Bat SotA' }) do
                    if u.type == 'Vampire Bat SotA' then
                        u:transform('Vampire Bat')
                    elseif u.type == 'Blood Bat SotA' then
                        u:transform('Blood Bat')
                    elseif u.type == 'Dread Bat SotA' then
                        u:transform('Dread Bat')
                    end
                end
            >>
        [/lua]

#ifndef HARD
        [terrain]
            x,y=28,7
            terrain=Xu
        [/terrain]
        [terrain]
            x=25
            y=10-11
            terrain=Rb
        [/terrain]
        [terrain]
            x,y=32,7
            terrain=Qxu
        [/terrain]
#endif

        # The value of spider_number is *not* the number of spiders, but this variable does
        # help determine how many spiders get placed. See the repeats below.
#ifdef EASY
        [set_variable]
            name=spider_number
            value=4
        [/set_variable]
#endif
#ifdef NORMAL
        [set_variable]
            name=spider_number
            value=5
        [/set_variable]
#endif
#ifdef HARD
        [set_variable]
            name=spider_number
            value=6
        [/set_variable]
#endif

        [repeat]
            times="$($spider_number+5)"
            [do]
                [unit]
                    x,y=23,7
                    type=Giant Spider
                    side=2
                    placement=map
                    passable=yes
                [/unit]
            [/do]
        [/repeat]

        [repeat]
            times=5
            [do]
                [unit]
                    x,y=9,8
                    type=Giant Spider
                    side=2
                    placement=map
                    passable=yes
                    ai_special=guardian
                [/unit]
            [/do]
        [/repeat]
        [repeat]
            times=$spider_number
            [do]
                [unit]
                    x,y=9,10
                    type=Giant Spider
                    side=2
                    placement=map
                    passable=yes
                    ai_special=guardian
                [/unit]
            [/do]
        [/repeat]

        [clear_variable]
            name=spider_number
        [/clear_variable]

        # We aren't using the normal macro for the following recalls because it
        # doesn't specify locations, and that's important this time.
        [recall]
            id=Ras-Tabahn
            x,y=44,11
        [/recall]
        [recall]
            id=Carcyn
            x,y=44,11
        [/recall]
        # Only one Shynal will be recalled.
        [recall]
            id=Shynal
            x,y=44,11
        [/recall]
        [recall]
            id=Corpse Shynal
            x,y=44,11
        [/recall]
        [recall]
            id=Bone Captain
            x,y=39,9
        [/recall]
        [recall]
            id=Vendraxis
            x,y=39,10
        [/recall]

        # This map is just a larger version of the previous one, and that one had
        # a lot of its shroud removed. These tags cause the same hexes to be revealed
        # on this map as well.
        [remove_shroud]
            x,y=51,4
            radius=3
        [/remove_shroud]
        [remove_shroud]
            x,y=48,6
            radius=4
        [/remove_shroud]
        [remove_shroud]
            x,y=45,8
            radius=5
        [/remove_shroud]
        [remove_shroud]
            x,y=44,4
            radius=2
        [/remove_shroud]

        # The experience of either leader could be enough to level it into an ancient
        # lich, but that won't happen automatically. Modifying a unit *does* cause it
        # to advance though, so we will modify them both just in case.
        [modify_unit]
            [filter]
                id=Ardonna
                [or]
                    id=Ras-Tabahn
                [/or]
            [/filter]
        [/modify_unit]

        # Update the profile in case Ardonna didn't level here.
        [fire_event]
            name=ardonna_profile
        [/fire_event]

        # For some reason, the handling of Ardonna (and not Ras-Tabahn) causes her
        # to not have full mp at the start of the scenario, so fix that.
        [heal_unit]
            [filter]
                id=Ardonna
            [/filter]
            moves=full
        [/heal_unit]

        # This image needs to look the same as the one in the previous scenario.
        [item]
            image=items/altar-sacrifice.png~NO_TOD_SHIFT()
            x,y=45,7
        [/item]
    [/event]

    # *************************** PLAY ***************************

    # The closest group of spiders can't be allowed to move the full amount during the first
    # turns, or they would immediately block the tunnel and make the scenario boring. We
    # need to increase their movement gradually, so we will make their movement points be
    # one less than the turn number. We change their movement points with an object instead
    # of [modify_unit] so the correct distance they can move will be visible to the player.
#define CHANGE_SPIDER_MOVES MOVES
    [store_unit]
        [filter]
            # The far group of spiders won't move because they are guardians.
            # So, store only the closest group:
            x=20-40
            side=2
        [/filter]
        variable=spiders
    [/store_unit]

    [for]
        array=spiders
        variable=i
        [do]
            [object]
                [filter]
                    id=$spiders[$i].id
                [/filter]
                [effect]
                    apply_to=movement
                    set={MOVES}
                [/effect]
                silent=yes
                duration=turn
            [/object]
        [/do]
    [/for]

    [clear_variable]
        name=spiders
    [/clear_variable]
#enddef

    # This event needs to happen on the refresh for *both* sides so the player can see
    # the results during their turn. This event will be removed at a later turn.
    [event]
        name=side 1 turn refresh,side 2 turn refresh
        first_time_only=no
        id=spider_move_restrictions
        {CHANGE_SPIDER_MOVES ("$($turn_number - 1)")}
    [/event]

    [event]
        name=side 1 turn 5
        [remove_event]
            id=spider_move_restrictions
        [/remove_event]
        {CHANGE_SPIDER_MOVES 6}
    [/event]

    # This moves one leader out of the cave. If it's the first one, they get
    # refreshed and stored. If it's the second one, the scenario ends.
    [event]
        name=moveto
        first_time_only=no
        [filter]
            id=Ardonna
            x,y=1,10
            [or]
                id=Ras-Tabahn
                x,y=1,10
            [/or]
        [/filter]
        [if]
            [variable]
                name=leader_gone.length
                equals=1
            [/variable]
            [then]
                [hide_unit]
                    canrecruit=yes
                [/hide_unit]
                [fire_event]
                    name=end_scenario
                [/fire_event]
            [/then]
            [else]
                [heal_unit]
                    [filter]
                        side=1
                    [/filter]
                    amount=full
                    restore_statuses=yes
                [/heal_unit]
                [store_unit]
                    [filter]
                        x,y=1,10
                    [/filter]
                    variable=leader_gone
                    kill=yes
                [/store_unit]
                # The [set_variables] are to refresh this leader since they won't
                # be on the map at the end to be refreshed automatically.
                [set_variables]
                    name=leader_gone
                    mode=merge
                    [value]
                        x=recall
                        y=recall
                    [/value]
                [/set_variables]
                # The leader can't be unstored to the recall list yet, or the
                # other leader would be able to recall them. We'll do it just
                # before [endlevel].
            [/else]
        [/if]
    [/event]

    # *************************** ENDING / OPENING ***************************
    [event]
        name=start
        [message]
            speaker=narrator
            image=portraits/undead/lich.png~SCALE(93, 93)
            message= _ "You can now advance dark sorcerers to liches! For the rest of the campaign, you can also right-click on an existing necromancer to turn it into a lich as long as it is in a castle. If the necromancer has gained enough experience, it will immediately level into an ancient lich!"
        [/message]

        [message]
            speaker=Ardonna
            message= _ "We have conquered death! Let us bring the news back to Wesnoth. We should to let them know that nobody ever need die again."
        [/message]
        [message]
            speaker=Ras-Tabahn
            message= _ "I was planning to go that direction myself, so I have no objection. But, let us not go over those wretched mountain passes again. These tunnels should connect to the realm of Knalga and take us there directly."
        [/message]
        [message]
            speaker=Ardonna
            message= _ "Why would the Dwarvish empire abandon an outpost like this?"
        [/message]
        [message]
            speaker=Ras-Tabahn
            message= _ "We may find out. We should proceed cautiously."
        [/message]
    [/event]

    [event]
        name=sighted
        [filter]
            side=2
        [/filter]
        [if]
            [have_unit]
                id=Vendraxis
            [/have_unit]
            [then]
                [message]
                    speaker=Vendraxis
                    message= _ "Giant spidersss! That explainsss the cobwebsss everywhere."  #no spellcheck
                [/message]
            [/then]
            [else]
                [message]
                    speaker=Ardonna
                    message= _ "Giant spiders! That explains the cobwebs everywhere."
                [/message]
            [/else]
        [/if]
        [message]
            speaker=Ardonna
            message= _ "They are not moving much. Hopefully they continue to stay sleepy."
        [/message]
        [event]
            name=side 2 turn 3
            [message]
                speaker=Shynal
                message= _ "They’re waking up now. Watch out."
            [/message]
        [/event]
    [/event]

    [event]
        name=die
        first_time_only=no
        [if]
            [not]
                [have_unit]
                    side=2
                [/have_unit]
            [/not]
            [then]
                [fire_event]
                    name=end_scenario
                [/fire_event]
            [/then]
        [/if]
    [/event]

    [event]
        name=end_scenario

        # If one of the leaders left the map and is stored, bring it back now:
        [if]
            [variable]
                name=leader_gone.length
                equals=1
            [/variable]
            [then]
                [unstore_unit]
                    variable=leader_gone
                [/unstore_unit]
                [clear_variable]
                    name=leader_gone
                [/clear_variable]
            [/then]
        [/if]

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

    {TURN_INTO_A_LICH}
    {HERO_DEATHS}
    {MANAGE_PROFILE}
    {KIDS_ADVANCE}

#undef CHANGE_SPIDER_MOVES
[/scenario]
