VendorTrade: allow vendors with 0 buy/sell rates #10
4 changed files with 200 additions and 14 deletions
61
src/RynthCore.Engine/Compatibility/VendorRatePolicy.cs
Normal file
61
src/RynthCore.Engine/Compatibility/VendorRatePolicy.cs
Normal file
|
|
@ -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
|
||||
{
|
||||
/// <summary>Rates at or above this are taken as a misread profile.</summary>
|
||||
public const float MaxRate = 1000f;
|
||||
|
||||
/// <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));
|
||||
}
|
||||
}
|
||||
|
|
@ -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"/>,
|
||||
|
|
@ -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)
|
||||
{
|
||||
|
|
|
|||
97
tools/VendorRateTests/Program.cs
Normal file
97
tools/VendorRateTests/Program.cs
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
using RynthCore.Engine.Compatibility;
|
||||
|
||||
namespace VendorRateTests;
|
||||
|
||||
/// <summary>VendorTrade's profile rate check (VendorRatePolicy). See the csproj.</summary>
|
||||
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);
|
||||
}
|
||||
}
|
||||
24
tools/VendorRateTests/VendorRateTests.csproj
Normal file
24
tools/VendorRateTests/VendorRateTests.csproj
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<!--
|
||||
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. 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>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<InvariantGlobalization>true</InvariantGlobalization>
|
||||
<RootNamespace>VendorRateTests</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="..\..\src\RynthCore.Engine\Compatibility\VendorRatePolicy.cs" Link="Engine\VendorRatePolicy.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Loading…
Reference in a new issue