diff --git a/src/RynthCore.Engine/Compatibility/VendorRatePolicy.cs b/src/RynthCore.Engine/Compatibility/VendorRatePolicy.cs
new file mode 100644
index 0000000..c115360
--- /dev/null
+++ b/src/RynthCore.Engine/Compatibility/VendorRatePolicy.cs
@@ -0,0 +1,61 @@
+// ============================================================================
+// 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.
+//
+// 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
+{
+ /// 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;
+
+ /// 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 8b089d6..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 ,
@@ -1425,7 +1428,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..b54cab2
--- /dev/null
+++ b/tools/VendorRateTests/Program.cs
@@ -0,0 +1,97 @@
+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");
+
+ 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++;
+ 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..ee94d9b
--- /dev/null
+++ b/tools/VendorRateTests/VendorRateTests.csproj
@@ -0,0 +1,24 @@
+
+
+
+
+ Exe
+ net10.0
+ enable
+ enable
+ true
+ VendorRateTests
+
+
+
+
+
+
+