From a3b46886080d74b8ad6552467d378d3ce4c54002 Mon Sep 17 00:00:00 2001 From: Llywelwyn Date: Fri, 21 Jul 2023 11:11:54 +0100 Subject: [PATCH] prefab sectionals and searching on wait --- src/map_builders/prefab_builder/mod.rs | 51 +++++++++++++++----------- src/player.rs | 7 +++- 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/src/map_builders/prefab_builder/mod.rs b/src/map_builders/prefab_builder/mod.rs index 56a805e..4456652 100644 --- a/src/map_builders/prefab_builder/mod.rs +++ b/src/map_builders/prefab_builder/mod.rs @@ -20,7 +20,7 @@ pub struct PrefabBuilder { depth: i32, history: Vec, mode: PrefabMode, - spawns: Vec<(usize, String)>, + spawn_list: Vec<(usize, String)>, previous_builder: Option>, } @@ -28,11 +28,6 @@ impl MapBuilder for PrefabBuilder { fn build_map(&mut self, rng: &mut RandomNumberGenerator) { return self.build(rng); } - fn spawn_entities(&mut self, ecs: &mut World) { - for entity in self.spawns.iter() { - spawner::spawn_entity(ecs, &(&entity.0, &entity.1)); - } - } // Getters fn get_map(&mut self) -> Map { return self.map.clone(); @@ -40,6 +35,9 @@ impl MapBuilder for PrefabBuilder { fn get_starting_pos(&mut self) -> Position { return self.starting_position.clone(); } + fn get_spawn_list(&self) -> &Vec<(usize, String)> { + return &self.spawn_list; + } // Mapgen visualisation stuff fn get_snapshot_history(&self) -> Vec { return self.history.clone(); @@ -64,7 +62,7 @@ impl PrefabBuilder { depth: new_depth, history: Vec::new(), mode: PrefabMode::Sectional { section: prefab_sections::UNDERGROUND_FORT }, - spawns: Vec::new(), + spawn_list: Vec::new(), previous_builder, } } @@ -98,15 +96,7 @@ impl PrefabBuilder { } pub fn apply_sectional(&mut self, section: &prefab_sections::PrefabSection, rng: &mut RandomNumberGenerator) { - // Build the map - let prev_builder = self.previous_builder.as_mut().unwrap(); - prev_builder.build_map(rng); - self.starting_position = prev_builder.get_starting_pos(); - self.map = prev_builder.get_map().clone(); - self.take_snapshot(); - use prefab_sections::*; - let string_vec = PrefabBuilder::read_ascii_to_vec(section.template); // Place the new section @@ -123,7 +113,26 @@ impl PrefabBuilder { VerticalPlacement::Center => chunk_y = (self.map.height / 2) - (section.height as i32 / 2), VerticalPlacement::Bottom => chunk_y = (self.map.height - 1) - section.height as i32, } - println!("{},{}", chunk_x, chunk_y); + + // Build the map + let prev_builder = self.previous_builder.as_mut().unwrap(); + prev_builder.build_map(rng); + self.starting_position = prev_builder.get_starting_pos(); + self.map = prev_builder.get_map().clone(); + // Iterate previous spawn list, culling entities within new section + for entity in prev_builder.get_spawn_list().iter() { + let idx = entity.0; + let x = idx as i32 % self.map.width; + let y = idx as i32 / self.map.width; + if x < chunk_x + || x > (chunk_x + section.width as i32) + || y < chunk_y + || y > (chunk_y + section.height as i32) + { + self.spawn_list.push((idx, entity.1.to_string())); + } + } + self.take_snapshot(); let mut i = 0; for ty in 0..section.height { @@ -151,23 +160,23 @@ impl PrefabBuilder { } 'g' => { self.map.tiles[idx] = TileType::Floor; - self.spawns.push((idx, "goblin".to_string())); + self.spawn_list.push((idx, "goblin".to_string())); } 'o' => { self.map.tiles[idx] = TileType::Floor; - self.spawns.push((idx, "orc".to_string())); + self.spawn_list.push((idx, "orc".to_string())); } '^' => { self.map.tiles[idx] = TileType::Floor; - self.spawns.push((idx, "bear trap".to_string())); + self.spawn_list.push((idx, "bear trap".to_string())); } '%' => { self.map.tiles[idx] = TileType::Floor; - self.spawns.push((idx, "rations".to_string())); + self.spawn_list.push((idx, "rations".to_string())); } '!' => { self.map.tiles[idx] = TileType::Floor; - self.spawns.push((idx, "health potion".to_string())); + self.spawn_list.push((idx, "health potion".to_string())); } _ => { rltk::console::log(format!("Unknown glyph loading map: {}", (ch as u8) as char)); diff --git a/src/player.rs b/src/player.rs index 6e01b53..0640754 100644 --- a/src/player.rs +++ b/src/player.rs @@ -193,7 +193,7 @@ pub fn try_next_level(ecs: &mut World) -> bool { fn skip_turn(ecs: &mut World) -> bool { let player_entity = ecs.fetch::(); - let viewshed_components = ecs.read_storage::(); + let mut viewsheds = ecs.write_storage::(); let monsters = ecs.read_storage::(); let worldmap_resource = ecs.fetch::(); let hunger_clocks = ecs.read_storage::(); @@ -202,7 +202,7 @@ fn skip_turn(ecs: &mut World) -> bool { let mut can_heal = true; // Check viewshed for monsters nearby. If we can see a monster, we can't heal. - let viewshed = viewshed_components.get(*player_entity).unwrap(); + let viewshed = viewsheds.get_mut(*player_entity).unwrap(); for tile in viewshed.visible_tiles.iter() { let idx = worldmap_resource.xy_idx(tile.x, tile.y); for entity_id in worldmap_resource.tile_content[idx].iter() { @@ -215,6 +215,8 @@ fn skip_turn(ecs: &mut World) -> bool { } } } + // Dirty viewshed (so we search for hidden tiles whenever we wait) + viewshed.dirty = true; // Check player's hunger state - if we're hungry or worse, we can't heal. let player_hunger_clock = hunger_clocks.get(*player_entity); @@ -244,6 +246,7 @@ fn skip_turn(ecs: &mut World) -> bool { } else { gamelog::Logger::new().append("You wait a turn.").log(); } + return true; }