Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function getNextDir(cid, playerpos, finalpos)
- local result = {}
- local xy = 999999
- local allowedCreatures = true -- possível puxar creaturas
- local ghost = false -- possível atravessar obstaculos
- for newDir = 0, 3 do
- local p = getPosByDir(playerpos, newDir)
- if doTileQueryAdd(cid, p, allowedCreatures and 4 or 0) == 1 or ghost then
- local d = math.abs(p.x - finalpos.x) + math.abs(p.y - finalpos.y)
- if d < xy then
- result = {newDir}
- xy = d
- elseif d == xy then
- table.insert(result, newDir)
- end
- end
- end
- if #result == 0 then
- for newDir = 4, 7 do
- local p = getPosByDir(playerpos, newDir)
- if doTileQueryAdd(cid, p, allowedCreatures and 4 or 0) == 1 or ghost then
- local d = math.abs(p.x - finalpos.x) + math.abs(p.y - finalpos.y)
- if d < xy then
- result = {newDir}
- xy = d
- elseif d == xy then
- table.insert(result, newDir)
- end
- end
- end
- return #result == 0 and false or result[math.random(#result)]
- else
- return result[math.random(#result)]
- end
- return false
- end
- -- para usar \/
- local finalPos = {x = x, y = y, z = z}
- local nextPos = {}
- nextPos = getPosByDir(getThingPos(cid), getNextDir(cid, getThingPos(cid), finalPos) or getDirectionTo(getThingPos(cid), finalPos))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement