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 <noreply@anthropic.com>
(cherry picked from commit fdfa952379cbed89db1b46077a4ce11b99083e5a)
This commit is contained in:
Ludovic Marie
2026-10-21 09:15:17 +02:00
parent b31cb42242
commit 33183952d5
5 changed files with 143 additions and 1 deletions
+18 -1
View File
@@ -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);
+48
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",
@@ -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<Value> = 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<Value> = 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!({
@@ -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);
}
+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 {
+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`.