// KitPlanner.cs - "Make kits": which kits the pack's ingredients make, how many, and the next
// combine. Pure (System only), so RynthCore.RynthAiTests links it; KitRunner does the work.
//
// Retail data (ACE World Database v0.9.293, recipes 4022-4025; ACE RecipeManager):
// Treated Mandrake (22448) on Treated Hyssop (22447), either way round
// -> Combined Hyssop and Mandrake (22446) Healing, difficulty 100
// Combined Hyssop and Mandrake (22446) on Soft Bandages (22445), either way round
// -> Plentiful Healing Kit (22449, 100 uses) Healing, difficulty 200
// Each combine uses one of each item, success or failure (a failure makes nothing). The chance is
// ACE's SkillCheck.GetSkillChance(buffed skill, difficulty) = 1 - 1/(1 + e^(0.03 (skill - diff))),
// plus the Craftsman augmentation's skill (which only raises it: our figure is the floor). An
// untrained skill is refused by the server.
// That is the only Plentiful kit in the retail data: there is no Plentiful mana or stamina kit to
// make there. Another kit chain is one more KitRecipe in Retail.
using System;
using System.Collections.Generic;
using System.Linq;
namespace RynthCore.Plugin.RynthAi.MiniRemote.Crafting;
/// An item a kit recipe uses or makes: its weenie class id and its name (the name is the fallback match).
internal sealed record KitPart(uint Wcid, string Name);
/// One combine: used on makes .
internal sealed record KitCombine(
string Label, KitPart Source, KitPart Target, KitPart Product, int SkillId, int Difficulty,
string SuccessText, string FailText);
/// A kit and the combines that make it (the last one makes the kit; earlier ones make its parts).
internal sealed record KitRecipe(string Id, string Name, KitPart Product, KitCombine[] Steps)
{
public KitCombine Final => Steps[^1];
/// The combine that makes , or null for an ingredient you carry.
public KitCombine? Producer(KitPart part) => Steps.FirstOrDefault(s => s.Product.Wcid == part.Wcid);
/// The items you carry for it (not made by an earlier step), in step order.
public IEnumerable Ingredients()
{
var seen = new HashSet();
foreach (var s in Steps)
foreach (var p in new[] { s.Source, s.Target })
if (seen.Add(p.Wcid)) yield return p;
}
}
/// A stack in the pack as the planner sees it (Wcid 0 = unknown: the name decides).
internal readonly record struct KitStack(int Id, uint Wcid, string Name, int Count);
/// The crafting skill: its buffed value and training (2 trained, 3 specialized); Known false when the engine can't read it.
internal readonly record struct KitSkill(bool Known, int Buffed, int Training)
{
public static readonly KitSkill Unknown = new(false, 0, 0);
public bool Trained => Training >= 2;
}
/// One step's chance in a plan (-1 percent = unknown).
internal readonly record struct KitStepChance(KitCombine Step, int Percent, bool UnderFloor);
/// What the pack makes of one kit.
internal sealed class KitPlanRow
{
public KitRecipe Recipe = null!;
/// Kits the pack makes if every combine works.
public int CanMake;
/// Each item the recipe uses (and makes on the way), with the count carried.
public List<(KitPart Part, int Count)> Parts = new();
public List Chances = new();
/// Why no combine can go now ("" when one can).
public string Blocked = string.Empty;
}
/// The next combine: the stacks to use, or why none (Step null).
internal readonly record struct KitNext(KitCombine? Step, int SourceId, int TargetId, int Percent, string Why)
{
public static KitNext None(string why) => new(null, 0, 0, -1, why);
}
internal static class KitPlanner
{
public const int SkillHealing = 21;
/// The Min chance % a new character starts with.
public const int DefaultFloorPercent = 90;
public static readonly KitPart SoftBandages = new(22445, "Soft Bandages");
public static readonly KitPart CombinedHerbs = new(22446, "Combined Hyssop and Mandrake");
public static readonly KitPart TreatedHyssop = new(22447, "Treated Hyssop");
public static readonly KitPart TreatedMandrake = new(22448, "Treated Mandrake");
public static readonly KitPart PlentifulHealingKit = new(22449, "Plentiful Healing Kit");
/// The retail kit recipes (ACE World Database).
public static readonly KitRecipe[] Retail =
{
new("plentiful-healing", "Plentiful Healing Kit", PlentifulHealingKit, new[]
{
new KitCombine("Combine the herbs", TreatedMandrake, TreatedHyssop, CombinedHerbs, SkillHealing, 100,
"You crumble the dried and treated leaves together.",
"You crumble the leaves and they become useless."),
new KitCombine("Pack the kit", CombinedHerbs, SoftBandages, PlentifulHealingKit, SkillHealing, 200,
"You pack the bandages with the herbs. The odor is pungent and heady. The kit appears to be bulkier than any others you have seen.",
"The herbs spill out of the bandages onto the ground. The bandages are ruined as they unravel onto the dirt."),
}),
};
public static string SkillName(int skillId) => skillId switch
{
SkillHealing => "Healing",
_ => $"skill {skillId}",
};
// ── Chance ──────────────────────────────────────────────────────────────
/// ACE SkillCheck.GetSkillChance (factor 0.03), 0-1.
public static double Chance(int skill, int difficulty)
{
double c = 1.0 - 1.0 / (1.0 + Math.Exp(0.03 * (skill - difficulty)));
return Math.Clamp(c, 0.0, 1.0);
}
/// The chance as the server's prompt shows it (a rounded percent).
public static int Percent(int skill, int difficulty) => (int)Math.Round(Chance(skill, difficulty) * 100.0, MidpointRounding.AwayFromZero);
/// A step's percent for this skill (-1 unknown).
public static int StepPercent(KitCombine step, Func skill)
{
var s = skill(step.SkillId);
return s.Known ? Percent(s.Buffed, step.Difficulty) : -1;
}
// ── Counting ────────────────────────────────────────────────────────────
public static bool Matches(KitPart part, KitStack stack)
=> stack.Wcid != 0 ? stack.Wcid == part.Wcid : string.Equals(stack.Name, part.Name, StringComparison.OrdinalIgnoreCase);
/// How many of a part the pack holds (every matching stack).
public static int Count(KitPart part, IReadOnlyList pack)
{
int n = 0;
foreach (var s in pack) if (Matches(part, s)) n += Math.Max(1, s.Count);
return n;
}
/// The first stack of a part (its object id), 0 when none.
public static int FirstStack(KitPart part, IReadOnlyList pack)
{
foreach (var s in pack) if (Matches(part, s)) return s.Id;
return 0;
}
/// Carried plus what the earlier steps can still make of it.
private static int Available(KitRecipe r, KitPart part, IReadOnlyList pack, int depth)
{
int have = Count(part, pack);
var producer = depth < 8 ? r.Producer(part) : null;
return producer == null ? have : have + Makeable(r, producer, pack, depth + 1);
}
/// How many times a step can go with what the pack holds and makes.
private static int Makeable(KitRecipe r, KitCombine step, IReadOnlyList pack, int depth = 0)
=> Math.Min(Available(r, step.Source, pack, depth), Available(r, step.Target, pack, depth));
/// Kits the pack makes if every combine works.
public static int CanMake(KitRecipe r, IReadOnlyList pack) => Makeable(r, r.Final, pack);
// ── Planning ────────────────────────────────────────────────────────────
/// What the pack makes of each recipe, with the chances at this skill and floor.
public static List Plan(IReadOnlyList recipes, IReadOnlyList pack,
Func skill, int floorPercent)
{
var rows = new List();
foreach (var r in recipes)
{
var row = new KitPlanRow { Recipe = r, CanMake = CanMake(r, pack) };
foreach (var p in r.Ingredients()) row.Parts.Add((p, Count(p, pack)));
foreach (var st in r.Steps)
{
int pct = StepPercent(st, skill);
row.Chances.Add(new KitStepChance(st, pct, pct >= 0 && pct < floorPercent));
}
var next = Next(r, pack, skill, floorPercent);
row.Blocked = next.Step == null ? next.Why : string.Empty;
rows.Add(row);
}
return rows;
}
///
/// The next combine for : the kit itself when its two parts are carried, else
/// the earlier step that makes the missing part. Nothing is combined that the kit can't use (no
/// herbs without bandages), and nothing when a step it needs is under the floor, untrained, or
/// its chance can't be read with a floor set.
///
public static KitNext Next(KitRecipe r, IReadOnlyList pack, Func skill, int floorPercent)
=> Next(r, r.Final, pack, skill, Math.Clamp(floorPercent, 0, 100), 0);
private static KitNext Next(KitRecipe r, KitCombine step, IReadOnlyList pack, Func skill, int floor, int depth)
{
if (Makeable(r, step, pack) <= 0)
return KitNext.None("out of " + Missing(r, step, pack));
var s = skill(step.SkillId);
string skillName = SkillName(step.SkillId);
if (s.Known && !s.Trained)
return KitNext.None($"{skillName} isn't trained (the server refuses the combine)");
int pct = s.Known ? Percent(s.Buffed, step.Difficulty) : -1;
if (pct < 0 && floor > 0)
return KitNext.None($"can't read your {skillName} skill to check the {floor}% floor (set Min chance to 0 to go without it)");
if (pct >= 0 && pct < floor)
return KitNext.None($"{step.Label}: {pct}% at {skillName} {s.Buffed} is under your {floor}% floor");
int src = FirstStack(step.Source, pack), tgt = FirstStack(step.Target, pack);
if (src != 0 && tgt != 0)
return new KitNext(step, src, tgt, pct, string.Empty);
// A part has to be made first.
var missing = src == 0 ? step.Source : step.Target;
var producer = depth < 8 ? r.Producer(missing) : null;
if (producer == null) return KitNext.None("out of " + missing.Name);
return Next(r, producer, pack, skill, floor, depth + 1);
}
/// The first item that runs out for a step (an ingredient you carry, not a part made on the way).
private static string Missing(KitRecipe r, KitCombine step, IReadOnlyList pack, int depth = 0)
{
foreach (var part in new[] { step.Source, step.Target })
{
if (Available(r, part, pack, depth) > 0) continue;
var producer = depth < 8 ? r.Producer(part) : null;
return producer == null ? part.Name : Missing(r, producer, pack, depth + 1);
}
return step.Source.Name;
}
// ── Text ────────────────────────────────────────────────────────────────
/// "Soft Bandages 3, Treated Hyssop 5, ..." (what the pack holds for a row).
public static string PartsText(KitPlanRow row)
=> string.Join(", ", row.Parts.Select(p => $"{p.Part.Name} {p.Count}"));
/// "Combine the herbs 99%, Pack the kit 95%" ("?" when the skill can't be read).
public static string ChancesText(KitPlanRow row)
=> string.Join(", ", row.Chances.Select(c => $"{c.Step.Label} {(c.Percent >= 0 ? c.Percent + "%" : "?")}"));
/// One chat line for a row (/ra kits).
public static string Line(KitPlanRow row)
{
string make = row.CanMake == 1 ? "1 kit" : $"{row.CanMake} kits";
string tail = row.Blocked.Length > 0 && row.CanMake > 0 ? $" - can't start: {row.Blocked}" : "";
return $"{row.Recipe.Name}: {make} ({PartsText(row)}; chance {ChancesText(row)}){tail}";
}
/// The run's closing line.
public static string Summary(string kitName, int made, int failed, IReadOnlyDictionary lost, string why)
{
string madeText = made == 1 ? $"made 1 {kitName}" : $"made {made} {kitName}s";
string failText = failed == 0 ? "no combine failed"
: (failed == 1 ? "1 combine failed" : $"{failed} combines failed")
+ (lost.Count > 0 ? " (lost " + string.Join(", ", lost.Select(kv => $"{kv.Value} {kv.Key}")) + ")" : "");
return $"{madeText}, {failText}. Stopped: {why}.";
}
}