From 0043be8bba36bd9a9f2fee18f3acfda33a26f339 Mon Sep 17 00:00:00 2001 From: Daniel Wallace Date: Fri, 30 Oct 2026 20:09:32 -0700 Subject: [PATCH] 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 + + + + + + +