diff --git a/crates/horizon-client/src/game/chat.rs b/crates/horizon-client/src/game/chat.rs index cb8a3a0b5..b78b97cb5 100644 --- a/crates/horizon-client/src/game/chat.rs +++ b/crates/horizon-client/src/game/chat.rs @@ -689,6 +689,16 @@ impl Game<'_> { c.partial -= whole; self.world.day_time += whole as i64; } + /// `ServerClockInstance.packNetworkState`: the rate players' clients + /// advance the overworld clock at, 0 while it is paused (`/time pause`) + /// or `advance_time` is off. + pub(super) fn network_clock_rate(&self) -> f32 { + if self.ui.rules.bool("advance_time") && !self.ui.clock.paused { + self.ui.clock.rate + } else { + 0.0 + } + } /// The engine-side rule values (block behaviour and health). pub(super) fn level_rules(&self) -> horizon_world::level::GameRules { let r = &self.ui.rules; diff --git a/crates/horizon-client/src/game/lan.rs b/crates/horizon-client/src/game/lan.rs index d35bf787a..d39422e35 100644 --- a/crates/horizon-client/src/game/lan.rs +++ b/crates/horizon-client/src/game/lan.rs @@ -814,6 +814,7 @@ impl Game<'_> { }; let spawn = self.spawn.map_or([0.5, 80.0, 0.5], |s| [s.x, s.y, s.z]); let events = { + let clock_rate = self.network_clock_rate(); let mut ctx = TickCtx { world: &mut self.world, host: state.as_ref(), @@ -822,11 +823,7 @@ impl Game<'_> { spawn, difficulty: i32::from(self.ui.difficulty), difficulty_locked: self.level.difficulty_locked, - clock_rate: if self.ui.rules.bool("advance_time") { - self.ui.clock.rate - } else { - 0.0 - }, + clock_rate, tick_rate: self.clock.rate(), tick_frozen: self.clock.frozen(), collision: Some(self.collision), @@ -991,6 +988,7 @@ impl Game<'_> { } else { entities }; + let clock_rate = self.network_clock_rate(); let mut ctx = TickCtx { world: &mut self.world, host: state, @@ -999,7 +997,7 @@ impl Game<'_> { spawn, difficulty: i32::from(self.ui.difficulty), difficulty_locked: self.level.difficulty_locked, - clock_rate: self.ui.clock.rate, + clock_rate, tick_rate: self.clock.rate(), tick_frozen: self.clock.frozen(), collision: Some(self.collision), diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index ced435d8d..42e96becc 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -1072,6 +1072,7 @@ impl Game<'_> { if let Some(play) = host.play.get_mut(&conn) { play.potions = Default::default(); } + let clock_rate = self.network_clock_rate(); let ctx = TickCtx { world: &mut self.world, host: None, @@ -1080,11 +1081,7 @@ impl Game<'_> { spawn, difficulty: i32::from(self.ui.difficulty), difficulty_locked: self.level.difficulty_locked, - clock_rate: if self.ui.rules.bool("advance_time") { - self.ui.clock.rate - } else { - 0.0 - }, + clock_rate, tick_rate: self.clock.rate(), tick_frozen: self.clock.frozen(), collision: Some(self.collision), diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 680360c93..710e507d3 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -149,7 +149,12 @@ pub(super) struct Remote { synced: Vec>, decode_errors: u32, clock: Option, + /// `ClientClockManager.ClientClockInstance`'s rate and partial tick for + /// the level's clock (its total is `world.day_time`), and + /// `lastTickGameTime`. clock_rate: f32, + clock_partial: f32, + clock_game_time: i64, /// `ClientChunkCache.Storage`'s view centre (ClientboundSetChunkCacheCenterPacket). chunk_center: (i32, i32), } @@ -331,6 +336,8 @@ impl Game<'_> { decode_errors: 0, clock: None, clock_rate: 1.0, + clock_partial: 0.0, + clock_game_time: 0, chunk_center: (0, 0), }); // The level's TickRateManager follows the server's ticking packets. @@ -1648,8 +1655,12 @@ impl Game<'_> { total: p.total, } } + // ClientPacketListener.handleSetTime: the game time, then + // ClientClockManager.handleUpdates: the clock catches up with the + // game time and takes the server's state. C::SetTime(p) => { self.world.game_time = p.game_time; + self.remote_clock_tick(); let clock = { let r = self.remote.as_ref(); let names = @@ -1670,6 +1681,7 @@ impl Game<'_> { if let Some(r) = self.remote.as_mut() { r.clock = Some(c.clock); r.clock_rate = c.rate; + r.clock_partial = c.partial_tick; } } } @@ -2379,15 +2391,12 @@ impl Game<'_> { horizon_protocol::packets::configuration::ClientInformation::new(view), )); } - // Minecraft.tick: TickRateManager.tick; ClientLevel.tick advances + // Minecraft.tick: TickRateManager.tick; ClientLevel.tickTime advances // the time only while the level runs normally. let running = self.clock.world_runs(); if running { self.world.game_time += 1; - let rate = self.remote.as_ref().map_or(1.0, |r| r.clock_rate); - if rate > 0.0 { - self.world.day_time += rate.round() as i64; - } + self.remote_clock_tick(); } // Weather levels drift toward their targets between server updates. let w = &mut self.world.weather; @@ -3701,6 +3710,20 @@ impl Game<'_> { .filter_map(|(id, uuid)| Some((*id as u32, r.players.get(uuid)?.clone()))) .collect() } + /// `ClientClockManager.tick`: the clock moves on by the game time that + /// passed since its last tick, at its rate. A game time the server sets + /// forward (its sync every second) moves the clock with it. + fn remote_clock_tick(&mut self) { + let Some(r) = self.remote.as_mut() else { + return; + }; + let delta = self.world.game_time - r.clock_game_time; + r.clock_game_time = self.world.game_time; + let partial = f64::from(r.clock_partial) + delta as f64 * f64::from(r.clock_rate); + let full = partial.floor(); + r.clock_partial = (partial - full) as f32; + self.world.day_time += full as i64; + } pub(super) fn remote_status(&self) -> Option { let r = self.remote.as_ref()?; Some(serde_json::json!({ diff --git a/crates/horizon-client/src/game/server_travel.rs b/crates/horizon-client/src/game/server_travel.rs index 6c0ec48e7..56fdbaca5 100644 --- a/crates/horizon-client/src/game/server_travel.rs +++ b/crates/horizon-client/src/game/server_travel.rs @@ -304,6 +304,7 @@ impl Game<'_> { /// The hub's view of the server for packets sent outside its own tick. pub(super) fn server_ctx(&mut self) -> TickCtx<'_> { let spawn = self.spawn.map_or([0.5, 80.0, 0.5], |s| [s.x, s.y, s.z]); + let clock_rate = self.network_clock_rate(); TickCtx { world: &mut self.world, host: None, @@ -312,11 +313,7 @@ impl Game<'_> { spawn, difficulty: i32::from(self.ui.difficulty), difficulty_locked: self.level.difficulty_locked, - clock_rate: if self.ui.rules.bool("advance_time") { - self.ui.clock.rate - } else { - 0.0 - }, + clock_rate, tick_rate: self.clock.rate(), tick_frozen: self.clock.frozen(), collision: Some(self.collision), diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index 4d13b1c22..b7d844de9 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -49,6 +49,8 @@ address. The script then checks, from both sides, what a guest's game used to ge guest as the host has them (kind, place, rotation, what they hold or wear, the variant). - projectiles: primed TNT (one falling), an end crystal and an arrow that lands reach the guest where the host has them; a falling block lands on both sides. +- time: the guest's clock is the host's: it stops with `/time pause`, and catches up with the + server's game time after the guest's game stalls for a few seconds (#848). - light: the guest's light matches the host's: around a torch placed near it, under a roof and once a block of the roof is removed under the sky, and in the last chunk it tracks next to glowstone in a chunk it doesn't (only the server's light updates bring that one). @@ -69,6 +71,7 @@ import json import os import re import shutil +import signal import subprocess import sys import tempfile @@ -84,7 +87,7 @@ ROOT = Path(__file__).resolve().parents[1] GUEST_NAME = 'Guest' # The host plays single player on its server thread (--server-thread). SERVER_THREAD = False -CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'containers', 'feedback', 'titles', 'commands', 'recipes', 'trials', 'clicks', 'sleep', 'push', 'eat', 'xp', 'objects', 'projectiles', 'light', 'effects', 'portals', 'distance'] +CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'containers', 'feedback', 'titles', 'commands', 'recipes', 'trials', 'clicks', 'sleep', 'push', 'eat', 'xp', 'objects', 'projectiles', 'light', 'time', 'effects', 'portals', 'distance'] class Game: @@ -1122,6 +1125,29 @@ def check_xp(host, guest, out): return {'seen': seen, 'guest': xp, 'host': host_xp} +def check_time(host, guest, out): + def times(): + return host('state')['time']['day_time'], guest('state')['time']['day_time'] + host('command', command='time set 1000') + host('command', command='time pause') + host.wait(60) + paused = times() + host('command', command='time resume') + guest.wait(20) + # A stalled game runs at most 10 missed ticks when it wakes (DeltaTracker); the server's + # game time, sent every second, moves the guest's clock on (ClientClockManager.tick). + os.kill(guest.pid, signal.SIGSTOP) + time.sleep(4) + os.kill(guest.pid, signal.SIGCONT) + guest.wait(60) + stalled = times() + detail = {'paused': paused, 'after_stall': stalled} + assert paused[0] == paused[1] and 1000 <= paused[0] < 1010, f'the clock while paused: {detail}' + # The two reads are a tick or two apart. + assert abs(stalled[0] - stalled[1]) <= 3, f'the guest\'s clock after its game stalled: {detail}' + return detail + + def objects_near(game, centre, key='objects'): """`status`'s objects within 8 blocks of centre, without their ids (each side numbers its own).""" out = [] @@ -1278,7 +1304,7 @@ CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour, 'name 'effects': check_effects, 'portals': check_portals, 'distance': check_distance, 'clicks': check_clicks, 'sleep': check_sleep, 'push': check_push, 'eat': check_eat, 'xp': check_xp, 'objects': check_objects, 'projectiles': check_projectiles, - 'light': check_light} + 'light': check_light, 'time': check_time} def run(ctl, out, resources, checks): @@ -1301,7 +1327,7 @@ def run(ctl, out, resources, checks): host('command', command='gamerule spawn_mobs false') port = host.raw('lan', 'open', 0, 'commands')['port'] host.player = host.raw('lan', 'status').get('host', 'Player') # the host's profile name - guest('launch', wait=False, offscreen=sys.platform == 'darwin') + guest.pid = guest('launch', wait=False, offscreen=sys.platform == 'darwin')['pid'] guest.raw('connect', f'127.0.0.1:{port}', GUEST_NAME) guest.raw('wait-for', 'loaded', '--timeout', 300) guest.wait(40)