2 Lua star example - Run with love (https://love2d.org/)
3 Copyright 2018 Wesley Werner <wesley.werner@gmail.com>
5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 https://en.wikipedia.org/wiki/A*_search_algorithm
13 https://www.redblobgames.com/pathfinding/a-star/introduction.html
14 https://www.raywenderlich.com/4946/introduction-to-a-pathfinding
17 --- Provides easy A* path finding.
22 --- Clears all cached paths.
23 function module:clearCached()
27 -- (Internal) Returns a unique key for the start and end points.
28 local function keyOf(start, goal)
29 return string.format("%d,%d,%d>%d,%d,%d", start.x, start.y, start.z, goal.x, goal.y, goal.z)
32 -- (Internal) Returns the cached path for start and end points.
33 local function getCached(start, goal)
35 local key = keyOf(start, goal)
36 return module.cache[key]
40 -- (Internal) Saves a path to the cache.
41 local function saveCached(start, goal, path)
42 module.cache = module.cache or { }
43 local key = keyOf(start, goal)
44 module.cache[key] = path
47 -- (Internal) Return the distance between two points.
48 -- This method doesn't bother getting the square root of s, it is faster
49 -- and it still works for our use.
50 local function distance(x1, y1, z1, x2, y2, z2)
54 local s = dx * dx + dy * dy + dz * dz
58 -- (Internal) Clamp a value to a range.
59 local function clamp(x, min, max)
60 return x < min and min or (x > max and max or x)
63 -- (Internal) Return the score of a node.
64 -- G is the cost from START to this node.
65 -- H is a heuristic cost, in this case the distance from this node to the goal.
66 -- Returns F, the sum of G and H.
67 local function calculateScore(previous, node, goal)
69 local G = previous.score + 1
70 local H = distance(node.x, node.y, node.z, goal.x, goal.y, goal.z)
75 -- (Internal) Returns true if the given list contains the specified item.
76 local function listContains(list, item)
77 for _, test in ipairs(list) do
78 if test.x == item.x and test.y == item.y and test.z == item.z then
85 -- (Internal) Returns the item in the given list.
86 local function listItem(list, item)
87 for _, test in ipairs(list) do
88 if test.x == item.x and test.y == item.y and test.z == item.z then
94 -- (Internal) Requests adjacent map values around the given node.
95 local function getAdjacent(width, height, depth, node, positionIsOpenFunc, includeDiagonals)
104 local a = math.abs(x) + math.abs(y) + math.abs(z)
106 if a ~= 0 and includeDiagonals or a == 1 then
107 table.insert(positions, {x = x, y = y, z = z})
113 for _, point in ipairs(positions) do
114 local px = clamp(node.x + point.x, 1, width)
115 local py = clamp(node.y + point.y, 1, height)
116 local pz = clamp(node.z + point.z, 1, depth)
118 local value = positionIsOpenFunc( px, py, pz )
120 table.insert( result, { x = px, y = py, z = pz } )
128 -- Returns the path from start to goal, or false if no path exists.
129 function module:find(width, height, depth, start, goal, positionIsOpenFunc, useCache, excludeDiagonalMoving)
132 local cachedPath = getCached(start, goal)
138 local success = false
144 start.H = distance(start.x, start.y, start.z, goal.x, goal.y, goal.z)
145 start.parent = { x = 0, y = 0, z = 0 }
146 table.insert(open, start)
148 while not success and #open > 0 do
150 -- sort by score: high to low
151 table.sort(open, function(a, b) return a.score > b.score end)
153 local current = table.remove(open)
155 table.insert(closed, current)
157 success = listContains(closed, goal)
161 local adjacentList = getAdjacent(width, height, depth, current, positionIsOpenFunc, not excludeDiagonalMoving)
163 for _, adjacent in ipairs(adjacentList) do
165 if not listContains(closed, adjacent) then
167 if not listContains(open, adjacent) then
169 adjacent.score = calculateScore(current, adjacent, goal)
170 adjacent.parent = current
171 table.insert(open, adjacent)
187 -- traverse the parents from the last point to get the path
188 local node = listItem(closed, closed[#closed])
193 table.insert(path, 1, { x = node.x, y = node.y, z = node.z } )
194 node = listItem(closed, node.parent)
198 saveCached(start, goal, path)
200 -- reverse the closed list to get the solution