sourceextensions/scene/demos/voxels-game.xtl
1⍝│ Voxels, 15: Gem Hunt. Ten gems are buried around the start, a few
2⍝│ blocks under the surface; dig them out in three minutes. The first
3⍝│ lies four blocks straight down from where you start; the others are
4⍝│ scattered within a dozen blocks, deeper. Gems glow a little (12 where
5⍝│ a lamp is 15), so a tunnel near one is lit -- and the corner of the
6⍝│ screen says how far the nearest gem you have not found is. Everything
7⍝│ from the voxel demos works: dig, place, lamps, water, night. G gives
8⍝│ up and shows the score. The game is X_eTaL: the gems are edits, a
9⍝│ find is a dig whose cell is a gem's, the clock is the frames (60 a
10⍝│ second), the labels are scene's.
11⍝│ Run: just demo scene voxels-game
12⍝│ click (or X) dig, right-click (or E) place, 1 to 8 choose the block
13⍝│ (8 a lamp), N night or day, W A S D walk, Space jump, F fly, drag or
14⍝│ the arrows to look, G give up, H back to the start, q quit
15
16"en:" u̲se< "Endless"
17"vx:" u̲se< "Voxels"
18"pl:" u̲se< "Play"
19"sc:" u̲se< "Scene"
20
21w ← "Voxels 15: Gem Hunt -- X_eTaL" ˢᶜo̲pen! 640 400
22g ← w ˢᶜf̲og! 22.0 44.0
23k ← w ˢᶜs̲ky! 0.55 0.72 0.92
24_ ← w ˢᶜc̲urve! 160.0
25
26"Gem Hunt: ten gems are buried near you. Dig them out in three minutes."
27"The first is four blocks straight down. Gems glow; the top of the screen says how far the nearest is."
28ᵖˡhelpLit
29"G: give up"
30
31⍝━━ The gems
32
33s ← ᵉⁿl̲ighting ᵉⁿn̲ew w
34x0 ← f̲loor ᵉⁿs̲tart s
35
36⍝ the ground's top block at column x z: one below the height
37ᵘt̲op ← { xz → (f̲irst r̲avel ᵛˣh̲eightsAt xz c̲at 1) − 1 }
38
39⍝│ u:m_ore gs: one more gem, at a random column within 12 blocks of the
40⍝│ start (not the start's own), 2 to 6 blocks under the ground's top,
41⍝│ never above the world's bottom
42ᵘm̲ore ← { gs →
43 o ← (r̲oll! 25 25) − 13
44 xz ← (1 3 s̲elect x0) + o
45 y ← 1 m̲ax (ᵘt̲op xz) − 1 + r̲oll! 5
46 gs c̲at 1 3 r̲eshape (1 s̲elect xz) c̲at y c̲at 2 s̲elect xz
47}
48
49⍝│ the first gem four blocks down under the start (its fourth dig), then
50⍝│ nine more
51gems ← 9 'ᵘm̲ore p̲ower 1 3 r̲eshape (1 s̲elect x0) c̲at ((ᵘt̲op 1 3 s̲elect x0) − 3) c̲at 3 s̲elect x0
52"the gems (x y z), the first under the start:"
53gems
54seeded ← (gems, 9) ᵉⁿs̲etAll s
55
56⍝━━ Playing
57
58⍝│ The game is a tuple: (the gems not found yet, n by 3; how many found;
59⍝│ the frames left; whether it is over; the edits seen so far). With the
60⍝│ world and its player and the chosen block it makes the state.
61
62minutes ← 3
63total ← 1 s̲elect s̲hape gems
64
65⍝ a cell's key, for finding gems among dug cells
66ᵘk̲ey ← { q → (200000 × (64 × 100000 + 1 s̲elect₂ q) + 2 s̲elect₂ q) + 100000 + 3 s̲elect₂ q }
67
68⍝ the time left as m:ss
69ᵘc̲lock ← { frames →
70 secs ← frames d̲iv 60
71 (f̲ormat secs d̲iv 60) c̲at ":" c̲at -2 t̲ake "0" c̲at f̲ormat secs m̲od 60
72}
73
74⍝ the labels: gems found, the time, how far the nearest gem is
75ᵘs̲how ← { (s, game) →
76 (gs, found, left, over, _) ← game
77 p ← ᵉⁿp̲osition s
78 _ ← ((f̲loat w) c̲at 1.0 16.0 14.0 14.0 1.0 1.0 1.0) ˢᶜl̲abel! "Gems " c̲at (f̲ormat found) c̲at " of " c̲at f̲ormat total
79 _ ← ((f̲loat w) c̲at 2.0 200.0 14.0 14.0 1.0 1.0 0.6) ˢᶜl̲abel! "Time " c̲at ᵘc̲lock left
80 hint ← (0 < t̲ally gs) ᵘn̲ear (gs, p)
81 _ ← ((f̲loat w) c̲at 3.0 340.0 14.0 14.0 0.6 1.0 1.0) ˢᶜl̲abel! hint
82 game
83}
84ᵘn̲ear ← { yes gp →
85 (gs, p) ← gp
86 0 = yes ? ""
87 d ← ((f̲loat gs) + 0.5) − ((t̲ally gs) c̲at 3) r̲eshape p
88 "Nearest gem " c̲at (f̲ormat f̲loor 0.5 + ('m̲in r̲/ '+ r̲/₂ d × d) ^ 0.5) c̲at " blocks"
89}
90
91⍝ the end: the score, big, across the middle
92ᵘe̲nd ← { game →
93 (gs, found, left, over, seen) ← game
94 over ? game
95 m ← (0 = t̲ally gs) ᵘv̲erdict (found, left)
96 _ ← p̲rint! "game over: " c̲at m
97 _ ← ((f̲loat w) c̲at 4.0 c̲at (320.0 − 1.5 × f̲loat (6 × t̲ally m) − 1) c̲at 170.0 21.0 1.0 0.85 0.3) ˢᶜl̲abel! m
98 (gs, found, left, 1, seen)
99}
100ᵘv̲erdict ← { all fl →
101 (found, left) ← fl
102 all ? "All " c̲at (f̲ormat total) c̲at " gems with " c̲at (ᵘc̲lock left) c̲at " to spare!"
103 left = 0 ? (f̲ormat found) c̲at " of " c̲at (f̲ormat total) c̲at " gems -- time is up"
104 (f̲ormat found) c̲at " of " c̲at (f̲ormat total) c̲at " gems -- stopped with " c̲at (ᵘc̲lock left) c̲at " left"
105}
106
107⍝ the gems found this frame: dug cells (the newest edits, block 0) that
108⍝ are gems' cells
109ᵘf̲inds ← { (s, game) →
110 (gs, found, left, over, seen) ← game
111 ed ← ᵉⁿe̲dits s
112 n ← t̲ally ed
113 new ← (n − seen) t̲ake ed
114 hit ← (ᵘk̲ey gs) m̲ember? ᵘk̲ey (0 = 4 s̲elect₂ new) r̲eplicate 1 2 3 s̲elect₂ new
115 k ← '+ r̲/ hit
116 said ← (k > 0) ᵘt̲ell (hit r̲eplicate gs, found + k)
117 ((0 = hit) r̲eplicate gs, found + k, left, over, n)
118}
119ᵘt̲ell ← { yes cf →
120 (cells, found) ← cf
121 0 = yes ? ""
122 p̲rint! "found a gem at " c̲at (f̲ormat r̲avel cells) c̲at " (" c̲at (f̲ormat found) c̲at " of " c̲at (f̲ormat total) c̲at ")"
123}
124
125⍝│ one frame: the world's frame (pl:f_rameV), then the game's turn and
126⍝│ its labels
127ᵘf̲rame ← { (s, sel, game) →
128 ᵉⁿs̲topped s ? (s, sel, game)
129 (world, held, v) ← ᵖˡf̲rameV (s, sel)
130 turned ← ᵘt̲urn (world, v, game)
131 (world, held, ᵘs̲how (world, turned))
132}
133
134⍝│ the game's turn: the gems found, the clock, and the end when the
135⍝│ time is up, all are found or G is pressed (once over, it stays over)
136ᵘt̲urn ← { (s, v, game) →
137 (gs, found, left, over, seen) ← game
138 over ? game
139 checked ← ᵘf̲inds (s, game)
140 (gemsLeft, foundNow, ticks, overNow, seenNow) ← checked
141 ticksLeft ← 0 m̲ax ticks − 1
142 after ← (gemsLeft, foundNow, ticksLeft, overNow, seenNow)
143 done ← (0 = ticksLeft) + (0 = t̲ally gemsLeft) + v m̲atch "key g"
144 0 < done ? ᵘe̲nd after
145 after
146}
147
148hud ← w ᵖˡh̲udLit 1
149streamed ← (4 × (1 + 2 × ᵉⁿreach) × 1 + 2 × ᵉⁿreach) 'ᵉⁿs̲tream p̲ower seeded
150game ← (gems, 0, minutes × 60 × 60, 0, t̲ally ᵉⁿe̲dits streamed)
151last ← 100000 'ᵘf̲rame p̲ower (streamed, 1, game)
152z ← ˢᶜc̲lose! w
153⍝ (accessors, not top-level tuple patterns: ask E10)
154ᵘg̲ameOf ← { (_, _, game) → game }
155ᵘf̲oundOf ← { (_, found, _, _, _) → found }
156"gems found:"
157ᵘf̲oundOf ᵘg̲ameOf last
158"bye"