Trait movingai::Map2D

source ·
pub trait Map2D<T> {
    // Required methods
    fn height(&self) -> usize;
    fn width(&self) -> usize;
    fn get(&self, coords: Coords2D) -> &T;
    fn is_out_of_bound(&self, coords: Coords2D) -> bool;
    fn is_traversable(&self, tile: Coords2D) -> bool;
    fn is_traversable_from(&self, from: Coords2D, to: Coords2D) -> bool;
    fn coords(&self) -> CoordsIter ;
    fn free_states(&self) -> usize;
    fn neighbors(&self, tile: Coords2D) -> Vec<Coords2D>;
}
Expand description

Contains data structure for 2D MovingAI maps. A trait representing common operations that can be performed on 2D Maps representations.

Required Methods§

source

fn height(&self) -> usize

Every Map2D must have an height.

source

fn width(&self) -> usize

Every Map2D must have a width.

source

fn get(&self, coords: Coords2D) -> &T

In every Map2D must be possible to get a tile.

§Arguments:
  • coords (Coords2D) : A tuple representing the desired coordinates.
§Examples:
use movingai::Map2D;
use movingai::MovingAiMap;

let mm = MovingAiMap::new_from_slice(
       String::from("test"),
       54,
       56,
       Box::new(['.'; 54*56])
   );
let result = mm.get((23,4));
assert_eq!(*result, '.')
source

fn is_out_of_bound(&self, coords: Coords2D) -> bool

Check if the given coordinates are out of bound.

§Examples
assert!(mm.is_out_of_bound((76,3)));
assert!(!mm.is_out_of_bound((23,23)));
source

fn is_traversable(&self, tile: Coords2D) -> bool

Check if a tile in the map can be traversed.

This check if a tile can be traversed by an agent in some situation. For instance, a water tile W is traversable if coming from another water tile, so this function will return true.

The only things that can not be traversed are trees (T), out of bounds, and other unpassable obstacles (@ and `O``).

source

fn is_traversable_from(&self, from: Coords2D, to: Coords2D) -> bool

Check if a tile in the map can be traversed coming from the from tile.

§Arguments
  • from The tile from which the agent starts moving.
  • to The destination tile.
§Details

For instance, in MovingAIMap the implementation encodes all the MovingAI rules about traversability.

In particular:

  • A water tile (W) can be traversed but only if the agent does not comes from regular terrain (. and G).
  • A swamp tile (S) can be traversed only if the agent comes from regular terrain.

For example, I can move from W to W or form W to ., but not from . to W. Or I can move from . to S or from S to ., or from S to S but not from S to W (and vice versa).

source

fn coords(&self) -> CoordsIter

Return an iterator returning all the coordinates in the map in row-major order.

source

fn free_states(&self) -> usize

Return the number of free states of a map.

For “free state” we means any tile that can potentially be traversed.

source

fn neighbors(&self, tile: Coords2D) -> Vec<Coords2D>

Return the list of accessible neighbors of a tile.

Implementors§