diff --git a/crates/horizon-client/examples/lan_flat.rs b/crates/horizon-client/examples/lan_flat.rs index 283c3a22b..53a4d62f7 100644 --- a/crates/horizon-client/examples/lan_flat.rs +++ b/crates/horizon-client/examples/lan_flat.rs @@ -118,6 +118,7 @@ fn main() -> Result<(), Box> { clock_rate: 1.0, tick_rate: 20.0, tick_frozen: false, + tick_steps: (0, 0), collision: None, }; for event in hub.tick(&mut ctx) { diff --git a/crates/horizon-client/src/clock.rs b/crates/horizon-client/src/clock.rs index accf07fc9..3240944ba 100644 --- a/crates/horizon-client/src/clock.rs +++ b/crates/horizon-client/src/clock.rs @@ -301,6 +301,9 @@ pub struct FixedStep { /// A server's client: its level's tick rate only slows the timer down /// (`Minecraft.getTickTargetMillis`), since the server runs the ticks. client: bool, + /// How many times `/tick step` started or stopped a step + /// (`ServerTickRateManager.updateStepTicks` broadcasts each one). + step_changes: u32, } impl Default for FixedStep { @@ -316,6 +319,7 @@ impl Default for FixedStep { aggregated: 0, tick_count: 0, client: false, + step_changes: 0, } } } @@ -372,6 +376,12 @@ impl FixedStep { pub fn fraction(&self) -> f64 { f64::from(self.timer.game_time_delta_partial_tick(false)) } + /// The camera entity's partial tick (`Camera.getCameraEntityPartialTicks` + /// for a player, who is never frozen): `getGameTimeDeltaPartialTick(true)`, + /// so the view and the hand stay smooth in a frozen level. + pub fn camera_fraction(&self) -> f64 { + f64::from(self.timer.game_time_delta_partial_tick(true)) + } /// After a frame: whether the game is paused by a screen (the residual /// stops accumulating) and whether the level is frozen. pub fn end_frame(&mut self, pause: bool) { @@ -438,11 +448,21 @@ impl FixedStep { } /// `stepGameIfPaused`: only works while frozen. pub fn start_step(&mut self, ticks: u32) -> bool { - self.manager - .step_game_if_paused(ticks.min(i32::MAX as u32) as i32) + let started = self + .manager + .step_game_if_paused(ticks.min(i32::MAX as u32) as i32); + self.step_changes += u32::from(started); + started } pub fn stop_step(&mut self) -> bool { - self.manager.stop_stepping() + let stopped = self.manager.stop_stepping(); + self.step_changes += u32::from(stopped); + stopped + } + /// What `ClientboundTickingStepPacket` carries (`frozenTicksToRun`), and + /// a count that changes each time `updateStepTicks` would broadcast it. + pub fn step_state(&self) -> (u32, i32) { + (self.step_changes, self.manager.base.frozen_ticks_to_run()) } /// Record how long a tick took (`MinecraftServer.tickServer` bookkeeping /// and `endTickWork`). @@ -506,6 +526,30 @@ mod tests { assert!(!m.base.runs_normally()); } #[test] + fn the_camera_keeps_interpolating_in_a_frozen_level() { + let mut clock = FixedStep::default(); + clock.set_frozen(true); + clock.world_runs(); + clock.advance(std::time::Duration::from_millis(25)); + clock.end_frame(false); + assert_eq!(clock.fraction(), 1.0, "frozen entities stay put"); + assert_eq!(clock.camera_fraction(), 0.5, "the player does not"); + } + #[test] + fn only_a_started_or_stopped_step_is_broadcast() { + let mut clock = FixedStep::default(); + assert!(!clock.start_step(5), "not frozen"); + assert_eq!(clock.step_state(), (0, 0)); + clock.set_frozen(true); + assert!(clock.start_step(5)); + assert_eq!(clock.step_state(), (1, 5)); + clock.world_runs(); + assert_eq!(clock.step_state(), (1, 4), "a step tick is not broadcast"); + assert!(clock.stop_step()); + assert!(!clock.stop_step()); + assert_eq!(clock.step_state(), (2, 0)); + } + #[test] fn a_servers_tick_rate_only_slows_its_client_down() { let quarter = std::time::Duration::from_millis(250); let mut client = FixedStep::client(); diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index fc6003af0..cd0249384 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -6016,7 +6016,9 @@ impl Game<'_> { && p.fire_ticks > 0 && !p.spectator { - let feet = self.previous_position.lerp(p.position, alpha); + let feet = self + .previous_position + .lerp(p.position, self.clock.camera_fraction()); horizon_client::fire_render::flames( [feet.x, feet.y, feet.z], p.dimensions.width, @@ -6082,7 +6084,7 @@ impl Game<'_> { self.ominous_item_spawner_geometry(camera, alpha, &mut out); // Mobs and their projectiles. self.mob_geometry(camera, alpha, &mut out); - self.player_model_geometry(camera, alpha, &mut out); + self.player_model_geometry(camera, self.clock.camera_fraction(), &mut out); self.guest_model_geometry(camera, alpha, &mut out); self.fangs_geometry(camera, alpha, &mut out); let mut see_through = std::mem::take(&mut self.see_through_vertices); @@ -9087,11 +9089,14 @@ impl Game<'_> { return Ok(false); }; let alpha = self.clock.fraction(); + // Camera.getCameraEntityPartialTicks: the view, the hand and the + // player's own model stay smooth while the level is frozen. + let camera_alpha = self.clock.camera_fraction(); let live = self.backdrop_live(); let view = if live { self.backdrop_view() } else { - self.camera_view(alpha as f32) + self.camera_view(camera_alpha as f32) }; let (camera, underwater, view_yaw, view_pitch) = match view { Some((c, yaw, pitch)) => { @@ -9232,12 +9237,12 @@ impl Game<'_> { } else if title { 85.0 } else { - self.fov_degrees(alpha as f32, camera.to_array()) + self.fov_degrees(camera_alpha as f32, camera.to_array()) }; let bob = if title { glam::Mat4::IDENTITY } else { - self.bob_matrix(alpha as f32) + self.bob_matrix(camera_alpha as f32) }; let terrain_distance = self.terrain_distance(); let distance = (terrain_distance * 16) as f32; @@ -9294,7 +9299,7 @@ impl Game<'_> { let hand = if title || self.scoping() { Vec::new() } else { - self.hand_geometry(alpha) + self.hand_geometry(camera_alpha) }; let screen = if title { Vec::new() @@ -9386,7 +9391,7 @@ impl Game<'_> { &[] }, hand: &hand, - hand_fov_degrees: self.hud_fov_degrees(alpha as f32, camera.to_array()), + hand_fov_degrees: self.hud_fov_degrees(camera_alpha as f32, camera.to_array()), screen: &screen, border, game_time: self.world.game_time as f32, diff --git a/crates/horizon-client/src/game/dedicated.rs b/crates/horizon-client/src/game/dedicated.rs index 44bd94082..f6e0ae567 100644 --- a/crates/horizon-client/src/game/dedicated.rs +++ b/crates/horizon-client/src/game/dedicated.rs @@ -695,14 +695,7 @@ impl Game<'_> { // MinecraftServer.tickServer records each tick's time, for /tick query (and the status). self.clock.record(tick_start.elapsed()); state.ticked(Instant::now()); - let rate = self.clock.rate().max(1.0); - next += Duration::from_secs_f64(1.0 / f64::from(rate)); - let now = Instant::now(); - if next > now { - std::thread::sleep(next - now); - } else if now - next > Duration::from_secs(2) { - next = now; - } + self.wait_for_next_server_tick(&mut next, paused); } self.stop_dedicated() } diff --git a/crates/horizon-client/src/game/integrated.rs b/crates/horizon-client/src/game/integrated.rs index f46a28ba8..07b96753f 100644 --- a/crates/horizon-client/src/game/integrated.rs +++ b/crates/horizon-client/src/game/integrated.rs @@ -362,13 +362,16 @@ impl Game<'_> { let mut next = Instant::now(); while !flags.stop.load(Ordering::Relaxed) { self.run_tasks(); - if flags.paused.load(Ordering::Relaxed) { + let paused = flags.paused.load(Ordering::Relaxed); + if paused { // Only the connections run. self.tick_server_timed(true)?; } else { self.follow_distances(&flags); let started = Instant::now(); self.tick_server_timed(false)?; + // MinecraftServer.tickServer's tick times, for /tick query. + self.clock.record(started.elapsed()); self.debug_charts.log_tick(started, started.elapsed()); flags.smoothed_tick_ms.store( self.debug_charts.smoothed_tick_ms.to_bits(), @@ -376,14 +379,7 @@ impl Game<'_> { ); } self.apply_publish(); - let rate = self.clock.rate().max(1.0); - next += Duration::from_secs_f64(1.0 / f64::from(rate)); - let now = Instant::now(); - if next > now { - std::thread::sleep(next - now); - } else if now - next > Duration::from_secs(2) { - next = now; - } + self.wait_for_next_server_tick(&mut next, paused); } self.stop_integrated() } diff --git a/crates/horizon-client/src/game/lan.rs b/crates/horizon-client/src/game/lan.rs index 57c44cb07..18325828d 100644 --- a/crates/horizon-client/src/game/lan.rs +++ b/crates/horizon-client/src/game/lan.rs @@ -847,6 +847,7 @@ impl Game<'_> { clock_rate, tick_rate: self.clock.rate(), tick_frozen: self.clock.frozen(), + tick_steps: self.clock.step_state(), collision: Some(self.collision), }; host.hub.movement_checks = ( @@ -1027,6 +1028,7 @@ impl Game<'_> { clock_rate, tick_rate: self.clock.rate(), tick_frozen: self.clock.frozen(), + tick_steps: self.clock.step_state(), collision: Some(self.collision), }; host.hub.place_new_player(conn, &mut ctx); diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index b00c74b8b..90aa639c8 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -1353,6 +1353,7 @@ impl Game<'_> { clock_rate, tick_rate: self.clock.rate(), tick_frozen: self.clock.frozen(), + tick_steps: self.clock.step_state(), collision: Some(self.collision), }; host.hub.respawn(i, spawn, &ctx); diff --git a/crates/horizon-client/src/game/levels.rs b/crates/horizon-client/src/game/levels.rs index 3225408f5..31b34a9d0 100644 --- a/crates/horizon-client/src/game/levels.rs +++ b/crates/horizon-client/src/game/levels.rs @@ -24,7 +24,11 @@ use horizon_client::{ ticket_levels::ChunkTickets, world::World, }; use horizon_world::level::{LevelEvent, Ticks}; -use std::{collections::HashMap, sync::Arc, time::Instant}; +use std::{ + collections::HashMap, + sync::Arc, + time::{Duration, Instant}, +}; /// Server ticks a level other than the Overworld stays loaded with no player /// in it, before it is saved and closed (vanilla unloads a level's chunks @@ -322,6 +326,35 @@ impl Game<'_> { result } + /// The end of `MinecraftServer.runServer`'s loop for a server thread: a + /// finished `/tick sprint` reports to the operators as the server, then + /// the next tick starts one tick length after this one, or straight away + /// while the sprint runs (`checkShouldSprintThisTick`). + pub(super) fn wait_for_next_server_tick(&mut self, next: &mut Instant, paused: bool) { + if let Some(report) = self.clock.take_sprint_report() { + // ServerTickRateManager.finishTickSprint: the server's sendSuccess. + self.console_broadcast = Some(true); + self.sprint_report(&report); + self.console_broadcast = None; + if let Some(mut host) = self.lan.take() { + self.send_admin_feedback(&mut host, None); + self.lan = Some(host); + } + self.admin_feedback.clear(); + } + let now = Instant::now(); + if !paused && self.clock.sprinting() { + *next = now; + return; + } + *next += self.clock.tick_duration(); + if *next > now { + std::thread::sleep(*next - now); + } else if now - *next > Duration::from_secs(2) { + *next = now; + } + } + /// The levels this server tick runs, in `MinecraftServer.levels` order /// (the Overworld, the Nether, the End): every level with a player in /// it, or the Overworld while nobody is online. diff --git a/crates/horizon-client/src/game/server_travel.rs b/crates/horizon-client/src/game/server_travel.rs index 56fdbaca5..1d857c1fe 100644 --- a/crates/horizon-client/src/game/server_travel.rs +++ b/crates/horizon-client/src/game/server_travel.rs @@ -316,6 +316,7 @@ impl Game<'_> { clock_rate, tick_rate: self.clock.rate(), tick_frozen: self.clock.frozen(), + tick_steps: self.clock.step_state(), collision: Some(self.collision), } } diff --git a/crates/horizon-client/src/lan/hub.rs b/crates/horizon-client/src/lan/hub.rs index 625f18eff..a961d57a4 100644 --- a/crates/horizon-client/src/lan/hub.rs +++ b/crates/horizon-client/src/lan/hub.rs @@ -103,6 +103,9 @@ pub struct TickCtx<'a> { /// TickRateManager: the tick rate and whether the game is frozen. pub tick_rate: f32, pub tick_frozen: bool, + /// `/tick step`: a count that changes with each step started or + /// stopped, and the ticks the frozen game still runs (`FixedStep::step_state`). + pub tick_steps: (u32, i32), /// Block collisions, for ServerGamePacketListenerImpl's moved-wrongly /// check (none: moves are not checked against blocks). pub collision: Option<&'a BlockCollisionRegistry<'a>>, @@ -145,6 +148,8 @@ pub struct Hub { last_clock: i64, last_rate: f32, ticking: (f32, bool), + /// The `tick_steps` count last broadcast. + stepping: u32, weather: (bool, f32, f32), /// The clock and weather last sent in the levels not in place, by /// dimension (`set_dimension` swaps them with the fields above). @@ -278,6 +283,7 @@ impl Hub { last_clock: i64::MIN, last_rate: 0.0, ticking: (20.0, false), + stepping: 0, ops: Default::default(), weather: (false, 0.0, 0.0), level_sync: Default::default(), @@ -880,7 +886,9 @@ impl Hub { })); // TickRateManager.updateJoiningPlayer. g.send(&ticking_state(ctx.tick_rate, ctx.tick_frozen)); - g.send(&raw(ids::TICKING_STEP, vec![0])); + g.send(&C::TickingStep(play::TickingStep { + steps: ctx.tick_steps.1, + })); } /// ServerClockManager.createFullSyncPacket: every clock. Horizon keeps the @@ -1018,6 +1026,7 @@ impl Hub { // (`updateJoiningPlayer`). if !self.guests.iter().any(|g| g.placed) { self.ticking = (ctx.tick_rate, ctx.tick_frozen); + self.stepping = ctx.tick_steps.0; } if !self .guests @@ -1146,6 +1155,13 @@ impl Hub { self.ticking = (ctx.tick_rate, ctx.tick_frozen); self.broadcast(&ticking_state(ctx.tick_rate, ctx.tick_frozen)); } + // ServerTickRateManager.updateStepTicks: a step started or stopped. + if ctx.tick_steps.0 != self.stepping { + self.stepping = ctx.tick_steps.0; + self.broadcast(&C::TickingStep(play::TickingStep { + steps: ctx.tick_steps.1, + })); + } let w = &world.weather; let (raining, rain, thunder) = self.weather; if w.raining != raining { diff --git a/crates/horizon-client/tests/lan_bot.rs b/crates/horizon-client/tests/lan_bot.rs index 79d411386..ae70cccb8 100644 --- a/crates/horizon-client/tests/lan_bot.rs +++ b/crates/horizon-client/tests/lan_bot.rs @@ -197,6 +197,7 @@ impl Server { clock_rate: 1.0, tick_rate: 20.0, tick_frozen: false, + tick_steps: (0, 0), collision: None, }; let events = self.hub.tick(&mut ctx);