diff --git a/crates/horizon-client/src/game/lan.rs b/crates/horizon-client/src/game/lan.rs index 711545ee2..e66797ce5 100644 --- a/crates/horizon-client/src/game/lan.rs +++ b/crates/horizon-client/src/game/lan.rs @@ -337,6 +337,13 @@ impl Game<'_> { "placed": g.placed, "loaded": g.loaded, "game_mode": g.game_mode, + "health": g.health, + // FoodData: food level, saturation and exhaustion. + "food": [ + f64::from(g.food), + f64::from(g.saturation), + g.motion.as_ref().map_or(0.0, |m| f64::from(m.health.food.exhaustion)), + ], "chunks": g.sent_chunks.len(), "tracked": g.tracker.len(), // The armour the host draws on this guest, head to feet. diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 8edeb871b..c241088d8 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -12,7 +12,7 @@ use horizon_client::{ entities::Experience, items::ItemStack, lan::{guest::SPECTATOR, menu::Rules}, - motion::{MotionWorld, PlayerMotion}, + motion::{MotionEvent, MotionWorld, PlayerMotion}, }; use horizon_core::geometry::Vec3; use horizon_protocol::{ @@ -770,23 +770,35 @@ impl Game<'_> { } /// The guest's body on the server each tick: ServerPlayer.tick's damage - /// clock and Entity.baseTick's fluid phase, then - /// ServerPlayer.doCheckFallDamage for each move the guest made (the fall - /// distance grows while they go down and hurts them where they land, as - /// the host's own landing does; water resets it, lava halves it), then - /// regeneration and the food tick (ServerPlayer.doTick). + /// clock and Entity.baseTick's fluid phase (burning), then for each move + /// the guest made ServerGamePacketListenerImpl.handleMovePlayer's jump + /// (`ServerPlayer.jumpFromGround`'s exhaustion), + /// ServerPlayer.doCheckFallDamage (the fall distance grows while they go + /// down and hurts them where they land, as the host's own landing does; + /// water resets it, lava halves it) and checkMovementStatistics (the + /// exhaustion of sprinting and swimming), then LivingEntity.baseTick's + /// hazards (drowning, lava, fire blocks, suffocation, the void), and + /// regeneration and the food tick (ServerPlayer.doTick). Their hurt + /// sounds are heard at the guest. fn guest_server_tick(&mut self, host: &mut LanHost, conn: u32) { let Some(g) = host.hub.guest_mut(conn) else { return; }; let moves = std::mem::take(&mut g.moves); + let from_ground = if moves.is_empty() { + g.on_ground + } else { + g.moves_from_ground + }; if g.spectator() { return; } - self.as_guest(host, conn, |game| { + let in_wall = !g.sleeping && self.in_wall(g.position, g.eye_height()); + let at = g.position; + let sounds = self.as_guest(host, conn, |game| { let (rules, health) = (game.world.health_rules, &game.motion.health); let Some(player) = game.player.as_mut() else { - return; + return Vec::new(); }; let mut w = MotionWorld { player, @@ -797,21 +809,58 @@ impl Game<'_> { }; w.player.health.advance_clock(); let _ = w.fluid_base_tick(); + w.player.ground.on_ground = from_ground; for (moved, on_ground) in moves { let movement = Vec3::new(moved[0], moved[1], moved[2]); + if w.player.ground.on_ground && !on_ground && moved[1] > 0.0 { + let sprinting = w.player.sprinting; + w.player + .health + .food + .add_exhaustion(if sprinting { 0.2 } else { 0.05 }); + } let landed = (|| -> horizon_client::Result<()> { w.set_on_ground_with_movement(on_ground, false, movement)?; let pos = w.legacy_on_position()?; let state = w.block_state(pos)?; - w.check_fall_damage(moved[1], on_ground, state, pos) + w.check_fall_damage(moved[1], on_ground, state, pos)?; + w.check_movement_statistics(movement) })(); if landed.is_err() { break; } } + let _ = w.tick_environment_damage(); + let _ = w.apply_fire_contact(); + let _ = w.server_hazards_tick(in_wall); let _ = w.player.peaceful_regeneration(rules); let _ = w.player.food_tick(health, rules); + std::mem::take(&mut w.player.events) + .into_iter() + .filter_map(|e| match e { + MotionEvent::HurtSound(sound) => Some(sound), + _ => None, + }) + .collect::>() }); + for sound in sounds.into_iter().flatten() { + self.player_hurt_sound(sound, at); + } + } + + /// `Player.isInWall`: a suffocating block in the 0.48-wide box at the + /// eyes. Suffocating is a full collision box that is also a full render + /// cube (`BlockBehaviour.isSuffocating`'s default; glass and the like + /// opt out), as the mobs' check has it. + fn in_wall(&self, at: [f64; 3], eye_height: f64) -> bool { + let (y, half) = ((at[1] + eye_height).floor() as i32, 0.24); + let range = |v: f64| (v - half).floor() as i32..=(v + half).floor() as i32; + range(at[0]).any(|x| { + range(at[2]).any(|z| { + let f = self.res.facts.get(self.world.block(x, y, z)); + f.full_collision && f.solid_render + }) + }) } /// The crack animation other players see. diff --git a/crates/horizon-client/src/lan/guest.rs b/crates/horizon-client/src/lan/guest.rs index e58544bc6..3967e3e83 100644 --- a/crates/horizon-client/src/lan/guest.rs +++ b/crates/horizon-client/src/lan/guest.rs @@ -102,6 +102,8 @@ pub struct Guest { /// The moves accepted since the game last looked (movement, on ground), /// for ServerPlayer.doCheckFallDamage. pub moves: Vec<([f64; 3], bool)>, + /// Whether the guest was on the ground before the first of `moves`. + pub moves_from_ground: bool, pub dead: bool, /// The join packets were sent (PlayerList.placeNewPlayer ran). pub placed: bool, @@ -178,6 +180,7 @@ impl Guest { joined_tick: 0, ticks: 0, moves: Vec::new(), + moves_from_ground: false, dead: false, placed: false, menu: None, diff --git a/crates/horizon-client/src/lan/hub.rs b/crates/horizon-client/src/lan/hub.rs index 8f4fc010e..fcae7c097 100644 --- a/crates/horizon-client/src/lan/hub.rs +++ b/crates/horizon-client/src/lan/hub.rs @@ -1461,6 +1461,9 @@ impl Hub { let moved = pos.map_or([0.0; 3], |_| { [0, 1, 2].map(|k| g.position[k] - g.previous_position[k]) }); + if g.moves.is_empty() { + g.moves_from_ground = g.on_ground; + } g.moves.push((moved, flags & 1 != 0)); if let Some((yaw, pitch)) = rot && yaw.is_finite() diff --git a/crates/horizon-client/src/motion/environment.rs b/crates/horizon-client/src/motion/environment.rs index 4ba5e7099..a9bb33678 100644 --- a/crates/horizon-client/src/motion/environment.rs +++ b/crates/horizon-client/src/motion/environment.rs @@ -91,7 +91,27 @@ impl MotionWorld<'_, '_, '_> { } Ok(()) } - pub(super) fn apply_fire_contact(&mut self) -> Result<()> { + /// What a server's `LivingEntity.baseTick` adds for a player it hosts: + /// the void (`Entity.checkBelowWorld`, 4 a tick, which creative players + /// take too) and suffocation (`isInWall`, which the caller decides from + /// the blocks at the eyes). + pub fn server_hazards_tick(&mut self, in_wall: bool) -> Result<()> { + if self.player.position.y < f64::from(crate::chunk::MIN_Y - 64) { + let id = self + .resources + .health + .damage + .id("minecraft:out_of_world") + .ok_or("missing environment damage type")?; + self.player + .hurt(&self.resources.health, self.scene.health_rules, id, 4.0)?; + } + if in_wall { + self.environment_hurt("minecraft:in_wall", 1.0)?; + } + Ok(()) + } + pub fn apply_fire_contact(&mut self) -> Result<()> { if self.player.spectator { self.player.fire_ticks = 0; return Ok(()); diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index ab46abef4..53f83566b 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -13,7 +13,9 @@ address. The script then checks, from both sides, what a guest's game used to ge closes on both when the guest closes it; - crafting: the guest crafts planks in the inventory's 2x2 grid and opens a crafting table; - damage: a guest in survival takes fall damage; -- mobs: with the host in creative, a zombie and a skeleton go for the guest and hurt them. +- mobs: with the host in creative, a zombie and a skeleton go for the guest and hurt them; +- hazards: a guest in survival gets hungry sprint-jumping and is hurt by lava, a block at the eyes, + drowning and the void (then respawns). Each check is reported on its own (a failing one doesn't stop the others) and the screenshots of both sides go to --out (host-*.png, guest-*.png). @@ -40,7 +42,7 @@ from agent_control_e2e import Cli, Failed, game_env # noqa: E402 ROOT = Path(__file__).resolve().parents[1] GUEST_NAME = 'Guest' -CHECKS = ['tab', 'self', 'armour', 'nametags', 'chest', 'crafting', 'damage', 'mobs'] +CHECKS = ['tab', 'self', 'armour', 'nametags', 'chest', 'crafting', 'damage', 'mobs', 'hazards'] class Game: @@ -300,9 +302,63 @@ def check_mobs(host, guest, out): return hurt +def check_hazards(host, guest, out): + guest('command', command='gamemode survival') + guest.wait(10) + # Sprint-jumping down a stone lane: ServerPlayer.jumpFromGround and checkMovementStatistics. + guest('command', command='fill ~ ~-1 ~-1 ~60 ~-1 ~1 minecraft:stone') + guest('command', command='fill ~ ~ ~-1 ~60 ~3 ~1 minecraft:air') + guest.wait(10) + x, y, z = pos_of(guest) + guest.raw('look-at', f'{x + 60:.2f}', f'{y + 1.6:.2f}', f'{z:.2f}') + food = host.raw('lan', 'status')['guests'][0]['food'] + for k in ('w', 'ctrl', 'space'): + guest.raw('key', k, 'down') + guest.wait(160) + for k in ('space', 'ctrl', 'w'): + guest.raw('key', k, 'up') + guest.wait(10) + fed = host.raw('lan', 'status')['guests'][0]['food'] + hunger = {'before': food, 'after': fed} + hurt = {} + hx, hy, hz = (int(v // 1) for v in pos_of(host)) + x, y, z = hx + 3, hy, hz + for name, fill, ticks in (('lava', 'minecraft:lava', 60), ('wall', 'minecraft:stone', 60), + ('drowning', 'minecraft:water', 500), ('void', None, 60)): + if remote(guest)['health'] <= 0: + guest.raw('respawn') + guest.wait(40) + # A stone box around the guest holds the lava and the water in. + guest('command', command=f'fill {x - 1} {y - 1} {z - 1} {x + 1} {y + 2} {z + 1} minecraft:stone') + guest('command', command=f'fill {x} {y} {z} {x} {y + 1} {z} minecraft:air') + guest('command', command=f'tp @s {x + 0.5} {y} {z + 0.5}') + guest.wait(100) + before = remote(guest)['health'] + if fill: + guest('command', command=f'fill {x} {y} {z} {x} {y + 1} {z} {fill}') + else: + guest('command', command=f'tp @s {x + 0.5} -140 {z + 0.5}') + hurt[name] = (before, first_hurt(guest, before, ticks)) + guest.shot(out, f'hazards-{name}') + # Water puts the lava's fire out. + guest('command', command=f'fill {x} {y} {z} {x} {y + 1} {z} minecraft:water') + guest.wait(5) + guest('command', command=f'fill {x - 1} {y - 1} {z - 1} {x + 1} {y + 2} {z + 1} minecraft:air') + guest('command', command=f'fill {x - 1} {y - 1} {z - 1} {x + 1} {y - 1} {z + 1} minecraft:stone') + guest.wait(20) + if remote(guest)['health'] <= 0: + guest.raw('respawn') + guest.wait(40) + # [food, saturation, exhaustion]: 8 s of sprint-jumping is about 2 of exhaustion. + assert fed[2] - food[2] > 1.0 or fed[1] < food[1], f'no hunger from moving: {hunger}' + for name, (before, after) in hurt.items(): + assert after < before, f'{name} never hurt the guest: health {before} -> {after} (all: {hurt})' + return {'hunger': hunger, **hurt} + + CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour, 'nametags': check_nametags, 'chest': check_chest, 'crafting': check_crafting, 'damage': check_damage, - 'mobs': check_mobs} + 'mobs': check_mobs, 'hazards': check_hazards} def run(ctl, out, resources, checks):