feat(stdlib): add Grid.* — tile geometry + A* pathfinding over the tilemap (#24)
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:
parent
b798e3024e
commit
07e5a20c0e
13 changed files with 4852 additions and 3835 deletions
28
docs/language/grid/grid-a_star.md
Normal file
28
docs/language/grid/grid-a_star.md
Normal 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)) }
|
||||
}
|
||||
}
|
||||
```
|
||||
Loading…
Add table
Add a link
Reference in a new issue