PathFindAStar.lua 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. local tremove = table.remove
  2. local tinsert = table.insert
  3. local INFINITY_COST = 0xFFFFFF
  4. local BIAS_VALUE = 14
  5. local LINE_VALUE = 10
  6. local PathNode = class("PathNode")
  7. function PathNode:ctor()
  8. self.x = 0
  9. self.y = 0
  10. self.t = "new"
  11. self.h = 0
  12. self.k = 0
  13. self.isObstacle = 0
  14. self.backpointer = nil
  15. end
  16. function PathNode:cost(node)
  17. if self.isObstacle == 1 or node.isObstacle == 1 then
  18. return INFINITY_COST
  19. end
  20. local line_value = ((self.x == node.x or self.y == node.y) and 10 or 14)
  21. return ((self.x-node.x)*(self.x-node.x) + (self.y-node.y)*(self.y-node.y)) + line_value
  22. end
  23. --
  24. local PathFindAStar = class('PathFindAStar')
  25. function PathFindAStar:ctor()
  26. self.mapNodes = {}
  27. self.openlist = {}
  28. self.mapData = nil
  29. end
  30. function PathFindAStar:cleanup()
  31. self.startPathNode = nil
  32. self.endPathNode = nil
  33. self.mapNodes = {}
  34. self.openlist = {}
  35. end
  36. function PathFindAStar:updateLimits(begX, begY, endX, endY)
  37. local mapData = global.sceneManager:GetMapData2DPtr()
  38. self.mapRows = mapData:getMapDataRows()
  39. self.mapCols = mapData:getMapDataCols()
  40. local disX = 24
  41. local disY = 16
  42. self.minX = math.min(math.max(begX - disX, 0), self.mapCols-1)
  43. self.maxX = math.min(math.max(begX + disX, 0), self.mapCols-1)
  44. self.minY = math.min(math.max(begY - disY, 0), self.mapRows-1)
  45. self.maxY = math.min(math.max(begY + disY, 0), self.mapRows-1)
  46. local limits = {}
  47. for x = self.minX, self.maxX do
  48. limits[x] = {}
  49. for y = self.minY, self.maxY do
  50. limits[x][y] = mapData:isObstacle(x, y)
  51. end
  52. end
  53. return limits
  54. end
  55. function PathFindAStar:getPathNode(x, y)
  56. if not self.mapData[x] or not self.mapData[x][y] then
  57. return nil
  58. end
  59. if self.mapNodes[x] and self.mapNodes[x][y] then
  60. return self.mapNodes[x][y]
  61. end
  62. local node = PathNode.new()
  63. node.x = x
  64. node.y = y
  65. node.h = 0
  66. node.k = 0
  67. node.t = "new"
  68. node.backpointer = nil
  69. node.isObstacle = self.mapData[x][y]
  70. if not self.mapNodes[x] then
  71. self.mapNodes[x] = {}
  72. end
  73. self.mapNodes[x][y] = node
  74. return node
  75. end
  76. function PathFindAStar:getNeighbors(node)
  77. local neighbors = {}
  78. local around =
  79. {
  80. { node.x + 1, node.y }, -- right
  81. { node.x, node.y + 1 }, -- bottom
  82. { node.x - 1, node.y }, -- left
  83. { node.x, node.y - 1 }, -- top
  84. { node.x + 1, node.y - 1 }, -- right up
  85. { node.x + 1, node.y + 1 }, -- right bottom
  86. { node.x - 1, node.y + 1 }, -- left bottom
  87. { node.x - 1, node.y - 1 }, -- left top
  88. }
  89. for i = 1, 8 do
  90. local x = around[i][1]
  91. local y = around[i][2]
  92. -- out of bounds
  93. if (x >= self.minX and x <= self.maxX and y >= self.minY and y <= self.maxY) and self.mapData[x][y] == 0 then
  94. local node = self:getPathNode(x, y)
  95. if node then
  96. tinsert(neighbors, node)
  97. end
  98. end
  99. end
  100. return neighbors
  101. end
  102. function PathFindAStar:getMinKNode()
  103. local node = nil
  104. local k = nil
  105. local index = nil
  106. for i, v in pairs(self.openlist) do
  107. if not node or v.k < node.k then
  108. node = v
  109. k = node.k
  110. index = i
  111. end
  112. end
  113. return node, k, index
  114. end
  115. function PathFindAStar:insert(node, h_new)
  116. if node.t == "new" then
  117. node.k = h_new
  118. node.h = h_new
  119. node.t = "open"
  120. tinsert(self.openlist, node)
  121. elseif node.t == "open" then
  122. node.k = math.min(node.k, h_new)
  123. elseif node.t == "closed" then
  124. node.k = math.min(node.h, h_new)
  125. node.h = h_new
  126. node.t = "open"
  127. tinsert(self.openlist, node)
  128. end
  129. return true
  130. end
  131. function PathFindAStar:process_state()
  132. if #self.openlist == 0 then
  133. return -1
  134. end
  135. local currentNode, currentK, index = self:getMinKNode()
  136. if currentK == currentNode.h then
  137. for _, neighbor in pairs(self:getNeighbors(currentNode)) do
  138. local cost = currentNode.h + currentNode:cost(neighbor)
  139. if (neighbor.t == "new") or (neighbor.backpointer == currentNode and neighbor.h ~= cost) or (neighbor.backpointer ~= currentNode and neighbor.h > cost) then
  140. neighbor.backpointer = currentNode
  141. self:insert(neighbor, cost)
  142. end
  143. end
  144. else
  145. for _, neighbor in pairs(self:getNeighbors(currentNode)) do
  146. local cost = neighbor.h + currentNode:cost(neighbor)
  147. if neighbor.h <= currentK and currentNode.h > cost then
  148. currentNode.backpointer = neighbor
  149. currentNode.h = cost
  150. end
  151. end
  152. end
  153. currentNode.t = "closed"
  154. table.remove(self.openlist, index)
  155. if #self.openlist == 0 then
  156. return -1
  157. end
  158. local currentNode, currentK, index = self:getMinKNode()
  159. return currentK, currentNode
  160. end
  161. function PathFindAStar:find_path(begX, begY, endX, endY)
  162. self:cleanup()
  163. local points = {}
  164. if true == global.PathFindController:IsImpass(cc.p(begX, begY)) then
  165. return points
  166. end
  167. if true == global.PathFindController:IsImpass(cc.p(endX, endY)) then
  168. return points
  169. end
  170. if false == global.PathFindController:CheckMoveAble(cc.p(endX, endY)) then
  171. return points
  172. end
  173. self.mapData = self:updateLimits(begX, begY, endX, endY)
  174. self.endPathNode = self:getPathNode(begX, begY)
  175. self.startPathNode = self:getPathNode(endX, endY)
  176. if nil == self.startPathNode or nil == self.endPathNode then
  177. return points
  178. end
  179. -- start
  180. self:insert(self.endPathNode, 0)
  181. while true do
  182. local ret = self:process_state()
  183. if ret == -1 then
  184. return points
  185. elseif self.startPathNode.t == "closed" then
  186. break
  187. end
  188. end
  189. -- points
  190. tinsert(points, {x=self.startPathNode.x, y=self.startPathNode.y})
  191. local node = self.startPathNode.backpointer
  192. while node ~= self.endPathNode do
  193. tinsert(points, {x=node.x, y=node.y})
  194. node = node.backpointer
  195. if nil == node then
  196. points = {}
  197. break
  198. end
  199. end
  200. return points
  201. end
  202. return PathFindAStar