diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 8448f0837..2d853cbe5 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -4253,11 +4253,18 @@ 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. + self.respawn_spawn(column) + } 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; diff --git a/crates/horizon-client/src/game/end.rs b/crates/horizon-client/src/game/end.rs index 32d5113b0..f11d4a61c 100644 --- a/crates/horizon-client/src/game/end.rs +++ b/crates/horizon-client/src/game/end.rs @@ -578,9 +578,13 @@ impl Game<'_> { 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); + 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((self.respawn_spawn(world), None)); + } + let y = self.land_column(x, z).map_or(target.y, f64::from); Ok((Vec3::new(target.x, y, target.z), None)) } diff --git a/crates/horizon-client/src/game/initial_spawn.rs b/crates/horizon-client/src/game/initial_spawn.rs index 3f7a071bb..bcea90337 100644 --- a/crates/horizon-client/src/game/initial_spawn.rs +++ b/crates/horizon-client/src/game/initial_spawn.rs @@ -121,33 +121,58 @@ impl Game<'_> { } Ok(Some([ox * 16 + 8, height, oz * 16 + 8])) } + /// PlayerSpawnFinder.findSpawn: the finder for a suggestion, its radius + /// the respawn radius cut to the world border. + fn spawn_finder(&self, suggestion: [i32; 3]) -> PlayerSpawnFinder { + if self.level.game_mode == 2 { + // Adventure mode: the suggestion itself. + 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) + }) + } + /// 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 if self.level.game_mode == 2 => { - // Adventure mode: the suggestion itself. - let mut finder = PlayerSpawnFinder::new(suggestion, 0, |_| 0); - finder.next_candidate(); - finder - } - None => { - 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) - }) - } + None => self.spawn_finder(suggestion), }; let found = loop { let Some(candidate) = finder.next_candidate() else { diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 43bb573dc..dcb3c8919 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -1853,6 +1853,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 @@ -1871,6 +1875,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/server_travel.rs b/crates/horizon-client/src/game/server_travel.rs index bcaf37a76..e22cd60e9 100644 --- a/crates/horizon-client/src/game/server_travel.rs +++ b/crates/horizon-client/src/game/server_travel.rs @@ -31,6 +31,9 @@ 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, /// Sent by a command: the player is already where it goes. placed: bool, } @@ -66,10 +69,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 +257,7 @@ impl Game<'_> { plan, gateway: None, respawn: None, + world_spawn: false, placed: false, }); if let Err(e) = self.enter_level(Some(host), source) { @@ -272,6 +277,7 @@ impl Game<'_> { plan: None, gateway: None, respawn: Some(config), + world_spawn: false, placed: false, }); } @@ -304,6 +310,27 @@ 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, + placed: false, + }); + } + /// TeleportTransition.missingRespawnBlock: the world spawn, in the /// Overworld. fn respawn_at_world_spawn(&mut self, host: &mut LanHost, conn: u32) { @@ -318,6 +345,7 @@ impl Game<'_> { rotation: None, }); self.run_level_teleports(host); + self.world_spawn_trip(conn); } /// `TheEndGatewayBlockEntity.getPortalPosition` and the teleport within @@ -343,6 +371,7 @@ impl Game<'_> { plan: None, gateway: Some(job), respawn: None, + world_spawn: false, placed: false, }); } @@ -383,6 +412,11 @@ 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); + (self.respawn_spawn(suggestion), yaw) } else if let Some(job) = trip.gateway { match self.resolve_gateway(job, trip.target) { Ok(at) => (at, yaw), @@ -458,6 +492,7 @@ impl Game<'_> { plan: None, gateway: None, respawn: None, + world_spawn: false, placed: true, }); if let Err(e) = self.enter_level(Some(host), source) {