• 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 function doCreateItemArea

Kahras

Member
Joined
Aug 6, 2012
Messages
101
Reaction score
7
Location
Warsaw
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.

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


jrkBRch.png

Example usage: doCreateItemArea(leftTopCorner, rightBottomCorner, 12)
12 - number of items to create


1uPx7CI.png


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 :D
 
i saw @oen432 great idea for doing it in his cursed chests code so credits goes to him i just copied and reflected the technique on your code

PS. your function must include position Z in the parameters because you won't always want floor 7
Lua:
function isBadTile(tile)
    return (tile == nil or tile:getGround() == nil or tile:hasProperty(TILESTATE_NONE) or tile:hasProperty(TILESTATE_FLOORCHANGE_EAST) or isItem(tile:getThing()) and not isMoveable(tile:getThing()) or tile:getTopCreature() or tile:hasFlag(TILESTATE_PROTECTIONZONE))
end

local function doCreateItemArea(itemId, stackCount, leftTopCorner, rightBottomCorner, floor, itemsCount)
--floor is the new parameter for the Z position & itemId for the itemId you want to create & stackCount for the amount of the item that will be created/tile
    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=floor}
                    local tile = Tile(poss)
                    local spawnTest = 0
                    while spawnTest < 50 do
                        if isBadTile(tile) then
                            poss = {x = math.random(leftTopCorner.z, rightBottomCorner.z), y = math.random(leftTopCorner.y, rightBottomCorner.y), z = floor}
                            tile = Tile(poss)
                            spawnTest = spawnTest + 1
                        else
                            break
                        end
                    end
                    Game.createItem(itemId, stackCount, poss)
                    end
            end
return true
end
that should do what you need
 
i saw @oen432 great idea for doing it in his cursed chests code so credits goes to him i just copied and reflected the technique on your code

PS. your function must include position Z in the parameters because you won't always want floor 7
Lua:
function isBadTile(tile)
    return (tile == nil or tile:getGround() == nil or tile:hasProperty(TILESTATE_NONE) or tile:hasProperty(TILESTATE_FLOORCHANGE_EAST) or isItem(tile:getThing()) and not isMoveable(tile:getThing()) or tile:getTopCreature() or tile:hasFlag(TILESTATE_PROTECTIONZONE))
end

local function doCreateItemArea(itemId, stackCount, leftTopCorner, rightBottomCorner, floor, itemsCount)
--floor is the new parameter for the Z position & itemId for the itemId you want to create & stackCount for the amount of the item that will be created/tile
    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=floor}
                    local tile = Tile(poss)
                    local spawnTest = 0
                    while spawnTest < 50 do
                        if isBadTile(tile) then
                            poss = {x = math.random(leftTopCorner.z, rightBottomCorner.z), y = math.random(leftTopCorner.y, rightBottomCorner.y), z = floor}
                            tile = Tile(poss)
                            spawnTest = spawnTest + 1
                        else
                            break
                        end
                    end
                    Game.createItem(itemId, stackCount, poss)
                    end
            end
return true
end
that should do what you need
One more thing to add, check if spawnTest < 50 before Game.createItem, otherwise it might scan 50 tiles that are "bad" and it will end up spawning item on a last, still bad, position. Or just increase number here while spawnTest < 50 do to lower probability of it failing.
 
It works very well and do you know how to make it create items at the top?
Im add to the script:
stackpos
Lua:
poss = {x = math.random(leftTopCorner.x, rightBottomCorner.x), y = math.random(leftTopCorner.y, rightBottomCorner.y), z = leftTopCorner.z, stackpos = 1}
I tried stackpos 1, 2, 0, 255 and on and on the same

ex6ewiP.png
 
Last edited:
Back
Top