Advertisement
Cavitt

Untitled

Jun 20th, 2013
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.27 KB | None | 0 0
  1. function getAdjacentTiles()
  2.     local pos = Self.Position()
  3.     local tiles = {
  4.         {x=pos.x,y=pos.y-1},
  5.         {x=pos.x+1,y=pos.y},
  6.         {x=pos.x,y=pos.y+1},
  7.         {x=pos.x-1,y=pos.y},
  8.         {x=pos.x-1,y=pos.y+1},
  9.         {x=pos.x+1,y=pos.y+1},
  10.         {x=pos.x-1,y=pos.y-1},
  11.         {x=pos.x+1,y=pos.y-1}
  12.     }
  13.     return tiles
  14. end
  15.  
  16. function comparePositions(pos1, pos2)
  17.     return pos1.x == pos2.x and pos1.y == pos2.y
  18. end
  19.  
  20. function getDirectionTo(pos1, pos2)
  21.     local dir = SOUTH
  22.     if(pos1.x > pos2.x) then
  23.         dir = WEST
  24.         if(pos1.y > pos2.y) then
  25.             dir = NORTHWEST
  26.         elseif(pos1.y < pos2.y) then
  27.             dir = SOUTHWEST
  28.         end
  29.     elseif(pos1.x < pos2.x) then
  30.         dir = EAST
  31.         if(pos1.y > pos2.y) then
  32.             dir = NORTHEAST
  33.         elseif(pos1.y < pos2.y) then
  34.             dir = SOUTHEAST
  35.         end
  36.     elseif(pos1.y > pos2.y) then
  37.         dir = NORTH
  38.     elseif(pos1.y < pos2.y) then
  39.         dir = SOUTH
  40.     end
  41.     return dir
  42. end
  43.  
  44. function getTileScore(tilePos, toPos, history)
  45.     local g = (getDirectionTo(Self.Position(), tilePos) > 3) and 15 or 10
  46.     local f = getDistanceBetween(tilePos, toPos)
  47.     local o = 0
  48.     local key = table.serialize(tilePos)
  49.     if(history[key])then
  50.         o = history[key] * 150
  51.     end
  52.     return g + f + o
  53. end
  54.  
  55. function getLowestNode(dest, history)
  56.     local tiles = getAdjacentTiles()
  57.     local target = 500
  58.     local chosen = nil
  59.     for i = 1, #tiles do
  60.         local pos = tiles[i]
  61.         local walkable = Map.IsTileWalkable(pos.x, pos.y, Self.Position().z)
  62.         print(tostring(walkable))
  63.         if(walkable)then
  64.             local score = getTileScore(pos, dest, history)
  65.             if(score < target)then
  66.                 target = score
  67.                 chosen = pos
  68.             end
  69.         end
  70.     end
  71.     return chosen
  72. end
  73.  
  74. function walk(dest, nodes)
  75.     local history = {}
  76.     wait(1500)
  77.     local nodes = nodes or 1000
  78.     while(true)do
  79.         nodes = nodes - 1
  80.         if(nodes < 0)then
  81.             break
  82.         end
  83.         local pos = Self.Position()
  84.         if(getDistanceBetween(pos, dest) <= 1)then
  85.             break
  86.         end
  87.         local nextPos = getLowestNode(dest, history)
  88.         if(not nextPos)then
  89.             break
  90.         end
  91.         local key = table.serialize(nextPos)
  92.         if(history[key])then
  93.             history[key] = history[key] + 1
  94.         else
  95.             history[key] = 1
  96.         end
  97.         Self.Step(getDirectionTo(pos, nextPos))
  98.         waitLimit = 600
  99.         repeat
  100.             wait(200)
  101.             waitLimit = waitLimit - 1
  102.         until (comparePositions(Self.Position(), nextPos)) or waitLimit < 0
  103.     end
  104. end
  105.  
  106. walk({x=32108,y=32208,z=7})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement