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

Lua [1.x] Lootbag seller

elnelson

Lunaria World Dev
Joined
Jun 20, 2009
Messages
580
Solutions
2
Reaction score
58
Location
México
Good afternoon, im trying to upgrade this script to make it work on TFS 1.X.

The objective of this script is to sell lootbags inside a box after pulling a lever.

I will appreciate a lot your help :)

Lua:
local items = { -- you need to set up all items here where :
                    [2466] = {cost = 100},
                    [2446] = {cost = 3000},
                    [2470] = {cost = 4000},
                    }


local box = {x=999,y=1001,z=7,stackpos = 1} -- box pos



-- // SCRIPT START \\ --

function getContentDescription(uid, li) -- credits to Cyko for main form of this function
    local ret, i, containers,removes, left = {}, 0, {}, {}, {}

    while i <= getContainerSize(uid) do
        local v = getContainerItem(uid, i)
        local k = v.uid
        local k2 = v.itemid

        local check = items[k2]
        if check then
            table.insert(ret, k2)
            table.insert(removes,k)
        end
        if isContainer(k) then
                table.insert(containers, k)
        end
        i = i + 1
    end
    for i = 1, #containers do
        local bah = getContentDescription(containers[i], li)
        for i = 1,#bah do
            if li == 1 then
                table.insert(removes,bah[i])
            elseif li == 2 then 
                table.insert(ret,bah[i])
            end
        end
    end
    return li== 1 and removes or ret
end

function getKey(t)
    local s = {}
    for k,v in pairs(t) do
        table.insert(s,k)
    end
    return s
end
function onUse(cid, item, fromPosition, itemEx, toPosition)
local exist = getThingFromPos(box).uid
local itm = getThingFromPos(box).itemid

    if isContainer(exist) then
        local t = getContentDescription(exist,2)
        local t2 = getContentDescription(exist,1)
        if #t > 0 then
            local f = {}
            for i = 1,#t do
                if not isInArray(getKey(f),t[i]) then
                    f[t[i]] = 1
                else
                    f[t[i]] = f[t[i]] + 1
                end
            end
            local str = "Sold items : "
            local money = 0
            for k,v in pairs(f) do
                str = str.."\n".."•••• "..v.."x "..getItemNameById(k).." : ".. ( tonumber(items[k].cost) * tonumber(v) ) .. " gold coins."
                money = money + tonumber(items[k].cost) * tonumber(v)

            end
            for i = 1,#t2 do
                doRemoveItem(t2[i])
            end
            doPlayerSendTextMessage(cid,27,str)
            doPlayerAddMoney(cid,money)
            if #t > 1 then
                doPlayerSendTextMessage(cid,19," Total money : ".. money.." gold coins.")
            end
        else
            doPlayerSendTextMessage(cid,18,"Warning : Sold nothing --> either bag is empty or items included arn't sellable here.")
            doSendMagicEffect(fromPosition,2)

        end
    else
        local merge = items[itm]
        if not merge then
            doPlayerSendTextMessage(cid,18,"Warning : This item isn't sellable here.")
            doSendMagicEffect(fromPosition,2)

        else
            doRemoveItem(exist)
            doPlayerSendTextMessage(cid,27,"Sold item : \n •••• 1x "..getItemNameById(itm).." : "..merge.cost.."." )
            doPlayerAddMoney(cid,merge.cost)
        end

    end
    return doTransformItem(item.uid,item.uid == 1945 and 1946 or 1945)
end
 
i was able to find this function, it suppose to scan items inside containers.

Lua:
function Container.getItems(self)
    local items = {}

    for index = 0, self:getSize() - 1 do
        local item = self:getItem(index)

        if (item) then
            if (item:isContainer()) then
                local containerItems = item:getItems()

                for _, item in ipairs(containerItems) do
                    table.insert(items, item)
                end
            else
                table.insert(items, item)
            end
        end
    end

    return items
end

i guess the next step is to compare a table like:

Code:
lootbag_items = {
--[item.ID] = price
    [3377] = 80, -- scale armor (80 gps)
    [3357] = 400,  -- plate armor (400 gps)
}

the thing i dont know (im newbie at 1.x yet) is how to make a script following these algorithm.

For example

1-. i have a lootbag with 3+ subcontainers: total of 10 scale armors and 9 plate armors,
2-. i take my lootbag inside a crate or box
3-. i pull a lever next to the crate or box
4-. all items are solt (removed) and the money goes directly to character (10x80 = 800 gp + 9x400 = 3600 gp, giving a total of 4400 gp to the player)
 
i made it..!

i will share with you because i love you :*

PD: transition from 0.4 is very rough i hope i can improve my skills in tfs 1.x ;D

Instructions:

1-. Add inside /data/events/scripts/player.lua:

Lua:
local sellables = {
    --[itemid] = price,
    [3412] = 50,
}

local SOME_ACTIONID = 6543

local function sellItems(player, item)
    local itemCount, totalValue = 0, 0
    if item:isContainer() then
        for _, it in ipairs(item:getItems()) do
            local count, value = sellItems(player, it)
            itemCount = itemCount + count
            totalValue = totalValue + value
        end
        return itemCount, totalValue
    else
        local value = sellables[item:getId()]
        if value then
            local count = item:getCount()
            item:remove()
            return count, count * value
        end
        return 0, 0
    end
end


function Player:onItemMoved(item, count, fromPosition, toPosition, fromCylinder, toCylinder)
    local ground = toCylinder:isTile() and toCylinder:getGround()
    if ground and ground:getActionId() == SOME_ACTIONID then
        local count, value = sellItems(player, item)
        toPosition:sendMagicEffect(57)
        self:sendMagicEffect(3)
        self:addMoney(value)
        self:sendTextMessage(MESSAGE_INFO_DESCR, string.format("Sold %s item(s) for %s gold.", count, value))
    end
    return true
end

2-. Put AID = 6543 on a floor tile
3-. drop lootbag or item you want to sell to AID tile
4-. enjoy!

Tested in TFS 1.2
 
Back
Top