sourceextensions/scene/demos/Endless.xtl
1⍝│ Endless.xtl -- the endless world and its player, shared by the voxel
2⍝│ demos that walk or fly in it (voxels-endless, voxels-fly, voxels-dig
3⍝│ and those after): columns of 16 by 16 blocks made around the player
4⍝│ and dropped behind it, each made over three frames so no frame waits
5⍝│ long; the player -- walking, swimming, flying -- moving against them;
6⍝│ and blocks dug out and placed, remembered as edits that every column
7⍝│ is made with, so they stay when the player comes back.
8⍝│ Import: "en:" u_se< "Endless"
9
10"vx:" u̲se< "Voxels"
11"sc:" u̲se< "Scene"
12
13⍝━━ The state
14
15⍝│ The state is a tuple of tuples, each part its own type:
16⍝│ (player, world, job, settings) --
17⍝│ the player (position x y z, velocity, yaw, pitch, on the ground 0 or
18⍝│ 1, flying 0 or 1); the world (the columns' keys, their numbers -- a
19⍝│ column's scene objects are 10 * number + block -- their solid masks
20⍝│ and their water masks, a box each, in the keys' order; the edits, n
21⍝│ by 4, x y z and block, the newest first; the keys of the columns to
22⍝│ make again, edited; the moving water, n by 6, x y z, amount and
23⍝│ direction; and the frames until its next tick); the job, the column being
24⍝│ made (its part 0 to 2, its column cx cz, its number, its data:
25⍝│ blocks, then faces); the settings (the window, the next number, the
26⍝│ start x y z, whether it runs). Made by en:n_ew or en:n_ewAt.
27
28⍝│ the columns are kept this many (16-block steps) from the player's,
29⍝│ each way, and made within it; beyond one more they are dropped
30ˡreach ← 3
31
32⍝│ en:p_layer s: the player (position, velocity, yaw, pitch, ground, fly)
33ˡp̲layer ← { (p, _, _, _) → p }
34⍝│ en:p_osition s: where the player's feet are, x y z
35ˡp̲osition ← { ((x, _, _, _, _, _), _, _, _) → x }
36⍝│ en:k_eys s: the columns' keys
37ˡk̲eys ← { (_, (ks, _, _, _, _, _, _, _), _, _) → ks }
38⍝│ en:w_orld s: the world (columns, masks, edits), for en:h_it and
39⍝│ en:p_ick
40ˡw̲orld ← { (_, wo, _, _) → wo }
41⍝│ en:e_dits s: the blocks dug out and placed, n by 4 (x y z block, air
42⍝│ 0 for dug), the newest first
43ˡe̲dits ← { (_, (_, _, _, _, ed, _, _, _), _, _) → ed }
44⍝│ en:e_ye s: the player's eye, x y z yaw pitch (1.6 above its feet)
45ˡe̲ye ← { ((x, _, yaw, pitch, _, _), _, _, _) → (x + 0.0 1.6 0.0) c̲at yaw c̲at pitch }
46⍝│ en:w_indow s: the scene window the world is drawn in
47ˡw̲indow ← { (_, _, _, (w, _, _, _, _)) → w }
48⍝│ en:s_tart s: where the player started (x y z)
49ˡs̲tart ← { (_, _, _, (_, _, x0, _, _)) → x0 }
50
51⍝ a column's key from its coordinates (cx cz), and back
52ʰk̲ey ← { c → (10000 × 1000 + 1 s̲elect c) + 1000 + 2 s̲elect c }
53ʰc̲x ← { k → (k d̲iv 10000) − 1000 }
54ʰc̲z ← { k → (k m̲od 10000) − 1000 }
55
56⍝│ p en:w_ithPlayer s: the state with the player p
57ˡw̲ithPlayer ← { p (_, wo, jb, st) → (p, wo, jb, st) }
58
59⍝│ en:n_ew w: a world for window w, no columns made yet, the player
60⍝│ standing above the highest ground of column (0, 0), facing north
61ˡn̲ew ← { w →
62 hv ← r̲avel ᵛˣh̲eightsAt 0 0 16
63 i ← f̲irst w̲here hv = 'm̲ax r̲/ hv
64 x0 ← ((f̲loat (i − 1) d̲iv 16) + 0.5) c̲at (f̲loat 2 + i s̲elect hv) c̲at (f̲loat (i − 1) m̲od 16) + 0.5
65 w ˡn̲ewAt x0
66}
67
68⍝│ w en:n_ewAt x0: a world for window w, no columns made yet, the player
69⍝│ standing at x0 (x y z, its feet; also where H goes back to), facing
70⍝│ north
71ˡn̲ewAt ← { w x0 →
72 p ← (x0, 0.0 0.0 0.0, 0.0, 0.0, 0.0, 0.0)
73 none ← 0 r̲eshape 0
74 wo ← (none, none, 0 r̲eshape e̲nclose none, 0 r̲eshape e̲nclose none, 0 4 r̲eshape 0, none, 0 6 r̲eshape 0, 0)
75 (p, wo, (0, 0 0, 0, none), (w, 1, x0, 1, 0))
76}
77
78⍝━━ Making and dropping columns
79
80⍝ the player's column (cx cz)
81ʰh̲ome ← { s →
82 x ← ˡp̲osition s
83 (f̲loor (1 s̲elect x) ÷ 16.0) c̲at f̲loor (3 s̲elect x) ÷ 16.0
84}
85
86⍝ the first column too far away, if any, dropped: its objects out of
87⍝ scene, its key, number and mask out of the world
88ʰt̲rim ← { s →
89 (p, (ks, ns, ms, wm, ed, rq, fl, fw), jb, st) ← s
90 h ← ʰh̲ome s
91 far ← ((a̲bs (ʰc̲x ks) − 1 s̲elect h) > ˡreach + 1) + (a̲bs (ʰc̲z ks) − 2 s̲elect h) > ˡreach + 1
92 0 = '+ r̲/ far ? s
93 j ← f̲irst w̲here far > 0
94 n ← j s̲elect ns
95 w ← ˡw̲indow s
96 _ ← '{ w ˢᶜr̲emove! (10 × n) + ⍵ } m̲ap r̲ange 9
97 keep ← j ≠ r̲ange t̲ally ks
98 (p, (keep r̲eplicate ks, keep r̲eplicate ns, keep r̲eplicate ms, keep r̲eplicate wm, ed, rq, fl, fw), jb, st)
99}
100
101⍝ (w n t) h:s_endOne f: block t's faces of column n, one object of quads
102⍝ (none: the object removed, for a column made again without that block)
103ʰs̲endOne ← { wnt f →
104 t ← 3 s̲elect wnt
105 sel ← (5 s̲elect₂ f) = t
106 0 = '+ r̲/ sel ? (f̲irst wnt) ˢᶜr̲emove! (10 × 2 s̲elect wnt) + t
107 head ← (f̲loat 1 s̲elect wnt) c̲at (f̲loat (10 × 2 s̲elect wnt) + t) c̲at ((1 + t) s̲elect ᵛˣcolors) c̲at (t = 5) r̲eplicate 0.6
108 fs ← sel r̲eplicate f
109 7 > 2 s̲elect s̲hape f ? head ˢᶜq̲uads! ᵛˣc̲orners fs
110 ⍝ lit: each corner its face's sunlight and lamplight, 0 to 1
111 k ← t̲ally fs
112 lc ← (1 + ((r̲ange 4 × k) − 1) d̲iv 4) s̲elect 6 7 s̲elect₂ fs
113 head ˢᶜq̲uads! (ᵛˣc̲orners fs) c̲at₂ (f̲loat lc) ÷ 15.0
114}
115
116⍝│ en:s_tream s: one frame's work on the world: the first column too far
117⍝│ away dropped, then a stage of making a column -- an edited one again,
118⍝│ or else the nearest missing one: its blocks and solid mask (part 0),
119⍝│ its faces (part 1), in a lit world its light (part 3), or sending
120⍝│ them to scene (part 2) -- about 12 ms (20 for light)
121⍝│ at most; or, when it is time, a tick of moving water
122ˡs̲tream ← { s →
123 trimmed ← ʰt̲rim s
124 (_, (_, _, _, _, _, rq, fl, fw), (part, _, _, _), _) ← trimmed
125 part = 1 ? ʰf̲aces trimmed
126 part = 2 ? ʰs̲end trimmed
127 part = 3 ? ʰl̲ight trimmed
128 ((fw ≤ 0) × 0 < t̲ally fl) ? ʰf̲low trimmed
129 ʰb̲locks ʰw̲ait trimmed
130}
131
132⍝ a frame less to wait for the water's next tick
133ʰw̲ait ← { (p, (ks, ns, ms, wm, ed, rq, fl, fw), jb, st) → (p, (ks, ns, ms, wm, ed, rq, fl, 0 m̲ax fw − 1), jb, st) }
134
135
136⍝ part 0: the nearest missing column's blocks and mask; the column is
137⍝ known (collision sees it) from now on, drawn two frames later
138ʰb̲locks ← { s →
139 (p, (ks, ns, ms, wm, ed, rq, fl, fw), jb, (w, nxt, x0, on, lit)) ← s
140 0 < t̲ally rq ? ʰr̲emake s
141 h ← ʰh̲ome s
142 r ← ˡreach
143 o ← (r̲ange 1 + 2 × r) − 1 + r
144 dx ← r̲avel o '+ t̲able 0 × o
145 dz ← r̲avel (0 × o) '+ t̲able o
146 ws ← (10000 × 1000 + dx + 1 s̲elect h) + 1000 + dz + 2 s̲elect h
147 miss ← (0 = ws m̲ember? ks) r̲eplicate (r̲ange t̲ally ws)
148 0 = t̲ally miss ? s
149 d ← (miss s̲elect dx × dx) + miss s̲elect dz × dz
150 i ← f̲irst (d = 'm̲in r̲/ d) r̲eplicate miss
151 c ← ((i s̲elect dx) + 1 s̲elect h) c̲at (i s̲elect dz) + 2 s̲elect h
152 b ← (ed, c) ˡe̲dited ᵛˣc̲olumnBlocks c
153 wo ← (ks c̲at i s̲elect ws, ns c̲at nxt, ms c̲at (e̲nclose ᵛˣc̲olumnMask b), wm c̲at (e̲nclose ᵛˣc̲olumnWater b), ed, rq, fl, fw)
154 (p, wo, (1, c, nxt, r̲avel b), (w, nxt + 1, x0, on, lit))
155}
156
157⍝ part 0 for an edited column: the first queued one's blocks again, with
158⍝ the edits, its mask replaced at once (collision sees the edit from
159⍝ now on); faces and sending as for a new column, its objects replaced
160ʰr̲emake ← { (p, (ks, ns, ms, wm, ed, rq, fl, fw), jb, st) →
161 k ← f̲irst rq
162 rest ← 1 d̲rop rq
163 j ← ks i̲ndexOf k
164 j > t̲ally ks ? (p, (ks, ns, ms, wm, ed, rest, fl, fw), jb, st)
165 c ← (ʰc̲x k) c̲at ʰc̲z k
166 b ← (ed, c) ˡe̲dited ᵛˣc̲olumnBlocks c
167 masks ← ((j − 1) t̲ake ms) c̲at (e̲nclose ᵛˣc̲olumnMask b) c̲at j d̲rop ms
168 waters ← ((j − 1) t̲ake wm) c̲at (e̲nclose ᵛˣc̲olumnWater b) c̲at j d̲rop wm
169 ⍝ the column changed: the water wakes (its masks are new now)
170 (p, (ks, ns, masks, waters, ed, rest, fl, 0), (1, c, j s̲elect ns, r̲avel b), st)
171}
172
173⍝│ (edits, c) en:e_dited b: the blocks b of column c (18 by 32 by 18,
174⍝│ with its border of one) with the edits (n by 4, x y z block) that fall
175⍝│ in them -- the newest first, so the newest edit of a cell wins
176ˡe̲dited ← { (ed, c) b →
177 0 = t̲ally ed ? b
178 lx ← (1 s̲elect₂ ed) − (16 × 1 s̲elect c) − 1
179 lz ← (3 s̲elect₂ ed) − (16 × 2 s̲elect c) − 1
180 ly ← 2 s̲elect₂ ed
181 here ← (lx ≥ 0) × (lx ≤ 17) × (lz ≥ 0) × lz ≤ 17
182 0 = '+ r̲/ here ? b
183 at ← here r̲eplicate 1 + lz + 18 × ly + 32 × lx
184 t ← here r̲eplicate 4 s̲elect₂ ed
185 v ← r̲avel b
186 hit ← at i̲ndexOf r̲ange t̲ally v
187 set ← hit ≤ t̲ally at
188 18 32 18 r̲eshape (v × 1 − set) + set × hit s̲elect t c̲at 0
189}
190
191⍝ part 1: the job's faces, from its blocks (in a lit world the blocks
192⍝ are kept too, for part 3)
193ʰf̲aces ← { (p, wo, (_, c, n, b), st) →
194 (_, _, _, _, lit) ← st
195 f ← c ᵛˣc̲olumnFaces 18 32 18 r̲eshape b
196 0 = lit ? (p, wo, (2, c, n, r̲avel f), st)
197 (p, wo, (3, c, n, b c̲at r̲avel f), st)
198}
199
200⍝ part 3, in a lit world: the column's light (vx:c_olumnLight) and each
201⍝ face's sunlight and lamplight, two more numbers a face
202ʰl̲ight ← { (p, wo, (_, c, n, v), st) →
203 b ← 18 32 18 r̲eshape 10368 t̲ake v
204 f ← (((t̲ally 10368 d̲rop v) d̲iv 5) c̲at 5) r̲eshape 10368 d̲rop v
205 shaded ← f c̲at₂ (c, ᵛˣc̲olumnLight b) ᵛˣf̲aceLight f
206 (p, wo, (2, c, n, r̲avel shaded), st)
207}
208
209⍝ part 2: the job's faces to scene, one object for each kind of block
210⍝ (5 numbers a face, or 7 with its light)
211ʰs̲end ← { (p, wo, (_, c, n, v), st) →
212 (w, _, _, _, lit) ← st
213 k ← 5 + 2 × 0 < lit
214 f ← (((t̲ally v) d̲iv k) c̲at k) r̲eshape v
215 sent ← '{ (w c̲at n c̲at ⍵) ʰs̲endOne f } m̲ap r̲ange 9
216 (p, wo, (0, c, n, 0 r̲eshape 0), st)
217}
218
219⍝━━ Collision
220
221⍝ wo h:c_ell v: whether any of the cells at column (x z) and heights ys
222⍝ (v is x z then the ys) is solid; a column not made yet is solid
223ʰc̲ell ← { (ks, _, ms, _, _, _, _, _) v →
224 x ← 1 s̲elect v
225 z ← 2 s̲elect v
226 ys ← 2 d̲rop v
227 i ← ks i̲ndexOf ʰk̲ey (x d̲iv 16) c̲at z d̲iv 16
228 i > t̲ally ks ? 1
229 0 < '+ r̲/ (1 + (z m̲od 16) + 16 × ys + 32 × x m̲od 16) s̲elect d̲isclose i s̲elect ms
230}
231
232⍝│ wo en:h_it q: whether the player's box (0.6 wide, 1.75 tall) with its
233⍝│ feet at q (x y z) overlaps a solid block of the world wo; below the
234⍝│ world is solid, above it air
235ˡh̲it ← { wo q →
236 x ← 1 s̲elect q
237 y ← 2 s̲elect q
238 z ← 3 s̲elect q
239 cx ← u̲nique f̲loor (x − 0.3) c̲at x + 0.3
240 cy ← u̲nique f̲loor y c̲at (y + 0.9) c̲at y + 1.75
241 cz ← u̲nique f̲loor (z − 0.3) c̲at z + 0.3
242 0 < '+ r̲/ cy < 0 ? 1.0
243 inside ← (cy ≤ 31) r̲eplicate cy
244 0 = t̲ally inside ? 0.0
245 px ← r̲avel cx '{ ⍺ + 0 × ⍵ } t̲able cz
246 pz ← r̲avel cx '{ ⍵ + 0 × ⍺ } t̲able cz
247 hits ← '{ wo ʰc̲ell (⍵ s̲elect px) c̲at (⍵ s̲elect pz) c̲at inside } e̲ach r̲ange t̲ally px
248 f̲loat 0 < '+ r̲/ hits
249}
250
251⍝━━ Digging and building
252
253⍝│ wo en:p_ick e: the block looked at from the eye e (x y z yaw pitch)
254⍝│ within 5 blocks, found by 100 points marched along the view ray and
255⍝│ looked up in the masks of the columns they cross: 1 and its cell x y
256⍝│ z, then 1 and the empty cell just before it (where a block placed
257⍝│ goes), or 0 0 0 0 when there is none; all 0 when nothing solid is that
258⍝│ near. Water is not solid (the ray goes through it); a column not made
259⍝│ yet is empty.
260ˡp̲ick ← { (ks, _, ms, _, _, _, _, _) e →
261 yaw ← 4 s̲elect e
262 pitch ← 5 s̲elect e
263 t ← 0.05 × f̲loat r̲ange 100
264 px ← f̲loor (1 s̲elect e) + t × (s̲in yaw) × c̲os pitch
265 py ← f̲loor (2 s̲elect e) + t × s̲in pitch
266 pz ← f̲loor (3 s̲elect e) − t × (c̲os yaw) × c̲os pitch
267 keys ← (10000 × 1000 + px d̲iv 16) + 1000 + pz d̲iv 16
268 u ← u̲nique keys
269 j ← ks i̲ndexOf u
270 all ← d̲isclose '{ e̲nclose (d̲isclose ⍺) c̲at d̲isclose ⍵ } r̲/ j s̲elect ms c̲at e̲nclose 0 × d̲isclose f̲irst ms
271 ok ← (py ≥ 0) × py ≤ 31
272 at ← (8192 × (u i̲ndexOf keys) − 1) + 1 + (pz m̲od 16) + 16 × (0 m̲ax 31 m̲in py) + 32 × px m̲od 16
273 solid ← ok × at s̲elect all
274 0 = '+ r̲/ solid ? 8 r̲eshape 0
275 i ← f̲irst w̲here solid > 0
276 cell ← (i s̲elect px) c̲at (i s̲elect py) c̲at i s̲elect pz
277 other ← 0 < (px ≠ i s̲elect px) + (py ≠ i s̲elect py) + pz ≠ i s̲elect pz
278 prev ← w̲here ((r̲ange 100) < i) × other
279 0 = t̲ally prev ? 1 c̲at cell c̲at 0 0 0 0
280 k ← f̲irst -1 t̲ake prev
281 1 c̲at cell c̲at 1 c̲at (k s̲elect px) c̲at (k s̲elect py) c̲at k s̲elect pz
282}
283
284⍝│ (cell, block) en:s_et s: the block at the cell (x y z) set -- air, 0,
285⍝│ to dig it out -- and remembered (the newest edit first); its column
286⍝│ made again before any new one, and a neighbor too when the cell is on
287⍝│ their border (its faces against the cell change). Water (block 5) is
288⍝│ not an edit: placed, it is a full block of moving water in the cell; dug
289⍝│ out beside the sea (at a side or above), the cell fills from it. A
290⍝│ solid block placed in moving water takes its place (that water is
291⍝│ gone). Any change wakes the water.
292ˡs̲et ← { (q, t) s →
293 (_, wo, _, _) ← s
294 t = 5 ? (1 6 r̲eshape q c̲at ˡfull c̲at 0 0) ˡp̲our s
295 placed ← ((1 3 r̲eshape q), t) ˡs̲etAll (q, t) ʰd̲isplace s
296 sea ← (t = 0) × 0 < '+ r̲/ 2 = wo ˡl̲ook (5 3 r̲eshape q) + 5 3 r̲eshape 0 1 0 -1 0 0 1 0 0 0 0 -1 0 0 1
297 0 = sea ? placed
298 (1 6 r̲eshape q c̲at ˡfull c̲at 0 0) ˡp̲our placed
299}
300
301⍝│ (cells, block) en:s_etAll s: the cells (n by 3, x y z) all set to the
302⍝│ block, as en:s_et (without the water), their columns queued once
303ˡs̲etAll ← { (q, t) s →
304 (p, (ks, ns, ms, wm, ed, rq, fl, fw), jb, st) ← s
305 n ← 1 s̲elect s̲hape q
306 edits ← (q c̲at₂ (n c̲at 1) r̲eshape t) c̲at ed
307 x ← 1 s̲elect₂ q
308 z ← 3 s̲elect₂ q
309 want ← 2 1 t̲ranspose (5 c̲at n) r̲eshape (n r̲eshape 1) c̲at ((x m̲od 16) = 0) c̲at ((x m̲od 16) = 15) c̲at ((z m̲od 16) = 0) c̲at (z m̲od 16) = 15
310 kx ← (x d̲iv 16) '+ t̲able 0 -1 1 0 0
311 near ← u̲nique (r̲avel want) r̲eplicate r̲avel (10000 × 1000 + kx) + 1000 + (z d̲iv 16) '+ t̲able 0 0 0 -1 1
312 (p, (ks, ns, ms, wm, edits, near c̲at (0 = rq m̲ember? near) r̲eplicate rq, fl, fw), jb, st)
313}
314
315⍝│ f en:p_our s: the cells f (n by 6: x y z, amount 1 to en:full, and the
316⍝│ direction it moves, dx dz) added to the moving water, which wakes
317ˡp̲our ← { f (p, (ks, ns, ms, wm, ed, rq, W, fw), jb, st) → (p, (ks, ns, ms, wm, ed, rq, f c̲at W, 0), jb, st) }
318
319⍝ (q, t) h:d_isplace s: moving water at cell q gone when a block (not
320⍝ air) is put there; the water woken either way
321ʰd̲isplace ← { qt (p, (ks, ns, ms, wm, ed, rq, W, fw), jb, st) →
322 (q, t) ← qt
323 keep ← (t = 0) + (ʰc̲ellKey 1 2 3 s̲elect₂ W) ≠ f̲irst ʰc̲ellKey 1 3 r̲eshape q
324 (p, (ks, ns, ms, wm, ed, rq, (0 < keep) r̲eplicate W, 0), jb, st)
325}
326⍝│ wo en:l_ook q: what is at each cell of q (n by 3, x y z) in the world
327⍝│ wo: 0 air, 1 solid, 2 water; below the world, and a column not made
328⍝│ yet, are solid; above it is air
329ˡl̲ook ← { (ks, _, ms, wm, _, _, _, _) q →
330 0 = t̲ally q ? 0 r̲eshape 0
331 0 = t̲ally ms ? (t̲ally q) r̲eshape 1
332 x ← 1 s̲elect₂ q
333 y ← 2 s̲elect₂ q
334 z ← 3 s̲elect₂ q
335 keys ← (10000 × 1000 + x d̲iv 16) + 1000 + z d̲iv 16
336 u ← u̲nique keys
337 j ← ks i̲ndexOf u
338 m ← 0 × d̲isclose f̲irst ms
339 sol ← d̲isclose '{ e̲nclose (d̲isclose ⍺) c̲at d̲isclose ⍵ } r̲/ j s̲elect ms c̲at e̲nclose 1 + m
340 wat ← d̲isclose '{ e̲nclose (d̲isclose ⍺) c̲at d̲isclose ⍵ } r̲/ j s̲elect wm c̲at e̲nclose m
341 ok ← (y ≥ 0) × y ≤ 31
342 at ← (8192 × (u i̲ndexOf keys) − 1) + 1 + (z m̲od 16) + 16 × (0 m̲ax 31 m̲in y) + 32 × x m̲od 16
343 hard ← 0 < (y < 0) + ok × at s̲elect sol
344 wet ← (1 − hard) × 0 < ok × at s̲elect wat
345 ⍝ an Int for certain (f_loor f_loat), so the type checker settles it
346 ⍝ the same way every run (ask E9)
347 f̲loor f̲loat hard + 2 × wet
348}
349
350⍝━━ Moving water
351
352⍝│ Water that moves is an amount in each cell it is in, 1 to 64 units
353⍝│ (en:full, a full block), and the direction it was last moving (dx dz), kept as
354⍝│ n by 6 Ints (en:w_ater) apart from the blocks; the sea is blocks, a
355⍝│ source that never empties. Water is conserved: a tick moves it from
356⍝│ cell to cell and never makes or loses any, except that a cell beside
357⍝│ the sea (at a side or above) is kept full by it and water falling
358⍝│ into the sea becomes the sea. Each tick, in order:
359⍝│
360⍝│ - gravity: each cell's water falls into the room below it, as much
361⍝│ as fits (into air, or moving water not yet full);
362⍝│ - then four passes, one a direction (west, east, north, south): a
363⍝│ cell that cannot fall (something solid, or full water, below) sends
364⍝│ part of the difference to a neighbor with less water, a share of
365⍝│ 4/16, more toward the way it was moving (momentum, 7/16) and toward
366⍝│ an edge with a drop below it (gravity pulling it on, 8/16) -- never
367⍝│ more than half, so neighbors even out instead of trading places --
368⍝│ and at least 1 when the difference is 2 or more; a stream's front
369⍝│ moves on into an empty cell the way it is going, or over an edge,
370⍝│ even by 1. Water one unit (1/64 of a block) above its neighbor
371⍝│ stays put.
372⍝│
373⍝│ So a breach in a lake's rim drains the lake: a lake of area A loses
374⍝│ one level for each A blocks of water that leave it, and stops at the
375⍝│ breach's bottom until the breach is dug deeper; the water that left
376⍝│ runs on in the direction it took and pours over edges. After a tick
377⍝│ that moved nothing the water sleeps until something changes.
378
379⍝│ the frames between ticks of moving water
380ˡpace ← 1
381
382⍝│ the units of water in a full block: fine enough that a lake can drain
383⍝│ to within a sliver of its breach (a difference of one unit stays put)
384ˡfull ← 64
385
386⍝ each cell's key (q n by 3), unique for cells within 100000 of the
387⍝ origin
388ʰc̲ellKey ← { q → (200000 × (64 × 100000 + 1 s̲elect₂ q) + 2 s̲elect₂ q) + 100000 + 3 s̲elect₂ q }
389
390⍝ the cells q moved by o (x y z)
391ʰs̲hift ← { q o → q + ((1 s̲elect s̲hape q) c̲at 3) r̲eshape o }
392
393⍝ one tick of moving water (or, if it moved nothing, the water asleep)
394ʰf̲low ← { s →
395 (p, wo, jb, st) ← s
396 (ks, ns, ms, wm, ed, rq, W, fw) ← wo
397 (P, A, D) ← (wo, 0 1) ʰs̲pread (wo, 0 -1) ʰs̲pread (wo, 1 0) ʰs̲pread (wo, -1 0) ʰs̲pread wo ʰf̲all wo ʰr̲efill W
398 live ← A > 0
399 n ← '+ r̲/ live
400 W2 ← (live r̲eplicate P) c̲at₂ ((n c̲at 1) r̲eshape live r̲eplicate A) c̲at₂ live r̲eplicate D
401 still ← W2 m̲atch W
402 z ← (wo, W2) ʰd̲rawWater ˡw̲indow s
403 (p, (ks, ns, ms, wm, ed, rq, W2, still ʰn̲ext 0), jb, st)
404}
405⍝ the frames to the next tick: a while when nothing moved (asleep until
406⍝ something changes)
407ʰn̲ext ← { still z →
408 still ? 1000000000
409 ˡpace
410}
411
412⍝ wo h:r_efill W: the cells and amounts and directions of W, a cell
413⍝ beside the sea (at a side or above) full
414ʰr̲efill ← { wo W →
415 P ← 1 2 3 s̲elect₂ W
416 A ← 4 s̲elect₂ W
417 D ← 5 6 s̲elect₂ W
418 sides ← (P ʰs̲hift -1 0 0) c̲at (P ʰs̲hift 1 0 0) c̲at (P ʰs̲hift 0 0 -1) c̲at (P ʰs̲hift 0 0 1) c̲at P ʰs̲hift 0 1 0
419 sea ← 0 < '+ r̲/₁ (5 c̲at t̲ally A) r̲eshape 2 = wo ˡl̲ook sides
420 (P, (ˡfull × sea) + (1 − sea) × A, D)
421}
422
423⍝ wo h:f_all (P, A, D): each cell's water fallen into the room below it;
424⍝ into the sea it is gone (the sea is full)
425ʰf̲all ← { wo (P, A, D) →
426 n ← t̲ally A
427 keys ← ʰc̲ellKey P
428 dn ← P ʰs̲hift 0 -1 0
429 kd ← wo ˡl̲ook dn
430 jd ← keys i̲ndexOf ʰc̲ellKey dn
431 room ← (kd = 0) × ˡfull − jd s̲elect A c̲at 0
432 f ← ((kd = 2) × A) + (kd ≠ 2) × A m̲in room
433 ja ← keys i̲ndexOf ʰc̲ellKey P ʰs̲hift 0 1 0
434 settled ← (A − f) + (ja s̲elect f c̲at 0) × ja ≤ n
435 new ← (f > 0) × (kd = 0) × jd > n
436 m ← '+ r̲/ new
437 (P c̲at new r̲eplicate dn, settled c̲at new r̲eplicate f, D c̲at new r̲eplicate D)
438}
439
440⍝ (wo, o) h:s_pread (P, A, D): one pass of spreading toward o (dx dz):
441⍝ each cell that cannot fall sends part of the difference to that
442⍝ neighbor when it holds less, and the neighbor takes o as its direction
443ʰs̲pread ← { woo (P, A, D) →
444 (wo, o) ← woo
445 n ← t̲ally A
446 off ← (1 s̲elect o) c̲at 0 c̲at 2 s̲elect o
447 keys ← ʰc̲ellKey P
448 side ← P ʰs̲hift off
449 ks ← wo ˡl̲ook side
450 js ← keys i̲ndexOf ʰc̲ellKey side
451 bs ← js s̲elect A c̲at 0
452 dn ← P ʰs̲hift 0 -1 0
453 jd ← keys i̲ndexOf ʰc̲ellKey dn
454 held ← ((wo ˡl̲ook dn) = 1) + ˡfull = jd s̲elect A c̲at 0
455 drop ← 0 = wo ˡl̲ook side ʰs̲hift 0 -1 0
456 ahead ← ((1 s̲elect₂ D) = 1 s̲elect o) × (2 s̲elect₂ D) = 2 s̲elect o
457 wt ← 4 m̲ax (7 × ahead) m̲ax 8 × drop
458 go ← (ks = 0) × (0 < held) × A > bs
459 ⍝ at most half the difference: neighbors even out, never trade places
460 ⍝ a stream's front keeps going the way it moves, and over an edge,
461 ⍝ even by 1 (only into an empty cell, so water cannot circle)
462 t ← ((go × ((A − bs) × wt) d̲iv 16) m̲ax
463 go × ((A − bs) ≥ 2) + (bs = 0) × 0 < ahead + drop)
464 moved ← t m̲in (ˡfull − bs) m̲in A
465 jf ← keys i̲ndexOf ʰc̲ellKey P ʰs̲hift 0 − off
466 got ← (jf s̲elect moved c̲at 0) × jf ≤ n
467 after ← (A − moved) + got
468 dx ← ((got > 0) × 1 s̲elect o) + (got = 0) × 1 s̲elect₂ D
469 heading ← 2 1 t̲ranspose (2 c̲at n) r̲eshape dx c̲at ((got > 0) × 2 s̲elect o) + (got = 0) × 2 s̲elect₂ D
470 new ← (moved > 0) × js > n
471 m ← '+ r̲/ new
472 (P c̲at new r̲eplicate side, after c̲at new r̲eplicate moved, heading c̲at (m c̲at 2) r̲eshape o)
473}
474
475⍝ (wo, W) h:d_rawWater w: the moving water drawn as one translucent
476⍝ object (9): each cell's top at its amount's height where no water is
477⍝ above it, its sides where air is beside it and its bottom over air
478ʰd̲rawWater ← { woW w →
479 (wo, W) ← woW
480 0 = t̲ally W ? w ˢᶜr̲emove! 9
481 P ← 1 2 3 s̲elect₂ W
482 A ← 4 s̲elect₂ W
483 keys ← ʰc̲ellKey P
484 n ← t̲ally A
485 x ← f̲loat 1 s̲elect₂ P
486 y ← f̲loat 2 s̲elect₂ P
487 z ← f̲loat 3 s̲elect₂ P
488 ht ← y + (f̲loat A) ÷ f̲loat ˡfull
489 d̲ry ← { o → (0 = wo ˡl̲ook P ʰs̲hift o) × n < keys i̲ndexOf ʰc̲ellKey P ʰs̲hift o }
490 top ← (n < keys i̲ndexOf ʰc̲ellKey P ʰs̲hift 0 1 0) × 2 ≠ wo ˡl̲ook P ʰs̲hift 0 1 0
491 q ← (((top, (x, ht, z), (x + 1.0, ht, z), (x + 1.0, ht, z + 1.0), (x, ht, z + 1.0)) ʰq̲uads 0) c̲at
492 ((d̲ry -1 0 0, (x, y, z), (x, y, z + 1.0), (x, ht, z + 1.0), (x, ht, z)) ʰq̲uads 0) c̲at
493 ((d̲ry 1 0 0, (x + 1.0, y, z), (x + 1.0, y, z + 1.0), (x + 1.0, ht, z + 1.0), (x + 1.0, ht, z)) ʰq̲uads 0) c̲at
494 ((d̲ry 0 0 -1, (x, y, z), (x + 1.0, y, z), (x + 1.0, ht, z), (x, ht, z)) ʰq̲uads 0) c̲at
495 ((d̲ry 0 0 1, (x, y, z + 1.0), (x + 1.0, y, z + 1.0), (x + 1.0, ht, z + 1.0), (x, ht, z + 1.0)) ʰq̲uads 0) c̲at
496 (d̲ry 0 -1 0, (x, y, z), (x + 1.0, y, z), (x + 1.0, y, z + 1.0), (x, y, z + 1.0)) ʰq̲uads 0)
497 0 = t̲ally q ? w ˢᶜr̲emove! 9
498 ⍝ in full sunlight: as bright as unlit by day, dimming at night
499 ((f̲loat w) c̲at 9.0 c̲at ((6 s̲elect ᵛˣcolors) c̲at 0.6)) ˢᶜq̲uads! q c̲at₂ ((t̲ally q) c̲at 2) r̲eshape 1.0 0.0
500}
501
502⍝ (mask, corner1 ... corner4) h:q_uads 0: a quad for each cell in the
503⍝ mask, its corners (each x y z, one value a cell) in order round it;
504⍝ 4m by 3
505ʰq̲uads ← { (m, k1, k2, k3, k4) z →
506 k ← '+ r̲/ m
507 0 = k ? 0 3 r̲eshape 0.0
508 c̲orner ← { (a, b, cz) → 2 1 t̲ranspose (3 c̲at k) r̲eshape (m r̲eplicate a) c̲at (m r̲eplicate b) c̲at m r̲eplicate cz }
509 ((4 × k) c̲at 3) r̲eshape 2 1 3 t̲ranspose (4 c̲at k c̲at 3) r̲eshape (c̲orner k1) c̲at (c̲orner k2) c̲at (c̲orner k3) c̲at c̲orner k4
510}
511
512⍝│ en:w_ater s: the moving water, n by 6: x y z, amount (1 to en:full
513⍝│ units, a full block) and the direction it moves (dx dz)
514ˡw̲ater ← { (_, (_, _, _, _, _, _, W, _), _, _) → W }
515
516⍝│ en:a_sleep s: whether the water is asleep (a tick moved nothing and
517⍝│ nothing has changed since)
518ˡa̲sleep ← { (_, (_, _, _, _, _, _, _, fw), _, _) → fw > 1000000 }
519
520⍝│ en:w_aterUnits s: how much water is moving, in units (en:full a block)
521ˡw̲aterUnits ← { s → '+ r̲/ 4 s̲elect₂ ˡw̲ater s }
522
523⍝│ p en:o_verlaps q: whether the player's box (0.6 wide, 1.75 tall) with
524⍝│ its feet at p (x y z) overlaps the cell q (x y z) -- no block is
525⍝│ placed there
526ˡo̲verlaps ← { p q →
527 d ← (f̲loat q) − p
528 3 = '+ r̲/ (d > -1.3 -1.0 -1.3) × d < 0.3 1.75 0.3
529}
530
531⍝━━ The player
532
533⍝│ a frame: 1/60 s
534ˡdt ← 1.0 ÷ 60.0
535
536⍝ (i v) h:s_et3 x: the 3-vector x with item i replaced by v
537ʰs̲et3 ← { l x → x + (f̲loat (r̲ange 3) = f̲loor 1 s̲elect l) × (2 s̲elect l) − x }
538
539⍝ v h:s_nap a: where to stop, moving at v, before the block at a
540ʰs̲nap ← { v a →
541 v > 0.0 ? (f̲loat f̲loor a + 0.3) − 0.3001
542 (f̲loat 1 + f̲loor a − 0.3) + 0.3001
543}
544
545⍝ wo h:m_x p, h:m_z, h:m_y: the three axes of a move against the world
546⍝ wo: x and z stop at a wall; y lands (on the ground) or bumps a ceiling
547ʰm̲x ← { wo (x, v, yaw, pitch, g, fly) →
548 a ← (1 s̲elect x) + ˡdt × 1 s̲elect v
549 q ← (1.0 c̲at a) ʰs̲et3 x
550 0 = wo ˡh̲it q ? (q, v, yaw, pitch, g, fly)
551 ((1.0 c̲at (1 s̲elect v) ʰs̲nap a) ʰs̲et3 x, (1.0 c̲at 0.0) ʰs̲et3 v, yaw, pitch, g, fly)
552}
553ʰm̲z ← { wo (x, v, yaw, pitch, g, fly) →
554 a ← (3 s̲elect x) + ˡdt × 3 s̲elect v
555 q ← (3.0 c̲at a) ʰs̲et3 x
556 0 = wo ˡh̲it q ? (q, v, yaw, pitch, g, fly)
557 ((3.0 c̲at (3 s̲elect v) ʰs̲nap a) ʰs̲et3 x, (3.0 c̲at 0.0) ʰs̲et3 v, yaw, pitch, g, fly)
558}
559ʰm̲y ← { wo (x, v, yaw, pitch, g, fly) →
560 a ← (2 s̲elect x) + ˡdt × 2 s̲elect v
561 q ← (2.0 c̲at a) ʰs̲et3 x
562 0 = wo ˡh̲it q ? (q, v, yaw, pitch, 0.0, fly)
563 (2 s̲elect v) > 0.0 ? ((2.0 c̲at (f̲loat f̲loor a + 1.75) − 1.7501) ʰs̲et3 x, (2.0 c̲at 0.0) ʰs̲et3 v, yaw, pitch, g, fly)
564 ((2.0 c̲at f̲loat 1 + f̲loor a) ʰs̲et3 x, (2.0 c̲at 0.0) ʰs̲et3 v, yaw, pitch, 1.0, fly)
565}
566
567⍝│ s en:m_ove k: one frame of the player under the controls k (mouse dx
568⍝│ dy, then 1 for each held: w a s d space shift left right up down),
569⍝│ walking or flying, the camera set to its eye. Walking: W A S D, Shift
570⍝│ runs, Space jumps; in water (all air below the sea at 14) it sinks
571⍝│ slowly, Space swims up, and from the surface Space climbs onto a bank
572⍝│ one block high. Flying: W A S D level by the heading, Space up, Shift
573⍝│ down, 15 blocks a second, no gravity, the blocks still stopping it.
574ˡm̲ove ← { s k →
575 ((x, v, yaw, pitch, g, fly), wo, jb, st) ← s
576 turn ← yaw + (0.005 × 1 s̲elect k) + 2.0 × ˡdt × (10 s̲elect k) − 9 s̲elect k
577 tilt ← -1.5 m̲ax 1.5 m̲in pitch + (-0.005 × 2 s̲elect k) + 1.5 × ˡdt × (11 s̲elect k) − 12 s̲elect k
578 fwd ← (3 s̲elect k) − 5 s̲elect k
579 side ← (6 s̲elect k) − 4 s̲elect k
580 wet ← (1.0 − fly) × f̲loat 14.0 > 0.9 + 2 s̲elect x
581 speed ← (fly × 15.0) + (1.0 − fly) × (1.0 − 0.4 × wet) × 4.5 × 1.0 + 8 s̲elect k
582 vx ← speed × ((s̲in turn) × fwd) + (c̲os turn) × side
583 vz ← speed × ((0.0 − c̲os turn) × fwd) + (s̲in turn) × side
584 surface ← (1.0 − fly) × (f̲loat 14.5 > 2 s̲elect x) × f̲loat 13.0 < 2 s̲elect x
585 jump ← (1.0 − fly) × (1.0 m̲in g + surface × 1.0 − wet) × 7 s̲elect k
586 swim ← wet × 7 s̲elect k
587 fall ← -30.0 m̲ax (2 s̲elect v) − 22.0 × ˡdt
588 sink ← -2.0 m̲ax (2 s̲elect v) − 6.0 × ˡdt
589 walkVy ← (7.5 × jump) + (1.0 − jump) × (4.0 × swim) + (1.0 − swim) × (wet × sink) + (1.0 − wet) × fall
590 flyVy ← ((80.0 − 2 s̲elect x) × 60.0) m̲in 15.0 × (7 s̲elect k) − 8 s̲elect k
591 vy ← (fly × flyVy) + (1.0 − fly) × walkVy
592 p ← wo ʰm̲y wo ʰm̲z wo ʰm̲x (x, vx c̲at vy c̲at vz, turn, tilt, g, fly)
593 (x2, _, _, _, _, _) ← p
594 _ ← (ˡw̲indow s) ˢᶜe̲ye! (1 s̲elect x2) c̲at ((2 s̲elect x2) + 1.6) c̲at (3 s̲elect x2) c̲at turn c̲at tilt
595 (p, wo, jb, st)
596}
597
598⍝│ en:h_omeAgain s: the player back at the start, standing still,
599⍝│ walking
600ˡh̲omeAgain ← { s → (ˡs̲tart s, 0.0 0.0 0.0, 0.0, 0.0, 0.0, 0.0) ˡw̲ithPlayer s }
601
602⍝│ en:t_oggleFly s: flying, or walking again (falling from where it is)
603ˡt̲oggleFly ← { s →
604 ((x, _, yaw, pitch, _, fly), wo, jb, st) ← s
605 ((x, 0.0 0.0 0.0, yaw, pitch, 0.0, 1.0 − fly), wo, jb, st)
606}
607
608⍝│ en:s_top s: the state marked as finished
609ˡs̲top ← { (p, wo, jb, (w, nxt, x0, _, lit)) → (p, wo, jb, (w, nxt, x0, 0, lit)) }
610⍝│ en:s_topped s: whether the state is marked as finished
611ˡs̲topped ← { (_, _, _, (_, _, _, on, _)) → on = 0 }
612
613⍝━━ Light
614
615⍝│ A world can be lit (en:l_ighting): each column is then made with its
616⍝│ light (a third stage after its faces: vx:c_olumnLight, about 20 ms)
617⍝│ and its faces sent with their sunlight and lamplight; scene shades
618⍝│ each by the larger of sunlight times its daylight and lamplight. Day
619⍝│ and night (en:n_ight) change only scene's daylight, so the switch is
620⍝│ instant.
621
622⍝│ en:l_it s: 0 when the world is not lit, 1 by day, 2 by night
623ˡl̲it ← { (_, _, _, (_, _, _, _, lit)) → lit }
624
625⍝│ en:l_ighting s: the world lit (call it before columns are made), by
626⍝│ day
627ˡl̲ighting ← { (p, wo, jb, (w, nxt, x0, on, _)) →
628 _ ← w ˢᶜd̲aylight! 1.0
629 (p, wo, jb, (w, nxt, x0, on, 1))
630}
631
632⍝│ en:n_ight s: day becomes night (the sunlight and the sky gone, the
633⍝│ lamps left) and night day
634ˡn̲ight ← { (p, wo, jb, (w, nxt, x0, on, lit)) →
635 _ ← w ˢᶜd̲aylight! f̲loat lit = 2
636 (p, wo, jb, (w, nxt, x0, on, 3 − lit))
637}