sourceextensions/scene/demos/Voxels.xtl
1⍝│ Voxels.xtl -- voxel chunks as arrays, shared by the voxels-* demos
2⍝│ (docs/voxels.md). A chunk is a 16 by 16 by 16 array of block numbers,
3⍝│ indexed x (east), y (up), z (south); 0 is air.
4⍝│ Import: "vx:" u_se< "Voxels"
5⍝│ (Not voxels.xtl: on a case-insensitive file system a demo of that name
6⍝│ would be this file -- ask E8.)
7
8⍝━━ Chunks
9
10⍝│ the side of a chunk
11ˡn ← 16
12⍝│ the cells of a chunk, 16 cubed
13ˡcells ← 4096
14
15⍝│ block 0: air
16ˡair ← 0
17⍝│ block 1: stone, below the dirt
18ˡstone ← 1
19⍝│ block 2: dirt, three below the grass
20ˡdirt ← 2
21⍝│ block 3: grass, on top
22ˡgrass ← 3
23⍝│ block 4: sand, where grass would be at or below the water
24ˡsand ← 4
25⍝│ block 5: water, filling air below its level (not solid)
26ˡwater ← 5
27⍝│ block 6: wood, a tree's trunk
28ˡwood ← 6
29⍝│ block 7: leaves, a tree's crown
30ˡleaves ← 7
31⍝│ block 8: a lamp, solid, that glows: light spreads from it (placed,
32⍝│ never made by the terrain)
33ˡlamp ← 8
34⍝│ block 9: a gem, solid, that glows a little (12 where a lamp is 15):
35⍝│ buried by the game (voxels-game), never made by the terrain
36ˡgem ← 9
37⍝│ the blocks' names, in their order
38⍝│ >> "vx:" u_se< "Voxels"
39⍝│ >> vx:names
40⍝│ air stone dirt grass sand water wood leaves lamp gem
41ˡnames ← "air stone dirt grass sand water wood leaves lamp gem"
42⍝│ a character for each block when a chunk is printed, air first:
43⍝│ stone #, dirt %, grass ", sand :, water ~, wood |, leaves *, lamp o,
44⍝│ gem $
45ˡglyphs ← " #%\":~|*o$"
46
47i ← (r̲ange 4096) − 1
48⍝│ every cell's x (east), as a vector in the chunk's order (x slowest)
49⍝│ >> "vx:" u_se< "Voxels"
50⍝│ >> 18 t_ake vx:xs
51⍝│ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
52ˡxs ← i d̲iv 256
53⍝│ every cell's y (up), in the chunk's order
54ˡys ← (i d̲iv 16) m̲od 16
55⍝│ every cell's z (south), in the chunk's order (z fastest)
56⍝│ >> "vx:" u_se< "Voxels"
57⍝│ >> 18 t_ake vx:zs
58⍝│ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 1
59ˡzs ← i m̲od 16
60
61⍝│ vx:h_ills phase: a 16 by 16 height field (x by z), rolling hills of
62⍝│ heights 3 to 12; the phase shifts them
63ˡh̲ills ← { p →
64 x ← f̲loat (r̲ange 16) − 1
65 a ← 2.5 × s̲in p + x ÷ 2.5
66 b ← 2.0 × c̲os p + x ÷ 3.0
67 f̲loor 7.5 + a '+ t̲able b
68}
69
70⍝│ vx:c_hunk h: the chunk on a height field h (x by z): grass on top,
71⍝│ three of dirt, stone below; water fills air below 7, and sand
72⍝│ replaces grass at or below it; one tree on the hill at (4, 4) when it
73⍝│ stands above the water
74ˡc̲hunk ← { h →
75 top ← (1 + ˡzs + 16 × ˡxs) s̲elect r̲avel h
76 d ← top − ˡys
77 b ← (d = 1) × 3
78 soil ← b + (d > 1) × (d < 5) × 2
79 ground ← soil + (d ≥ 5) × 1
80 shore ← ground + (ground = 3) × (ˡys ≤ 7) × 1
81 sea ← shore + (shore = 0) × (ˡys < 7) × 5
82 ⍝ the tree: a trunk of 3 at (4, 4), leaves around its top
83 t ← (1 + 4 + 16 × 4) s̲elect r̲avel h
84 up ← t > 7
85 ax ← a̲bs ˡxs − 4
86 az ← a̲bs ˡzs − 4
87 trunk ← up × (ax = 0) × (az = 0) × (ˡys ≥ t) × ˡys < t + 3
88 crown ← up × (ax ≤ 1) × (az ≤ 1) × (ˡys ≥ t + 2) × (ˡys ≤ t + 3) × 1 − trunk
89 trunked ← sea + trunk × 6 − sea
90 grown ← trunked + crown × (trunked = 0) × 7
91 16 16 16 r̲eshape grown
92}
93
94⍝│ vx:c_ounts b: how many of each block, air first
95ˡc̲ounts ← { b → '+ r̲/₂ ((r̲ange 8) − 1) '= t̲able r̲avel b }
96
97⍝│ x vx:s_ide b: the slice at x, as characters, up at the top
98ˡs̲ide ← { x b → r̲ev (1 + (x + 1) s̲elect b) s̲elect ˡglyphs }
99
100⍝│ a column value (x by z) at every cell of its column
101ˡc̲olumn ← { m → 16 16 16 r̲eshape (1 + ˡzs + 16 × ˡxs) s̲elect r̲avel m }
102
103⍝│ vx:t_op b: what is seen from above: the highest non-air block of each
104⍝│ column (x by z) -- the top's height by a max-reduction down each
105⍝│ column, then the block there picked by comparing heights
106ˡt̲op ← { b →
107 y1 ← 16 16 16 r̲eshape ˡys + 1
108 hi ← 'm̲ax r̲/₂ y1 × b > 0
109 '+ r̲/₂ b × y1 = ˡc̲olumn hi
110}
111
112⍝━━ Faces
113
114⍝│ the cells' coordinates as 16 by 16 by 16 arrays
115X ← 16 16 16 r̲eshape ˡxs
116Y ← 16 16 16 r̲eshape ˡys
117Z ← 16 16 16 r̲eshape ˡzs
118
119⍝│ a solid block is any but air and water
120ˡs̲olid ← { b → (b > 0) × b ≠ 5 }
121
122⍝│ the exposed faces in the six directions -- west, east, down, up,
123⍝│ north, south: numbered 0 to 5 -- as a 6 by 4096 Boolean array: a
124⍝│ solid cell whose neighbor that way is not solid. The neighbor is a
125⍝│ rotation along the axis (the six rotations of the Life idiom) with
126⍝│ the plane that wrapped around made air: beyond the border is air.
127ˡe̲xposed ← { b →
128 s ← ˡs̲olid b
129 w ← s > (-1 o̲-₁ s) × X > 0
130 e ← s > (1 o̲-₁ s) × X < 15
131 d ← s > (-1 o̲-₂ s) × Y > 0
132 u ← s > (1 o̲-₂ s) × Y < 15
133 n ← s > (-1 o̲-₃ s) × Z > 0
134 so ← s > (1 o̲-₃ s) × Z < 15
135 6 4096 r̲eshape (r̲avel w) c̲at (r̲avel e) c̲at (r̲avel d) c̲at (r̲avel u) c̲at (r̲avel n) c̲at r̲avel so
136}
137
138⍝│ water's surface: water with air above it, facing up
139ˡs̲urface ← { b → (b = 5) > (1 o̲-₂ b > 0) × Y < 15 }
140
141⍝│ d r_ows mb: the faces of one direction as 5 rows -- x, y, z of the
142⍝│ cell, the direction, the block -- from mb, the mask times 1 + the
143⍝│ block (so one array carries both)
144ʰr̲ows ← { d mb →
145 k ← w̲here mb > 0
146 (5 c̲at t̲ally k) r̲eshape (k s̲elect ˡxs) c̲at (k s̲elect ˡys) c̲at (k s̲elect ˡzs) c̲at ((t̲ally k) r̲eshape d) c̲at (k s̲elect mb) − 1
147}
148
149⍝│ vx:f_aces b: every face to draw, one row each -- x y z direction
150⍝│ block -- the six directions' exposed faces, then water's surface
151ˡf̲aces ← { b →
152 m ← ˡe̲xposed b
153 bb ← 1 + r̲avel b
154 r ← (((0 ʰr̲ows bb × 1 s̲elect m) c̲at₂ (1 ʰr̲ows bb × 2 s̲elect m) c̲at₂ (2 ʰr̲ows bb × 3 s̲elect m)) c̲at₂
155 (3 ʰr̲ows bb × 4 s̲elect m) c̲at₂ (4 ʰr̲ows bb × 5 s̲elect m) c̲at₂ (5 ʰr̲ows bb × 6 s̲elect m))
156 2 1 t̲ranspose r c̲at₂ 3 ʰr̲ows bb × r̲avel ˡs̲urface b
157}
158
159⍝│ vx:c_ount f: how many faces in each direction (west east down up
160⍝│ north south); a single block shows one face each way:
161⍝│ >> "vx:" u_se< "Voxels"
162⍝│ >> one := 16 16 16 r_eshape (vx:xs = 8) * (vx:ys = 8) * vx:zs = 8
163⍝│ >> vx:c_ount vx:f_aces one
164⍝│ 1 1 1 1 1 1
165ˡc̲ount ← { f → '+ r̲/₂ (r̲ange 6) '= t̲able 1 + 4 s̲elect₂ f }
166
167⍝━━ Drawing
168
169⍝│ the corners of each direction's face, from its cell's corner, taken
170⍝│ in pairs round the square (1 2, 2 3, 3 4, 4 1): x, y, z, 6 by 8 each
171ox ← 6 8 r̲eshape (8 r̲eshape 0) c̲at (8 r̲eshape 1) c̲at (0 1 1 1 1 0 0 0) c̲at (0 1 1 1 1 0 0 0) c̲at (0 1 1 1 1 0 0 0) c̲at 0 1 1 1 1 0 0 0
172oy ← 6 8 r̲eshape (0 1 1 1 1 0 0 0) c̲at (0 1 1 1 1 0 0 0) c̲at (8 r̲eshape 0) c̲at (8 r̲eshape 1) c̲at (0 0 0 1 1 1 1 0) c̲at 0 0 0 1 1 1 1 0
173oz ← 6 8 r̲eshape (0 0 0 1 1 1 1 0) c̲at (0 0 0 1 1 1 1 0) c̲at (0 0 0 1 1 1 1 0) c̲at (0 0 0 1 1 1 1 0) c̲at (8 r̲eshape 0) c̲at 8 r̲eshape 1
174
175⍝│ vx:e_dges f: the faces' outlines as segments for scene: 8 points a
176⍝│ face (4 edges, 2 points each), n*8 by 3, the chunk centered on the
177⍝│ origin and scaled to -1..1
178ˡe̲dges ← { f →
179 d ← 1 + 4 s̲elect₂ f
180 z8 ← 8 r̲eshape 0
181 px ← (d s̲elect ox) + (1 s̲elect₂ f) '+ t̲able z8
182 py ← (d s̲elect oy) + (2 s̲elect₂ f) '+ t̲able z8
183 p ← 2 1 t̲ranspose (3 c̲at 8 × t̲ally f) r̲eshape (r̲avel px) c̲at (r̲avel py) c̲at r̲avel (d s̲elect oz) + (3 s̲elect₂ f) '+ t̲able z8
184 ((f̲loat p) − 8.0) ÷ 8.0
185}
186
187⍝│ each block's color, red green blue (air first)
188ˡcolors ← 10 3 r̲eshape 0 0 0 0.55 0.55 0.58 0.55 0.38 0.22 0.35 0.7 0.25 0.9 0.82 0.55 0.25 0.45 0.9 0.45 0.3 0.15 0.2 0.5 0.2 1.0 0.88 0.45 0.25 0.95 0.95
189
190⍝ the corners of each direction's face in order round it: the
191⍝ outlines' corners 1 3 5 7
192cx ← 1 3 5 7 s̲elect₂ ox
193cy ← 1 3 5 7 s̲elect₂ oy
194cz ← 1 3 5 7 s̲elect₂ oz
195⍝│ vx:c_orners f: each face's four corners in order round it, n*4 by 3,
196⍝│ in blocks -- the quads scene draws for the faces
197ˡc̲orners ← { f →
198 d ← 1 + 4 s̲elect₂ f
199 z4 ← 4 r̲eshape 0
200 px ← (d s̲elect cx) + (1 s̲elect₂ f) '+ t̲able z4
201 py ← (d s̲elect cy) + (2 s̲elect₂ f) '+ t̲able z4
202 pz ← (d s̲elect cz) + (3 s̲elect₂ f) '+ t̲able z4
203 f̲loat 2 1 t̲ranspose (3 c̲at 4 × t̲ally f) r̲eshape (r̲avel px) c̲at (r̲avel py) c̲at r̲avel pz
204}
205
206⍝│ vx:q_uads f: the faces as filled quads for scene, a chunk centered
207⍝│ on the origin and scaled to -1..1, like vx:e_dges
208ˡq̲uads ← { f → ((ˡc̲orners f) − 8.0) ÷ 8.0 }
209
210⍝━━ Worlds: many chunks
211
212⍝ A world is one array, x by 32 by z, made of 16-cubes; its exposed
213⍝ faces are found over the whole of it at once (so faces between
214⍝ chunks hide each other), then grouped by chunk for scene's ids.
215
216⍝│ value noise, made by linear algebra: random heights on a grid every s
217⍝│ blocks, interpolated to n by n -- M G M', M the n by k hat-function
218⍝│ weights of each grid line
219ʰw̲eights ← { s n →
220 k ← 1 + n d̲iv s
221 t ← (f̲loat (r̲ange n) − 1) ÷ f̲loat s
222 0.0 m̲ax 1.0 − a̲bs t '− t̲able f̲loat (r̲ange k) − 1
223}
224ʰn̲oise ← { s n →
225 m ← s ʰw̲eights n
226 k ← 1 + n d̲iv s
227 g ← (f̲loat r̲oll! (k × k) r̲eshape 1000) ÷ 1000.0
228 m '+ '× i̲nner ((k c̲at k) r̲eshape g) '+ '× i̲nner 2 1 t̲ranspose m
229}
230
231⍝│ vx:h_eights n: an n by n height field (x by z), three octaves of
232⍝│ value noise -- every 16, 8 and 4 blocks -- from 4 to 29 (seeded)
233ˡh̲eights ← { n →
234 v ← (16.0 × 16 ʰn̲oise n) + (7.0 × 8 ʰn̲oise n) + 3.0 × 4 ʰn̲oise n
235 4 m̲ax 29 m̲in f̲loor 4.0 + v
236}
237
238⍝│ a world's cells' coordinates, for a shape nx ny nz (x slowest)
239ʰc̲oords ← { sh →
240 j ← (r̲ange '× r̲/ sh) − 1
241 yz ← (2 s̲elect sh) × 3 s̲elect sh
242 (3 c̲at t̲ally j) r̲eshape (j d̲iv yz) c̲at ((j d̲iv 3 s̲elect sh) m̲od 2 s̲elect sh) c̲at j m̲od 3 s̲elect sh
243}
244
245⍝│ vx:t_errain h: the world on a height field h (nx by nz), 32 tall:
246⍝│ grass, dirt, stone, water filling air below 14, sand at the shore --
247⍝│ the chunk's rules, over the whole world
248ˡt̲errain ← { h →
249 nx ← 1 s̲elect s̲hape h
250 nz ← 2 s̲elect s̲hape h
251 c ← ʰc̲oords nx c̲at 32 c̲at nz
252 xs ← 1 s̲elect c
253 ys ← 2 s̲elect c
254 zs ← 3 s̲elect c
255 d ← ((1 + zs + nz × xs) s̲elect r̲avel h) − ys
256 b ← (d = 1) × 3
257 soil ← b + (d > 1) × (d < 5) × 2
258 ground ← soil + (d ≥ 5) × 1
259 shore ← ground + (ground = 3) × (ys ≤ 14) × 1
260 sea ← shore + (shore = 0) × (ys < 14) × 5
261 (nx c̲at 32 c̲at nz) r̲eshape sea
262}
263
264⍝│ t vx:t_ree b: a tree at column t (x z) when it stands on grass: a
265⍝│ trunk of 4 and a crown of leaves, placed into b -- only the tree's
266⍝│ own cells are worked out (their indexes into b), then set by a
267⍝│ membership mask, so a tree costs little in a big block
268ˡt̲ree ← { t b →
269 sh ← s̲hape b
270 ny ← 2 s̲elect sh
271 nz ← 3 s̲elect sh
272 tx ← 1 s̲elect t
273 tz ← 2 s̲elect t
274 v ← r̲avel b
275 col ← (1 + tz + nz × ((r̲ange ny) − 1) + ny × tx) s̲elect v
276 0 = '+ r̲/ col = 3 ? b
277 y0 ← 1 + '+ r̲/ (col = 3) × (r̲ange ny) − 1
278 ty ← y0 + (r̲ange 4) − 1
279 tyIn ← (ty < ny) r̲eplicate ty
280 trunk ← 1 + tz + nz × tyIn + ny × tx
281 d5 ← (r̲ange 5) − 3
282 dx ← r̲avel d5 '{ ⍺ + 0 × ⍵ } t̲able d5
283 dz ← r̲avel d5 '{ ⍵ + 0 × ⍺ } t̲able d5
284 wide ← ((a̲bs dx) + a̲bs dz) < 4
285 small ← ((a̲bs dx) ≤ 1) × (a̲bs dz) ≤ 1
286 u̲p ← { y m →
287 y ≥ ny ? 0 r̲eshape 0
288 1 + (tz + m r̲eplicate dz) + nz × y + ny × tx + m r̲eplicate dx
289 }
290 crown ← ((y0 + 3) u̲p wide) c̲at ((y0 + 4) u̲p wide) c̲at (y0 + 5) u̲p small
291 n ← t̲ally v
292 trunked ← v + ((r̲ange n) m̲ember? trunk) × 6 − v
293 grown ← trunked + ((r̲ange n) m̲ember? crown) × (trunked = 0) × 7
294 sh r̲eshape grown
295}
296
297⍝│ vx:w_orldFaces b: every face of a world, one row each -- x y z
298⍝│ direction block -- as vx:f_aces does for a chunk, the borders of the
299⍝│ world air
300ˡw̲orldFaces ← { b →
301 sh ← s̲hape b
302 c ← ʰc̲oords sh
303 s ← ˡs̲olid b
304 xs ← sh r̲eshape 1 s̲elect c
305 ys ← sh r̲eshape 2 s̲elect c
306 zs ← sh r̲eshape 3 s̲elect c
307 mx ← (1 s̲elect sh) − 1
308 my ← (2 s̲elect sh) − 1
309 mz ← (3 s̲elect sh) − 1
310 w ← s > (-1 o̲-₁ s) × xs > 0
311 e ← s > (1 o̲-₁ s) × xs < mx
312 d ← s > (-1 o̲-₂ s) × ys > 0
313 u ← s > (1 o̲-₂ s) × ys < my
314 n ← s > (-1 o̲-₃ s) × zs > 0
315 so ← s > (1 o̲-₃ s) × zs < mz
316 top ← (b = 5) > (1 o̲-₂ b > 0) × ys < my
317 bb ← 1 + r̲avel b
318 r̲w ← { d m →
319 k ← w̲here m > 0
320 (5 c̲at t̲ally k) r̲eshape (k s̲elect 1 s̲elect c) c̲at (k s̲elect 2 s̲elect c) c̲at (k s̲elect 3 s̲elect c) c̲at ((t̲ally k) r̲eshape d) c̲at (k s̲elect m) − 1
321 }
322 r ← (((0 r̲w bb × r̲avel w) c̲at₂ (1 r̲w bb × r̲avel e) c̲at₂ (2 r̲w bb × r̲avel d) c̲at₂ (3 r̲w bb × r̲avel u)) c̲at₂
323 (4 r̲w bb × r̲avel n) c̲at₂ (5 r̲w bb × r̲avel so) c̲at₂ 3 r̲w bb × r̲avel top)
324 2 1 t̲ranspose r
325}
326
327⍝│ vx:c_hunkOf f: the chunk of each face (1 up): x, z and y in 16s
328ˡc̲hunkOf ← { f → 1 + ((1 s̲elect₂ f) d̲iv 16) + (4 × (3 s̲elect₂ f) d̲iv 16) + 16 × (2 s̲elect₂ f) d̲iv 16 }
329
330⍝│ ts vx:f_orest b: trees at the columns ts (n by 2: x z), each where it
331⍝│ stands on grass -- vx:t_ree folded over them
332ˡf̲orest ← { ts b →
333 boxes ← ('{ ⍵ s̲elect ts } m̲ap r̲ange t̲ally ts) c̲at e̲nclose b
334 d̲isclose '{ e̲nclose (d̲isclose ⍺) ˡt̲ree d̲isclose ⍵ } r̲/ boxes
335}
336
337⍝━━ Endless worlds
338
339⍝│ a grid point's random value, 0 to 1, from its coordinates alone (i
340⍝│ by j, any integers): the same point always gets the same value, so
341⍝│ any part of an endless world can be made by itself and fits its
342⍝│ neighbors
343⍝│ >> "vx:" u_se< "Voxels"
344⍝│ >> (3 vx:h_ash 4) = 3 vx:h_ash 4
345⍝│ 1
346ˡh̲ash ← { i j →
347 v ← 43758.5453 × s̲in (12.9898 × f̲loat i) '+ t̲able 78.233 × f̲loat j
348 v − f̲loat f̲loor v
349}
350
351⍝ the smooth weight of grid line k for the coordinates t (in grid
352⍝ units): smoothstep of 1 - |t - k|, 0 beyond a grid step
353ʰw̲eight ← { t k →
354 d ← 0.0 m̲ax 1.0 − a̲bs t '− t̲able f̲loat k
355 d × d × 3.0 − 2.0 × d
356}
357
358⍝ s n_oiseOf c: value noise every s blocks over the n by n blocks from
359⍝ c (x0 z0): grid values hashed from their coordinates, interpolated as
360⍝ M G M'
361ʰn̲oiseOf ← { s c →
362 x0 ← 1 s̲elect c
363 z0 ← 2 s̲elect c
364 n ← 3 s̲elect c
365 tx ← (f̲loat x0 + (r̲ange n) − 1) ÷ f̲loat s
366 tz ← (f̲loat z0 + (r̲ange n) − 1) ÷ f̲loat s
367 gx ← (f̲loor 'm̲in r̲/ tx) + (r̲ange 2 + (f̲loor 'm̲ax r̲/ tx) − f̲loor 'm̲in r̲/ tx) − 1
368 gz ← (f̲loor 'm̲in r̲/ tz) + (r̲ange 2 + (f̲loor 'm̲ax r̲/ tz) − f̲loor 'm̲in r̲/ tz) − 1
369 (tx ʰw̲eight gx) '+ '× i̲nner (gx ˡh̲ash gz) '+ '× i̲nner 2 1 t̲ranspose tz ʰw̲eight gz
370}
371
372⍝│ vx:h_eightsAt x0 z0 n: the heights of the n by n columns from block
373⍝│ (x0, z0), x by z, of the endless world: three octaves of value noise
374⍝│ (every 32, 16 and 8 blocks), 3 to 29, the sea at 14 -- any square
375⍝│ anywhere, always the same
376ˡh̲eightsAt ← { c →
377 v ← (18.0 × 32 ʰn̲oiseOf c) + (8.0 × 16 ʰn̲oiseOf c) + 3.0 × 8 ʰn̲oiseOf c
378 3 m̲ax 29 m̲in f̲loor 2.0 + v
379}
380
381⍝ the cells of a 16 by 32 by 16 column inside its 18 by 32 by 18 block
382⍝ (a border of one from the neighbors), as indexes into the bigger one
383cell ← (r̲ange 8192) − 1
384inner ← 1 + (1 + cell m̲od 16) + 18 × ((cell d̲iv 16) m̲od 32) + 32 × 1 + cell d̲iv 512
385
386⍝│ vx:p_atch c: column c (cx cz) of the endless world, 16 by 32 by 16
387⍝│ blocks: made with a border of one block from its neighbors' heights,
388⍝│ so its faces at the border hide where a neighbor is solid; trees (two
389⍝│ places hashed from the column, kept inside it). A box of its faces
390⍝│ (world coordinates, one row each: x y z direction block) and a box of
391⍝│ its solid mask (16 by 32 by 16, flat, x slowest)
392ˡp̲atch ← { c →
393 x0 ← (16 × 1 s̲elect c) − 1
394 z0 ← (16 × 2 s̲elect c) − 1
395 b ← ˡt̲errain ˡh̲eightsAt x0 c̲at z0 c̲at 18
396 r ← (1 s̲elect c) ˡh̲ash 2 s̲elect c
397 q ← (2 s̲elect c) ˡh̲ash 1 s̲elect c
398 t ← 4 + f̲loor 10.0 × (r̲avel r) c̲at (r̲avel q) c̲at (r̲avel r × q) c̲at r̲avel 1.0 − r × q
399 wooded ← (2 2 r̲eshape t) ˡf̲orest b
400 f ← ˡw̲orldFaces wooded
401 keep ← ((1 s̲elect₂ f) ≥ 1) × ((1 s̲elect₂ f) ≤ 16) × ((3 s̲elect₂ f) ≥ 1) × (3 s̲elect₂ f) ≤ 16
402 kept ← keep r̲eplicate f
403 moved ← kept + ((t̲ally kept) c̲at 5) r̲eshape x0 c̲at 0 c̲at z0 c̲at 0 0
404 (e̲nclose moved) c̲at e̲nclose inner s̲elect r̲avel ˡs̲olid wooded
405}
406
407⍝━━ Columns, quickly
408
409⍝ the 18 by 32 by 18 block of a column with its border: its cells'
410⍝ coordinates, made once (flat, x slowest, and as arrays), and which
411⍝ cells are the column's own (x and z from 1 to 16)
412cc ← ʰc̲oords 18 32 18
413cx18 ← 1 s̲elect cc
414cy18 ← 2 s̲elect cc
415cz18 ← 3 s̲elect cc
416X18 ← 18 32 18 r̲eshape cx18
417Y18 ← 18 32 18 r̲eshape cy18
418Z18 ← 18 32 18 r̲eshape cz18
419own ← (cx18 ≥ 1) × (cx18 ≤ 16) × (cz18 ≥ 1) × cz18 ≤ 16
420
421⍝│ vx:c_olumnBlocks c: the blocks of column c (cx cz) of the endless
422⍝│ world with a border of one from its neighbors, 18 by 32 by 18: the
423⍝│ heights, the terrain's rules, two trees placed by hash -- the first
424⍝│ of the three parts of making a column (about 12 ms)
425ˡc̲olumnBlocks ← { c →
426 x0 ← (16 × 1 s̲elect c) − 1
427 z0 ← (16 × 2 s̲elect c) − 1
428 h ← ˡh̲eightsAt x0 c̲at z0 c̲at 18
429 d ← ((1 + cz18 + 18 × cx18) s̲elect r̲avel h) − cy18
430 b ← (d = 1) × 3
431 soil ← b + (d > 1) × (d < 5) × 2
432 ground ← soil + (d ≥ 5) × 1
433 shore ← ground + (ground = 3) × (cy18 ≤ 14) × 1
434 blocks ← 18 32 18 r̲eshape shore + (shore = 0) × (cy18 < 14) × 5
435 r ← (1 s̲elect c) ˡh̲ash 2 s̲elect c
436 q ← (2 s̲elect c) ˡh̲ash 1 s̲elect c
437 t ← 4 + f̲loor 10.0 × (r̲avel r) c̲at (r̲avel q) c̲at (r̲avel r × q) c̲at r̲avel 1.0 − r × q
438 (2 2 r̲eshape t) ˡf̲orest blocks
439}
440
441⍝│ vx:c_olumnMask b: the solid mask of a column's own cells (16 by 32
442⍝│ by 16, flat, x slowest), from its blocks; Ints (f_loor f_loat: so the
443⍝│ type checker settles the masks' type the same way every run, ask E9)
444ˡc̲olumnMask ← { b → inner s̲elect r̲avel f̲loor f̲loat ˡs̲olid b }
445
446⍝│ vx:c_olumnWater b: the water mask of a column's own cells (16 by 32
447⍝│ by 16, flat, x slowest), from its blocks: where water can be seen and
448⍝│ swum, and where flowing water cannot go again
449ˡc̲olumnWater ← { b → inner s̲elect r̲avel f̲loor f̲loat b = 5 }
450
451⍝│ c vx:c_olumnFaces b: the faces of column c's own cells, in world
452⍝│ coordinates, one row each (x y z direction block) -- found over the
453⍝│ block with its border, so faces against a solid neighbor hide; water
454⍝│ shows its top where air is above it and its sides and bottom where
455⍝│ air is beside or below it (water flowing down a slope); the second
456⍝│ part of making a column
457ˡc̲olumnFaces ← { c b →
458 s ← ˡs̲olid b
459 w ← s > (-1 o̲-₁ s) × X18 > 0
460 e ← s > (1 o̲-₁ s) × X18 < 17
461 d ← s > (-1 o̲-₂ s) × Y18 > 0
462 u ← s > (1 o̲-₂ s) × Y18 < 31
463 n ← s > (-1 o̲-₃ s) × Z18 > 0
464 so ← s > (1 o̲-₃ s) × Z18 < 17
465 top ← (b = 5) > (1 o̲-₂ b > 0) × Y18 < 31
466 wet ← b = 5
467 dry ← b = 0
468 ww ← wet × (-1 o̲-₁ dry) × X18 > 0
469 we ← wet × (1 o̲-₁ dry) × X18 < 17
470 wd ← wet × (-1 o̲-₂ dry) × Y18 > 0
471 wn ← wet × (-1 o̲-₃ dry) × Z18 > 0
472 ws ← wet × (1 o̲-₃ dry) × Z18 < 17
473 bb ← own × 1 + r̲avel b
474 x0 ← (16 × 1 s̲elect c) − 1
475 z0 ← (16 × 2 s̲elect c) − 1
476 r̲w ← { d m →
477 k ← w̲here m > 0
478 (5 c̲at t̲ally k) r̲eshape (x0 + k s̲elect cx18) c̲at (k s̲elect cy18) c̲at (z0 + k s̲elect cz18) c̲at ((t̲ally k) r̲eshape d) c̲at (k s̲elect m) − 1
479 }
480 r ← (((0 r̲w bb × r̲avel w) c̲at₂ (1 r̲w bb × r̲avel e) c̲at₂ (2 r̲w bb × r̲avel d) c̲at₂ (3 r̲w bb × r̲avel u)) c̲at₂
481 ((4 r̲w bb × r̲avel n) c̲at₂ (5 r̲w bb × r̲avel so) c̲at₂ 3 r̲w bb × r̲avel top) c̲at₂
482 ((0 r̲w bb × r̲avel ww) c̲at₂ (1 r̲w bb × r̲avel we) c̲at₂ (2 r̲w bb × r̲avel wd)) c̲at₂
483 (4 r̲w bb × r̲avel wn) c̲at₂ 5 r̲w bb × r̲avel ws)
484 2 1 t̲ranspose r
485}
486
487⍝━━ Light
488
489⍝ the edges of a column's 18 by 32 by 18 cells: where a shifted neighbor
490⍝ would wrap around (made once)
491ex0 ← (X18 > 0) × 1
492ex1 ← (X18 < 17) × 1
493ey0 ← (Y18 > 0) × 1
494ey1 ← (Y18 < 31) × 1
495ez0 ← (Z18 > 0) × 1
496ez1 ← (Z18 < 17) × 1
497
498⍝│ vx:c_olumnLight b: the light in a column's blocks with their border
499⍝│ (18 by 32 by 18), as (sunlight, lamplight), each 0 to 15 a cell.
500⍝│ Sunlight: an open cell (air or water) with only open cells above it
501⍝│ sees the sky, 15 -- a running minimum down each column, one scan.
502⍝│ Lamplight: a lamp is 15, a gem 12 (none: none to spread). Each then spreads 5
503⍝│ rounds through open cells, a cell taking the brightest of its six
504⍝│ neighbors less 3 (the same six shifts as the faces), so light reaches
505⍝│ under overhangs and into tunnels and fades over 5 blocks. Light from
506⍝│ beyond the border is not seen: a lamp more than a block into a
507⍝│ neighboring column does not light this one.
508⍝│ >> "vx:" u_se< "Voxels"
509⍝│ >> u:s_unOf := { (sun, _) -> sun }
510⍝│ >> 'm_ax r_/ r_avel u:s_unOf vx:c_olumnLight vx:c_olumnBlocks 0 0
511⍝│ 15
512ˡc̲olumnLight ← { b →
513 op ← 1 − ˡs̲olid b
514 sun ← 5 '{ op ʰg̲low ⍵ } p̲ower 15 × r̲ev₂ 'm̲in s̲\₂ r̲ev₂ op
515 glow ← (15 × b = 8) m̲ax 12 × b = 9
516 0 = '+ r̲/ r̲avel glow ? (sun, 0 × sun)
517 (sun, 5 '{ op ʰg̲low ⍵ } p̲ower glow)
518}
519
520⍝ op h:g_low L: one round of light spreading: each open cell the
521⍝ brightest of its six neighbors less 3, if that is brighter
522ʰg̲low ← { op L →
523 n ← (((-1 o̲-₁ L) × ex0) m̲ax
524 ((1 o̲-₁ L) × ex1) m̲ax
525 ((-1 o̲-₂ L) × ey0) m̲ax
526 ((1 o̲-₂ L) × ey1) m̲ax
527 ((-1 o̲-₃ L) × ez0) m̲ax
528 (1 o̲-₃ L) × ez1)
529 L m̲ax op × 0 m̲ax n − 3
530}
531
532⍝│ (c, (sun, lamp)) vx:f_aceLight f: each face's sunlight and lamplight
533⍝│ (n by 2, 0 to 15): the light of the cell it faces (the sky above the
534⍝│ world); a lamp's and a gem's own faces are fully lit
535ˡf̲aceLight ← { csl f →
536 (c, (sun, lamp)) ← csl
537 d ← 1 + 4 s̲elect₂ f
538 ox ← d s̲elect -1 1 0 0 0 0
539 oy ← d s̲elect 0 0 -1 1 0 0
540 oz ← d s̲elect 0 0 0 0 -1 1
541 lx ← ox + (1 s̲elect₂ f) − (16 × 1 s̲elect c) − 1
542 ly ← oy + 2 s̲elect₂ f
543 lz ← oz + (3 s̲elect₂ f) − (16 × 2 s̲elect c) − 1
544 i ← 1 + lz + 18 × (0 m̲ax 31 m̲in ly) + 32 × lx
545 s ← (15 × ly > 31) m̲ax i s̲elect r̲avel sun
546 l ← (15 × 8 ≤ 5 s̲elect₂ f) m̲ax i s̲elect r̲avel lamp
547 2 1 t̲ranspose (2 c̲at t̲ally s) r̲eshape s c̲at l
548}