Started as an allocation pass over `StepCache` and grew into a cleanup of the surrounding pathing engine. Four commits, each independently reviewable; net **−560 lines**. Build clean (0 warnings). All 122 `Server.Tests.Pathfinding` tests pass. --- ## 1. `perf`: pool the strata buffer, cut a hot-path dictionary lookup **The headline is that `TryGetMask` — the actual hot path — was already allocation-free.** `StepMask` is a readonly struct, `StaticTileEnumerable` is a `ref struct`, `ChunkMissState` is a struct in a `Dictionary`. So most of this is a bake-throughput and GC-churn win, with one exception noted below. `BuildChunk` accumulated packed multi-Z strata into a `List<byte>` that grew by doubling (256 → 512 → 1024 → …) and then paid a final `ToArray()`. A full map bake runs it ~114k times. It now writes into a `byte[]` rented from `STArrayPool<byte>.Shared` through a span writer, and hands the chunk one exact-size copy. **This required fixing a latent out-of-bounds guard.** The record-fit check reserved headroom for **8** strata (`StratumByteLength * 8`) while `ComputeStandableSurfaceZs` can return up to **16** — so a cell could write 305 bytes starting from a 65,383-byte offset. Against a `List` that was benign (it just grew past 64 KB, and emitted offsets stayed under the `NoStrata` sentinel). Against a fixed-size rented buffer it is an out-of-bounds write, so tightening it was a *prerequisite* for the pooling, not a drive-by. The guard is now exact, which additionally proves no emitted offset can collide with `NoStrata == ushort.MaxValue`. **One genuine query-path win:** `ShouldPromoteAfterMiss` did *two* dictionary lookups per miss — a `TryGetValue`, then an indexer assignment that re-hashes and re-probes. It now mutates in place via `CollectionsMarshal.GetValueRefOrNullRef`. This runs on every uncached chunk touch during A* expansion. The window-expiry branch keeps its explicit early return, so `MissPromotionThreshold == 1` still resets rather than promoting. Also dropped `StepProbe.ComputeStrataAt` / `ComputedStratum` (dead code, zero callers) and collapsed six 18-argument `new StepMask(0, 0, …, kind)` blocks into `Fallthrough(kind)`. **Considered and rejected:** pooling the `Direction[]` that `Find` returns. It *escapes* the call — `MovementPath` holds it across ticks while `PathFollower` walks `m_Index` through it — so it cannot be rented-and-returned, and it cannot be borrowed from the shared `BitmapAStarAlgorithm.Instance` without one creature clobbering another's in-flight path. `CheckPath` rate-limits repaths to one per 2s per creature, putting this at roughly 60 KB/sec at 1,000 pathing creatures. Not worth a public API break plus a use-after-return footgun. ## 2. `docs`: rewrite the comments for publication The comments had accumulated as development notes: internal phase jargon (`Tier 4`, `the Phase-2 synthesizer`), change narration aimed at a reviewer (`which the old ComputeStandingZ anchor missed`, `legacy behavior`), benchmark anecdotes (`benchmarked as near-optimal`, `a ~20 ns lookup`), and paragraphs restating the code. Rewritten to keep the rationale you cannot recover by reading the code — why the source-Z guard cannot be widened, why multis fall through with a halo, why the promotion gate counts Finds rather than calls, why `ComputeFingerprint` must hash the *files* and not the live tile tables — and drop the history that got us there. Three comments were **factually wrong**, not just wordy: - `CacheEvictionTimer` and `CacheStats` documented a class called `StaticWalkabilityCache`. No such class exists — it is `StepCache`. - `StepCacheFile` declared `File layout v8` while `FormatVersion` is 9, and called the current record layout "the v6 layout" in four places. The layout descriptions are now unversioned so they cannot drift again. - `StepProbe.ComputeStandingZ` claimed `StepCache` uses it to bake `SourceZ`. It has not since the baker moved to the clearance-aware `ComputeStandableSurfaceZs`; only a parity test calls it. ## 3. `refactor`: simplify `StepCacheFile.Write`, consolidate the format tests `SaveToFile` walked `_keysList` **twice** — once to count the map's chunks, then again through a `ChunkEnumerator` closure to emit them — because `Write` needed the count up front to size its index array. Both loops had the same root cause. Passing a **span** collapses them: the count is just `span.Length`. That deletes the `ChunkEnumerator` delegate, the closure over the list enumerator, and **both `InvalidOperationException` throws**, which existed only to police the delegate's "yield exactly `chunkCount` chunks" contract — a contract a span makes unrepresentable. `Write` now patches the header's `IndexOffset` by seeking back to it rather than reaching into the writer's live buffer with `BinaryPrimitives`. That also retires `IndexOffsetFieldPosition`, a hand-maintained byte offset that had to track the header layout, and sidesteps the stale-array hazard that motivated the manual patch (`BufferWriter` reallocates on growth). **Tests:** `StepCacheFileV6/V7/V8Tests` were named for the format version that introduced each transform — and the format is now **v9**, so all three names described formats the loader rejects outright. Beyond triplicated builders and plumbing, two things were actually broken: - The three near-identical rejection tests each cited a `MinSupportedVersion` that had since moved (`"version 5 < MinSupportedVersion 6"`, `"6 < 7"`, `"7 < 8"`). They passed for the wrong reason. - `AssertBaseEqual` (used by V7 and V8) **silently skipped the swim and strata trailers**. A regression dropping either would not have failed those tests. Now one `StepCacheFileFormatTests`, named for behavior — predictive-Z elision, compression, compact index — with a single `AssertIdentical` that does check both trailers, the three rejection tests folded into one theory that also covers a future version, and a zero-chunk case the delegate-based writer never had coverage for. ## 4. `test`: consolidate the parity and lifecycle tests Three files tested "parity" and none of the names said *which*. They were three different layers, and the seams are the useful part, so they are now one `StepCacheParityTests` that names them: | Test | Compares | Answers | |---|---|---| | `ProbeMatchesSlowPath` | StepProbe vs MovementImpl | Is the bake right? | | `CacheMatchesProbe` | StepCache vs StepProbe | Is it stored and returned intact? | | `CacheServesReachableWalkStates` | StepCache vs MovementImpl | End to end, over the states A* visits | Merging removed a duplicated stub `Mobile`, duplicated region seeds, and a filename/class mismatch (`StepProbeParityTests.cs` declared `StaticWalkabilityParityTests`). `SwimBake_ProducesWetCells` moved with it — it lived in the cache parity file but never touched the cache. Tests reached into `StepCache._chunks` via `GetField` in **9 places**, each rebuilding the key encoding and cell-index arithmetic by hand. `StepCache` now exposes `GetResidentChunk` and `ResidentIndexInSync` alongside the internal test hooks it already had (`LazyReaderHasChunk`, `CurrentFindGeneration`), and the shared arithmetic moved to `PathingTestSupport`. All 9 reflection blocks are gone. `StepCacheLifecycleTests` is regrouped by what it covers — promotion gate, fallthrough routes, strata, swim layer, eviction — with the `Tier4*` names dropped. Removed `Singleton_IsAvailable`, which asserted an inline-initialized static property was not null; that is the entire 123 → 122 test-count delta. --- ## Verification Tests were mutation-checked rather than just run, since round-trip and parity tests can pass while a transform silently no-ops: - Injecting an off-by-one into the `IndexOffset` patch fails **15 of 123** — the format tests are load-bearing. - Offsetting the cache's cell index by one fails **7 of 10** parity cases, and the 3 that stay green are exactly the ones that do not touch the cache. The layering localizes a fault rather than just reporting one.
572 lines
20 KiB
C#
572 lines
20 KiB
C#
/*************************************************************************
|
|
* ModernUO *
|
|
* Copyright 2019-2026 - ModernUO Development Team *
|
|
* Email: hi@modernuo.com *
|
|
* File: BitmapAStarAlgorithm.cs *
|
|
* *
|
|
* This program is free software: you can redistribute it and/or modify *
|
|
* it under the terms of the GNU General Public License as published by *
|
|
* the Free Software Foundation, either version 3 of the License, or *
|
|
* (at your option) any later version. *
|
|
* *
|
|
* You should have received a copy of the GNU General Public License *
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
|
************************************************************************/
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Runtime.CompilerServices;
|
|
using Server.Engines.Pathing;
|
|
using Server.Engines.Pathing.Cache;
|
|
using Server.Mobiles;
|
|
using Server.Systems.FeatureFlags;
|
|
using CalcMoves = Server.Movement.Movement;
|
|
using MoveImpl = Server.Movement.MovementImpl;
|
|
|
|
namespace Server.PathAlgorithms;
|
|
|
|
/// <summary>
|
|
/// A* pathfinder that expands a cell with a single <see cref="StepCache.TryGetMask"/> lookup,
|
|
/// which returns all 8 directions' walkability and destination Zs at once. Where the cache can't
|
|
/// answer — a fallthrough on that cell, or a flying creature the static cache can't model —
|
|
/// <see cref="GetSuccessorsSlowPath"/> runs the per-direction <see cref="CalcMoves.CheckMovement"/>
|
|
/// loop for that one cell instead, so a partial cache miss costs only the cells it affects.
|
|
/// </summary>
|
|
public class BitmapAStarAlgorithm : PathAlgorithm
|
|
{
|
|
private struct PathNode
|
|
{
|
|
public int cost;
|
|
public int total;
|
|
public int parent;
|
|
public int z;
|
|
}
|
|
|
|
private const int AreaSize = 38;
|
|
|
|
private const int NodeCount = AreaSize * AreaSize * PlaneCount;
|
|
|
|
private const int PlaneOffset = 128;
|
|
private const int PlaneCount = 13;
|
|
private const int PlaneHeight = 20;
|
|
// The shared default. A differently-configured variant is just another instance.
|
|
public static readonly BitmapAStarAlgorithm Instance = new();
|
|
|
|
// Scratch reused across every Find on this instance — roughly 320 KB of it, so create
|
|
// instances once and hold them, never per call. Per-instance rather than static so two
|
|
// differently-configured algorithms don't share state. Reuse is safe because the game loop is
|
|
// single-threaded and Find never re-enters.
|
|
private readonly Direction[] _path = new Direction[AreaSize * AreaSize];
|
|
private readonly PathNode[] _nodes = new PathNode[NodeCount];
|
|
private readonly byte[] _nodeStates = new byte[NodeCount];
|
|
private readonly int[] _successors = new int[8];
|
|
private readonly PriorityQueue<int, int> _openQueue = new();
|
|
|
|
// Expansion budget: the search gives up and returns null past this many nodes. It bounds the
|
|
// cost of an unreachable goal, which would otherwise exhaust the whole search window. A
|
|
// successful search stops when it finds the goal, so the budget only binds on hard or hopeless
|
|
// routes — it needs to stay high enough to solve walled-off indoor ones.
|
|
public int MaxSearchNodes { get; set; } = 1000;
|
|
|
|
private int _xOffset;
|
|
private int _yOffset;
|
|
|
|
// Every expansion goes to the slow path: the creature can fly, and arbitrary Z-jumping is
|
|
// outside what a static cache can model.
|
|
private bool _currentMobileNeedsSlowPath;
|
|
|
|
// Diagonal corner-cut uses the strict rule — both cardinal partners walkable, not just one.
|
|
// Non-GM players only. The cache still applies; the partner bits are in the same mask byte.
|
|
private bool _currentMobilePlayerStrict;
|
|
|
|
// Capability overlay on the cache's two rule sets, applied per cell as
|
|
// effective = (walkMask & !cantWalk) | (wetMask & canSwim)
|
|
private bool _currentMobileCanSwim;
|
|
private bool _currentMobileCantWalk;
|
|
|
|
// Per-mobile flags for the dynamic-obstacle pass, derived once in Find rather than per cell.
|
|
private bool _currentMobileIgnoreDoors;
|
|
private bool _currentMobileIgnoreSpellFields;
|
|
private bool _currentMobileIgnoreMovableImpassables;
|
|
|
|
public static void Configure()
|
|
{
|
|
// Shard-tunable expansion budget for the shared instance; see MaxSearchNodes. Written back
|
|
// to server.cfg on first boot so it's discoverable.
|
|
Instance.MaxSearchNodes = ServerConfiguration.GetOrUpdateSetting(
|
|
"pathfinding.maxSearchNodes",
|
|
1000
|
|
);
|
|
}
|
|
|
|
private Point3D _goal;
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public int Heuristic(int x, int y, int z)
|
|
{
|
|
x -= _goal.X - _xOffset;
|
|
y -= _goal.Y - _yOffset;
|
|
z -= _goal.Z;
|
|
|
|
x *= 11;
|
|
y *= 11;
|
|
|
|
return x * x + y * y + z * z;
|
|
}
|
|
|
|
public override bool CheckCondition(Mobile m, Map map, Point3D start, Point3D goal) =>
|
|
Utility.InRange(start, goal, AreaSize);
|
|
|
|
public override Direction[] Find(Mobile m, Map map, Point3D start, Point3D goal)
|
|
{
|
|
if (!Utility.InRange(start, goal, AreaSize))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
// The frontier probes a given chunk dozens of times over one search; opening a generation
|
|
// is what makes the cache's promotion gate count all of that as a single touch.
|
|
StepCache.Instance.BeginFindGeneration();
|
|
|
|
PathfindRecorder.RecordIfEnabled(m, map, start, goal);
|
|
|
|
_currentMobileNeedsSlowPath = RequiresSlowPath(m);
|
|
_currentMobilePlayerStrict = m.Player && m.AccessLevel < AccessLevel.GameMaster;
|
|
if (m is BaseCreature creature)
|
|
{
|
|
_currentMobileCanSwim = creature.CanSwim;
|
|
_currentMobileCantWalk = creature.CantWalk;
|
|
_currentMobileIgnoreDoors = creature.CanOpenDoors;
|
|
_currentMobileIgnoreMovableImpassables = creature.CanMoveOverObstacles;
|
|
}
|
|
else
|
|
{
|
|
_currentMobileCanSwim = false;
|
|
_currentMobileCantWalk = false;
|
|
_currentMobileIgnoreDoors = false;
|
|
_currentMobileIgnoreMovableImpassables = false;
|
|
}
|
|
|
|
// Dead and spectral mobiles pass through doors too. Mirrors MovementImpl.
|
|
_currentMobileIgnoreDoors |= !m.Alive || m.Body.BodyID == 0x3DB || m.IsDeadBondedPet;
|
|
_currentMobileIgnoreSpellFields = m is PlayerMobile && map != Map.Felucca;
|
|
|
|
Array.Clear(_nodeStates);
|
|
|
|
_goal = goal;
|
|
|
|
_xOffset = (start.X + goal.X - AreaSize) / 2;
|
|
_yOffset = (start.Y + goal.Y - AreaSize) / 2;
|
|
|
|
var fromNode = GetIndex(start.X, start.Y, start.Z);
|
|
var destNode = GetIndex(goal.X, goal.Y, goal.Z);
|
|
|
|
_nodes[fromNode].cost = 0;
|
|
_nodes[fromNode].total = Heuristic(start.X - _xOffset, start.Y - _yOffset, start.Z);
|
|
_nodes[fromNode].parent = -1;
|
|
_nodes[fromNode].z = start.Z;
|
|
|
|
_openQueue.Enqueue(fromNode, _nodes[fromNode].total);
|
|
_nodeStates[fromNode] = 1;
|
|
|
|
var bc = m as BaseCreature;
|
|
|
|
int backtrack = 0, depth = 0;
|
|
|
|
var path = _path;
|
|
|
|
while (_openQueue.Count > 0)
|
|
{
|
|
if (++depth > MaxSearchNodes)
|
|
{
|
|
break;
|
|
}
|
|
|
|
if (!_openQueue.TryDequeue(out var bestNode, out var bestTotal))
|
|
{
|
|
break;
|
|
}
|
|
|
|
// Duplicate, lower priority
|
|
if (_nodeStates[bestNode] == 2 || _nodes[bestNode].total != bestTotal)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
_nodeStates[bestNode] = 2;
|
|
|
|
// MovementImpl reads these statics, so a slow-path fallthrough on any cell below needs
|
|
// them set for this mobile.
|
|
if (bc != null)
|
|
{
|
|
MoveImpl.AlwaysIgnoreDoors = bc.CanOpenDoors;
|
|
MoveImpl.IgnoreMovableImpassables = bc.CanMoveOverObstacles;
|
|
}
|
|
|
|
MoveImpl.Goal = goal;
|
|
|
|
var vals = _successors;
|
|
var count = GetSuccessors(bestNode, m, map);
|
|
|
|
MoveImpl.AlwaysIgnoreDoors = false;
|
|
MoveImpl.IgnoreMovableImpassables = false;
|
|
MoveImpl.Goal = Point3D.Zero;
|
|
|
|
if (count == 0)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
for (var i = 0; i < count; ++i)
|
|
{
|
|
var newNode = vals[i];
|
|
|
|
// Skip if the node is already closed
|
|
if (_nodeStates[newNode] == 2)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var isDiagonal = i % 2 == 1;
|
|
var moveCost = isDiagonal ? 14 : 10;
|
|
var newCost = _nodes[bestNode].cost + moveCost;
|
|
var newTotal = newCost + Heuristic(
|
|
newNode % AreaSize,
|
|
newNode / AreaSize % AreaSize,
|
|
_nodes[newNode].z
|
|
);
|
|
|
|
if (_nodeStates[newNode] == 0 || newTotal < _nodes[newNode].total)
|
|
{
|
|
_nodes[newNode].parent = bestNode;
|
|
_nodes[newNode].cost = newCost;
|
|
_nodes[newNode].total = newTotal;
|
|
|
|
// Requeue (duplicates allowed), and mark as open
|
|
_openQueue.Enqueue(newNode, newTotal);
|
|
_nodeStates[newNode] = 1;
|
|
}
|
|
|
|
if (newNode != destNode)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var pathCount = 0;
|
|
var parent = _nodes[newNode].parent;
|
|
|
|
while (parent != -1)
|
|
{
|
|
path[pathCount++] = GetDirection(
|
|
parent % AreaSize,
|
|
parent / AreaSize % AreaSize,
|
|
newNode % AreaSize,
|
|
newNode / AreaSize % AreaSize
|
|
);
|
|
newNode = parent;
|
|
parent = _nodes[newNode].parent;
|
|
|
|
if (newNode == fromNode)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
var dirs = new Direction[pathCount];
|
|
|
|
while (pathCount > 0)
|
|
{
|
|
dirs[backtrack++] = path[--pathCount];
|
|
}
|
|
|
|
_openQueue.Clear();
|
|
_currentMobileNeedsSlowPath = false;
|
|
_currentMobilePlayerStrict = false;
|
|
_currentMobileCanSwim = false;
|
|
_currentMobileCantWalk = false;
|
|
_currentMobileIgnoreDoors = false;
|
|
_currentMobileIgnoreSpellFields = false;
|
|
_currentMobileIgnoreMovableImpassables = false;
|
|
return dirs;
|
|
}
|
|
}
|
|
|
|
_openQueue.Clear();
|
|
_currentMobileNeedsSlowPath = false;
|
|
_currentMobilePlayerStrict = false;
|
|
_currentMobileCanSwim = false;
|
|
_currentMobileCantWalk = false;
|
|
_currentMobileIgnoreDoors = false;
|
|
_currentMobileIgnoreSpellFields = false;
|
|
_currentMobileIgnoreMovableImpassables = false;
|
|
return null;
|
|
}
|
|
|
|
private int GetIndex(int x, int y, int z)
|
|
{
|
|
x -= _xOffset;
|
|
y -= _yOffset;
|
|
z += PlaneOffset;
|
|
z /= PlaneHeight;
|
|
|
|
return x + y * AreaSize + z * AreaSize * AreaSize;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Expands one cell into its walkable neighbours. A single cache lookup covers all 8
|
|
/// directions, including the partner bits the diagonal corner-cut needs, so no neighbouring
|
|
/// cell has to be consulted. Falls back to <see cref="GetSuccessorsSlowPath"/> for this cell
|
|
/// alone when the cache can't answer.
|
|
/// </summary>
|
|
private int GetSuccessors(int p, Mobile m, Map map)
|
|
{
|
|
var px = p % AreaSize;
|
|
var py = p / AreaSize % AreaSize;
|
|
var pz = _nodes[p].z;
|
|
|
|
var p3D = new Point3D(px + _xOffset, py + _yOffset, pz);
|
|
|
|
var vals = _successors;
|
|
|
|
if (_currentMobileNeedsSlowPath || !ContentFeatureFlags.BitmapPathfindingCache)
|
|
{
|
|
return GetSuccessorsSlowPath(m, map, px, py, p3D, vals);
|
|
}
|
|
|
|
var count = 0;
|
|
|
|
var lookup = StepCache.Instance.TryGetMask(map, p3D.X, p3D.Y, (sbyte)p3D.Z);
|
|
|
|
if (!lookup.IsHit)
|
|
{
|
|
// A multi-covered cell still gets a whole-cell mask, synthesized over the house or boat
|
|
// components rather than looked up. That keeps it out of the slow path's 8 separate
|
|
// CheckMovement calls, and the result flows through the same overlay, corner-cut and
|
|
// dynamic-obstacle logic below as a cache hit would.
|
|
if (lookup.HitKind == CacheHitKind.Fallthrough_Multi)
|
|
{
|
|
lookup = MultiMaskCache.Instance.GetMask(map, p3D.X, p3D.Y, (sbyte)p3D.Z);
|
|
}
|
|
else
|
|
{
|
|
return GetSuccessorsSlowPath(m, map, px, py, p3D, vals);
|
|
}
|
|
}
|
|
|
|
// Overlay the mobile's capabilities onto the cache's two rule sets. The corner-cut below
|
|
// reads its partner bits from this effective mask, not the raw one.
|
|
var walkBits = _currentMobileCantWalk ? (byte)0 : lookup.WalkMask;
|
|
var swimBits = _currentMobileCanSwim ? lookup.WetMask : (byte)0;
|
|
var mask = (byte)(walkBits | swimBits);
|
|
|
|
for (var i = 0; i < 8; ++i)
|
|
{
|
|
var x = px;
|
|
var y = py;
|
|
CalcMoves.Offset((Direction)i, ref x, ref y);
|
|
|
|
if (x is < 0 or >= AreaSize || y is < 0 or >= AreaSize)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if ((mask & (1 << i)) == 0)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
// Diagonal corner-cut: a creature needs at least one of the two flanking cardinals to
|
|
// be walkable, a non-GM player needs both.
|
|
if ((i & 1) == 1)
|
|
{
|
|
var leftBit = 1 << ((i - 1) & 0x7);
|
|
var rightBit = 1 << ((i + 1) & 0x7);
|
|
if (_currentMobilePlayerStrict
|
|
? (mask & leftBit) == 0 || (mask & rightBit) == 0
|
|
: (mask & leftBit) == 0 && (mask & rightBit) == 0)
|
|
{
|
|
continue;
|
|
}
|
|
}
|
|
|
|
// Walking wins over swimming where both are possible. MovementImpl picks the surface
|
|
// closest to the start Z, and for a creature standing on land that is always the walk
|
|
// surface.
|
|
var useWalkZ = (walkBits & (1 << i)) != 0;
|
|
var z = useWalkZ
|
|
? i switch
|
|
{
|
|
0 => lookup.WalkZ_N,
|
|
1 => lookup.WalkZ_NE,
|
|
2 => lookup.WalkZ_E,
|
|
3 => lookup.WalkZ_SE,
|
|
4 => lookup.WalkZ_S,
|
|
5 => lookup.WalkZ_SW,
|
|
6 => lookup.WalkZ_W,
|
|
7 => lookup.WalkZ_NW,
|
|
_ => (sbyte)0
|
|
}
|
|
: i switch
|
|
{
|
|
0 => lookup.SwimZ_N,
|
|
1 => lookup.SwimZ_NE,
|
|
2 => lookup.SwimZ_E,
|
|
3 => lookup.SwimZ_SE,
|
|
4 => lookup.SwimZ_S,
|
|
5 => lookup.SwimZ_SW,
|
|
6 => lookup.SwimZ_W,
|
|
7 => lookup.SwimZ_NW,
|
|
_ => (sbyte)0
|
|
};
|
|
|
|
var absX = x + _xOffset;
|
|
var absY = y + _yOffset;
|
|
|
|
// The cache only knows static terrain, so items and mobiles at the target cell have to
|
|
// be checked live.
|
|
if (IsBlockedByDynamic(m, map, absX, absY, z))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var idx = GetIndex(absX, absY, z);
|
|
|
|
if (idx >= 0 && idx < NodeCount)
|
|
{
|
|
_nodes[idx].z = z;
|
|
vals[count++] = idx;
|
|
}
|
|
}
|
|
|
|
return count;
|
|
}
|
|
|
|
private const int PersonHeightConst = 16;
|
|
private const int MobileHeight = 15;
|
|
|
|
/// <summary>
|
|
/// MovementImpl's item and mobile collision phase for one target cell: impassable items
|
|
/// overlapping the mobile's vertical envelope block it, subject to the capability overrides,
|
|
/// as does any other mobile it can't move over.
|
|
/// </summary>
|
|
private bool IsBlockedByDynamic(Mobile m, Map map, int x, int y, int z)
|
|
{
|
|
var ourTop = z + PersonHeightConst;
|
|
|
|
foreach (var item in map.GetItemsAt(x, y))
|
|
{
|
|
var itemData = item.ItemData;
|
|
if (!itemData.ImpassableSurface)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (_currentMobileIgnoreMovableImpassables && item.Movable)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var itemId = item.ItemID & TileData.MaxItemValue;
|
|
if (_currentMobileIgnoreDoors
|
|
&& (itemData.Door
|
|
|| itemId is 0x692 or 0x846 or 0x873
|
|
|| itemId >= 0x6F5 && itemId <= 0x6F6))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (_currentMobileIgnoreSpellFields && itemId is 0x82 or 0x3946 or 0x3956)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var checkZ = item.Z;
|
|
var checkTop = checkZ + itemData.CalcHeight;
|
|
if (checkTop > z && ourTop > checkZ)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
// The goal cell is usually occupied by whatever the mobile is chasing, so blocking on it
|
|
// would fail every pursuit. The follower stops short of the goal anyway. Every other cell
|
|
// still blocks on mobiles.
|
|
var skipMobCheck = x == MoveImpl.Goal.X && y == MoveImpl.Goal.Y;
|
|
|
|
if (!skipMobCheck)
|
|
{
|
|
foreach (var mob in map.GetMobilesAt(x, y))
|
|
{
|
|
if (mob == m)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (mob.Z + MobileHeight > z && z + MobileHeight > mob.Z && !CanMoveOver(m, mob))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// True when m can step onto t's cell — a corpse, hidden staff, and so on. Mirrors
|
|
/// MovementImpl.CanMoveOver.
|
|
/// </summary>
|
|
private static bool CanMoveOver(Mobile m, Mobile t) =>
|
|
!t.Alive || !m.Alive || t.IsDeadBondedPet || m.IsDeadBondedPet
|
|
|| t.Hidden && t.AccessLevel > AccessLevel.Player;
|
|
|
|
/// <summary>
|
|
/// Expands one cell the long way, with a CheckMovement call per direction. The same-cell
|
|
/// mobile check is layered on top because MovementImpl doesn't iterate those.
|
|
/// </summary>
|
|
private int GetSuccessorsSlowPath(Mobile m, Map map, int px, int py, Point3D p3D, int[] vals)
|
|
{
|
|
var count = 0;
|
|
|
|
for (var i = 0; i < 8; ++i)
|
|
{
|
|
var x = px;
|
|
var y = py;
|
|
CalcMoves.Offset((Direction)i, ref x, ref y);
|
|
|
|
if (x is < 0 or >= AreaSize || y is < 0 or >= AreaSize)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (!CalcMoves.CheckMovement(m, map, p3D, (Direction)i, out var z))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var absX = x + _xOffset;
|
|
var absY = y + _yOffset;
|
|
if (IsBlockedByDynamic(m, map, absX, absY, z))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var idx = GetIndex(absX, absY, z);
|
|
if (idx >= 0 && idx < NodeCount)
|
|
{
|
|
_nodes[idx].z = z;
|
|
vals[count++] = idx;
|
|
}
|
|
}
|
|
|
|
return count;
|
|
}
|
|
|
|
/// <summary>
|
|
/// True for creatures the static cache can't model at all. Only flying ones qualify: they
|
|
/// Z-jump freely, so the cache's source-Z guard would reject nearly every cell anyway. Swim
|
|
/// and cant-walk are handled by the capability overlay, and the door / obstacle capabilities
|
|
/// only affect dynamic items, so none of those disqualify the cache.
|
|
/// </summary>
|
|
private static bool RequiresSlowPath(Mobile m) => m is BaseCreature bc && bc.CanFly;
|
|
}
|