diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 9c369a2bc..d2454a55a 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -1461,6 +1461,9 @@ struct Game<'a> { spawn_spiral: horizon_client::world_spawn::ChunkSpiral, /// A new player's spawn search, while its candidates' chunks load. player_spawn: Option, + /// Chunks a respawn search waits for this tick, kept loading beside the + /// player's own (taken by `update_world`). + spawn_wait: Vec<(i32, i32)>, mobs: Vec, creatures: creatures::Creatures, sculk: sculk::SculkState, @@ -4731,11 +4734,22 @@ impl Game<'_> { if !(-1..=1).all(|dz| (-1..=1).all(|dx| self.streaming.is_lit(cx + dx, cz + dz))) { return Ok(()); } - // WorldSpawn: the stored column, lifted onto its current surface. - let y = self - .land_column(s.x.floor() as i32, s.z.floor() as i32) - .map_or(s.y, f64::from); - Vec3::new(s.x, y.max(s.y), s.z) + let column = [s.x.floor() as i32, s.y.floor() as i32, s.z.floor() as i32]; + if self.level.world_spawn == Some(column) { + // TeleportTransition.missingRespawnBlock: PlayerSpawnFinder + // around the world spawn, not its exact column; its candidates' + // chunks load first. + let Some(feet) = self.find_player_spawn(column)? else { + return Ok(()); + }; + feet + } else { + // A spawnpoint: the stored column, lifted onto its current surface. + let y = self + .land_column(column[0], column[2]) + .map_or(s.y, f64::from); + Vec3::new(s.x, y.max(s.y), s.z) + } }; let mut player = PlayerMotion::new(self.motion, feet)?; player.yaw = self.yaw; @@ -12042,8 +12056,14 @@ impl Game<'_> { self.streaming.loading = self.options.execution_role.is_client() && self.loading_reason().is_some(); self.tick_pregen(); + let areas = self.streaming.extra.len(); + let waiting = std::mem::take(&mut self.spawn_wait); + self.streaming + .extra + .extend(waiting.into_iter().map(|chunk| (chunk, 0))); let (uploads, unloaded) = horizon_client::spans::time("streaming", || self.streaming.update(&mut self.world)); + self.streaming.extra.truncate(areas); if let Some(lod) = &mut self.lod { self.lod_observe .extend(uploads.iter().map(|(key, _)| (key.0, key.2))); @@ -13397,6 +13417,7 @@ impl<'a> Game<'a> { spawn_origin: None, spawn_spiral: Default::default(), player_spawn: None, + spawn_wait: Vec::new(), mobs: Vec::new(), creatures: Default::default(), sculk: Default::default(), diff --git a/crates/horizon-client/src/game/end.rs b/crates/horizon-client/src/game/end.rs index 41c4e1628..3c5cce750 100644 --- a/crates/horizon-client/src/game/end.rs +++ b/crates/horizon-client/src/game/end.rs @@ -12,6 +12,7 @@ use horizon_client::{ motion::PlayerMotion, particles::{ParticleOptions, ParticleType}, vanillagen::VanillaTerrain, + world_spawn::PlayerSpawnFinder, }; use horizon_core::{ geometry::Vec3, @@ -581,7 +582,15 @@ impl Game<'_> { let position = if let Some(job) = self.end.job.take() { self.resolve_gateway(job, target)? } else { - let (position, yaw) = self.end_arrival(target)?; + let mut finder = self.player_spawn.take(); + let arrival = self.end_arrival(target, &mut finder); + self.player_spawn = finder; + let Some((position, yaw)) = arrival? else { + // A spawn candidate's chunk is loading: try again next tick. + self.travelling = Some((player, target)); + self.end_travel = true; + return Ok(()); + }; if let Some(yaw) = yaw { self.yaw = yaw; player.yaw = yaw; @@ -606,7 +615,13 @@ impl Game<'_> { /// Where an End portal's traveller lands once `target` has loaded, and /// the yaw it faces there: the platform below the End's spawn point, /// facing west, or the top of the Overworld's spawn column. - pub(super) fn end_arrival(&mut self, target: Vec3) -> Result<(Vec3, Option)> { + /// None while a candidate of the world spawn's search loads (`finder` + /// keeps it, see `search_spawn`). + pub(super) fn end_arrival( + &mut self, + target: Vec3, + finder: &mut Option, + ) -> Result)>> { if self.dim == Dim::End { // EndPlatformFeature.createEndPlatform below the spawn point. let origin = [END_SPAWN[0], END_SPAWN[1] - 1, END_SPAWN[2]]; @@ -626,22 +641,32 @@ impl Game<'_> { } } // Direction.WEST.toYRot(). - return Ok((target, Some(90.0))); + return Ok(Some((target, Some(90.0)))); } // ServerPlayer.findRespawnPositionAndUseSpawnBlock: where the bed or // anchor puts the player, facing it; else onto the spawn column. let mut target = target; if let Some(c) = self.respawn_config.filter(|c| c.dim == self.dim) { if let Some((at, yaw)) = self.find_respawn_and_use(c, false)? { - return Ok((at, Some(yaw))); + return Ok(Some((at, Some(yaw)))); } let s = self.level.spawn.unwrap_or([0.0, 64.0, 0.0]); target = Vec3::new(s[0].floor() + 0.5, s[1], s[2].floor() + 0.5); } - let y = self - .land_column(target.x.floor() as i32, target.z.floor() as i32) - .map_or(target.y, f64::from); - Ok((Vec3::new(target.x, y, target.z), None)) + let (x, z) = (target.x.floor() as i32, target.z.floor() as i32); + // TeleportTransition.missingRespawnBlock: PlayerSpawnFinder around + // the world spawn. + if let Some(world) = self.level.world_spawn.filter(|w| w[0] == x && w[2] == z) { + return Ok(match self.search_spawn(world, finder) { + Ok(at) => Some((at, None)), + Err(chunk) => { + self.spawn_wait.push(chunk); + None + } + }); + } + let y = self.land_column(x, z).map_or(target.y, f64::from); + Ok(Some((Vec3::new(target.x, y, target.z), None))) } /// Dying away from the Overworld respawns at the world spawn there. diff --git a/crates/horizon-client/src/game/initial_spawn.rs b/crates/horizon-client/src/game/initial_spawn.rs index c1149b26a..ca727c5d0 100644 --- a/crates/horizon-client/src/game/initial_spawn.rs +++ b/crates/horizon-client/src/game/initial_spawn.rs @@ -212,39 +212,58 @@ impl Game<'_> { ) .is_ok_and(|shape| shape.is_empty()) } - /// 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> { - let mut finder = match self.player_spawn { - Some(finder) => finder, - None => self.spawn_finder(suggestion), - }; - let found = loop { + /// PlayerSpawnFinder.findSpawn around the world spawn, as far as the loaded + /// chunks allow: the feet of the first free column within the respawn + /// radius, else the suggestion's own column. The finder waits in `state` + /// (None to begin); Err gives the chunk of the candidate being waited for, + /// which the caller must have loaded (the finder tries at most 1024 + /// columns). + pub(super) fn search_spawn( + &self, + suggestion: [i32; 3], + state: &mut Option, + ) -> std::result::Result { + let mut finder = state + .take() + .unwrap_or_else(|| self.spawn_finder(suggestion)); + let suggestion = finder.suggestion; + loop { let Some(candidate) = finder.next_candidate() else { - break Some(self.fixup_spawn_height(suggestion)); + return Ok(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; + *state = Some(finder); + return Err((x >> 4, z >> 4)); } 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)); + return Ok(bottom_center(pos)); } } - SpawnCandidate::Suggestion => break Some(self.fixup_spawn_height(suggestion)), + SpawnCandidate::Suggestion => return Ok(self.fixup_spawn_height(suggestion)), } - }; - self.player_spawn = found.is_none().then_some(finder); - Ok(found) + } + } + /// 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> { + let mut state = self.player_spawn.take(); + match self.search_spawn(suggestion, &mut state) { + Ok(feet) => Ok(Some(feet)), + Err(chunk) => { + self.streaming.center = chunk; + self.player_spawn = state; + Ok(None) + } + } } /// A new PlayerSpawnFinder around `suggestion`: the respawn radius, cut /// by the world border; the suggestion itself in adventure mode. diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index a6a683827..f941b4b31 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -1968,6 +1968,10 @@ impl Game<'_> { } if let Some(config) = elsewhere { self.respawn_trip(conn, config); + } else if in_frame_bed.is_none() && host.hub.guests[i].spawn.is_none() { + // TeleportTransition.createDefault: no point of its own, so the + // world spawn's free column once its chunks have loaded. + self.world_spawn_trip(conn); } if let Some(c) = in_frame_bed && self.dim == super::Dim::Overworld @@ -1986,6 +1990,10 @@ impl Game<'_> { host.hub.guests[i].yaw = yaw; [at.x, at.y, at.z] }); + if found.is_none() { + // TeleportTransition.missingRespawnBlock. + self.world_spawn_trip(conn); + } host.hub.teleport(i, Some(at)); } if away && let Err(e) = self.enter_level(Some(host), back) { diff --git a/crates/horizon-client/src/game/nether_portal.rs b/crates/horizon-client/src/game/nether_portal.rs index 95c38d9d1..d8c76231d 100644 --- a/crates/horizon-client/src/game/nether_portal.rs +++ b/crates/horizon-client/src/game/nether_portal.rs @@ -297,7 +297,8 @@ impl Game<'_> { let p = plan.approximate; let centre = Vec3::new(f64::from(p.x) + 0.5, f64::from(p.y), f64::from(p.z) + 0.5); if plan.dim == Dim::End { - if let Err(e) = self.end_arrival(centre) { + // In the End the arrival only builds the platform: no spawn search. + if let Err(e) = self.end_arrival(centre, &mut None) { log::error!("End platform: {e}"); } return centre; diff --git a/crates/horizon-client/src/game/server_travel.rs b/crates/horizon-client/src/game/server_travel.rs index bcaf37a76..e516d3f67 100644 --- a/crates/horizon-client/src/game/server_travel.rs +++ b/crates/horizon-client/src/game/server_travel.rs @@ -11,7 +11,7 @@ use super::end::GatewayJob; use super::nether_portal::{PLAYER_DELAY, Plan}; use super::respawn_anchor::RespawnConfig; use super::{Dim, Game, lan::LanHost}; -use horizon_client::{lan::hub::TickCtx, text::Text}; +use horizon_client::{lan::hub::TickCtx, text::Text, world_spawn::PlayerSpawnFinder}; use horizon_core::{geometry::Vec3, position::BlockPos}; use horizon_protocol::packets::play::{self, Clientbound as C}; @@ -31,6 +31,11 @@ pub(super) struct Trip { /// A respawn at this point outside the Overworld, resolved through its /// block on arrival. respawn: Option, + /// A respawn at the world spawn, moved onto a free column within the + /// respawn radius once the chunks around it have loaded. + world_spawn: bool, + /// The search for that column while a candidate's chunk loads. + finder: Option, /// Sent by a command: the player is already where it goes. placed: bool, } @@ -66,10 +71,11 @@ impl Game<'_> { /// in it (Nether portals count up their transition time, End portals /// take them at once). pub(super) fn tick_server_travel(&mut self, host: &mut LanHost) { + // Outside a server only the respawns at the world spawn wait for chunks. + self.arrive_server_trips(host); if self.options.dedicated.is_none() { return; } - self.arrive_server_trips(host); let conns: Vec = host .hub .guests @@ -253,6 +259,8 @@ impl Game<'_> { plan, gateway: None, respawn: None, + world_spawn: false, + finder: None, placed: false, }); if let Err(e) = self.enter_level(Some(host), source) { @@ -272,6 +280,8 @@ impl Game<'_> { plan: None, gateway: None, respawn: Some(config), + world_spawn: false, + finder: None, placed: false, }); } @@ -304,6 +314,28 @@ impl Game<'_> { } } + /// TeleportTransition.createDefault and missingRespawnBlock: the player + /// waits at the world spawn for its chunks, then PlayerSpawnFinder puts + /// it on a free column within the respawn radius. + pub(super) fn world_spawn_trip(&mut self, conn: u32) { + let Some([x, y, z]) = self.level.world_spawn else { + return; + }; + self.server_trips.retain(|t| !t.is(conn)); + self.server_trips.push(Trip { + conn, + dim: Dim::Overworld, + target: Vec3::new(f64::from(x) + 0.5, f64::from(y), f64::from(z) + 0.5), + end: false, + plan: None, + gateway: None, + respawn: None, + world_spawn: true, + finder: None, + placed: false, + }); + } + /// TeleportTransition.missingRespawnBlock: the world spawn, in the /// Overworld. fn respawn_at_world_spawn(&mut self, host: &mut LanHost, conn: u32) { @@ -318,6 +350,7 @@ impl Game<'_> { rotation: None, }); self.run_level_teleports(host); + self.world_spawn_trip(conn); } /// `TheEndGatewayBlockEntity.getPortalPosition` and the teleport within @@ -343,6 +376,8 @@ impl Game<'_> { plan: None, gateway: Some(job), respawn: None, + world_spawn: false, + finder: None, placed: false, }); } @@ -369,7 +404,7 @@ impl Game<'_> { k += 1; continue; } - let trip = self.server_trips.remove(k); + let mut trip = self.server_trips.remove(k); if trip.placed { continue; } @@ -383,6 +418,22 @@ impl Game<'_> { continue; } } + } else if trip.world_spawn { + // PlayerSpawnFinder.findSpawn around the world spawn. + let suggestion = + [trip.target.x, trip.target.y, trip.target.z].map(|v| v.floor() as i32); + match self.search_spawn(suggestion, &mut trip.finder) { + // The respawn already put the player on this column: + // vanilla teleports once, so no second move. + Ok(at) if host.hub.guests[i].position == [at.x, at.y, at.z] => continue, + Ok(at) => (at, yaw), + Err(chunk) => { + self.spawn_wait.push(chunk); + self.server_trips.insert(k, trip); + k += 1; + continue; + } + } } else if let Some(job) = trip.gateway { match self.resolve_gateway(job, trip.target) { Ok(at) => (at, yaw), @@ -392,12 +443,19 @@ impl Game<'_> { } } } else if trip.end { - let arrival = match self.as_guest(host, trip.conn, |g| g.end_arrival(trip.target)) { + let arrival = match self.as_guest(host, trip.conn, |g| { + g.end_arrival(trip.target, &mut trip.finder) + }) { Some(arrival) => arrival, - None => self.end_arrival(trip.target), + None => self.end_arrival(trip.target, &mut trip.finder), }; match arrival { - Ok((at, turned)) => (at, turned.unwrap_or(yaw)), + Ok(Some((at, turned))) => (at, turned.unwrap_or(yaw)), + Ok(None) => { + self.server_trips.insert(k, trip); + k += 1; + continue; + } Err(e) => { log::error!("End arrival: {e}"); (trip.target, yaw) @@ -458,6 +516,8 @@ impl Game<'_> { plan: None, gateway: None, respawn: None, + world_spawn: false, + finder: None, placed: true, }); if let Err(e) = self.enter_level(Some(host), source) {