Sign in
world

Moonsilver Vaults

by Potluck · version 1 · Checked for all ages

Far down in the slate, where the moonsilver runs, the old stone people built vaults: pillared halls of pale moonstone, lit by lanterns that have never gone out, with veins of moonsilver in the walls that Stone Sentinels still guard. At the far end a moon seal closes the sanctum. It has a round hollow the size of a sentinel's core. Inside lies the best gear on the island: moonsilver, a moonblade and a moon crown, and now and then a star chart for the Curator.

1--!strict
2-- Moonsilver Vaults: far down in the slate, where the moonsilver runs, the
3-- old stone people built vaults. When a player goes deep enough, a vault
4-- opens in the rock near them with a passage to where they are: a pillared
5-- hall floored with moonstone, lit by lanterns in its walls, with veins
6-- of moonsilver ore in the slate around it. The Stone Sentinels creation
7-- stands its guards by moonsilver ore near players this deep, so the
8-- vault's veins are guarded too.
9--
10-- At the hall's far end a moon seal closes the sanctum, in moonstone no
11-- pickaxe cuts. Use the seal with a sentinel core in hand (what a beaten
12-- sentinel leaves) and it rolls away, for good; on a Peaceful world, where
13-- no sentinels stand guard, three moonsilver ingots do instead. The coffer
14-- inside holds the best gear on the island: moonsilver, a moonblade (a
15-- weapon of this creation's, the hardest hitting there is), a moon crown
16-- (armor for the head that guards best and keeps the cold off) and now and
17-- then a star chart, a relic that the Curator on the Green collects.
18--
19-- Where each vault is, whether its seal is open and whether its coffer
20-- still holds something are kept with @potluck/store, so each is built
21-- once. Only natural stone is dug.
22
23local world = require("@potluck/world@0.33")
24local tweak = require("@potluck/tweak")
25local store = require("@potluck/store")
26local ui = require("@potluck/ui@0.2")
27local sound = require("@potluck/sound@0.2")
28
29local knobs = tweak {
30 apart = { 300, min = 100, max = 1200, label = "How far apart vaults are", order = 1 },
31 prizes = { 1, min = 0.5, max = 3, label = "How much is in the coffers", order = 2 },
32 veins = { 0.08, min = 0, max = 0.3, label = "Moonsilver in the walls", order = 3 },
33}
34
35local STONE, SEAL, COFFER, LAMP = "Moonstone", "Moon seal", "Moonsilver coffer", "Lantern"
36local CORE, INGOT = "Sentinel core", "Moonsilver ingot"
37local MOST = 32 -- vaults a world keeps track of
38local RX, RZ, TOP = 4, 7, 5 -- the hall's half width and length, and its top, from its middle on the floor
39local BELOW = -82 -- vaults open only below this
40local DEEP = 8 -- and with this much rock over the player
41
42-- what may be dug
43local NATURAL: { [string]: boolean } = {
44 Stone = true, Dirt = true, Gravel = true, Sand = true, Slate = true, Sandstone = true, Cobblestone = true,
45 ["Coal ore"] = true, ["Copper ore"] = true, ["Iron ore"] = true, ["Gold ore"] = true, ["Crystal ore"] = true, ["Moonsilver ore"] = true,
46 Glowcap = true,
47}
48local UP = vector.create(0, 1, 0)
49
50-- ---- what's in the coffers ----
51
52type Prize = { thing: string, chance: number, least: number, most: number }
53local PRIZES: { Prize } = {
54 { thing = "Moonsilver ingot", chance = 0.9, least = 3, most = 6 },
55 { thing = "Raw moonsilver", chance = 0.5, least = 2, most = 4 },
56 { thing = "Moonblade", chance = 0.6, least = 1, most = 1 },
57 { thing = "Moon crown", chance = 0.45, least = 1, most = 1 },
58 { thing = "Moonsilver pickaxe", chance = 0.3, least = 1, most = 1 },
59 { thing = "Moonsilver coat", chance = 0.2, least = 1, most = 1 },
60 { thing = "Moonsilver leggings", chance = 0.2, least = 1, most = 1 },
61 { thing = "Moonsilver boots", chance = 0.2, least = 1, most = 1 },
62 { thing = "Gold ingot", chance = 0.5, least = 2, most = 4 },
63 { thing = "Crystal", chance = 0.4, least = 2, most = 5 },
64 { thing = "Star chart", chance = 0.5, least = 1, most = 1 },
65 { thing = "Moonsilver music box", chance = 0.4, least = 1, most = 1 },
66}
67local SEAL_XP, COFFER_XP = 15, 20
68local PEACEFUL_INGOTS = 3
69
70-- ---- what's kept ----
71
72-- each vault: its hall's middle on the floor, whether it's built and its
73-- seal open, whether its coffer still holds something, and while it's
74-- being built, the way in
75type Vault = { at: vector, built: boolean, open: boolean, coffer: boolean, way: { vector }? }
76local vaults: { Vault } = store.get("vaults") or {}
77
78local function save()
79 store.set("vaults", vaults)
80end
81
82local function key(at: vector): string
83 return string.format("%d,%d,%d", math.floor(at.x), math.floor(at.y), math.floor(at.z))
84end
85
86-- Whether a thing is in this world: old coins are the Curator's.
87local here: { [string]: boolean } = {}
88local function exists(name: string): boolean
89 if here[name] == nil then
90 here[name] = (pcall(world.kind, name))
91 end
92 return here[name] == true
93end
94
95-- the same number every time, for a place
96local function hash(x: number, y: number, z: number): number
97 local h = (math.floor(x) * 73856093 + math.floor(y) * 19349663 + math.floor(z) * 83492791) % 1000003
98 return (h * 7919 % 1000003) / 1000003
99end
100
101local shown: { [number]: number } = {}
102local function tell(player, text: string)
103 ui.show(player, "vault", text, { at = "bottom" })
104 local mine = (shown[player.id] or 0) + 1
105 shown[player.id] = mine
106 world.after(5, function()
107 if shown[player.id] == mine then
108 shown[player.id] = nil
109 ui.hide(player, "vault")
110 end
111 end)
112end
113
114local function flat(a: vector, b: vector): number
115 local dx, dz = a.x - b.x, a.z - b.z
116 return math.sqrt(dx * dx + dz * dz)
117end
118
119local function v(x: number, y: number, z: number): vector
120 return vector.create(x, y, z)
121end
122
123-- ---- where things are in a vault ----
124
125-- the seal (two blocks high, in the hall's north wall), and the coffer
126-- behind it
127local function seal_cells(w: Vault): { vector }
128 return { w.at + v(0, 0, -RZ - 1), w.at + v(0, 1, -RZ - 1) }
129end
130local function coffer_at(w: Vault): vector
131 return w.at + v(0, 0, -RZ - 3)
132end
133
134-- inside the hall, from its middle on the floor
135local function inside(x: number, y: number, z: number): boolean
136 return math.abs(x) <= RX and math.abs(z) <= RZ and y >= 0 and y <= TOP
137end
138
139-- ---- finding a place ----
140
141-- Whether a vault and its sanctum fit around `c`: deep in natural rock (a
142-- cave through it is fine), with nothing built and no water.
143local function fits(c: vector): boolean
144 if world.top(c.x, c.z) < c.y + TOP + DEEP then
145 return false
146 end
147 local air, n = 0, 0
148 for _, dx in { -RX - 1, -2, 0, 2, RX + 1 } do
149 for _, dz in { -RZ - 5, -RZ - 1, -3, 0, 3, RZ + 1 } do
150 for _, dy in { -1, 2, TOP + 1 } do
151 local b = world.block(c + v(dx, dy, dz))
152 n += 1
153 if b == "Air" then
154 air += 1
155 elseif not NATURAL[b] then
156 return false
157 end
158 end
159 end
160 end
161 return air * 3 <= n
162end
163
164-- The way from a vault's middle out to where a player is: the cells to
165-- dig, up to the first open air outside it, or nil if it's built or wet in
166-- between, or too far.
167local function passage(c: vector, to: vector): { vector }?
168 local d = to - c
169 local len = vector.magnitude(d)
170 if len < 1 then
171 return nil
172 end
173 local step = d / len
174 local out = {}
175 for t = 0, 40 do
176 local at = c + step * t
177 local y = c.y + (to.y - c.y) * math.clamp((t - RZ) / math.max(len - RZ, 1), 0, 1)
178 local cell = v(math.floor(at.x), math.floor(y), math.floor(at.z))
179 local rel = cell - c
180 if not inside(rel.x, rel.y, rel.z) then
181 local b, above = world.block(cell), world.block(cell + UP)
182 if b == "Air" and above == "Air" then
183 return out
184 end
185 if (b ~= "Air" and not NATURAL[b]) or (above ~= "Air" and not NATURAL[above]) then
186 return nil
187 end
188 table.insert(out, cell)
189 end
190 end
191 return nil
192end
193
194-- A place for a vault near a player far down, north of them so the way in
195-- comes from the south and the sanctum's behind, and the way to it.
196local function look_near(p): (vector?, { vector }?)
197 for _ = 1, 8 do
198 local a = world.random(math.pi + 0.6, 2 * math.pi - 0.6)
199 local r = world.random(16, 22)
200 local c = v(
201 math.floor(p.position.x + math.cos(a) * r),
202 math.floor(p.position.y + world.random(-2, 1)),
203 math.floor(p.position.z + math.sin(a) * r)
204 )
205 if c.y - 1 > -127 and fits(c) then
206 local way = passage(c, p.position)
207 if way then
208 return c, way
209 end
210 end
211 end
212 return nil, nil
213end
214
215local function far_from_others(pos: vector, apart: number): boolean
216 for _, w in vaults do
217 if flat(w.at, pos) < apart then
218 return false
219 end
220 end
221 return true
222end
223
224-- ---- building it ----
225
226-- what's left to put down: a place and a block ("" to dig)
227type Work = { at: vector, name: string }
228local work: { Work } = {}
229local done = 0
230local building: Vault? = nil
231-- what it may put its own blocks over while it builds
232local OURS: { [string]: boolean } = { Air = true, [STONE] = true, Column = true }
233
234local function plan(w: Vault)
235 local c = w.at
236 work, done = {}, 0
237 local function put(at: vector, name: string)
238 table.insert(work, { at = at, name = name })
239 end
240 -- the hall, floored with moonstone and walled in slate veined with
241 -- moonsilver
242 for x = -RX - 1, RX + 1 do
243 for z = -RZ - 1, RZ + 1 do
244 for y = -1, TOP + 1 do
245 local at = c + v(x, y, z)
246 if inside(x, y, z) then
247 put(at, "")
248 elseif y == -1 then
249 put(at, STONE)
250 elseif hash(at.x, at.y, at.z) < knobs.veins then
251 put(at, "Moonsilver ore")
252 else
253 put(at, "Slate")
254 end
255 end
256 end
257 end
258 -- pillars down each side, and lamps in the walls between them
259 for _, x in { -3, 3 } do
260 for _, z in { -4, 0, 4 } do
261 for y = 0, TOP do
262 put(c + v(x, y, z), "Column")
263 end
264 end
265 end
266 for _, x in { -RX - 1, RX + 1 } do
267 for _, z in { -6, -2, 2, 6 } do
268 put(c + v(x, 3, z), LAMP)
269 end
270 end
271 -- the sanctum behind the north wall, all in moonstone
272 for x = -2, 2 do
273 for z = -RZ - 5, -RZ - 1 do
274 for y = -1, 3 do
275 local hollow = math.abs(x) <= 1 and z >= -RZ - 4 and z <= -RZ - 2 and y >= 0 and y <= 2
276 put(c + v(x, y, z), if hollow then "" else STONE)
277 end
278 end
279 end
280 for _, at in seal_cells(w) do
281 put(at, SEAL)
282 end
283 put(coffer_at(w), COFFER)
284 put(coffer_at(w) + v(0, 2, 0), LAMP)
285 -- the way in
286 for i, cell in w.way or {} do
287 put(cell, "")
288 put(cell + UP, "")
289 if i <= 3 then
290 put(cell - UP, STONE)
291 end
292 end
293end
294
295-- puts down a few hundred blocks a tick
296local function step()
297 local last = math.min(done + 250, #work)
298 for i = done + 1, last do
299 local w = work[i]
300 local b = world.block(w.at)
301 if w.name == "" then
302 if NATURAL[b] or OURS[b] then
303 world.set_block(w.at, "Air")
304 end
305 elseif NATURAL[b] or OURS[b] then
306 world.set_block(w.at, w.name)
307 end
308 end
309 done = last
310 if done >= #work then
311 work, done = {}, 0
312 local w = building
313 if w then
314 w.built = true
315 w.way = nil
316 building = nil
317 save()
318 end
319 end
320end
321
322-- ---- the seal ----
323
324local function vault_at(pos: vector, reach: number): Vault?
325 for _, w in vaults do
326 if vector.magnitude(pos - w.at) < reach then
327 return w
328 end
329 end
330 return nil
331end
332
333local function open(w: Vault, player)
334 w.open = true
335 save()
336 for _, at in seal_cells(w) do
337 if world.block(at) == SEAL then
338 world.set_block(at, "Air")
339 end
340 end
341 sound.play("seal", { at = seal_cells(w)[1] })
342 world.reward(player, SEAL_XP)
343 tell(player, "The moon seal rolls away.")
344end
345
346-- nothing cuts the moonstone or the seal
347world.drops(STONE, { gives = false, level = 9 })
348world.drops(SEAL, { gives = false, level = 9 })
349world.drops(COFFER, { gives = false })
350
351-- ---- the coffer ----
352
353type Find = { thing: string, n: number }
354-- what's still in coffers players' pockets were too full for, by vault
355local leftover: { [string]: { Find } } = {}
356
357local function roll(): { Find }
358 local out: { Find } = {}
359 for _, p in PRIZES do
360 if exists(p.thing) and world.random() < math.min(1, p.chance * knobs.prizes) then
361 local n = math.floor(world.random(p.least, p.most + 0.999) * knobs.prizes)
362 table.insert(out, { thing = p.thing, n = math.clamp(n, 1, if p.most == 1 then 1 else 64) })
363 end
364 end
365 if #out == 0 then
366 table.insert(out, { thing = INGOT, n = 3 })
367 end
368 return out
369end
370
371local function say(finds: { Find }): string
372 local parts = {}
373 for _, f in finds do
374 table.insert(parts, f.n .. " " .. string.lower(f.thing))
375 end
376 return table.concat(parts, ", ")
377end
378
379-- Gives what's in a coffer; true when it's all given.
380local function rummage(player, w: Vault): boolean
381 local k = key(w.at)
382 local finds = leftover[k] or roll()
383 local got, still = {}, {}
384 for _, f in finds do
385 local n = world.give(player, f.thing, f.n)
386 if n > 0 then
387 table.insert(got, { thing = f.thing, n = n })
388 end
389 if n < f.n then
390 table.insert(still, { thing = f.thing, n = f.n - n })
391 end
392 end
393 if #got > 0 then
394 ui.ask(player, { title = "The moonsilver coffer", text = "You found " .. say(got) .. "." }, function() end)
395 end
396 if #still > 0 then
397 leftover[k] = still
398 tell(player, "Your pockets are full.")
399 return false
400 end
401 leftover[k] = nil
402 w.coffer = false
403 save()
404 world.reward(player, COFFER_XP)
405 sound.play("sparkle", { at = coffer_at(w), caption = "Treasure in the sanctum" })
406 tell(player, "The old stone people's treasure is yours.")
407 return true
408end
409
410world.on("block_used", function(player, at: vector, block: string)
411 if block ~= SEAL and block ~= COFFER then
412 return
413 end
414 local w = vault_at(at, 16)
415 if block == SEAL then
416 if not w or w.open then
417 return
418 end
419 if world.pockets(player) == nil then
420 tell(player, "A round hollow, the size of a sentinel core.")
421 return
422 end
423 if world.held(player) == CORE and world.take(player, CORE, 1) then
424 open(w, player)
425 elseif world.danger() == "peaceful" and world.held(player) == INGOT and world.has(player, INGOT) >= PEACEFUL_INGOTS then
426 world.take(player, INGOT, PEACEFUL_INGOTS)
427 open(w, player)
428 elseif world.danger() == "peaceful" then
429 tell(player, "It wants three moonsilver ingots, or a sentinel core.")
430 else
431 tell(player, "A round hollow, the size of a sentinel core.")
432 end
433 return
434 end
435 if world.pockets(player) == nil then
436 tell(player, "Moonsilver vaults hold prizes for survival.")
437 elseif w and w.coffer and key(at) == key(coffer_at(w)) then
438 rummage(player, w)
439 else
440 tell(player, "An empty coffer.")
441 end
442end)
443
444world.on("block_broken", function(player, at: vector, block: string)
445 if block ~= COFFER then
446 return
447 end
448 local w = vault_at(at, 16)
449 if w and w.coffer and key(at) == key(coffer_at(w)) and world.pockets(player) ~= nil then
450 rummage(player, w)
451 w.coffer = false
452 leftover[key(w.at)] = nil
453 save()
454 end
455end)
456
457-- ---- watching players ----
458
459local told: { [string]: boolean } = {}
460local turn = 0
461world.every(0.1, function()
462 if #work > 0 then
463 step()
464 return
465 end
466 turn += 1
467 if turn % 5 == 0 then
468 for i, w in vaults do
469 if w.built then
470 for _, p in world.players() do
471 local k = p.name .. "/" .. i
472 if not told[k] and vector.magnitude(p.position - w.at) < RZ + 2 then
473 told[k] = true
474 -- not over something they've just been told
475 if shown[p.id] then
476 continue
477 end
478 tell(p, if w.open then "A moonsilver vault. Its seal stands open." else "A moonsilver vault. The seal at its end is shut.")
479 end
480 end
481 end
482 end
483 end
484 if turn % 20 ~= 0 then
485 return
486 end
487 local players = world.players()
488 if #players == 0 or #vaults >= MOST then
489 return
490 end
491 local p = players[(turn // 20) % #players + 1]
492 local pos = p.position
493 -- the Long Dusk has finds of its own
494 if world.place(pos) == "dusk" then
495 return
496 end
497 -- only far down, under plenty of rock
498 if pos.y > BELOW or world.top(pos.x, pos.z) < pos.y + DEEP then
499 return
500 end
501 if not far_from_others(pos, knobs.apart) then
502 return
503 end
504 local c, way = look_near(p)
505 if c and way then
506 local w: Vault = { at = c, built = false, open = false, coffer = true, way = way }
507 table.insert(vaults, w)
508 building = w
509 plan(w)
510 save()
511 end
512end)
513
514-- a vault left half built (the world stopped mid-way) is built on
515for _, w in vaults do
516 if not w.built then
517 building = w
518 plan(w)
519 break
520 end
521end
522
523world.on("player_left", function(p)
524 shown[p.id] = nil
525end)