using System; using System.Collections.Generic; namespace RynthCore.Plugin.RynthAi; // Loot by name (2026-11-02). A short list of names the player types (/ra lootname add Unearthed Cache); // while botting, anything lying in the world with one of those names inside the radius is used: a // container opens (and the chest looter takes it), an item on the ground is picked up. The list, the // matching, the settings string and the attempt bookkeeping live here, with no game types, so they can be tested alone; // RynthAiPlugin.LootByName.cs is the host side. /// The names to loot and how far to look (the settings' Loot By Names and Loot By Name Radius). internal sealed class LootByNameList { public const double DefaultRadius = 30, MinRadius = 3, MaxRadius = 200; public const int MaxNames = 50; private readonly List _names = new(); public double Radius { get; private set; } = DefaultRadius; public IReadOnlyList Names => _names; public bool IsEmpty => _names.Count == 0; /// True when added (false: empty, already there, or the list is full). public bool Add(string? name) { string n = Clean(name); if (n.Length == 0 || _names.Count >= MaxNames || IndexOf(n) >= 0) return false; _names.Add(n); return true; } public bool Remove(string? name) { int i = IndexOf(Clean(name)); if (i < 0) return false; _names.RemoveAt(i); return true; } public void Clear() => _names.Clear(); public void SetRadius(double yards) => Radius = Math.Clamp(yards, MinRadius, MaxRadius); public bool MatchesAny(string? objectName) { if (string.IsNullOrEmpty(objectName)) return false; foreach (string p in _names) if (Matches(p, objectName)) return true; return false; } /// /// Whole name, ignoring case. A * at the start and/or end makes it "ends with", "starts with" or "contains": /// "*cache*" matches "Unearthed Cache". A name with no * never matches part of another ("Cache" does not /// match "Unearthed Cache"), so a short word cannot loot half the world. /// public static bool Matches(string pattern, string name) { if (pattern.Length == 0 || name.Length == 0) return false; bool head = pattern[0] == '*', tail = pattern.Length > 1 && pattern[^1] == '*'; string core = pattern.Trim('*'); if (core.Length == 0) return false; // "*" alone matches nothing var cmp = StringComparison.OrdinalIgnoreCase; if (head && tail) return name.Contains(core, cmp); if (head) return name.EndsWith(core, cmp); if (tail) return name.StartsWith(core, cmp); return name.Equals(core, cmp); } private int IndexOf(string n) { for (int i = 0; i < _names.Count; i++) if (string.Equals(_names[i], n, StringComparison.OrdinalIgnoreCase)) return i; return -1; } private static string Clean(string? s) => (s ?? string.Empty).Trim().Trim('"').Trim(); // ── The settings string ──────────────────────────────────────────────── /// The names as saved in the settings: separated by |. public const char Separator = '|'; /// A list from the settings' names string and radius (blank and repeated names dropped). public static LootByNameList Parse(string? names, double radius) { var list = new LootByNameList(); list.SetRadius(radius > 0 ? radius : DefaultRadius); foreach (string n in (names ?? string.Empty).Split(Separator, StringSplitOptions.RemoveEmptyEntries)) list.Add(n); return list; } /// The names string the settings keep: "Unearthed Cache | *Doubloon*". public string ToSettingString() => string.Join(" | ", _names); /// A names string tidied the way a list would save it (the settings box can hold anything typed). public static string Normalize(string? names) => Parse(names, DefaultRadius).ToSettingString(); } /// /// What was tried on each object: how often it was used and when, which ones are finished (opened, or given up /// on). Entries for objects that left the world are dropped by , so a chest that comes back /// (a new one with a new id, or a regenerating one) is looted again. /// internal sealed class LootNameTracker { /// Uses on one object before giving up on it for a while. public const int MaxUses = 4; /// Between two uses: the client walks to the object in this time. public const long UseGapMs = 5000; /// After MaxUses with nothing to show, the object is left alone this long. public const long GiveUpMs = 120_000; private sealed class Entry { public int Uses; public long LastUseAt; public long BlockedUntil; public bool Done; } private readonly Dictionary _entries = new(); /// The object is finished or being left alone: do not pick it. public bool IsBlocked(int id, long now) => _entries.TryGetValue(id, out var e) && (e.Done || now < e.BlockedUntil); /// A use may go out now: none yet, or the last one is old enough and the budget is not spent. public bool CanUse(int id, long now) { if (!_entries.TryGetValue(id, out var e)) return true; if (e.Done || now < e.BlockedUntil) return false; return e.Uses < MaxUses && now - e.LastUseAt >= UseGapMs; } /// True when this was the last use of the budget and the gap after it has passed: give up on it. public bool Exhausted(int id, long now) => _entries.TryGetValue(id, out var e) && e.Uses >= MaxUses && now - e.LastUseAt >= UseGapMs; public void NoteUse(int id, long now) { if (!_entries.TryGetValue(id, out var e)) _entries[id] = e = new Entry(); e.Uses++; e.LastUseAt = now; } /// Opened or picked up: finished until the object is gone. public void MarkDone(int id) { if (!_entries.TryGetValue(id, out var e)) _entries[id] = e = new Entry(); e.Done = true; } public void GiveUp(int id, long now) { if (!_entries.TryGetValue(id, out var e)) _entries[id] = e = new Entry(); e.Uses = 0; e.BlockedUntil = now + GiveUpMs; } public int Uses(int id) => _entries.TryGetValue(id, out var e) ? e.Uses : 0; /// Forget objects that are no longer in the world. public void Prune(Func stillThere) { List? gone = null; foreach (int id in _entries.Keys) if (!stillThere(id)) (gone ??= new()).Add(id); if (gone != null) foreach (int id in gone) _entries.Remove(id); } public void Clear() => _entries.Clear(); }