-- needs to be somewhere where it only gets loaded once (lib file, OUTSIDE of an interface function such as onUse/onSay/onKill/etc)
TP_validPositions = {}
local fromPos = Position(1000, 1000, 7)
local toPos = Position(1050, 1050, 7)
local tempPos = Position(0, 0, fromPos.z)
for x = fromPos.x, fromPos.x do
for y = toPos.y, toPos.y do
tempPos.x = x
tempPos.y = y
local tile = Tile(tempPos)
if tile and not tile:hasFlag(TILESTATE_PROTECTIONZONE) then
TP_validPositions[#TP_validPositions+1] = Position(tempPos.x, tempPos.y, tempPos.z) -- copying to avoid issues with table references
end
end
end
-- example
function onUse(player)
local randomValidPos = TP_validPositions[math.random(#TP_validPositions)]
player:teleportTo(randomValidPos)
return true
end