diff --git a/crates/horizon-client/examples/mob_ai_probe.rs b/crates/horizon-client/examples/mob_ai_probe.rs index 087fd3edf..0b3adec31 100644 --- a/crates/horizon-client/examples/mob_ai_probe.rs +++ b/crates/horizon-client/examples/mob_ai_probe.rs @@ -999,6 +999,7 @@ fn main() -> horizon_client::Result<()> { hero: false, uuid: PLAYER_UUID, allies: Default::default(), + remote: false, } }); let mut scratch = LegacyRandom::new(0); diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 076c2fbef..acac3b596 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -196,6 +196,7 @@ impl Game<'_> { hero: self.potions.effects.get("hero_of_the_village").is_some(), uuid: self.level.player_uuid.unwrap_or_default(), allies: std::sync::Arc::new(self.player_allies()), + remote: false, }) } /// Biome facts at a position for finalizeSpawn and variant rules. diff --git a/crates/horizon-client/src/game/lan_mobs.rs b/crates/horizon-client/src/game/lan_mobs.rs index 7cef7cda6..e12446b0c 100644 --- a/crates/horizon-client/src/game/lan_mobs.rs +++ b/crates/horizon-client/src/game/lan_mobs.rs @@ -107,6 +107,7 @@ impl Game<'_> { v.velocity = Vec3::new(moved[0], moved[1], moved[2]); v.uuid = horizon_client::lan::playerdata::uuid_ints(uuid); v.allies = Default::default(); + v.remote = true; views.push((conn, v)); } } @@ -271,6 +272,7 @@ mod tests { hero: false, uuid: [0; 4], allies: Default::default(), + remote: false, } } diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 05e1f81c3..1200da1ca 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -14,12 +14,13 @@ use horizon_client::{ entities::{Experience, ItemEntity}, hud::{HudBatch, rgba}, items::ItemStack, - mobs::{Mob, MobType}, + mobs::{Mob, MobType, Who}, motion::{MotionWorld, PlayerMotion}, net::{Event, Session}, save::SaveDir, sprint::Keys, streaming::{ChunkGenerator, Proto, Streaming}, + tab_list::SPECTATOR, world::World, }; use horizon_core::{geometry::Vec3, paletted::PalettedContainer, random::RandomSource}; @@ -2125,6 +2126,7 @@ impl Game<'_> { self.tick_particles(); self.tick_client_effects(); let ghast_collisions = self.happy_ghast_player_collisions(); + let pushed = self.remote_pushes(); let Some(player) = self.player.as_mut() else { return Ok(()); }; @@ -2177,6 +2179,7 @@ impl Game<'_> { player.yaw = self.yaw; let dead = self.screen == Screen::Dead; if !dead { + player.velocity = player.velocity.add_vector(pushed); MotionWorld { entities: &ghast_collisions, player, @@ -2229,6 +2232,70 @@ impl Game<'_> { self.send(S::ClientTickEnd(Empty)); Ok(()) } + /// The local player's share of every touching entity's + /// `LivingEntity.pushEntities`: on a client `EntitySelector.pushableBy` + /// lets entities push only the local player, so the server never sends + /// these pushes. `doPush(player)` is `player.push(entity)`. + fn remote_pushes(&self) -> Vec3 { + let (Some(p), Some(r)) = (self.player.as_ref(), self.remote.as_ref()) else { + return Vec3::ZERO; + }; + // LivingEntity.isPushable: alive, no spectator, off ladders. + let feet = [ + p.position.x.floor() as i32, + p.position.y.floor() as i32, + p.position.z.floor() as i32, + ]; + if self.screen == Screen::Dead + || p.spectator + || self.block_in_tag("minecraft:climbable", &self.block_name(feet)) + { + return Vec3::ZERO; + } + let bounds = p.bounds(); + let teams = self.mob_teams(); + let mut push = Vec3::ZERO; + for m in &self.mobs { + let spectator = r + .level_players + .get(&(m.id as i32)) + .and_then(|uuid| r.tab_players.get(uuid)) + .is_some_and(|t| t.game_mode == SPECTATOR); + // Bats never push; a spectator has no physics; passengers of the + // same vehicle never push each other. + if !m.alive() + || spectator + || m.species() == horizon_client::mobs::Species::Bat + || self + .riding + .is_some_and(|v| v == m.id || m.extra.mount == Some(v)) + || !m.bounds().intersects(bounds) + || !horizon_client::scoreboard::pushable_by( + teams.of(Who::Mob(m.id)), + teams.of(Who::Player), + true, + ) + { + continue; + } + // Entity.push(Entity), `this` the player. + let (mut xa, mut za) = (m.position.x - p.position.x, m.position.z - p.position.z); + let mut dd = xa.abs().max(za.abs()); + if dd < f64::from(0.01_f32) { + continue; + } + dd = dd.sqrt(); + xa /= dd; + za /= dd; + let pow = (1.0 / dd).min(1.0); + xa *= pow; + za *= pow; + xa *= f64::from(0.05_f32); + za *= f64::from(0.05_f32); + push = Vec3::new(push.x - xa, push.y, push.z - za); + } + push + } /// ClientLevel.tickEntities; a frozen level only ticks the players /// (`TickRateManager.isEntityFrozen`). fn tick_remote_entities(&mut self, running: bool) { diff --git a/crates/horizon-client/src/game/respawn_anchor.rs b/crates/horizon-client/src/game/respawn_anchor.rs index 5a31dfd5c..aa94d5b17 100644 --- a/crates/horizon-client/src/game/respawn_anchor.rs +++ b/crates/horizon-client/src/game/respawn_anchor.rs @@ -324,7 +324,7 @@ impl Game<'_> { ) .unwrap_or_else(|_| VoxelShape::empty()) } - fn block_in_tag(&self, tag: &str, block: &str) -> bool { + pub(super) fn block_in_tag(&self, tag: &str, block: &str) -> bool { self.res .items .block_tags diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index 2ab4c0557..53143d2df 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -1398,6 +1398,9 @@ pub struct PlayerView { /// The mobs on the player's team (`isAlliedTo`): combat targeting /// leaves the player alone. pub allies: std::sync::Arc>, + /// A LAN guest: its own client moves it, and touching mobs push it there + /// (`EntitySelector.pushableBy` on the client). + pub remote: bool, } pub struct MobWorld<'a> { pub world: &'a World, diff --git a/crates/horizon-client/src/mobs/ai.rs b/crates/horizon-client/src/mobs/ai.rs index 9ab29d0a5..f74a3c7d1 100644 --- a/crates/horizon-client/src/mobs/ai.rs +++ b/crates/horizon-client/src/mobs/ai.rs @@ -822,10 +822,14 @@ fn push_entities(m: &mut Mob, w: &MobWorld, events: &mut Vec) { za *= pow; xa *= f64::from(0.05_f32); za *= f64::from(0.05_f32); - events.push(MobEvent::Push { - target: who, - velocity: [-xa, 0.0, -za], - }); + // A guest's server-side push never reaches its client (ServerPlayer + // motion is synced only when hurt): its client pushes it itself. + if who != Who::Player || w.player.as_ref().is_none_or(|p| !p.remote) { + events.push(MobEvent::Push { + target: who, + velocity: [-xa, 0.0, -za], + }); + } if self_pushable { m.velocity = Vec3::new(m.velocity.x + xa, m.velocity.y, m.velocity.z + za); } diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index 667fdda2d..85b2bb12a 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -20,6 +20,8 @@ address. The script then checks, from both sides, what a guest's game used to ge - containers: the guest's ender chest holds the guest's items, not the host's, and a double chest opens as 9x6 with the right half first; - feedback: the guest's command feedback names the guest and shows the numbers. +- push: a cow standing in the guest pushes it just clear of the cow (the guest's own client + pushes it, as in vanilla), not ever faster across the ground. 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). @@ -48,7 +50,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', 'walls', 'chest', 'crafting', 'damage', 'mobs', 'hazards', 'pvp', 'containers', 'feedback', 'trials'] +CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'damage', 'mobs', 'hazards', 'pvp', 'containers', 'feedback', 'trials', 'push'] class Game: @@ -622,11 +624,32 @@ def check_trials(host, guest, out): return {'spawner': trial, 'vault': active, 'rewarded': uuid(rewarded), 'host': uuid(host_uuid)} +def check_push(host, guest, out): + guest('command', command='gamemode survival') + x, y, z = (int(v // 1) + 0.5 for v in pos_of(guest)) + guest('command', command=f'tp @s {x} {y - 0.5} {z}') + guest.wait(20) + x, y, z = pos_of(guest) + # A cow that doesn't walk, 0.3 east of the guest: the two boxes overlap until their centres are + # 0.75 apart, so the push stops a fraction of a block away. + guest('command', command=f'summon minecraft:cow {x + 0.3:.2f} {y:.2f} {z:.2f} {{NoAI:1b}}') + guest.wait(60) + nx, _, nz = pos_of(guest) + guest.shot(out, 'push') + guest('command', command='kill @e[type=minecraft:cow]') + moved = ((nx - x) ** 2 + (nz - z) ** 2) ** 0.5 + detail = {'from': [round(x, 3), round(z, 3)], 'to': [round(nx, 3), round(nz, 3)], 'moved': round(moved, 3)} + assert nx < x, f'the cow did not push the guest west: {detail}' + assert moved < 1.5, f'the cow pushed the guest too far: {detail}' + return detail + + 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, 'hazards': check_hazards, 'pvp': check_pvp, 'containers': check_containers, 'feedback': check_feedback, 'trials': check_trials, - 'walls': check_walls} + 'walls': check_walls, + 'push': check_push} def run(ctl, out, resources, checks):