• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[Action] TFS 1.4.2 - Use Item on Monster to Create New Monster

Jaed Le Raep

★Gaeming★
Joined
Sep 3, 2007
Messages
1,289
Reaction score
436
This script was made by @Alpha per my public request. Wanted to share it because it could end up being useful to other projects.

What this script does is when you use an item on a creature, it despawns the creature and spawns a new one in its place. A good example for this is to use a torch of a leaf golem, and it can spawn a fire elemental or a rotting golem, whatever creature you want. Pretty simple but also has a good amount of self-explanatory config.

Step 1:
In data/lib/core/table paste the following. If you don't already have table.lua file, create one.
Lua:
-- add to data/lib/core/table.lua if table.keys doesnt already exist
function table.keys(tbl)
    local keys = {}
    for key in pairs(tbl) do
        table.insert(keys, key)
    end

    return keys
end

Step 2:
Add TO THE TOP data/lib/core/core.lua
dofile('data/lib/core/table.lua')

Step 3:
Create .lua file in /data/scripts/ and paste the following code
Lua:
local items = {
    [1234] = { -- id of some usable item
        ["Leaf Golem"] = { -- name of targetable monster
            ["Rotting Golem"] = { -- name of possible choice for targeted monster
                chance = 100, -- chance of this monster being spawned, if it was chosen - if this fails, we roll again from the list of possible monster choices
                effect = CONST_ME_MAGIC_GREEN, -- effect to display if this monster gets spawned - this can also be left out, it will then default to CONST_ME_TELEPORT
            },
            ["Demon"] = { -- name of another possible choice for targeted monster
                chance = 20,
                effect = CONST_ME_HITBYFIRE,
            },
        },
        ["Rat"] = {
            ["Wolf"] = {
                chance = 100,
                effect = CONST_ME_MAGIC_RED,
            },
        }
    },
    [5678] = { -- id of some other usable item
        ["Cat"] = {
            ["Dog"] = {
                chance = 100,
            },
            ["Deer"] = {
                chance = 20,
            },
        },
        ["Rat"] = {
            ["Wolf"] = {
                chance = 100,
                effect = CONST_ME_ENERGYHIT,
            },
        }
    },
}

for _, itemData in pairs(items) do
    for _, targetData in pairs(itemData) do
        for possibleName, possibleData in pairs(targetData) do
            possibleData.name = possibleName
        end
    end
end

local action = Action()
function action.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local tool = items[item:getId()]
    if not tool then
        return false
    end

    if not target:isMonster() or target:getMaster() then
        return false
    end

    local monster = tool[target:getName()]
    if not monster then
        return false
    end

    local choices = table.keys(monster)
    local chosenMonster = nil
    repeat
        local choice = monster[choices[math.random(#choices)]]
        if math.random(100) <= choice.chance then
            chosenMonster = choice
        end
    until chosenMonster

    local targetPosition = target:getPosition()
    target:remove()
    Game.createMonster(chosenMonster.name, targetPosition, false, true, chosenMonster.effect)
    item:remove(1)
    return true
end

for id in pairs(items) do
    action:id(id)
end

action:register()

Again, 100% credit goes to @Alpha. It was with his permission that I posted this script.
 
Back
Top