From 1473029204743bdc2be9b1878e4d91acf15060ef Mon Sep 17 00:00:00 2001 From: Joshua Barretto Date: Fri, 8 Oct 2021 13:55:26 +0100 Subject: [PATCH] Added ability to toggle world features --- assets/world/features.ron | 12 ++++++++++++ server/src/lib.rs | 2 +- world/src/config.rs | 20 ++++++++++++++++++++ world/src/index.rs | 22 +++++++++++++++------- world/src/lib.rs | 30 ++++++++++++++++++++++-------- 5 files changed, 70 insertions(+), 16 deletions(-) create mode 100644 assets/world/features.ron diff --git a/assets/world/features.ron b/assets/world/features.ron new file mode 100644 index 0000000000..05c703d615 --- /dev/null +++ b/assets/world/features.ron @@ -0,0 +1,12 @@ +#![enable(unwrap_newtypes)] +#![enable(implicit_some)] + +( + caverns: false, // TODO: Disabled by default until cave overhaul + caves: true, + shrubs: true, + trees: true, + scatter: true, + paths: true, + spots: true, +) diff --git a/server/src/lib.rs b/server/src/lib.rs index be13805968..fb69658408 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -851,7 +851,7 @@ impl Server { let ecs = self.state.ecs_mut(); let slow_jobs = ecs.write_resource::(); - index.reload_colors_if_changed(|index| { + index.reload_if_changed(|index| { let mut chunk_generator = ecs.write_resource::(); let client = ecs.read_storage::(); let mut terrain = ecs.write_resource::(); diff --git a/world/src/config.rs b/world/src/config.rs index e6db31e6f2..b666c82c24 100644 --- a/world/src/config.rs +++ b/world/src/config.rs @@ -1,3 +1,6 @@ +use common::assets; +use serde::Deserialize; + pub struct Config { pub sea_level: f32, pub mountain_scale: f32, @@ -69,3 +72,20 @@ pub const CONFIG: Config = Config { river_min_height: 0.25, river_width_to_depth: 8.0, }; + +#[derive(Deserialize)] +pub struct Features { + pub caverns: bool, + pub caves: bool, + pub shrubs: bool, + pub trees: bool, + pub scatter: bool, + pub paths: bool, + pub spots: bool, +} + +impl assets::Asset for Features { + type Loader = assets::RonLoader; + + const EXTENSION: &'static str = "ron"; +} diff --git a/world/src/index.rs b/world/src/index.rs index 7d5d6e4685..0edef3eaf7 100644 --- a/world/src/index.rs +++ b/world/src/index.rs @@ -1,7 +1,7 @@ use crate::{ layer::wildlife::{self, DensityFn, SpawnEntry}, site::{economy::TradeInformation, Site}, - Colors, + Colors, Features, }; use common::{ assets::{AssetExt, AssetHandle}, @@ -13,6 +13,7 @@ use noise::{Seedable, SuperSimplex}; use std::sync::Arc; const WORLD_COLORS_MANIFEST: &str = "world.style.colors"; +const WORLD_FEATURES_MANIFEST: &str = "world.features"; pub struct Index { pub seed: u32, @@ -22,6 +23,7 @@ pub struct Index { pub trade: TradeInformation, pub wildlife_spawns: Vec<(AssetHandle, DensityFn)>, colors: AssetHandle>, + features: AssetHandle>, } /// An owned reference to indexed data. @@ -32,6 +34,7 @@ pub struct Index { #[derive(Clone)] pub struct IndexOwned { colors: Arc, + features: Arc, index: Arc, } @@ -47,6 +50,7 @@ impl Deref for IndexOwned { #[derive(Clone, Copy)] pub struct IndexRef<'a> { pub colors: &'a Colors, + pub features: &'a Features, pub index: &'a Index, } @@ -60,6 +64,7 @@ impl Index { /// NOTE: Panics if the color manifest cannot be loaded. pub fn new(seed: u32) -> Self { let colors = Arc::::load_expect(WORLD_COLORS_MANIFEST); + let features = Arc::::load_expect(WORLD_FEATURES_MANIFEST); let wildlife_spawns = wildlife::spawn_manifest() .into_iter() .map(|(e, f)| (SpawnEntry::load_expect(e), f)) @@ -73,6 +78,7 @@ impl Index { trade: Default::default(), wildlife_spawns, colors, + features, } } @@ -89,10 +95,12 @@ impl Index { impl IndexOwned { pub fn new(index: Index) -> Self { let colors = index.colors.cloned(); + let features = index.features.cloned(); Self { index: Arc::new(index), colors, + features, } } @@ -103,13 +111,12 @@ impl IndexOwned { /// solution. /// /// Ideally, this should be called about once per tick. - pub fn reload_colors_if_changed( - &mut self, - reload: impl FnOnce(&mut Self) -> R, - ) -> Option { - self.index.colors.reloaded_global().then(move || { - // Reload the color from the asset handle, which is updated automatically + pub fn reload_if_changed(&mut self, reload: impl FnOnce(&mut Self) -> R) -> Option { + let reloaded = self.index.colors.reloaded_global() || self.index.features.reloaded_global(); + reloaded.then(move || { + // Reload the fields from the asset handle, which is updated automatically self.colors = self.index.colors.cloned(); + self.features = self.index.features.cloned(); reload(self) }) } @@ -117,6 +124,7 @@ impl IndexOwned { pub fn as_index_ref(&self) -> IndexRef { IndexRef { colors: &self.colors, + features: &self.features, index: &self.index, } } diff --git a/world/src/lib.rs b/world/src/lib.rs index 0cd1c4f8d1..182c2a94d4 100644 --- a/world/src/lib.rs +++ b/world/src/lib.rs @@ -28,7 +28,7 @@ pub mod util; // Reexports pub use crate::{ canvas::{Canvas, CanvasInfo}, - config::CONFIG, + config::{Features, CONFIG}, land::Land, }; pub use block::BlockGen; @@ -347,13 +347,27 @@ impl World { entities: Vec::new(), }; - layer::apply_caverns_to(&mut canvas, &mut dynamic_rng); - layer::apply_caves_to(&mut canvas, &mut dynamic_rng); - layer::apply_shrubs_to(&mut canvas, &mut dynamic_rng); - layer::apply_trees_to(&mut canvas, &mut dynamic_rng); - layer::apply_scatter_to(&mut canvas, &mut dynamic_rng); - layer::apply_paths_to(&mut canvas); - layer::apply_spots_to(&mut canvas, &mut dynamic_rng); + if index.features.caverns { + layer::apply_caverns_to(&mut canvas, &mut dynamic_rng); + } + if index.features.caves { + layer::apply_caves_to(&mut canvas, &mut dynamic_rng); + } + if index.features.shrubs { + layer::apply_shrubs_to(&mut canvas, &mut dynamic_rng); + } + if index.features.trees { + layer::apply_trees_to(&mut canvas, &mut dynamic_rng); + } + if index.features.scatter { + layer::apply_scatter_to(&mut canvas, &mut dynamic_rng); + } + if index.features.paths { + layer::apply_paths_to(&mut canvas); + } + if index.features.spots { + layer::apply_spots_to(&mut canvas, &mut dynamic_rng); + } // layer::apply_coral_to(&mut canvas); // Apply site generation