Sign in
mod

Circuits

by Potluck · version 1 · Checked for all ages

Copper wire carries power from levers, buttons, pressure plates and daylight sensors, through inverters and delays, to copper lamps and copper gates. Power is on or off. Inverters and delays face the way you put them down, take power from behind and give it out in front. Power a piston and it pushes what's in front of it; power a note block and it plays, a step higher each time. A Grid tap beside power cable is a source while its grid has enough power.

1--!strict
2-- Circuits: copper wire carries power from levers, buttons, pressure plates
3-- and daylight sensors, through inverters and delays, to copper lamps and
4-- copper gates, and to pistons and note blocks. Power is on or off, nothing
5-- in between.
6--
7-- Wire joins wire beside it, above or below it, and a step up or down, so
8-- a line of wire can climb stairs. Anything that gives power (a lever
9-- pulled, a button pressed, someone on a plate, a sensor in daylight, an
10-- inverter or delay giving out, a Grid tap whose power grid has enough) powers the wire, lamps and gates it
11-- touches. Inverters and delays face the way you put them down: they take
12-- power from behind (the side nearest you as you put it down) and give it
13-- out in front. An inverter gives power when none comes in; a delay passes
14-- on what comes in a moment later (use it to set how long). Gates touching
15-- each other open together.
16--
17-- A piston pushes what's in front of it (up to twelve blocks) one along
18-- when power reaches it from any other side, and pulls its head back when
19-- the power goes; an up piston does the same upward. It can't move what
20-- keeps things, circuit parts or other machines, and what can't move stops
21-- it. A note block plays a note as power reaches it: use it to tune it a
22-- step higher (25 notes, F#3 to F#5), and what it stands on gives it its
23-- voice: a bass on wood, a bell on stone and brick, a chime on glass and a
24-- harp on anything else.
25--
26-- The circuit is worked out ten times a second, so every inverter and
27-- delay takes at least a tenth of a second: a loop of them makes a clock.
28-- Everything is saved in the world's store. The blocks are in potluck.toml;
29-- what each is called is in BLOCKS below.
30
31local world = require("@potluck/world@0.23")
32local tweak = require("@potluck/tweak")
33local store = require("@potluck/store")
34local ui = require("@potluck/ui@0.2")
35local sound = require("@potluck/sound@0.2")
36
37local knobs = tweak {
38 daylight = { 0.5, min = 0.05, max = 1, label = "How light it is when a sensor turns on", order = 1 },
39 button = { 1, min = 0.2, max = 5, label = "Seconds a button stays pressed", order = 2 },
40 blink = { 0.25, min = 0.1, max = 1, label = "Fastest a lamp changes, in seconds", order = 3 },
41 sounds = { true, label = "Levers and gates make sounds", order = 4 },
42 reach = { 12, min = 1, max = 12, label = "How many blocks a piston pushes", order = 5 },
43 loud = { 0.8, min = 0.1, max = 1, label = "How loud note blocks are", order = 6 },
44}
45
46-- What each part is, by its blocks' names: [off, on].
47local BLOCKS: { [string]: { string } } = {
48 wire = { "Copper wire", "Live wire" },
49 lever = { "Lever", "Pulled lever" },
50 button = { "Button" },
51 plate = { "Pressure plate" },
52 sensor = { "Daylight sensor" },
53 inverter = { "Inverter", "Lit inverter" },
54 delay = { "Delay", "Lit delay" },
55 lamp = { "Copper lamp", "Lit copper lamp" },
56 gate = { "Copper gate", "Open copper gate" },
57 piston = { "Piston" },
58 lift = { "Up piston" },
59 note = { "Note block" },
60 -- the Power creation turns it live while its grid has enough
61 tap = { "Grid tap", "Live grid tap" },
62}
63local HEAD, UP_HEAD = "Piston head", "Up piston head"
64-- Which kind each block is, and whether it's its on look.
65local KIND: { [string]: { kind: string, on: boolean } } = {}
66for kind, names in BLOCKS do
67 for i, name in names do
68 KIND[name] = { kind = kind, on = i == 2 }
69 end
70end
71
72local SOURCES = { lever = true, button = true, plate = true, sensor = true, tap = true }
73local GATES = { inverter = true, delay = true }
74-- what turns the way it's put down
75local TURNS = { inverter = true, delay = true, piston = true }
76
77-- The way each facing points.
78local AHEAD: { [string]: vector } = {
79 north = vector.create(0, 0, -1),
80 east = vector.create(1, 0, 0),
81 south = vector.create(0, 0, 1),
82 west = vector.create(-1, 0, 0),
83}
84
85local SIDES = {
86 vector.create(1, 0, 0),
87 vector.create(-1, 0, 0),
88 vector.create(0, 1, 0),
89 vector.create(0, -1, 0),
90 vector.create(0, 0, 1),
91 vector.create(0, 0, -1),
92}
93local UP = vector.create(0, 1, 0)
94-- Wire also joins wire a step up or down.
95local STEPS = {}
96for _, d in { vector.create(1, 0, 0), vector.create(-1, 0, 0), vector.create(0, 0, 1), vector.create(0, 0, -1) } do
97 table.insert(STEPS, d + vector.create(0, 1, 0))
98 table.insert(STEPS, d - vector.create(0, 1, 0))
99end
100
101-- One part of a circuit: what it is, where, which way it faces (inverters,
102-- delays and pistons), whether it's on, for a delay its tenths of a second
103-- and what's coming through it, and for a note block its note.
104type Part = {
105 at: vector,
106 kind: string,
107 facing: string?,
108 on: boolean,
109 -- what it shows: a lamp only changes its look every so often
110 shown: boolean,
111 shown_at: number,
112 tenths: number,
113 ends: number,
114 queue: { { at: number, on: boolean } },
115 heard: boolean,
116 tune: number,
117}
118local parts: { [string]: Part } = {}
119-- Wire networks: their wire, what touches them that could power them, and
120-- whether they're live (nil until first worked out, so they show it).
121type Net = { wires: { string }, feeders: { { part: Part, to: vector } }, on: boolean? }
122local net_of: { [string]: number } = {} -- each wire's network
123local nets: { Net } = {}
124local shaped = false -- the networks need working out again
125local changed = false -- something to keep in the store
126
127local function key(at: vector): string
128 return math.floor(at.x) .. "," .. math.floor(at.y) .. "," .. math.floor(at.z)
129end
130
131local function middle(at: vector): vector
132 return vector.create(math.floor(at.x) + 0.5, math.floor(at.y) + 0.5, math.floor(at.z) + 0.5)
133end
134
135local function add(at: vector, block: string): Part?
136 local k = KIND[block]
137 if not k then
138 return nil
139 end
140 local p: Part = {
141 at = middle(at),
142 kind = k.kind,
143 facing = if TURNS[k.kind] then world.facing(at) or "north" else nil,
144 on = k.on,
145 shown = k.on,
146 shown_at = -1,
147 tenths = 1,
148 ends = 0,
149 queue = {},
150 heard = k.on,
151 tune = 12,
152 }
153 parts[key(at)] = p
154 shaped, changed = true, true
155 return p
156end
157
158-- Parts put down before this creation was here, or by another creation
159-- (the old wire in a lost workshop), join in when a part beside them is
160-- put down or used: everything they reach through each other, a few
161-- hundred at a time.
162local function join(from: vector)
163 local todo = { from }
164 local n = 0
165 while #todo > 0 and n < 256 do
166 local at = table.remove(todo) :: vector
167 local p = parts[key(at)]
168 for _, list in { SIDES, STEPS } do
169 for _, d in list do
170 local q = at + d
171 if not parts[key(q)] then
172 local b = world.block(q)
173 local k = KIND[b]
174 -- wire joins wire a step up or down, and parts beside it
175 if k and (list == SIDES or (k.kind == "wire" and p ~= nil and p.kind == "wire")) and add(q, b) then
176 n += 1
177 table.insert(todo, q)
178 end
179 end
180 end
181 end
182 end
183end
184
185-- Wire into networks: everything one piece of wire can reach through others.
186local function shape()
187 shaped = false
188 net_of, nets = {}, {}
189 for k, p in parts do
190 if p.kind ~= "wire" or net_of[k] then
191 continue
192 end
193 local n = #nets + 1
194 local net: Net = { wires = {}, feeders = {}, on = nil }
195 nets[n] = net
196 local todo = { p.at }
197 net_of[k] = n
198 while #todo > 0 do
199 local at = table.remove(todo) :: vector
200 table.insert(net.wires, key(at))
201 for _, d in SIDES do
202 local q = parts[key(at + d)]
203 if q and q.kind ~= "wire" then
204 table.insert(net.feeders, { part = q, to = at })
205 end
206 end
207 for _, list in { SIDES, STEPS } do
208 for _, d in list do
209 local q = at + d
210 local qk = key(q)
211 local other = parts[qk]
212 if other and other.kind == "wire" and not net_of[qk] then
213 net_of[qk] = n
214 table.insert(todo, other.at)
215 end
216 end
217 end
218 end
219 end
220end
221
222-- Whether a part gives power out to `to`, one block away: a source that's
223-- on gives it every way, an inverter or delay only in front.
224local function gives_to(p: Part, to: vector): boolean
225 if not p.on then
226 return false
227 end
228 if SOURCES[p.kind] then
229 return true
230 end
231 if GATES[p.kind] then
232 return key(p.at + AHEAD[p.facing or "north"]) == key(to)
233 end
234 return false
235end
236
237-- Whether power reaches `at` from the part or wire at `from`.
238local function powered_from(from: vector, at: vector): boolean
239 local q = parts[key(from)]
240 if not q then
241 return false
242 end
243 if q.kind == "wire" then
244 local n = net_of[key(from)]
245 return n ~= nil and nets[n].on == true
246 end
247 return gives_to(q, at)
248end
249
250-- Whether anything beside `at` powers it (but not from `front`, where a
251-- piston's head goes).
252local function powered(at: vector, front: vector?): boolean
253 for _, d in SIDES do
254 local from = at + d
255 if from ~= front and powered_from(from, at) then
256 return true
257 end
258 end
259 return false
260end
261
262-- Shows a part's state with its own block, keeping the way it faces.
263local function show(p: Part, on: boolean)
264 local names = BLOCKS[p.kind]
265 local want = names[if on and #names > 1 then 2 else 1]
266 if world.block(p.at) ~= want then
267 world.set_block(p.at, want)
268 end
269end
270
271-- ---- pistons ----
272
273-- What a piston can't move: what keeps things in it, circuit parts,
274-- machines, and what's fixed in the world.
275local FIXED: { [string]: boolean } = { [HEAD] = true, [UP_HEAD] = true }
276for _, name in {
277 "Bedrock", "Crate", "Barrel", "Dispenser", "Chute", "Furnace", "Lit furnace", "Campfire", "Cooking pot",
278 "Old chest", "Lost pack", "Beehive", "Anvil", "Charm table", "Table", "Balloon basket",
279} do
280 FIXED[name] = true
281end
282for name in KIND do
283 FIXED[name] = true
284end
285-- What a pushed block goes into, and crushes.
286local GIVES_WAY: { [string]: boolean } = { Air = true, Water = true, ["Tall grass"] = true, Fern = true, Bush = true, Fire = true }
287
288-- The way a piston's head goes.
289local function ahead(p: Part): vector
290 if p.kind == "lift" then
291 return UP
292 end
293 return AHEAD[p.facing or "north"]
294end
295
296-- Pushes the line of blocks in front one along and puts the head out.
297-- False if something fixed, or too many, is in the way.
298local function push(p: Part): boolean
299 local d = ahead(p)
300 local front = p.at + d
301 local line: { { at: vector, name: string, facing: string? } } = {}
302 local q = front
303 while not GIVES_WAY[world.block(q)] do
304 local b = world.block(q)
305 if FIXED[b] or #line >= math.floor(knobs.reach) or q.y >= 127 then
306 return false
307 end
308 table.insert(line, { at = q, name = b, facing = world.facing(q) })
309 q += d
310 end
311 if q.y >= 128 then
312 return false
313 end
314 for i = #line, 1, -1 do
315 local e = line[i]
316 world.set_block(e.at + d, e.name, e.facing)
317 end
318 if p.kind == "lift" then
319 world.set_block(front, UP_HEAD)
320 else
321 world.set_block(front, HEAD, p.facing or "north")
322 end
323 sound.play("push", { at = p.at })
324 return true
325end
326
327-- Pulls the head back in.
328local function pull(p: Part)
329 local front = p.at + ahead(p)
330 local b = world.block(front)
331 if b == HEAD or b == UP_HEAD then
332 world.set_block(front, "Air")
333 sound.play("pull", { at = p.at })
334 end
335end
336
337-- ---- note blocks ----
338
339local NAMES = { "F#", "G", "G#", "A", "A#", "B", "C", "C#", "D", "D#", "E", "F" }
340-- "F#4": tune 0 is F#3, 12 is F#4 and 24 is F#5
341local function note_name(t: number): string
342 return NAMES[t % 12 + 1] .. (3 + math.floor((t + 6) / 12))
343end
344
345-- its voice, by what it stands on
346local function voice(at: vector): string
347 local under = string.lower(world.block(at - UP))
348 if string.find(under, "glass", 1, true) then
349 return "chime"
350 end
351 for _, w in { "plank", "log", "wood", "crate", "barrel", "bookshelf" } do
352 if string.find(under, w, 1, true) then
353 return "bass"
354 end
355 end
356 for _, s in { "stone", "brick", "cobble", "slate", "column" } do
357 if string.find(under, s, 1, true) then
358 return "bell"
359 end
360 end
361 return "harp"
362end
363
364local function play(p: Part)
365 sound.play(voice(p.at), {
366 at = p.at + vector.create(0, 0.5, 0),
367 pitch = 2 ^ ((p.tune - 12) / 12),
368 volume = knobs.loud,
369 caption = "A note: " .. note_name(p.tune),
370 })
371end
372
373-- Some players on a plate.
374local function pressed(p: Part, players: { any }): boolean
375 for _, who in players do
376 if key(who.position) == key(p.at) then
377 return true
378 end
379 end
380 return false
381end
382
383-- Works the circuit out once: sources, then the wire they power, then
384-- what that wire powers. Inverters and delays look at what came in last
385-- time, so each takes a tenth of a second and a loop of them ticks.
386local function step()
387 if shaped then
388 shape()
389 end
390 local now = world.time()
391 local players = world.players()
392 local light = world.daylight()
393 for _, p in parts do
394 if p.kind == "button" then
395 p.on = now < p.ends
396 elseif p.kind == "plate" then
397 p.on = pressed(p, players)
398 elseif p.kind == "sensor" then
399 p.on = light >= knobs.daylight
400 elseif p.kind == "tap" then
401 -- the Power creation says, with the block it puts there
402 p.on = world.block(p.at) == "Live grid tap"
403 end
404 end
405 -- what reaches each wire network
406 for _, net in nets do
407 local on = false
408 for _, f in net.feeders do
409 if gives_to(f.part, f.to) then
410 on = true
411 break
412 end
413 end
414 if on ~= net.on then
415 net.on = on
416 for _, wk in net.wires do
417 show(parts[wk], on)
418 end
419 end
420 end
421 -- what comes into each inverter and delay, from behind
422 local coming: { [Part]: boolean } = {}
423 for _, p in parts do
424 if GATES[p.kind] then
425 coming[p] = powered_from(p.at - AHEAD[p.facing or "north"], p.at)
426 end
427 end
428 for p, input in coming do
429 local was = p.on
430 if p.kind == "inverter" then
431 p.on = not input
432 else
433 local last = if #p.queue > 0 then p.queue[#p.queue].on else p.heard
434 if input ~= last and #p.queue < 40 then
435 table.insert(p.queue, { at = now + p.tenths / 10 - 0.05, on = input })
436 end
437 while #p.queue > 0 and now >= p.queue[1].at do
438 p.heard = table.remove(p.queue, 1).on
439 p.on = p.heard
440 end
441 end
442 if p.on ~= was then
443 show(p, p.on)
444 end
445 end
446 -- lamps, and gates with every gate they touch
447 local seen: { [string]: boolean } = {}
448 for k, p in parts do
449 if p.kind == "lamp" then
450 p.on = powered(p.at)
451 if p.on ~= p.shown and now - p.shown_at >= knobs.blink then
452 p.shown, p.shown_at = p.on, now
453 show(p, p.on)
454 end
455 elseif p.kind == "gate" and not seen[k] then
456 local group, todo, open = {}, { p }, false
457 seen[k] = true
458 while #todo > 0 do
459 local g = table.remove(todo) :: Part
460 table.insert(group, g)
461 open = open or powered(g.at)
462 for _, d in SIDES do
463 local q = parts[key(g.at + d)]
464 if q and q.kind == "gate" and not seen[key(q.at)] then
465 seen[key(q.at)] = true
466 table.insert(todo, q)
467 end
468 end
469 end
470 for _, g in group do
471 if g.on ~= open then
472 g.on = open
473 show(g, open)
474 end
475 end
476 if open ~= p.shown then
477 p.shown = open
478 if knobs.sounds then
479 sound.play("thud", { at = p.at, volume = 0.4, caption = if open then "A gate opens" else "A gate shuts" })
480 end
481 end
482 elseif p.kind == "piston" or p.kind == "lift" or p.kind == "note" then
483 local on = powered(p.at, if p.kind == "note" then nil else p.at + ahead(p))
484 if on ~= p.on then
485 p.on = on
486 changed = true
487 if p.kind == "note" then
488 if on then
489 play(p)
490 end
491 elseif on then
492 -- nothing moves if it can't: it tries again when power comes back
493 push(p)
494 else
495 pull(p)
496 end
497 end
498 end
499 end
500end
501
502world.every(0.1, step)
503
504world.on("block_placed", function(_player, at: vector, block: string)
505 local here = parts[key(at)]
506 if here then
507 parts[key(at)] = nil
508 shaped, changed = true, true
509 end
510 if add(at, block) then
511 join(at)
512 end
513end)
514
515world.on("block_broken", function(_player, at: vector, _block: string)
516 local p = parts[key(at)]
517 if p then
518 -- a piston taken up takes its head with it
519 if p.on and (p.kind == "piston" or p.kind == "lift") then
520 pull(p)
521 end
522 parts[key(at)] = nil
523 shaped, changed = true, true
524 end
525end)
526
527world.on("block_used", function(player, at: vector, block: string)
528 local p = parts[key(at)]
529 if not p then
530 -- one put down before this creation was here
531 if not KIND[block] then
532 return
533 end
534 p = add(at, block)
535 join(at)
536 end
537 if not p then
538 return
539 end
540 if p.kind == "lever" then
541 p.on = not p.on
542 show(p, p.on)
543 changed = true
544 if knobs.sounds then
545 sound.play("click", { at = p.at, pitch = if p.on then 1.2 else 0.9, caption = if p.on then "Lever on" else "Lever off" })
546 end
547 elseif p.kind == "button" then
548 p.ends = world.time() + knobs.button
549 if knobs.sounds then
550 sound.play("click", { at = p.at, pitch = 1.4, caption = "Button pressed" })
551 end
552 elseif p.kind == "delay" then
553 -- 1, 2, 3, 5 then 10 tenths of a second, and around again
554 local next = { [1] = 2, [2] = 3, [3] = 5, [5] = 10, [10] = 1 }
555 p.tenths = next[p.tenths] or 1
556 changed = true
557 ui.show(player, "delay", string.format("Delay: %.1f seconds", p.tenths / 10), { at = "bottom" })
558 world.after(2, function()
559 ui.hide(player, "delay")
560 end)
561 if knobs.sounds then
562 sound.play("click", { at = p.at, pitch = 1.6, caption = "Delay set" })
563 end
564 elseif p.kind == "note" then
565 -- a step higher each time, and around again
566 p.tune = (p.tune + 1) % 25
567 changed = true
568 play(p)
569 ui.show(player, "note", "Tuned to " .. note_name(p.tune), { at = "bottom" })
570 world.after(2, function()
571 ui.hide(player, "note")
572 end)
573 end
574end)
575
576-- On looks break into what they are: a live wire into copper wire.
577for _, names in BLOCKS do
578 if #names > 1 then
579 world.drops(names[2], { gives = names[1] })
580 end
581end
582-- and pistons' heads into nothing: they're part of the piston
583world.drops(HEAD, { gives = false })
584world.drops(UP_HEAD, { gives = false })
585
586-- Whether a part's on is kept: a lever's, and a machine's, so a piston
587-- already out doesn't push again, nor a note play, after a restart.
588local KEEPS_ON = { lever = true, piston = true, lift = true, note = true }
589
590-- Now and then, forget parts that aren't there any more (taken away by
591-- another creation, or the world changed under them), a few at a time.
592local checking: { string } = {}
593world.every(0.5, function()
594 if #checking == 0 then
595 for k in parts do
596 table.insert(checking, k)
597 end
598 return
599 end
600 for _ = 1, 32 do
601 local k = table.remove(checking)
602 if not k then
603 break
604 end
605 local p = parts[k]
606 local k2 = p and KIND[world.block(p.at)]
607 if p and not (k2 and k2.kind == p.kind) then
608 parts[k] = nil
609 shaped, changed = true, true
610 end
611 end
612end)
613
614-- Keeps the circuits in the world's store, so they're there after a restart.
615world.every(2, function()
616 if not changed then
617 return
618 end
619 changed = false
620 local kept = {}
621 for _, p in parts do
622 table.insert(kept, { p.at.x, p.at.y, p.at.z, p.kind, p.facing or "", if KEEPS_ON[p.kind] and p.on then 1 else 0, p.tenths, p.tune })
623 end
624 store.set("parts", kept)
625end)
626
627do
628 for _, s in (store.get("parts") or {}) :: { { any } } do
629 local at = vector.create(s[1], s[2], s[3])
630 local k = KIND[world.block(at)]
631 if k and k.kind == s[4] then
632 local p = add(at, world.block(at)) :: Part
633 p.on = p.on or s[6] == 1
634 p.tenths = s[7] or 1
635 p.tune = s[8] or 12
636 end
637 end
638 changed = false
639end