RynthSuite/Tools/RynthCore.RynthAiHostTests/Tests/VTankLootTests.cs

518 lines
31 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using RynthCore.Loot;
using RynthCore.Loot.VTank;
using RynthCore.Plugin.RynthAi;
using RynthCore.Plugin.RynthAi.Loot;
using RynthCore.RynthAiHostTests.Fakes;
using RynthCore.RynthAiTests.Harness;
using N = RynthCore.Loot.VTank.VTankNodeTypes;
namespace RynthCore.RynthAiHostTests.Tests;
// VTank (.utl) loot rules: VTankLootEvaluator per node type, first-match rule order
// (FirstMatch, which corpse looting uses), the character/spell context read through the
// fake host, and whole .utl texts parsed and then evaluated.
internal static class VTankLootTests
{
private const uint Player = 0x50000A01;
public static void Register(Runner r)
{
r.Add("loot vtank: no item never matches; no conditions always does", NullAndEmpty);
r.Add("loot vtank: first match in list order, Priority ignored", RuleOrder);
r.Add("loot vtank: DisabledRule node (9999)", DisabledRuleNode);
r.Add("loot vtank: string value match", StringValueMatch);
r.Add("loot vtank: long keys LE/GE/E/NE and flag, at the boundary", LongKeys);
r.Add("loot vtank: Decal's Type key (0x0D000000) is the WCID - 'loot everything that isn't retail'", WcidKey);
r.Add("loot vtank: double keys LE/GE", DoubleKeys);
r.Add("loot vtank: object class", ObjectClass);
r.Add("loot vtank: value and workmanship requirements", ValueAndWorkmanship);
r.Add("loot vtank: damage nodes (min, median, missile, tinked, percent)", DamageNodes);
r.Add("loot vtank: buffed long/double keys and total ratings", BuffedKeysAndRatings);
r.Add("loot vtank: colour nodes pass, not-implemented nodes fail", ColourAndUnimplemented);
r.Add("loot vtank: spell nodes pass with no context", SpellNodesNoContext);
r.Add("loot vtank: spell name match / spell match / spell count through the host", SpellNodesWithContext);
r.Add("loot vtank: spell nodes with a host that cannot list spells", SpellNodesNoSpellApi);
r.Add("loot vtank: character skill, base skill, level, pack slots", CharacterNodes);
r.Add("loot vtank: slot exact palette", SlotExactPalette);
r.Add("loot vtank: malformed data lines make the rule not match", MalformedData);
r.Add("loot vtank: unknown node type", UnknownNodeType);
r.Add("loot vtank: .utl text parsed then evaluated (keep, keep #, salvage, sell, read)", ParsedProfile);
r.Add("loot vtank: fixture file with a SalvageCombine block", FixtureFile);
r.Add("loot vtank: malformed .utl text", MalformedUtl);
r.Add("loot keep#: counts whole stacks of what the rule matches", KeepCapCountsStacks);
r.Add("loot keep#: pending approvals and merged stacks", KeepCapPending);
r.Add("loot keep#: the cap boundary, zero and missing counts", KeepCapBoundary);
}
private static VTankLootCondition C(int node, params string[] data) => new(node, "0", data);
private static VTankLootRule Rule(string name, VTankLootAction action, params VTankLootCondition[] conds)
{
var r = new VTankLootRule { Name = name, Action = action };
r.Conditions.AddRange(conds);
return r;
}
private static bool M(WorldObject item, params VTankLootCondition[] conds)
=> VTankLootEvaluator.Match(Rule("r", VTankLootAction.Keep, conds), item);
private static bool MC(WorldObject item, VTankLootContext ctx, params VTankLootCondition[] conds)
=> VTankLootEvaluator.Match(Rule("r", VTankLootAction.Keep, conds), item, ctx);
private static void NullAndEmpty()
{
Check.False(VTankLootEvaluator.Match(Rule("r", VTankLootAction.Keep), null), "null item");
Check.True(VTankLootEvaluator.Match(Rule("r", VTankLootAction.Keep), new Item("x")), "no conditions = unconditional");
Check.True(VTankLootEvaluator.Match(Rule("r", VTankLootAction.Keep), new Item("")), "even with an empty name");
}
private static void RuleOrder()
{
var sell = Rule("sell junk", VTankLootAction.Sell, C(N.ObjectClass, "11"));
sell.Priority = 99;
var keep = Rule("keep gems", VTankLootAction.Keep, C(N.ObjectClass, "11"));
keep.Priority = 1;
var profile = new VTankLootProfile();
profile.Rules.AddRange(new[] { sell, keep });
var gem = new Item("Opal", AcObjectClass.Gem);
var hit = VTankLootEvaluator.FirstMatch(profile, gem, null, out int idx);
Check.True(ReferenceEquals(hit, sell), "the first rule in the list wins even with a worse priority");
Check.Eq(idx, 0, "index of the winner");
var miss = VTankLootEvaluator.FirstMatch(profile, new Item("Plate", AcObjectClass.Armor), null, out int idx2);
Check.Null(miss, "no rule for armor");
Check.Eq(idx2, -1, "index -1 when nothing matches");
var none = VTankLootEvaluator.FirstMatch(new VTankLootProfile(), gem, null, out int idx3);
Check.Null(none, "empty profile");
Check.Eq(idx3, -1, "empty profile index");
var nullItem = VTankLootEvaluator.FirstMatch(profile, null, null, out _);
Check.Null(nullItem, "null item matches nothing");
// A rule that throws inside (bad data) does not stop later rules from matching.
var broken = Rule("broken", VTankLootAction.Keep, C(N.LongValKeyGE, "abc", "19"));
var profile2 = new VTankLootProfile();
profile2.Rules.AddRange(new[] { broken, keep });
Check.True(ReferenceEquals(VTankLootEvaluator.FirstMatch(profile2, gem, null, out int i4), keep) && i4 == 1, "broken rule skipped");
}
private static void DisabledRuleNode()
{
var item = new Item("x");
Check.False(M(item, C(N.DisabledRule, "true")), "true disables");
Check.False(M(item, C(N.DisabledRule, " TRUE ")), "any case, trimmed");
Check.True(M(item, C(N.DisabledRule, "false")), "false leaves it enabled");
Check.True(M(item, C(N.DisabledRule, "yes")), "anything but true leaves it enabled");
Check.True(M(item, new VTankLootCondition(N.DisabledRule, "0", Array.Empty<string>())), "no data line: enabled");
var r = Rule("r", VTankLootAction.Keep, C(N.ObjectClass, "8"));
r.Enabled = false;
Check.False(VTankLootEvaluator.Match(r, new Item("x", AcObjectClass.Misc)), "Enabled=false adds a disabling node");
r.Enabled = true;
Check.True(VTankLootEvaluator.Match(r, new Item("x", AcObjectClass.Misc)), "Enabled=true removes it again");
}
private static void StringValueMatch()
{
var kit = new Item("Peerless Healing Kit");
Check.True(M(kit, C(N.StringValueMatch, "healing kit", "1")), "name via the object-name fallback, any case");
Check.False(M(kit, C(N.StringValueMatch, "^Healing", "1")), "anchored mismatch");
Check.False(M(kit, C(N.StringValueMatch, "Healing Kit (", "1")), "a bad regex is no match, not an exception");
var scroll = new Item("Scroll").Str(16, "Inscribed");
Check.True(M(scroll, C(N.StringValueMatch, "^inscribed$", "16")), "long description key");
Check.True(M(scroll, C(N.StringValueMatch, "^$", "15")), "missing string reads empty");
}
private static void LongKeys()
{
var item = new Item("x").Int(19, 1000).Int(9, 0x6);
Check.True(M(item, C(N.LongValKeyGE, "1000", "19")), "GE equal");
Check.False(M(item, C(N.LongValKeyGE, "1001", "19")), "GE above");
Check.True(M(item, C(N.LongValKeyLE, "1000", "19")), "LE equal");
Check.False(M(item, C(N.LongValKeyLE, "999", "19")), "LE below");
Check.True(M(item, C(N.LongValKeyE, "1000", "19")), "E");
Check.False(M(item, C(N.LongValKeyNE, "1000", "19")), "NE same");
Check.True(M(item, C(N.LongValKeyNE, "0", "19")), "NE different");
Check.True(M(item, C(N.LongValKeyFlagExists, "2", "9")), "flag set");
Check.False(M(item, C(N.LongValKeyFlagExists, "8", "9")), "flag not set");
Check.True(M(item, C(N.LongValKeyLE, "5", "105")), "missing key reads 0 and passes LE");
Check.True(M(item, C(N.LongValKeyGE, " 1000 ", " 19 ")), "surrounding spaces in data lines are accepted");
}
// Tom (2026-10-04): a rule that loots everything not retail by weenie id. Retail WCIDs on
// Aelrynth end at 53488; custom content is above. VTank writes it as LongValKeyGE on Decal's
// synthetic "Type" key, which RynthAi now reads as the WCID through the object cache.
private static void WcidKey()
{
FakeHost.Reset();
var host = FakeHost.Create();
var cache = new WorldObjectCache(host);
const uint Custom = 0x80001001, Retail = 0x80001002;
FakeHost.Wcids[Custom] = 3000105;
FakeHost.Wcids[Retail] = 20646;
var custom = new WorldObject(unchecked((int)Custom), "Driftwarden Greataxe", AcObjectClass.MeleeWeapon) { Cache = cache };
var retail = new WorldObject(unchecked((int)Retail), "Ust", AcObjectClass.Ust) { Cache = cache };
string type = WorldObjectCache.DecalTypeKey.ToString(System.Globalization.CultureInfo.InvariantCulture);
Check.Eq(custom.Values(unchecked((int)WorldObjectCache.DecalTypeKey), 0), 3000105, "the Type key reads the WCID");
Check.True(M(custom, C(N.LongValKeyGE, "53489", type)), "custom WCID 3000105 >= 53489: looted");
Check.False(M(retail, C(N.LongValKeyGE, "53489", type)), "retail WCID 20646: not");
Check.True(M(retail, C(N.LongValKeyE, "20646", type)), "an exact WCID match works too");
}
private static void DoubleKeys()
{
var item = new Item("x").Dbl(29, 1.25);
Check.True(M(item, C(N.DoubleValKeyGE, "1.25", "29")), "GE equal");
Check.False(M(item, C(N.DoubleValKeyGE, "1.26", "29")), "GE above");
Check.True(M(item, C(N.DoubleValKeyLE, "1.25", "29")), "LE equal");
Check.False(M(item, C(N.DoubleValKeyLE, "1.24", "29")), "LE below");
Check.True(M(item, C(N.DoubleValKeyGE, "1e0", "29")), "exponent notation parses");
}
private static void ObjectClass()
{
Check.True(M(new Item("x", AcObjectClass.Salvage), C(N.ObjectClass, "39")), "salvage is class 39");
Check.False(M(new Item("x", AcObjectClass.Gem), C(N.ObjectClass, "39")), "gem is not");
Check.True(M(new Item("x", AcObjectClass.MeleeWeapon), C(N.ObjectClass, "1")), "melee weapon is 1");
}
private static void ValueAndWorkmanship()
{
// "Keep anything worth 5000+"; "salvage workmanship 7+ granite"
var valuable = Rule("value", VTankLootAction.Keep, C(N.LongValKeyGE, "5000", "19"));
Check.True(VTankLootEvaluator.Match(valuable, new Item("x").Int(19, 5000)), "value at the threshold");
Check.False(VTankLootEvaluator.Match(valuable, new Item("x").Int(19, 4999)), "value one below");
Check.False(VTankLootEvaluator.Match(valuable, new Item("x")), "unappraised value reads 0");
var granite = Rule("granite", VTankLootAction.Salvage,
C(N.ObjectClass, "39"), C(N.LongValKeyE, "67", "131"), C(N.LongValKeyGE, "7", "105"));
Check.True(VTankLootEvaluator.Match(granite, new Item("Salvage", AcObjectClass.Salvage).Int(131, 67).Int(105, 7)), "wk 7 granite bag");
Check.False(VTankLootEvaluator.Match(granite, new Item("Salvage", AcObjectClass.Salvage).Int(131, 67).Int(105, 6)), "wk 6");
Check.False(VTankLootEvaluator.Match(granite, new Item("Salvage", AcObjectClass.Salvage).Int(131, 64).Int(105, 9)), "steel");
}
private static void DamageNodes()
{
var sword = new Item("Sword").Int(54, 40).Dbl(22, 0.5); // min 20, median 30
Check.True(M(sword, C(N.MinDamageGE, "20")), "min 20");
Check.False(M(sword, C(N.MinDamageGE, "20.5")), "min above");
Check.True(M(sword, C(N.BuffedMedianDamageGE, "30")), "median 30");
Check.False(M(sword, C(N.BuffedMedianDamageGE, "30.1")), "median above");
Check.True(M(sword, C(N.CalcdBuffedTinkedDamageGE, "40")), "tinked damage reads max damage");
Check.False(M(sword, C(N.DamagePercentGE, "0")), "damage percent always fails (as VTank)");
var nodmg = new Item("Stick");
Check.False(M(nodmg, C(N.MinDamageGE, "0")), "no max damage: min damage fails even at 0");
Check.False(M(nodmg, C(N.BuffedMedianDamageGE, "0")), "no max damage: median fails");
Check.False(M(nodmg, C(N.BuffedMissileDamageGE, "0")), "no max damage: missile fails");
var bow = new Item("Bow").Int(54, 50).Dbl(152, 1.2);
Check.True(M(bow, C(N.BuffedMissileDamageGE, "60")), "50 * elemental 1.2 = 60");
Check.False(M(bow, C(N.BuffedMissileDamageGE, "60.1")), "just above");
var weakBow = new Item("Bow").Int(54, 50).Dbl(152, 0.5);
Check.True(M(weakBow, C(N.BuffedMissileDamageGE, "50")), "an elemental bonus below 1 counts as 1");
}
private static void BuffedKeysAndRatings()
{
var item = new Item("x").Int(28, 300).Dbl(29, 1.3);
Check.True(M(item, C(N.BuffedLongValKeyGE, "300", "28")), "buffed long reads the same key");
Check.True(M(item, C(N.BuffedLongValKeyGE, "299.5", "28")), "value may be fractional");
Check.False(M(item, C(N.BuffedLongValKeyGE, "300.5", "28")), "fractional above");
Check.True(M(item, C(N.BuffedDoubleValKeyGE, "1.3", "29")), "buffed double");
var gear = new Item("Gear").Int(370, 3).Int(379, 2).Int(377, 10);
Check.True(M(gear, C(N.TotalRatingsGE, "5")), "370 + 379");
Check.False(M(gear, C(N.TotalRatingsGE, "6")), "377 not counted");
}
private static void ColourAndUnimplemented()
{
var item = new Item("x");
Check.True(M(item, C(N.AnySimilarColor, "1", "2", "3", "4", "5")), "any similar colour passes");
Check.True(M(item, C(N.SimilarColorArmorType, "1", "2", "3", "4", "5", "6")), "armor-type colour passes");
Check.True(M(item, C(N.SlotSimilarColor, "1", "2", "3", "4", "5", "6")), "slot colour passes");
Check.False(M(item, C(N.CalcedBuffedTinkedTargetMeleeGE, "1", "1", "1")), "tinked target melee is not implemented: fails");
}
private static void SpellNodesNoContext()
{
var item = new Item("x");
Check.True(M(item, C(N.SpellNameMatch, "Blood Drinker")), "spell name match with no context");
Check.True(M(item, C(N.SpellMatch, "Blood", "", "1")), "spell match with no context");
Check.True(M(item, C(N.SpellCountGE, "5")), "spell count with no context");
Check.True(M(item, C(N.CharacterSkillGE, "999", "34")), "character skill with no context");
Check.True(M(item, C(N.CharacterLevelGE, "999")), "level GE with no context");
Check.True(M(item, C(N.CharacterLevelLE, "0")), "level LE with no context");
Check.True(M(item, C(N.CharacterMainPackEmptySlotsGE, "999")), "pack slots with no context");
Check.True(M(item, C(N.CharacterBaseSkill, "34", "5", "6")), "base skill with no context");
}
private static int Spell(string name)
{
int id = SpellDatabase.GetIdByName(name);
if (id == 0) throw new InvalidOperationException($"spell '{name}' is not in SpellData.txt");
return id;
}
private static void SpellNodesWithContext()
{
FakeHost.Reset();
var host = FakeHost.Create();
var ctx = new VTankLootContext(host, Player);
var wand = new Item("Wand", AcObjectClass.WandStaffOrb);
uint wandId = unchecked((uint)wand.Wo.Id);
FakeHost.SpellIds[wandId] = new[]
{
(uint)Spell("Aura of Blood Drinker Self VI"),
(uint)Spell("Strength Other VI"),
(uint)Spell("Strength Self VI"),
};
Check.True(MC(wand, ctx, C(N.SpellNameMatch, "blood drinker")), "name regex, any case");
Check.False(MC(wand, ctx, C(N.SpellNameMatch, "^Heart Seeker")), "spell it does not have");
Check.False(MC(wand, ctx, C(N.SpellNameMatch, "Blood (")), "bad regex: no match");
Check.True(MC(wand, ctx, C(N.SpellCountGE, "3")), "three spells");
Check.False(MC(wand, ctx, C(N.SpellCountGE, "4")), "not four");
Check.True(MC(wand, ctx, C(N.SpellMatch, "Strength", "", "2")), "two Strength spells");
Check.False(MC(wand, ctx, C(N.SpellMatch, "Strength", "Other", "2")), "only one once Other is excluded");
Check.True(MC(wand, ctx, C(N.SpellMatch, "Strength", "Other", "1")), "one Strength Self");
Check.True(MC(wand, ctx, C(N.SpellMatch, "Strength", " ", "1")), "a blank exclude pattern excludes nothing");
Check.False(MC(wand, ctx, C(N.SpellMatch, "Strength (", "", "1")), "bad include regex: no match");
var plain = new Item("Stick");
Check.False(MC(plain, ctx, C(N.SpellNameMatch, ".")), "an item with no spells");
Check.True(MC(plain, ctx, C(N.SpellCountGE, "0")), "count >= 0 holds for no spells");
Check.True(MC(plain, ctx, C(N.SpellMatch, ".", "", "0")) == false, "spell match with count 0 still needs one matching spell");
}
private static void SpellNodesNoSpellApi()
{
var ctx = new VTankLootContext(default, Player); // null host: no spell list at all
var wand = new Item("Wand");
Check.False(MC(wand, ctx, C(N.SpellNameMatch, ".")), "a host that cannot list spells: name match fails");
Check.False(MC(wand, ctx, C(N.SpellCountGE, "1")), "count reads 0");
Check.True(MC(wand, ctx, C(N.SlotExactPalette, "0", "123")), "no palette data passes");
}
private static void CharacterNodes()
{
FakeHost.Reset();
FakeHost.Skills[(Player, 34)] = (Buffed: 400, Training: 3);
FakeHost.Ints[(Player, 25)] = 275; // LEVEL
var ctx = new VTankLootContext(FakeHost.Create(), Player);
var item = new Item("x");
Check.True(MC(item, ctx, C(N.CharacterSkillGE, "400", "34")), "skill 400 >= 400");
Check.False(MC(item, ctx, C(N.CharacterSkillGE, "401", "34")), "skill 400 < 401");
Check.False(MC(item, ctx, C(N.CharacterSkillGE, "1", "33")), "a skill the host has no value for reads 0");
// "Base skill" is checked against the training class (2 trained, 3 specialized).
Check.True(MC(item, ctx, C(N.CharacterBaseSkill, "34", "3", "3")), "specialized in [3,3]");
Check.False(MC(item, ctx, C(N.CharacterBaseSkill, "34", "0", "2")), "not in [0,2]");
Check.True(MC(item, ctx, C(N.CharacterLevelGE, "275")), "level GE at the value");
Check.False(MC(item, ctx, C(N.CharacterLevelGE, "276")), "level GE above");
Check.True(MC(item, ctx, C(N.CharacterLevelLE, "275")), "level LE at the value");
Check.False(MC(item, ctx, C(N.CharacterLevelLE, "274")), "level LE below");
Check.True(MC(item, ctx, C(N.CharacterMainPackEmptySlotsGE, "102")), "no cache: all 102 main-pack slots count as empty");
Check.False(MC(item, ctx, C(N.CharacterMainPackEmptySlotsGE, "103")), "never more than 102");
var noPlayer = new VTankLootContext(FakeHost.Create(), 0);
Check.False(MC(item, noPlayer, C(N.CharacterLevelGE, "1")), "no player id: level reads 0");
Check.True(MC(item, noPlayer, C(N.CharacterLevelLE, "0")), "no player id: level 0 passes LE 0");
}
private static void SlotExactPalette()
{
FakeHost.Reset();
var ctx = new VTankLootContext(FakeHost.Create(), Player);
var robe = new Item("Robe");
FakeHost.Palettes[unchecked((uint)robe.Wo.Id)] = new uint[] { 0x04001234, 0x04005678 };
// The palette id in the rule is decimal; only the low 24 bits are compared.
Check.True(MC(robe, ctx, C(N.SlotExactPalette, "1", "22136")), "slot 1 = 0x005678 = 22136");
Check.True(MC(robe, ctx, C(N.SlotExactPalette, "0", "4660")), "slot 0 = 0x001234 = 4660");
Check.True(MC(robe, ctx, C(N.SlotExactPalette, "0", "117445172")), "the high byte is ignored (0x07001234)");
Check.False(MC(robe, ctx, C(N.SlotExactPalette, "0", "22136")), "wrong palette for the slot");
Check.False(MC(robe, ctx, C(N.SlotExactPalette, "2", "4660")), "slot past the end");
Check.False(MC(robe, ctx, C(N.SlotExactPalette, "-1", "4660")), "negative slot");
var plain = new Item("Plain");
Check.True(MC(plain, ctx, C(N.SlotExactPalette, "5", "1")), "an item with no palette data passes");
}
private static void MalformedData()
{
var item = new Item("x").Int(19, 10);
Check.False(M(item, C(N.LongValKeyGE, "ten", "19")), "non-numeric value");
Check.False(M(item, C(N.LongValKeyGE, "5")), "missing key line");
Check.False(M(item, new VTankLootCondition(N.ObjectClass, "0", Array.Empty<string>())), "no data lines at all");
Check.False(M(item, C(N.DoubleValKeyGE, "1.5.5", "29")), "garbled double");
Check.False(M(item, C(N.LongValKeyGE, "99999999999", "19")), "value out of int range");
Check.Throws<FormatException>(() => VTankLootEvaluator.MatchCondition(C(N.LongValKeyGE, "ten", "19"), item, null),
"MatchCondition lets the parse error out (AutoVendor treats it as 'cannot tell')");
}
private static void UnknownNodeType()
{
var item = new Item("x");
Check.False(M(item, C(4242, "a")), "unknown node type: the rule does not match");
Check.Throws<InvalidOperationException>(() => VTankLootEvaluator.MatchCondition(C(4242, "a"), item, null),
"MatchCondition reports an unsupported node type");
}
// A small hand-written v1 profile: Keep, Keep # 3, Salvage, Sell, Read.
internal const string SampleUtl =
"UTL\n1\n6\n" +
"Disabled keep all\n\n0;1;9999\n0\ntrue\n" +
"Keep valuables\n\n0;1;3\n0\n5000\n19\n" +
"Keep 3 kits\n\n0;10;1\n3\n0\nHealing Kit$\n1\n" +
"Salvage granite 7+\n\n0;2;7;12;3\n0\n39\n0\n67\n131\n0\n7\n105\n" +
"Sell weapons\n\n0;3;7\n0\n1\n" +
"Read books\n\n0;4;7\n0\n33\n";
private static void ParsedProfile()
{
var p = VTankLootParser.LoadFromText(SampleUtl);
Check.Eq(p.Rules.Count, 6, "six rules parsed");
Check.Eq(p.Rules[2].Action, VTankLootAction.KeepUpTo, "rule 3 is Keep #");
Check.Eq(p.Rules[2].KeepCount ?? -1, 3, "Keep # count");
Check.False(p.Rules[0].Enabled, "rule 1 is disabled");
(VTankLootAction? Action, int Index) Classify(WorldObject wo)
{
var hit = VTankLootEvaluator.FirstMatch(p, wo, null, out int i);
return (hit?.Action, i);
}
Check.Eq(Classify(new Item("Pearl").Int(19, 6000)), ((VTankLootAction?)VTankLootAction.Keep, 1), "valuable -> Keep (the disabled catch-all is skipped)");
Check.Eq(Classify(new Item("Peerless Healing Kit")), ((VTankLootAction?)VTankLootAction.KeepUpTo, 2), "kit -> Keep #");
Check.Eq(Classify(new Item("Salvage", AcObjectClass.Salvage).Int(131, 67).Int(105, 8)), ((VTankLootAction?)VTankLootAction.Salvage, 3), "granite 8 -> Salvage");
Check.Eq(Classify(new Item("Salvage", AcObjectClass.Salvage).Int(131, 67).Int(105, 6)), ((VTankLootAction?)null, -1), "granite 6 -> nothing");
Check.Eq(Classify(new Item("Sword", AcObjectClass.MeleeWeapon).Int(19, 6000)), ((VTankLootAction?)VTankLootAction.Keep, 1), "a valuable sword is kept before the sell rule");
Check.Eq(Classify(new Item("Sword", AcObjectClass.MeleeWeapon).Int(19, 100)), ((VTankLootAction?)VTankLootAction.Sell, 4), "a cheap sword -> Sell");
Check.Eq(Classify(new Item("Tome", AcObjectClass.Book)), ((VTankLootAction?)VTankLootAction.Read, 5), "book -> Read");
Check.Eq(Classify(new Item("Kit of healing")), ((VTankLootAction?)null, -1), "the $ anchor keeps a near-miss name out");
}
private static void FixtureFile()
{
string path = Path.Combine(AppContext.BaseDirectory, "Fixtures", "salvage-combine.utl");
var p = VTankLootParser.Load(path);
Check.Eq(p.FileVersion, 1, "v1 file");
Check.Eq(p.Rules.Count, 2, "two rules");
Check.NotNull(p.SalvageCombine, "SalvageCombine block read");
if (p.SalvageCombine != null)
{
Check.True(p.SalvageCombine.Enabled, "combine enabled");
Check.Eq(p.SalvageCombine.DefaultBands, "1-6,7-8,9,10", "default bands");
Check.Eq(p.SalvageCombine.PerMaterial.Count, 1, "one per-material override");
Check.Eq(p.SalvageCombine.PerMaterial.TryGetValue(67, out string? g) ? g : "", "1-10", "granite combines everything");
Check.Eq(p.SalvageCombine.GetBandKey(64, 7), "7-8", "steel wk 7 -> default band 7-8");
Check.Eq(p.SalvageCombine.GetBandKey(64, 10), "10-10", "a single-number band");
Check.Eq(p.SalvageCombine.GetBandKey(67, 3), "1-10", "granite uses its own band");
Check.Null(p.SalvageCombine.GetBandKey(64, 11), "outside every band: do not combine");
Check.Null(p.SalvageCombine.GetBandKey(64, 0), "workmanship 0 is outside the bands");
}
Check.Eq(SalvageCombineSettings.ParseBands("a-b, 5, 3-, 7 - 8").Count, 2, "only '5' and '7 - 8' parse");
Check.Eq(SalvageCombineSettings.ParseBands("").Count, 0, "blank bands");
var reversed = new SalvageCombineSettings { DefaultBands = "8-7" };
Check.Null(reversed.GetBandKey(1, 7), "a reversed band matches nothing");
var hit = VTankLootEvaluator.FirstMatch(p, new Item("Salvage", AcObjectClass.Salvage).Int(131, 64).Int(105, 3), null, out int i);
Check.Eq(hit?.Action, VTankLootAction.Salvage, "any salvage bag -> Salvage");
Check.Eq(i, 1, "second rule");
Check.Eq(VTankLootParser.Load(Path.Combine(AppContext.BaseDirectory, "Fixtures", "nope.utl")).Rules.Count, 0, "a missing file loads as an empty profile");
}
private static void MalformedUtl()
{
Check.Throws<InvalidOperationException>(() => VTankLootParser.LoadFromText(""), "empty text");
Check.Throws<InvalidOperationException>(() => VTankLootParser.LoadFromText("UTL\n1"), "too short");
Check.Throws<InvalidOperationException>(() => VTankLootParser.LoadFromText("UTL\nx\n1\n"), "bad version");
Check.Throws<InvalidOperationException>(() => VTankLootParser.LoadFromText("UTL\n1\nmany\n"), "bad rule count");
Check.Throws<InvalidOperationException>(() => VTankLootParser.LoadFromText("{\n\"Rules\": []\n}"), "a JSON profile is not a .utl");
// Declares 3 rules but holds 1: the one that is there still works.
var p = VTankLootParser.LoadFromText("UTL\n1\n3\nKeep gems\n\n0;1;7\n0\n11\n");
Check.Eq(p.Rules.Count, 1, "truncated file: rules that exist are kept");
Check.True(VTankLootEvaluator.Match(p.Rules[0], new Item("Opal", AcObjectClass.Gem)), "and they match");
// A rule cut off in the middle of its data lines does not match anything.
var cut = VTankLootParser.LoadFromText("UTL\n1\n1\nValue\n\n0;1;3\n0\n5000\n");
Check.Eq(cut.Rules.Count, 1, "cut rule parsed");
Check.False(VTankLootEvaluator.Match(cut.Rules[0], new Item("x").Int(19, 9000)), "missing key line: no match");
// Legacy v0 (no header): rule count first, no custom expression or length-code lines.
var v0 = VTankLootParser.LoadFromText("1\nKeep gems\n0;1;7\n11\n");
Check.Eq(v0.FileVersion, 0, "v0 file");
Check.True(v0.Rules.Count == 1 && VTankLootEvaluator.Match(v0.Rules[0], new Item("Opal", AcObjectClass.Gem)), "v0 rule works");
// Unknown action number and unknown node type survive parsing; the rule just never matches.
var odd = VTankLootParser.LoadFromText("UTL\n1\n2\nOdd\n\n0;7;4242\n0\nwhatever\nKeep gems\n\n0;1;7\n0\n11\n");
Check.Eq(odd.Rules.Count, 2, "unknown node type slurped up to the next rule");
Check.Eq((int)odd.Rules[0].Action, 7, "unknown action kept as a number");
Check.False(VTankLootEvaluator.Match(odd.Rules[0], new Item("x")), "unknown node: no match");
Check.True(ReferenceEquals(VTankLootEvaluator.FirstMatch(odd, new Item("Opal", AcObjectClass.Gem), null, out _), odd.Rules[1]), "next rule still reached");
// CRLF line endings read the same as LF.
var crlf = VTankLootParser.LoadFromText(SampleUtl.Replace("\n", "\r\n"));
Check.Eq(crlf.Rules.Count, 6, "CRLF file");
Check.Eq(crlf.Rules[2].KeepCount ?? -1, 3, "CRLF Keep # count");
}
// ── Keep # arithmetic (LootKeepCap, used by corpse looting) ──────────────
private static readonly IReadOnlyDictionary<int, int> NoPending = new Dictionary<int, int>();
private static bool IsKit(WorldObject wo) => wo.Name.Contains("Healing Kit", StringComparison.OrdinalIgnoreCase);
private static void KeepCapCountsStacks()
{
var pack = new List<WorldObject>
{
new Item("Healing Kit").Int(12, 5), // a stack of 5
new Item("Healing Kit"), // no stack count: counts 1
new Item("Healing Kit").Int(12, 0), // stack 0 still counts 1
new Item("Mana Stone").Int(12, 50), // not matched
new Item(""), // nameless: skipped before matching
};
Check.Eq(LootKeepCap.CountHave(pack, IsKit, NoPending, _ => true), 7, "5 + 1 + 1");
Check.Eq(LootKeepCap.CountHave(null, IsKit, NoPending, _ => true), 0, "no cache: nothing counted");
Check.Eq(LootKeepCap.CountHave(new List<WorldObject>(), IsKit, NoPending, _ => true), 0, "empty pack");
Check.Eq(LootKeepCap.CountHave(pack, _ => throw new InvalidOperationException(), NoPending, _ => true), 0, "a matcher that throws counts as no match");
Check.Eq(LootKeepCap.StackOf(new Item("x").Int(12, 25)), 25, "stack of 25");
Check.Eq(LootKeepCap.StackOf(new Item("x").Int(12, -3)), 1, "negative stack counts 1");
}
private static void KeepCapPending()
{
var inPack = new Item("Healing Kit", id: 0x70000001).Int(12, 2);
var pack = new List<WorldObject> { inPack };
var pending = new Dictionary<int, int>
{
[0x70000001] = 2, // approved earlier and already in the pack: not counted twice
[0x70000002] = 3, // approved, still on its way
[0x70000003] = 4, // approved, then merged into a pack stack and deleted
};
bool Exists(int id) => id != 0x70000003;
Check.Eq(LootKeepCap.CountHave(pack, IsKit, pending, Exists), 5, "2 in the pack + 3 on the way");
Check.Eq(LootKeepCap.CountHave(null, IsKit, pending, _ => false), 0, "no cache: pending ones cannot be confirmed");
}
private static void KeepCapBoundary()
{
Check.True(LootKeepCap.UnderCap(4, 5), "4 of 5: loot");
Check.False(LootKeepCap.UnderCap(5, 5), "5 of 5: leave");
Check.False(LootKeepCap.UnderCap(9, 5), "over the cap: leave");
Check.False(LootKeepCap.UnderCap(0, 0), "Keep # 0 never loots");
Check.False(LootKeepCap.UnderCap(0, -1), "a negative count never loots");
// A .utl Keep # rule with no count line reads KeepCount null, which corpse looting turns into 0.
var p = VTankLootParser.LoadFromText("UTL\n1\n1\nKeep kits\n\n0;10;1\n");
Check.Null(p.Rules[0].KeepCount, "no count line: null");
Check.False(LootKeepCap.UnderCap(0, p.Rules[0].KeepCount ?? 0), "so it loots nothing");
// Note the cap compares what the player has BEFORE the new item: with 4 of 5, a stack of 10 is still looted.
Check.True(LootKeepCap.UnderCap(4, 5) && LootKeepCap.StackOf(new Item("Healing Kit").Int(12, 10)) == 10,
"a whole stack is taken while under the cap, even if it overshoots");
}
}