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 <noreply@anthropic.com>
This commit is contained in:
Daniel Wallace 2026-10-30 20:16:09 -07:00
commit f68d9c2199
4 changed files with 119 additions and 15 deletions

View file

@ -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
/// <summary>True when <paramref name="rate"/> is finite and in [0, MaxRate).</summary>
public static bool LooksSane(float rate) => float.IsFinite(rate) && rate >= 0f && rate < MaxRate;
/// <summary>True when both rates are exactly 0: a free vendor (it neither charges nor pays).</summary>
public static bool IsFreeVendor(float buyRate, float sellRate) => buyRate == 0f && sellRate == 0f;
public const uint ItemTypePromissoryNote = 0x00040000;
/// <summary>
/// What the player pays for one created stack worth <paramref name="stackValue"/> (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.
/// </summary>
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));
}
/// <summary>
/// What the vendor pays for one item worth <paramref name="value"/> (ACE Vendor.GetBuyCost):
/// max(1, floor(rate * value + 0.1)), promissory notes at 1.0. A vendor rate of exactly 0 pays 0.
/// </summary>
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));
}
}

View file

@ -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<uint> _zeroRateLogged = new();
private static readonly Queue<string> _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
// ════════════════════════════════════════════════════════════════════════════
/// <summary>What the player pays for one created stack worth <paramref name="stackValue"/> (ACE Vendor.GetSellCost).</summary>
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);
/// <summary>What the vendor pays for one item worth <paramref name="value"/> (ACE Vendor.GetBuyCost).</summary>
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);
/// <summary>
/// Cost / slots / burden for buying <paramref name="amount"/> of <paramref name="item"/>,

View file

@ -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++;

View file

@ -3,8 +3,10 @@
<!--
Offline checks for VendorTrade's profile rate check (2026-10-30, a free storage vendor on a
custom server, buy=0 sell=0, had trading turned off): src\RynthCore.Engine\Compatibility\VendorRatePolicy.cs,
compiled in. 0 and ordinary rates pass; negative, NaN, infinite and huge rates fail.
No AC, no files. Run: dotnet run -c Release (exit 0 = pass, 1 = fail)
compiled in. 0 and ordinary rates pass; negative, NaN, infinite and huge rates fail. A 0 rate
prices at 0 (cost and payout), any rate above 0 keeps ACE's 1-per-stack floor. Reads
src\RynthCore.Engine\Compatibility\VendorTrade.cs (found by walking up) to pin that it calls
VendorRatePolicy. No AC. Run: dotnet run -c Release (exit 0 = pass, 1 = fail)
-->
<PropertyGroup>
<OutputType>Exe</OutputType>