From 0ab07f720967092847a487a70052dc8a53fed497 Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Mon, 19 Oct 2026 01:10:17 +0100 Subject: [PATCH] Fix spectator scroll wheel flight speed --- .../examples/game_loop_probe.rs | 24 ++++++--- crates/horizon-client/src/game.rs | 22 +++++--- crates/horizon-client/src/game/remote.rs | 7 ++- crates/horizon-client/src/motion.rs | 50 +++++++++++++++++-- crates/horizon-client/src/mouse.rs | 6 +++ .../parity/VanillaGameLoopOracle.java | 11 +++- tools/test_game_loop_parity.py | 13 +++-- 7 files changed, 111 insertions(+), 22 deletions(-) diff --git a/crates/horizon-client/examples/game_loop_probe.rs b/crates/horizon-client/examples/game_loop_probe.rs index 56da454d9..66a428162 100644 --- a/crates/horizon-client/examples/game_loop_probe.rs +++ b/crates/horizon-client/examples/game_loop_probe.rs @@ -233,22 +233,32 @@ fn mouse(c: &Value, out: &mut impl Write) -> Result<()> { fn scroll(c: &Value, out: &mut impl Write) -> Result<()> { let mut handler = MouseLook::default(); let mut slot = c["slot"].as_i64().unwrap() as i32; + let spectator = c["spectator"].as_bool().unwrap_or(false); + let mut speed = 0.05; // OptionInstance.set quantises the wheel sensitivity. let mut file = OptionsFile::default(); file.set("mouseWheelSensitivity", V::Double(db(&c["sens"]))); let sensitivity = file.double("mouseWheelSensitivity"); writeln!(out, "scroll")?; for op in c["ops"].as_array().unwrap() { - if let Some((wheel, _)) = + if let Some((wheel, (_, y))) = handler.on_scroll(db(&op[0]), db(&op[1]), b(&c["discrete"]), sensitivity) { - slot = horizon_client::mouse::ScrollWheelHandler::next_selection( - f64::from(wheel), - slot, - 9, - ); + if spectator { + speed = horizon_client::mouse::spectator_flying_speed(speed, y); + } else { + slot = horizon_client::mouse::ScrollWheelHandler::next_selection( + f64::from(wheel), + slot, + 9, + ); + } + } + if spectator { + writeln!(out, "{slot} {}", fb(speed))?; + } else { + writeln!(out, "{slot}")?; } - writeln!(out, "{slot}")?; } Ok(()) } diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 2cbdabaa4..aa49c539f 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -9925,15 +9925,25 @@ impl ApplicationHandler for Game<'_> { && self.sleeping.is_none() && self.player.is_some() && !self.extras_scroll(d) - && let Some((wheel, _)) = + && let Some((wheel, (_, vertical_steps))) = self.mouse .on_scroll(raw_x, raw_y, discrete, wheel_sensitivity) { - self.selected = horizon_client::mouse::ScrollWheelHandler::next_selection( - f64::from(wheel), - self.selected as i32, - 9, - ) as usize; + if self.spectator() { + if let Some(player) = &mut self.player { + player.flying_speed = horizon_client::mouse::spectator_flying_speed( + player.flying_speed, + vertical_steps, + ); + } + } else { + self.selected = + horizon_client::mouse::ScrollWheelHandler::next_selection( + f64::from(wheel), + self.selected as i32, + 9, + ) as usize; + } } if self.screen == Screen::Chat && d != 0.0 { self.chat_scroll(d); diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 36c9fe4f3..f454771f3 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -1805,7 +1805,12 @@ impl Game<'_> { } _ => {} }, - C::PlayerAbilities(p) => self.creative = p.flags & 8 != 0, + C::PlayerAbilities(p) => { + self.creative = p.flags & 8 != 0; + if let Some(player) = &mut self.player { + player.flying_speed = p.flying_speed; + } + } C::SetHeldSlot(p) => { self.selected = p.slot.clamp(0, 8) as usize; if let Some(r) = self.remote.as_mut() { diff --git a/crates/horizon-client/src/motion.rs b/crates/horizon-client/src/motion.rs index 293aa61a2..6fca66c1d 100644 --- a/crates/horizon-client/src/motion.rs +++ b/crates/horizon-client/src/motion.rs @@ -224,6 +224,8 @@ pub struct PlayerMotion { /// Spectator mode: no physics, always flying (`Player.tick`). pub spectator: bool, pub flying: bool, + /// Client-side `Abilities.flyingSpeed`, adjusted by the spectator scroll wheel. + pub flying_speed: f32, /// The jump key last tick (LocalPlayer.aiStep's wasJumping). pub was_jumping: bool, /// `LocalPlayer.autoJumpEnabled` (the Auto-Jump option) and @@ -387,6 +389,7 @@ impl PlayerMotion { invulnerable: false, spectator: false, flying: false, + flying_speed: 0.05, was_jumping: false, auto_jump_enabled: false, auto_jump_time: 0, @@ -789,7 +792,7 @@ impl MotionWorld<'_, '_, '_> { // Vertical flight: jump up, shift down, three times the flying speed. let ya = i32::from(jumping) - i32::from(self.player.shift_key_down); if ya != 0 { - self.player.velocity.y += f64::from(ya as f32 * 0.05 * 3.0); + self.player.velocity.y += f64::from(ya as f32 * self.player.flying_speed * 3.0); } } self.dry_tick(TickInput::PreparedLocal { @@ -1863,8 +1866,8 @@ impl LivingAirWorld for MotionWorld<'_, '_, '_> { } fn flying_speed(&mut self) -> Result { if self.player.flying { - // Abilities.flyingSpeed 0.05, doubled when sprinting. - return Ok(if self.player.sprinting { 0.1 } else { 0.05 }); + // Player.getFlyingSpeed: abilities speed, doubled when sprinting. + return Ok(self.player.flying_speed * if self.player.sprinting { 2.0 } else { 1.0 }); } Ok(if self.player.sprinting { 0.025999999 @@ -2123,6 +2126,47 @@ mod tests { assert!(player.position.y < 84.0); } + #[test] + fn spectator_scroll_speed_controls_horizontal_and_vertical_flight() { + let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../.local/horizon-client/26.3"); + if !root.join("movement-properties.nbt").exists() { + return; + } + let assets = Assets::load(&root).unwrap(); + let render = assets.reload(&root).unwrap(); + let binding = crate::collisions::load(&root, assets.registry).unwrap(); + let resources = MotionResources::load(&root, &assets).unwrap(); + let run = |steps| { + let mut scene = World::test_scene(&assets, &render, 2).unwrap(); + let start = Vec3::new(20.5, 12.0, 20.5); + let mut player = PlayerMotion::new(&resources, start).unwrap(); + player.spectator = true; + player.flying_speed = crate::mouse::spectator_flying_speed(player.flying_speed, steps); + MotionWorld { + entities: &[], + player: &mut player, + scene: &mut scene, + assets: &assets, + collision_binding: &binding, + resources: &resources, + } + .controlled_simulation_tick(crate::sprint::Keys { + forward: true, + jump: true, + ..Default::default() + }) + .unwrap(); + player.position.subtract(start) + }; + let stopped = run(-100); + assert_eq!(stopped, Vec3::ZERO); + let normal = run(0); + let fast = run(10); + assert!(normal.y > 0.0 && normal.z > 0.0); + assert!((fast.y - normal.y * 2.0).abs() < 1e-6); + assert!((fast.z - normal.z * 2.0).abs() < 1e-6); + } + #[test] fn resident_spectator_flight_ignores_solid_and_climbable_blocks() { let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../.local/horizon-client/26.3"); diff --git a/crates/horizon-client/src/mouse.rs b/crates/horizon-client/src/mouse.rs index d2794d790..1cee2be87 100644 --- a/crates/horizon-client/src/mouse.rs +++ b/crates/horizon-client/src/mouse.rs @@ -80,6 +80,12 @@ impl ScrollWheelHandler { } } +/// `MouseHandler.onScroll` with the spectator menu closed. Only vertical whole +/// wheel steps adjust `Abilities.flyingSpeed`; horizontal scrolling does not. +pub fn spectator_flying_speed(speed: f32, vertical_steps: i32) -> f32 { + (speed + vertical_steps as f32 * 0.005).clamp(0.0, 0.2) +} + /// Entity rotation as `Entity.turn` keeps it. #[derive(Clone, Copy, Debug, PartialEq)] pub struct Rotation { diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaGameLoopOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaGameLoopOracle.java index 0be89888a..294ceacac 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaGameLoopOracle.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaGameLoopOracle.java @@ -118,13 +118,14 @@ public final class VanillaGameLoopOracle { static final class StubPlayer extends LocalPlayer { boolean scoping; + boolean spectator; StubPlayer() { super(null, null, null, null, null, Input.EMPTY, false, null, null); } @Override public boolean isScoping() { return scoping; } - @Override public boolean isSpectator() { return false; } + @Override public boolean isSpectator() { return spectator; } } static Minecraft shell; @@ -327,6 +328,12 @@ public final class VanillaGameLoopOracle { StubPlayer player = make(StubPlayer.class); Inventory inventory = make(Inventory.class); inventory.setSelectedSlot(c.get("slot").getAsInt()); + player.spectator = c.has("spectator") && c.get("spectator").getAsBoolean(); + field(net.minecraft.world.entity.player.Player.class, "abilities").set(player, new net.minecraft.world.entity.player.Abilities()); + var hud = make(net.minecraft.client.gui.Hud.class); + field(net.minecraft.client.gui.Hud.class, "spectatorGui").set(hud, + make(net.minecraft.client.gui.components.spectator.SpectatorGui.class)); + field(Gui.class, "hud").set(mc.gui, hud); field(net.minecraft.world.entity.player.Player.class, "inventory").set(player, inventory); mc.player = player; options.discreteMouseScroll().set(c.get("discrete").getAsBoolean()); @@ -336,7 +343,7 @@ public final class VanillaGameLoopOracle { for (var e : c.getAsJsonArray("ops")) { var op = e.getAsJsonArray(); h.onScroll(1L, d(op.get(0)), d(op.get(1))); - out.println(inventory.getSelectedSlot()); + out.println(inventory.getSelectedSlot() + (player.spectator ? " " + fb(player.getAbilities().getFlyingSpeed()) : "")); } } diff --git a/tools/test_game_loop_parity.py b/tools/test_game_loop_parity.py index d724dc6ac..101b3f238 100644 --- a/tools/test_game_loop_parity.py +++ b/tools/test_game_loop_parity.py @@ -120,8 +120,15 @@ def scroll_cases(rng): x = rng.choice([0.0, 0.0, 0.0, 1.0, -1.0, 0.25, -0.25, 0.4, 2.0, 5.5, -7.0, rng.uniform(-3, 3)]) y = rng.choice([0.0, 1.0, -1.0, 1.0, -1.0, 0.1, -0.3, 0.5, -0.5, 2.0, -3.0, 120.0, rng.uniform(-3, 3)]) ops.append([dbits(x), dbits(y)]) - yield dict(t='scroll', discrete=rng.random() < 0.4, sens=dbits(rng.choice([1.0, 0.01, 10.0, 0.5, 2.0, 3.1622776601683795])), - slot=rng.randrange(9), ops=ops) + case = dict(t='scroll', discrete=rng.random() < 0.4, sens=dbits(rng.choice([1.0, 0.01, 10.0, 0.5, 2.0, 3.1622776601683795])), + slot=rng.randrange(9), ops=ops) + yield case + yield dict(case, spectator=True) + # Bounds, recovery from each bound, fractional accumulation, and horizontal-only input. + yield dict(t='scroll', discrete=False, sens=dbits(1.0), slot=4, spectator=True, + ops=[[dbits(x), dbits(y)] for x, y in + [(0, 100), (0, -1), (0, -100), (0, 1), (0, .4), (0, .7), + (0, -.4), (0, -.7), (5, 0), (-5, 0)]]) def key_cases(rng): @@ -152,7 +159,7 @@ def main(): 'Window.calculateScale/setGuiScale and MouseHandler scaled positions, FramerateLimitTracker throttle reasons and ' 'limits (not the focus-lost exclusive-fullscreen branch, which reads SDL), MouseHandler.onMove/turnPlayer with ' 'sensitivity, cinematic smoothing, scoping and inversion, Entity.turn clamps, onScroll with ScrollWheelHandler ' - 'accumulation, discrete scrolling and hotbar selection, and ToggleKeyMapping/KeyMapping down, click and ' + 'accumulation, discrete scrolling, hotbar selection and spectator flight speed, and ToggleKeyMapping/KeyMapping down, click and ' 'screen-release state. The frame clock source (Blaze3D.getTime, System.nanoTime) is injected, so sleeping in ' 'FramerateLimiter and vsync present-mode selection are covered by Rust unit tests only.') harness.differential(