6ce83021b5
Powder snow was inert: the player's collision context never carried its
fall distance or boots, block-inside effects never ran for the player,
frozen ticks stayed 0 and the empty bucket only took fluids.
Player (PlayerMotion, LivingEntity.aiStep order):
- PowderSnowBlock.getCollisionShape gets the player's fall distance and
leather boots: it sinks through, walks on top in leather boots (sinks
when crouching), lands on the 0.9-high shape after a fall of more
than 2.5 blocks.
- Entity.applyEffectsFromBlocks over this tick's movement path (the
ported block sweep, StepBasedCollector steps): entityInside makes it
stuck (0.9, 1.5, 0.9) while its feet are in powder snow and sends the
server's snowflake from the level random; FREEZE sets isInPowderSnow
and adds a frozen tick per step; EXTINGUISH destroys the snow if the
player burns and puts the fire out. ServerPlayer.onInsideBlock fires
enter_block for every block entered (which also unlocks the boat
recipes in water, and the end gateway advancement).
- baseTick's wasInPowderSnow lets leather boots climb out (living_air).
- Server freezing: thawing 2 a tick, the powder_snow frost modifier
(-0.05 x percent frozen, not over air), freeze damage every 40 ticks
when fully frozen; leather armor (#freeze_immune_wearables) and
spectators don't freeze.
- HUD: the powder snow outline overlay and frozen hearts.
Items and blocks:
- BucketItem.use on powder snow: pickupBlock (2001), used statistic,
fill sound, FLUID_PICKUP, createFilledResult, filled_bucket trigger.
- SolidBucketItem: the empty_powder_snow place sound and an empty bucket
back in survival.
- Falling blocks land on powder snow, or end up inside it and break
after a fall of more than 2.5 blocks.
test_native_scene_motion gains powder snow fixtures ("snow_tick": the
oracle runs vanilla's real applyEffectsFromBlocks and freezing) and
prints frozen ticks, the snow flags and the stuck multiplier.
Fixes #62
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
289 lines
10 KiB
Rust
289 lines
10 KiB
Rust
//! Collision queries against the same resident chunks used by the renderer.
|
|
//! The development scene has no entities, block entities or world border.
|
|
//! Entity-dependent rules use the defaults for players and the supported mobs.
|
|
use crate::{Result, world::World};
|
|
use horizon_core::{
|
|
geometry::{Aabb, Vec3, VoxelShape},
|
|
position::{BlockPos, ChunkPos},
|
|
};
|
|
use horizon_world::{
|
|
block_collision::{BlockCollisionRegistry, DynamicCollisionWorld, ShulkerCollisionState},
|
|
block_collision_scan::{BlockCollisionWorld, CollisionBlock},
|
|
collision::CachedCollisionRegistry,
|
|
collision_context::CollisionContext,
|
|
dynamic_support::DynamicSupportWorld,
|
|
entity_obstruction::EntityIdentity,
|
|
fluid::FluidState,
|
|
liquid_collision::LiquidCollisionWorld,
|
|
nbt::{self, Limits},
|
|
powder_snow_collision::{PowderSnowEntity, PowderSnowWorld},
|
|
registry::BlockRegistry,
|
|
world_movement::{
|
|
CollisionCheckRequest, EntityMovementWorld, MovementRequest, no_world_collision,
|
|
resolve_world_movement,
|
|
},
|
|
};
|
|
use std::{fs, io, path::Path, sync::Arc};
|
|
|
|
pub fn load<'r>(root: &Path, registry: &'r BlockRegistry) -> Result<BlockCollisionRegistry<'r>> {
|
|
let data = nbt::read_named(
|
|
&fs::read(root.join("cached-collision.nbt"))?,
|
|
Limits::default(),
|
|
)?
|
|
.0
|
|
.tag;
|
|
let cached = CachedCollisionRegistry::from_nbt(&data, registry)?;
|
|
if !cached.has_canonical_identities() {
|
|
return Err("scene collisions require canonical shape identities".into());
|
|
}
|
|
Ok(BlockCollisionRegistry::new(Arc::new(cached))?)
|
|
}
|
|
|
|
/// What powder snow's contextual collision asks of the colliding entity
|
|
/// (`PowderSnowBlock.getCollisionShape`).
|
|
#[derive(Clone, Copy, Debug, Default)]
|
|
pub struct PowderSnowSubject {
|
|
pub fall_distance: f64,
|
|
/// `FallingBlockEntity`.
|
|
pub falling_block: bool,
|
|
/// A living entity with leather boots on its feet.
|
|
pub leather_boots: bool,
|
|
/// `EntityTypeTags.POWDER_SNOW_WALKABLE_MOBS`.
|
|
pub walkable_type: bool,
|
|
}
|
|
|
|
pub struct SceneCollisions<'w, 'r> {
|
|
pub world: &'w World,
|
|
pub binding: &'w BlockCollisionRegistry<'r>,
|
|
/// The entity colliding, for powder snow; None: no fall, no boots.
|
|
pub subject: Option<PowderSnowSubject>,
|
|
}
|
|
impl SceneCollisions<'_, '_> {
|
|
/// Position contexts permit geometric movement probes without inventing a
|
|
/// player, inventory, effects or fluid-standing behavior.
|
|
pub fn resolve(
|
|
&mut self,
|
|
bounds: Aabb,
|
|
movement: Vec3,
|
|
max_step: f32,
|
|
on_ground: bool,
|
|
) -> Result<Vec3> {
|
|
Ok(resolve_world_movement(
|
|
self,
|
|
MovementRequest {
|
|
entity: EntityIdentity(0),
|
|
context: CollisionContext::position(bounds.min_position().y),
|
|
bounds,
|
|
movement,
|
|
max_step,
|
|
on_ground,
|
|
candidate_budget: 1_000_000,
|
|
},
|
|
)?)
|
|
}
|
|
pub fn unobstructed(&mut self, bounds: Aabb) -> Result<bool> {
|
|
Ok(no_world_collision(
|
|
self,
|
|
CollisionCheckRequest {
|
|
entity: EntityIdentity(0),
|
|
context: CollisionContext::position(bounds.min_position().y),
|
|
bounds,
|
|
candidate_budget: 1_000_000,
|
|
},
|
|
)?)
|
|
}
|
|
}
|
|
/// `noCollision(null, box)`: block collisions under the empty collision
|
|
/// context, as entity-less queries such as spawn checks use.
|
|
pub fn no_collision_empty<W, E>(world: &mut W, bounds: Aabb) -> bool
|
|
where
|
|
W: BlockCollisionWorld<Error = E> + EntityMovementWorld<Error = E>,
|
|
{
|
|
no_world_collision(
|
|
world,
|
|
CollisionCheckRequest {
|
|
entity: EntityIdentity(0),
|
|
context: CollisionContext::empty(false),
|
|
bounds,
|
|
candidate_budget: 1_000_000,
|
|
},
|
|
)
|
|
.unwrap_or(false)
|
|
}
|
|
impl BlockCollisionWorld for SceneCollisions<'_, '_> {
|
|
type Error = io::Error;
|
|
type Chunk = ChunkPos;
|
|
fn collision_chunk(&mut self, p: ChunkPos) -> io::Result<Option<ChunkPos>> {
|
|
Ok(self.world.chunks.contains_key(&(p.x, p.z)).then_some(p))
|
|
}
|
|
fn collision_block(&mut self, chunk: ChunkPos, p: BlockPos) -> io::Result<CollisionBlock> {
|
|
let chunk = self
|
|
.world
|
|
.chunks
|
|
.get(&(chunk.x, chunk.z))
|
|
.ok_or_else(|| io::Error::other("resident collision chunk disappeared"))?;
|
|
self.binding.collision_block(chunk.block(p.x, p.y, p.z))
|
|
}
|
|
fn is_suffocating(&mut self, _: ChunkPos, _: CollisionBlock, _: BlockPos) -> io::Result<bool> {
|
|
Ok(false)
|
|
}
|
|
fn context_shape(
|
|
&mut self,
|
|
block: CollisionBlock,
|
|
p: BlockPos,
|
|
context: &CollisionContext<'_>,
|
|
) -> io::Result<VoxelShape> {
|
|
self.binding
|
|
.collision_shape_with_context(self, block.state, p, context)
|
|
.map_err(io::Error::other)
|
|
}
|
|
}
|
|
impl EntityMovementWorld for SceneCollisions<'_, '_> {
|
|
type Error = io::Error;
|
|
fn entity_collisions(&mut self, _: EntityIdentity, _: Aabb) -> io::Result<Vec<VoxelShape>> {
|
|
Ok(Vec::new())
|
|
}
|
|
fn movement_border(&mut self, _: EntityIdentity, _: Aabb) -> io::Result<Option<VoxelShape>> {
|
|
Ok(None)
|
|
}
|
|
}
|
|
impl DynamicSupportWorld for SceneCollisions<'_, '_> {
|
|
type Error = io::Error;
|
|
fn shulker_is_closed(&mut self, _: [i32; 3]) -> io::Result<Option<bool>> {
|
|
Ok(None)
|
|
}
|
|
fn moving_piston_shape(&mut self, _: [i32; 3]) -> io::Result<Option<VoxelShape>> {
|
|
Ok(None)
|
|
}
|
|
}
|
|
impl DynamicCollisionWorld for SceneCollisions<'_, '_> {
|
|
fn shulker_collision_state(
|
|
&mut self,
|
|
_: [i32; 3],
|
|
) -> io::Result<Option<ShulkerCollisionState>> {
|
|
Ok(None)
|
|
}
|
|
}
|
|
impl LiquidCollisionWorld for SceneCollisions<'_, '_> {
|
|
type Error = io::Error;
|
|
/// Players and the supported mobs cannot stand on fluids (no striders).
|
|
fn entity_liquid_shape(&mut self, _: EntityIdentity) -> io::Result<VoxelShape> {
|
|
Ok(VoxelShape::empty())
|
|
}
|
|
fn fluid(&mut self, p: BlockPos) -> io::Result<FluidState> {
|
|
FluidState::from_block(
|
|
self.binding
|
|
.registry()
|
|
.state(self.world.block(p.x, p.y, p.z))
|
|
.ok_or_else(|| io::Error::other("scene fluid state outside registry"))?,
|
|
)
|
|
}
|
|
fn entity_can_stand(&mut self, _: EntityIdentity, _: FluidState) -> io::Result<bool> {
|
|
Ok(false)
|
|
}
|
|
}
|
|
impl PowderSnowWorld for SceneCollisions<'_, '_> {
|
|
type Error = io::Error;
|
|
fn powder_snow_entity(&mut self, _: EntityIdentity) -> io::Result<PowderSnowEntity> {
|
|
let subject = self.subject.unwrap_or_default();
|
|
Ok(PowderSnowEntity {
|
|
fall_distance: subject.fall_distance,
|
|
falling_block: subject.falling_block,
|
|
})
|
|
}
|
|
fn powder_snow_walkable_tag(&mut self, _: EntityIdentity) -> io::Result<bool> {
|
|
Ok(self.subject.is_some_and(|s| s.walkable_type))
|
|
}
|
|
fn feet_are_leather_boots(&mut self, _: EntityIdentity) -> io::Result<bool> {
|
|
Ok(self.subject.is_some_and(|s| s.leather_boots))
|
|
}
|
|
}
|
|
|
|
/// 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> {
|
|
pub block: &'w dyn Fn(i32, i32, i32) -> horizon_world::registry::BlockStateId,
|
|
pub binding: &'w BlockCollisionRegistry<'r>,
|
|
}
|
|
impl BlockCollisionWorld for LookupCollisions<'_, '_> {
|
|
type Error = io::Error;
|
|
type Chunk = ChunkPos;
|
|
fn collision_chunk(&mut self, p: ChunkPos) -> io::Result<Option<ChunkPos>> {
|
|
Ok(Some(p))
|
|
}
|
|
fn collision_block(&mut self, _: ChunkPos, p: BlockPos) -> io::Result<CollisionBlock> {
|
|
self.binding.collision_block((self.block)(p.x, p.y, p.z))
|
|
}
|
|
fn is_suffocating(&mut self, _: ChunkPos, _: CollisionBlock, _: BlockPos) -> io::Result<bool> {
|
|
Ok(false)
|
|
}
|
|
fn context_shape(
|
|
&mut self,
|
|
block: CollisionBlock,
|
|
p: BlockPos,
|
|
context: &CollisionContext<'_>,
|
|
) -> io::Result<VoxelShape> {
|
|
self.binding
|
|
.collision_shape_with_context(self, block.state, p, context)
|
|
.map_err(io::Error::other)
|
|
}
|
|
}
|
|
impl EntityMovementWorld for LookupCollisions<'_, '_> {
|
|
type Error = io::Error;
|
|
fn entity_collisions(&mut self, _: EntityIdentity, _: Aabb) -> io::Result<Vec<VoxelShape>> {
|
|
Ok(Vec::new())
|
|
}
|
|
fn movement_border(&mut self, _: EntityIdentity, _: Aabb) -> io::Result<Option<VoxelShape>> {
|
|
Ok(None)
|
|
}
|
|
}
|
|
impl DynamicSupportWorld for LookupCollisions<'_, '_> {
|
|
type Error = io::Error;
|
|
fn shulker_is_closed(&mut self, _: [i32; 3]) -> io::Result<Option<bool>> {
|
|
Ok(None)
|
|
}
|
|
fn moving_piston_shape(&mut self, _: [i32; 3]) -> io::Result<Option<VoxelShape>> {
|
|
Ok(None)
|
|
}
|
|
}
|
|
impl DynamicCollisionWorld for LookupCollisions<'_, '_> {
|
|
fn shulker_collision_state(
|
|
&mut self,
|
|
_: [i32; 3],
|
|
) -> io::Result<Option<ShulkerCollisionState>> {
|
|
Ok(None)
|
|
}
|
|
}
|
|
impl LiquidCollisionWorld for LookupCollisions<'_, '_> {
|
|
type Error = io::Error;
|
|
fn entity_liquid_shape(&mut self, _: EntityIdentity) -> io::Result<VoxelShape> {
|
|
Ok(VoxelShape::empty())
|
|
}
|
|
fn fluid(&mut self, p: BlockPos) -> io::Result<FluidState> {
|
|
FluidState::from_block(
|
|
self.binding
|
|
.registry()
|
|
.state((self.block)(p.x, p.y, p.z))
|
|
.ok_or_else(|| io::Error::other("fluid state outside registry"))?,
|
|
)
|
|
}
|
|
fn entity_can_stand(&mut self, _: EntityIdentity, _: FluidState) -> io::Result<bool> {
|
|
Ok(false)
|
|
}
|
|
}
|
|
impl PowderSnowWorld for LookupCollisions<'_, '_> {
|
|
type Error = io::Error;
|
|
fn powder_snow_entity(&mut self, _: EntityIdentity) -> io::Result<PowderSnowEntity> {
|
|
Ok(PowderSnowEntity {
|
|
fall_distance: 0.0,
|
|
falling_block: false,
|
|
})
|
|
}
|
|
fn powder_snow_walkable_tag(&mut self, _: EntityIdentity) -> io::Result<bool> {
|
|
Ok(false)
|
|
}
|
|
fn feet_are_leather_boots(&mut self, _: EntityIdentity) -> io::Result<bool> {
|
|
Ok(false)
|
|
}
|
|
}
|