feat: port retail magic lifecycle and retained spell UI
Complete the retail cast-intent, target, component, enchantment, and busy-state paths; mount the DAT-authored spell bar, spellbook, component book, effects panels, and shared panel lifecycle; and add scoped input plus conformance coverage. Co-Authored-By: Codex <noreply@openai.com>
This commit is contained in:
parent
7b7ffcd278
commit
07be994d97
84 changed files with 17822 additions and 1051 deletions
|
|
@ -1,6 +1,6 @@
|
|||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace AcDream.Core.Spells;
|
||||
|
||||
|
|
@ -17,9 +17,14 @@ namespace AcDream.Core.Spells;
|
|||
/// </summary>
|
||||
public sealed class Spellbook
|
||||
{
|
||||
private readonly HashSet<uint> _learnedSpells = new();
|
||||
private readonly ConcurrentDictionary<uint, ActiveEnchantmentRecord> _activeByLayer = new();
|
||||
private readonly Dictionary<uint, float> _learnedSpells = new();
|
||||
private readonly Dictionary<uint, ActiveEnchantmentRecord> _activeById = new();
|
||||
private readonly Dictionary<uint, List<uint>> _enchantmentOrderByBucket = new();
|
||||
private readonly List<uint>[] _favoriteSpells = Enumerable.Range(0, 8)
|
||||
.Select(_ => new List<uint>()).ToArray();
|
||||
private readonly Dictionary<uint, uint> _desiredComponents = new();
|
||||
private readonly SpellTable _table;
|
||||
private uint _spellbookFilters = 0x3FFFu;
|
||||
|
||||
/// <summary>
|
||||
/// Build a Spellbook with an optional <see cref="SpellTable"/>
|
||||
|
|
@ -69,39 +74,98 @@ public sealed class Spellbook
|
|||
/// <summary>Fires when an enchantment is removed (expired / dispelled).</summary>
|
||||
public event Action<ActiveEnchantmentRecord>? EnchantmentRemoved;
|
||||
|
||||
/// <summary>Fires when any manifest-backed magic UI state changes.</summary>
|
||||
public event Action? StateChanged;
|
||||
|
||||
/// <summary>Fires when learned spells, favorites, or spellbook filters change.</summary>
|
||||
public event Action? SpellbookChanged;
|
||||
|
||||
/// <summary>Fires when the active enchantment set changes.</summary>
|
||||
public event Action? EnchantmentsChanged;
|
||||
|
||||
/// <summary>Fires when the desired spell-component levels change.</summary>
|
||||
public event Action? DesiredComponentsChanged;
|
||||
|
||||
/// <summary>All currently learned spell ids.</summary>
|
||||
public IReadOnlyCollection<uint> LearnedSpells => _learnedSpells;
|
||||
public IReadOnlyCollection<uint> LearnedSpells => _learnedSpells.Keys;
|
||||
|
||||
/// <summary>Stable learned-spell snapshot, including server spell power.</summary>
|
||||
public IReadOnlyList<LearnedSpellRecord> LearnedSpellSnapshot => _learnedSpells
|
||||
.Select(pair => new LearnedSpellRecord(pair.Key, pair.Value))
|
||||
.OrderBy(spell => spell.SpellId)
|
||||
.ToArray();
|
||||
|
||||
/// <summary>All currently-active enchantments.</summary>
|
||||
public IEnumerable<ActiveEnchantmentRecord> ActiveEnchantments => _activeByLayer.Values;
|
||||
public IEnumerable<ActiveEnchantmentRecord> ActiveEnchantments => _activeById.Values;
|
||||
|
||||
public IReadOnlyList<ActiveEnchantmentRecord> ActiveEnchantmentSnapshot =>
|
||||
_activeById.Values.OrderBy(record => record.Identity).ToArray();
|
||||
|
||||
/// <summary>
|
||||
/// Retail ordinary-effect projection after mult/add filtering and the
|
||||
/// spell-category duel. This is the source for effects-list rows; retail's
|
||||
/// indicator counts scan the raw mult/add records before this projection.
|
||||
/// </summary>
|
||||
public IReadOnlyList<ActiveEnchantmentRecord> EnchantmentsInEffectSnapshot
|
||||
{
|
||||
get
|
||||
{
|
||||
var ordered = new List<ActiveEnchantmentRecord>();
|
||||
AppendBucket(bucket: 1u, ordered);
|
||||
AppendBucket(bucket: 2u, ordered);
|
||||
return EnchantmentRegistryProjection.GetEnchantmentsInEffect(ordered);
|
||||
}
|
||||
}
|
||||
|
||||
public IReadOnlyList<uint> GetFavorites(int tabIndex)
|
||||
{
|
||||
if ((uint)tabIndex >= _favoriteSpells.Length) return Array.Empty<uint>();
|
||||
return _favoriteSpells[tabIndex].ToArray();
|
||||
}
|
||||
|
||||
public IReadOnlyDictionary<uint, uint> DesiredComponents => _desiredComponents;
|
||||
|
||||
public uint SpellbookFilters => _spellbookFilters;
|
||||
|
||||
public int LearnedCount => _learnedSpells.Count;
|
||||
public int ActiveCount => _activeByLayer.Count;
|
||||
public int ActiveCount => _activeById.Count;
|
||||
|
||||
public bool Knows(uint spellId) => _learnedSpells.Contains(spellId);
|
||||
public bool Knows(uint spellId) => _learnedSpells.ContainsKey(spellId);
|
||||
|
||||
// ── Inbound handlers ─────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>0x02C1 MagicUpdateSpell: learn a spell.</summary>
|
||||
public void OnSpellLearned(uint spellId)
|
||||
public void OnSpellLearned(uint spellId, float power = 0f)
|
||||
{
|
||||
if (_learnedSpells.Add(spellId))
|
||||
if (_learnedSpells.TryAdd(spellId, power))
|
||||
{
|
||||
SpellLearned?.Invoke(spellId);
|
||||
NotifySpellbookChanged();
|
||||
}
|
||||
else if (_learnedSpells[spellId] != power)
|
||||
{
|
||||
_learnedSpells[spellId] = power;
|
||||
NotifySpellbookChanged();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>0x01A8 MagicRemoveSpell: forget a spell.</summary>
|
||||
public void OnSpellForgotten(uint spellId)
|
||||
{
|
||||
if (_learnedSpells.Remove(spellId))
|
||||
{
|
||||
SpellForgotten?.Invoke(spellId);
|
||||
NotifySpellbookChanged();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>0x02C2 MagicUpdateEnchantment: enchantment added / refreshed.</summary>
|
||||
public void OnEnchantmentAdded(uint spellId, uint layerId, float duration, uint casterGuid)
|
||||
{
|
||||
var record = new ActiveEnchantmentRecord(spellId, layerId, duration, casterGuid);
|
||||
_activeByLayer[layerId] = record;
|
||||
UpsertLiveEnchantment(record);
|
||||
EnchantmentAdded?.Invoke(record);
|
||||
NotifyEnchantmentsChanged();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -113,56 +177,305 @@ public sealed class Spellbook
|
|||
/// </summary>
|
||||
public void OnEnchantmentAdded(ActiveEnchantmentRecord record)
|
||||
{
|
||||
_activeByLayer[record.LayerId] = record;
|
||||
UpsertLiveEnchantment(record);
|
||||
EnchantmentAdded?.Invoke(record);
|
||||
NotifyEnchantmentsChanged();
|
||||
}
|
||||
|
||||
/// <summary>0x02C3 / 0x02C7 MagicRemove/DispelEnchantment.</summary>
|
||||
public void OnEnchantmentRemoved(uint layerId, uint spellId)
|
||||
{
|
||||
if (_activeByLayer.TryRemove(layerId, out var record))
|
||||
uint identity = ActiveEnchantmentRecord.MakeIdentity(spellId, layerId);
|
||||
if (RemoveActiveEnchantment(identity, out ActiveEnchantmentRecord record))
|
||||
{
|
||||
EnchantmentRemoved?.Invoke(record);
|
||||
else
|
||||
EnchantmentRemoved?.Invoke(new ActiveEnchantmentRecord(spellId, layerId, 0f, 0));
|
||||
NotifyEnchantmentsChanged();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>0x02C6 MagicPurgeEnchantments: clear all active buffs.</summary>
|
||||
public void OnEnchantmentsAdded(IEnumerable<ActiveEnchantmentRecord> records)
|
||||
{
|
||||
foreach (ActiveEnchantmentRecord record in records)
|
||||
{
|
||||
UpsertLiveEnchantment(record);
|
||||
EnchantmentAdded?.Invoke(record);
|
||||
}
|
||||
NotifyEnchantmentsChanged();
|
||||
}
|
||||
|
||||
public void OnEnchantmentsRemoved(IEnumerable<(uint SpellId, uint Layer)> spells)
|
||||
{
|
||||
bool changed = false;
|
||||
foreach ((uint spellId, uint layer) in spells)
|
||||
{
|
||||
uint identity = ActiveEnchantmentRecord.MakeIdentity(spellId, layer);
|
||||
if (!RemoveActiveEnchantment(identity, out ActiveEnchantmentRecord record)) continue;
|
||||
changed = true;
|
||||
EnchantmentRemoved?.Invoke(record);
|
||||
}
|
||||
if (changed) NotifyEnchantmentsChanged();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retail EnchantmentRegistry::PurgeEnchantments (0x00594DE0): purge only
|
||||
/// finite-duration multiplicative/additive enchantments. Vitae, cooldowns,
|
||||
/// and permanent enchantments live in separate registries and survive.
|
||||
/// </summary>
|
||||
public void OnPurgeAll()
|
||||
{
|
||||
foreach (var rec in _activeByLayer.Values)
|
||||
ActiveEnchantmentRecord[] removed = _activeById.Values
|
||||
.Where(IsPurgeable)
|
||||
.ToArray();
|
||||
foreach (ActiveEnchantmentRecord record in removed)
|
||||
RemoveActiveEnchantment(record.Identity, out _);
|
||||
foreach (var rec in removed)
|
||||
EnchantmentRemoved?.Invoke(rec);
|
||||
_activeByLayer.Clear();
|
||||
if (removed.Length != 0) NotifyEnchantmentsChanged();
|
||||
}
|
||||
|
||||
public void OnPurgeBadEnchantments()
|
||||
{
|
||||
// Retail PurgeBadEnchantments (0x00594E00) tests the StatMod
|
||||
// Beneficial bit directly; spell-table labels do not participate.
|
||||
const uint BeneficialStatMod = 0x02000000u;
|
||||
ActiveEnchantmentRecord[] removed = _activeById.Values
|
||||
.Where(record => IsPurgeable(record)
|
||||
&& (record.StatModType.GetValueOrDefault() & BeneficialStatMod) == 0)
|
||||
.ToArray();
|
||||
foreach (ActiveEnchantmentRecord record in removed)
|
||||
RemoveActiveEnchantment(record.Identity, out _);
|
||||
foreach (ActiveEnchantmentRecord record in removed)
|
||||
EnchantmentRemoved?.Invoke(record);
|
||||
if (removed.Length != 0) NotifyEnchantmentsChanged();
|
||||
}
|
||||
|
||||
private static bool IsPurgeable(ActiveEnchantmentRecord record) =>
|
||||
(record.Bucket is 1u or 2u) && record.Duration != -1f;
|
||||
|
||||
/// <summary>
|
||||
/// PlayerDescription is a complete character manifest. Replace every
|
||||
/// magic collection atomically so relogging or switching characters cannot
|
||||
/// retain state from the prior snapshot.
|
||||
/// </summary>
|
||||
public void ReplaceManifest(
|
||||
IReadOnlyDictionary<uint, float> learnedSpells,
|
||||
IEnumerable<ActiveEnchantmentRecord> enchantments,
|
||||
IReadOnlyList<IReadOnlyList<uint>> favorites,
|
||||
IReadOnlyList<(uint Id, uint Amount)> desiredComponents,
|
||||
uint spellbookFilters)
|
||||
{
|
||||
_learnedSpells.Clear();
|
||||
foreach ((uint spellId, float power) in learnedSpells)
|
||||
_learnedSpells[spellId] = power;
|
||||
|
||||
_activeById.Clear();
|
||||
_enchantmentOrderByBucket.Clear();
|
||||
foreach (ActiveEnchantmentRecord enchantment in enchantments)
|
||||
UpsertManifestEnchantment(enchantment);
|
||||
|
||||
for (int i = 0; i < _favoriteSpells.Length; i++)
|
||||
{
|
||||
_favoriteSpells[i].Clear();
|
||||
if (i < favorites.Count)
|
||||
_favoriteSpells[i].AddRange(favorites[i]);
|
||||
}
|
||||
|
||||
_desiredComponents.Clear();
|
||||
foreach ((uint id, uint amount) in desiredComponents)
|
||||
_desiredComponents[id] = amount;
|
||||
_spellbookFilters = spellbookFilters;
|
||||
SpellbookChanged?.Invoke();
|
||||
EnchantmentsChanged?.Invoke();
|
||||
DesiredComponentsChanged?.Invoke();
|
||||
StateChanged?.Invoke();
|
||||
}
|
||||
|
||||
public void SetFavorite(int tabIndex, int position, uint spellId)
|
||||
{
|
||||
if ((uint)tabIndex >= _favoriteSpells.Length || position < 0) return;
|
||||
List<uint> tab = _favoriteSpells[tabIndex];
|
||||
if (position > tab.Count) position = tab.Count;
|
||||
tab.Remove(spellId);
|
||||
tab.Insert(Math.Min(position, tab.Count), spellId);
|
||||
NotifySpellbookChanged();
|
||||
}
|
||||
|
||||
public void RemoveFavorite(int tabIndex, uint spellId)
|
||||
{
|
||||
if ((uint)tabIndex >= _favoriteSpells.Length) return;
|
||||
if (_favoriteSpells[tabIndex].Remove(spellId)) NotifySpellbookChanged();
|
||||
}
|
||||
|
||||
public void SetSpellbookFilters(uint filters)
|
||||
{
|
||||
if (_spellbookFilters == filters) return;
|
||||
_spellbookFilters = filters;
|
||||
NotifySpellbookChanged();
|
||||
}
|
||||
|
||||
public void SetDesiredComponent(uint componentId, uint amount)
|
||||
{
|
||||
uint current = _desiredComponents.TryGetValue(componentId, out uint existing)
|
||||
? existing : 0u;
|
||||
if (current == amount) return;
|
||||
if (amount == 0) _desiredComponents.Remove(componentId);
|
||||
else _desiredComponents[componentId] = amount;
|
||||
DesiredComponentsChanged?.Invoke();
|
||||
StateChanged?.Invoke();
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
_learnedSpells.Clear();
|
||||
_activeByLayer.Clear();
|
||||
_activeById.Clear();
|
||||
_enchantmentOrderByBucket.Clear();
|
||||
foreach (List<uint> tab in _favoriteSpells) tab.Clear();
|
||||
_desiredComponents.Clear();
|
||||
_spellbookFilters = 0x3FFFu;
|
||||
SpellbookChanged?.Invoke();
|
||||
EnchantmentsChanged?.Invoke();
|
||||
DesiredComponentsChanged?.Invoke();
|
||||
StateChanged?.Invoke();
|
||||
}
|
||||
|
||||
private void NotifySpellbookChanged()
|
||||
{
|
||||
SpellbookChanged?.Invoke();
|
||||
StateChanged?.Invoke();
|
||||
}
|
||||
|
||||
private void NotifyEnchantmentsChanged()
|
||||
{
|
||||
EnchantmentsChanged?.Invoke();
|
||||
StateChanged?.Invoke();
|
||||
}
|
||||
|
||||
private void AppendBucket(uint bucket, List<ActiveEnchantmentRecord> destination)
|
||||
{
|
||||
if (!_enchantmentOrderByBucket.TryGetValue(bucket, out List<uint>? order)) return;
|
||||
foreach (uint identity in order)
|
||||
if (_activeById.TryGetValue(identity, out ActiveEnchantmentRecord record)
|
||||
&& record.Bucket == bucket)
|
||||
destination.Add(record);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retail <c>AddEnchantmentToList @ 0x00593DF0</c> inserts a new live
|
||||
/// mult/add record at the list head. Refreshing an existing identity keeps
|
||||
/// its node position; moving buckets creates a new head node.
|
||||
/// </summary>
|
||||
private void UpsertLiveEnchantment(ActiveEnchantmentRecord record)
|
||||
{
|
||||
uint identity = record.Identity;
|
||||
if (_activeById.TryGetValue(identity, out ActiveEnchantmentRecord previous))
|
||||
{
|
||||
_activeById[identity] = record;
|
||||
if (previous.Bucket == record.Bucket)
|
||||
{
|
||||
EnsureOrdered(identity, record.Bucket, insertAtHead: true);
|
||||
return;
|
||||
}
|
||||
RemoveFromOrder(previous);
|
||||
}
|
||||
else
|
||||
{
|
||||
_activeById.Add(identity, record);
|
||||
}
|
||||
GetBucketOrder(record.Bucket).Insert(0, identity);
|
||||
}
|
||||
|
||||
/// <summary>Bulk UnPack preserves the received head-to-tail list order.</summary>
|
||||
private void UpsertManifestEnchantment(ActiveEnchantmentRecord record)
|
||||
{
|
||||
uint identity = record.Identity;
|
||||
if (_activeById.TryGetValue(identity, out ActiveEnchantmentRecord previous))
|
||||
{
|
||||
_activeById[identity] = record;
|
||||
if (previous.Bucket == record.Bucket)
|
||||
{
|
||||
EnsureOrdered(identity, record.Bucket, insertAtHead: false);
|
||||
return;
|
||||
}
|
||||
RemoveFromOrder(previous);
|
||||
}
|
||||
else
|
||||
{
|
||||
_activeById.Add(identity, record);
|
||||
}
|
||||
GetBucketOrder(record.Bucket).Add(identity);
|
||||
}
|
||||
|
||||
private bool RemoveActiveEnchantment(
|
||||
uint identity,
|
||||
out ActiveEnchantmentRecord record)
|
||||
{
|
||||
if (!_activeById.Remove(identity, out record)) return false;
|
||||
RemoveFromOrder(record);
|
||||
return true;
|
||||
}
|
||||
|
||||
private void EnsureOrdered(uint identity, uint bucket, bool insertAtHead)
|
||||
{
|
||||
List<uint> order = GetBucketOrder(bucket);
|
||||
if (order.Contains(identity)) return;
|
||||
if (insertAtHead) order.Insert(0, identity);
|
||||
else order.Add(identity);
|
||||
}
|
||||
|
||||
private List<uint> GetBucketOrder(uint bucket)
|
||||
{
|
||||
if (_enchantmentOrderByBucket.TryGetValue(bucket, out List<uint>? order))
|
||||
return order;
|
||||
order = new List<uint>();
|
||||
_enchantmentOrderByBucket.Add(bucket, order);
|
||||
return order;
|
||||
}
|
||||
|
||||
private void RemoveFromOrder(ActiveEnchantmentRecord record)
|
||||
{
|
||||
if (!_enchantmentOrderByBucket.TryGetValue(record.Bucket, out List<uint>? order))
|
||||
return;
|
||||
order.Remove(record.Identity);
|
||||
if (order.Count == 0)
|
||||
_enchantmentOrderByBucket.Remove(record.Bucket);
|
||||
}
|
||||
}
|
||||
|
||||
public readonly record struct LearnedSpellRecord(uint SpellId, float Power);
|
||||
|
||||
/// <summary>
|
||||
/// Summary of one active enchantment layer on the player. The
|
||||
/// optional StatMod fields (issue #12) carry the wire-level
|
||||
/// `_smod` triad <c>(type, key, val)</c> when available — only
|
||||
/// `PlayerDescription`'s enchantment block currently populates these
|
||||
/// (<see cref="AcDream.Core.Net.Messages.PlayerDescriptionParser"/>).
|
||||
/// `MagicUpdateEnchantment` events still produce records with these
|
||||
/// fields null until the wire parser is extended.
|
||||
/// `_smod` triad <c>(type, key, val)</c>. Both the complete
|
||||
/// `PlayerDescription` snapshot and live MagicUpdateEnchantment events
|
||||
/// populate the same record shape.
|
||||
///
|
||||
/// <para>
|
||||
/// <see cref="Bucket"/> tells <c>EnchantmentMath</c> whether this
|
||||
/// enchantment's StatMod is multiplicative (<c>0x01</c>), additive
|
||||
/// (<c>0x02</c>), cooldown (<c>0x04</c>), or vitae (<c>0x08</c>) per
|
||||
/// (<c>0x02</c>), vitae (<c>0x04</c>), or cooldown (<c>0x08</c>) per
|
||||
/// the retail <c>EnchantmentMask</c> classification.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public readonly record struct ActiveEnchantmentRecord(
|
||||
uint SpellId,
|
||||
uint LayerId,
|
||||
float Duration,
|
||||
double Duration,
|
||||
uint CasterGuid,
|
||||
uint? StatModType = null,
|
||||
uint? StatModKey = null,
|
||||
float? StatModValue = null,
|
||||
uint Bucket = 0);
|
||||
uint Bucket = 0,
|
||||
double StartTime = 0,
|
||||
uint SpellCategory = 0,
|
||||
uint PowerLevel = 0,
|
||||
float DegradeModifier = 0,
|
||||
float DegradeLimit = 0,
|
||||
double LastTimeDegraded = 0,
|
||||
uint? SpellSetId = null)
|
||||
{
|
||||
public uint Identity => MakeIdentity(SpellId, LayerId);
|
||||
|
||||
public static uint MakeIdentity(uint spellId, uint layerId) =>
|
||||
(spellId & 0xFFFFu) | ((layerId & 0xFFFFu) << 16);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue