diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 2d853cbe5..e6abe53b4 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -1366,6 +1366,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, @@ -4256,8 +4259,12 @@ impl Game<'_> { 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. - self.respawn_spawn(column) + // 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 @@ -11137,8 +11144,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))); @@ -12426,6 +12439,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 f11d4a61c..cffe3aa4b 100644 --- a/crates/horizon-client/src/game/end.rs +++ b/crates/horizon-client/src/game/end.rs @@ -11,6 +11,7 @@ use horizon_client::{ motion::PlayerMotion, particles::{ParticleOptions, ParticleType}, vanillagen::VanillaTerrain, + world_spawn::PlayerSpawnFinder, }; use horizon_core::{ geometry::Vec3, @@ -521,7 +522,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; @@ -546,7 +555,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]]; @@ -566,14 +581,14 @@ 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); @@ -582,10 +597,16 @@ impl Game<'_> { // 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((self.respawn_spawn(world), None)); + 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((Vec3::new(target.x, y, target.z), None)) + 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 bcea90337..d4c48c140 100644 --- a/crates/horizon-client/src/game/initial_spawn.rs +++ b/crates/horizon-client/src/game/initial_spawn.rs @@ -145,61 +145,58 @@ impl Game<'_> { entity_random().next_int_bound(n).unwrap_or(0) }) } - /// PlayerSpawnFinder.findSpawn around the world spawn for a player placed - /// at once (TeleportTransition.createDefault and missingRespawnBlock): the - /// first free column within the respawn radius, the suggestion's own - /// column when none is. A candidate whose chunk is not loaded counts as - /// not free; the suggestion's chunk must be. - pub(super) fn respawn_spawn(&self, suggestion: [i32; 3]) -> Vec3 { - let mut finder = self.spawn_finder(suggestion); - while let Some(candidate) = finder.next_candidate() { - let SpawnCandidate::Column(x, z) = candidate else { - break; - }; - if !self.streaming.is_lit(x >> 4, z >> 4) { - continue; - } - if let Some(pos) = level_respawn_pos(&self.spawn_columns(), x, z) - && self.player_fits(pos) - { - return bottom_center(pos); - } - } - self.fixup_spawn_height(suggestion) - } - /// 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) + } + } } /// PlayerSpawnFinder.noCollisionNoLiquid: a player's box standing at the /// block's bottom centre touches no block or liquid. diff --git a/crates/horizon-client/src/game/server_travel.rs b/crates/horizon-client/src/game/server_travel.rs index e22cd60e9..ac983dff1 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}; @@ -34,6 +34,8 @@ pub(super) struct Trip { /// 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, } @@ -258,6 +260,7 @@ impl Game<'_> { gateway: None, respawn: None, world_spawn: false, + finder: None, placed: false, }); if let Err(e) = self.enter_level(Some(host), source) { @@ -278,6 +281,7 @@ impl Game<'_> { gateway: None, respawn: Some(config), world_spawn: false, + finder: None, placed: false, }); } @@ -327,6 +331,7 @@ impl Game<'_> { gateway: None, respawn: None, world_spawn: true, + finder: None, placed: false, }); } @@ -372,6 +377,7 @@ impl Game<'_> { gateway: Some(job), respawn: None, world_spawn: false, + finder: None, placed: false, }); } @@ -398,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; } @@ -416,7 +422,15 @@ impl Game<'_> { // PlayerSpawnFinder.findSpawn around the world spawn. let suggestion = [trip.target.x, trip.target.y, trip.target.z].map(|v| v.floor() as i32); - (self.respawn_spawn(suggestion), yaw) + match self.search_spawn(suggestion, &mut trip.finder) { + 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), @@ -426,12 +440,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) @@ -493,6 +514,7 @@ impl Game<'_> { gateway: None, respawn: None, world_spawn: false, + finder: None, placed: true, }); if let Err(e) = self.enter_level(Some(host), source) {