From 28ee668dc086c7934dd32512cef2c2926cafc23c Mon Sep 17 00:00:00 2001 From: Joshua Barretto Date: Tue, 22 Jun 2021 02:58:51 +0100 Subject: [PATCH] Test spot entity spawning --- .../common/entity/spot/bandit_camp/saurok.ron | 12 ++++++ world/src/canvas.rs | 4 ++ world/src/layer/spot.rs | 37 +++++++++++++++++-- world/src/lib.rs | 9 +++-- 4 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 assets/common/entity/spot/bandit_camp/saurok.ron diff --git a/assets/common/entity/spot/bandit_camp/saurok.ron b/assets/common/entity/spot/bandit_camp/saurok.ron new file mode 100644 index 0000000000..0cd483ef45 --- /dev/null +++ b/assets/common/entity/spot/bandit_camp/saurok.ron @@ -0,0 +1,12 @@ +EntityConfig ( + name: Some("Saurok Bandit"), + body: Some(RandomWith("saurok_mighty")), + + loot: Some(LootTable("common.loot_tables.creature.biped_large.saurok")), + + main_tool: Some(Item("common.items.npc_weapons.bow.saurok_bow")), + second_tool: None, + + loadout_asset: None, + skillset_asset: None, +) diff --git a/world/src/canvas.rs b/world/src/canvas.rs index ba5dfad29b..23d6fce138 100644 --- a/world/src/canvas.rs +++ b/world/src/canvas.rs @@ -8,6 +8,7 @@ use crate::{ util::Grid, }; use common::{ + generation::EntityInfo, terrain::{Block, Structure, TerrainChunk, TerrainChunkSize}, vol::{ReadVol, RectVolSize, WriteVol}, }; @@ -112,6 +113,7 @@ impl<'a> CanvasInfo<'a> { pub struct Canvas<'a> { pub(crate) info: CanvasInfo<'a>, pub(crate) chunk: &'a mut TerrainChunk, + pub(crate) entities: Vec, } impl<'a> Canvas<'a> { @@ -205,6 +207,8 @@ impl<'a> Canvas<'a> { } }); } + + pub fn spawn(&mut self, entity: EntityInfo) { self.entities.push(entity); } } impl<'a> Deref for Canvas<'a> { diff --git a/world/src/layer/spot.rs b/world/src/layer/spot.rs index 71dce812d0..0bd20d7e92 100644 --- a/world/src/layer/spot.rs +++ b/world/src/layer/spot.rs @@ -3,7 +3,11 @@ use crate::{ util::seed_expan, Canvas, }; -use common::terrain::{Block, BlockKind, Structure}; +use common::{ + comp, + generation::EntityInfo, + terrain::{Block, BlockKind, Structure}, +}; use rand::prelude::*; use rand_chacha::ChaChaRng; use vek::*; @@ -11,7 +15,7 @@ use vek::*; #[derive(Copy, Clone, Debug)] pub enum Spot { Camp, - Hideout, + BanditCamp, } impl Spot { @@ -24,7 +28,7 @@ impl Spot { false, ); Self::generate_spots( - Spot::Hideout, + Spot::BanditCamp, world, 10.0, |g, c| g < 0.25 && !c.near_cliffs() && !c.river.near_water(), @@ -83,11 +87,36 @@ pub fn apply_spots_to(canvas: &mut Canvas, dynamic_rng: &mut impl Rng) { }, ); }, - Spot::Hideout => { + Spot::BanditCamp => { let structures = Structure::load_group("dungeon_entrances").read(); let structure = structures.choose(&mut rng).unwrap(); let origin = spot_wpos.with_z(canvas.land().get_alt_approx(spot_wpos) as i32); canvas.blit_structure(origin, &structure, seed); + + let spawn_radius = 12; + let avg_num = 5.0; + + canvas.foreach_col_area( + Aabr { + min: spot_wpos - spawn_radius, + max: spot_wpos + spawn_radius, + }, + |canvas, wpos2d, col| { + if dynamic_rng.gen_bool(avg_num / (spawn_radius * 2).pow(2) as f64) { + if let Some(z) = (-8..8).rev().map(|z| col.alt as i32 + z).find(|z| { + canvas.get(wpos2d.with_z(z + 2)).is_fluid() + && canvas.get(wpos2d.with_z(z + 1)).is_fluid() + && canvas.get(wpos2d.with_z(z + 0)).is_solid() + }) { + canvas.spawn( + EntityInfo::at(wpos2d.map(|e| e as f32 + 0.5).with_z(z as f32)) + .with_asset_expect("common.entity.spot.bandit_camp.saurok") + .with_alignment(comp::Alignment::Enemy), + ); + } + } + }, + ); }, } } diff --git a/world/src/lib.rs b/world/src/lib.rs index c9e8ca6ada..bce5091a93 100644 --- a/world/src/lib.rs +++ b/world/src/lib.rs @@ -367,6 +367,7 @@ impl World { chunk: sim_chunk, }, chunk: &mut chunk, + entities: Vec::new(), }; layer::apply_caves_to(&mut canvas, &mut dynamic_rng); @@ -382,6 +383,10 @@ impl World { .iter() .for_each(|site| index.sites[*site].apply_to(&mut canvas, &mut dynamic_rng)); + let mut supplement = ChunkSupplement { + entities: canvas.entities, + }; + let gen_entity_pos = |dynamic_rng: &mut rand::rngs::ThreadRng| { let lpos2d = TerrainChunkSize::RECT_SIZE .map(|sz| dynamic_rng.gen::().rem_euclid(sz) as i32); @@ -398,10 +403,6 @@ impl World { (Vec3::from(chunk_wpos2d) + lpos).map(|e: i32| e as f32) + 0.5 }; - let mut supplement = ChunkSupplement { - entities: Vec::new(), - }; - if sim_chunk.contains_waypoint { supplement.add_entity(EntityInfo::at(gen_entity_pos(&mut dynamic_rng)).into_waypoint()); }