#textdomain wesnoth-sota

# Two events in the "play" section are to remove four HP from Ras-Tabahn each turn. (He
# is suffering from tuberculosis.) There are some mages who put holy water around
# Crelanu. The water destroys undead, but can be removed by living units. It is there
# to prevent easy assassination of Crelanu with a few specters.

[scenario]
    name= _ "The Mage"
    map_file=16_The_Mage.map

    id=16_The_Mage
    next_scenario=17_Mortality
    victory_when_enemies_defeated=no

    [story]
        [part]
            [background_layer]
                image=story/mountain-pass.webp
                scale=no
            [/background_layer]
            story= _ "3 VI, 23 YW

We crossed the high plateaus, and forded the Arkan-thoria. Then we headed up into the mountains again before we found the mage. This was Ras-Tabahn’s plan, and with the goal in sight, he gained a little strength. However, he is quite weak and ill now, and breathing raggedly. I don’t know how much longer he can survive."
        [/part]
        {JOURNEY_PART 16}
    [/story]

    {DEFAULT_SCHEDULE}
    {DEFAULT_MUSIC_PLAYLIST}
    {TURNS 22 22 22}

    # wmllint: validate-off
    [side]
        {SIDE_1_BOTH}
        {GOLD 160 140 120}
        # There is only a little bit shrouded in this scenario (in the cave), but
        # we still need this:
        shroud=yes
    [/side]
    # wmllint: validate-on

    [side]
        side=2
        type=Elder Mage
        controller=ai
        user_team_name= _ "Crelanu"
        id=Crelanu
        profile=portraits/crelanu.webp
        name= _ "Crelanu"
        team_name=bad
        recruit=Red Mage,White Mage,Mage
        [ai]
            # Keep Crelanu within his ring of protective holy waters.
            passive_leader="yes"
        [/ai]
        {GOLD 160 180 200}
        {INCOME 10 15 20}
        color=green  # To match his color in LoW
        {FLAG_VARIANT loyalist}
    [/side]

    {STARTING_VILLAGES  1 6}
    {STARTING_VILLAGES  2 6}

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

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

            {HOW_TO_LOSE_WITH_BOTH}

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

            [note]
                description= _ "Ras-Tabahn is slowed and will lose four hitpoints every turn. He will die if he reaches zero."
            [/note]
            [note]
                description= _ "Moving any unit onto a bottle of holy water will remove the bottle. An undead unit will be destroyed."
            [/note]
        [/objectives]

        [remove_shroud]
            side=1
        [/remove_shroud]
        # For the cave:
        [place_shroud]
            side=1
            x=0-3
            y=24
        [/place_shroud]

        {RECALL_LOYAL_UNITS}

        [store_unit]
            [filter]
                id=Ras-Tabahn
            [/filter]
            variable=rastabahn_stored
        [/store_unit]
        [set_variables]
            name=rastabahn_stored.status
            mode=merge
            [value]
                slowed=yes
            [/value]
        [/set_variables]
        [unstore_unit]
            variable=rastabahn_stored
        [/unstore_unit]
    [/event]
    # *************************** PLAY ***************************
    # This gives side 2 level-3 units, but gives us control over when they appear.
    # Their appearance doesn't depend on side 2 saving gold for them either.
    [event]
        name=side 2 turn 1 refresh, side 2 turn 5 refresh
        first_time_only=no
        # He's not paying for these, but hopefully no one will notice!
        [unit]
            type=Arch Mage
            side=2
            x,y=20,7  # Otherwise, the first one goes for a village.
            moves=0  # Pretending it was just recruited.
        [/unit]
    [/event]

    # The next two events take four HP from Ras-Tabahn. If his HP is very low, he needs
    # to receive any healing (from villages or resting) *first*, or he will die. On the
    # other hand, if he has a lot of HP, and he is healed first, he will not receive
    # the full benefit. That is why we need two events. We will also be slowing him at
    # this time.
    [event]
        name=side 1 turn
        first_time_only=no
        [store_unit]
            [filter]
                id=Ras-Tabahn
            [/filter]
            variable=rastabahn_stored
        [/store_unit]
        [set_variable]
            name=rastabahn_harmed
            value=no
        [/set_variable]
        [if]
            [variable]
                name=rastabahn_stored.hitpoints
                greater_than=4
            [/variable]
            [then]
                [harm_unit]
                    [filter]
                        id=Ras-Tabahn
                    [/filter]
                    amount=4
                    fire_event=yes
                [/harm_unit]
                [set_variable]
                    name=rastabahn_harmed
                    value=yes
                [/set_variable]
            [/then]
            # [else] This will happen in the next event, *after* the refresh.
        [/if]
    [/event]

    [event]
        name=side 1 turn refresh
        first_time_only=no

        [store_unit]
            [filter]
                id=Ras-Tabahn
            [/filter]
            variable=rastabahn_stored
        [/store_unit]
        [set_variables]
            name=rastabahn_stored.status
            mode=merge
            [value]
                slowed=yes
            [/value]
        [/set_variables]
        [unstore_unit]
            variable=rastabahn_stored
        [/unstore_unit]

        [if]
            [variable]
                name=rastabahn_harmed
                boolean_not_equals=yes
            [/variable]
            [then]
                [harm_unit]
                    [filter]
                        id=Ras-Tabahn
                    [/filter]
                    amount=4
                    fire_event=yes
                [/harm_unit]
            [/then]
        [/if]
    [/event]

    # The next two events control the holy water.
    [event]
        name=recruit, recall
        [filter]
            race=undead
        [/filter]

        [unit]
            x,y=21,7
            type=White Mage
            name= _ "Ginmera"
            id=Ginmera
            side=2
            [modifications]
                {TRAIT_RESILIENT}
                {TRAIT_INTELLIGENT}
            [/modifications]
        [/unit]

#define PLACE_HOLY_WATER X Y
    [item]
        x={X}
        y={Y}
        image=items/holy-water.png
        name=holy_water
    [/item]
#enddef

        {PLACE_HOLY_WATER 20 6}
        {PLACE_HOLY_WATER 20 5}
        {PLACE_HOLY_WATER 21 5}
        {PLACE_HOLY_WATER 22 5}
        {PLACE_HOLY_WATER 22 6}

#undef PLACE_HOLY_WATER

        [store_items]
            variable=holy_waters
            item_name=holy_water
        [/store_items]

        [message]
            speaker=Ginmera
            message= _ "It seems that the invaders can raise undead soldiers, so I placed some holy water around the castle for protection."
        [/message]
        [unit]
            x,y=21,5
            type=White Mage
            name= _ "Rinconan"
            id=Rinconan
            side=2
            [modifications]
                {TRAIT_RESILIENT}
                {TRAIT_STRONG}
            [/modifications]
        [/unit]
        {FREEZE_UNIT Rinconan}
        {FREEZE_UNIT Ginmera}
        [message]
            speaker=Ginmera
            message= _ "Rinconan and I will stay here to guard you."
        [/message]
        [message]
            speaker=Crelanu
            message= _ "Yes. Thank you Ginmera."
        [/message]

        [message]
            speaker=narrator
            image=logo.png
            message= _ "Moving a unit onto a bottle of holy water will remove the bottle. A living unit can do this safely, but any undead unit will be destroyed."
        [/message]
    [/event]

    [event]
        name=moveto
        first_time_only=no
        [filter]
            side=1
            [filter_location]
                find_in=holy_waters
            [/filter_location]
        [/filter]
        [if]
            [have_unit]
                x,y=$x1,$y1
                [and]
                    race=undead
                [/and]
            [/have_unit]
            [then]
                [kill]
                    x,y=$x1,$y1
                    animate=yes
                    fire_event=yes
                [/kill]
            [/then]
        [/if]
        [remove_item]
            x,y=$x1,$y1
            image=holy_water
        [/remove_item]
        [store_items]
            variable=holy_waters
            item_name=holy_water
        [/store_items]
    [/event]

    [event]
        name=moveto
        [filter]
            id=Crelanu
        [/filter]
        {THAW_UNIT Ginmera}
        {THAW_UNIT Rinconan}
    [/event]

    # *************************** ENDING / OPENING ***************************
    [event]
        name=start
        [message]
            speaker=Ras-Tabahn
            message= _ "Crelanu! (<i>Cough, cough</i>) We have come... for your book."
        [/message]
        [message]
            speaker=Ardonna
            message= _ "We need to borrow it."
        [/message]
        [message]
            speaker=Crelanu
            message= _ "Yes, I knew people like you would be coming, but the book is out of reach now — for all of us. I have given it to the elves. Only they know where it is."
        [/message]
        [message]
            speaker=Ardonna
            message= _ "Why would you do such a thing?"
        [/message]
        [message]
            speaker=Crelanu
            message= _ "So that one like you would not be tempted to do what you intend. Oh, yes. I know well what you seek."
        [/message]
        [message]
            speaker=Ardonna
            message= _ "Then you can tell us what we want to know."
        [/message]
        [message]
            speaker=Crelanu
            message= _ "I shall not."
        [/message]
        [message]
            speaker=Ras-Tabahn
            message= _ "We shall see."
        [/message]
        [message]
            speaker=narrator
            image=logo.png
            message= _ "Ras-Tabahn is sick. He is slowed, and will lose four hitpoints every turn. He will die if he reaches zero."
        [/message]
    [/event]

    [event]
        name=last breath
        [filter]
            id=Crelanu
        [/filter]

        # Bring Crelanu back to life, barely:
        [store_unit]
            [filter]
                id=Crelanu
            [/filter]
            variable=crelanu_stored
            kill=yes
        [/store_unit]
        [set_variable]
            name=crelanu_stored.hitpoints
            value=1
        [/set_variable]
        [unstore_unit]
            variable=crelanu_stored
        [/unstore_unit]

        [message]
            speaker=Ardonna
            message= _ "Now, you will tell us the secret of immortality or die!"
        [/message]
        [message]
            speaker=Crelanu
            message= _ "There <i>is no secret</i>, you fools! Magic cannot stop aging. You cannot live forever. It is the way of nature, and the greatest thing we have in common with all of creation. You <i>must</i> die, and your soul depart. I would not try this thing for any reason."
        [/message]
        [message]
            speaker=Ardonna
            message= _ "You are the fool, for you have, indeed, told us all we required. For that, I will spare you to live a few more lonely years with your cowardice."
        [/message]

        [teleport]
            [filter]
                side=1
                canrecruit=yes
            [/filter]
            x,y=6,21
            animate=no
        [/teleport]
        [message]
            speaker=Ras-Tabahn
            message= _ "I don’t understand. What did Crelanu tell us?"
        [/message]
        [message]
            speaker=Ardonna
            message= _ "I will explain. Let us get you somewhere safe first. We’ll go into the cave. Come with me."
        [/message]
        [move_unit]
            id=Ardonna
            to_x=1
            to_y=24
        [/move_unit]
        [hide_unit]
            id=Ardonna
        [/hide_unit]
        # Move Ardonna out of the way so that Ras-Tabahn can reach this hex too.
        [teleport]
            [filter]
                id=Ardonna
            [/filter]
            x,y=1,22
            animate=no
        [/teleport]

        [move_unit]
            id=Ras-Tabahn
            to_x=1
            to_y=24
        [/move_unit]
        # Since no side-1 units should be around to see what Crelanu does next,
        # we'll hide everyone.
        [hide_unit]
            side=1
        [/hide_unit]

        # Crelanu moves to his keep to recruit a gryphon.
        [move_unit]
            id=Crelanu
            to_x=21
            to_y=6
        [/move_unit]
        [store_unit]
            [filter]
                id=Crelanu
            [/filter]
            variable=crelanu_stored
        [/store_unit]

        # We'd prefer the gryphon to be recruited in a castle hex, so we'll try to
        # find an empty one.
        {NEAREST_HEX 21 6 3 (
            terrain=C*
            [not]
                [filter]
                [/filter]
            [/not]
        ) location}
        [if]
            [variable]
                name=location.length
                greater_than=0
            [/variable]
            [then]
                [unit]
                    type=Gryphon
                    side=2
                    x=$location.x
                    y=$location.y
                    animate=yes
                    id=messenger
                [/unit]
            [/then]
            [else]
                [unit]
                    type=Gryphon
                    side=2
                    placement=leader
                    animate=yes
                    id=messenger
                [/unit]
            [/else]
        [/if]
        [move_unit]
            id=messenger
            to_x=16
            to_y=24
        [/move_unit]
        [kill]
            id=messenger
        [/kill]
        [redraw][/redraw]

        [clear_variable]
            name=rastabahn_stored, rastabahn_harmed, location, holy_waters
        [/clear_variable]
        [endlevel]
            result=victory
            bonus=yes
            {NEW_GOLD_CARRYOVER 40}
        [/endlevel]
    [/event]

    [event]
        # Crelanu should still be there as the scenario ends. The last breath event
        # is going to remove him, but we stored him so we can put him back.
        name=victory
        [unstore_unit]
            variable=crelanu_stored
        [/unstore_unit]
        [clear_variable]
            name=crelanu_stored
        [/clear_variable]
    [/event]

    {HERO_DEATHS}
    {MAKE_BATS_NORMAL}
    {ZOMBIES}
    {KIDS_ADVANCE}
[/scenario]
