Snapshot for release 2026.10.28.2 (internal 48c46bd)

This commit is contained in:
Tom Bohar 2026-10-28 19:47:19 -04:00
commit 636adfcef2
41 changed files with 3402 additions and 120 deletions

View file

@ -2,7 +2,7 @@ using System;
namespace RynthCore.Plugin.RynthDefective.Model;
internal enum DefVerb { Toggle, Help, Use, Set, Clear, Bind, Unbind, Weapons, Favourite, Slots, List, Status, Error, Panels, Selection, Settings }
internal enum DefVerb { Toggle, Help, Use, Set, Clear, Bind, Unbind, Weapons, Favourite, Slots, List, Status, Error, Panels, Selection, Settings, Spell }
/// <summary>A parsed "/rdef ..." line. Use: <see cref="Slot"/> (1-based) or <see cref="Text"/> (an item name).</summary>
internal sealed record DefCommand(DefVerb Verb, int Slot = 0, string Text = "", string Error = "");
@ -10,7 +10,7 @@ internal sealed record DefCommand(DefVerb Verb, int Slot = 0, string Text = "",
internal static class DefCommandParser
{
public const string Usage =
"/rdef [use <slot or item name> | set <slot> [item name] | clear <slot> | bind <slot> <key> | unbind <slot> | "
"/rdef [use <slot or item name> | set <slot> [item name] | spell <slot> <spell name> | clear <slot> | bind <slot> <key> | unbind <slot> | "
+ "weapons [search] | fav | slots <count> | list | status | settings | panels (the game panel buttons) | selection]";
/// <summary>Parses the arguments after "/rdef" (blank = show or hide the bar).</summary>
@ -43,6 +43,13 @@ internal static class DefCommandParser
if (slotText == null || !TrySlot(slotText, out int slot)) return Fail("Which slot? e.g. /rdef set 3 (the item selected in the game goes in slot 3) or /rdef set 3 Peerless Healing Kit");
return new DefCommand(DefVerb.Set, slot, tail);
}
case "spell":
{
SplitSlot(rest, out string? slotText, out string tail);
if (slotText == null || !TrySlot(slotText, out int slot) || tail.Length == 0)
return Fail("Usage: /rdef spell <slot> <spell name>, e.g. /rdef spell 3 Heal Self VI (one of your spells; the slot casts it)");
return new DefCommand(DefVerb.Spell, slot, tail);
}
case "clear":
{
return TrySlot(rest, out int slot) ? new DefCommand(DefVerb.Clear, slot) : Fail("Which slot? e.g. /rdef clear 3");

View file

@ -4,17 +4,29 @@ using RynthCore.Plugin.RynthDefective.Items;
namespace RynthCore.Plugin.RynthDefective.Model;
/// <summary>One slot of the bar: an item (or nothing) and how pressing it uses that item.</summary>
/// <summary>One slot of the bar: an item or a spell (or nothing), and how pressing it uses that item.</summary>
internal sealed class DefSlot
{
/// <summary>The item the slot holds; null = empty.</summary>
/// <summary>The item the slot holds; null = none.</summary>
public ItemRef? Item;
/// <summary>How pressing it uses the item. <see cref="UseMode.Auto"/> picks by what the item is.</summary>
public UseMode Mode = UseMode.Auto;
/// <summary>The spell the slot casts (2026-10-28); 0 = none. A slot holds an item or a spell, never both.</summary>
public uint SpellId;
/// <summary>The spell's name when it was put in the slot (shown when the spell table doesn't know it).</summary>
public string SpellName = string.Empty;
public bool Empty => Item == null;
public bool Empty => Item == null && SpellId == 0;
public bool IsSpell => Item == null && SpellId != 0;
public DefSlot Clone() => new() { Item = Item?.Clone(), Mode = Mode };
/// <summary>What the slot holds, by name: the item's, the spell's, or "".</summary>
public string Label => Item?.FullName ?? (SpellId != 0 ? SpellLabel : string.Empty);
/// <summary>The spell's name: the table's, else the saved one, else "Spell 1234".</summary>
public string SpellLabel =>
SpellTable.TryGet(SpellId, out SpellInfo s) ? s.Name : SpellName.Length > 0 ? SpellName : SpellTable.Name(SpellId);
public DefSlot Clone() => new() { Item = Item?.Clone(), Mode = Mode, SpellId = SpellId, SpellName = SpellName };
}
/// <summary>The "use as" choice of a slot.</summary>

View file

@ -83,6 +83,12 @@ internal sealed class DefStore
slot.Item = ParseRef(e, "item");
int mode = Int(e, "mode", 0);
slot.Mode = mode >= 0 && mode <= (int)UseMode.Equip ? (UseMode)mode : UseMode.Auto;
// A spell (2026-10-28; a file from before has none): only in a slot without an item.
if (slot.Item == null && e.TryGetProperty("spell", out JsonElement sp) && sp.ValueKind == JsonValueKind.Object)
{
slot.SpellId = UInt(sp, "id");
slot.SpellName = slot.SpellId != 0 ? Str(sp, "name") : string.Empty;
}
}
}
ParseRefs(root, "favourites", d.Favourites, DefData.MaxFavourites);
@ -167,6 +173,13 @@ internal sealed class DefStore
{
w.WriteStartObject();
if (slot.Item != null) WriteRef(w, "item", slot.Item);
else if (slot.SpellId != 0)
{
w.WriteStartObject("spell");
w.WriteNumber("id", slot.SpellId);
w.WriteString("name", slot.SpellLabel);
w.WriteEndObject();
}
w.WriteNumber("mode", (int)slot.Mode);
w.WriteEndObject();
}

View file

@ -0,0 +1,188 @@
// ============================================================================
// RynthDefective - Model/Spells.cs
// Spell slots (2026-10-28): what a spell is called, whom it is cast on, and
// finding one of the character's spells by name. Pure (the tests run it).
//
// The engine's plugin API has no spell names or targets and no way to read
// the spell selected in AC's spellbook or spell bar, so the names and the
// "self-targeted" / "harmful" flags come from RynthAi's spell table
// (Combat\SpellData.txt, linked in as RynthInventory and RynthOracle link it:
// id, name, category, family, words, duration, difficulty, then three flags,
// the second harmful, the third self-targeted), and the player picks a spell
// by name from the ones the character knows (ReadKnownSpells).
// ============================================================================
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Reflection;
namespace RynthCore.Plugin.RynthDefective.Model;
/// <summary>One spell: its name, and whether it is cast on yourself only (recalls, Self buffs) or harms its target (war, debuffs).</summary>
internal readonly record struct SpellInfo(uint Id, string Name, bool SelfTargeted, bool Harmful);
internal static class SpellTable
{
private const string ResourceName = "RynthDefective.SpellData.txt";
private static Dictionary<uint, SpellInfo>? _byId;
public static bool TryGet(uint id, out SpellInfo info)
{
EnsureLoaded();
return _byId!.TryGetValue(id, out info);
}
/// <summary>The spell's name, or "Spell 1234" for one the table doesn't have (a server's own).</summary>
public static string Name(uint id) => TryGet(id, out SpellInfo s) ? s.Name : "Spell " + id.ToString(CultureInfo.InvariantCulture);
public static int Count
{
get { EnsureLoaded(); return _byId!.Count; }
}
private static void EnsureLoaded()
{
if (_byId != null) return;
Dictionary<uint, SpellInfo> map;
try
{
using Stream? s = Assembly.GetExecutingAssembly().GetManifestResourceStream(ResourceName);
if (s == null) map = new Dictionary<uint, SpellInfo>();
else
{
using var reader = new StreamReader(s, detectEncodingFromByteOrderMarks: true);
map = Parse(reader);
}
}
catch
{
map = new Dictionary<uint, SpellInfo>(); // a missing table costs names ("Spell 1234") and targets (by the selection)
}
_byId = map;
}
/// <summary>The table's lines (the tests read their own).</summary>
internal static Dictionary<uint, SpellInfo> Parse(TextReader reader)
{
var map = new Dictionary<uint, SpellInfo>(6400);
while (reader.ReadLine() is string line)
{
string[] c = line.Split('\t');
if (c.Length < 2 || !uint.TryParse(c[0].Trim(), NumberStyles.None, CultureInfo.InvariantCulture, out uint id) || id == 0) continue;
string name = c[1].Trim();
if (name.Length == 0) continue;
bool harmful = c.Length > 8 && c[8].Trim().Equals("True", StringComparison.OrdinalIgnoreCase);
bool self = c.Length > 9 && c[9].Trim().Equals("True", StringComparison.OrdinalIgnoreCase);
map[id] = new SpellInfo(id, name, self, harmful);
}
return map;
}
/// <summary>The character's spells (ids) with names, A-Z; ones the table doesn't name are listed by number.</summary>
public static SpellInfo[] Known(ReadOnlySpan<uint> ids)
{
var list = new List<SpellInfo>(ids.Length);
var seen = new HashSet<uint>();
foreach (uint id in ids)
{
if (id == 0 || !seen.Add(id)) continue;
list.Add(TryGet(id, out SpellInfo s) ? s : new SpellInfo(id, Name(id), false, false));
}
list.Sort((a, b) =>
{
int c = string.Compare(a.Name, b.Name, StringComparison.OrdinalIgnoreCase);
return c != 0 ? c : a.Id.CompareTo(b.Id);
});
return list.ToArray();
}
/// <summary>
/// The spells in <paramref name="known"/> that match what the player typed, best first: the exact
/// name, then names starting with it, then names holding every word of it ("heal self", "vi heal").
/// At most <paramref name="max"/>; <paramref name="total"/> is how many matched. Blank = all of them.
/// </summary>
public static List<SpellInfo> Search(IReadOnlyList<SpellInfo> known, string? query, int max, out int total)
{
string q = (query ?? string.Empty).Trim();
string[] words = q.Split(' ', StringSplitOptions.RemoveEmptyEntries);
var exact = new List<SpellInfo>();
var starts = new List<SpellInfo>();
var contains = new List<SpellInfo>();
foreach (SpellInfo s in known)
{
if (q.Length == 0) { contains.Add(s); continue; }
if (s.Name.Equals(q, StringComparison.OrdinalIgnoreCase)) exact.Add(s);
else if (s.Name.StartsWith(q, StringComparison.OrdinalIgnoreCase)) starts.Add(s);
else if (AllWords(s.Name, words)) contains.Add(s);
}
total = exact.Count + starts.Count + contains.Count;
var result = new List<SpellInfo>(Math.Min(total, Math.Max(0, max)));
foreach (List<SpellInfo> group in new[] { exact, starts, contains })
foreach (SpellInfo s in group)
{
if (result.Count >= max) return result;
result.Add(s);
}
return result;
}
private static bool AllWords(string name, string[] words)
{
if (words.Length == 0) return false;
foreach (string w in words)
if (name.IndexOf(w, StringComparison.OrdinalIgnoreCase) < 0) return false;
return true;
}
}
/// <summary>Whom a spell slot's cast goes to.</summary>
internal enum CastAt { Yourself, Selection, Refused }
internal static class SpellTargeting
{
/// <summary>
/// The target for casting <paramref name="spell"/> now. A self-targeted spell (a recall, a Self buff)
/// is cast on yourself whatever is selected. Any other spell goes on the selection; with nothing
/// selected a beneficial one (Heal Other, a buff, an item spell) is cast on yourself, a harmful one
/// (war, void, a debuff) is refused, and so is a harmful one with yourself selected. A spell the table
/// doesn't know goes on the selection, else on yourself. <paramref name="why"/> says why it was refused.
/// </summary>
public static CastAt Decide(SpellInfo? spell, uint selected, uint player, out uint target, out string why)
{
why = string.Empty;
target = 0;
if (player == 0)
{
why = "the character isn't ready";
return CastAt.Refused;
}
if (spell is { SelfTargeted: true })
{
target = player;
return CastAt.Yourself;
}
bool harmful = spell is { Harmful: true };
if (selected != 0 && selected != player)
{
target = selected;
return CastAt.Selection;
}
if (harmful)
{
why = selected == player ? "select a monster first (you have yourself selected)" : "select a target first";
return CastAt.Refused;
}
target = player;
return CastAt.Yourself;
}
/// <summary>"cast on you" / "cast on the selection (else on you)" / "cast on the selected monster", for the slot's tooltip.</summary>
public static string Describe(SpellInfo? spell) => spell switch
{
{ SelfTargeted: true } => "cast on you",
{ Harmful: true } => "cast on the selected target",
_ => "cast on the selected target, or on you with nothing selected",
};
}