From 41b77a9b107198a482d9b80cda05f3a186af24d3 Mon Sep 17 00:00:00 2001 From: Joshua Barretto Date: Wed, 26 Feb 2020 22:44:30 +0000 Subject: [PATCH] Removed old settlement code, removed zcache from dependency of site generators for forward compatibility --- common/src/sys/agent.rs | 5 +- world/src/lib.rs | 18 +++++-- world/src/sim/mod.rs | 1 - world/src/sim/settlement.rs | 86 -------------------------------- world/src/site/mod.rs | 13 +++-- world/src/site/settlement/mod.rs | 34 +++++++------ 6 files changed, 42 insertions(+), 115 deletions(-) delete mode 100644 world/src/sim/settlement.rs diff --git a/common/src/sys/agent.rs b/common/src/sys/agent.rs index b47f05f2b9..941cff84ab 100644 --- a/common/src/sys/agent.rs +++ b/common/src/sys/agent.rs @@ -109,7 +109,10 @@ impl<'a> System<'a> for Sys { .ray( pos.0 + Vec3::unit_z(), pos.0 - + Vec3::from(*bearing).normalized() * 1.5 + + Vec3::from(*bearing) + .try_normalized() + .unwrap_or(Vec3::zero()) + * 1.5 + Vec3::unit_z(), ) .until(|block| block.is_solid()) diff --git a/world/src/lib.rs b/world/src/lib.rs index 00e627bd46..2d23d2aba3 100644 --- a/world/src/lib.rs +++ b/world/src/lib.rs @@ -136,10 +136,20 @@ impl World { } } - sim_chunk - .sites - .iter() - .for_each(|site| site.apply_to(chunk_wpos2d, &zcache_grid, &mut chunk)); + // Apply site generation + sim_chunk.sites.iter().for_each(|site| { + site.apply_to( + chunk_wpos2d, + |offs| { + zcache_grid + .get(offs) + .map(Option::as_ref) + .flatten() + .map(|zc| &zc.sample) + }, + &mut chunk, + ) + }); let gen_entity_pos = || { let lpos2d = TerrainChunkSize::RECT_SIZE diff --git a/world/src/sim/mod.rs b/world/src/sim/mod.rs index 16552bd397..5eae203b22 100644 --- a/world/src/sim/mod.rs +++ b/world/src/sim/mod.rs @@ -2,7 +2,6 @@ mod diffusion; mod erosion; mod location; mod map; -mod settlement; mod util; // Reexports diff --git a/world/src/sim/settlement.rs b/world/src/sim/settlement.rs deleted file mode 100644 index 951350bcfb..0000000000 --- a/world/src/sim/settlement.rs +++ /dev/null @@ -1,86 +0,0 @@ -use rand::Rng; -use vek::*; - -#[derive(Clone, Debug)] -pub struct Settlement { - lot: Lot, -} - -impl Settlement { - pub fn generate(rng: &mut impl Rng) -> Self { - Self { - lot: Lot::generate(0, 32.0, 1.0, rng), - } - } - - pub fn get_at(&self, pos: Vec2) -> Option<&Building> { self.lot.get_at(pos) } -} - -#[derive(Clone, Debug)] -pub struct Building { - pub seed: u32, -} - -#[derive(Clone, Debug)] -enum Lot { - None, - One(Building), - Many { split_x: bool, lots: Vec }, -} - -impl Lot { - pub fn generate(deep: usize, depth: f32, aspect: f32, rng: &mut impl Rng) -> Self { - let depth = if deep < 3 { 8.0 } else { depth }; - - if (depth < 1.0 || deep > 6) && !(deep < 3 || deep % 2 == 1) { - if rng.gen::() < 0.5 { - Lot::One(Building { seed: rng.gen() }) - } else { - Lot::None - } - } else { - Lot::Many { - split_x: aspect > 1.0, - lots: { - let pow2 = 1 + rng.gen::() % 1; - let n = 1 << pow2; - - let new_aspect = if aspect > 1.0 { - aspect / n as f32 - } else { - aspect * n as f32 - }; - - let vari = (rng.gen::() - 0.35) * 2.8; - let new_depth = depth * 0.5 * (1.0 + vari); - - (0..n) - .map(|_| Lot::generate(deep + 1, new_depth, new_aspect, rng)) - .collect() - }, - } - } - } - - pub fn get_at(&self, pos: Vec2) -> Option<&Building> { - match self { - Lot::None => None, - Lot::One(building) => { - if pos.map(|e| e > 0.1 && e < 0.9).reduce_and() { - Some(building) - } else { - None - } - }, - Lot::Many { split_x, lots } => { - let split_dim = if *split_x { pos.x } else { pos.y }; - let idx = (split_dim * lots.len() as f32).floor() as usize; - lots[idx.min(lots.len() - 1)].get_at(if *split_x { - Vec2::new((pos.x * lots.len() as f32).fract(), pos.y) - } else { - Vec2::new(pos.x, (pos.y * lots.len() as f32).fract()) - }) - }, - } - } -} diff --git a/world/src/site/mod.rs b/world/src/site/mod.rs index e42016b9ee..6245c40e07 100644 --- a/world/src/site/mod.rs +++ b/world/src/site/mod.rs @@ -4,13 +4,12 @@ mod settlement; pub use self::settlement::Settlement; use crate::{ - block::ZCache, column::ColumnSample, util::{Grid, Sampler}, }; use common::{ terrain::Block, - vol::{BaseVol, WriteVol}, + vol::{BaseVol, RectSizedVol, WriteVol}, }; use std::sync::Arc; use vek::*; @@ -33,14 +32,14 @@ impl Site { } } - pub fn apply_to( - &self, + pub fn apply_to<'a>( + &'a self, wpos2d: Vec2, - zcaches: &Grid>, - vol: &mut (impl BaseVol + WriteVol), + get_column: impl FnMut(Vec2) -> Option<&'a ColumnSample<'a>>, + vol: &mut (impl BaseVol + RectSizedVol + WriteVol), ) { match self { - Site::Settlement(settlement) => settlement.apply_to(wpos2d, zcaches, vol), + Site::Settlement(settlement) => settlement.apply_to(wpos2d, get_column, vol), } } } diff --git a/world/src/site/settlement/mod.rs b/world/src/site/settlement/mod.rs index bab462ba22..6f19c608bd 100644 --- a/world/src/site/settlement/mod.rs +++ b/world/src/site/settlement/mod.rs @@ -1,5 +1,5 @@ use crate::{ - block::ZCache, + column::ColumnSample, sim::{SimChunk, WorldSim}, util::{Grid, RandomField, Sampler, StructureGen2d}, }; @@ -8,7 +8,7 @@ use common::{ path::Path, spiral::Spiral2d, terrain::{Block, BlockKind}, - vol::{BaseVol, WriteVol}, + vol::{BaseVol, RectSizedVol, WriteVol}, }; use hashbrown::{HashMap, HashSet}; use rand::prelude::*; @@ -124,6 +124,7 @@ impl Settlement { this } + /// Designate hazardous terrain based on world data pub fn designate_from_world(&mut self, sim: &WorldSim, rng: &mut impl Rng) { let tile_radius = self.radius() as i32 / AREA_SIZE as i32; let hazard = self.land.new_plot(Plot::Hazard); @@ -146,6 +147,7 @@ impl Settlement { }) } + /// Testing only pub fn place_river(&mut self, rng: &mut impl Rng) { let river_dir = Vec2::new(rng.gen::() - 0.5, rng.gen::() - 0.5).normalized(); let radius = 500.0 + rng.gen::().powf(2.0) * 1000.0; @@ -352,20 +354,20 @@ impl Settlement { pub fn radius(&self) -> f32 { 1200.0 } - pub fn apply_to( - &self, + pub fn apply_to<'a>( + &'a self, wpos2d: Vec2, - zcaches: &Grid>, - vol: &mut (impl BaseVol + WriteVol), + mut get_column: impl FnMut(Vec2) -> Option<&'a ColumnSample<'a>>, + vol: &mut (impl BaseVol + RectSizedVol + WriteVol), ) { let rand_field = RandomField::new(0); - for y in 0..zcaches.size().y { - for x in 0..zcaches.size().x { + for y in 0..vol.size_xy().y as i32 { + for x in 0..vol.size_xy().x as i32 { let offs = Vec2::new(x, y); - let zcache = if let Some(Some(zcache)) = zcaches.get(offs) { - zcache + let col_sample = if let Some(col_sample) = get_column(offs) { + col_sample } else { continue; }; @@ -385,7 +387,7 @@ impl Settlement { < ((1.0 - z as f32 / 12.0) * 2.0).min(1.0) { vol.set( - Vec3::new(offs.x, offs.y, zcache.sample.alt.floor() as i32 + z), + Vec3::new(offs.x, offs.y, col_sample.alt.floor() as i32 + z), Block::new(BlockKind::Normal, color), ); } @@ -394,7 +396,7 @@ impl Settlement { Sample::Tower(Tower::Wall, _pos) => { for z in 0..16 { vol.set( - Vec3::new(offs.x, offs.y, zcache.sample.alt.floor() as i32 + z), + Vec3::new(offs.x, offs.y, col_sample.alt.floor() as i32 + z), Block::new(BlockKind::Normal, Rgb::new(50, 50, 50)), ); } @@ -446,14 +448,14 @@ impl Settlement { Vec2::new(-1, 1), ]; let furrow_dir = furrow_dirs[*seed as usize % furrow_dirs.len()]; - let furrow = (pos * furrow_dir).sum().rem_euclid(4) < 2; + let furrow = (pos * furrow_dir).sum().rem_euclid(6) < 3; Rgb::new( if furrow { - 120 + 100 } else { - 48 + seed.to_le_bytes()[0] % 64 + 32 + seed.to_le_bytes()[0] % 64 }, - 128 + seed.to_le_bytes()[1] % 128, + 64 + seed.to_le_bytes()[1] % 128, 16 + seed.to_le_bytes()[2] % 32, ) },