• 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+ Scancontainer script, cant find bag?

Mjmackan

Mapper ~ Writer
Premium User
Joined
Jul 18, 2009
Messages
1,424
Solutions
15
Reaction score
177
Location
Sweden
Using printer's version of this script i tried to make it search for the bag, seems like i cant even print out that bag id for some reason. Anyone mind taking a look?

What i want: Make the script look through the corpse, any bags inside, send money to the bank and the rest should get to the backpack, depending on your autoloot choices.
What it does right now: Sends money to the bank and loots but just from the corpse.
What i recently tried: Printing out all itemids from the corpse, seeing that the bag id in the corpse didnt show at all.
What distro im using: TFS 1.3

Lua:
local function scanContainer(cid, position)
    local player = Player(cid)
    if not player then
        return
    end
    local corpse = Tile(position):getTopDownItem()
    if not corpse or not corpse:isContainer() then
        return
    end
    if corpse:getType():isCorpse() and corpse:getAttribute(ITEM_ATTRIBUTE_CORPSEOWNER) == cid then
        for a = corpse:getSize() - 1, 0, -1 do
            local containerItem = corpse:getItem(a)
            if containerItem then
                for b = AUTOLOOT_STORAGE_START, AUTOLOOT_STORAGE_END do
                    if player:getStorageValue(b) == containerItem:getId() then
                    containerItem:moveTo(player)
                    print(containerItem:getId())
        if containerItem:isContainer() then
            print("triue")
            for i = containerItem:getSize() - 1, 0, -1 do
                local containerBagItem = containerItem:getItem(i)
                if containerBagItem then
                    containerBagItem:moveTo(player)
                end
            end
        end
                    end               
                end
                if isItemStackable(containerItem:getId()) then
                    if containerItem:getId() == 2148 then
                        containerItem:remove()
                        doPlayerSetBalance(player, getPlayerBalance(player) + containerItem:getCount())
                    end
                    if containerItem:getId() == 2152 then
                        containerItem:remove()
                        doPlayerSetBalance(player, getPlayerBalance(player) + (containerItem:getCount() * 100))
                    end
                    if containerItem:getId() == 2160 then
                        containerItem:remove()
                        doPlayerSetBalance(player, getPlayerBalance(player) + (containerItem:getCount() * 10000))
                    end
                end
            end
        end
    end
end
 
Solution
Here is a better version of it:
Lua:
local coins = {
    [2148] = 1,
    [2152] = 100,
    [2160] = 10000
}

local function scanContainer(cid, position)
    local player = Player(cid)
    if not player then
        return
    end

    local tile = Tile(position)
    local corpse = tile and tile:getTopDownItem()
    if not corpse or not corpse:isContainer() then
        return
    end

    if corpse:getType():isCorpse() and corpse:getAttribute(ITEM_ATTRIBUTE_CORPSEOWNER) ~~= cid then
        return
    end

    for _, item in pairs(corpse:getItems(true)) do
        local coin = coins[item.itemid]
        if coin then
            player:setBankBalance(player:getBankBalance() + (item:getCount() * coin))
            item:remove()
        else...
why not just remove bag from all monsters? a simple regEx in notepad++ would remove everything from all monsters files at once, and bags are not used in monster loot since 8.54 version

or even simple search and replace:

search for (separately):
<item id="1987" chance="100000">
<inside>
</inside>
</item>

and replace each one with blank (leave the replace field empty)
 
Last edited by a moderator:
why not just remove bag from all monsters? a simple regEx in notepad++ would remove everything from all monsters files at once, and bags are not used in monster loot since 8.54 version

or even simple search and replace:

search for (separately):
<item id="1987" chance="100000">
<inside>
</inside>
</item>

and replace each one with blank (leave the replace field empty)
Sounds too easy, I don't like it..

Actually this was my plan for some time, then i got interested in how to make this functional.
 
Sounds too easy, I don't like it..

Actually this was my plan for some time, then i got interested in how to make this functional.
One of the rare times in life when something seems too easy to be true, but I can confirm. I had to recently convert a monster pack exactly in this way.

regex, that shit's magic.
 
Here is a better version of it:
Lua:
local coins = {
    [2148] = 1,
    [2152] = 100,
    [2160] = 10000
}

local function scanContainer(cid, position)
    local player = Player(cid)
    if not player then
        return
    end

    local tile = Tile(position)
    local corpse = tile and tile:getTopDownItem()
    if not corpse or not corpse:isContainer() then
        return
    end

    if corpse:getType():isCorpse() and corpse:getAttribute(ITEM_ATTRIBUTE_CORPSEOWNER) ~~= cid then
        return
    end

    for _, item in pairs(corpse:getItems(true)) do
        local coin = coins[item.itemid]
        if coin then
            player:setBankBalance(player:getBankBalance() + (item:getCount() * coin))
            item:remove()
        else
            for value = AUTOLOOT_STORAGE_START, AUTOLOOT_STORAGE_END do
                if player:getStorageValue(value) == item.itemid then
                    item:moveTo(player)
                end
            end
        end
    end
end
 
Solution
It was sometime since I touched lua, but are you not removing the item before you do the count and add it to balance?

containerItem:remove()
doPlayerSetBalance(player, getPlayerBalance(player) + containerItem:getCount())

So the getCount() would be null or 0?
 
It was sometime since I touched lua, but are you not removing the item before you do the count and add it to balance?

containerItem:remove()
doPlayerSetBalance(player, getPlayerBalance(player) + containerItem:getCount())

So the getCount() would be null or 0?
Item 'remove' is special function. It does not remove item from RAM, it marks item as 'removed' and adds item to C++ list 'remove on next cleanup' ('cleanup' executes one time per second as I remember). So item will be removed some time after LUA script execution.

It works like that now, but in future, it may remove item instantly. Then 'getCount' on not existing item will result in SERVER CRASH. You should never call functions on removed objects.
 
Item 'remove' is special function. It does not remove item from RAM, it marks item as 'removed' and adds item to C++ list 'remove on next cleanup' ('cleanup' executes one time per second as I remember). So item will be removed some time after LUA script execution.

It works like that now, but in future, it may remove item instantly. Then 'getCount' on not existing item will result in SERVER CRASH. You should never call functions on removed objects.
Ah, I see!
 
Back
Top