Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/xiejiangzhi/astar
A star path finder for Lua
https://github.com/xiejiangzhi/astar
astar-algorithm lua path-finder
Last synced: 6 days ago
JSON representation
A star path finder for Lua
- Host: GitHub
- URL: https://github.com/xiejiangzhi/astar
- Owner: xiejiangzhi
- License: mit
- Created: 2020-03-23T12:19:05.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-09-25T05:58:44.000Z (about 1 year ago)
- Last Synced: 2024-08-02T06:16:10.544Z (3 months ago)
- Topics: astar-algorithm, lua, path-finder
- Language: Lua
- Size: 189 KB
- Stars: 11
- Watchers: 2
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
- awesome-love2d - astar - An other A* library. Any map (grid, point, mesh or infinite map) and support path cost. (AI)
README
AStar
========An other A* library. You can use the node or map mode you like(gird, point, mesh or infinite map).
The library don't need to init a map and don't care the size of your map.
We just tell the algo how to get node, neighbors and estimate cost.Base algorithm logic referenced [a-star-lua](https://github.com/lattejed/a-star-lua)
![img](./example.png)
## Use
A example for grid map
```lua
local AStar = require 'astar'local map = {}
local cached_nodes = {}-- Node must be able to check if they are the same
-- Cannot directly return different tables for the same coord
-- The library doesn't change nodes, so you able to reuse your node or create a C struct for faster
local function get_node(x, y)
local row = cached_nodes[y]
if not row then row = {}; cached_nodes[y] = row end
local node = row[x]
if not node then node = { x = x, y = y }; row[x] = node end
return node
endlocal neighbors_offset = {
{ -1, -1 }, { 0, -1 }, { 1, -1 },
{ -1, 0 }, { 1, 0 },
{ -1, 1 }, { 0, 1 }, { 1, 1 },
}
-- Return all neighbor nodes. Means a target that can be moved from the current node
-- add_neighbor_cb: function(new_node, cost)
-- cost is optional, call map:get_cost to get cost if no cost value
function map:get_neighbors(node, from_node, add_neighbor_cb, userdata)
for i, offset in ipairs(neighbors_offset) do
add_neighbor_cb(get_node(node.x + offset[1], node.y + offset[2]))
end
end-- Cost of two adjacent nodes.
-- Distance, distance + cost or other comparison value you want
function map:get_cost(from_node, to_node, userdata)
return math.sqrt(math.pow(from_node.x - to_node.x, 2) + math.pow(from_node.y - to_node.y, 2))
end-- For heuristic. Estimate cost of current node to goal node
-- As close to the real cost as possible
function map:estimate_cost(node, goal_node, userdata)
return self:get_cost(node, goal_node)
endlocal finder = AStar.new(map)
local start, goal = get_node(1, 1), get_node(3, 4)
local userdata = { 'mydata' }
local path = finder:find(start, goal, userdata)if path then
for _, node in ipairs(path) do
print(node.x, node.y)
end
else
print("Not found path")
end
```And you can try to run the `main.lua` by Love2d
## Better path
[This](https://www.gamasutra.com/view/feature/131505/toward_more_realistic_pathfinding.php?print=1) article tells us how to generate a smooth path with a turning radius.