From 33183952d54524a91c7c2bcdae1639268bc20602 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Wed, 21 Oct 2026 09:15:17 +0200 Subject: [PATCH 1/2] Hurt and attack logs per player, read with `hurtlog` on the control socket Every hurt a player takes (PlayerMotion::hurt_blocking, which all player damage goes through) is kept: the damage type, the amount before blocking and armour, what a raised blocking item stopped, the health and absorption lost, the armour and armour toughness it met, the fire ticks, the health left and, for a mob's blow, the knockback it gave. Every melee blow on a mob is kept too: the damage after the attack strength (getAttackStrengthScale), the strength, a critical hit, the mob, whether it landed and killed. The last 64 of each are kept. `hurtlog` on the control socket hands them out, per player (the host and each LAN guest), and clears them: a harness can read what a fight did hit by hit and compare it with vanilla's numbers. Co-Authored-By: Claude Opus 5.5 (cherry picked from commit fdfa952379cbed89db1b46077a4ce11b99083e5a) --- crates/horizon-client/src/game.rs | 19 +++++- crates/horizon-client/src/game/control.rs | 48 +++++++++++++++ crates/horizon-client/src/game/creatures.rs | 5 ++ crates/horizon-client/src/health.rs | 65 +++++++++++++++++++++ crates/horizon-client/src/motion.rs | 7 +++ 5 files changed, 143 insertions(+), 1 deletion(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 460899bff..b1cac5ee7 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -6808,7 +6808,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); diff --git a/crates/horizon-client/src/game/control.rs b/crates/horizon-client/src/game/control.rs index 5cdf46110..c6c0e2225 100644 --- a/crates/horizon-client/src/game/control.rs +++ b/crates/horizon-client/src/game/control.rs @@ -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", @@ -450,6 +451,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; @@ -707,6 +712,49 @@ 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, p: &mut horizon_client::motion::PlayerMotion) -> Value { + let hurts: Vec = p + .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 = p + .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", p)); + } + 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, m)); + } + } + } + json!({ "ok": true, "players": players }) + } + fn control_status(&self) -> Value { let player = self.player.as_ref().map(|p| { json!({ diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 113558e34..d1645c087 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -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); } diff --git a/crates/horizon-client/src/health.rs b/crates/horizon-client/src/health.rs index 493488732..442a826f0 100644 --- a/crates/horizon-client/src/health.rs +++ b/crates/horizon-client/src/health.rs @@ -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 { diff --git a/crates/horizon-client/src/motion.rs b/crates/horizon-client/src/motion.rs index a2fbd47ba..b25915085 100644 --- a/crates/horizon-client/src/motion.rs +++ b/crates/horizon-client/src/motion.rs @@ -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, + /// The last hurts taken (`hurt_blocking`), newest last, for tests and + /// the control socket's `hurtlog`. + pub hurt_log: Vec, + /// The last melee attacks made on mobs, newest last. + pub attack_log: Vec, } 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`. From 169be6374c43ede203f7b0eb97eeebc4b5f68fcc Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Wed, 21 Oct 2026 12:09:18 +0200 Subject: [PATCH 2/2] hurtlog keeps a LAN guest's hurts across a respawn A guest's body is rebuilt when it respawns, and its hurt and attack logs went with the old one: the control socket's hurtlog lost the hurts that killed it whenever the death and the respawn fell between two reads. The logs now pass to the new body, and a dead guest's are read too. Co-Authored-By: Claude Opus 5.5 (cherry picked from commit df9fa5f0185766f7a7338df5046200d1a2e67d22) --- crates/horizon-client/src/game/control.rs | 19 ++++++++++++------- crates/horizon-client/src/game/lan_actions.rs | 10 +++++++++- crates/horizon-client/src/lan/guest.rs | 8 ++++++++ 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/crates/horizon-client/src/game/control.rs b/crates/horizon-client/src/game/control.rs index c6c0e2225..bb1728ad3 100644 --- a/crates/horizon-client/src/game/control.rs +++ b/crates/horizon-client/src/game/control.rs @@ -715,9 +715,12 @@ 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, p: &mut horizon_client::motion::PlayerMotion) -> Value { - let hurts: Vec = p - .hurt_log + fn drain( + name: &str, + hurt_log: &mut Vec, + attack_log: &mut Vec, + ) -> Value { + let hurts: Vec = hurt_log .drain(..) .map(|r| { json!({ @@ -728,8 +731,7 @@ impl Game<'_> { }) }) .collect(); - let attacks: Vec = p - .attack_log + let attacks: Vec = attack_log .drain(..) .map(|r| { json!({ @@ -742,13 +744,16 @@ impl Game<'_> { } let mut players = Vec::new(); if let Some(p) = self.player.as_mut() { - players.push(drain("host", p)); + 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, m)); + 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)); } } } diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index af127f42a..34fb88be5 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -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]; @@ -1336,7 +1341,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, diff --git a/crates/horizon-client/src/lan/guest.rs b/crates/horizon-client/src/lan/guest.rs index 91bf2709f..f46e37abc 100644 --- a/crates/horizon-client/src/lan/guest.rs +++ b/crates/horizon-client/src/lan/guest.rs @@ -122,6 +122,13 @@ pub struct Guest { pub tracker: Tracker, // Player state the gameplay code reads and writes. pub motion: Option>, + /// 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, + Vec, + )>, /// The 36 inventory slots (hotbar first), armor (head..feet) and off hand. pub inventory: Vec>, pub armor: [Option; 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,