using System; using System.Collections.Generic; using System.Globalization; using System.Text.RegularExpressions; namespace RynthCore.Loot.T11; /// One "Wield requires: N <counter>" gate. public readonly record struct T11WieldGate(T11Counter Counter, int Amount); /// One line of the appraisal's "Modifiers:" section. public sealed class T11Modifier { /// Catalog key (T11Catalog.Modifiers), or 0 for a name the catalog doesn't know. public int Key { get; init; } /// The modifier name as printed. public string Name { get; init; } = string.Empty; /// The rolled value (the first number after the name; 0 for value-less specials). public int Value { get; init; } /// Low end of the "[min-max]" roll band, or null when the line has none. public int? Min { get; init; } /// High end of the "[min-max]" roll band, or null when the line has none. public int? Max { get; init; } /// Where the value landed in its band, 0-100, or -1 without a usable band. public int RollPercent { get; init; } = -1; /// The retail Gear* rating key the modifier sets, or 0. public int RatingKey { get; init; } /// True for a one-per-slot special (Fortify Vitals, Battle Mending, ...). public bool IsSlotSpecial { get; init; } } /// /// What the client can learn about a T11 (tier 11+) ACECustom drop. The server keeps the /// T11 properties (weapon quality, loot tier, the Zone Control record, the item-aug wield /// gate) out of the appraisal and prints them as description text instead, so this is /// parsed from the name, LongDesc (key 16) and Use (key 14). See ACECustom AppraiseInfo /// (BuildProperties, BuildWeapon, PromoteZoneModifierLines) for the formats. /// public sealed class T11ItemInfo { /// A non-T11 item: every field at its default. public static readonly T11ItemInfo None = new(); /// True when the item is T11 gear (name prefix, or T11-only text in its description). public bool IsT11 { get; private set; } /// True when the name starts with "T11 - ". public bool HasNamePrefix { get; private set; } /// Weapon sub-grade label ("B+"), or null. public string? Grade { get; private set; } /// Sub-grade rank, S = 16 ... F- = 1, 0 = no grade. public int GradeRank { get; private set; } /// "N% of max damage" from the grade line, or -1. public int DamagePercent { get; private set; } = -1; /// Every wield gate, at most one per counter. public IReadOnlyList WieldGates => _gates; /// The Zone Control modifier lines, in appraisal order. public IReadOnlyList Modifiers => _modifiers; /// "Cast on Strike" lines: spell name and proc chance percent. public IReadOnlyList<(string Name, double ChancePercent)> Procs => _procs; /// True when the appraisal said "Zone Locked (Power Reduced)" (depends on where the examiner stood). public bool ZoneLocked { get; private set; } /// True when the item had appraisal text (false for a T11 name seen before its ID). public bool HasText { get; private set; } private readonly List _gates = new(); private readonly List _modifiers = new(); private readonly List<(string, double)> _procs = new(); /// The item-augmentation gate, or 0 when there is none. public int WieldItemAugs => GateAmount(T11Counter.ItemAugmentations); /// The amount required of , or 0 when it isn't gated. public int GateAmount(T11Counter counter) { foreach (var g in _gates) if (g.Counter == counter) return g.Amount; return 0; } /// /// The estimated loot tier (11-25), or 0 for non-T11 items. T16 and up come from the /// Triune Weave gate; an item without one reads at most 15. /// public int EstimatedTier => T11Catalog.EstimateTier(WieldItemAugs, GateAmount(T11Counter.TriuneWeave), IsT11); /// Sum of modifier values that land on the TotalRatings keys. public int ModifierRatingTotal { get { int total = 0; foreach (var m in _modifiers) if (Array.IndexOf(T11Catalog.TotalRatingKeys, m.RatingKey) >= 0) total += m.Value; return total; } } /// Sum of modifier values that set , or 0. public int RatingFromModifiers(int ratingKey) { int total = 0; foreach (var m in _modifiers) if (m.RatingKey == ratingKey) total += m.Value; return total; } /// The value of the modifier with catalog key , or 0 when absent. public int ModifierValue(int key) { foreach (var m in _modifiers) if (m.Key == key) return m.Value; return 0; } /// Average roll position (0-100) of the banded modifiers, or 0 without any. public int AverageRollPercent { get { int sum = 0, n = 0; foreach (var m in _modifiers) if (m.RollPercent >= 0) { sum += m.RollPercent; n++; } return n == 0 ? 0 : (int)Math.Round(sum / (double)n, MidpointRounding.AwayFromZero); } } /// Best roll position (0-100) of the banded modifiers, or 0 without any. public int BestRollPercent { get { int best = 0; foreach (var m in _modifiers) if (m.RollPercent > best) best = m.RollPercent; return best; } } /// True when any modifier is a slot special. public bool HasSlotSpecial { get { foreach (var m in _modifiers) if (m.IsSlotSpecial) return true; return false; } } // ── Parsing ───────────────────────────────────────────────────────────── private const RegexOptions Opt = RegexOptions.CultureInvariant | RegexOptions.IgnoreCase; // "- Weapon Grade: B+ (86% of max damage)" / "- Weapon Grade: S" private static readonly Regex GradeLine = new( @"^-?\s*Weapon Grade:\s*(?S|[A-DF][+-]?)(?=\s|\(|$)(?:\s*\((?\d+)% of max damage\))?", Opt); // "- Wield requires: 2,000 Item Augmentations" (weapons) / "Wield requires: 2,000 Item Augmentations" (armor). // ACE prints N0, so the group separator follows the server's culture: keep digits only. private static readonly Regex WieldLine = new( @"^-?\s*Wield requires:\s*(?\d[\d,.\s']*?)\s+(?[A-Za-z' ]+?)\s*$", Opt); // "- Cast on Strike: Force Arc (13% proc chance)" private static readonly Regex ProcLine = new( @"^-\s*Cast on Strike:\s*(?.+?)\s*\((?\d+(?:\.\d+)?)% proc chance\)\s*$", Opt); // A modifier's value: the first signed whole number after the name. private static readonly Regex FirstNumber = new(@"[+-]?\d+", RegexOptions.CultureInvariant); // The roll band at the end of a modifier line: "[14-69]". private static readonly Regex Band = new(@"\[(?\d+)\s*-\s*(?\d+)\]\s*$", RegexOptions.CultureInvariant); // An uncatalogued modifier from a newer server: "Some Name +12 [5-20]" or "Modifier 60 +3 [1-5]". private static readonly Regex GenericModifier = new(@"^(?.+?)\s+(?[+-]?\d+)", RegexOptions.CultureInvariant); // Checked before GenericModifier, whose lazy name would stop at "Modifier" and read the key as the value. private static readonly Regex UnknownKeyName = new(@"^(?Modifier (?\d+))(?=\s|$)", RegexOptions.CultureInvariant); private const string ModifiersHeader = "Modifiers:"; private const string ZoneLockedText = "Zone Locked (Power Reduced)"; /// /// Parses an item. , and /// may be null or empty (an item not yet appraised has no text; /// the name prefix alone still marks it T11). Never throws. /// public static T11ItemInfo Parse(string? name, string? longDesc, string? use) { bool prefix = !string.IsNullOrEmpty(name) && name.StartsWith(T11Catalog.NamePrefix, StringComparison.Ordinal); if (!prefix && string.IsNullOrEmpty(longDesc) && string.IsNullOrEmpty(use)) return None; var info = new T11ItemInfo { HasNamePrefix = prefix, HasText = !string.IsNullOrEmpty(longDesc) || !string.IsNullOrEmpty(use), }; try { info.ParseText(longDesc); info.ParseText(use); } catch { // Malformed text never breaks loot evaluation; whatever parsed so far stands. } // Grade, gates and banded modifiers only appear on T11 gear; Cast on Strike and the // zone-lock line alone do not mark an item. info.IsT11 = prefix || info.GradeRank > 0 || info._gates.Count > 0 || info._modifiers.Count > 0; return info.IsT11 ? info : None; } private void ParseText(string? text) { if (string.IsNullOrEmpty(text)) return; string[] lines = text.Replace("\r\n", "\n").Split('\n'); for (int i = 0; i < lines.Length; i++) { string line = lines[i].Trim(); if (line.Length == 0) continue; if (line.Contains(ZoneLockedText, StringComparison.OrdinalIgnoreCase)) { ZoneLocked = true; continue; } if (string.Equals(line, ModifiersHeader, StringComparison.OrdinalIgnoreCase)) { i = ParseModifierBlock(lines, i + 1) - 1; continue; } Match m = GradeLine.Match(line); if (m.Success && GradeRank == 0) { Grade = m.Groups["g"].Value.ToUpperInvariant(); GradeRank = T11Catalog.GradeRank(Grade); if (m.Groups["pct"].Success && int.TryParse(m.Groups["pct"].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out int pct)) DamagePercent = pct; continue; } m = WieldLine.Match(line); if (m.Success && T11Catalog.CounterByName(m.Groups["what"].Value) is T11Counter counter) { int amount = DigitsOnly(m.Groups["n"].Value); if (amount > 0) AddGate(counter, amount); continue; } m = ProcLine.Match(line); if (m.Success && double.TryParse(m.Groups["pct"].Value, NumberStyles.Float, CultureInfo.InvariantCulture, out double chance)) _procs.Add((m.Groups["name"].Value, chance)); } } /// Reads "- ..." lines after a "Modifiers:" header; returns the index of the first line not consumed. private int ParseModifierBlock(string[] lines, int start) { int i = start; for (; i < lines.Length; i++) { string line = lines[i].Trim(); if (line.Length == 0 || !line.StartsWith("-", StringComparison.Ordinal)) break; string body = line.Substring(1).Trim(); if (body.Length == 0) continue; if (body.Contains(ZoneLockedText, StringComparison.OrdinalIgnoreCase)) { ZoneLocked = true; continue; } _modifiers.Add(ParseModifier(body)); } return i; } private static T11Modifier ParseModifier(string body) { T11ModifierDef? def = T11Catalog.MatchModifier(body); string name; string rest; int key = 0; if (def != null) { name = def.Name; key = def.Key; rest = body.Substring(def.Name.Length); } else if (UnknownKeyName.Match(body) is { Success: true } k && int.TryParse(k.Groups["key"].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out int parsedKey)) { key = parsedKey; name = k.Groups["name"].Value; rest = body.Substring(name.Length); def = T11Catalog.ModifierByKey(parsedKey); if (def != null) name = def.Name; } else { Match g = GenericModifier.Match(body); name = g.Success ? g.Groups["name"].Value.Trim() : body; rest = g.Success ? body.Substring(g.Groups["name"].Length) : string.Empty; } int value = 0; Match num = FirstNumber.Match(rest); if (num.Success) int.TryParse(num.Value, NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out value); int? min = null, max = null; int roll = -1; Match band = Band.Match(rest); if (band.Success && int.TryParse(band.Groups["min"].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out int lo) && int.TryParse(band.Groups["max"].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out int hi)) { min = lo; max = hi; if (hi > lo) roll = Math.Clamp((int)Math.Round((value - lo) * 100.0 / (hi - lo), MidpointRounding.AwayFromZero), 0, 100); } return new T11Modifier { Key = key, Name = name, Value = value, Min = min, Max = max, RollPercent = roll, RatingKey = def?.RatingKey ?? 0, IsSlotSpecial = def?.SlotSpecial ?? false, }; } private void AddGate(T11Counter counter, int amount) { for (int i = 0; i < _gates.Count; i++) { if (_gates[i].Counter != counter) continue; if (amount > _gates[i].Amount) _gates[i] = new T11WieldGate(counter, amount); return; } _gates.Add(new T11WieldGate(counter, amount)); } private static int DigitsOnly(string s) { long v = 0; foreach (char c in s) { if (c < '0' || c > '9') continue; v = v * 10 + (c - '0'); if (v > int.MaxValue) return int.MaxValue; } return (int)v; } }