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:

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:

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:

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

Link to this section Types

Link to this type

t() View Source
t() :: %Pathfinding.Grid{
  costs: term(),
  extra_costs: term(),
  tiles: term(),
  type: term(),
  unstoppable_coords: term(),
  unwalkable_coords: term(),
  walkable_tiles: term()
}

Link to this section Functions

Link to this function

add_extra_cost(grid, x, y, cost) View Source
add_extra_cost(t(), Number.t(), Number.t(), Number.t()) :: t()

Link to this function

add_unstoppable_coord(grid, x, y) View Source
add_unstoppable_coord(t(), Number.t(), Number.t()) :: t()

Link to this function

add_unwalkable_coord(grid, x, y) View Source
add_unwalkable_coord(t(), Number.t(), Number.t()) :: t()

Link to this function

clear_extra_costs(grid) View Source
clear_extra_costs(t()) :: t()

Link to this function

clear_unstoppable_coords(grid) View Source
clear_unstoppable_coords(t()) :: t()

Link to this function

clear_unwalkable_coords(grid) View Source
clear_unwalkable_coords(t()) :: t()

Link to this function

get_coord_cost(grid, x, y) View Source
get_coord_cost(t(), Number.t(), Number.t()) :: Number.t()

Link to this function

get_extra_cost(grid, x, y) View Source
get_extra_cost(t(), Number.t(), Number.t()) :: Number.t()

Link to this function

in_grid?(grid, x, y) View Source
in_grid?(t(), Number.t(), Number.t()) :: Boolean.t()

Link to this function

is_cardinal?(grid) View Source
is_cardinal?(t()) :: Boolean.t()

Link to this function

is_coord_stoppable?(grid, x, y) View Source
is_coord_stoppable?(t(), Number.t(), Number.t()) :: Boolean.t()

Link to this function

is_coord_walkable?(grid, x, y) View Source
is_coord_walkable?(t(), Number.t(), Number.t()) :: Boolean.t()

Link to this function

is_hex?(grid) View Source
is_hex?(t()) :: Boolean.t()

Link to this function

is_intercardinal?(grid) View Source
is_intercardinal?(t()) :: Boolean.t()

Link to this function

remove_extra_cost(grid, x, y) View Source
remove_extra_cost(t(), Number.t(), Number.t()) :: t()

Link to this function

remove_unstoppable_coord(grid, x, y) View Source
remove_unstoppable_coord(t(), Number.t(), Number.t()) :: t()

Link to this function

remove_unwalkable_coord(grid, x, y) View Source
remove_unwalkable_coord(t(), Number.t(), Number.t()) :: t()

Link to this function

set_tile_cost(grid, tile, cost) View Source
set_tile_cost(t(), Number.t(), Number.t()) :: t()

Link to this function

to_coord_map(coords, map \\ %{}, value \\ true) View Source
to_coord_map([%{x: Number.t(), y: Number.t()}, ...], Map.t(), Boolean.t()) ::
  Map.t()

Converts a list of coordinates into a map of nested coordinates.