Merge pull request #1088 from ludovic111/fix/tick-server-thread
🤖 Agent: merged after integration C: the desktop ran 322/325 harnesses on main + #1088–#1093, and its 3 failures were the known dedicated_server flake plus the two display harnesses; world_reopen and save_roundtrip pass on the Mac mini (main + the six branches).
This commit is contained in:
@@ -118,6 +118,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
clock_rate: 1.0,
|
||||
tick_rate: 20.0,
|
||||
tick_frozen: false,
|
||||
tick_steps: (0, 0),
|
||||
collision: None,
|
||||
};
|
||||
for event in hub.tick(&mut ctx) {
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -6172,7 +6172,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,
|
||||
@@ -6238,7 +6240,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);
|
||||
@@ -9212,11 +9214,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)) => {
|
||||
@@ -9357,12 +9362,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;
|
||||
@@ -9419,7 +9424,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()
|
||||
@@ -9512,7 +9517,7 @@ impl Game<'_> {
|
||||
},
|
||||
stars: stars.filter(|_| self.settings.horizon.sky || title),
|
||||
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,
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user