Files
horizon-airdef/crates/horizon-client/src/game/initial_spawn.rs
T
Ludovic Marie 67c27701e9 Server: new players join around the spawn, not all on one spot
The server put every player new to the world exactly on the world spawn.
On the server thread the host's own player joins that way too, so the host
and each new guest stood inside each other: the guest's client targets
the host's player (its eye inside that box, as vanilla's getEntityHitResult
does), so it could open no crafting table, chest or furnace, and Entity.push
never parts two players with the same centre. A dedicated server stacked
its new players the same way.

Vanilla places each new player with ServerPlayer.adjustSpawnLocation:
PlayerSpawnFinder.findSpawn, a free column at a random offset within the
respawn radius. Game::server_player_spawn does it for a server's new
players with the window's own finder, passing over the candidates whose
chunk isn't loaded yet (vanilla waits for them; a server tick can't).

lan_e2e's new check `spawned` asserts that the host and the guest join on
different spots, then has the guest open a crafting table, a chest, a
furnace and a stonecutter where it joined.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 21:16:43 +01:00

248 lines
9.4 KiB
Rust

//! A new world's spawn and its first player's position: MinecraftServer.
//! setInitialSpawn walks a chunk spiral around NoiseBasedChunkGenerator.
//! getOrigin, then PlayerSpawnFinder.findSpawn picks a free column around
//! the world spawn within the respawn radius.
use super::{Game, dispenser::entity_random};
use horizon_client::{
Result,
assets::Properties,
chunk::{HEIGHT, MIN_Y},
collisions::{SceneCollisions, no_collision_or_liquid},
world::World,
world_spawn::{
PlayerSpawnFinder, SpawnCandidate, SpawnLevel, level_respawn_pos, spawn_pos_in_chunk,
},
};
use horizon_core::{
geometry::{Aabb, Direction, ShapeOp, Vec3, VoxelShape},
position::BlockPos,
random::RandomSource,
};
use horizon_world::{
block_collision::BlockCollisionRegistry, collision_context::CollisionContext,
heightmap::HeightmapType, worldgen::terrain::TerrainBlockStates,
};
/// The loaded world as the spawn searches query it.
struct Columns<'a, 'r> {
world: &'a World,
properties: &'a Properties,
binding: &'a BlockCollisionRegistry<'r>,
}
impl SpawnLevel for Columns<'_, '_> {
fn min_y(&self) -> i32 {
MIN_Y
}
fn height(&self, kind: HeightmapType, x: i32, z: i32) -> i32 {
(MIN_Y..MIN_Y + HEIGHT)
.rev()
.find(|&y| kind.is_opaque(self.properties.heightmap_state(self.world.block(x, y, z))))
.map_or(MIN_Y, |y| y + 1)
}
fn has_fluid(&self, x: i32, y: i32, z: i32) -> bool {
use horizon_world::heightmap::HeightmapState;
self.properties
.heightmap_state(self.world.block(x, y, z))
.has_fluid()
}
fn top_face_full(&self, x: i32, y: i32, z: i32) -> bool {
let mut scene = SceneCollisions {
world: self.world,
binding: self.binding,
subject: None,
};
let state = self.world.block(x, y, z);
let Ok(shape) = self.binding.collision_shape_with_context(
&mut scene,
state,
BlockPos::new(x, y, z),
&CollisionContext::empty(false),
) else {
return false;
};
shape.face_shape(Direction::Up).is_ok_and(|face| {
VoxelShape::block()
.join_is_not_empty(&face, ShapeOp::XOR)
.is_ok_and(|differs| !differs)
})
}
}
impl Game<'_> {
fn spawn_columns(&self) -> Columns<'_, '_> {
Columns {
world: &self.world,
properties: &self.pack.assets.properties,
binding: self.collision,
}
}
/// ChunkGenerator.getOrigin and getSpawnHeight of the overworld.
fn spawn_origin(&self) -> Result<((i32, i32), i32)> {
if let Some(flat) =
horizon_client::save::world_gen_settings(&self.world_root).and_then(|s| {
horizon_client::flatgen::FlatTerrain::from_settings(
&s,
self.pack.assets.registry,
self.res.biomes(),
)
})
{
return Ok(((0, 0), flat.spawn_height()));
}
if self.options.test_terrain {
return Ok(((0, 0), 64));
}
let origin = match self.end.locator() {
Some(terrain) => terrain.origin()?,
None => horizon_core::position::ChunkPos::new(0, 0),
};
Ok(((origin.x, origin.z), 64))
}
/// MinecraftServer.setInitialSpawn: the first chunk of the spiral with a
/// valid respawn column gives the world spawn, else the origin chunk's
/// centre at the spawn height. None while a chunk to test is loading.
pub(super) fn find_world_spawn(&mut self) -> Result<Option<[i32; 3]>> {
let ((ox, oz), height) = match self.spawn_origin {
Some(origin) => origin,
None => *self.spawn_origin.insert(self.spawn_origin()?),
};
while let Some((dx, dz)) = self.spawn_spiral.current() {
if self.spawn_spiral.tested() {
let (cx, cz) = (ox + dx, oz + dz);
if !self.streaming.is_lit(cx, cz) {
self.streaming.center = (cx, cz);
return Ok(None);
}
if let Some(pos) = spawn_pos_in_chunk(&self.spawn_columns(), cx, cz) {
return Ok(Some(pos));
}
}
self.spawn_spiral.advance();
}
Ok(Some([ox * 16 + 8, height, oz * 16 + 8]))
}
/// PlayerSpawnFinder.findSpawn around the world spawn: the feet of a
/// player who has no position yet. None while a candidate's chunk loads.
pub(super) fn find_player_spawn(&mut self, suggestion: [i32; 3]) -> Result<Option<Vec3>> {
let mut finder = match self.player_spawn {
Some(finder) => finder,
None => self.spawn_finder(suggestion),
};
let found = loop {
let Some(candidate) = finder.next_candidate() else {
break Some(self.fixup_spawn_height(suggestion));
};
let (x, z) = match candidate {
SpawnCandidate::Column(x, z) => (x, z),
SpawnCandidate::Suggestion => (suggestion[0], suggestion[2]),
};
if !self.streaming.is_lit(x >> 4, z >> 4) {
self.streaming.center = (x >> 4, z >> 4);
finder.retry();
break None;
}
match candidate {
SpawnCandidate::Column(x, z) => {
if let Some(pos) = level_respawn_pos(&self.spawn_columns(), x, z)
&& self.player_fits(pos)
{
break Some(bottom_center(pos));
}
}
SpawnCandidate::Suggestion => break Some(self.fixup_spawn_height(suggestion)),
}
};
self.player_spawn = found.is_none().then_some(finder);
Ok(found)
}
/// A new PlayerSpawnFinder around `suggestion`: the respawn radius, cut
/// by the world border; the suggestion itself in adventure mode.
fn spawn_finder(&self, suggestion: [i32; 3]) -> PlayerSpawnFinder {
if self.level.game_mode == 2 {
let mut finder = PlayerSpawnFinder::new(suggestion, 0, |_| 0);
finder.next_candidate();
return finder;
}
let mut radius = self.ui.rules.int("respawn_radius").max(0);
let to_border = self
.border
.distance(f64::from(suggestion[0]), f64::from(suggestion[2]))
.floor() as i32;
if to_border < radius {
radius = to_border;
}
if to_border <= 1 {
radius = 1;
}
PlayerSpawnFinder::new(suggestion, radius, |n| {
entity_random().next_int_bound(n).unwrap_or(0)
})
}
/// ServerPlayer.adjustSpawnLocation for a server's new player: where
/// PlayerSpawnFinder.findSpawn puts their feet around the world spawn
/// (`spawn`, the feet there), so that players new to a world don't all
/// stand in one spot. Vanilla waits for each candidate's chunk; a server
/// tick can't, so a candidate in a chunk not loaded yet is passed over,
/// and `spawn` stays when the search never reaches a loaded column.
pub(super) fn server_player_spawn(&self, spawn: [f64; 3]) -> [f64; 3] {
let suggestion = spawn.map(|v| v.floor() as i32);
let mut finder = self.spawn_finder(suggestion);
while let Some(candidate) = finder.next_candidate() {
let (x, z) = match candidate {
SpawnCandidate::Column(x, z) => (x, z),
SpawnCandidate::Suggestion => (suggestion[0], suggestion[2]),
};
if !self.streaming.is_lit(x >> 4, z >> 4) {
continue;
}
let feet = match candidate {
SpawnCandidate::Column(x, z) => level_respawn_pos(&self.spawn_columns(), x, z)
.filter(|&pos| self.player_fits(pos))
.map(bottom_center),
SpawnCandidate::Suggestion => Some(self.fixup_spawn_height(suggestion)),
};
if let Some(feet) = feet {
return [feet.x, feet.y, feet.z];
}
}
spawn
}
/// PlayerSpawnFinder.noCollisionNoLiquid: a player's box standing at the
/// block's bottom centre touches no block or liquid.
fn player_fits(&self, pos: [i32; 3]) -> bool {
let feet = bottom_center(pos);
let bounds = Aabb::new(
Vec3::new(feet.x - 0.3, feet.y, feet.z - 0.3),
Vec3::new(feet.x + 0.3, feet.y + 1.8, feet.z + 0.3),
);
let mut scene = SceneCollisions {
world: &self.world,
binding: self.collision,
subject: None,
};
no_collision_or_liquid(&mut scene, bounds)
}
/// PlayerSpawnFinder.fixupSpawnHeight: up out of anything solid, then
/// down onto the ground.
fn fixup_spawn_height(&self, pos: [i32; 3]) -> Vec3 {
let [x, mut y, z] = pos;
while !self.player_fits([x, y, z]) && y < MIN_Y + HEIGHT - 1 {
y += 1;
}
y -= 1;
while self.player_fits([x, y, z]) && y > MIN_Y {
y -= 1;
}
bottom_center([x, y + 1, z])
}
}
/// Vec3.atBottomCenterOf.
fn bottom_center(pos: [i32; 3]) -> Vec3 {
Vec3::new(
f64::from(pos[0]) + 0.5,
f64::from(pos[1]),
f64::from(pos[2]) + 0.5,
)
}