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

Simple Auto Loot System [TFS 1.3]

Evil Puncker

prolonged absenteeism
TFS Developer
Joined
May 30, 2009
Messages
7,915
Solutions
197
Reaction score
3,733
For anybody who is interested in it, I fixed it a bit in lua part.
Now if you have no space in backpack or cap the loot will simply stay in the corpse and player will get a message.
The memory leak is taken care of too.

data/scripts/eventcallbacks/monster/default_onDropLoot.lua
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()
    local monsterLoot = mType:getLoot()
    for i = 1, #monsterLoot do
        local item = corpse:createLootItem(monsterLoot[i])
        if not item then
            print('[Warning] DropLoot:', 'Could not add loot item to corpse.')
        end
    end

    if player then
        local autolootContainer = Game.createItem(1987, 1)
        local items = corpse:getItems()
        local item_weight = 0
        local item_slots = 0
       
        for _, item in ipairs(items) do
            if (player:getAutoLootItem(item.itemid)) then
                item_weight = item_weight + item:getWeight()
                item_slots = item_slots + 1
            end
        end
       
        local bp = player:getSlotItem(CONST_SLOT_BACKPACK)
        if bp and bp:getEmptySlots(true) < item_slots then
            player:sendCancelMessage("Autoloot: No space in backpack!")
        elseif player:getFreeCapacity() < (item_weight * 100) then
            player:sendCancelMessage("Autoloot: Not enough cap!")
        else
            for _, item in ipairs(items) do
                if (player:getAutoLootItem(item.itemid)) then
                    item:moveTo(autolootContainer)
                end
            end
        end
       
        local text = ("Loot of %s: %s"):format(mType:getNameDescription(), corpse:getContentDescription())
       
        if (autolootContainer:getSize() > 0) then
            text = text .. ' and ' .. autolootContainer:getContentDescription() .. ' that was auto looted'
            local autolootItems = autolootContainer:getItems()

            for _, item in ipairs(autolootItems) do
                item:moveTo(player)
            end
        end
       
        autolootContainer:remove()

        text = text .. '.'
           
        local party = player:getParty()
        if party then
            party:broadcastPartyLoot(text)
        else
            player:sendTextMessage(MESSAGE_INFO_DESCR, text)
        end
    end
end

ec:register()
aren't you checking for free slots in the player equipped backpack instead of the autolootContainer one?
 

dami1310

◄ Unidentified ►
Joined
Jan 27, 2013
Messages
626
Solutions
8
Reaction score
530
Location
Poland
aren't you checking for free slots in the player equipped backpack instead of the autolootContainer one?
Of course I am checking player backpack, because I want to know if he has the slots/cap to carry the items.
This was the reason why the items "disappeared". Script wasn't checking it at all, and was always moving the items to autolootContainer when the mob died.
In case player had not cap/slots it couldn't move the items to inventory, so it was just stuck in autolootContainer and the loot was gone.

Btw. (item_weight * 100) should be changed to just item_weight, I can't edit the post and don't know why it was even like that.
 
Top