Add Manhattan/Chebyshev distance and line-of-sight public API#24
Merged
Conversation
Expose three lightweight spatial queries on Pathfinder for range and visibility logic that doesn't need a full path: - GetManhattanDistance / GetChebyshevDistance: static, pure, allocation-free integer grid metrics over Coordinate (the heuristic and 8-dir metrics). - HasLineOfSight: instance Bresenham visibility test that reuses the existing line-tracing logic from string-pulling smoothing. A cell between the endpoints blocks sight when not walkable, or occupied when IsCalculatingOccupiedCells is enabled; endpoints are never tested and the ray is single-cell (agent size ignored). Lives in a new Pathfinder_Geometry.cs partial under the shared Unity Source folder (consumed by both the NuGet and Unity packages). Adds PathfinderGeometryTests (17 cases) plus API reference, a new guide, and README/CLAUDE updates. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
HasLineOfSight gains an optional LineOfSightMode parameter: - BlockedByUnwalkableCells (default): non-walkable cells block sight (unchanged behavior). - IgnoreUnwalkableCells: non-walkable cells are transparent — for obstacles that block movement but not vision (water, pits, glass). The mode governs walkability only; occupancy stays orthogonal, so an occupied cell still blocks under either mode when IsCalculatingOccupiedCells is enabled. The per-cell test is factored into IsLineOfSightBlocked, shared with string-pulling smoothing (which always uses BlockedByUnwalkableCells). Adds 4 tests (100 total) and documents the new enum (API page, guide, README, CLAUDE). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds three lightweight spatial queries to
Pathfinder's public API for range and visibility logic that doesn't require computing a full path:static int GetManhattanDistance(Coordinate from, Coordinate to)|dx| + |dy|(the A* heuristic; cardinal-only grids).static int GetChebyshevDistance(Coordinate from, Coordinate to)max(|dx|, |dy|)(8-directional grids).bool HasLineOfSight(Coordinate from, Coordinate to, LineOfSightMode mode = BlockedByUnwalkableCells)Why
These are common needs in tactics/strategy and action games — range checks, AI target sorting, fog-of-war, ranged attacks, or short-circuiting pathfinding when a target is in plain sight — that previously required either a full
GetPathcall or rolling your own metric.Line-of-sight modes
HasLineOfSighttakes an optionalLineOfSightModeso callers can decide whether obstacles that block movement also block vision (MPath models terrain with a singleIsWalkableflag):IsCalculatingOccupiedCells)BlockedByUnwalkableCells(default)IgnoreUnwalkableCellsThe mode governs walkability only — occupancy stays orthogonal, so "ignore terrain, but units still block the shot" is expressible.
Implementation notes
Pathfinder_Geometry.cspartial +LineOfSightModeenum under the sharedsrc/mpath-unity-project/Packages/MPath/Source/folder, so both the NuGet (Migs.MPath) and Unity (com.migsweb.mpath) packages pick them up. Kept engine-agnostic (Source/has noUnityEnginereferences).static, pure, allocation-free integer functions — they ignore walls, weights and settings and return the geometric grid distance, not a traversal cost.HasLineOfSightreuses the existing Bresenham line-tracing already used by string-pulling smoothing, now sharing a single mode-awareIsLineOfSightBlockedper-cell test (smoothing always passesBlockedByUnwalkableCells, so paths still can't be shortcut through walls). Endpoints are never tested, the ray is single-cell (agent size ignored), a cell always sees itself, and an out-of-range coordinate throwsArgumentException.IPathfinderSettings/FastPathfinderSettings.Verification
dotnet build src/mpath-source/Migs.MPath.sln— succeeds (0 errors; only pre-existing warnings).dotnet test src/mpath-source/Migs.MPath.Tests/Migs.MPath.Tests.csproj— 100 passed, 0 failed, including 21 cases inPathfinderGeometryTests(distance correctness/symmetry/zero; LOS clear/diagonal/self, wall on vs. beside the line, endpoint exclusion, occupancy-setting behavior, the two modes incl. "ignore terrain but units still block", symmetry, repeated calls, invalid-coordinate validation).Docs
docs/api/Pathfinder.md— methods table + a "Distance and line of sight" section.docs/api/LineOfSightMode.md— new enum page (added to the API index).docs/guides/distance-and-line-of-sight.md— new guide (added todocs/README.mdindex).README.md— feature bullet + quick-start snippet.CLAUDE.md— architecture note for the new partial and enum.🤖 Generated with Claude Code