← tile_world · API reference · version 0.14.3
8 file(s), 2422 lines. This is the source the published version ships, and it is loft — the library is written in the same language you call it from.
Public items
A worked example is a call site the author chose as the thing to read first. Call sites are every line naming the item, found mechanically over the source with comments excluded — so an item with no worked example is not an item nobody uses.
| Item | Defined | Worked example | Call sites |
|---|---|---|---|
VOID const | 60 | — | 357 383 401 430 580 591 … (25 total) |
DARKEST const | 63 | — | 805 806 807 808 810 833 … (21 total) |
NORTH const | 66 | — | 465 488 1217 1249 49 54 … (26 total) |
EAST const | 67 | — | 488 1181 100 39 43 45 … (21 total) |
SOUTH const | 68 | — | 466 488 41 47 113 186 |
WEST const | 69 | — | 467 488 1181 1218 1250 50 … (27 total) |
NORTH_EAST const | 73 | — | 41 35 75 129 185 187 |
SOUTH_EAST const | 74 | — | 31 32 70 166 |
SOUTH_WEST const | 75 | — | 102 33 88 187 |
NORTH_WEST const | 76 | — | 531 1187 52 34 43 54 … (8 total) |
World struct | 152 | — | 166 167 175 185 208 221 … (85 total) |
world fn | 166 | — | 1054 1199 13 13 28 17 … (30 total) |
changes fn | 175 | — | 159 167 176 187 209 214 … (30 total) |
changed_since fn | 185 | — | 30 60 69 70 71 72 … (19 total) |
terrain_kind fn | 241 | — | 14 15 14 15 18 19 … (17 total) |
thing_kind fn | 251 | — | 16 16 112 20 21 24 … (13 total) |
edge_kind fn | 261 | — | 17 17 22 23 24 26 … (9 total) |
walkable_kind fn | 268 | — | 653 |
thing_height fn | 275 | — | 645 |
chunk_of fn | 284 | — | 290 345 346 356 366 421 … (15 total) |
inside fn | 289 | — | 358 368 423 449 457 572 … (9 total) |
chunk_count fn | 294 | — | 126 55 57 |
layers fn | 299 | — | 110 139 223 224 |
chunks fn | 311 | — | 153 167 295 301 313 347 … (24 total) |
stacks fn | 324 | — | 154 167 326 582 598 599 … (23 total) |
set_terrain fn | 354 | — | 416 38 53 68 83 124 … (33 total) |
clear fn | 365 | — | 45 57 131 30 |
is_set fn | 375 | — | 129 132 94 167 195 256 |
set_ground fn | 381 | — | 108 124 134 136 140 73 |
ground_layers fn | 388 | — | 138 |
ground fn | 399 | — | 429 569 916 917 132 133 … (10 total) |
ground_tile fn | 406 | — | 437 |
fill fn | 413 | — | 18 19 20 18 54 25 … (23 total) |
terrain fn | 427 | — | 591 653 666 683 990 1016 … (33 total) |
tile fn | 435 | — | 139 354 357 381 383 409 … (15 total) |
set_edge fn | 474 | — | 1217 1218 1249 1250 49 50 … (40 total) |
edge fn | 481 | — | 489 54 59 101 33 40 … (30 total) |
crossable fn | 495 | — | 725 56 58 59 60 62 |
see_through fn | 504 | — | 773 774 777 779 57 58 … (9 total) |
set_rise fn | 515 | — | 1214 1253 51 61 40 55 … (14 total) |
rise fn | 521 | — | 643 644 55 58 152 156 … (19 total) |
set_slant fn | 530 | — | 1215 1254 52 62 41 102 … (20 total) |
slant fn | 538 | — | 55 58 114 25 32 36 … (24 total) |
set_ceiling fn | 547 | — | 1216 1255 33 36 38 40 … (14 total) |
ceiling fn | 556 | — | 26 34 37 39 41 56 … (15 total) |
block fn | 567 | — | 70 71 73 84 85 106 |
open_sky fn | 590 | — | 685 78 127 135 |
place fn | 597 | — | 1065 1094 1224 1262 71 43 … (35 total) |
take fn | 608 | — | 44 58 96 73 74 75 … (7 total) |
things fn | 624 | — | 116 599 602 611 613 617 … (24 total) |
top fn | 633 | — | 983 127 92 108 109 167 … (12 total) |
height fn | 641 | — | 147 251 254 278 150 152 … (9 total) |
passable fn | 652 | — | 727 77 126 130 71 80 … (12 total) |
opaque fn | 665 | — | 123 131 145 241 244 252 … (18 total) |
lit fn | 682 | — | 813 900 907 953 73 76 … (7 total) |
shortest_path fn | 702 | TLW-001 | 72 75 69 72 82 84 … (14 total) |
line_of_sight fn | 756 | — | 78 101 104 112 114 78 … (11 total) |
shades fn | 802 | — | 73 76 92 43 51 53 … (19 total) |
reach fn | 846 | — | 135 168 173 175 |
view fn | 973 | — | 208 |
region fn | 1013 | — | 44 52 53 54 55 64 … (8 total) |
copy fn | 1052 | — | 85 120 92 85 103 111 |
paste fn | 1077 | TLW-002 | 87 122 125 94 89 107 … (8 total) |
stamp_box fn | 1120 | — | 1152 1203 159 163 |
stamp_size fn | 1151 | — | 160 |
stamp_spot fn | 1160 | — | 1212 1223 161 |
turned_side fn | 1179 | — | 1217 1218 186 |
turned_slant fn | 1186 | — | 1215 187 |
turned fn | 1198 | — | 160 162 163 166 179 183 … (19 total) |
lay fn | 1240 | TLW-002 | 242 254 |
src/tile_world.loft
// Copyright (c) 2026 Jurjen Stellingwerff
// SPDX-License-Identifier: LGPL-3.0-or-later
//
// tile_world — an unbounded, layered square-grid world, and the routines that answer
// questions about it: what is on a cell, how far it is to walk somewhere, what can be seen
// from where, and how much light falls on each cell. What the pieces of a game DO —
// pushing, climbing, picking up — is the game's: this library holds the world and answers.
//
// ── The memory model ─────────────────────────────────────────────────
//
// The world is a stack of LAYERS — `z` is the floor level, negative below the ground,
// positive above — and each layer is an unbounded 2-D grid. Like hex_world and moros's map,
// a layer is held in 32x32 CHUNKS that exist only where something was set, found through a
// hash keyed on (cx, cy, z), so a lookup costs the same in a world of ten chunks or ten
// million. Chunk coordinates are the floor of a coordinate over 32, so the world grows west
// and north of the origin as it does east and south.
//
// A cell's TERRAIN is two bytes: a kind (0..15) in the top four bits and a tile number (the
// picture, 0..4095) in the low twelve. A chunk is 2 KB. A cell nobody set reads as its
// layer's GROUND (`set_ground`: open land that costs no memory), or void when the layer has
// none; a cell set to void stays void (a pit in the ground), and `clear` makes it unset again.
//
// A cell can also carry its two EDGES — the north and the west one; the east and the south
// edge are its neighbours' west and north — and a RISE, its own height in steps (a hillside).
// They share one 32-bit word a cell, in an array a chunk gets only when one of its cells first
// has an edge or a rise. The same word holds the cell's SLANT: a 45-degree face, which way its
// open half looks (`set_slant`) — a picture only, for the game to draw its kind cut along a
// diagonal; every routine reads the cell as its kind, whole. And its CEILING: how many steps the
// ceiling over the cell hangs below the floor of the layer above (`set_ceiling`) — 0, the default,
// is that floor's underside — so a renderer that draws ceilings can lower, slope and vault them as
// the rise lifts and slopes a floor. Kept for whoever draws it; the routines do not read it.
//
// THINGS move, so they are not in the terrain: each cell that holds any has a STACK (bottom
// first) in a second hash keyed on (x, y, z), removed when it empties. A thing is a number
// the game chooses.
//
// ── Changes ──────────────────────────────────────────────────────────
//
// Every change to the world counts one (`changes`): a cell's terrain, edge, rise, slant or ceiling
// set to something else, a thing put down or taken, a layer's ground, a kind declared. A write
// that leaves a cell as it was counts nothing. Each BLOCK of 8 x 8 cells on a layer keeps the
// count after the last change in it, each layer the count after its ground last changed, and the
// kinds theirs, so a view that keeps something built from the world (a mesh, a picture, faces)
// remembers the count it built at and asks `changed_since` of the box it read: nothing needs
// building again until the answer is yes, and then only where it is. `changed_since` compares
// `w.changes <= since` within the same world (answering false when the count has not grown past
// `since`); a new world starts at 0, so a caller that swaps in another world resets `since = 0`
// or checks `changes(w) < since` first.
//
// ── Kinds ────────────────────────────────────────────────────────────
//
// The game declares what each terrain kind, each thing and each edge kind is, and the routines
// read only that: whether it can be walked on (or, for a thing, walked through; for an edge,
// walked across), whether it is opaque (stops sight and light), whether it gives light, how
// high a thing stands, and a glyph for `view`. An undeclared terrain kind other than void is
// a wall: not walkable, opaque. An undeclared thing is 1 high, blocks, and is not opaque. An
// undeclared edge kind is a wall: not crossed, opaque.
/// Void: a cell nobody set. Nothing stands there; sight and light pass.
pub const VOID = 0;
/// The darkest shade `shades` gives: a cell this many steps or more from every light.
pub const DARKEST = 7;
/// The four sides of a cell, for its edges.
pub const NORTH = 0;
pub const EAST = 1;
pub const SOUTH = 2;
pub const WEST = 3;
/// The four ways a cell's slant can look (`set_slant`): the corner its open half lies in. 0 is
/// none, a whole cell.
pub const NORTH_EAST = 1;
pub const SOUTH_EAST = 2;
pub const SOUTH_WEST = 3;
pub const NORTH_WEST = 4;
// A cell's raw terrain: 0 is unset (the layer's ground), 1 is void set on purpose.
const UNSET = 0;
const SET_VOID = 1;
const CHUNK = 32;
struct Chunk {
const cx: integer,
const cy: integer,
const z: integer,
cells: vector<u16>,
// One word a cell — north edge (bits 0..7), west edge (8..15), rise (16..19), slant (20..22),
// ceiling (23..26) — or empty
// while no cell of the chunk has any.
extra: vector<u32>
}
// The count after the last change in a block of MARK x MARK cells on a layer, and after a layer's
// ground last changed.
const MARK = 8;
struct Mark {
const bx: integer,
const by: integer,
const bz: integer,
at: integer
}
struct LayerMark {
const lz: integer,
at: integer
}
// The things on one cell, bottom first.
struct Stack {
const x: integer,
const y: integer,
const z: integer,
things: vector<integer>
}
// What the game declared about a terrain kind, an edge kind, and a thing.
struct TerrainKind {
const kd: integer,
walk: boolean,
opaque: boolean,
light: boolean,
glyph: text
}
struct EdgeKind {
const kd: integer,
walk: boolean,
opaque: boolean,
glyph: text
}
// What an unset cell of layer `gz` reads as.
struct Ground {
const gz: integer,
kind: integer,
tile: integer
}
struct ThingKind {
const kd: integer,
walk: boolean,
opaque: boolean,
light: boolean,
height: integer,
glyph: text
}
/// The world: its chunks, its stacks, the kinds the game declared, and what changed when.
pub struct World {
chunks: hash<Chunk[cx, cy, z]>,
stacks: hash<Stack[x, y, z]>,
terrains: hash<TerrainKind[kd]>,
objects: hash<ThingKind[kd]>,
edges: hash<EdgeKind[kd]>,
grounds: hash<Ground[gz]>,
changes: integer,
marks: hash<Mark[bx, by, bz]>,
layer_marks: hash<LayerMark[lz]>,
kinds_at: integer
}
/// An empty world: every cell void, no kinds declared, no changes counted.
pub fn world() -> World {
World { chunks: [], stacks: [], terrains: [], objects: [], edges: [], grounds: [], changes: 0, marks: [],
layer_marks: [], kinds_at: 0 }
}
// --- Changes ---
/// How many changes the world has had (the header's § Changes): remembered, it tells whether
/// anything changed since.
pub fn changes(w: const World) -> integer {
w.changes
}
/// Has anything in cells x0 ..= x1, y0 ..= y1 on layers z0 ..= z1 changed after this world counted
/// `since` (a `changes(w)` remembered from the same world)? Answers false at once when
/// `w.changes <= since` — callers swapping worlds reset `since = 0` or check `changes(w) < since`
/// first. Told by blocks of 8 x 8 cells: a change in a block the box overlaps counts, though it
/// was outside the box. A layer's ground set, or a kind declared, after `since` changes every
/// cell it reaches: yes.
pub fn changed_since(w: const World, x0: integer, y0: integer, x1: integer, y1: integer, z0: integer, z1: integer,
since: integer) -> boolean {
if w.changes <= since { return false; }
if w.kinds_at > since { return true; }
for z in z0..=z1 {
l = w.layer_marks[z];
if l && l.at > since { return true; }
for by in block_of(y0)..=block_of(y1) {
for bx in block_of(x0)..=block_of(x1) {
m = w.marks[bx, by, z];
if m && m.at > since { return true; }
}
}
}
false
}
// floor(v / MARK): the block a coordinate is in.
fn block_of(v: integer) -> integer {
if v >= 0 { v / MARK ?? 0 } else { 0 - ((MARK - 1 - v) / MARK ?? 0) }
}
// A change to cell (x, y, z), counted.
fn mark(w: World, x: integer, y: integer, z: integer) {
w.changes += 1;
bx = block_of(x);
by = block_of(y);
m = &w.marks[bx, by, z];
if m {
m.at = w.changes;
return;
}
w.marks += [Mark { bx: bx, by: by, bz: z, at: w.changes }];
}
// A change to layer z's ground, counted.
fn mark_layer(w: World, z: integer) {
w.changes += 1;
l = &w.layer_marks[z];
if l {
l.at = w.changes;
return;
}
w.layer_marks += [LayerMark { lz: z, at: w.changes }];
}
// A kind declared, counted.
fn mark_kinds(w: World) {
w.changes += 1;
w.kinds_at = w.changes;
}
// --- Kinds ---
/// Declare terrain kind `kind` (1..15): whether it can be walked on, whether it stops sight
/// and light, whether it gives light (a lava floor, a window), and its glyph in `view`.
pub fn terrain_kind(w: World, kind: integer, walk: boolean, opaque: boolean,
light: boolean = false, glyph: text = "?") {
w.terrains[kind] = null;
w.terrains += [TerrainKind { kd: kind, walk: walk, opaque: opaque, light: light, glyph: glyph }];
mark_kinds(w);
}
/// Declare thing `thing`: how many height steps it adds to its stack, whether it blocks a cell
/// for walking (`passable`, `shortest_path`), whether it stops sight and light, whether it
/// gives light (a torch), and its glyph in `view`.
pub fn thing_kind(w: World, thing: integer, height: integer, blocks: boolean,
opaque: boolean = false, light: boolean = false, glyph: text = "?") {
w.objects[thing] = null;
w.objects += [ThingKind { kd: thing, walk: !blocks, opaque: opaque, light: light, height: height, glyph: glyph }];
mark_kinds(w);
}
/// Declare edge kind `kind` (1..255): whether it can be walked across (an open doorway) and
/// whether it stops sight and light (a wall, a closed door; not a fence or a window), and its
/// glyph.
pub fn edge_kind(w: World, kind: integer, walk: boolean, opaque: boolean, glyph: text = "|") {
w.edges[kind] = null;
w.edges += [EdgeKind { kd: kind, walk: walk, opaque: opaque, glyph: glyph }];
mark_kinds(w);
}
/// Can terrain kind `kind` be walked on: as declared; void and an undeclared kind cannot.
pub fn walkable_kind(w: const World, kind: integer) -> boolean {
k = w.terrains[kind];
if !k { return false; }
k.walk
}
/// How many height steps thing `thing` adds: as declared, 1 for an undeclared thing.
pub fn thing_height(w: const World, thing: integer) -> integer {
k = w.objects[thing];
if !k { return 1; }
k.height
}
// --- Addressing ---
/// floor(v / 32): the chunk a coordinate is in, west and north of the origin included.
pub fn chunk_of(v: integer) -> integer {
if v >= 0 { v / CHUNK } else { -((CHUNK - 1 - v) / CHUNK) }
}
/// v mod 32 in [0, 31]: the coordinate inside its chunk.
pub fn inside(v: integer) -> integer {
v - chunk_of(v) * CHUNK
}
/// How many chunks the world holds — the measure of its memory, 2 KB each.
pub fn chunk_count(w: const World) -> integer {
len(w.chunks)
}
/// The layers that hold any terrain, lowest first — what saving the world walks.
pub fn layers(w: const World) -> vector<integer> {
out: vector<integer> = [];
for c in w.chunks {
at = 0;
while at < len(out) && (out[at] ?? 0) < c.z { at += 1; }
if at == len(out) || (out[at] ?? 0) != c.z { out.insert(at, c.z); }
}
out
}
/// The chunks layer z holds, as (cx, cy), in reading order (row by row, west to east) — each
/// one 32x32 cells from (cx * 32, cy * 32).
pub fn chunks(w: const World, z: integer) -> vector<(integer, integer)> {
out: vector<(integer, integer)> = [];
for c in w.chunks {
if c.z != z { continue; }
at = 0;
while at < len(out) && before(out[at].1, out[at].0, c.cy, c.cx) { at += 1; }
out.insert(at, (c.cx, c.cy));
}
out
}
/// Every cell that holds things, as (x, y, z): layer by layer, lowest first, each in reading
/// order.
pub fn stacks(w: const World) -> vector<(integer, integer, integer)> {
out: vector<(integer, integer, integer)> = [];
for s in w.stacks {
at = 0;
while at < len(out) && (out[at].2 < s.z || (out[at].2 == s.z && before(out[at].1, out[at].0, s.y, s.x))) {
at += 1;
}
out.insert(at, (s.x, s.y, s.z));
}
out
}
// Does row-major position (ay, ax) come before (by, bx)?
fn before(ay: integer, ax: integer, by: integer, bx: integer) -> boolean {
ay < by || (ay == by && ax < bx)
}
// --- Terrain ---
// Make the chunk holding (x, y, z) when it does not exist yet.
fn ensure_chunk(w: World, x: integer, y: integer, z: integer) {
cx = chunk_of(x);
cy = chunk_of(y);
if !w.chunks[cx, cy, z] {
w.chunks += [Chunk { cx: cx, cy: cy, z: z, cells: [0 as u16; CHUNK * CHUNK], extra: [] }];
}
}
/// Set a cell's terrain: a kind (0..15) and, for the picture, a tile number (0..4095). VOID
/// makes the cell void even on a layer with a ground — a pit, a hole; `clear` unsets it.
pub fn set_terrain(w: World, x: integer, y: integer, z: integer, kind: integer, tile: integer = 0) {
ensure_chunk(w, x, y, z);
c = &w.chunks[chunk_of(x), chunk_of(y), z];
v = if kind == VOID { SET_VOID } else { (kind & 15) * 4096 + (tile & 4095) };
i = inside(y) * CHUNK + inside(x);
if ((c.cells[i] ?? 0) as integer) == (v & 65535) { return; }
c.cells[i] = (v & 65535) as u16;
mark(w, x, y, z);
}
/// Make a cell unset again: it reads as its layer's ground, or as void.
pub fn clear(w: World, x: integer, y: integer, z: integer) {
c = &w.chunks[chunk_of(x), chunk_of(y), z];
if !c { return; }
i = inside(y) * CHUNK + inside(x);
if ((c.cells[i] ?? 0) as integer) == UNSET { return; }
c.cells[i] = UNSET as u16;
mark(w, x, y, z);
}
/// Was this cell set (to anything, void included), rather than read from the ground?
pub fn is_set(w: const World, x: integer, y: integer, z: integer) -> boolean {
raw(w, x, y, z) != UNSET
}
/// Give layer z a ground: every cell of it nobody set reads as terrain `kind` with picture
/// `tile` — open land that costs no memory. Kind VOID takes the ground away.
pub fn set_ground(w: World, z: integer, kind: integer, tile: integer = 0) {
w.grounds[z] = null;
if kind != VOID { w.grounds += [Ground { gz: z, kind: kind & 15, tile: tile & 4095 }]; }
mark_layer(w, z);
}
/// The layers that have a ground, lowest first — with `layers`, what saving a world walks.
pub fn ground_layers(w: const World) -> vector<integer> {
out: vector<integer> = [];
for g in w.grounds {
at = 0;
while at < len(out) && (out[at] ?? 0) < g.gz { at += 1; }
out.insert(at, g.gz);
}
out
}
/// Layer z's ground kind, VOID when it has none.
pub fn ground(w: const World, z: integer) -> integer {
g = w.grounds[z];
if !g { return VOID; }
g.kind
}
/// Layer z's ground tile, 0 when it has none.
pub fn ground_tile(w: const World, z: integer) -> integer {
g = w.grounds[z];
if !g { return 0; }
g.tile
}
/// Set a rectangle of terrain, corners included.
pub fn fill(w: World, x0: integer, y0: integer, x1: integer, y1: integer, z: integer,
kind: integer, tile: integer = 0) {
for y in y0..=y1 {
for x in x0..=x1 { set_terrain(w, x, y, z, kind, tile); }
}
}
fn raw(w: const World, x: integer, y: integer, z: integer) -> integer {
c = w.chunks[chunk_of(x), chunk_of(y), z];
if !c { return 0; }
(c.cells[inside(y) * CHUNK + inside(x)] ?? 0) as integer
}
/// A cell's terrain kind: what it was set to, or its layer's ground when it was not.
pub fn terrain(w: const World, x: integer, y: integer, z: integer) -> integer {
r = raw(w, x, y, z);
if r == UNSET { return ground(w, z); }
if r == SET_VOID { return VOID; }
r / 4096
}
/// A cell's tile number, its picture: what it was set to, or its layer's ground tile.
pub fn tile(w: const World, x: integer, y: integer, z: integer) -> integer {
r = raw(w, x, y, z);
if r == UNSET { return ground_tile(w, z); }
if r == SET_VOID { return 0; }
r % 4096
}
// --- Edges and rise ---
fn word(w: const World, x: integer, y: integer, z: integer) -> integer {
c = w.chunks[chunk_of(x), chunk_of(y), z];
if !c { return 0; }
if len(c.extra) == 0 { return 0; }
// Masked to the 31 bits a word holds (`set_word`): so it is an integer under `#width 32` too.
((c.extra[inside(y) * CHUNK + inside(x)] ?? 0) & 2147483647) as integer
}
fn set_word(w: World, x: integer, y: integer, z: integer, v: integer) {
ensure_chunk(w, x, y, z);
c = &w.chunks[chunk_of(x), chunk_of(y), z];
if !c { return; }
if len(c.extra) == 0 { c.extra = [0 as u32; CHUNK * CHUNK]; }
i = inside(y) * CHUNK + inside(x);
if (((c.extra[i] ?? 0) & 2147483647) as integer) == (v & 2147483647) { return; }
c.extra[i] = (v & 2147483647) as u32;
mark(w, x, y, z);
}
// Which cell holds the edge on `side` of (x, y), and in which byte: (x, y, shift).
fn edge_home(x: integer, y: integer, side: integer) -> (integer, integer, integer) {
if side == NORTH { return (x, y, 0); }
if side == SOUTH { return (x, y + 1, 0); }
if side == WEST { return (x, y, 8); }
(x + 1, y, 8)
}
/// Put edge kind `kind` (1..255) on `side` (NORTH, EAST, SOUTH, WEST) of cell (x, y, z); 0
/// takes the edge away. The east edge of a cell is its east neighbour's west edge, and the
/// same edge: setting one sets both.
pub fn set_edge(w: World, x: integer, y: integer, z: integer, side: integer, kind: integer) {
h = edge_home(x, y, side);
v = word(w, h.0, h.1, z);
set_word(w, h.0, h.1, z, (v & ~(255 << h.2)) | ((kind & 255) << h.2));
}
/// The edge kind on `side` of cell (x, y, z), 0 for none.
pub fn edge(w: const World, x: integer, y: integer, z: integer, side: integer) -> integer {
h = edge_home(x, y, side);
(word(w, h.0, h.1, z) >> h.2) & 255
}
// The edge crossed stepping from (x, y) by (dx, dy), one of the four directions.
fn between(w: const World, x: integer, y: integer, z: integer, dx: integer, dy: integer) -> integer {
side = if dx > 0 { EAST } else if dx < 0 { WEST } else if dy > 0 { SOUTH } else { NORTH };
edge(w, x, y, z, side)
}
/// Can a walker step from (x, y, z) by (dx, dy) — one of the four directions — across the edge
/// between: no edge, or one declared walkable. Whether the cell it lands on can be stood on is
/// `passable`'s question.
pub fn crossable(w: const World, x: integer, y: integer, z: integer, dx: integer, dy: integer) -> boolean {
e = between(w, x, y, z, dx, dy);
if e == 0 { return true; }
k = w.edges[e];
if !k { return false; }
k.walk
}
/// Do sight and light pass from (x, y, z) by (dx, dy) across the edge between.
pub fn see_through(w: const World, x: integer, y: integer, z: integer, dx: integer, dy: integer) -> boolean {
e = between(w, x, y, z, dx, dy);
if e == 0 { return true; }
k = w.edges[e];
if !k { return false; }
!k.opaque
}
/// Give cell (x, y, z) a rise of `steps` (0..15): its own height, under whatever stands on it.
/// A hillside is cells rising one step at a time, so a walker that steps up one at a time walks
/// up it.
pub fn set_rise(w: World, x: integer, y: integer, z: integer, steps: integer) {
v = word(w, x, y, z);
set_word(w, x, y, z, (v & ~(15 << 16)) | ((steps & 15) << 16));
}
/// A cell's rise in steps, 0 for a flat one.
pub fn rise(w: const World, x: integer, y: integer, z: integer) -> integer {
(word(w, x, y, z) >> 16) & 15
}
/// Give cell (x, y, z) a slant: a 45-degree face looking `toward` (`NORTH_EAST`, `SOUTH_EAST`,
/// `SOUTH_WEST`, `NORTH_WEST`; 0 takes it away), so the game draws its kind filling only the half
/// away from that corner — a rock face or a wall running at 45 degrees. A picture only: walking,
/// sight, light and standing on it read the cell as its kind, whole. Kept when the terrain
/// changes, like the edges and the rise.
pub fn set_slant(w: World, x: integer, y: integer, z: integer, toward: integer) {
t = if toward < 0 || toward > NORTH_WEST { 0 } else { toward };
v = word(w, x, y, z);
if t == 0 && (v & (7 << 20)) == 0 { return; }
set_word(w, x, y, z, (v & ~(7 << 20)) | (t << 20));
}
/// Which way cell (x, y, z)'s slant looks, 0 for a whole cell.
pub fn slant(w: const World, x: integer, y: integer, z: integer) -> integer {
(word(w, x, y, z) >> 20) & 7
}
/// Lower the ceiling over cell (x, y, z) to `steps` (0..15) below the floor of the layer above: 0,
/// the default, is that floor's underside. A ceiling's slope is its neighbours' differences, as a
/// floor's is its rise's. Kept for a renderer that draws ceilings; no routine here reads it — a
/// cell under the open sky has no ceiling, whatever this holds. Kept when the terrain changes, like
/// the edges, the rise and the slant.
pub fn set_ceiling(w: World, x: integer, y: integer, z: integer, steps: integer) {
t = if steps < 0 { 0 } else if steps > 15 { 15 } else { steps };
v = word(w, x, y, z);
if t == 0 && (v & (15 << 23)) == 0 { return; }
set_word(w, x, y, z, (v & ~(15 << 23)) | (t << 23));
}
/// How many steps the ceiling over cell (x, y, z) hangs below the floor of the layer above, 0 for
/// none.
pub fn ceiling(w: const World, x: integer, y: integer, z: integer) -> integer {
(word(w, x, y, z) >> 23) & 15
}
/// What each cell of x0 ..= x1, y0 ..= y1 on layer z holds, row by row, two numbers a cell: first
/// its terrain kind (bits 0..3, as `terrain` reads it), rise (4..7), slant (8..10), ceiling
/// (11..14), and bit 15 when things lie on it; then its north edge (bits 0..7) and west edge
/// (8..15). Two numbers, not one: under `#width 32` an integer holds 31 bits. Read once for the
/// box — the cell's chunk and its stack looked up once each — where asking `terrain`, `rise`,
/// `edge` and the rest of each cell asks for its chunk again every time: what a renderer reading a
/// box of cells (a mesh's halo, a tile of faces) wants.
pub fn block(w: const World, x0: integer, y0: integer, x1: integer, y1: integer, z: integer) -> vector<integer> {
out: vector<integer> = [];
g = ground(w, z);
for y in y0..=y1 {
for x in x0..=x1 {
ix = inside(y) * CHUNK + inside(x);
c = w.chunks[chunk_of(x), chunk_of(y), z];
r = UNSET;
wd = 0;
if c {
r = (c.cells[ix] ?? 0) as integer;
if len(c.extra) > 0 { wd = ((c.extra[ix] ?? 0) & 2147483647) as integer; }
}
kind = if r == UNSET { g } else if r == SET_VOID { VOID } else { r / 4096 };
v = kind + ((wd >> 16) & 15) * 16 + ((wd >> 20) & 7) * 256 + ((wd >> 23) & 15) * 2048;
if w.stacks[x, y, z] { v += 32768; }
out += [v, (wd & 255) + ((wd >> 8) & 255) * 256];
}
}
out
}
/// Is (x, y, z) under the open sky: nothing set on the layer above it.
pub fn open_sky(w: const World, x: integer, y: integer, z: integer) -> boolean {
terrain(w, x, y, z + 1) == VOID
}
// --- Things ---
/// Put a thing on top of a cell's stack.
pub fn place(w: World, x: integer, y: integer, z: integer, thing: integer) {
if !w.stacks[x, y, z] {
w.stacks += [Stack { x: x, y: y, z: z, things: [] }];
}
s = &w.stacks[x, y, z];
s.things += [thing];
mark(w, x, y, z);
}
/// Take the top thing off a cell's stack and answer it; 0 when the cell has none. The stack
/// goes when it empties.
pub fn take(w: World, x: integer, y: integer, z: integer) -> integer {
s = &w.stacks[x, y, z];
if !s { return 0; }
n = len(s.things);
if n == 0 { return 0; }
t = s.things[n - 1] ?? 0;
if n == 1 {
w.stacks[x, y, z] = null;
} else {
s.things = s.things[0..n - 1];
}
mark(w, x, y, z);
t
}
/// The things on a cell, bottom first.
pub fn things(w: const World, x: integer, y: integer, z: integer) -> vector<integer> {
s = w.stacks[x, y, z];
if !s { return []; }
out: vector<integer> = [];
for t in s.things { out += [t]; }
out
}
/// The top thing on a cell, 0 when there is none.
pub fn top(w: const World, x: integer, y: integer, z: integer) -> integer {
s = w.stacks[x, y, z];
if !s { return 0; }
n = len(s.things);
if n == 0 { 0 } else { s.things[n - 1] ?? 0 }
}
/// The height of a cell in steps: its rise and the declared heights of the things on it.
pub fn height(w: const World, x: integer, y: integer, z: integer) -> integer {
s = w.stacks[x, y, z];
if !s { return rise(w, x, y, z); }
h = rise(w, x, y, z);
for t in s.things { h += thing_height(w, t); }
h
}
// --- Questions about cells ---
/// Can a cell be walked into: walkable terrain and nothing on it that blocks.
pub fn passable(w: const World, x: integer, y: integer, z: integer) -> boolean {
if !walkable_kind(w, terrain(w, x, y, z)) { return false; }
s = w.stacks[x, y, z];
if !s { return true; }
for t in s.things {
k = w.objects[t];
if !k || !k.walk { return false; }
}
true
}
/// Does a cell stop sight and light: opaque terrain, or an opaque thing on it. An
/// undeclared terrain kind (not void) is opaque.
pub fn opaque(w: const World, x: integer, y: integer, z: integer) -> boolean {
kind = terrain(w, x, y, z);
if kind != VOID {
k = w.terrains[kind];
if !k || k.opaque { return true; }
}
s = w.stacks[x, y, z];
if !s { return false; }
for t in s.things {
o = w.objects[t];
if o && o.opaque { return true; }
}
false
}
/// Does a cell give light: under the open sky (by day: `daylight`, the default), lit terrain, or a
/// lit thing on it. A void cell gives none.
pub fn lit(w: const World, x: integer, y: integer, z: integer, daylight: boolean = true) -> boolean {
kind = terrain(w, x, y, z);
if kind == VOID { return false; }
if daylight && open_sky(w, x, y, z) { return true; }
k = w.terrains[kind];
if k && k.light { return true; }
for t in things(w, x, y, z) {
o = w.objects[t];
if o && o.light { return true; }
}
false
}
// --- Routines ---
/// The shortest walk from (x0, y0) to (x1, y1) on layer z, in steps to the four neighbours over
/// `passable` cells and `crossable` edges: the cells passed through, the goal last, the start left out. Empty when
/// the goal cannot be reached within `limit` steps (or is the start). The goal itself need
/// not be passable, so a path can lead up to a thing to act on.
// Example: @TLW-001
pub fn shortest_path(w: const World, x0: integer, y0: integer, x1: integer, y1: integer,
z: integer, limit: integer = 64) -> vector<(integer, integer)> {
out: vector<(integer, integer)> = [];
if (x0 == x1 && y0 == y1) || limit <= 0 { return out; }
// A square of `limit` round the start holds every cell such a walk can reach.
gx = x0 - limit;
gy = y0 - limit;
gw = 2 * limit + 1;
from: vector<integer> = [-1; gw * gw];
start = limit * gw + limit;
from[start] = start;
queue: vector<integer> = [start];
at = 0;
found = -1;
while at < len(queue) && found < 0 {
c = queue[at] ?? 0;
at += 1;
cx = c % gw ?? 0;
cy = c / gw ?? 0;
for n in [(cx + 1, cy), (cx - 1, cy), (cx, cy + 1), (cx, cy - 1)] {
nx = n.0;
ny = n.1;
if nx < 0 || nx >= gw || ny < 0 || ny >= gw || (from[ny * gw + nx] ?? 0) >= 0 { continue; }
if !crossable(w, gx + cx, gy + cy, z, nx - cx, ny - cy) { continue; }
goal = gx + nx == x1 && gy + ny == y1;
if !goal && !passable(w, gx + nx, gy + ny, z) { continue; }
from[ny * gw + nx] = c;
if goal {
found = ny * gw + nx;
break;
}
queue += [ny * gw + nx];
}
}
if found < 0 { return out; }
back: vector<integer> = [];
c = found;
while c != start {
back += [c];
c = from[c] ?? start;
}
if len(back) > limit { return out; }
i = len(back) - 1;
while i >= 0 {
b = back[i] ?? 0;
out += [(gx + (b % gw ?? 0), gy + (b / gw ?? 0))];
i -= 1;
}
out
}
/// Can (x1, y1) be seen from (x0, y0) on layer z: no opaque cell on the straight line between
/// them (Bresenham's), and no opaque edge it crosses. The two ends themselves do not count, so a wall is seen, and an eye
/// in a doorway sees out.
pub fn line_of_sight(w: const World, x0: integer, y0: integer, x1: integer, y1: integer,
z: integer) -> boolean {
dx = if x1 > x0 { x1 - x0 } else { x0 - x1 };
dy = if y1 > y0 { y0 - y1 } else { y1 - y0 };
sx = if x0 < x1 { 1 } else { -1 };
sy = if y0 < y1 { 1 } else { -1 };
err = dx + dy;
x = x0;
y = y0;
while x != x1 || y != y1 {
if (x != x0 || y != y0) && opaque(w, x, y, z) { return false; }
e2 = 2 * err;
mx = e2 >= dy;
my = e2 <= dx;
// The edges the step crosses: a diagonal step sees past a corner when either way round it
// is open.
if mx && my {
round_x = see_through(w, x, y, z, sx, 0) && see_through(w, x + sx, y, z, 0, sy);
round_y = see_through(w, x, y, z, 0, sy) && see_through(w, x, y + sy, z, sx, 0);
if !round_x && !round_y { return false; }
} else if mx {
if !see_through(w, x, y, z, sx, 0) { return false; }
} else if my {
if !see_through(w, x, y, z, 0, sy) { return false; }
}
if mx {
err += dy;
x += sx;
}
if my {
err += dx;
y += sy;
}
}
true
}
/// How dark each cell of x0..=x1, y0..=y1 on layer z is, row by row: 0 at a light, one
/// shade darker for each step from the nearest one, up to `DARKEST`, never black. A light is
/// a `lit` cell or one of `lights` (cells the game says give light now: what a figure carries,
/// a torch). A light of `lights` starts at its shade in `start` (0, the default, is full; a
/// dimmer light — a torch burning down — reaches less far), the k-th entry for the k-th light.
/// Light spreads over every cell but an opaque one, which is lit but passes no light on unless
/// it is itself a light, and never across an opaque edge. A light up to `DARKEST` cells outside the rectangle counts.
/// `daylight` false: the open sky is no light — night, when only what gives light of itself (a
/// lamp, a lit terrain) and `lights` light the world.
pub fn shades(w: const World, x0: integer, y0: integer, x1: integer, y1: integer, z: integer,
lights: const vector<(integer, integer)> = [],
start: const vector<integer> = [], daylight: boolean = true) -> vector<integer> {
gx = x0 - DARKEST;
gy = y0 - DARKEST;
gw = x1 - x0 + 1 + 2 * DARKEST;
gh = y1 - y0 + 1 + 2 * DARKEST;
win = window(w, gx, gy, gw, gh, z, daylight);
dist: vector<integer> = [DARKEST; gw * gh];
source: vector<boolean> = [false; gw * gh];
for c in 0..gw * gh {
if win.lit[c] ?? false {
dist[c] = 0;
source[c] = true;
}
}
for k in 0..len(lights) {
l = lights[k];
i = l.0 - gx;
j = l.1 - gy;
from = start[k] ?? 0;
if from < 0 { from = 0; }
if i >= 0 && i < gw && j >= 0 && j < gh && from < (dist[j * gw + i] ?? 0) {
dist[j * gw + i] = from;
source[j * gw + i] = true;
}
}
spread(w, win, gw, gh, dist, source);
out: vector<integer> = [];
for y in y0..=y1 {
for x in x0..=x1 {
out += [dist[(y - gy) * gw + (x - gx)] ?? DARKEST];
}
}
out
}
/// The shades one light gives alone, as `shades` spreads it, over the square of DARKEST cells
/// round it on layer z, row by row (2 * DARKEST + 1 a side, its corner at (x - DARKEST,
/// y - DARKEST)): `start` at the light, one darker a step, DARKEST where it does not reach. What
/// a GPU lights a torch by, apart from the others (the scene contract's torches): `shades` with
/// these lights is, cell by cell, the brightest of `shades` without them and each light's reach,
/// but where a light stands on an opaque cell another light reaches more brightly than it starts
/// — there `shades` passes that light on, a light's reach does not.
pub fn reach(w: const World, x: integer, y: integer, z: integer, start: integer = 0) -> vector<integer> {
gx = x - DARKEST;
gy = y - DARKEST;
gw = 2 * DARKEST + 1;
gh = gw;
win = window(w, gx, gy, gw, gh, z, false);
dist: vector<integer> = [DARKEST; gw * gh];
source: vector<boolean> = [false; gw * gh];
from = if start < 0 { 0 } else { start };
if from < DARKEST {
dist[DARKEST * gw + DARKEST] = from;
source[DARKEST * gw + DARKEST] = true;
}
spread(w, win, gw, gh, dist, source);
dist
}
// The light spread from its sources over a window gw x gh: `dist` holds each cell's shade (a
// source's own, DARKEST elsewhere), lowered as the light reaches it.
fn spread(w: const World, win: const Window, gw: integer, gh: integer, dist: vector<integer>,
source: const vector<boolean>) {
// Shade by shade, so a cell takes its light from the brightest way first: the cells a shade
// reached, with the lights that start at that shade, spread one shade darker.
frontier: vector<integer> = [];
for s in 0..DARKEST - 1 {
for c in 0..gw * gh {
if (source[c] ?? false) && (dist[c] ?? DARKEST) == s { frontier += [c]; }
}
next: vector<integer> = [];
for c in frontier {
if (dist[c] ?? DARKEST) != s { continue; }
cx = c % gw ?? 0;
cy = c / gw ?? 0;
if !(source[c] ?? false) && (win.opq[c] ?? false) { continue; }
for n in [(cx - 1, cy), (cx + 1, cy), (cx, cy - 1), (cx, cy + 1)] {
nx = n.0;
ny = n.1;
if nx >= 0 && nx < gw && ny >= 0 && ny < gh && (dist[ny * gw + nx] ?? 0) > s + 1
&& sees(w, win, c, ny * gw + nx, nx - cx, ny - cy) {
dist[ny * gw + nx] = s + 1;
next += [ny * gw + nx];
}
}
}
frontier = next;
}
}
// What `shades` asks of each cell of its window, read once: whether it is a light (`lit`),
// whether it is opaque (`opaque`), and its word (the edges `see_through` reads). Asking each
// question of the world looked the cell's chunk up again every time — about ten hash lookups a
// cell — so the window is read as a halo grid is (moros's hex_mesh): its chunk once, the layer
// above once by day, its stack once. The answers are `lit`'s, `opaque`'s and `see_through`'s.
struct Window {
lit: vector<boolean>,
opq: vector<boolean>,
word: vector<integer>
}
fn window(w: const World, gx: integer, gy: integer, gw: integer, gh: integer, z: integer, daylight: boolean) -> Window {
n = gw * gh;
win = Window { lit: [false; n], opq: [false; n], word: [0; n] };
// Each kind's light and opacity, asked once; an undeclared kind is opaque.
klit: vector<boolean> = [];
kopq: vector<boolean> = [];
for k in 0..16 {
d = w.terrains[k];
klit += [if d { d.light } else { false }];
kopq += [if d { d.opaque } else { true }];
}
g0 = ground(w, z);
g1 = ground(w, z + 1);
for j in 0..gh {
y = gy + j;
for i in 0..gw {
x = gx + i;
at = j * gw + i;
ix = inside(y) * CHUNK + inside(x);
c = w.chunks[chunk_of(x), chunk_of(y), z];
r = UNSET;
if c {
r = (c.cells[ix] ?? 0) as integer;
if len(c.extra) > 0 { win.word[at] = ((c.extra[ix] ?? 0) & 2147483647) as integer; }
}
kind = if r == UNSET { g0 } else if r == SET_VOID { VOID } else { r / 4096 };
lt = false;
op = false;
if kind != VOID {
op = kopq[kind] ?? true;
lt = klit[kind] ?? false;
if daylight && !lt {
a = w.chunks[chunk_of(x), chunk_of(y), z + 1];
ra = if a { (a.cells[ix] ?? 0) as integer } else { UNSET };
above = if ra == UNSET { g1 } else if ra == SET_VOID { VOID } else { ra / 4096 };
if above == VOID { lt = true; }
}
}
s = w.stacks[x, y, z];
if s {
for t in s.things {
o = w.objects[t];
if o {
if o.opaque { op = true; }
if o.light && kind != VOID { lt = true; }
}
}
}
win.lit[at] = lt;
win.opq[at] = op;
}
}
win
}
// Do sight and light pass from window cell c to its neighbour n, (dx, dy) away: `see_through`,
// from the words read. The edge between is the north or west one of the cell that holds it.
fn sees(w: const World, win: const Window, c: integer, n: integer, dx: integer, dy: integer) -> boolean {
e = if dx > 0 { ((win.word[n] ?? 0) >> 8) & 255 } else if dx < 0 { ((win.word[c] ?? 0) >> 8) & 255 }
else if dy > 0 { (win.word[n] ?? 0) & 255 } else { (win.word[c] ?? 0) & 255 };
if e == 0 { return true; }
k = w.edges[e];
if !k { return false; }
!k.opaque
}
/// A layer as text, one line per row: each cell's top thing's glyph, or its terrain's, or a
/// space for void. `marks` puts a glyph of the game's at a cell (a figure): (x, y, glyph).
pub fn view(w: const World, x0: integer, y0: integer, x1: integer, y1: integer, z: integer,
marks: const vector<(integer, integer, text)> = []) -> text {
out = "";
for y in y0..=y1 {
for x in x0..=x1 {
g = "";
for m in marks {
if m.0 == x && m.1 == y { g = m.2; }
}
if g == "" {
t = top(w, x, y, z);
o = w.objects[t];
if t != 0 && o {
g = o.glyph;
} else if t != 0 {
g = "?";
} else {
kind = terrain(w, x, y, z);
k = w.terrains[kind];
g = if kind == VOID { " " } else if k { k.glyph } else { "#" };
}
}
out += g;
}
out += "\n";
}
out
}
// --- Regions and stamps ---
struct Seen {
const sx: integer,
const sy: integer
}
/// The cells of layer z connected to (x, y) that hold the same terrain as it — the same kind
/// and tile, set or read from the ground — four ways, never across an edge between two cells:
/// what a fill paints. Nearest first (breadth first), (x, y) itself first, and at most `limit`
/// of them, because a layer's ground has no end: a region exactly `limit` long may go on.
pub fn region(w: const World, x: integer, y: integer, z: integer, limit: integer) -> vector<(integer, integer)> {
out: vector<(integer, integer)> = [];
if limit <= 0 { return out; }
kind = terrain(w, x, y, z);
t = tile(w, x, y, z);
seen: hash<Seen[sx, sy]> = [Seen { sx: x, sy: y }];
out += [(x, y)];
at = 0;
while at < len(out) && len(out) < limit {
c = out[at] ?? (x, y);
at += 1;
for d in [(1, 0), (-1, 0), (0, 1), (0, -1)] {
nx = c.0 + d.0;
ny = c.1 + d.1;
if len(out) >= limit || seen[nx, ny] { continue; }
if between(w, c.0, c.1, z, d.0, d.1) != 0 { continue; }
if terrain(w, nx, ny, z) != kind || tile(w, nx, ny, z) != t { continue; }
seen += [Seen { sx: nx, sy: ny }];
out += [(nx, ny)];
}
}
out
}
// Set a cell's raw terrain word (as `raw` reads it), making its chunk when needed.
fn put_raw(w: World, x: integer, y: integer, z: integer, r: integer) {
ensure_chunk(w, x, y, z);
c = &w.chunks[chunk_of(x), chunk_of(y), z];
if !c { return; }
i = inside(y) * CHUNK + inside(x);
if ((c.cells[i] ?? 0) as integer) == (r & 65535) { return; }
c.cells[i] = (r & 65535) as u16;
mark(w, x, y, z);
}
/// A stamp: the cells of the box x0..=x1, y0..=y1 on layers z0..=z1, corners included — terrain
/// as set (an unset cell stays unset), rise, slant, ceiling, the north and west edges each cell holds, and
/// the stacks — moved so the corner (x0, y0, z0) is (0, 0, 0). The kinds come along: the stamp is
/// a world of its own, to `paste` back or keep.
pub fn copy(w: const World, x0: integer, y0: integer, x1: integer, y1: integer, z0: integer,
z1: integer) -> World {
out = world();
out.terrains = w.terrains;
out.objects = w.objects;
out.edges = w.edges;
for z in z0..=z1 {
for y in y0..=y1 {
for x in x0..=x1 {
r = raw(w, x, y, z);
if r != UNSET { put_raw(out, x - x0, y - y0, z - z0, r); }
v = word(w, x, y, z);
if v != 0 { set_word(out, x - x0, y - y0, z - z0, v); }
for t in things(w, x, y, z) { place(out, x - x0, y - y0, z - z0, t); }
}
}
}
out
}
/// Write stamp `from` (a `copy`) into `w` with its (0, 0, 0) at (x, y, z). Each cell the stamp
/// holds anything at replaces the cell there: its terrain when the stamp set it, its rise, slant,
/// ceiling and north and west edges, and its stack (none when the stamp's cell has none). A cell the
/// stamp holds nothing at leaves `w` as it was.
// Example: @TLW-002
pub fn paste(w: World, from: const World, x: integer, y: integer, z: integer) {
for c in from.chunks {
for j in 0..CHUNK {
for i in 0..CHUNK {
fx = c.cx * CHUNK + i;
fy = c.cy * CHUNK + j;
r = raw(from, fx, fy, c.z);
v = word(from, fx, fy, c.z);
if r == UNSET && v == 0 { continue; }
if r != UNSET { put_raw(w, x + fx, y + fy, z + c.z, r); }
if v != 0 || word(w, x + fx, y + fy, z + c.z) != 0 { set_word(w, x + fx, y + fy, z + c.z, v); }
unstack(w, x + fx, y + fy, z + c.z);
}
}
}
for s in from.stacks {
unstack(w, x + s.x, y + s.y, z + s.z);
for t in s.things { place(w, x + s.x, y + s.y, z + s.z, t); }
}
}
// A cell's stack gone, the change counted when there was one.
fn unstack(w: World, x: integer, y: integer, z: integer) {
if !w.stacks[x, y, z] { return; }
w.stacks[x, y, z] = null;
mark(w, x, y, z);
}
// --- A stamp turned, mirrored and laid ---
//
// A stamp (`copy`) is a part of a world: a room's corner, a cellar, a broken wall. The same
// part is wanted the other way round — a back door on any of a house's four walls — so a stamp
// can be MIRRORED east to west and TURNED clockwise a quarter at a time: the eight ways a part
// of a square grid can lie. And it is wanted LAID as well as pasted: a template of a house's
// part takes the shape of the house it is laid on, so a cell it left unset stays as the world
// has it and an edge it does not hold is left alone — it adds and changes, and removes nothing
// it does not name.
fn quarter(turn: integer) -> integer { ((turn % 4) + 4) % 4 }
/// The box of a stamp: the cells it set or put things on, over every layer, as (x0, y0, x1, y1),
/// corners included. A cell that only holds an edge, a rise, a slant or a ceiling is not counted:
/// a part's size is its cells'. An empty stamp's box is the single cell (0, 0).
pub fn stamp_box(s: const World) -> (integer, integer, integer, integer) {
any = false;
x0 = 0;
y0 = 0;
x1 = 0;
y1 = 0;
for c in s.chunks {
for j in 0..CHUNK {
for i in 0..CHUNK {
if ((c.cells[j * CHUNK + i] ?? 0) as integer) == UNSET { continue; }
fx = c.cx * CHUNK + i;
fy = c.cy * CHUNK + j;
if !any || fx < x0 { x0 = fx; }
if !any || fx > x1 { x1 = fx; }
if !any || fy < y0 { y0 = fy; }
if !any || fy > y1 { y1 = fy; }
any = true;
}
}
}
for st in s.stacks {
if !any || st.x < x0 { x0 = st.x; }
if !any || st.x > x1 { x1 = st.x; }
if !any || st.y < y0 { y0 = st.y; }
if !any || st.y > y1 { y1 = st.y; }
any = true;
}
(x0, y0, x1, y1)
}
/// The size of a stamp's box, (width, height), once turned `turn` quarters.
pub fn stamp_size(s: const World, turn: integer) -> (integer, integer) {
b = stamp_box(s);
if quarter(turn) % 2 == 1 { (b.3 - b.1 + 1, b.2 - b.0 + 1) } else { (b.2 - b.0 + 1, b.3 - b.1 + 1) }
}
/// Where cell (x, y) of a stamp whose box is (x0, y0, x1, y1) lands, counted from the turned box's
/// north-west corner: mirrored east to west first (`flip`), then turned clockwise `turn` quarters.
/// For what a game keeps beside its world — a figure on a cell, a lock on a door — to go where its
/// cell goes. A cell outside the box lands outside the turned box, as far.
pub fn stamp_spot(x0: integer, y0: integer, x1: integer, y1: integer, turn: integer, flip: boolean,
x: integer, y: integer) -> (integer, integer) {
u = x - x0;
v = y - y0;
w = x1 - x0 + 1;
h = y1 - y0 + 1;
if flip { u = w - 1 - u; }
for _ in 0..quarter(turn) {
nu = h - 1 - v;
v = u;
u = nu;
hw = w;
w = h;
h = hw;
}
(u, v)
}
/// The side (NORTH, EAST, SOUTH, WEST) `side` becomes: mirrored east to west, then turned clockwise.
pub fn turned_side(side: integer, turn: integer, flip: boolean) -> integer {
s = side;
if flip && (s == EAST || s == WEST) { s = 4 - s; }
(s + quarter(turn)) % 4
}
/// The way a slant looks (NORTH_EAST .. NORTH_WEST, 0 for none) once mirrored and turned.
pub fn turned_slant(way: integer, turn: integer, flip: boolean) -> integer {
if way <= 0 || way > NORTH_WEST { return 0; }
s = if flip { 5 - way } else { way };
for _ in 0..quarter(turn) { s = s % 4 + 1; }
s
}
/// Stamp `s` mirrored east to west (`flip`) and then turned clockwise `turn` quarters, as a stamp:
/// its box's north-west corner at (0, 0), its layers as they were. Every cell goes where
/// `stamp_spot` says, with its terrain, rise, ceiling and stack; its slant looks the turned way;
/// its edges are on their turned sides. Turned four times, or mirrored twice, it is the stamp
/// again (moved so its box begins at (0, 0)).
pub fn turned(s: const World, turn: integer, flip: boolean = false) -> World {
out = world();
out.terrains = s.terrains;
out.objects = s.objects;
out.edges = s.edges;
b = stamp_box(s);
for c in s.chunks {
for j in 0..CHUNK {
for i in 0..CHUNK {
fx = c.cx * CHUNK + i;
fy = c.cy * CHUNK + j;
r = raw(s, fx, fy, c.z);
v = word(s, fx, fy, c.z);
if r == UNSET && v == 0 { continue; }
at = stamp_spot(b.0, b.1, b.2, b.3, turn, flip, fx, fy);
if r != UNSET { put_raw(out, at.0, at.1, c.z, r); }
if ((v >> 16) & 15) != 0 { set_rise(out, at.0, at.1, c.z, (v >> 16) & 15); }
if ((v >> 20) & 7) != 0 { set_slant(out, at.0, at.1, c.z, turned_slant((v >> 20) & 7, turn, flip)); }
if ((v >> 23) & 15) != 0 { set_ceiling(out, at.0, at.1, c.z, (v >> 23) & 15); }
if (v & 255) != 0 { set_edge(out, at.0, at.1, c.z, turned_side(NORTH, turn, flip), v & 255); }
if ((v >> 8) & 255) != 0 { set_edge(out, at.0, at.1, c.z, turned_side(WEST, turn, flip), (v >> 8) & 255); }
}
}
}
for st in s.stacks {
at = stamp_spot(b.0, b.1, b.2, b.3, turn, flip, st.x, st.y);
for t in st.things { place(out, at.0, at.1, st.z, t); }
}
out
}
/// Lay stamp `from` on `w` with its (0, 0, 0) at (x, y, z): only what the stamp holds is written.
/// - a cell it SET (to anything, void included) replaces the cell there: its terrain, rise,
/// slant and ceiling, and its stack (none when the stamp's cell has none);
/// - a cell it left UNSET leaves `w`'s cell as it is, so the stamp takes the shape of what it is
/// laid on;
/// - an edge it holds is written; an edge it does not hold is left alone, so a stamp adds and
/// changes edges and never removes one;
/// - things on a cell it left unset still replace the stack there.
/// `paste` is the other rule: there every cell the stamp holds anything at is replaced whole, its
/// edges with it.
// Example: @TLW-002
pub fn lay(w: World, from: const World, x: integer, y: integer, z: integer) {
for c in from.chunks {
for j in 0..CHUNK {
for i in 0..CHUNK {
fx = c.cx * CHUNK + i;
fy = c.cy * CHUNK + j;
r = raw(from, fx, fy, c.z);
v = word(from, fx, fy, c.z);
if r == UNSET && v == 0 { continue; }
if (v & 255) != 0 { set_edge(w, x + fx, y + fy, z + c.z, NORTH, v & 255); }
if ((v >> 8) & 255) != 0 { set_edge(w, x + fx, y + fy, z + c.z, WEST, (v >> 8) & 255); }
if r == UNSET { continue; }
put_raw(w, x + fx, y + fy, z + c.z, r);
set_rise(w, x + fx, y + fy, z + c.z, (v >> 16) & 15);
set_slant(w, x + fx, y + fy, z + c.z, (v >> 20) & 7);
set_ceiling(w, x + fx, y + fy, z + c.z, (v >> 23) & 15);
unstack(w, x + fx, y + fy, z + c.z);
}
}
}
for s in from.stacks {
unstack(w, x + s.x, y + s.y, z + s.z);
for t in s.things { place(w, x + s.x, y + s.y, z + s.z, t); }
}
}
tests/ceilings.loft
// A cell's ceiling: how many steps it hangs below the floor of the layer above, for a renderer that
// draws ceilings. It shares the cell's word with the edges, the rise and the slant, and no routine
// reads it. Every answer is worked out by hand in its cell.
use tile_world;
const FLOOR = 1;
const WALL = 2;
const LAMP = 1;
const E_DOOR = 1;
// A floor x 0 .. 4, y 0 .. 2 on layer 0, walled round, under a ceiling: layer 1 is wall over it.
fn room() -> tile_world::World {
w = tile_world::world();
tile_world::terrain_kind(w, FLOOR, true, false, false, ".");
tile_world::terrain_kind(w, WALL, false, true, false, "#");
tile_world::thing_kind(w, LAMP, 0, false, false, true, "*");
tile_world::edge_kind(w, E_DOOR, true, true, "+");
tile_world::fill(w, -1, -1, 5, 3, 0, WALL);
tile_world::fill(w, 0, 0, 4, 2, 0, FLOOR);
tile_world::fill(w, -1, -1, 5, 3, 1, WALL);
w
}
fn test_ceiling_starts_at_zero() {
w = room();
assert(tile_world::ceiling(w, 2, 1, 0) == 0, "no lowered ceiling to start with");
}
// Each height reads back; past 15 it is 15, below 0 it is 0; far out it makes its chunk.
fn test_a_ceiling_reads_back() {
w = room();
for s in [1, 3, 15, 0] {
tile_world::set_ceiling(w, 2, 1, 0, s);
assert(tile_world::ceiling(w, 2, 1, 0) == s, "{s} steps");
}
tile_world::set_ceiling(w, 1, 1, 0, 40);
assert(tile_world::ceiling(w, 1, 1, 0) == 15, "40 is 15");
tile_world::set_ceiling(w, 1, 1, 0, -2);
assert(tile_world::ceiling(w, 1, 1, 0) == 0, "-2 is 0");
tile_world::set_ceiling(w, 90, -50, -3, 2);
assert(tile_world::ceiling(w, 90, -50, -3) == 2 && tile_world::ceiling(w, 91, -50, -3) == 0, "far out, its neighbour none");
}
// All five in one word at their highest — north and west edge 255, rise 15, slant 4 (north-west),
// ceiling 15 — each reads back; taking the ceiling away keeps the other four, and setting them
// keeps the ceiling.
fn test_a_ceiling_keeps_the_rest_of_the_word() {
w = room();
tile_world::set_edge(w, 3, 1, 0, tile_world::NORTH, 255);
tile_world::set_edge(w, 3, 1, 0, tile_world::WEST, 255);
tile_world::set_rise(w, 3, 1, 0, 15);
tile_world::set_slant(w, 3, 1, 0, tile_world::NORTH_WEST);
tile_world::set_ceiling(w, 3, 1, 0, 15);
assert(tile_world::edge(w, 3, 1, 0, tile_world::NORTH) == 255 && tile_world::edge(w, 3, 1, 0, tile_world::WEST) == 255, "the edges");
assert(tile_world::rise(w, 3, 1, 0) == 15 && tile_world::slant(w, 3, 1, 0) == 4, "the rise and the slant");
assert(tile_world::ceiling(w, 3, 1, 0) == 15, "the ceiling");
tile_world::set_ceiling(w, 3, 1, 0, 0);
assert(tile_world::rise(w, 3, 1, 0) == 15 && tile_world::slant(w, 3, 1, 0) == 4
&& tile_world::edge(w, 3, 1, 0, tile_world::WEST) == 255, "the ceiling taken away: the rest kept");
tile_world::set_ceiling(w, 3, 1, 0, 9);
tile_world::set_rise(w, 3, 1, 0, 0);
tile_world::set_slant(w, 3, 1, 0, 0);
tile_world::set_edge(w, 3, 1, 0, tile_world::NORTH, 0);
assert(tile_world::ceiling(w, 3, 1, 0) == 9, "the rest taken away: the ceiling kept");
}
// No routine reads it: walking, sight, light and the open sky are the same with every ceiling of
// the room hung as low as it goes.
fn test_no_routine_reads_a_ceiling() {
w = room();
tile_world::place(w, 0, 1, 0, LAMP);
path = tile_world::shortest_path(w, 0, 0, 4, 2, 0);
lit = tile_world::shades(w, 0, 0, 4, 2, 0);
for y in 0..3 { for x in 0..5 { tile_world::set_ceiling(w, x, y, 0, 15); } }
assert(tile_world::shortest_path(w, 0, 0, 4, 2, 0) == path && len(path) == 6, "the same path, 6 steps");
assert(tile_world::shades(w, 0, 0, 4, 2, 0) == lit, "the same light");
assert(tile_world::passable(w, 2, 1, 0) && !tile_world::opaque(w, 2, 1, 0), "still walked, still seen through");
assert(tile_world::line_of_sight(w, 0, 0, 4, 2, 0) && !tile_world::open_sky(w, 2, 1, 0), "sight, and under its ceiling");
}
// A stamp carries it, and pasting puts it at the stamp's place.
fn test_a_ceiling_is_stamped() {
w = room();
tile_world::set_ceiling(w, 1, 0, 0, 3);
s = tile_world::copy(w, 0, 0, 2, 1, 0, 1);
assert(tile_world::ceiling(s, 1, 0, 0) == 3, "in the stamp at (1, 0)");
tile_world::paste(w, s, 20, 20, 4);
assert(tile_world::ceiling(w, 21, 20, 4) == 3 && tile_world::ceiling(w, 20, 20, 4) == 0, "pasted at (21, 20, 4) only");
}
tests/changes.loft
// The world's changes counted (`changes`, `changed_since`): what counts, what does not, and where
// a change is told — by blocks of 8 x 8 cells on a layer, a layer's ground and the kinds
// everywhere. Kinds: floor, wall; a box; an edge wall. Every answer worked out by hand.
use tile_world;
const FLOOR = 1;
const WALL = 2;
const BOX = 1;
const E_WALL = 1;
// A floor x 0 .. 15, y 0 .. 15 on layer 0; the count remembered after it.
fn floor() -> tile_world::World {
w = tile_world::world();
tile_world::terrain_kind(w, FLOOR, true, false, false, ".");
tile_world::terrain_kind(w, WALL, false, true, false, "#");
tile_world::thing_kind(w, BOX, 1, true, false, false, "b");
tile_world::edge_kind(w, E_WALL, false, true, "|");
tile_world::fill(w, 0, 0, 15, 15, 0, FLOOR);
w
}
fn test_floor_counts_kinds_and_cells() {
w = floor();
assert(tile_world::changes(w) == 4 + 256, "four kinds and 256 cells: {tile_world::changes(w)}");
}
fn test_a_new_world_has_no_changes() {
w = tile_world::world();
assert(tile_world::changes(w) == 0, "none");
assert(!tile_world::changed_since(w, -100, -100, 100, 100, -5, 5, 0), "nothing changed");
}
// Each kind declared and each cell set counts one.
fn test_every_change_counts_one() {
w = floor();
assert(tile_world::changes(w) == 260, "four kinds, 256 cells: {tile_world::changes(w)}");
n = tile_world::changes(w);
tile_world::set_terrain(w, 3, 3, 0, WALL);
tile_world::set_edge(w, 3, 3, 0, tile_world::NORTH, E_WALL);
tile_world::set_rise(w, 4, 3, 0, 2);
tile_world::set_slant(w, 5, 3, 0, tile_world::NORTH_EAST);
tile_world::set_ceiling(w, 6, 3, 0, 1);
tile_world::place(w, 7, 3, 0, BOX);
_ = tile_world::take(w, 7, 3, 0);
tile_world::clear(w, 3, 3, 0);
assert(tile_world::changes(w) == n + 8, "eight changes: {tile_world::changes(w) - n}");
}
// Setting a cell to what it is, clearing an unset one, taking from an empty cell: no change.
fn test_a_write_that_changes_nothing_counts_nothing() {
w = floor();
n = tile_world::changes(w);
tile_world::set_terrain(w, 3, 3, 0, FLOOR);
tile_world::fill(w, 0, 0, 15, 15, 0, FLOOR);
tile_world::set_rise(w, 4, 3, 0, 0);
tile_world::set_edge(w, 3, 3, 0, tile_world::WEST, 0);
tile_world::clear(w, 40, 40, 0);
_ = tile_world::take(w, 7, 3, 0);
assert(tile_world::changes(w) == n, "nothing changed: {tile_world::changes(w) - n}");
assert(!tile_world::changed_since(w, 0, 0, 15, 15, 0, 0, n), "nor anywhere");
}
// A change is told in its block of 8 x 8 cells on its layer: a box that overlaps the block hears
// it, though the cell is outside the box; one beside the block, or on another layer, does not.
fn test_a_change_is_told_by_its_block() {
w = floor();
n = tile_world::changes(w);
tile_world::set_terrain(w, 10, 3, 0, WALL);
assert(tile_world::changed_since(w, 10, 3, 10, 3, 0, 0, n), "the cell itself");
assert(tile_world::changed_since(w, 8, 0, 8, 0, 0, 0, n), "the block's corner (8, 0) .. (15, 7)");
assert(tile_world::changed_since(w, 0, 0, 8, 7, 0, 0, n), "a box reaching into the block");
assert(!tile_world::changed_since(w, 0, 0, 7, 15, 0, 0, n), "the blocks west of it");
assert(!tile_world::changed_since(w, 8, 8, 15, 15, 0, 0, n), "the block south of it");
assert(!tile_world::changed_since(w, 10, 3, 10, 3, 1, 1, n), "the layer above");
assert(!tile_world::changed_since(w, 10, 3, 10, 3, 0, 0, n + 1), "nothing after it");
}
// West and north of the origin the blocks are the floor of a coordinate over 8: -1 is in the block
// -8 .. -1, not with 0.
fn test_the_blocks_west_of_the_origin() {
w = floor();
n = tile_world::changes(w);
tile_world::set_terrain(w, -1, -1, 0, WALL);
assert(tile_world::changed_since(w, -8, -8, -8, -8, 0, 0, n), "the block -8 .. -1");
assert(!tile_world::changed_since(w, 0, 0, 0, 0, 0, 0, n), "not the block 0 .. 7");
}
// A thing put down or taken, and an edge, are changes of their cell.
fn test_things_and_edges_are_told_at_their_cell() {
w = floor();
n = tile_world::changes(w);
tile_world::place(w, 12, 12, 0, BOX);
assert(tile_world::changed_since(w, 12, 12, 12, 12, 0, 0, n), "put down");
assert(!tile_world::changed_since(w, 0, 0, 7, 7, 0, 0, n), "not elsewhere");
m = tile_world::changes(w);
_ = tile_world::take(w, 12, 12, 0);
assert(tile_world::changed_since(w, 12, 12, 12, 12, 0, 0, m), "taken");
// The east edge of (7, 0) is held by (8, 0): its block is (8, 0) .. (15, 7).
k = tile_world::changes(w);
tile_world::set_edge(w, 7, 0, 0, tile_world::EAST, E_WALL);
assert(tile_world::changed_since(w, 8, 0, 8, 0, 0, 0, k), "the edge's cell");
}
// A layer's ground reaches every cell of it; a kind, every cell of every layer.
fn test_a_ground_or_a_kind_changes_everything_it_reaches() {
w = floor();
n = tile_world::changes(w);
tile_world::set_ground(w, 2, FLOOR);
assert(tile_world::changed_since(w, 500, 500, 500, 500, 2, 2, n), "anywhere on layer 2");
assert(!tile_world::changed_since(w, 500, 500, 500, 500, 0, 1, n), "not on layers 0 and 1");
m = tile_world::changes(w);
tile_world::thing_kind(w, BOX, 2, true, false, false, "B");
assert(tile_world::changed_since(w, -900, -900, -900, -900, -7, -7, m), "a kind: everywhere");
}
// A pasted stamp is a change of each cell it replaces, and of each stack it takes away.
fn test_a_paste_counts_what_it_replaces() {
w = floor();
tile_world::place(w, 20, 20, 0, BOX);
st = tile_world::copy(w, 0, 0, 1, 1, 0, 0);
n = tile_world::changes(w);
tile_world::paste(w, st, 0, 0, 0);
assert(tile_world::changes(w) == n, "the same cells pasted on themselves: no change");
tile_world::set_terrain(st, 0, 0, 0, WALL);
tile_world::paste(w, st, 20, 20, 0);
assert(tile_world::changed_since(w, 20, 20, 20, 20, 0, 0, n), "the cell replaced, its box gone");
assert(tile_world::terrain(w, 20, 20, 0) == WALL && tile_world::top(w, 20, 20, 0) == 0, "as pasted");
}
tests/edges.loft
// Edges between cells, a layer's ground, and a cell's rise. Kinds: floor (walkable), wall
// (opaque); a box (1 high, blocks) and a lamp (a light). Edge kinds: a wall (not crossed,
// opaque), a door (crossed, opaque), a window (not crossed, see-through). Every answer is
// worked out by hand in its cell.
use tile_world;
const FLOOR = 1;
const WALL = 2;
const BOX = 1;
const LAMP = 2;
const E_WALL = 1;
const E_DOOR = 2;
const E_WINDOW = 3;
// A room x 0 .. 4, y 0 .. 2 on layer 0, walled round, under a ceiling.
fn room() -> tile_world::World {
w = tile_world::world();
tile_world::terrain_kind(w, FLOOR, true, false, false, ".");
tile_world::terrain_kind(w, WALL, false, true, false, "#");
tile_world::thing_kind(w, BOX, 1, true, false, false, "b");
tile_world::thing_kind(w, LAMP, 0, false, false, true, "*");
tile_world::edge_kind(w, E_WALL, false, true, "|");
tile_world::edge_kind(w, E_DOOR, true, true, "+");
tile_world::edge_kind(w, E_WINDOW, false, false, "=");
tile_world::fill(w, -1, -1, 5, 3, 0, WALL);
tile_world::fill(w, 0, 0, 4, 2, 0, FLOOR);
tile_world::fill(w, -1, -1, 5, 3, 1, FLOOR);
w
}
fn test_no_edges_to_start_with() {
w = room();
assert(tile_world::edge(w, 2, 2, 0, tile_world::NORTH) == 0, "no edges to start with");
}
// A cell's east edge is its east neighbour's west edge; its south edge is the cell below's north.
fn test_an_edge_is_shared_by_its_two_cells() {
w = room();
tile_world::set_edge(w, 2, 1, 0, tile_world::EAST, E_WALL);
assert(tile_world::edge(w, 3, 1, 0, tile_world::WEST) == E_WALL, "the same edge from the east");
tile_world::set_edge(w, 2, 1, 0, tile_world::SOUTH, E_WINDOW);
assert(tile_world::edge(w, 2, 2, 0, tile_world::NORTH) == E_WINDOW, "the same edge from below");
assert(tile_world::edge(w, 2, 1, 0, tile_world::EAST) == E_WALL, "setting the south kept the east");
tile_world::set_edge(w, 3, 1, 0, tile_world::WEST, 0);
assert(tile_world::edge(w, 2, 1, 0, tile_world::EAST) == 0, "taken away from either side");
tile_world::set_edge(w, -40, 70, 0, tile_world::NORTH, E_DOOR);
assert(tile_world::edge(w, -40, 69, 0, tile_world::SOUTH) == E_DOOR, "far out, in a new chunk");
}
// Crossing and seeing across each edge kind, both ways.
fn test_crossing_and_seeing() {
w = room();
tile_world::set_edge(w, 1, 0, 0, tile_world::EAST, E_WALL);
tile_world::set_edge(w, 1, 1, 0, tile_world::EAST, E_DOOR);
tile_world::set_edge(w, 1, 2, 0, tile_world::EAST, E_WINDOW);
assert(!tile_world::crossable(w, 1, 0, 0, 1, 0) && !tile_world::crossable(w, 2, 0, 0, -1, 0), "a wall, both ways");
assert(!tile_world::see_through(w, 1, 0, 0, 1, 0), "a wall stops sight");
assert(tile_world::crossable(w, 2, 1, 0, -1, 0) && !tile_world::see_through(w, 2, 1, 0, -1, 0), "a door: crossed, not seen through");
assert(!tile_world::crossable(w, 1, 2, 0, 1, 0) && tile_world::see_through(w, 1, 2, 0, 1, 0), "a window: seen through, not crossed");
assert(tile_world::crossable(w, 0, 0, 0, 0, 1) && tile_world::see_through(w, 0, 0, 0, 0, 1), "no edge");
tile_world::set_edge(w, 3, 0, 0, tile_world::EAST, 9);
assert(!tile_world::crossable(w, 3, 0, 0, 1, 0) && !tile_world::see_through(w, 3, 0, 0, 1, 0), "an undeclared edge kind is a wall");
}
// Wall edges between x 1 and x 2 on rows 0 and 1 leave a gap on row 2: from (0, 0) to (3, 0) the
// walk goes down to row 2, across, and back up: 3 across + 2 down + 2 up = 7 steps.
fn test_a_path_goes_round_wall_edges() {
w = room();
assert(len(tile_world::shortest_path(w, 0, 0, 3, 0, 0)) == 3, "3 steps with no edges");
tile_world::set_edge(w, 1, 0, 0, tile_world::EAST, E_WALL);
tile_world::set_edge(w, 1, 1, 0, tile_world::EAST, E_WALL);
p = tile_world::shortest_path(w, 0, 0, 3, 0, 0);
assert(len(p) == 7, "7 steps, got {len(p)}");
px = 0;
crossed_at = -1;
for c in p {
if px == 1 && c.0 == 2 { crossed_at = c.1; }
px = c.0;
}
assert(crossed_at == 2, "crossed from x 1 to x 2 on row 2, got {crossed_at}");
tile_world::set_edge(w, 1, 2, 0, tile_world::EAST, E_DOOR);
assert(len(tile_world::shortest_path(w, 0, 0, 3, 0, 0)) == 7, "a door on the gap is still a way through");
tile_world::set_edge(w, 1, 2, 0, tile_world::EAST, E_WINDOW);
assert(len(tile_world::shortest_path(w, 0, 0, 3, 0, 0)) == 0, "a window is not");
}
// Light from a lamp at (0, 0), along row 0 from x -1 (the west wall) to x 5 (the east wall).
// Walls on the east edges of x 2, every row: x 3 and on are dark. Windows instead: the light
// passes, one shade a step.
fn row(w: const tile_world::World) -> text {
t = "";
for s in tile_world::shades(w, -1, 0, 5, 0, 0) { t += "{s},"; }
t
}
fn test_light_and_sight_stop_at_opaque_edges() {
w = room();
tile_world::place(w, 0, 0, 0, LAMP);
for y in 0..3 { tile_world::set_edge(w, 2, y, 0, tile_world::EAST, E_WALL); }
assert(row(w) == "1,0,1,2,7,7,7,", "wall edges: {row(w)}");
assert(!tile_world::line_of_sight(w, 0, 1, 4, 1, 0), "no sight across them");
for y in 0..3 { tile_world::set_edge(w, 2, y, 0, tile_world::EAST, E_WINDOW); }
assert(row(w) == "1,0,1,2,3,4,5,", "window edges: {row(w)}");
assert(tile_world::line_of_sight(w, 0, 1, 4, 1, 0), "sight through the windows");
}
// One diagonal step from (0, 0) to (1, 1) passes the corner either way round: with the east edge
// of (0, 0) a wall it goes round by (0, 1); with the south edge a wall too, there is no way.
fn test_a_diagonal_sees_round_one_side_of_a_corner() {
w = room();
tile_world::set_edge(w, 0, 0, 0, tile_world::EAST, E_WALL);
assert(tile_world::line_of_sight(w, 0, 0, 1, 1, 0), "round by the south");
tile_world::set_edge(w, 0, 0, 0, tile_world::SOUTH, E_WALL);
assert(!tile_world::line_of_sight(w, 0, 0, 1, 1, 0), "both ways shut");
}
// A ground: every unset cell of layer 0 reads as floor with tile 7 and costs no chunk; a cell set
// to void is a pit; `clear` gives it back to the ground. A ground on the layer above is a
// ceiling over every cell.
fn test_a_ground_is_open_land_for_nothing() {
w = room();
v = tile_world::world();
tile_world::terrain_kind(v, FLOOR, true, false, false, ".");
tile_world::set_ground(v, 0, FLOOR, 7);
assert(tile_world::terrain(v, 500, -300, 0) == FLOOR && tile_world::tile(v, 500, -300, 0) == 7, "floor, tile 7, far out");
assert(tile_world::chunk_count(v) == 0 && tile_world::passable(v, 500, -300, 0), "no chunk, and walkable");
assert(tile_world::open_sky(v, 3, 3, 0), "nothing above layer 0");
tile_world::set_terrain(v, 1, 1, 0, tile_world::VOID);
assert(tile_world::terrain(v, 1, 1, 0) == tile_world::VOID && tile_world::is_set(v, 1, 1, 0), "a pit");
assert(!tile_world::passable(v, 1, 1, 0), "a pit is not walked on");
tile_world::clear(v, 1, 1, 0);
assert(tile_world::terrain(v, 1, 1, 0) == FLOOR && !tile_world::is_set(v, 1, 1, 0), "the ground again");
assert(tile_world::terrain(v, 9, 9, 1) == tile_world::VOID, "layer 1 has no ground");
tile_world::set_ground(v, 1, FLOOR);
assert(!tile_world::open_sky(v, 3, 3, 0), "a ground on layer 1 is a ceiling");
tile_world::set_ground(v, -2, FLOOR);
gl = "";
for z in tile_world::ground_layers(v) { gl += "{z},"; }
assert(gl == "-2,0,1,", "the layers with a ground: {gl}");
tile_world::set_ground(v, 0, tile_world::VOID);
assert(tile_world::terrain(v, 500, -300, 0) == tile_world::VOID && tile_world::ground(v, 0) == tile_world::VOID, "taken away");
assert(tile_world::terrain(w, 2, 2, 0) == FLOOR, "a world without a ground is as it was");
}
// A hillside: rises 1, 2, 3 at x 1, 2, 3 — each one step above the last. A box on the top
// stands 4. A rise keeps the cell's edges, and the edges keep it.
fn test_a_rise_makes_a_slope() {
w = room();
for x in 1..4 { tile_world::set_rise(w, x, 0, 0, x); }
assert(tile_world::height(w, 0, 0, 0) == 0 && tile_world::height(w, 2, 0, 0) == 2, "0 at the foot, 2 halfway");
tile_world::place(w, 3, 0, 0, BOX);
assert(tile_world::height(w, 3, 0, 0) == 4, "a box on rise 3 stands 4");
tile_world::set_edge(w, 2, 0, 0, tile_world::NORTH, E_WALL);
tile_world::set_edge(w, 2, 0, 0, tile_world::WEST, E_DOOR);
tile_world::set_rise(w, 2, 0, 0, 15);
assert(tile_world::rise(w, 2, 0, 0) == 15, "rise 15, the most");
assert(tile_world::edge(w, 2, 0, 0, tile_world::NORTH) == E_WALL && tile_world::edge(w, 2, 0, 0, tile_world::WEST) == E_DOOR,
"the edges stayed");
tile_world::set_edge(w, 2, 0, 0, tile_world::NORTH, 0);
assert(tile_world::rise(w, 2, 0, 0) == 15, "and the rise stayed");
}
tests/light.loft
// `shades` read through its window (the cells read once): a cell's shade does not hang on the box
// it is asked over — across chunk borders and west and north of the origin — and each thing the
// window reads counts as `lit`, `opaque` and `see_through` say. Kinds: floor, wall, lava (a light);
// a crate (opaque), a lamp (a light); edges: a door (opaque), a window (seen through). Every
// answer worked out by hand.
use tile_world;
const FLOOR = 1;
const WALL = 2;
const LAVA = 3;
const CRATE = 1;
const LAMP = 2;
const E_DOOR = 1;
const E_WINDOW = 2;
// A floor x -40 .. 40, y -40 .. 40 on layer 0, roofed on layer 1 but for a hole at (30, 30); a
// door on the west side of (1, 5) and a window on the west side of (1, -5); lava at (-30, -30); a
// crate at (33, 3); a lamp on void at (-20, 20).
fn world() -> tile_world::World {
w = tile_world::world();
tile_world::terrain_kind(w, FLOOR, true, false, false, ".");
tile_world::terrain_kind(w, WALL, false, true, false, "#");
tile_world::terrain_kind(w, LAVA, true, false, true, "~");
tile_world::thing_kind(w, CRATE, 1, true, true, false, "c");
tile_world::thing_kind(w, LAMP, 0, false, false, true, "*");
tile_world::edge_kind(w, E_DOOR, true, true, "+");
tile_world::edge_kind(w, E_WINDOW, false, false, "=");
tile_world::fill(w, -40, -40, 40, 40, 0, FLOOR);
tile_world::fill(w, -40, -40, 40, 40, 1, WALL);
tile_world::clear(w, 30, 30, 1);
tile_world::set_terrain(w, 30, 30, 1, tile_world::VOID);
tile_world::set_terrain(w, -30, -30, 0, LAVA);
tile_world::place(w, 33, 3, 0, CRATE);
tile_world::set_terrain(w, -20, 20, 0, tile_world::VOID);
tile_world::place(w, -20, 20, 0, LAMP);
tile_world::set_edge(w, 1, 5, 0, tile_world::WEST, E_DOOR);
tile_world::set_edge(w, 1, -5, 0, tile_world::WEST, E_WINDOW);
w
}
fn test_shades_box_length() {
w = world();
assert(len(tile_world::shades(w, 0, 0, 3, 3, 0)) == 16, "a box of 16");
}
// The same cells asked over a big box and over small ones, by day and by night, with a light the
// game carries at (5, 0): the same shades.
fn test_a_shade_does_not_hang_on_the_box() {
w = world();
for day in [true, false] {
big = tile_world::shades(w, -36, -36, 36, 36, 0, [(5, 0)], [1], day);
for b in [(-36, -36, -30, -30), (-2, -8, 5, 8), (28, 28, 33, 33), (31, 1, 34, 5), (-22, 18, -18, 22), (-1, -1, 0, 0)] {
small = tile_world::shades(w, b.0, b.1, b.2, b.3, 0, [(5, 0)], [1], day);
i = 0;
for y in b.1..=b.3 {
for x in b.0..=b.2 {
s = small[i] ?? -1;
l = big[(y + 36) * 73 + (x + 36)] ?? -2;
assert(s == l, "({x}, {y}) day {day}: {s} in a small box, {l} in the big one");
i += 1;
}
}
}
}
}
// Cell (x, y)'s shade of the box -40 .. 40 both ways.
fn at(sh: const vector<integer>, x: integer, y: integer) -> integer {
sh[(y + 40) * 81 + (x + 40)] ?? -1
}
// What the window reads, each as its routine says.
fn test_the_window_reads_what_the_routines_say() {
w = world();
sh = tile_world::shades(w, -40, -40, 40, 40, 0);
// The hole in the roof is the open sky by day: a light; not by night.
assert(at(sh, 30, 30) == 0, "under the hole: {at(sh, 30, 30)}");
assert(at(sh, 30, 33) == 3, "three cells from it: {at(sh, 30, 33)}");
night = tile_world::shades(w, 30, 30, 30, 30, 0, [], [], false);
assert((night[0] ?? -1) == tile_world::DARKEST, "the hole by night: {night[0] ?? -1}");
// Lava gives light.
assert(at(sh, -30, -30) == 0 && at(sh, -30, -27) == 3, "lava: {at(sh, -30, -30)}, {at(sh, -30, -27)}");
// A lamp on void gives none: a void cell is no light.
assert(at(sh, -20, 20) == tile_world::DARKEST, "the lamp on void: {at(sh, -20, 20)}");
// The crate is opaque: lit by a light beside it, but passing none on — the cell past it is lit
// round it, (32, 4), (33, 4), (34, 4), (34, 3): 4, not 2. By night, the roof's hole no light.
crate = tile_world::shades(w, 33, 3, 34, 3, 0, [(32, 3)], [], false);
assert((crate[0] ?? -1) == 1 && (crate[1] ?? -1) == 4, "the crate, past it: {crate[0] ?? -1}, {crate[1] ?? -1}");
// A light at (2, 5): the door (opaque) keeps it from crossing to (0, 5), which is lit round it,
// (1, 4), (0, 4), (0, 5): 4, not 2; the window lets a light at (2, -5) through to (0, -5): 2.
d = tile_world::shades(w, 0, 5, 1, 5, 0, [(2, 5)]);
assert((d[1] ?? -1) == 1 && (d[0] ?? -1) == 4, "round the door, not through it: {d[0] ?? -1}");
v = tile_world::shades(w, 0, -5, 1, -5, 0, [(2, -5)]);
assert((v[0] ?? -1) == 2, "through the window: {v[0] ?? -1}");
}
// `block` answers what `terrain`, `rise`, `slant`, `ceiling`, `edge` and `things` answer, cell by
// cell, two numbers a cell, across chunk borders and west and north of the origin.
fn test_a_block_reads_what_each_cell_answers() {
w = world();
tile_world::set_rise(w, -33, 2, 0, 3);
tile_world::set_slant(w, 31, -1, 0, tile_world::SOUTH_WEST);
tile_world::set_ceiling(w, 32, 0, 0, 15);
tile_world::set_edge(w, -1, -33, 0, tile_world::NORTH, E_DOOR);
for z in [0, 1, 2] {
b = tile_world::block(w, -34, -34, 34, 6, z);
i = 0;
for y in -34..=6 {
for x in -34..=34 {
v = b[2 * i] ?? -1;
e = b[2 * i + 1] ?? -1;
assert((v & 15) == tile_world::terrain(w, x, y, z), "({x}, {y}, {z}) kind");
assert(((v >> 4) & 15) == tile_world::rise(w, x, y, z), "({x}, {y}, {z}) rise");
assert(((v >> 8) & 7) == tile_world::slant(w, x, y, z), "({x}, {y}, {z}) slant");
assert(((v >> 11) & 15) == tile_world::ceiling(w, x, y, z), "({x}, {y}, {z}) ceiling");
assert((((v >> 15) & 1) == 1) == (len(tile_world::things(w, x, y, z)) > 0), "({x}, {y}, {z}) things");
assert((e & 255) == tile_world::edge(w, x, y, z, tile_world::NORTH), "({x}, {y}, {z}) north");
assert(((e >> 8) & 255) == tile_world::edge(w, x, y, z, tile_world::WEST), "({x}, {y}, {z}) west");
i += 1;
}
}
assert(2 * i == len(b), "two numbers a cell");
}
}
// `shades` with lights, rebuilt from `shades` without them and each light's `reach`: cell by cell
// the brightest. Lights by the door, by the window, in the open, two of them overlapping, one
// burnt down (start 3), by day and by night, over a box round them all.
fn rebuilt(w: const tile_world::World, ls: const vector<(integer, integer)>, starts: const vector<integer>,
day: boolean, x0: integer, y0: integer, x1: integer, y1: integer) -> vector<integer> {
out = tile_world::shades(w, x0, y0, x1, y1, 0, [], [], day);
d = tile_world::DARKEST;
for k in 0..len(ls) {
l = ls[k];
r = tile_world::reach(w, l.0, l.1, 0, starts[k] ?? 0);
for j in 0..2 * d + 1 {
for i in 0..2 * d + 1 {
x = l.0 - d + i;
y = l.1 - d + j;
if x < x0 || x > x1 || y < y0 || y > y1 { continue; }
c = (y - y0) * (x1 - x0 + 1) + (x - x0);
s = r[j * (2 * d + 1) + i] ?? d;
if s < (out[c] ?? d) { out[c] = s; }
}
}
}
out
}
fn test_the_shades_are_the_brightest_of_each_lights_reach() {
w = world();
ls = [(3, 5), (-1, -5), (8, 0), (10, 2), (20, -12)];
starts = [0, 1, 0, 3, 2];
for day in [true, false] {
want = tile_world::shades(w, -12, -24, 32, 12, 0, ls, starts, day);
got = rebuilt(w, ls, starts, day, -12, -24, 32, 12);
differ = 0;
for c in 0..len(want) { if (want[c] ?? 0) != (got[c] ?? 0) { differ += 1; } }
assert(differ == 0, "day {day}: {differ} cells differ");
}
}
// One light alone: full at its cell, a shade darker a step, not through the door west of (1, 5).
fn test_a_lights_reach_is_its_own() {
w = world();
d = tile_world::DARKEST;
side = 2 * d + 1;
r = tile_world::reach(w, 1, 5, 0);
assert(len(r) == side * side, "a square of {side}: {len(r)}");
assert(r[d * side + d] == 0 && r[d * side + d + 1] == 1 && r[(d + 2) * side + d] == 2, "0, then 1, 2 steps away");
// West of the door: the long way round it, or not at all.
assert(r[d * side + d - 1] > 1, "the door passes no light: {r[d * side + d - 1]}");
dim = tile_world::reach(w, 1, 5, 0, 3);
assert(dim[d * side + d] == 3 && dim[d * side + d + 1] == 4, "burnt down: from 3");
assert(tile_world::reach(w, 1, 5, 0, d)[d * side + d] == d, "a light that starts at DARKEST gives none");
}
// The exception `reach` names: a light on an opaque cell (the crate at (33, 3)) that another light
// reaches more brightly than it starts. `shades` passes the brighter light on from the crate;
// the crate's own reach starts at its start.
fn test_a_light_on_an_opaque_cell_passes_on_only_its_own() {
w = world();
ls = [(33, 3), (32, 3)];
starts = [4, 0];
want = tile_world::shades(w, 28, 0, 38, 6, 0, ls, starts, false);
got = rebuilt(w, ls, starts, false, 28, 0, 38, 6);
differ = 0;
for c in 0..len(want) { if (want[c] ?? 0) != (got[c] ?? 0) { differ += 1; } }
assert(differ > 0, "the crate passes on the brighter light in shades");
}
tests/slants.loft
// A cell's slant: a 45-degree face, which corner its open half lies in. A picture only: it is
// kept apart from the edges and the rise in the cell's word, and walking, sight, light and the
// stacks read a slanted cell as its kind, whole. Every answer is worked out by hand in its cell.
use tile_world;
const FLOOR = 1;
const WALL = 2;
const BOX = 1;
const E_DOOR = 1;
// A floor x 0 .. 4, y 0 .. 2 on layer 0, walled round; the layer above is floor.
fn room() -> tile_world::World {
w = tile_world::world();
tile_world::terrain_kind(w, FLOOR, true, false, false, ".");
tile_world::terrain_kind(w, WALL, false, true, false, "#");
tile_world::thing_kind(w, BOX, 1, true, false, false, "b");
tile_world::edge_kind(w, E_DOOR, true, true, "+");
tile_world::fill(w, -1, -1, 5, 3, 0, WALL);
tile_world::fill(w, 0, 0, 4, 2, 0, FLOOR);
w
}
fn test_no_slant_to_start_with() {
w = room();
assert(tile_world::slant(w, -1, -1, 0) == 0, "no slant to start with");
}
// Each of the four ways reads back; 0 takes it away; a value outside the four is none.
fn test_a_slant_reads_back() {
w = room();
tile_world::set_slant(w, -1, -1, 0, tile_world::SOUTH_EAST);
assert(tile_world::slant(w, -1, -1, 0) == tile_world::SOUTH_EAST, "south-east");
tile_world::set_slant(w, 5, -1, 0, tile_world::SOUTH_WEST);
tile_world::set_slant(w, 5, 3, 0, tile_world::NORTH_WEST);
tile_world::set_slant(w, -1, 3, 0, tile_world::NORTH_EAST);
assert(tile_world::slant(w, 5, -1, 0) == 3 && tile_world::slant(w, 5, 3, 0) == 4
&& tile_world::slant(w, -1, 3, 0) == 1, "the other three: 3, 4 and 1");
assert(tile_world::slant(w, 0, -1, 0) == 0, "the neighbour keeps none");
tile_world::set_slant(w, -1, -1, 0, 0);
assert(tile_world::slant(w, -1, -1, 0) == 0, "0 takes it away");
tile_world::set_slant(w, 2, -1, 0, 9);
assert(tile_world::slant(w, 2, -1, 0) == 0, "9 is not a way: none");
tile_world::set_slant(w, -70, 40, 3, tile_world::NORTH_WEST);
assert(tile_world::slant(w, -70, 40, 3) == 4, "far out, in a new chunk, on another layer");
}
// The slant shares the cell's word with its north and west edges and its rise: setting any one
// keeps the other three.
fn test_a_slant_keeps_the_edges_and_the_rise() {
w = room();
tile_world::set_edge(w, 2, 0, 0, tile_world::NORTH, E_DOOR);
tile_world::set_edge(w, 2, 0, 0, tile_world::WEST, E_DOOR);
tile_world::set_rise(w, 2, 0, 0, 15);
tile_world::set_slant(w, 2, 0, 0, tile_world::NORTH_WEST);
assert(tile_world::edge(w, 2, 0, 0, tile_world::NORTH) == E_DOOR
&& tile_world::edge(w, 2, 0, 0, tile_world::WEST) == E_DOOR, "the edges kept");
assert(tile_world::rise(w, 2, 0, 0) == 15, "the highest rise kept");
assert(tile_world::slant(w, 2, 0, 0) == 4, "the slant beside them");
tile_world::set_rise(w, 2, 0, 0, 0);
tile_world::set_edge(w, 2, 0, 0, tile_world::NORTH, 0);
assert(tile_world::slant(w, 2, 0, 0) == 4, "taking the rise and an edge away keeps the slant");
tile_world::set_slant(w, 2, 0, 0, 0);
assert(tile_world::edge(w, 2, 0, 0, tile_world::WEST) == E_DOOR, "taking the slant away keeps the west edge");
}
// A slanted wall is a wall: not walked into, opaque; a slanted floor is still walked on. A path
// round a slanted corner takes the same steps as round a whole one.
fn test_a_slanted_cell_is_its_kind() {
w = room();
tile_world::set_slant(w, 2, -1, 0, tile_world::SOUTH_EAST);
assert(!tile_world::passable(w, 2, -1, 0), "a slanted wall is not walked into");
assert(tile_world::opaque(w, 2, -1, 0), "a slanted wall stops sight");
tile_world::fill(w, 2, 1, 2, 1, 0, WALL);
before = tile_world::shortest_path(w, 1, 1, 3, 1, 0);
tile_world::set_slant(w, 2, 1, 0, tile_world::NORTH_EAST);
after = tile_world::shortest_path(w, 1, 1, 3, 1, 0);
assert(len(before) == 4 && len(after) == 4, "round the wall in 4 steps, slanted or not: {len(before)} {len(after)}");
assert(!tile_world::line_of_sight(w, 1, 1, 3, 1, 0), "the slanted wall between still blocks sight");
tile_world::set_slant(w, 0, 0, 0, tile_world::NORTH_WEST);
assert(tile_world::passable(w, 0, 0, 0), "a slanted floor is still floor");
tile_world::place(w, 0, 0, 0, BOX);
assert(tile_world::height(w, 0, 0, 0) == 1 && tile_world::slant(w, 0, 0, 0) == 4, "a stack on it; the slant kept");
}
// The slant goes with the cell when the terrain changes, and with a stamp.
fn test_a_slant_is_kept_and_stamped() {
w = room();
tile_world::set_slant(w, 0, -1, 0, tile_world::SOUTH_WEST);
tile_world::fill(w, 0, -1, 0, -1, 0, FLOOR);
assert(tile_world::slant(w, 0, -1, 0) == 3, "kept when the terrain changes");
tile_world::fill(w, 0, -1, 0, -1, 0, WALL);
s = tile_world::copy(w, -1, -1, 1, 0, 0, 0);
assert(tile_world::slant(s, 1, 0, 0) == 3, "in the stamp at (1, 0)");
tile_world::paste(w, s, 10, 10, 2);
assert(tile_world::slant(w, 11, 10, 2) == 3, "pasted at (11, 10, 2)");
assert(tile_world::slant(w, 10, 10, 2) == 0 && !tile_world::passable(w, 11, 10, 2), "its neighbour none; still a wall");
}
tests/stamps.loft
// Copyright (c) 2026 Jurjen Stellingwerff
// SPDX-License-Identifier: LGPL-3.0-or-later
//
// Regions and stamps: `region` (what a fill paints), `copy` and `paste` (a stamp). Every
// expected value is worked out by hand in its cell.
use tile_world;
const FLOOR = 1;
const WALL = 2;
const BOX = 1;
const LAMP = 2;
const FENCE = 1;
fn kinds(w: tile_world::World) {
tile_world::terrain_kind(w, FLOOR, true, false, false, ".");
tile_world::terrain_kind(w, WALL, false, true, false, "#");
tile_world::thing_kind(w, BOX, 1, true, false, false, "b");
tile_world::thing_kind(w, LAMP, 0, false, false, true, "*");
tile_world::edge_kind(w, FENCE, false, false, "-");
}
// Floor x 0..4, y 0..2 on layer 0, with a wall at (2, 0) and (2, 1): the west part (x 0..1)
// meets the east part (x 3..4) only through (2, 2).
//
// . . # . .
// . . # . .
// . . . . .
fn room() -> tile_world::World {
w = tile_world::world();
kinds(w);
tile_world::fill(w, 0, 0, 4, 2, 0, FLOOR);
tile_world::set_terrain(w, 2, 0, 0, WALL);
tile_world::set_terrain(w, 2, 1, 0, WALL);
w
}
fn show(cells: const vector<(integer, integer)>) -> text {
t = "";
for c in cells { t += "{c.0},{c.1} "; }
t
}
fn test_region_floor_round_wall() {
assert(len(tile_world::region(room(), 0, 0, 0, 100)) == 13, "the floor round the wall");
}
// Breadth first from (0, 0), east before west before south before north: (1, 0) and (0, 1)
// come next. The whole floor is 15 cells less the two walls: 13. The wall's region is its
// own two cells.
fn test_a_region_is_the_connected_same_terrain() {
w = room();
assert(show(tile_world::region(w, 0, 0, 0, 3)) == "0,0 1,0 0,1 ", "the first three: {show(tile_world::region(w, 0, 0, 0, 3))}");
assert(len(tile_world::region(w, 0, 0, 0, 100)) == 13, "all of it");
assert(show(tile_world::region(w, 2, 0, 0, 100)) == "2,0 2,1 ", "the wall");
assert(len(tile_world::region(w, 0, 0, 0, 0)) == 0, "a limit of none");
}
// A fence edge west of (3, 2) cuts the only way east: the west part (6) and (2, 2) — 7. A
// different tile is a different terrain: (0, 1) with tile 7 is left out, and with it nothing
// else, since (0, 2) and (1, 1) still connect the rest — 6.
fn test_edges_and_tiles_bound_a_region() {
w = room();
tile_world::set_edge(w, 3, 2, 0, tile_world::WEST, FENCE);
assert(len(tile_world::region(w, 0, 0, 0, 100)) == 7, "fenced off: {len(tile_world::region(w, 0, 0, 0, 100))}");
tile_world::set_terrain(w, 0, 1, 0, FLOOR, 7);
assert(len(tile_world::region(w, 0, 0, 0, 100)) == 6, "a tile apart: {len(tile_world::region(w, 0, 0, 0, 100))}");
}
// An unset layer with a ground has no end: the limit is the answer.
fn test_a_ground_region_stops_at_the_limit() {
w = tile_world::world();
kinds(w);
tile_world::set_ground(w, 0, FLOOR);
assert(len(tile_world::region(w, 5, 5, 0, 50)) == 50, "fifty");
}
// The room's corner x 0..1, y 0..1, with a rise 2 at (0, 0), a box at (1, 1) and a fence west
// of (1, 0), stamped at (10, 10, 0) into an empty world: the four cells, the rise, the box and
// the fence come along; (12, 10), outside the stamp, stays unset.
fn test_a_stamp_is_copied_and_pasted() {
w = room();
tile_world::set_rise(w, 0, 0, 0, 2);
tile_world::place(w, 1, 1, 0, BOX);
tile_world::set_edge(w, 1, 0, 0, tile_world::WEST, FENCE);
stamp = tile_world::copy(w, 0, 0, 1, 1, 0, 0);
assert(tile_world::terrain(stamp, 1, 1, 0) == FLOOR && tile_world::rise(stamp, 0, 0, 0) == 2, "the stamp holds them");
t = tile_world::world();
kinds(t);
tile_world::paste(t, stamp, 10, 10, 0);
assert(tile_world::terrain(t, 10, 10, 0) == FLOOR && tile_world::terrain(t, 11, 11, 0) == FLOOR, "the floor");
assert(tile_world::rise(t, 10, 10, 0) == 2, "the rise");
assert(tile_world::top(t, 11, 11, 0) == BOX, "the box");
assert(tile_world::edge(t, 11, 10, 0, tile_world::WEST) == FENCE, "the fence");
assert(!tile_world::is_set(t, 12, 10, 0), "outside the stamp");
}
// Pasting over a stack replaces it: a lamp at (11, 11) under the stamp's box leaves the box
// alone, and a lamp under a stamp cell with no stack (10, 11) is gone. A cell the stamp left
// unset keeps what was there: (2, 0) of a stamp of x 2..2 copied from an unset world.
fn test_a_paste_replaces_what_it_covers() {
w = room();
tile_world::place(w, 1, 1, 0, BOX);
stamp = tile_world::copy(w, 0, 0, 1, 1, 0, 0);
t = room();
tile_world::place(t, 11, 11, 0, LAMP);
tile_world::place(t, 10, 11, 0, LAMP);
tile_world::paste(t, stamp, 10, 10, 0);
assert(len(tile_world::things(t, 11, 11, 0)) == 1 && tile_world::top(t, 11, 11, 0) == BOX, "the box alone");
assert(tile_world::top(t, 10, 11, 0) == 0, "the lamp gone");
empty = tile_world::world();
tile_world::paste(t, tile_world::copy(empty, 0, 0, 0, 0, 0, 0), 2, 0, 0);
assert(tile_world::terrain(t, 2, 0, 0) == WALL, "an unset stamp cell changes nothing");
}
// A part to turn, three cells by two, the middle of its south row left unset:
//
// . . # rise 2 at (0, 0); the wall (2, 0) slanted, looking north-east;
// . . a fence north of (1, 0) and one west of (0, 1);
// a box on (2, 1); a lamp on the unset cell (1, 1).
fn part() -> tile_world::World {
w = tile_world::world();
kinds(w);
tile_world::set_terrain(w, 0, 0, 0, FLOOR);
tile_world::set_terrain(w, 1, 0, 0, FLOOR);
tile_world::set_terrain(w, 2, 0, 0, WALL);
tile_world::set_terrain(w, 0, 1, 0, FLOOR);
tile_world::set_terrain(w, 2, 1, 0, FLOOR);
tile_world::set_rise(w, 0, 0, 0, 2);
tile_world::set_slant(w, 2, 0, 0, tile_world::NORTH_EAST);
tile_world::set_edge(w, 1, 0, 0, tile_world::NORTH, FENCE);
tile_world::set_edge(w, 0, 1, 0, tile_world::WEST, FENCE);
tile_world::place(w, 2, 1, 0, BOX);
tile_world::place(w, 1, 1, 0, LAMP);
w
}
// How many edges a stamp holds in the box x -1..4, y -1..4, each counted once (a cell's north
// and west).
fn fences(w: const tile_world::World) -> integer {
n = 0;
for y in -1..5 {
for x in -1..5 {
if tile_world::edge(w, x, y, 0, tile_world::NORTH) != 0 { n += 1; }
if tile_world::edge(w, x, y, 0, tile_world::WEST) != 0 { n += 1; }
}
}
n
}
// The part's box is its set cells and its things, 3 by 2; turned a quarter it is 2 by 3.
// Turned a quarter clockwise, cell (x, y) lands on (1 - y, x):
//
// . . (0, 0) -> (1, 0) with its rise; (1, 0) -> (1, 1); the wall (2, 0) -> (1, 2), its
// . slant now looking south-east; (0, 1) -> (0, 0); (2, 1) -> (0, 2) with the box; the
// . # lamp's unset cell (1, 1) -> (0, 1). The fence north of (1, 0) is east of (1, 1);
// the fence west of (0, 1) is north of (0, 0).
fn test_a_stamp_turned_a_quarter() {
s = part();
assert(tile_world::stamp_box(s) == (0, 0, 2, 1), "its box");
assert(tile_world::stamp_size(s, 0) == (3, 2) && tile_world::stamp_size(s, 1) == (2, 3) && tile_world::stamp_size(s, 6) == (3, 2), "its size, turned");
assert(tile_world::stamp_spot(0, 0, 2, 1, 1, false, 2, 0) == (1, 2) && tile_world::stamp_spot(0, 0, 2, 1, 1, false, 0, 1) == (0, 0), "where a cell lands");
t = tile_world::turned(s, 1);
assert(tile_world::stamp_box(t) == (0, 0, 1, 2), "the turned box begins at (0, 0)");
assert(tile_world::terrain(t, 1, 0, 0) == FLOOR && tile_world::rise(t, 1, 0, 0) == 2, "(0, 0) on (1, 0), its rise with it");
assert(tile_world::terrain(t, 1, 1, 0) == FLOOR && tile_world::terrain(t, 0, 0, 0) == FLOOR && tile_world::terrain(t, 0, 2, 0) == FLOOR, "the floor");
assert(tile_world::terrain(t, 1, 2, 0) == WALL && tile_world::slant(t, 1, 2, 0) == tile_world::SOUTH_EAST, "the wall, its slant turned");
assert(!tile_world::is_set(t, 0, 1, 0) && tile_world::top(t, 0, 1, 0) == LAMP, "the unset cell stays unset, its lamp on it");
assert(tile_world::top(t, 0, 2, 0) == BOX && len(tile_world::things(t, 0, 2, 0)) == 1, "the box");
assert(tile_world::edge(t, 1, 1, 0, tile_world::EAST) == FENCE, "the north fence is an east one");
assert(tile_world::edge(t, 0, 0, 0, tile_world::NORTH) == FENCE, "the west fence is a north one");
assert(fences(t) == 2, "and no other edge: {fences(t)}");
}
// Mirrored east to west, cell (x, y) lands on (2 - x, y): the wall on (0, 0), its slant looking
// north-west; the fence north of (1, 0) stays; the fence west of (0, 1) is east of (2, 1).
// Mirrored and then turned a quarter, (x, y) lands on (1 - y, 2 - x).
fn test_a_stamp_mirrored() {
s = part();
f = tile_world::turned(s, 0, true);
assert(tile_world::terrain(f, 0, 0, 0) == WALL && tile_world::slant(f, 0, 0, 0) == tile_world::NORTH_WEST, "the wall, its slant mirrored");
assert(tile_world::rise(f, 2, 0, 0) == 2 && tile_world::top(f, 0, 1, 0) == BOX && tile_world::top(f, 1, 1, 0) == LAMP, "the rise, the box, the lamp");
assert(tile_world::edge(f, 1, 0, 0, tile_world::NORTH) == FENCE && tile_world::edge(f, 2, 1, 0, tile_world::EAST) == FENCE && fences(f) == 2, "the fences");
ft = tile_world::turned(s, 1, true);
assert(tile_world::terrain(ft, 1, 2, 0) == FLOOR && tile_world::rise(ft, 1, 2, 0) == 2, "mirrored then turned: (0, 0) on (1, 2)");
assert(tile_world::terrain(ft, 1, 0, 0) == WALL && tile_world::slant(ft, 1, 0, 0) == tile_world::NORTH_EAST, "the wall on (1, 0), looking north-east again");
assert(tile_world::turned_side(tile_world::WEST, 1, true) == tile_world::SOUTH && tile_world::turned_side(tile_world::NORTH, 3, false) == tile_world::WEST, "a side, mirrored and turned");
assert(tile_world::turned_slant(0, 2, true) == 0 && tile_world::turned_slant(tile_world::SOUTH_WEST, 2, false) == tile_world::NORTH_EAST, "a slant, turned");
}
// Are two stamps the same over x -1..4, y -1..4 on layer 0: terrain, tile, rise, slant, ceiling,
// both edges and the stack of every cell?
fn alike(a: const tile_world::World, b: const tile_world::World) -> boolean {
for y in -1..5 {
for x in -1..5 {
if tile_world::is_set(a, x, y, 0) != tile_world::is_set(b, x, y, 0) || tile_world::terrain(a, x, y, 0) != tile_world::terrain(b, x, y, 0)
|| tile_world::tile(a, x, y, 0) != tile_world::tile(b, x, y, 0) || tile_world::rise(a, x, y, 0) != tile_world::rise(b, x, y, 0)
|| tile_world::slant(a, x, y, 0) != tile_world::slant(b, x, y, 0) || tile_world::ceiling(a, x, y, 0) != tile_world::ceiling(b, x, y, 0)
|| tile_world::edge(a, x, y, 0, tile_world::NORTH) != tile_world::edge(b, x, y, 0, tile_world::NORTH)
|| tile_world::edge(a, x, y, 0, tile_world::WEST) != tile_world::edge(b, x, y, 0, tile_world::WEST)
|| tile_world::things(a, x, y, 0) != tile_world::things(b, x, y, 0) {
return false;
}
}
}
true
}
// Eight ways and no more: four quarters bring a stamp back, so do two mirrors, a quarter after a
// quarter is a half turn, and a mirror then a quarter is three quarters then a mirror. With a
// ceiling lowered on one cell, to go round with the rest.
fn test_the_eight_ways_a_stamp_lies() {
s = part();
tile_world::set_ceiling(s, 0, 1, 0, 3);
four = tile_world::turned(tile_world::turned(tile_world::turned(tile_world::turned(s, 1), 1), 1), 1);
assert(alike(four, s), "four quarters: the stamp again");
assert(alike(tile_world::turned(s, 4), s) && alike(tile_world::turned(s, -3), tile_world::turned(s, 1)), "a turn is counted in quarters, either way round");
assert(alike(tile_world::turned(tile_world::turned(s, 0, true), 0, true), s), "two mirrors: the stamp again");
assert(alike(tile_world::turned(tile_world::turned(s, 1), 1), tile_world::turned(s, 2)), "a quarter and a quarter: a half");
assert(alike(tile_world::turned(tile_world::turned(s, 0, true), 1), tile_world::turned(s, 1, true)), "mirrored, then turned");
assert(alike(tile_world::turned(tile_world::turned(s, 3), 0, true), tile_world::turned(s, 1, true)), "a mirror then a quarter is three quarters then a mirror");
assert(!alike(tile_world::turned(s, 1), s) && !alike(tile_world::turned(s, 0, true), s), "and a quarter, or a mirror, is another stamp");
assert(tile_world::ceiling(tile_world::turned(s, 1), 0, 0, 0) == 3, "the ceiling went with its cell");
}
// Laid, a stamp writes only what it holds. On a floor x 10..13, y 10..12 with a wall at
// (11, 11), a box on it, a rise 1 at (10, 10) and a fence east of (10, 10):
// - the cells the part set replace what is there: (10, 10) has the part's rise 2, (12, 10) is wall;
// - the cell it left unset, (11, 11), keeps its wall, but the part's lamp replaces its box;
// - the fences it holds are added; the fence east of (10, 10), which it does not hold, STAYS.
// Pasted, the same stamp replaces each cell it holds anything at, whole: that fence is gone.
// @TLW-002 — lay writes only set cells and edges, while paste replaces each touched cell whole.
fn test_a_stamp_laid_adds_and_never_removes() {
s = part();
for way in 0..2 {
w = tile_world::world();
kinds(w);
tile_world::fill(w, 10, 10, 13, 12, 0, FLOOR);
tile_world::set_terrain(w, 11, 11, 0, WALL);
tile_world::place(w, 11, 11, 0, BOX);
tile_world::set_rise(w, 10, 10, 0, 1);
tile_world::set_edge(w, 10, 10, 0, tile_world::EAST, FENCE);
if way == 0 { tile_world::lay(w, s, 10, 10, 0); } else { tile_world::paste(w, s, 10, 10, 0); }
assert(tile_world::rise(w, 10, 10, 0) == 2 && tile_world::terrain(w, 12, 10, 0) == WALL, "way {way}: the set cells replace");
assert(tile_world::terrain(w, 11, 11, 0) == WALL, "way {way}: the unset cell keeps its wall");
assert(len(tile_world::things(w, 11, 11, 0)) == 1 && tile_world::top(w, 11, 11, 0) == LAMP, "way {way}: the lamp replaces the box");
assert(tile_world::edge(w, 11, 10, 0, tile_world::NORTH) == FENCE && tile_world::edge(w, 10, 11, 0, tile_world::WEST) == FENCE, "way {way}: its fences added");
assert(tile_world::terrain(w, 13, 12, 0) == FLOOR && tile_world::rise(w, 13, 10, 0) == 0, "way {way}: beyond the part, as it was");
kept = tile_world::edge(w, 10, 10, 0, tile_world::EAST) == FENCE;
assert(kept == (way == 0), "way {way}: a fence the stamp does not hold stays when it is laid, and goes when it is pasted");
}
// laid turned: the part a quarter round, at (20, 20) of an empty world
e = tile_world::world();
kinds(e);
tile_world::lay(e, tile_world::turned(s, 1), 20, 20, 0);
assert(tile_world::terrain(e, 21, 22, 0) == WALL && tile_world::rise(e, 21, 20, 0) == 2 && tile_world::top(e, 20, 22, 0) == BOX, "the turned part, laid");
assert(!tile_world::is_set(e, 20, 21, 0) && tile_world::top(e, 20, 21, 0) == LAMP, "its unset cell unset, the lamp on it");
}
tests/world.loft
// Copyright (c) 2026 Jurjen Stellingwerff
// SPDX-License-Identifier: LGPL-3.0-or-later
//
// The world model and its routines, over kinds a test declares: floor (walkable), wall
// (opaque), window (neither walkable nor opaque); a box (1 high, blocks), a lamp (a light,
// walked over), a curtain (opaque, walked through). Every expected value is worked out by
// hand in its cell.
use tile_world;
const FLOOR = 1;
const WALL = 2;
const WINDOW = 3;
const BOX = 1;
const LAMP = 2;
const CURTAIN = 3;
fn kinds(w: tile_world::World) {
tile_world::terrain_kind(w, FLOOR, true, false, false, ".");
tile_world::terrain_kind(w, WALL, false, true, false, "#");
tile_world::terrain_kind(w, WINDOW, false, false, false, "=");
tile_world::thing_kind(w, BOX, 1, true, false, false, "b");
tile_world::thing_kind(w, LAMP, 0, false, false, true, "*");
tile_world::thing_kind(w, CURTAIN, 0, false, true, false, "|");
}
// A room x 0 .. 4, y 0 .. 2 on layer 0, walled round, under a ceiling (layer 1 set over it),
// so nothing is open sky.
fn room() -> tile_world::World {
w = tile_world::world();
kinds(w);
tile_world::fill(w, -1, -1, 5, 3, 0, WALL);
tile_world::fill(w, 0, 0, 4, 2, 0, FLOOR);
tile_world::fill(w, -1, -1, 5, 3, 1, FLOOR);
w
}
fn test_empty_world_is_void() {
w = tile_world::world();
assert(tile_world::terrain(w, 5, 5, 0) == tile_world::VOID, "an empty world is void");
}
fn test_addressing() {
assert(tile_world::chunk_of(0) == 0 && tile_world::chunk_of(31) == 0, "0 and 31 in chunk 0");
assert(tile_world::chunk_of(32) == 1 && tile_world::chunk_of(-1) == -1, "32 in 1, -1 in -1");
assert(tile_world::chunk_of(-32) == -1 && tile_world::chunk_of(-33) == -2, "-32 in -1, -33 in -2");
assert(tile_world::inside(-1) == 31 && tile_world::inside(-33) == 31 && tile_world::inside(33) == 1,
"slots 31, 31, 1");
}
// One cell far out and one on another layer make two chunks; a neighbour adds none.
fn test_sparse_layers() {
w = tile_world::world();
tile_world::set_terrain(w, -1000000, 7, 0, WINDOW, 12);
tile_world::set_terrain(w, -1000000, 7, -3, FLOOR, 4095);
assert(tile_world::chunk_count(w) == 2, "two chunks, got {tile_world::chunk_count(w)}");
tile_world::set_terrain(w, -999999, 8, 0, FLOOR);
assert(tile_world::chunk_count(w) == 2, "a neighbour in the same chunk adds none");
assert(tile_world::terrain(w, -1000000, 7, 0) == WINDOW && tile_world::tile(w, -1000000, 7, 0) == 12, "kind and tile");
assert(tile_world::tile(w, -1000000, 7, -3) == 4095, "the largest tile number");
assert(tile_world::terrain(w, -1000000, 7, -2) == tile_world::VOID, "nothing in between");
}
// Heights add up as declared (a box 1, an undeclared thing 1, a lamp 0); take answers the top
// and the stack goes when it empties.
fn test_stacks() {
w = room();
tile_world::place(w, 1, 1, 0, BOX);
tile_world::place(w, 1, 1, 0, BOX);
tile_world::place(w, 1, 1, 0, LAMP);
assert(tile_world::height(w, 1, 1, 0) == 2, "two boxes and a lamp: 2");
tile_world::place(w, 2, 1, 0, 99);
assert(tile_world::height(w, 2, 1, 0) == 1, "an undeclared thing is 1 high");
assert(tile_world::take(w, 1, 1, 0) == LAMP && tile_world::top(w, 1, 1, 0) == BOX, "the lamp came off");
_ = tile_world::take(w, 1, 1, 0);
_ = tile_world::take(w, 1, 1, 0);
assert(len(tile_world::things(w, 1, 1, 0)) == 0 && tile_world::take(w, 1, 1, 0) == 0, "empty");
}
fn test_passable() {
w = room();
tile_world::place(w, 1, 1, 0, BOX);
tile_world::place(w, 2, 1, 0, LAMP);
assert(tile_world::passable(w, 0, 0, 0), "floor");
assert(!tile_world::passable(w, -1, 0, 0), "a wall");
assert(!tile_world::passable(w, 9, 9, 0), "the void");
assert(!tile_world::passable(w, 1, 1, 0), "a box blocks");
assert(tile_world::passable(w, 2, 1, 0), "a lamp does not");
}
// A wall down x 2 for y 0 .. 1 leaves a gap at y 2. From (0, 0) to (4, 0) every shortest walk
// goes through the gap: 4 steps east, 2 down and 2 back up, 8.
// @TLW-001 — shortest_path routes around a wall through a single passable gap.
fn test_shortest_path_goes_round() {
w = room();
tile_world::fill(w, 2, 0, 2, 1, 0, WALL);
p = tile_world::shortest_path(w, 0, 0, 4, 0, 0);
assert(len(p) == 8, "8 steps, got {len(p)}");
assert(p[7].0 == 4 && p[7].1 == 0, "ends at the goal");
px = 0;
py = 0;
through_gap = false;
for c in p {
d = (c.0 - px) * (c.0 - px) + (c.1 - py) * (c.1 - py);
assert(d == 1, "each step to a neighbour: {px},{py} -> {c.0},{c.1}");
if c.0 == 2 && c.1 == 2 { through_gap = true; }
px = c.0;
py = c.1;
}
assert(through_gap, "through the gap at (2, 2)");
}
fn test_shortest_path_limits() {
w = room();
tile_world::fill(w, 2, 0, 2, 2, 0, WALL);
assert(len(tile_world::shortest_path(w, 0, 0, 4, 0, 0)) == 0, "walled off: no path");
v = room();
tile_world::place(v, 3, 0, 0, BOX);
p = tile_world::shortest_path(v, 0, 0, 3, 0, 0);
assert(len(p) == 3 && p[2].0 == 3, "a path may end on a box: 3 steps");
assert(len(tile_world::shortest_path(v, 0, 0, 4, 2, 0, 5)) == 0, "6 steps away, limit 5");
assert(len(tile_world::shortest_path(v, 0, 0, 4, 2, 0, 6)) == 6, "limit 6 reaches it");
assert(len(tile_world::shortest_path(v, 0, 0, 0, 0, 0)) == 0, "the start itself");
}
// Sight: a wall in between blocks; the wall itself is seen; a window and a lamp do not block,
// a curtain does.
fn test_line_of_sight() {
w = room();
assert(tile_world::line_of_sight(w, 0, 0, 4, 2, 0), "across the open room");
tile_world::set_terrain(w, 2, 1, 0, WALL);
assert(!tile_world::line_of_sight(w, 0, 1, 4, 1, 0), "the wall at (2, 1) is in the way");
assert(tile_world::line_of_sight(w, 0, 1, 2, 1, 0), "the wall itself is seen");
tile_world::set_terrain(w, 2, 1, 0, WINDOW);
assert(tile_world::line_of_sight(w, 0, 1, 4, 1, 0), "a window is not");
tile_world::place(w, 2, 1, 0, CURTAIN);
assert(!tile_world::line_of_sight(w, 0, 1, 4, 1, 0), "a curtain is");
}
// Light along the room's row y 0, read from x -1 (the west wall) to x 5 (the east wall).
fn row(w: const tile_world::World, lights: const vector<(integer, integer)>) -> text {
t = "";
for s in tile_world::shades(w, -1, 0, 5, 0, 0, lights) { t += "{s},"; }
t
}
fn test_shades() {
w = room();
none: vector<(integer, integer)> = [];
assert(row(w, none) == "7,7,7,7,7,7,7,", "dark, never black");
tile_world::place(w, 0, 0, 0, LAMP);
assert(row(w, none) == "1,0,1,2,3,4,5,", "a lamp at x 0: {row(w, none)}");
// A single wall cell at (2, 0) is walked round through row 1: (3, 0) is 5 steps from the lamp.
tile_world::set_terrain(w, 2, 0, 0, WALL);
assert(row(w, none) == "1,0,1,2,5,6,7,", "light goes round one wall cell: {row(w, none)}");
// A whole wall down x 2 is lit and stops it.
tile_world::fill(w, 2, 0, 2, 2, 0, WALL);
assert(row(w, none) == "1,0,1,2,7,7,7,", "a wall at x 2 is lit and stops it: {row(w, none)}");
carried: vector<(integer, integer)> = [(4, 0)];
assert(row(w, carried) == "1,0,1,2,1,0,1,", "a light the game passes in, at x 4: {row(w, carried)}");
tile_world::set_terrain(w, 4, 0, 1, tile_world::VOID);
assert(row(w, none) == "1,0,1,2,1,0,1,", "the open sky over x 4 is a light: {row(w, none)}");
}
// Night: `daylight` false, the open sky gives no light — the cells open to it are as dark as any,
// and only a lamp or a light the game passes in lights them.
fn test_night() {
w = room();
none: vector<(integer, integer)> = [];
tile_world::set_terrain(w, 4, 0, 1, tile_world::VOID);
assert(row(w, none) == "5,4,3,2,1,0,1,", "by day the open sky over x 4 is a light: {row(w, none)}");
t = "";
for s in tile_world::shades(w, -1, 0, 5, 0, 0, none, [], false) { t += "{s},"; }
assert(t == "7,7,7,7,7,7,7,", "by night it is not: {t}");
tile_world::place(w, 0, 0, 0, LAMP);
t = "";
for s in tile_world::shades(w, -1, 0, 5, 0, 0, none, [], false) { t += "{s},"; }
assert(t == "1,0,1,2,3,4,5,", "a lamp still lights by night: {t}");
}
// Light along row y 0 from lights that start dimmer: the k-th light at shade `start[k]` (a torch
// burning down), a missing entry full.
fn dim_row(w: const tile_world::World, lights: const vector<(integer, integer)>,
start: const vector<integer>) -> text {
t = "";
for s in tile_world::shades(w, -1, 0, 5, 0, 0, lights, start) { t += "{s},"; }
t
}
fn test_dimmed_lights() {
w = room();
at0: vector<(integer, integer)> = [(0, 0)];
assert(dim_row(w, at0, [3]) == "4,3,4,5,6,7,7,", "a light at x 0 from shade 3: {dim_row(w, at0, [3])}");
// The dim one at x 0 keeps its 3 (the bright one at x 4 reaches it at 4); x 1 takes 3 from the
// bright side. The second light has no entry: full.
both: vector<(integer, integer)> = [(0, 0), (4, 0)];
assert(dim_row(w, both, [3]) == "4,3,3,2,1,0,1,", "the brighter way wins: {dim_row(w, both, [3])}");
// A dim light on the wall itself (opaque) is a light: it spreads.
wall: vector<(integer, integer)> = [(-1, 0)];
assert(dim_row(w, wall, [2]) == "2,3,4,5,6,7,7,", "a wall light from shade 2: {dim_row(w, wall, [2])}");
assert(dim_row(w, at0, [7]) == "7,7,7,7,7,7,7,", "as dark as the dark: nothing");
}
fn test_view() {
w = room();
tile_world::place(w, 1, 0, 0, BOX);
tile_world::set_terrain(w, 3, 0, 0, WINDOW);
marks: vector<(integer, integer, text)> = [(0, 0, "@")];
got = tile_world::view(w, -1, 0, 5, 0, 0, marks);
assert(got == "#@b.=.#\n", "got {got}");
}
// What a world holds, listed: layers lowest first, a layer's chunks in reading order, the
// cells with things layer by layer in reading order. Chunks: (-1, 0) holds x -32 .. -1.
fn test_enumeration() {
w = tile_world::world();
kinds(w);
tile_world::set_terrain(w, 40, 5, 2, FLOOR);
tile_world::set_terrain(w, -1, 5, 0, FLOOR);
tile_world::set_terrain(w, 3, 70, 0, FLOOR);
tile_world::set_terrain(w, 3, 3, 0, WALL);
tile_world::set_terrain(w, 0, 0, -1, FLOOR);
ls = "";
for z in tile_world::layers(w) { ls += "{z},"; }
assert(ls == "-1,0,2,", "layers {ls}");
cs = "";
for c in tile_world::chunks(w, 0) { cs += "({c.0},{c.1})"; }
assert(cs == "(-1,0)(0,0)(0,2)", "layer 0's chunks {cs}");
assert(len(tile_world::chunks(w, 5)) == 0, "an empty layer has none");
tile_world::place(w, 3, 3, 0, BOX);
tile_world::place(w, 1, 9, 0, LAMP);
tile_world::place(w, 2, 3, 0, BOX);
tile_world::place(w, 0, 0, -1, BOX);
ss = "";
for s in tile_world::stacks(w) { ss += "({s.0},{s.1},{s.2})"; }
assert(ss == "(0,0,-1)(2,3,0)(3,3,0)(1,9,0)", "stacks {ss}");
}