Files
horizon-airdef/crates/horizon-client/src/entities.rs
T
Ludovic Marie 6ce83021b5 Powder snow: sinking, freezing, leather boots, buckets and falling blocks
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>
2026-10-14 15:45:55 +02:00

500 lines
16 KiB
Rust

//! Non-player entities simulated on the client world. Dropped items follow
//! `ItemEntity.tick`: gravity 0.04, 0.98 drag (block friction on the ground),
//! a ground bounce of -0.5, water buoyancy, pickup delay and 6000-tick despawn.
use crate::{
blocks::{BlockFacts, Fluid},
collisions::SceneCollisions,
items::ItemStack,
world::World,
};
use horizon_core::geometry::{Aabb, Vec3};
use horizon_world::block_collision::BlockCollisionRegistry;
#[derive(Clone, Debug)]
pub struct ItemEntity {
pub id: u32,
pub stack: ItemStack,
pub position: Vec3,
pub previous: Vec3,
pub velocity: Vec3,
pub on_ground: bool,
pub age: u32,
pub pickup_delay: u32,
pub bob_offset: f32,
}
impl ItemEntity {
pub const WIDTH: f64 = 0.25;
pub fn bounds(&self) -> Aabb {
let h = Self::WIDTH / 2.0;
Aabb::new(
Vec3::new(self.position.x - h, self.position.y, self.position.z - h),
Vec3::new(
self.position.x + h,
self.position.y + Self::WIDTH,
self.position.z + h,
),
)
}
/// Returns false when the item should be removed.
pub fn tick(
&mut self,
world: &World,
facts: &BlockFacts,
collision: &BlockCollisionRegistry<'_>,
) -> bool {
self.previous = self.position;
if self.pickup_delay > 0 && self.pickup_delay != u32::MAX {
self.pickup_delay -= 1;
}
let eye = world.block(
self.position.x.floor() as i32,
(self.position.y + 0.1).floor() as i32,
self.position.z.floor() as i32,
);
let in_water = facts.get(eye).fluid == Fluid::Water;
if in_water {
// ItemEntity.setUnderwaterMovement
self.velocity = Vec3::new(
self.velocity.x * 0.99,
self.velocity.y + if self.velocity.y < 0.06 { 5.0e-4 } else { 0.0 },
self.velocity.z * 0.99,
);
} else {
self.velocity.y -= 0.04;
}
let horizontal = self.velocity.x * self.velocity.x + self.velocity.z * self.velocity.z;
if !self.on_ground || horizontal > 1.0e-5 || (self.age + self.id).is_multiple_of(4) {
let mut scene = SceneCollisions {
world,
binding: collision,
subject: None,
};
let wanted = self.velocity;
let moved = scene
.resolve(self.bounds(), wanted, 0.0, self.on_ground)
.unwrap_or(Vec3::ZERO);
self.position = Vec3::new(
self.position.x + moved.x,
self.position.y + moved.y,
self.position.z + moved.z,
);
self.on_ground = wanted.y < 0.0 && moved.y != wanted.y;
if moved.x != wanted.x {
self.velocity.x = 0.0;
}
if moved.z != wanted.z {
self.velocity.z = 0.0;
}
if moved.y != wanted.y {
self.velocity.y = 0.0;
}
let mut friction = 0.98;
if self.on_ground {
let below = world.block(
self.position.x.floor() as i32,
(self.position.y - 0.999_999).floor() as i32,
self.position.z.floor() as i32,
);
friction = f64::from(facts.get(below).friction) * 0.98;
}
self.velocity = Vec3::new(
self.velocity.x * friction,
self.velocity.y * 0.98,
self.velocity.z * friction,
);
if self.on_ground && self.velocity.y < 0.0 {
self.velocity.y *= -0.5;
}
}
self.age += 1;
self.age < 6000 && self.position.y > -128.0
}
}
/// Arrows (`AbstractArrow.tick`): 0.99 drag, 0.05 gravity, sticking in blocks.
#[derive(Clone, Debug)]
pub struct Arrow {
/// Shot by the player (hits mobs) rather than a mob (hits the player).
pub from_player: bool,
pub critical: bool,
pub position: Vec3,
pub previous: Vec3,
pub velocity: Vec3,
pub stuck: bool,
pub age: u32,
pub base_damage: f64,
}
pub enum ArrowHit {
None,
Player(f32),
}
impl Arrow {
pub fn tick(
&mut self,
world: &World,
facts: &BlockFacts,
player: Option<Aabb>,
) -> (bool, ArrowHit) {
self.previous = self.position;
self.age += 1;
if self.stuck {
return (self.age < 1200, ArrowHit::None);
}
let steps = 4;
for _ in 0..steps {
let next = Vec3::new(
self.position.x + self.velocity.x / f64::from(steps),
self.position.y + self.velocity.y / f64::from(steps),
self.position.z + self.velocity.z / f64::from(steps),
);
if let Some(b) = player {
let (min, max) = (b.min_position(), b.max_position());
if next.x >= min.x
&& next.x <= max.x
&& next.y >= min.y
&& next.y <= max.y
&& next.z >= min.z
&& next.z <= max.z
{
let speed = (self.velocity.x.powi(2)
+ self.velocity.y.powi(2)
+ self.velocity.z.powi(2))
.sqrt();
// AbstractArrow.onHitEntity: ceil(speed * baseDamage).
let damage = (speed * self.base_damage).ceil().max(0.0) as f32;
return (false, ArrowHit::Player(damage));
}
}
let block = world.block(
next.x.floor() as i32,
next.y.floor() as i32,
next.z.floor() as i32,
);
if facts.get(block).full_collision {
self.stuck = true;
self.age = 0;
return (true, ArrowHit::None);
}
self.position = next;
}
self.velocity = Vec3::new(
self.velocity.x * 0.99,
self.velocity.y * 0.99 - 0.05,
self.velocity.z * 0.99,
);
(self.position.y > -128.0 && self.age < 1200, ArrowHit::None)
}
}
/// ExperienceOrb: gravity 0.03, homing towards the player within 8 blocks.
#[derive(Clone, Debug)]
pub struct ExperienceOrb {
pub position: Vec3,
pub previous: Vec3,
pub velocity: Vec3,
pub value: u32,
pub age: u32,
pub on_ground: bool,
}
impl ExperienceOrb {
/// ExperienceOrb.getExperienceValue: the largest orb size not above `value`.
pub fn split(value: u32) -> u32 {
[2477, 1237, 617, 307, 149, 73, 37, 17, 7, 3, 1]
.into_iter()
.find(|&v| value >= v)
.unwrap_or(1)
}
/// ExperienceOrb.getIcon
pub fn icon(&self) -> u32 {
match self.value {
2477.. => 10,
1237.. => 9,
617.. => 8,
307.. => 7,
149.. => 6,
73.. => 5,
37.. => 4,
17.. => 3,
7.. => 2,
3.. => 1,
_ => 0,
}
}
pub fn tick(
&mut self,
world: &World,
facts: &BlockFacts,
collision: &BlockCollisionRegistry<'_>,
player: Option<[f64; 3]>,
) -> bool {
self.previous = self.position;
let block = world.block(
self.position.x.floor() as i32,
self.position.y.floor() as i32,
self.position.z.floor() as i32,
);
if facts.get(block).fluid == Fluid::Water {
self.velocity = Vec3::new(
self.velocity.x * 0.99,
(self.velocity.y + 5.0e-4).min(0.06),
self.velocity.z * 0.99,
);
} else {
self.velocity.y -= 0.03;
}
if let Some(p) = player {
let d = [
(p[0] - self.position.x) / 8.0,
(p[1] - self.position.y) / 8.0,
(p[2] - self.position.z) / 8.0,
];
let length = (d[0] * d[0] + d[1] * d[1] + d[2] * d[2]).sqrt();
let power = 1.0 - length;
if power > 0.0 && length > 1e-6 {
let power = power * power;
self.velocity = Vec3::new(
self.velocity.x + d[0] / length * power * 0.1,
self.velocity.y + d[1] / length * power * 0.1,
self.velocity.z + d[2] / length * power * 0.1,
);
}
}
let h = 0.25;
let bounds = Aabb::new(
Vec3::new(self.position.x - h, self.position.y, self.position.z - h),
Vec3::new(
self.position.x + h,
self.position.y + 0.5,
self.position.z + h,
),
);
let mut scene = SceneCollisions {
world,
binding: collision,
subject: None,
};
let wanted = self.velocity;
let moved = scene
.resolve(bounds, wanted, 0.0, self.on_ground)
.unwrap_or(Vec3::ZERO);
self.position = Vec3::new(
self.position.x + moved.x,
self.position.y + moved.y,
self.position.z + moved.z,
);
self.on_ground = wanted.y < 0.0 && moved.y != wanted.y;
let mut friction = 0.98;
if self.on_ground {
let below = world.block(
self.position.x.floor() as i32,
(self.position.y - 1.0).floor() as i32,
self.position.z.floor() as i32,
);
friction = f64::from(facts.get(below).friction) * 0.98;
}
self.velocity = Vec3::new(
self.velocity.x * friction,
self.velocity.y * 0.98,
self.velocity.z * friction,
);
if self.on_ground {
self.velocity.y *= -0.9;
}
self.age += 1;
self.age < 6000
}
}
/// Player experience (Player.giveExperiencePoints / getXpNeededForNextLevel).
#[derive(Clone, Copy, Debug, Default)]
pub struct Experience {
pub level: i32,
pub progress: f32,
pub total: i32,
}
impl Experience {
pub fn needed(level: i32) -> i32 {
if level >= 30 {
112 + (level - 30) * 9
} else if level >= 15 {
37 + (level - 15) * 5
} else {
7 + level * 2
}
}
/// Returns true when a level was gained.
pub fn give(&mut self, points: i32) -> bool {
let start = self.level;
self.progress += points as f32 / Self::needed(self.level) as f32;
self.total = self.total.saturating_add(points).max(0);
while self.progress >= 1.0 {
self.progress = (self.progress - 1.0) * Self::needed(self.level) as f32;
self.level += 1;
self.progress /= Self::needed(self.level) as f32;
}
self.level > start
}
}
/// A painting (`Painting`, a `HangingEntity`): the block it hangs in, the
/// 2D data value of the direction it faces, and its variant.
#[derive(Clone, Debug)]
pub struct Painting {
pub id: u32,
pub pos: [i32; 3],
/// Direction.get2DDataValue: south 0, west 1, north 2, east 3.
pub facing: u8,
pub variant: std::sync::Arc<str>,
pub width: i32,
pub height: i32,
/// BlockAttachedEntity.checkInterval.
pub check_interval: u32,
}
impl Painting {
pub fn step(&self) -> [i32; 3] {
[[0, 0, 1], [-1, 0, 0], [0, 0, -1], [1, 0, 0]][usize::from(self.facing & 3)]
}
/// Direction.getCounterClockWise (the painting's left as it faces out).
pub fn left(&self) -> [i32; 3] {
[[1, 0, 0], [0, 0, 1], [-1, 0, 0], [0, 0, -1]][usize::from(self.facing & 3)]
}
/// Painting.calculateBoundingBox: (min, max).
pub fn bounds(&self) -> ([f64; 3], [f64; 3]) {
let s = self.step().map(f64::from);
let l = self.left().map(f64::from);
let off = |n: i32| if n % 2 == 0 { 0.5 } else { 0.0 };
let (h, v) = (off(self.width), off(self.height));
let c = [
f64::from(self.pos[0]) + 0.5 - s[0] * 0.46875 + l[0] * h,
f64::from(self.pos[1]) + 0.5 + v,
f64::from(self.pos[2]) + 0.5 - s[2] * 0.46875 + l[2] * h,
];
let along_x = s[0] != 0.0;
let size = [
if along_x {
0.0625
} else {
f64::from(self.width)
},
f64::from(self.height),
if along_x {
f64::from(self.width)
} else {
0.0625
},
];
(
std::array::from_fn(|i| c[i] - size[i] / 2.0),
std::array::from_fn(|i| c[i] + size[i] / 2.0),
)
}
}
/// An item frame or glow item frame (`ItemFrame`).
#[derive(Clone, Debug)]
pub struct ItemFrame {
pub id: u32,
pub pos: [i32; 3],
/// Direction.get3DDataValue: down 0, up 1, north 2, south 3, west 4, east 5.
pub facing: u8,
pub glow: bool,
pub item: Option<crate::items::ItemStack>,
/// 0..8, in 45 degree steps.
pub rotation: u8,
pub drop_chance: f32,
pub fixed: bool,
pub invisible: bool,
pub check_interval: u32,
}
impl ItemFrame {
pub fn step(&self) -> [i32; 3] {
[
[0, -1, 0],
[0, 1, 0],
[0, 0, -1],
[0, 0, 1],
[-1, 0, 0],
[1, 0, 0],
][usize::from(self.facing.min(5))]
}
pub fn kind(&self) -> &'static str {
if self.glow {
"minecraft:glow_item_frame"
} else {
"minecraft:item_frame"
}
}
/// ItemFrame.createBoundingBox (without a framed map): (min, max).
pub fn bounds(&self) -> ([f64; 3], [f64; 3]) {
let s = self.step().map(f64::from);
let c: [f64; 3] = std::array::from_fn(|i| f64::from(self.pos[i]) + 0.5 - s[i] * 0.46875);
let size: [f64; 3] = std::array::from_fn(|i| if s[i] != 0.0 { 0.0625 } else { 0.75 });
(
std::array::from_fn(|i| c[i] - size[i] / 2.0),
std::array::from_fn(|i| c[i] + size[i] / 2.0),
)
}
}
/// An armor stand (`ArmorStand`).
#[derive(Clone, Debug)]
pub struct ArmorStand {
pub id: u32,
pub position: Vec3,
pub velocity: Vec3,
pub on_ground: bool,
pub yaw: f32,
/// mainhand, offhand, feet, legs, chest, head (EquipmentSlot order).
pub equipment: [Option<crate::items::ItemStack>; 6],
pub small: bool,
pub show_arms: bool,
pub no_base_plate: bool,
pub invisible: bool,
pub marker: bool,
/// Game time of the last hit (the wobble and the second-hit window).
pub last_hit: i64,
}
impl ArmorStand {
pub const SLOTS: [&'static str; 6] = ["mainhand", "offhand", "feet", "legs", "chest", "head"];
pub fn new(id: u32, position: Vec3, yaw: f32) -> Self {
Self {
id,
position,
velocity: Vec3::ZERO,
on_ground: true,
yaw,
equipment: Default::default(),
small: false,
show_arms: false,
no_base_plate: false,
invisible: false,
marker: false,
last_hit: i64::MIN / 2,
}
}
/// EntityType.ARMOR_STAND dimensions (0.5 x 1.975, half for small).
pub fn bounds(&self) -> Aabb {
let s = if self.small { 0.5 } else { 1.0 };
let (w, h) = (0.25 * s, 1.975 * s);
let p = self.position;
Aabb::new(
Vec3::new(p.x - w, p.y, p.z - w),
Vec3::new(p.x + w, p.y + h, p.z + w),
)
}
/// LivingEntity.travel with gravity 0.08 and 0.98 drag, falling until it
/// lands (armor stands have no other movement).
pub fn tick(&mut self, scene: &mut crate::collisions::SceneCollisions) {
if self.marker {
return;
}
let mut v = self.velocity;
v.y -= 0.08;
let wanted = v;
if let Ok(moved) = scene.resolve(self.bounds(), wanted, 0.0, self.on_ground) {
self.position = Vec3::new(
self.position.x + moved.x,
self.position.y + moved.y,
self.position.z + moved.z,
);
self.on_ground = wanted.y < 0.0 && moved.y > wanted.y;
v = Vec3::new(0.0, if moved.y != wanted.y { 0.0 } else { v.y * 0.98 }, 0.0);
}
self.velocity = v;
}
}