← hex_walk · API reference · version 0.1.2
5 file(s), 1417 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 |
|---|---|---|---|
Stretch struct | 43 | — | 57 91 135 136 182 377 … (24 total) |
stretch_near fn | 57 | — | 92 379 |
stretch_dist fn | 91 | — | 424 42 44 |
Walker struct | 98 | — | 118 119 363 377 401 412 … (15 total) |
walker_new fn | 118 | — | 41 45 49 55 66 69 … (17 total) |
walk_stretches fn | 135 | — | 90 61 111 |
Floors struct | 192 | — | 220 316 323 363 |
FLOOR_SEAM const | 209 | — | 220 |
FLOOR_STEEP const | 212 | — | 161 247 |
walk_floors fn | 220 | — | 58 73 76 86 |
floor_heights fn | 323 | — | 368 59 60 61 62 63 … (10 total) |
walk_floor fn | 363 | — | 87 88 89 90 91 92 … (7 total) |
walk_push_from fn | 377 | — | 405 406 413 32 37 40 |
walk_push_out fn | 401 | — | 794 72 |
walk_fits fn | 412 | — | 428 440 |
walk_ease fn | 422 | — | 756 45 |
LEAF_OPEN const | 451 | — | 488 621 790 41 60 69 … (10 total) |
Leaf struct | 463 | — | 485 489 495 502 508 519 … (13 total) |
leaf_of fn | 484 | — | 28 77 131 132 195 |
leaf_set fn | 495 | — | 109 197 |
leaf_ends fn | 502 | — | 509 537 592 |
leaf_stretch fn | 508 | — | 520 527 569 612 791 88 |
leaf_clear fn | 519 | — | 639 776 788 |
leaves_stretches fn | 526 | — | 552 761 |
leaves_quads fn | 535 | — | 111 199 |
leaf_turn fn | 557 | — | 783 |
leaf_in_hand fn | 568 | — | 611 |
leaf_pull_way fn | 591 | — | 620 |
walk_pull fn | 604 | — | 60 72 105 |
leaves_swing fn | 629 | — | 64 68 106 |
Ladder struct | 650 | — | 662 683 689 145 |
ladder_at fn | 662 | — | 735 768 796 |
Place struct | 680 | — | 604 629 687 690 726 26 … (7 total) |
place_new fn | 687 | — | 33 27 76 130 144 75 |
Body struct | 696 | — | 604 629 706 707 711 716 … (9 total) |
body_new fn | 706 | — | 34 38 49 59 72 91 … (16 total) |
body_way fn | 711 | — | 729 |
body_view fn | 716 | — | 605 |
walk_step fn | 726 | — | 35 39 50 92 100 134 … (10 total) |
walk_look fn | 810 | — | 159 164 |
walk_settle fn | 836 | — | 178 180 186 |
look_way fn | 842 | — | 850 |
look_ray fn | 849 | — | 167 |
src/hex_walk.loft
// Copyright (c) 2026 Jurjen Stellingwerff
// SPDX-License-Identifier: LGPL-3.0-or-later
//
// hex_walk — A BODY WALKING THROUGH A BUILT WORLD: stopped by its walls, stepping up what a foot
// takes, eased into a doorway it is a little off, pushing door leaves open and shut, pulling one with
// its hand, climbing a ladder, and turning its head before its body. What `hex_body` does not: it
// answers what a body DOES next, frame by frame, where `hex_body` answers where a part is now. Every
// leaf is a `hex_body` rig — the wall's way fixed, the leaf on its hinge — so a leaf's limits are its
// joint's.
//
// THE MEASURES ARE THE FEEL. Every number a player feels is a field of `Walker` (`walker_new` gives
// the ones the tile-world house review settled on, played and approved by its owner): how wide the
// body is, how high a step it takes, how far ahead it looks for a doorway and how hard it eases into
// one, how close a hand reaches, how fast a leaf swings. A game changes a field; the routines stay.
//
// THE FRAME. Plan coordinates: x east, y north, z up, in metres; a way is a unit vector on the plan,
// a body's yaw degrees from north towards the east. A wall is a `Stretch`: a line or an arc on the
// plan from one height to another (`walk_stretches` takes them from `hex_edge` faces — an arc where
// the face was drawn flat for one, so a round wall is met round, not in its drawn pieces). The ground and floors
// under a point are the caller's: a routine `floor(x, y, from)` answering the height feet stand at
// there, with them at `from` before (so a storey above or a cellar below is told apart).
//
// THE RULES, each a test in `tests/`:
// - A body is a circle of `wk_radius` on the plan; a stretch stops it when it spans the body's
// height (from `wk_step_up` over its feet up to `wk_height`), and pushes it out square from its
// line or arc, or round its end (@FR-HW-Arc).
// - A walk is swept in steps no longer than `wk_sub`, the body pushed out after each, so no wall is
// stepped through between two looks at it. A step up higher than `wk_step_up` ends the walk.
// - A walk stopped within `wk_ahead` but free with a sidestep of at most `wk_nudge` eases that way,
// by no more than `wk_ease` of its own pace: it lines up before it is stopped and never halts. A
// wall square across the way has no such sidestep. Leaves are no stop to the look: walking into
// one opens it.
// - Walking into a leaf turns it about its hinge the way the walk turns it — a walk along it (its
// turn below `wk_turns`) slides past — as far as clears the body, and it swings on to open or
// shut; a leaf at its limit stops the body. A pull swings the leaf in hand towards the body. A
// swinging leaf stops against the body until it stands clear.
use hex_body;
use hex_edge;
/// A wall as a walking body meets it: a line on the plan from (sr_ax, sr_ay) to (sr_bx, sr_by), from
/// height sr_z0 up to sr_z1 — or, with a radius `sr_r` over 0, the arc of the circle of that radius
/// round (sr_cx, sr_cy) from the first end anticlockwise to the second, both on it.
pub struct Stretch {
pub sr_ax: float,
pub sr_ay: float,
pub sr_bx: float,
pub sr_by: float,
pub sr_z0: float,
pub sr_z1: float,
pub sr_cx: float = 0.0,
pub sr_cy: float = 0.0,
pub sr_r: float = 0.0,
}
// @FR-HW-Arc: an arc stretch is met at its nearest point, as a line is
/// The point of stretch k nearest (px, py) on the plan: on its line, or its arc, or at its nearer end.
pub fn stretch_near(k: const Stretch, px: float, py: float) -> (float, float) {
if k.sr_r > 0.0 {
dx = px - k.sr_cx;
dy = py - k.sr_cy;
d = sqrt(dx * dx + dy * dy) ?? 0.0;
// the turn from the first end to the point and to the second end, anticlockwise, in 0 .. 2π
ta = atan2(k.sr_ay - k.sr_cy, k.sr_ax - k.sr_cx);
tb = arc_turn(atan2(k.sr_by - k.sr_cy, k.sr_bx - k.sr_cx) - ta);
tp = arc_turn(atan2(dy, dx) - ta);
if d > 0.000001 && tp <= tb { return (k.sr_cx + dx / d * k.sr_r ?? k.sr_ax, k.sr_cy + dy / d * k.sr_r ?? k.sr_ay); }
ea = (px - k.sr_ax) * (px - k.sr_ax) + (py - k.sr_ay) * (py - k.sr_ay);
eb = (px - k.sr_bx) * (px - k.sr_bx) + (py - k.sr_by) * (py - k.sr_by);
return if ea <= eb { (k.sr_ax, k.sr_ay) } else { (k.sr_bx, k.sr_by) };
}
dx = k.sr_bx - k.sr_ax;
dy = k.sr_by - k.sr_ay;
l2 = dx * dx + dy * dy;
s = ((px - k.sr_ax) * dx + (py - k.sr_ay) * dy) / l2 ?? 0.0;
if s < 0.0 { s = 0.0; }
if s > 1.0 { s = 1.0; }
(k.sr_ax + dx * s, k.sr_ay + dy * s)
}
// An angle brought into 0 .. 2π.
fn arc_turn(a: float) -> float {
t = a;
for _ in 0..3 {
if t < 0.0 { t += 2.0 * PI; }
if t >= 2.0 * PI { t -= 2.0 * PI; }
}
t
}
/// How far (px, py) lies from stretch k on the plan.
pub fn stretch_dist(k: const Stretch, px: float, py: float) -> float {
(nx, ny) = stretch_near(k, px, py);
sqrt((px - nx) * (px - nx) + (py - ny) * (py - ny)) ?? 0.0
}
/// How a body walks — every measure a player feels. `walker_new` answers the tile-world house
/// review's, played and approved.
pub struct Walker {
pub wk_radius: float, // the body's half width on the plan (m)
pub wk_step_up: float, // the highest step a foot takes (m); what stands no higher is walked over
pub wk_height: float, // the eye over the feet (m); what starts above it stops nothing
pub wk_pace: float, // walking pace (m/s)
pub wk_sub: float, // the longest step a walk is swept in (m)
pub wk_ahead: float, // how far ahead a walk looks for a stop it can ease past (m)
pub wk_nudge: float, // the widest sidestep it eases by (m)
pub wk_ease: float, // how fast it eases, a part of its own pace
pub wk_climb: float, // climbing pace on a ladder (m/s)
pub wk_turns: float, // the least turn (a sine) a walk must give a leaf to swing it
pub wk_hand: float, // how far the hand reaches to pull a leaf (m)
pub wk_swing: float, // how fast a leaf swings (turns/s)
pub wk_look: float, // degrees the head turns for a pixel of the pointer's move
pub wk_neck: float, // the most the head turns either way of the body (degrees); past it the body turns
pub wk_pitch: float, // the most the head looks up or down (degrees)
pub wk_settle: float, // how fast the head settles straight ahead as the body moves (s, a time constant)
}
/// A walking person at the tile-world game's pace: a tile (1.5 m) in 140 ms.
pub fn walker_new() -> Walker {
Walker { wk_radius: 0.2, wk_step_up: 0.45, wk_height: 1.6, wk_pace: 1.5 / 0.14, wk_sub: 0.05,
wk_ahead: 0.75, wk_nudge: 0.3, wk_ease: 0.35, wk_climb: 1.2, wk_turns: 0.3, wk_hand: 1.2,
wk_swing: 0.6, wk_look: 0.1, wk_neck: 70.0, wk_pitch: 60.0, wk_settle: 0.5 }
}
// --- Walls ---
// @FR-HW-Stand: a face stops the body when it stands up — steeper than a foot stands on — at any height
/// The stretches of `q`'s faces from face `from` on: each face that stands up — steeper than
/// `FLOOR_STEEP`, so neither stood on (`walk_floors`) nor a ceiling — however low or thin, as the
/// line between its two corners furthest apart on the plan, from its lowest corner to its highest — or,
/// for a face drawn flat for a circle (`hex_edge::quad_bend`), the arc of that circle between those
/// corners. Whether one stops a body is the walker's to say, by its step and its height
/// (`walk_push_from`): an arm or a rail at the chest stops it, a riser under a step does not, and the
/// same faces drawn at any scale give the same stretches, scaled. A face whose part is in `passes` (a
/// wall's top, glass, a ladder's rungs) stops nothing.
pub fn walk_stretches(q: const hex_edge::Quads, from: integer, passes: const vector<integer>) -> vector<Stretch> {
out: vector<Stretch> = [];
for i in from..q.qd_n {
p = q.qd_part[i] ?? 0;
passed = false;
for pp in passes { if pp == p { passed = true; } }
if passed { continue; }
k = i * 12;
lo = q.qd_xyz[k + 2] ?? 0.0;
hi = lo;
for m in 1..4 {
z = q.qd_xyz[k + m * 3 + 2] ?? 0.0;
if z < lo { lo = z; }
if z > hi { hi = z; }
}
// its normal by Newell's sum: a face no steeper than a floor (either way up) stands up nowhere
nx = 0.0; ny = 0.0; nz = 0.0;
for m in 0..4 {
a = k + m * 3;
c = k + ((m + 1) % 4) * 3;
ax = q.qd_xyz[a] ?? 0.0; ay = q.qd_xyz[a + 1] ?? 0.0; az = q.qd_xyz[a + 2] ?? 0.0;
cx = q.qd_xyz[c] ?? 0.0; cy = q.qd_xyz[c + 1] ?? 0.0; cz = q.qd_xyz[c + 2] ?? 0.0;
nx += (ay - cy) * (az + cz);
ny += (az - cz) * (ax + cx);
nz += (ax - cx) * (ay + cy);
}
if nz * nz >= FLOOR_STEEP * FLOOR_STEEP * (nx * nx + ny * ny + nz * nz) || hi <= lo { continue; }
best = 0.0;
ax = 0.0; ay = 0.0; bx = 0.0; by = 0.0;
for m in 0..4 {
for n in m + 1..4 {
dx = (q.qd_xyz[k + m * 3] ?? 0.0) - (q.qd_xyz[k + n * 3] ?? 0.0);
dy = (q.qd_xyz[k + m * 3 + 1] ?? 0.0) - (q.qd_xyz[k + n * 3 + 1] ?? 0.0);
if dx * dx + dy * dy > best {
best = dx * dx + dy * dy;
ax = q.qd_xyz[k + m * 3] ?? 0.0; ay = q.qd_xyz[k + m * 3 + 1] ?? 0.0;
bx = q.qd_xyz[k + n * 3] ?? 0.0; by = q.qd_xyz[k + n * 3 + 1] ?? 0.0;
}
}
}
// no way along on the plan against its own height: a face standing on a point, at any scale
if best <= hex_edge::SCALE_EPS * (best + (hi - lo) * (hi - lo)) { continue; }
(cx, cy, r) = hex_edge::quad_bend(q, i);
// on a circle, its two ends anticlockwise: the second the first's turn on by less than a half turn
if r > 0.0 && (ax - cx) * (by - cy) - (ay - cy) * (bx - cx) < 0.0 {
(ax, ay, bx, by) = (bx, by, ax, ay);
}
out += [Stretch { sr_ax: ax, sr_ay: ay, sr_bx: bx, sr_by: by, sr_z0: lo, sr_z1: hi, sr_cx: cx, sr_cy: cy, sr_r: r }];
}
out
}
// --- Floors ---
/// The floors of a built world as a walking body stands on them (`walk_floors`): its faces that face
/// up, each a plane over its corners, kept on a grid of square cells so a point finds the faces over it
/// without looking at the others.
pub struct Floors {
pub fl_xyz: vector<float>, // each face's four corners (x, y, z), 12 a face
pub fl_plane: vector<float>, // each face's plane z = a·x + b·y + c: (a, b, c), 3 a face
pub fl_x0: float, // the grid's south-west corner
pub fl_y0: float,
pub fl_cell: float, // a cell's width (m)
pub fl_seam: float, // how far off a face's edge a point still stands on it (m)
pub fl_cols: integer,
pub fl_rows: integer,
pub fl_start: vector<integer>, // cell k's faces are fl_list[fl_start[k] .. fl_start[k + 1]]
pub fl_list: vector<integer>,
}
/// How far off a floor's edge a point still stands on it (m), unless `walk_floors` is given another:
/// two faces drawn to meet, a hair apart where their edges are cut differently (a floor's chord
/// against a step's), are walked as one. A world drawn at scale s is walked with a seam (and cells)
/// s times as wide.
pub const FLOOR_SEAM = 0.02;
/// The steepest face a foot stands on: the upward part of its unit normal at least this, 45°.
pub const FLOOR_STEEP = 0.7071;
// @FR-HW-Floor: a body stands on what is drawn — the faces facing up, no level declared beside them
/// The floors of `q`'s faces from face `from` on: every face facing up — wound counter-clockwise seen
/// from above (`hex_edge::quad_add`), no steeper than `FLOOR_STEEP` — whatever its part, so a floor's
/// top, a wall's top, a doorway's sill and a step's tread are all stood on, and a floor's underside and
/// a hole a floor was opened by are not. Kept on cells `cell` wide; a point `seam` off a face's edge
/// still stands on it.
pub fn walk_floors(q: const hex_edge::Quads, from: integer, cell: float, seam: float = FLOOR_SEAM) -> Floors {
xyz: vector<float> = [];
plane: vector<float> = [];
for i in from..q.qd_n {
k = i * 12;
// the face's normal by Newell's sum over its edges: a corner given twice adds nothing
nx = 0.0;
ny = 0.0;
nz = 0.0;
for m in 0..4 {
a = k + m * 3;
c = k + ((m + 1) % 4) * 3;
ax = q.qd_xyz[a] ?? 0.0; ay = q.qd_xyz[a + 1] ?? 0.0; az = q.qd_xyz[a + 2] ?? 0.0;
cx = q.qd_xyz[c] ?? 0.0; cy = q.qd_xyz[c + 1] ?? 0.0; cz = q.qd_xyz[c + 2] ?? 0.0;
nx += (ay - cy) * (az + cz);
ny += (az - cz) * (ax + cx);
nz += (ax - cx) * (ay + cy);
}
nl = sqrt(nx * nx + ny * ny + nz * nz) ?? 0.0;
// no area against its own size (`hex_edge::SCALE_EPS`): a face of no area at any scale
lx = 0.0;
for m in 1..4 {
dx = (q.qd_xyz[k + m * 3] ?? 0.0) - (q.qd_xyz[k] ?? 0.0);
dy = (q.qd_xyz[k + m * 3 + 1] ?? 0.0) - (q.qd_xyz[k + 1] ?? 0.0);
dz = (q.qd_xyz[k + m * 3 + 2] ?? 0.0) - (q.qd_xyz[k + 2] ?? 0.0);
if dx * dx + dy * dy + dz * dz > lx { lx = dx * dx + dy * dy + dz * dz; }
}
if nl <= hex_edge::SCALE_EPS * lx || nz < FLOOR_STEEP * nl { continue; }
// its plane through its corners' middle
mx = 0.0; my = 0.0; mz = 0.0;
for m in 0..4 {
mx += (q.qd_xyz[k + m * 3] ?? 0.0) * 0.25;
my += (q.qd_xyz[k + m * 3 + 1] ?? 0.0) * 0.25;
mz += (q.qd_xyz[k + m * 3 + 2] ?? 0.0) * 0.25;
}
pa = 0.0 - nx / nz ?? 0.0;
pb = 0.0 - ny / nz ?? 0.0;
plane += [pa, pb, mz - pa * mx - pb * my];
for m in 0..12 { xyz += [q.qd_xyz[k + m] ?? 0.0]; }
}
n = len(plane) / 3;
// the grid over every face, each face in every cell its corners' box (and a seam round it) touches
x0 = 1000000000.0; y0 = 1000000000.0; x1 = -1000000000.0; y1 = -1000000000.0;
for f in 0..n {
for m in 0..4 {
x = xyz[f * 12 + m * 3] ?? 0.0;
y = xyz[f * 12 + m * 3 + 1] ?? 0.0;
if x < x0 { x0 = x; }
if x > x1 { x1 = x; }
if y < y0 { y0 = y; }
if y > y1 { y1 = y; }
}
}
if n == 0 { x0 = 0.0; y0 = 0.0; x1 = 0.0; y1 = 0.0; }
x0 -= seam;
y0 -= seam;
cols = (floor((x1 + seam - x0) / cell ?? 0.0) as integer) + 1;
rows = (floor((y1 + seam - y0) / cell ?? 0.0) as integer) + 1;
counts: vector<integer> = [for _ in 0..cols * rows + 1 { 0 }];
boxes: vector<integer> = [];
for f in 0..n {
bx0 = 1000000000.0; by0 = 1000000000.0; bx1 = -1000000000.0; by1 = -1000000000.0;
for m in 0..4 {
x = xyz[f * 12 + m * 3] ?? 0.0;
y = xyz[f * 12 + m * 3 + 1] ?? 0.0;
if x < bx0 { bx0 = x; }
if x > bx1 { bx1 = x; }
if y < by0 { by0 = y; }
if y > by1 { by1 = y; }
}
c0 = floor((bx0 - seam - x0) / cell ?? 0.0) as integer;
c1 = floor((bx1 + seam - x0) / cell ?? 0.0) as integer;
r0 = floor((by0 - seam - y0) / cell ?? 0.0) as integer;
r1 = floor((by1 + seam - y0) / cell ?? 0.0) as integer;
if c0 < 0 { c0 = 0; }
if r0 < 0 { r0 = 0; }
if c1 > cols - 1 { c1 = cols - 1; }
if r1 > rows - 1 { r1 = rows - 1; }
boxes += [c0, c1, r0, r1];
for r in r0..r1 + 1 {
for c in c0..c1 + 1 { counts[r * cols + c] = (counts[r * cols + c] ?? 0) + 1; }
}
}
start: vector<integer> = [0];
for k in 0..cols * rows { start += [(start[k] ?? 0) + (counts[k] ?? 0)]; }
list: vector<integer> = [for _ in 0..(start[cols * rows] ?? 0) { 0 }];
fill: vector<integer> = [for k in 0..cols * rows { start[k] ?? 0 }];
for f in 0..n {
for r in (boxes[f * 4 + 2] ?? 0)..(boxes[f * 4 + 3] ?? 0) + 1 {
for c in (boxes[f * 4] ?? 0)..(boxes[f * 4 + 1] ?? 0) + 1 {
at = fill[r * cols + c] ?? 0;
list[at] = f;
fill[r * cols + c] = at + 1;
}
}
}
Floors { fl_xyz: xyz, fl_plane: plane, fl_x0: x0, fl_y0: y0, fl_cell: cell, fl_seam: seam, fl_cols: cols, fl_rows: rows,
fl_start: start, fl_list: list }
}
// @FR-HW-Floor
/// The heights of every floor over (x, y): each face it lies on, or no further than its seam
/// (`walk_floors`) off its edge, at the height of that face's plane there. Lowest first.
pub fn floor_heights(fl: const Floors, x: float, y: float) -> vector<float> {
out: vector<float> = [];
c = floor((x - fl.fl_x0) / fl.fl_cell ?? 0.0) as integer;
r = floor((y - fl.fl_y0) / fl.fl_cell ?? 0.0) as integer;
if c < 0 || r < 0 || c >= fl.fl_cols || r >= fl.fl_rows { return out; }
k = r * fl.fl_cols + c;
for j in (fl.fl_start[k] ?? 0)..(fl.fl_start[k + 1] ?? 0) {
f = fl.fl_list[j] ?? 0;
on = true;
for m in 0..4 {
a = f * 12 + m * 3;
e = f * 12 + ((m + 1) % 4) * 3;
ax = fl.fl_xyz[a] ?? 0.0;
ay = fl.fl_xyz[a + 1] ?? 0.0;
ex = (fl.fl_xyz[e] ?? 0.0) - ax;
ey = (fl.fl_xyz[e + 1] ?? 0.0) - ay;
el = sqrt(ex * ex + ey * ey) ?? 0.0;
if el < 0.000001 { continue; }
// counter-clockwise from above, the face lies left of each edge: how far right of it (x, y) lies
if (ey * (x - ax) - ex * (y - ay)) / el > fl.fl_seam { on = false; }
}
if !on { continue; }
z = (fl.fl_plane[f * 3] ?? 0.0) * x + (fl.fl_plane[f * 3 + 1] ?? 0.0) * y + (fl.fl_plane[f * 3 + 2] ?? 0.0);
// kept lowest first
i = len(out);
out += [z];
while i > 0 && (out[i - 1] ?? 0.0) > z {
out[i] = out[i - 1] ?? 0.0;
i -= 1;
}
out[i] = z;
}
out
}
// @FR-HW-Floor: of the floors under the feet, the highest a step up reaches, else the lowest (a fall)
/// The height a body's feet stand at over (x, y), with them at `from` before: of the floors there
/// (`floor_heights`) and the heights in `more` (a stair's slope through its steps' front edges, which
/// the caller draws as steps), the highest no more than `wk_step_up` over `from`; else the lowest — the
/// body falls; with none at all, `from`: where nothing is drawn the body is not let fall out of the world.
pub fn walk_floor(w: const Walker, fl: const Floors, x: float, y: float, from: float, more: const vector<float>) -> float {
best = 0.0;
low = 0.0;
found = false;
any = false;
for z in floor_heights(fl, x, y) + more {
if z <= from + w.wk_step_up && (!found || z > best) { best = z; found = true; }
if !any || z < low { low = z; any = true; }
}
if found { best } else if any { low } else { from }
}
/// The body at (px, py), its feet at `feet`, pushed out of stretch k: `wk_radius` from its line or
/// arc, square from it or round its end. A stretch that does not span the body's height stops nothing.
pub fn walk_push_from(w: const Walker, k: const Stretch, px: float, py: float, feet: float) -> (float, float) {
if k.sr_z1 <= feet + w.wk_step_up || k.sr_z0 >= feet + w.wk_height { return (px, py); }
(nx, ny) = stretch_near(k, px, py);
ex = px - nx;
ey = py - ny;
dl = sqrt(ex * ex + ey * ey) ?? 0.0;
if dl >= w.wk_radius { return (px, py); }
if dl < 0.00001 {
// on it: out to the right of the way from its first end to its second — an arc's outside
if k.sr_r > 0.0 {
ox = nx - k.sr_cx;
oy = ny - k.sr_cy;
return (nx + ox / k.sr_r * w.wk_radius ?? nx, ny + oy / k.sr_r * w.wk_radius ?? ny);
}
dx = k.sr_bx - k.sr_ax;
dy = k.sr_by - k.sr_ay;
ll = sqrt(dx * dx + dy * dy) ?? 1.0;
return (px + dy / ll * w.wk_radius ?? 0.0, py - dx / ll * w.wk_radius ?? 0.0);
}
(px + ex / dl * (w.wk_radius - dl) ?? 0.0, py + ey / dl * (w.wk_radius - dl) ?? 0.0)
}
/// The body at (x, y), its feet at `feet`, pushed out of every stretch of `walls` and `more`, three
/// times round (a corner pushes it from one into the other).
pub fn walk_push_out(w: const Walker, walls: const vector<Stretch>, more: const vector<Stretch>, x: float, y: float, feet: float) -> (float, float) {
px = x;
py = y;
for _ in 0..3 {
for k in walls { (px, py) = walk_push_from(w, k, px, py, feet); }
for k in more { (px, py) = walk_push_from(w, k, px, py, feet); }
}
(px, py)
}
/// Whether the body fits at (x, y), its feet at `feet`: none of `ks` pushes it.
pub fn walk_fits(w: const Walker, ks: const vector<Stretch>, x: float, y: float, feet: float) -> boolean {
for k in ks { p = walk_push_from(w, k, x, y, feet); if p.0 != x || p.1 != y { return false; } }
true
}
/// The sidestep a body walking from (x, y) along (wx, wy) (unit) eases by, and where its way is
/// stopped: if a wall stops it within `wk_ahead` but a sidestep of at most `wk_nudge` frees it — on
/// past the stop by `wk_ahead` again, a passage and not a notch — the smallest such sidestep, across
/// the walk (positive to its right), found in twelfths of `wk_nudge`; else 0. (0, -1) when nothing
/// stops it. Only the walls the look can reach are looked at.
pub fn walk_ease(w: const Walker, walls: const vector<Stretch>, x: float, y: float, feet: float, wx: float, wy: float) -> (float, float) {
reach = 2.0 * w.wk_ahead + w.wk_nudge + w.wk_radius + 0.05;
ks: vector<Stretch> = [for k in walls if stretch_dist(k, x, y) < reach { k }];
stop = -1.0;
for i in 1..9 {
a = w.wk_ahead * (i as float) / 8.0;
if !walk_fits(w, ks, x + wx * a, y + wy * a, feet) { stop = a; break; }
}
if stop < 0.0 { return (0.0, stop); }
// the walk's right
rx = wy;
ry = 0.0 - wx;
for j in 1..13 {
for sg in [1.0, -1.0] {
o = w.wk_nudge * (j as float) / 12.0 * sg;
clear = true;
for i in 0..9 {
a = (stop + w.wk_ahead) * (i as float) / 8.0;
if !walk_fits(w, ks, x + rx * o + wx * a, y + ry * o + wy * a, feet) { clear = false; break; }
}
if clear { return (o, stop); }
}
}
(0.0, stop)
}
// --- Door leaves ---
/// The most a leaf opens: a quarter turn, square to its wall.
pub const LEAF_OPEN = 0.25;
/// A door's leaf: a `hex_body` rig — the wall's way (`lf_dir`, turns) fixed, the leaf on its hinge
/// at (lf_hx, lf_hy) turning from shut (0) to `LEAF_OPEN`, anticlockwise seen from above — `lf_width`
/// wide from `lf_z0` up to `lf_z1`, `lf_thick` thick to the right of its way; where it stands now
/// (`lf_value`, turns open) and where it swings to (`lf_to`). A leaf of `lf_hand` -1 is its mirror:
/// it turns clockwise, its thickness to the left of its way — the second leaf of a pair, hinged at the
/// far post and shut back along the wall towards the first. It is drawn `lf_play` clear of its frame
/// and of a second leaf (`hex_edge::LEAF_PLAY`), and a walk meets it over that play too: a body cannot
/// pass a gap of millimetres, and meeting two leaves' ends apart would push it sideways off them.
/// Its top runs along `lf_top`: (distance from its hinge, height) pairs from its hinge to its free
/// edge, `lf_z1` its highest — a square leaf's top is level, an arched leaf's follows its arch.
pub struct Leaf {
pub lf_rig: hex_body::Rig,
pub lf_hx: float,
pub lf_hy: float,
pub lf_dir: float,
pub lf_width: float,
pub lf_z0: float,
pub lf_z1: float,
pub lf_thick: float,
pub lf_mat: integer,
pub lf_value: float,
pub lf_to: float,
pub lf_hand: float,
pub lf_play: float,
pub lf_top: vector<float>,
}
/// A door's leaf as `hex_edge::feature_leaf` answers it — (hinge x, hinge y, the wall's unit way x and y,
/// width, z0, z1) — `thick` thick, of material `mat`, shut; turning anticlockwise (`hand` 1) or, its
/// mirror, clockwise (-1); `play` clear of what is round it at either end; its top along `top`, the
/// (distance from its hinge, height) pairs of an arched leaf's, or level at z1 when none are given.
pub fn leaf_of(fl: (float, float, float, float, float, float, float), thick: float, mat: integer, hand: float = 1.0,
play: float = hex_edge::LEAF_PLAY, top: vector<float> = []) -> Leaf {
(hx, hy, ux, uy, width, z0, z1) = fl;
dir = uy.atan2(ux) / (2.0 * PI) ?? 0.0;
rig = hex_body::rig_bone(hex_body::rig_bone(hex_body::rig_new(), -1, 0.0, 0.0, 0.0, dir, dir), 0, 0.0, 0.0, width, 0.0, LEAF_OPEN);
Leaf { lf_rig: rig, lf_hx: hx, lf_hy: hy, lf_dir: dir, lf_width: width, lf_z0: z0, lf_z1: z1,
lf_thick: thick, lf_mat: mat, lf_value: 0.0, lf_to: 0.0, lf_hand: if hand < 0.0 { -1.0 } else { 1.0 },
lf_play: play, lf_top: if len(top) >= 4 { top } else { [0.0, z1, width, z1] } }
}
/// The leaf stood `v` turns open, and left there (its joint's limits keep `v` in them).
pub fn leaf_set(l: Leaf, v: float) {
nv = hex_body::joint_offer(hex_body::rig_joint(l.lf_rig, 1), v);
l.lf_value = nv;
l.lf_to = nv;
}
/// The leaf at `v` turns open on the plan — `hex_body`'s pose of its rig: its hinge and its free end.
pub fn leaf_ends(l: const Leaf, v: float) -> (float, float, float, float) {
sg = hex_body::rig_world_seg(l.lf_rig, [l.lf_dir, l.lf_hand * v], 1);
(l.lf_hx + sg.0, l.lf_hy + sg.1, l.lf_hx + sg.2, l.lf_hy + sg.3)
}
/// The leaf at `v` as a walking body meets it: the line down its middle, on over its play at both ends.
pub fn leaf_stretch(l: const Leaf, v: float) -> Stretch {
(x0, y0, x1, y1) = leaf_ends(l, v);
lx = (x1 - x0) / l.lf_width ?? 1.0;
ly = (y1 - y0) / l.lf_width ?? 0.0;
h = l.lf_thick * 0.5 * l.lf_hand;
p = l.lf_play;
Stretch { sr_ax: x0 + ly * h - lx * p, sr_ay: y0 - lx * h - ly * p, sr_bx: x1 + ly * h + lx * p, sr_by: y1 - lx * h + ly * p,
sr_z0: l.lf_z0, sr_z1: l.lf_z1 }
}
/// Whether the body at (x, y), its feet at `feet`, stands clear of the leaf at `v`.
pub fn leaf_clear(w: const Walker, l: const Leaf, v: float, x: float, y: float, feet: float) -> boolean {
k = leaf_stretch(l, v);
if k.sr_z1 <= feet + w.wk_step_up || k.sr_z0 >= feet + w.wk_height { return true; }
hex_body::seg_dist(k.sr_ax, k.sr_ay, k.sr_bx, k.sr_by, x, y) >= w.wk_radius - 0.001
}
/// Every leaf where it stands, as the walk meets it.
pub fn leaves_stretches(ls: const vector<Leaf>) -> vector<Stretch> {
[for l in ls { leaf_stretch(l, l.lf_value) }]
}
/// Every leaf where it stands into `q`, part `PART_LEAF`: a slice between each two points of its top,
/// its thickness to the right of its way (the left for a mirrored leaf), the slices composed so the
/// faces between them are not drawn, and one picture laid over the whole leaf (`hex_edge::quads_lay`)
/// from its hinge's foot to its free edge and its highest top, which turns with it. Answers the
/// stretches the walk meets.
pub fn leaves_quads(ls: const vector<Leaf>, q: hex_edge::Quads) -> vector<Stretch> {
for l in ls {
(x0, y0, x1, y1) = leaf_ends(l, l.lf_value);
lx = (x1 - x0) / l.lf_width ?? 1.0;
ly = (y1 - y0) / l.lf_width ?? 0.0;
n = if l.lf_hand < 0.0 { (0.0 - l.lf_thick, 0.0) } else { (0.0, l.lf_thick) };
t = l.lf_top;
sl = hex_edge::quads_new();
for j in 0..len(t) / 2 - 1 {
hex_edge::beam_add(sl, x0, y0, lx, ly, (t[j * 2] ?? 0.0, t[j * 2 + 2] ?? 0.0), n,
(l.lf_z0, t[j * 2 + 1] ?? l.lf_z1), (l.lf_z0, t[j * 2 + 3] ?? l.lf_z1), l.lf_mat, hex_edge::PART_LEAF);
}
sl = hex_edge::quads_compose(sl);
from = q.qd_n;
for f in 0..sl.qd_n { hex_edge::quad_copy(q, sl, f); }
hex_edge::quads_lay(q, from, (x0, y0, l.lf_z0), (x1 - x0, y1 - y0, 0.0), (0.0, 0.0, l.lf_z1 - l.lf_z0));
}
leaves_stretches(ls)
}
/// How a walk (mx, my) with the body touching the leaf at (hx, hy) turns it about its hinge: the sine
/// of the walk's angle to the arm from the hinge to that point, positive turning it open.
pub fn leaf_turn(l: const Leaf, hx: float, hy: float, mx: float, my: float) -> float {
rx = hx - l.lf_hx;
ry = hy - l.lf_hy;
r = sqrt(rx * rx + ry * ry) ?? 0.0;
if r < 0.1 { return 0.0; }
l.lf_hand * ((rx * my - ry * mx) / r ?? 0.0)
}
/// Whether the body at (x, y) looking along (fx, fy) has its hand on the leaf: where its eye meets the
/// leaf within `wk_hand`, or the leaf's nearest point within `wk_hand` and within 45 degrees of where
/// it looks — an open door's edge, seen end on, is taken too.
pub fn leaf_in_hand(w: const Walker, l: const Leaf, x: float, y: float, fx: float, fy: float) -> boolean {
k = leaf_stretch(l, l.lf_value);
ex = k.sr_bx - k.sr_ax;
ey = k.sr_by - k.sr_ay;
det = ex * fy - fx * ey;
if det > 0.000001 || det < -0.000001 {
cx = k.sr_ax - x;
cy = k.sr_ay - y;
t = (ex * cy - ey * cx) / det ?? -1.0;
sl = (fx * cy - fy * cx) / det ?? -1.0;
if t > 0.0 && t <= w.wk_hand && sl >= 0.0 && sl <= 1.0 { return true; }
}
sn = ((x - k.sr_ax) * ex + (y - k.sr_ay) * ey) / (ex * ex + ey * ey) ?? 0.0;
if sn < 0.0 { sn = 0.0; }
if sn > 1.0 { sn = 1.0; }
px = k.sr_ax + ex * sn - x;
py = k.sr_ay + ey * sn - y;
dl = sqrt(px * px + py * py) ?? 0.0;
dl <= w.wk_hand && px * fx + py * fy >= dl * 0.7071
}
/// Which way a pull turns the leaf, the body at (x, y): towards the body — the sine of the body's
/// angle to the leaf about its hinge, positive turning it open.
pub fn leaf_pull_way(l: const Leaf, x: float, y: float) -> float {
(x0, y0, x1, y1) = leaf_ends(l, l.lf_value);
ex = x1 - x0;
ey = y1 - y0;
bx = x - x0;
by = y - y0;
len2 = sqrt((ex * ex + ey * ey) * (bx * bx + by * by)) ?? 0.0;
l.lf_hand * ((ex * by - ey * bx) / len2 ?? 0.0)
}
/// A pull by the body, its hand where its head looks: the nearest leaf of the place in hand
/// (`leaf_in_hand`) is sent swinging towards the body — open from behind a shut door, shut from in
/// front of an open one. Answers its number, -1 for none in hand.
pub fn walk_pull(w: const Walker, pl: Place, b: const Body) -> integer {
(fx, fy) = body_view(b);
held = -1;
near = w.wk_hand + 1.0;
for k in 0..len(pl.pl_leaves) {
l = pl.pl_leaves[k]!;
if l.lf_z1 <= b.bd_feet + w.wk_step_up || l.lf_z0 >= b.bd_feet + w.wk_height { continue; }
if !leaf_in_hand(w, l, b.bd_x, b.bd_y, fx, fy) { continue; }
kb = leaf_stretch(l, l.lf_value);
dl = hex_body::seg_dist(kb.sr_ax, kb.sr_ay, kb.sr_bx, kb.sr_by, b.bd_x, b.bd_y);
if dl < near {
near = dl;
held = k;
}
}
if held >= 0 {
tq = leaf_pull_way(pl.pl_leaves[held]!, b.bd_x, b.bd_y);
if tq > 0.05 { pl.pl_leaves[held].lf_to = LEAF_OPEN; } else if tq < -0.05 { pl.pl_leaves[held].lf_to = 0.0; }
}
held
}
/// Every leaf of the place swung on for `dt` seconds towards where it was sent, its joint keeping it in
/// its limits; one that would swing into the body stops against it until the body stands clear
/// (unless `through`: a body that passes through walls, flying). Answers whether any moved.
pub fn leaves_swing(w: const Walker, pl: Place, b: const Body, dt: float, through: boolean) -> boolean {
moved = false;
for k in 0..len(pl.pl_leaves) {
l = pl.pl_leaves[k]!;
v = l.lf_value;
to = l.lf_to;
if v == to { continue; }
st = w.wk_swing * dt;
nv = if to > v { if v + st > to { to } else { v + st } } else { if v - st < to { to } else { v - st } };
nv = hex_body::joint_offer(hex_body::rig_joint(l.lf_rig, 1), nv);
if !through && !leaf_clear(w, l, nv, b.bd_x, b.bd_y, b.bd_feet) { continue; }
pl.pl_leaves[k].lf_value = nv;
moved = true;
}
moved
}
// --- Ladders ---
/// A ladder: its foot's middle (ld_x, ld_y) at height ld_z, climbed ld_h up from the side (ld_nx,
/// ld_ny) faces (unit), ld_width wide.
pub struct Ladder {
pub ld_x: float,
pub ld_y: float,
pub ld_z: float,
pub ld_nx: float,
pub ld_ny: float,
pub ld_width: float,
pub ld_h: float,
}
/// The ladder (its number) the body at (x, y), its feet at `feet`, is on: in front of it between its
/// foot and its top, or over the edge behind its top with its feet there; -1 for none.
pub fn ladder_at(ls: const vector<Ladder>, x: float, y: float, feet: float) -> integer {
for i in 0..len(ls) {
p = ls[i]!;
t = (x - p.ld_x) * p.ld_nx + (y - p.ld_y) * p.ld_ny;
sl = (y - p.ld_y) * p.ld_nx - (x - p.ld_x) * p.ld_ny;
reach = p.ld_width * 0.5 + 0.1;
top = p.ld_z + p.ld_h;
front = t >= 0.0 && t <= 0.5 && feet >= p.ld_z - 0.01 && feet <= top + 0.01;
behind = t >= -0.35 && t < 0.0 && feet >= top - 0.01 && feet <= top + 0.01;
if (front || behind) && sl >= 0.0 - reach && sl <= reach { return i; }
}
-1
}
// --- The walk ---
/// What a body walks among: the walls, the door leaves (where each stands and is swinging to) and the
/// ladders.
pub struct Place {
pub pl_walls: vector<Stretch>,
pub pl_leaves: vector<Leaf>,
pub pl_ladders: vector<Ladder>,
}
/// A place of these walls, no leaves and no ladders yet.
pub fn place_new(walls: const vector<Stretch>) -> Place {
ls: vector<Leaf> = [];
ld: vector<Ladder> = [];
Place { pl_walls: walls, pl_leaves: ls, pl_ladders: ld }
}
/// A walking body: where it stands (bd_x, bd_y) and its feet's height; its yaw (degrees from north
/// towards the east), the way it walks; its head's turn from that (degrees, as far as `wk_neck`) and
/// its pitch (degrees up), the way it looks.
pub struct Body {
pub bd_x: float,
pub bd_y: float,
pub bd_feet: float,
pub bd_yaw: float,
pub bd_head: float,
pub bd_pitch: float,
}
/// A body standing at (x, y) with its feet at `feet`, facing `yaw`, looking straight ahead.
pub fn body_new(x: float, y: float, feet: float, yaw: float) -> Body {
Body { bd_x: x, bd_y: y, bd_feet: feet, bd_yaw: yaw, bd_head: 0.0, bd_pitch: 0.0 }
}
/// The way the body walks, on the plan, of unit length.
pub fn body_way(b: const Body) -> (float, float) {
(sin(b.bd_yaw * PI / 180.0), cos(b.bd_yaw * PI / 180.0))
}
/// The way the body's head looks, on the plan, of unit length.
pub fn body_view(b: const Body) -> (float, float) {
(sin((b.bd_yaw + b.bd_head) * PI / 180.0), cos((b.bd_yaw + b.bd_head) * PI / 180.0))
}
/// One frame's walk, `dt` seconds of it, by the body its own way, forward (`go` 1), back (-1) or
/// standing (0), in the place, its feet on `floor(x, y, from)`. On a ladder, walking into it climbs
/// it and away from it climbs down. Else the walk eases into a free way it is a little off, is swept
/// in steps of at most `wk_sub`, pushes the leaves it walks into, and is pushed out of every wall and
/// leaf after each step; a step up higher than `wk_step_up` ends it. Moves the body; answers whether a
/// leaf moved.
pub fn walk_step(w: const Walker, pl: Place, b: Body, go: float, dt: float, floor: fn(float, float, float) -> float) -> boolean {
moved = false;
if go == 0.0 { return moved; }
(fx, fy) = body_way(b);
x = b.bd_x;
y = b.bd_y;
feet = b.bd_feet;
gone = if go < 0.0 { 0.0 - go } else { go };
step = w.wk_pace * dt;
li = ladder_at(pl.pl_ladders, x, y, feet);
if li >= 0 {
lp = pl.pl_ladders[li]!;
top = lp.ld_z + lp.ld_h;
// how straight the walk runs into the ladder (1) or away from it (-1); along it, it is walked past
into = 0.0 - (fx * go * lp.ld_nx + fy * go * lp.ld_ny);
front = (x - lp.ld_x) * lp.ld_nx + (y - lp.ld_y) * lp.ld_ny >= 0.0;
if front && into >= 0.5 && feet < top - 0.001 {
up = feet + w.wk_climb * dt;
b.bd_feet = if up > top { top } else { up };
return moved;
}
if front && into <= -0.5 && feet > lp.ld_z + 0.001 {
down = feet - w.wk_climb * dt;
b.bd_feet = if down < lp.ld_z { lp.ld_z } else { down };
return moved;
}
}
// a little off a free way, the walk eases into it as it comes: across it by no more than wk_ease of
// how far it goes this frame; the walls only, a leaf in the way is no stop (walking into it opens it)
wg = if go < 0.0 { -1.0 } else { 1.0 };
(o, _stop) = walk_ease(w, pl.pl_walls, x, y, feet, fx * wg, fy * wg);
most = w.wk_ease * step * gone;
lat = if o > most { most } else if o < 0.0 - most { 0.0 - most } else { o };
side_x = fy * wg * lat;
side_y = 0.0 - fx * wg * lat;
lb = leaves_stretches(pl.pl_leaves);
nsub = ceil((step * gone) / w.wk_sub ?? 0.0) as integer;
for _ in 0..nsub {
tx = x + (fx * go * step + side_x) / (nsub as float) ?? 0.0;
ty = y + (fy * go * step + side_y) / (nsub as float) ?? 0.0;
fe = floor(tx, ty, feet);
// on a ladder the body holds on: it does not fall through the hatch it stands over
if fe < feet && ladder_at(pl.pl_ladders, tx, ty, feet) >= 0 { fe = feet; }
if fe - feet > w.wk_step_up { break; }
// walking into a leaf pushes it: turned the way the walk turns it about its hinge, as far as
// clears the body, and from there it swings on, open or shut; a leaf it cannot turn (pushed
// against its hinge, or at its limit) stops the body
for k in 0..len(pl.pl_leaves) {
l = pl.pl_leaves[k]!;
v = l.lf_value;
if leaf_clear(w, l, v, tx, ty, fe) { continue; }
kb = lb[k]!;
ex = kb.sr_bx - kb.sr_ax;
ey = kb.sr_by - kb.sr_ay;
sn = ((tx - kb.sr_ax) * ex + (ty - kb.sr_ay) * ey) / (ex * ex + ey * ey) ?? 0.0;
if sn < 0.0 { sn = 0.0; }
if sn > 1.0 { sn = 1.0; }
tq = leaf_turn(l, kb.sr_ax + ex * sn, kb.sr_ay + ey * sn, fx * go, fy * go);
if tq <= w.wk_turns && tq >= 0.0 - w.wk_turns { continue; }
sg = if tq > 0.0 { 1.0 } else { -1.0 };
j = hex_body::rig_joint(l.lf_rig, 1);
nv = v;
while !leaf_clear(w, l, nv, tx, ty, fe) && hex_body::joint_fits(j, nv + sg * 0.004) { nv += sg * 0.004; }
pl.pl_leaves[k].lf_value = nv;
pl.pl_leaves[k].lf_to = if sg > 0.0 { LEAF_OPEN } else { 0.0 };
lb[k] = leaf_stretch(pl.pl_leaves[k]!, nv);
moved = true;
}
(x, y) = walk_push_out(w, pl.pl_walls, lb, tx, ty, fe);
fa = floor(x, y, fe);
if fa < fe && ladder_at(pl.pl_ladders, x, y, fe) >= 0 { fa = fe; }
feet = fa;
}
b.bd_x = x;
b.bd_y = y;
b.bd_feet = feet;
moved
}
// --- The head ---
/// The body's head after the pointer moved (dx, dy) pixels: it turns `wk_look` degrees a pixel, up to
/// `wk_neck` either way of the body — past that the body turns with it (no neck turns free), its yaw
/// kept within -180 .. 180 — and looks up and down as far as `wk_pitch`.
pub fn walk_look(w: const Walker, b: Body, dx: float, dy: float) {
h = b.bd_head + dx * w.wk_look;
y = b.bd_yaw;
p = b.bd_pitch - dy * w.wk_look;
if h > w.wk_neck {
y += h - w.wk_neck;
h = w.wk_neck;
}
if h < 0.0 - w.wk_neck {
y += h + w.wk_neck;
h = 0.0 - w.wk_neck;
}
if p > w.wk_pitch { p = w.wk_pitch; }
if p < 0.0 - w.wk_pitch { p = 0.0 - w.wk_pitch; }
while y > 180.0 { y -= 360.0; }
while y < -180.0 { y += 360.0; }
b.bd_head = h;
b.bd_yaw = y;
b.bd_pitch = p;
}
/// The body's head settling as the body walks or turns, `dt` seconds of it: its turn from the body eases
/// back to straight ahead, a part `1 - e^(-dt / wk_settle)` of it a frame, so after a while of moving it
/// looks the way it goes without the pointer moving (owner, 2026-12-04). Its pitch stays: a walk may
/// look down a stair it goes down. A caller settles the head only while the body moves — standing, a
/// head keeps looking where it was turned.
pub fn walk_settle(w: const Walker, b: Body, dt: float) {
if w.wk_settle <= 0.0 { b.bd_head = 0.0; return; }
b.bd_head = b.bd_head * (exp(0.0 - dt / w.wk_settle) ?? 0.0);
}
/// The way an eye looks `yaw` degrees from north towards the east and `pitch` up, of unit length.
pub fn look_way(yaw: float, pitch: float) -> (float, float, float) {
cp = cos(pitch * PI / 180.0);
(sin(yaw * PI / 180.0) * cp, cos(yaw * PI / 180.0) * cp, sin(pitch * PI / 180.0))
}
/// The way through window pixel (px, py) of a `w` x `h` window, from an eye looking `yaw` and
/// `pitch` (as `look_way`) with a view `field` degrees high, of unit length.
pub fn look_ray(yaw: float, pitch: float, field: float, px: integer, py: integer, w: integer, h: integer) -> (float, float, float) {
f = look_way(yaw, pitch);
// the eye's right: east when it looks north; its up: the right across the way it looks
rx = cos(yaw * PI / 180.0);
ry = 0.0 - sin(yaw * PI / 180.0);
ux = ry * f.2;
uy = 0.0 - rx * f.2;
uz = rx * f.1 - ry * f.0;
half = tan(field * PI / 360.0);
aspect = (w as float) / (h as float) ?? 1.0;
sx = (2.0 * ((px as float) + 0.5) / (w as float) - 1.0) ?? 0.0;
sy = (1.0 - 2.0 * ((py as float) + 0.5) / (h as float)) ?? 0.0;
dx = f.0 + (rx * sx * aspect + ux * sy) * half;
dy = f.1 + (ry * sx * aspect + uy * sy) * half;
dz = f.2 + uz * sy * half;
l = sqrt(dx * dx + dy * dy + dz * dz) ?? 1.0;
(dx / l ?? 0.0, dy / l ?? 1.0, dz / l ?? 0.0)
}
tests/01-walk.loft
// Copyright (c) 2026 Jurjen Stellingwerff
// SPDX-License-Identifier: LGPL-3.0-or-later
//
// A body walking among walls (`walk_step`, `walk_ease`, `walk_stretches`): stopped by a wall, stepping
// up what a foot takes and no higher, and eased into a doorway it is a little off without ever being
// stopped. Every expected value is worked out by hand from the fixture beside it.
//
// The measures are `walker_new`'s: a body 0.2 wide each way, a step up of 0.45, a pace of 1.5 / 0.14
// m/s; at 60 frames a second a frame's walk is 0.178571 m, swept in four steps of 0.044643.
use hex_walk::*;
use hex_edge;
// A few steps of a single in a 32-bit program, where a walk's sum of steps lands a step or two off.
const EPS = 0.0001;
const DT = 1.0 / 60.0;
const FRAME = 1.5 / 0.14 / 60.0;
fn near(a: float, b: float) -> boolean { a - b < EPS && b - a < EPS }
fn wall(ax: float, ay: float, bx: float, by: float) -> Stretch {
Stretch { sr_ax: ax, sr_ay: ay, sr_bx: bx, sr_by: by, sr_z0: 0.0, sr_z1: 2.5 }
}
fn flat(_x: float, _y: float, _from: float) -> float { 0.0 }
// A floor 0.4 up from y = 1, and 0.9 up from y = 2: one step a foot takes, then one it does not.
fn steps(_x: float, y: float, _from: float) -> float {
if y < 1.0 { 0.0 } else if y < 2.0 { 0.4 } else { 0.9 }
}
// The body walked north from (x, y) for `frames` frames; where it ends and its feet.
fn walk_north(w: const Walker, walls: const vector<Stretch>, x: float, y: float, frames: integer, floor: fn(float, float, float) -> float) -> (float, float, float) {
pl = place_new(walls);
b = body_new(x, y, 0.0, 0.0);
for _ in 0..frames { _m = walk_step(w, pl, b, 1.0, DT, floor); }
(b.bd_x, b.bd_y, b.bd_feet)
}
fn test_a_wall_square_across_stops_the_body_a_radius_off() {
// a wall along y = 1 from x -2 to 2: walking north from (0, 0) the body stops at 1 - 0.2
e = walk_north(walker_new(), [wall(-2.0, 1.0, 2.0, 1.0)], 0.0, 0.0, 30, flat);
assert(near(e.0, 0.0) && near(e.1, 0.8), "stopped at (0, 0.8): {e.0}, {e.1}");
// and no sidestep eases it: from (0, 0.5) the way is stopped 0.375 ahead (the first eighth of 0.75
// past 0.3), and no sidestep of 0.3 frees a wall 4 wide
ea = walk_ease(walker_new(), [wall(-2.0, 1.0, 2.0, 1.0)], 0.0, 0.5, 0.0, 0.0, 1.0);
assert(near(ea.0, 0.0) && near(ea.1, 0.375), "no sidestep, stopped 0.375 ahead: {ea.0}, {ea.1}");
// a wall that does not span the body's height stops nothing: one 0.4 high (under the step up)
low = Stretch { sr_ax: -2.0, sr_ay: 1.0, sr_bx: 2.0, sr_by: 1.0, sr_z0: 0.0, sr_z1: 0.4 };
assert(near(walk_north(walker_new(), [low], 0.0, 0.0, 10, flat).1, 10.0 * FRAME), "walked over a kerb");
}
fn test_a_step_a_foot_takes_is_walked_up_and_a_higher_one_ends_the_walk() {
// up 0.4 at y = 1, taken; 0.5 more at y = 2, not: the last of the walk's steps short of 2 is its
// 44th, at 44 x 0.044643 = 1.964286, its feet 0.4
e = walk_north(walker_new(), [], 0.0, 0.0, 30, steps);
assert(near(e.1, 11.0 * FRAME) && near(e.2, 0.4), "stopped at 1.964286 with its feet at 0.4: {e.1}, {e.2}");
}
fn test_a_body_a_little_off_a_doorway_eases_into_it_and_is_never_stopped() {
// a doorway 1 wide in a wall along y = 1, its sides at x = -0.5 and 0.5; the body at x 0.36, 0.14
// off the side, needs 0.06 to pass (x at most 0.3). Frame 0 (y = 0) sees no stop within 0.75; frame
// 1 (y = 0.178571) sees it 0.75 ahead and eases by the smallest sidestep its look finds free, -0.05
// (x 0.31); frame 2 by -0.025 more (x 0.285); from there the way is free. After 10 frames: (0.285,
// 10 frames' walk), never slowed
doorway = [wall(-3.0, 1.0, -0.5, 1.0), wall(0.5, 1.0, 3.0, 1.0)];
e = walk_north(walker_new(), doorway, 0.36, 0.0, 10, flat);
assert(near(e.0, 0.285) && near(e.1, 10.0 * FRAME), "eased to x 0.285, walked on: {e.0}, {e.1}");
// without the ease (wk_ease 0) the side of the doorway stops it and turns it round its end
stiff = walker_new();
stiff.wk_ease = 0.0;
s = walk_north(stiff, doorway, 0.36, 0.0, 10, flat);
assert(s.1 < 10.0 * FRAME - 0.01, "without it, held up at the doorway's side: {s.1}");
}
// A face standing (@FR-HW-Stand), at s times: a face 2 long and 2.5 high along y = 0 with its top
// (passing); a kerb 0.2 high along y = 1; a rail 0.05 high at the chest, 1.1 to 1.15, along y = 3; a
// ramp from 1 up at y = 4 to 1.5 at y = 5, its normal's upward part 1 / √1.25 = 0.894, over
// `FLOOR_STEEP`. Each that stands is a stretch from (0, y) to (2, y) between its foot and its top,
// times s — the top and the ramp stand nowhere (a foot stands on them), and the top passes besides. Walked north from (1, 0.5) at full size the body steps over the
// kerb (its top 0.2, under the step of 0.45) and stops 0.2 short of the rail, at y 2.8; at 1/100 and
// 1/1000 of the size the stretches are the same, scaled — the kerb 2 mm long and 0.2 mm high.
fn test_every_face_that_stands_is_a_stretch_and_the_walker_says_which_stop_it() {
for s in [1.0, 0.01, 0.001] {
q = hex_edge::quads_new();
hex_edge::quad_add(q, (0.0, 0.0, 0.0), (2.0 * s, 0.0, 0.0), (2.0 * s, 0.0, 2.5 * s), (0.0, 0.0, 2.5 * s), (0.0, -1.0, 0.0), 1, hex_edge::PART_FACE);
hex_edge::quad_add(q, (0.0, 0.0, 2.5 * s), (2.0 * s, 0.0, 2.5 * s), (2.0 * s, 0.2 * s, 2.5 * s), (0.0, 0.2 * s, 2.5 * s), (0.0, 0.0, 1.0), 1, hex_edge::PART_TOP);
hex_edge::quad_add(q, (0.0, s, 0.0), (2.0 * s, s, 0.0), (2.0 * s, s, 0.2 * s), (0.0, s, 0.2 * s), (0.0, -1.0, 0.0), 1, hex_edge::PART_FACE);
hex_edge::quad_add(q, (0.0, 3.0 * s, 1.1 * s), (2.0 * s, 3.0 * s, 1.1 * s), (2.0 * s, 3.0 * s, 1.15 * s), (0.0, 3.0 * s, 1.15 * s), (0.0, -1.0, 0.0), 1, hex_edge::PART_FACE);
hex_edge::quad_add(q, (0.0, 4.0 * s, s), (2.0 * s, 4.0 * s, s), (2.0 * s, 5.0 * s, 1.5 * s), (0.0, 5.0 * s, 1.5 * s), (0.0, -0.5, 1.0), 1, hex_edge::PART_FACE);
ks = walk_stretches(q, 0, [hex_edge::PART_TOP]);
assert(len(ks) == 3, "at {s}, three stretches: {len(ks)}");
for c in [(0, 0.0, 0.0, 2.5), (1, 1.0, 0.0, 0.2), (2, 3.0, 1.1, 1.15)] {
k = ks[c.0]!;
// measured back at full size, so 1/1000 of it is held as closely as the whole
assert(near(k.sr_ax / s ?? 0.0, 0.0) && near(k.sr_ay / s ?? 0.0, c.1) && near(k.sr_bx / s ?? 0.0, 2.0) && near(k.sr_by / s ?? 0.0, c.1),
"at {s}, stretch {c.0} along y {c.1}");
assert(near(k.sr_z0 / s ?? 0.0, c.2) && near(k.sr_z1 / s ?? 0.0, c.3), "at {s}, stretch {c.0} from {k.sr_z0} to {k.sr_z1}");
}
if s == 1.0 {
at = walk_north(walker_new(), ks, 1.0, 0.5, 30, flat);
assert(near(at.0, 1.0) && near(at.1, 2.8), "over the kerb, stopped by the rail: ({at.0}, {at.1})");
}
}
}
fn main() {
test_a_wall_square_across_stops_the_body_a_radius_off();
test_a_step_a_foot_takes_is_walked_up_and_a_higher_one_ends_the_walk();
test_a_body_a_little_off_a_doorway_eases_into_it_and_is_never_stopped();
test_every_face_that_stands_is_a_stretch_and_the_walker_says_which_stop_it();
}
tests/02-doors-ladders-head.loft
// Copyright (c) 2026 Jurjen Stellingwerff
// SPDX-License-Identifier: LGPL-3.0-or-later
//
// Door leaves, ladders and the head (`walk_step`, `walk_pull`, `leaves_swing`, `walk_look`,
// `look_ray`): a leaf pushed open by walking into it and stopping the body pushed the other way, a pull
// swinging it towards the body and stopping against it, a ladder climbed, and a head that turns the
// body once it has turned as far as a neck goes. Every expected value is worked out by hand from the
// fixture beside it.
//
// The fixture: a wall along y = 1 with a doorway from x = -0.5 to 0.5, and in it a leaf hinged at
// (-0.5, 1), shut along +x, 1 wide, 0 to 2.2 high, 0.05 thick to the right of its way (south): the
// line the walk meets runs down its middle at y = 0.975. It opens anticlockwise, into the north.
use hex_walk::*;
const EPS = 0.0001;
const DT = 1.0 / 60.0;
fn near(a: float, b: float) -> boolean { a - b < EPS && b - a < EPS }
fn wall(ax: float, ay: float, bx: float, by: float) -> Stretch {
Stretch { sr_ax: ax, sr_ay: ay, sr_bx: bx, sr_by: by, sr_z0: 0.0, sr_z1: 2.5 }
}
fn flat(_x: float, _y: float, _from: float) -> float { 0.0 }
fn doorway() -> Place {
pl = place_new([wall(-3.0, 1.0, -0.5, 1.0), wall(0.5, 1.0, 3.0, 1.0)]);
pl.pl_leaves += [leaf_of((-0.5, 1.0, 1.0, 0.0, 1.0, 0.0, 2.2), 0.05, 7)];
pl
}
fn test_a_leaf_walked_into_from_behind_swings_open_and_from_in_front_stops_the_body() {
// from (0, 0) north: the body meets the leaf at its middle, the arm from the hinge (0.5, -0.025)
// square to the walk, a turn of 0.9988 (over wk_turns 0.3): pushed open as far as clears the body,
// sent on to a quarter turn; the body walks on unstopped, 30 frames of 0.178571
w = walker_new();
pl = doorway();
b = body_new(0.0, 0.0, 0.0, 0.0);
for _ in 0..30 { _m = walk_step(w, pl, b, 1.0, DT, flat); }
l = pl.pl_leaves[0]!;
assert(l.lf_value > 0.0 && near(l.lf_to, LEAF_OPEN), "pushed open, swinging to a quarter turn: {l.lf_value}, {l.lf_to}");
// a pushed leaf stops once it clears the body by its radius less 0.001 (`leaf_clear`), so the walk
// brushes it by at most that: the body ends within 0.001 of where it walks to
full = 30.0 * 1.5 / 0.14 / 60.0;
assert(b.bd_x < 0.001 && b.bd_x > -0.001 && b.bd_y > full - 0.001 && b.bd_y <= full + EPS, "walked on through: {b.bd_x}, {b.bd_y}");
// from (0, 2) south: the walk turns it shut (-0.9988), past its limit: it stays and stops the body
// 0.2 north of its line, at 1.175
pl2 = doorway();
b2 = body_new(0.0, 2.0, 0.0, 180.0);
for _ in 0..30 { _m = walk_step(w, pl2, b2, 1.0, DT, flat); }
assert(near(pl2.pl_leaves[0].lf_value, 0.0) && near(b2.bd_y, 1.175), "stopped at 1.175, the leaf shut: {b2.bd_y}");
}
fn test_a_pull_swings_the_leaf_towards_the_body_and_it_stops_against_it() {
// from (0, 1.6) looking south: the eye meets the leaf 0.625 off (within wk_hand 1.2); the body is
// to the leaf's left (0.6 / 0.781 = 0.768), so the pull sends it open, towards the body
w = walker_new();
pl = doorway();
b = body_new(0.0, 1.6, 0.0, 180.0);
assert(walk_pull(w, pl, b) == 0 && near(pl.pl_leaves[0].lf_to, LEAF_OPEN), "leaf 0 in hand, sent open");
// it swings 0.01 a frame; the body 0.781 from the hinge at 50.19 degrees: at 0.10 turns (36
// degrees) the leaf's line passes 0.2165 from it, at 0.11 (39.6) 0.1685, under the body's 0.2 — so
// it stops at 0.10
for _ in 0..30 { _m = leaves_swing(w, pl, b, DT, false); }
assert(near(pl.pl_leaves[0].lf_value, 0.10), "stopped against the body at 0.10: {pl.pl_leaves[0].lf_value}");
// the body steps away: it swings on to a quarter turn, 15 frames more
b.bd_y = 3.0;
for _ in 0..20 { _m = leaves_swing(w, pl, b, DT, false); }
assert(near(pl.pl_leaves[0].lf_value, LEAF_OPEN), "on to a quarter turn: {pl.pl_leaves[0].lf_value}");
// looking away (north), nothing is in hand
pl3 = doorway();
assert(walk_pull(w, pl3, body_new(0.0, 1.6, 0.0, 0.0)) == -1, "nothing in hand looking away");
}
fn mirrored() -> Place {
pl = place_new([wall(-3.0, 1.0, -0.5, 1.0), wall(0.5, 1.0, 3.0, 1.0)]);
pl.pl_leaves += [leaf_of((0.5, 1.0, -1.0, 0.0, 1.0, 0.0, 2.2), 0.05, 7, -1.0)];
pl
}
fn test_a_leaf_hung_on_the_right_is_the_mirror_of_one_hung_on_the_left() {
// the doorway's leaf mirrored across x = 0: hinged at (0.5, 1), shut along -x, turning clockwise — so
// still into the north — its thickness to the left of its way, south, so the walk meets the same line
// y = 0.975, on over its play (0.005) at both ends. Every walk of the leaf's tests mirrored answers
// the same, mirrored.
w = walker_new();
pl = mirrored();
k = leaf_stretch(pl.pl_leaves[0]!, 0.0);
assert(near(k.sr_ay, 0.975) && near(k.sr_by, 0.975) && near(k.sr_ax, 0.505) && near(k.sr_bx, -0.505), "its line from (0.505, 0.975) to (-0.505, 0.975): ({k.sr_ax}, {k.sr_ay}), ({k.sr_bx}, {k.sr_by})");
// walked into from the south it swings open and the body walks on through
b = body_new(0.0, 0.0, 0.0, 0.0);
for _ in 0..30 { _m = walk_step(w, pl, b, 1.0, DT, flat); }
l = pl.pl_leaves[0]!;
full = 30.0 * 1.5 / 0.14 / 60.0;
assert(l.lf_value > 0.0 && near(l.lf_to, LEAF_OPEN), "pushed open: {l.lf_value}, {l.lf_to}");
assert(b.bd_x < 0.001 && b.bd_x > -0.001 && b.bd_y > full - 0.001 && b.bd_y <= full + EPS, "walked on through: {b.bd_x}, {b.bd_y}");
// from the north it stays shut and stops the body at 1.175
pl2 = mirrored();
b2 = body_new(0.0, 2.0, 0.0, 180.0);
for _ in 0..30 { _m = walk_step(w, pl2, b2, 1.0, DT, flat); }
assert(near(pl2.pl_leaves[0].lf_value, 0.0) && near(b2.bd_y, 1.175), "stopped at 1.175, the leaf shut: {b2.bd_y}");
// pulled from (0, 1.6) looking south: sent open, it stops against the body at 0.10, as the other does
pl3 = mirrored();
b3 = body_new(0.0, 1.6, 0.0, 180.0);
assert(walk_pull(w, pl3, b3) == 0 && near(pl3.pl_leaves[0].lf_to, LEAF_OPEN), "in hand, sent open");
for _ in 0..30 { _m = leaves_swing(w, pl3, b3, DT, false); }
assert(near(pl3.pl_leaves[0].lf_value, 0.10), "stopped against the body at 0.10: {pl3.pl_leaves[0].lf_value}");
// a quarter turn open it stands north from its hinge, 1 long, its thickness west of it: x 0.45 .. 0.5
leaf_set(pl3.pl_leaves[0], LEAF_OPEN);
q = hex_edge::quads_new();
_s = leaves_quads(pl3.pl_leaves, q);
lo_x = 9.0; hi_x = -9.0; lo_y = 9.0; hi_y = -9.0;
for i in 0..q.qd_n * 4 {
x = q.qd_xyz[i * 3] ?? 0.0;
y = q.qd_xyz[i * 3 + 1] ?? 0.0;
if x < lo_x { lo_x = x; } if x > hi_x { hi_x = x; }
if y < lo_y { lo_y = y; } if y > hi_y { hi_y = y; }
}
assert(near(lo_x, 0.45) && near(hi_x, 0.5) && near(lo_y, 1.0) && near(hi_y, 2.0), "drawn x 0.45 .. 0.5, y 1 .. 2: {lo_x} .. {hi_x}, {lo_y} .. {hi_y}");
}
fn test_a_pair_of_leaves_shut_with_play_between_them_stops_a_body_straight() {
// a doorway from x = -1 to 1 closed by a pair, as hex_edge draws one: each 0.9925 wide (the 2 less
// their play, 0.005, at the posts and between them, halved), the first hinged at (-0.995, 1) and
// shut along +x to -0.0025, its mirror hinged at (0.995, 1) and shut along -x to 0.0025 — 5 mm
// apart. Walked into from the north, 0.001 east of the middle, neither can turn shut further, so
// the body stops a radius off their line y = 0.975, at 1.175, where it walked: the walk meets each
// over its play, so the two lines overlap and a body never meets the end of either
w = walker_new();
pl = place_new([wall(-3.0, 1.0, -1.0, 1.0), wall(1.0, 1.0, 3.0, 1.0)]);
pl.pl_leaves += [leaf_of((-0.995, 1.0, 1.0, 0.0, 0.9925, 0.0, 2.2), 0.05, 7)];
pl.pl_leaves += [leaf_of((0.995, 1.0, -1.0, 0.0, 0.9925, 0.0, 2.2), 0.05, 7, -1.0)];
b = body_new(0.001, 2.0, 0.0, 180.0);
for _ in 0..90 { _m = walk_step(w, pl, b, 1.0, DT, flat); }
assert(near(pl.pl_leaves[0].lf_value, 0.0) && near(pl.pl_leaves[1].lf_value, 0.0), "both stay shut: {pl.pl_leaves[0].lf_value}, {pl.pl_leaves[1].lf_value}");
assert(near(b.bd_y, 1.175) && near(b.bd_x, 0.001), "stopped at (0.001, 1.175), not pushed along: ({b.bd_x}, {b.bd_y})");
}
fn test_walking_into_a_ladder_climbs_it_and_away_from_it_climbs_down() {
// a ladder at (0, 0.5) climbed from the south, 0.5 wide, 2 up: from (0, 0.3) walking north the feet
// rise 1.2 m/s, 0.02 a frame — 0.6 after 30 frames, the body where it was; walking back, 10 frames
// down to 0.4
w = walker_new();
pl = place_new([]);
pl.pl_ladders += [Ladder { ld_x: 0.0, ld_y: 0.5, ld_z: 0.0, ld_nx: 0.0, ld_ny: -1.0, ld_width: 0.5, ld_h: 2.0 }];
b = body_new(0.0, 0.3, 0.0, 0.0);
for _ in 0..30 { _m = walk_step(w, pl, b, 1.0, DT, flat); }
assert(near(b.bd_feet, 0.6) && near(b.bd_y, 0.3), "climbed to 0.6, standing where it was: {b.bd_feet}, {b.bd_y}");
for _ in 0..10 { _m = walk_step(w, pl, b, -1.0, DT, flat); }
assert(near(b.bd_feet, 0.4), "down to 0.4: {b.bd_feet}");
}
fn test_the_head_turns_the_body_once_it_has_turned_as_far_as_a_neck_goes() {
// head 60, yaw 10: 200 pixels right turn the head 20 more, 10 past wk_neck 70 — the body turns 10;
// 100 pixels up look 10 up
w = walker_new();
b = body_new(0.0, 0.0, 0.0, 10.0);
b.bd_head = 60.0;
walk_look(w, b, 200.0, -100.0);
assert(near(b.bd_head, 70.0) && near(b.bd_yaw, 20.0) && near(b.bd_pitch, 10.0), "head 70, yaw 20, pitch 10: {b.bd_head}, {b.bd_yaw}, {b.bd_pitch}");
// turned left past the neck at yaw -175: the body to -185, which is 175; a look 100 up stops at 60
c = body_new(0.0, 0.0, 0.0, -175.0);
c.bd_head = -70.0;
walk_look(w, c, -100.0, -1000.0);
assert(near(c.bd_head, -70.0) && near(c.bd_yaw, 175.0) && near(c.bd_pitch, 60.0), "yaw 175, pitch 60: {c.bd_yaw}, {c.bd_pitch}");
// the middle pixel of a 3 x 3 window looks where the eye does: yaw 90 is east
r = look_ray(90.0, 0.0, 70.0, 1, 1, 3, 3);
assert(near(r.0, 1.0) && near(r.2, 0.0) && near(r.1, 0.0), "east: {r.0}, {r.1}, {r.2}");
}
fn test_the_head_settles_straight_ahead_as_the_body_moves() {
// head 40 at yaw 10, pitch -20: 30 frames of moving (half a second, wk_settle 0.5) leave 40 / e =
// 14.715178 of its turn, 90 frames (1.5 s) 40 / e^3 = 1.991483; the body's yaw and the pitch as they were
w = walker_new();
b = body_new(0.0, 0.0, 0.0, 10.0);
b.bd_head = 40.0;
b.bd_pitch = -20.0;
for _ in 0..30 { walk_settle(w, b, DT); }
assert(near(b.bd_head, 14.715178) && near(b.bd_yaw, 10.0) && near(b.bd_pitch, -20.0), "head 14.715178 after half a second: {b.bd_head}, {b.bd_yaw}, {b.bd_pitch}");
for _ in 0..60 { walk_settle(w, b, DT); }
assert(near(b.bd_head, 1.991483), "1.991483 after 1.5 s: {b.bd_head}");
// a walker that does not settle slowly looks straight ahead at once
w.wk_settle = 0.0;
c = body_new(0.0, 0.0, 0.0, 0.0);
c.bd_head = -30.0;
walk_settle(w, c, DT);
assert(near(c.bd_head, 0.0), "straight ahead at once: {c.bd_head}");
}
fn test_an_arched_leaf_is_drawn_in_slices_and_its_picture_turns_with_it() {
// a leaf hinged at the origin along +x, 1 wide, 0.05 thick, its top 2 at the hinge, 2.4 at its
// middle and 2.2 at its free edge: two slices, 12 faces less the two between them, 10. Its
// picture is laid from the hinge's foot, 1 along the leaf and 2.4 up: shut, a corner (x, y, z)
// lies at (x, z / 2.4); a quarter turn open the leaf runs north, and a corner at (y, z / 2.4)
l = leaf_of((0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 2.4), 0.05, 7, 1.0, hex_edge::LEAF_PLAY, [0.0, 2.0, 0.5, 2.4, 1.0, 2.2]);
for open in [false, true] {
if open { leaf_set(l, LEAF_OPEN); }
q = hex_edge::quads_new();
_s = leaves_quads([l], q);
assert(q.qd_n == 10, "two slices, the faces between them gone: {q.qd_n}");
crown = 0;
for i in 0..q.qd_n {
p = hex_edge::quad_picture(q, i);
for c in 0..4 {
x = q.qd_xyz[i * 12 + c * 3] ?? 0.0;
y = q.qd_xyz[i * 12 + c * 3 + 1] ?? 0.0;
z = q.qd_xyz[i * 12 + c * 3 + 2] ?? 0.0;
f = p[c] ?? (9.0, 9.0);
along = if open { y } else { x };
assert(near(f.0, along) && near(f.1, z / 2.4), "open {open}, face {i}: ({x}, {y}, {z}) at ({f.0}, {f.1})");
if near(f.0, 0.5) && near(f.1, 1.0) { crown += 1; }
}
}
assert(crown > 0, "its crown is the picture's top: open {open}");
}
}
fn main() {
test_a_leaf_walked_into_from_behind_swings_open_and_from_in_front_stops_the_body();
test_a_pull_swings_the_leaf_towards_the_body_and_it_stops_against_it();
test_a_leaf_hung_on_the_right_is_the_mirror_of_one_hung_on_the_left();
test_a_pair_of_leaves_shut_with_play_between_them_stops_a_body_straight();
test_walking_into_a_ladder_climbs_it_and_away_from_it_climbs_down();
test_the_head_turns_the_body_once_it_has_turned_as_far_as_a_neck_goes();
test_the_head_settles_straight_ahead_as_the_body_moves();
test_an_arched_leaf_is_drawn_in_slices_and_its_picture_turns_with_it();
}
tests/03-floors.loft
// Copyright (c) 2026 Jurjen Stellingwerff
// SPDX-License-Identifier: LGPL-3.0-or-later
//
// A body standing on what is drawn (`walk_floors`, `floor_heights`, `walk_floor`, @FR-HW-Floor): the
// faces facing up are its floors, whatever their part; a face's underside and a slope too steep to
// stand on are not; two faces a hair apart are one floor; of the floors under the feet the highest a
// step up reaches is stood on, else the lowest. Every expected value is worked out by hand from the
// fixture.
//
// The fixture, every face wound counter-clockwise seen from the side it faces (`hex_edge::quad_add`):
// - the ground: 0 over (0, 0) .. (10, 10);
// - a platform: its top at 0.4 over (2, 2) .. (4, 4), its underside at 0.2 under the same;
// - a deck at 3 over (2, 2) .. (6, 6), drawn as two halves 0.01 apart: (2, 2) .. (4, 6) and
// (4.01, 2) .. (6, 6);
// - a ramp of 30° over (6, 0) .. (8, 2), rising north: at y its height y · tan 30° = 0.57735 y;
// - a slope of 60° over (8, 0) .. (9, 1), rising east from x = 8: too steep to stand on;
// - a wall, upright, along x = 1 from y 0 to 10, 2 high;
// - a sliver of an underside at 0.25 over (5, 7) .. (5.03, 7.03): narrower than two seams, so its
// middle lies within `FLOOR_SEAM` of every edge whichever way it is wound — only its facing down
// keeps it from being a floor.
use hex_walk::*;
use hex_edge;
const EPS = 0.0001;
fn near(a: float, b: float) -> boolean { a - b < EPS && b - a < EPS }
const UP = (0.0, 0.0, 1.0);
const DOWN = (0.0, 0.0, -1.0);
fn fixture() -> hex_edge::Quads {
q = hex_edge::quads_new();
hex_edge::quad_add(q, (0.0, 0.0, 0.0), (10.0, 0.0, 0.0), (10.0, 10.0, 0.0), (0.0, 10.0, 0.0), UP, 1, hex_edge::PART_TOP);
hex_edge::quad_add(q, (2.0, 2.0, 0.4), (4.0, 2.0, 0.4), (4.0, 4.0, 0.4), (2.0, 4.0, 0.4), UP, 2, 7);
hex_edge::quad_add(q, (2.0, 2.0, 0.2), (4.0, 2.0, 0.2), (4.0, 4.0, 0.2), (2.0, 4.0, 0.2), DOWN, 2, 8);
hex_edge::quad_add(q, (2.0, 2.0, 3.0), (4.0, 2.0, 3.0), (4.0, 6.0, 3.0), (2.0, 6.0, 3.0), UP, 3, hex_edge::PART_TOP);
hex_edge::quad_add(q, (4.01, 2.0, 3.0), (6.0, 2.0, 3.0), (6.0, 6.0, 3.0), (4.01, 6.0, 3.0), UP, 3, hex_edge::PART_TOP);
t30 = tan(PI / 6.0);
hex_edge::quad_add(q, (6.0, 0.0, 0.0), (8.0, 0.0, 0.0), (8.0, 2.0, 2.0 * t30), (6.0, 2.0, 2.0 * t30), UP, 4, hex_edge::PART_TOP);
t60 = tan(PI / 3.0);
hex_edge::quad_add(q, (8.0, 0.0, 0.0), (9.0, 0.0, t60), (9.0, 1.0, t60), (8.0, 1.0, 0.0), UP, 5, hex_edge::PART_TOP);
hex_edge::quad_add(q, (1.0, 0.0, 0.0), (1.0, 10.0, 0.0), (1.0, 10.0, 2.0), (1.0, 0.0, 2.0), (-1.0, 0.0, 0.0), 6, 0);
hex_edge::quad_add(q, (5.0, 7.0, 0.25), (5.03, 7.0, 0.25), (5.03, 7.03, 0.25), (5.0, 7.03, 0.25), DOWN, 7, 0);
q
}
fn same(a: const vector<float>, b: const vector<float>) -> boolean {
if len(a) != len(b) { return false; }
for i in 0..len(a) { if !near(a[i] ?? 0.0, b[i] ?? 0.0) { return false; } }
true
}
// Over the platform: the ground, its top and the deck — not its underside at 0.2. On the ramp at
// y = 1: the ground and 0.57735. On the steep slope: the ground alone. On the wall's line: the ground
// alone (an upright face is no floor). In the sliver's middle: the ground alone. Off everything: none. The same on a grid of 1 m cells as of 4.
fn test_the_floors_are_the_faces_facing_up() {
for cell in [1.0, 4.0] {
fl = walk_floors(fixture(), 0, cell);
assert(same(floor_heights(fl, 3.0, 3.0), [0.0, 0.4, 3.0]), "over the platform, cell {cell}: {floor_heights(fl, 3.0, 3.0)}");
assert(same(floor_heights(fl, 7.0, 1.0), [0.0, tan(PI / 6.0)]), "on the ramp: {floor_heights(fl, 7.0, 1.0)}");
assert(same(floor_heights(fl, 8.5, 0.5), [0.0]), "on the steep slope: {floor_heights(fl, 8.5, 0.5)}");
assert(same(floor_heights(fl, 1.0, 5.0), [0.0]), "on the wall's line: {floor_heights(fl, 1.0, 5.0)}");
assert(same(floor_heights(fl, 5.015, 7.015), [0.0]), "in the sliver: {floor_heights(fl, 5.015, 7.015)}");
assert(len(floor_heights(fl, 20.0, 20.0)) == 0, "off everything");
}
}
// The deck's two halves, 0.01 apart, are one floor: at x = 4.005 both are within 0.02, so the deck is
// under the feet (twice) — at y = 5, off the platform, whose edge at x = 4 is as near. 0.05 past the
// deck's east edge, at x = 6.05, it is not. Kept with a seam of 0.004 of its own (a world drawn at a
// fifth of the size walks with a fifth of the seam), x = 4.005 is 0.005 off both halves: no deck there.
fn test_two_faces_a_hair_apart_are_one_floor() {
fl = walk_floors(fixture(), 0, 1.0);
assert(same(floor_heights(fl, 4.005, 5.0), [0.0, 3.0, 3.0]), "the seam: {floor_heights(fl, 4.005, 5.0)}");
assert(same(floor_heights(fl, 6.05, 3.0), [0.0]), "past the edge: {floor_heights(fl, 6.05, 3.0)}");
narrow = walk_floors(fixture(), 0, 1.0, 0.004);
assert(same(floor_heights(narrow, 4.005, 5.0), [0.0]), "a narrower seam: {floor_heights(narrow, 4.005, 5.0)}");
}
// Over the platform, with feet at 0 the step up of 0.45 reaches its top, 0.4; at 3 the deck; at 1 the
// platform again (the deck is 2 over). At -5 nothing is within a step: the lowest, the ground. On the
// ramp at y = 1 with feet at 0.3: 0.57735. A height in `more` (a stair's slope, 0.3) is a floor like
// the drawn ones. Off everything, the feet stay where they were.
fn test_the_highest_a_step_reaches_else_the_lowest() {
w = walker_new();
fl = walk_floors(fixture(), 0, 1.0);
assert(near(walk_floor(w, fl, 3.0, 3.0, 0.0, []), 0.4), "onto the platform {walk_floor(w, fl, 3.0, 3.0, 0.0, [])}");
assert(near(walk_floor(w, fl, 3.0, 3.0, 3.0, []), 3.0), "on the deck {walk_floor(w, fl, 3.0, 3.0, 3.0, [])}");
assert(near(walk_floor(w, fl, 3.0, 3.0, 1.0, []), 0.4), "down to the platform {walk_floor(w, fl, 3.0, 3.0, 1.0, [])}");
assert(near(walk_floor(w, fl, 3.0, 3.0, -5.0, []), 0.0), "nothing in reach: the lowest {walk_floor(w, fl, 3.0, 3.0, -5.0, [])}");
assert(near(walk_floor(w, fl, 7.0, 1.0, 0.3, []), tan(PI / 6.0)), "on the ramp {walk_floor(w, fl, 7.0, 1.0, 0.3, [])}");
assert(near(walk_floor(w, fl, 5.0, 8.0, 0.0, [0.3]), 0.3), "a slope given {walk_floor(w, fl, 5.0, 8.0, 0.0, [0.3])}");
assert(near(walk_floor(w, fl, 20.0, 20.0, 1.3, []), 1.3), "off everything {walk_floor(w, fl, 20.0, 20.0, 1.3, [])}");
}
tests/04-arcs.loft
// Copyright (c) 2026 Jurjen Stellingwerff
// SPDX-License-Identifier: LGPL-3.0-or-later
//
// A round wall met round (`Stretch` with `sr_r`, `stretch_near`, @FR-HW-Arc): a body is pushed out
// square from an arc, as from a line — along its radius — and round its ends; a face drawn flat for a
// circle (`hex_edge::quad_bend`) is met on that circle, not on its flat piece, so a body comes to rest
// the same 0.2 from a round wall wherever along it. Every expected value is worked out by hand from
// the geometry written beside it.
use hex_walk::*;
use hex_edge;
fn near(a: float, b: float, eps: float) -> boolean { a - b < eps && b - a < eps }
// The quarter of the circle of radius 3 round the origin from (3, 0) anticlockwise to (0, 3), 2.5 high.
fn quarter() -> Stretch {
Stretch { sr_ax: 3.0, sr_ay: 0.0, sr_bx: 0.0, sr_by: 3.0, sr_z0: 0.0, sr_z1: 2.5, sr_cx: 0.0, sr_cy: 0.0, sr_r: 3.0 }
}
fn at(r: float, deg: float) -> (float, float) { (r * cos(deg * PI / 180.0), r * sin(deg * PI / 180.0)) }
// At 45°: from radius 2.9 the body (0.2 wide each way) goes in to 2.8, from 3.1 out to 3.2, from on
// the arc out to 3.2 (its outside), all along the radius. At -2°, past the first end (3, 0) by a
// chord of 2·3·sin 1° = 0.104714, it is pushed round that end to 0.2 from it, along the chord's way
// at -91°: (3 + 0.2 cos -91°, 0.2 sin -91°) = (2.996510, -0.199970). At -20° it is 1.0419 from that
// end and stays. The middle is 3 from both ends (the nearest point is an end there); at 200° the
// nearer end is (0, 3), 2·3·sin 55° = 4.914912 off.
fn test_an_arc_is_met_square_and_round_its_ends() {
w = walker_new();
k = quarter();
for c in [(2.9, 2.8), (3.1, 3.2), (3.0, 3.2)] {
(x, y) = at(c.0, 45.0);
(px, py) = walk_push_from(w, k, x, y, 0.0);
(ex, ey) = at(c.1, 45.0);
assert(near(px, ex, 0.000001) && near(py, ey, 0.000001), "from radius {c.0}: ({px}, {py}), not radius {c.1}");
}
(x, y) = at(3.0, -2.0);
(px, py) = walk_push_from(w, k, x, y, 0.0);
assert(near(px, 2.996510, 0.000002) && near(py, -0.199970, 0.000002), "round the end: ({px}, {py})");
(x2, y2) = at(3.0, -20.0);
(qx, qy) = walk_push_from(w, k, x2, y2, 0.0);
assert(qx == x2 && qy == y2, "1.04 off the end it stays");
assert(near(stretch_dist(k, 0.0, 0.0), 3.0, 0.000001), "the middle: {stretch_dist(k, 0.0, 0.0)}");
(x3, y3) = at(3.0, 200.0);
assert(near(stretch_dist(k, x3, y3), 4.914912, 0.000002), "behind it: {stretch_dist(k, x3, y3)}");
}
// A ring round the origin, its surface at radius 3, 0.6 thick (faces at 3.3 and 2.7), 3 high, with a
// door 1 wide at angle 0 (from -0.5 to 0.5 along, sill 0, head 2.2). Its faces become arcs of their
// circles; its doorway's sides lines. A body 0.1 inside the inner face at 101.25° — the middle of a
// drawn piece (pieces of 7.5° from 0°), where the flat piece stands 2.7 (1 - cos 3.75°) = 0.0058
// inside the circle — comes to rest at 2.5, 0.2 from the circle. Walking north from the middle it
// stops at 2.5; walking east it goes out through the door (the doorway 2 x 2.7 sin(0.5/3) = 0.896
// wide at the inner face, the body 0.4) to past 4.
fn test_a_round_wall_is_walked_round_not_in_its_pieces() {
q = hex_edge::quads_new();
sf = hex_edge::surfaces_new();
f = hex_edge::features_new();
id = hex_edge::surf_arc(sf, 0.0, 0.0, 3.0);
_ = hex_edge::feature_add(f, id, -0.5, 0.5, 0.0, 2.2, hex_edge::FEAT_DOOR, 1);
_ = hex_edge::wall_quads(q, sf, f, id, -3.0 * PI, 3.0 * PI, -3.0 * PI, 3.0 * PI, 0.0, 3.0, 0.6, 1);
ks = walk_stretches(q, 0, [hex_edge::PART_TOP]);
arcs = 0;
for k in ks {
if k.sr_r > 0.0 {
assert(k.sr_cx == 0.0 && k.sr_cy == 0.0 && (near(k.sr_r, 3.3, 0.000001) || near(k.sr_r, 2.7, 0.000001)), "an arc of radius {k.sr_r}");
arcs += 1;
}
}
assert(arcs >= 94, "both sides' faces are arcs: {arcs}");
w = walker_new();
(x, y) = at(2.6, 101.25);
(px, py) = walk_push_out(w, ks, [], x, y, 0.0);
rr = sqrt(px * px + py * py) ?? 0.0;
assert(near(rr, 2.5, 0.0001), "at rest {rr} from the middle, not 2.5");
pl = place_new(ks);
flat = fn(_x: float, _y: float, _f: float) -> float { 0.0 };
b = body_new(0.0, 0.0, 0.0, 0.0);
for _ in 0..10 { _ = walk_step(w, pl, b, 1.0, 0.05, flat); }
assert(near(b.bd_x, 0.0, 0.001) && near(b.bd_y, 2.5, 0.001), "north stops at ({b.bd_x}, {b.bd_y})");
e = body_new(0.0, 0.0, 0.0, 90.0);
for _ in 0..10 { _ = walk_step(w, pl, e, 1.0, 0.05, flat); }
assert(e.bd_x > 4.0 && near(e.bd_y, 0.0, 0.01), "east goes out through the door to ({e.bd_x}, {e.bd_y})");
}
// Composed (`hex_edge::quads_compose`), a face loses what a face back to back with it covers, and
// the pieces left are answered in whatever order composing winds them: here faces laid against half
// of each of the ring's first twelve outer faces. Every arc the walk takes from them still runs
// anticlockwise from its first end, a short turn — no more than a piece's 7.5° — and not the rest of
// the circle the other way round, which would stand across the whole ring.
fn test_every_arc_runs_anticlockwise_from_its_first_end() {
q = hex_edge::quads_new();
sf = hex_edge::surfaces_new();
f = hex_edge::features_new();
id = hex_edge::surf_arc(sf, 0.0, 0.0, 3.0);
_ = hex_edge::wall_quads(q, sf, f, id, -3.0 * PI, 3.0 * PI, -3.0 * PI, 3.0 * PI, 0.0, 3.0, 0.6, 1);
n = q.qd_n;
laid = 0;
for i in 0..n {
(_, _, r) = hex_edge::quad_bend(q, i);
if r < 3.2 || laid >= 12 { continue; }
laid += 1;
c: vector<(float, float, float)> = [];
for k in 0..4 { c += [(q.qd_xyz[i * 12 + k * 3] ?? 0.0, q.qd_xyz[i * 12 + k * 3 + 1] ?? 0.0, q.qd_xyz[i * 12 + k * 3 + 2] ?? 0.0)]; }
a = c[0]!; b = c[1]!; cc = c[2]!; d = c[3]!;
m1 = ((a.0 + b.0) * 0.5, (a.1 + b.1) * 0.5, (a.2 + b.2) * 0.5);
m2 = ((d.0 + cc.0) * 0.5, (d.1 + cc.1) * 0.5, (d.2 + cc.2) * 0.5);
hex_edge::quad_add(q, a, m1, m2, d, (0.0 - (a.0 + cc.0), 0.0 - (a.1 + cc.1), 0.0), 1, hex_edge::PART_FACE);
}
out = hex_edge::quads_compose(q);
arcs = 0;
for k in walk_stretches(out, 0, [hex_edge::PART_TOP]) {
if k.sr_r <= 0.0 { continue; }
turn = atan2(k.sr_by, k.sr_bx) - atan2(k.sr_ay, k.sr_ax);
for _ in 0..2 { if turn < 0.0 { turn += 2.0 * PI; } }
assert(turn > 0.0 && turn <= 7.5 * PI / 180.0 + 0.000001, "an arc turning {turn * 180.0 / PI}°");
arcs += 1;
}
assert(arcs >= 94, "the arcs: {arcs}");
}