// Ported from Oracle of Dereth by Advis Eveldan (advis61), MIT. See THIRD_PARTY_NOTICES.md. using System; using System.Collections.Generic; using System.Text.RegularExpressions; namespace RynthCore.Plugin.RynthOracle.Data; internal enum VoidFamily { Corrosion, Corruption, Curse } /// One of your Void damage-over-time spells on a target. internal sealed class VoidDot { public uint TargetId; public string TargetName = ""; public uint SpellId; public string SpellName = ""; public VoidFamily Family; public long StartedMs; public long TickedMs; // 0 until the first "periodic nether damage" line /// Cast while Surge of Destruction was up (shown highlighted). public bool Destruction; /// A server's own rule scales void DoT duration (Conquest's, from RynthConquest's profile; 1.0 elsewhere). public double DurationScale = 1.0; /// Corrosion 15 s, Corruption and Destructive Curse 30 s (upstream's retail values), times the server's scale. public int Duration => (int)((Family == VoidFamily.Corrosion ? 15 : 30) * DurationScale); /// /// Seconds left. Before the first tick: from the cast, plus 2 s for the first tick's delay; /// after it: from the first tick (upstream's timing). /// public int SecondsRemaining(long nowMs) => TickedMs == 0 ? Duration - (int)((nowMs - StartedMs) / 1000) + 2 : Duration - (int)((nowMs - TickedMs) / 1000); } /// /// Tracks your own Corrosion, Corruption and Destructive Curse on each target from the chat /// lines the server sends you ("You cast X on Y", "You scar Y for N points of periodic nether /// damage"). Upstream also uses Decal's SpellCast event for the target's id; RynthCore has no /// spell-cast event for plugins, so the id is the selected target when its name matches, else /// unknown (the spell then matches targets by name). Only your own spells; Corruption's splash /// isn't tracked. /// internal sealed class VoidTracker { private static readonly Regex YouCast = new(@"^You cast (.+?) on (.+?)(?:,.*)?$", RegexOptions.CultureInvariant); private static readonly Regex PeriodicNether = new(@"^You scar (.+?) for (\d+) points of periodic nether damage", RegexOptions.CultureInvariant); private readonly List _dots = new(); private Dictionary? _byName; public IReadOnlyList Dots => _dots; public int Revision { get; private set; } /// /// A chat line. finds a target's id by name; /// says whether Surge of Destruction is on you now. /// public bool OnChatLine(string text, long nowMs, Func resolveTarget, Func destructionUp, double durationScale = 1.0) { if (text.StartsWith("You cast ", StringComparison.Ordinal)) { Match m = YouCast.Match(text); if (!m.Success) return false; if (!SpellsByName().TryGetValue(m.Groups[1].Value.Trim(), out var spell)) return false; string target = m.Groups[2].Value.Trim(); uint targetId = resolveTarget(target); VoidDot? existing = Find(targetId, target, spell.Family, nowMs); bool destruction = destructionUp(); bool keepDestruction = existing != null && existing.Destruction; if (existing != null) _dots.Remove(existing); _dots.Insert(0, new VoidDot { TargetId = targetId, TargetName = target, SpellId = spell.Id, SpellName = m.Groups[1].Value.Trim(), Family = spell.Family, StartedMs = nowMs, DurationScale = durationScale > 0 ? durationScale : 1.0, Destruction = keepDestruction || (destruction && existing == null), }); Revision++; return true; } if (text.StartsWith("You scar ", StringComparison.Ordinal)) { Match m = PeriodicNether.Match(text); if (!m.Success) return false; string target = m.Groups[1].Value.Trim(); foreach (VoidDot d in _dots) if (d.TickedMs == 0 && d.TargetName == target) d.TickedMs = nowMs; Revision++; return true; } return false; } private VoidDot? Find(uint targetId, string targetName, VoidFamily family, long nowMs) { foreach (VoidDot d in _dots) if (d.Family == family && d.SecondsRemaining(nowMs) > 0 && (targetId != 0 ? d.TargetId == targetId : d.TargetName == targetName)) return d; return null; } /// /// Seconds left on a family's spell on a target, or -1; destruction = cast with the surge up. /// A spell whose target id wasn't known at the cast matches by name. /// public int Remaining(uint targetId, string targetName, VoidFamily family, long nowMs, out bool destruction) { destruction = false; foreach (VoidDot d in _dots) { if (d.Family != family) continue; if (d.TargetId != 0 ? d.TargetId != targetId : d.TargetName != targetName) continue; int s = d.SecondsRemaining(nowMs); if (s <= 0) continue; destruction = d.Destruction; return s; } return -1; } public void RemoveExpired(long nowMs) { if (_dots.RemoveAll(d => d.SecondsRemaining(nowMs) <= 0) > 0) Revision++; } public void Clear() { _dots.Clear(); Revision++; } private Dictionary SpellsByName() { if (_byName != null) return _byName; var map = new Dictionary(StringComparer.OrdinalIgnoreCase); void Add(HashSet ids, VoidFamily f) { foreach (uint id in ids) if (SpellTable.TryGet(id, out SpellInfo s) && !string.IsNullOrEmpty(s.Name)) map[s.Name] = (id, f); } Add(SpellIds.Corrosion, VoidFamily.Corrosion); Add(SpellIds.Corruption, VoidFamily.Corruption); Add(SpellIds.Curse, VoidFamily.Curse); return _byName = map; } }