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>
62 lines
2 KiB
Text
62 lines
2 KiB
Text
# grid.ludic — Grid.* tile geometry and pathfinding over the Map tilemap. A
|
|
# tiny map with a wall maze; each assertion that holds prints its number, so a
|
|
# full run prints: 1 2 3 4 5 6 7 8 9 10 11 12 13
|
|
# Pathfinding is A* (runtime/native/grid.ludic), 4-connected, `'#'` = wall.
|
|
program Grid {
|
|
property Tag { v: int = 0 }
|
|
model Marker { Tag }
|
|
|
|
handler Boot phase Start {
|
|
# 01234567
|
|
Map.size(8, 5)
|
|
Map.row(0, "########")
|
|
Map.row(1, "#......#")
|
|
Map.row(2, "#.####.#")
|
|
Map.row(3, "#....#.#")
|
|
Map.row(4, "########")
|
|
|
|
# --- Bresenham line ---
|
|
let hl = Grid.line(1, 1, 6, 1)
|
|
if len(hl) == 6 { print(1) }
|
|
let dl = Grid.line(0, 0, 3, 3)
|
|
if len(dl) == 4 { print(2) }
|
|
|
|
# --- line of sight (a clear corridor vs one crossing a wall) ---
|
|
if Grid.line_of_sight(1, 1, 6, 1, '#') { print(3) }
|
|
if not Grid.line_of_sight(1, 1, 4, 3, '#') { print(4) }
|
|
|
|
# --- flood fill: every floor cell reachable from (1,1) ---
|
|
if len(Grid.flood(1, 1, '#')) == 13 { print(5) }
|
|
|
|
# --- A* shortest path around the maze ---
|
|
let p = Grid.a_star(1, 1, 6, 3, '#')
|
|
if len(p) == 8 { print(6) }
|
|
if p[0].x == 1 and p[0].y == 1 { print(7) }
|
|
let last = p[len(p) - 1]
|
|
if last.x == 6 and last.y == 3 { print(8) }
|
|
|
|
# the path is contiguous (each step 4-adjacent) and never crosses a wall
|
|
var ok = true
|
|
var i = 1
|
|
while i < len(p) {
|
|
let dx = p[i].x - p[i - 1].x
|
|
let dy = p[i].y - p[i - 1].y
|
|
var ad = dx
|
|
if ad < 0 { ad = 0 - ad }
|
|
var ay = dy
|
|
if ay < 0 { ay = 0 - ay }
|
|
if ad + ay != 1 { ok = false }
|
|
if Grid.blocked(p[i].x, p[i].y, '#') { ok = false }
|
|
i = i + 1
|
|
}
|
|
if ok { print(9) }
|
|
|
|
# --- unreachable / blocked cases ---
|
|
if len(Grid.a_star(1, 1, 3, 2, '#')) == 0 { print(10) } # (3,2) is a wall
|
|
if Grid.blocked(3, 2, '#') { print(11) }
|
|
if not Grid.blocked(1, 1, '#') { print(12) }
|
|
if Grid.blocked(0 - 1, 0, '#') { print(13) } # out of bounds
|
|
}
|
|
|
|
handler Run phase Update { quit() }
|
|
}
|