• 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!

TFS 1.X+ Set loot rate *1 to specific creatures

ralke

(҂ ͠❛ ෴ ͡❛)ᕤ
Joined
Dec 17, 2011
Messages
1,521
Solutions
27
Reaction score
870
Location
Santiago - Chile
GitHub
ralke23
Twitch
ralke23
Hi guys! Like the title said, how can I set lootrate *1 to specific creatures? I was thinking on dividing creature loot chances for the amount of loot rate manually, but there's a way to do it via .lua? Thanks in advance. I use TFS 1.5 downgrades 8.6.

PS: Can be source/side too. The thing is that it doesn't get affected by config.lua.
 
scripts\eventcallbacks\monster\default_onDropLoot.lua

maybe . i have a doubt with the getname

Lua:
local mType = self:getType()
local yourchance = XXX
...

if mType:getName() == "ralke"  then


     for i = 1, #monsterLoot do
            if monsterLoot[i].chance then
                monsterLoot[i].chance =    math.min(monsterLoot[i].chance * yourchance, MAX_LOOTCHANCE)
            end
....

improve it with
local array = {names}
isinarray and :lower


Better to use python and edit the xml (use import xml.etree.ElementTree as ET)
why make a function that is not necessary, knowing that you can do it from xml
 
Last edited:
scripts\eventcallbacks\monster\default_onDropLoot.lua

maybe . i have a doubt with the getname

Lua:
local mType = self:getType()
local yourchance = XXX
...

if mType:getName() == "ralke"  then


     for i = 1, #monsterLoot do
            if monsterLoot[i].chance then
                monsterLoot[i].chance =    math.min(monsterLoot[i].chance * yourchance, MAX_LOOTCHANCE)
            end
....

improve it with
local array = {names}
isinarray and :lower
Don't do this in onDropLoot!

You should do this on startup, and loop through each monster via a table that has their name, and a multiplier for each. You can then just set their loot item chances once. (may need some slight c++ edits depending on whether setChance can be used on each LootBlock, which should be ok)
 
Last edited:
Lua:
function onCreatureKill(creature, killer)
    local creaturesWith1xLoot = {
        "CreatureName1", -- Define the creatures for which you want to set the loot rate to 1x
        "CreatureName2",
    }

    local creatureName = creature:getName()
    for _, name in ipairs(creaturesWith1xLoot) do
        if creatureName == name then
            creature:setLootRate(1) -- Set the loot rate to 1x for the specified creature
            return
        end
    end

    creature:setLootRate(2) -- For all other creatures, use the default loot rate (2x), 2x is example, set this to the loot stage from config.lua
end
 
@Fjorda @Error 502 thanks for the answers ^^
So... which way I start testing it? Since it has to be on startup I guess @Error 502 solution won't work. And loading by .xml on python is something I don't even know where to start testing or creating. Thanks in advance!
I can manipulate your monster XML files if you wish to do it that way, just send me a PM.
 

I have something similar but wearing the items
and replace a new creature on old spawn. rat.xml loot normal call a new tag = "ratSpecial" chance spawn custom rat.
 
Lua:
local lootRate = {

["rat"] = 1,
["dragon"] = 1,
["rotworm"] = 2, -- Choose your rate
}

local monsterName = mType:getName():lower()
local customRate = 1
if lootRate[monsterName] then
customRate = lootRate[monsterName]
end

if not player or player:getStamina() > 840 then
        local monsterLoot = mType:getLoot()
 for i = 1, #monsterLoot do
            if monsterLoot[i].chance then
                monsterLoot[i].chance =    math.min(monsterLoot[i].chance * customRate, MAX_LOOTCHANCE)
            end
        end
 
local lootRate = { ["rat"] = 1, ["dragon"] = 1, ["rotworm"] = 2, -- Choose your rate } local monsterName = mType:getName():lower() local customRate = 1 if lootRate[monsterName] then customRate = lootRate[monsterName] end if not player or player:getStamina() > 840 then local monsterLoot = mType:getLoot() for i = 1, #monsterLoot do if monsterLoot.chance then monsterLoot.chance = math.min(monsterLoot.chance * customRate, MAX_LOOTCHANCE) end end
I'm a bit lost, I tried to merge on default_onDropLoot.lua but couldn't do it properly. Here's how I have it
Lua:
local ec = EventCallback

ec.onDropLoot = function(self, corpse)
    if configManager.getNumber(configKeys.RATE_LOOT) == 0 then
        return
    end

    local player = Player(corpse:getCorpseOwner())
    local mType = self:getType()
    if not player or player:getStamina() > 840 then
        local monsterLoot = mType:getLoot()
        for i = 1, #monsterLoot do
            local item = corpse:createLootItem(monsterLoot[i], self:getName())
            if not item then
                print('[Warning] DropLoot:', 'Could not add loot item to corpse.')
            end
        end

        if player then
            local gold = corpse:removeCoins()
            local text = ("Botín de %s: %s"):format(mType:getNameDescription(), corpse:getContentDescription())

            if (gold > 0) then
                player:setBankBalance(player:getBankBalance() + gold)
                text = ("%s, x%d monedas de oro fueron enviadas a su saldo bancario."):format(text, gold)
            end
           
            local party = player:getParty()
            if party then
                party:broadcastPartyLoot(text)
            else
                player:sendChannelMessage("", text, TALKTYPE_CHANNEL_O, 6)
            end
        end
    else
        local text = ("Botín de %s: nada (debido a la baja resistencia)"):format(mType:getNameDescription())
        local party = player:getParty()
        if party then
            party:broadcastPartyLoot(text)
        else
            player:sendTextMessage(MESSAGE_INFO_DESCR, text)
        end
    end
end

ec:register()

If possible please guide me on where to add this piece of code. Thanks a lot!

function onCreatureKill(creature, killer) local creaturesWith1xLoot = { "CreatureName1", -- Define the creatures for which you want to set the loot rate to 1x "CreatureName2", } local creatureName = creature:getName() for _, name in ipairs(creaturesWith1xLoot) do if creatureName == name then creature:setLootRate(1) -- Set the loot rate to 1x for the specified creature return end end creature:setLootRate(2) -- For all other creatures, use the default loot rate (2x), 2x is example, set this to the loot stage from config.lua end

I will test asap! Thanks :)
 
Last edited:
If possible please guide me on where to add this piece of code. Thanks a lot!
I took your script and Klank's, combined the codes, and corrected it in less than 3 minutes. Then I tested it in the game... Is it working. Give it a try and let me know your feedback afterward.

Lua:
local ec = EventCallback

local lootRate = {
    ["rat"] = 1,
    ["dragon"] = 1,
    ["rotworm"] = 2,
}

ec.onDropLoot = function(self, corpse)
    if configManager.getNumber(configKeys.RATE_LOOT) == 0 then
        return
    end

    local player = Player(corpse:getCorpseOwner())
    local mType = self:getType()
    local monsterName = mType:getName():lower()
    local customRate = 1

    if lootRate[monsterName] then
        customRate = lootRate[monsterName]
    end

    if not player or player:getStamina() > 840 then
        local monsterLoot = mType:getLoot()
        for i = 1, #monsterLoot do
            if monsterLoot[i].chance then
                monsterLoot[i].chance = math.min(monsterLoot[i].chance * customRate, MAX_LOOTCHANCE)
            end
        end

        for i = 1, #monsterLoot do
            local item = corpse:createLootItem(monsterLoot[i], self:getName())
            if not item then
                print('[Warning] DropLoot:', 'Could not add loot item to corpse.')
            end
        end

        local gold = 0
        if corpse:isContainer() then
            local container = Container(corpse:getUniqueId())
            if container then
                for slot = 0, container:getSize() - 1 do
                    local item = container:getItem(slot)
                    if item and item:getId() == ITEM_GOLD_COIN then
                        gold = gold + item:getCount()
                        item:remove()
                    end
                end
            else
                print("[Warning] DropLoot:", "Container not found.")
            end
        end

        local text = ("Botín de %s: %s"):format(mType:getNameDescription(), corpse:getContentDescription())

        if gold > 0 then
            player:addMoney(gold)
            local balance = player:getBankBalance()
            player:setBankBalance(balance + gold)
            text = ("%s, x%d monedas de oro fueron enviadas a su saldo bancario."):format(text, gold)
        end

        local party = player:getParty()
        if party then
            party:broadcastPartyLoot(text)
        else
            player:sendChannelMessage("", text, TALKTYPE_CHANNEL_O, 6)
        end
    else
        local text = ("Botín de %s: nada (debido a la baja resistencia)"):format(mType:getNameDescription())
        local party = player:getParty()
        if party then
            party:broadcastPartyLoot(text)
        else
            player:sendTextMessage(MESSAGE_INFO_DESCR, text)
        end
    end
end

ec:register()
Post automatically merged:

The updated post now includes changes regarding banking.
 
Last edited:
customRate=1: Set a default drop rate to 1 for all monsters. config.lua loot rate: The script checks whether loot is enabled or disabled in the game using the config.lua file configuration. If it is activated, the withdrawal is processed; if it is disabled, the withdrawal is ignored.
 
Back
Top