From 0043be8bba36bd9a9f2fee18f3acfda33a26f339 Mon Sep 17 00:00:00 2001 From: Daniel Wallace Date: Fri, 30 Oct 2026 20:09:32 -0700 Subject: [PATCH 1/2] VendorTrade: allow vendors with 0 buy/sell rates A custom server can run a free storage vendor whose BuyPrice and SellPrice are both 0: it pays nothing for items handed to it and charges nothing to take them back. The profile check required rate > 0, so opening such a vendor logged VendorTrade: vendor 0x8007810B profile rates look wrong (buy=0 sell=0); trading with it is off. and every VendorBuy/VendorSell (RynthAi AutoVendor, RynthLua vendor actions, /rc vendor) was refused with "vendor profile unreadable". The rate check now accepts finite rates in [0, 1000). Negative, NaN, infinite and huge values are still treated as a misread profile, and the warning still fires for them. The check moves to VendorRatePolicy.cs so tools\VendorRateTests can compile it in. Rate consumers checked: PlayerPaysForStack, VendorPaysForStack, EstimateBuy and UnitPrice multiply by the rate and floor at 1 per stack (the stock ACE rule), so a 0 rate gives a 1-per-stack estimate; there is no division by a rate anywhere. FillInfo and the /rc vendor info line only copy or print the rates. Co-Authored-By: Claude Opus 5.5 --- .../Compatibility/VendorRatePolicy.cs | 23 ++++++++++++ .../Compatibility/VendorTrade.cs | 3 +- tools/VendorRateTests/Program.cs | 36 +++++++++++++++++++ tools/VendorRateTests/VendorRateTests.csproj | 22 ++++++++++++ 4 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 src/RynthCore.Engine/Compatibility/VendorRatePolicy.cs create mode 100644 tools/VendorRateTests/Program.cs create mode 100644 tools/VendorRateTests/VendorRateTests.csproj diff --git a/src/RynthCore.Engine/Compatibility/VendorRatePolicy.cs b/src/RynthCore.Engine/Compatibility/VendorRatePolicy.cs new file mode 100644 index 0000000..dd5ba49 --- /dev/null +++ b/src/RynthCore.Engine/Compatibility/VendorRatePolicy.cs @@ -0,0 +1,23 @@ +// ============================================================================ +// RynthCore.Engine - Compatibility/VendorRatePolicy.cs +// The pure part of VendorTrade's profile check: whether a buy/sell rate read +// from the client's vendor profile is a real rate or a misread. Compiled into +// tools\VendorRateTests. +// +// A rate of exactly 0 is a real rate. A custom server can run a free storage +// vendor (a "vault") whose BuyPrice and SellPrice are both 0; it pays nothing +// for what you hand it and charges nothing for what you take back. Refusing +// 0 turned trading off for such vendors. Negative, NaN, infinite and huge +// values still mean the profile was misread, and trading stays off for them. +// ============================================================================ + +namespace RynthCore.Engine.Compatibility; + +internal static class VendorRatePolicy +{ + /// Rates at or above this are taken as a misread profile. + public const float MaxRate = 1000f; + + /// True when is finite and in [0, MaxRate). + public static bool LooksSane(float rate) => float.IsFinite(rate) && rate >= 0f && rate < MaxRate; +} diff --git a/src/RynthCore.Engine/Compatibility/VendorTrade.cs b/src/RynthCore.Engine/Compatibility/VendorTrade.cs index 8b089d6..0058165 100644 --- a/src/RynthCore.Engine/Compatibility/VendorTrade.cs +++ b/src/RynthCore.Engine/Compatibility/VendorTrade.cs @@ -1425,7 +1425,8 @@ internal static class VendorTrade return sb.ToString(); } - private static bool RateLooksSane(float rate) => float.IsFinite(rate) && rate > 0f && rate < 1000f; + // 0 is a real rate (a free storage vendor); see VendorRatePolicy. + private static bool RateLooksSane(float rate) => VendorRatePolicy.LooksSane(rate); private static bool InModule(IntPtr p) { diff --git a/tools/VendorRateTests/Program.cs b/tools/VendorRateTests/Program.cs new file mode 100644 index 0000000..1f023a5 --- /dev/null +++ b/tools/VendorRateTests/Program.cs @@ -0,0 +1,36 @@ +using RynthCore.Engine.Compatibility; + +namespace VendorRateTests; + +/// VendorTrade's profile rate check (VendorRatePolicy). See the csproj. +internal static class Program +{ + private static int _checks, _failed; + + private static int Main() + { + // A free storage vendor: BuyPrice 0, SellPrice 0. This is the case that used to turn trading off. + Check(VendorRatePolicy.LooksSane(0f), "rate 0 (free storage vendor)"); + Check(VendorRatePolicy.LooksSane(-0f), "rate -0 (compares equal to 0)"); + + // Ordinary vendors and promissory-note rates. + foreach (float r in new[] { 0.001f, 0.1f, 0.5f, 0.75f, 1f, 1.15f, 2.5f, 10f, 999.9f }) + Check(VendorRatePolicy.LooksSane(r), $"rate {r}"); + + // Misreads stay rejected. + foreach (float r in new[] { -0.001f, -1f, float.NaN, float.PositiveInfinity, float.NegativeInfinity, + VendorRatePolicy.MaxRate, 1e6f, float.MaxValue, float.MinValue }) + Check(!VendorRatePolicy.LooksSane(r), $"rate {r} rejected"); + + Console.WriteLine(_failed == 0 ? $"PASS ({_checks} checks)" : $"FAIL ({_failed} of {_checks} checks failed)"); + return _failed == 0 ? 0 : 1; + } + + private static void Check(bool ok, string what) + { + _checks++; + if (ok) { Console.WriteLine("ok " + what); return; } + _failed++; + Console.WriteLine("FAIL " + what); + } +} diff --git a/tools/VendorRateTests/VendorRateTests.csproj b/tools/VendorRateTests/VendorRateTests.csproj new file mode 100644 index 0000000..6cfaada --- /dev/null +++ b/tools/VendorRateTests/VendorRateTests.csproj @@ -0,0 +1,22 @@ + + + + + Exe + net10.0 + enable + enable + true + VendorRateTests + + + + + + + -- 2.43.0 From f68d9c21993138df39d95d942936f69b07169811 Mon Sep 17 00:00:00 2001 From: Daniel Wallace Date: Fri, 30 Oct 2026 20:16:09 -0700 Subject: [PATCH 2/2] VendorTrade: price a 0-rate vendor at 0, log free vendors, pin wiring Review follow-up to the 0-rate fix. - A vendor rate of exactly 0 now means free: PlayerPaysForStack and VendorPaysForStack return 0 instead of the 1-per-stack floor (notes included). Every rate above 0 keeps the stock ACE floor. With the floor, a free storage vendor refused withdrawals to a player with no pyreals ("costs N pyreals, you have 0") and deposits with no free pack slot (the estimated "payout" needed one), though the vault charges and pays nothing. The server stays the authority on the real price. - The two price helpers move to VendorRatePolicy.cs; VendorTrade keeps thin forwarders so its call sites are unchanged. - A vendor with buy=0 and sell=0 is logged once per vendor id as a zero-rate (free) vendor. - tools\VendorRateTests covers the pricing (rate 0 -> cost 0 and payout 0; rate 0.001, value 1 -> still 1) and reads VendorTrade.cs to pin that it calls VendorRatePolicy, so reverting the wiring fails the test. Co-Authored-By: Claude Opus 5.5 --- .../Compatibility/VendorRatePolicy.cs | 38 ++++++++++++ .../Compatibility/VendorTrade.cs | 29 +++++---- tools/VendorRateTests/Program.cs | 61 +++++++++++++++++++ tools/VendorRateTests/VendorRateTests.csproj | 6 +- 4 files changed, 119 insertions(+), 15 deletions(-) diff --git a/src/RynthCore.Engine/Compatibility/VendorRatePolicy.cs b/src/RynthCore.Engine/Compatibility/VendorRatePolicy.cs index dd5ba49..c115360 100644 --- a/src/RynthCore.Engine/Compatibility/VendorRatePolicy.cs +++ b/src/RynthCore.Engine/Compatibility/VendorRatePolicy.cs @@ -9,8 +9,14 @@ // for what you hand it and charges nothing for what you take back. Refusing // 0 turned trading off for such vendors. Negative, NaN, infinite and huge // values still mean the profile was misread, and trading stays off for them. +// +// The price helpers live here too so the test can pin them: a 0 rate prices +// at 0 (the vault's own GetSellCost/GetBuyCost return 0), while any rate +// above 0 keeps ACE's 1-per-stack floor. The server stays the authority. // ============================================================================ +using System; + namespace RynthCore.Engine.Compatibility; internal static class VendorRatePolicy @@ -20,4 +26,36 @@ internal static class VendorRatePolicy /// True when is finite and in [0, MaxRate). public static bool LooksSane(float rate) => float.IsFinite(rate) && rate >= 0f && rate < MaxRate; + + /// True when both rates are exactly 0: a free vendor (it neither charges nor pays). + public static bool IsFreeVendor(float buyRate, float sellRate) => buyRate == 0f && sellRate == 0f; + + public const uint ItemTypePromissoryNote = 0x00040000; + + /// + /// What the player pays for one created stack worth (ACE Vendor.GetSellCost): + /// max(1, ceil(rate * value - 0.1)), promissory notes at 1.15. A vendor rate of exactly 0 is a free + /// vendor and costs 0 (no 1-per-stack floor); every rate above 0 keeps the stock ACE floor. + /// + public static long PlayerPaysForStack(float sellRate, uint itemType, long stackValue) + { + if (sellRate == 0f) + return 0; + float rate = itemType == ItemTypePromissoryNote ? 1.15f : sellRate; + float product = rate * stackValue; + return Math.Max(1L, (long)Math.Ceiling(product - 0.1)); + } + + /// + /// What the vendor pays for one item worth (ACE Vendor.GetBuyCost): + /// max(1, floor(rate * value + 0.1)), promissory notes at 1.0. A vendor rate of exactly 0 pays 0. + /// + public static long VendorPaysForStack(float buyRate, uint itemType, long value) + { + if (buyRate == 0f) + return 0; + float rate = itemType == ItemTypePromissoryNote ? 1.0f : buyRate; + float product = rate * value; + return Math.Max(1L, (long)Math.Floor(product + 0.1)); + } } diff --git a/src/RynthCore.Engine/Compatibility/VendorTrade.cs b/src/RynthCore.Engine/Compatibility/VendorTrade.cs index 0058165..9af8300 100644 --- a/src/RynthCore.Engine/Compatibility/VendorTrade.cs +++ b/src/RynthCore.Engine/Compatibility/VendorTrade.cs @@ -168,7 +168,7 @@ internal static class VendorTrade private const int PwdItemsCapacity = 48, PwdContainersCapacity = 52, PwdType = 56, PwdValue = 60; private const int PwdStackSize = 96, PwdMaxStackSize = 100, PwdBitfield = 104, PwdBurden = 116; - private const uint ItemTypePromissoryNote = 0x00040000; + private const uint ItemTypePromissoryNote = VendorRatePolicy.ItemTypePromissoryNote; private const int PyrealMaxStack = 25000; // coinstack MaxStackSize (payout slots) private const int MaxVendorItems = 1024; private const int MaxTradeEntries = 100; @@ -232,6 +232,8 @@ internal static class VendorTrade // Chat echo for /rc vendor (drained on the main thread; written from the drain, // never from inside the outgoing-chat detour). private static readonly object _echoLock = new(); + // Zero-rate (free) vendors already logged, so each vendor id is logged once per engine. + private static readonly HashSet _zeroRateLogged = new(); private static readonly Queue _echo = new(); private static volatile int _echoCount; private const int MaxEcho = 48; @@ -316,6 +318,13 @@ internal static class VendorTrade snap.ProfileRead = RateLooksSane(snap.BuyRate) && RateLooksSane(snap.SellRate); if (!snap.ProfileRead) RynthLog.Warn($"VendorTrade: vendor 0x{vendorId:X8} profile rates look wrong (buy={snap.BuyRate} sell={snap.SellRate}); trading with it is off."); + else if (VendorRatePolicy.IsFreeVendor(snap.BuyRate, snap.SellRate)) + { + bool first; + lock (_zeroRateLogged) first = _zeroRateLogged.Add(vendorId); + if (first) + RynthLog.Compat($"VendorTrade: vendor 0x{vendorId:X8} has buy=0 sell=0: a zero-rate (free) vendor; trades are priced at 0."); + } } if (list != IntPtr.Zero && Readable(list, ListSize)) @@ -962,20 +971,14 @@ internal static class VendorTrade // ════════════════════════════════════════════════════════════════════════════ /// What the player pays for one created stack worth (ACE Vendor.GetSellCost). - internal static long PlayerPaysForStack(float sellRate, uint itemType, long stackValue) - { - float rate = itemType == ItemTypePromissoryNote ? 1.15f : sellRate; - float product = rate * stackValue; - return Math.Max(1L, (long)Math.Ceiling(product - 0.1)); - } + // A 0 rate (free vendor) costs 0; see VendorRatePolicy. + internal static long PlayerPaysForStack(float sellRate, uint itemType, long stackValue) => + VendorRatePolicy.PlayerPaysForStack(sellRate, itemType, stackValue); /// What the vendor pays for one item worth (ACE Vendor.GetBuyCost). - internal static long VendorPaysForStack(float buyRate, uint itemType, long value) - { - float rate = itemType == ItemTypePromissoryNote ? 1.0f : buyRate; - float product = rate * value; - return Math.Max(1L, (long)Math.Floor(product + 0.1)); - } + // A 0 rate (free vendor) pays 0; see VendorRatePolicy. + internal static long VendorPaysForStack(float buyRate, uint itemType, long value) => + VendorRatePolicy.VendorPaysForStack(buyRate, itemType, value); /// /// Cost / slots / burden for buying of , diff --git a/tools/VendorRateTests/Program.cs b/tools/VendorRateTests/Program.cs index 1f023a5..b54cab2 100644 --- a/tools/VendorRateTests/Program.cs +++ b/tools/VendorRateTests/Program.cs @@ -22,10 +22,71 @@ internal static class Program VendorRatePolicy.MaxRate, 1e6f, float.MaxValue, float.MinValue }) Check(!VendorRatePolicy.LooksSane(r), $"rate {r} rejected"); + Pricing(); + WiringPinned(); + Console.WriteLine(_failed == 0 ? $"PASS ({_checks} checks)" : $"FAIL ({_failed} of {_checks} checks failed)"); return _failed == 0 ? 0 : 1; } + private static void Eq(long got, long want, string what) => Check(got == want, $"{what}: got {got}, want {want}"); + + private const uint Note = VendorRatePolicy.ItemTypePromissoryNote; + private const uint Misc = 0x00000080; + + // A 0 rate is free (the vault's GetSellCost/GetBuyCost return 0); any rate above 0 keeps ACE's 1 floor. + private static void Pricing() + { + Check(VendorRatePolicy.IsFreeVendor(0f, 0f), "buy=0 sell=0 is a free vendor"); + Check(!VendorRatePolicy.IsFreeVendor(0f, 1.15f), "buy=0 sell=1.15 is not a free vendor"); + Check(!VendorRatePolicy.IsFreeVendor(0.5f, 0f), "buy=0.5 sell=0 is not a free vendor"); + + Eq(VendorRatePolicy.PlayerPaysForStack(0f, Misc, 0), 0, "rate 0, value 0: cost"); + Eq(VendorRatePolicy.PlayerPaysForStack(0f, Misc, 1), 0, "rate 0, value 1: cost"); + Eq(VendorRatePolicy.PlayerPaysForStack(0f, Misc, 250_000), 0, "rate 0, value 250000: cost"); + Eq(VendorRatePolicy.PlayerPaysForStack(0f, Note, 250_000), 0, "rate 0, a note: cost"); + Eq(VendorRatePolicy.VendorPaysForStack(0f, Misc, 1), 0, "rate 0, value 1: payout"); + Eq(VendorRatePolicy.VendorPaysForStack(0f, Misc, 250_000), 0, "rate 0, value 250000: payout"); + Eq(VendorRatePolicy.VendorPaysForStack(0f, Note, 250_000), 0, "rate 0, a note: payout"); + + // The floor stays for every rate above 0 (stock ACE). + Eq(VendorRatePolicy.PlayerPaysForStack(0.001f, Misc, 1), 1, "rate 0.001, value 1: cost floors at 1"); + Eq(VendorRatePolicy.VendorPaysForStack(0.001f, Misc, 1), 1, "rate 0.001, value 1: payout floors at 1"); + Eq(VendorRatePolicy.PlayerPaysForStack(0.5f, Misc, 0), 1, "rate 0.5, value 0: cost floors at 1"); + Eq(VendorRatePolicy.PlayerPaysForStack(1.15f, Misc, 100), 115, "rate 1.15, value 100: cost"); + Eq(VendorRatePolicy.VendorPaysForStack(0.75f, Misc, 100), 75, "rate 0.75, value 100: payout"); + Eq(VendorRatePolicy.PlayerPaysForStack(2f, Note, 100), 115, "a note costs 1.15x whatever the rate"); + Eq(VendorRatePolicy.VendorPaysForStack(0.5f, Note, 100), 100, "a note pays 1.0x whatever the rate"); + } + + // VendorTrade.cs must route through VendorRatePolicy, or the checks above test code the engine doesn't run. + private static void WiringPinned() + { + string? src = FindUp(Path.Combine("src", "RynthCore.Engine", "Compatibility", "VendorTrade.cs")); + Check(src != null, "found src\\RynthCore.Engine\\Compatibility\\VendorTrade.cs walking up from " + AppContext.BaseDirectory); + if (src == null) return; + string text = File.ReadAllText(src); + foreach (string needle in new[] + { + "snap.ProfileRead = RateLooksSane(snap.BuyRate) && RateLooksSane(snap.SellRate);", + "RateLooksSane(float rate) => VendorRatePolicy.LooksSane(rate);", + "VendorRatePolicy.PlayerPaysForStack(sellRate, itemType, stackValue);", + "VendorRatePolicy.VendorPaysForStack(buyRate, itemType, value);", + "VendorRatePolicy.IsFreeVendor(snap.BuyRate, snap.SellRate)", + }) + Check(text.Contains(needle, StringComparison.Ordinal), "VendorTrade.cs contains: " + needle); + } + + private static string? FindUp(string relative) + { + for (DirectoryInfo? d = new(AppContext.BaseDirectory); d != null; d = d.Parent) + { + string p = Path.Combine(d.FullName, relative); + if (File.Exists(p)) return p; + } + return null; + } + private static void Check(bool ok, string what) { _checks++; diff --git a/tools/VendorRateTests/VendorRateTests.csproj b/tools/VendorRateTests/VendorRateTests.csproj index 6cfaada..ee94d9b 100644 --- a/tools/VendorRateTests/VendorRateTests.csproj +++ b/tools/VendorRateTests/VendorRateTests.csproj @@ -3,8 +3,10 @@ Exe -- 2.43.0