using System; using System.Collections.Generic; using System.IO; namespace RynthCore.Plugin.RynthAi; /// /// Buff profiles (Spells window / Loaded Files → Buffs): when one is chosen, its enabled /// spells replace the built-in buff list, in the profile's order. Each entry is a base name /// ("Strength Self") resolved to the best tier the character knows like the built-in list; /// spells without a tier ladder (server-custom or lore names) fall back to the best known /// spell of the same ACE spell family. /// public partial class BuffManager { // Re-checking the file's timestamp every call would hit the disk ~30x a second. private const int BuffProfileRecheckMs = 3000; private string _buffProfilePath = string.Empty; private DateTime _buffProfileWriteUtc; private long _buffProfileCheckedAt = long.MinValue / 2; private List? _profileKeys; private readonly Dictionary _profileByKey = new(StringComparer.OrdinalIgnoreCase); /// Name of the buff profile in use, or null when buffing uses its built-in list. public string? ActiveBuffProfileName { get; private set; } /// Spells (enabled entries) in the active buff profile; 0 with no profile. public int ActiveBuffProfileCount => _profileKeys?.Count ?? 0; /// /// Follows : loads the profile when the /// path changes or the file is re-saved (the Spells window's Save), drops it when the path is /// cleared. A missing or unreadable file means the built-in list, with one chat line. /// private void SyncBuffProfile() { string path = _settings.CurrentBuffProfilePath ?? string.Empty; long now = Environment.TickCount64; bool pathChanged = !path.Equals(_buffProfilePath, StringComparison.OrdinalIgnoreCase); if (!pathChanged && now - _buffProfileCheckedAt < BuffProfileRecheckMs) return; _buffProfileCheckedAt = now; if (path.Length == 0) { if (pathChanged) ClearBuffProfile("[RynthAi] Buffing: built-in buff list."); _buffProfilePath = path; return; } DateTime writeUtc; try { writeUtc = File.Exists(path) ? File.GetLastWriteTimeUtc(path) : DateTime.MinValue; } catch { writeUtc = DateTime.MinValue; } if (!pathChanged && writeUtc == _buffProfileWriteUtc) return; _buffProfilePath = path; _buffProfileWriteUtc = writeUtc; SpellCatalog.EnsureLoaded(m => _host.Log(m)); if (!BuffProfileStore.TryLoad(path, out BuffProfile profile, out string error)) { ClearBuffProfile($"[RynthAi] Buff profile '{Path.GetFileNameWithoutExtension(path)}' could not be read ({error}) - using the built-in buff list."); return; } ApplyBuffProfile(profile); _host.WriteToChat($"[RynthAi] Buffing: profile '{profile.Name}' ({ActiveBuffProfileCount} spell(s)).", 5); } /// Uses 's enabled entries as the buff list (tests call this directly). internal void ApplyBuffProfile(BuffProfile profile) { SpellCatalog.EnsureLoaded(m => _host.Log(m)); _profileByKey.Clear(); var keys = new List(profile.Entries.Count); var instant = new List(); foreach (var e in profile.Entries) { if (!e.Enabled || string.IsNullOrWhiteSpace(e.Key)) continue; // One cast per base name: two tiers of the same line would fight over one family. if (_profileByKey.ContainsKey(e.Key)) continue; // A spell with no duration (Mana Boost restores mana at once) never stays on, so as a buff // it would be cast again on every pass. if (SpellCatalog.Get(e.SpellId) is { DurationSec: < 0 }) { instant.Add(e.Name); continue; } _profileByKey[e.Key] = e; keys.Add(e.Key); } _profileKeys = keys; ActiveBuffProfileName = profile.Name; if (instant.Count > 0) _host.Log($"[RynthAi] Buff profile '{profile.Name}': left out {string.Join(", ", instant)} (no duration: not a buff, it would be recast every pass)."); } private void ClearBuffProfile(string chatLine) { bool had = _profileKeys != null; _profileKeys = null; _profileByKey.Clear(); ActiveBuffProfileName = null; if (had || chatLine.Contains("could not be read", StringComparison.Ordinal)) _host.WriteToChat(chatLine, 5); } /// The active profile's base names, or null to use the built-in list. private List? ProfileBuffList() { SyncBuffProfile(); return _profileKeys is { Count: > 0 } keys ? new List(keys) : null; } /// Casting skill for a buff: the catalog school for profile spells, else by name. internal AcSkillType SkillFor(string baseName) => _profileByKey.TryGetValue(baseName, out var e) && e.School.Length > 0 ? SpellCatalog.SkillForSchool(e.School) : SkillForBuff(baseName); /// /// Profile spells whose base name has no tier ladder: the best spell of the same family the /// character knows, capped by the buffing tier for its skill. Self and item spells win over /// Other ones at the same level. 0 when the spellbook snapshot is cold or nothing is known. /// private int ResolveProfileFallback(string baseName, AcSkillType skill) { if (!_profileByKey.TryGetValue(baseName, out var entry) || !_spellManager.IsKnownSnapshotWarm) return 0; int maxTier = _spellManager.GetHighestBuffSpellTier(skill); SpellCatalogEntry? best = null; foreach (var c in SpellCatalog.Family(entry.FamilyId)) { // Fellowship versions are Use Fellowship Buffs' to pick (FellowshipVersion), not the profile's. if (!c.IsBuff || c.Level > maxTier || !_spellManager.IsKnownSpellId(c.Id) || IsFellowshipSpell(c)) continue; if (LevelUnusable(c.Id)) continue; // refused by the server for components: a lower level if (best == null || Rank(c) > Rank(best)) best = c; } if (best != null) return best.Id; // Family unknown to the catalog: the exact spell picked in the Spells window. return entry.SpellId != 0 && _spellManager.IsKnownSpellId(entry.SpellId) && !LevelUnusable(entry.SpellId) ? entry.SpellId : 0; static long Rank(SpellCatalogEntry c) => (long)c.Level * 1_000_000 + (c.IsSelf || c.IsItemTarget ? 100_000 : 0) + c.Power; } }