#textdomain wesnoth-did

#define CLEAR_CORPSE_HORDE
    [kill]
        type=Walking Corpse
        [not]
            variation=drake,dwarf,goblin,gryphon,mounted,saurian,spider,swimmer,troll,wose,wolf,bat
        [/not]
        side=1
        [filter_wml]
            experience=0
        [/filter_wml]
    [/kill]
#enddef

# MODIFY_ONE_UNIT alters a unit variable for a single unit
#define MODIFY_ONE_UNIT FILTER WML
    [store_unit]
        [filter]
            {FILTER}
        [/filter]
        variable=MODIFY_UNIT_store
        kill=no
    [/store_unit]

    [modify_unit]
        [filter]
            id=$MODIFY_UNIT_store[0].id
        [/filter]
        {WML}
    [/modify_unit]

    {CLEAR_VARIABLE MODIFY_UNIT_store}
#enddef

# Create a new ghost-type "advisor" if Malin doesn't already have one
#define CREATE_ADVISOR
    [if]
        [have_unit]
            side=1
            role=advisor
            search_recall_list=yes
        [/have_unit]
        [then]
            # Recall an advisor if we have one
            [recall]
                role=advisor
            [/recall]
        [/then]
        [else]
            # Else, make a new advisor from a ghost unit
            [role]
                role=advisor
                side=1
                type=Spectre,Nightgaunt,Wraith,Shadow,Ghost
                search_recall_list=yes
                [auto_recall][/auto_recall]
                [else]
                    # If that fails too, make a brand new ghost advisor
                    [unit]
                        type=Ghost
                        side=1
                        role=advisor
                        animate=yes
                        placement=leader
                    [/unit]
                [/else]
            [/role]
        [/else]
    [/if]
#enddef

#define MANOR_DOORS ALLOWED_SIDES
    # Triggers when moving next to any door terrain and will replace it
    # with an 'opened' variant.

    [event]
        name=moveto
        first_time_only=no

        [filter]
            side={ALLOWED_SIDES}

            [filter_location]
                [filter_adjacent_location]
                    terrain=*^Pr*
                [/filter_adjacent_location]
            [/filter_location]
            [not]
                [filter_location]
                    [filter_adjacent_location]
                        terrain=*^Pr/o,*^Pr\o,*^Pr|o
                    [/filter_adjacent_location]
                [/filter_location]
            [/not]
        [/filter]

        [store_locations]
            terrain=*^Pr*

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

            variable=door_to_open
        [/store_locations]

        [terrain]
            x,y=$door_to_open.x,$door_to_open.y
            terrain="$door_to_open.terrain|o" # Append 'o' to the terrain string to get open variant.
            layer=both
        [/terrain]

        [redraw]
            side=$unit.side
        [/redraw]

        {CLEAR_VARIABLE door_to_open}
    [/event]
#enddef

# This event turns a necromancer into a lich. It can be activated by an event or from
# a right-click menu option. It needs $unit to be set or it won't do anything.
#
# Taken from Secrets of the Ancients, with the character-specific code removed.
#define TURN_INTO_A_LICH
    [event]
        name=lichify
        first_time_only=no

        # This is just to get a more descriptive name for the "unit" variable.
        [set_variables]
            name=stored_necromancer
            mode=replace
            to_variable=unit
        [/set_variables]

        # In DiD, only Mal Keshar himself can advance to be an Ancient Lich, and his
        # transformation is handled in scenario 11. I've left the SotA AMLA calculation
        # code in here, partly because it's easier to keep the code the same, and
        # partly in case someone enables the fourth level for other liches.
        #
        # The necromancer may have AMLAd and had its experience reset to 0. Now, as a
        # lich, there is a fourth level available for the unit, and all that experience
        # should be applied towards the level-up instead of simply being lost. We will
        # "unwind" each AMLA and calculate how much it cost, so we can give back those
        # experience points.
        [set_variable]
            name=num_amlas
            # This gives us the number of AMLAs that the unit has had:
            value=$stored_necromancer.modifications.advancement.length
        [/set_variable]
        [set_variable]
            # The experience that will be used for the new lich will be at least
            # the necromancer's current actual experience.
            name=experience_gained_after_max_level
            value=$stored_necromancer.experience
        [/set_variable]
        [set_variable]
            # Each AMLA took 20% more experience, so we will need to reduce this
            # value as we unwind them. The variable starts out storing the amount
            # needed for the *next* AMLA, so we will need to reduce it one step as
            # the first operation.
            name=experience_for_one_amla
            value=$stored_necromancer.max_experience
        [/set_variable]

        [while]
            [variable]
                name=num_amlas
                greater_than=0
            [/variable]
            [do]
                [set_variable]
                    # Each AMLA increased the experience needed by 20%, which
                    # is the same as multiplying by 1.2. We reverse that here.
                    name=experience_for_one_amla
                    divide=1.2
                [/set_variable]
                [set_variable]
                    # Round it off in case it's a float:
                    name=experience_for_one_amla
                    round=0
                [/set_variable]
                [set_variable]
                    # Add the experience that was needed for the previous AMLA
                    # to the amount that will be given to the lich:
                    name=experience_gained_after_max_level
                    add=$experience_for_one_amla
                [/set_variable]
                [set_variable]
                    name=num_amlas
                    sub=1
                [/set_variable]
            [/do]
        [/while]

        # We are going to create a new unit instead of transforming the old one.
        # Using the old unit would leave its AMLAs in effect, changing the hitpoints
        # and experience needed to level up.
        [unit]
            side=1
            type=Lich
            x=$stored_necromancer.x
            y=$stored_necromancer.y
            id=$stored_necromancer.id
            name=$stored_necromancer.name
            facing=$stored_necromancer.facing
            moves=$stored_necromancer.moves
            overlays=$stored_necromancer.overlays
            hitpoints=$stored_necromancer.hitpoints
            canrecruit=$stored_necromancer.canrecruit
            attacks_left=$stored_necromancer.attacks_left
            experience=$experience_gained_after_max_level

            # Copy the original traits into the new unit:
            [modifications]
                [insert_tag]
                    name=trait
                    variable=stored_necromancer.modifications.trait[0]
                [/insert_tag]
                [insert_tag]
                    name=trait
                    variable=stored_necromancer.modifications.trait[1]
                [/insert_tag]
            [/modifications]

            to_variable=new_lich  # Don't create the unit yet. Just store it.
        [/unit]

        # Hitpoints are transferred to the new unit so that turning into a lich doesn't
        # heal it. However, this could result in the lich having more HP than its
        # maximum. If that is the case, we will reset it to the maximum.
        [if]
            [variable]
                name=new_lich.hitpoints
                greater_than=$new_lich.max_hitpoints
            [/variable]
            [then]
                [set_variable]
                    name=new_lich.hitpoints
                    value=$new_lich.max_hitpoints
                [/set_variable]
            [/then]
        [/if]

        [unstore_unit]
            # The unit will automatically AMLA if its experience is sufficient.
            # However, in this campaign only Mal Keshar himself can advance to
            # be an Ancient Lich (his transformation is handled in scenario 11).
            variable=new_lich
            # po: This text goes by quickly, so it must be short.
            text= _ "Lich"
            red,green,blue=220,0,220  # dark-magic purple
            animate=yes
        [/unstore_unit]

        [clear_variable]
            name=stored_necromancer, new_lich, num_amlas, experience_for_one_amla, experience_gained_after_max_level
        [/clear_variable]
    [/event]
#enddef

#define SET_GOT_INSIDE_MANOR
    {MODIFY_UNIT (
        side=1
        {NOT_ON_RECALL_LIST}
        [not]
            id=Malin Keshar

            [or]
                id=Darken Volk
            [/or]
        [/not]
    ) variables.got_inside_manor yes}
#enddef
