From 9675d2b45aba8d08f7fb54eda5224394c4b215db Mon Sep 17 00:00:00 2001 From: Sam Date: Sun, 5 Sep 2021 11:21:48 -0400 Subject: [PATCH] Cultists no longer attack the starting town. --- server/src/lib.rs | 4 ++-- server/src/rtsim/mod.rs | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/server/src/lib.rs b/server/src/lib.rs index 20bf71ea60..704b4c7a17 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -127,7 +127,7 @@ use world::{ }; #[derive(Copy, Clone)] -pub struct SpawnPoint(Vec3); +pub struct SpawnPoint(pub Vec3); impl Default for SpawnPoint { fn default() -> Self { Self(Vec3::new(0.0, 0.0, 256.0)) } @@ -492,7 +492,7 @@ impl Server { // Initiate real-time world simulation #[cfg(feature = "worldgen")] - rtsim::init(&mut state, &world, index.as_index_ref()); + rtsim::init(&mut state, &world, index.as_index_ref(), spawn_point); #[cfg(not(feature = "worldgen"))] rtsim::init(&mut state); diff --git a/server/src/rtsim/mod.rs b/server/src/rtsim/mod.rs index 0735f72b98..c48a40f514 100644 --- a/server/src/rtsim/mod.rs +++ b/server/src/rtsim/mod.rs @@ -108,6 +108,7 @@ pub fn init( state: &mut State, #[cfg(feature = "worldgen")] world: &world::World, #[cfg(feature = "worldgen")] index: world::IndexRef, + #[cfg(feature = "worldgen")] spawn_point: crate::SpawnPoint, ) { #[cfg(feature = "worldgen")] let mut rtsim = RtSim::new(world.sim().get_size()); @@ -143,6 +144,16 @@ pub fn init( .filter_map(|(site_id, site)| site.site_tmp.map(|id| (site_id, &index.sites[id]))) { use world::site::SiteKind; + let spawn_town_id = world + .civs() + .sites + .iter() + .filter(|(_, site)| site.is_settlement()) + .min_by_key(|(_, site)| { + let wpos = site.center * TerrainChunk::RECT_SIZE.map(|x| x as i32); + wpos.distance_squared(spawn_point.0.xy().map(|x| x as i32)) + }) + .map(|(id, _)| id); #[allow(clippy::single_match)] match &site.kind { #[allow(clippy::single_match)] @@ -153,7 +164,11 @@ pub fn init( .civs() .sites .iter() - .filter(|s| s.1.is_settlement()) + .filter(|&(site_id, site)| { + site.is_settlement() + // TODO: Remove this later, starting town should not be special-cased + && spawn_town_id.map_or(false, |spawn_id| spawn_id != site_id) + }) .min_by_key(|(_, site)| { let wpos = site.center * TerrainChunk::RECT_SIZE.map(|e| e as i32); wpos.map(|e| e as f32)