Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.88 KB | None | 0 0
  1. local function newNode (posX, posY, distance, priority)
  2.     node = {}
  3.     node.posX = posX
  4.     node.posY = posY
  5.     node.distance = distance
  6.     node.priority = priority
  7.     -- Estimation function for the remaining distance to the goal.
  8.     function node:estimate(destX, destY)
  9.         local dx = destX - self.posX
  10.         local dy = destY - self.posY
  11.         -- Manhattan distance
  12.         -- abs(dx) + abs(dy)
  13.         --Euclidian Distance
  14.         return math.sqrt(dx * dx + dy * dy)
  15.     end
  16.     function node:updatePriority(destX, destY)
  17.         self.priority = self.distance + self:estimate(destX, destY) * 10
  18.     end
  19.     function node:nextMove()
  20.         self.distance = self.distance + 10
  21.     end
  22.     mt = { __lt =   function (a, b)
  23.                         return { value = a.priority < b.priority }
  24.                     end }
  25.     setmetatable(node, mt)
  26.  
  27.     return node
  28. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement