Merge pull request #2563 from levzzz5154/fix/thrown-projectile-flight
🤖 Agent: verified by Ludovic's AI agent (contrib gate 20): thrown items fly between server and client (lan_e2e thrown passes). It passed in this wave's union with main on the Mac mini: the selected parity harnesses (332/332, then 122/122 on the final union), clippy -D warnings and the tests, and lan_e2e with the host on its server thread and in-frame (18/18 each). Thanks @levzzz5154!
This commit is contained in:
@@ -77,6 +77,14 @@ fn remote_arrow_tick(p: &mut Projectile, w: &impl ProjectileWorld) {
|
||||
}
|
||||
p.velocity.y -= 0.05;
|
||||
}
|
||||
/// A server's client ticks a projectile itself: it flies on between the
|
||||
/// tracker's updates (10 to 20 ticks apart) with its own gravity and drag,
|
||||
/// and a block stops it there; only the server decides what it hits.
|
||||
pub(super) fn remote_projectile_tick(p: &mut Projectile, w: &impl ProjectileWorld) {
|
||||
if !matches!(p.tick(w).1, Hit::None) {
|
||||
p.velocity = Vec3::ZERO;
|
||||
}
|
||||
}
|
||||
|
||||
impl Game<'_> {
|
||||
/// `handleAddEntity` for these types; false when `kind` isn't one.
|
||||
@@ -347,11 +355,7 @@ impl Game<'_> {
|
||||
p.age += 1;
|
||||
p.position = p.position.add_vector(p.velocity);
|
||||
}
|
||||
_ => {
|
||||
if !matches!(p.tick(&world).1, Hit::None) {
|
||||
p.velocity = Vec3::ZERO;
|
||||
}
|
||||
}
|
||||
_ => remote_projectile_tick(p, &world),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -533,3 +537,68 @@ impl Game<'_> {
|
||||
out
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use horizon_client::mobs::Who;
|
||||
use horizon_core::geometry::Aabb;
|
||||
|
||||
/// A world with nothing in it: the client tick of a projectile only
|
||||
/// needs the blocks around it.
|
||||
struct Empty;
|
||||
impl ProjectileWorld for Empty {
|
||||
fn clip(&self, _: Vec3, _: Vec3) -> Option<(Vec3, [i32; 3], [i32; 3])> {
|
||||
None
|
||||
}
|
||||
fn in_water(&self, _: Vec3) -> bool {
|
||||
false
|
||||
}
|
||||
fn targets(&self) -> &[(Who, Aabb)] {
|
||||
&[]
|
||||
}
|
||||
}
|
||||
|
||||
/// A thrown item, as a server's client tracks it: the spawn position and
|
||||
/// the motion the server gave it.
|
||||
fn thrown(kind: ProjectileKind) -> Projectile {
|
||||
let mut p = Projectile::new(kind, Vec3::new(0.0, 100.0, 0.0), None);
|
||||
p.velocity = Vec3::new(1.5, 0.0, 0.0);
|
||||
p
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_thrown_item_flies_on_between_the_server_updates() {
|
||||
// A thrown egg, an ender pearl and a wind charge (#1762): the server
|
||||
// sends their position every 10 ticks, and the client ticks them
|
||||
// itself in between, as vanilla's no-op interpolation handler does.
|
||||
for kind in [
|
||||
ProjectileKind::Egg,
|
||||
ProjectileKind::EnderPearl,
|
||||
ProjectileKind::WindCharge,
|
||||
] {
|
||||
let mut p = thrown(kind);
|
||||
remote_projectile_tick(&mut p, &Empty);
|
||||
assert!(p.position.x > 1.0, "the projectile did not fly on: {p:?}");
|
||||
// A second tick moves it further: it keeps flying, not a hop.
|
||||
let first = p.position.x;
|
||||
remote_projectile_tick(&mut p, &Empty);
|
||||
assert!(p.position.x > first, "the projectile stopped: {p:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_thrown_item_falls_and_drags_like_a_throwable_projectile() {
|
||||
// ThrowableProjectile.tick: gravity 0.03, then the air drag 0.99.
|
||||
let mut p = thrown(ProjectileKind::Egg);
|
||||
remote_projectile_tick(&mut p, &Empty);
|
||||
assert!((p.velocity.x - 1.5 * 0.99).abs() < 1e-9, "{p:?}");
|
||||
assert!((p.velocity.y - (-0.03 * 0.99)).abs() < 1e-9, "{p:?}");
|
||||
// A wind charge keeps its speed: no gravity and no drag
|
||||
// (AbstractWindCharge: acceleration 0, inertia 1).
|
||||
let mut p = thrown(ProjectileKind::WindCharge);
|
||||
remote_projectile_tick(&mut p, &Empty);
|
||||
assert!((p.velocity.x - 1.5).abs() < 1e-9, "{p:?}");
|
||||
assert!((p.velocity.y - 0.0).abs() < 1e-9, "{p:?}");
|
||||
}
|
||||
}
|
||||
|
||||
+10
-2
@@ -4795,9 +4795,17 @@ def run(ctl, out, resources, checks):
|
||||
host, guest = Game('host', ctl, resources), Game('guest', ctl, resources)
|
||||
# Either way explicitly: the game's default is the server thread.
|
||||
host.d.env['HORIZON_SERVER_THREAD'] = '1' if SERVER_THREAD else '0'
|
||||
# A headless run (HORIZON_E2E_HEADLESS) has no focused window to draw in: the games render
|
||||
# offscreen and never pause on the focus loss, or the world would stand still.
|
||||
headless = bool(os.environ.get('HORIZON_E2E_HEADLESS'))
|
||||
launch = ['launch']
|
||||
if headless or sys.platform == 'darwin':
|
||||
launch.append('--offscreen')
|
||||
if headless:
|
||||
launch.append('--no-pause')
|
||||
results = {}
|
||||
try:
|
||||
host.pid = host('launch', wait=False, offscreen=sys.platform == 'darwin')['pid']
|
||||
host.pid = host.raw(*launch)['pid']
|
||||
# Guests' commands need the world's Allow Commands as well as the LAN's (#372): a creative
|
||||
# world has them; everyone plays in survival.
|
||||
host.raw('open', 'Lan', 5, 'creative')
|
||||
@@ -4811,7 +4819,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.pid = guest('launch', wait=False, offscreen=sys.platform == 'darwin')['pid']
|
||||
guest.pid = guest.raw(*launch)['pid']
|
||||
guest.raw('connect', f'127.0.0.1:{port}', GUEST_NAME)
|
||||
guest.raw('wait-for', 'loaded', '--timeout', 300)
|
||||
guest.wait(40)
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
#!/usr/bin/env python3
|
||||
"""A connected client advances ghast fireballs between tracker packets.
|
||||
"""A connected client advances ghast fireballs and thrown items 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.
|
||||
AbstractHurtingProjectile.tick/applyInertia run on clients as well as servers, and
|
||||
a thrown item ticks itself on the client too. 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 (#1762).
|
||||
"""
|
||||
import json
|
||||
import math
|
||||
@@ -40,6 +41,44 @@ def check_motion(host, guest, out):
|
||||
return {'positions': samples}
|
||||
|
||||
|
||||
def check_thrown(host, guest, out):
|
||||
"""A thrown egg, an ender pearl and a wind charge fly on between tracker packets.
|
||||
|
||||
The server sends a thrown item's position every 10 ticks; a client that only
|
||||
eased towards it hung in the air for half a second (#1762). Vanilla's client
|
||||
ticks the projectile itself, as Entity's no-op interpolation handler does.
|
||||
"""
|
||||
x, y, z = (math.floor(v) for v in lan.pos_of(guest))
|
||||
for game in (host, guest):
|
||||
game('command', command='gamemode survival')
|
||||
results = {}
|
||||
for item, kind in (('minecraft:egg', 'Egg'), ('minecraft:ender_pearl', 'EnderPearl'),
|
||||
('minecraft:wind_charge', 'WindCharge')):
|
||||
# A clear lane along +X at the guest's eye height, with room to fall.
|
||||
host('command', command=f'fill {x + 2} {y - 7} {z - 2} {x + 60} {y - 7} {z + 2} minecraft:stone')
|
||||
host('command', command=f'fill {x + 2} {y - 6} {z - 2} {x + 60} {y + 3} {z + 2} minecraft:air')
|
||||
guest('command', command=f'tp @s {x + 0.5:.2f} {y:.2f} {z + 0.5:.2f}')
|
||||
guest.wait(20)
|
||||
guest('command', command='clear @s')
|
||||
guest('command', command=f'item replace entity @s weapon.mainhand with {item}')
|
||||
guest.wait(10)
|
||||
# The server throws along the rotation it has: let it arrive first.
|
||||
guest.raw('look-at', f'{x + 40:.2f}', f'{y + 1.62:.2f}', f'{z + 0.5:.2f}')
|
||||
guest.wait(5)
|
||||
guest.raw('use-item')
|
||||
samples = []
|
||||
for _ in range(6):
|
||||
shots = [p for p in guest('status')['projectiles'] if p['kind'] == kind]
|
||||
samples.append(shots[0]['position'][0] if shots else None)
|
||||
guest.wait(2)
|
||||
seen = [p for p in samples if p is not None]
|
||||
moves = sum(b != a for a, b in zip(seen, seen[1:]))
|
||||
results[item] = {'positions': samples, 'moves': moves}
|
||||
assert len(seen) >= 4, f'{item} visible in only {len(seen)} samples: {samples}'
|
||||
assert moves >= len(seen) - 2, f'{item} stalled between tracker updates: {samples}'
|
||||
return results
|
||||
|
||||
|
||||
def main():
|
||||
subprocess.run(['cargo', 'build', '--locked', '--profile', 'fast', '-p', 'horizon-client',
|
||||
'--bin', 'horizon-client', '--bin', 'horizon-ctl'], cwd=lab.ROOT, check=True)
|
||||
@@ -47,6 +86,7 @@ def main():
|
||||
['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
|
||||
lan.CHECK_FNS['remote-thrown'] = check_thrown
|
||||
# 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')
|
||||
@@ -57,7 +97,7 @@ def main():
|
||||
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'])
|
||||
reports[mode] = lan.run(ctl, out, lab.RESOURCES.resolve(), ['remote-fireball', 'remote-thrown'])
|
||||
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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user