diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index c97af3a47..e99287edd 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -1779,6 +1779,8 @@ struct Game<'a> { creaking: creaking::CreakingState, /// Monster spawners' display mobs (`game/spawner.rs`). spawners: spawner::SpawnerState, + /// Reused candidate buffers for natural spawning. + spawn_scratch: natural_spawning::SpawnScratch, trident_model: Option, arrow_model: Option, tnts: Vec, @@ -13895,6 +13897,7 @@ impl<'a> Game<'a> { ominous_spawners: Vec::new(), creaking: Default::default(), spawners: Default::default(), + spawn_scratch: Default::default(), trident_model: prepared.trident_model, arrow_model: prepared.arrow_model, tnts: Vec::new(), diff --git a/crates/horizon-client/src/game/lightning.rs b/crates/horizon-client/src/game/lightning.rs index 6caf4458a..4d29466e5 100644 --- a/crates/horizon-client/src/game/lightning.rs +++ b/crates/horizon-client/src/game/lightning.rs @@ -29,10 +29,13 @@ impl Game<'_> { if rain <= 0.2 || thunder <= 0.9 || self.dim != super::Dim::Overworld { return; } - let (_, mut chunks) = self.spawning_chunks(&self.level_players()); + let mut scratch = std::mem::take(&mut self.spawn_scratch); + self.spawning_chunks(&self.level_players(), &mut scratch); let levels = self.tickets.levels(); - chunks.retain(|&(x, z)| levels.random_ticking(x, z)); - for (cx, cz) in chunks { + scratch + .candidates + .retain(|&(x, z)| levels.random_ticking(x, z)); + for (cx, cz) in scratch.candidates.drain(..) { if self.world.random.next_int_bound(100_000).unwrap_or(1) != 0 { continue; } @@ -77,6 +80,7 @@ impl Game<'_> { ]; self.spawn_bolt(at, trap); } + self.spawn_scratch = scratch; } /// `ServerLevel.findLightningRod`: `PoiManager.findClosest` over the /// lightning rod points of interest within 128 blocks of `center` whose diff --git a/crates/horizon-client/src/game/natural_spawning.rs b/crates/horizon-client/src/game/natural_spawning.rs index 5afa72416..48411e8cd 100644 --- a/crates/horizon-client/src/game/natural_spawning.rs +++ b/crates/horizon-client/src/game/natural_spawning.rs @@ -24,6 +24,15 @@ use horizon_core::{ use horizon_world::registry::BlockStateId; use std::collections::HashMap; +/// Reused buffers for one pass over the spawning chunks: the candidate chunks +/// and the set that keeps each one once. The buffers keep their capacity +/// between ticks. +#[derive(Default)] +pub(super) struct SpawnScratch { + pub(super) candidates: Vec<(i32, i32)>, + pub(super) seen: horizon_client::fast_hash::FxHashSet<(i32, i32)>, +} + /// A player in the level in place, as spawning and despawning see it. pub(super) struct LevelPlayer { pub(super) position: Vec3, @@ -445,9 +454,17 @@ impl<'a> Game<'a> { /// chunks (within 8 chunks of a player, `getSpawnCandidateChunks`) that /// are ticking and within 128 blocks of a non-spectator. Also returns how /// many chunks the tracker holds (`getNaturalSpawnChunkCount`). - pub(super) fn spawning_chunks(&self, players: &[LevelPlayer]) -> (i32, Vec<(i32, i32)>) { - let mut candidates: Vec<(i32, i32)> = Vec::with_capacity(289 * players.len()); - let mut seen = horizon_client::fast_hash::FxHashSet::default(); + pub(super) fn spawning_chunks( + &self, + players: &[LevelPlayer], + scratch: &mut SpawnScratch, + ) -> i32 { + let candidates = &mut scratch.candidates; + candidates.clear(); + candidates.reserve(289 * players.len()); + let seen = &mut scratch.seen; + seen.clear(); + seen.reserve(289 * players.len()); for p in players { let (pcx, pcz) = ( p.position.x.floor() as i32 >> 4, @@ -471,7 +488,7 @@ impl<'a> Game<'a> { .iter() .any(|p| close_enough_for_spawning(p, (cx, cz))) }); - (count, candidates) + count } /// `ServerChunkCache.tickChunks`: the spawn state over the loaded mobs, /// the categories under their global caps (creatures every 400 ticks), @@ -482,8 +499,9 @@ impl<'a> Game<'a> { if players.is_empty() { return; } - let (spawnable, mut chunks) = self.spawning_chunks(&players); - for &key in &chunks { + let mut scratch = std::mem::take(&mut self.spawn_scratch); + let spawnable = self.spawning_chunks(&players, &mut scratch); + for &key in &scratch.candidates { *self.world.inhabited.entry(key).or_insert(0) += 1; } let spawn_mobs = self.ui.rules.bool("spawn_mobs"); @@ -524,12 +542,12 @@ impl<'a> Game<'a> { Vec::new() }; // Util.shuffle. - for i in (1..chunks.len()).rev() { + for i in (1..scratch.candidates.len()).rev() { let j = random.next_int_bound(i as i32 + 1).unwrap_or(0) as usize; - chunks.swap(i, j); + scratch.candidates.swap(i, j); } if !categories.is_empty() { - for chunk in chunks { + for chunk in scratch.candidates.drain(..) { let mut spawn = |l: &GameLevel, r: &SpawnRequest| { let spawned = l.game.finalize_spawned(r, &mut pending); if let Some(s) = &spawned { @@ -551,6 +569,7 @@ impl<'a> Game<'a> { } self.world.random = random; self.add_spawned(pending, false); + self.spawn_scratch = scratch; } /// `NoiseBasedChunkGenerator.spawnOriginalMobs`: creature packs for a /// freshly generated chunk, from the biome at its corner column's top,