using System; using RynthCore.Plugin.Shared; using RynthCore.PluginSdk; namespace RynthCore.Plugin.RynthAi.CreatureData; /// /// The difficulty tier where the player stands and the monster health a tier adds, as the active /// server's plugin reports them, and which server this is for per-server data (2026-10-03 as one /// server's AwakenedTier; server-neutral since 2026-10-21). /// /// Some servers hold the same monsters (same names, same wcids) at several difficulty tiers, with /// health, skills and damage scaled per tier. A server plugin that knows its server's tiers says so /// in its "RynthServer.Profile" values (Plugins/Shared/RynthServerProfileApi.cs): /// "difficultyTier" the tier where the player stands (0 = the unscaled world); /// "difficultyHealthPercent" the health one tier adds, in percent of tier 0's (absent: 0). /// They are read from the plugin that owns RynthCore's active server (ServerStateClient, plugin API /// v86), about once a second. RynthAi knows nothing else about the server. /// /// The rule: while no server plugin reports a tier this is inert - is 0, /// false and every health scale 1 - so every store keyed by tier behaves /// exactly as before. A reported tier 0 is Active (the Damage panel shows the picker) and still /// changes nothing about the numbers. /// /// : the folder of the per-server creature data (CreatureProfileStore), see /// . /// internal sealed class DifficultyTier { /// The largest tier taken as reported (anything above is read as this). public const int MaxTier = 10_000; /// The largest health percent per tier taken (anything else, or below 0, reads as 0). public const double MaxHealthPercent = 1000; /// One reading of the server plugin's values: replaced whole (render and pump threads read it). internal sealed record Reading(bool Active, int Tier, double HealthPercent, string Plugin) { public static readonly Reading None = new(false, 0, 0, string.Empty); } private readonly ServerStateClient _server = new(); private volatile Reading _reading = Reading.None; private volatile string _serverKey = string.Empty; private long _nextRefreshMs; private int _appliedRevision = -1; private string _appliedWorld = string.Empty; /// The active server's plugin reports a difficulty tier. False everywhere else. public bool Active => _reading.Active; /// The tier where the player stands; 0 whenever is false. public int Current => _reading.Active ? _reading.Tier : 0; /// The monster health one tier adds, in percent of tier 0's; 0 whenever is false. public double HealthPercentPerTier => _reading.Active ? _reading.HealthPercent : 0; /// The plugin the tier comes from ("" while none reports one). public string Source => _reading.Plugin; /// /// The server's key for per-server data (creatures.json): see ; /// "" while unknown. /// public string ServerKey => _serverKey; /// The multiplier this server's tier puts on a monster's health (1 without tiers). public double HealthScale(int tier) => HealthScale(tier, HealthPercentPerTier); /// The multiplier a tier puts on a monster's health: 1 + % per tier. public static double HealthScale(int tier, double percentPerTier) => tier <= 0 || percentPerTier <= 0 ? 1.0 : 1.0 + percentPerTier / 100.0 * tier; /// Logout: nothing carries to the next login (it may be another server). public void Reset() { _server.Clear(); _reading = Reading.None; _serverKey = string.Empty; _nextRefreshMs = 0; _appliedRevision = -1; _appliedWorld = string.Empty; } /// Re-reads the server and its tier, at most once a second (: now). Pump thread. public void Refresh(RynthCoreHost host, bool force = false) { long now = Environment.TickCount64; if (!force && now < _nextRefreshMs) return; _nextRefreshMs = now + 1000; try { _server.Poll(host, now, force: true); // throttled here, not by the client's own 2 s string world = host.HasGetWorldName && host.TryGetWorldName(out string w) ? (w ?? string.Empty).Trim() : string.Empty; if (_server.Revision == _appliedRevision && world == _appliedWorld) return; _appliedRevision = _server.Revision; _appliedWorld = world; Apply(_server.State, _server.ProfileJson, _server.ProfilePlugin, world, CreatureProfileStore.ExistingServerFolder); } catch { } } /// /// What RynthCore's server list, the active server owner's profile JSON and the world name mean: /// the tier reading and the creature data key. answers whether a /// per-server creature data folder exists (its name as on disk, or null). Pure apart from it. /// internal void Apply(EngineServerState? state, string? profileJson, string? plugin, string? world, Func existingFolder) { _reading = Read(profileJson, plugin); _serverKey = ResolveServerKey(state, world, existingFolder); } /// The tier values of a profile JSON; without a "difficultyTier". Pure. internal static Reading Read(string? profileJson, string? plugin) { double tier = ServerProfileView.Value(profileJson, RynthServerProfile.ValueDifficultyTier, double.NaN); if (double.IsNaN(tier)) return Reading.None; double percent = ServerProfileView.Value(profileJson, RynthServerProfile.ValueDifficultyHealthPercent, 0); if (!(percent >= 0 && percent <= MaxHealthPercent)) percent = 0; return new Reading(true, (int)Math.Clamp(Math.Round(tier), 0, MaxTier), percent, plugin ?? string.Empty); } /// /// The folder name of this server's creature data. Pure apart from . /// * RynthCore names the server (the active one, else one its detection found and this character /// turned off): a folder that already holds data is kept, looked for under the server's id, then /// its name, then the world name (RynthAi before 2026-10-21 used the world name, and "Aelrynth" /// for that server's worlds, which its name finds); with none, the server's id. /// * Otherwise (no server, an engine before v86): the world name, as before. /// Every key is made safe for a folder name (the SettingsProfiles rule); "" while nothing is known. /// internal static string ResolveServerKey(EngineServerState? state, string? world, Func existingFolder) { string legacy = SafeKey(world); EngineServer? server = ServerOf(state); if (server == null) return legacy; string id = SafeKey(server.Id); foreach (string candidate in new[] { id, SafeKey(server.Name), legacy }) { if (candidate.Length == 0) continue; string? found = existingFolder(candidate); if (!string.IsNullOrEmpty(found)) return found; } return id.Length > 0 ? id : legacy; } /// RynthCore's active server, else the first one its detection found (turned off by an override); null for none. private static EngineServer? ServerOf(EngineServerState? state) { if (state == null) return null; if (state.HasActive) return state.Find(state.Active) ?? new EngineServer(state.Active, state.ActiveName, true, true, "auto", false, state.ActivePlugin, ""); foreach (EngineServer s in state.Servers) if (s.Detected) return s; return null; } /// A world or server name as a folder name (the SettingsProfiles rule); "" for none. public static string SafeKey(string? name) { if (string.IsNullOrWhiteSpace(name)) return string.Empty; var sb = new System.Text.StringBuilder(name.Length); foreach (char c in name.Trim()) sb.Append(char.IsLetterOrDigit(c) || c == '\'' || c == '-' ? c : '_'); return sb.ToString(); } }