Files
horizon-airdef/crates/horizon-client/src/entities.rs
T
Ludovic Marie df14adbfba Format the workspace with rustfmt
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 00:28:32 +02:00

333 lines
11 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) % 4 == 0 {
let mut scene = SceneCollisions {
world,
binding: collision,
};
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,
};
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
}
}