Skip to content

Commit

Permalink
🚚 Rename Room into Map for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
bal7hazar committed Sep 11, 2024
1 parent 106a150 commit c8a91cd
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 93 deletions.
8 changes: 4 additions & 4 deletions crates/map/src/helpers/spreader.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ mod tests {
let width = 18;
let height = 14;
let grid: felt252 = 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;
let room = Spreader::generate(grid, width, height, 35, SEED);
assert_eq!(room, 0x802800084000A006408008401003008004308C0002410E01080200008120);
let map = Spreader::generate(grid, width, height, 35, SEED);
assert_eq!(map, 0x802800084000A006408008401003008004308C0002410E01080200008120);
}

#[test]
Expand All @@ -121,8 +121,8 @@ mod tests {
let width = 18;
let height = 14;
let grid: felt252 = 0x38000E000380;
let room = Spreader::generate(grid, width, height, 4, SEED);
assert_eq!(room, 0x180000000300);
let map = Spreader::generate(grid, width, height, 4, SEED);
assert_eq!(map, 0x180000000300);
}
}

2 changes: 1 addition & 1 deletion crates/map/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod hex;
pub mod room;
pub mod map;

pub mod types {
pub mod direction;
Expand Down
176 changes: 88 additions & 88 deletions crates/map/src/room.cairo → crates/map/src/map.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Room struct and generation methods.
//! Map struct and generation methods.

// Internal imports

Expand All @@ -13,143 +13,143 @@ use origami_map::helpers::astar::Astar;

/// Types.
#[derive(Copy, Drop)]
pub struct Room {
pub struct Map {
pub width: u8,
pub height: u8,
pub grid: felt252,
pub seed: felt252,
}

/// Implementation of the `RoomTrait` trait for the `Room` struct.
/// Implementation of the `MapTrait` trait for the `Map` struct.
#[generate_trait]
pub impl RoomImpl of RoomTrait {
/// Create a room.
pub impl MapImpl of MapTrait {
/// Create a map.
/// # Arguments
/// * `grid` - The grid of the room
/// * `width` - The width of the room
/// * `height` - The height of the room
/// * `seed` - The seed to generate the room
/// * `grid` - The grid of the map
/// * `width` - The width of the map
/// * `height` - The height of the map
/// * `seed` - The seed to generate the map
/// # Returns
/// * The corresponding room
/// * The corresponding map
#[inline]
fn new(grid: felt252, width: u8, height: u8, seed: felt252) -> Room {
Room { width, height, grid, seed }
fn new(grid: felt252, width: u8, height: u8, seed: felt252) -> Map {
Map { width, height, grid, seed }
}

/// Create an empty room.
/// Create an empty map.
/// # Arguments
/// * `width` - The width of the room
/// * `height` - The height of the room
/// * `seed` - The seed to generate the room
/// * `width` - The width of the map
/// * `height` - The height of the map
/// * `seed` - The seed to generate the map
/// # Returns
/// * The generated room
/// * The generated map
#[inline]
fn new_empty(width: u8, height: u8, seed: felt252) -> Room {
fn new_empty(width: u8, height: u8, seed: felt252) -> Map {
// [Check] Valid dimensions
Asserter::assert_valid_dimension(width, height);
// [Effect] Generate room according to the method
// [Effect] Generate map according to the method
let grid = Private::empty(width, height);
// [Effect] Create room
Room { width, height, grid, seed }
// [Effect] Create map
Map { width, height, grid, seed }
}

/// Create a room with a maze.
/// Create a map with a maze.
/// # Arguments
/// * `width` - The width of the room
/// * `height` - The height of the room
/// * `seed` - The seed to generate the room
/// * `width` - The width of the map
/// * `height` - The height of the map
/// * `seed` - The seed to generate the map
/// # Returns
/// * The generated room
/// * The generated map
#[inline]
fn new_maze(width: u8, height: u8, seed: felt252) -> Room {
fn new_maze(width: u8, height: u8, seed: felt252) -> Map {
let grid = Mazer::generate(width, height, seed);
Room { width, height, grid, seed }
Map { width, height, grid, seed }
}

/// Create a room with a cave.
/// Create a map with a cave.
/// # Arguments
/// * `width` - The width of the room
/// * `height` - The height of the room
/// * `width` - The width of the map
/// * `height` - The height of the map
/// * `order` - The order of the cave
/// * `seed` - The seed to generate the room
/// * `seed` - The seed to generate the map
/// # Returns
/// * The generated room
/// * The generated map
#[inline]
fn new_cave(width: u8, height: u8, order: u8, seed: felt252) -> Room {
fn new_cave(width: u8, height: u8, order: u8, seed: felt252) -> Map {
let grid = Caver::generate(width, height, order, seed);
Room { width, height, grid, seed }
Map { width, height, grid, seed }
}

/// Create a room with a random walk.
/// Create a map with a random walk.
/// # Arguments
/// * `width` - The width of the room
/// * `height` - The height of the room
/// * `width` - The width of the map
/// * `height` - The height of the map
/// * `steps` - The number of steps to walk
/// * `seed` - The seed to generate the room
/// * `seed` - The seed to generate the map
/// # Returns
/// * The generated room
/// * The generated map
#[inline]
fn new_random_walk(width: u8, height: u8, steps: u16, seed: felt252) -> Room {
fn new_random_walk(width: u8, height: u8, steps: u16, seed: felt252) -> Map {
let grid = Walker::generate(width, height, steps, seed);
Room { width, height, grid, seed }
Map { width, height, grid, seed }
}

/// Open the room with a corridor.
/// Open the map with a corridor.
/// # Arguments
/// * `position` - The position of the corridor
/// # Returns
/// * The room with the corridor
/// * The map with the corridor
#[inline]
fn open_with_corridor(ref self: Room, position: u8) {
// [Effect] Add a corridor to open the room
fn open_with_corridor(ref self: Map, position: u8) {
// [Effect] Add a corridor to open the map
self.grid = Digger::corridor(self.width, self.height, position, self.grid, self.seed);
}

/// Open the room with a maze.
/// Open the map with a maze.
/// # Arguments
/// * `position` - The position of the maze
/// # Returns
/// * The room with the maze
/// * The map with the maze
#[inline]
fn open_with_maze(ref self: Room, position: u8) {
// [Effect] Add a maze to open the room
fn open_with_maze(ref self: Map, position: u8) {
// [Effect] Add a maze to open the map
self.grid = Digger::maze(self.width, self.height, position, self.grid, self.seed);
}

/// Compute a distribution of objects in the room.
/// Compute a distribution of objects in the map.
/// # Arguments
/// * `count` - The number of objects to distribute
/// # Returns
/// * The distribution of objects
#[inline]
fn compute_distribution(self: Room, count: u8, seed: felt252) -> felt252 {
fn compute_distribution(self: Map, count: u8, seed: felt252) -> felt252 {
Spreader::generate(self.grid, self.width, self.height, count, seed)
}

/// Search a path in the room.
/// Search a path in the map.
/// # Arguments
/// * `from` - The starting position
/// * `to` - The target position
/// # Returns
/// * The path from the target (incl.) to the start (excl.)
/// * If the path is empty, the target is not reachable
#[inline]
fn search_path(self: Room, from: u8, to: u8) -> Span<u8> {
fn search_path(self: Map, from: u8, to: u8) -> Span<u8> {
Astar::search(self.grid, self.width, self.height, from, to)
}
}

#[generate_trait]
impl Private of PrivateTrait {
/// Generate an empty room.
/// Generate an empty map.
/// # Arguments
/// * `width` - The width of the room
/// * `height` - The height of the room
/// * `width` - The width of the map
/// * `height` - The height of the map
/// # Returns
/// * The generated empty room
/// * The generated empty map
#[inline]
fn empty(width: u8, height: u8) -> felt252 {
// [Effect] Generate empty room
// [Effect] Generate empty map
let offset: u256 = TwoPower::pow(width);
let row: felt252 = ((offset - 1) / 2).try_into().unwrap() - 1; // Remove head and tail bits
let offset: felt252 = offset.try_into().unwrap();
Expand All @@ -171,27 +171,27 @@ impl Private of PrivateTrait {
mod tests {
// Local imports

use super::{Room, RoomTrait};
use super::{Map, MapTrait};
use origami_map::helpers::seeder::Seeder;

// Constants

const SEED: felt252 = 'S33D';

#[test]
fn test_room_new() {
fn test_map_new() {
let width = 18;
let height = 14;
let grid = 0x1FFFE7FFF9FFFE7FFF9FFFE7FFF9FFFE7FFF9FFFE7FFF9FFFE7FFF80002;
let room: Room = RoomTrait::new(grid, width, height, SEED);
assert_eq!(room.width, width);
assert_eq!(room.height, height);
assert_eq!(room.grid, grid);
assert_eq!(room.seed, SEED);
let map: Map = MapTrait::new(grid, width, height, SEED);
assert_eq!(map.width, width);
assert_eq!(map.height, height);
assert_eq!(map.grid, grid);
assert_eq!(map.seed, SEED);
}

#[test]
fn test_room_new_empty() {
fn test_map_new_empty() {
// 000000000000000000
// 011111111111111110
// 011111111111111110
Expand All @@ -208,13 +208,13 @@ mod tests {
// 000000000000000010
let width = 18;
let height = 14;
let mut room: Room = RoomTrait::new_empty(width, height, SEED);
room.open_with_corridor(1);
assert_eq!(room.grid, 0x1FFFE7FFF9FFFE7FFF9FFFE7FFF9FFFE7FFF9FFFE7FFF9FFFE7FFF80002);
let mut map: Map = MapTrait::new_empty(width, height, SEED);
map.open_with_corridor(1);
assert_eq!(map.grid, 0x1FFFE7FFF9FFFE7FFF9FFFE7FFF9FFFE7FFF9FFFE7FFF9FFFE7FFF80002);
}

#[test]
fn test_room_maze() {
fn test_map_maze() {
// 000000000000000000
// 010111011111110110
// 011101101000011100
Expand All @@ -231,13 +231,13 @@ mod tests {
// 000000000000000010
let width = 18;
let height = 14;
let mut room: Room = RoomTrait::new_maze(width, height, SEED);
room.open_with_corridor(1);
assert_eq!(room.grid, 0x177F676870B6EA33279B9EA59D69BAD623668F6B6673116B5C77BD80002);
let mut map: Map = MapTrait::new_maze(width, height, SEED);
map.open_with_corridor(1);
assert_eq!(map.grid, 0x177F676870B6EA33279B9EA59D69BAD623668F6B6673116B5C77BD80002);
}

#[test]
fn test_room_cave() {
fn test_map_cave() {
// 000000000000000000
// 001100001100000000
// 011111001100000000
Expand All @@ -256,13 +256,13 @@ mod tests {
let height = 14;
let order = 3;
let seed: felt252 = Seeder::shuffle(SEED, SEED);
let mut room: Room = RoomTrait::new_cave(width, height, order, seed);
room.open_with_corridor(1);
assert_eq!(room.grid, 0xC3007CC01F1867E719F8C07E001FC007FC01FFC07FF98FFFE3FFF80002);
let mut map: Map = MapTrait::new_cave(width, height, order, seed);
map.open_with_corridor(1);
assert_eq!(map.grid, 0xC3007CC01F1867E719F8C07E001FC007FC01FFC07FF98FFFE3FFF80002);
}

#[test]
fn test_room_random_walk() {
fn test_map_random_walk() {
// 010000000000000000
// 010000000011000000
// 011000000111001100
Expand All @@ -280,13 +280,13 @@ mod tests {
let width = 18;
let height = 14;
let steps: u16 = 2 * width.into() * height.into();
let mut room: Room = RoomTrait::new_random_walk(width, height, steps, SEED);
room.open_with_maze(250);
assert_eq!(room.grid, 0x4000100C060730D1FE6E3F8FFFE69FF8A77E6FFF93FFE4FFF9BFE037F800000);
let mut map: Map = MapTrait::new_random_walk(width, height, steps, SEED);
map.open_with_maze(250);
assert_eq!(map.grid, 0x4000100C060730D1FE6E3F8FFFE69FF8A77E6FFF93FFE4FFF9BFE037F800000);
}

#[test]
fn test_room_compute_distribution() {
fn test_map_compute_distribution() {
// 000000000000000000
// 000000000011000000
// 000000000111001100
Expand Down Expand Up @@ -319,13 +319,13 @@ mod tests {
let width = 18;
let height = 14;
let steps: u16 = 2 * width.into() * height.into();
let mut room: Room = RoomTrait::new_random_walk(width, height, steps, SEED);
let distribution = room.compute_distribution(10, SEED);
let mut map: Map = MapTrait::new_random_walk(width, height, steps, SEED);
let distribution = map.compute_distribution(10, SEED);
assert_eq!(distribution, 0x8021002008000000000001200100000000420000000000);
}

#[test]
fn test_room_search_path() {
fn test_map_search_path() {
// 000000000000000000
// 000000000011000000
// 000000000111001100
Expand All @@ -343,8 +343,8 @@ mod tests {
let width = 18;
let height = 14;
let steps: u16 = 2 * width.into() * height.into();
let mut room: Room = RoomTrait::new_random_walk(width, height, steps, SEED);
let path = room.search_path(23, 133);
let mut map: Map = MapTrait::new_random_walk(width, height, steps, SEED);
let path = map.search_path(23, 133);
assert_eq!(path, array![133, 132, 131, 113, 95, 77, 59, 41].span());
}
}

0 comments on commit c8a91cd

Please sign in to comment.