Coder Social home page Coder Social logo

a-star-lua's Introduction

A* for Lua

A clean, simple implementation of the A* pathfinding algorithm for Lua.

This implementation has no dependencies and has a simple interface. It takes a table of nodes, a start and end point and a "valid neighbor" function which makes it easy to adapt the module's behavior, especially in circumstances where valid paths would frequently change.

The module automatically caches paths which saves CPU time for frequently accessed start and end nodes. Cached paths can be ignored by setting the appropriate parameter in the main function. Cached paths can be purged with the clear_cached_paths () function.

Usage example

-- this function determines which neighbors are valid (e.g., within range)
local valid_node_func = function ( node, neighbor ) 

	local MAX_DIST = 300
		
	-- helper function in the a-star module, returns distance between points
	if astar.distance ( node.x, node.y, neighbor.x, neighbor.y ) < MAX_DIST then
		return true
	end
	return false
end

local ignore = true -- ignore cached paths

local path = astar.path ( start, end, all_nodes, ignore, valid_node_func )

if path then
	-- do something with path (a lua table of ordered nodes from start to end)
end

Notes

This assumes that nodes are objects (tables) with (at least) members "x" and "y" that hold the node's coordinates.

node = {}
node.x = 123
node.y = 456
node.foo = "bar"

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.