sourcegames/raycaster/Rays.xtl
1⍝│ The raycaster's rules, a library: raycaster.xtl (scripted), play.xtl
2⍝│ (the game: keys and the clock) and the web page all use these. The map
3⍝│ is a grid of walls; you stand anywhere in it, facing any way. The view
4⍝│ is one ray per column of the picture, every ray stepped through the
5⍝│ grid at once (each to its next line across or down, whichever is
6⍝│ nearer) until all of them have hit a wall: a fixed point. A wall's
7⍝│ column is as tall as it is near. A game's state is a tuple: (where you
8⍝│ are, x and y in tiles, y down; which way you face, radians, 0 east,
9⍝│ turning right as it grows; the seconds played; how it stands: 0
10⍝│ walking, 1 out of the door).
11
12"sv:" u̲se< "Svg"
13"t:" u̲se< "Text"
14
15⍝━━ The data
16
17⍝│ The map: R rows of C tiles, row by row: 0 floor, 1 to 4 a wall's
18⍝│ color, 5 the door.
19rows ← "raycaster.toml" ⎕L̲IST "map"
20cells ← ᵗj̲oin rows
21R ← t̲ally rows
22C ← (t̲ally cells) d̲iv R
23GRID ← (".1234E@" i̲ndexOf cells) s̲elect 0 1 2 3 4 5 0
24⍝│ The colors: a wall's by its kind, the door's.
25COLORS ← 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"
26⍝│ A step, a turn, the view's width, the rays.
27moves ← 'ˢᵛn̲um e̲ach "raycaster.toml" ⎕L̲IST "moves"
28STEP ← f̲irst moves
29TURN ← 2 s̲elect moves
30WIDE ← 3 s̲elect moves
31RAYS ← f̲loor 4 s̲elect moves
32⍝│ Where you start: the middle of the @ tile, facing east.
33AT ← f̲irst w̲here cells = f̲irst "@"
34
35⍝━━ Walking
36
37⍝ :: (Any a, Num b) => a -> (Float, Float, Float, Float, b)
38⍝│ A new game: you at the start, facing east.
39ˡ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) }
40⍝│ What is on the tiles at x, y (any number of them): 0 floor, 1 to 4 a
41⍝│ wall, 5 the door; beyond the map, a wall.
42ʰt̲ile ← { (x, y) →
43 c ← f̲loor x
44 r ← f̲loor y
45 out ← (c < 0) ∨ (r < 0) ∨ (c ≥ C) ∨ r ≥ R
46 k ← 1 + (C × 0 m̲ax r m̲in R − 1) + 0 m̲ax c m̲in C − 1
47 (k s̲elect GRID) m̲ax 3 × 0 + out
48}
49⍝│ Whether you fit at x, y: no wall within 0.2 tiles, the four corners
50⍝│ of your square tested at once.
51ʰ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) }
52⍝│ Whether a step to x, y reaches the door.
53ʰ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) }
54⍝│ You moved by (dx, dy), if you fit there; out of the door if it is
55⍝│ there.
56ʰm̲oved ← { (x, y, a, t, st, dx, dy) →
57 ʰd̲oor (x + dx, y + dy) ? (x, y, a, t, 1)
58 ʰf̲its (x + dx, y + dy) ? (x + dx, y + dy, a, t, st)
59 (x, y, a, t, st)
60}
61⍝ :: (Any a, Num b) => (Float, Float, Float, a, b) -> Char -> (Float, Float, Float, a, b)
62⍝│ A key: up (or w) steps ahead, down (or s) steps back, left and right
63⍝│ turn, a and d step sideways; other keys, and keys once out, do
64⍝│ nothing.
65ˡk̲ey ← { s name →
66 (x, y, a, t, st) ← s
67 k ← ("UP" "DOWN" "LEFT" "RIGHT" "w" "s" "a" "d") i̲ndexOf e̲nclose name
68 (k > 8) ∨ st > 0 ? s
69 k = 3 ? (x, y, a − TURN, t, st)
70 k = 4 ? (x, y, a + TURN, t, st)
71 f ← (1 2 5 6 7 8 i̲ndexOf k) s̲elect 1.0 -1.0 1.0 -1.0 0.0 0.0
72 g ← (1 2 5 6 7 8 i̲ndexOf k) s̲elect 0.0 0.0 0.0 0.0 -1.0 1.0
73 ʰ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)
74}
75⍝ :: (Any a, Any b, Any c, Num d, Num e) => (a, b, c, d, e) -> d -> (a, b, c, d, e)
76⍝│ A tick of the clock: the time moves (once out, it stops).
77ˡt̲ick ← { s dt →
78 (x, y, a, t, st) ← s
79 st > 0 ? s
80 (x, y, a, t + dt, st)
81}
82
83⍝━━ Looking
84
85⍝│ The rays: one for each column, from the left of the view to the
86⍝│ right, each a direction (x and y).
87ʰr̲ays ← { a →
88 cam ← (2.0 × (f̲loat (r̲ange RAYS) − 1) ÷ f̲loat RAYS − 1) − 1.0
89 ((c̲os a) − WIDE × cam × s̲in a, (s̲in a) + WIDE × cam × c̲os a)
90}
91⍝│ Every ray one line further (across where its next line across is
92⍝│ nearer, else down), all at once, until all have hit a wall (or forty
93⍝│ lines, the map's diagonal and more).
94ʰs̲tep ← { (mx, my, sx, sy, dx, dy, ix, iy, side, hit, n) →
95 ('∧ r̲/ hit) ∨ n ≥ 40 ? (mx, my, sx, sy, dx, dy, ix, iy, side, hit, n)
96 across ← (n̲ot hit) ∧ sx < sy
97 down ← (n̲ot hit) ∧ n̲ot across
98 fa ← f̲loat across
99 fd ← f̲loat down
100 tx ← mx + ix × 0 + across
101 ty ← my + iy × 0 + down
102 struck ← (side × 0 + n̲ot across ∨ down) + 0 + down
103 wall ← 0 < ʰt̲ile (0.5 + f̲loat tx, 0.5 + f̲loat ty)
104 ʰs̲tep (tx, ty, sx + fa × dx, sy + fd × dy, dx, dy, ix, iy, struck, wall, n + 1)
105}
106⍝│ What every ray sees from x, y facing a: the wall it hits (its kind),
107⍝│ which way it was struck (0 across, 1 down) and how far it is (along
108⍝│ the way you face, so walls stand straight).
109ʰc̲ast ← { (x, y, a) →
110 (rx, ry) ← ʰr̲ays a
111 dx ← 1.0 ÷ 0.000001 m̲ax a̲bs rx
112 dy ← 1.0 ÷ 0.000001 m̲ax a̲bs ry
113 mx0 ← RAYS r̲eshape f̲loor x
114 my0 ← RAYS r̲eshape f̲loor y
115 ix ← (0 + rx > 0.0) − 0 + rx < 0.0
116 iy ← (0 + ry > 0.0) − 0 + ry < 0.0
117 fx ← x − f̲loat f̲loor x
118 fy ← y − f̲loat f̲loor y
119 sx0 ← dx × (fx × f̲loat rx < 0.0) + (1.0 − fx) × f̲loat rx ≥ 0.0
120 sy0 ← dy × (fy × f̲loat ry < 0.0) + (1.0 − fy) × f̲loat ry ≥ 0.0
121 (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)
122 far ← ((sx − dx) × f̲loat side = 0) + (sy − dy) × f̲loat side = 1
123 (ʰt̲ile (0.5 + f̲loat mx, 0.5 + f̲loat my), side, 0.05 m̲ax far)
124}
125
126⍝━━ What it shows
127
128⍝ :: (Any a, Any b, Any c, Any d, Any e) => (a, b, c, d, e) -> e
129⍝│ 0 walking, 1 out of the door.
130ˡs̲tatus ← { (x, y, a, t, st) → st }
131⍝ :: Any b => (a, a, a, a, b) -> a
132⍝│ Where you are, which way you face, the seconds.
133ˡy̲ou ← { (x, y, a, t, st) → x c̲at y c̲at a c̲at t }
134⍝ :: (Num a, Any b, Any c) => (Float, Float, a, b, c) -> Float
135⍝│ What the rays see, a row each: the wall's kind, its side, how far.
136ˡs̲ees ← { (x, y, a, t, st) →
137 (k, side, far) ← ʰc̲ast (x, y, a)
138 o̲\ (3 c̲at RAYS) r̲eshape (f̲loat k) c̲at (f̲loat side) c̲at far
139}
140⍝│ The picture's size; the view below the top line.
141VIEW ← 0.0 0.0 800.0 560.0
142TOP ← 60.0
143HIGH ← 500.0
144⍝│ One column of the view: its place, its wall's height and color
145⍝│ (darker for a side struck down, and with the distance).
146ʰc̲olumn ← { (i, k, side, far) →
147 h ← HIGH m̲in (0.9 × HIGH) ÷ far
148 w ← (3 s̲elect VIEW) ÷ f̲loat RAYS
149 dim ← (1.0 − 0.3 × f̲loat side) × 0.25 m̲ax 1.0 − far ÷ 14.0
150 rgb ← f̲loor dim × (1 m̲ax k) s̲elect COLORS
151 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})\"/>"
152}
153⍝│ The mini map, drawn once: the walls, 8 picture units a tile.
154MAPX ← 788.0 − 8.0 × f̲loat C
155MAPY ← 72.0
156ʰm̲apTile ← { i →
157 k ← i s̲elect GRID
158 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}\"/>"
159}
160ʰm̲apFill ← { k → k = 5 ? "#2a9d8f"◆ k > 0 ? "#c8c8c8"◆ "#20222e" }
161MAP ← ᵗj̲oin 'ʰm̲apTile e̲ach r̲ange t̲ally cells
162⍝│ You on the mini map: a dot and the way you face.
163ʰy̲ouOnMap ← { (x, y, a) →
164 px ← MAPX + 8.0 × x
165 py ← MAPY + 8.0 × y
166 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>"
167}
168⍝│ Words on the dark picture.
169ʰ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>" }
170ʰs̲ays ← { (t, st) →
171 st = 1 ? @ f̲ormat< "You found the way out in {f_loor t} seconds!"
172 @ f̲ormat< "Find the way out (the teal door) | Time {f_loor t} s | Arrows: walk, turn; a, d: sideways"
173}
174⍝ :: (Num a, Num b, Num c) => (Float, Float, a, b, c) -> Char
175⍝│ The picture, as SVG text: the ceiling and the floor, a column for
176⍝│ every ray, the mini map with you on it, and the top line.
177ˡd̲raw ← { (x, y, a, t, st) →
178 (k, side, far) ← ʰc̲ast (x, y, a)
179 cols ← '{ i → ʰc̲olumn (i, i s̲elect k, i s̲elect side, i s̲elect far) } e̲ach r̲ange RAYS
180 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\"/>"
181 top ← (16.0, 40.0, 20.0) ʰw̲ords ʰs̲ays (t, st)
182 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
183}
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 ")\"/>")
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 "\"/>")
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>")
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>")
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"
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\"/>")