diff --git a/Plugins/RynthCore.Plugin.RynthAi/Vendor/AutoVendorManager.cs b/Plugins/RynthCore.Plugin.RynthAi/Vendor/AutoVendorManager.cs index b9dd984..9123049 100644 --- a/Plugins/RynthCore.Plugin.RynthAi/Vendor/AutoVendorManager.cs +++ b/Plugins/RynthCore.Plugin.RynthAi/Vendor/AutoVendorManager.cs @@ -1369,10 +1369,11 @@ internal sealed class AutoVendorManager double bestDist = double.MaxValue; foreach (var wo in _cache.GetLandscapeObjects()) { - if (wo.ObjectClass != AcObjectClass.Vendor || wo.Id == player) continue; + if (wo.Id == player) continue; string n = (wo.Name ?? string.Empty).ToLowerInvariant(); bool match = lower.Length == 0 || (partial ? n.Contains(lower) : n == lower); if (!match) continue; + if (!IsVendorObject(wo)) continue; double d = _cache.Distance(player, wo.Id); if (best == null || d < bestDist) { best = wo; bestDist = d; } } @@ -1382,7 +1383,24 @@ internal sealed class AutoVendorManager private bool IsVendor(uint id, out WorldObject? wo) { wo = id != 0 ? _cache[unchecked((int)id)] : null; - return wo != null && wo.ObjectClass == AcObjectClass.Vendor; + return wo != null && IsVendorObject(wo); + } + + // PublicWeenieDesc._bitfield BF_VENDOR (ObjectDescriptionFlag.Vendor). + private const uint BfVendor = 0x200; + + // A vendor by cache class, or by the server's vendor flag: a server vendor the cache classifies + // otherwise (e.g. a server-summoned vault NPC) is still a vendor to open. + private bool IsVendorObject(WorldObject wo) + { + if (wo.ObjectClass == AcObjectClass.Vendor) return true; + try + { + return _host.HasGetObjectBitfield + && _host.TryGetObjectBitfield(unchecked((uint)wo.Id), out uint bf) + && (bf & BfVendor) != 0; + } + catch { return false; } } // ── /ub vendor addsell ── diff --git a/Tools/RynthCore.RynthAiHostTests/Program.cs b/Tools/RynthCore.RynthAiHostTests/Program.cs index 589dd0f..b5a70ee 100644 --- a/Tools/RynthCore.RynthAiHostTests/Program.cs +++ b/Tools/RynthCore.RynthAiHostTests/Program.cs @@ -132,6 +132,7 @@ internal static class Program ServerPluginForwardTests.Register(runner); IgnoreMonsterTests.Register(runner); WorldCacheTypedTests.Register(runner); + VendorFlagTests.Register(runner); NavOverlayTests.Register(runner); ActionPacingTests.Register(runner); HazardEscapeTests.Register(runner); diff --git a/Tools/RynthCore.RynthAiHostTests/Tests/VendorFlagTests.cs b/Tools/RynthCore.RynthAiHostTests/Tests/VendorFlagTests.cs new file mode 100644 index 0000000..1c228d5 --- /dev/null +++ b/Tools/RynthCore.RynthAiHostTests/Tests/VendorFlagTests.cs @@ -0,0 +1,98 @@ +using System; +using System.Linq; +using RynthCore.Loot; +using RynthCore.Plugin.RynthAi; +using RynthCore.Plugin.RynthAi.LegacyUi; +using RynthCore.Plugin.RynthAi.Vendor; +using RynthCore.RynthAiHostTests.Fakes; +using RynthCore.RynthAiTests.Harness; + +namespace RynthCore.RynthAiHostTests.Tests; + +// /ub vendor open finds an object the server flags as a vendor (PublicWeenieDesc BF_VENDOR 0x200) +// even when the world cache filed it as something else (a server-summoned vault NPC with 0/0 rates +// reaches the cache as a typed item). Without the flag it is still not a vendor. +internal static class VendorFlagTests +{ + private const uint Player = 0x50000E01; + private const uint Here = 0xA9B4001C; + private const uint Flagged = 0x80310001; + private const uint Plain = 0x80310002; + private const uint BfVendor = 0x200; + private const uint TGem = 0x800; + private const string Attempt = "AutoVendor: attempting to open vendor"; + + public static void Register(Runner r) + { + r.Add("vendor open: a cache non-Vendor with the vendor bitfield is found by name", FoundByName); + r.Add("vendor open: a cache non-Vendor with the vendor bitfield is found by id", FoundById); + r.Add("vendor open: a cache non-Vendor without the vendor bitfield is not found", NotFlagged); + r.Add("vendor open: a partial name finds the flagged object", FoundByPartial); + } + + private static AutoVendorManager Rig() + { + FakeHost.Reset(); + FakeHost.Names[Player] = "Vendor Tester"; + FakeHost.Positions[Player] = (Here, 96f, 96f, 0f); + FakeHost.PlayerPose = (Here, 96f, 96f, 0f); + FakeHost.Containers[Player] = 0; + foreach (var (id, name) in new[] { (Flagged, "Vault Keeper"), (Plain, "Plain Gem") }) + { + FakeHost.Names[id] = name; + FakeHost.Positions[id] = (Here, 97f, 97f, 0f); + FakeHost.ItemTypes[id] = TGem; + FakeHost.Containers[id] = 0; + FakeHost.NotAttackable.Add(id); + } + FakeHost.Bitfields[Flagged] = BfVendor; + + var host = FakeHost.Create(Player); + var cache = new WorldObjectCache(host); + cache.SetPlayerId(Player); + cache.OnCreateObject(Flagged); + cache.OnCreateObject(Plain); + for (int i = 0; i < 12; i++) cache.Tick(); + // Precondition: the cache really does file both as a non-Vendor. + foreach (uint id in new[] { Flagged, Plain }) + Check.True(cache.AllKnownObjects().Any(w => w.Id == unchecked((int)id) && w.ObjectClass != AcObjectClass.Vendor), + "precondition: cached as a non-Vendor"); + return new AutoVendorManager(host, new LegacyUiSettings(), cache, Player, () => string.Empty); + } + + private static bool Attempted() => FakeHost.Logs.Any(l => l.Contains(Attempt, StringComparison.Ordinal)); + + // The open attempt names the chosen object: "...attempting to open vendor ". + private static bool Opened(string name) => FakeHost.Logs.Any(l => l.EndsWith(Attempt + " " + name, StringComparison.Ordinal)); + + private static void FoundByName() + { + var m = Rig(); + m.CommandOpen("Vault Keeper", false); + Check.True(Opened("Vault Keeper"), "the flagged object is the one opened by name"); + } + + private static void FoundById() + { + var m = Rig(); + m.CommandOpen("0x" + Flagged.ToString("X8"), false); + Check.True(Opened("Vault Keeper"), "the flagged object is the one opened by hex id"); + } + + private static void NotFlagged() + { + var m = Rig(); + m.CommandOpen("Plain Gem", false); + Check.False(Attempted(), "an unflagged object is not opened by name"); + m.CommandOpen("0x" + Plain.ToString("X8"), false); + Check.False(Attempted(), "an unflagged object is not opened by id"); + } + + private static void FoundByPartial() + { + var m = Rig(); + m.CommandOpen("keeper", true); + Check.True(Opened("Vault Keeper"), "a partial name opens the flagged object"); + } + +}