From 85e5ba9d8529e230177f95b942135cadf3f7e5a6 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 19 Oct 2026 10:36:23 +0200 Subject: [PATCH] Natural spawning: look up only the structures that override spawns Co-Authored-By: Claude Opus 5.5 --- .../examples/natural_spawn_fixture.rs | 10 ++++- .../src/game/natural_spawning.rs | 10 ++++- crates/horizon-client/src/game/structures.rs | 12 ++++++ .../src/mobs/spawn_selection.rs | 40 +++++++++++++------ 4 files changed, 56 insertions(+), 16 deletions(-) diff --git a/crates/horizon-client/examples/natural_spawn_fixture.rs b/crates/horizon-client/examples/natural_spawn_fixture.rs index 3fb3d5719..528f8195d 100644 --- a/crates/horizon-client/examples/natural_spawn_fixture.rs +++ b/crates/horizon-client/examples/natural_spawn_fixture.rs @@ -268,10 +268,16 @@ impl NaturalLevel for World<'_> { self.biome_info(x, y, z) .map_or(0.1, |b| b.creature_probability) } - fn structures_at(&self, x: i32, y: i32, z: i32) -> Vec { + fn structures_at( + &self, + x: i32, + y: i32, + z: i32, + wanted: &dyn Fn(&str) -> bool, + ) -> Vec { let (cx, cz) = (x >> 4, z >> 4); let mut out: Vec = Vec::new(); - for start in &self.starts { + for start in self.starts.iter().filter(|s| wanted(&s.id)) { let b = start.bounds.0; // Referenced: the start's box reaches the chunk's columns. if b[3] < cx * 16 || b[0] > cx * 16 + 15 || b[5] < cz * 16 || b[2] > cz * 16 + 15 { diff --git a/crates/horizon-client/src/game/natural_spawning.rs b/crates/horizon-client/src/game/natural_spawning.rs index 9bf7023af..0fab09bde 100644 --- a/crates/horizon-client/src/game/natural_spawning.rs +++ b/crates/horizon-client/src/game/natural_spawning.rs @@ -170,9 +170,15 @@ impl NaturalLevel for GameLevel<'_, '_> { fn creature_probability(&self, x: i32, y: i32, z: i32) -> f32 { self.biome(x, y, z).map_or(0.1, |b| b.creature_probability) } - fn structures_at(&self, x: i32, y: i32, z: i32) -> Vec { + fn structures_at( + &self, + x: i32, + y: i32, + z: i32, + wanted: &dyn Fn(&str) -> bool, + ) -> Vec { self.game - .structures_reaching([x, y, z]) + .structures_reaching_where([x, y, z], wanted) .into_iter() .map(|(id, in_piece)| StructureAt { id, diff --git a/crates/horizon-client/src/game/structures.rs b/crates/horizon-client/src/game/structures.rs index e941f2fd9..95a65e4ae 100644 --- a/crates/horizon-client/src/game/structures.rs +++ b/crates/horizon-client/src/game/structures.rs @@ -7,6 +7,15 @@ impl Game<'_> { /// Structures reaching `pos`: (id, inside a piece). `getStructureAt` /// tests the start's whole box, `getStructureWithPieceAt` a piece's. pub(super) fn structures_reaching(&self, pos: [i32; 3]) -> Vec<(String, bool)> { + self.structures_reaching_where(pos, &|_| true) + } + /// The same, for the structures whose id `wanted` takes: the others' + /// starts aren't looked up. + pub(super) fn structures_reaching_where( + &self, + pos: [i32; 3], + wanted: &dyn Fn(&str) -> bool, + ) -> Vec<(String, bool)> { let Some(chunk) = self.world.chunks.get(&(pos[0] >> 4, pos[2] >> 4)) else { return Vec::new(); }; @@ -18,6 +27,9 @@ impl Game<'_> { }; let mut out: Vec<(String, bool)> = Vec::new(); for (id, (sx, sz)) in &chunk.structure_refs { + if !wanted(id) { + continue; + } for start in locator.starts_at(*sx, *sz) { if *start.name != **id || !start.bounding_box.is_inside(pos) { continue; diff --git a/crates/horizon-client/src/mobs/spawn_selection.rs b/crates/horizon-client/src/mobs/spawn_selection.rs index fa1ec736a..c87c3d97a 100644 --- a/crates/horizon-client/src/mobs/spawn_selection.rs +++ b/crates/horizon-client/src/mobs/spawn_selection.rs @@ -15,6 +15,7 @@ use super::Species; use super::spawn_rules::{self, Reason, SpawnLevel}; use crate::biome::SpawnEntry; +use crate::fast_hash::FxHashMap; use horizon_core::{ geometry::{Aabb, Vec3}, random::RandomSource, @@ -365,9 +366,15 @@ pub trait NaturalLevel: SpawnLevel { fn spawn_cost(&self, entity: &str, x: i32, y: i32, z: i32) -> Option; /// The `creature_world_gen_spawn_probability` attribute at a position. fn creature_probability(&self, x: i32, y: i32, z: i32) -> f32; - /// Every structure whose references reach the position's chunk, in the - /// chunk's reference order. - fn structures_at(&self, x: i32, y: i32, z: i32) -> Vec; + /// Every structure whose references reach the position's chunk and + /// whose id `wanted` takes, in the chunk's reference order. + fn structures_at( + &self, + x: i32, + y: i32, + z: i32, + wanted: &dyn Fn(&str) -> bool, + ) -> Vec; fn structure_spawns(&self) -> &StructureSpawns; /// `getNearestPlayer(x, y, z, -1, false)`: the squared distance to the /// nearest player that is not a spectator. @@ -624,7 +631,9 @@ pub struct Override { /// Every structure's `spawn_overrides`, by structure id then category. #[derive(Clone, Debug, Default)] pub struct StructureSpawns { - pub overrides: HashMap>, + /// Fx-hashed: natural spawning asks for every structure reaching every + /// position it tries. + pub overrides: FxHashMap>, } impl StructureSpawns { /// The overrides of the resource root first asked for, loaded once. @@ -633,7 +642,7 @@ impl StructureSpawns { SHARED.get_or_init(|| Self::load(root)) } pub fn load(root: &Path) -> Self { - let mut overrides = HashMap::new(); + let mut overrides = FxHashMap::default(); let dir = root.join("data/minecraft/worldgen/structure"); for entry in fs::read_dir(dir).into_iter().flatten().flatten() { let path = entry.path(); @@ -646,7 +655,7 @@ impl StructureSpawns { else { continue; }; - let map: HashMap = v["spawn_overrides"] + let map: FxHashMap = v["spawn_overrides"] .as_object() .into_iter() .flatten() @@ -709,7 +718,14 @@ pub fn mobs_at( y: i32, z: i32, ) -> Vec { - let structures = l.structures_at(x, y, z); + // Only a fortress or a structure overriding this category's spawns can + // change the list, so only theirs are looked up (the references reaching + // most chunks are mineshafts, which override nothing). + let overrides = l.structure_spawns(); + let monster = category == Category::Monster; + let structures = l.structures_at(x, y, z, &|id| { + (monster && id == "minecraft:fortress") || overrides.get(id, category.name()).is_some() + }); if category == Category::Monster && name_at(l, x, y - 1, z) == "minecraft:nether_bricks" && structures @@ -719,7 +735,7 @@ pub fn mobs_at( return fortress_enemies(); } for s in &structures { - if let Some(o) = l.structure_spawns().get(&s.id, category.name()) + if let Some(o) = overrides.get(&s.id, category.name()) && (if o.piece { s.in_piece } else { s.in_bounds }) { return o.spawns.clone(); @@ -801,8 +817,8 @@ pub struct SpawnState { counts: [i32; 8], charges: Vec<([i32; 3], f64)>, players: Vec, - players_near: HashMap<(i32, i32), Vec>, - player_counts: HashMap, + players_near: FxHashMap<(i32, i32), Vec>, + player_counts: FxHashMap, last_checked: Option<([i32; 3], String, f64)>, } impl SpawnState { @@ -821,8 +837,8 @@ impl SpawnState { counts: [0; 8], charges: Vec::new(), players, - players_near: HashMap::new(), - player_counts: HashMap::new(), + players_near: FxHashMap::default(), + player_counts: FxHashMap::default(), last_checked: None, }; for e in entities {