# misc/grid (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/misc/grid/

Grid utility.
- A port of the original misc/grid.js. Gathers things rebuilt in every grid game, like connectivity checks (flood fill) and tile collision checks.
- Whether a cell is passable is decided by the is_passable_handler(column, row) callback. All functions are static.
- Differences from the original:
  The original { column, row } object is returned as GridCell, and the { x, y } of to_world_center as Vector2.
  Cell indexes are i32 so negative cells (outside the top left of the world) can be represented.
  Coordinates and cell math are f64 like the original (JS number). (the 0.000001 subtraction in test_rect_overlap matches the original)

## GridCell (struct) (Clone, Copy, Debug, Default, PartialEq, Eq, Hash)

Cell coordinate. (original { column, row })

Example: Convert between world and cell coordinates

```rust
use vanilla::{Grid, GridCell, Vector2};

let cell = Grid::to_cell(70.0, 31.9, 32.0);
assert_eq!(cell, GridCell::create(2, 0));
assert_eq!(Grid::to_world_center(cell.column, cell.row, 32.0), Vector2::create(80.0, 16.0));
assert_eq!(Grid::to_cell(-1.0, 0.0, 32.0), GridCell::create(-1, 0));
```

- `pub column: i32` (field, since 0.3.2): Horizontal cell index. (left is 0, outside is negative)
  - returns i32
- `pub row: i32` (field, since 0.3.2): Vertical cell index. (top is 0, outside is negative)
  - returns i32
- `pub const fn create(column: i32, row: i32) -> GridCell` (static, since 0.3.2): Constructor.
  - `column` i32: Horizontal cell index.
  - `row` i32: Vertical cell index.
  - returns GridCell: That cell coordinate.

## Grid (struct)

Grid utility. (a set of static functions)

Example: Count regions separated by walls

```rust
use vanilla::{Grid, GridCell};

let map_rows = ["..#..", "..#..", "###..", "....."];
let is_passable = |column: i32, row: i32| map_rows[row as usize].as_bytes()[column as usize] != b'#';

let top_left_area = Grid::flood_fill(5, 4, GridCell::create(0, 0), is_passable);
assert_eq!(top_left_area.len(), 4);
assert_eq!(top_left_area[0], GridCell::create(0, 0));

assert!(Grid::is_all_connected(5, 4, GridCell::create(4, 3), is_passable, 11));
assert!(Grid::flood_fill(5, 4, GridCell::create(2, 0), is_passable).is_empty());
```

- `pub fn flood_fill(column_count: i32, row_count: i32, start: GridCell, mut is_passable_handler: impl FnMut(i32, i32) -> bool) -> Vec<GridCell>` (static, since 0.3.2): Flood fill. (returns every passable cell connected up, down, left, and right from the start cell, in visiting order)
  - The callback is only called with cells inside the grid. Runs with a stack, so the order is depth first.
  - `column_count` i32: Number of columns.
  - `row_count` i32: Number of rows.
  - `start` GridCell: Start cell.
  - `is_passable_handler` impl FnMut(i32, i32) -> bool: Function that returns true if the (column, row) cell is passable.
  - returns Vec<GridCell>: List of reached cells. (start cell first) An empty list if the start cell is outside the grid or blocked.
- `pub fn is_all_connected(column_count: i32, row_count: i32, start: GridCell, is_passable_handler: impl FnMut(i32, i32) -> bool, expected_count: usize) -> bool` (static, since 0.3.2): Connectivity check. (whether the number of cells reached from the start cell equals expected_count. e.g. whether every part is connected to the core)
  - `column_count` i32: Number of columns.
  - `row_count` i32: Number of rows.
  - `start` GridCell: Start cell. (e.g. the core position)
  - `is_passable_handler` impl FnMut(i32, i32) -> bool: Function that returns true if the (column, row) cell is passable.
  - `expected_count` usize: Number of cells that should be reached if everything is connected. (e.g. the part count)
  - returns bool: true if the number of cells reached by flood_fill equals expected_count.
- `pub fn test_rect_overlap(world_rect: Rect, tile_size: f64, mut is_blocked_handler: impl FnMut(i32, i32) -> bool) -> bool` (static, since 0.3.2): Rectangle-tile collision test. (walks the tiles a world rectangle spans and returns true if it overlaps a blocked tile)
  - Used in movement checks to "cancel if the position after moving overlaps an obstacle".
  - If the right or bottom edge lands exactly on a tile boundary, the next tile is not spanned. Asks top to bottom, left to right, and stops at the first blocked tile.
  - `world_rect` Rect: World rectangle to test.
  - `tile_size` f64: Length of one side of a tile. (world units)
  - `is_blocked_handler` impl FnMut(i32, i32) -> bool: Function that returns true if the (column, row) tile is blocked. (negative cells are asked too)
  - returns bool: true if any spanned tile is blocked.

  Example: Cancel the move if it overlaps a wall after moving

  ```rust
  use vanilla::{Grid, Rect, Vector2};
  
  let is_blocked = |column: i32, _row: i32| column >= 3;
  let mut hero_position = Vector2::create(40.0, 0.0);
  let hero_size = Vector2::create(24.0, 24.0);
  let next_position = Vector2::create(hero_position.x + 40.0, hero_position.y);
  let next_rect = Rect::create(next_position.x, next_position.y, hero_size.x, hero_size.y);
  if !Grid::test_rect_overlap(next_rect, 32.0, is_blocked) {
  	hero_position = next_position;
  }
  assert_eq!(hero_position, Vector2::create(40.0, 0.0));
  ```

- `pub fn to_cell(world_x: f64, world_y: f64, tile_size: f64) -> GridCell` (static, since 0.3.2): World coordinate → tile coordinate.
  - `world_x` f64: World x.
  - `world_y` f64: World y.
  - `tile_size` f64: Length of one side of a tile. (world units)
  - returns GridCell: The cell containing that point. (rounds down, so negative coordinates give negative cells)
- `pub fn to_world_center(column: i32, row: i32, tile_size: f64) -> Vector2` (static, since 0.3.2): Tile coordinate → world coordinate of the tile center.
  - `column` i32: Horizontal cell index.
  - `row` i32: Vertical cell index.
  - `tile_size` f64: Length of one side of a tile. (world units)
  - returns Vector2: World coordinate of that cell's center.
