averatec
Advanced OT User
Lua:
local config = {
{ rate = 1, item = 2160, count = { min = 1, max = 2 }},
{ rate = 2, item = 2152, count = { min = 1, max = 100 }},
{ rate = 2, item = 2400, count = { const = 1 }}
}
local sumOfRate = 0
for i,v in ipairs(config) do
sumOfRate = sumOfRate + v.rate
end
function onUse(cid, item, fromPosition, itemEx, toPosition)
local rand = math.random(1, sumOfRate)
local subSum = 0
local exactItem = nil
for i,v in ipairs(config) do
if (subSum > rand) then
if (i > 1) then
exactItem = i-1
end
break
end
subSum = subSum + v.rate
if (subSum >= rand) then
exactItem = i
break
end
end
if (config[exactItem].count.const ~= nil) then
doPlayerAddItem(cid, config[exactItem].item, config[exactItem].count.const)
else
doPlayerAddItem(cid, config[exactItem].item, math.random(config[exactItem].count.min, config[exactItem].count.max))
end
doRemoveItem(item.uid, 1)
end
Rate is a chance, sum of all rates is 5, then chance to get second item is 2/5=40%
@edit
typo in title, should be "... get an r..."
Last edited: