acdream/src/AcDream.Plugins.MossTank/BuffProfile.cs
Erik 17ebfc434d feat(mosstank): buff trained skills and attributes, pick tiers by skill, manage mana
Reworks MossTank against user feedback and the Virindi Tank feature docs
(virindi.net is reachable again over https with a self-signed cert; the
research doc's "unreachable" note is stale).

VTank's stated default is the spec: "automatically buffs every Attribute and
Skill you have trained", and "all buff spells are recast when they go below 5
minutes". The previous pass buffed the whole spellbook and refreshed at 60s;
both are corrected.

The hard problem was working out WHICH stat each buff raises. The client's
spell table has no such link -- it arrives from the server with the
enchantment -- and the naming is too irregular to infer: Invulnerability
raises Melee Defense, Impregnability raises Missile Defense, Fealty raises
Loyalty, Sprint raises Run, Arcane Enlightenment raises Arcane Lore, and the
line called Willpower raises the attribute named Self. Any name-matching
scheme dies on that last one.

Retail states it outright in each spell's own description ("Increases the
caster's Life Magic skill by 10 points"), so BuffProfile derives the whole
mapping from shipped data at runtime. It also carries the one alias the data
needs: the spell text says "Assess Monster" where the skill table says "Assess
Creature", and without that the skill silently never matches.

Two data facts that would each have caused a real bug, found by dumping the
spell table rather than assuming:

* Family is NOT a spell-line identity in general. Retail groups the
  instantaneous vital transfers by SOURCE vital, so family 89 holds both
  "Stamina to Health" and "Stamina to Mana". Picking the strongest tier in a
  family would convert into the wrong vital about half the time. Buff lines
  group by family (correct for duration buffs, which is retail's own stacking
  bucket); the conversions are found by name stem instead.
* Instantaneous spells have no duration and must be excluded from buff lines
  entirely, or they are treated as buffs that never appear to land.

Tier selection now follows the character's skill in the casting school against
the spell's difficulty (VTank's SpellDiffExcessThreshold-Buff), which is why
PluginSpellInfo gained School as a SKILL id -- MagicSchool is retail's 1-5
school enum, not something a character trains.

Mana upkeep is the loop asked for: convert stamina to mana when mana is low,
Revitalize when that leaves stamina too low to convert, and refuse to drain
stamina past a floor. Unknown vitals read as zero and are treated as "no
information" rather than "empty", so it will not cast on a healthy character.

Panel no longer shows at character select. IsAvailable is now the runtime's
own lifecycle state rather than a proxy, and markup gained visible="{Binding}"
plus UiElement.VisibleSource -- evaluated before the visible gate, because
TickSelfAndChildren returns early when hidden and an element could otherwise
never un-hide itself.

Also: a generated SpellId enum of all 6,266 spells (tools/SpellDump --enum),
generated from portal.dat rather than copied, so it cannot drift and carries
no third-party licence; skill and spell names now come from the retail tables
for display; and the Buff click logs unconditionally, so "nothing happened"
can be told apart from "the click never arrived".

Solution builds clean; 14,433 tests pass on the standard hermetic lane filter,
0 failures, including 21 covering the buff profile, tier selection and mana
loop.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-20 18:09:02 +02:00

119 lines
4.3 KiB
C#

using System.Text.RegularExpressions;
using AcDream.Plugin.Abstractions;
namespace AcDream.Plugins.MossTank;
/// <summary>What a buff line raises.</summary>
public enum BuffTargetKind
{
Unknown = 0,
Skill,
Attribute,
}
/// <summary>One buff line: a family, what it raises, and its known tiers.</summary>
public sealed record BuffLine(
uint Family,
BuffTargetKind Kind,
string TargetName,
List<PluginSpellInfo> Tiers);
/// <summary>
/// Works out which stat each known self-buff raises, straight from retail data.
/// </summary>
/// <remarks>
/// <para>
/// The client's spell table carries no link between a spell and the stat it
/// modifies — that arrives from the server with the enchantment. But retail
/// writes it in the spell's own description:
/// </para>
/// <code>
/// Increases the caster's Life Magic skill by 10 points.
/// Increases the caster's Strength by 10 points.
/// </code>
/// <para>
/// So the mapping is derived from shipped data rather than hard-coded, which
/// matters because the naming is genuinely irregular and no rule would cover
/// it: <b>Invulnerability</b> raises Melee Defense, <b>Impregnability</b>
/// raises Missile Defense, <b>Fealty</b> raises Loyalty, <b>Sprint</b> raises
/// Run, <b>Arcane Enlightenment</b> raises Arcane Lore, and — the one that
/// would silently poison any name-matching scheme — the spell line called
/// <b>Willpower</b> raises the attribute named <b>Self</b>.
/// </para>
/// </remarks>
public static partial class BuffProfile
{
[GeneratedRegex(
@"^Increases (?:the caster's|your) (?<target>.+?)(?<skill>\s+skill)? by ",
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)]
private static partial Regex IncreasesPattern();
/// <summary>
/// Retail's spell text says "Assess Monster" where the skill table says
/// "Assess Creature". Without this the skill silently never matches and its
/// buff is quietly dropped from every plan.
/// </summary>
private static readonly Dictionary<string, string> SkillNameAliases =
new(StringComparer.OrdinalIgnoreCase)
{
["Assess Monster"] = "Assess Creature",
};
/// <summary>
/// Group the character's known self-buffs into buff lines, keeping only
/// lines that raise a stat and last long enough to be worth maintaining.
/// </summary>
public static List<BuffLine> Build(IReadOnlyList<PluginSpellInfo> knownSelfBuffs)
{
var byFamily = new Dictionary<uint, BuffLine>();
foreach (PluginSpellInfo spell in knownSelfBuffs)
{
// Instantaneous spells (the vital transfers, heals) are not buffs;
// they also share families across unrelated lines, so grouping them
// by family would be wrong twice over.
if (spell.DurationSeconds <= 0f)
continue;
if (!TryParseTarget(spell.Description, out BuffTargetKind kind, out string target))
continue;
if (!byFamily.TryGetValue(spell.Family, out BuffLine? line))
{
line = new BuffLine(spell.Family, kind, target, new List<PluginSpellInfo>());
byFamily.Add(spell.Family, line);
}
line.Tiers.Add(spell);
}
foreach (BuffLine line in byFamily.Values)
line.Tiers.Sort(static (a, b) => b.Tier.CompareTo(a.Tier)); // strongest first
return byFamily.Values.ToList();
}
/// <summary>Parse "Increases the caster's X [skill] by N points."</summary>
public static bool TryParseTarget(
string? description, out BuffTargetKind kind, out string target)
{
kind = BuffTargetKind.Unknown;
target = string.Empty;
if (string.IsNullOrEmpty(description))
return false;
Match match = IncreasesPattern().Match(description);
if (!match.Success)
return false;
target = match.Groups["target"].Value.Trim();
if (target.Length == 0)
return false;
if (SkillNameAliases.TryGetValue(target, out string? alias))
target = alias;
// The word "skill" is what separates a skill buff from an attribute
// buff in retail's own wording.
kind = match.Groups["skill"].Success ? BuffTargetKind.Skill : BuffTargetKind.Attribute;
return true;
}
}