Pathfinding v0.1.1 Pathfinding.Grid View Source
Grid definition that calls to Pathfinding.find_path
and
Pathfinding.find_walkable
will search against. The grid is defined so its
easy to make repeated searches across it without repeatedly reconstructing it.
Tiles, Walkability, Costs
%Pathfinding.Grid{
tiles: [
[1, 1, 0, 1, 1],
[1, 1, 0, 1, 1],
[1, 1, 0, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]
]
}
tiles
is the tile definition that most of the functions in
Pathfinding.Grid
and Pathfinding
use for traversal. Whatever numbers you
choose for the tiles is unimportant, except that they are a superset of all
potential values in walkable_tiles
and keys in costs
.
Important to mix with tiles
is walkable_tiles
because it determines what
tiles are valid for traversal, without specifying walkable_tiles
the grid
will not be pathable at all.
%Pathfinding.Grid{
tiles: [
[1, 0, 1],
[1, 0, 1],
[1, 1, 1]
],
walkable_tiles: [1]
}
In the example above, only 1
is walkable, so a path from (0, 0) to (0, 2)
will avoid (0, 1).
Costs
%Pathfinding.Grid{
costs: %{
0 => 5
}
tiles: [
[1, 0, 1],
[1, 0, 1],
[1, 1, 1]
],
walkable_tiles: [0, 1]
}
Specifying a costs
map will cause different tiles to have different weights,
a detail that can be much better explained by the A-star algorithm.
Any tiles not specified in the costs
map, or if a costs
map is not
specified at all, will have a cost of 1.
Extra Costs
%Pathfinding.Grid{
costs: %{
1 => 3
}
extra_costs: %{
1 => %{
2 => 2
}
}
tiles: [
[1, 0, 1],
[1, 0, 1],
[1, 1, 1]
],
walkable_tiles: [0, 1]
}
Similar to costs
, extra_costs
indicates a specific coordinate as an
increased weight associated with it. Any coord with an extra cost will have
its tile's cost overriden, so in the above example the cost of traversing
(1, 2) is 2.
Specifying this map is programmatically cumbersome, so two alternatives exist:
Pathfinding.Grid.add_extra_cost/4
andPathfinding.Grid.remove_extra_cost/3
can be used to specify a single unwalkable coordinate.Pathfinding.Grid.to_coord_map/2
can convert a list of coordinates into anextra_costs
map.
Unwalkable Coords
%Pathfinding.Grid{
tiles: [
[1, 0, 1],
[1, 0, 1],
[1, 1, 1]
],
unwalkable_coords: %{
1 => %{
2 => true
}
},
walkable_tiles: [1]
}
unwalkable_coords
is a map that can be specified to mark a specific
coordinate invalid for traversal, even though the tile is normally pathable.
This is useful to simulate an obstruction that is not typically represented
in the grid of tiles. In the above example, the right-most column is
unreachable by the left-most column because (1, 2) is unwalkable.
Specifying this map is programmatically cumbersome, so two alternatives exist:
Pathfinding.Grid.add_unwalkable_coord/3
andPathfinding.Grid.remove_unwalkable_coord/3
can be used to specify a single unwalkable coordinate.Pathfinding.Grid.to_coord_map/2
can convert a list of coordinates into anunwalkable_coords
map.
Unstoppable Coords
%Pathfinding.Grid{
tiles: [
[1, 0, 1],
[1, 0, 1],
[1, 1, 1]
],
unstoppable_coords: %{
1 => %{
2 => true
}
},
walkable_tiles: [1]
}
In the same vein as unwalkable_coords
, unstoppable_coords
are pathable, but
not valid destinations. This is useful to simulate an obstruction can be moved
through. In the above example, (1, 2) cannot be pathed to, but it can be
passed through, making the the right-most column accessible from left-most
column.
Specifying this map is programmatically cumbersome, so two alternatives exist:
Pathfinding.Grid.add_unstoppable_coord/3
andPathfinding.Grid.remove_unstoppable_coord/3
can be used to specify a single unstoppable coordinate.Pathfinding.Grid.to_coord_map/2
can convert a list of coordinates into anunstoppable_coords
map.
Grid Type
The grid's type
determines how the grid will be traversed. The examples
below specify the order in which a coordinate's neighbors are traversed for
each grid type.
:cardinal
1
4 x 2
3
:hex
1 2
6 x 3
5 4
:intercardinal
8 1 2
7 x 3
6 5 4
Link to this section Summary
Functions
Converts a list of coordinates into a map of nested coordinates
Link to this section Types
Link to this section Functions
add_extra_cost(grid, x, y, cost) View Source
add_unstoppable_coord(grid, x, y) View Source
add_unwalkable_coord(grid, x, y) View Source
clear_extra_costs(grid) View Source
clear_unstoppable_coords(grid) View Source
clear_unwalkable_coords(grid) View Source
get_coord_cost(grid, x, y)
View Source
get_coord_cost(t(), Number.t(), Number.t()) :: Number.t()
get_coord_cost(t(), Number.t(), Number.t()) :: Number.t()
get_extra_cost(grid, x, y)
View Source
get_extra_cost(t(), Number.t(), Number.t()) :: Number.t()
get_extra_cost(t(), Number.t(), Number.t()) :: Number.t()
in_grid?(grid, x, y)
View Source
in_grid?(t(), Number.t(), Number.t()) :: Boolean.t()
in_grid?(t(), Number.t(), Number.t()) :: Boolean.t()
is_cardinal?(grid)
View Source
is_cardinal?(t()) :: Boolean.t()
is_cardinal?(t()) :: Boolean.t()
is_coord_stoppable?(grid, x, y)
View Source
is_coord_stoppable?(t(), Number.t(), Number.t()) :: Boolean.t()
is_coord_stoppable?(t(), Number.t(), Number.t()) :: Boolean.t()
is_coord_walkable?(grid, x, y)
View Source
is_coord_walkable?(t(), Number.t(), Number.t()) :: Boolean.t()
is_coord_walkable?(t(), Number.t(), Number.t()) :: Boolean.t()
is_hex?(grid)
View Source
is_hex?(t()) :: Boolean.t()
is_hex?(t()) :: Boolean.t()
is_intercardinal?(grid)
View Source
is_intercardinal?(t()) :: Boolean.t()
is_intercardinal?(t()) :: Boolean.t()
remove_extra_cost(grid, x, y) View Source
remove_unstoppable_coord(grid, x, y) View Source
remove_unwalkable_coord(grid, x, y) View Source
set_tile_cost(grid, tile, cost) View Source
to_coord_map(coords, map \\ %{}, value \\ true) View Source
Converts a list of coordinates into a map of nested coordinates.