fix(D.5.3a): selected-object meter visual-gate fixes (name, gate, flash, magenta)

Visual gate against retail surfaced several fidelity gaps in the selected-object
strip; all fixed and user-confirmed. Faithful to gmToolbarUI::HandleSelectionChanged
(acclient_2013_pseudo_c.txt:198635) + RecvNotice_UpdateObjectHealth (:196213).

- UiMeter.DrawHBar: guard each slice on `id != 0` BEFORE resolve. resolve(0)
  returns the 1x1 magenta placeholder with a non-zero GL handle, so the single-
  image meter (caps id=0) was drawing 1px magenta caps at the bar's ends. The
  3-slice vitals meter (all ids set) was unaffected. (the magenta-lines bug)
- SelectedObjectController: meter visibility is now UpdateHealth-driven (shown when
  health is known for the selected guid — HasHealth at select or HealthChanged),
  not shown-on-select; brief green selection flash via Tick revert; overlay floated
  above the meter so the flash isn't hidden by the bar; name top-aligned into the
  bar sprite's black band (NameBandHeight) with the bar below.
- GameWindow.IsHealthBarTarget: gate the health bar on the server PWD bits
  BF_ATTACKABLE (0x10) | BF_PLAYER (0x8) — friendly/vendor NPCs and attackable
  Doors (Misc type) are name-only; players/monsters get the bar. Replaces the
  too-loose IsLiveCreatureTarget. Wired SelectedObjectController.Tick in OnUpdate.
- CombatState.HasHealth(guid): distinguishes a known health value from the 1.0
  default, so a re-selected already-assessed target shows its bar immediately.
- TextureCache.GetOrUploadRenderSurface: resolve the surface's DefaultPaletteId
  so paletted (P8/INDEX16) UI sprites decode instead of falling to magenta.
- ToolbarController.HiddenIds: also hide 0x100001A3 (stack-entry box) — retail
  hides it in HandleSelectionChanged; it was rendering as a stray black box.

Divergence register: AP-47 (meter-visible timing) retired (now faithful); AP-46
rewritten to the BF_ATTACKABLE/BF_PLAYER gate approximation. Full suite green
(2,688 passed / 4 skipped). User-confirmed: name on top, NPC name-only, monster
bar on assess, green flash, no magenta.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Erik 2026-06-20 09:37:15 +02:00
parent 6636e50c2a
commit 8f627cce0e
8 changed files with 438 additions and 368 deletions

View file

@ -5,74 +5,108 @@ using AcDream.App.UI;
namespace AcDream.App.UI.Layout;
/// <summary>
/// Controller for the action bar's selected-object strip (ids 0x1000019F0x100001A1).
/// Controller for the action bar's selected-object strip (ids 0x1000019E0x100001A1).
/// Analogue of retail <c>gmToolbarUI::HandleSelectionChanged</c>
/// (<c>docs/research/named-retail/acclient_2013_pseudo_c.txt:198635</c>).
/// (<c>docs/research/named-retail/acclient_2013_pseudo_c.txt:198635</c>) +
/// <c>RecvNotice_UpdateObjectHealth</c> (<c>:196213</c>).
///
/// <para>
/// On selection change: clears the strip (name, overlay state, health meter), then
/// if a guid is provided it sets the name, puts the overlay field into the appropriate
/// state ("ObjectSelected" or "StackedItemSelected"), and for health-bearing targets
/// shows the health meter and sends a <c>QueryHealth (0x01BF)</c> request.
/// On selection change: clears the strip (name, overlay flash, health meter), then if a
/// guid is provided it sets the name, flashes the selection overlay briefly, and (for
/// health-bearing targets) sends a <c>QueryHealth (0x01BF)</c> request. The Health meter
/// becomes visible only when the server actually reports health for the selected guid —
/// either an <c>UpdateHealth (0x01C0)</c> arrives (retail
/// <c>RecvNotice_UpdateObjectHealth</c> → <c>SetVisible(1)</c>) or the value is already
/// cached. So a friendly NPC you have not assessed shows name-only (no bar), and a
/// monster's bar appears after damage / a successful assess — matching retail.
/// </para>
///
/// <para>
/// <strong>Divergence — meter-visible timing.</strong>
/// Retail makes the health meter visible from <c>RecvNotice_UpdateObjectHealth</c>
/// (when the queried value arrives, cite HandleSelectionChanged:198757).
/// acdream shows it immediately on select (fill polls <see cref="CombatState.GetHealthPercent"/>
/// which returns 1.0 until the reply lands). Recorded in the divergence register.
/// <strong>Retail element roles</strong> (PostInit, <c>:198119</c>): <c>m_pSelObjectField</c>
/// is the container <c>0x1000019E</c> whose <c>SetState(0x1000000b/0c)</c> drives a
/// 0.25s <c>Pause→Normal</c> flash that cascades to the overlay child's green frame.
/// acdream has no state-cascade / transition-animation system, so this controller drives
/// the overlay element <c>0x100001A0</c> directly and reverts it after the same
/// <see cref="FlashSeconds"/> to reproduce the brief flash. The name element
/// <c>0x1000019F</c> is bumped to the top of the strip's z-order so it draws OVER the
/// overlay frame and the health bar (retail draws the name over the bar — see the
/// "Drudge Slinker" reference shot).
/// </para>
///
/// <para>
/// <strong>Divergence — health-target gate approximation.</strong>
/// Retail gates on <c>IsPlayer() || pet_owner || ObjectIsAttackable()</c>
/// (cite HandleSelectionChanged:198754). acdream uses <c>IsLiveCreatureTarget</c>
/// (the <c>ItemType.Creature</c> flag). Recorded in the divergence register.
/// Retail sends <c>Event_QueryHealth</c> for <c>IsPlayer() || pet_owner || ObjectIsAttackable()</c>
/// (<c>:198754</c>). acdream uses <c>IsLiveCreatureTarget</c> (the <c>ItemType.Creature</c>
/// flag) to gate the QueryHealth send. Visibility itself is health-data-driven (above), so
/// the gate only affects whether we proactively query; recorded in the divergence register.
/// </para>
/// </summary>
public sealed class SelectedObjectController
{
// ── Element ids (toolbar LayoutDesc 0x21000016) ─────────────────────────
/// <summary>Selected-object name element id.</summary>
/// <summary>Selected-object container / field element id (retail m_pSelObjectField).</summary>
public const uint ContainerId = 0x1000019E;
/// <summary>Selected-object name element id (retail m_pSelObjectName, UIElement_Text).</summary>
public const uint NameId = 0x1000019F;
/// <summary>Selected-object overlay field element id (states: ObjectSelected / StackedItemSelected).</summary>
/// <summary>Selected-object overlay element id (states: ObjectSelected / StackedItemSelected).</summary>
public const uint OverlayId = 0x100001A0;
/// <summary>Selected-object health meter element id.</summary>
/// <summary>Selected-object health meter element id (retail m_pSelObjectHealthMeter).</summary>
public const uint HealthMeterId = 0x100001A1;
/// <summary>Selection-overlay flash duration — retail's container ObjectSelected state is a
/// Pause(0.25s)→Normal transition (toolbar dump, element 0x1000019E).</summary>
private const double FlashSeconds = 0.25;
/// <summary>Z-order for the name so it draws OVER the overlay frame + health bar.
/// The strip's other children sit at ReadOrder 14; this floats the name to the top.</summary>
private const int NameZOrderOnTop = 1_000_000;
/// <summary>Z-order for the selection-flash overlay — above the health meter (so the green
/// flash isn't hidden by the bar) but below the name (so the name stays readable).</summary>
private const int OverlayZOrder = NameZOrderOnTop - 1;
/// <summary>Height (px) of the black name band at the top of the 31px bar sprite. The name
/// label is constrained to this band (top-aligned) so the health bar shows below it —
/// retail "name on the black, bar below". The bar sprite's colored region starts ~y14.</summary>
private const float NameBandHeight = 15f;
// ── Found elements (any may be null for partial/test layouts) ───────────
private readonly UiElement? _name;
private readonly UiDatElement? _overlay;
private readonly UiMeter? _healthMeter;
// ── Captured delegates ───────────────────────────────────────────────────
private readonly Func<uint, bool> _isHealthTarget;
private readonly Func<uint, string?> _name_;
private readonly Func<uint, float> _healthPercent;
private readonly Func<uint, uint> _stackSize;
private readonly Action<uint> _sendQueryHealth;
private readonly Func<uint, bool> _isHealthTarget;
private readonly Func<uint, string?> _resolveName;
private readonly Func<uint, float> _healthPercent;
private readonly Func<uint, bool> _hasHealth;
private readonly Func<uint, uint> _stackSize;
private readonly Action<uint> _sendQueryHealth;
// ── Live state (read by closures on the per-frame draw path) ────────────
private uint? _current;
private string? _currentName;
private double _flashRemaining; // > 0 while the selection overlay is flashing
/// <summary>White label color for the name line.</summary>
private static readonly Vector4 NameColor = new(1f, 1f, 1f, 1f);
private SelectedObjectController(
ImportedLayout layout,
Action<Action<uint?>> subscribeSelectionChanged,
Action<Action<uint?>> subscribeSelectionChanged,
Action<Action<uint, float>> subscribeHealthChanged,
Func<uint, bool> isHealthTarget,
Func<uint, string?> name,
Func<uint, float> healthPercent,
Func<uint, bool> hasHealth,
Func<uint, uint> stackSize,
Action<uint> sendQueryHealth,
UiDatFont? datFont)
{
_isHealthTarget = isHealthTarget;
_name_ = name;
_resolveName = name;
_healthPercent = healthPercent;
_hasHealth = hasHealth;
_stackSize = stackSize;
_sendQueryHealth = sendQueryHealth;
@ -81,26 +115,36 @@ public sealed class SelectedObjectController
_overlay = layout.FindElement(OverlayId) as UiDatElement;
_healthMeter = layout.FindElement(HealthMeterId) as UiMeter;
// The selection-flash overlay must draw OVER the health meter (which spans the whole
// strip) — otherwise the meter hides the green flash whenever a bar is visible (i.e.
// for players/monsters). Float it just below the name so the name stays readable.
if (_overlay is not null) _overlay.ZOrder = OverlayZOrder;
// This controller owns the health meter's initial-hidden state.
if (_healthMeter is not null)
{
_healthMeter.Visible = false;
// Fill polls live: _current holds the currently-selected guid (or null).
// Returns 0f when nothing is selected (empty bar), healthPercent(g) otherwise.
_healthMeter.Fill = () => _current is uint g ? _healthPercent(g) : (float?)0f;
}
// Attach a centered UiText child to the name element for the object name display.
// Mirrors VitalsController.BindMeter's number attach (same decoration style).
// Mirrors VitalsController.BindMeter's number attach. The name is floated to the
// top of the strip's z-order so it draws OVER the overlay frame and the health bar
// (retail renders the object name over the bar).
//
// The bar sprite (0x0600193E/F, 146x31) carries a ~14px BLACK name band across its
// TOP with the colored bar in the lower portion (confirmed from the dat). Retail
// draws the object name in that black band with the health bar BELOW it — so the
// label is TOP-aligned by constraining its height to the band, not centered over the
// whole 31px strip (which overlapped the bar's middle).
if (_name is not null)
{
_name.ZOrder = NameZOrderOnTop;
var label = new UiText
{
Left = 0f,
Top = 0f,
Width = _name.Width,
Height = _name.Height,
Anchors = AnchorEdges.Left | AnchorEdges.Top | AnchorEdges.Right | AnchorEdges.Bottom,
Left = 0f, Top = 0f, Width = _name.Width, Height = NameBandHeight,
Anchors = AnchorEdges.Left | AnchorEdges.Top | AnchorEdges.Right,
Centered = true,
DatFont = datFont,
ClickThrough = true,
@ -109,7 +153,6 @@ public sealed class SelectedObjectController
CapturesPointerDrag = false,
LinesProvider = () =>
{
// Returns a single white line when a name is available; empty otherwise.
var n = _currentName;
return string.IsNullOrEmpty(n)
? Array.Empty<UiText.Line>()
@ -119,90 +162,107 @@ public sealed class SelectedObjectController
_name.AddChild(label);
}
// Register the handler LAST so the initial state is fully set up first.
// Register the handlers LAST so the initial state is fully set up first.
subscribeSelectionChanged(OnSelectionChanged);
subscribeHealthChanged(OnHealthChanged);
}
/// <summary>
/// Create and bind a <see cref="SelectedObjectController"/> to <paramref name="layout"/>.
/// Port of retail <c>gmToolbarUI::HandleSelectionChanged</c>
/// (<c>acclient_2013_pseudo_c.txt:198635</c>).
/// Port of retail <c>gmToolbarUI::HandleSelectionChanged</c> + <c>RecvNotice_UpdateObjectHealth</c>.
/// </summary>
/// <param name="layout">Imported toolbar layout (LayoutDesc 0x21000016).</param>
/// <param name="subscribeSelectionChanged">
/// Called once with the controller's <see cref="OnSelectionChanged"/> handler.
/// Typical host: <c>h =&gt; SelectionChanged += h</c> — keeps the controller
/// decoupled from <c>GameWindow</c>.
/// </param>
/// <param name="isHealthTarget">
/// Returns true for guids that should show a health meter (proxy for retail's
/// <c>IsPlayer() || pet_owner || ObjectIsAttackable()</c>).
/// </param>
/// <param name="subscribeSelectionChanged">Called once with <see cref="OnSelectionChanged"/>
/// (typical host: <c>h =&gt; SelectionChanged += h</c>).</param>
/// <param name="subscribeHealthChanged">Called once with <see cref="OnHealthChanged"/>
/// (typical host: <c>h =&gt; Combat.HealthChanged += h</c>) — drives meter visibility.</param>
/// <param name="isHealthTarget">Returns true for guids that may show a health meter
/// (proxy for retail's <c>IsPlayer() || pet_owner || ObjectIsAttackable()</c>).</param>
/// <param name="name">Returns the display name for a given guid (or null if unknown).</param>
/// <param name="healthPercent">Returns the health fill fraction [0..1] for a given guid.</param>
/// <param name="stackSize">Returns the stack size for a given guid (0 or 1 = non-stacked).</param>
/// <param name="sendQueryHealth">
/// Sends retail <c>QueryHealth (0x01BF)</c>; server replies with <c>UpdateHealth (0x01C0)</c>.
/// May be a no-op when offline.
/// </param>
/// <param name="hasHealth">Returns true if real health has been received for a guid
/// (so a re-selected, already-known target shows its bar immediately).</param>
/// <param name="stackSize">Returns the stack size for a guid (0 or 1 = non-stacked).</param>
/// <param name="sendQueryHealth">Sends retail <c>QueryHealth (0x01BF)</c>; may be a no-op offline.</param>
/// <param name="datFont">Dat font for the name label; null = debug bitmap font fallback.</param>
public static SelectedObjectController Bind(
ImportedLayout layout,
Action<Action<uint?>> subscribeSelectionChanged,
Action<Action<uint?>> subscribeSelectionChanged,
Action<Action<uint, float>> subscribeHealthChanged,
Func<uint, bool> isHealthTarget,
Func<uint, string?> name,
Func<uint, float> healthPercent,
Func<uint, bool> hasHealth,
Func<uint, uint> stackSize,
Action<uint> sendQueryHealth,
UiDatFont? datFont)
{
return new SelectedObjectController(
layout, subscribeSelectionChanged,
isHealthTarget, name, healthPercent, stackSize, sendQueryHealth, datFont);
}
=> new SelectedObjectController(
layout, subscribeSelectionChanged, subscribeHealthChanged,
isHealthTarget, name, healthPercent, hasHealth, stackSize, sendQueryHealth, datFont);
/// <summary>
/// Port of <c>gmToolbarUI::HandleSelectionChanged</c>
/// (<c>acclient_2013_pseudo_c.txt:198635</c>):
/// Port of <c>gmToolbarUI::HandleSelectionChanged</c> (<c>:198635</c>):
/// clear-then-populate the selected-object strip on any selection change.
/// Registered via <c>subscribeSelectionChanged</c> at bind time; called by
/// <c>GameWindow.SelectionChanged</c> and by the despawn-clear path.
/// </summary>
public void OnSelectionChanged(uint? guid)
{
// ── 1. Clear first (retail: UIElement_Text::SetText + m_pSelObjectField->SetState(0)
// + SetVisible(0) on the health meter). ──────────────────────────────────────
// ── 1. Clear first (retail: SetText("") + m_pSelObjectField->SetState(0)
// + SetVisible(0) on the meters). ──────────────────────────────────────
if (_healthMeter is not null) _healthMeter.Visible = false;
if (_overlay is not null) _overlay.ActiveState = "";
_currentName = null;
_currentName = null;
_current = guid;
// Update the backing current guid so the Fill closure reflects the new state.
_current = guid;
// ── 2. Selection == null → strip stays cleared, done. ───────────────────────────
if (guid is null) return;
if (guid is null)
{
// Deselect: clear the overlay flash immediately too.
SetOverlayState("");
_flashRemaining = 0;
return;
}
uint g = guid.Value;
// ── 3. Selection != null — populate the strip. ──────────────────────────────────
// ── 2. Name (displayed via the UiText child's LinesProvider reading _currentName). ──
_currentName = _resolveName(g);
// Name (displayed via the UiText child's LinesProvider reading _currentName).
_currentName = _name_(g);
// ── 3. Selection overlay: brief flash (retail container ObjectSelected
// = Pause(0.25s)→Normal). "StackedItemSelected" for stacks. ──────────────
SetOverlayState(_stackSize(g) > 1u ? "StackedItemSelected" : "ObjectSelected");
_flashRemaining = FlashSeconds;
// Overlay state: "StackedItemSelected" for stacked items, "ObjectSelected" otherwise.
// Retail ref: m_pSelObjectField->SetState(0x1000000b) = "ObjectSelected"
// (acclient_2013_pseudo_c.txt:198754). Stack sprite id 0x06004CF4 confirmed in toolbar dump.
if (_overlay is not null)
_overlay.ActiveState = _stackSize(g) > 1u ? "StackedItemSelected" : "ObjectSelected";
// Health meter: visible + QueryHealth for health-bearing targets.
// Divergence: retail shows the meter only when RecvNotice_UpdateObjectHealth arrives;
// acdream shows it immediately (fill reads GetHealthPercent = 1.0 until the reply).
// Retail ref: CM_Combat::Event_QueryHealth (acclient_2013_pseudo_c.txt:198757).
// ── 4. Health: query, and show the meter only if real health is already known.
// Otherwise the meter appears when OnHealthChanged fires for this guid
// (retail RecvNotice_UpdateObjectHealth :196213). ──────────────────────────
if (_isHealthTarget(g))
{
if (_healthMeter is not null) _healthMeter.Visible = true;
_sendQueryHealth(g);
if (_hasHealth(g) && _healthMeter is not null)
_healthMeter.Visible = true;
}
}
/// <summary>
/// Port of <c>gmToolbarUI::RecvNotice_UpdateObjectHealth</c> (<c>:196213</c>): when the
/// server reports health for the currently-selected guid, make the Health meter visible.
/// The fill value is read live by the meter's <see cref="UiMeter.Fill"/> provider.
/// </summary>
public void OnHealthChanged(uint guid, float percent)
{
if (_current is uint c && c == guid && _isHealthTarget(guid) && _healthMeter is not null)
_healthMeter.Visible = true;
}
/// <summary>Per-frame tick: reverts the selection overlay after the brief flash window.</summary>
public void Tick(double deltaSeconds)
{
if (_flashRemaining <= 0) return;
_flashRemaining -= deltaSeconds;
if (_flashRemaining <= 0)
SetOverlayState(""); // flash done → overlay back to blank
}
private void SetOverlayState(string state)
{
if (_overlay is not null) _overlay.ActiveState = state;
}
}

View file

@ -39,11 +39,13 @@ public sealed class ToolbarController
// Ids confirmed from the toolbar LayoutDesc dump.
// 0x100001A1 (health meter) is now OWNED by SelectedObjectController (D.5.3a) —
// it hides A1 at bind and shows it on a health-target selection, so A1 is removed
// from here to avoid double-ownership. 0x100001A2 (mana meter) and 0x100001A4
// (stack slider) are DEFERRED features (mana #140, stack-split UI) with no controller
// yet, so they stay hidden here — otherwise their dat back-track sprites render as
// stray empty bars on the toolbar.
private static readonly uint[] HiddenIds = { 0x100001A2, 0x100001A4 };
// from here to avoid double-ownership. 0x100001A2 (mana meter), 0x100001A3 (stack-size
// entry box) and 0x100001A4 (stack slider) are DEFERRED features (mana #140, stack-split
// UI) with no controller yet, so they stay hidden here — otherwise their dat sprites
// render as stray bars / a black box on the toolbar. Retail hides A3/A4 in
// gmToolbarUI::HandleSelectionChanged (acclient_2013_pseudo_c.txt:198660/198742),
// showing them only for a stacked-item selection.
private static readonly uint[] HiddenIds = { 0x100001A2, 0x100001A3, 0x100001A4 };
// Four mutually-exclusive combat-mode indicator elements — exactly one visible at a time.
// Index 0 = NonCombat (peace), 1 = Melee, 2 = Missile, 3 = Magic.

View file

@ -135,9 +135,14 @@ public sealed class UiMeter : UiElement
{
if (clipW <= 0f) return;
float w = Width, h = Height;
var (lt, lw, _) = resolve(leftId);
var (mt, mw, _) = resolve(midId);
var (rt, rw, _) = resolve(rightId);
// Only resolve a slice when its id is non-zero. resolve(0) returns the 1x1 MAGENTA
// placeholder with a NON-ZERO GL handle, so resolving a zero (absent) cap id and then
// testing `tex != 0` would draw a 1px magenta cap. The single-image meter (toolbar
// selected-object bar) has no left/right caps (ids 0); the 3-slice vitals meter sets
// all six ids. Guard on the id, not the resolved handle.
var (lt, lw, _) = leftId != 0 ? resolve(leftId) : (0u, 0, 0);
var (mt, mw, _) = midId != 0 ? resolve(midId) : (0u, 0, 0);
var (rt, rw, _) = rightId != 0 ? resolve(rightId) : (0u, 0, 0);
float capL = lt != 0 ? MathF.Min(lw, w) : 0f;
float capR = rt != 0 ? MathF.Min(rw, w - capL) : 0f;