Safe Haskell | None |
---|---|
Language | Haskell2010 |
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.
.......... ..........
@
- type TCODPathFunc = Int -> Int -> Int -> Int -> IO Double
- type TCODPathFuncRaw = CInt -> CInt -> CInt -> CInt -> Ptr () -> IO CFloat
- data TCODPath = TCODPath {
- unTCODPath :: Ptr ()
- pathHaskFun :: Maybe (FunPtr TCODPathFuncRaw)
- pathNewUsingMap :: TCODMap -> Float -> IO TCODPath
- pathNewUsingFunction :: Int -> Int -> TCODPathFunc -> Float -> IO TCODPath
- pathDelete :: TCODPath -> IO ()
- pathCompute :: TCODPath -> Int -> Int -> Int -> Int -> IO Bool
- pathWalk :: TCODPath -> Bool -> IO (Bool, Int, Int)
- pathIsEmpty :: TCODPath -> IO Bool
- pathSize :: TCODPath -> IO Int
- pathReverse :: TCODPath -> IO ()
- pathGet :: TCODPath -> Int -> IO (Int, Int)
- pathGetOrigin :: TCODPath -> IO (Int, Int)
- pathGetDesitnation :: TCODPath -> IO (Int, Int)
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
Reference to TCOD path finder state
TCODPath | |
|
:: 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.
:: 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
:: 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.
:: 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*.
pathReverse :: TCODPath -> IO () Source #
Reversing a path
Once you computed a path, you can exchange origin and destination
Read the path cells' coordinates
You can get the coordinates of each point along the path