Skip to content

Add Manhattan/Chebyshev distance and line-of-sight public API#24

Merged
migus88 merged 2 commits into
masterfrom
feature/distance-line-of-sight-api
Jun 24, 2026
Merged

Add Manhattan/Chebyshev distance and line-of-sight public API#24
migus88 merged 2 commits into
masterfrom
feature/distance-line-of-sight-api

Conversation

@migus88

@migus88 migus88 commented Jun 24, 2026

Copy link
Copy Markdown
Owner

What

Adds three lightweight spatial queries to Pathfinder's public API for range and visibility logic that doesn't require computing a full path:

API Kind Description
static int GetManhattanDistance(Coordinate from, Coordinate to) pure Taxicab distance |dx| + |dy| (the A* heuristic; cardinal-only grids).
static int GetChebyshevDistance(Coordinate from, Coordinate to) pure Chessboard distance max(|dx|, |dy|) (8-directional grids).
bool HasLineOfSight(Coordinate from, Coordinate to, LineOfSightMode mode = BlockedByUnwalkableCells) instance Whether an unobstructed straight line connects two cells.

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 GetPath call or rolling your own metric.

Line-of-sight modes

HasLineOfSight takes an optional LineOfSightMode so callers can decide whether obstacles that block movement also block vision (MPath models terrain with a single IsWalkable flag):

Mode Unwalkable cells Occupied cells (when IsCalculatingOccupiedCells)
BlockedByUnwalkableCells (default) block sight block sight
IgnoreUnwalkableCells transparent (water, pits, glass walls) block sight

The mode governs walkability only — occupancy stays orthogonal, so "ignore terrain, but units still block the shot" is expressible.

Implementation notes

  • New Pathfinder_Geometry.cs partial + LineOfSightMode enum under the shared src/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 no UnityEngine references).
  • Distance metrics are static, pure, allocation-free integer functions — they ignore walls, weights and settings and return the geometric grid distance, not a traversal cost.
  • HasLineOfSight reuses the existing Bresenham line-tracing already used by string-pulling smoothing, now sharing a single mode-aware IsLineOfSightBlocked per-cell test (smoothing always passes BlockedByUnwalkableCells, 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 throws ArgumentException.
  • No new settings, so nothing to thread through 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.csproj100 passed, 0 failed, including 21 cases in PathfinderGeometryTests (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 to docs/README.md index).
  • README.md — feature bullet + quick-start snippet.
  • CLAUDE.md — architecture note for the new partial and enum.

🤖 Generated with Claude Code

migus88 and others added 2 commits June 24, 2026 23:11
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>
@migus88 migus88 merged commit b23a5a6 into master Jun 24, 2026
1 check passed
@migus88 migus88 deleted the feature/distance-line-of-sight-api branch June 24, 2026 20:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant