Merge pull request #1145 from ludovic111/fix/hurtlog-guest-respawn

🤖 Agent: merged. Verify passes. B5's local integration of all its PRs on main: parity --changed 102/102, plus lan_e2e (flakes listed in FEATURES.md LATER 4l).
This commit is contained in:
Ludovic Marie
2026-10-22 04:05:02 +02:00
committed by GitHub
7 changed files with 165 additions and 2 deletions
+18 -1
View File
@@ -6881,7 +6881,24 @@ impl Game<'_> {
Some(f) => self.attack_source(f),
None => horizon_client::mobs::Damage::of(horizon_client::mobs::DamageKind::Generic),
};
if self.hurt_by_player(index, amount, source) {
let landed = self.hurt_by_player(index, amount, source);
if let Some(p) = self.player.as_mut() {
if p.attack_log.len() >= 64 {
p.attack_log.remove(0);
}
p.attack_log.push(horizon_client::health::AttackRecord {
game_time: p.health.game_time(),
target: target_kind.clone(),
id: self.mobs[index].id,
killed: landed && !self.mobs[index].alive(),
amount,
scale,
crit,
landed,
dealt: old_health - self.mobs[index].health,
});
}
if landed {
if knockback > 0.0 {
let (xd, zd) = self.extra_knockback_direction();
self.mobs[index].knockback(f64::from(knockback), xd, zd);
+53
View File
@@ -24,6 +24,7 @@ const HELP: &[&str] = &[
"time TICKS set the time of day",
"summon KIND [X Y Z] [baby] spawn a mob (in front of the player without a position)",
"mobs [RADIUS] list mobs near the player with their AI state",
"hurtlog the hurts taken and melee attacks made since the last call, per player (host and guests)",
"fill X1 Y1 Z1 X2 Y2 Z2 BLOCK set a box of loaded blocks",
"resume leave the pause menu",
"screenshot PATH write the next frame as PNG",
@@ -469,6 +470,10 @@ impl Game<'_> {
let radius: f64 = words.get(1).map_or(Ok(64.0), |r| r.parse())?;
Ok(Some(self.control_mobs(radius)))
}
"hurtlog" => {
self.require_world()?;
Ok(Some(self.control_hurt_log()))
}
"resume" => {
if self.screen == Screen::Paused {
self.screen = Screen::Playing;
@@ -726,6 +731,54 @@ impl Game<'_> {
}
}
/// The hurts and melee attacks each player logged since the last call
/// (`PlayerMotion::hurt_log`, `attack_log`), taken out.
fn control_hurt_log(&mut self) -> Value {
fn drain(
name: &str,
hurt_log: &mut Vec<horizon_client::health::HurtRecord>,
attack_log: &mut Vec<horizon_client::health::AttackRecord>,
) -> Value {
let hurts: Vec<Value> = hurt_log
.drain(..)
.map(|r| {
json!({
"t": r.game_time, "source": r.source, "amount": r.amount,
"blocked": r.blocked, "taken": r.taken, "hurt": r.hurt,
"armor": r.armor, "toughness": r.toughness, "health": r.health,
"fire_ticks": r.fire_ticks, "knockback": r.knockback,
})
})
.collect();
let attacks: Vec<Value> = attack_log
.drain(..)
.map(|r| {
json!({
"t": r.game_time, "target": r.target, "id": r.id, "killed": r.killed, "amount": r.amount,
"scale": r.scale, "crit": r.crit, "landed": r.landed, "dealt": r.dealt,
})
})
.collect();
json!({ "name": name, "hurts": hurts, "attacks": attacks })
}
let mut players = Vec::new();
if let Some(p) = self.player.as_mut() {
players.push(drain("host", &mut p.hurt_log, &mut p.attack_log));
}
if let Some(host) = self.lan.as_mut() {
for g in &mut host.hub.guests {
let name = g.name.clone();
if let Some(m) = g.motion.as_mut() {
players.push(drain(&name, &mut m.hurt_log, &mut m.attack_log));
} else if let Some((mut hurts, mut attacks)) = g.logs_kept.take() {
// Dead, not yet respawned: what its last body logged.
players.push(drain(&name, &mut hurts, &mut attacks));
}
}
}
json!({ "ok": true, "players": players })
}
fn control_status(&self) -> Value {
let player = self.player.as_ref().map(|p| {
json!({
@@ -1442,6 +1442,11 @@ impl Game<'_> {
{
let (xd, zd) = (from[0] - player.position.x, from[2] - player.position.z);
self.knock_back_player(f64::from(0.4_f32), xd, zd);
if let Some(p) = self.player.as_mut()
&& let Some(r) = p.hurt_log.last_mut()
{
r.knockback = Some([p.velocity.x, p.velocity.y, p.velocity.z]);
}
self.hurt_armor(amount);
self.thorns(from);
}
@@ -200,6 +200,11 @@ impl Game<'_> {
host.hub.guests[i].position[2],
);
host.hub.guests[i].motion = PlayerMotion::new(self.motion, p).ok().map(Box::new);
let kept = host.hub.guests[i].logs_kept.take();
if let (Some(m), Some((hurts, attacks))) = (host.hub.guests[i].motion.as_mut(), kept) {
m.hurt_log = hurts;
m.attack_log = attacks;
}
}
let mut play = host.play.remove(&conn).unwrap_or_default();
let g = &mut host.hub.guests[i];
@@ -1348,7 +1353,10 @@ impl Game<'_> {
g.food = 20;
g.saturation = 5.0;
g.sent_health = None;
g.motion = None;
if let Some(m) = g.motion.take() {
let m = *m;
g.logs_kept = Some((m.hurt_log, m.attack_log));
}
let mut xp = Experience {
level: g.xp_level,
progress: g.xp_progress,
+65
View File
@@ -231,6 +231,10 @@ impl PlayerHealth {
.get(self.ids.absorption)
.expect("validated absorption field")
}
/// The game time the damage clock has counted.
pub fn game_time(&self) -> i64 {
self.game_time
}
pub fn dead_or_dying(&self) -> bool {
self.health() <= 0.0 || self.dead
}
@@ -274,6 +278,11 @@ impl PlayerMotion {
.damage
.source::<()>(source, None, None, None)
.ok_or("unknown resident damage source")?;
let armor = self.attribute("minecraft:armor").unwrap_or(0.0) as f32;
let toughness = self.attribute("minecraft:armor_toughness").unwrap_or(0.0) as f32;
let before = self.health.health() + self.health.absorption();
let fire_ticks = self.fire_ticks;
let source_name = source.definition().name.clone();
let totem_ready = self.health.totem_ready;
let invulnerable = self.invulnerable;
let h = &mut self.health;
@@ -312,6 +321,23 @@ impl PlayerMotion {
if outcome.hurt {
h.hurt_animation_time = h.hurt.hurt_time;
}
let after = h.health() + h.absorption();
if self.hurt_log.len() >= HURT_LOG {
self.hurt_log.remove(0);
}
self.hurt_log.push(HurtRecord {
game_time: h.game_time,
source: source_name,
amount,
blocked: outcome.blocked,
taken: before - after,
hurt: outcome.hurt,
armor,
toughness,
health: after,
fire_ticks,
knockback: None,
});
Ok(outcome)
}
pub fn set_health(&mut self, value: f32, absorption: bool) -> Result<()> {
@@ -363,6 +389,45 @@ pub struct Blocking {
/// Angle between the head's view and the source position (pi without one).
pub angle: f64,
}
/// How many hurts `PlayerMotion::hurt_log` keeps.
const HURT_LOG: usize = 64;
/// One hurt as it was taken: what came in, what a raised blocking item
/// stopped, the armour it met and what health it left.
#[derive(Clone, Debug, PartialEq)]
pub struct HurtRecord {
pub game_time: i64,
/// The damage type.
pub source: String,
/// The damage before blocking, armour and effects.
pub amount: f32,
pub blocked: f32,
/// Health and absorption lost.
pub taken: f32,
pub hurt: bool,
/// `minecraft:armor` and `minecraft:armor_toughness` when it came.
pub armor: f32,
pub toughness: f32,
pub health: f32,
/// The fire ticks before the hurt.
pub fire_ticks: i32,
/// The push the hurt gave, if the caller applied one.
pub knockback: Option<[f64; 3]>,
}
/// One melee attack on a mob: the damage after the attack strength, the
/// strength (`getAttackStrengthScale`), and the health it took.
#[derive(Clone, Debug, PartialEq)]
pub struct AttackRecord {
pub game_time: i64,
pub target: String,
/// The mob's entity id, and whether the blow killed it.
pub id: u32,
pub killed: bool,
pub amount: f32,
pub scale: f32,
pub crit: bool,
pub landed: bool,
pub dealt: f32,
}
/// The outcome of a hurt with a raised blocking item.
#[derive(Clone, Copy, Debug, Default)]
pub struct BlockOutcome {
+8
View File
@@ -122,6 +122,13 @@ pub struct Guest {
pub tracker: Tracker,
// Player state the gameplay code reads and writes.
pub motion: Option<Box<PlayerMotion>>,
/// The hurt and attack logs of the body it had before it respawned,
/// until the new body takes them over (the control socket's `hurtlog`
/// reads across a death).
pub logs_kept: Option<(
Vec<crate::health::HurtRecord>,
Vec<crate::health::AttackRecord>,
)>,
/// The 36 inventory slots (hotbar first), armor (head..feet) and off hand.
pub inventory: Vec<Option<ItemStack>>,
pub armor: [Option<ItemStack>; 4],
@@ -251,6 +258,7 @@ impl Guest {
batch_quota: 0.0,
tracker: Tracker::default(),
motion: None,
logs_kept: None,
inventory: vec![None; 36],
armor: [None, None, None, None],
offhand: None,
+7
View File
@@ -290,6 +290,11 @@ pub struct PlayerMotion {
/// The powder snow blocks this tick's movement went into: the game
/// destroys them if the player is burning, then puts the fire out.
pub powder_snow_entered: Vec<BlockPos>,
/// The last hurts taken (`hurt_blocking`), newest last, for tests and
/// the control socket's `hurtlog`.
pub hurt_log: Vec<crate::health::HurtRecord>,
/// The last melee attacks made on mobs, newest last.
pub attack_log: Vec<crate::health::AttackRecord>,
}
fn attribute_error(error: impl std::fmt::Debug) -> io::Error {
io::Error::other(format!("movement attribute: {error:?}"))
@@ -434,6 +439,8 @@ impl PlayerMotion {
old_position: position,
block_effects: true,
powder_snow_entered: Vec::new(),
hurt_log: Vec::new(),
attack_log: Vec::new(),
})
}
/// `Entity.getTicksRequiredToFreeze`.