//! Block.pushEntitiesUp when replacement collision geometry adds occupied space. use crate::{entity_obstruction::EntityIdentity, registry::BlockStateId}; use horizon_core::{ geometry::{Aabb, Axis, ShapeError, ShapeOp, Vec3, VoxelShape}, position::BlockPos, }; pub trait BlockPushWorld { type Error; /// Native no-context block collision query, first old state, then new state. fn collision_shape( &mut self, state: BlockStateId, position: BlockPos, ) -> Result; /// Return the native-selected nonspectator entities in query order. Selection /// excludes no entity and uses the added shape's bounds, not exact overlap. fn entities(&mut self, bounds: Aabb) -> Result, Self::Error>; /// Read each current box just before its teleport: an earlier teleport can /// change later entities. Handles must remain valid throughout this call. fn entity_bounds(&self, entity: EntityIdentity) -> Aabb; fn teleport_relative( &mut self, entity: EntityIdentity, movement: Vec3, ) -> Result<(), Self::Error>; } #[derive(Debug)] pub enum BlockPushError { World(E), Shape(ShapeError), } impl std::fmt::Display for BlockPushError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::World(e) => e.fmt(f), Self::Shape(e) => e.fmt(f), } } } impl std::error::Error for BlockPushError {} pub fn push_entities_up( world: &mut W, old: BlockStateId, new: BlockStateId, position: BlockPos, ) -> Result> { let old_shape = world .collision_shape(old, position) .map_err(BlockPushError::World)?; let new_shape = world .collision_shape(new, position) .map_err(BlockPushError::World)?; let added = old_shape .join_unoptimized(&new_shape, ShapeOp::ONLY_SECOND) .map_err(BlockPushError::Shape)? .moved(Vec3::new( f64::from(position.x), f64::from(position.y), f64::from(position.z), )); if let Some(bounds) = added.bounds() { let entities = world.entities(bounds).map_err(BlockPushError::World)?; for entity in entities { let bounds = world .entity_bounds(entity) .move_by(Vec3::new(0.0, 1.0, 0.0)); let dy = added.collide(Axis::Y, bounds, -1.0); world .teleport_relative(entity, Vec3::new(0.0, 1.0 + dy, 0.0)) .map_err(BlockPushError::World)?; } } Ok(new) }