From 3027597352ed15550a12c1b44dfe2a604f93b4a6 Mon Sep 17 00:00:00 2001 From: Isse Date: Wed, 27 Apr 2022 10:53:08 +0000 Subject: [PATCH] Site2 giant tree entity spawning --- CHANGELOG.md | 1 + assets/world/features.ron | 2 +- world/src/site2/mod.rs | 31 +++++++++++++++++----- world/src/site2/plot/giant_tree.rs | 42 +++++++++++++++++++++++++++++- 4 files changed, 67 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c000dca122..737973da21 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Improved site placement - [Server] Kick clients who send messages on the wrong stream - Reworked Merchant trade price calculation, Merchants offer more wares +- Enable new giant trees, changed what entities spawn at them ### Removed diff --git a/assets/world/features.ron b/assets/world/features.ron index 57903371eb..c2fb99492b 100644 --- a/assets/world/features.ron +++ b/assets/world/features.ron @@ -11,6 +11,6 @@ paths: true, spots: true, site2_towns: true, - site2_giant_trees: false, + site2_giant_trees: true, wildlife_density: 1.0, ) diff --git a/world/src/site2/mod.rs b/world/src/site2/mod.rs index 4f09f7e947..76a186803c 100644 --- a/world/src/site2/mod.rs +++ b/world/src/site2/mod.rs @@ -1029,7 +1029,7 @@ impl Site { let info = canvas.info(); for plot in plots_to_render { - let (prim_tree, fills, entities) = match &self.plots[plot].kind { + let (prim_tree, fills, mut entities) = match &self.plots[plot].kind { PlotKind::House(house) => house.render_collect(self, canvas), PlotKind::Workshop(workshop) => workshop.render_collect(self, canvas), PlotKind::Castle(castle) => castle.render_collect(self, canvas), @@ -1040,9 +1040,14 @@ impl Site { _ => continue, }; - for entity in entities { - canvas.spawn(entity); - } + let mut spawn = |pos, last_block| { + if let Some(entity) = match &self.plots[plot].kind { + PlotKind::GiantTree(tree) => tree.entity_at(pos, &last_block, dynamic_rng), + _ => None, + } { + entities.push(entity); + } + }; for (prim, fill) in fills { for mut aabb in Fill::get_bounds_disjoint(&prim_tree, prim) { @@ -1062,19 +1067,31 @@ impl Site { { continue; } - + let mut last_block = None; for z in aabb.min.z..aabb.max.z { let pos = Vec3::new(x, y, z); canvas.map(pos, |block| { - fill.sample_at(&prim_tree, prim, pos, &info, block) - .unwrap_or(block) + let current_block = + fill.sample_at(&prim_tree, prim, pos, &info, block); + if let (Some(last_block), None) = (last_block, current_block) { + spawn(pos, last_block); + } + last_block = current_block; + current_block.unwrap_or(block) }); } + if let Some(block) = last_block { + spawn(Vec3::new(x, y, aabb.max.z), block); + } } } } } + + for entity in entities { + canvas.spawn(entity); + } } } diff --git a/world/src/site2/plot/giant_tree.rs b/world/src/site2/plot/giant_tree.rs index 0c98bf2183..08a47c4ba4 100644 --- a/world/src/site2/plot/giant_tree.rs +++ b/world/src/site2/plot/giant_tree.rs @@ -5,7 +5,10 @@ use crate::{ util::FastNoise, Land, Sampler, }; -use common::terrain::{Block, BlockKind}; +use common::{ + generation::EntityInfo, + terrain::{Block, BlockKind}, +}; use rand::Rng; use vek::*; @@ -36,6 +39,43 @@ impl GiantTree { pub fn radius(&self) -> f32 { 100.0 } pub fn tree(&self) -> &ProceduralTree { &self.tree } + + pub fn entity_at( + &self, + pos: Vec3, + above_block: &Block, + dynamic_rng: &mut impl Rng, + ) -> Option { + if above_block.kind() == BlockKind::Leaves && dynamic_rng.gen_bool(0.001) { + let entity = EntityInfo::at(pos.as_()); + match dynamic_rng.gen_range(0..=4) { + 0 => { + Some(entity.with_asset_expect( + "common.entity.wild.aggressive.horn_beetle", + dynamic_rng, + )) + }, + 1 => { + Some(entity.with_asset_expect( + "common.entity.wild.aggressive.stag_beetle", + dynamic_rng, + )) + }, + 2 => Some( + entity.with_asset_expect("common.entity.wild.aggressive.deadwood", dynamic_rng), + ), + 3 => Some( + entity.with_asset_expect("common.entity.wild.aggressive.maneater", dynamic_rng), + ), + 4 => Some( + entity.with_asset_expect("common.entity.wild.peaceful.parrot", dynamic_rng), + ), + _ => None, + } + } else { + None + } + } } impl Structure for GiantTree {