From 84c7f391481c4cbe08e612c953025ab7e74781c6 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 22 Oct 2026 10:58:39 +0200 Subject: [PATCH] Natural spawning: borrow the spawn lists, and only the nearest distance Natural spawning was a quarter of a 40-player server thread. Every spawn attempt cloned the position's whole spawn list (NaturalSpawner.mobsAt) to check the picked entry is still in it, and every group cloned it again to pick from; mobs_at now returns the biome's or the structure's list as it is stored (the fortress list lives in a static). The nearest player's distance, asked on every attempt, is now the minimum of the distances instead of a search that kept the player too. Same lists, same draws, same distances. Co-Authored-By: Claude Opus 5.5 --- .../src/game/natural_spawning.rs | 8 +++- .../src/mobs/spawn_selection.rs | 45 ++++++++++--------- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/crates/horizon-client/src/game/natural_spawning.rs b/crates/horizon-client/src/game/natural_spawning.rs index 0fab09bde..68aabbbc6 100644 --- a/crates/horizon-client/src/game/natural_spawning.rs +++ b/crates/horizon-client/src/game/natural_spawning.rs @@ -191,7 +191,13 @@ impl NaturalLevel for GameLevel<'_, '_> { StructureSpawns::shared(&self.game.options.resources) } fn nearest_player_distance_sqr(&self, x: f64, y: f64, z: f64) -> Option { - nearest_player(&self.players, x, y, z).map(|p| p.distance_sqr(x, y, z)) + // The nearest player's distance (`nearest_player`), without finding + // which player it is: every spawn attempt asks. + self.players + .iter() + .filter(|p| !p.spectator) + .map(|p| p.distance_sqr(x, y, z)) + .reduce(f64::min) } fn respawn(&self) -> Option<[i32; 3]> { // The world spawn point lives in the overworld. diff --git a/crates/horizon-client/src/mobs/spawn_selection.rs b/crates/horizon-client/src/mobs/spawn_selection.rs index c87c3d97a..614a2b2b5 100644 --- a/crates/horizon-client/src/mobs/spawn_selection.rs +++ b/crates/horizon-client/src/mobs/spawn_selection.rs @@ -691,33 +691,37 @@ pub struct StructureAt { } /// `NetherFortressStructure.FORTRESS_ENEMIES`. -pub fn fortress_enemies() -> Vec { - let e = |entity: &str, weight, min, max, constant| SpawnEntry { - entity: entity.to_owned(), - weight, - min, - max, - constant, - }; - vec![ - e("minecraft:blaze", 10, 2, 3, false), - e("minecraft:zombified_piglin", 5, 4, 4, true), - e("minecraft:wither_skeleton", 8, 5, 5, true), - e("minecraft:skeleton", 2, 5, 5, true), - e("minecraft:magma_cube", 3, 4, 4, true), - ] +pub fn fortress_enemies() -> &'static [SpawnEntry] { + static ENEMIES: std::sync::LazyLock> = std::sync::LazyLock::new(|| { + let e = |entity: &str, weight, min, max, constant| SpawnEntry { + entity: entity.to_owned(), + weight, + min, + max, + constant, + }; + vec![ + e("minecraft:blaze", 10, 2, 3, false), + e("minecraft:zombified_piglin", 5, 4, 4, true), + e("minecraft:wither_skeleton", 8, 5, 5, true), + e("minecraft:skeleton", 2, 5, 5, true), + e("minecraft:magma_cube", 3, 4, 4, true), + ] + }); + &ENEMIES } /// `NaturalSpawner.mobsAt`: the fortress bounds on nether bricks, then /// `ChunkGenerator.getMobsAt` (the first referenced structure whose override -/// for the category holds the position), else the biome's list. +/// for the category holds the position), else the biome's list. Borrowed from +/// the level: a spawn attempt looks it up up to twice. pub fn mobs_at( l: &L, category: Category, x: i32, y: i32, z: i32, -) -> Vec { +) -> &[SpawnEntry] { // 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). @@ -738,13 +742,12 @@ pub fn mobs_at( if let Some(o) = overrides.get(&s.id, category.name()) && (if o.piece { s.in_piece } else { s.in_bounds }) { - return o.spawns.clone(); + return &o.spawns; } } l.biome_spawns(x, y, z) .and_then(|b| b.get(category.name())) - .cloned() - .unwrap_or_default() + .map_or(&[], Vec::as_slice) } /// `WeightedList.getRandom`. @@ -1103,7 +1106,7 @@ pub fn spawn_category_for_position( { None } else { - weighted_pick(&mobs_at(l, category, x, y, z), random) + weighted_pick(mobs_at(l, category, x, y, z), random) }; let Some(entry) = list else { break;