Advertisement
Skymagnum

Untitled

Jun 17th, 2013
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.51 KB | None | 0 0
  1. local function getNextDir(cid, playerpos, finalpos)
  2.  
  3. local result = {}
  4. local xy = 999999
  5. local allowedCreatures = true -- possível puxar creaturas
  6. local ghost = false -- possível atravessar obstaculos
  7.  
  8.     for newDir = 0, 3 do
  9.         local p = getPosByDir(playerpos, newDir)
  10.         if doTileQueryAdd(cid, p, allowedCreatures and 4 or 0) == 1 or ghost then
  11.             local d = math.abs(p.x - finalpos.x) + math.abs(p.y - finalpos.y)
  12.             if d < xy then
  13.                 result = {newDir}
  14.                 xy = d
  15.             elseif d == xy then
  16.                 table.insert(result, newDir)
  17.             end
  18.         end
  19.     end
  20.    
  21.     if #result == 0 then
  22.         for newDir = 4, 7 do
  23.             local p = getPosByDir(playerpos, newDir)
  24.             if doTileQueryAdd(cid, p, allowedCreatures and 4 or 0) == 1 or ghost then
  25.                 local d = math.abs(p.x - finalpos.x) + math.abs(p.y - finalpos.y)
  26.                 if d < xy then
  27.                     result = {newDir}
  28.                     xy = d
  29.                 elseif d == xy then
  30.                     table.insert(result, newDir)
  31.                 end
  32.             end
  33.         end
  34.         return #result == 0 and false or result[math.random(#result)]
  35.     else
  36.         return result[math.random(#result)]
  37.     end
  38. return false
  39. end
  40.  
  41. -- para usar \/
  42. local finalPos = {x = x, y = y, z = z}
  43. local nextPos = {}
  44. nextPos = getPosByDir(getThingPos(cid), getNextDir(cid, getThingPos(cid), finalPos) or getDirectionTo(getThingPos(cid), finalPos))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement