...

/

Calling Cellular Automata Maps

Calling Cellular Automata Maps

Learn how to place the adventurer and call cellular automata maps.

Place the adventurer

In the room-based architecture, placing the adventurer was easy. We simply put them in the first room. But now, there are no clearly defined rooms with this algorithm, so we have to use a different approach. In this case, we’ll find all of the floor tiles and determine their distance to the center. We’ll then start the player in the closest open tile to the center. Add another function to CellularAutomataArchitect:

Press + to interact
fn find_start(&self, map: &Map) -> Point {
let center = Point::new(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
let closest_point = map.tiles
.iter()
.enumerate()
.filter(|(_, t)| **t == TileType::Floor)
.map(|(idx, _)| (idx, DistanceAlg::Pythagoras.distance2d(
center,
map.index_to_point2d(idx)
)))
.min_by(|(_, distance), (_, distance2)|
distance.partial_cmp(&distance2).unwrap()
)
.map(|(idx, _)| idx)
.unwrap();
map.index_to_point2d(closest_point)
}
  • Line 2: This stores the center of the map in a Point.

  • Line 4: This iterates all map tiles.

  • Line 5: This calls enumerate() to append the tile’s index in the tile vector to the result. Each iteration now contains a tuple of (index, ...