diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 670fcdf61..a25e8fdeb 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -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); diff --git a/crates/horizon-client/src/game/control.rs b/crates/horizon-client/src/game/control.rs index bf732eb12..6c69904ac 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", @@ -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, + attack_log: &mut Vec, + ) -> Value { + let hurts: Vec = 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 = 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!({ diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 958f7229c..218d9e85c 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/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 765184491..1c38f9542 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]; @@ -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, 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/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, 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`.