diff --git a/src/RynthCore.Engine/Compatibility/AutoIdPolicy.cs b/src/RynthCore.Engine/Compatibility/AutoIdPolicy.cs index 3b7b3f7..939f805 100644 --- a/src/RynthCore.Engine/Compatibility/AutoIdPolicy.cs +++ b/src/RynthCore.Engine/Compatibility/AutoIdPolicy.cs @@ -22,9 +22,12 @@ // or in a side pack) that have no cached appraisal. // Container and Inventory wait until the object is MinAgeMs old, and until // DeferMs after a world transition's settle window ends (death, portal, -// teleport, landblock jump; walking into the next landblock is not one). -// A container the player opened after that settle window ended is not -// deferred. Creatures, ground items, other players: never. +// teleport, landblock jump; walking into the next landblock is not one), +// and until DeferMs after login completes (login is a transition too: +// a client crashed in AC's CreateObject handling during the +// login create burst). A container the player opened after the last +// transition ended is not deferred. Inventory sends are capped at 5 a +// second. Creatures, ground items, other players: never. // Off nothing, the selected target included. // Plugins calling the host RequestId are not limited by any of this. // @@ -46,9 +49,17 @@ internal static class AutoIdPolicy /// Container and Inventory appraisals wait until the object is this old. public const long MinAgeMs = 2_000; - /// Container and Inventory appraisals wait this long after a transition's settle window ends. + /// Container and Inventory appraisals wait this long after a transition's settle window ends, + /// and after login completes. public const long DeferMs = 30_000; + /// + /// Inventory appraisals go out at most one per this many ms (5 a second). In one case + /// a client crashed in AC's CreateObject handling during the login create burst, 47 s after + /// start, and the pack's appraisals at 30 a second are what that burst must not carry. + /// + public const long InventoryMinIntervalMs = 200; + public enum Class { /// The selected target (the priority path). @@ -75,6 +86,8 @@ internal static class AutoIdPolicy WaitDefer, /// Its appraisal is cached (fresh): drop it. SkipFresh, + /// Inventory over its send rate (InventoryMinIntervalMs): keep it and ask again later. + WaitRate, } /// @@ -85,9 +98,10 @@ internal static class AutoIdPolicy /// settle window ended (), which exempts it from the defer. /// : its appraisal is cached and recent (Target, Container). /// : its appraisal is cached at all (Inventory). + /// : Inventory only, the send rate allows one now (). /// public static Verdict Decide(AutoIdMode mode, Class cls, long ageMs, bool inDefer, bool openedAfterSettle, - bool fresh, bool cached) + bool fresh, bool cached, bool inventoryRateOk = true) { switch (mode) { @@ -114,6 +128,7 @@ internal static class AutoIdPolicy if (cached) return Verdict.SkipFresh; if (ageMs < MinAgeMs) return Verdict.WaitAge; if (inDefer) return Verdict.WaitDefer; + if (!inventoryRateOk) return Verdict.WaitRate; return Verdict.Send; case Class.Unknown: // Its place shows up in the snapshot within a tick or two; until it is @@ -126,17 +141,30 @@ internal static class AutoIdPolicy } /// - /// Inside the post-transition defer: in a transition now, or less than - /// after the last settle window ended. is - /// WorldTransitionState.SettleUntilMs (0 = no transition yet); it only moves on a death, - /// a portal, a teleport or a landblock jump, never on a walk into the next landblock. + /// When the last transition ended: the later of the settle window's end + /// (WorldTransitionState.SettleUntilMs, 0 = none yet; it only moves on a death, a portal, + /// a teleport or a landblock jump, never on a walk into the next landblock) and login + /// completing (; login counts as a transition). /// - public static bool InDefer(long nowMs, long settleUntilMs, bool inTransition) => - inTransition || (settleUntilMs > 0 && nowMs < settleUntilMs + DeferMs); + public static long TransitionEndMs(long settleUntilMs, long loginCompleteAtMs) => + Math.Max(settleUntilMs, loginCompleteAtMs); - /// The player opened the container (at ) after the last settle window ended. - public static bool OpenedAfterSettle(long openedAtMs, long settleUntilMs) => - openedAtMs > 0 && openedAtMs >= settleUntilMs; + /// + /// Inside the defer: in a transition now, login not complete yet + /// ( 0: the login create burst), or less than + /// after . + /// + public static bool InDefer(long nowMs, long settleUntilMs, long loginCompleteAtMs, bool inTransition) => + inTransition || loginCompleteAtMs <= 0 || nowMs < TransitionEndMs(settleUntilMs, loginCompleteAtMs) + DeferMs; + + /// The player opened the container (at ) after the last transition ended + /// (, ). + public static bool OpenedAfterSettle(long openedAtMs, long transitionEndMs) => + openedAtMs > 0 && openedAtMs >= transitionEndMs; + + /// An Inventory appraisal may go out now: none yet, or the last was at least InventoryMinIntervalMs ago. + public static bool InventoryRateAllows(long nowMs, long lastInventorySendMs) => + lastInventorySendMs <= 0 || nowMs - lastInventorySendMs >= InventoryMinIntervalMs; /// /// Where an object is, from its PublicWeenieDesc ownership fields ( = diff --git a/src/RynthCore.Engine/Compatibility/AutoIdService.cs b/src/RynthCore.Engine/Compatibility/AutoIdService.cs index 2bc24b5..f8267e4 100644 --- a/src/RynthCore.Engine/Compatibility/AutoIdService.cs +++ b/src/RynthCore.Engine/Compatibility/AutoIdService.cs @@ -17,8 +17,8 @@ // - AutoIdPolicy (on drain): the AutoIdMode (engine.json "AutoIdMode", /rc autoid). // Limited (the default) appraises only the selected target, the // items in the container the player has open, and items in the player's own -// inventory with nothing cached; the last two wait for a minimum object age and a -// post-transition defer. All is the old behaviour; Off sends nothing. +// inventory with nothing cached (at most 5 a second); the last two wait for a +// minimum object age and a defer after login and after every world transition. All is the old behaviour; Off sends nothing. // // Every send is queued with who asked (Net/IdRequester.cs), so the flight recorder tags // it [eng:autoid-target|autoid-container|autoid-inventory|autoid-world]. @@ -69,7 +69,7 @@ internal static class AutoIdService { public uint Id; public long AtMs; // created (or moved, or its container opened) at - public byte Counted; // waits already counted: 1 = age, 2 = defer + public byte Counted; // waits already counted: 1 = age, 2 = defer, 4 = rate } private static readonly List _pending = new(256); private static readonly HashSet _pendingIds = new(); @@ -87,7 +87,8 @@ internal static class AutoIdService private static long _rescanDueMs; private static long _skippedPriorityFresh; - private static long _skipMode, _skipAge, _skipDefer, _skipFresh, _overflow; + private static long _skipMode, _skipAge, _skipDefer, _skipFresh, _skipRate, _overflow; + private static long _lastInventorySendMs; // drain timer only: the Inventory send cap /// Re-creates not appraised because a recent appraisal is still cached. public static long SkippedRecentCount @@ -364,8 +365,9 @@ internal static class AutoIdService long openedAt = open != 0 && unchecked((uint)Volatile.Read(ref _openedContainerId)) == open ? Interlocked.Read(ref _openedAtMs) : 0; long settleUntil = PlayerLifecycleLog.SettleUntilMs; - bool inDefer = AutoIdPolicy.InDefer(now, settleUntil, inTransition: false); - bool openedAfterSettle = AutoIdPolicy.OpenedAfterSettle(openedAt, settleUntil); + long loginAt = LoginLifecycleHooks.LoginCompleteAtMs; + bool inDefer = AutoIdPolicy.InDefer(now, settleUntil, loginAt, inTransition: false); + bool openedAfterSettle = AutoIdPolicy.OpenedAfterSettle(openedAt, AutoIdPolicy.TransitionEndMs(settleUntil, loginAt)); int keep = 0, evaluated = 0; for (int i = 0; i < _pending.Count; i++) @@ -395,7 +397,10 @@ internal static class AutoIdService bool cached = AppraisalHooks.HasAppraisalData(p.Id); bool fresh = cached && AutoIdEligibility.IsAppraisalFresh( cached, AppraisalHooks.GetLastIdTime(p.Id), nowUnixSec, ReappraiseWindowMs); - AutoIdPolicy.Verdict v = AutoIdPolicy.Decide(mode, cls, now - p.AtMs, inDefer, openedAfterSettle, fresh, cached); + bool rateOk = AutoIdPolicy.InventoryRateAllows(now, _lastInventorySendMs); + AutoIdPolicy.Verdict v = AutoIdPolicy.Decide(mode, cls, now - p.AtMs, inDefer, openedAfterSettle, fresh, cached, rateOk); + if (v == AutoIdPolicy.Verdict.Send && cls == AutoIdPolicy.Class.Inventory && mode == AutoIdMode.Limited) + _lastInventorySendMs = now; switch (v) { case AutoIdPolicy.Verdict.Send: @@ -416,6 +421,10 @@ internal static class AutoIdService if ((p.Counted & 2) == 0) { p.Counted |= 2; Interlocked.Increment(ref _skipDefer); } _pending[keep++] = p; break; + case AutoIdPolicy.Verdict.WaitRate: + if ((p.Counted & 4) == 0) { p.Counted |= 4; Interlocked.Increment(ref _skipRate); } + _pending[keep++] = p; + break; case AutoIdPolicy.Verdict.SkipFresh: Interlocked.Increment(ref _skipFresh); _pendingIds.Remove(p.Id); @@ -459,7 +468,7 @@ internal static class AutoIdService IdRequester.Plugin, IdRequester.Self, IdRequester.Ui, IdRequester.None, }; private static readonly long[] _lastSent = new long[IdRequesters.Count]; - private static long _lastForeign, _lastSkipMode, _lastSkipAge, _lastSkipDefer, _lastSkipFresh; + private static long _lastForeign, _lastSkipMode, _lastSkipAge, _lastSkipDefer, _lastSkipFresh, _lastSkipRate; /// /// The last minute's IdentifyObject counts (heartbeat thread, once a minute): sent per @@ -471,7 +480,7 @@ internal static class AutoIdService { long foreign = FlightRecorder.ForeignIdentifyTotal; long sm = Interlocked.Read(ref _skipMode), sa = Interlocked.Read(ref _skipAge); - long sd = Interlocked.Read(ref _skipDefer), sf = Interlocked.Read(ref _skipFresh); + long sd = Interlocked.Read(ref _skipDefer), sf = Interlocked.Read(ref _skipFresh), sr = Interlocked.Read(ref _skipRate); var sb = new System.Text.StringBuilder(160); sb.Append("autoid 60s: mode=").Append(AutoIdPolicy.Name(Mode)).Append(" sent"); long total = 0; @@ -497,9 +506,9 @@ internal static class AutoIdService } sb.Append(" tot=").Append(total); sb.Append(" skip mode=").Append(sm - _lastSkipMode).Append(" age=").Append(sa - _lastSkipAge) - .Append(" defer=").Append(sd - _lastSkipDefer).Append(" fresh=").Append(sf - _lastSkipFresh); + .Append(" defer=").Append(sd - _lastSkipDefer).Append(" fresh=").Append(sf - _lastSkipFresh).Append(" rate=").Append(sr - _lastSkipRate); sb.Append(" pend=").Append(Volatile.Read(ref _pendingCount)); - _lastSkipMode = sm; _lastSkipAge = sa; _lastSkipDefer = sd; _lastSkipFresh = sf; + _lastSkipMode = sm; _lastSkipAge = sa; _lastSkipDefer = sd; _lastSkipFresh = sf; _lastSkipRate = sr; return sb.ToString(); } @@ -530,14 +539,14 @@ internal static class AutoIdService { AutoIdMode.All => "all (every created object, the old behaviour)", AutoIdMode.Off => "off (no background appraisals, not even the selected target)", - _ => $"limited (selected target; open container and own inventory after {AutoIdPolicy.MinAgeMs / 1000} s, {AutoIdPolicy.DeferMs / 1000} s after a portal/death/teleport)", + _ => $"limited (selected target; open container and own inventory after {AutoIdPolicy.MinAgeMs / 1000} s, {AutoIdPolicy.DeferMs / 1000} s after login or a portal/death/teleport; inventory at most {1000 / AutoIdPolicy.InventoryMinIntervalMs}/s)", }; return new[] { $"AutoId: {modeText}. Saved in engine.json; /rc autoid all|limited|off switches it now. Plugins' own identifies are never limited.", $"Identifies sent since start: {sent}; by AC or Decal: {(foreign >= 0 ? foreign.ToString() : "? (flight recorder off)")}.", $"AutoId skipped: mode {Interlocked.Read(ref _skipMode)}, fresh {Interlocked.Read(ref _skipFresh)}, recent re-create {SkippedRecentCount}; " + - $"waited: age {Interlocked.Read(ref _skipAge)}, defer {Interlocked.Read(ref _skipDefer)}; pending now {Volatile.Read(ref _pendingCount)}" + + $"waited: age {Interlocked.Read(ref _skipAge)}, defer {Interlocked.Read(ref _skipDefer)}, rate {Interlocked.Read(ref _skipRate)}; pending now {Volatile.Read(ref _pendingCount)}" + (Interlocked.Read(ref _overflow) > 0 ? $", overflow {Interlocked.Read(ref _overflow)}" : "") + ".", }; } diff --git a/src/RynthCore.Engine/Compatibility/LoginLifecycleHooks.cs b/src/RynthCore.Engine/Compatibility/LoginLifecycleHooks.cs index 545f678..d2fa113 100644 --- a/src/RynthCore.Engine/Compatibility/LoginLifecycleHooks.cs +++ b/src/RynthCore.Engine/Compatibility/LoginLifecycleHooks.cs @@ -30,6 +30,11 @@ internal static class LoginLifecycleHooks public static bool IsInstalled { get; private set; } public static bool HasObservedLoginComplete { get; private set; } + + /// When login completed (TickCount64), or 0 before it / after a logout. A hot reload + /// marks it at the reload. AutoId defers its background appraisals 30 s past it. + public static long LoginCompleteAtMs => System.Threading.Interlocked.Read(ref _loginCompleteAtMs); + private static long _loginCompleteAtMs; public static string StatusMessage => _statusMessage; public static event Action? LoginComplete; @@ -100,6 +105,7 @@ internal static class LoginLifecycleHooks public static void ResetObservation() { HasObservedLoginComplete = false; + System.Threading.Interlocked.Exchange(ref _loginCompleteAtMs, 0); // Queued engine work (appraisals, health queries, chat, actions) from this login is stale // for the next one (2026-10-06, AcMainThreadQueue). try { AcMainThreadQueue.NewLoginEpoch(); } catch { } @@ -137,6 +143,7 @@ internal static class LoginLifecycleHooks return; HasObservedLoginComplete = true; + System.Threading.Interlocked.Exchange(ref _loginCompleteAtMs, Environment.TickCount64); _statusMessage = $"Login complete observed via {source}."; RynthLog.Verbose($"Compat: login complete observed via {source}."); LoginComplete?.Invoke(); diff --git a/tools/EngineRobustnessTests/AutoIdLimitTests.cs b/tools/EngineRobustnessTests/AutoIdLimitTests.cs index 871ff04..1bc8d14 100644 --- a/tools/EngineRobustnessTests/AutoIdLimitTests.cs +++ b/tools/EngineRobustnessTests/AutoIdLimitTests.cs @@ -16,6 +16,8 @@ internal static partial class Program AutoIdDecisionTable(); AutoIdDecisionSpotCases(); AutoIdDeferWindow(); + AutoIdLoginDefer(); + AutoIdInventoryRate(); AutoIdClassify(); AutoIdModeParse(); IdMarkerScopes(); @@ -28,7 +30,7 @@ internal static partial class Program private static readonly long[] Ages = { 0, AutoIdPolicy.MinAgeMs - 1, AutoIdPolicy.MinAgeMs, 60_000 }; /// The rule as the spec states it, written out independently of AutoIdPolicy.Decide. - private static V Expected(AutoIdMode mode, C cls, long age, bool defer, bool openedAfter, bool fresh, bool cached) + private static V Expected(AutoIdMode mode, C cls, long age, bool defer, bool openedAfter, bool fresh, bool cached, bool rateOk) { if (mode == AutoIdMode.Off) return V.SkipMode; if (mode == AutoIdMode.All) return cls == C.Target && fresh ? V.SkipFresh : V.Send; @@ -37,7 +39,7 @@ internal static partial class Program { C.Target => fresh ? V.SkipFresh : V.Send, C.Container => fresh ? V.SkipFresh : young ? V.WaitAge : defer && !openedAfter ? V.WaitDefer : V.Send, - C.Inventory => cached ? V.SkipFresh : young ? V.WaitAge : defer ? V.WaitDefer : V.Send, + C.Inventory => cached ? V.SkipFresh : young ? V.WaitAge : defer ? V.WaitDefer : !rateOk ? V.WaitRate : V.Send, C.Unknown => young ? V.WaitAge : V.SkipMode, _ => V.SkipMode, }; @@ -54,16 +56,17 @@ internal static partial class Program foreach (bool opened in new[] { false, true }) foreach (bool cached in new[] { false, true }) foreach (bool fresh in new[] { false, true }) + foreach (bool rateOk in new[] { false, true }) { if (fresh && !cached) continue; // fresh implies cached rows++; - V got = AutoIdPolicy.Decide(mode, cls, age, defer, opened, fresh, cached); - V want = Expected(mode, cls, age, defer, opened, fresh, cached); + V got = AutoIdPolicy.Decide(mode, cls, age, defer, opened, fresh, cached, rateOk); + V want = Expected(mode, cls, age, defer, opened, fresh, cached, rateOk); if (got != want && wrong++ < 10) - Check(false, $"{mode} {cls} age={age} defer={defer} opened={opened} cached={cached} fresh={fresh}: want {want}, got {got}"); + Check(false, $"{mode} {cls} age={age} defer={defer} opened={opened} cached={cached} fresh={fresh} rateOk={rateOk}: want {want}, got {got}"); } Check(wrong == 0, $"{wrong} of {rows} rows wrong"); - Check(rows == 3 * 5 * 4 * 2 * 2 * 3, $"row count {rows}"); + Check(rows == 3 * 5 * 4 * 2 * 2 * 3 * 2, $"row count {rows}"); } /// The same rules as named cases, so a mutation is easy to read off a failure. @@ -96,6 +99,10 @@ internal static partial class Program Check(AutoIdPolicy.Decide(AutoIdMode.Limited, C.Inventory, AutoIdPolicy.MinAgeMs - 1, false, false, false, false) == V.WaitAge, "pack item waits for the minimum age"); Check(AutoIdPolicy.Decide(AutoIdMode.Limited, C.Inventory, Old, true, true, false, false) == V.WaitDefer, "pack item deferred (the opened-container exemption does not apply)"); Check(AutoIdPolicy.Decide(AutoIdMode.Limited, C.Inventory, Old, false, false, false, false) == V.Send, "uncached pack item sent"); + Check(AutoIdPolicy.Decide(AutoIdMode.Limited, C.Inventory, Old, false, false, false, false, inventoryRateOk: false) == V.WaitRate, "pack item over the rate waits"); + Check(AutoIdPolicy.Decide(AutoIdMode.Limited, C.Container, Old, false, true, false, false, inventoryRateOk: false) == V.Send, "the rate cap is the inventory's only (container)"); + Check(AutoIdPolicy.Decide(AutoIdMode.Limited, C.Target, 0, true, false, false, false, inventoryRateOk: false) == V.Send, "the rate cap never holds the selected target"); + Check(AutoIdPolicy.Decide(AutoIdMode.All, C.Inventory, 0, false, false, false, false, inventoryRateOk: false) == V.Send, "All mode keeps the old behaviour (no cap)"); // Limited: unknown place. Check(AutoIdPolicy.Decide(AutoIdMode.Limited, C.Unknown, 100, false, false, false, false) == V.WaitAge, "unknown place waits while young"); Check(AutoIdPolicy.Decide(AutoIdMode.Limited, C.Unknown, Old, false, false, false, false) == V.SkipMode, "still unknown when old: skipped"); @@ -109,26 +116,30 @@ internal static partial class Program _case = "autoid defer window"; const long D = AutoIdPolicy.DeferMs; const long S = WorldTransitionState.SettleMs; + // Login completed long before (at 1); every time below is shifted past its defer. + const long B = 1_000_000; + static bool Def(long now, long settle, bool inTransition) => + AutoIdPolicy.InDefer(now + B, settle == 0 ? 0 : settle + B, 1, inTransition); // No transition yet: nothing is deferred. var w = new WorldTransitionState(); w.Sample(1000, dead: false, portaling: false, landblock: 0xA9B4); - Check(!AutoIdPolicy.InDefer(1000, w.SettleUntilMs, w.InTransition(1000)), "no transition, no defer"); + Check(!Def(1000, w.SettleUntilMs, w.InTransition(1000)), "no transition, no defer"); // Walking into the next landblock never defers. w.Sample(2000, false, false, 0xAAB4); w.Sample(3000, false, false, 0xAAB5); Check(w.SettleUntilMs == 0, "an adjacent walk sets no settle window"); - Check(!AutoIdPolicy.InDefer(3000, w.SettleUntilMs, w.InTransition(3000)), "adjacent walk: no defer"); + Check(!Def(3000, w.SettleUntilMs, w.InTransition(3000)), "adjacent walk: no defer"); // A portal: deferred through portal space, the settle window, and DeferMs after it. w.Sample(10_000, false, true, 0xAAB5); - Check(AutoIdPolicy.InDefer(10_000, w.SettleUntilMs, w.InTransition(10_000)), "in portal space: deferred"); + Check(Def(10_000, w.SettleUntilMs, w.InTransition(10_000)), "in portal space: deferred"); w.Sample(12_000, false, false, 0x0007); long settleEnd = w.SettleUntilMs; Check(settleEnd == 12_000 + S, $"settle ends {S} ms after the portal exit, got {settleEnd}"); - Check(AutoIdPolicy.InDefer(settleEnd + D - 1, settleEnd, w.InTransition(settleEnd + D - 1)), "deferred until DeferMs after the settle"); - Check(!AutoIdPolicy.InDefer(settleEnd + D, settleEnd, w.InTransition(settleEnd + D)), "not deferred at DeferMs after the settle"); + Check(Def(settleEnd + D - 1, settleEnd, w.InTransition(settleEnd + D - 1)), "deferred until DeferMs after the settle"); + Check(!Def(settleEnd + D, settleEnd, w.InTransition(settleEnd + D)), "not deferred at DeferMs after the settle"); // A container opened after the settle ended is exempt; one opened before is not. Check(AutoIdPolicy.OpenedAfterSettle(settleEnd + 1, settleEnd), "opened after the settle: exempt"); @@ -140,24 +151,91 @@ internal static partial class Program d.Sample(1000, false, false, 0x1234); d.Sample(2000, true, false, 0x1234); d.Sample(3000, false, false, 0x1234); - Check(AutoIdPolicy.InDefer(3000 + S + D - 1, d.SettleUntilMs, false), "death: deferred"); - Check(!AutoIdPolicy.InDefer(3000 + S + D, d.SettleUntilMs, false), "death: defer ends"); + Check(Def(3000 + S + D - 1, d.SettleUntilMs, false), "death: deferred"); + Check(!Def(3000 + S + D, d.SettleUntilMs, false), "death: defer ends"); // A landblock jump without portal space defers. var j = new WorldTransitionState(); j.Sample(1000, false, false, 0x00AE); j.Sample(1100, false, false, 0xA9B4); - Check(AutoIdPolicy.InDefer(1100 + S + D - 1, j.SettleUntilMs, false), "landblock jump: deferred"); + Check(Def(1100 + S + D - 1, j.SettleUntilMs, false), "landblock jump: deferred"); // A PlayerTeleport message (a recall into the next landblock) defers. var t = new WorldTransitionState(); t.Sample(1000, false, false, 0x1234); t.OnTeleportMessage(5000); - Check(AutoIdPolicy.InDefer(5000 + S + D - 1, t.SettleUntilMs, false), "teleport message: deferred"); - Check(!AutoIdPolicy.InDefer(5000 + S + D, t.SettleUntilMs, false), "teleport message: defer ends"); + Check(Def(5000 + S + D - 1, t.SettleUntilMs, false), "teleport message: deferred"); + Check(!Def(5000 + S + D, t.SettleUntilMs, false), "teleport message: defer ends"); // In a transition right now: deferred whatever the settle time says. - Check(AutoIdPolicy.InDefer(1, 0, inTransition: true), "in a transition: deferred"); + Check(Def(1, 0, inTransition: true), "in a transition: deferred"); + } + + // ---- login is a transition (a client crashed in the login create burst) ---------------- + + private static void AutoIdLoginDefer() + { + _case = "autoid login defer"; + const long D = AutoIdPolicy.DeferMs; + const long Login = 500_000; + + // Before login completes (the create burst): everything background is deferred. + Check(AutoIdPolicy.InDefer(Login - 1, 0, 0, false), "login not complete: deferred"); + Check(AutoIdPolicy.InDefer(10_000_000, 0, 0, false), "login never completed: still deferred"); + // After: DeferMs past login-complete, exactly like the post-teleport defer. + Check(AutoIdPolicy.InDefer(Login, 0, Login, false), "at login-complete: deferred"); + Check(AutoIdPolicy.InDefer(Login + D - 1, 0, Login, false), "deferred until DeferMs after login"); + Check(!AutoIdPolicy.InDefer(Login + D, 0, Login, false), "not deferred at DeferMs after login"); + // The later of login and the last settle window decides. + Check(AutoIdPolicy.TransitionEndMs(0, Login) == Login, "no transition yet: the login is the last one"); + Check(AutoIdPolicy.TransitionEndMs(Login + 9_000, Login) == Login + 9_000, "a portal after login: the portal's settle"); + Check(AutoIdPolicy.TransitionEndMs(Login - 9_000, Login) == Login, "a portal before login: the login"); + Check(AutoIdPolicy.InDefer(Login + 9_000 + D - 1, Login + 9_000, Login, false), "portal after login: its own defer"); + Check(AutoIdPolicy.InDefer(Login + D - 1, Login - 9_000, Login, false), "a settle before login does not shorten the login defer"); + // A container the player opens after login-complete is exempt; one opened before it is not. + Check(AutoIdPolicy.OpenedAfterSettle(Login + 1, AutoIdPolicy.TransitionEndMs(0, Login)), "opened after login: exempt"); + Check(!AutoIdPolicy.OpenedAfterSettle(Login - 1, AutoIdPolicy.TransitionEndMs(0, Login)), "opened before login completed: not exempt"); + // The decision during the login defer: pack and container items wait, the target does not. + bool inDefer = AutoIdPolicy.InDefer(Login + 1_000, 0, Login, false); + Check(AutoIdPolicy.Decide(AutoIdMode.Limited, C.Inventory, 60_000, inDefer, false, false, false) == V.WaitDefer, "login: pack item waits"); + Check(AutoIdPolicy.Decide(AutoIdMode.Limited, C.Container, 60_000, inDefer, false, false, false) == V.WaitDefer, "login: container item waits"); + Check(AutoIdPolicy.Decide(AutoIdMode.Limited, C.Target, 0, inDefer, false, false, false) == V.Send, "login: the selected target is exempt"); + } + + private static void AutoIdInventoryRate() + { + _case = "autoid inventory rate"; + const long I = AutoIdPolicy.InventoryMinIntervalMs; + Check(I == 200, "5 a second"); + Check(AutoIdPolicy.InventoryRateAllows(1_000, 0), "the first send is allowed"); + Check(!AutoIdPolicy.InventoryRateAllows(1_000 + I - 1, 1_000), "not again inside the interval"); + Check(AutoIdPolicy.InventoryRateAllows(1_000 + I, 1_000), "again at the interval"); + + // Simulate the drain: a 100 ms timer, 3 sends a tick at most, 50 uncached pack items + // past their age and the defer. Sends per second never pass 5. + long last = 0; + int pending = 50; + var sendTimes = new List(); + for (long now = 100; now <= 20_000 && pending > 0; now += 100) + { + int sentThisTick = 0; + for (int k = 0; k < pending && sentThisTick < 3; k++) + { + bool ok = AutoIdPolicy.InventoryRateAllows(now, last); + V v = AutoIdPolicy.Decide(AutoIdMode.Limited, C.Inventory, 60_000, false, false, false, false, ok); + if (v != V.Send) continue; + last = now; + sentThisTick++; + sendTimes.Add(now); + } + pending -= sentThisTick; + } + Check(pending == 0, $"all 50 sent eventually, {pending} left"); + int worst = 0; + foreach (long t0 in sendTimes) + worst = Math.Max(worst, sendTimes.Count(t => t >= t0 && t < t0 + 1_000)); + Check(worst <= 5, $"at most 5 inventory sends in any second, got {worst}"); + Check(worst == 5, $"and the cap is reached (5), got {worst}"); } // ---- where an object is ------------------------------------------------------------