diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index cf5bac65e..d9d082411 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -1413,6 +1413,22 @@ impl Game<'_> { m.on_ground = on_ground; return; } + if let Some(p) = self.creatures.projectiles.iter_mut().find(|p| { + p.id == id as u32 + && matches!( + p.kind, + horizon_client::projectiles::ProjectileKind::LargeFireball { .. } + ) + }) { + if path.is_some() { + p.position = Vec3::new(position[0], position[1], position[2]); + } + if let Some((yaw, pitch)) = rotation { + p.yaw = yaw; + p.pitch = pitch; + } + return; + } if let Some(p) = r.pending_items.get_mut(&id) { p.0 = position; } @@ -2941,6 +2957,13 @@ impl Game<'_> { f.velocity = v; } else if let Some(t) = self.tnts.iter_mut().find(|t| t.id == id as u32) { t.velocity = v; + } else if let Some(p) = self + .creatures + .projectiles + .iter_mut() + .find(|p| p.id == id as u32) + { + p.velocity = v; } else if let Some(p) = self .remote .as_mut() diff --git a/crates/horizon-client/src/game/remote_projectiles.rs b/crates/horizon-client/src/game/remote_projectiles.rs index 3fade3165..2ba4510ea 100644 --- a/crates/horizon-client/src/game/remote_projectiles.rs +++ b/crates/horizon-client/src/game/remote_projectiles.rs @@ -249,7 +249,25 @@ impl Game<'_> { for p in &mut self.creatures.projectiles { p.previous = p.position; p.age += 1; - p.position = step(p.id, p.position); + if matches!(p.kind, ProjectileKind::LargeFireball { .. }) { + // AbstractHurtingProjectile.tick advances on the client between + // the tracker updates (ten ticks apart for a ghast's fireball). + let at = p.position; + let water = self + .res + .facts() + .get(self.world.block( + at.x.floor() as i32, + at.y.floor() as i32, + at.z.floor() as i32, + )) + .fluid + == horizon_client::blocks::Fluid::Water; + p.apply_hurting_inertia(water); + p.position = p.position.add_vector(p.velocity); + } else { + p.position = step(p.id, p.position); + } } for t in &mut self.tridents { let id = t.id; diff --git a/crates/horizon-client/src/projectiles.rs b/crates/horizon-client/src/projectiles.rs index 8244da4d9..219d59b6b 100644 --- a/crates/horizon-client/src/projectiles.rs +++ b/crates/horizon-client/src/projectiles.rs @@ -572,6 +572,23 @@ impl Projectile { } (self.position.y > -128.0, Hit::None) } + /// AbstractHurtingProjectile.applyInertia, on both server and client. + pub fn apply_hurting_inertia(&mut self, water: bool) { + // WitherSkull.getInertia: 0.73 for dangerous skulls. + let inertia = match self.kind { + ProjectileKind::WitherSkull { dangerous: true } => 0.73, + _ if water => 0.8, + _ => 0.95, + }; + let v = self.velocity; + let len = (v.x * v.x + v.y * v.y + v.z * v.z).sqrt().max(1.0e-9); + self.velocity = Vec3::new( + (v.x + v.x / len * self.acceleration) * inertia, + (v.y + v.y / len * self.acceleration) * inertia, + (v.z + v.z / len * self.acceleration) * inertia, + ); + } + /// One tick: returns whether to keep the projectile and what it hit. pub fn tick(&mut self, w: &impl ProjectileWorld) -> (bool, Hit) { self.previous = self.position; @@ -584,19 +601,7 @@ impl Projectile { ProjectileKind::SmallFireball | ProjectileKind::LargeFireball { .. } | ProjectileKind::WitherSkull { .. } => { - // WitherSkull.getInertia: 0.73 for dangerous skulls. - let inertia = match self.kind { - ProjectileKind::WitherSkull { dangerous: true } => 0.73, - _ if water => 0.8, - _ => 0.95, - }; - let v = self.velocity; - let len = (v.x * v.x + v.y * v.y + v.z * v.z).sqrt().max(1.0e-9); - self.velocity = Vec3::new( - (v.x + v.x / len * self.acceleration) * inertia, - (v.y + v.y / len * self.acceleration) * inertia, - (v.z + v.z / len * self.acceleration) * inertia, - ); + self.apply_hurting_inertia(water); } // Arrows tick with `arrows::tick`. ProjectileKind::Arrow { .. } => {} @@ -819,6 +824,21 @@ mod tests { // (0.1 + 0.1) * 0.95 assert!((p.velocity.x - 0.19).abs() < 1e-9); } + + #[test] + fn fireball_inertia_preserves_custom_acceleration() { + let mut p = Projectile::fireball( + ProjectileKind::SmallFireball, + Vec3::new(0.0, 100.0, 0.0), + None, + [1.0, 0.0, 0.0], + ); + // A redirected projectile can carry a different acceleration power. + p.acceleration = 0.2; + p.tick(&Empty(vec![])); + assert!((p.velocity.x - 0.285).abs() < 1e-9); + assert!((p.position.x - 0.285).abs() < 1e-9); + } } #[cfg(test)] diff --git a/tools/test_remote_fireball_parity.py b/tools/test_remote_fireball_parity.py new file mode 100644 index 000000000..7e60ff30a --- /dev/null +++ b/tools/test_remote_fireball_parity.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python3 +"""A connected client advances ghast fireballs between tracker packets. + +AbstractHurtingProjectile.tick/applyInertia run on clients as well as servers. +Like lan_e2e's falling-entity check, sample motion more often than the tracker +interval; an interpolation-only client stalls for most of that interval. +""" +import json +import math +import subprocess + +import lab +import lan_e2e as lan + + +def check_motion(host, guest, out): + x, y, z = (math.floor(v) for v in lan.pos_of(guest)) + for game in (host, guest): + game('command', command='gamemode creative') + tag = 'remote_fireball_parity' + host('command', command=f'fill {x + 2} {y + 18} {z - 1} {x + 48} {y + 23} {z + 1} minecraft:air') + host('command', command=f'summon minecraft:fireball {x + 3.5} {y + 20} {z + 0.5} ' + f'{{Tags:["{tag}"],Motion:[0.1d,0.0d,0.0d]}}') + samples = [] + try: + for _ in range(8): + balls = [p for p in guest('status')['projectiles'] if p['kind'] == 'LargeFireball'] + samples.append(balls[0]['position'][0] if balls else None) + guest.wait(2) + finally: + host('command', command=f'kill @e[tag={tag}]') + seen = [p for p in samples if p is not None] + assert len(seen) >= 6, f'fireball visible in only {len(seen)} samples: {samples}' + moves = sum(b != a for a, b in zip(seen, seen[1:])) + assert moves >= len(seen) - 2, f'fireball stalled between tracker updates: {samples}' + return {'positions': samples} + + +def main(): + subprocess.run(['cargo', 'build', '--locked', '--profile', 'fast', '-p', 'horizon-client', + '--bin', 'horizon-client', '--bin', 'horizon-ctl'], cwd=lab.ROOT, check=True) + metadata = json.loads(subprocess.check_output( + ['cargo', 'metadata', '--format-version', '1', '--no-deps'], cwd=lab.ROOT, text=True)) + ctl = lab.ROOT / metadata['target_directory'] / 'fast' / 'horizon-ctl' + lan.CHECK_FNS['remote-fireball'] = check_motion + # Use the runner's current display (xvfb on Linux), with fresh isolated homes. + lan.game_env = lambda _screen, home: lab.game_env( + HORIZON_HOME=home, HORIZON_TELEMETRY_URL='off') + reports = {} + for threaded in (True, False): + lan.SERVER_THREAD = threaded + mode = 'server-thread' if threaded else 'in-frame' + out = lab.LOCAL / 'remote-fireball-parity' / mode + out.mkdir(parents=True, exist_ok=True) + # lan.Game launches each client with lab.game_env and private scratch homes. + reports[mode] = lan.run(ctl, out, lab.RESOURCES.resolve(), ['remote-fireball']) + lab.write_json(lab.LOCAL / 'remote-fireball-parity' / 'report.json', reports) + assert all(r['pass'] for mode in reports.values() for r in mode.values()), reports + + +if __name__ == '__main__': + main()