Minecraft: Java Edition 26.3 rewritten from scratch in Rust, checked against vanilla with 281 differential harnesses. Get it with the Horizon launcher.
Rust differential-testinggame-enginegamedevlinuxminecraftminecraft-javareimplementationrustvoxelvulkanwgpu
//! The game's side of `NaturalSpawner` (`mobs::spawn_selection`): the level
//! it reads, the spawning chunks and caps of `ServerChunkCache.tickChunks`,
//! and the mobs it adds (finalized with the game's `finalizeSpawn`).
use super::{Dim, Game};
use horizon_client::vanillagen::VanillaTerrain;
use horizon_client::{
biome::{BiomeInfo, SpawnEntry},
blocks::BlockFacts,
collisions::{SceneCollisions, no_collision_empty},
mobs::{
Mob, MobWorld, Species,
data::{self, MobData},
seen_grid::SeenGrid,
spawn_rules::{Dimension, SpawnLevel},
spawn_selection::{
self, Category, Counted, Heightmap, NaturalLevel, SpawnCost, SpawnPlayer, SpawnRequest,
SpawnState, Spawned, StructureAt, StructureSpawns,
},
},
};
use horizon_core::{
geometry::{Aabb, Vec3},
random::{LegacyRandom, RandomSource, WorldgenRandom, XoroshiroRandom},
};
use horizon_world::{
nbt::{Compound, Tag},
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,
pub(super) bounds: Aabb,
pub(super) alive: bool,
pub(super) creative: bool,
pub(super) spectator: bool,
}
impl LevelPlayer {
fn distance_sqr(&self, x: f64, y: f64, z: f64) -> f64 {
(self.position.x - x).powi(2)
+ (self.position.y - y).powi(2)
+ (self.position.z - z).powi(2)
}
/// `Level.hasNearbyAlivePlayer`'s test of one player (any distance for a
/// negative range).
fn alive_near(&self, x: f64, y: f64, z: f64, range: f64) -> bool {
self.alive && !self.spectator && (range < 0.0 || self.distance_sqr(x, y, z) < range * range)
}
}
/// `ChunkMap.euclideanDistanceSquared` below 128 blocks
/// (`playerIsCloseEnoughForSpawning`).
fn close_enough_for_spawning(p: &LevelPlayer, (cx, cz): (i32, i32)) -> bool {
let (x, z) = (
f64::from(cx * 16 + 8) - p.position.x,
f64::from(cz * 16 + 8) - p.position.z,
);
!p.spectator && x * x + z * z < 16384.0
}
/// `EntityGetter.getNearestPlayer(x, y, z, -1, false)`: the nearest
/// non-spectator, alive or not.
pub(super) fn nearest_player(
players: &[LevelPlayer],
x: f64,
y: f64,
z: f64,
) -> Option<&LevelPlayer> {
let mut best: Option<(&LevelPlayer, f64)> = None;
for p in players.iter().filter(|p| !p.spectator) {
let d = p.distance_sqr(x, y, z);
if best.is_none_or(|(_, b)| d < b) {
best = Some((p, d));
}
}
best.map(|(p, _)| p)
}
/// The game level as natural spawning reads it.
pub(super) struct GameLevel<'g, 'a> {
pub(super) game: &'g Game<'a>,
pub(super) w: MobWorld<'g>,
/// Every player in the level ([`Game::level_players`]).
pub(super) players: Vec<LevelPlayer>,
/// Where the non-spectators among them are (`getNearestPlayer`).
pub(super) nearest: Vec<[f64; 3]>,
/// Boxes of the mobs this pass created, not yet in the game's list.
pub(super) pending: std::cell::RefCell<Vec<Aabb>>,
/// The types and boxes of what a spawner created this tick.
pub(super) pending_ids: std::cell::RefCell<Vec<(String, Aabb)>>,
/// The game's mobs by box, built once a pass has asked more than
/// [`SCANNED_QUERIES`] box queries (the mobs do not change while it
/// holds the game); the first ones scan, as the build costs a few scans.
pub(super) boxes: std::cell::OnceCell<MobBoxes>,
pub(super) box_queries: std::cell::Cell<u32>,
}
const SCANNED_QUERIES: u32 = 8;
/// A mob not removed: its index in `Game::mobs`, box, alive, and whether
/// it is a living shulker.
type MobBox = (usize, Aabb, bool, bool);
fn mob_box(i: usize, m: &Mob) -> MobBox {
let alive = m.alive();
(
i,
m.bounds(),
alive,
alive && m.species() == Species::Shulker,
)
}
/// The boxes of the game's mobs not removed, and a grid over them.
pub(super) struct MobBoxes {
entries: Vec<MobBox>,
grid: SeenGrid,
}
impl MobBoxes {
fn new(mobs: &[Mob]) -> Self {
let entries: Vec<_> = mobs
.iter()
.enumerate()
.filter(|(_, m)| !m.removed)
.map(|(i, m)| mob_box(i, m))
.collect();
let grid = SeenGrid::new(entries.iter().map(|e| e.1));
Self { entries, grid }
}
/// The entries whose boxes meet `area` (and possibly a few more).
fn near(&self, area: Aabb) -> impl Iterator<Item = MobBox> + '_ {
let mut near = Vec::new();
let indexed = self.grid.candidates(area, &mut near);
let all = (!indexed).then(|| self.entries.iter());
near.into_iter()
.map(|i| self.entries[i as usize])
.chain(all.into_iter().flatten().copied())
}
}
impl GameLevel<'_, '_> {
/// The mobs not removed whose boxes may meet `area` (callers test them).
fn mobs_near(&self, area: Aabb) -> Box<dyn Iterator<Item = MobBox> + '_> {
let asked = self.box_queries.get() + 1;
self.box_queries.set(asked);
if asked <= SCANNED_QUERIES && self.boxes.get().is_none() {
return Box::new(
self.game
.mobs
.iter()
.enumerate()
.filter(|(_, m)| !m.removed)
.map(|(i, m)| mob_box(i, m)),
);
}
Box::new(
self.boxes
.get_or_init(|| MobBoxes::new(&self.game.mobs))
.near(area),
)
}
fn biome(&self, x: i32, y: i32, z: i32) -> Option<&BiomeInfo> {
let chunk = self.game.world.chunks.get(&(x >> 4, z >> 4))?;
Some(world_biomes!(self.game).get(chunk.biome(x, y, z)))
}
/// The block whose noise cell `BiomeManager.getBiome` reads.
fn zoomed(&self, x: i32, y: i32, z: i32) -> [i32; 3] {
let zoom = self.game.streaming.biome_zoom;
let [qx, qy, qz] = zoom.quart_at_block(horizon_core::position::BlockPos::new(x, y, z));
[qx << 2, qy << 2, qz << 2]
}
fn zoomed_biome(&self, x: i32, y: i32, z: i32) -> Option<&BiomeInfo> {
let [x, y, z] = self.zoomed(x, y, z);
self.biome(x, y, z)
}
}
impl SpawnLevel for GameLevel<'_, '_> {
fn state(&self, x: i32, y: i32, z: i32) -> BlockStateId {
self.w.state(x, y, z)
}
fn facts(&self) -> &BlockFacts {
self.w.facts
}
fn mob_data(&self) -> &MobData {
self.w.data
}
fn block_tag(&self, tag: &str, block: &str) -> bool {
self.w.block_tag(tag, block)
}
// Level.getBiome: the zoomed biome.
fn biome_tag(&self, tag: &str, x: i32, y: i32, z: i32) -> bool {
let [x, y, z] = self.zoomed(x, y, z);
self.w.biome_tag(tag, x, y, z)
}
fn sky_light(&self, x: i32, y: i32, z: i32) -> i32 {
self.w.sky_light(x, y, z)
}
fn block_light(&self, x: i32, y: i32, z: i32) -> i32 {
self.w.block_light(x, y, z)
}
fn sky_darken(&self) -> i32 {
SpawnLevel::sky_darken(&self.w)
}
fn thundering(&self) -> bool {
self.w.thundering
}
fn sea_level(&self) -> i32 {
self.w.sea_level()
}
fn world_surface(&self, x: i32, z: i32) -> i32 {
self.w.world_surface(x, z)
}
fn peaceful(&self) -> bool {
self.w.peaceful()
}
fn dimension(&self) -> Dimension {
self.w.dimension()
}
fn surface_slime_chance(&self, x: i32, y: i32, z: i32) -> f32 {
self.w.surface_slime_chance(x, y, z)
}
fn world_seed(&self) -> i64 {
self.w.world_seed
}
/// `getNearestPlayer(x, y, z, distance, true)`, over every player.
fn player_near(&self, x: f64, y: f64, z: f64, distance: f64) -> bool {
self.players.iter().any(|p| {
p.alive && !p.creative && !p.spectator && p.distance_sqr(x, y, z) < distance * distance
})
}
}
impl NaturalLevel for GameLevel<'_, '_> {
fn min_y(&self) -> i32 {
self.w.world.bottom_y()
}
fn max_y(&self) -> i32 {
self.w.world.top_y() - 1
}
fn ambient_light(&self) -> f32 {
match self.game.dim {
Dim::Nether => 0.1,
Dim::End => 0.25,
_ => 0.0,
}
}
fn has_ceiling(&self) -> bool {
self.game.dim == Dim::Nether
}
fn mob_type(&self, entity: &str) -> Option<(Species, f32, f32, Category)> {
let t = self.game.res.mob_types().get(entity)?;
Some((
t.species,
t.width as f32,
t.height as f32,
Category::from_name(&t.category),
))
}
// The `natural_mob_spawns` attribute reads the zoomed biome
// (fullResolutionBiomes), the spawn probability the noise cell.
fn biome_spawns(&self, x: i32, y: i32, z: i32) -> Option<&HashMap<String, Vec<SpawnEntry>>> {
self.zoomed_biome(x, y, z).map(|b| &b.spawns)
}
fn spawn_cost(&self, entity: &str, x: i32, y: i32, z: i32) -> Option<SpawnCost> {
self.zoomed_biome(x, y, z)?.spawn_costs.get(entity).copied()
}
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,
wanted: &dyn Fn(&str) -> bool,
) -> Vec<StructureAt> {
self.game
.structures_reaching_where([x, y, z], wanted)
.into_iter()
.map(|(id, in_piece)| StructureAt {
id,
in_piece,
in_bounds: true,
})
.collect()
}
fn structure_spawns(&self) -> &StructureSpawns {
StructureSpawns::shared(&self.game.options.resources)
}
fn nearest_player_positions(&self) -> &[[f64; 3]] {
&self.nearest
}
fn respawn(&self) -> Option<[i32; 3]> {
// The world spawn point lives in the overworld.
if self.game.dim != Dim::Overworld {
return None;
}
self.game.level.spawn.map(|s| {
[
s[0].floor() as i32,
s[1].floor() as i32,
s[2].floor() as i32,
]
})
}
fn can_spawn_in_chunk(&self, cx: i32, cz: i32) -> bool {
self.game.world.chunks.contains_key(&(cx, cz))
}
fn no_collision(&self, bounds: Aabb) -> bool {
let mut scene = SceneCollisions {
world: &self.game.world,
binding: self.w.collision,
subject: None,
};
// getEntityCollisions(null, box): living shulkers are solid.
let grown = bounds.inflate(Vec3::new(1e-7, 1e-7, 1e-7));
no_collision_empty(&mut scene, bounds)
&& !self
.mobs_near(grown)
.any(|(_, b, _, shulker)| shulker && b.intersects(grown))
&& !self
.pending_ids
.borrow()
.iter()
.any(|(id, b)| id == "minecraft:shulker" && b.intersects(grown))
}
fn unobstructed(&self, bounds: Aabb) -> bool {
let player = self
.players
.iter()
.any(|p| p.alive && !p.spectator && p.bounds.intersects(bounds));
!player
&& !self.pending.borrow().iter().any(|b| b.intersects(bounds))
&& !self
.mobs_near(bounds)
.any(|(_, b, alive, _)| alive && b.intersects(bounds))
}
fn spawn_mobs(&self) -> bool {
self.game.ui.rules.bool("spawn_mobs")
}
/// WORLD_SURFACE (every spawn attempt's) is the chunk's kept heightmap.
/// The others are scanned down the column: sections of air alone are
/// skipped (air counts for no heightmap). An unloaded column reads as air.
fn heightmap(&self, kind: Heightmap, x: i32, z: i32) -> i32 {
let world = self.w.world;
if kind == Heightmap::WorldSurface {
return self.w.world_surface(x, z);
}
world
.chunks
.get(&(x >> 4, z >> 4))
.and_then(|chunk| {
chunk.highest(world.air.0, |y| {
spawn_selection::heightmap_blocks(self, kind, x, y, z)
})
})
.map_or(self.w.world.bottom_y(), |y| y + 1)
}
fn pathfindable_land(&self, x: i32, y: i32, z: i32) -> bool {
self.w.data.state(self.state(x, y, z)).flags & data::LAND != 0
}
}
impl horizon_client::mobs::spawner::SpawnerLevel for GameLevel<'_, '_> {
/// `Level.hasNearbyAlivePlayer`, over every player.
fn alive_player_near(&self, x: f64, y: f64, z: f64, range: f64) -> bool {
self.players.iter().any(|p| p.alive_near(x, y, z, range))
}
fn spawner_blocks_work(&self) -> bool {
self.game.ui.rules.bool("spawner_blocks_work")
}
fn entity_exists(&self, id: &str) -> bool {
self.game.res.mob_types().contains_key(id) || self.game.entity_type_exists(id)
}
fn count_entities(&self, id: &str, bounds: Aabb) -> usize {
let mobs = self
.mobs_near(bounds)
.filter(|&(i, b, ..)| self.game.mobs[i].kind.id == id && b.intersects(bounds))
.count();
let pending = self
.pending_ids
.borrow()
.iter()
.filter(|(k, b)| k == id && b.intersects(bounds))
.count();
mobs + pending
}
}
impl<'a> Game<'a> {
pub(super) fn natural_level(&self) -> GameLevel<'_, 'a> {
let players = self.level_players();
let nearest = players
.iter()
.filter(|p| !p.spectator)
.map(|p| [p.position.x, p.position.y, p.position.z])
.collect();
GameLevel {
game: self,
w: self.mob_world_base(&[]),
players,
nearest,
pending: Default::default(),
pending_ids: Default::default(),
boxes: Default::default(),
box_queries: Default::default(),
}
}
/// Creates a mob the spawner accepted, finalized the game's way; the
/// caller adds it with [`Game::add_spawned`].
fn finalize_spawned(&self, r: &SpawnRequest, pending: &mut Vec<Mob>) -> Option<Spawned> {
let kind = self.res.mob_types().get(r.entity)?.clone();
let [x, y, z] = r.position;
let mut mob = Mob::new(0, kind, Vec3::new(x, y, z), r.yaw);
let facts = self.biome_facts(x.floor() as i32, y.floor() as i32, z.floor() as i32);
let difficulty = self.local_difficulty(r.position);
horizon_client::mobs::spawning::finalize(&mut mob, &facts, &difficulty);
horizon_client::mobs::raider::finalize(
&mut mob,
horizon_client::mobs::raider::Reason::Natural,
);
let bounds = mob.bounds();
pending.push(mob);
Some(Spawned {
bounds,
school: true,
})
}
/// Adds the mobs a spawn pass created: ids, enchanted gear and jockeys.
fn add_spawned(&mut self, pending: Vec<Mob>, persistent: bool) {
for mut mob in pending {
self.next_entity += 1;
mob.id = self.next_entity;
mob.persistent = persistent;
let id = mob.id;
self.mobs.push(mob);
self.native_mobs_joined(self.mobs.len() - 1, true);
self.enchant_spawn_equipment(id);
self.halloween_spawn_head(id);
if !persistent {
self.spawn_jockeys(id, true);
}
}
}
/// Every player in the level in place: the local player, and the LAN
/// guests or server players in it.
pub(super) fn level_players(&self) -> Vec<LevelPlayer> {
self.level_players_iter().collect()
}
/// `level_players` one at a time, without collecting them.
fn level_players_iter(&self) -> impl Iterator<Item = LevelPlayer> + '_ {
let local = self.player_view().map(|p| LevelPlayer {
position: p.position,
bounds: p.bounds,
alive: p.alive,
creative: p.creative,
spectator: p.spectator,
});
let guests = self.lan.iter().flat_map(|h| {
h.hub
.guests
.iter()
.filter(|g| h.hub.here(g))
.map(|g| LevelPlayer {
position: Vec3::new(g.position[0], g.position[1], g.position[2]),
bounds: super::lan_mobs::guest_bounds(g),
alive: !g.dead,
creative: g.creative(),
spectator: g.spectator(),
})
});
local.into_iter().chain(guests)
}
/// `Level.hasNearbyAlivePlayer` before a level is built for the spawn
/// code: what a spawner asks first, every tick.
pub(super) fn alive_player_near(&self, x: f64, y: f64, z: f64, range: f64) -> bool {
self.level_players_iter()
.any(|p| p.alive_near(x, y, z, range))
}
/// `ChunkMap.collectSpawningChunks`: the natural spawn chunk tracker's
/// 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],
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,
p.position.z.floor() as i32 >> 4,
);
for dx in -8..=8 {
for dz in -8..=8 {
let key = (pcx + dx, pcz + dz);
if seen.insert(key) {
candidates.push(key);
}
}
}
}
let count = candidates.len() as i32;
let levels = self.tickets.levels();
candidates.retain(|&(cx, cz)| {
levels.loaded_ticking(cx, cz)
&& super::ticking_chunk(&self.world, cx, cz)
&& players
.iter()
.any(|p| close_enough_for_spawning(p, (cx, cz)))
});
count
}
/// `ServerChunkCache.tickChunks`: the spawn state over the loaded mobs,
/// the categories under their global caps (creatures every 400 ticks),
/// then `tickSpawningChunk` in the shuffled spawning chunks of every
/// player: inhabited time, then `spawnForChunk`.
pub(super) fn spawn_natural(&mut self) {
// `tickChunks` does nothing in a Debug world.
if self.world_debug {
return;
}
let players = self.level_players();
if players.is_empty() {
return;
}
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");
let spawn_enemies = self.ui.rules.bool("spawn_monsters");
let mut random = std::mem::replace(&mut self.world.random, LegacyRandom::new(0));
let mut pending = Vec::new();
let last_state;
{
let level = self.natural_level();
let counted: Vec<Counted> = self
.mobs
.iter()
.map(|m| Counted {
entity: &m.kind.id,
category: Category::from_name(&m.kind.category),
block: [
m.position.x.floor() as i32,
m.position.y.floor() as i32,
m.position.z.floor() as i32,
],
is_mob: true,
persistent: m.persistent
|| horizon_client::mobs::ai::requires_custom_persistence(m),
})
.collect();
let players = players
.iter()
.map(|p| SpawnPlayer {
position: [p.position.x, p.position.y, p.position.z],
spectator: p.spectator,
})
.collect();
let mut state = SpawnState::new(&level, spawnable, players, &counted, |cx, cz| {
self.world.chunks.contains_key(&(cx, cz))
});
last_state = state.counts();
let categories = if spawn_mobs {
state.filtered_categories(spawn_enemies, self.world.game_time % 400 == 0)
} else {
Vec::new()
};
// Util.shuffle.
for i in (1..scratch.candidates.len()).rev() {
let j = random.next_int_bound(i as i32 + 1).unwrap_or(0) as usize;
scratch.candidates.swap(i, j);
}
if !categories.is_empty() {
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 {
l.pending.borrow_mut().push(s.bounds);
}
spawned
};
spawn_selection::spawn_for_chunk(
&level,
&mut random,
&mut state,
&categories,
chunk,
&mut spawn,
&mut |_| {},
);
}
}
}
self.world.random = random;
// ServerChunkCache.lastSpawnState, for the debug overlay.
self.spawn_counts.insert(self.dim, last_state);
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,
/// with the chunk's decoration-seeded random, finalized with the
/// region's random as generation adds them (mobs are not generated in
/// the End).
pub(super) fn spawn_generation_creatures(&mut self, cx: i32, cz: i32) {
if !self.world.chunks.contains_key(&(cx, cz)) || self.dim == Dim::End {
return;
}
let Some(terrain) = self.end.locator() else {
return;
};
// `FlatLevelSource.spawnOriginalMobs` is empty: a superflat world places no packs.
if terrain.is_flat() {
return;
}
let mut region = terrain.region_random(cx, cz);
let any = region.as_any();
let added = if let Some(r) = any.downcast_mut::<LegacyRandom>() {
self.generation_creatures(cx, cz, &terrain, r)
} else if let Some(r) = any.downcast_mut::<XoroshiroRandom>() {
self.generation_creatures(cx, cz, &terrain, r)
} else {
return;
};
for nbt in added {
self.load_mob(Tag::Compound(nbt), None);
}
}
fn generation_creatures<Q: RandomSource + 'static>(
&self,
cx: i32,
cz: i32,
terrain: &VanillaTerrain,
region: &mut Q,
) -> Vec<Compound> {
let mut random = WorldgenRandom::new(LegacyRandom::new(0));
random.set_decoration_seed(self.level.seed, cx * 16, cz * 16);
let mut added = Vec::new();
let mut group = None;
let level = self.natural_level();
let source = [cx * 16, level.max_y(), cz * 16];
let mut spawn = |l: &GameLevel, r: &SpawnRequest, region: &mut Q| {
// Each pack starts without group data.
if r.group_index == 0 {
group = None;
}
let [x, y, z] = r.position;
let (bx, by, bz) = (x.floor() as i32, y.floor() as i32, z.floor() as i32);
let biome = l.zoomed_biome(bx, by, bz).map_or("", |b| b.name.as_str());
let at = Vec3::new(x, y, z);
match terrain.spawn_generated(r.entity, at, r.yaw, biome, region, &mut group) {
Some(nbt) => {
added.push(nbt);
true
}
None => false,
}
};
spawn_selection::spawn_mobs_for_chunk_generation(
&level,
source,
(cx, cz),
&mut random,
region,
&mut spawn,
&mut |_| {},
);
added
}
}