• 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!
  • New resources must be posted under Resources tab. A discussion thread will be created automatically, you can't open threads manually anymore.

Action onUse get a random item(chance configuration)

averatec

Advanced OT User
Joined
Jun 1, 2007
Messages
2,243
Solutions
4
Reaction score
159
Location
Poland
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:
Where put this code? '0'

in data/actions/action.xml:
HTML:
<action itemid="3954" event="script" value="random item.lua"/>
then create file in data/actions/scripts/random item.lua and put this inside of it:
HTML:
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
 
Back
Top