feat(stdlib): add Grid.* — tile geometry + A* pathfinding over the tilemap (#24)
All checks were successful
bootstrap / cfree-fixpoint (push) Successful in 16s
ci / build-and-test (push) Successful in 1m9s
commit-lint / conventional-commits (push) Successful in 3s
docs / build-and-deploy (push) Successful in 18s

Grid.* operates on the Map tilemap (Map.size/Map.row): a cell is passable unless
it is out of bounds or holds the caller's `wall` tile (a char code, e.g. '#'), so
any impassable glyph works. Everything is integer and deterministic.

  - Grid.line(x0,y0,x1,y1) -> []Cell        Bresenham line cells (LOS/raycast base)
  - Grid.blocked(x,y,wall) -> bool          the shared passability test
  - Grid.line_of_sight(x0,y0,x1,y1,wall)    unobstructed straight line?
  - Grid.flood(x,y,wall) -> []Cell          4-connected reachable region (BFS)
  - Grid.a_star(x0,y0,x1,y1,wall) -> []Cell shortest 4-connected path (A*,
                                            Manhattan heuristic), empty if unreachable

The engine (runtime/native/grid.ludic, ~150 lines of Ludic, C-free) is spliced
into a game via core.ludic since it reads the tilemap runtime; returned Cell
slices are ordinary Ludic slices (`len` / `[i]`; each cell has `.x` `.y`).
Pathfinding lives under Grid rather than a `Path` namespace — that name is
already the filesystem-paths library (#10).

Verified against Python references: a 1500-case fuzzer over random maps agrees
exactly on A* path length (optimal, == BFS), flood-fill count, and line-of-sight.
examples/library/grid.ludic asserts the behaviour and is wired into `x test`
(now 61 passed); docs: a Grid section + 5 per-symbol pages, inventory/coverage
green. Seed reseeded; the C-free bootstrap fixpoint holds.

Scope: this lands the Grid.*/pathfinding half of #24. The ECS Query.* helpers
(count/first, and nearest/within which want a runtime spatial index) remain the
tracked follow-up the issue calls out as blocked.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Orkun ÇAKILKAYA 2026-08-31 12:36:12 +03:00
parent b798e3024e
commit 07e5a20c0e
13 changed files with 4852 additions and 3835 deletions

View file

@ -0,0 +1,7 @@
---
id: grid
title: Grid
order: 27
---
Tile geometry and pathfinding over the <a href="map"><code>Map</code></a> tilemap. A cell is passable unless it is out of bounds or holds the caller's <code>wall</code> tile (a char code, e.g. <code>'#'</code>), so any impassable glyph works. Everything is integer and deterministic — same map and query reproduce the same path every run. <a href="grid-line"><code>Grid.line</code></a>/<a href="grid-flood"><code>Grid.flood</code></a>/<a href="grid-a_star"><code>Grid.a_star</code></a> return <code>Cell</code> slices (index them with <code>len</code> / <code>[i]</code>; each cell has <code>.x</code> and <code>.y</code>).

View file

@ -0,0 +1,28 @@
---
id: grid-a_star
name: Grid.a_star
category: grid
kind: namespace-method
tokens: Grid.a_star
sig: Grid.a_star(x0, y0, x1, y1, wall) -> []Cell
tip: The shortest 4-connected path between two cells (A*), or an empty list.
order: 5
ns: Grid
member: a_star
---
Returns the shortest path from <code>(x0, y0)</code> to <code>(x1, y1)</code> over passable (non-<code>wall</code>) cells, 4-connected with uniform step cost, as a <code>Cell</code> slice from start to goal inclusive — an <strong>A*</strong> search with a Manhattan heuristic. Empty if the goal is unreachable (or start/goal is a wall). (Named under <code>Grid</code> rather than <code>Path</code>, which is the filesystem-paths library.)
Parameters:
- `x0`, `y0` — the start cell
- `x1`, `y1` — the goal cell
- `wall` — the impassable tile char, e.g. `'#'`
```ludic
program Demo {
handler H phase Update {
let path = Grid.a_star(1, 1, 20, 12, '#')
if len(path) > 0 { print(len(path)) }
}
}
```

View file

@ -0,0 +1,26 @@
---
id: grid-blocked
name: Grid.blocked
category: grid
kind: namespace-method
tokens: Grid.blocked
sig: Grid.blocked(x, y, wall) -> bool
tip: True if the cell is out of bounds or holds the wall tile.
order: 2
ns: Grid
member: blocked
---
Whether <code>(x, y)</code> blocks movement — <code>true</code> if it is out of bounds or its tile equals <code>wall</code>. The passability test the other Grid functions share.
Parameters:
- `x`, `y` — the cell
- `wall` — the impassable tile char, e.g. `'#'`
```ludic
program Demo {
handler H phase Update {
if Grid.blocked(0, 0, '#') { print(1) }
}
}
```

View file

@ -0,0 +1,27 @@
---
id: grid-flood
name: Grid.flood
category: grid
kind: namespace-method
tokens: Grid.flood
sig: Grid.flood(x, y, wall) -> []Cell
tip: Every passable cell reachable from (x,y), 4-connected, in BFS order.
order: 4
ns: Grid
member: flood
---
Returns every passable cell reachable from <code>(x, y)</code> by 4-connected steps (up/down/left/right over non-<code>wall</code> cells), in breadth-first order — for connectivity checks, filling a room, or measuring an enclosed area. Empty if the start itself is blocked.
Parameters:
- `x`, `y` — the seed cell
- `wall` — the impassable tile char
```ludic
program Demo {
handler H phase Update {
let region = Grid.flood(2, 2, '#')
print(len(region))
}
}
```

View file

@ -0,0 +1,27 @@
---
id: grid-line
name: Grid.line
category: grid
kind: namespace-method
tokens: Grid.line
sig: Grid.line(x0, y0, x1, y1) -> []Cell
tip: Every cell a straight line from (x0,y0) to (x1,y1) crosses (Bresenham).
order: 1
ns: Grid
member: line
---
Returns the cells a straight line from <code>(x0, y0)</code> to <code>(x1, y1)</code> passes through, endpoints included, using integer Bresenham — the basis of line-of-sight, ray-casting, and drawing on the grid.
Parameters:
- `x0`, `y0` — the start cell
- `x1`, `y1` — the end cell
```ludic
program Demo {
handler H phase Update {
let cells = Grid.line(0, 0, 5, 3)
print(len(cells))
}
}
```

View file

@ -0,0 +1,27 @@
---
id: grid-line_of_sight
name: Grid.line_of_sight
category: grid
kind: namespace-method
tokens: Grid.line_of_sight
sig: Grid.line_of_sight(x0, y0, x1, y1, wall) -> bool
tip: True if the straight line between two cells crosses no wall.
order: 3
ns: Grid
member: line_of_sight
---
Whether an unobstructed straight line connects <code>(x0, y0)</code> and <code>(x1, y1)</code> — <code>true</code> when no cell it crosses (endpoints included) is a <code>wall</code>. Use it for visibility, aggro checks, and cover.
Parameters:
- `x0`, `y0` — the viewer cell
- `x1`, `y1` — the target cell
- `wall` — the impassable tile char
```ludic
program Demo {
handler H phase Update {
if Grid.line_of_sight(1, 1, 8, 1, '#') { print(1) }
}
}
```