darcioantonio
www.adventurerpg.com.br
- Joined
- Jul 30, 2013
- Messages
- 165
- Solutions
- 1
- Reaction score
- 5
- Location
- Brasil
- Twitch
- darcio_
- YouTube
- UCEXCOEw_dYchojHNz
I've created this script to generate random items on the map, but it's currently placing them inside walls, on top of rivers, etc. Is there a possibility to limit the placement to areas where the player can walk?"
OTX 0.4 (OTX Server Version: (2.15))
OTX 0.4 (OTX Server Version: (2.15))
LUA:
local config = {
topoEsquerdo = {x = 93, y = 1035, z = 7},
sulDireito = {x = 278, y = 1281, z = 7},
itensCreate = {13785, 13786},
itensQuant = 3000,
}
function isWalkable(cid, pos)
local closest = getClosestFreeTile(cid, pos)
return type(closest) == "table" and doComparePositions(closest, pos)
end
function isWithinArea(pos)
return pos.x >= config.topoEsquerdo.x and pos.x <= config.sulDireito.x
and pos.y >= config.topoEsquerdo.y and pos.y <= config.sulDireito.y
and pos.z == config.topoEsquerdo.z
end
function getItemCountFromPosition(item, pos)
local item_pos = {x = pos.x, y = pos.y, z = pos.z}
local item_count = 0
for i = 1, 255 do
local check_pos = {x = item_pos.x, y = item_pos.y, z = item_pos.z, stackpos = i}
if getThingFromPos(check_pos).itemid <= 0 then
break
elseif getThingFromPos(check_pos).itemid == item then
if isItemStackable(getThingFromPos(check_pos).itemid) == true then
item_count = item_count + getThingFromPos(check_pos).type
else
item_count = item_count + 1
end
end
end
return item_count
end
function onUse(cid, item, frompos, item2, topos)
local createdItems = 0 -- Inicializar contador de itens criados
while createdItems < config.itensQuant do
local randomX = math.random(config.topoEsquerdo.x, config.sulDireito.x)
local randomY = math.random(config.topoEsquerdo.y, config.sulDireito.y)
local randomZ = config.topoEsquerdo.z -- Você pode ajustar isso se quiser um intervalo diferente de Z
local randomPos = {x = randomX, y = randomY, z = randomZ}
if isWithinArea(randomPos) and isWalkable(cid, randomPos) then
local itemToCreate = config.itensCreate[math.random(1, #config.itensCreate)]
if getItemCountFromPosition(itemToCreate, randomPos) == 0 then
doCreateItem(itemToCreate, 1, randomPos)
createdItems = createdItems + 1 -- Incrementar contador de itens criados
end
end
end
end