Added ability to toggle world features

This commit is contained in:
Joshua Barretto 2021-10-08 13:55:26 +01:00
parent bee922156e
commit 1473029204
5 changed files with 70 additions and 16 deletions

12
assets/world/features.ron Normal file
View File

@ -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,
)

View File

@ -851,7 +851,7 @@ impl Server {
let ecs = self.state.ecs_mut();
let slow_jobs = ecs.write_resource::<SlowJobPool>();
index.reload_colors_if_changed(|index| {
index.reload_if_changed(|index| {
let mut chunk_generator = ecs.write_resource::<ChunkGenerator>();
let client = ecs.read_storage::<Client>();
let mut terrain = ecs.write_resource::<common::terrain::TerrainGrid>();

View File

@ -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";
}

View File

@ -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<SpawnEntry>, DensityFn)>,
colors: AssetHandle<Arc<Colors>>,
features: AssetHandle<Arc<Features>>,
}
/// An owned reference to indexed data.
@ -32,6 +34,7 @@ pub struct Index {
#[derive(Clone)]
pub struct IndexOwned {
colors: Arc<Colors>,
features: Arc<Features>,
index: Arc<Index>,
}
@ -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::<Colors>::load_expect(WORLD_COLORS_MANIFEST);
let features = Arc::<Features>::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<R>(
&mut self,
reload: impl FnOnce(&mut Self) -> R,
) -> Option<R> {
self.index.colors.reloaded_global().then(move || {
// Reload the color from the asset handle, which is updated automatically
pub fn reload_if_changed<R>(&mut self, reload: impl FnOnce(&mut Self) -> R) -> Option<R> {
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,
}
}

View File

@ -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