#textdomain wesnoth-help
#define WEAPON_SPECIAL_SOTA_PLAGUE
    # Written this way to allow overwriting the base Necromancer weapon in the SotA Necromancer implementation
    [plague]
        id=SotA_plague
        name= _ "plague"
        description= _ "When a unit is killed by a Plague attack, that unit is replaced with a Walking Corpse on the same side as the unit with the Plague attack. This doesn’t work on Undead or units in villages."
        type=null # prevents normal corpse, and allows SotA code to handle it
    [/plague]
#enddef

#textdomain wesnoth-sota
#define ZOMBIE_INIT
    [event]
        name=prestart
        #wmllint: markcheck off
        # TODO: Most of the sota_variation strings could probably reuse race names from core and just add the parentheses in the Lua code
        # sota_variation will be used in the zombie recruit dialog
        [set_variables]
            name=zombies
            [value]
                type=SotA Flying Corpse_Bat
                # po: shown instead of the unit_type name in the recruitment dialog
                sota_name_in_recruit_dialog= _ "recruit^Flying Corpse (Bat)"
                allow_recruit_message= _ "You can now raise bat corpses!"
                allow_recruit=no
            [/value]
            [value]
                type=SotA Walking Corpse_Rat
                sota_name_in_recruit_dialog= _ "recruit^Walking Corpse (Rat)"
                allow_recruit_message= _ "You can now raise rat corpses!"
                allow_recruit=no
            [/value]
            [value]
                type=SotA Walking Corpse_Wolf
                sota_name_in_recruit_dialog= _ "recruit^Walking Corpse (Wolf)"
                allow_recruit_message= _ "You can now raise wolf corpses!"
                allow_recruit=no
            [/value]
            [value]
                type=SotA Walking Corpse_Human
                sota_name_in_recruit_dialog= _ "recruit^Walking Corpse (Human)"
                allow_recruit_message= _ "You can now raise human corpses!"
                allow_recruit=no
            [/value]
            [value]
                type=SotA Walking Corpse_Mounted
                # po: the mount is an undead horse (not a wolf), this is shown instead of the unit_type name in the recruitment dialog
                sota_name_in_recruit_dialog= _ "recruit^Walking Corpse (Mounted)"
                allow_recruit_message= _ "You can now raise mounted corpses!"
                allow_recruit=no
            [/value]
            [value]
                type=SotA Walking Corpse_Wolf Rider
                sota_name_in_recruit_dialog= _ "recruit^Walking Corpse (Wolf Rider)"
                allow_recruit_message= _ "You can now raise wolf rider corpses!"
                allow_recruit=no
            [/value]
            [value]
                type=SotA Swimming Corpse
                # po: this is used for both naga and merfolk zombies
                sota_name_in_recruit_dialog= _ "recruit^Swimming Corpse"
                # po: this is used for both naga and merfolk zombies
                allow_recruit_message= _ "You can now raise swimming corpses!"
                allow_recruit=no
            [/value]
            [value]
                type=SotA Walking Corpse_Saurian
                sota_name_in_recruit_dialog= _ "recruit^Walking Corpse (Saurian)"
                allow_recruit_message= _ "You can now raise saurian corpses!"
                allow_recruit=no
            [/value]
            [value]
                type=SotA Walking Corpse_Wose
                sota_name_in_recruit_dialog= _ "recruit^Walking Corpse (Wose)"
                allow_recruit_message= _ "You can now raise wose corpses!"
                allow_recruit=no
            [/value]
            [value]
                type=SotA Walking Corpse_Goblin
                sota_name_in_recruit_dialog= _ "recruit^Walking Corpse (Goblin)"
                allow_recruit_message= _ "You can now raise goblin corpses!"
                allow_recruit=no
            [/value]
            [value]
                type=SotA Flying Corpse_Gryphon
                sota_name_in_recruit_dialog= _ "recruit^Flying Corpse (Gryphon)"
                allow_recruit_message= _ "You can now raise gryphon corpses!"
                allow_recruit=no
            [/value]
            [value]
                type=SotA Walking Corpse_Spider
                sota_name_in_recruit_dialog= _ "recruit^Walking Corpse (Spider)"
                allow_recruit_message= _ "You can now raise spider corpses!"
                allow_recruit=no
            [/value]
            [value]
                type=SotA Walking Corpse_Troll
                sota_name_in_recruit_dialog= _ "recruit^Walking Corpse (Troll)"
                allow_recruit_message= _ "You can now raise troll corpses!"
                allow_recruit=no
            [/value]
            [value]
                type=SotA Walking Corpse_Dwarf
                sota_name_in_recruit_dialog= _ "recruit^Walking Corpse (Dwarf)"
                allow_recruit_message= _ "You can now raise dwarf corpses!"
                allow_recruit=no
            [/value]
        [/set_variables]
        #wmllint: markcheck on
    [/event]
#enddef

#define Z_CASE RACE ZOMBIE_TYPE
    [case]
        value={RACE}
        [set_variable]
            name=zombie_type
            value={ZOMBIE_TYPE}
        [/set_variable]
    [/case]
#enddef

# This macro saves the currently recruitable zombies into a string that can be
# used in a [set_variables] statement later to restore the list. Note that the
# 'zombies' variable is initialized by ZOMBIE_INIT at the start of the campaign
# and existing save files won't be changed even if the list in ZOMBIE_INIT is
# reordered or has new types added; so there's no danger of the saved list
# becoming out-of-sync with the player's zombie list due to any such changes.
#define SAVE_ZOMBIE_LIST VARIABLE
    [set_variable]
        name={VARIABLE}
        [join]
            variable=zombies
            key=allow_recruit
            separator=","
        [/join]
    [/set_variable]
#enddef

# Overwrite the current list of recruitable zombies with a saved list. This is
# used during the split-party section when the narrative switches from Ras to
# Ardonna, so any unit types that Ras has unlocked should be locked again until
# Ardonna discovers that type.
#define LOAD_ZOMBIE_LIST VARIABLE
    [set_variables]
        name=zombies
        mode=merge
        [split]
            list=${VARIABLE}
            key=allow_recruit
            separator=","
        [/split]
    [/set_variables]
#enddef

# When the two leaders join up again (in S13 Together Again), ensure that all
# types unlocked by either leader are still unlocked.
#define MERGE_ZOMBIE_LIST VARIABLE
    [set_variables]
        name=temp_merge_zombie_array
        [split]
            list=${VARIABLE}
            key=allow_recruit
            separator=","
        [/split]
    [/set_variables]
    [foreach]
        array=zombies
        [do]
            [if]
                [variable]
                    name=temp_merge_zombie_array[$i].allow_recruit
                    boolean_equals=yes
                [/variable]
                [then]
                    [set_variable]
                        name=this_item.allow_recruit
                        value="yes"
                    [/set_variable]
                [/then]
            [/if]
        [/do]
    [/foreach]
    [clear_variable]
        name=temp_merge_zombie_array
    [/clear_variable]
#enddef

# Each enemy unit type that is killed and *not* immediately raised as a corpse results
# in that type being added to the recruit list instead. We need a copy of the event for
# each enemy type that the player could run into. The bat version also works on the
# player's own bats. After there is more than one zombie on the list, zombies are no
# longer added to the recruit list directly, but to the zombie recruit dialog.
#define NEW_UNIT_TYPE_KILLED FILTER ZOMBIE_TYPE PROFILE_IMAGE
    [event]
        # Only humans are ever added with the custom event name, but we include it for
        # that case.
        name=die, add_{ZOMBIE_TYPE}
        [filter]
            {FILTER}
            [not]
                id=Ardonna,Ras-Tabahn # Don't trigger for hero death
            [/not]
        [/filter]
        # The unit won't immediately be raised if the attacking weapon was not a plague
        # weapon, or if the unit was in a village. (In which case, the weapon doesn't
        # matter.)
        [filter_condition]
            [variable]
                name=second_weapon.specials.plague.id
                # This matches a SotA zombie's touch or a SotA necromancer's staff.
                not_equals=SotA_plague
            [/variable]
            [or]
                [have_location]
                    x,y=$unit.x, $unit.y
                    terrain=*^V*
                [/have_location]
            [/or]
        [/filter_condition]
        # This is necessary to do math with this variable later.
        [set_variable]
            name=num_zombie_types
            value=0
        [/set_variable]
        # Find the current type in the zombie array.
        [foreach]
            array=zombies
            [do]
                [if]
                    [variable]
                        name=this_item.allow_recruit
                        boolean_equals=yes
                    [/variable]
                    [then]
                        [set_variable]
                            # This won't include the current one being added.
                            name=num_zombie_types
                            add=1
                        [/set_variable]
                    [/then]
                [/if]
                [if]
                    [variable]
                        name=this_item.type
                        equals={ZOMBIE_TYPE}
                    [/variable]
                    [variable]
                        name=this_item.allow_recruit
                        boolean_equals=no
                    [/variable]
                    [then]
                        [set_variable]
                            name=this_item.allow_recruit
                            value=yes
                        [/set_variable]
                        [message]
                            speaker=narrator
                            image={PROFILE_IMAGE}~SCALE(93,93)
                            message=$this_item.allow_recruit_message
                        [/message]
                    [/then]
                [/if]
            [/do]
        [/foreach]
        [if]
            [variable]
                name=num_zombie_types
                # If the number of previously available zombies is less than or equal to
                # this value, zombies are added to the recruit list.
                less_than_equal_to=1
            [/variable]
            [then]
                [allow_recruit]
                    type={ZOMBIE_TYPE}
                    side=1
                [/allow_recruit]
            [/then]
            [elseif]
                [variable]
                    name=num_zombie_types
                    # If the number of previously available zombies reaches this value,
                    # zombies are no longer directly added to the recruit list. They are
                    # added to the special recruit dialog instead.
                    equals=2
                [/variable]
                [then]
                    [foreach]
                        array=zombies
                        [do]
                            [disallow_recruit]
                                type=$this_item.type
                                side=1
                            [/disallow_recruit]
                        [/do]
                    [/foreach]
                    [allow_recruit]
                        type=SotA Walking Corpse_Dummy
                        side=1
                    [/allow_recruit]
                [/then]
            [/elseif]
        [/if]
        [clear_variable]
            name=num_zombie_types
        [/clear_variable]
    [/event]
#enddef

# This macro defines all the events that will be need to be included in each scenario.
#define ZOMBIES
    [event]
        name=prerecruit
        first_time_only=no
        [filter]
            type = SotA Walking Corpse_Dummy
        [/filter]
        # This lua function creates a dialog box for choosing a walking corpse type.
        # It sets two WML variables: recruitedZombieType and recruitedZombieCost.
        # If nothing was recruited after all, recruitedZombieType is set to "cancel".
        [lua]
            code = "wesnoth.dofile 'campaigns/Secrets_of_the_Ancients/utils/zombie_recruit_dialog.lua'"
        [/lua]
        [if]
            [variable]
                name = recruitedZombieType
                equals = "cancel"
            [/variable]
            [then]
                [kill]
                    id=$unit.id
                [/kill]
            [/then]
            [else]
                [transform_unit]
                    id=$unit.id
                    transform_to=$recruitedZombieType
                [/transform_unit]
                # At the moment, the new unit has the original single hitpoint, so:
                {FULL_HEAL id=$unit.id}
                # The player has already paid one gold (the minimum), but we still need
                # to take away the rest.
                [gold]
                    side=1
                    amount = "$(($recruitedZombieCost-1) * -1)"
                [/gold]
            [/else]
        [/if]
        [clear_variable]
            name=recruitedZombieType, recruitedZombieCost
        [/clear_variable]
    [/event]

    # This event shouldn't ever be fired, but a lua error in the recruit dialog would
    # allow the dummy unit to actually be recruited. This event prevents that.
    [event]
        name=recruit
        first_time_only=no
        [filter]
            type=SotA Walking Corpse_Dummy
        [/filter]
        [kill]
            id=$unit.id
        [/kill]
        [gold]
            side=1
            amount=1
        [/gold]
        [message]
            speaker=narrator
            message= _ "A lua error has occurred, and a corpse cannot be recruited."
        [/message]
    [/event]

    # This event recreates the plague special for SotA corpses.
    [event]
        name=die
        first_time_only=no

        [filter_condition]
            [variable]
                name=second_weapon.specials.plague.id
                # This matches a SotA zombie's touch or a SotA necromancer's staff.
                equals=SotA_plague
            [/variable]
        [/filter_condition]
        [filter]
            [not]
                race=undead
            [/not]
            [filter_location]
                [not]
                    terrain=*^V*
                [/not]
            [/filter_location]
        [/filter]

        # This will be the default (for elves, orcs, and humans).
        [set_variable]
            name=zombie_type
            value=SotA Walking Corpse_Human
        [/set_variable]
        # This switch statement chooses the type of zombie that will be created.
        [switch]
            variable=unit.race
            [case]
                value=monster
                [if]
                    [variable]
                        name=unit.type
                        equals=Giant Rat
                    [/variable]
                    [then]
                        [set_variable]
                            name=zombie_type
                            value=SotA Walking Corpse_Rat
                        [/set_variable]
                    [/then]
                    [else]
                        # Apparently, it's a spider.
                        [set_variable]
                            name=zombie_type
                            value=SotA Walking Corpse_Spider
                        [/set_variable]
                    [/else]
                [/if]
            [/case]
            [case]
                value=human
                [if]
                    [variable]
                        name=unit.undead_variation
                        equals=mounted
                    [/variable]
                    [then]
                        [set_variable]
                            name=zombie_type
                            value=SotA Walking Corpse_Mounted
                        [/set_variable]
                    [/then]
                [/if]
            [/case]
            [case]
                value=wolf
                [if]
                    [variable]
                        name=unit.type
                        equals=Wolf
                    [/variable]
                    [or]
                        [variable]
                            name=unit.type
                            equals=Great Wolf
                        [/variable]
                    [/or]
                    [then]
                        [set_variable]
                            name=zombie_type
                            value=SotA Walking Corpse_Wolf
                        [/set_variable]
                    [/then]
                    [else]
                        [set_variable]
                            name=zombie_type
                            value=SotA Walking Corpse_Wolf Rider
                        [/set_variable]
                    [/else]
                [/if]
            [/case]
            {Z_CASE bats "SotA Flying Corpse_Bat"}
            {Z_CASE (merman, naga) "SotA Swimming Corpse"}
            {Z_CASE  lizard "SotA Walking Corpse_Saurian"}
            {Z_CASE  wose "SotA Walking Corpse_Wose"}
            {Z_CASE goblin "SotA Walking Corpse_Goblin"}
            {Z_CASE  gryphon "SotA Flying Corpse_Gryphon"}
            {Z_CASE  troll "SotA Walking Corpse_Troll"}
            {Z_CASE  dwarf "SotA Walking Corpse_Dwarf"}
        [/switch]
        [kill]
            id=$unit.id
        [/kill]
        [unit]
            type=$zombie_type
            side=1
            x, y=$unit.x, $unit.y
            moves=0
            attacks_left=0
            animate=yes
        [/unit]
        [clear_variable]
            name=zombie_type
        [/clear_variable]
    [/event]

    {NEW_UNIT_TYPE_KILLED (race=bats) ("SotA Flying Corpse_Bat") "portraits/undead/zombie-bat.webp"}
    {NEW_UNIT_TYPE_KILLED (type=Giant Rat) ("SotA Walking Corpse_Rat") "portraits/undead/zombie-rat.webp"}
    {NEW_UNIT_TYPE_KILLED (type_adv_tree=Wolf) ("SotA Walking Corpse_Wolf") "portraits/undead/zombie-wolf.webp"}
    {NEW_UNIT_TYPE_KILLED (race=human) ("SotA Walking Corpse_Human") "portraits/undead/walking-corpse.webp"}
    {NEW_UNIT_TYPE_KILLED (type_adv_tree=Horseman, Cavalryman) ("SotA Walking Corpse_Mounted") "portraits/undead/walking-corpse.webp"}
    {NEW_UNIT_TYPE_KILLED (type_adv_tree=Wolf Rider) ("SotA Walking Corpse_Wolf Rider") "portraits/undead/zombie-goblin.webp"}
    {NEW_UNIT_TYPE_KILLED (race=merman,naga) ("SotA Swimming Corpse") "portraits/undead/zombie-swimmer.webp"}
    {NEW_UNIT_TYPE_KILLED (race=lizard) ("SotA Walking Corpse_Saurian") "portraits/undead/zombie-saurian.webp"}
    {NEW_UNIT_TYPE_KILLED (race=wose) ("SotA Walking Corpse_Wose") "portraits/undead/zombie-wose.webp"}
    {NEW_UNIT_TYPE_KILLED (race=goblin) ("SotA Walking Corpse_Goblin") "portraits/undead/zombie-goblin.webp"}
    {NEW_UNIT_TYPE_KILLED (race=gryphon) ("SotA Flying Corpse_Gryphon") "portraits/undead/zombie-gryphon.webp"}
    {NEW_UNIT_TYPE_KILLED (type=Giant Spider) ("SotA Walking Corpse_Spider") "portraits/undead/zombie-spider.webp"}
    {NEW_UNIT_TYPE_KILLED (race=troll) ("SotA Walking Corpse_Troll") "portraits/undead/zombie-troll.webp"}
    {NEW_UNIT_TYPE_KILLED (race=dwarf) ("SotA Walking Corpse_Dwarf") "portraits/undead/zombie-dwarf.webp"}
#enddef
