Hello, I have a problem with a function I wrote.
It is supposed to work so that from position to position it creates items, but only in places where it is possible.
Example usage: doCreateItemArea(leftTopCorner, rightBottomCorner, 12)
12 - number of items to create
I want it to check if the position is free, when it is it creates an item and when not it draws another place.
Can someone help
It is supposed to work so that from position to position it creates items, but only in places where it is possible.
Lua:
function Position:isWalkable()
local tile = Tile(self)
if not tile then
return false
end
local ground = tile:getGround()
if not ground or ground:hasProperty(CONST_PROP_BLOCKSOLID) then
return false
end
local items = tile:getItems()
for i = 1, tile:getItemCount() do
local item = items[i]
local itemType = item:getType()
if itemType:getType() ~= ITEM_TYPE_MAGICFIELD and not itemType:isMovable() and item:hasProperty(CONST_PROP_BLOCKSOLID) then
return false
end
end
return true
end
local function doCreateItemArea(leftTopCorner, rightBottomCorner, itemsCount)
if leftTopCorner.x > rightBottomCorner.x then
return false
end
for i = 1, itemsCount do
local posx = math.random(leftTopCorner.x, rightBottomCorner.x)
local posy = math.random(leftTopCorner.y, rightBottomCorner.y)
local poss = {x = posx, y = posy, z = 7}
print(Position.isWalkable(poss))
if Position.isWalkable(poss) == FALSE then
addEvent(doCreateItemArea, 1, poss.x, poss.y, z = 15, 1)
else
Game.createItem(3698, 1, {x = posx, y = posy, z = 15})
end
end
return true
end
Example usage: doCreateItemArea(leftTopCorner, rightBottomCorner, 12)
12 - number of items to create
I want it to check if the position is free, when it is it creates an item and when not it draws another place.
Can someone help