diff --git a/crates/horizon-client/src/collisions.rs b/crates/horizon-client/src/collisions.rs index 9ec271b27..d35a1adfa 100644 --- a/crates/horizon-client/src/collisions.rs +++ b/crates/horizon-client/src/collisions.rs @@ -161,8 +161,21 @@ impl> EntityMovementWorld for EntityCo border.collision_shape().map(Some).map_err(io::Error::other) } } -impl EntityCollisions<'_, SceneCollisions<'_, '_>> { - /// `SceneCollisions::resolve` that also stops at the entity boxes. +impl<'a, W> EntityCollisions<'a, W> +where + W: BlockCollisionWorld + EntityMovementWorld, +{ + /// `EntityCollisions` for a body the world border also stops: the border + /// and the body's own x and z (`WorldBorder.isInsideCloseToBorder`). + pub fn with_border(blocks: W, border: &'a BorderGeometry, at: [f64; 2]) -> Self { + Self { + blocks, + entities: &[], + border: Some((border, at)), + } + } + /// `SceneCollisions::resolve` that also stops at the entity boxes and, + /// with a border, at the world border's walls. pub fn resolve( &mut self, bounds: Aabb, @@ -170,12 +183,11 @@ impl EntityCollisions<'_, SceneCollisions<'_, '_>> { max_step: f32, on_ground: bool, ) -> Result { - let context = self.blocks.context(bounds); Ok(resolve_world_movement( self, MovementRequest { entity: EntityIdentity(0), - context, + context: CollisionContext::position(bounds.min_position().y), bounds, movement, max_step, @@ -409,6 +421,42 @@ impl PowderSnowWorld for SceneCollisions<'_, '_> { } } +/// A collision world without blocks: no chunk is resident, so only the +/// world border stops a body. +pub struct EmptyCollisions; + +impl BlockCollisionWorld for EmptyCollisions { + type Error = io::Error; + type Chunk = ChunkPos; + fn collision_chunk(&mut self, _: ChunkPos) -> io::Result> { + Ok(None) + } + fn collision_block(&mut self, _: ChunkPos, _: BlockPos) -> io::Result { + Err(io::Error::other("no chunk is resident")) + } + fn is_suffocating(&mut self, _: ChunkPos, _: CollisionBlock, _: BlockPos) -> io::Result { + Ok(false) + } + fn context_shape( + &mut self, + _: CollisionBlock, + _: BlockPos, + _: &CollisionContext<'_>, + ) -> io::Result { + Ok(VoxelShape::empty()) + } +} + +impl EntityMovementWorld for EmptyCollisions { + type Error = io::Error; + fn entity_collisions(&mut self, _: EntityIdentity, _: Aabb) -> io::Result> { + Ok(Vec::new()) + } + fn movement_border(&mut self, _: EntityIdentity, _: Aabb) -> io::Result> { + Ok(None) + } +} + /// Collision queries over any block lookup (test worlds without chunks): /// every chunk is present, nothing is suffocating, no entities collide. pub struct LookupCollisions<'w, 'r> { @@ -520,3 +568,46 @@ mod piston_noclip_tests { assert_eq!(PISTON_NOCLIP.with(|value| value.get()), None); } } + +#[cfg(test)] +mod border_movement_tests { + use super::*; + + /// A border 10 wide around the origin: its wall's inner face is at x = 5. + fn border() -> BorderGeometry { + BorderGeometry::new([0.0, 0.0], 10.0, 29_999_984).expect("a border") + } + + /// `WorldBorder.isInsideCloseToBorder` and `WorldBorder.getCollisionShape`: + /// a body inside close to the border collides with the border's walls. + #[test] + fn a_body_next_to_the_border_stops_at_its_wall() { + // A body 0.6 wide, 0.4 blocks short of the wall, moved east into it. + let bounds = Aabb::new(Vec3::new(4.0, 0.0, 0.0), Vec3::new(4.6, 1.8, 0.6)); + let border = border(); + let mut collisions = EntityCollisions::with_border(EmptyCollisions, &border, [4.3, 0.3]); + let moved = collisions + .resolve(bounds, Vec3::new(0.51, 0.0, 0.0), 0.0, false) + .expect("the movement resolves"); + // The wall stops the body 0.4 blocks short of the whole move. + assert!( + (moved.x - 0.4).abs() < 1.0e-9, + "the body was not stopped by the border: {moved:?}" + ); + } + + /// Away from the border, the same body moves the whole way. + #[test] + fn a_body_away_from_the_border_moves_whole() { + let bounds = Aabb::new(Vec3::new(0.0, 0.0, 0.0), Vec3::new(0.6, 1.8, 0.6)); + let border = border(); + let mut collisions = EntityCollisions::with_border(EmptyCollisions, &border, [0.3, 0.3]); + let moved = collisions + .resolve(bounds, Vec3::new(0.51, 0.0, 0.0), 0.0, false) + .expect("the movement resolves"); + assert!( + (moved.x - 0.51).abs() < 1.0e-9, + "the body did not move the whole way: {moved:?}" + ); + } +} diff --git a/crates/horizon-client/src/game/piston_push.rs b/crates/horizon-client/src/game/piston_push.rs index 9c3eebb50..6bbf1cb39 100644 --- a/crates/horizon-client/src/game/piston_push.rs +++ b/crates/horizon-client/src/game/piston_push.rs @@ -8,10 +8,13 @@ //! wall instead of passing through it. //! use super::Game; -use horizon_client::collisions::{SceneCollisions, with_piston_noclip}; -use horizon_core::geometry::{Aabb, Direction, Vec3}; +use horizon_client::collisions::{EntityCollisions, SceneCollisions, with_piston_noclip}; +use horizon_core::geometry::{Aabb, Direction, Vec3, border::BorderGeometry}; use horizon_core::position::BlockPos; -use horizon_world::{level::pistons::MovingPiston, piston_movement::PistonMovement, piston_push}; +use horizon_world::{ + block_collision_scan::BlockCollisionWorld, level::pistons::MovingPiston, + piston_movement::PistonMovement, piston_push, world_movement::EntityMovementWorld, +}; /// What one piston asks for this tick: where it is, its state, and the progress /// `moveCollidedEntities` runs at (before the tick stores it). @@ -137,6 +140,7 @@ impl Game<'_> { self.move_pushed( target, before, + [body.position.x, body.position.z], body.step_height, body.on_ground, step.scale(amount), @@ -159,6 +163,7 @@ impl Game<'_> { self.move_pushed( target, after.bounds, + [after.position.x, after.position.z], after.step_height, after.on_ground, back.scale(fix), @@ -186,6 +191,7 @@ impl Game<'_> { self.move_pushed( target, body.bounds, + [body.position.x, body.position.z], body.step_height, body.on_ground, piston_push::step_vector(movement).scale(delta_progress), @@ -342,11 +348,12 @@ impl Game<'_> { } /// `Entity.move(MoverType.PISTON, movement)` for one pushed entity: the - /// per-tick piston limit, then the block collision scan. + /// per-tick piston limit, then the block and world border collision scan. fn move_pushed( &mut self, target: Target, before: Aabb, + at: [f64; 2], step_height: f32, on_ground: bool, movement: Vec3, @@ -361,12 +368,19 @@ impl Game<'_> { return; } let resolved = with_piston_noclip(piston_direction, || { - SceneCollisions { - world: &self.world, - binding: self.collision, - subject: None, - } - .resolve(before, limited, step_height, on_ground) + pushed_movement( + SceneCollisions { + world: &self.world, + binding: self.collision, + subject: None, + }, + self.border.geometry(), + at, + before, + limited, + step_height, + on_ground, + ) .unwrap_or(Vec3::ZERO) }); if resolved.length_squared() <= 1.0e-7 @@ -477,3 +491,83 @@ impl Game<'_> { } } } + +/// `Entity.move(MoverType.PISTON, movement)` for a pushed body: the movement +/// it makes. The world's blocks stop it, and so does the world border: +/// `Entity.collectCollidersIgnoringWorldBorder` adds the border's walls when +/// the body is inside close to the border, as ordinary movement does. +fn pushed_movement( + blocks: W, + border: &BorderGeometry, + at: [f64; 2], + before: Aabb, + movement: Vec3, + step_height: f32, + on_ground: bool, +) -> horizon_client::Result +where + W: BlockCollisionWorld + EntityMovementWorld, +{ + EntityCollisions::with_border(blocks, border, at).resolve( + before, + movement, + step_height, + on_ground, + ) +} + +#[cfg(test)] +mod tests { + use super::*; + use horizon_client::collisions::EmptyCollisions; + + /// A border 10 wide around the origin: its wall's inner face is at x = 5. + fn border() -> BorderGeometry { + BorderGeometry::new([0.0, 0.0], 10.0, 29_999_984).expect("a border") + } + + /// `Entity.collectCollidersIgnoringWorldBorder`: the border's walls stop a + /// pushed body that is inside close to the border. + #[test] + fn a_pushed_body_stops_at_the_world_border() { + // A body 0.6 wide, 0.4 blocks short of the wall, pushed east into it. + let before = Aabb::new(Vec3::new(4.0, 0.0, 0.0), Vec3::new(4.6, 1.8, 0.6)); + let border = border(); + let moved = pushed_movement( + EmptyCollisions, + &border, + [4.3, 0.3], + before, + Vec3::new(0.51, 0.0, 0.0), + 0.0, + false, + ) + .expect("the push resolves"); + // The wall stops the body 0.4 blocks short of the whole push. + assert!( + (moved.x - 0.4).abs() < 1.0e-9, + "the body was not stopped by the border: {moved:?}" + ); + } + + /// Away from the border, the same push moves the whole way. + #[test] + fn a_pushed_body_moves_whole_away_from_the_border() { + let before = Aabb::new(Vec3::new(0.0, 0.0, 0.0), Vec3::new(0.6, 1.8, 0.6)); + let border = border(); + let moved = pushed_movement( + EmptyCollisions, + &border, + [0.3, 0.3], + before, + Vec3::new(0.51, 0.0, 0.0), + 0.0, + false, + ) + .expect("the push resolves"); + assert!( + (moved.x - 0.51).abs() < 1.0e-9, + "the body did not move the whole push: {moved:?}" + ); + } +} diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index 4bb9a478b..585b3000b 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -2453,6 +2453,56 @@ def check_piston_push(host, guest, out): after = next(p['pos'][0] for p in pigs if p['id'] == pig['id']) results['pig'] = after - before assert results['pig'] > 0.3, f'piston did not push pig: {before} -> {after}' + results.update(piston_push_at_border(host, guest)) + return results + + +def piston_push_at_border(host, guest): + """A piston next to the world border: the border's wall stops the pushed + body, as it stops ordinary movement (`Entity.collectCollidersIgnoringWorldBorder`).""" + hx, _, hz = pos_of(host) + x, y, z = (int(hx // 16) * 16 + 8, 100, int(hz // 16) * 16 + 8) + host('command', command=f'fill {x-4} {y-1} {z-4} {x+7} {y-1} {z+4} minecraft:stone') + host('command', command=f'fill {x-4} {y} {z-4} {x+7} {y+5} {z+4} minecraft:air') + host('command', command=f'setblock {x} {y} {z} minecraft:piston[facing=east]') + # The border's east wall stands at `x + 2`, the east face of the block the + # head extends into: a body pushed east stops there instead of being + # pushed out through the border. `worldborder center` centres integers on + # the block, so the centre is `x - 8.5` and the wall lands on `x + 2`. + wall = x + 2 + center = [x - 8.5, z + 0.5] + results = {} + try: + host('command', command=f'worldborder center {x - 9} {z}') + host('command', command='worldborder set 20.5') + seen = None + for _ in range(10): + host.wait(2) + seen = [g('status').get('border') for g in (host, guest)] + if all(b and b['size'] == 20.5 and b['center'] == center for b in seen): + break + results['border'] = seen + for player, name in ((host, host.player), (guest, GUEST_NAME)): + host('command', command=f'setblock {x-1} {y} {z} minecraft:air') + host.wait(10) + host('command', command=f'tp @s {x+5.5} {y} {z+2.5}') + host('command', command=f'tp {GUEST_NAME} {x+5.5} {y} {z-1.5}') + host('command', command=f'tp {"@s" if player is host else GUEST_NAME} {x+1.65} {y} {z+0.5}') + player.wait(15) + before = pos_of(player) + assert abs(before[0] - (x + 1.65)) < 0.1, f'{name} did not reach the piston: {before}' + host('command', command=f'setblock {x-1} {y} {z} minecraft:redstone_block') + player.wait(30) + after = pos_of(player) + # The wall's inner face is at `wall`: the body's box stops there. + results[name] = round(after[0] - before[0], 3) + assert after[0] > before[0], f'the piston did not push {name}: {before} -> {after}' + assert after[0] <= wall - 0.3 + 0.01, f'the border did not stop {name}: {before} -> {after}' + host('command', command=f'setblock {x-1} {y} {z} minecraft:air') + host.wait(10) + finally: + host('command', command='worldborder center 0 0') + host('command', command='worldborder set 59999968') return results