using System; using System.Collections.Generic; using RynthCore.PluginSdk; namespace RynthCore.Plugin.RynthAi; /// /// Reads each listed weapon's element from its properties () and /// identifies the ones whose DamageType isn't known yet: one identify at a time, at most one /// every , at most per weapon. The /// engine (2026.9.30.5+) keeps every identified property, so a weapon is identified once. /// Before the identify, CreateObject gives the icon's element glow (UiEffects) and the name. /// internal sealed class WeaponElementTracker { private readonly RynthCoreHost _host; private readonly Func _now; private readonly Dictionary _requests = new(); private int _inFlightId; private DateTime _inFlightAt = DateTime.MinValue; private DateTime _lastRequestAt = DateTime.MinValue; public double RequestGapMs { get; set; } = 2000; public double RequestTimeoutMs { get; set; } = 5000; public int MaxRequestsPerItem { get; set; } = 2; /// Identify requests sent (diagnostics and tests). public int RequestsSent { get; private set; } public WeaponElementTracker(RynthCoreHost host, Func? now = null) { _host = host; _now = now ?? (() => RynthClock.Now); } /// What is known about 's element right now. public WeaponElements.Info Read(int id, string? name) => ReadFrom(_host, id, name); /// The same, for callers without a tracker (the Items panel's Add buttons). public static WeaponElements.Info ReadFrom(RynthCoreHost host, int id, string? name) { uint uid = unchecked((uint)id); int? ReadInt(uint stype) => host.HasGetObjectIntProperty && host.TryGetObjectIntProperty(uid, stype, out int v) ? v : null; double? mod = null; if (host.HasGetObjectDoubleProperty && host.TryGetObjectDoubleProperty(uid, WeaponElements.PropElementalDamageMod, out double m)) mod = m; return WeaponElements.Detect(ReadInt(WeaponElements.PropDamageType), ReadInt(WeaponElements.PropImbuedEffect), ReadInt(WeaponElements.PropUiEffects), name, mod); } /// True once the weapon has been identified (its DamageType, if it has one, is in). public bool IsIdentified(int id) { uint uid = unchecked((uint)id); if (_host.HasHasAppraisalData) return _host.HasAppraisalData(uid); return ReadInt(uid, WeaponElements.PropDamageType) is int dt && dt != 0; } /// /// Sends at most one identify, for the first of that isn't identified /// yet. Call it every few hundred milliseconds; it paces itself. Returns the id asked for (0 = none). /// public int Pump(IEnumerable ids) { if (!_host.HasRequestId) return 0; DateTime now = _now(); if (_inFlightId != 0) { if (!IsIdentified(_inFlightId) && (now - _inFlightAt).TotalMilliseconds < RequestTimeoutMs) return 0; _inFlightId = 0; } if ((now - _lastRequestAt).TotalMilliseconds < RequestGapMs) return 0; foreach (int id in ids) { if (id == 0 || IsIdentified(id)) continue; int n = _requests.TryGetValue(id, out int c) ? c : 0; if (n >= MaxRequestsPerItem) continue; bool taken = false; try { taken = _host.RequestId(unchecked((uint)id)); } catch { } _lastRequestAt = now; // paces a retry as it paces a request if (!taken) return 0; // the engine refused (a death or a portal): not counted, asked again after the gap _requests[id] = n + 1; _inFlightId = id; _inFlightAt = now; RequestsSent++; return id; } return 0; } /// Forget the request counts (a relog: the ids are new). public void Reset() { _requests.Clear(); _inFlightId = 0; } private int? ReadInt(uint id, uint stype) { if (!_host.HasGetObjectIntProperty) return null; return _host.TryGetObjectIntProperty(id, stype, out int v) ? v : (int?)null; } }