tcod.path

Example:

>>> import numpy as np
>>> import tcod.path
>>> dungeon = np.array(
...     [
...         [1, 0, 1, 1, 1],
...         [1, 0, 1, 0, 1],
...         [1, 1, 1, 0, 1],
...     ],
...     dtype=np.int8,
...     )
...

# Create a pathfinder from a numpy array.
# This is the recommended way to use the tcod.path module.
>>> astar = tcod.path.AStar(dungeon)
>>> print(astar.get_path(0, 0, 2, 4))
[(1, 0), (2, 1), (1, 2), (0, 3), (1, 4), (2, 4)]
>>> astar.cost[0, 1] = 1 # You can access the map array via this attribute.
>>> print(astar.get_path(0, 0, 2, 4))
[(0, 1), (0, 2), (0, 3), (1, 4), (2, 4)]

# Create a pathfinder from an edge_cost function.
# Calling Python functions from C is known to be very slow.
>>> def edge_cost(my_x, my_y, dest_x, dest_y):
...     return dungeon[dest_x, dest_y]
...
>>> dijkstra = tcod.path.Dijkstra(
...     tcod.path.EdgeCostCallback(edge_cost, dungeon.shape),
...     )
...
>>> dijkstra.set_goal(0, 0)
>>> print(dijkstra.get_path(2, 4))
[(0, 1), (0, 2), (0, 3), (1, 4), (2, 4)]

Changed in version 5.0: All path-finding functions now respect the NumPy array shape (if a NumPy array is used.)

class tcod.path.AStar(cost: Any, diagonal: float = 1.41)[source]
Parameters:
  • cost (Union[tcod.map.Map, numpy.ndarray, Any]) –
  • diagonal (float) – Multiplier for diagonal movement. A value of 0 will disable diagonal movement entirely.
get_path(start_x: int, start_y: int, goal_x: int, goal_y: int) → List[Tuple[int, int]][source]

Return a list of (x, y) steps to reach the goal point, if possible.

Parameters:
  • start_x (int) – Starting X position.
  • start_y (int) – Starting Y position.
  • goal_x (int) – Destination X position.
  • goal_y (int) – Destination Y position.
Returns:

A list of points, or an empty list if there is no valid path.

Return type:

List[Tuple[int, int]]

class tcod.path.Dijkstra(cost: Any, diagonal: float = 1.41)[source]
Parameters:
  • cost (Union[tcod.map.Map, numpy.ndarray, Any]) –
  • diagonal (float) – Multiplier for diagonal movement. A value of 0 will disable diagonal movement entirely.
get_path(x: int, y: int) → List[Tuple[int, int]][source]

Return a list of (x, y) steps to reach the goal point, if possible.

set_goal(x: int, y: int) → None[source]

Set the goal point and recompute the Dijkstra path-finder.

class tcod.path.EdgeCostCallback(callback: Callable[[int, int, int, int], float], shape: Tuple[int, int])[source]

Calculate cost from an edge-cost callback.

callback is the custom userdata to send to the C call.

shape is a 2-item tuple representing the maximum boundary for the algorithm. The callback will not be called with parameters outside of these bounds.

Changed in version 5.0: Now only accepts a shape argument instead of width and height.

class tcod.path.NodeCostArray[source]

Calculate cost from a numpy array of nodes.

array is a NumPy array holding the path-cost of each node. A cost of 0 means the node is blocking.

static __new__(cls, array: numpy.ndarray) → tcod.path.NodeCostArray[source]

Validate a numpy array and setup a C callback.