// ============================================================================ // RynthDefective - RynthDefectivePlugin.Appraisal.cs // The Appraisal window's tick (2026-11-28; Appraisal/AppraisalWindow.cs draws // it, Appraisal/AppraisalText.cs words it). // // What it shows is the PLAYER's pick, from the engine (plugin API v100 // GetPlayerPick): a middle-click on something in the world (the object under // the cursor; AC's selection doesn't change, so a running bot keeps its // target), or a selection the player made (a click in the world or a pack, // the selection keys, a radar dot). Selections a plugin makes through the API // (RynthAi's targets and loot) never reach it. Pinned (the window's pin, saved // in the character file): only a middle-click changes it. // // A new pick is appraised (RequestId) unless the server answered for it in the // last few seconds; a creature's health is asked for every 2 s while it shows, // and it is assessed again every 15 s. A middle-click opens the window when it // is shut. The view is rebuilt 4 times a second while the window draws. // // Needs API v100 (the pick); on an older engine the window isn't declared. // ============================================================================ using System; using RynthCore.Plugin.RynthDefective.Appraisal; using RynthCore.Plugin.RynthDefective.Model; using RynthCore.PluginSdk; namespace RynthCore.Plugin.RynthDefective; public sealed partial class RynthDefectivePlugin { internal const string AppraisalSurface = "window:appraisal"; private AppraisalWindow? _appraisalWindow; // render thread private volatile AppraisalView _appraisalView = AppraisalView.NotReady; private long _appraisalDrawnMs = long.MinValue / 2, _nextAppraisalMs, _nextAppraisalHealthMs, _nextAppraisalIdMs; private int _pickSeq = -1; private uint _appraisalId; private string _appraisalSource = string.Empty; private long _appraisalPickedMs; private AppraisalDoc? _appraisalLast; private int _appraisalFailures; private volatile AppraisalHealth? _appraisalHealth; private sealed record AppraisalHealth(uint Id, float Ratio); private const long AppraisalBuildMs = 250, AppraisalHealthMs = 2000, CreatureReassessMs = 15000, RecentIdSeconds = 5; /// The engine has v100's player picks: the Appraisal window is declared. internal bool UsesAppraisal => UsesShortcutBar && Host.HasPlayerPick; private bool AppraisalPinned => _identityKnown && _session.Data.Settings.AppraisalPinned; /// RynthPluginRenderEmbed "window:appraisal" (":popout" when popped out). 1 drawn, 0 not ready, -2 failed (three in a row). Render thread. internal int RenderAppraisal(bool popped, out float wantHeight) { wantHeight = 0; if (!UsesAppraisal) return 0; System.Threading.Volatile.Write(ref _appraisalDrawnMs, Environment.TickCount64); try { _appraisalWindow ??= new AppraisalWindow(Host) { Refresh = () => QueueBar(AppraiseAgain), TogglePin = () => QueueBar(ToggleAppraisalPin), Select = id => QueueBar(() => { if (!Host.SelectItem(id)) Print("That can't be selected now."); }), }; wantHeight = _appraisalWindow.Draw(_appraisalView, popped); _appraisalFailures = 0; return 1; } catch (Exception ex) { Host.Log($"[RynthDefective] appraisal draw failed: {ex.GetType().Name}: {ex.Message}"); return ++_appraisalFailures >= 3 ? -2 : 0; } } /// Health from AC for the appraised creature (OnUpdateHealth, any thread). private void NoteAppraisalHealth(uint targetId, float ratio) { if (targetId != 0 && targetId == _appraisalId) _appraisalHealth = new AppraisalHealth(targetId, ratio); } /// Plugin tick: a new pick, the asks to the server, and the view while the window draws. private void TickAppraisal(long now) { if (!UsesAppraisal || !_identityKnown) return; try { TakePick(now); bool shown = now - System.Threading.Volatile.Read(ref _appraisalDrawnMs) <= 2000; if (!shown || _appraisalId == 0) { if (_appraisalId == 0) _appraisalView = new AppraisalView(true, null, "", AppraisalPinned, false); return; } AskServer(now); if (now < _nextAppraisalMs) return; _nextAppraisalMs = now + AppraisalBuildMs; _appraisalView = BuildAppraisal(now); } catch (Exception ex) { Host.Log($"[RynthDefective] appraisal read failed: {ex.GetType().Name}: {ex.Message}"); } } /// The engine's last player pick, when it is new and this window takes it. private void TakePick(long now) { int seq = Host.GetPlayerPick(out uint id, out RynthPickKind kind); if (_pickSeq < 0) { _pickSeq = seq; if (seq > 0 && id != 0) Adopt(id, kind, now, open: false); return; } // the last pick before a reload if (seq == _pickSeq || id == 0) return; _pickSeq = seq; if (kind == RynthPickKind.Selected && AppraisalPinned) return; Adopt(id, kind, now, open: kind == RynthPickKind.MiddleClick); } private void Adopt(uint id, RynthPickKind kind, long now, bool open) { bool same = id == _appraisalId; _appraisalId = id; _appraisalPickedMs = now; _appraisalSource = kind == RynthPickKind.MiddleClick ? "Middle-clicked" : "Your selection"; if (!same) { _appraisalHealth = null; _appraisalLast = null; } _nextAppraisalMs = 0; _nextAppraisalHealthMs = 0; // A new pick is appraised unless the server just answered for it (AC appraises a selection itself). if (!RecentlyIdentified(id)) Host.RequestId(id); _nextAppraisalIdMs = now + CreatureReassessMs; if (open && Host.SetPanelOpen(AppraisalWindow.Title, RynthPanelOpen.Ask) == 0) Host.SetPanelOpen(AppraisalWindow.Title, RynthPanelOpen.Open); } private bool RecentlyIdentified(uint id) { long last = Host.GetLastIdTime(id); return last > 0 && DateTimeOffset.UtcNow.ToUnixTimeSeconds() - last <= RecentIdSeconds; } /// A creature's health every 2 s, and an assess every 15 s while it shows. private void AskServer(long now) { bool creature = Host.TryGetItemType(_appraisalId, out uint type) && (type & CreatureType) != 0; if (!creature) return; if (now >= _nextAppraisalHealthMs) { _nextAppraisalHealthMs = now + AppraisalHealthMs; Host.QueryHealth(_appraisalId); } if (now >= _nextAppraisalIdMs) { _nextAppraisalIdMs = now + CreatureReassessMs; Host.RequestId(_appraisalId); } } private AppraisalView BuildAppraisal(long now) { uint id = _appraisalId; string source = _appraisalSource + " ยท " + Ago(now - _appraisalPickedMs); if (!Host.TryGetObjectProperties(id, out ObjectProperties? p) || p == null) { // Gone from view (a despawn, out of range): keep what was last read. return new AppraisalView(true, _appraisalLast, source, AppraisalPinned, Gone: true); } if (p.QualitiesPending && _appraisalLast != null) return new AppraisalView(true, _appraisalLast, source, AppraisalPinned, false); // the engine reads AC's tables next tick var input = new AppraisalInput { Id = id, Name = Host.TryGetObjectName(id, out string n) ? n : string.Empty, ItemType = Host.TryGetItemType(id, out uint type) ? type : 0, Identified = p.HasIdData, IsPlayer = p.IsPlayer || IsPlayerObject(id), Ints = p.Ints, Int64s = p.Int64s, Bools = p.Bools, Floats = p.Floats, Strings = p.Strings, DataIds = p.DataIds, InstanceIds = p.InstanceIds, Spells = p.Spells, SpellName = SpellTable.Name, }; bool creature = (input.ItemType & CreatureType) != 0; if (creature) { if (Host.TryGetTargetVitals(id, out uint h, out uint hm, out uint s, out uint sm, out uint m, out uint mm) && hm > 0) input.Vitals = (h, hm, s, sm, m, mm); Span attrs = stackalloc uint[6]; if (Host.TryGetCreatureAttributes(id, attrs, out uint bits) && attrs[0] + attrs[1] + attrs[2] + attrs[3] + attrs[4] + attrs[5] > 0) { input.Attributes = attrs.ToArray(); input.EnchantBits = bits; } if (_appraisalHealth is { } hs && hs.Id == id) input.HealthRatio = hs.Ratio; } AppraisalDoc doc = AppraisalText.Build(input); _appraisalLast = doc; return new AppraisalView(true, doc, source, AppraisalPinned, false); } /// A player's character (the engine flags the player itself; others by their ObjectDescriptionFlags Player bit). private bool IsPlayerObject(uint id) => id == Host.GetPlayerId() || Host.TryGetObjectBitfield(id, out uint bits) && (bits & 0x8) != 0; private void AppraiseAgain() { if (_appraisalId == 0) { PrintFail("Nothing is in the Appraisal window."); return; } if (!Host.RequestId(_appraisalId)) PrintFail("The server can't be asked now."); _nextAppraisalMs = 0; } private void ToggleAppraisalPin() { if (!_identityKnown) return; _session.Data.Settings.AppraisalPinned = !_session.Data.Settings.AppraisalPinned; RequestSave(); _nextAppraisalMs = 0; Print(_session.Data.Settings.AppraisalPinned ? "Appraisal pinned: only a middle-click changes it." : "Appraisal follows your own clicks and middle-clicks."); } /// /rdef appraisal [pin|unpin|0x8000ABCD]: the window, the pin, or (a test hook) appraise that object. private void RunAppraisalCommand(string text) { if (!UsesAppraisal) { PrintFail("This RynthCore can't show the Appraisal window (it needs plugin API v100)."); return; } string t = text.Trim(); if (t.Length == 0) { Answer(DefReplies.Window("Appraisal window", Host.SetPanelOpen(AppraisalWindow.Title, RynthPanelOpen.Toggle))); return; } switch (t.ToLowerInvariant()) { case "pin": if (!_session.Data.Settings.AppraisalPinned) ToggleAppraisalPin(); else Print("Appraisal is already pinned."); return; case "unpin": if (_session.Data.Settings.AppraisalPinned) ToggleAppraisalPin(); else Print("Appraisal already follows your clicks."); return; case "again": case "refresh": AppraiseAgain(); return; case "popout": case "pop": case "out": Answer(DefReplies.PoppedOut("Appraisal window", Host.SetPanelPopOut(AppraisalWindow.Title, RynthPanelPopOut.PopOut))); return; case "dock": case "in": Answer(DefReplies.PoppedOut("Appraisal window", Host.SetPanelPopOut(AppraisalWindow.Title, RynthPanelPopOut.Dock))); return; } if (DefReplies.TryParseSelection(t, out uint pick, out string why)) { Adopt(pick, RynthPickKind.MiddleClick, Environment.TickCount64, open: true); Print($"Appraising {_session.ObjectName(pick)} (0x{pick:X8})."); } else PrintFail(why + " Usage: /rdef appraisal [pin | unpin | again | popout | dock | 0x8000ABCD]"); } private static string Ago(long ms) { long s = Math.Max(0, ms / 1000); return s < 5 ? "just now" : s < 60 ? $"{s} s ago" : s < 3600 ? $"{s / 60} min ago" : $"{s / 3600} h ago"; } }