Files
horizon-airdef/crates/horizon-client/src/hunger.rs
T
Ludovic Marie e28d1a5890 Add the ender dragon fight
The dragon with its phases, path nodes, fireballs and breath, end crystals
that heal it, the boss bar and boss music, and the kill: XP, the exit
portal, the egg and a new gateway. The fight state is saved where 26.3
saves it. Creative players are now invulnerable, as in vanilla.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 04:59:58 +02:00

150 lines
5.1 KiB
Rust

//! Hunger and regeneration over the resident player's actual health, damage,
//! attribute and exhaustion storage. Starvation borrows the same FoodData that
//! is mid-tick, preserving damage callbacks and the following timer reset.
use crate::{
Result,
health::{Attributes, Environment, HealthEvent, HealthResources, HealthRules, State, Updates},
motion::{MotionEvent, PlayerMotion},
};
use horizon_world::{
food::{Difficulty, FoodData, FoodWorld},
player_health::{PlayerAttributeHealth, PlayerFloatIds},
server_player_combat::ServerPlayerCombat,
synced_floats::SyncedFloats,
};
impl PlayerMotion {
/// ServerPlayer.tickRegeneration, dispatched from Player.aiStep before
/// inventory and living movement. The separately invoked food tick follows
/// movement in ServerPlayer.doTick.
pub fn peaceful_regeneration(&mut self, rules: HealthRules) -> Result<()> {
if rules.difficulty != Difficulty::Peaceful || !rules.natural_regeneration {
return Ok(());
}
if self.health.tick_count % 20 == 0 {
let health = self.health.health();
let maximum = self.attribute("minecraft:max_health")? as f32;
if health < maximum {
let h = &mut self.health;
let mut observer = Updates(&mut self.events);
PlayerAttributeHealth::new(
&mut self.attributes,
&mut h.data,
h.ids,
h.dead,
&mut observer,
)?
.heal(1.0)?;
}
let saturation = self.health.food.saturation;
if saturation < 20.0 {
self.health.food.saturation = saturation + 1.0;
}
}
if self.health.tick_count % 10 == 0 && self.health.food.needs_food() {
self.health.food.food = self.health.food.food.wrapping_add(1);
}
Ok(())
}
pub fn food_tick(&mut self, resources: &HealthResources, rules: HealthRules) -> Result<()> {
let h = &mut self.health;
let mut world = TickWorld {
resources,
rules,
attributes: &mut self.attributes,
data: &mut h.data,
ids: h.ids,
hurt: &mut h.hurt,
dead: &mut h.dead,
sync_velocity: &mut h.sync_velocity,
game_time: h.game_time,
events: &mut self.events,
};
h.food.tick(&mut world)
}
}
struct TickWorld<'a> {
resources: &'a HealthResources,
rules: HealthRules,
attributes: &'a mut Attributes,
data: &'a mut SyncedFloats,
ids: PlayerFloatIds,
hurt: &'a mut State,
dead: &'a mut bool,
sync_velocity: &'a mut bool,
game_time: i64,
events: &'a mut Vec<MotionEvent>,
}
impl FoodWorld for TickWorld<'_> {
type Error = Box<dyn std::error::Error>;
type Level = ();
fn level(&mut self, _: &mut FoodData) -> Result<()> {
Ok(())
}
fn difficulty(&mut self, _: (), _: &mut FoodData) -> Result<Difficulty> {
Ok(self.rules.difficulty)
}
fn natural_regeneration(&mut self, _: (), _: &mut FoodData) -> Result<bool> {
Ok(self.rules.natural_regeneration)
}
fn is_hurt(&mut self, _: &mut FoodData) -> Result<bool> {
let mut observer = Updates(self.events);
Ok(PlayerAttributeHealth::new(
self.attributes,
self.data,
self.ids,
*self.dead,
&mut observer,
)?
.is_hurt()?)
}
fn heal(&mut self, _: &mut FoodData, amount: f32) -> Result<()> {
let mut observer = Updates(self.events);
Ok(PlayerAttributeHealth::new(
self.attributes,
self.data,
self.ids,
*self.dead,
&mut observer,
)?
.heal(amount)?)
}
fn health(&mut self, _: &mut FoodData) -> Result<f32> {
Ok(self
.data
.get(self.ids.health)
.expect("validated health field"))
}
fn hurt_starvation(&mut self, _: (), food: &mut FoodData, amount: f32) -> Result<()> {
let id = self
.resources
.damage
.id("minecraft:starve")
.expect("validated starvation source");
self.events
.push(MotionEvent::Health(HealthEvent::StarvationDamage(amount)));
let mut environment = Environment {
invulnerable: false,
rules: self.rules,
source: self
.resources
.damage
.source::<()>(id, None, None, None)
.expect("validated starvation source"),
events: self.events,
dead: self.dead,
sync_velocity: self.sync_velocity,
game_time: self.game_time,
status: Default::default(),
blocking: None,
blocked: 0.0,
block_sound: false,
totem: false,
totem_fired: false,
};
ServerPlayerCombat::new(self.attributes, self.data, food, self.ids, &mut environment)?
.hurt(self.hurt, amount)?;
Ok(())
}
}