From fd9f8e7463098cf573a61abbc88ca7597a47ca48 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 3 Feb 2022 14:59:35 -0500 Subject: [PATCH] Use new rng module instead of passing around a variable. Completes section 5.31 --- src/damage_system.rs | 8 +- src/effects/damage.rs | 6 +- src/main.rs | 3 +- src/map/dungeon.rs | 29 ++-- src/map_builders.rs | 90 +++++----- src/map_builders/area_ending_point.rs | 8 +- src/map_builders/area_starting_points.rs | 8 +- src/map_builders/bsp_dungeon.rs | 27 ++- src/map_builders/bsp_interior.rs | 33 ++-- src/map_builders/cellular_automata.rs | 13 +- src/map_builders/cull_unreachable.rs | 8 +- src/map_builders/distant_exit.rs | 8 +- src/map_builders/dla.rs | 25 ++- src/map_builders/door_placement.rs | 11 +- src/map_builders/drunkard.rs | 19 +- src/map_builders/dwarf_fort.rs | 23 +-- src/map_builders/forest.rs | 18 +- src/map_builders/limestone_cavern.rs | 44 ++--- src/map_builders/maze.rs | 20 +-- src/map_builders/mushroom_forest.rs | 21 +-- src/map_builders/prefab_builder.rs | 40 ++--- src/map_builders/room_based_spawner.rs | 9 +- src/map_builders/room_based_stairs.rs | 8 +- .../room_based_starting_position.rs | 8 +- src/map_builders/room_corner_rounding.rs | 8 +- src/map_builders/room_corridor_spawner.rs | 10 +- src/map_builders/room_draw.rs | 11 +- src/map_builders/room_exploder.rs | 13 +- src/map_builders/room_sorter.rs | 8 +- src/map_builders/rooms_corridors_bsp.rs | 19 +- src/map_builders/rooms_corridors_dogleg.rs | 11 +- src/map_builders/rooms_corridors_lines.rs | 8 +- src/map_builders/rooms_corridors_nearest.rs | 8 +- src/map_builders/simple_map.rs | 17 +- src/map_builders/town.rs | 164 +++++++----------- src/map_builders/voronoi.rs | 13 +- src/map_builders/voronoi_spawning.rs | 16 +- src/map_builders/waveform_collapse.rs | 9 +- src/map_builders/waveform_collapse/solver.rs | 9 +- src/player.rs | 6 +- src/random_table.rs | 18 +- src/raws/rawmaster.rs | 14 +- src/rng.rs | 4 +- src/spawner.rs | 26 ++- src/systems/ai/default_move_system.rs | 11 +- src/systems/ai/initiative_system.rs | 7 +- src/systems/ai/quipping.rs | 10 +- src/systems/ai/visible_ai_system.rs | 9 +- src/systems/melee_combat_system.rs | 12 +- src/systems/ranged_combat_system.rs | 13 +- src/systems/visibility_system.rs | 19 +- 51 files changed, 392 insertions(+), 568 deletions(-) diff --git a/src/damage_system.rs b/src/damage_system.rs index 5e0103f..e59c3f3 100644 --- a/src/damage_system.rs +++ b/src/damage_system.rs @@ -1,4 +1,3 @@ -use ::rltk::RandomNumberGenerator; use ::specs::prelude::*; use crate::components::{ @@ -6,6 +5,7 @@ use crate::components::{ }; use crate::effects::*; use crate::raws::{self, SpawnType, RAWS}; +use crate::rng::roll_dice; use crate::{colors, gamelog, Map, RunState}; pub fn delete_the_dead(ecs: &mut World) { @@ -49,7 +49,6 @@ pub fn delete_the_dead(ecs: &mut World) { let mut carried = ecs.write_storage::(); let mut positions = ecs.write_storage::(); let loot_tables = ecs.read_storage::(); - let mut rng = ecs.write_resource::(); for victim in dead.iter() { let pos = positions.get(*victim); @@ -72,7 +71,7 @@ pub fn delete_the_dead(ecs: &mut World) { if let Some(table) = loot_tables.get(*victim) { let drop_finder = - raws::get_item_drop(&crate::raws::RAWS.lock().unwrap(), &mut rng, &table.table); + raws::get_item_drop(&crate::raws::RAWS.lock().unwrap(), &table.table); if let Some(tag) = drop_finder { if let Some(pos) = pos { to_spawn.push((tag, *pos)); @@ -108,9 +107,8 @@ pub fn delete_the_dead(ecs: &mut World) { for victim in dead.iter() { let death_effects = ecs.read_storage::(); if let Some(death_effect) = death_effects.get(*victim) { - let mut rng = ecs.fetch_mut::(); for effect in death_effect.abilities.iter() { - if rng.roll_dice(1, 100) <= (effect.chance * 100.0) as i32 { + if roll_dice(1, 100) <= (effect.chance * 100.0) as i32 { let map = ecs.fetch::(); if let Some(pos) = ecs.read_storage::().get(*victim) { let spell_entity = diff --git a/src/effects/damage.rs b/src/effects/damage.rs index af2a01f..c518327 100644 --- a/src/effects/damage.rs +++ b/src/effects/damage.rs @@ -1,4 +1,4 @@ -use ::rltk::{Point, RandomNumberGenerator}; +use ::rltk::Point; use ::specs::prelude::*; use ::specs::saveload::{MarkedBuilder, SimpleMarker}; @@ -7,6 +7,7 @@ use crate::components::{ Attributes, Confusion, DamageOverTime, Duration, Name, Player, Pools, SerializeMe, Skills, Slow, }; use crate::gamesystem::{mana_at_level, player_hp_at_level}; +use crate::rng::roll_dice; use crate::{colors, gamelog, EquipmentChanged, Map, StatusEffect}; pub fn inflict_damage(ecs: &mut World, damage: &EffectSpawner, target: Entity) { @@ -90,8 +91,7 @@ pub fn death(ecs: &mut World, effect: &EffectSpawner, target: Entity) { .log(); // Improve a random attribute - let mut rng = ecs.fetch_mut::(); - match rng.roll_dice(1, 4) { + match roll_dice(1, 4) { 1 => { player_attributes.might.base += 1; gamelog::log_color_line(colors::GREEN, "You feel stronger!"); diff --git a/src/main.rs b/src/main.rs index 62e0c8b..0ee6452 100644 --- a/src/main.rs +++ b/src/main.rs @@ -154,7 +154,7 @@ fn register_components(state: &mut State) { /// * Creates the [`Player`](crate::spawner::player) /// * Generates the first [`map`](crate::state::State::generate_world_map) fn init_state() -> State { - use ::rltk::{Point, RandomNumberGenerator}; + use ::rltk::Point; use systems::particle_system::ParticleBuilder; let mut state = State::new(); @@ -170,7 +170,6 @@ fn init_state() -> State { state.ecs.insert(MasterDungeonMap::new()); state.ecs.insert(Map::new(1, 64, 64, "New Map")); state.ecs.insert(Point::zero()); - state.ecs.insert(RandomNumberGenerator::new()); let player_entity = spawner::player(&mut state.ecs, 0, 0); state.ecs.insert(player_entity); diff --git a/src/map/dungeon.rs b/src/map/dungeon.rs index 6e199f7..8a0c6eb 100644 --- a/src/map/dungeon.rs +++ b/src/map/dungeon.rs @@ -1,7 +1,7 @@ //! Functionality that is common to all the currently generated maps. use std::collections::{HashMap, HashSet}; -use ::rltk::{Point, RandomNumberGenerator}; +use ::rltk::Point; use ::serde::{Deserialize, Serialize}; use ::specs::prelude::*; @@ -9,6 +9,7 @@ use crate::components::{OtherLevelPosition, Position, Viewshed}; use crate::map::{Map, TileType}; use crate::map_builders::level_builder; use crate::raws; +use crate::rng::roll_dice; #[derive(Default, Serialize, Deserialize, Clone)] pub struct MasterDungeonMap { @@ -27,16 +28,15 @@ impl MasterDungeonMap { potion_mappings: HashMap::new(), }; - let mut rng = RandomNumberGenerator::new(); for scroll_tag in raws::get_scroll_tags().iter() { - let masked_name = make_scroll_name(&mut rng); + let masked_name = make_scroll_name(); dm.scroll_mappings .insert(scroll_tag.to_string(), masked_name); } let mut used_potion_names = HashSet::new(); for potion_tag in raws::get_potion_tags().iter() { - let masked_name = make_potion_name(&mut rng, &mut used_potion_names); + let masked_name = make_potion_name(&mut used_potion_names); dm.potion_mappings .insert(potion_tag.to_string(), masked_name); } @@ -57,13 +57,13 @@ impl MasterDungeonMap { } } -fn make_scroll_name(rng: &mut rltk::RandomNumberGenerator) -> String { - let length = 4 + rng.roll_dice(1, 4); +fn make_scroll_name() -> String { + let length = 4 + roll_dice(1, 4); let mut name = "Scroll of ".to_string(); for i in 0..length { if i % 2 == 0 { - name += match rng.roll_dice(1, 5) { + name += match roll_dice(1, 5) { 1 => "a", 2 => "e", 3 => "i", @@ -71,7 +71,7 @@ fn make_scroll_name(rng: &mut rltk::RandomNumberGenerator) -> String { _ => "u", } } else { - name += match rng.roll_dice(1, 21) { + name += match roll_dice(1, 21) { 1 => "b", 2 => "c", 3 => "d", @@ -113,13 +113,12 @@ const POTION_ADJECTIVES: &[&str] = &[ "Glowing", ]; -fn make_potion_name(rng: &mut RandomNumberGenerator, used_names: &mut HashSet) -> String { +fn make_potion_name(used_names: &mut HashSet) -> String { loop { - let mut name = POTION_ADJECTIVES - [rng.roll_dice(1, POTION_ADJECTIVES.len() as i32) as usize - 1] + let mut name = POTION_ADJECTIVES[roll_dice(1, POTION_ADJECTIVES.len() as i32) as usize - 1] .to_string(); name += " "; - name += POTION_COLORS[rng.roll_dice(1, POTION_COLORS.len() as i32) as usize - 1]; + name += POTION_COLORS[roll_dice(1, POTION_COLORS.len() as i32) as usize - 1]; name += " Potion"; if !used_names.contains(&name) { @@ -148,9 +147,8 @@ pub fn level_transition(ecs: &mut World, new_depth: i32, offset: i32) -> Option< } fn transition_to_new_map(ecs: &mut World, new_depth: i32) -> Vec { - let mut rng = ecs.write_resource::(); - let mut builder = level_builder(new_depth, &mut rng, 80, 50); - builder.build_map(&mut rng); + let mut builder = level_builder(new_depth, 80, 50); + builder.build_map(); if new_depth > 1 { if let Some(pos) = &builder.build_data.starting_position { @@ -168,7 +166,6 @@ fn transition_to_new_map(ecs: &mut World, new_depth: i32) -> Vec { } // Spawn bad guys - std::mem::drop(rng); builder.spawn_entities(ecs); // Place the player and update resources diff --git a/src/map_builders.rs b/src/map_builders.rs index 2276a3a..8e7dda3 100644 --- a/src/map_builders.rs +++ b/src/map_builders.rs @@ -34,7 +34,6 @@ mod voronoi; mod voronoi_spawning; mod waveform_collapse; -use ::rltk::RandomNumberGenerator; use ::specs::prelude::*; use area_ending_point::{AreaEndingPosition, XEnd, YEnd}; use area_starting_points::{AreaStartingPosition, XStart, YStart}; @@ -72,6 +71,7 @@ use voronoi::VoronoiCellBuilder; use voronoi_spawning::VoronoiSpawning; use waveform_collapse::WaveformCollapseBuilder; +use crate::rng::roll_dice; use crate::{spawner, Map, Position, Rect, SHOW_MAPGEN_VISUALIZER}; pub struct BuilderMap { @@ -140,18 +140,18 @@ impl BuilderChain { self } - pub fn build_map(&mut self, rng: &mut RandomNumberGenerator) -> &mut Self { + pub fn build_map(&mut self) -> &mut Self { match &mut self.starter { None => panic!("Cannot run a map builder chain without starting a build system"), Some(starter) => { // Build the starting map - starter.build_map(rng, &mut self.build_data); + starter.build_map(&mut self.build_data); } } // Build additional layers in turn for metabuilder in self.builders.iter_mut() { - metabuilder.build_map(rng, &mut self.build_data); + metabuilder.build_map(&mut self.build_data); } self @@ -183,21 +183,21 @@ impl BuilderChain { } pub trait InitialMapBuilder { - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap); + fn build_map(&mut self, build_data: &mut BuilderMap); } pub trait MetaMapBuilder { - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap); + fn build_map(&mut self, build_data: &mut BuilderMap); } -pub fn random_start_position(rng: &mut rltk::RandomNumberGenerator) -> (XStart, YStart) { - let x = match rng.roll_dice(1, 3) { +pub fn random_start_position() -> (XStart, YStart) { + let x = match roll_dice(1, 3) { 1 => XStart::Left, 2 => XStart::Center, _ => XStart::Right, }; - let y = match rng.roll_dice(1, 3) { + let y = match roll_dice(1, 3) { 1 => YStart::Bottom, 2 => YStart::Center, _ => YStart::Top, @@ -206,8 +206,8 @@ pub fn random_start_position(rng: &mut rltk::RandomNumberGenerator) -> (XStart, (x, y) } -fn random_room_builder(rng: &mut RandomNumberGenerator, builder: &mut BuilderChain) { - let build_roll = rng.roll_dice(1, 3); +fn random_room_builder(builder: &mut BuilderChain) { + let build_roll = roll_dice(1, 3); match build_roll { 1 => builder.start_with(SimpleMapBuilder::new()), 2 => builder.start_with(BspDungeonBuilder::new()), @@ -217,7 +217,7 @@ fn random_room_builder(rng: &mut RandomNumberGenerator, builder: &mut BuilderCha // BSP Interior still makes holes in the walls if build_roll != 3 { // Sort by one of the 5 available algorithms - let room_sort = match rng.roll_dice(1, 5) { + let room_sort = match roll_dice(1, 5) { 1 => RoomSort::LeftMost, 2 => RoomSort::RightMost, 3 => RoomSort::TopMost, @@ -229,7 +229,7 @@ fn random_room_builder(rng: &mut RandomNumberGenerator, builder: &mut BuilderCha builder.with(RoomDrawer::new()); // Pick a corridor type - match rng.roll_dice(1, 4) { + match roll_dice(1, 4) { 1 => builder.with(DoglegCorridors::new()), 2 => builder.with(NearestCorridors::new()), 3 => builder.with(StraightLineCorridors::new()), @@ -237,12 +237,12 @@ fn random_room_builder(rng: &mut RandomNumberGenerator, builder: &mut BuilderCha }; // Maybe spawn stuff in the corridor - if rng.roll_dice(1, 2) == 1 { + if roll_dice(1, 2) == 1 { builder.with(CorridorSpawner::new()); } // Add additional modifier - match rng.roll_dice(1, 6) { + match roll_dice(1, 6) { 1 => { builder.with(RoomExploder::new()); } @@ -254,29 +254,29 @@ fn random_room_builder(rng: &mut RandomNumberGenerator, builder: &mut BuilderCha } // Find a starting position - match rng.roll_dice(1, 2) { + match roll_dice(1, 2) { 1 => builder.with(RoomBasedStartingPosition::new()), _ => { - let (start_x, start_y) = random_start_position(rng); + let (start_x, start_y) = random_start_position(); builder.with(AreaStartingPosition::new(start_x, start_y)) } }; // Find an exit - match rng.roll_dice(1, 2) { + match roll_dice(1, 2) { 1 => builder.with(RoomBasedStairs::new()), _ => builder.with(DistantExit::new()), }; // Spawn stuff - match rng.roll_dice(1, 2) { + match roll_dice(1, 2) { 1 => builder.with(RoomBasedSpawner::new()), _ => builder.with(VoronoiSpawning::new()), }; } -fn random_shape_builder(rng: &mut RandomNumberGenerator, builder: &mut BuilderChain) { - let first_builder: Box = match rng.roll_dice(1, 16) { +fn random_shape_builder(builder: &mut BuilderChain) { + let first_builder: Box = match roll_dice(1, 16) { 1 => CellularAutomataBuilder::new(), 2 => DrunkardsWalkBuilder::open_area(), 3 => DrunkardsWalkBuilder::open_halls(), @@ -300,7 +300,7 @@ fn random_shape_builder(rng: &mut RandomNumberGenerator, builder: &mut BuilderCh .with(CullUnreachable::new()); // Now set the start ot a random starting area - let (start_x, start_y) = random_start_position(rng); + let (start_x, start_y) = random_start_position(); builder.with(AreaStartingPosition::new(start_x, start_y)); // Setup an exit and spawn mobs @@ -308,24 +308,19 @@ fn random_shape_builder(rng: &mut RandomNumberGenerator, builder: &mut BuilderCh builder.with(DistantExit::new()); } -pub fn random_builder( - new_depth: i32, - rng: &mut RandomNumberGenerator, - width: i32, - height: i32, -) -> BuilderChain { +pub fn random_builder(new_depth: i32, width: i32, height: i32) -> BuilderChain { let mut builder = BuilderChain::new(new_depth, width, height, "New Map"); - match rng.roll_dice(1, 2) { - 1 => random_room_builder(rng, &mut builder), - _ => random_shape_builder(rng, &mut builder), + match roll_dice(1, 2) { + 1 => random_room_builder(&mut builder), + _ => random_shape_builder(&mut builder), }; - if rng.roll_dice(1, 3) == 1 { + if roll_dice(1, 3) == 1 { builder.with(WaveformCollapseBuilder::new()); // Now set the start to a random starting area - let (start_x, start_y) = random_start_position(rng); + let (start_x, start_y) = random_start_position(); builder.with(AreaStartingPosition::new(start_x, start_y)); // Setup an exit and spawn mobs @@ -334,7 +329,7 @@ pub fn random_builder( .with(DistantExit::new()); } - if rng.roll_dice(1, 20) == 1 { + if roll_dice(1, 20) == 1 { builder.with(PrefabBuilder::sectional( prefab_builder::prefab_sections::UNDERGROUND_FORT, )); @@ -346,23 +341,18 @@ pub fn random_builder( builder } -pub fn level_builder( - new_depth: i32, - rng: &mut RandomNumberGenerator, - width: i32, - height: i32, -) -> BuilderChain { +pub fn level_builder(new_depth: i32, width: i32, height: i32) -> BuilderChain { rltk::console::log(format!("Depth: {}", new_depth)); match new_depth { - 1 => town_builder(new_depth, rng, width, height), - 2 => forest_builder(new_depth, rng, width, height), - 3 => limestone_cavern_builder(new_depth, rng, width, height), - 4 => limestone_deep_cavern_builder(new_depth, rng, width, height), - 5 => limestone_transition_builder(new_depth, rng, width, height), - 6 => dwarf_fort_builder(new_depth, rng, width, height), - 7 => mushroom_entrance(new_depth, rng, width, height), - 8 => mushroom_builder(new_depth, rng, width, height), - 9 => mushroom_exit(new_depth, rng, width, height), - _ => random_builder(new_depth, rng, width, height), + 1 => town_builder(new_depth, width, height), + 2 => forest_builder(new_depth, width, height), + 3 => limestone_cavern_builder(new_depth, width, height), + 4 => limestone_deep_cavern_builder(new_depth, width, height), + 5 => limestone_transition_builder(new_depth, width, height), + 6 => dwarf_fort_builder(new_depth, width, height), + 7 => mushroom_entrance(new_depth, width, height), + 8 => mushroom_builder(new_depth, width, height), + 9 => mushroom_exit(new_depth, width, height), + _ => random_builder(new_depth, width, height), } } diff --git a/src/map_builders/area_ending_point.rs b/src/map_builders/area_ending_point.rs index 7d78a47..4d039f9 100644 --- a/src/map_builders/area_ending_point.rs +++ b/src/map_builders/area_ending_point.rs @@ -1,4 +1,4 @@ -use ::rltk::{DistanceAlg, Point, RandomNumberGenerator}; +use ::rltk::{DistanceAlg, Point}; use super::{BuilderMap, MetaMapBuilder}; use crate::{map, TileType}; @@ -23,8 +23,8 @@ pub struct AreaEndingPosition { } impl MetaMapBuilder for AreaEndingPosition { - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - self.build(rng, build_data); + fn build_map(&mut self, build_data: &mut BuilderMap) { + self.build(build_data); } } @@ -34,7 +34,7 @@ impl AreaEndingPosition { Box::new(AreaEndingPosition { x, y }) } - fn build(&mut self, _rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + fn build(&mut self, build_data: &mut BuilderMap) { let seed_x = match self.x { XEnd::Left => 1, XEnd::Center => build_data.map.width / 2, diff --git a/src/map_builders/area_starting_points.rs b/src/map_builders/area_starting_points.rs index e832655..0c9b6d0 100644 --- a/src/map_builders/area_starting_points.rs +++ b/src/map_builders/area_starting_points.rs @@ -1,5 +1,3 @@ -use ::rltk::RandomNumberGenerator; - use crate::map_builders::{BuilderMap, MetaMapBuilder}; use crate::{map, Position}; @@ -23,8 +21,8 @@ pub struct AreaStartingPosition { } impl MetaMapBuilder for AreaStartingPosition { - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - self.build(rng, build_data); + fn build_map(&mut self, build_data: &mut BuilderMap) { + self.build(build_data); } } @@ -34,7 +32,7 @@ impl AreaStartingPosition { Box::new(AreaStartingPosition { x, y }) } - fn build(&mut self, _rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + fn build(&mut self, build_data: &mut BuilderMap) { let seed_x = match self.x { XStart::Left => 1, XStart::Center => build_data.map.width / 2, diff --git a/src/map_builders/bsp_dungeon.rs b/src/map_builders/bsp_dungeon.rs index 05a7212..a480a1e 100644 --- a/src/map_builders/bsp_dungeon.rs +++ b/src/map_builders/bsp_dungeon.rs @@ -1,6 +1,5 @@ -use ::rltk::RandomNumberGenerator; - use crate::map_builders::{BuilderMap, InitialMapBuilder}; +use crate::rng::roll_dice; use crate::{Rect, TileType}; pub struct BspDungeonBuilder { @@ -9,8 +8,8 @@ pub struct BspDungeonBuilder { impl InitialMapBuilder for BspDungeonBuilder { #[allow(dead_code)] - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - self.build(rng, build_data); + fn build_map(&mut self, build_data: &mut BuilderMap) { + self.build(build_data); } } @@ -19,7 +18,7 @@ impl BspDungeonBuilder { Box::new(BspDungeonBuilder { rects: Vec::new() }) } - fn build(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + fn build(&mut self, build_data: &mut BuilderMap) { let mut rooms: Vec = Vec::new(); self.rects.clear(); @@ -37,8 +36,8 @@ impl BspDungeonBuilder { // a room in there, we place it and add it to the rooms list. let mut n_rooms = 0; while n_rooms < 240 { - let rect = self.get_random_rect(rng); - let candidate = self.get_random_sub_rect(rect, rng); + let rect = self.get_random_rect(); + let candidate = self.get_random_sub_rect(rect); if self.is_possible(candidate, build_data, &rooms) { rooms.push(candidate); @@ -79,25 +78,25 @@ impl BspDungeonBuilder { )); } - fn get_random_rect(&mut self, rng: &mut RandomNumberGenerator) -> Rect { + fn get_random_rect(&mut self) -> Rect { if self.rects.len() == 1 { return self.rects[0]; } - let idx = (rng.roll_dice(1, self.rects.len() as i32) - 1) as usize; + let idx = (roll_dice(1, self.rects.len() as i32) - 1) as usize; self.rects[idx] } - fn get_random_sub_rect(&self, rect: Rect, rng: &mut RandomNumberGenerator) -> Rect { + fn get_random_sub_rect(&self, rect: Rect) -> Rect { let mut result = rect; let rect_width = i32::abs(rect.x1 - rect.x2); let rect_height = i32::abs(rect.y1 - rect.y2); - let w = i32::max(3, rng.roll_dice(1, i32::min(rect_width, 10)) - 1) + 1; - let h = i32::max(3, rng.roll_dice(1, i32::min(rect_height, 10)) - 1) + 1; + let w = i32::max(3, roll_dice(1, i32::min(rect_width, 10)) - 1) + 1; + let h = i32::max(3, roll_dice(1, i32::min(rect_height, 10)) - 1) + 1; - result.x1 += rng.roll_dice(1, 6) - 1; - result.y1 += rng.roll_dice(1, 6) - 1; + result.x1 += roll_dice(1, 6) - 1; + result.y1 += roll_dice(1, 6) - 1; result.x2 = result.x1 + w; result.y2 = result.y1 + h; diff --git a/src/map_builders/bsp_interior.rs b/src/map_builders/bsp_interior.rs index 9db6689..b363af9 100644 --- a/src/map_builders/bsp_interior.rs +++ b/src/map_builders/bsp_interior.rs @@ -1,7 +1,6 @@ -use ::rltk::RandomNumberGenerator; - use crate::map_builders::common::draw_corridor; use crate::map_builders::{BuilderMap, InitialMapBuilder}; +use crate::rng::roll_dice; use crate::{Rect, TileType}; const MIN_ROOM_SIZE: i32 = 8; @@ -12,8 +11,8 @@ pub struct BspInteriorBuilder { impl InitialMapBuilder for BspInteriorBuilder { #[allow(dead_code)] - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - self.build(rng, build_data); + fn build_map(&mut self, build_data: &mut BuilderMap) { + self.build(build_data); } } @@ -22,7 +21,7 @@ impl BspInteriorBuilder { Box::new(BspInteriorBuilder { rects: Vec::new() }) } - fn build(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + fn build(&mut self, build_data: &mut BuilderMap) { let mut rooms: Vec = Vec::new(); self.rects.clear(); @@ -35,7 +34,7 @@ impl BspInteriorBuilder { )); let first_room = self.rects[0]; - self.add_subrects(first_room, rng); // Divide the first room + self.add_subrects(first_room); // Divide the first room let rooms_copy = self.rects.clone(); for r in rooms_copy.iter() { @@ -60,12 +59,10 @@ impl BspInteriorBuilder { for i in 0..rooms.len() - 1 { let room = rooms[i]; let next_room = rooms[i + 1]; - let start_x = room.x1 + (rng.roll_dice(1, i32::abs(room.x1 - room.x2)) - 1); - let start_y = room.y1 + (rng.roll_dice(1, i32::abs(room.y1 - room.y2) - 1)); - let end_x = - next_room.x1 + (rng.roll_dice(1, i32::abs(next_room.x1 - next_room.x2)) - 1); - let end_y = - next_room.y1 + (rng.roll_dice(1, i32::abs(next_room.y1 - next_room.y2)) - 1); + let start_x = room.x1 + (roll_dice(1, i32::abs(room.x1 - room.x2)) - 1); + let start_y = room.y1 + (roll_dice(1, i32::abs(room.y1 - room.y2) - 1)); + let end_x = next_room.x1 + (roll_dice(1, i32::abs(next_room.x1 - next_room.x2)) - 1); + let end_y = next_room.y1 + (roll_dice(1, i32::abs(next_room.y1 - next_room.y2)) - 1); draw_corridor(&mut build_data.map, start_x, start_y, end_x, end_y); build_data.take_snapshot(); @@ -74,7 +71,7 @@ impl BspInteriorBuilder { build_data.rooms = Some(rooms); } - fn add_subrects(&mut self, rect: Rect, rng: &mut RandomNumberGenerator) { + fn add_subrects(&mut self, rect: Rect) { // Remove the last rect from the list if !self.rects.is_empty() { self.rects.remove(self.rects.len() - 1); @@ -86,33 +83,33 @@ impl BspInteriorBuilder { let half_width = width / 2; let half_height = height / 2; - let split = rng.roll_dice(1, 4); + let split = roll_dice(1, 4); if split <= 2 { // Horizontal split let h1 = Rect::new(rect.x1, rect.y1, half_width - 1, height); self.rects.push(h1); if half_width > MIN_ROOM_SIZE { - self.add_subrects(h1, rng); + self.add_subrects(h1); } let h2 = Rect::new(rect.x1 + half_width, rect.y1, half_width, height); self.rects.push(h2); if half_width > MIN_ROOM_SIZE { - self.add_subrects(h2, rng); + self.add_subrects(h2); } } else { // Vertical split let v1 = Rect::new(rect.x1, rect.y1, width, half_height - 1); self.rects.push(v1); if half_height > MIN_ROOM_SIZE { - self.add_subrects(v1, rng); + self.add_subrects(v1); } let v2 = Rect::new(rect.x1, rect.y1 + half_height, width, half_height); self.rects.push(v2); if half_height > MIN_ROOM_SIZE { - self.add_subrects(v2, rng); + self.add_subrects(v2); } } } diff --git a/src/map_builders/cellular_automata.rs b/src/map_builders/cellular_automata.rs index 88ced9b..15581fc 100644 --- a/src/map_builders/cellular_automata.rs +++ b/src/map_builders/cellular_automata.rs @@ -1,20 +1,19 @@ -use ::rltk::RandomNumberGenerator; - use crate::map_builders::{BuilderMap, InitialMapBuilder, MetaMapBuilder}; +use crate::rng::roll_dice; use crate::TileType; pub struct CellularAutomataBuilder {} impl InitialMapBuilder for CellularAutomataBuilder { #[allow(dead_code)] - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - self.build(rng, build_data); + fn build_map(&mut self, build_data: &mut BuilderMap) { + self.build(build_data); } } impl MetaMapBuilder for CellularAutomataBuilder { #[allow(dead_code)] - fn build_map(&mut self, _rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + fn build_map(&mut self, build_data: &mut BuilderMap) { self.apply_iteration(build_data); } } @@ -73,11 +72,11 @@ impl CellularAutomataBuilder { build_data.take_snapshot(); } - fn build(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + fn build(&mut self, build_data: &mut BuilderMap) { // First we completely randomize the map, setting 55% of it to be floor. for y in 1..build_data.map.height - 1 { for x in 1..build_data.map.width - 1 { - let roll = rng.roll_dice(1, 100); + let roll = roll_dice(1, 100); let idx = build_data.map.xy_idx(x, y); if roll > 55 { diff --git a/src/map_builders/cull_unreachable.rs b/src/map_builders/cull_unreachable.rs index 85d7656..dae2ec1 100644 --- a/src/map_builders/cull_unreachable.rs +++ b/src/map_builders/cull_unreachable.rs @@ -1,13 +1,11 @@ -use ::rltk::RandomNumberGenerator; - use crate::map_builders::{BuilderMap, MetaMapBuilder}; use crate::TileType; pub struct CullUnreachable {} impl MetaMapBuilder for CullUnreachable { - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - self.build(rng, build_data); + fn build_map(&mut self, build_data: &mut BuilderMap) { + self.build(build_data); } } @@ -17,7 +15,7 @@ impl CullUnreachable { Box::new(CullUnreachable {}) } - fn build(&mut self, _rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + fn build(&mut self, build_data: &mut BuilderMap) { let starting_pos = *build_data.starting_position.as_ref().unwrap(); let start_idx = build_data.map.xy_idx(starting_pos.x, starting_pos.y); diff --git a/src/map_builders/distant_exit.rs b/src/map_builders/distant_exit.rs index 915b416..7bea5b1 100644 --- a/src/map_builders/distant_exit.rs +++ b/src/map_builders/distant_exit.rs @@ -1,13 +1,11 @@ -use ::rltk::RandomNumberGenerator; - use crate::map_builders::{BuilderMap, MetaMapBuilder}; use crate::TileType; pub struct DistantExit {} impl MetaMapBuilder for DistantExit { - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - self.build(rng, build_data); + fn build_map(&mut self, build_data: &mut BuilderMap) { + self.build(build_data); } } @@ -17,7 +15,7 @@ impl DistantExit { Box::new(DistantExit {}) } - fn build(&mut self, _rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + fn build(&mut self, build_data: &mut BuilderMap) { let starting_pos = *build_data.starting_position.as_ref().unwrap(); let start_idx = build_data.map.xy_idx(starting_pos.x, starting_pos.y); diff --git a/src/map_builders/dla.rs b/src/map_builders/dla.rs index e385d0c..b7c04d7 100644 --- a/src/map_builders/dla.rs +++ b/src/map_builders/dla.rs @@ -1,8 +1,7 @@ -use ::rltk::RandomNumberGenerator; - use super::common::{paint, Symmetry}; use crate::components::Position; use crate::map_builders::{BuilderMap, InitialMapBuilder, MetaMapBuilder}; +use crate::rng::roll_dice; use crate::TileType; #[derive(PartialEq, Copy, Clone)] @@ -20,14 +19,14 @@ pub struct DLABuilder { } impl InitialMapBuilder for DLABuilder { - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - self.build(rng, build_data); + fn build_map(&mut self, build_data: &mut BuilderMap) { + self.build(build_data); } } impl MetaMapBuilder for DLABuilder { - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - self.build(rng, build_data); + fn build_map(&mut self, build_data: &mut BuilderMap) { + self.build(build_data); } } @@ -83,7 +82,7 @@ impl DLABuilder { } #[allow(clippy::map_entry)] - fn build(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + fn build(&mut self, build_data: &mut BuilderMap) { let starting_position = Position { x: build_data.map.width / 2, y: build_data.map.height / 2, @@ -113,8 +112,8 @@ impl DLABuilder { while floor_tile_count < desired_floor_tiles { match self.algorithm { DLAAlgorithm::WalkInwards => { - let mut digger_x = rng.roll_dice(1, build_data.map.width - 3) + 1; - let mut digger_y = rng.roll_dice(1, build_data.map.height - 3) + 1; + let mut digger_x = roll_dice(1, build_data.map.width - 3) + 1; + let mut digger_y = roll_dice(1, build_data.map.height - 3) + 1; let mut prev_x = digger_x; let mut prev_y = digger_y; let mut digger_idx = build_data.map.xy_idx(digger_x, digger_y); @@ -123,7 +122,7 @@ impl DLABuilder { prev_x = digger_x; prev_y = digger_y; - let stagger_direction = rng.roll_dice(1, 4); + let stagger_direction = roll_dice(1, 4); match stagger_direction { 1 => { if digger_x > 2 { @@ -165,7 +164,7 @@ impl DLABuilder { let mut digger_idx = build_data.map.xy_idx(digger_x, digger_y); while build_data.map.tiles[digger_idx] == TileType::Floor { - let stagger_direction = rng.roll_dice(1, 4); + let stagger_direction = roll_dice(1, 4); match stagger_direction { 1 => { if digger_x > 2 { @@ -202,8 +201,8 @@ impl DLABuilder { } DLAAlgorithm::CentralAttractor => { - let mut digger_x = rng.roll_dice(1, build_data.map.width - 3) + 1; - let mut digger_y = rng.roll_dice(1, build_data.map.height - 3) + 1; + let mut digger_x = roll_dice(1, build_data.map.width - 3) + 1; + let mut digger_y = roll_dice(1, build_data.map.height - 3) + 1; let mut prev_x = digger_x; let mut prev_y = digger_y; let mut digger_idx = build_data.map.xy_idx(digger_x, digger_y); diff --git a/src/map_builders/door_placement.rs b/src/map_builders/door_placement.rs index e0f394d..8e88085 100644 --- a/src/map_builders/door_placement.rs +++ b/src/map_builders/door_placement.rs @@ -1,14 +1,13 @@ -use ::rltk::RandomNumberGenerator; - use super::{BuilderMap, MetaMapBuilder}; +use crate::rng::roll_dice; use crate::TileType; pub struct DoorPlacement {} impl MetaMapBuilder for DoorPlacement { #[allow(dead_code)] - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - self.doors(rng, build_data); + fn build_map(&mut self, build_data: &mut BuilderMap) { + self.doors(build_data); } } @@ -18,7 +17,7 @@ impl DoorPlacement { Box::new(DoorPlacement {}) } - fn doors(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + fn doors(&mut self, build_data: &mut BuilderMap) { if let Some(halls_original) = &build_data.corridors { let halls = halls_original.clone(); for hall in halls.iter() { @@ -33,7 +32,7 @@ impl DoorPlacement { for (i, tile) in tiles.iter().enumerate() { if *tile == TileType::Floor && self.door_possible(build_data, i) - && rng.roll_dice(1, 3) == 1 + && roll_dice(1, 3) == 1 { build_data.spawn_list.push((i, "Door".to_string())); } diff --git a/src/map_builders/drunkard.rs b/src/map_builders/drunkard.rs index 566d719..ccf9011 100644 --- a/src/map_builders/drunkard.rs +++ b/src/map_builders/drunkard.rs @@ -1,8 +1,7 @@ -use ::rltk::RandomNumberGenerator; - use super::common::{paint, Symmetry}; use crate::components::Position; use crate::map_builders::{BuilderMap, InitialMapBuilder, MetaMapBuilder}; +use crate::rng::roll_dice; use crate::TileType; #[derive(PartialEq, Copy, Clone)] @@ -24,15 +23,15 @@ pub struct DrunkardsWalkBuilder { } impl InitialMapBuilder for DrunkardsWalkBuilder { - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - self.build(rng, build_data); + fn build_map(&mut self, build_data: &mut BuilderMap) { + self.build(build_data); } } impl MetaMapBuilder for DrunkardsWalkBuilder { #[allow(dead_code)] - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - self.build(rng, build_data); + fn build_map(&mut self, build_data: &mut BuilderMap) { + self.build(build_data); } } @@ -92,7 +91,7 @@ impl DrunkardsWalkBuilder { } #[allow(clippy::map_entry)] - fn build(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + fn build(&mut self, build_data: &mut BuilderMap) { // Set a central starting point let starting_position = Position { x: build_data.map.width / 2, @@ -127,8 +126,8 @@ impl DrunkardsWalkBuilder { drunk_x = starting_position.x; drunk_y = starting_position.y; } else { - drunk_x = rng.roll_dice(1, build_data.map.width - 3) + 1; - drunk_y = rng.roll_dice(1, build_data.map.height - 3) + 1; + drunk_x = roll_dice(1, build_data.map.width - 3) + 1; + drunk_y = roll_dice(1, build_data.map.height - 3) + 1; } } } @@ -150,7 +149,7 @@ impl DrunkardsWalkBuilder { ); build_data.map.tiles[drunk_idx] = TileType::DownStairs; - let stagger_direction = rng.roll_dice(1, 4); + let stagger_direction = roll_dice(1, 4); match stagger_direction { 1 => { if drunk_x > 2 { diff --git a/src/map_builders/dwarf_fort.rs b/src/map_builders/dwarf_fort.rs index 86bfed4..e31c1c7 100644 --- a/src/map_builders/dwarf_fort.rs +++ b/src/map_builders/dwarf_fort.rs @@ -1,4 +1,4 @@ -use ::rltk::{DistanceAlg, Point, RandomNumberGenerator}; +use ::rltk::{DistanceAlg, Point}; use super::{ AreaEndingPosition, AreaStartingPosition, BspCorridors, BspDungeonBuilder, BuilderChain, @@ -7,12 +7,7 @@ use super::{ }; use crate::{tile_walkable, TileType}; -pub fn dwarf_fort_builder( - new_depth: i32, - _rng: &mut RandomNumberGenerator, - width: i32, - height: i32, -) -> BuilderChain { +pub fn dwarf_fort_builder(new_depth: i32, width: i32, height: i32) -> BuilderChain { let mut chain = BuilderChain::new(new_depth, width, height, "Dwarven Fortress"); chain .start_with(BspDungeonBuilder::new()) @@ -34,8 +29,8 @@ pub fn dwarf_fort_builder( pub struct DragonsLair {} impl MetaMapBuilder for DragonsLair { - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - self.build(rng, build_data); + fn build_map(&mut self, build_data: &mut BuilderMap) { + self.build(build_data); } } @@ -45,13 +40,13 @@ impl DragonsLair { Box::new(DragonsLair {}) } - fn build(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + fn build(&mut self, build_data: &mut BuilderMap) { build_data.map.depth = 7; build_data.take_snapshot(); let mut builder = BuilderChain::new(6, build_data.width, build_data.height, "New Map"); builder.start_with(DLABuilder::insectoid()); - builder.build_map(rng); + builder.build_map(); // Add the history to our history for h in builder.build_data.history.iter() { @@ -72,8 +67,8 @@ impl DragonsLair { pub struct DragonSpawner {} impl MetaMapBuilder for DragonSpawner { - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - self.build(rng, build_data); + fn build_map(&mut self, build_data: &mut BuilderMap) { + self.build(build_data); } } @@ -83,7 +78,7 @@ impl DragonSpawner { Box::new(DragonSpawner {}) } - fn build(&mut self, _rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + fn build(&mut self, build_data: &mut BuilderMap) { // Find a central location that isn't occupied let seed_x = build_data.map.width / 2; let seed_y = build_data.map.height / 2; diff --git a/src/map_builders/forest.rs b/src/map_builders/forest.rs index f73df89..e919539 100644 --- a/src/map_builders/forest.rs +++ b/src/map_builders/forest.rs @@ -1,18 +1,12 @@ -use ::rltk::RandomNumberGenerator; - use super::{ AreaStartingPosition, BuilderChain, CellularAutomataBuilder, CullUnreachable, MetaMapBuilder, VoronoiSpawning, XStart, YStart, }; use crate::map_builders::BuilderMap; +use crate::rng::roll_dice; use crate::{map, TileType}; -pub fn forest_builder( - new_depth: i32, - _rng: &mut RandomNumberGenerator, - width: i32, - height: i32, -) -> BuilderChain { +pub fn forest_builder(new_depth: i32, width: i32, height: i32) -> BuilderChain { let mut chain = BuilderChain::new(new_depth, width, height, "Into the Woods"); chain @@ -29,8 +23,8 @@ pub fn forest_builder( pub struct YellowBrickRoad {} impl MetaMapBuilder for YellowBrickRoad { - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - self.build(rng, build_data); + fn build_map(&mut self, build_data: &mut BuilderMap) { + self.build(build_data); } } @@ -80,7 +74,7 @@ impl YellowBrickRoad { } } - fn build(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + fn build(&mut self, build_data: &mut BuilderMap) { let starting_pos = *build_data.starting_position.as_ref().unwrap(); let start_idx = build_data.map.xy_idx(starting_pos.x, starting_pos.y); @@ -107,7 +101,7 @@ impl YellowBrickRoad { build_data.take_snapshot(); // Place exit - let exit_dir = rng.roll_dice(1, 2); + let exit_dir = roll_dice(1, 2); let (seed_x, seed_y, stream_startx, stream_starty) = if exit_dir == 1 { (build_data.map.width - 1, 1, 0, build_data.height - 1) } else { diff --git a/src/map_builders/limestone_cavern.rs b/src/map_builders/limestone_cavern.rs index eb889f8..fd49162 100644 --- a/src/map_builders/limestone_cavern.rs +++ b/src/map_builders/limestone_cavern.rs @@ -1,5 +1,3 @@ -use ::rltk::RandomNumberGenerator; - use super::prefab_builder::prefab_sections; use super::{ AreaEndingPosition, AreaStartingPosition, BspDungeonBuilder, BuilderChain, BuilderMap, @@ -8,13 +6,9 @@ use super::{ RoomSort, RoomSorter, VoronoiSpawning, XEnd, XStart, YEnd, YStart, }; use crate::map::TileType; +use crate::rng::roll_dice; -pub fn limestone_cavern_builder( - new_depth: i32, - _rng: &mut RandomNumberGenerator, - width: i32, - height: i32, -) -> BuilderChain { +pub fn limestone_cavern_builder(new_depth: i32, width: i32, height: i32) -> BuilderChain { let mut chain = BuilderChain::new(new_depth, width, height, "Limestone Caverns"); chain .start_with(DrunkardsWalkBuilder::winding_passages()) @@ -28,12 +22,7 @@ pub fn limestone_cavern_builder( chain } -pub fn limestone_deep_cavern_builder( - new_depth: i32, - _rng: &mut RandomNumberGenerator, - width: i32, - height: i32, -) -> BuilderChain { +pub fn limestone_deep_cavern_builder(new_depth: i32, width: i32, height: i32) -> BuilderChain { let mut chain = BuilderChain::new(new_depth, width, height, "Deep Limestone Caverns"); chain .start_with(DLABuilder::central_attractor()) @@ -46,12 +35,7 @@ pub fn limestone_deep_cavern_builder( chain } -pub fn limestone_transition_builder( - new_depth: i32, - _rng: &mut RandomNumberGenerator, - width: i32, - height: i32, -) -> BuilderChain { +pub fn limestone_transition_builder(new_depth: i32, width: i32, height: i32) -> BuilderChain { let mut chain = BuilderChain::new(new_depth, width, height, "Dwarf Fort - Upper Reaches"); chain .start_with(CellularAutomataBuilder::new()) @@ -71,8 +55,8 @@ pub fn limestone_transition_builder( pub struct CaveDecorator {} impl MetaMapBuilder for CaveDecorator { - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - self.build(rng, build_data); + fn build_map(&mut self, build_data: &mut BuilderMap) { + self.build(build_data); } } @@ -82,13 +66,13 @@ impl CaveDecorator { Box::new(CaveDecorator {}) } - fn build(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + fn build(&mut self, build_data: &mut BuilderMap) { let old_map = build_data.map.clone(); for (idx, tt) in build_data.map.tiles.iter_mut().enumerate() { // Gravel Spawning - if *tt == TileType::Floor && rng.roll_dice(1, 6) == 1 { + if *tt == TileType::Floor && roll_dice(1, 6) == 1 { *tt = TileType::Gravel; - } else if *tt == TileType::Floor && rng.roll_dice(1, 10) == 1 { + } else if *tt == TileType::Floor && roll_dice(1, 10) == 1 { // Spawn passable pools *tt = TileType::ShallowWater; } else if *tt == TileType::Wall { @@ -115,7 +99,7 @@ impl CaveDecorator { if neighbors == 2 { *tt = TileType::DeepWater; } else if neighbors == 1 { - *tt = match rng.roll_dice(1, 4) { + *tt = match roll_dice(1, 4) { 1 => TileType::Stalactite, 2 => TileType::Stalagmite, _ => *tt, @@ -132,8 +116,8 @@ impl CaveDecorator { pub struct CaveTransition {} impl MetaMapBuilder for CaveTransition { - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - self.build(rng, build_data); + fn build_map(&mut self, build_data: &mut BuilderMap) { + self.build(build_data); } } @@ -143,7 +127,7 @@ impl CaveTransition { Box::new(CaveTransition {}) } - fn build(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + fn build(&mut self, build_data: &mut BuilderMap) { build_data.map.depth = 5; build_data.take_snapshot(); @@ -156,7 +140,7 @@ impl CaveTransition { .with(NearestCorridors::new()) .with(RoomExploder::new()) .with(RoomBasedSpawner::new()); - builder.build_map(rng); + builder.build_map(); // Add the history to our history for h in builder.build_data.history.iter() { diff --git a/src/map_builders/maze.rs b/src/map_builders/maze.rs index f12fcbe..dcb0feb 100644 --- a/src/map_builders/maze.rs +++ b/src/map_builders/maze.rs @@ -1,15 +1,14 @@ use std::cmp::{max, min}; -use ::rltk::RandomNumberGenerator; - use crate::map_builders::{BuilderMap, InitialMapBuilder}; +use crate::rng::roll_dice; use crate::{Map, TileType}; pub struct MazeBuilder {} impl InitialMapBuilder for MazeBuilder { - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - self.build(rng, build_data); + fn build_map(&mut self, build_data: &mut BuilderMap) { + self.build(build_data); } } @@ -19,12 +18,11 @@ impl MazeBuilder { } #[allow(clippy::map_entry)] - fn build(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + fn build(&mut self, build_data: &mut BuilderMap) { // Maze gen let mut maze = Grid::new( (build_data.map.width / 2) - 2, (build_data.map.height / 2) - 2, - rng, ); maze.generate_maze(build_data); } @@ -73,24 +71,22 @@ impl Cell { } } -struct Grid<'a> { +struct Grid { width: i32, height: i32, cells: Vec, backtrace: Vec, current: usize, - rng: &'a mut RandomNumberGenerator, } -impl<'a> Grid<'a> { - fn new(width: i32, height: i32, rng: &mut RandomNumberGenerator) -> Grid { +impl Grid { + fn new(width: i32, height: i32) -> Grid { let mut grid = Grid { width, height, cells: Vec::new(), backtrace: Vec::new(), current: 0, - rng, }; for row in 0..height { @@ -138,7 +134,7 @@ impl<'a> Grid<'a> { return if neighbors.len() == 1 { Some(neighbors[0]) } else { - Some(neighbors[(self.rng.roll_dice(1, neighbors.len() as i32) - 1) as usize]) + Some(neighbors[(roll_dice(1, neighbors.len() as i32) - 1) as usize]) }; } diff --git a/src/map_builders/mushroom_forest.rs b/src/map_builders/mushroom_forest.rs index 43c1368..dd432d3 100644 --- a/src/map_builders/mushroom_forest.rs +++ b/src/map_builders/mushroom_forest.rs @@ -5,12 +5,7 @@ use super::{ YStart, }; -pub fn mushroom_entrance( - new_depth: i32, - _rng: &mut ::rltk::RandomNumberGenerator, - width: i32, - height: i32, -) -> BuilderChain { +pub fn mushroom_entrance(new_depth: i32, width: i32, height: i32) -> BuilderChain { let mut chain = BuilderChain::new(new_depth, width, height, "Into the Mushroom Grove"); chain .start_with(CellularAutomataBuilder::new()) @@ -25,12 +20,7 @@ pub fn mushroom_entrance( chain } -pub fn mushroom_builder( - new_depth: i32, - _rng: &mut ::rltk::RandomNumberGenerator, - width: i32, - height: i32, -) -> BuilderChain { +pub fn mushroom_builder(new_depth: i32, width: i32, height: i32) -> BuilderChain { let mut chain = BuilderChain::new(new_depth, width, height, "Into the Mushroom Grove"); chain .start_with(CellularAutomataBuilder::new()) @@ -44,12 +34,7 @@ pub fn mushroom_builder( chain } -pub fn mushroom_exit( - new_depth: i32, - _rng: &mut rltk::RandomNumberGenerator, - width: i32, - height: i32, -) -> BuilderChain { +pub fn mushroom_exit(new_depth: i32, width: i32, height: i32) -> BuilderChain { let mut chain = BuilderChain::new(new_depth, width, height, "Into The Mushroom Grove"); chain .start_with(CellularAutomataBuilder::new()) diff --git a/src/map_builders/prefab_builder.rs b/src/map_builders/prefab_builder.rs index b1c6054..4cec6f7 100644 --- a/src/map_builders/prefab_builder.rs +++ b/src/map_builders/prefab_builder.rs @@ -4,9 +4,8 @@ pub mod prefab_sections; use std::collections::HashSet; -use ::rltk::RandomNumberGenerator; - use crate::map_builders::{BuilderMap, InitialMapBuilder, MetaMapBuilder}; +use crate::rng::roll_dice; use crate::{Position, TileType}; #[derive(PartialEq, Copy, Clone)] @@ -30,14 +29,14 @@ pub struct PrefabBuilder { } impl MetaMapBuilder for PrefabBuilder { - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - self.build(rng, build_data); + fn build_map(&mut self, build_data: &mut BuilderMap) { + self.build(build_data); } } impl InitialMapBuilder for PrefabBuilder { - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - self.build(rng, build_data); + fn build_map(&mut self, build_data: &mut BuilderMap) { + self.build(build_data); } } @@ -77,12 +76,12 @@ impl PrefabBuilder { }) } - fn build(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + fn build(&mut self, build_data: &mut BuilderMap) { match self.mode { PrefabMode::RexLevel { template } => self.load_rex_map(template, build_data), PrefabMode::Constant { level } => self.load_ascii_map(&level, build_data), - PrefabMode::Sectional { section } => self.apply_sectional(§ion, rng, build_data), - PrefabMode::RoomVaults => self.apply_room_vaults(rng, build_data), + PrefabMode::Sectional { section } => self.apply_sectional(§ion, build_data), + PrefabMode::RoomVaults => self.apply_room_vaults(build_data), } build_data.take_snapshot(); } @@ -189,12 +188,8 @@ impl PrefabBuilder { } } - fn apply_previous_iteration( - &mut self, - mut filter: F, - _rng: &mut RandomNumberGenerator, - build_data: &mut BuilderMap, - ) where + fn apply_previous_iteration(&mut self, mut filter: F, build_data: &mut BuilderMap) + where F: FnMut(i32, i32) -> bool, { let width = build_data.map.width; @@ -211,7 +206,7 @@ impl PrefabBuilder { fn apply_sectional( &mut self, section: &prefab_sections::PrefabSection, - rng: &mut RandomNumberGenerator, + build_data: &mut BuilderMap, ) { use prefab_sections::*; @@ -239,7 +234,6 @@ impl PrefabBuilder { || y < chunk_y || y > (chunk_y + section.height as i32) }, - rng, build_data, ); @@ -262,14 +256,14 @@ impl PrefabBuilder { build_data.take_snapshot(); } - fn apply_room_vaults(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + fn apply_room_vaults(&mut self, build_data: &mut BuilderMap) { use prefab_rooms::*; // Apply the previous builder, and keep all entities it spawns (for now) - self.apply_previous_iteration(|_x, _y| true, rng, build_data); + self.apply_previous_iteration(|_x, _y| true, build_data); // Do we want a vault at all? - let vault_roll = rng.roll_dice(1, 6) + build_data.map.depth; + let vault_roll = roll_dice(1, 6) + build_data.map.depth; if vault_roll < 4 { return; } @@ -290,14 +284,14 @@ impl PrefabBuilder { return; } - let n_vaults = i32::min(rng.roll_dice(1, 3), possible_vaults.len() as i32); + let n_vaults = i32::min(roll_dice(1, 3), possible_vaults.len() as i32); let mut used_tiles: HashSet = HashSet::new(); for _i in 0..n_vaults { let vault_index = if possible_vaults.len() == 1 { 0 } else { - (rng.roll_dice(1, possible_vaults.len() as i32) - 1) as usize + (roll_dice(1, possible_vaults.len() as i32) - 1) as usize }; let vault = possible_vaults[vault_index]; @@ -344,7 +338,7 @@ impl PrefabBuilder { let pos_idx = if vault_positions.len() == 1 { 0 } else { - (rng.roll_dice(1, vault_positions.len() as i32) - 1) as usize + (roll_dice(1, vault_positions.len() as i32) - 1) as usize }; let pos = &vault_positions[pos_idx]; diff --git a/src/map_builders/room_based_spawner.rs b/src/map_builders/room_based_spawner.rs index 1c2700b..a11652f 100644 --- a/src/map_builders/room_based_spawner.rs +++ b/src/map_builders/room_based_spawner.rs @@ -1,12 +1,10 @@ -use ::rltk::RandomNumberGenerator; - use crate::map_builders::{spawner, BuilderMap, MetaMapBuilder}; pub struct RoomBasedSpawner {} impl MetaMapBuilder for RoomBasedSpawner { - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - self.build(rng, build_data); + fn build_map(&mut self, build_data: &mut BuilderMap) { + self.build(build_data); } } @@ -16,12 +14,11 @@ impl RoomBasedSpawner { Box::new(RoomBasedSpawner {}) } - fn build(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + fn build(&mut self, build_data: &mut BuilderMap) { if let Some(rooms) = &build_data.rooms { for room in rooms.iter().skip(1) { spawner::spawn_room( &build_data.map, - rng, room, build_data.map.depth, &mut build_data.spawn_list, diff --git a/src/map_builders/room_based_stairs.rs b/src/map_builders/room_based_stairs.rs index 73fb200..06a4cb3 100644 --- a/src/map_builders/room_based_stairs.rs +++ b/src/map_builders/room_based_stairs.rs @@ -1,13 +1,11 @@ -use ::rltk::RandomNumberGenerator; - use crate::map_builders::{BuilderMap, MetaMapBuilder}; use crate::TileType; pub struct RoomBasedStairs {} impl MetaMapBuilder for RoomBasedStairs { - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - self.build(rng, build_data); + fn build_map(&mut self, build_data: &mut BuilderMap) { + self.build(build_data); } } @@ -17,7 +15,7 @@ impl RoomBasedStairs { Box::new(RoomBasedStairs {}) } - fn build(&mut self, _rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + fn build(&mut self, build_data: &mut BuilderMap) { if let Some(rooms) = &build_data.rooms { let stairs_position = rooms[rooms.len() - 1].center(); let stairs_idx = build_data.map.xy_idx(stairs_position.0, stairs_position.1); diff --git a/src/map_builders/room_based_starting_position.rs b/src/map_builders/room_based_starting_position.rs index e70ee96..c2b41f8 100644 --- a/src/map_builders/room_based_starting_position.rs +++ b/src/map_builders/room_based_starting_position.rs @@ -1,13 +1,11 @@ -use ::rltk::RandomNumberGenerator; - use crate::map_builders::{BuilderMap, MetaMapBuilder}; use crate::Position; pub struct RoomBasedStartingPosition {} impl MetaMapBuilder for RoomBasedStartingPosition { - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - self.build(rng, build_data); + fn build_map(&mut self, build_data: &mut BuilderMap) { + self.build(build_data); } } @@ -17,7 +15,7 @@ impl RoomBasedStartingPosition { Box::new(RoomBasedStartingPosition {}) } - fn build(&mut self, _rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + fn build(&mut self, build_data: &mut BuilderMap) { if let Some(rooms) = &build_data.rooms { let start_pos = rooms[0].center(); build_data.starting_position = Some(Position { diff --git a/src/map_builders/room_corner_rounding.rs b/src/map_builders/room_corner_rounding.rs index 6574886..d32d026 100644 --- a/src/map_builders/room_corner_rounding.rs +++ b/src/map_builders/room_corner_rounding.rs @@ -1,13 +1,11 @@ -use ::rltk::RandomNumberGenerator; - use crate::map_builders::{BuilderMap, MetaMapBuilder}; use crate::{Rect, TileType}; pub struct RoomCornerRounder {} impl MetaMapBuilder for RoomCornerRounder { - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - self.build(rng, build_data); + fn build_map(&mut self, build_data: &mut BuilderMap) { + self.build(build_data); } } @@ -41,7 +39,7 @@ impl RoomCornerRounder { } } - fn build(&mut self, _rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + fn build(&mut self, build_data: &mut BuilderMap) { let rooms: Vec; if let Some(rooms_builder) = &build_data.rooms { rooms = rooms_builder.clone(); diff --git a/src/map_builders/room_corridor_spawner.rs b/src/map_builders/room_corridor_spawner.rs index 153596b..995b646 100644 --- a/src/map_builders/room_corridor_spawner.rs +++ b/src/map_builders/room_corridor_spawner.rs @@ -1,13 +1,11 @@ -use ::rltk::RandomNumberGenerator; - use super::{BuilderMap, MetaMapBuilder}; use crate::spawner; pub struct CorridorSpawner {} impl MetaMapBuilder for CorridorSpawner { - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - self.build(rng, build_data); + fn build_map(&mut self, build_data: &mut BuilderMap) { + self.build(build_data); } } @@ -17,11 +15,11 @@ impl CorridorSpawner { Box::new(CorridorSpawner {}) } - fn build(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + fn build(&mut self, build_data: &mut BuilderMap) { if let Some(corridors) = &build_data.corridors { for c in corridors.iter() { let depth = build_data.map.depth; - spawner::spawn_region(&build_data.map, rng, c, depth, &mut build_data.spawn_list); + spawner::spawn_region(&build_data.map, c, depth, &mut build_data.spawn_list); } } else { panic!("Corridor Based Spawning only works after corridors have been created."); diff --git a/src/map_builders/room_draw.rs b/src/map_builders/room_draw.rs index b7ecc5f..1c4d003 100644 --- a/src/map_builders/room_draw.rs +++ b/src/map_builders/room_draw.rs @@ -1,13 +1,12 @@ -use ::rltk::RandomNumberGenerator; - use super::{BuilderMap, MetaMapBuilder}; +use crate::rng::roll_dice; use crate::{Rect, TileType}; pub struct RoomDrawer {} impl MetaMapBuilder for RoomDrawer { - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - self.build(rng, build_data); + fn build_map(&mut self, build_data: &mut BuilderMap) { + self.build(build_data); } } @@ -49,7 +48,7 @@ impl RoomDrawer { } } - fn build(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + fn build(&mut self, build_data: &mut BuilderMap) { let rooms: Vec; if let Some(rooms_builder) = &build_data.rooms { rooms = rooms_builder.clone(); @@ -59,7 +58,7 @@ impl RoomDrawer { for room in rooms.iter() { // Choose which shape of room to render - match rng.roll_dice(1, 4) { + match roll_dice(1, 4) { 1 => self.circle(build_data, room), _ => self.rectangle(build_data, room), }; diff --git a/src/map_builders/room_exploder.rs b/src/map_builders/room_exploder.rs index dec34ee..79d1946 100644 --- a/src/map_builders/room_exploder.rs +++ b/src/map_builders/room_exploder.rs @@ -1,14 +1,13 @@ -use ::rltk::RandomNumberGenerator; - use crate::map_builders::common::{paint, Symmetry}; use crate::map_builders::{BuilderMap, MetaMapBuilder}; +use crate::rng::roll_dice; use crate::{Rect, TileType}; pub struct RoomExploder {} impl MetaMapBuilder for RoomExploder { - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - self.build(rng, build_data); + fn build_map(&mut self, build_data: &mut BuilderMap) { + self.build(build_data); } } @@ -18,7 +17,7 @@ impl RoomExploder { Box::new(RoomExploder {}) } - fn build(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + fn build(&mut self, build_data: &mut BuilderMap) { let rooms: Vec; if let Some(rooms_builder) = &build_data.rooms { rooms = rooms_builder.clone(); @@ -28,7 +27,7 @@ impl RoomExploder { for room in rooms.iter() { let start = room.center(); - let n_diggers = rng.roll_dice(1, 20) - 5; + let n_diggers = roll_dice(1, 20) - 5; if n_diggers > 0 { for _i in 0..n_diggers { @@ -47,7 +46,7 @@ impl RoomExploder { paint(&mut build_data.map, Symmetry::None, 1, drunk_x, drunk_y); build_data.map.tiles[drunk_idx] = TileType::DownStairs; - match rng.roll_dice(1, 4) { + match roll_dice(1, 4) { 1 => { if drunk_x > 2 { drunk_x -= 1; diff --git a/src/map_builders/room_sorter.rs b/src/map_builders/room_sorter.rs index f7eb477..9ac85b8 100644 --- a/src/map_builders/room_sorter.rs +++ b/src/map_builders/room_sorter.rs @@ -1,5 +1,3 @@ -use ::rltk::RandomNumberGenerator; - use super::{BuilderMap, MetaMapBuilder}; use crate::Rect; @@ -17,8 +15,8 @@ pub struct RoomSorter { impl MetaMapBuilder for RoomSorter { #[allow(dead_code)] - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - self.sorter(rng, build_data); + fn build_map(&mut self, build_data: &mut BuilderMap) { + self.sorter(build_data); } } @@ -28,7 +26,7 @@ impl RoomSorter { Box::new(RoomSorter { sort_by }) } - fn sorter(&mut self, _rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + fn sorter(&mut self, build_data: &mut BuilderMap) { let rooms = build_data.rooms.as_mut().unwrap(); match self.sort_by { diff --git a/src/map_builders/rooms_corridors_bsp.rs b/src/map_builders/rooms_corridors_bsp.rs index 51659f4..e2d9dbf 100644 --- a/src/map_builders/rooms_corridors_bsp.rs +++ b/src/map_builders/rooms_corridors_bsp.rs @@ -1,15 +1,14 @@ -use ::rltk::RandomNumberGenerator; - use super::common::draw_corridor; use super::{BuilderMap, MetaMapBuilder}; +use crate::rng::roll_dice; use crate::Rect; pub struct BspCorridors {} impl MetaMapBuilder for BspCorridors { #[allow(dead_code)] - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - self.corridors(rng, build_data); + fn build_map(&mut self, build_data: &mut BuilderMap) { + self.corridors(build_data); } } @@ -19,7 +18,7 @@ impl BspCorridors { Box::new(BspCorridors {}) } - fn corridors(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + fn corridors(&mut self, build_data: &mut BuilderMap) { let rooms: Vec; if let Some(rooms_builder) = &build_data.rooms { rooms = rooms_builder.clone(); @@ -31,12 +30,10 @@ impl BspCorridors { for i in 0..rooms.len() - 1 { let room = rooms[i]; let next_room = rooms[i + 1]; - let start_x = room.x1 + (rng.roll_dice(1, i32::abs(room.x1 - room.x2)) - 1); - let start_y = room.y1 + (rng.roll_dice(1, i32::abs(room.y1 - room.y2)) - 1); - let end_x = - next_room.x1 + (rng.roll_dice(1, i32::abs(next_room.x1 - next_room.x2)) - 1); - let end_y = - next_room.y1 + (rng.roll_dice(1, i32::abs(next_room.y1 - next_room.y2)) - 1); + let start_x = room.x1 + (roll_dice(1, i32::abs(room.x1 - room.x2)) - 1); + let start_y = room.y1 + (roll_dice(1, i32::abs(room.y1 - room.y2)) - 1); + let end_x = next_room.x1 + (roll_dice(1, i32::abs(next_room.x1 - next_room.x2)) - 1); + let end_y = next_room.y1 + (roll_dice(1, i32::abs(next_room.y1 - next_room.y2)) - 1); let corridor = draw_corridor(&mut build_data.map, start_x, start_y, end_x, end_y); corridors.push(corridor); diff --git a/src/map_builders/rooms_corridors_dogleg.rs b/src/map_builders/rooms_corridors_dogleg.rs index 504338e..c9b4d1b 100644 --- a/src/map_builders/rooms_corridors_dogleg.rs +++ b/src/map_builders/rooms_corridors_dogleg.rs @@ -1,15 +1,14 @@ -use ::rltk::RandomNumberGenerator; - use super::common::{apply_horizontal_tunnel, apply_vertical_tunnel}; use crate::map_builders::{BuilderMap, MetaMapBuilder}; +use crate::rng::range; use crate::Rect; pub struct DoglegCorridors {} impl MetaMapBuilder for DoglegCorridors { #[allow(dead_code)] - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - self.corridors(rng, build_data); + fn build_map(&mut self, build_data: &mut BuilderMap) { + self.corridors(build_data); } } @@ -19,7 +18,7 @@ impl DoglegCorridors { Box::new(DoglegCorridors {}) } - fn corridors(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + fn corridors(&mut self, build_data: &mut BuilderMap) { let rooms: Vec; if let Some(rooms_builder) = &build_data.rooms { rooms = rooms_builder.clone(); @@ -33,7 +32,7 @@ impl DoglegCorridors { let (new_x, new_y) = room.center(); let (prev_x, prev_y) = rooms[i as usize - 1].center(); - if rng.range(0, 2) == 1 { + if range(0, 2) == 1 { let mut c1 = apply_horizontal_tunnel(&mut build_data.map, prev_x, new_x, prev_y); let mut c2 = apply_vertical_tunnel(&mut build_data.map, prev_y, new_y, new_x); diff --git a/src/map_builders/rooms_corridors_lines.rs b/src/map_builders/rooms_corridors_lines.rs index a748e3a..9871d15 100644 --- a/src/map_builders/rooms_corridors_lines.rs +++ b/src/map_builders/rooms_corridors_lines.rs @@ -1,7 +1,5 @@ use std::collections::HashSet; -use ::rltk::RandomNumberGenerator; - use super::{BuilderMap, MetaMapBuilder}; use crate::{Rect, TileType}; @@ -9,8 +7,8 @@ pub struct StraightLineCorridors {} impl MetaMapBuilder for StraightLineCorridors { #[allow(dead_code)] - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - self.corridors(rng, build_data); + fn build_map(&mut self, build_data: &mut BuilderMap) { + self.corridors(build_data); } } @@ -20,7 +18,7 @@ impl StraightLineCorridors { Box::new(StraightLineCorridors {}) } - fn corridors(&mut self, _rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + fn corridors(&mut self, build_data: &mut BuilderMap) { let rooms: Vec; if let Some(rooms_builder) = &build_data.rooms { rooms = rooms_builder.clone(); diff --git a/src/map_builders/rooms_corridors_nearest.rs b/src/map_builders/rooms_corridors_nearest.rs index b1af41b..32c57c5 100644 --- a/src/map_builders/rooms_corridors_nearest.rs +++ b/src/map_builders/rooms_corridors_nearest.rs @@ -1,7 +1,5 @@ use std::collections::HashSet; -use ::rltk::RandomNumberGenerator; - use super::common::draw_corridor; use super::{BuilderMap, MetaMapBuilder}; use crate::Rect; @@ -10,8 +8,8 @@ pub struct NearestCorridors {} impl MetaMapBuilder for NearestCorridors { #[allow(dead_code)] - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - self.corridors(rng, build_data); + fn build_map(&mut self, build_data: &mut BuilderMap) { + self.corridors(build_data); } } @@ -21,7 +19,7 @@ impl NearestCorridors { Box::new(NearestCorridors {}) } - fn corridors(&mut self, _rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + fn corridors(&mut self, build_data: &mut BuilderMap) { let rooms: Vec; if let Some(rooms_builder) = &build_data.rooms { rooms = rooms_builder.clone(); diff --git a/src/map_builders/simple_map.rs b/src/map_builders/simple_map.rs index ebd6764..abd35da 100644 --- a/src/map_builders/simple_map.rs +++ b/src/map_builders/simple_map.rs @@ -1,14 +1,13 @@ -use ::rltk::RandomNumberGenerator; - use crate::map_builders::{BuilderMap, InitialMapBuilder}; +use crate::rng::{range, roll_dice}; use crate::Rect; pub struct SimpleMapBuilder {} impl InitialMapBuilder for SimpleMapBuilder { #[allow(dead_code)] - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - self.build_rooms(rng, build_data); + fn build_map(&mut self, build_data: &mut BuilderMap) { + self.build_rooms(build_data); } } @@ -18,17 +17,17 @@ impl SimpleMapBuilder { Box::new(SimpleMapBuilder {}) } - fn build_rooms(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + fn build_rooms(&mut self, build_data: &mut BuilderMap) { const MAX_ROOMS: i32 = 30; const MIN_SIZE: i32 = 6; const MAX_SIZE: i32 = 10; let mut rooms: Vec = Vec::new(); for _ in 0..MAX_ROOMS { - let w = rng.range(MIN_SIZE, MAX_SIZE); - let h = rng.range(MIN_SIZE, MAX_SIZE); - let x = rng.roll_dice(1, build_data.map.width - w - 1) - 1; - let y = rng.roll_dice(1, build_data.map.height - h - 1) - 1; + let w = range(MIN_SIZE, MAX_SIZE); + let h = range(MIN_SIZE, MAX_SIZE); + let x = roll_dice(1, build_data.map.width - w - 1) - 1; + let y = roll_dice(1, build_data.map.height - h - 1) - 1; let new_room = Rect::new(x, y, w, h); let mut ok = true; diff --git a/src/map_builders/town.rs b/src/map_builders/town.rs index 6b44e41..a67c47a 100644 --- a/src/map_builders/town.rs +++ b/src/map_builders/town.rs @@ -1,16 +1,12 @@ use std::collections::HashSet; -use ::rltk::{Point, RandomNumberGenerator}; +use ::rltk::Point; use super::{BuilderChain, BuilderMap, InitialMapBuilder}; +use crate::rng::roll_dice; use crate::{Position, TileType}; -pub fn town_builder( - new_depth: i32, - _rng: &mut RandomNumberGenerator, - width: i32, - height: i32, -) -> BuilderChain { +pub fn town_builder(new_depth: i32, width: i32, height: i32) -> BuilderChain { let mut chain = BuilderChain::new(new_depth, width, height, "The Town of Ion"); chain.start_with(TownBuilder::new()); @@ -21,8 +17,8 @@ pub struct TownBuilder {} impl InitialMapBuilder for TownBuilder { #[allow(dead_code)] - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - self.build_rooms(rng, build_data); + fn build_map(&mut self, build_data: &mut BuilderMap) { + self.build_rooms(build_data); } } @@ -44,13 +40,13 @@ impl TownBuilder { Box::new(TownBuilder {}) } - pub fn build_rooms(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + pub fn build_rooms(&mut self, build_data: &mut BuilderMap) { self.grass_layer(build_data); - self.water_and_piers(rng, build_data); + self.water_and_piers(build_data); - let (mut available_building_tiles, wall_gap_y) = self.town_walls(rng, build_data); - let mut buildings = self.buildings(rng, build_data, &mut available_building_tiles); - let doors = self.add_doors(rng, build_data, &mut buildings, wall_gap_y); + let (mut available_building_tiles, wall_gap_y) = self.town_walls(build_data); + let mut buildings = self.buildings(build_data, &mut available_building_tiles); + let doors = self.add_doors(build_data, &mut buildings, wall_gap_y); self.add_paths(build_data, &doors); for y in wall_gap_y - 3..wall_gap_y + 4 { @@ -59,10 +55,10 @@ impl TownBuilder { } let building_size = self.sort_buildings(&buildings); - self.building_factory(rng, build_data, &buildings, &building_size); + self.building_factory(build_data, &buildings, &building_size); - self.spawn_dockers(build_data, rng); - self.spawn_townsfolk(build_data, rng, &mut available_building_tiles); + self.spawn_dockers(build_data); + self.spawn_townsfolk(build_data, &mut available_building_tiles); // Make visible for screenshot for t in build_data.map.visible_tiles.iter_mut() { @@ -79,12 +75,12 @@ impl TownBuilder { build_data.take_snapshot(); } - fn water_and_piers(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - let mut n = (rng.roll_dice(1, 65535) as f32) / 65535_f32; + fn water_and_piers(&mut self, build_data: &mut BuilderMap) { + let mut n = (roll_dice(1, 65535) as f32) / 65535_f32; let mut water_width: Vec = Vec::new(); for y in 0..build_data.height { - let n_water = (f32::sin(n) * 10.0) as i32 + 14 + rng.roll_dice(1, 6); + let n_water = (f32::sin(n) * 10.0) as i32 + 14 + roll_dice(1, 6); water_width.push(n_water); n += 0.1; @@ -101,9 +97,9 @@ impl TownBuilder { build_data.take_snapshot(); // Add piers - for _i in 0..rng.roll_dice(1, 4) + 6 { - let y = rng.roll_dice(1, build_data.height) - 1; - for x in 2 + rng.roll_dice(1, 6)..water_width[y as usize] + 4 { + for _i in 0..roll_dice(1, 4) + 6 { + let y = roll_dice(1, build_data.height) - 1; + for x in 2 + roll_dice(1, 6)..water_width[y as usize] + 4 { let idx = build_data.map.xy_idx(x, y); build_data.map.tiles[idx] = TileType::WoodFloor; } @@ -111,13 +107,9 @@ impl TownBuilder { build_data.take_snapshot(); } - fn town_walls( - &mut self, - rng: &mut RandomNumberGenerator, - build_data: &mut BuilderMap, - ) -> (HashSet, i32) { + fn town_walls(&mut self, build_data: &mut BuilderMap) -> (HashSet, i32) { let mut available_building_tiles: HashSet = HashSet::new(); - let wall_gap_y = rng.roll_dice(1, build_data.height - 9) + 5; + let wall_gap_y = roll_dice(1, build_data.height - 9) + 5; for y in 1..build_data.height - 2 { if !(y > wall_gap_y - 4 && y < wall_gap_y + 4) { let idx = build_data.map.xy_idx(30, y); @@ -156,7 +148,7 @@ impl TownBuilder { fn buildings( &mut self, - rng: &mut RandomNumberGenerator, + build_data: &mut BuilderMap, available_building_tiles: &mut HashSet, ) -> Vec<(i32, i32, i32, i32)> { @@ -164,10 +156,10 @@ impl TownBuilder { let mut n_buildings = 0; while n_buildings < 12 { - let bx = rng.roll_dice(1, build_data.map.width - 32) + 30; - let by = rng.roll_dice(1, build_data.map.height) - 2; - let bw = rng.roll_dice(1, 8) + 4; - let bh = rng.roll_dice(1, 8) + 4; + let bx = roll_dice(1, build_data.map.width - 32) + 30; + let by = roll_dice(1, build_data.map.height) - 2; + let bw = roll_dice(1, 8) + 4; + let bh = roll_dice(1, 8) + 4; let mut possible = true; for y in by..by + bh { @@ -237,14 +229,14 @@ impl TownBuilder { fn add_doors( &mut self, - rng: &mut RandomNumberGenerator, + build_data: &mut BuilderMap, buildings: &mut Vec<(i32, i32, i32, i32)>, wall_gap_y: i32, ) -> Vec { let mut doors = Vec::new(); for building in buildings.iter() { - let door_x = building.0 + 1 + rng.roll_dice(1, building.2 - 3); + let door_x = building.0 + 1 + roll_dice(1, building.2 - 3); let cy = building.1 + (building.3 / 2); let idx = if cy > wall_gap_y { // Door on the north wall @@ -332,7 +324,7 @@ impl TownBuilder { fn building_factory( &mut self, - rng: &mut RandomNumberGenerator, + build_data: &mut BuilderMap, buildings: &[(i32, i32, i32, i32)], building_index: &[(usize, i32, BuildingTag)], @@ -340,14 +332,14 @@ impl TownBuilder { for (i, building) in buildings.iter().enumerate() { let build_type = &building_index[i].2; match build_type { - BuildingTag::Pub => self.build_pub(building, build_data, rng), - BuildingTag::Temple => self.build_temple(building, build_data, rng), - BuildingTag::Blacksmith => self.build_smith(building, build_data, rng), - BuildingTag::Clothier => self.build_clothier(building, build_data, rng), - BuildingTag::Alchemist => self.build_alchemist(building, build_data, rng), - BuildingTag::PlayerHouse => self.build_my_house(building, build_data, rng), - BuildingTag::Hovel => self.build_hovel(building, build_data, rng), - BuildingTag::Abandoned => self.build_abandoned_house(building, build_data, rng), + BuildingTag::Pub => self.build_pub(building, build_data), + BuildingTag::Temple => self.build_temple(building, build_data), + BuildingTag::Blacksmith => self.build_smith(building, build_data), + BuildingTag::Clothier => self.build_clothier(building, build_data), + BuildingTag::Alchemist => self.build_alchemist(building, build_data), + BuildingTag::PlayerHouse => self.build_my_house(building, build_data), + BuildingTag::Hovel => self.build_hovel(building, build_data), + BuildingTag::Abandoned => self.build_abandoned_house(building, build_data), _ => {} } } @@ -357,7 +349,7 @@ impl TownBuilder { &mut self, building: &(i32, i32, i32, i32), build_data: &mut BuilderMap, - rng: &mut RandomNumberGenerator, + to_place: &mut Vec<&str>, player_idx: usize, ) { @@ -366,7 +358,7 @@ impl TownBuilder { let idx = build_data.map.xy_idx(x, y); if build_data.map.tiles[idx] == TileType::WoodFloor && idx != player_idx - && rng.roll_dice(1, 3) == 1 + && roll_dice(1, 3) == 1 && !to_place.is_empty() { let entity_tag = to_place[0]; @@ -377,12 +369,7 @@ impl TownBuilder { } } - fn build_pub( - &mut self, - building: &(i32, i32, i32, i32), - build_data: &mut BuilderMap, - rng: &mut RandomNumberGenerator, - ) { + fn build_pub(&mut self, building: &(i32, i32, i32, i32), build_data: &mut BuilderMap) { // Place the player build_data.starting_position = Some(Position { x: building.0 + (building.2 / 2), @@ -404,15 +391,10 @@ impl TownBuilder { "Table", "Chair", ]; - self.random_building_spawn(building, build_data, rng, &mut to_place, player_idx); + self.random_building_spawn(building, build_data, &mut to_place, player_idx); } - fn build_temple( - &mut self, - building: &(i32, i32, i32, i32), - build_data: &mut BuilderMap, - rng: &mut RandomNumberGenerator, - ) { + fn build_temple(&mut self, building: &(i32, i32, i32, i32), build_data: &mut BuilderMap) { // Place items let mut to_place: Vec<&str> = vec![ "Priest", @@ -424,15 +406,10 @@ impl TownBuilder { "Candle", "Candle", ]; - self.random_building_spawn(building, build_data, rng, &mut to_place, 0); + self.random_building_spawn(building, build_data, &mut to_place, 0); } - fn build_smith( - &mut self, - building: &(i32, i32, i32, i32), - build_data: &mut BuilderMap, - rng: &mut RandomNumberGenerator, - ) { + fn build_smith(&mut self, building: &(i32, i32, i32, i32), build_data: &mut BuilderMap) { // Place items let mut to_place: Vec<&str> = vec![ "Blacksmith", @@ -441,66 +418,45 @@ impl TownBuilder { "Weapon Rack", "Armor Stand", ]; - self.random_building_spawn(building, build_data, rng, &mut to_place, 0); + self.random_building_spawn(building, build_data, &mut to_place, 0); } - fn build_clothier( - &mut self, - building: &(i32, i32, i32, i32), - build_data: &mut BuilderMap, - rng: &mut RandomNumberGenerator, - ) { + fn build_clothier(&mut self, building: &(i32, i32, i32, i32), build_data: &mut BuilderMap) { // Place items let mut to_place: Vec<&str> = vec!["Clothier", "Cabinet", "Table", "Loom", "Hide Rack"]; - self.random_building_spawn(building, build_data, rng, &mut to_place, 0); + self.random_building_spawn(building, build_data, &mut to_place, 0); } - fn build_alchemist( - &mut self, - building: &(i32, i32, i32, i32), - build_data: &mut BuilderMap, - rng: &mut RandomNumberGenerator, - ) { + fn build_alchemist(&mut self, building: &(i32, i32, i32, i32), build_data: &mut BuilderMap) { // Place items let mut to_place: Vec<&str> = vec!["Alchemist", "Chemistry Set", "Dead Thing", "Chair", "Table"]; - self.random_building_spawn(building, build_data, rng, &mut to_place, 0); + self.random_building_spawn(building, build_data, &mut to_place, 0); } - fn build_my_house( - &mut self, - building: &(i32, i32, i32, i32), - build_data: &mut BuilderMap, - rng: &mut RandomNumberGenerator, - ) { + fn build_my_house(&mut self, building: &(i32, i32, i32, i32), build_data: &mut BuilderMap) { // Place items let mut to_place: Vec<&str> = vec!["Mom", "Bed", "Cabinet", "Chair", "Table"]; - self.random_building_spawn(building, build_data, rng, &mut to_place, 0); + self.random_building_spawn(building, build_data, &mut to_place, 0); } - fn build_hovel( - &mut self, - building: &(i32, i32, i32, i32), - build_data: &mut BuilderMap, - rng: &mut RandomNumberGenerator, - ) { + fn build_hovel(&mut self, building: &(i32, i32, i32, i32), build_data: &mut BuilderMap) { // Place items let mut to_place: Vec<&str> = vec!["Peasant", "Bed", "Chair", "Table"]; - self.random_building_spawn(building, build_data, rng, &mut to_place, 0); + self.random_building_spawn(building, build_data, &mut to_place, 0); } fn build_abandoned_house( &mut self, building: &(i32, i32, i32, i32), build_data: &mut BuilderMap, - rng: &mut RandomNumberGenerator, ) { for y in building.1..building.1 + building.3 { for x in building.0..building.0 + building.2 { let idx = build_data.map.xy_idx(x, y); if build_data.map.tiles[idx] == TileType::WoodFloor && idx != 0 - && rng.roll_dice(1, 2) == 1 + && roll_dice(1, 2) == 1 { build_data.spawn_list.push((idx, "Rat".to_string())); } @@ -508,10 +464,10 @@ impl TownBuilder { } } - fn spawn_dockers(&mut self, build_data: &mut BuilderMap, rng: &mut RandomNumberGenerator) { + fn spawn_dockers(&mut self, build_data: &mut BuilderMap) { for (idx, tt) in build_data.map.tiles.iter().enumerate() { - if *tt == TileType::Bridge && rng.roll_dice(1, 6) == 1 { - match rng.roll_dice(1, 3) { + if *tt == TileType::Bridge && roll_dice(1, 6) == 1 { + match roll_dice(1, 3) { 1 => build_data.spawn_list.push((idx, "Dock Worker".to_string())), 2 => build_data .spawn_list @@ -525,12 +481,12 @@ impl TownBuilder { fn spawn_townsfolk( &mut self, build_data: &mut BuilderMap, - rng: &mut RandomNumberGenerator, + available_building_tiles: &mut HashSet, ) { for idx in available_building_tiles.iter() { - if rng.roll_dice(1, 10) == 1 { - match rng.roll_dice(1, 4) { + if roll_dice(1, 10) == 1 { + match roll_dice(1, 4) { 1 => build_data.spawn_list.push((*idx, "Peasant".to_string())), 2 => build_data.spawn_list.push((*idx, "Drunk".to_string())), 3 => build_data diff --git a/src/map_builders/voronoi.rs b/src/map_builders/voronoi.rs index 06e25b4..3accdc0 100644 --- a/src/map_builders/voronoi.rs +++ b/src/map_builders/voronoi.rs @@ -1,6 +1,5 @@ -use ::rltk::RandomNumberGenerator; - use crate::map_builders::{BuilderMap, InitialMapBuilder}; +use crate::rng::roll_dice; use crate::TileType; #[derive(PartialEq, Copy, Clone)] @@ -17,8 +16,8 @@ pub struct VoronoiCellBuilder { } impl InitialMapBuilder for VoronoiCellBuilder { - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - self.build(rng, build_data); + fn build_map(&mut self, build_data: &mut BuilderMap) { + self.build(build_data); } } @@ -44,13 +43,13 @@ impl VoronoiCellBuilder { } #[allow(clippy::map_entry)] - fn build(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + fn build(&mut self, build_data: &mut BuilderMap) { // Make a Voronoi diagram. We'll do this the hard way to learn about the technique! let mut voronoi_seeds: Vec<(usize, rltk::Point)> = Vec::new(); while voronoi_seeds.len() < self.n_seeds { - let vx = rng.roll_dice(1, build_data.map.width - 1); - let vy = rng.roll_dice(1, build_data.map.height - 1); + let vx = roll_dice(1, build_data.map.width - 1); + let vy = roll_dice(1, build_data.map.height - 1); let vidx = build_data.map.xy_idx(vx, vy); let candidate = (vidx, rltk::Point::new(vx, vy)); if !voronoi_seeds.contains(&candidate) { diff --git a/src/map_builders/voronoi_spawning.rs b/src/map_builders/voronoi_spawning.rs index 65b18bb..5af1cb4 100644 --- a/src/map_builders/voronoi_spawning.rs +++ b/src/map_builders/voronoi_spawning.rs @@ -1,15 +1,16 @@ use std::collections::HashMap; -use ::rltk::RandomNumberGenerator; +use ::rltk::{CellularDistanceFunction, FastNoise, NoiseType}; use crate::map_builders::{BuilderMap, MetaMapBuilder}; +use crate::rng::roll_dice; use crate::{spawner, TileType}; pub struct VoronoiSpawning {} impl MetaMapBuilder for VoronoiSpawning { - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - self.build(rng, build_data); + fn build_map(&mut self, build_data: &mut BuilderMap) { + self.build(build_data); } } @@ -20,12 +21,12 @@ impl VoronoiSpawning { } #[allow(clippy::map_entry)] - fn build(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + fn build(&mut self, build_data: &mut BuilderMap) { let mut noise_areas: HashMap> = HashMap::new(); - let mut noise = rltk::FastNoise::seeded(rng.roll_dice(1, 65536) as u64); - noise.set_noise_type(rltk::NoiseType::Cellular); + let mut noise = FastNoise::seeded(roll_dice(1, 65536) as u64); + noise.set_noise_type(NoiseType::Cellular); noise.set_frequency(0.08); - noise.set_cellular_distance_function(rltk::CellularDistanceFunction::Manhattan); + noise.set_cellular_distance_function(CellularDistanceFunction::Manhattan); for y in 1..build_data.map.height - 1 { for x in 1..build_data.map.width - 1 { @@ -47,7 +48,6 @@ impl VoronoiSpawning { for area in noise_areas.iter() { spawner::spawn_region( &build_data.map, - rng, area.1, build_data.map.depth, &mut build_data.spawn_list, diff --git a/src/map_builders/waveform_collapse.rs b/src/map_builders/waveform_collapse.rs index 949829b..c3f71e4 100644 --- a/src/map_builders/waveform_collapse.rs +++ b/src/map_builders/waveform_collapse.rs @@ -2,7 +2,6 @@ mod common; mod constraints; mod solver; -use ::rltk::RandomNumberGenerator; use common::*; use constraints::*; use solver::*; @@ -13,8 +12,8 @@ use crate::Map; pub struct WaveformCollapseBuilder {} impl MetaMapBuilder for WaveformCollapseBuilder { - fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - self.build(rng, build_data); + fn build_map(&mut self, build_data: &mut BuilderMap) { + self.build(build_data); } } @@ -25,7 +24,7 @@ impl WaveformCollapseBuilder { Box::new(WaveformCollapseBuilder {}) } - fn build(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + fn build(&mut self, build_data: &mut BuilderMap) { const CHUNK_SIZE: i32 = 8; build_data.take_snapshot(); @@ -47,7 +46,7 @@ impl WaveformCollapseBuilder { let mut tries = 0; loop { let mut solver = Solver::new(constraints.clone(), CHUNK_SIZE, &build_data.map); - while !solver.iteration(&mut build_data.map, rng) { + while !solver.iteration(&mut build_data.map) { build_data.take_snapshot(); } build_data.take_snapshot(); diff --git a/src/map_builders/waveform_collapse/solver.rs b/src/map_builders/waveform_collapse/solver.rs index 69ef045..4a673e0 100644 --- a/src/map_builders/waveform_collapse/solver.rs +++ b/src/map_builders/waveform_collapse/solver.rs @@ -1,6 +1,7 @@ use std::collections::HashSet; use super::MapChunk; +use crate::rng::roll_dice; use crate::Map; pub struct Solver { @@ -71,7 +72,7 @@ impl Solver { neighbors } - pub fn iteration(&mut self, map: &mut Map, rng: &mut rltk::RandomNumberGenerator) -> bool { + pub fn iteration(&mut self, map: &mut Map) -> bool { if self.remaining.is_empty() { return true; } @@ -95,7 +96,7 @@ impl Solver { // Pick a random chunk we haven't dealt with yet and get its index, remove from remaining list let remaining_index = if !neighbors_exist { - (rng.roll_dice(1, self.remaining.len() as i32) - 1) as usize + (roll_dice(1, self.remaining.len() as i32) - 1) as usize } else { 0_usize }; @@ -154,7 +155,7 @@ impl Solver { if neighbors == 0 { // There is nothing nearby, so we can have anything! - let new_chunk_idx = (rng.roll_dice(1, self.constraints.len() as i32) - 1) as usize; + let new_chunk_idx = (roll_dice(1, self.constraints.len() as i32) - 1) as usize; self.chunks[chunk_index] = Some(new_chunk_idx); let left_x = chunk_x as i32 * self.chunk_size; let right_x = (chunk_x + 1) as i32 * self.chunk_size; @@ -202,7 +203,7 @@ impl Solver { let new_chunk_idx = if possible_options.len() == 1 { 0 } else { - rng.roll_dice(1, possible_options.len() as i32) - 1 + roll_dice(1, possible_options.len() as i32) - 1 }; self.chunks[chunk_index] = Some(new_chunk_idx as usize); let left_x = chunk_x as i32 * self.chunk_size; diff --git a/src/player.rs b/src/player.rs index 020b0c2..240ec92 100644 --- a/src/player.rs +++ b/src/player.rs @@ -1,7 +1,7 @@ //! Player-related functionality use std::cmp::{max, min}; -use rltk::{DistanceAlg, Point, RandomNumberGenerator, Rltk, VirtualKeyCode}; +use rltk::{DistanceAlg, Point, Rltk, VirtualKeyCode}; use specs::prelude::*; use crate::components::{ @@ -10,6 +10,7 @@ use crate::components::{ WantsToMelee, WantsToPickupItem, WantsToShoot, }; use crate::raws::{self, Reaction, RAWS}; +use crate::rng::roll_dice; use crate::{ colors, gamelog, spatial, Map, RunState, State, TileType, VendorMode, WantsToCastSpell, Weapon, }; @@ -358,8 +359,7 @@ fn skip_turn(ecs: &mut World) -> RunState { let pools = health_components.get_mut(*player_entity).unwrap(); pools.hit_points.current = i32::min(pools.hit_points.current + 1, pools.hit_points.max); - let mut rng = ecs.fetch_mut::(); - if rng.roll_dice(1, 6) == 1 { + if roll_dice(1, 6) == 1 { pools.mana.current = i32::min(pools.mana.current + 1, pools.mana.max); } } diff --git a/src/random_table.rs b/src/random_table.rs index b37b100..5db99bd 100644 --- a/src/random_table.rs +++ b/src/random_table.rs @@ -1,7 +1,6 @@ //! Weighted random generation of entities -use ::rltk::RandomNumberGenerator; - use crate::raws::{spawn_type_by_name, RawMaster, SpawnTableType}; +use crate::rng::roll_dice; pub struct RandomEntry { name: String, @@ -41,12 +40,11 @@ impl MasterTable { } } - pub fn roll(&self, rng: &mut RandomNumberGenerator) -> String { - let roll = rng.roll_dice(1, 4); - match roll { - 1 => self.items.roll(rng), - 2 => self.props.roll(rng), - 3 => self.mobs.roll(rng), + pub fn roll(&self) -> String { + match roll_dice(1, 4) { + 1 => self.items.roll(), + 2 => self.props.roll(), + 3 => self.mobs.roll(), _ => "None".to_string(), } } @@ -73,12 +71,12 @@ impl RandomTable { } } - pub fn roll(&self, rng: &mut RandomNumberGenerator) -> String { + pub fn roll(&self) -> String { if self.total_weight == 0 { return "None".to_string(); } - let mut roll = rng.roll_dice(1, self.total_weight) - 1; + let mut roll = roll_dice(1, self.total_weight) - 1; let mut index: usize = 0; while roll > 0 { diff --git a/src/raws/rawmaster.rs b/src/raws/rawmaster.rs index 5e68c5a..b99c5b4 100644 --- a/src/raws/rawmaster.rs +++ b/src/raws/rawmaster.rs @@ -1,7 +1,7 @@ use std::collections::{HashMap, HashSet}; use ::regex::Regex; -use ::rltk::{console, RandomNumberGenerator, RGB}; +use ::rltk::{console, RGB}; use ::specs::prelude::*; use ::specs::saveload::{MarkedBuilder, SimpleMarker}; @@ -10,6 +10,7 @@ use crate::components::*; use crate::gamesystem::{mana_at_level, npc_hp}; use crate::map::MasterDungeonMap; use crate::random_table::{MasterTable, RandomTable}; +use crate::rng::roll_dice; pub fn parse_dice_string(dice: &str) -> (i32, i32, i32) { lazy_static! { @@ -714,9 +715,8 @@ pub fn spawn_named_mob( total_weight: 0., total_initiative_penalty: 0., gold: if let Some(gold) = &mob_template.gold { - let mut rng = RandomNumberGenerator::new(); let (n, d, b) = parse_dice_string(gold); - (rng.roll_dice(n, d) + b) as f32 + (roll_dice(n, d) + b) as f32 } else { 0.0 }, @@ -975,11 +975,7 @@ pub fn get_spawn_table_for_depth(raws: &RawMaster, depth: i32) -> MasterTable { rt } -pub fn get_item_drop( - raws: &RawMaster, - rng: &mut RandomNumberGenerator, - table: &str, -) -> Option { +pub fn get_item_drop(raws: &RawMaster, table: &str) -> Option { if raws.loot_index.contains_key(table) { let mut rt = RandomTable::new(); let available_options = &raws.raws.loot_tables[raws.loot_index[table]]; @@ -987,7 +983,7 @@ pub fn get_item_drop( rt.add(item.name.clone(), item.weight); } - return Some(rt.roll(rng)); + return Some(rt.roll()); } None diff --git a/src/rng.rs b/src/rng.rs index b942b92..b3710ee 100644 --- a/src/rng.rs +++ b/src/rng.rs @@ -1,10 +1,12 @@ -use ::rltk::prelude::*; use std::sync::Mutex; +use ::rltk::prelude::*; + lazy_static! { static ref RNG: Mutex = Mutex::new(RandomNumberGenerator::new()); } +#[allow(dead_code)] pub fn reseed(seed: u64) { *RNG.lock().unwrap() = RandomNumberGenerator::seeded(seed); } diff --git a/src/spawner.rs b/src/spawner.rs index 7988e04..ad7f179 100644 --- a/src/spawner.rs +++ b/src/spawner.rs @@ -1,7 +1,7 @@ //! Spawns things use std::collections::HashMap; -use ::rltk::{Point, RandomNumberGenerator}; +use ::rltk::prelude::*; use ::specs::prelude::*; use ::specs::saveload::{MarkedBuilder, SimpleMarker}; @@ -10,6 +10,7 @@ use crate::random_table::MasterTable; use crate::raws::{ get_spawn_table_for_depth, spawn_all_spells, spawn_named_entity, SpawnType, RAWS, }; +use crate::rng::roll_dice; use crate::{colors, Map, MasterDungeonMap, Rect, TileType}; /// Spawns the player and returns their entity object @@ -19,7 +20,7 @@ pub fn player(ecs: &mut World, player_x: i32, player_y: i32) -> Entity { .create_entity() .with(Position::from((player_x, player_y))) .with(Renderable { - glyph: rltk::to_cp437('@'), + glyph: to_cp437('@'), fg: colors::YELLOW, bg: colors::BLACK, render_order: 0, @@ -84,13 +85,7 @@ fn room_table(map_depth: i32) -> MasterTable { /// fills a room with stuff! #[allow(clippy::map_entry)] -pub fn spawn_room( - map: &Map, - rng: &mut RandomNumberGenerator, - room: &Rect, - map_depth: i32, - spawn_list: &mut Vec<(usize, String)>, -) { +pub fn spawn_room(map: &Map, room: &Rect, map_depth: i32, spawn_list: &mut Vec<(usize, String)>) { let mut possible_targets: Vec = Vec::new(); // Borrow scope - to keep access to the map separated @@ -105,12 +100,11 @@ pub fn spawn_room( } } - spawn_region(map, rng, &possible_targets, map_depth, spawn_list); + spawn_region(map, &possible_targets, map_depth, spawn_list); } pub fn spawn_region( _map: &Map, - rng: &mut RandomNumberGenerator, area: &[usize], map_depth: i32, spawn_list: &mut Vec<(usize, String)>, @@ -123,7 +117,7 @@ pub fn spawn_region( { let num_spawns = i32::min( areas.len() as i32, - rng.roll_dice(1, MAX_MONSTERS + 3) + (map_depth - 1) - 3, + roll_dice(1, MAX_MONSTERS + 3) + (map_depth - 1) - 3, ); if num_spawns == 0 { return; @@ -133,10 +127,10 @@ pub fn spawn_region( let array_index = if areas.len() == 1 { 0_usize } else { - (rng.roll_dice(1, areas.len() as i32) - 1) as usize + (roll_dice(1, areas.len() as i32) - 1) as usize }; let map_idx = areas[array_index]; - spawn_points.insert(map_idx, spawn_table.roll(rng)); + spawn_points.insert(map_idx, spawn_table.roll()); areas.remove(array_index); } } @@ -169,7 +163,7 @@ pub fn spawn_entity(ecs: &mut World, spawn: &(&usize, &String)) { ); if item_result.is_none() { - ::rltk::console::log(format!("WARNING: We don't know how to spawn [{}]!", name)); + console::log(format!("WARNING: We don't know how to spawn [{}]!", name)); } } @@ -205,7 +199,7 @@ pub fn spawn_town_portal(ecs: &mut World) { depth: 1, }) .with(Renderable { - glyph: ::rltk::to_cp437('♥'), + glyph: to_cp437('♥'), fg: colors::CYAN, bg: colors::BLACK, render_order: 0, diff --git a/src/systems/ai/default_move_system.rs b/src/systems/ai/default_move_system.rs index 851acb4..717de79 100644 --- a/src/systems/ai/default_move_system.rs +++ b/src/systems/ai/default_move_system.rs @@ -1,7 +1,7 @@ -use ::rltk::RandomNumberGenerator; use ::specs::prelude::*; use crate::components::{ApplyMove, MoveMode, Movement, MyTurn, Position}; +use crate::rng::roll_dice; use crate::{spatial, tile_walkable, Map}; pub struct DefaultMoveAI {} @@ -13,13 +13,12 @@ impl<'a> System<'a> for DefaultMoveAI { WriteStorage<'a, MoveMode>, ReadStorage<'a, Position>, ReadExpect<'a, Map>, - WriteExpect<'a, RandomNumberGenerator>, WriteStorage<'a, ApplyMove>, Entities<'a>, ); fn run(&mut self, data: Self::SystemData) { - let (mut turns, mut move_mode, positions, map, mut rng, mut apply_move, entities) = data; + let (mut turns, mut move_mode, positions, map, mut apply_move, entities) = data; let mut turn_done: Vec = Vec::new(); for (entity, pos, mut mode, _myturn) in @@ -33,7 +32,7 @@ impl<'a> System<'a> for DefaultMoveAI { let mut x = pos.x; let mut y = pos.y; - match rng.roll_dice(1, 5) { + match roll_dice(1, 5) { 1 => x -= 1, 2 => x += 1, 3 => y -= 1, @@ -67,8 +66,8 @@ impl<'a> System<'a> for DefaultMoveAI { mode.mode = Movement::RandomWaypoint { path: None }; } } else { - let target_x = rng.roll_dice(1, map.width - 2); - let target_y = rng.roll_dice(1, map.height - 2); + let target_x = roll_dice(1, map.width - 2); + let target_y = roll_dice(1, map.height - 2); let idx = map.xy_idx(target_x, target_y); if tile_walkable(map.tiles[idx]) { let path = ::rltk::a_star_search( diff --git a/src/systems/ai/initiative_system.rs b/src/systems/ai/initiative_system.rs index f63511e..6c9b99d 100644 --- a/src/systems/ai/initiative_system.rs +++ b/src/systems/ai/initiative_system.rs @@ -1,10 +1,11 @@ -use ::rltk::{DistanceAlg, Point, RandomNumberGenerator}; +use ::rltk::prelude::*; use ::specs::prelude::*; use crate::components::{ Attributes, DamageOverTime, Duration, EquipmentChanged, Initiative, MyTurn, Pools, Position, StatusEffect, }; +use crate::rng::roll_dice; use crate::RunState; pub struct InitiativeSystem {} @@ -16,7 +17,6 @@ impl<'a> System<'a> for InitiativeSystem { ReadStorage<'a, Position>, WriteStorage<'a, MyTurn>, Entities<'a>, - WriteExpect<'a, RandomNumberGenerator>, ReadStorage<'a, Attributes>, WriteExpect<'a, RunState>, ReadExpect<'a, Entity>, @@ -34,7 +34,6 @@ impl<'a> System<'a> for InitiativeSystem { positions, mut turns, entities, - mut rng, attributes, mut runstate, player, @@ -60,7 +59,7 @@ impl<'a> System<'a> for InitiativeSystem { let mut myturn = true; // Re-roll - initiative.current = 6 + rng.roll_dice(1, 6); + initiative.current = 6 + roll_dice(1, 6); // Give a bonus for quickness if let Some(attr) = attributes.get(entity) { diff --git a/src/systems/ai/quipping.rs b/src/systems/ai/quipping.rs index 51705cd..32b664d 100644 --- a/src/systems/ai/quipping.rs +++ b/src/systems/ai/quipping.rs @@ -1,8 +1,9 @@ -use ::rltk::{Point, RandomNumberGenerator}; +use ::rltk::prelude::*; use ::specs::prelude::*; use crate::components::{MyTurn, Name, Quips, Viewshed}; use crate::gamelog; +use crate::rng::roll_dice; pub struct QuipSystem {} @@ -14,21 +15,20 @@ impl<'a> System<'a> for QuipSystem { ReadStorage<'a, MyTurn>, ReadExpect<'a, Point>, ReadStorage<'a, Viewshed>, - WriteExpect<'a, RandomNumberGenerator>, ); fn run(&mut self, data: Self::SystemData) { - let (mut quips, names, turns, player_pos, viewsheds, mut rng) = data; + let (mut quips, names, turns, player_pos, viewsheds) = data; for (quip, name, viewshed, _turn) in (&mut quips, &names, &viewsheds, &turns).join() { if !quip.available.is_empty() && viewshed.visible_tiles.contains(&player_pos) - && rng.roll_dice(1, 6) == 1 + && roll_dice(1, 6) == 1 { let quip_index = if quip.available.len() == 1 { 0 } else { - (rng.roll_dice(1, quip.available.len() as i32) - 1) as usize + (roll_dice(1, quip.available.len() as i32) - 1) as usize }; gamelog::Logger::new() diff --git a/src/systems/ai/visible_ai_system.rs b/src/systems/ai/visible_ai_system.rs index 6445576..dcb9bfd 100644 --- a/src/systems/ai/visible_ai_system.rs +++ b/src/systems/ai/visible_ai_system.rs @@ -1,11 +1,12 @@ -use rltk::{console, DistanceAlg, Point, RandomNumberGenerator}; -use specs::prelude::*; +use ::rltk::prelude::*; +use ::specs::prelude::*; use crate::components::{ Chasing, Equipped, Faction, MyTurn, Name, Position, SpecialAbilities, SpellTemplate, Viewshed, WantsToApproach, WantsToCastSpell, WantsToFlee, WantsToShoot, Weapon, }; use crate::raws::{self, find_spell_entity_by_name, Reaction, RAWS}; +use crate::rng::roll_dice; use crate::{spatial, Map}; pub struct VisibleAI {} @@ -24,7 +25,6 @@ impl<'a> System<'a> for VisibleAI { ReadStorage<'a, Viewshed>, WriteStorage<'a, Chasing>, ReadStorage<'a, SpecialAbilities>, - WriteExpect<'a, RandomNumberGenerator>, WriteStorage<'a, WantsToCastSpell>, ReadStorage<'a, Name>, ReadStorage<'a, SpellTemplate>, @@ -46,7 +46,6 @@ impl<'a> System<'a> for VisibleAI { viewsheds, mut chasing, abilities, - mut rng, mut casting, names, spells, @@ -84,7 +83,7 @@ impl<'a> System<'a> for VisibleAI { for ability in abilities.abilities.iter() { if range >= ability.min_range && range <= ability.range - && rng.roll_dice(1, 100) >= (ability.chance * 100.0) as i32 + && roll_dice(1, 100) >= (ability.chance * 100.0) as i32 { casting .insert( diff --git a/src/systems/melee_combat_system.rs b/src/systems/melee_combat_system.rs index 965936e..cf26fe7 100644 --- a/src/systems/melee_combat_system.rs +++ b/src/systems/melee_combat_system.rs @@ -1,4 +1,3 @@ -use ::rltk::RandomNumberGenerator; use ::specs::prelude::*; use crate::components::{ @@ -7,6 +6,7 @@ use crate::components::{ }; use crate::effects::{add_effect, EffectType, Targets}; use crate::gamesystem::skill_bonus; +use crate::rng::roll_dice; use crate::{colors, gamelog}; pub struct MeleeCombatSystem {} @@ -21,7 +21,6 @@ impl<'a> System<'a> for MeleeCombatSystem { ReadStorage<'a, Skills>, ReadStorage<'a, HungerClock>, ReadStorage<'a, Pools>, - WriteExpect<'a, RandomNumberGenerator>, ReadStorage<'a, Equipped>, ReadStorage<'a, Weapon>, ReadStorage<'a, Wearable>, @@ -37,7 +36,6 @@ impl<'a> System<'a> for MeleeCombatSystem { skills, hunger_clock, pools, - mut rng, equipped_items, meleeweapons, wearables, @@ -78,7 +76,7 @@ impl<'a> System<'a> for MeleeCombatSystem { let attack_index = if nat.attacks.len() == 1 { 0 } else { - rng.roll_dice(1, nat.attacks.len() as i32) as usize - 1 + roll_dice(1, nat.attacks.len() as i32) as usize - 1 }; let attk = &nat.attacks[attack_index]; @@ -100,7 +98,7 @@ impl<'a> System<'a> for MeleeCombatSystem { } } - let natural_roll = rng.roll_dice(1, 20); + let natural_roll = roll_dice(1, 20); let attribute_hit_bonus = if weapon_info.attribute == WeaponAttribute::Might { attacker_attributes.might.bonus } else { @@ -140,7 +138,7 @@ impl<'a> System<'a> for MeleeCombatSystem { if natural_roll != 1 && (natural_roll == 20 || modified_hit_roll > armor_class) { // Target hit! Until we support weapons, we're going with 1d4 - let base_damage = rng.roll_dice(1, 4); + let base_damage = roll_dice(1, 4); let attr_damage_bonus = attacker_attributes.might.bonus; let skill_damage_bonus = skill_bonus(Skill::Melee, &*attacker_skills); let weapon_damage_bonus = weapon_info.damage_bonus; @@ -171,7 +169,7 @@ impl<'a> System<'a> for MeleeCombatSystem { // Proc effects if let Some(chance) = &weapon_info.proc_chance { - if rng.roll_dice(1, 100) <= (chance * 100.0) as i32 { + if roll_dice(1, 100) <= (chance * 100.0) as i32 { let effect_target = if weapon_info.proc_target.unwrap() == "Self" { Targets::Single { target: entity } } else { diff --git a/src/systems/ranged_combat_system.rs b/src/systems/ranged_combat_system.rs index dd315ba..7446f34 100644 --- a/src/systems/ranged_combat_system.rs +++ b/src/systems/ranged_combat_system.rs @@ -1,5 +1,5 @@ +use ::rltk::prelude::*; use ::specs::prelude::*; -use rltk::{to_cp437, LineAlg, Point, RandomNumberGenerator}; use crate::components::{ Attributes, EquipmentSlot, Equipped, HungerClock, HungerState, Name, NaturalAttackDefense, @@ -7,6 +7,7 @@ use crate::components::{ }; use crate::effects::{add_effect, EffectType, Targets}; use crate::gamesystem::skill_bonus; +use crate::rng::roll_dice; use crate::{colors, gamelog, Map}; pub struct RangedCombatSystem {} @@ -21,7 +22,6 @@ impl<'a> System<'a> for RangedCombatSystem { ReadStorage<'a, Skills>, ReadStorage<'a, HungerClock>, ReadStorage<'a, Pools>, - WriteExpect<'a, RandomNumberGenerator>, ReadStorage<'a, Equipped>, ReadStorage<'a, Weapon>, ReadStorage<'a, Wearable>, @@ -39,7 +39,6 @@ impl<'a> System<'a> for RangedCombatSystem { skills, hunger_clock, pools, - mut rng, equipped_items, weapons, wearables, @@ -104,7 +103,7 @@ impl<'a> System<'a> for RangedCombatSystem { let attack_index = if nat.attacks.len() == 1 { 0 } else { - rng.roll_dice(1, nat.attacks.len() as i32) as usize - 1 + roll_dice(1, nat.attacks.len() as i32) as usize - 1 }; let attk = &nat.attacks[attack_index]; @@ -125,7 +124,7 @@ impl<'a> System<'a> for RangedCombatSystem { } } - let natural_roll = rng.roll_dice(1, 20); + let natural_roll = roll_dice(1, 20); let attribute_hit_bonus = if weapon_info.attribute == WeaponAttribute::Might { attacker_attributes.might.bonus } else { @@ -165,7 +164,7 @@ impl<'a> System<'a> for RangedCombatSystem { if natural_roll != 1 && (natural_roll == 20 || modified_hit_roll > armor_class) { // Target hit! Until we support weapons, we're going with 1d4 - let base_damage = rng.roll_dice(1, 4); + let base_damage = roll_dice(1, 4); let attr_damage_bonus = attacker_attributes.might.bonus; let skill_damage_bonus = skill_bonus(Skill::Melee, &*attacker_skills); let weapon_damage_bonus = weapon_info.damage_bonus; @@ -195,7 +194,7 @@ impl<'a> System<'a> for RangedCombatSystem { .log(); // Proc effects if let Some(chance) = &weapon_info.proc_chance { - if rng.roll_dice(1, 100) <= (chance * 100.0) as i32 { + if roll_dice(1, 100) <= (chance * 100.0) as i32 { let effect_target = if weapon_info.proc_target.unwrap() == "Self" { Targets::Single { target: entity } } else { diff --git a/src/systems/visibility_system.rs b/src/systems/visibility_system.rs index c2f93a0..823e223 100644 --- a/src/systems/visibility_system.rs +++ b/src/systems/visibility_system.rs @@ -1,7 +1,8 @@ -use ::rltk::{field_of_view, Point, RandomNumberGenerator}; +use ::rltk::prelude::*; use ::specs::prelude::*; use crate::components::{BlocksVisibility, Hidden, Name}; +use crate::rng::roll_dice; use crate::{colors, gamelog, spatial, Map, Player, Position, Viewshed}; pub struct VisibilitySystem {} @@ -15,23 +16,13 @@ impl<'a> System<'a> for VisibilitySystem { ReadStorage<'a, Position>, ReadStorage<'a, Player>, WriteStorage<'a, Hidden>, - WriteExpect<'a, RandomNumberGenerator>, ReadStorage<'a, Name>, ReadStorage<'a, BlocksVisibility>, ); fn run(&mut self, data: Self::SystemData) { - let ( - mut map, - entities, - mut viewshed, - pos, - player, - mut hidden, - mut rng, - names, - blocks_visibility, - ) = data; + let (mut map, entities, mut viewshed, pos, player, mut hidden, names, blocks_visibility) = + data; map.view_blocked.clear(); for (block_pos, _block) in (&pos, &blocks_visibility).join() { @@ -64,7 +55,7 @@ impl<'a> System<'a> for VisibilitySystem { spatial::for_each_tile_content(idx, |e| { let maybe_hidden = hidden.get(e); if let Some(_maybe_hidden) = maybe_hidden { - if rng.roll_dice(1, 24) == 1 { + if roll_dice(1, 24) == 1 { let name = names.get(e); if let Some(name) = name { gamelog::line("You spotted:")