{"id":13524122,"url":"https://github.com/xiejiangzhi/astar","last_synced_at":"2025-04-01T02:30:32.358Z","repository":{"id":147735073,"uuid":"249422636","full_name":"xiejiangzhi/astar","owner":"xiejiangzhi","description":"A star path finder for Lua","archived":false,"fork":false,"pushed_at":"2023-09-25T05:58:44.000Z","size":194,"stargazers_count":12,"open_issues_count":1,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-02T08:31:57.767Z","etag":null,"topics":["astar-algorithm","lua","path-finder"],"latest_commit_sha":null,"homepage":null,"language":"Lua","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xiejiangzhi.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2020-03-23T12:19:05.000Z","updated_at":"2024-10-14T12:26:58.000Z","dependencies_parsed_at":"2024-01-23T07:49:32.881Z","dependency_job_id":null,"html_url":"https://github.com/xiejiangzhi/astar","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xiejiangzhi%2Fastar","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xiejiangzhi%2Fastar/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xiejiangzhi%2Fastar/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xiejiangzhi%2Fastar/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xiejiangzhi","download_url":"https://codeload.github.com/xiejiangzhi/astar/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246572121,"owners_count":20798897,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["astar-algorithm","lua","path-finder"],"created_at":"2024-08-01T06:01:07.163Z","updated_at":"2025-04-01T02:30:32.116Z","avatar_url":"https://github.com/xiejiangzhi.png","language":"Lua","funding_links":[],"categories":["AI"],"sub_categories":[],"readme":"AStar\n========\n\nAn other A* library. You can use the node or map mode you like(gird, point, mesh or infinite map).\n\nThe library don't need to init a map and don't care the size of your map.\nWe just tell the algo how to get node, neighbors and estimate cost.\n\nBase algorithm logic referenced [a-star-lua](https://github.com/lattejed/a-star-lua)\n\n![img](./example.png)\n\n## Use\n\nA example for grid map\n\n```lua\nlocal AStar = require 'astar'\n\nlocal map = {}\nlocal cached_nodes = {}\n\n-- Node must be able to check if they are the same\n-- Cannot directly return different tables for the same coord\n-- The library doesn't change nodes, so you able to reuse your node or create a C struct for faster\nlocal function get_node(x, y)\n  local row = cached_nodes[y]\n  if not row then row = {}; cached_nodes[y] = row end\n  local node = row[x]\n  if not node then node = { x = x, y = y }; row[x] = node end\n  return node\nend\n\nlocal neighbors_offset = {\n  { -1, -1 }, { 0, -1 }, { 1, -1 },\n  { -1, 0 }, { 1, 0 },\n  { -1, 1 }, { 0, 1 }, { 1, 1 },\n}\n-- Return all neighbor nodes. Means a target that can be moved from the current node\n-- add_neighbor_cb: function(new_node, cost)\n--    cost is optional, call map:get_cost to get cost if no cost value\nfunction map:get_neighbors(node, from_node, add_neighbor_cb, userdata)\n  for i, offset in ipairs(neighbors_offset) do\n    add_neighbor_cb(get_node(node.x + offset[1], node.y + offset[2]))\n  end\nend\n\n-- Cost of two adjacent nodes.\n-- Distance, distance + cost or other comparison value you want\nfunction map:get_cost(from_node, to_node, userdata)\n  return math.sqrt(math.pow(from_node.x - to_node.x, 2) + math.pow(from_node.y - to_node.y, 2))\nend\n\n-- For heuristic. Estimate cost of current node to goal node\n-- As close to the real cost as possible\nfunction map:estimate_cost(node, goal_node, userdata)\n  return self:get_cost(node, goal_node)\nend\n\nlocal finder = AStar.new(map)\nlocal start, goal = get_node(1, 1), get_node(3, 4)\nlocal userdata = { 'mydata' }\nlocal path = finder:find(start, goal, userdata)\n\nif path then\n  for _, node in ipairs(path) do\n    print(node.x, node.y)\n  end\nelse\n  print(\"Not found path\")\nend\n```\n\nAnd you can try to run the `main.lua` by Love2d\n\n## Better path\n\n[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.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxiejiangzhi%2Fastar","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxiejiangzhi%2Fastar","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxiejiangzhi%2Fastar/lists"}