librarygames/raycaster/Rays.xtl

The raycaster's rules, a library: raycaster.xtl (scripted), play.xtl (the game: keys and the clock) and the web page all use these. The map is a grid of walls; you stand anywhere in it, facing any way. The view is one ray per column of the picture, every ray stepped through the grid at once (each to its next line across or down, whichever is nearer) until all of them have hit a wall: a fixed point. A wall's column is as tall as it is near. A game's state is a tuple: (where you are, x and y in tiles, y down; which way you face, radians, 0 east, turning right as it grows; the seconds played; how it stands: 0 walking, 1 out of the door).

source · imports sv: lib/Svg.xtl; t: lib/Text.xtl

The data

rows : Box Char

value (private) · line 19

The map: R rows of C tiles, row by row: 0 floor, 1 to 4 a wall's color, 5 the door.

rows ← "raycaster.toml" ⎕L̲IST "map"
Used in: cells, R

cells : Char

value (private) · line 20
cells ← ᵗj̲oin rows
Used in: C, GRID, AT, MAP

R : Int

value (private) · line 21
R ← t̲ally rows
Used in: C, ʰt̲ile

C : Int

value (private) · line 22
C ← (t̲ally cells) d̲iv R

GRID : Int

value (private) · line 23
GRID ← (".1234E@" i̲ndexOf cells) s̲elect 0 1 2 3 4 5 0

COLORS : Float

value (private) · line 25

The colors: a wall's by its kind, the door's.

COLORS ← 5 3 r̲eshape n̲umbers ᵗj̲oin '{ x → e̲nclose (d̲isclose x) c̲at " " } e̲ach ("raycaster.toml" ⎕L̲IST "colors") c̲at "raycaster.toml" ⎕L̲IST "door"
Used in: ʰc̲olumn

moves : Float

value (private) · line 27

A step, a turn, the view's width, the rays.

moves ← 'ˢᵛn̲um e̲ach "raycaster.toml" ⎕L̲IST "moves"
Used in: STEP, TURN, WIDE, RAYS

STEP : Float

value (private) · line 28
STEP ← f̲irst moves
Used in: ˡk̲ey

TURN : Float

value (private) · line 29
TURN ← 2 s̲elect moves
Used in: ˡk̲ey

WIDE : Float

value (private) · line 30
WIDE ← 3 s̲elect moves
Used in: ʰr̲ays

RAYS : Int

value (private) · line 31
RAYS ← f̲loor 4 s̲elect moves

AT : Int

value (private) · line 33

Where you start: the middle of the @ tile, facing east.

AT ← f̲irst w̲here cells = f̲irst "@"
Used in: ˡn̲ew

Walking

ˡn̲ew : (Any a, Num b) => a -> (Float, Float, Float, Float, b)

function · line 39

A new game: you at the start, facing east.

ˡn̲ew ← { z → (0.5 + f̲loat (AT − 1) m̲od C, 0.5 + f̲loat (AT − 1) d̲iv C, 0.0, 0.0, 0) }

ʰt̲ile : (Num a, Num b) => (a, b) -> Int

function (private) · line 42

What is on the tiles at x, y (any number of them): 0 floor, 1 to 4 a wall, 5 the door; beyond the map, a wall.

ʰt̲ile ← { (x, y) →
  c ← f̲loor x
  r ← f̲loor y
  out ← (c < 0) ∨ (r < 0) ∨ (c ≥ C) ∨ r ≥ R
  k ← 1 + (C × 0 m̲ax r m̲in R − 1) + 0 m̲ax c m̲in C − 1
  (k s̲elect GRID) m̲ax 3 × 0 + out
}

ʰf̲its : Truthy a => (Float, Float) -> a

function (private) · line 51

Whether you fit at x, y: no wall within 0.2 tiles, the four corners of your square tested at once.

ʰf̲its ← { (x, y) → '∧ r̲/ 0 = ʰt̲ile (x + 0.2 -0.2 0.2 -0.2, y + 0.2 0.2 -0.2 -0.2) }
Used in: ʰm̲oved

ʰd̲oor : Truthy a => (Float, Float) -> a

function (private) · line 53

Whether a step to x, y reaches the door.

ʰd̲oor ← { (x, y) → '∨ r̲/ 5 = ʰt̲ile (x + 0.2 -0.2 0.2 -0.2, y + 0.2 0.2 -0.2 -0.2) }
Used in: ʰm̲oved

ʰm̲oved : (Any a, Any b, Num c) => (Float, Float, a, b, c, Float, Float) -> (Float, Float, a, b, c)

function (private) · line 56

You moved by (dx, dy), if you fit there; out of the door if it is there.

ʰm̲oved ← { (x, y, a, t, st, dx, dy) →
  ʰd̲oor (x + dx, y + dy) ? (x, y, a, t, 1)
  ʰf̲its (x + dx, y + dy) ? (x + dx, y + dy, a, t, st)
  (x, y, a, t, st)
}
Used in: ˡk̲ey

ˡk̲ey : (Any a, Num b) => (Float, Float, Float, a, b) -> Char -> (Float, Float, Float, a, b)

function · line 65

A key: up (or w) steps ahead, down (or s) steps back, left and right turn, a and d step sideways; other keys, and keys once out, do nothing.

ˡk̲ey ← { s name →
  (x, y, a, t, st) ← s
  k ← ("UP" "DOWN" "LEFT" "RIGHT" "w" "s" "a" "d") i̲ndexOf e̲nclose name
  (k > 8) ∨ st > 0 ? s
  k = 3 ? (x, y, a − TURN, t, st)
  k = 4 ? (x, y, a + TURN, t, st)
  f ← (1 2 5 6 7 8 i̲ndexOf k) s̲elect 1.0 -1.0 1.0 -1.0 0.0 0.0
  g ← (1 2 5 6 7 8 i̲ndexOf k) s̲elect 0.0 0.0 0.0 0.0 -1.0 1.0
  ʰm̲oved (x, y, a, t, st, STEP × (f × c̲os a) − g × s̲in a, STEP × (f × s̲in a) + g × c̲os a)
}

ˡt̲ick : (Any a, Any b, Any c, Num d, Num e) => (a, b, c, d, e) -> d -> (a, b, c, d, e)

function · line 77

A tick of the clock: the time moves (once out, it stops).

ˡt̲ick ← { s dt →
  (x, y, a, t, st) ← s
  st > 0 ? s
  (x, y, a, t + dt, st)
}
Used in: ᵘl̲oop

Looking

ʰr̲ays : Num a => a -> (Float, Float)

function (private) · line 87

The rays: one for each column, from the left of the view to the right, each a direction (x and y).

ʰr̲ays ← { a →
  cam ← (2.0 × (f̲loat (r̲ange RAYS) − 1) ÷ f̲loat RAYS − 1) − 1.0
  ((c̲os a) − WIDE × cam × s̲in a, (s̲in a) + WIDE × cam × c̲os a)
}
Used in: ʰc̲ast

ʰs̲tep : (Num a, Truthy a, Truthy b, Num c) => (a, a, Float, Float, Float, Float, a, a, a, b, c) -> (a, a, Float, Float, Float, Float, a, a, a, b, c)

function (private) · line 94

Every ray one line further (across where its next line across is nearer, else down), all at once, until all have hit a wall (or forty lines, the map's diagonal and more).

ʰs̲tep ← { (mx, my, sx, sy, dx, dy, ix, iy, side, hit, n) →
  ('∧ r̲/ hit) ∨ n ≥ 40 ? (mx, my, sx, sy, dx, dy, ix, iy, side, hit, n)
  across ← (n̲ot hit) ∧ sx < sy
  down ← (n̲ot hit) ∧ n̲ot across
  fa ← f̲loat across
  fd ← f̲loat down
  tx ← mx + ix × 0 + across
  ty ← my + iy × 0 + down
  struck ← (side × 0 + n̲ot across ∨ down) + 0 + down
  wall ← 0 < ʰt̲ile (0.5 + f̲loat tx, 0.5 + f̲loat ty)
  ʰs̲tep (tx, ty, sx + fa × dx, sy + fd × dy, dx, dy, ix, iy, struck, wall, n + 1)
}
Used in: ʰs̲tep, ʰc̲ast

ʰc̲ast : Num a => (Float, Float, a) -> (Int, Int, Float)

function (private) · line 109

What every ray sees from x, y facing a: the wall it hits (its kind), which way it was struck (0 across, 1 down) and how far it is (along the way you face, so walls stand straight).

ʰc̲ast ← { (x, y, a) →
  (rx, ry) ← ʰr̲ays a
  dx ← 1.0 ÷ 0.000001 m̲ax a̲bs rx
  dy ← 1.0 ÷ 0.000001 m̲ax a̲bs ry
  mx0 ← RAYS r̲eshape f̲loor x
  my0 ← RAYS r̲eshape f̲loor y
  ix ← (0 + rx > 0.0) − 0 + rx < 0.0
  iy ← (0 + ry > 0.0) − 0 + ry < 0.0
  fx ← x − f̲loat f̲loor x
  fy ← y − f̲loat f̲loor y
  sx0 ← dx × (fx × f̲loat rx < 0.0) + (1.0 − fx) × f̲loat rx ≥ 0.0
  sy0 ← dy × (fy × f̲loat ry < 0.0) + (1.0 − fy) × f̲loat ry ≥ 0.0
  (mx, my, sx, sy, _, _, _, _, side, _, _) ← ʰs̲tep (mx0, my0, sx0, sy0, dx, dy, ix, iy, RAYS r̲eshape 0, RAYS r̲eshape 0 = 1, 0)
  far ← ((sx − dx) × f̲loat side = 0) + (sy − dy) × f̲loat side = 1
  (ʰt̲ile (0.5 + f̲loat mx, 0.5 + f̲loat my), side, 0.05 m̲ax far)
}
Used in: ˡs̲ees, ˡd̲raw

What it shows

ˡs̲tatus : (Any a, Any b, Any c, Any d, Any e) => (a, b, c, d, e) -> e

function · line 130

0 walking, 1 out of the door.

ˡs̲tatus ← { (x, y, a, t, st) → st }
Used in: ᵘs̲how, _

ˡy̲ou : Any b => (a, a, a, a, b) -> a

function · line 133

Where you are, which way you face, the seconds.

ˡy̲ou ← { (x, y, a, t, st) → x c̲at y c̲at a c̲at t }

ˡs̲ees : (Num a, Any b, Any c) => (Float, Float, a, b, c) -> Float

function · line 136

What the rays see, a row each: the wall's kind, its side, how far.

ˡs̲ees ← { (x, y, a, t, st) →
  (k, side, far) ← ʰc̲ast (x, y, a)
  o̲\ (3 c̲at RAYS) r̲eshape (f̲loat k) c̲at (f̲loat side) c̲at far
}

VIEW : Float

value (private) · line 141

The picture's size; the view below the top line.

VIEW ← 0.0 0.0 800.0 560.0

TOP : Float

value (private) · line 142
TOP ← 60.0

HIGH : Float

value (private) · line 143
HIGH ← 500.0

ʰc̲olumn : (Num a, Num b) => (a, Int, b, Float) -> Box Char

function (private) · line 146

One column of the view: its place, its wall's height and color (darker for a side struck down, and with the distance).

ʰc̲olumn ← { (i, k, side, far) →
  h ← HIGH m̲in (0.9 × HIGH) ÷ far
  w ← (3 s̲elect VIEW) ÷ f̲loat RAYS
  dim ← (1.0 − 0.3 × f̲loat side) × 0.25 m̲ax 1.0 − far ÷ 14.0
  rgb ← f̲loor dim × (1 m̲ax k) s̲elect COLORS
  e̲nclose @ f̲ormat< "<rect x=\"{sv:w_hole w * f_loat i - 1}\" y=\"{sv:w_hole (TOP + 0.5 * HIGH) - 0.5 * h}\" width=\"{sv:w_hole w + 0.6}\" height=\"{sv:w_hole h}\" fill=\"rgb({f_irst rgb},{2 s_elect rgb},{3 s_elect rgb})\"/>"
}
f̲ormat< expands to
("<rect x=\"" c̲at (f̲ormat (ˢᵛw̲hole w × f̲loat i − 1)) c̲at "\" y=\"" c̲at (f̲ormat (ˢᵛw̲hole (TOP + 0.5 × HIGH) − 0.5 × h)) c̲at "\" width=\"" c̲at (f̲ormat (ˢᵛw̲hole w + 0.6)) c̲at "\" height=\"" c̲at (f̲ormat (ˢᵛw̲hole h)) c̲at "\" fill=\"rgb(" c̲at (f̲ormat (f̲irst rgb)) c̲at "," c̲at (f̲ormat (2 s̲elect rgb)) c̲at "," c̲at (f̲ormat (3 s̲elect rgb)) c̲at ")\"/>")
Used in: ˡd̲raw

MAPX : Float

value (private) · line 154

The mini map, drawn once: the walls, 8 picture units a tile.

MAPX ← 788.0 − 8.0 × f̲loat C

MAPY : Float

value (private) · line 155
MAPY ← 72.0

ʰm̲apTile : Int -> Box Char

function (private) · line 156
ʰm̲apTile ← { i →
  k ← i s̲elect GRID
  e̲nclose @ f̲ormat< "<rect x=\"{sv:w_hole MAPX + 8.0 * f_loat (i - 1) m_od C}\" y=\"{sv:w_hole MAPY + 8.0 * f_loat (i - 1) d_iv C}\" width=\"8\" height=\"8\" fill=\"{h:m_apFill k}\"/>"
}
f̲ormat< expands to
("<rect x=\"" c̲at (f̲ormat (ˢᵛw̲hole MAPX + 8.0 × f̲loat (i − 1) m̲od C)) c̲at "\" y=\"" c̲at (f̲ormat (ˢᵛw̲hole MAPY + 8.0 × f̲loat (i − 1) d̲iv C)) c̲at "\" width=\"8\" height=\"8\" fill=\"" c̲at (f̲ormat (ʰm̲apFill k)) c̲at "\"/>")
Used in: MAP

ʰm̲apFill : Int -> Char

function (private) · line 160
ʰm̲apFill ← { k → k = 5 ? "#2a9d8f"◆ k > 0 ? "#c8c8c8"◆ "#20222e" }
Used in: ʰm̲apTile

MAP : Char

value (private) · line 161
MAP ← ᵗj̲oin 'ʰm̲apTile e̲ach r̲ange t̲ally cells
Used in: ˡd̲raw

ʰy̲ouOnMap : Num a => (Float, Float, a) -> Box Char

function (private) · line 163

You on the mini map: a dot and the way you face.

ʰy̲ouOnMap ← { (x, y, a) →
  px ← MAPX + 8.0 × x
  py ← MAPY + 8.0 × y
  e̲nclose @ f̲ormat< "<g class=\"you\"><circle cx=\"{sv:w_hole px}\" cy=\"{sv:w_hole py}\" r=\"3\" fill=\"#e63946\"/><line x1=\"{sv:w_hole px}\" y1=\"{sv:w_hole py}\" x2=\"{sv:w_hole px + 9.0 * c_os a}\" y2=\"{sv:w_hole py + 9.0 * s_in a}\" stroke=\"#e63946\" stroke-width=\"2\"/></g>"
}
f̲ormat< expands to
("<g class=\"you\"><circle cx=\"" c̲at (f̲ormat (ˢᵛw̲hole px)) c̲at "\" cy=\"" c̲at (f̲ormat (ˢᵛw̲hole py)) c̲at "\" r=\"3\" fill=\"#e63946\"/><line x1=\"" c̲at (f̲ormat (ˢᵛw̲hole px)) c̲at "\" y1=\"" c̲at (f̲ormat (ˢᵛw̲hole py)) c̲at "\" x2=\"" c̲at (f̲ormat (ˢᵛw̲hole px + 9.0 × c̲os a)) c̲at "\" y2=\"" c̲at (f̲ormat (ˢᵛw̲hole py + 9.0 × s̲in a)) c̲at "\" stroke=\"#e63946\" stroke-width=\"2\"/></g>")
Used in: ˡd̲raw

ʰw̲ords : (Float, Float, Float) -> a -> Box Char

function (private) · line 169

Words on the dark picture.

ʰw̲ords ← { (x, y, z) t → e̲nclose @ f̲ormat< "<text x=\"{sv:w_hole x}\" y=\"{sv:w_hole y}\" font-size=\"{sv:w_hole z}\" fill=\"#e9ecef\">{t}</text>" }
f̲ormat< expands to
("<text x=\"" c̲at (f̲ormat (ˢᵛw̲hole x)) c̲at "\" y=\"" c̲at (f̲ormat (ˢᵛw̲hole y)) c̲at "\" font-size=\"" c̲at (f̲ormat (ˢᵛw̲hole z)) c̲at "\" fill=\"#e9ecef\">" c̲at (f̲ormat (t)) c̲at "</text>")
Used in: ˡd̲raw

ʰs̲ays : (Num a, Num b) => (a, b) -> Char

function (private) · line 170
ʰs̲ays ← { (t, st) →
  st = 1 ? @ f̲ormat< "You found the way out in {f_loor t} seconds!"
  @ f̲ormat< "Find the way out (the teal door) | Time {f_loor t} s | Arrows: walk, turn; a, d: sideways"
}
f̲ormat< expands to
("You found the way out in " c̲at (f̲ormat (f̲loor t)) c̲at " seconds!")
f̲ormat< expands to
"Find the way out (the teal door) | Time " c̲at (f̲ormat (f̲loor t)) c̲at " s | Arrows: walk, turn; a, d: sideways"
Used in: ˡd̲raw

ˡd̲raw : (Num a, Num b, Num c) => (Float, Float, a, b, c) -> Char

function · line 177

The picture, as SVG text: the ceiling and the floor, a column for every ray, the mini map with you on it, and the top line.

ˡd̲raw ← { (x, y, a, t, st) →
  (k, side, far) ← ʰc̲ast (x, y, a)
  cols ← '{ i → ʰc̲olumn (i, i s̲elect k, i s̲elect side, i s̲elect far) } e̲ach r̲ange RAYS
  back ← e̲nclose @ f̲ormat< "<rect y=\"{sv:w_hole TOP}\" width=\"800\" height=\"{sv:w_hole 0.5 * HIGH}\" fill=\"#2b2d42\"/><rect y=\"{sv:w_hole TOP + 0.5 * HIGH}\" width=\"800\" height=\"{sv:w_hole 0.5 * HIGH}\" fill=\"#4a4e45\"/>"
  top ← (16.0, 40.0, 20.0) ʰw̲ords ʰs̲ays (t, st)
  VIEW ˢᵛp̲icture (VIEW ˢᵛr̲ect "fill=\"#111111\"") c̲at back c̲at cols c̲at (e̲nclose MAP) c̲at (ʰy̲ouOnMap (x, y, a)) c̲at top
}
f̲ormat< expands to
("<rect y=\"" c̲at (f̲ormat (ˢᵛw̲hole TOP)) c̲at "\" width=\"800\" height=\"" c̲at (f̲ormat (ˢᵛw̲hole 0.5 × HIGH)) c̲at "\" fill=\"#2b2d42\"/><rect y=\"" c̲at (f̲ormat (ˢᵛw̲hole TOP + 0.5 × HIGH)) c̲at "\" width=\"800\" height=\"" c̲at (f̲ormat (ˢᵛw̲hole 0.5 × HIGH)) c̲at "\" fill=\"#4a4e45\"/>")
Used in: ᵘs̲how, _