From f14674ee9ae2e7a27f142d66d66a21d3aba4459a Mon Sep 17 00:00:00 2001 From: Joshua Barretto Date: Sat, 3 Aug 2019 22:11:31 +0100 Subject: [PATCH] Switched to UnitChooser --- world/src/block/natural.rs | 17 +++-------------- world/src/column/mod.rs | 6 ++++-- world/src/util/mod.rs | 2 ++ world/src/util/unit_chooser.rs | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 16 deletions(-) create mode 100644 world/src/util/unit_chooser.rs diff --git a/world/src/block/natural.rs b/world/src/block/natural.rs index 1ecac53823..557015a92c 100644 --- a/world/src/block/natural.rs +++ b/world/src/block/natural.rs @@ -2,7 +2,7 @@ use super::{BlockGen, StructureInfo, StructureMeta, ZCache}; use crate::{ all::ForestKind, column::{ColumnGen, ColumnSample}, - util::{HashCache, RandomPerm, Sampler}, + util::{HashCache, RandomPerm, Sampler, UnitChooser}, CONFIG, }; use common::{assets, terrain::Structure}; @@ -11,7 +11,7 @@ use std::sync::Arc; use vek::*; static VOLUME_RAND: RandomPerm = RandomPerm::new(0xDB21C052); -static UNIT_RAND: RandomPerm = RandomPerm::new(0x700F4EC7); +static UNIT_CHOOSER: UnitChooser = UnitChooser::new(0x700F4EC7); static QUIRKY_RAND: RandomPerm = RandomPerm::new(0xA634460F); pub fn structure_gen<'a>( @@ -61,22 +61,11 @@ pub fn structure_gen<'a>( } }; - const UNIT_CHOICES: [(Vec2, Vec2); 8] = [ - (Vec2 { x: 1, y: 0 }, Vec2 { x: 0, y: 1 }), - (Vec2 { x: 1, y: 0 }, Vec2 { x: 0, y: -1 }), - (Vec2 { x: -1, y: 0 }, Vec2 { x: 0, y: 1 }), - (Vec2 { x: -1, y: 0 }, Vec2 { x: 0, y: -1 }), - (Vec2 { x: 0, y: 1 }, Vec2 { x: 1, y: 0 }), - (Vec2 { x: 0, y: 1 }, Vec2 { x: -1, y: 0 }), - (Vec2 { x: 0, y: -1 }, Vec2 { x: 1, y: 0 }), - (Vec2 { x: 0, y: -1 }, Vec2 { x: -1, y: 0 }), - ]; - Some(StructureInfo { pos: st_pos3d, seed: st_seed, meta: StructureMeta::Volume { - units: UNIT_CHOICES[UNIT_RAND.get(st_seed) as usize % UNIT_CHOICES.len()], + units: UNIT_CHOOSER.get(st_seed), volume: &volumes[(VOLUME_RAND.get(st_seed) / 13) as usize % volumes.len()], }, }) diff --git a/world/src/column/mod.rs b/world/src/column/mod.rs index 02114eddde..443739b17d 100644 --- a/world/src/column/mod.rs +++ b/world/src/column/mod.rs @@ -2,7 +2,7 @@ use crate::{ all::ForestKind, block::StructureMeta, sim::{LocationInfo, SimChunk}, - util::Sampler, + util::{Sampler, UnitChooser}, World, CONFIG, }; use common::{ @@ -23,6 +23,8 @@ pub struct ColumnGen<'a> { world: &'a World, } +static UNIT_CHOOSER: UnitChooser = UnitChooser::new(0x700F4EC7); + lazy_static! { pub static ref DUNGEONS: Vec> = vec![ // green oaks @@ -65,7 +67,7 @@ impl<'a> ColumnGen<'a> { pos, seed, meta: Some(StructureMeta::Volume { - units: (Vec2::unit_x(), Vec2::unit_y()), + units: UNIT_CHOOSER.get(seed), volume: &DUNGEONS[0], }), }) diff --git a/world/src/util/mod.rs b/world/src/util/mod.rs index fd538e21ce..a8a71d0c03 100644 --- a/world/src/util/mod.rs +++ b/world/src/util/mod.rs @@ -2,6 +2,7 @@ pub mod hash_cache; pub mod random; pub mod sampler; pub mod structure; +pub mod unit_chooser; // Reexports pub use self::{ @@ -9,4 +10,5 @@ pub use self::{ random::{RandomField, RandomPerm}, sampler::{Sampler, SamplerMut}, structure::StructureGen2d, + unit_chooser::UnitChooser, }; diff --git a/world/src/util/unit_chooser.rs b/world/src/util/unit_chooser.rs new file mode 100644 index 0000000000..c5c0203090 --- /dev/null +++ b/world/src/util/unit_chooser.rs @@ -0,0 +1,34 @@ +use super::{RandomPerm, Sampler}; +use vek::*; + +const UNIT_CHOICES: [(Vec2, Vec2); 8] = [ + (Vec2 { x: 1, y: 0 }, Vec2 { x: 0, y: 1 }), + (Vec2 { x: 1, y: 0 }, Vec2 { x: 0, y: -1 }), + (Vec2 { x: -1, y: 0 }, Vec2 { x: 0, y: 1 }), + (Vec2 { x: -1, y: 0 }, Vec2 { x: 0, y: -1 }), + (Vec2 { x: 0, y: 1 }, Vec2 { x: 1, y: 0 }), + (Vec2 { x: 0, y: 1 }, Vec2 { x: -1, y: 0 }), + (Vec2 { x: 0, y: -1 }, Vec2 { x: 1, y: 0 }), + (Vec2 { x: 0, y: -1 }, Vec2 { x: -1, y: 0 }), +]; + +pub struct UnitChooser { + perm: RandomPerm, +} + +impl UnitChooser { + pub const fn new(seed: u32) -> Self { + Self { + perm: RandomPerm::new(seed), + } + } +} + +impl Sampler for UnitChooser { + type Index = u32; + type Sample = (Vec2, Vec2); + + fn get(&self, perm: Self::Index) -> Self::Sample { + UNIT_CHOICES[self.perm.get(perm) as usize % UNIT_CHOICES.len()] + } +}