// ============================================================================ // DoorInteractionController.cs — partial class RynthAiPlugin // // Detects closed doors while the macro is running and automatically opens them. // Optionally uses lockpicks from the Consumable Items list on a door the server // says is locked. // // State machine: // None -> Pending (a use waits for its turn) -> Opening (the use is out) -> // Settling (the server answered; the door gets a moment to swing) -> done, // or back to Pending for another use, or Unlocking (a lockpick is out) -> Pending, // or Cooldown. // // Two ways in: the proximity scan (OpenDoors setting, nearest closed door in // range), and a requested door (RequestDoorOpen): the nav engine's recovery // detour asks for the closed door on its next leg. A requested door is opened // even with OpenDoors off, since the detour was planned through that doorway. // // The rules (2026-10-13, a mage on DreamWeave: "doors aren't opening"; 13 doors, // 5 timeouts, 3 lockpick cooldowns with no locked door anywhere): // * One door use at a time, sent only when nothing else is waiting for the server // (ActionPacer.MayStart: no unanswered use/cast/stance change, no animation, the // stop before it settled) and the client isn't busy (up to DoorBusyWaitMaxMs: // the busy count only drives AC's cursor and reads 1 for seconds after a slow // cast or a portal, BusyCountHooks' watchdog clears it after 5 s). The old code // sent the use at once (busy=1) and again after 3 s with the first still // unanswered: on ACE a second use restarts the server's walk to the door, and a // use of a door that just opened closes it again. // * The server walks the character to the door before it opens it: the answer // (UseDone) took 0.3-3.7 s in that log, so the use is given DoorAnswerMaxMs // before it counts as lost. A door that reads open ends it at once, whatever // the wait, so a door that opens is never waited on. // * "Locked" only when the server says so: ACE answers a locked door with the // transient text "The door is locked!" (Door.ActOnUse), which RynthAi gets as a // server message (API v78) or a chat line. A door still closed after an answered // use is used again (DoorMaxAttempts), never picked. // * Nothing while in portal space or for DoorPostPortalMs after it: two of the // log's false "locked" doors were targeted inside portal space (busy=1 from the // portal), their uses lost to the teleport. // ============================================================================ using System; using System.Linq; using System.Text.RegularExpressions; using RynthCore.Plugin.RynthAi.LegacyUi; using RynthCore.Plugin.Shared; namespace RynthCore.Plugin.RynthAi; public sealed partial class RynthAiPlugin { private int _doorTargetId; private long _doorActionAt; // when the current state started (Pending: when the use was wanted) private int _doorAttempts; // door uses that came back with the door still closed private int _doorPicks; // lockpicks used on the current door private DoorState _doorState = DoorState.None; private long _doorLastDiagAt; private int _doorRequestedId; // asked for by the nav recovery detour; taken on the next idle tick private bool _doorTargetRequested; // the current target came from a request private bool _doorWantLockpick; // Pending sends a lockpick (the server said locked), not an open private long _doorSentAt; // when the last door use / lockpick went out private int _doorSeqAtSend; // the UseDone count read just before it went out private uint _doorAnswerErr; // the error of the UseDone that answered it (0 = done) private bool _doorWaitLogged; // one "waits" line per Pending private long _doorLastPortalAt = long.MinValue / 2; // "The door is locked!" (ACE Door.ActOnUse, a transient string): when it last arrived. private long _doorLockedSaidAt = long.MinValue / 2; // Doors we've already opened — skip for 60s so we don't re-target them private readonly System.Collections.Generic.Dictionary _doorOpenedRecently = new(); // Locked doors we can't pick (no lockpick, Auto Unlock off, out of picks) — skip a while private readonly System.Collections.Generic.Dictionary _doorLockedRecently = new(); /// Clock (ms) for the door logic; tests replace it. internal static Func DoorClock = static () => Environment.TickCount64; /// A use with no answer this long is taken as lost (the longest answer in the 10-13 log was 3.7 s; /// ActionPacer gives a world use 5 s). private const long DoorAnswerMaxMs = 6000; /// After the server's answer, the door gets this long to read open before the use counts as failed. private const long DoorOpenSettleMs = 1200; /// A use waits for the client's busy count to read 0 at most this long (see the header). private const long DoorBusyWaitMaxMs = 2500; /// A use that can't go out this long (something else keeps the server busy) gives up for now. private const long DoorSendWaitMaxMs = 10000; /// No door is targeted for this long after portal space. private const long DoorPostPortalMs = 1500; private const long DoorCooldownMs = 5000; private const long DoorOpenedSkipMs = 60000; private const long DoorLockedSkipMs = 30000; private const int DoorMaxAttempts = 3; private const int DoorMaxPicks = 3; private const uint DoorTooBusyErr = 0x1D; private const float YardsToMeters = 0.9144f; // ACE: "The door is locked!"; other servers name the door ("The Gate is locked."). Server text only. private static readonly Regex DoorLockedText = new(@"^\s*the\s.+\sis\s+locked\s*[.!]?\s*$", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled); private enum DoorState { None, Pending, Opening, Settling, Unlocking, Cooldown } /// /// Called each tick when the macro is running. /// Returns true if door interaction is blocking navigation. /// private bool TickDoorInteraction() { var settings = _dashboard?.Settings; bool requested = _doorRequestedId != 0 || _doorTargetRequested; if (settings == null || !settings.IsMacroRunning || (!settings.OpenDoors && !requested)) { ResetDoorState(); _doorRequestedId = 0; return false; } long now = DoorClock(); PruneDoorSkips(_doorOpenedRecently, now, DoorOpenedSkipMs); PruneDoorSkips(_doorLockedRecently, now, DoorLockedSkipMs); // Portal space: the server drops a use sent now, and the client's busy count reads 1 for the // portal. Whatever was under way is forgotten; nothing new starts until a moment after it. if (ActionPacer.IsPortaling(Host)) { _doorLastPortalAt = now; if (_doorState != DoorState.None) { Host.Log($"[RynthAi] Door: portal space - dropping 0x{(uint)_doorTargetId:X8} (the server drops a use sent now)"); ResetDoorState(); } return false; } // ── Idle: scan for a nearby closed door ───────────────────────────── if (_doorState == DoorState.None) { if (now - _doorLastPortalAt < DoorPostPortalMs) return false; int doorId = 0; if (_doorRequestedId != 0) { int req = _doorRequestedId; _doorRequestedId = 0; if (!IsDoorOpen(req) && !_doorLockedRecently.ContainsKey(req)) { doorId = req; _doorTargetRequested = true; } } if (doorId == 0 && settings.OpenDoors) doorId = FindNearestClosedDoor(settings.OpenDoorRange, now); if (doorId == 0) return false; string why = _doorTargetRequested ? " (asked for by the nav detour)" : ""; Host.Log($"[RynthAi] Door: targeting 0x{(uint)doorId:X8}{why} (state={GetDoorStateDebug(doorId)}, busy={_busyCount})"); _doorTargetId = doorId; _doorAttempts = 0; _doorPicks = 0; _doorWantLockpick = false; _navigationEngine?.Stop(); if (Host.HasStopCompletely) Host.StopCompletelyBy("Door"); // The use goes once the stop has settled and nothing else waits for the server. EnterDoorPending(now); return true; } switch (_doorState) { case DoorState.Pending: return TickDoorPending(settings, now); case DoorState.Opening: { if (IsDoorOpen(_doorTargetId)) { Host.Log($"[RynthAi] Door: 0x{(uint)_doorTargetId:X8} opened OK after {now - _doorSentAt} ms — ready for next"); MarkDoorOpenedAndReset(now); return false; } if (DoorSaidLocked()) return OnDoorLocked(settings, now); if (DoorUseAnswered(out uint err)) { _doorAnswerErr = err; _doorState = DoorState.Settling; _doorActionAt = now; return true; } if (now - _doorSentAt >= DoorAnswerMaxMs) { Host.Log($"[RynthAi] Door: no answer from the server to the use of 0x{(uint)_doorTargetId:X8} in {now - _doorSentAt} ms (busy={_busyCount})"); return DoorUseFailed(now, "the server never answered the use"); } return true; } case DoorState.Settling: { if (IsDoorOpen(_doorTargetId)) { Host.Log($"[RynthAi] Door: 0x{(uint)_doorTargetId:X8} opened OK after {now - _doorSentAt} ms — ready for next"); MarkDoorOpenedAndReset(now); return false; } if (DoorSaidLocked()) return OnDoorLocked(settings, now); if (_doorAnswerErr == DoorTooBusyErr) return DoorUseFailed(now, "the server answered 'too busy' (0x1D)"); if (now - _doorActionAt < DoorOpenSettleMs) return true; Host.Log($"[RynthAi] Door: 0x{(uint)_doorTargetId:X8} not open {now - _doorSentAt} ms after the use ({GetDoorStateDebug(_doorTargetId)}, answer err=0x{_doorAnswerErr:X}); no 'locked' from the server"); return DoorUseFailed(now, "still closed after the server's answer"); } case DoorState.Unlocking: { if (IsDoorOpen(_doorTargetId)) { Host.Log($"[RynthAi] Door: 0x{(uint)_doorTargetId:X8} unlocked & opened OK — ready for next"); MarkDoorOpenedAndReset(now); return false; } bool answered = DoorUseAnswered(out _); if (!answered && now - _doorSentAt < DoorAnswerMaxMs) return true; Host.Log(answered ? $"[RynthAi] Door: lockpick answered, opening 0x{(uint)_doorTargetId:X8} again" : $"[RynthAi] Door: no answer to the lockpick in {now - _doorSentAt} ms, opening 0x{(uint)_doorTargetId:X8} again"); _doorWantLockpick = false; EnterDoorPending(now); return true; } case DoorState.Cooldown: if (now - _doorActionAt >= DoorCooldownMs) { Host.Log($"[RynthAi] Door: cooldown done for 0x{(uint)_doorTargetId:X8}, resetting"); ResetDoorState(); } return _doorState != DoorState.None; } return false; } /// A door use (or a lockpick) is wanted: it goes when lets it. private void EnterDoorPending(long now) { _doorState = DoorState.Pending; _doorActionAt = now; _doorWaitLogged = false; } /// Sends the wanted use once nothing else waits for the server; true while the door holds nav. private bool TickDoorPending(LegacyUiSettings settings, long now) { // Open already (another player, or the last use's swing showed late): never use it again, // a use of an open door closes it. if (IsDoorOpen(_doorTargetId)) { Host.Log($"[RynthAi] Door: 0x{(uint)_doorTargetId:X8} reads open — ready for next"); MarkDoorOpenedAndReset(now); return false; } long waited = now - _doorActionAt; if (!DoorMayUseNow(waited, out string why)) { if (waited >= DoorSendWaitMaxMs) { Host.Log($"[RynthAi] Door: couldn't use 0x{(uint)_doorTargetId:X8} for {waited} ms ({why}), cooldown"); EnterDoorCooldown(now); return true; } if (!_doorWaitLogged) { _doorWaitLogged = true; Host.Log($"[RynthAi] Door: 0x{(uint)_doorTargetId:X8} waits: {why}"); } return true; } int seqBefore = ActionPacer.UseDoneSeqNow(Host); bool sent; if (_doorWantLockpick) { var lockpick = FindLockpickInConsumables(); if (lockpick == null) { Host.Log("[RynthAi] Door: the lockpick is gone, cooldown"); EnterDoorCooldown(now, locked: true); return true; } Host.Log($"[RynthAi] Door: using lockpick {lockpick.Name} on 0x{(uint)_doorTargetId:X8}"); sent = Host.UseOnFor(unchecked((uint)lockpick.Id), unchecked((uint)_doorTargetId), "Door", "Auto Unlock Doors: the server says this door is locked"); if (sent) { _doorPicks++; _doorState = DoorState.Unlocking; } } else { string reason = _doorAttempts > 0 ? $"try {_doorAttempts + 1}: still closed after the last use" : _doorPicks > 0 ? "open after the lockpick" : _doorTargetRequested ? "the nav detour asked to open this door" : $"Open Doors: closed door within {settings.OpenDoorRange:0} yd"; sent = Host.UseFor(unchecked((uint)_doorTargetId), "Door", reason); if (sent) _doorState = DoorState.Opening; } if (!sent) { // Held (salvage panel), or the engine refused it: try again on a later tick. if (waited >= DoorSendWaitMaxMs) { Host.Log($"[RynthAi] Door: the use of 0x{(uint)_doorTargetId:X8} didn't go out for {waited} ms, cooldown"); EnterDoorCooldown(now); } return true; } _doorSentAt = now; _doorActionAt = now; _doorSeqAtSend = seqBefore; _doorAnswerErr = 0; _doorLockedSaidAt = long.MinValue / 2; // only a "locked" answer to THIS use counts return true; } /// May a door use go out now? No while another server action is unanswered, the character /// animates or its stop hasn't settled (ActionPacer), nor while the client reads busy (up to /// DoorBusyWaitMaxMs). private bool DoorMayUseNow(long waited, out string why) { if (!ActionPacer.MayStart(Host, "Door", HealNeed.None, out why)) return false; if (_busyCount > 0 && waited < DoorBusyWaitMaxMs) { why = $"the client is busy ({_busyCount})"; return false; } why = ""; return true; } /// The server answered the last door use / lockpick (a UseDone since it went out); err = its error. private bool DoorUseAnswered(out uint err) { err = 0; try { if (!Host.HasUseDoneSeq) return DoorClock() - _doorSentAt >= ActionPacer.NoUseDoneFallbackMs; // no UseDone on this engine int seq = Host.GetUseDoneSeq(); if (seq == _doorSeqAtSend) return false; if (Host.TryGetLastUseDone(out int ls, out uint le) && ls == _doorSeqAtSend + 1) err = le; return true; } catch { return false; } } /// The server said "locked" since the last door use went out. private bool DoorSaidLocked() => _doorLockedSaidAt >= _doorSentAt; /// A door use came back with the door still closed: use it again, or give up for now. private bool DoorUseFailed(long now, string why) { _doorAttempts++; if (_doorAttempts >= DoorMaxAttempts) { Host.Log($"[RynthAi] Door: 0x{(uint)_doorTargetId:X8} still closed after {_doorAttempts} uses ({why}), cooldown"); EnterDoorCooldown(now); return true; } Host.Log($"[RynthAi] Door: 0x{(uint)_doorTargetId:X8} {why} - using it again (try {_doorAttempts + 1} of {DoorMaxAttempts})"); EnterDoorPending(now); return true; } /// The server said the door is locked: a lockpick if allowed and carried, else leave it for a while. private bool OnDoorLocked(LegacyUiSettings settings, long now) { if (!settings.AutoUnlockDoors) { Host.Log($"[RynthAi] Door: the server says 0x{(uint)_doorTargetId:X8} is locked; Auto Unlock Doors is off, cooldown"); EnterDoorCooldown(now, locked: true); return true; } if (_doorPicks >= DoorMaxPicks) { Host.Log($"[RynthAi] Door: 0x{(uint)_doorTargetId:X8} still locked after {_doorPicks} lockpick(s), cooldown"); EnterDoorCooldown(now, locked: true); return true; } var lockpick = FindLockpickInConsumables(); if (lockpick == null) { Host.Log($"[RynthAi] Door: the server says 0x{(uint)_doorTargetId:X8} is locked but there's no lockpick in consumables, cooldown"); EnterDoorCooldown(now, locked: true); return true; } Host.Log($"[RynthAi] Door: the server says 0x{(uint)_doorTargetId:X8} is locked - lockpick next"); _doorWantLockpick = true; EnterDoorPending(now); return true; } private void EnterDoorCooldown(long now, bool locked = false) { if (locked && _doorTargetId != 0) _doorLockedRecently[_doorTargetId] = now; _doorState = DoorState.Cooldown; _doorActionAt = now; } /// /// Server text (a transient string from the server-message stream, or a chat line): remembers a /// "The door is locked!" for the door logic. Called from both, so whichever the client delivers /// first counts. /// internal void NoteDoorServerText(string? text) { if (string.IsNullOrEmpty(text) || text.Length > 120) return; if (!DoorLockedText.IsMatch(text)) return; _doorLockedSaidAt = DoorClock(); if (_doorState is DoorState.Opening or DoorState.Settling) Host.Log($"[RynthAi] Door: server says \"{text.Trim()}\" (0x{(uint)_doorTargetId:X8})"); } private static void PruneDoorSkips(System.Collections.Generic.Dictionary skips, long now, long forMs) { if (skips.Count == 0) return; foreach (int key in skips.Where(kv => now - kv.Value >= forMs).Select(kv => kv.Key).ToList()) skips.Remove(key); } private void MarkDoorOpenedAndReset(long now) { _doorOpenedRecently[_doorTargetId] = now; ResetDoorState(); } private void ResetDoorState() { _doorTargetId = 0; _doorState = DoorState.None; _doorAttempts = 0; _doorPicks = 0; _doorWantLockpick = false; _doorTargetRequested = false; } /// /// Nav recovery: open this door on the next tick, whether or not OpenDoors is on. /// Ignored while another door is being handled (the detour asks again later). /// private void RequestDoorOpen(int doorId) { if (doorId != 0 && _doorState == DoorState.None) _doorRequestedId = doorId; } /// /// Nav recovery: the closed door nearest the start of the leg (ax, ay) to (bx, by), in /// world units, within of it and about the same height as /// ; 0 when there is none. Doors are static objects with BF_DOOR /// (or a door-like name when the bitfield isn't available). /// private int FindClosedDoorOnLeg(double ax, double ay, double bx, double by, double z, double corridor) { if (_objectCache == null || !Host.HasGetObjectPosition) return 0; bool hasBitfield = Host.HasGetObjectBitfield; double dx = bx - ax, dy = by - ay; double len2 = dx * dx + dy * dy; int bestId = 0; double bestT = double.MaxValue; foreach (var wo in _objectCache.GetLandscapeObjects()) { uint uid = unchecked((uint)wo.Id); if (uid >= 0x80000000u) continue; if (hasBitfield) { if (!Host.TryGetObjectBitfield(uid, out uint bf) || (bf & 0x1000u) == 0) continue; } else { string nl = (wo.Name ?? string.Empty).ToLowerInvariant(); if (!nl.Contains("door") && !nl.Contains("gate") && !nl.Contains("hatch") && !nl.Contains("portcullis")) continue; } if (IsDoorOpen(wo.Id)) continue; if (!Host.TryGetObjectPosition(uid, out uint cell, out float x, out float y, out float oz)) continue; if (Math.Abs(oz - z) > 4.0) continue; double px = ((cell >> 24) & 0xFF) * 192.0 + x; double py = ((cell >> 16) & 0xFF) * 192.0 + y; double t = len2 < 1e-6 ? 0.0 : Math.Clamp(((px - ax) * dx + (py - ay) * dy) / len2, 0.0, 1.0); double cx = ax + dx * t - px, cy = ay + dy * t - py; if (cx * cx + cy * cy > corridor * corridor) continue; if (t < bestT) { bestT = t; bestId = wo.Id; } } return bestId; } private int FindNearestClosedDoor(float rangeYards, long now) { if (_objectCache == null || _playerId == 0) return 0; float rangeMeters = rangeYards * YardsToMeters; int bestId = 0; double bestDist = double.MaxValue; bool hasBitfield = Host.HasGetObjectBitfield; int totalLandscape = 0, doorsFound = 0, openSkipped = 0, recentSkipped = 0, lockedSkipped = 0; foreach (var wo in _objectCache.GetLandscapeObjects()) { uint uid = unchecked((uint)wo.Id); totalLandscape++; // Doors have static GUIDs (< 0x80000000) if (uid >= 0x80000000u) continue; // Primary: BF_DOOR bitfield check if (hasBitfield) { if (!Host.TryGetObjectBitfield(uid, out uint bf) || (bf & 0x1000u) == 0) continue; } else { string nl = (wo.Name ?? string.Empty).ToLowerInvariant(); if (!nl.Contains("door") && !nl.Contains("gate") && !nl.Contains("hatch") && !nl.Contains("portcullis")) continue; } doorsFound++; // Skip doors we recently opened if (_doorOpenedRecently.TryGetValue(wo.Id, out long openedAt) && (now - openedAt) < DoorOpenedSkipMs) { recentSkipped++; continue; } // Skip locked doors we couldn't pick if (_doorLockedRecently.ContainsKey(wo.Id)) { lockedSkipped++; continue; } // Skip already-open doors if (IsDoorOpen(wo.Id)) { openSkipped++; continue; } double dist = _objectCache.Distance(unchecked((int)_playerId), wo.Id); if (dist > rangeMeters || dist >= bestDist) continue; bestDist = dist; bestId = wo.Id; } // Periodic diagnostic when doors are nearby but none targeted if (bestId == 0 && doorsFound > 0 && (now - _doorLastDiagAt) >= 5000) { _doorLastDiagAt = now; Host.Log($"[RynthAi] Door scan: {totalLandscape} landscape, {doorsFound} doors, {openSkipped} open, {recentSkipped} recent-skip, {lockedSkipped} locked-skip, range={rangeMeters:F1}m"); } return bestId; } private bool IsDoorOpen(int doorId) { uint uid = unchecked((uint)doorId); // Primary: read CPhysicsObj::m_state directly — ETHEREAL_PS (0x4) = open if (Host.HasGetObjectState && Host.TryGetObjectState(uid, out uint physState)) return (physState & 0x4u) != 0; // Fallback: DoMotionHooks tracks MotionOn/MotionOff per object if (Host.HasGetObjectMotionOn && Host.TryGetObjectMotionOn(uid, out bool isOn)) return isOn; return false; } private string GetDoorStateDebug(int doorId) { uint uid = unchecked((uint)doorId); if (Host.HasGetObjectState && Host.TryGetObjectState(uid, out uint ps)) return $"phys=0x{ps:X8} ethereal={(ps & 0x4u) != 0}"; if (Host.HasGetObjectMotionOn && Host.TryGetObjectMotionOn(uid, out bool on)) return $"motionOn={on}"; return "no-state"; } /// The first listed lockpick still carried: its own stack, else one of the same name (ConsumableLookup). private WorldObject? FindLockpickInConsumables() { var settings = _dashboard?.Settings; if (settings == null || _objectCache == null) return null; foreach (var r in settings.ConsumableRules) if (r.Type.Equals("Lockpick", StringComparison.OrdinalIgnoreCase) && ConsumableLookup.Resolve(_objectCache, r) is WorldObject pick) return pick; return null; } }