feat(combat): Phase E.4 AttackTargetRequest + combat notification pipeline

Completes the client-side combat loop: send attacks, receive server's
damage broadcasts, maintain per-entity health state for HP bars +
damage floaters. All atop Phase F.1's GameEvent dispatcher.

Wire layer:
- AttackTargetRequest (0x0008 C→S, inside 0xF7B1): targetGuid +
  powerLevel + accuracyLevel + attackHeight. 28-byte body.
- GameEvents parsers for all combat notifications from r08 §4:
  - VictimNotification (0x01AC) — you got hit, full details
  - KillerNotification (0x01AD) — you killed X
  - AttackerNotification (0x01B1) — you hit X for Y (damage%)
  - DefenderNotification (0x01B2) — X hit you
  - EvasionAttackerNotification (0x01B3) — X evaded
  - EvasionDefenderNotification (0x01B4) — you evaded X
  - AttackDone (0x01A7) — attack sequence completed

Core layer:
- CombatState: per-entity health-percent cache + typed events
  (HealthChanged, DamageTaken, DamageDealtAccepted, EvadedIncoming,
  MissedOutgoing, AttackDone). Each event carries enough detail for
  the UI to render damage floaters, HP bars, and a combat log panel.
  Server is authoritative; client only mirrors state.

The server computes damage (armor, resist, crit, hit-chance); the
client only displays results. Predictive UI like "estimated damage
at 0.75 power" still works via the existing CombatMath helper class
that was in the scaffold (r02 §5 formulas).

Tests (13 new):
- AttackTargetRequest byte-exact wire encoding
- VictimNotification / AttackerNotification / EvasionAttacker /
  AttackDone round-trip parse.
- CombatState: UpdateHealth caches + fires, Victim fires DamageTaken,
  Attacker fires DamageDealt, Evasion routes to right event, AttackDone
  carries sequence+error, Clear resets cache.

Build green, 544 tests pass (up from 532).

Ref: r02 §7 (wire formats), r08 §4 (event payloads), ACE
GameEvent*Notification.cs families.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Erik 2026-04-18 16:58:14 +02:00
parent 2561f5599f
commit 2e3f9d7a04
5 changed files with 516 additions and 0 deletions

View file

@ -0,0 +1,62 @@
using System.Buffers.Binary;
namespace AcDream.Core.Net.Messages;
/// <summary>
/// Outbound <c>0x0008 AttackTargetRequest</c> GameAction.
///
/// <para>
/// Wire layout (inside the <c>0xF7B1</c> GameAction envelope):
/// <code>
/// u32 0xF7B1 // GameAction envelope opcode
/// u32 gameActionSequence // client sequence
/// u32 0x0008 // sub-opcode
/// u32 targetGuid // who to attack
/// f32 powerLevel // [0.0, 1.0] — the power bar position
/// f32 accuracyLevel // [0.0, 1.0] — for missile weapons
/// u32 attackHeight // 1=High, 2=Medium, 3=Low
/// </code>
/// </para>
///
/// <para>
/// The server ALREADY knows the attacker (it's the session's player),
/// so this message only carries the target + attack params. The server
/// then rolls damage, picks a body part, and broadcasts
/// <see cref="GameEventType.VictimNotification"/> / AttackerNotification
/// / DefenderNotification / EvasionAttackerNotification /
/// EvasionDefenderNotification with the result.
/// </para>
///
/// <para>
/// References: r02 §7 (wire format), r08 §3 opcode 0x0008.
/// </para>
/// </summary>
public static class AttackTargetRequest
{
public const uint GameActionEnvelope = 0xF7B1u;
public const uint SubOpcode = 0x0008u;
/// <summary>
/// Build the wire body for an attack request.
/// </summary>
/// <param name="powerLevel">[0..1] melee power bar position.</param>
/// <param name="accuracyLevel">[0..1] missile accuracy bar position; pass 0 for melee.</param>
/// <param name="attackHeight">1=High, 2=Medium, 3=Low.</param>
public static byte[] Build(
uint gameActionSequence,
uint targetGuid,
float powerLevel,
float accuracyLevel,
uint attackHeight)
{
byte[] body = new byte[28];
BinaryPrimitives.WriteUInt32LittleEndian(body, GameActionEnvelope);
BinaryPrimitives.WriteUInt32LittleEndian(body.AsSpan(4), gameActionSequence);
BinaryPrimitives.WriteUInt32LittleEndian(body.AsSpan(8), SubOpcode);
BinaryPrimitives.WriteUInt32LittleEndian(body.AsSpan(12), targetGuid);
BinaryPrimitives.WriteSingleLittleEndian(body.AsSpan(16), powerLevel);
BinaryPrimitives.WriteSingleLittleEndian(body.AsSpan(20), accuracyLevel);
BinaryPrimitives.WriteUInt32LittleEndian(body.AsSpan(24), attackHeight);
return body;
}
}

View file

@ -145,6 +145,125 @@ public static class GameEvents
return BinaryPrimitives.ReadUInt32LittleEndian(payload);
}
// ── Combat notifications ────────────────────────────────────────────────
/// <summary>0x01AC VictimNotification — "you got hit for X".</summary>
public readonly record struct VictimNotification(
string AttackerName,
uint AttackerGuid,
uint DamageType,
uint Damage,
uint HitQuadrant,
uint Critical,
uint AttackType);
public static VictimNotification? ParseVictimNotification(ReadOnlySpan<byte> payload)
{
int pos = 0;
try
{
string name = ReadString16L(payload, ref pos);
if (payload.Length - pos < 24) return null;
uint guid = BinaryPrimitives.ReadUInt32LittleEndian(payload.Slice(pos)); pos += 4;
uint damageType = BinaryPrimitives.ReadUInt32LittleEndian(payload.Slice(pos)); pos += 4;
uint damage = BinaryPrimitives.ReadUInt32LittleEndian(payload.Slice(pos)); pos += 4;
uint quad = BinaryPrimitives.ReadUInt32LittleEndian(payload.Slice(pos)); pos += 4;
uint crit = BinaryPrimitives.ReadUInt32LittleEndian(payload.Slice(pos)); pos += 4;
uint atkType = BinaryPrimitives.ReadUInt32LittleEndian(payload.Slice(pos)); pos += 4;
return new VictimNotification(name, guid, damageType, damage, quad, crit, atkType);
}
catch { return null; }
}
/// <summary>0x01AD KillerNotification — "you killed X".</summary>
public readonly record struct KillerNotification(string VictimName, uint VictimGuid);
public static KillerNotification? ParseKillerNotification(ReadOnlySpan<byte> payload)
{
int pos = 0;
try
{
string name = ReadString16L(payload, ref pos);
if (payload.Length - pos < 4) return null;
uint guid = BinaryPrimitives.ReadUInt32LittleEndian(payload.Slice(pos));
return new KillerNotification(name, guid);
}
catch { return null; }
}
/// <summary>0x01B1 AttackerNotification — "you hit X for Y%".</summary>
public readonly record struct AttackerNotification(
string DefenderName,
uint DamageType,
uint Damage,
float DamagePercent);
public static AttackerNotification? ParseAttackerNotification(ReadOnlySpan<byte> payload)
{
int pos = 0;
try
{
string name = ReadString16L(payload, ref pos);
if (payload.Length - pos < 12) return null;
uint damageType = BinaryPrimitives.ReadUInt32LittleEndian(payload.Slice(pos)); pos += 4;
uint damage = BinaryPrimitives.ReadUInt32LittleEndian(payload.Slice(pos)); pos += 4;
float pct = BinaryPrimitives.ReadSingleLittleEndian(payload.Slice(pos)); pos += 4;
return new AttackerNotification(name, damageType, damage, pct);
}
catch { return null; }
}
/// <summary>0x01B2 DefenderNotification — "X hit you for Y".</summary>
public readonly record struct DefenderNotification(
string AttackerName,
uint AttackerGuid,
uint DamageType,
uint Damage,
uint HitQuadrant,
uint Critical);
public static DefenderNotification? ParseDefenderNotification(ReadOnlySpan<byte> payload)
{
int pos = 0;
try
{
string name = ReadString16L(payload, ref pos);
if (payload.Length - pos < 20) return null;
uint guid = BinaryPrimitives.ReadUInt32LittleEndian(payload.Slice(pos)); pos += 4;
uint dtype = BinaryPrimitives.ReadUInt32LittleEndian(payload.Slice(pos)); pos += 4;
uint dmg = BinaryPrimitives.ReadUInt32LittleEndian(payload.Slice(pos)); pos += 4;
uint quad = BinaryPrimitives.ReadUInt32LittleEndian(payload.Slice(pos)); pos += 4;
uint crit = BinaryPrimitives.ReadUInt32LittleEndian(payload.Slice(pos)); pos += 4;
return new DefenderNotification(name, guid, dtype, dmg, quad, crit);
}
catch { return null; }
}
/// <summary>0x01B3 EvasionAttackerNotification — "X evaded".</summary>
public static string? ParseEvasionAttackerNotification(ReadOnlySpan<byte> payload)
{
int pos = 0;
try { return ReadString16L(payload, ref pos); } catch { return null; }
}
/// <summary>0x01B4 EvasionDefenderNotification — "you evaded X".</summary>
public static string? ParseEvasionDefenderNotification(ReadOnlySpan<byte> payload)
{
int pos = 0;
try { return ReadString16L(payload, ref pos); } catch { return null; }
}
/// <summary>0x01A7 AttackDone — (attackSequence, weenieError).</summary>
public readonly record struct AttackDone(uint AttackSequence, uint WeenieError);
public static AttackDone? ParseAttackDone(ReadOnlySpan<byte> payload)
{
if (payload.Length < 8) return null;
return new AttackDone(
BinaryPrimitives.ReadUInt32LittleEndian(payload),
BinaryPrimitives.ReadUInt32LittleEndian(payload.Slice(4)));
}
// ── Appraise / identify ─────────────────────────────────────────────────
/// <summary>0x00C9 IdentifyObjectResponse header.</summary>

View file

@ -0,0 +1,126 @@
using System;
using System.Collections.Concurrent;
namespace AcDream.Core.Combat;
/// <summary>
/// Client-side combat state — tracks per-entity health percent and
/// emits typed events when UpdateHealth / Victim / Attacker / Defender
/// notifications arrive. Powers target HP bars, damage floaters, combat
/// log panel.
///
/// <para>
/// Retail client-side combat responsibilities (r02 §7):
/// <list type="bullet">
/// <item><description>
/// Maintain a cache of "last known health percent" per entity guid.
/// UpdateHealth (0x01C0) is sent when the player queries or the
/// server broadcasts a change.
/// </description></item>
/// <item><description>
/// Convert raw damage events into UI-ready notifications (colored
/// floating numbers, "Critical!" flashes, body-part locations).
/// </description></item>
/// <item><description>
/// Track self-centered notifications (you hit / you got hit / you
/// evaded / you were evaded) so the log panel can format them
/// correctly.
/// </description></item>
/// </list>
/// </para>
///
/// <para>
/// The server is authoritative: this class does NOT simulate damage
/// locally (exception: a predictive-ish "estimated damage" display for
/// the attack bar UI, which can use <see cref="CombatMath"/>).
/// </para>
/// </summary>
public sealed class CombatState
{
private readonly ConcurrentDictionary<uint, float> _healthByGuid = new();
/// <summary>Fires when a target's health percent changes (from UpdateHealth).</summary>
public event Action<uint /*guid*/, float /*percent*/>? HealthChanged;
/// <summary>You (the player) got hit for some damage.</summary>
public event Action<DamageIncoming>? DamageTaken;
/// <summary>You (the player) dealt some damage.</summary>
public event Action<DamageDealt>? DamageDealtAccepted;
/// <summary>You (the player) evaded an incoming hit.</summary>
public event Action<string>? EvadedIncoming;
/// <summary>The target evaded your hit.</summary>
public event Action<string>? MissedOutgoing;
/// <summary>An attack commit completed (0x01A7). WeenieError = 0 on success.</summary>
public event Action<uint /*attackSeq*/, uint /*weenieError*/>? AttackDone;
public readonly record struct DamageIncoming(
string AttackerName,
uint AttackerGuid,
uint DamageType,
uint Damage,
uint HitQuadrant,
bool Critical,
uint AttackType);
public readonly record struct DamageDealt(
string DefenderName,
uint DamageType,
uint Damage,
float DamagePercent);
/// <summary>Retrieve last known health percent for a guid, or 1.0 if unknown.</summary>
public float GetHealthPercent(uint guid) =>
_healthByGuid.TryGetValue(guid, out var pct) ? pct : 1f;
public int TrackedTargetCount => _healthByGuid.Count;
// ── Inbound handlers (wired from WorldSession.GameEvents) ────────────────
public void OnUpdateHealth(uint targetGuid, float healthPercent)
{
_healthByGuid[targetGuid] = healthPercent;
HealthChanged?.Invoke(targetGuid, healthPercent);
}
public void OnVictimNotification(
string attackerName, uint attackerGuid, uint damageType, uint damage,
uint hitQuadrant, uint critical, uint attackType)
{
DamageTaken?.Invoke(new DamageIncoming(
attackerName, attackerGuid, damageType, damage, hitQuadrant,
critical != 0, attackType));
}
public void OnDefenderNotification(
string attackerName, uint attackerGuid, uint damageType, uint damage,
uint hitQuadrant, uint critical)
{
// DefenderNotification is semantically the same as VictimNotification
// from the defender's POV — the client log merges them.
DamageTaken?.Invoke(new DamageIncoming(
attackerName, attackerGuid, damageType, damage, hitQuadrant,
critical != 0, 0));
}
public void OnAttackerNotification(
string defenderName, uint damageType, uint damage, float damagePercent)
{
DamageDealtAccepted?.Invoke(new DamageDealt(
defenderName, damageType, damage, damagePercent));
}
public void OnEvasionAttackerNotification(string defenderName)
=> MissedOutgoing?.Invoke(defenderName);
public void OnEvasionDefenderNotification(string attackerName)
=> EvadedIncoming?.Invoke(attackerName);
public void OnAttackDone(uint attackSequence, uint weenieError)
=> AttackDone?.Invoke(attackSequence, weenieError);
public void Clear() => _healthByGuid.Clear();
}

View file

@ -0,0 +1,112 @@
using System;
using System.Buffers.Binary;
using System.Text;
using AcDream.Core.Net.Messages;
using Xunit;
namespace AcDream.Core.Net.Tests.Messages;
public sealed class CombatEventTests
{
private static byte[] MakeString16L(string s)
{
byte[] data = Encoding.ASCII.GetBytes(s);
int recordSize = 2 + data.Length;
int padding = (4 - (recordSize & 3)) & 3;
byte[] result = new byte[recordSize + padding];
BinaryPrimitives.WriteUInt16LittleEndian(result, (ushort)data.Length);
Array.Copy(data, 0, result, 2, data.Length);
return result;
}
[Fact]
public void AttackTargetRequest_Build_EmitsCorrectWireBytes()
{
byte[] body = AttackTargetRequest.Build(
gameActionSequence: 3,
targetGuid: 0x12345678u,
powerLevel: 0.75f,
accuracyLevel: 0.5f,
attackHeight: 2);
Assert.Equal(28, body.Length);
Assert.Equal(AttackTargetRequest.GameActionEnvelope,
BinaryPrimitives.ReadUInt32LittleEndian(body));
Assert.Equal(3u,
BinaryPrimitives.ReadUInt32LittleEndian(body.AsSpan(4)));
Assert.Equal(AttackTargetRequest.SubOpcode,
BinaryPrimitives.ReadUInt32LittleEndian(body.AsSpan(8)));
Assert.Equal(0x12345678u,
BinaryPrimitives.ReadUInt32LittleEndian(body.AsSpan(12)));
Assert.Equal(0.75f,
BinaryPrimitives.ReadSingleLittleEndian(body.AsSpan(16)), 4);
Assert.Equal(0.5f,
BinaryPrimitives.ReadSingleLittleEndian(body.AsSpan(20)), 4);
Assert.Equal(2u,
BinaryPrimitives.ReadUInt32LittleEndian(body.AsSpan(24)));
}
[Fact]
public void ParseVictimNotification_RoundTrip()
{
byte[] name = MakeString16L("Attacker");
byte[] tail = new byte[24];
BinaryPrimitives.WriteUInt32LittleEndian(tail, 0xAAu); // guid
BinaryPrimitives.WriteUInt32LittleEndian(tail.AsSpan(4), 1u); // damageType
BinaryPrimitives.WriteUInt32LittleEndian(tail.AsSpan(8), 42u); // damage
BinaryPrimitives.WriteUInt32LittleEndian(tail.AsSpan(12), 3u); // quadrant
BinaryPrimitives.WriteUInt32LittleEndian(tail.AsSpan(16), 1u); // crit
BinaryPrimitives.WriteUInt32LittleEndian(tail.AsSpan(20), 8u); // attackType
byte[] payload = new byte[name.Length + tail.Length];
Buffer.BlockCopy(name, 0, payload, 0, name.Length);
Buffer.BlockCopy(tail, 0, payload, name.Length, tail.Length);
var parsed = GameEvents.ParseVictimNotification(payload);
Assert.NotNull(parsed);
Assert.Equal("Attacker", parsed!.Value.AttackerName);
Assert.Equal(0xAAu, parsed.Value.AttackerGuid);
Assert.Equal(42u, parsed.Value.Damage);
Assert.Equal(1u, parsed.Value.Critical);
}
[Fact]
public void ParseAttackerNotification_RoundTrip()
{
byte[] name = MakeString16L("Drudge");
byte[] tail = new byte[12];
BinaryPrimitives.WriteUInt32LittleEndian(tail, 1u); // damageType
BinaryPrimitives.WriteUInt32LittleEndian(tail.AsSpan(4), 30u); // damage
BinaryPrimitives.WriteSingleLittleEndian(tail.AsSpan(8), 0.15f); // percent
byte[] payload = new byte[name.Length + tail.Length];
Buffer.BlockCopy(name, 0, payload, 0, name.Length);
Buffer.BlockCopy(tail, 0, payload, name.Length, tail.Length);
var parsed = GameEvents.ParseAttackerNotification(payload);
Assert.NotNull(parsed);
Assert.Equal("Drudge", parsed!.Value.DefenderName);
Assert.Equal(30u, parsed.Value.Damage);
Assert.Equal(0.15f, parsed.Value.DamagePercent, 4);
}
[Fact]
public void ParseEvasionAttackerNotification_RoundTrip()
{
byte[] payload = MakeString16L("Thrower");
Assert.Equal("Thrower", GameEvents.ParseEvasionAttackerNotification(payload));
}
[Fact]
public void ParseAttackDone_RoundTrip()
{
byte[] payload = new byte[8];
BinaryPrimitives.WriteUInt32LittleEndian(payload, 42u);
BinaryPrimitives.WriteUInt32LittleEndian(payload.AsSpan(4), 0u); // no error
var parsed = GameEvents.ParseAttackDone(payload);
Assert.NotNull(parsed);
Assert.Equal(42u, parsed!.Value.AttackSequence);
Assert.Equal(0u, parsed.Value.WeenieError);
}
}

View file

@ -0,0 +1,97 @@
using AcDream.Core.Combat;
using Xunit;
namespace AcDream.Core.Tests.Combat;
public sealed class CombatStateTests
{
[Fact]
public void OnUpdateHealth_CachesPercent_AndFiresEvent()
{
var state = new CombatState();
uint seenGuid = 0;
float seenPct = 0;
state.HealthChanged += (g, p) => { seenGuid = g; seenPct = p; };
state.OnUpdateHealth(0xBEEF, 0.33f);
Assert.Equal(0xBEEFu, seenGuid);
Assert.Equal(0.33f, seenPct, 4);
Assert.Equal(0.33f, state.GetHealthPercent(0xBEEF), 4);
}
[Fact]
public void GetHealthPercent_Unknown_ReturnsFullHealth()
{
var state = new CombatState();
Assert.Equal(1f, state.GetHealthPercent(0xDEAD));
}
[Fact]
public void OnVictimNotification_FiresDamageTaken()
{
var state = new CombatState();
CombatState.DamageIncoming seen = default;
state.DamageTaken += d => seen = d;
state.OnVictimNotification("Drudge", 0xAA, 1, 42, 3, 1, 8);
Assert.Equal("Drudge", seen.AttackerName);
Assert.Equal(42u, seen.Damage);
Assert.True(seen.Critical);
}
[Fact]
public void OnAttackerNotification_FiresDamageDealt()
{
var state = new CombatState();
CombatState.DamageDealt seen = default;
state.DamageDealtAccepted += d => seen = d;
state.OnAttackerNotification("Drudge", 1, 30, 0.15f);
Assert.Equal("Drudge", seen.DefenderName);
Assert.Equal(30u, seen.Damage);
Assert.Equal(0.15f, seen.DamagePercent, 4);
}
[Fact]
public void OnEvasionNotification_FiresCorrectEvent()
{
var state = new CombatState();
string? evaded = null, missed = null;
state.EvadedIncoming += a => evaded = a;
state.MissedOutgoing += d => missed = d;
state.OnEvasionDefenderNotification("Rat"); // you evaded rat
state.OnEvasionAttackerNotification("Tusker"); // tusker evaded you
Assert.Equal("Rat", evaded);
Assert.Equal("Tusker", missed);
}
[Fact]
public void OnAttackDone_FiresAttackDone()
{
var state = new CombatState();
uint seenSeq = 0, seenErr = 999;
state.AttackDone += (s, e) => { seenSeq = s; seenErr = e; };
state.OnAttackDone(5, 0);
Assert.Equal(5u, seenSeq);
Assert.Equal(0u, seenErr);
}
[Fact]
public void Clear_ResetsHealthCache()
{
var state = new CombatState();
state.OnUpdateHealth(1, 0.5f);
state.OnUpdateHealth(2, 0.8f);
state.Clear();
Assert.Equal(1f, state.GetHealthPercent(1)); // back to default
Assert.Equal(0, state.TrackedTargetCount);
}
}