GithubHelp home page GithubHelp logo

gloryfish / lua-astar Goto Github PK

View Code? Open in Web Editor NEW
31.0 4.0 4.0 124 KB

A simple implementation of the A* (A Star) pathfinding algorithm for lua

Home Page: http://gloryfish.org

License: Other

Lua 100.00%

lua-astar's Introduction

Lua A*

About

This is a simple implementation of the A* (A Star) pathfinding algorithm for lua. It is intended to be relatively easy to read and learn from.

It is based on the similarly easy to understand Python implementation by John Eriksson.

http://www.pygame.org/project-AStar-195-.html

Usage

Add astar.lua and middleclass.lua to your project.

Begin by implementing a map handler for your specific game. The map handler's job is to determine which nodes are valid for a given path and to calculate a heuristic score for each.

You can find an example handler in "examplehandler.lua". Simply implement those methods in a table (most likely in your level or game object) and pass that table to the AStar class.

local maphandler = MyMapHandler()
local astar = AStar(maphandler)

When you want to calculate a path

local path = astar:findPath(start, goal) 

"start" and "goal" are locations in your game. In the example handler, they are simple vectors with x, y components. However, they could be anything: hex tiles, states in a game of tic-tac-toe, etc. You simply need to implement the handler methods for your specific game needs.

astar:findPath() will return a Path object with two methods.

path:getTotalMoveCost() returns the total cost of the path. You could use it to limit enemy movement if the path cost was above a certain threshold.

path:getNodes() returns a table containing each node in the path, from start to goal.

Example

Run test.lua to see an example using the TiledMapHandler. This is similar to the implementation used in my Reddit Game Jam 05 submission "Unrequited".

The source code is on GitHub.

Contributors

Sandy Maguire

License

See license.txt

lua-astar's People

Contributors

gloryfish avatar isovector avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

lua-astar's Issues

getBestOpenNode() is O(n)

Replace the linear table lookup in getBestOpenNode with a MinHeap pop.

Need to implement a min heap. Replace astar.on with the heap. Check to see if astar.o needs to be replaced.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.