// IltAugCosts.cs — luminance augmentation cost rules, per server (world name). // Originally SilentKelpie's ILT Hub (RynthAi IltHub/); moved to RynthILT 2026-10-13. // // Defaults are UB's Leaftide Augs tab (correct for InfiniteLeaftide): // level i (1-based) costs (Base + (i-1) * Base * LinearPercent/100) * multiplier(i) // multiplier(i) = M0 below S1, M1 from S1, M2 from S2, M3 from S3, M4 from S4 // (UB: 1 / 4 / 8 / 16 / 24; a tier starts AT its S level). // Another server can change the multipliers and every aug's base, growth, S1..S4, target cap // and coins per level from the Progression tab; those edits are saved for that world only in // one shared file, so every character on the world prices augs the same way. using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text.Json; namespace RynthCore.Plugin.RynthILT.Hub; /// The saved file: one cost profile per world that was changed from the defaults. public sealed class IltAugCostFile { public Dictionary Servers = new(StringComparer.OrdinalIgnoreCase); } /// A world's cost rules: the five tier multipliers and one row per aug. public sealed class IltAugCostProfile { public double[] Multipliers = (double[])IltAugCosts.DefaultMultipliers.Clone(); public List Augs = new(); } /// One aug's cost rule. Thresholds are 1-based levels; Cap 0 = no target limit. public sealed class IltAugCostRow { public string Key = string.Empty; public long Base; public double LinearPercent; public int CoinsPerLevel; public int S1, S2, S3, S4; public int Cap; public IltAugCostRow Clone() => (IltAugCostRow)MemberwiseClone(); } internal sealed class IltAugCosts { /// UB's tier multipliers: below S1, from S1, from S2, from S3, from S4. public static readonly double[] DefaultMultipliers = { 1, 4, 8, 16, 24 }; /// Highest target the planner accepts when an aug has no cap (keeps the plan sane). public const int NoCapLimit = 100_000; /// Display order and labels; also the only keys a profile may hold. public static readonly (string Key, string Label)[] Labels = { ("creature", "Creature"), ("item", "Item"), ("life", "Life"), ("war", "War"), ("void", "Void"), ("duration", "Duration"), ("melee", "Melee"), ("missile", "Missile"), ("specialization", "Specialization"), ("summon", "Summon"), }; /// UB Augs.cs (ILT): cost models, coins per level and soft caps S1..S4; no target cap. public static IltAugCostRow[] DefaultRows() => new[] { Row("creature", 750_000, 130, 3, 2750, 4000, 4750, 5250), Row("item", 1_000_000, 165, 18, 1250, 2000, 3000, 3500), Row("life", 975_000, 195, 12, 1000, 2000, 3000, 3750), Row("war", 750_000, 140, 5, 1750, 2500, 3000, 3750), Row("void", 800_000, 160, 5, 1750, 2500, 3000, 3750), Row("duration", 400_000, 120, 5, 1000, 2000, 2500, 3000), Row("melee", 750_000, 140, 5, 1750, 2500, 3000, 3750), Row("missile", 750_000, 140, 5, 1750, 2500, 3000, 3750), Row("specialization", 3_000_000, 200, 50, 1750, 2500, 3000, 3750), Row("summon", 500_000, 125, 36, 1750, 2500, 3000, 3750), }; private static IltAugCostRow Row(string key, long b, double lp, int coins, int s1, int s2, int s3, int s4) => new() { Key = key, Base = b, LinearPercent = lp, CoinsPerLevel = coins, S1 = s1, S2 = s2, S3 = s3, S4 = s4, Cap = 0 }; /// The shared settings file. Tests point it at a scratch folder before first use. public static string FilePath { get; set; } = Path.Combine(IltHubStore.SharedRoot, "ilt-aug-costs.json"); private IltAugCostFile? _file; private string _lastJson = string.Empty; private static readonly IltAugCostProfile Defaults = NewDefaultProfile(); public static string Label(string key) { foreach (var l in Labels) if (l.Key.Equals(key, StringComparison.OrdinalIgnoreCase)) return l.Label; return key; } private static IltAugCostProfile NewDefaultProfile() => new() { Multipliers = (double[])DefaultMultipliers.Clone(), Augs = DefaultRows().ToList() }; private IltAugCostFile File_ { get { if (_file != null) return _file; _file = new IltAugCostFile(); try { if (File.Exists(FilePath)) { string json = File.ReadAllText(FilePath); _file = JsonSerializer.Deserialize(json, IltHubJsonContext.Default.IltAugCostFile) ?? new IltAugCostFile(); _lastJson = json; } } catch (Exception ex) { RynthLog.Exception(LogCat.IltStore, ex, $"load {FilePath}"); _file = new IltAugCostFile(); } var fixedUp = new Dictionary(StringComparer.OrdinalIgnoreCase); foreach (var kv in _file.Servers ?? new()) if (kv.Value != null) fixedUp[kv.Key ?? string.Empty] = Normalize(kv.Value); _file.Servers = fixedUp; return _file; } } /// /// Repairs a loaded or edited profile: five multipliers, exactly one row per known aug (missing /// ones from the defaults, unknown ones dropped), non-negative values and S1 <= S2 <= S3 <= S4. /// public static IltAugCostProfile Normalize(IltAugCostProfile p) { var m = new double[5]; for (int i = 0; i < 5; i++) { double v = p.Multipliers != null && i < p.Multipliers.Length ? p.Multipliers[i] : DefaultMultipliers[i]; m[i] = double.IsFinite(v) ? Math.Clamp(v, 0, 1000) : DefaultMultipliers[i]; } p.Multipliers = m; var byKey = new Dictionary(StringComparer.OrdinalIgnoreCase); foreach (var r in p.Augs ?? new()) if (r != null && !string.IsNullOrEmpty(r.Key)) byKey[r.Key] = r; var rows = new List(Labels.Length); foreach (var def in DefaultRows()) { var r = byKey.TryGetValue(def.Key, out var have) ? have : def; r.Key = def.Key; r.Base = Math.Max(0, r.Base); r.LinearPercent = double.IsFinite(r.LinearPercent) ? Math.Clamp(r.LinearPercent, 0, 100_000) : def.LinearPercent; r.CoinsPerLevel = Math.Max(0, r.CoinsPerLevel); r.S1 = Math.Max(0, r.S1); r.S2 = Math.Max(r.S1, r.S2); r.S3 = Math.Max(r.S2, r.S3); r.S4 = Math.Max(r.S3, r.S4); r.Cap = Math.Max(0, r.Cap); rows.Add(r); } p.Augs = rows; return p; } /// The rules for : its saved profile, or the UB defaults. public IltAugCostProfile For(string world) => File_.Servers.TryGetValue(world ?? string.Empty, out var p) ? p : Defaults; /// True when has its own saved rules. public bool IsCustom(string world) => File_.Servers.ContainsKey(world ?? string.Empty); /// Applies an edit to 's rules (copying the defaults first) and saves. public void Edit(string world, Action change) { string key = world ?? string.Empty; if (!File_.Servers.TryGetValue(key, out var p)) { p = NewDefaultProfile(); File_.Servers[key] = p; } change(p); Normalize(p); Save(); } /// Drops 's saved rules: it goes back to the UB defaults. public void Reset(string world) { if (File_.Servers.Remove(world ?? string.Empty)) Save(); } private void Save() { try { string json = JsonSerializer.Serialize(File_, IltHubJsonContext.Default.IltAugCostFile); if (json == _lastJson) return; if (RynthCore.Plugin.Shared.DataWriteGuard.TrySave("the aug cost table", FilePath, () => { Directory.CreateDirectory(Path.GetDirectoryName(FilePath)!); string tmp = FilePath + ".tmp"; File.WriteAllText(tmp, json); File.Move(tmp, FilePath, overwrite: true); })) _lastJson = json; } catch (Exception ex) { RynthLog.Exception(LogCat.IltStore, ex, $"save {FilePath}"); } } /// Highest target the planner allows for at current level . public static int Ceiling(IltAugCostRow r, int cur) => Math.Max(cur, r.Cap > 0 ? r.Cap : NoCapLimit); /// /// Luminance for levels cur+1 .. tgt. Same result as UB's per-level loop, summed per tier in /// closed form: within a tier the per-level base grows linearly, so each tier is an arithmetic series. /// public static decimal LumCost(IltAugCostRow r, double[] mult, int cur, int tgt) { int first = Math.Max(1, cur + 1); if (tgt < first) return 0; decimal b = r.Base; decimal step = b * (decimal)r.LinearPercent / 100m; // Tier t covers levels [lo[t], hi[t]) (1-based); the last tier has no end. long[] lo = { 1, r.S1, r.S2, r.S3, r.S4 }; long[] hi = { r.S1, r.S2, r.S3, r.S4, long.MaxValue }; decimal total = 0; for (int t = 0; t < 5; t++) { long a = Math.Max(first, lo[t]); long z = Math.Min(tgt, hi[t] - 1); if (z < a) continue; decimal n = z - a + 1; // sum over i=a..z of (b + (i-1)*step) = n*b + step * sum(i-1) = n*b + step * n*(a-1 + z-1)/2 decimal levels = n * b + step * (n * ((a - 1) + (z - 1)) / 2m); total += levels * (decimal)mult[t]; } return total; } }