using RynthCore.Engine.Compatibility; using RynthCore.Engine.ImGuiBackend.Hud; namespace CleaveGhostTests; internal static class Program { private static int _checks, _failed; private static string _case = ""; private static int Main() { Run("cleave: the second mob a Tetsubo hits loses its share", CleaveOntoUnfedPlate); Run("cleave: a hit the server already reported (queried target) changes nothing", ServerAlreadyReported); Run("cleave: a plate with a live server feed is left to the server", LiveFeedLeftAlone); Run("cleave: never a kill, never a bad share, nothing to match", Bounds); Run("cleave: an earlier estimate counts down further", EstimateChains); Run("freshness: object messages are noted, a delete forgets, others are ignored", Freshness); Run("health probe: fight plates first, then never-read, then old quiet ones; never the stream's owner", ProbeFightFirst); Run("health probe: the stalest fight plate first, never-read plates nearest first", ProbeStalestAndNearest); Run("health probe: nothing due (fresh, dead, no id) sends nothing", ProbeNothingDue); Run("health probe: a probe's replies are the HUD's; the owner's and plugins' are theirs", ProbeReplies); Run("plugin reading (v93): the server's last ratio and its age; never an estimate", PluginReadingServerOnly); Run("plugin reading (v93): a probe reply is recorded for plugins to read, while it still isn't theirs as an event", PluginReadingProbeReply); Run("plugin reading (v93): safe to read from another thread while the game thread records", PluginReadingThreads); Console.WriteLine($"{_checks} check(s), {_failed} failed."); return _failed == 0 ? 0 : 1; } private static void Run(string name, Action test) { _case = name; int before = _failed; try { test(); } catch (Exception ex) { Fail($"threw {ex.GetType().Name}: {ex.Message}"); } Console.WriteLine($"[{(_failed == before ? "PASS" : "FAIL")}] {name}"); } private static void Check(bool ok, string what) { _checks++; if (!ok) Fail(what); } private static void Fail(string what) { _failed++; Console.WriteLine($" FAIL {_case}: {what}"); } private static bool Near(float a, float b) => Math.Abs(a - b) < 0.0005f; private const float Share204 = 204f / 2300f; // 204 on an Olthoi Slasher (2300 hp): 0.089 private static void CleaveOntoUnfedPlate() { // The queried target (0x10) got its 0x01C0 for its own hit; the second mob (0x20, 1.6 m) // and a third further off (0x30, 4 m) have no feed. CleaveHealth.Candidate[] c = [ new(0x10, 1.4f, true, 0.60f, 0.70f, false, -0.05), // queried target: its own 0x01C0 for its own (bigger) hit new(0x20, 1.6f, false, 1f, -1f, false, 0), new(0x30, 4.0f, true, 0.80f, -1f, true, 3.0), // an older estimate ]; uint id = CleaveHealth.Pick(c, Share204, out float ratio); Check(id == 0x20, $"nearest unfed plate takes it (got 0x{id:X})"); Check(Near(ratio, 1f - Share204), $"full -> {1f - Share204:0.000} (got {ratio:0.000})"); } private static void ServerAlreadyReported() { // Same name, the queried target's 0x01C0 dropped it by the same share 0.1 s after the hit. CleaveHealth.Candidate[] c = [ new(0x10, 1.4f, true, 0.70f - Share204, 0.70f, false, -0.1), new(0x20, 1.6f, false, 1f, -1f, false, 0), ]; Check(CleaveHealth.Pick(c, Share204, out _) == 0, "the server's own update was this hit: no estimate anywhere"); } private static void LiveFeedLeftAlone() { // The only plate with the name has a live feed (updated 0.5 s before) but the drop doesn't match. CleaveHealth.Candidate[] c = [new(0x10, 1.4f, true, 0.5f, 0.52f, false, 0.5)]; Check(CleaveHealth.Pick(c, Share204, out _) == 0, "live-fed plate: no estimate"); // An old feed (20 s) is not live: the plate takes the hit. CleaveHealth.Candidate[] old = [new(0x10, 1.4f, true, 0.5f, 0.52f, false, 20)]; Check(CleaveHealth.Pick(old, Share204, out float r) == 0x10 && Near(r, 0.5f - Share204), "stale feed: estimate from its last ratio"); } private static void Bounds() { CleaveHealth.Candidate[] c = [new(0x20, 1.6f, true, 0.05f, -1f, true, 2)]; Check(CleaveHealth.Pick(c, 0.25f, out float r) == 0x20 && Near(r, CleaveHealth.MinRatio), "never below MinRatio (a kill comes from the kill message)"); Check(CleaveHealth.Pick(c, 0f, out _) == 0, "no share: nothing"); Check(CleaveHealth.Pick(c, float.NaN, out _) == 0, "NaN share: nothing"); Check(CleaveHealth.Pick(c, 3f, out _) == 0, "an impossible share: nothing"); Check(CleaveHealth.Pick(ReadOnlySpan.Empty, Share204, out _) == 0, "no plate with the name: nothing"); } private static void EstimateChains() { // Two hits on the same unfed mob: the second starts from the first estimate (estimates are never "live feed"). CleaveHealth.Candidate[] first = [new(0x20, 1.6f, false, 1f, -1f, false, 0)]; CleaveHealth.Pick(first, Share204, out float r1); CleaveHealth.Candidate[] second = [new(0x20, 1.6f, true, r1, 1f, true, 1.2)]; Check(CleaveHealth.Pick(second, Share204, out float r2) == 0x20 && Near(r2, 1f - 2 * Share204), $"second hit: {r2:0.000}"); } private static void Freshness() { ObjectFreshness.Clear(); Check(ObjectFreshness.AgeMs(0x800B87E3, 1000) == -1, "never heard of: -1"); ObjectFreshness.Note(0xF74C, 0x800B87E3, 1000); // UpdateMotion Check(ObjectFreshness.AgeMs(0x800B87E3, 4000) == 3000, "3 s since its motion update"); ObjectFreshness.Note(0xF748, 0x800B87E3, 5000); // UpdatePosition Check(ObjectFreshness.AgeMs(0x800B87E3, 5200) == 200, "position update refreshes it"); ObjectFreshness.Note(0xF7B0, 0x800B87E3, 9000); // a game event: not about the object Check(ObjectFreshness.AgeMs(0x800B87E3, 9000) == 4000, "game events don't count"); ObjectFreshness.Note(0xF747, 0x800B87E3, 9500); // DeleteObject Check(ObjectFreshness.AgeMs(0x800B87E3, 9600) == -1, "deleted: forgotten"); ObjectFreshness.Note(0xF745, 0x800B87E3, 10000); // CreateObject again Check(ObjectFreshness.AgeMs(0x800B87E3, 10000) == 0, "created again: fresh"); Check(ObjectFreshness.IsObjectMessage(0xF74E) && ObjectFreshness.IsObjectMessage(0xF7DB) && !ObjectFreshness.IsObjectMessage(0xF7E0), "vector/update-object count, server text doesn't"); ObjectFreshness.Note(0xF74C, 0, 1); Check(ObjectFreshness.AgeMs(0, 2) == -1, "id 0 ignored"); } // ── HealthProbe: the unselected plates' health ────────────────────── private static void ProbeFightFirst() { // 0x10 is the selection (streamed). 0x20 is in the fight, last read 3 s ago; 0x30 never read; // 0x40 quiet at full health, read 20 s ago. HealthProbe.Candidate[] c = [ new(0x10, 1.0f, true, 5.0, true), new(0x20, 4.0f, true, 3.0, true), new(0x30, 2.0f, false, 0, false), new(0x40, 1.5f, true, 20.0, false), ]; Check(HealthProbe.Pick(c, owner: 0x10) == 0x20, "a fight plate the server isn't streaming goes first"); Check(HealthProbe.Pick(c, owner: 0x20) == 0x10, "the stream's owner is never probed (the selection is, once it isn't the owner)"); HealthProbe.Candidate[] noFight = [c[2], c[3]]; Check(HealthProbe.Pick(noFight, owner: 0x10) == 0x30, "then a plate never read"); HealthProbe.Candidate[] quiet = [c[3]]; Check(HealthProbe.Pick(quiet, owner: 0x10) == 0x40, "then a quiet plate whose reading is old"); } private static void ProbeStalestAndNearest() { HealthProbe.Candidate[] fight = [ new(0x20, 1.0f, true, 2.5, true), new(0x21, 9.0f, true, 6.0, true), new(0x22, 3.0f, false, 0, true), // hitting you, never read: before any reading ]; Check(HealthProbe.Pick(fight, 0) == 0x22, "a fight plate never read beats every read one"); Check(HealthProbe.Pick(fight[..2], 0) == 0x21, "the stalest fight plate first"); HealthProbe.Candidate[] unseen = [ new(0x30, 7.0f, false, 0, false), new(0x31, 2.0f, false, 0, false), new(0x32, 5.0f, false, 0, false), ]; Check(HealthProbe.Pick(unseen, 0) == 0x31, "never-read plates: the nearest first"); } private static void ProbeNothingDue() { HealthProbe.Candidate[] c = [ new(0x20, 1.0f, true, HealthProbe.ActiveRefreshSec - 0.1, true), // fresh fight plate new(0x40, 1.0f, true, HealthProbe.IdleRefreshSec - 0.1, false), // quiet, read recently new(0x50, 1.0f, true, 30.0, true, Dead: true), // dead new(0, 1.0f, false, 0, true), // no id ]; Check(HealthProbe.Pick(c, 0) == 0, "nothing due: no query (no traffic)"); Check(HealthProbe.Pick(ReadOnlySpan.Empty, 0) == 0, "no plates: nothing"); HealthProbe.Candidate[] nan = [new(0x20, 1.0f, true, double.NaN, true)]; Check(HealthProbe.Pick(nan, 0) == 0, "a NaN age is never due"); HealthProbe.Candidate[] due = [new(0x20, 1.0f, true, HealthProbe.ActiveRefreshSec, true)]; Check(HealthProbe.Pick(due, 0) == 0x20, "a fight plate exactly ActiveRefreshSec old is due"); } private static void ProbeReplies() { HealthProbe.Reset(); Check(!HealthProbe.TryGetOwner(out _), "after a reset the stream's owner is unknown"); HealthProbe.NoteQuery(0x10); // the selection / a plugin asked for 0x10 Check(HealthProbe.TryGetOwner(out uint o) && o == 0x10, "the owner is the last queried id"); HealthProbe.NoteProbe(0x20, 1000); Check(HealthProbe.IsProbeReply(0x20, 1150), "the reply to a probe is the HUD's"); Check(HealthProbe.IsProbeReply(0x20, 1300), "... every path that reports it (window, not consume-once)"); Check(!HealthProbe.IsProbeReply(0x10, 1150), "the owner's replies go to plugins"); Check(!HealthProbe.IsProbeReply(0x30, 1150), "an id nobody probed goes to plugins"); Check(!HealthProbe.IsProbeReply(0x20, 1000 + HealthProbe.ReplyWindowMs + 1), "after the window the id's replies go to plugins again"); Check(!HealthProbe.IsProbeReply(0x20, 1200), "... and the expired entry is gone"); HealthProbe.NoteProbe(0x21, 2000); HealthProbe.NoteQuery(0x21); // a plugin asked for the same monster right after Check(!HealthProbe.IsProbeReply(0x21, 2100), "a plugin's own query for a probed id takes its replies back"); Check(HealthProbe.TryGetOwner(out o) && o == 0x21, "... and owns the stream"); // No owner to hand the stream back to: the probed id keeps streaming to us, all of it the HUD's. HealthProbe.NoteQuery(0); HealthProbe.NoteProbe(0x22, 3000, sticky: true); Check(HealthProbe.IsProbeReply(0x22, 3000 + 60_000), "sticky probe: still the HUD's a minute later"); HealthProbe.NoteQuery(0x10); Check(!HealthProbe.IsProbeReply(0x22, 3000 + 60_001), "the next query moves the stream: sticky entry gone"); for (uint i = 1; i <= 20; i++) HealthProbe.NoteProbe(0x100 + i, 5000); // more than the ledger holds Check(HealthProbe.IsProbeReply(0x114, 5100), "the newest probes are remembered when the ledger wraps"); Check(!HealthProbe.IsProbeReply(0x101, 5100), "the oldest are dropped (bounded)"); HealthProbe.NoteProbe(0, 5000); Check(!HealthProbe.IsProbeReply(0, 5000), "id 0 never"); long before = HealthProbe.ProbeCount; HealthProbe.NoteProbe(0x200, 6000); Check(HealthProbe.ProbeCount == before + 1, "probes are counted"); HealthProbe.Reset(); Check(!HealthProbe.IsProbeReply(0x114, 5100) && !HealthProbe.TryGetOwner(out _), "reset (logout) forgets everything"); } // ── Plugin API v93 GetMonsterHealth (MonsterHudData.TryGetServerHealth) ── private static long Ms(double ms) => (long)(ms * System.Diagnostics.Stopwatch.Frequency / 1000.0); private static void PluginReadingServerOnly() { MonsterHudData.Clear(); long t0 = System.Diagnostics.Stopwatch.GetTimestamp(); Check(!MonsterHudData.TryGetServerHealth(0x40, t0, out float r, out uint age) && r == -1f && age == uint.MaxValue, "never read: false, -1, max age"); Check(!MonsterHudData.TryGetServerHealth(0, t0, out _, out _), "id 0: false"); MonsterHudData.RecordHealth(0x40, 0.6f, 0, 0); long recorded = System.Diagnostics.Stopwatch.GetTimestamp(); Check(MonsterHudData.TryGetServerHealth(0x40, recorded + Ms(2500), out r, out age) && Near(r, 0.6f), $"the server's 60% (got {r:0.000})"); Check(age >= 2500 && age < 2600, $"2.5 s old (got {age} ms)"); // The plates' estimate from the player's own hit shows on the plate, not in the plugin reading. MonsterHudData.RecordHealth(0x40, 0.3f, 0, 0, estimated: true); Check(MonsterHudData.TryGetHealth(0x40, out HealthObservation h) && Near(h.Ratio, 0.3f) && h.Estimated, "the plate shows the estimate"); Check(MonsterHudData.TryGetServerHealth(0x40, recorded + Ms(3000), out r, out age) && Near(r, 0.6f) && age >= 3000, $"the reading stays the server's 60% and keeps its age (got {r:0.000}, {age} ms)"); // An estimate on a plate the server never read: nothing for plugins. MonsterHudData.RecordHealth(0x41, 0.8f, 0, 0, estimated: true); Check(!MonsterHudData.TryGetServerHealth(0x41, recorded, out _, out _), "estimate only: false"); // The next server reading replaces it. MonsterHudData.RecordHealth(0x41, 0.45f, 450, 1000); Check(MonsterHudData.TryGetServerHealth(0x41, System.Diagnostics.Stopwatch.GetTimestamp(), out r, out age) && Near(r, 0.45f) && age < 100, $"server 45%, fresh (got {r:0.000}, {age} ms)"); // A dead monster reads 0 (the plugin decides what that means for it). MonsterHudData.RecordHealth(0x42, 0f, 0, 0); Check(MonsterHudData.TryGetServerHealth(0x42, System.Diagnostics.Stopwatch.GetTimestamp(), out r, out _) && r == 0f, "dead: 0"); // A clock behind the reading never gives a negative or wrapped age; a very old one saturates. Check(MonsterHudData.TryGetServerHealth(0x41, 0, out _, out age) && age == 0, $"now before the reading: 0 ms (got {age})"); Check(MonsterHudData.TryGetServerHealth(0x41, recorded + Ms(60.0 * 24 * 3600 * 1000), out _, out age) && age == uint.MaxValue, $"60 days: saturates (got {age})"); MonsterHudData.Clear(); Check(!MonsterHudData.TryGetServerHealth(0x41, recorded, out _, out _), "logout clears it"); } private static void PluginReadingProbeReply() { // PluginManager.QueueUpdateHealth records every 0x01C0 in MonsterHudData first, then drops a probe's // reply from the plugins' UpdateHealth queue: the reading is there, the event is not. MonsterHudData.Clear(); HealthProbe.Reset(); HealthProbe.NoteQuery(0x10); // the fight target owns the stream HealthProbe.NoteProbe(0x20, 1000); // the plates probe a monster someone else is fighting MonsterHudData.RecordHealth(0x20, 0.25f, 0, 0); Check(HealthProbe.IsProbeReply(0x20, 1100), "the reply is the probe's (kept from plugins as an event)"); Check(MonsterHudData.TryGetServerHealth(0x20, System.Diagnostics.Stopwatch.GetTimestamp(), out float r, out _) && Near(r, 0.25f), $"... and its 25% is readable by id (got {r:0.000})"); HealthProbe.Reset(); MonsterHudData.Clear(); } private static void PluginReadingThreads() { MonsterHudData.Clear(); int bad = 0; long reads = 0; using var stop = new CancellationTokenSource(); var reader = Task.Run(() => { while (!stop.IsCancellationRequested) for (uint id = 1; id <= 64; id++) { if (MonsterHudData.TryGetServerHealth(id, out float r, out _) && (r < 0f || r > 1f || float.IsNaN(r))) Interlocked.Increment(ref bad); Interlocked.Increment(ref reads); } }); // The game thread's side, including a full table's wholesale clear (2048 entries). for (int round = 0; round < 40; round++) for (uint id = 1; id <= 2100; id++) MonsterHudData.RecordHealth(id, (id % 100) / 100f, 0, 0, estimated: (id & 3) == 0); stop.Cancel(); reader.Wait(); Check(bad == 0, $"every reading in range ({bad} bad)"); Check(Interlocked.Read(ref reads) > 0, "the reader ran"); MonsterHudData.Clear(); } }