```
local function teleportPos(ppos, tpos)
local pdiff = {
x = ppos.x - tpos.x,
y = ppos.y - tpos.y
}
if pdiff.x > 0 then
tpos.x = tpos.x + 1
elseif pdiff.x < 0 then
tpos.x = tpos.x - 1
elseif pdiff.y > 0 then
tpos.y = tpos.y + 1
elseif pdiff.y < 0 then
tpos.y = tpos.y - 1
end
return tpos
end
local function checkPos(tgtpos, telpos)
local tile = Tile(telpos)
local rtile = {
{-1, -1}, {0, -1}, {1, -1},
{-1, 0}, {1, 0},
{-1, 1}, {0, 1}, {1, 1}
}
local finalpos = tgtpos
local i = 1
math.randomseed(os.time())
while i < 9 do
if not tile:getGround() or tile:getCreatureCount() > 0 or tile:hasFlag(bit.bor(TILESTATE_IMMOVABLEBLOCKSOLID, TILESTATE_IMMOVABLEBLOCKPATH, TILESTATE_FLOORCHANGE, TILESTATE_PROTECTIONZONE, TILESTATE_TELEPORT)) then
local j = math.random(i, 8)
finalpos = tgtpos
finalpos.x = tgtpos.x + rtile[j][1]
finalpos.y = tgtpos.y + rtile[j][2]
rtile[j], rtile[i] = rtile[i], rtile[j]
tile = Tile(finalpos)
i = i + 1
else
return finalpos
end
end
return false
end
function onCastSpell(creature, variant)
local tgtpos = creature:getTarget():getPosition()
local telpos = teleportPos(creature:getPosition(), tgtpos)
local checkpos = checkPos(tgtpos, telpos)
if creature:getTarget() ~= isCreature then
creature:getPosition():sendMagicEffect(CONST_ME_POFF)
creature:sendCancelMessage("You can only teleport to other players")
return false
elseif not checkpos then
creature:getPosition():sendMagicEffect(CONST_ME_POFF)
creature:sendCancelMessage("Not enough room")
return false
else
creature:teleportTo(checkpos)
creature:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
end
return false
end
```