{"id":15616475,"url":"https://github.com/shakadak/lua-astar","last_synced_at":"2025-09-16T20:48:40.932Z","repository":{"id":75551463,"uuid":"68203410","full_name":"Shakadak/lua-astar","owner":"Shakadak","description":"A simple generic Lua implementation of the A* (A star) pathfinding algorithm","archived":false,"fork":false,"pushed_at":"2016-10-19T16:46:39.000Z","size":23,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-28T13:08:50.075Z","etag":null,"topics":["a-star","agnostic-pathfinding","agnostic-pathfinding-algorithm","astar","generic","graph","lua","pathfinding","pathfinding-algorithm"],"latest_commit_sha":null,"homepage":"","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/Shakadak.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,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-09-14T12:14:33.000Z","updated_at":"2024-07-27T08:23:11.000Z","dependencies_parsed_at":"2023-06-06T21:15:33.625Z","dependency_job_id":null,"html_url":"https://github.com/Shakadak/lua-astar","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Shakadak/lua-astar","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shakadak%2Flua-astar","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shakadak%2Flua-astar/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shakadak%2Flua-astar/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shakadak%2Flua-astar/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Shakadak","download_url":"https://codeload.github.com/Shakadak/lua-astar/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shakadak%2Flua-astar/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":275482969,"owners_count":25473109,"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","status":"online","status_checked_at":"2025-09-16T02:00:10.229Z","response_time":65,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["a-star","agnostic-pathfinding","agnostic-pathfinding-algorithm","astar","generic","graph","lua","pathfinding","pathfinding-algorithm"],"created_at":"2024-10-03T07:09:15.694Z","updated_at":"2025-09-16T20:48:40.885Z","avatar_url":"https://github.com/Shakadak.png","language":"Lua","funding_links":[],"categories":[],"sub_categories":[],"readme":"# lua-astar\nA simple implementation of the A* pathfinding algorithm\n\n# How to use it\nThe `aStar` function expect a few more arguments than most other implementations available anywhere.  \nHere is a reproduction of the `example.lua` file:\n\n```lua\nlocal aStar = require \"AStar\"\n```\nHere we define our graph. A simple table containing for each key (node)\nan array of node.\n```lua\nlocal graph = {}\ngraph[\"A\"] = {\"B\", \"C\"}\ngraph[\"B\"] = {\"D\"}\ngraph[\"C\"] = {\"A\", \"E\"}\ngraph[\"E\"] = {\"C\", \"D\"}\ngraph[\"D\"] = {\"E\", \"F\"}\ngraph[\"F\"] = {\"D\", \"G\"}\ngraph[\"G\"] = {\"F\", \"H\", \"E\"}\ngraph[\"H\"] = {\"E\"}\n```\nSo we currently have the following representation:\n```\nA ↔ C ↔ E ← H\n↓     ⤢   ↖ ↑\nB → D ↔ F ↔ G\n```\nFirst we need to define a function that, given a node, returns an array `{node} / {index = node } / {key = node}`\nof the nodes linked to the given node.\nSince our graph is simply defined, it will be straightforward. We will not safeguard it as our graph is fully defined.\nCheck `exemple.lua` to see slightly more.\n```lua\nlocal function expand(n)\n    return graph[n]\nend\n```\nNow we need to define a function that, given two nodes, return the cost of going\nfrom the first to the second.\nAgain, we want to keep it simple. Our graph does not hold these value, so we will\nsimply return `1` every time.  \nAs needed, we will define a curried function.\n```lua\nlocal function cost(from)\n    return function(to)\n        return 1\n    end\nend\n```\n\nNow we need to define a function that, given a node, return the estimated cost\nof the path left to reach the goal.\nAs always, since our graph is so simple, we will content ourselves with returning `0`\nevery time. This will make our `aStar` equivalent to a `dijkstra`.\n```lua\nlocal function heuristic(n)\n    return 0\nend\n```\n\nLast, but not least, we need to define that, given a node, return whether we have\nreached our goal or not. As the first example, we will want to find the path\nfrom `A` to `D`.\n```lua\nlocal goalD = function(n)\n    return n == \"D\"\nend\n```\n\nTo avoid repeated call of the same functions, we will define a `simpleAStar`\nin order to concern ourselves only with the goal and the start.\n```lua\nlocal simpleAStar = aStar(expand)(cost)(heuristic)\n```\nBecause aStar is curried, you must pass each argument separatly.\nJust make sure to apply the arguments in the correct order.\n\nWe can now ask `simpleAStar` to find the path from `A` to `D`.\n```lua\nlocal path = simpleAStar(goalD)(\"A\")\n```\n\nThe only thing left to do is display the path we found.\nWe do need a function to convert our path to something printable.\n```lua\nlocal function pathToString(path)\n    if path == nil then\n        return \"No path found\"\n    else\n        local ret = table.remove(path, 1)\n        for _, n in ipairs(path) do\n            ret = ret .. \" → \" .. n\n        end\n        return ret\n    end\nend\n```\n\nIf everything worked fine, it should display `A → B → D`.\n```lua\nprint(pathToString(path))\n```\n\nNow we want the path to go from `D` to `A`.\nFor future reuse, we will define a curried function that simply check\nif our node is in an array of node.\n```lua\nlocal function goal(targets)\n    return function(current)\n        for _, target in pairs(targets) do\n            if current == target then\n                return true\n            end\n        end\n        return false\n    end\nend\n\n```\nThis time we cannot pass by B, it should display `D → E → C → A`.\n```lua\nprint(pathToString(simpleAStar(goal({\"A\"}))(\"D\")))\n```\n\nWe could now considere that both `B` and `D` are point of interest,\nand we want to get to one of them, we do not particularly care\nbut we want the one with the shorter travel. Our graph is too simple\nto present something meaningful here, but by changing the starting\nnode, we can show how the path differe.\n\nStarting with `C` should give us either `C → A → B` or `C → E → D`.\n```lua\nprint(pathToString(simpleAStar(goal({\"B\", \"D\"}))(\"C\")))\n```\nStarting with `A` should give us `A → B`.\n```lua\nprint(pathToString(simpleAStar(goal({\"B\", \"D\"}))(\"A\")))\n```\nStarting with `E` should give us `E → D`.\n```lua\nprint(pathToString(simpleAStar(goal({\"B\", \"D\"}))(\"E\")))\n```\n\nStarting with `H` should give us `H → E → D`.\n```lua\nprint(pathToString(simpleAStar(goal({\"B\", \"D\"}))(\"H\")))\n```\nStarting with `A` should give `A → B → D → F → G → H`\n```lua\nprint(pathToString(simpleAStar(goal({\"H\"}))(\"A\")))\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshakadak%2Flua-astar","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshakadak%2Flua-astar","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshakadak%2Flua-astar/lists"}