// TinkerPlanner.cs - What a Crafting tab would apply to one item, and which bag to use. // // Pure: the scan reads the item into TinkerItemFacts and the runner re-plans from fresh facts, // so these rules are the only place the apply order and caps live (and the host tests drive // them directly). Server rows say which rules apply to them (TinkerMaterial, from the server // plugin's profile); nothing here knows a server: // * rows run in TinkerMaterial.Order (imbues and server crit lines before stat tinks; with // "rend last" the rend moves to the end, the way UB-IT's foolproof-rend option does); // * a tink-slot row is cut to the free slots (10 minus the item's tinks); no-slot rows aren't; // * one imbue per item (the item's own, or the first planned one); // * a row with an applied int is skipped once that int reads 2 on the item; // * rows of one stack group stop at the group's cap combined; // * an after-cap row goes on last, once, and only when the tink slots are full; // * a prepared row needs a bag prepared at the chosen tier (its name says Minor, Moderate or // Major); a raw bag becomes one on that tier's Ammunition. using System; using System.Collections.Generic; using System.Linq; using RynthCore.Loot; namespace RynthCore.Plugin.RynthAi.MiniRemote.Crafting; /// What the planner needs to know about an item. internal sealed class TinkerItemFacts { public int Id; public string Name = string.Empty; public AcObjectClass Class; /// The item's workmanship (ItemWorkmanship 105; 0 = can't be tinkered). public double Workmanship; /// NumTimesTinkered (171). public int Tinks; /// ImbuedEffect (179) is set. public bool Imbued; /// DamageType (45): melee/missile damage, the caster's element. public int DamageType; /// Retained (bool 91). public bool Retained; /// ItemMaxMana (108). public int MaxMana; public bool Equipped; /// Reads an int property of the item (a row's applied int: 2 = that salvage is on). public Func FlagInt = _ => 0; } /// One planned application: the row and the salvage it resolves to (a rend resolves per item). internal readonly record struct TinkerStep(TinkerMaterial Row, int Material); /// A bag a step could use (for ). internal readonly record struct TinkerBagChoice(int Id, double Workmanship, double? Chance); internal static class TinkerPlanner { /// /// The rows a tab offers: retail, then (the connected server's own /// salvage, from the plugin that owns it), then the player's custom rows. /// public static List Rows(TinkerBench bench, IEnumerable? server, IEnumerable? custom) { var rows = new List(TinkerCatalog.RetailFor(bench)); if (server != null) rows.AddRange(server); if (custom != null) { foreach (var c in custom) { if (c.Bench != (int)bench || c.Material <= 0) continue; var kind = c.Imbue ? TinkerKind.Imbue : c.Once ? TinkerKind.Once : TinkerKind.Tink; rows.Add(new TinkerMaterial(c.Material, TinkerCatalog.MaterialName(c.Material), TinkerOrigin.Custom, kind, string.IsNullOrWhiteSpace(c.Note) ? "custom" : c.Note, 900 + rows.Count, UsesTinkSlot: c.UsesTinkSlot, NameMatch: c.NameMatch ?? string.Empty)); } } return rows; } /// The order a row runs in under these settings. public static int OrderOf(TinkerMaterial row, TinkerBenchSettings s) => row.Id == TinkerCatalog.RendId && s.RendLast ? 990 : row.Order; /// Why the scan leaves an item out ("" = it's eligible and the plan decides). public static string Excluded(TinkerItemFacts item, TinkerBenchSettings s) { if (item.Equipped) return "equipped"; if (item.Workmanship <= 0) return "no workmanship"; if (item.Tinks > Math.Clamp(s.MaxTinks, 0, TinkerCatalog.MaxTinks)) return $"{item.Tinks} tinks (max {s.MaxTinks})"; if (s.MinFreeTinks > 0 && TinkerCatalog.MaxTinks - item.Tinks < s.MinFreeTinks) return $"fewer than {s.MinFreeTinks} free tinks"; if (s.SkipImbued && item.Imbued) return "imbued"; return string.Empty; } /// /// The ordered steps for one item (empty when nothing ticked applies). /// is how many times this run already tried a row key on the /// item: the item's tinks count failures too, but no-slot server rows and the ticked counts /// don't show on it, so a re-plan would otherwise repeat them. /// public static List Plan(TinkerBench bench, TinkerItemFacts item, TinkerBenchSettings s, IReadOnlyList rows, Func? attempted = null) { var steps = new List(); int free = Math.Max(0, TinkerCatalog.MaxTinks - item.Tinks); bool imbueTaken = item.Imbued; Dictionary? stacked = null; List? afterCap = null; foreach (var row in rows.OrderBy(r => OrderOf(r, s))) { string key = TinkerCatalog.Key(row); int count = s.Counts.TryGetValue(key, out int c) ? c : 0; int tried = attempted?.Invoke(key) ?? 0; if (row.Kind != TinkerKind.Tink) count = Math.Min(count, 1); count -= tried; if (count <= 0) continue; int material = row.Id == TinkerCatalog.RendId ? TinkerCatalog.RendFor(item.DamageType, s.PierceForCombo) : row.Id; if (material <= 0 || !Applies(row, material, item)) continue; int n = row.Kind == TinkerKind.Tink ? count : 1; if (row.Kind == TinkerKind.Imbue) { if (imbueTaken) continue; imbueTaken = true; } if (row.AppliedInt != 0 && item.FlagInt(row.AppliedInt) == 2) continue; if (row.StackGroup.Length > 0 && row.StackCap > 0) { stacked ??= new Dictionary(StringComparer.OrdinalIgnoreCase); int used = stacked.TryGetValue(row.StackGroup, out int u) ? u : 0; n = Math.Min(n, row.StackCap - used); stacked[row.StackGroup] = used + Math.Max(0, n); } if (row.AfterCap) { (afterCap ??= new List()).Add(new TinkerStep(row, material)); continue; } if (row.UsesTinkSlot) { n = Math.Min(n, free); free -= Math.Max(0, n); } for (int i = 0; i < n; i++) steps.Add(new TinkerStep(row, material)); } if (afterCap != null && free == 0) steps.AddRange(afterCap); return steps; } /// Row-level requirements the item's own properties decide. private static bool Applies(TinkerMaterial row, int material, TinkerItemFacts item) { if (row.Origin != TinkerOrigin.Retail) return true; return material switch { TinkerCatalog.Moonstone => item.MaxMana > 0, TinkerCatalog.Leather => !item.Retained, TinkerCatalog.Sandstone => item.Retained, _ => true, }; } /// /// Whether a salvage bag can serve a step: a full bag (100 units) of the material, server bags /// matched by name (Abyssal-Touched Gems share Black Garnet's id, Foolproof rend bags may not /// carry a material at all). /// public static bool BagMatches(TinkerStep step, string bagName, int bagMaterial, int bagUses, int trinketTier = 0) { if (bagUses < 100) return false; bagName ??= string.Empty; if (step.Row.Prepared) return bagMaterial == step.Material && TierOf(bagName) == Math.Clamp(trinketTier, 0, TinkerCatalog.TrinketTiers.Length - 1); bool abyssal = bagName.IndexOf("Abyssal", StringComparison.OrdinalIgnoreCase) >= 0; if (step.Row.NameMatch.Length > 0) return bagName.IndexOf(step.Row.NameMatch, StringComparison.OrdinalIgnoreCase) >= 0; if (abyssal) return false; if (bagMaterial == step.Material) return true; return step.Row.Id == TinkerCatalog.RendId && TinkerCatalog.RendFromFoolproofName(bagName) == step.Material; } /// The prepared tier a name carries (0 Minor, 1 Moderate, 2 Major), else -1. public static int TierOf(string name) { name ??= string.Empty; for (int i = 0; i < TinkerCatalog.TrinketTiers.Length; i++) if (name.IndexOf(TinkerCatalog.TrinketTiers[i], StringComparison.OrdinalIgnoreCase) >= 0) return i; return -1; } /// A full, not yet prepared bag of a prepared row's salvage (no tier in its name). public static bool IsRawTrinketBag(int material, string bagName, int bagMaterial, int bagUses) => bagUses >= 100 && bagMaterial == material && TierOf(bagName) < 0; /// The Ammunition that prepares a raw bag into a tier bag. public static bool IsTierAmmunition(string name, int tier) => (name ?? string.Empty).IndexOf("Ammunition", StringComparison.OrdinalIgnoreCase) >= 0 && TierOf(name!) == Math.Clamp(tier, 0, TinkerCatalog.TrinketTiers.Length - 1); /// True for a bag that can't fail ("Foolproof ..."). public static bool IsFoolproof(string bagName) => (bagName ?? string.Empty).IndexOf("Foolproof", StringComparison.OrdinalIgnoreCase) >= 0; /// /// Picks the bag for a step (-1 = none qualifies). With a minimum percent: the lowest /// workmanship bag that reaches it, so good bags are kept for hard tinks. Without one: the /// best chance (lowest workmanship on a tie). A bag whose chance isn't known (server rows) /// is the best workmanship one; the server's own percent is checked when it asks. /// public static int SelectBag(IReadOnlyList bags, int minPercent) { int best = -1; for (int i = 0; i < bags.Count; i++) { var b = bags[i]; if (b.Chance is not double c) { if (best < 0 || (bags[best].Chance == null && b.Workmanship > bags[best].Workmanship)) best = i; continue; } if (minPercent > 0) { if (TinkerMath.Percent(c) < minPercent) continue; if (best < 0 || bags[best].Chance == null || b.Workmanship < bags[best].Workmanship) best = i; } else { if (best < 0 || bags[best].Chance == null) { best = i; continue; } double bc = bags[best].Chance!.Value; if (c > bc + 1e-9 || (Math.Abs(c - bc) <= 1e-9 && b.Workmanship < bags[best].Workmanship)) best = i; } } return best; } /// "Iron x3, Brass x2" for a plan. public static string Summary(IReadOnlyList steps) { if (steps.Count == 0) return "-"; var parts = new List(); int i = 0; while (i < steps.Count) { int j = i; while (j < steps.Count && steps[j].Material == steps[i].Material && steps[j].Row.Origin == steps[i].Row.Origin) j++; string name = TinkerCatalog.MaterialName(steps[i].Material); parts.Add(j - i > 1 ? $"{name} x{j - i}" : name); i = j; } return string.Join(", ", parts); } }