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>
This commit is contained in:
parent
9d1117b923
commit
17ebfc434d
18 changed files with 14052 additions and 279 deletions
|
|
@ -2,108 +2,154 @@ using AcDream.Plugin.Abstractions;
|
|||
|
||||
namespace AcDream.Plugins.MossTank;
|
||||
|
||||
/// <summary>
|
||||
/// Decides which self-buffs are missing and in what order to cast them.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// Pure function of (known self-buffs, active enchantments). No host calls, no
|
||||
/// state, no clock — so the buff policy can be reasoned about and tested
|
||||
/// without a live session, which is the whole reason it lives here rather than
|
||||
/// in the host.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// <b>Why the spellbook is the source of truth.</b> The obvious reading of
|
||||
/// "buff every trained and specialised skill" is to enumerate skills and map
|
||||
/// each to its buff line. The client cannot do that honestly: the link between
|
||||
/// a spell and the stat it modifies arrives from the <em>server</em>, in the
|
||||
/// enchantment message, and is absent from the client's own spell table. What
|
||||
/// the client does know is which spells the character has learned — and a
|
||||
/// character only learns the buffs for the skills they actually use. Driving
|
||||
/// from the spellbook reaches the same set without inventing a mapping the
|
||||
/// client has no grounds for.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
internal static class BuffPlan
|
||||
/// <summary>Settings that shape a buff pass. Defaults follow Virindi Tank's.</summary>
|
||||
public sealed record BuffSettings
|
||||
{
|
||||
/// <summary>
|
||||
/// The strongest known buff per family that is not already in force at an
|
||||
/// equal or higher tier, ordered so the plan is stable between passes.
|
||||
/// VTank recasts buffs once they drop below five minutes remaining
|
||||
/// ("all buff spells are recast when they go below 5 minutes").
|
||||
/// </summary>
|
||||
public static List<PluginSpellInfo> Build(
|
||||
IReadOnlyList<PluginSpellInfo> knownSelfBuffs,
|
||||
IReadOnlyList<PluginActiveEnchantment> active,
|
||||
double refreshWhenUnderSeconds)
|
||||
{
|
||||
// Best known candidate per family. Family 0 is retail's "does not
|
||||
// stack" bucket: those spells share no family identity, so collapsing
|
||||
// them would drop all but one unrelated buff.
|
||||
var bestByFamily = new Dictionary<uint, PluginSpellInfo>();
|
||||
var unstackable = new List<PluginSpellInfo>();
|
||||
public double RebuffWhenUnderSeconds { get; init; } = 300.0;
|
||||
|
||||
foreach (PluginSpellInfo spell in knownSelfBuffs)
|
||||
/// <summary>
|
||||
/// How far the casting skill must exceed a spell's difficulty before the
|
||||
/// tier is considered reliable — VTank's
|
||||
/// <c>SpellDiffExcessThreshold-Buff</c>.
|
||||
/// </summary>
|
||||
public int SkillExcessOverDifficulty { get; init; } = 10;
|
||||
|
||||
/// <summary>Buff every attribute (VTank's default).</summary>
|
||||
public bool BuffAttributes { get; init; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Buff trained and specialised skills only — VTank's stated default:
|
||||
/// "automatically buffs every Attribute and Skill you have trained".
|
||||
/// </summary>
|
||||
public bool BuffTrainedSkillsOnly { get; init; } = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Chooses which buffs to cast, at which tier, in which order.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// A pure function of (buff lines, character state) so the policy can be tested
|
||||
/// without a session. This is the part that stays in plugin-land: the host
|
||||
/// supplies spell data and a cast primitive, MossTank decides.
|
||||
/// </remarks>
|
||||
public static class BuffPlan
|
||||
{
|
||||
public static List<PluginSpellInfo> Build(
|
||||
IReadOnlyList<BuffLine> lines,
|
||||
IReadOnlyList<PluginSkillInfo> skills,
|
||||
IReadOnlyList<PluginAttributeInfo> attributes,
|
||||
IReadOnlyList<PluginActiveEnchantment> active,
|
||||
BuffSettings settings)
|
||||
{
|
||||
var trainedSkills = new Dictionary<string, PluginSkillInfo>(
|
||||
StringComparer.OrdinalIgnoreCase);
|
||||
foreach (PluginSkillInfo skill in skills)
|
||||
{
|
||||
if (spell.Family == 0)
|
||||
if (!settings.BuffTrainedSkillsOnly
|
||||
|| skill.Training is PluginSkillTraining.Trained
|
||||
or PluginSkillTraining.Specialized)
|
||||
{
|
||||
unstackable.Add(spell);
|
||||
continue;
|
||||
}
|
||||
if (!bestByFamily.TryGetValue(spell.Family, out PluginSpellInfo held)
|
||||
|| spell.Tier > held.Tier)
|
||||
{
|
||||
bestByFamily[spell.Family] = spell;
|
||||
trainedSkills[skill.Name] = skill;
|
||||
}
|
||||
}
|
||||
|
||||
// Strongest in-force tier per family, and how long it has left.
|
||||
var activeByFamily = new Dictionary<uint, (int Tier, double Seconds)>();
|
||||
var activeSpellSeconds = new Dictionary<uint, double>();
|
||||
var attributeNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
foreach (PluginAttributeInfo attribute in attributes)
|
||||
attributeNames.Add(attribute.Name);
|
||||
|
||||
// Strongest in-force tier per family, and its remaining time.
|
||||
var inForce = new Dictionary<uint, (int Tier, double Seconds)>();
|
||||
foreach (PluginActiveEnchantment enchantment in active)
|
||||
{
|
||||
activeSpellSeconds[enchantment.SpellId] = enchantment.SecondsRemaining;
|
||||
if (enchantment.Family == 0)
|
||||
continue;
|
||||
if (!activeByFamily.TryGetValue(enchantment.Family, out var held)
|
||||
if (!inForce.TryGetValue(enchantment.Family, out var held)
|
||||
|| enchantment.Tier > held.Tier)
|
||||
{
|
||||
activeByFamily[enchantment.Family] =
|
||||
inForce[enchantment.Family] =
|
||||
(enchantment.Tier, enchantment.SecondsRemaining);
|
||||
}
|
||||
}
|
||||
|
||||
var skillLevels = new Dictionary<uint, uint>();
|
||||
foreach (PluginSkillInfo skill in skills)
|
||||
skillLevels[skill.SkillId] = skill.Current;
|
||||
|
||||
var plan = new List<PluginSpellInfo>();
|
||||
|
||||
foreach (PluginSpellInfo candidate in bestByFamily.Values)
|
||||
foreach (BuffLine line in lines)
|
||||
{
|
||||
if (!activeByFamily.TryGetValue(candidate.Family, out var inForce))
|
||||
bool wanted = line.Kind switch
|
||||
{
|
||||
plan.Add(candidate);
|
||||
BuffTargetKind.Attribute =>
|
||||
settings.BuffAttributes && attributeNames.Contains(line.TargetName),
|
||||
BuffTargetKind.Skill => trainedSkills.ContainsKey(line.TargetName),
|
||||
_ => false,
|
||||
};
|
||||
if (!wanted)
|
||||
continue;
|
||||
}
|
||||
// A weaker enchantment in force is still worth replacing: recasting
|
||||
// at a higher tier supersedes it.
|
||||
if (candidate.Tier > inForce.Tier
|
||||
|| inForce.Seconds < refreshWhenUnderSeconds)
|
||||
|
||||
if (!TryPickTier(line, skillLevels, settings, out PluginSpellInfo pick))
|
||||
continue;
|
||||
|
||||
if (inForce.TryGetValue(line.Family, out var held)
|
||||
&& held.Tier >= pick.Tier
|
||||
&& held.Seconds >= settings.RebuffWhenUnderSeconds)
|
||||
{
|
||||
plan.Add(candidate);
|
||||
continue; // already covered at this strength, and not expiring
|
||||
}
|
||||
|
||||
plan.Add(pick);
|
||||
}
|
||||
|
||||
foreach (PluginSpellInfo candidate in unstackable)
|
||||
{
|
||||
if (!activeSpellSeconds.TryGetValue(candidate.SpellId, out double seconds)
|
||||
|| seconds < refreshWhenUnderSeconds)
|
||||
{
|
||||
plan.Add(candidate);
|
||||
}
|
||||
}
|
||||
|
||||
// Cheapest first: if mana runs out mid-pass, more buffs landed than if
|
||||
// the expensive ones had gone first.
|
||||
// Cheapest first: if mana runs out mid-pass, more buffs land than if the
|
||||
// expensive ones had gone first.
|
||||
plan.Sort(static (a, b) =>
|
||||
a.ManaCost != b.ManaCost
|
||||
? a.ManaCost.CompareTo(b.ManaCost)
|
||||
: a.SpellId.CompareTo(b.SpellId));
|
||||
return plan;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The strongest tier the character's skill in that school can carry.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Tiers are pre-sorted strongest-first, so the first one clearing the
|
||||
/// difficulty threshold is the answer. When no school skill is known the
|
||||
/// threshold cannot be evaluated, and the weakest tier is chosen rather
|
||||
/// than none — a buff that lands beats a buff that was never attempted.
|
||||
/// </remarks>
|
||||
public static bool TryPickTier(
|
||||
BuffLine line,
|
||||
IReadOnlyDictionary<uint, uint> skillLevels,
|
||||
BuffSettings settings,
|
||||
out PluginSpellInfo pick)
|
||||
{
|
||||
pick = default;
|
||||
if (line.Tiers.Count == 0)
|
||||
return false;
|
||||
|
||||
foreach (PluginSpellInfo tier in line.Tiers)
|
||||
{
|
||||
if (tier.School == 0 || !skillLevels.TryGetValue(tier.School, out uint level))
|
||||
continue;
|
||||
if (level >= tier.Difficulty + settings.SkillExcessOverDifficulty)
|
||||
{
|
||||
pick = tier;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
PluginSpellInfo weakest = line.Tiers[^1];
|
||||
if (weakest.School != 0 && skillLevels.ContainsKey(weakest.School))
|
||||
return false; // school known, but even the weakest tier is out of reach
|
||||
|
||||
pick = weakest;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
119
src/AcDream.Plugins.MossTank/BuffProfile.cs
Normal file
119
src/AcDream.Plugins.MossTank/BuffProfile.cs
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
|
@ -6,34 +6,21 @@ namespace AcDream.Plugins.MossTank;
|
|||
/// The panel's binding object and the buff loop's state machine.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// The markup binds <c>{Buff}</c> to <see cref="Buff"/> and its labels to the
|
||||
/// status properties. Everything here runs on the host's update thread — the
|
||||
/// button click and the tick both arrive there — so no locking is needed, and
|
||||
/// none is used, deliberately: adding a lock would imply a second thread that
|
||||
/// does not exist.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// <b>Pacing.</b> The loop casts one spell, then waits. There is no
|
||||
/// cast-completed event in the plugin API yet, so completion is inferred the
|
||||
/// honest way: re-evaluate the plan each pass and let landed buffs drop out of
|
||||
/// it. A fizzle simply leaves its buff missing and it gets retried on the next
|
||||
/// pass. That is self-correcting without pretending to know an outcome the
|
||||
/// host has not reported.
|
||||
/// </para>
|
||||
/// Everything here runs on the host's update thread — the button click and the
|
||||
/// tick both arrive there — so no locking is used, deliberately: a lock would
|
||||
/// imply a second thread that does not exist.
|
||||
/// </remarks>
|
||||
internal sealed class MossTankPanel
|
||||
{
|
||||
/// <summary>Roughly a retail cast plus windup, so casts do not stack up.</summary>
|
||||
private const double CastIntervalSeconds = 3.0;
|
||||
|
||||
/// <summary>Refresh a buff already in force but nearly expired.</summary>
|
||||
private const double RefreshWhenUnderSeconds = 60.0;
|
||||
|
||||
/// <summary>Give up on a pass that stops making progress.</summary>
|
||||
private const double StallTimeoutSeconds = 20.0;
|
||||
private const double StallTimeoutSeconds = 25.0;
|
||||
|
||||
private readonly IPluginHost _host;
|
||||
private readonly BuffSettings _buffSettings = new();
|
||||
private readonly VitalSettings _vitalSettings = new();
|
||||
|
||||
private List<PluginSpellInfo> _plan = new();
|
||||
private int _planIndex;
|
||||
|
|
@ -48,28 +35,62 @@ internal sealed class MossTankPanel
|
|||
/// <summary>Bound to the panel's Buff button.</summary>
|
||||
public Action Buff => StartOrStop;
|
||||
|
||||
public string Title => "MossTank";
|
||||
public string Status => _status;
|
||||
/// <summary>
|
||||
/// Bound to the panel's <c>visible</c>. Keeps the window off the character
|
||||
/// select and login screens, where there is no character to buff.
|
||||
/// </summary>
|
||||
public bool IsInWorld => _host.Automation.IsAvailable;
|
||||
|
||||
public string ButtonText => _running ? "Stop" : "Buff";
|
||||
|
||||
public string Detail
|
||||
public string Status => _status;
|
||||
|
||||
/// <summary>Vitals line, using the same numbers the character panel shows.</summary>
|
||||
public string Vitals
|
||||
{
|
||||
get
|
||||
{
|
||||
ICharacterInfo character = _host.Automation.Character;
|
||||
if (!_host.Automation.IsAvailable)
|
||||
return string.Empty;
|
||||
return $"Health {character.CurrentHealth}/{character.MaxHealth}"
|
||||
+ $" Stam {character.CurrentStamina}/{character.MaxStamina}"
|
||||
+ $" Mana {character.CurrentMana}/{character.MaxMana}";
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>What a buff pass would cover, named from the retail tables.</summary>
|
||||
public string Coverage
|
||||
{
|
||||
get
|
||||
{
|
||||
IAutomationSurface automation = _host.Automation;
|
||||
if (!automation.IsAvailable)
|
||||
return "Not in world.";
|
||||
int known = automation.Spells.KnownSelfBuffs.Count;
|
||||
int active = automation.Character.ActiveEnchantments.Count;
|
||||
return $"{known} self-buffs known / {active} active"
|
||||
+ $" · mana {automation.Character.CurrentMana}"
|
||||
+ $"/{automation.Character.MaxMana}";
|
||||
return string.Empty;
|
||||
|
||||
int trained = 0;
|
||||
foreach (PluginSkillInfo skill in automation.Character.Skills)
|
||||
{
|
||||
if (skill.Training is PluginSkillTraining.Trained
|
||||
or PluginSkillTraining.Specialized)
|
||||
{
|
||||
trained++;
|
||||
}
|
||||
}
|
||||
int attributes = automation.Character.Attributes.Count;
|
||||
int lines = BuffProfile.Build(automation.Spells.KnownSelfBuffs).Count;
|
||||
return $"{attributes} attributes, {trained} trained skills, {lines} buff lines known";
|
||||
}
|
||||
}
|
||||
|
||||
private void StartOrStop()
|
||||
{
|
||||
// Logged unconditionally: "nothing happened when I clicked" is
|
||||
// ambiguous between the click never arriving and the click arriving and
|
||||
// declining to act. This line separates those two without guesswork.
|
||||
_host.Log.Info(
|
||||
$"MossTank: Buff clicked (running={_running}, inWorld={_host.Automation.IsAvailable})");
|
||||
|
||||
if (_running)
|
||||
{
|
||||
Stop("Stopped.");
|
||||
|
|
@ -83,24 +104,16 @@ internal sealed class MossTankPanel
|
|||
return;
|
||||
}
|
||||
|
||||
_plan = BuffPlan.Build(
|
||||
automation.Spells.KnownSelfBuffs,
|
||||
automation.Character.ActiveEnchantments,
|
||||
RefreshWhenUnderSeconds);
|
||||
_plan = BuildPlan(automation);
|
||||
_planIndex = 0;
|
||||
_castThisPass = 0;
|
||||
_sinceLastCast = CastIntervalSeconds; // cast the first one immediately
|
||||
_sinceProgress = 0;
|
||||
|
||||
if (_plan.Count == 0)
|
||||
{
|
||||
_status = "Already fully buffed.";
|
||||
return;
|
||||
}
|
||||
|
||||
_running = true;
|
||||
_status = $"Buffing 0/{_plan.Count}…";
|
||||
_host.Log.Info($"MossTank: buff pass started, {_plan.Count} spell(s) to cast");
|
||||
_status = _plan.Count == 0
|
||||
? "Checking…"
|
||||
: $"Buffing 0/{_plan.Count}…";
|
||||
_host.Log.Info($"MossTank: pass started, {_plan.Count} buff(s) queued");
|
||||
}
|
||||
|
||||
private void Stop(string status)
|
||||
|
|
@ -111,6 +124,25 @@ internal sealed class MossTankPanel
|
|||
_status = status;
|
||||
}
|
||||
|
||||
private List<PluginSpellInfo> BuildPlan(IAutomationSurface automation)
|
||||
{
|
||||
List<BuffLine> lines = BuffProfile.Build(automation.Spells.KnownSelfBuffs);
|
||||
return BuffPlan.Build(
|
||||
lines,
|
||||
automation.Character.Skills,
|
||||
automation.Character.Attributes,
|
||||
automation.Character.ActiveEnchantments,
|
||||
_buffSettings);
|
||||
}
|
||||
|
||||
private Dictionary<uint, uint> SkillLevels(IAutomationSurface automation)
|
||||
{
|
||||
var levels = new Dictionary<uint, uint>();
|
||||
foreach (PluginSkillInfo skill in automation.Character.Skills)
|
||||
levels[skill.SkillId] = skill.Current;
|
||||
return levels;
|
||||
}
|
||||
|
||||
/// <summary>Driven by <see cref="IEvents.Tick"/> on the host update thread.</summary>
|
||||
public void OnTick(double elapsedSeconds)
|
||||
{
|
||||
|
|
@ -130,24 +162,26 @@ internal sealed class MossTankPanel
|
|||
if (_sinceProgress > StallTimeoutSeconds)
|
||||
{
|
||||
Stop($"Stalled after {_castThisPass} cast(s).");
|
||||
_host.Log.Warn("MossTank: buff pass stalled; stopping");
|
||||
_host.Log.Warn("MossTank: pass stalled; stopping");
|
||||
return;
|
||||
}
|
||||
|
||||
if (_sinceLastCast < CastIntervalSeconds || automation.Magic.IsCasting)
|
||||
return;
|
||||
|
||||
// Vitals come first: a buff pass that runs itself out of mana and keeps
|
||||
// trying is worse than one that pauses to convert stamina.
|
||||
if (TryVitalUpkeep(automation))
|
||||
return;
|
||||
|
||||
// Re-derive against current enchantments so anything that landed since
|
||||
// the pass began drops out rather than being cast twice.
|
||||
_plan = BuffPlan.Build(
|
||||
automation.Spells.KnownSelfBuffs,
|
||||
automation.Character.ActiveEnchantments,
|
||||
RefreshWhenUnderSeconds);
|
||||
_plan = BuildPlan(automation);
|
||||
|
||||
if (_plan.Count == 0)
|
||||
{
|
||||
Stop($"Done — {_castThisPass} cast(s).");
|
||||
_host.Log.Info($"MossTank: buff pass complete ({_castThisPass} cast)");
|
||||
_host.Log.Info($"MossTank: pass complete ({_castThisPass} cast)");
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -155,30 +189,54 @@ internal sealed class MossTankPanel
|
|||
_planIndex = 0;
|
||||
|
||||
PluginSpellInfo next = _plan[_planIndex];
|
||||
PluginCastGate gate = automation.Magic.EvaluateGate(next.SpellId);
|
||||
if (gate != PluginCastGate.Ready)
|
||||
{
|
||||
// Skip it rather than blocking the pass; the next tick tries the
|
||||
// one after. A permanently ungateable spell falls out when the
|
||||
// stall timeout fires.
|
||||
if (!TryCast(automation, next, $"Buffing {_castThisPass + 1}/{_plan.Count}"))
|
||||
_planIndex++;
|
||||
_status = $"Skipped {next.Name} ({gate}).";
|
||||
return;
|
||||
}
|
||||
|
||||
private bool TryVitalUpkeep(IAutomationSurface automation)
|
||||
{
|
||||
VitalAction action = VitalPlan.Decide(automation.Character, _vitalSettings);
|
||||
if (action == VitalAction.None)
|
||||
return false;
|
||||
|
||||
string stem = action == VitalAction.StaminaToMana
|
||||
? VitalPlan.StaminaToManaStem
|
||||
: VitalPlan.RevitalizeStem;
|
||||
|
||||
if (!VitalPlan.TryFind(
|
||||
automation.Spells.KnownSelfBuffs, stem, SkillLevels(automation),
|
||||
_buffSettings.SkillExcessOverDifficulty, out PluginSpellInfo spell))
|
||||
{
|
||||
// Not knowing the conversion is not an error — plenty of characters
|
||||
// do not have it. Fall through to buffing rather than stalling.
|
||||
return false;
|
||||
}
|
||||
|
||||
if (automation.Magic.Cast(next.SpellId))
|
||||
return TryCast(automation, spell, action.ToString());
|
||||
}
|
||||
|
||||
private bool TryCast(
|
||||
IAutomationSurface automation, PluginSpellInfo spell, string label)
|
||||
{
|
||||
PluginCastGate gate = automation.Magic.EvaluateGate(spell.SpellId);
|
||||
if (gate != PluginCastGate.Ready)
|
||||
{
|
||||
_castThisPass++;
|
||||
_sinceLastCast = 0;
|
||||
_sinceProgress = 0;
|
||||
_planIndex = 0;
|
||||
_status = $"Casting {next.Name} ({_castThisPass} cast)…";
|
||||
_host.Log.Info($"MossTank: casting {next.Name} (0x{next.SpellId:X4})");
|
||||
_status = $"{spell.Name}: {gate}";
|
||||
return false;
|
||||
}
|
||||
else
|
||||
|
||||
if (!automation.Magic.Cast(spell.SpellId))
|
||||
{
|
||||
_planIndex++;
|
||||
_status = $"Refused {next.Name}.";
|
||||
_status = $"Refused {spell.Name}.";
|
||||
return false;
|
||||
}
|
||||
|
||||
_castThisPass++;
|
||||
_sinceLastCast = 0;
|
||||
_sinceProgress = 0;
|
||||
_planIndex = 0;
|
||||
_status = $"{label}: {spell.Name}";
|
||||
_host.Log.Info($"MossTank: casting {spell.Name} (0x{spell.SpellId:X4})");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
12544
src/AcDream.Plugins.MossTank/SpellId.g.cs
Normal file
12544
src/AcDream.Plugins.MossTank/SpellId.g.cs
Normal file
File diff suppressed because it is too large
Load diff
111
src/AcDream.Plugins.MossTank/VitalPlan.cs
Normal file
111
src/AcDream.Plugins.MossTank/VitalPlan.cs
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
using AcDream.Plugin.Abstractions;
|
||||
|
||||
namespace AcDream.Plugins.MossTank;
|
||||
|
||||
/// <summary>What MossTank wants to do about the character's vitals right now.</summary>
|
||||
public enum VitalAction
|
||||
{
|
||||
None = 0,
|
||||
/// <summary>Convert stamina into mana.</summary>
|
||||
StaminaToMana,
|
||||
/// <summary>Restore stamina, so stamina-to-mana has something to convert.</summary>
|
||||
Revitalize,
|
||||
}
|
||||
|
||||
/// <summary>Thresholds for vital upkeep, following VTank's Recharge-* settings.</summary>
|
||||
public sealed record VitalSettings
|
||||
{
|
||||
/// <summary>Convert stamina to mana below this fraction of max mana.</summary>
|
||||
public double ManaFloor { get; init; } = 0.50;
|
||||
|
||||
/// <summary>Stop converting once mana is back above this fraction.</summary>
|
||||
public double ManaTarget { get; init; } = 0.85;
|
||||
|
||||
/// <summary>Refuse to drain stamina below this fraction — the conversion
|
||||
/// takes half your stamina, and stranding the character at zero is worse
|
||||
/// than being short of mana.</summary>
|
||||
public double StaminaFloor { get; init; } = 0.35;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Picks the vital-upkeep spell to cast, if any.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// The loop the user asked for: when mana runs low, convert stamina into mana;
|
||||
/// when that leaves stamina low, restore stamina with Revitalize, which lets
|
||||
/// the conversion continue.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// <b>These spells cannot be identified by family.</b> Retail groups the vital
|
||||
/// transfers by <em>source</em> vital, so family 89 contains both "Stamina to
|
||||
/// Health" and "Stamina to Mana", and family 87 both "Health to Mana" and
|
||||
/// "Health to Stamina". Picking the strongest tier in a family would therefore
|
||||
/// convert into the wrong vital roughly half the time. They are identified by
|
||||
/// their retail name stem instead, which is stable and comes from the same
|
||||
/// spell table.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public static class VitalPlan
|
||||
{
|
||||
public const string StaminaToManaStem = "Stamina to Mana";
|
||||
public const string RevitalizeStem = "Revitalize";
|
||||
|
||||
public static VitalAction Decide(ICharacterInfo character, VitalSettings settings)
|
||||
{
|
||||
double mana = Fraction(character.CurrentMana, character.MaxMana);
|
||||
double stamina = Fraction(character.CurrentStamina, character.MaxStamina);
|
||||
|
||||
// Unknown vitals (no session, or nothing published yet) must not be
|
||||
// read as "empty" — that would cast on a character that is fine.
|
||||
if (character.MaxMana == 0 || character.MaxStamina == 0)
|
||||
return VitalAction.None;
|
||||
|
||||
if (mana >= settings.ManaTarget)
|
||||
return VitalAction.None;
|
||||
|
||||
if (mana < settings.ManaFloor)
|
||||
{
|
||||
return stamina > settings.StaminaFloor
|
||||
? VitalAction.StaminaToMana
|
||||
: VitalAction.Revitalize;
|
||||
}
|
||||
|
||||
return VitalAction.None;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The strongest castable spell whose name contains <paramref name="stem"/>.
|
||||
/// </summary>
|
||||
public static bool TryFind(
|
||||
IReadOnlyList<PluginSpellInfo> known,
|
||||
string stem,
|
||||
IReadOnlyDictionary<uint, uint> skillLevels,
|
||||
int skillExcess,
|
||||
out PluginSpellInfo pick)
|
||||
{
|
||||
pick = default;
|
||||
bool found = false;
|
||||
|
||||
foreach (PluginSpellInfo spell in known)
|
||||
{
|
||||
if (spell.Name.IndexOf(stem, StringComparison.OrdinalIgnoreCase) < 0)
|
||||
continue;
|
||||
if (spell.School != 0
|
||||
&& skillLevels.TryGetValue(spell.School, out uint level)
|
||||
&& level < spell.Difficulty + skillExcess)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (!found || spell.Tier > pick.Tier)
|
||||
{
|
||||
pick = spell;
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
private static double Fraction(uint current, uint max) =>
|
||||
max == 0 ? 1.0 : (double)current / max;
|
||||
}
|
||||
|
|
@ -1,8 +1,10 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- MossTank's panel. Bindings resolve by property name against MossTankPanel:
|
||||
{Buff} is an Action bound to the button, the rest are read every frame. -->
|
||||
<panel x="40" y="120" w="300" h="118" title="MossTank">
|
||||
<label x="12" y="28" text="{Detail}" color="#FFB9C7A0" />
|
||||
<label x="12" y="48" text="{Status}" color="#FFE8E4C8" />
|
||||
<button x="12" y="72" w="104" h="28" text="Buff" onclick="{Buff}" />
|
||||
{Buff} is an Action bound to the button, {IsInWorld} hides the whole panel
|
||||
at character select, the rest are read every frame. -->
|
||||
<panel x="40" y="120" w="360" h="132" title="MossTank" visible="{IsInWorld}">
|
||||
<label x="12" y="30" text="{Vitals}" color="#FFB9C7A0" />
|
||||
<label x="12" y="50" text="{Coverage}" color="#FF8F9C78" />
|
||||
<label x="12" y="70" text="{Status}" color="#FFE8E4C8" />
|
||||
<button x="12" y="94" w="108" h="28" text="{ButtonText}" onclick="{Buff}" />
|
||||
</panel>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue