using System;
using System.Collections.Generic;
using System.Linq;
namespace RynthCore.Plugin.RynthAi;
public enum WeaponCategory { Bow, Crossbow, Atlatl }
/// One fletching combine from the world database: the head is applied to the shaft.
public sealed class AmmoRecipe
{
public string HeadName { get; }
public string ShaftName { get; }
public string OutputName { get; }
public WeaponCategory Category { get; }
///
/// The recipe's skill difficulty. Also the ammo's quality rank: the retail data makes
/// better ammo harder (plain Arrowheads 5 … Deadly Prismatic 400).
///
public int Difficulty { get; }
///
/// What to craft or equip first: the Prismatic family above everything else — it is
/// what players actually craft — then difficulty (Deadly > Greater > plain).
///
public int Rank { get; }
internal string HeadKey { get; }
internal string ShaftKey { get; }
public AmmoRecipe(string head, string shaft, string output, WeaponCategory category, int difficulty)
{
HeadName = head;
ShaftName = shaft;
OutputName = output;
Category = category;
Difficulty = difficulty;
Rank = difficulty + (output.Contains("Prismatic", StringComparison.OrdinalIgnoreCase) ? AmmoRecipes.PrismaticFirst : 0);
HeadKey = AmmoRecipes.Normalize(head);
ShaftKey = AmmoRecipes.Normalize(shaft);
}
public bool IsHead(string itemName) => AmmoRecipes.Normalize(itemName) == HeadKey;
public bool IsShaft(string itemName) => AmmoRecipes.Normalize(itemName) == ShaftKey;
}
///
/// Ammo crafting data and the decisions made over it. No host calls, so the whole table
/// can be checked offline. The rows (AmmoRecipes.Data.cs) are generated from the ACE world
/// database by Tools/gen-ammo-recipes.py — regenerate rather than hand-edit.
///
public static partial class AmmoRecipes
{
///
/// Lowest success chance worth attempting. A failed fletching combine destroys both the
/// head and the shaft bundles (Infinite heads: the shafts), so this wants skill about
/// 73 over the recipe's difficulty.
///
public const double MinSuccessChance = 0.90;
/// Added to a Prismatic recipe's rank; above the hardest difficulty (400).
internal const int PrismaticFirst = 1000;
/// ACE's SkillCheck.GetSkillChance (default factor 0.03), which crafting uses.
public static double SuccessChance(int skill, int difficulty) =>
1.0 - 1.0 / (1.0 + Math.Exp(0.03 * (skill - difficulty)));
///
/// The best (highest-ranked) recipe for whose head and shaft are
/// both carried and that (buffed skill) makes reliably.
/// is the best one skipped for skill, so the caller can say so.
///
public static AmmoRecipe? BestCraftable(IEnumerable carriedNames, WeaponCategory category,
int fletching, out AmmoRecipe? tooHard)
{
var carried = new HashSet();
foreach (string n in carriedNames)
if (!string.IsNullOrEmpty(n)) carried.Add(Normalize(n));
tooHard = null;
foreach (var r in Index.ByRank)
{
if (r.Category != category) continue;
if (!carried.Contains(r.HeadKey) || !carried.Contains(r.ShaftKey)) continue;
if (SuccessChance(fletching, r.Difficulty) >= MinSuccessChance) return r;
tooHard ??= r;
}
return null;
}
/// Rank of finished ammo on the recipes' scale (), or 1
/// for ammo no recipe makes (quest arrows and the like) so crafted ammo outranks it.
public static int OutputRank(string name, WeaponCategory category)
{
if (string.IsNullOrEmpty(name)) return 0;
return Index.OutputRank.TryGetValue((category, Normalize(name)), out int d) ? d : 1;
}
/// Finished ammo for the category — not a head, a shaft or a bundle.
public static bool IsLooseAmmoName(string name, WeaponCategory category)
{
if (string.IsNullOrEmpty(name)) return false;
string n = Normalize(name);
if (n.Contains("bundle") || n.Contains("wrapped")) return false;
if (n.Contains("arrowhead") || n.Contains("arrowshaft")) return false;
if (n.Contains("quarrelhead") || n.Contains("quarrelshaft")) return false;
if (n.Contains("darthead") || n.Contains("dartshaft")) return false;
return category switch
{
WeaponCategory.Bow => n.Contains("arrow"),
WeaponCategory.Crossbow => n.Contains("quarrel") || n.Contains("bolt"),
WeaponCategory.Atlatl => n.Contains("dart"),
_ => false,
};
}
// Names come from the object cache, so the length is attacker-ish input as far as
// this frame is concerned — an unbounded stackalloc on it can blow the stack
// (2026-06-03 audit P2). Item names are far under this; anything longer heaps.
private const int MaxStackNameChars = 256;
/// Lower-case letters and digits only: "Frog-Crotch" and "Frog Crotch" compare equal.
public static string Normalize(string value)
{
char[]? rented = value.Length > MaxStackNameChars ? new char[value.Length] : null;
Span buffer = rented is null ? stackalloc char[MaxStackNameChars] : rented;
int count = 0;
foreach (char ch in value)
{
if (!char.IsLetterOrDigit(ch))
continue;
buffer[count++] = char.ToLowerInvariant(ch);
}
return new string(buffer[..count]);
}
// Built on first use, in its own type so it can't initialise before All (the two
// partial files' static initialisers run in no guaranteed order).
private static class Index
{
public static readonly AmmoRecipe[] ByRank = All.OrderByDescending(r => r.Rank).ToArray();
public static readonly Dictionary<(WeaponCategory, string), int> OutputRank = Build();
private static Dictionary<(WeaponCategory, string), int> Build()
{
var d = new Dictionary<(WeaponCategory, string), int>();
foreach (var r in All)
{
var key = (r.Category, Normalize(r.OutputName));
if (!d.TryGetValue(key, out int have) || r.Rank > have) d[key] = r.Rank;
}
return d;
}
}
}