tcod-haskell-0.1.0.0: Bindings to libtcod roguelike engine

Safe HaskellNone
LanguageHaskell2010

Game.TCOD.Path

Description

This toolkit allows one to easily calculate the optimal path between two points in your dungeon by using either the href="http://en.wikipedia.org/wiki/A*"A* algorithm/a or href="http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm"Dijkstra's algorithm/a. Please note that the paths generated with the two algorithms may differ slightly. Due to how they're implemented, A* will usually prefer diagonal moves over orthogonal, while Dijkstra will have the opposite preference. In other words, paths from point X to point Y will look like this: @ Dijkstra: A*: .......... .......... .X........ .X*....... ..*....... ...**..... ...*...... .....**... ....****Y. .......*Y. .......... .......... @

Synopsis

Documentation

type TCODPathFunc = Int -> Int -> Int -> Int -> IO Double Source #

Callback path function, takes from (x, y) and to (x, y), returns cell cost

A custom function that must return the walk cost from coordinates xFrom,yFrom to coordinates xTo,yTo. The cost must be > 0.0f if the cell xTo,yTo is walkable. It must be equal to 0.0f if it's not. You must not take additional cost due to diagonal movements into account as it's already done by the pathfinder.

type TCODPathFuncRaw = CInt -> CInt -> CInt -> CInt -> Ptr () -> IO CFloat Source #

C type of TCODPathFunc

data TCODPath Source #

Reference to TCOD path finder state

Constructors

TCODPath 

Fields

Instances

pathNewUsingMap Source #

Arguments

:: TCODMap 
-> Float

diagonal cost. Cost of a diagonal movement compared to an horizontal or vertical movement. On a standard cartesian map, it should be sqrt(2) (1.41f). It you want the same cost for all movements, use 1.0f. If you don't want the path finder to use diagonal movements, use 0.0f.

-> IO TCODPath 

Allocating a pathfinder from a map

First, you have to allocate a path using a map from Fov module.

pathNewUsingFunction Source #

Arguments

:: Int

map width

-> Int

map height

-> TCODPathFunc

A custom function that must return the walk cost from coordinates xFrom,yFrom to coordinates xTo,yTo. The cost must be > 0.0f if the cell xTo,yTo is walkable. It must be equal to 0.0f if it's not. You must not take additional cost due to diagonal movements into account as it's already done by the pathfinder.

-> Float

diagonal cost. Cost of a diagonal movement compared to an horizontal or vertical movement. On a standard cartesian map, it should be sqrt(2) (1.41f). It you want the same cost for all movements, use 1.0f. If you don't want the path finder to use diagonal movements, use 0.0f.

-> IO TCODPath 

Allocating a pathfinder using a callback

Since the walkable status of a cell may depend on a lot of parameters (the creature type, the weather, the terrain type...), you can also create a path by providing a function rather than relying on a TCODMap.

pathDelete :: TCODPath -> IO () Source #

To release the resources used by a path, destroy it

pathCompute Source #

Arguments

:: TCODPath 
-> Int

ox. Coordinates of the origin of the path.

-> Int

oy. Coordinates of the origin of the path.

-> Int

dx. Coordinates of the destination of the path.

-> Int

dy. Coordinates of the destination of the path.

-> IO Bool 

Computing an A* path

Once you created a TCODPath object, you can compute the path between two points. Both points should be inside the map, and at a walkable position. The function returns false if there is no possible path.

pathWalk Source #

Arguments

:: TCODPath 
-> Bool

recalculate when needed. If the next point is no longer walkable (another creature may be in the way), recalculate a new path and walk it.

-> IO (Bool, Int, Int) 

Walking the path

You can walk the path and go to the next step with : Note that walking the path consume one step (and decrease the path size by one). The function returns false if recalculateWhenNeeded is false and the next cell on the path is no longer walkable, or if recalculateWhenNeeded is true, the next cell on the path is no longer walkable and no other path has been found. Also note that recalculateWhenNeeded only applies to A*.

pathIsEmpty :: TCODPath -> IO Bool Source #

Check is path has no steps

pathSize :: TCODPath -> IO Int Source #

Get number of steps the path consists of

pathReverse :: TCODPath -> IO () Source #

Reversing a path

Once you computed a path, you can exchange origin and destination

pathGet Source #

Arguments

:: TCODPath 
-> Int

index. Step number. 0 <= index < path size

-> IO (Int, Int) 

Read the path cells' coordinates

You can get the coordinates of each point along the path

pathGetOrigin :: TCODPath -> IO (Int, Int) Source #

Get origin/start of the path

pathGetDesitnation :: TCODPath -> IO (Int, Int) Source #

Get destination/end of the path