Sign in
world

Lost Workshops

by Potluck · version 1 · Checked for all ages

Long ago tinkerers worked down in the rock, and some of their workshops are still there. Wander deep enough underground and you may find one: bookshelves, a workbench, a tinker's pot and a vault in riveted iron that no pickaxe can cut. The vault opens with power. The old wire to it is broken, so mend it and pull the lever. Inside are ingots, circuit parts, tools and now and then a clockwork bird for the Curator.

1--!strict
2-- Lost Workshops: long ago tinkerers worked down in the rock. When a player
3-- wanders deep enough underground, an old workshop opens in the stone near
4-- them with a low passage to where they are: mossy brick walls, a plank
5-- floor under oak beams, bookshelves, a table with the tinker's pot on it,
6-- crates, a candle, and on the far wall a vault in riveted iron that no
7-- pickaxe can cut.
8--
9-- The vault opens with power: a live wire, a pulled lever, or a lit
10-- inverter or delay facing it, beside its door (the blocks the Circuits
11-- creation makes, read by name as Rails reads them). A lever stands in the
12-- workshop with an old copper wire from it to the vault door, broken in
13-- two places; the tinker's pot holds a few pieces of wire to mend it, and
14-- a pair of tinker's goggles. Mend it, pull the lever, and the door swings
15-- open, for good. The vault chest inside holds ingots, circuit parts, a
16-- tool or two and now and then a clockwork bird, a relic (an item of this
17-- creation's) that the Curator on the Green collects. Use the bird in hand
18-- and it sings.
19--
20-- Where each workshop is, whether its vault is open and what's left in its
21-- box and chest are kept with @potluck/store, so each is built once. Only
22-- natural stone is dug.
23
24local world = require("@potluck/world@0.33")
25local tweak = require("@potluck/tweak")
26local store = require("@potluck/store")
27local ui = require("@potluck/ui@0.2")
28local sound = require("@potluck/sound@0.2")
29
30local knobs = tweak {
31 apart = { 240, min = 80, max = 1000, label = "How far apart workshops are", order = 1 },
32 prizes = { 1, min = 0.5, max = 3, label = "How much is in the vaults", order = 2 },
33 gaps = { 2, min = 0, max = 4, label = "Breaks in the old wire", order = 3 },
34}
35
36-- the tinker's pot is a plain clay pot on the table, known by where it is
37-- (a crate would be Storage's)
38local IRON, DOOR, CHEST, BOX = "Riveted iron", "Vault door", "Vault chest", "Clay pot"
39local WALL, FLOOR, BEAM = "Mossy brick", "Planks", "Oak log"
40local MOST = 48 -- workshops a world keeps track of
41local RX, RZ, TOP = 5, 4, 3 -- the room's half width and depth, and its top, from its middle on the floor
42local DEEP = 6 -- how much rock is over a player before a workshop opens near them
43
44-- what may be dug
45local NATURAL: { [string]: boolean } = {
46 Stone = true, Dirt = true, Gravel = true, Sand = true, Slate = true, Sandstone = true, Cobblestone = true,
47 ["Coal ore"] = true, ["Copper ore"] = true, ["Iron ore"] = true, ["Gold ore"] = true, ["Crystal ore"] = true, ["Moonsilver ore"] = true,
48 Glowcap = true,
49}
50
51-- The way each facing points, and power that comes through a block.
52local AHEAD: { [string]: vector } = {
53 north = vector.create(0, 0, -1),
54 east = vector.create(1, 0, 0),
55 south = vector.create(0, 0, 1),
56 west = vector.create(-1, 0, 0),
57}
58local SIDES = {
59 vector.create(1, 0, 0), vector.create(-1, 0, 0), vector.create(0, 1, 0),
60 vector.create(0, -1, 0), vector.create(0, 0, 1), vector.create(0, 0, -1),
61}
62local UP = vector.create(0, 1, 0)
63
64-- ---- what's in the vaults ----
65
66type Prize = { thing: string, chance: number, least: number, most: number }
67local PRIZES: { Prize } = {
68 { thing = "Iron ingot", chance = 0.8, least = 2, most = 4 },
69 { thing = "Copper ingot", chance = 0.7, least = 3, most = 6 },
70 { thing = "Gold ingot", chance = 0.5, least = 1, most = 3 },
71 { thing = "Moonsilver ingot", chance = 0.2, least = 1, most = 1 },
72 { thing = "Piston", chance = 0.4, least = 1, most = 2 },
73 { thing = "Note block", chance = 0.4, least = 1, most = 3 },
74 { thing = "Copper lamp", chance = 0.4, least = 2, most = 3 },
75 { thing = "Delay", chance = 0.3, least = 1, most = 2 },
76 { thing = "Hammer", chance = 0.2, least = 1, most = 1 },
77 { thing = "Iron pickaxe", chance = 0.15, least = 1, most = 1 },
78 { thing = "Old coin", chance = 0.3, least = 1, most = 3 },
79 { thing = "Clockwork bird", chance = 0.45, least = 1, most = 1 },
80 { thing = "Brass music box", chance = 0.3, least = 1, most = 1 },
81}
82local VAULT_XP, CHEST_XP = 10, 8
83local WIRE_IN_BOX = 3
84
85-- ---- what's kept ----
86
87-- each workshop: its middle on the floor, which way its lever stands (1
88-- east, -1 west), whether it's built and its vault open, whether its box
89-- and chest still hold something, and while it's being built, the way in
90type Workshop = { at: vector, side: number, built: boolean, open: boolean, box: boolean, chest: boolean, way: { vector }? }
91local workshops: { Workshop } = store.get("workshops") or {}
92
93local function save()
94 store.set("workshops", workshops)
95end
96
97local function key(at: vector): string
98 return string.format("%d,%d,%d", math.floor(at.x), math.floor(at.y), math.floor(at.z))
99end
100
101-- Whether a thing is in this world: wire and levers are the Circuits
102-- creation's, old coins the Curator's.
103local here: { [string]: boolean } = {}
104local function exists(name: string): boolean
105 if here[name] == nil then
106 here[name] = (pcall(world.kind, name))
107 end
108 return here[name] == true
109end
110
111-- the same number every time, for a place
112local function hash(x: number, y: number, z: number): number
113 local h = (math.floor(x) * 73856093 + math.floor(y) * 19349663 + math.floor(z) * 83492791) % 1000003
114 return (h * 7919 % 1000003) / 1000003
115end
116
117local shown: { [number]: number } = {}
118local function tell(player, text: string)
119 ui.show(player, "workshop", text, { at = "bottom" })
120 local mine = (shown[player.id] or 0) + 1
121 shown[player.id] = mine
122 world.after(5, function()
123 if shown[player.id] == mine then
124 shown[player.id] = nil
125 ui.hide(player, "workshop")
126 end
127 end)
128end
129
130local function flat(a: vector, b: vector): number
131 local dx, dz = a.x - b.x, a.z - b.z
132 return math.sqrt(dx * dx + dz * dz)
133end
134
135-- ---- where things are in a workshop ----
136
137local function v(x: number, y: number, z: number): vector
138 return vector.create(x, y, z)
139end
140
141-- the vault's door (two blocks high, in the north wall), and its chest
142local function door_cells(w: Workshop): { vector }
143 return { w.at + v(0, 0, -RZ - 1), w.at + v(0, 1, -RZ - 1) }
144end
145local function chest_at(w: Workshop): vector
146 return w.at + v(0, 0, -RZ - 3)
147end
148local function box_at(w: Workshop): vector
149 return w.at + v(-3 * w.side, 1, 3)
150end
151local function lever_at(w: Workshop): vector
152 return w.at + v(4 * w.side, 0, 2)
153end
154
155-- the old wire, from the lever to the door, a cell at a time
156local function wire_cells(w: Workshop): { vector }
157 local out = {}
158 for x = 3, 1, -1 do
159 table.insert(out, w.at + v(x * w.side, 0, 2))
160 end
161 for z = 2, -RZ, -1 do
162 table.insert(out, w.at + v(0, 0, z))
163 end
164 return out
165end
166
167-- which of them are broken: the same every time for a workshop, never the
168-- first, and never two side by side
169local function broken(w: Workshop): { [number]: boolean }
170 local cells = wire_cells(w)
171 local out: { [number]: boolean } = {}
172 local n, tries = 0, 0
173 while n < math.floor(knobs.gaps) and tries < 40 do
174 tries += 1
175 local i = 2 + math.floor(hash(w.at.x + tries * 11, w.at.y, w.at.z - tries * 5) * (#cells - 1))
176 if not out[i] and not out[i - 1] and not out[i + 1] then
177 out[i] = true
178 n += 1
179 end
180 end
181 return out
182end
183
184-- inside the room, from its middle on the floor
185local function inside(x: number, y: number, z: number): boolean
186 return math.abs(x) <= RX and math.abs(z) <= RZ and y >= 0 and y <= TOP
187end
188
189-- ---- finding a place ----
190
191-- Whether a workshop and its vault fit around `c`: deep in natural rock (a
192-- cave through it is fine), with nothing built and no water.
193local function fits(c: vector): boolean
194 if world.top(c.x, c.z) < c.y + TOP + DEEP then
195 return false
196 end
197 local air, n = 0, 0
198 for _, dx in { -RX - 1, -3, 0, 3, RX + 1 } do
199 for _, dz in { -RZ - 5, -RZ - 1, 0, RZ + 1 } do
200 for _, dy in { -1, 1, TOP + 1 } do
201 local b = world.block(c + v(dx, dy, dz))
202 n += 1
203 if b == "Air" then
204 air += 1
205 elseif not NATURAL[b] then
206 return false
207 end
208 end
209 end
210 end
211 return air * 3 <= n
212end
213
214-- The way from a workshop's middle out to where a player is: the cells to
215-- dig, up to the first open air outside it, or nil if it's built or wet in
216-- between, or too far.
217local function passage(c: vector, to: vector): { vector }?
218 local d = to - c
219 local len = vector.magnitude(d)
220 if len < 1 then
221 return nil
222 end
223 local step = d / len
224 local out = {}
225 for t = 0, 40 do
226 local at = c + step * t
227 local y = c.y + (to.y - c.y) * math.clamp((t - RX) / math.max(len - RX, 1), 0, 1)
228 local cell = v(math.floor(at.x), math.floor(y), math.floor(at.z))
229 local rel = cell - c
230 if not inside(rel.x, rel.y, rel.z) then
231 local b, above = world.block(cell), world.block(cell + UP)
232 if b == "Air" and above == "Air" then
233 return out
234 end
235 if (b ~= "Air" and not NATURAL[b]) or (above ~= "Air" and not NATURAL[above]) then
236 return nil
237 end
238 table.insert(out, cell)
239 end
240 end
241 return nil
242end
243
244-- A place for a workshop near a player deep underground, north of them so
245-- the way in comes from the south and the vault's behind, and the way to it.
246local function look_near(p): (vector?, { vector }?)
247 for _ = 1, 8 do
248 local a = world.random(math.pi + 0.5, 2 * math.pi - 0.5)
249 local r = world.random(12, 18)
250 local c = v(
251 math.floor(p.position.x + math.cos(a) * r),
252 math.floor(p.position.y + world.random(-2, 1)),
253 math.floor(p.position.z + math.sin(a) * r)
254 )
255 if fits(c) then
256 local way = passage(c, p.position)
257 if way then
258 return c, way
259 end
260 end
261 end
262 return nil, nil
263end
264
265local function far_from_others(pos: vector, apart: number): boolean
266 for _, w in workshops do
267 if flat(w.at, pos) < apart then
268 return false
269 end
270 end
271 return true
272end
273
274-- ---- building it ----
275
276-- what's left to put down: a place and a block ("" to dig)
277type Work = { at: vector, name: string }
278local work: { Work } = {}
279local done = 0
280local building: Workshop? = nil
281-- what it may put its own blocks over while it builds
282local OURS: { [string]: boolean } = { Air = true, [WALL] = true, [FLOOR] = true, [BEAM] = true }
283
284local function plan(w: Workshop)
285 local c = w.at
286 local s = w.side
287 work, done = {}, 0
288 local function put(at: vector, name: string)
289 table.insert(work, { at = at, name = name })
290 end
291 -- the room, in its shell
292 for x = -RX - 1, RX + 1 do
293 for z = -RZ - 1, RZ + 1 do
294 for y = -1, TOP + 1 do
295 if inside(x, y, z) then
296 put(c + v(x, y, z), "")
297 elseif y == -1 then
298 put(c + v(x, y, z), FLOOR)
299 elseif y == TOP + 1 then
300 put(c + v(x, y, z), if x % 3 == 0 then BEAM else WALL)
301 else
302 put(c + v(x, y, z), WALL)
303 end
304 end
305 end
306 end
307 -- the vault behind the north wall, all in riveted iron
308 for x = -2, 2 do
309 for z = -RZ - 5, -RZ - 1 do
310 for y = -1, 2 do
311 local hollow = math.abs(x) <= 1 and z >= -RZ - 4 and z <= -RZ - 2 and y >= 0 and y <= 1
312 put(c + v(x, y, z), if hollow then "" else IRON)
313 end
314 end
315 end
316 for _, at in door_cells(w) do
317 put(at, DOOR)
318 end
319 put(chest_at(w), CHEST)
320 -- the way in
321 for i, cell in w.way or {} do
322 put(cell, "")
323 put(cell + UP, "")
324 if i <= 3 then
325 put(cell - UP, FLOOR)
326 end
327 end
328 -- what's in it
329 for z = -3, -1 do
330 for y = 0, 1 do
331 put(c + v(-RX * s, y, z), "Bookshelf")
332 end
333 end
334 put(c + v(-3 * s, 0, 3), "Table")
335 put(box_at(w), BOX)
336 put(c + v(-2 * s, 0, 3), "Stool")
337 put(c + v(-4 * s, 0, 3), "Stool")
338 put(c + v(RX * s, 0, -RZ), "Crate")
339 put(c + v(RX * s, 1, -RZ), "Crate")
340 put(c + v((RX - 1) * s, 0, -RZ), "Barrel")
341 put(c + v((RX - 1) * s, 1, -RZ), "Candle")
342 -- the lever and the old wire, if there are circuits in this world
343 if exists("Lever") and exists("Copper wire") then
344 put(lever_at(w), "Lever")
345 local gaps = broken(w)
346 for i, cell in wire_cells(w) do
347 if not gaps[i] then
348 put(cell, "Copper wire")
349 end
350 end
351 end
352end
353
354-- puts down a few hundred blocks a tick
355local function step()
356 local last = math.min(done + 250, #work)
357 for i = done + 1, last do
358 local w = work[i]
359 local b = world.block(w.at)
360 if w.name == "" then
361 if NATURAL[b] or OURS[b] then
362 world.set_block(w.at, "Air")
363 end
364 elseif NATURAL[b] or OURS[b] or b == IRON then
365 world.set_block(w.at, w.name)
366 end
367 end
368 done = last
369 if done >= #work then
370 work, done = {}, 0
371 local w = building
372 if w then
373 w.built = true
374 w.way = nil
375 building = nil
376 save()
377 end
378 end
379end
380
381-- ---- the vault ----
382
383-- the workshop a place belongs to
384local function workshop_at(pos: vector, reach: number): Workshop?
385 for _, w in workshops do
386 if vector.magnitude(pos - w.at) < reach then
387 return w
388 end
389 end
390 return nil
391end
392
393-- Whether power reaches `at` from beside it, by the Circuits creation's
394-- blocks: live wire and a pulled lever every way, a lit inverter or delay
395-- only in front.
396local function powered(at: vector): boolean
397 for _, d in SIDES do
398 local q = at + d
399 local b = world.block(q)
400 if b == "Live wire" or b == "Pulled lever" then
401 return true
402 end
403 if b == "Lit inverter" or b == "Lit delay" then
404 local f = world.facing(q)
405 if f and key(q + AHEAD[f]) == key(at) then
406 return true
407 end
408 end
409 end
410 return false
411end
412
413local function open(w: Workshop)
414 w.open = true
415 save()
416 for _, at in door_cells(w) do
417 if world.block(at) == DOOR then
418 world.set_block(at, "Air")
419 end
420 end
421 sound.play("creak", { at = door_cells(w)[1] })
422 for _, p in world.players() do
423 if vector.magnitude(p.position - w.at) < 16 then
424 tell(p, "The vault door swings open!")
425 world.reward(p, VAULT_XP)
426 end
427 end
428end
429
430-- the vault door can't be cut or broken, nor its walls
431world.drops(IRON, { gives = false, level = 9 })
432world.drops(DOOR, { gives = false, level = 9 })
433
434-- ---- the box and the chest ----
435
436type Find = { thing: string, n: number }
437-- what's still in chests players' pockets were too full for, by workshop
438local leftover: { [string]: { Find } } = {}
439
440local function roll(): { Find }
441 local out: { Find } = {}
442 for _, p in PRIZES do
443 if exists(p.thing) and world.random() < math.min(1, p.chance * knobs.prizes) then
444 local n = math.floor(world.random(p.least, p.most + 0.999) * knobs.prizes)
445 table.insert(out, { thing = p.thing, n = math.clamp(n, 1, if p.most == 1 then 1 else 64) })
446 end
447 end
448 if #out == 0 then
449 table.insert(out, { thing = "Iron ingot", n = 2 })
450 end
451 return out
452end
453
454local function say(finds: { Find }): string
455 local parts = {}
456 for _, f in finds do
457 table.insert(parts, f.n .. " " .. string.lower(f.thing))
458 end
459 return table.concat(parts, ", ")
460end
461
462-- Gives what's in a vault chest; true when it's all given.
463local function rummage(player, w: Workshop): boolean
464 local k = key(w.at)
465 local finds = leftover[k] or roll()
466 local got, still = {}, {}
467 for _, f in finds do
468 local n = world.give(player, f.thing, f.n)
469 if n > 0 then
470 table.insert(got, { thing = f.thing, n = n })
471 end
472 if n < f.n then
473 table.insert(still, { thing = f.thing, n = f.n - n })
474 end
475 end
476 if #got > 0 then
477 ui.ask(player, { title = "The vault chest", text = "You found " .. say(got) .. "." }, function() end)
478 end
479 if #still > 0 then
480 leftover[k] = still
481 tell(player, "Your pockets are full.")
482 return false
483 end
484 leftover[k] = nil
485 w.chest = false
486 save()
487 world.reward(player, CHEST_XP)
488 sound.play("sparkle", { at = chest_at(w), caption = "Treasure in the vault" })
489 tell(player, "The vault's treasure is yours.")
490 return true
491end
492
493-- the note in the tinker's pot, when there's wire in it
494local NOTE = "A note in a careful hand: \"The roof came down and broke the wire to the vault. Mend it with these and pull the lever. And don't lose the bird.\""
495
496-- What's in the tinker's pot: wire to mend the old line, and the goggles.
497local function open_box(player, w: Workshop)
498 local got = {}
499 local wire = exists("Copper wire") and world.give(player, "Copper wire", WIRE_IN_BOX) > 0
500 if wire then
501 table.insert(got, WIRE_IN_BOX .. " copper wire")
502 end
503 if world.give(player, "Tinker's goggles", 1) > 0 then
504 table.insert(got, "tinker's goggles")
505 end
506 if #got == 0 then
507 tell(player, "Your pockets are full.")
508 return
509 end
510 w.box = false
511 save()
512 sound.play("click", { at = box_at(w), pitch = 1.5, caption = "Rummaging in a pot" })
513 tell(player, "You found " .. table.concat(got, " and ") .. ".")
514 if wire then
515 ui.ask(player, { title = "The tinker's pot", text = NOTE }, function() end)
516 end
517end
518
519-- the chest breaks into nothing: what's in it comes out instead (the pot
520-- is a pot, so it stays one)
521world.drops(CHEST, { gives = false })
522
523world.on("block_used", function(player, at: vector, block: string)
524 if block ~= BOX and block ~= CHEST and block ~= DOOR then
525 return
526 end
527 local w = workshop_at(at, 12)
528 if block == BOX and not (w and key(at) == key(box_at(w))) then
529 return -- just a pot
530 end
531 if block == DOOR then
532 tell(player, if exists("Lever") then "It's shut fast. It opens with power." else "It's shut fast.")
533 return
534 end
535 if world.pockets(player) == nil then
536 tell(player, "Lost workshops hold prizes for survival.")
537 return
538 end
539 if block == BOX then
540 if w and w.box then
541 open_box(player, w)
542 else
543 tell(player, "The tinker's pot is empty.")
544 end
545 elseif w and w.chest and key(at) == key(chest_at(w)) then
546 rummage(player, w)
547 else
548 tell(player, "An empty chest.")
549 end
550end)
551
552world.on("block_broken", function(player, at: vector, block: string)
553 if block ~= BOX and block ~= CHEST then
554 return
555 end
556 local w = workshop_at(at, 12)
557 if not w or world.pockets(player) == nil then
558 return
559 end
560 if block == BOX and w.box and key(at) == key(box_at(w)) then
561 open_box(player, w)
562 w.box = false
563 save()
564 elseif block == CHEST and w.chest and key(at) == key(chest_at(w)) then
565 rummage(player, w)
566 w.chest = false
567 leftover[key(w.at)] = nil
568 save()
569 end
570end)
571
572-- ---- the clockwork bird ----
573
574world.on("item_used", function(player, item: string?)
575 if item ~= "Clockwork bird" then
576 return
577 end
578 for i, pitch in { 1, 1.26, 0.84 } do
579 world.after((i - 1) * 0.35, function()
580 sound.play("tweet", { at = player.position, pitch = pitch })
581 end)
582 end
583end)
584
585-- ---- watching players ----
586
587local told: { [string]: boolean } = {}
588local turn = 0
589world.every(0.1, function()
590 if #work > 0 then
591 step()
592 return
593 end
594 turn += 1
595 -- vaults with somebody near, twice a second
596 if turn % 5 == 0 then
597 for i, w in workshops do
598 if w.built and not w.open and world.nearest_player(w.at, 32) then
599 for _, at in door_cells(w) do
600 if powered(at) then
601 open(w)
602 break
603 end
604 end
605 end
606 if w.built then
607 for _, p in world.players() do
608 local k = p.name .. "/" .. i
609 if not told[k] and vector.magnitude(p.position - w.at) < RX + 3 then
610 told[k] = true
611 -- not over something they've just been told
612 if shown[p.id] then
613 continue
614 end
615 tell(p, if w.open then "A lost workshop. Its vault stands open." else "A lost workshop. Its vault opens with power.")
616 end
617 end
618 end
619 end
620 end
621 if turn % 20 ~= 0 then
622 return
623 end
624 local players = world.players()
625 if #players == 0 or #workshops >= MOST then
626 return
627 end
628 local p = players[(turn // 20) % #players + 1]
629 local pos = p.position
630 -- the Long Dusk has finds of its own
631 if world.place(pos) == "dusk" then
632 return
633 end
634 -- only underground, well away from the Green
635 if world.top(pos.x, pos.z) < pos.y + DEEP or pos.y > 24 then
636 return
637 end
638 for _, name in { "green", "start" } do
639 local at = world.spot(name)
640 if at and flat(pos, at) < 90 then
641 return
642 end
643 end
644 if not far_from_others(pos, knobs.apart) then
645 return
646 end
647 local c, way = look_near(p)
648 if c and way then
649 local w: Workshop = {
650 at = c,
651 side = if hash(c.x, c.y, c.z) < 0.5 then 1 else -1,
652 built = false,
653 open = false,
654 box = true,
655 chest = true,
656 way = way,
657 }
658 table.insert(workshops, w)
659 building = w
660 plan(w)
661 save()
662 end
663end)
664
665-- a workshop left half built (the world stopped mid-way) is built on
666for _, w in workshops do
667 if not w.built then
668 building = w
669 plan(w)
670 break
671 end
672end
673
674world.on("player_left", function(p)
675 shown[p.id] = nil
676end)