// ============================================================================
// RynthCore.Engine - Compatibility/AutoIdPolicy.cs
//
// Which background appraisals AutoIdService sends (the decision only; AutoIdService
// does the queueing, the lookups and the sends).
//
// Why: in one 26-minute session (1585 s) the client sent 3211 IdentifyObject (0xC8),
// about three quarters of everything it sent, for 2479 objects against 28
// kills. AutoId appraised every object the client created. The bot (RynthAi) already
// appraises what it needs at the point of use (corpse items its loot rules need, the
// selected creature, equipment, meta conditions), so most of that was traffic for
// nothing; it is now limited (engine.json "AutoIdMode", /rc autoid).
//
// Modes:
// All the old behaviour: every created object except the player, the selected
// target with its fresh-cache skip.
// Limited (default) only
// Target the selected target (fresh-cache skip, no age, no defer);
// Container items in the container the player has open now (a corpse,
// a chest, a pack opened through ViewContents);
// Inventory items in the player's own inventory (worn, in the main pack
// 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.
// Off nothing, the selected target included.
// Plugins calling the host RequestId are not limited by any of this.
//
// Pure: no engine state, no locking. Compiled into tools\EngineRobustnessTests and
// tools\EngineSettingsTests.
// ============================================================================
using System;
namespace RynthCore.Engine.Compatibility;
/// engine.json "AutoIdMode" (see ).
internal enum AutoIdMode { All = 0, Limited = 1, Off = 2 }
internal static class AutoIdPolicy
{
public const AutoIdMode DefaultMode = AutoIdMode.Limited;
/// 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.
public const long DeferMs = 30_000;
public enum Class
{
/// The selected target (the priority path).
Target,
/// In the container the player has open now.
Container,
/// In the player's own inventory (worn, main pack, side pack).
Inventory,
/// Anything else the client knows where it is: a creature, a ground item, another player.
World,
/// Not in the engine's object snapshot yet (created moments ago), so its place is unknown.
Unknown,
}
public enum Verdict
{
/// Appraise it now.
Send,
/// The mode does not appraise this: drop it.
SkipMode,
/// Too young: keep it and ask again later.
WaitAge,
/// Inside the post-transition defer: keep it and ask again later.
WaitDefer,
/// Its appraisal is cached (fresh): drop it.
SkipFresh,
}
///
/// The decision for one candidate.
/// : how long the object has existed (or since it moved, for a move).
/// : inside the post-transition defer window ().
/// : Container only, the player opened it after the last
/// settle window ended (), which exempts it from the defer.
/// : its appraisal is cached and recent (Target, Container).
/// : its appraisal is cached at all (Inventory).
///
public static Verdict Decide(AutoIdMode mode, Class cls, long ageMs, bool inDefer, bool openedAfterSettle,
bool fresh, bool cached)
{
switch (mode)
{
case AutoIdMode.Off:
return Verdict.SkipMode;
case AutoIdMode.All:
// The old behaviour: the create-time eligibility already decided; only the
// selected target has the fresh-cache skip.
if (cls == Class.Target && fresh) return Verdict.SkipFresh;
return Verdict.Send;
default: // Limited
switch (cls)
{
case Class.Target:
return fresh ? Verdict.SkipFresh : Verdict.Send;
case Class.Container:
if (fresh) return Verdict.SkipFresh;
if (ageMs < MinAgeMs) return Verdict.WaitAge;
if (inDefer && !openedAfterSettle) return Verdict.WaitDefer;
return Verdict.Send;
case Class.Inventory:
if (cached) return Verdict.SkipFresh;
if (ageMs < MinAgeMs) return Verdict.WaitAge;
if (inDefer) return Verdict.WaitDefer;
return Verdict.Send;
case Class.Unknown:
// Its place shows up in the snapshot within a tick or two; until it is
// MinAgeMs old it may still turn out to be in the open container or the pack.
return ageMs < MinAgeMs ? Verdict.WaitAge : Verdict.SkipMode;
default: // World
return Verdict.SkipMode;
}
}
}
///
/// 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.
///
public static bool InDefer(long nowMs, long settleUntilMs, bool inTransition) =>
inTransition || (settleUntilMs > 0 && nowMs < settleUntilMs + DeferMs);
/// The player opened the container (at ) after the last settle window ended.
public static bool OpenedAfterSettle(long openedAtMs, long settleUntilMs) =>
openedAtMs > 0 && openedAtMs >= settleUntilMs;
///
/// Where an object is, from its PublicWeenieDesc ownership fields ( =
/// the snapshot had it). is the container's own container
/// (a side pack's owner), 0 when unknown or none. is the
/// container the player has open now (0 = none).
///
public static Class Classify(bool known, uint playerId, uint openContainer,
uint container, uint wielder, uint containerParent)
{
if (!known) return Class.Unknown;
if (playerId != 0 && (container == playerId || wielder == playerId || (container != 0 && containerParent == playerId)))
return Class.Inventory;
// openContainer != 0 also keeps a ground item (container 0) out of here.
if (openContainer != 0 && (container == openContainer || containerParent == openContainer))
return Class.Container;
return Class.World;
}
public static bool TryParseMode(string? text, out AutoIdMode mode)
{
switch ((text ?? "").Trim().ToLowerInvariant())
{
case "all": mode = AutoIdMode.All; return true;
case "limited": case "limit": mode = AutoIdMode.Limited; return true;
case "off": case "none": mode = AutoIdMode.Off; return true;
default: mode = DefaultMode; return false;
}
}
public static string Name(AutoIdMode mode) => mode switch
{
AutoIdMode.All => "all",
AutoIdMode.Off => "off",
_ => "limited",
};
}