From 8a5600267ce44fa7700ad08fa74904bf2e4d612f Mon Sep 17 00:00:00 2001 From: Llywelwyn Date: Fri, 21 Jul 2023 13:35:17 +0100 Subject: [PATCH] flipping vaults --- src/map_builders/prefab_builder/mod.rs | 39 ++++++++++++++++++- .../prefab_builder/prefab_vaults.rs | 39 ++++++++++++++++--- 2 files changed, 71 insertions(+), 7 deletions(-) diff --git a/src/map_builders/prefab_builder/mod.rs b/src/map_builders/prefab_builder/mod.rs index 6f23403..b4d6b65 100644 --- a/src/map_builders/prefab_builder/mod.rs +++ b/src/map_builders/prefab_builder/mod.rs @@ -188,6 +188,31 @@ impl PrefabBuilder { (rng.roll_dice(1, possible_vaults.len() as i32) - 1) as usize }; let vault = possible_vaults[vault_idx]; + // Decide if we want to flip the vault + let mut flip_x: bool = false; + let mut flip_y: bool = false; + match vault.can_flip { + // Equal chance at every orientation + Flipping::None => {} + Flipping::Horizontal => { + flip_x = rng.roll_dice(1, 2) == 1; + } + Flipping::Vertical => { + flip_y = rng.roll_dice(1, 2) == 1; + } + Flipping::Both => { + let roll = rng.roll_dice(1, 4); + match roll { + 1 => {} + 2 => flip_x = true, + 3 => flip_y = true, + _ => { + flip_x = true; + flip_y = true; + } + } + } + } // Make a list of all places the vault can fit let mut vault_positions: Vec = Vec::new(); @@ -248,7 +273,16 @@ impl PrefabBuilder { let mut i = 0; for tile_y in 0..vault.height { for tile_x in 0..vault.width { - let idx = self.map.xy_idx(tile_x as i32 + chunk_x, tile_y as i32 + chunk_y); + let mut x_: i32 = tile_x as i32; + let mut y_: i32 = tile_y as i32; + // Handle flipping + if flip_x { + x_ = vault.height as i32 - 1 - x_; + } + if flip_y { + y_ = vault.width as i32 - 1 - y_; + } + self.map.xy_idx(x_ + chunk_x, y_ as i32 + chunk_y); self.char_to_map(string_vec[i], idx); used_tiles.insert(idx); i += 1; @@ -348,6 +382,9 @@ impl PrefabBuilder { for x in 0..layer.width { let cell = layer.get(x, y).unwrap(); if x < self.map.width as usize && y < self.map.height as usize { + // Saving these for later, for flipping the pref horizontally/vertically/both. + // let flipped_x = (self.map.width - 1) - x as i32; + // let flipped_y = (self.map.height - 1) - y as i32; let idx = self.map.xy_idx(x as i32, y as i32); // We're doing some nasty casting to make it easier to type things like '#' in the match self.char_to_map(cell.ch as u8 as char, idx); diff --git a/src/map_builders/prefab_builder/prefab_vaults.rs b/src/map_builders/prefab_builder/prefab_vaults.rs index b561a82..2f0fde2 100644 --- a/src/map_builders/prefab_builder/prefab_vaults.rs +++ b/src/map_builders/prefab_builder/prefab_vaults.rs @@ -1,16 +1,31 @@ #[allow(dead_code)] #[derive(PartialEq, Copy, Clone)] +pub enum Flipping { + None, + Horizontal, + Vertical, + Both, +} +#[allow(dead_code)] +#[derive(PartialEq, Copy, Clone)] pub struct PrefabVault { pub template: &'static str, pub width: usize, pub height: usize, pub first_depth: i32, pub last_depth: i32, + pub can_flip: Flipping, } #[allow(dead_code)] -pub const CLASSIC_TRAP_5X5: PrefabVault = - PrefabVault { template: CLASSIC_TRAP_5X5_V, width: 5, height: 5, first_depth: 0, last_depth: 100 }; +pub const CLASSIC_TRAP_5X5: PrefabVault = PrefabVault { + template: CLASSIC_TRAP_5X5_V, + width: 5, + height: 5, + first_depth: 0, + last_depth: 100, + can_flip: Flipping::None, +}; #[allow(dead_code)] const CLASSIC_TRAP_5X5_V: &str = " @@ -21,8 +36,14 @@ const CLASSIC_TRAP_5X5_V: &str = " "; #[allow(dead_code)] -pub const GOBLINS_4X4: PrefabVault = - PrefabVault { template: GOBLINS_4X4_V, width: 4, height: 4, first_depth: 0, last_depth: 100 }; +pub const GOBLINS_4X4: PrefabVault = PrefabVault { + template: GOBLINS_4X4_V, + width: 4, + height: 4, + first_depth: 0, + last_depth: 100, + can_flip: Flipping::Both, +}; const GOBLINS_4X4_V: &str = " #^   #G# @@ -31,8 +52,14 @@ const GOBLINS_4X4_V: &str = " "; #[allow(dead_code)] -pub const GOBLINS2_4X4: PrefabVault = - PrefabVault { template: GOBLINS2_4X4_V, width: 4, height: 4, first_depth: 0, last_depth: 100 }; +pub const GOBLINS2_4X4: PrefabVault = PrefabVault { + template: GOBLINS2_4X4_V, + width: 4, + height: 4, + first_depth: 0, + last_depth: 100, + can_flip: Flipping::Both, +}; const GOBLINS2_4X4_V: &str = " #^#g G# #