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' own tests: `potluck-server --test=circuits`. |
| 3 | |
| 4 | local test = require("@potluck/test@0.7") |
| 5 | local world = require("@potluck/world@0.23") |
| 6 | |
| 7 | -- a row of spots in the air east of a player, clear to build in |
| 8 | local function row(p, n: number): { vector } |
| 9 | local x, z = math.floor(p.position.x) + 3, math.floor(p.position.z) + 3 |
| 10 | local y = world.top(x, z) + 4 |
| 11 | local out = {} |
| 12 | for i = 0, n - 1 do |
| 13 | local at = vector.create(x + i, y, z) |
| 14 | world.set_block(at, "Air") |
| 15 | table.insert(out, at) |
| 16 | end |
| 17 | return out |
| 18 | end |
| 19 | |
| 20 | local function lit(at: vector): boolean |
| 21 | return world.block(at) == "Lit copper lamp" |
| 22 | end |
| 23 | |
| 24 | test.case("its blocks are in the world", function() |
| 25 | for _, name in { "Copper wire", "Live wire", "Lever", "Pulled lever", "Button", "Pressure plate", "Daylight sensor", "Inverter", "Lit inverter", "Delay", "Lit delay", "Copper lamp", "Lit copper lamp", "Copper gate", "Open copper gate", "Piston", "Piston head", "Up piston", "Up piston head", "Note block", "Grid tap", "Live grid tap" } do |
| 26 | test.check(pcall(world.kind, name), name) |
| 27 | end |
| 28 | end) |
| 29 | |
| 30 | test.case("a lever lights a lamp along a wire, and puts it out", function() |
| 31 | local p = test.player("Ada") |
| 32 | local at = row(p, 5) |
| 33 | test.place(p, at[1], "Lever") |
| 34 | for i = 2, 4 do |
| 35 | test.place(p, at[i], "Copper wire") |
| 36 | end |
| 37 | test.place(p, at[5], "Copper lamp") |
| 38 | test.wait(0.5) |
| 39 | test.check(not lit(at[5]), "dark to start with") |
| 40 | test.use_block(p, at[1]) |
| 41 | test.wait(0.5) |
| 42 | test.same(world.block(at[1]), "Pulled lever") |
| 43 | test.same(world.block(at[3]), "Live wire", "the wire is live") |
| 44 | test.check(lit(at[5]), "and the lamp lit") |
| 45 | test.use_block(p, at[1]) |
| 46 | test.wait(0.5) |
| 47 | test.same(world.block(at[3]), "Copper wire") |
| 48 | test.check(not lit(at[5]), "out again") |
| 49 | end) |
| 50 | |
| 51 | test.case("wire another creation put down joins in when the lever beside it is used, or wire is built on it", function() |
| 52 | local p = test.player("Ada") |
| 53 | local at = row(p, 7) |
| 54 | -- laid by code, as a lost workshop's old wire is: Circuits doesn't hear it |
| 55 | world.set_block(at[1], "Lever") |
| 56 | world.set_block(at[2], "Copper wire") |
| 57 | world.set_block(at[3], "Copper wire") |
| 58 | world.set_block(at[5], "Copper wire") |
| 59 | world.set_block(at[6], "Copper wire") |
| 60 | world.set_block(at[7], "Copper lamp") |
| 61 | test.wait(0.5) |
| 62 | test.use_block(p, at[1]) |
| 63 | test.wait(0.5) |
| 64 | test.same(world.block(at[3]), "Live wire", "the old wire's live up to the break") |
| 65 | test.same(world.block(at[5]), "Copper wire", "but not past it") |
| 66 | test.check(not lit(at[7]), "nor the lamp") |
| 67 | -- mend it |
| 68 | test.place(p, at[4], "Copper wire") |
| 69 | test.wait(0.5) |
| 70 | test.same(world.block(at[6]), "Live wire", "live right along") |
| 71 | test.check(lit(at[7]), "and the lamp lit") |
| 72 | end) |
| 73 | |
| 74 | test.case("an inverter faces away from whoever put it down, and turns power around", function() |
| 75 | local p = test.player("Bo") |
| 76 | local at = row(p, 4) |
| 77 | test.place(p, at[1], "Lever") |
| 78 | test.place(p, at[2], "Copper wire") |
| 79 | test.place(p, at[3], "Inverter", "east") |
| 80 | test.place(p, at[4], "Copper lamp") |
| 81 | test.same(world.facing(at[3]), "east") |
| 82 | test.same(world.facing(at[2]), nil, "wire doesn't turn") |
| 83 | test.wait(0.6) |
| 84 | test.same(world.block(at[3]), "Lit inverter", "nothing in, so power out") |
| 85 | test.same(world.facing(at[3]), "east", "it still faces east lit") |
| 86 | test.check(lit(at[4]), "the lamp in front is lit") |
| 87 | test.use_block(p, at[1]) |
| 88 | test.wait(0.6) |
| 89 | test.same(world.block(at[3]), "Inverter") |
| 90 | test.check(not lit(at[4]), "power in, so none out") |
| 91 | end) |
| 92 | |
| 93 | test.case("an inverter gives power only in front", function() |
| 94 | local p = test.player("Cy") |
| 95 | local at = row(p, 3) |
| 96 | -- facing west, its front is the lamp at at[1]; the lamp behind stays dark |
| 97 | test.place(p, at[1], "Copper lamp") |
| 98 | test.place(p, at[2], "Inverter", "west") |
| 99 | test.place(p, at[3], "Copper lamp") |
| 100 | test.wait(0.6) |
| 101 | test.check(lit(at[1]), "in front") |
| 102 | test.check(not lit(at[3]), "behind") |
| 103 | end) |
| 104 | |
| 105 | test.case("a delay passes power on later, as long as it's set to", function() |
| 106 | local p = test.player("Di") |
| 107 | local at = row(p, 5) |
| 108 | test.place(p, at[1], "Lever") |
| 109 | test.place(p, at[2], "Copper wire") |
| 110 | test.place(p, at[3], "Delay", "east") |
| 111 | test.place(p, at[4], "Copper wire") |
| 112 | test.place(p, at[5], "Copper lamp") |
| 113 | -- 0.1, then 0.2, 0.3 and 0.5 seconds |
| 114 | for _ = 1, 3 do |
| 115 | test.use_block(p, at[3]) |
| 116 | end |
| 117 | test.same(test.shown(p, "delay"), "Delay: 0.5 seconds") |
| 118 | test.wait(0.3) |
| 119 | test.use_block(p, at[1]) |
| 120 | test.wait(0.3) |
| 121 | test.check(not lit(at[5]), "not yet") |
| 122 | test.wait(0.6) |
| 123 | test.same(world.block(at[3]), "Lit delay") |
| 124 | test.check(lit(at[5]), "now") |
| 125 | end) |
| 126 | |
| 127 | test.case("a button gives power for a moment", function() |
| 128 | local p = test.player("Ed") |
| 129 | local at = row(p, 2) |
| 130 | test.place(p, at[1], "Button") |
| 131 | test.place(p, at[2], "Copper lamp") |
| 132 | test.use_block(p, at[1]) |
| 133 | test.wait(0.5) |
| 134 | test.check(lit(at[2]), "pressed") |
| 135 | test.wait(1.5) |
| 136 | test.check(not lit(at[2]), "and let go") |
| 137 | end) |
| 138 | |
| 139 | test.case("a pressure plate gives power while someone stands on it", function() |
| 140 | local p = test.player("Fay") |
| 141 | local at = row(p, 2) |
| 142 | test.place(p, at[1], "Pressure plate") |
| 143 | test.place(p, at[2], "Copper lamp") |
| 144 | test.wait(0.3) |
| 145 | test.check(not lit(at[2]), "nobody on it") |
| 146 | test.move(p, at[1] + vector.create(0.5, 0, 0.5)) |
| 147 | test.wait(0.5) |
| 148 | test.check(lit(at[2]), "someone on it") |
| 149 | test.move(p, at[1] + vector.create(0.5, 0, 6.5)) |
| 150 | test.wait(0.5) |
| 151 | test.check(not lit(at[2]), "they stepped off") |
| 152 | end) |
| 153 | |
| 154 | test.case("a daylight sensor gives power by day", function() |
| 155 | local p = test.player("Gus") |
| 156 | local at = row(p, 2) |
| 157 | test.place(p, at[1], "Daylight sensor") |
| 158 | test.place(p, at[2], "Copper lamp") |
| 159 | test.wait(0.5) |
| 160 | test.same(lit(at[2]), world.daylight() >= 0.5) |
| 161 | end) |
| 162 | |
| 163 | test.case("wire climbs a step, and gates touching open together", function() |
| 164 | local p = test.player("Hal") |
| 165 | local at = row(p, 3) |
| 166 | local up = at[3] + vector.create(0, 1, 0) |
| 167 | world.set_block(up, "Air") |
| 168 | world.set_block(up + vector.create(0, 1, 0), "Air") |
| 169 | test.place(p, at[1], "Lever") |
| 170 | test.place(p, at[2], "Copper wire") |
| 171 | test.place(p, up, "Copper wire") |
| 172 | test.place(p, up + vector.create(1, 0, 0), "Copper gate") |
| 173 | test.place(p, up + vector.create(1, 1, 0), "Copper gate") |
| 174 | test.use_block(p, at[1]) |
| 175 | test.wait(0.5) |
| 176 | test.same(world.block(up), "Live wire", "up the step") |
| 177 | test.same(world.block(up + vector.create(1, 0, 0)), "Open copper gate") |
| 178 | test.same(world.block(up + vector.create(1, 1, 0)), "Open copper gate", "the one above too") |
| 179 | test.use_block(p, at[1]) |
| 180 | test.wait(0.5) |
| 181 | test.same(world.block(up + vector.create(1, 1, 0)), "Copper gate", "shut again") |
| 182 | end) |
| 183 | |
| 184 | test.case("a live wire breaks into copper wire", function() |
| 185 | local p = test.player("Ivy", { survival = true, carrying = { Lever = 1, ["Copper wire"] = 1 } }) |
| 186 | local at = row(p, 2) |
| 187 | test.place(p, at[1], "Lever") |
| 188 | test.place(p, at[2], "Copper wire") |
| 189 | test.use_block(p, at[1]) |
| 190 | test.wait(0.5) |
| 191 | test.same(world.block(at[2]), "Live wire") |
| 192 | test.check(test.dig(p, at[2]), "it breaks") |
| 193 | test.same(world.has(p, "Copper wire"), 1) |
| 194 | test.same(world.has(p, "Live wire"), 0) |
| 195 | test.check(test.dig(p, at[1]), "and the lever") |
| 196 | test.same(world.has(p, "Lever"), 1, "a pulled lever breaks into a lever") |
| 197 | end) |
| 198 | |
| 199 | test.case("an inverter fed its own power ticks, and a lamp on it blinks no faster than the knob says", function() |
| 200 | local p = test.player("Jo") |
| 201 | local at = row(p, 3) |
| 202 | -- the inverter at at[2] faces east; wire runs from its front around to its back |
| 203 | local loop = { |
| 204 | at[3], |
| 205 | at[3] + vector.create(0, 0, 1), |
| 206 | at[2] + vector.create(0, 0, 1), |
| 207 | at[1] + vector.create(0, 0, 1), |
| 208 | at[1], |
| 209 | } |
| 210 | for _, w in loop do |
| 211 | world.set_block(w, "Air") |
| 212 | test.place(p, w, "Copper wire") |
| 213 | end |
| 214 | local lamp = at[2] + vector.create(0, 0, 2) |
| 215 | world.set_block(lamp, "Air") |
| 216 | test.place(p, lamp, "Copper lamp") |
| 217 | test.place(p, at[2], "Inverter", "east") |
| 218 | local flips, last = 0, lit(lamp) |
| 219 | for _ = 1, 30 do |
| 220 | test.wait(0.1) |
| 221 | if lit(lamp) ~= last then |
| 222 | flips += 1 |
| 223 | last = lit(lamp) |
| 224 | end |
| 225 | end |
| 226 | test.check(flips >= 3, "it ticks: " .. flips) |
| 227 | test.check(flips <= 13, "but no faster than four times a second: " .. flips) |
| 228 | end) |
| 229 | |
| 230 | local UP = vector.create(0, 1, 0) |
| 231 | |
| 232 | test.case("a piston pushes a line of blocks along, and pulls its head back", function() |
| 233 | local p = test.player("Kit") |
| 234 | local at = row(p, 6) |
| 235 | test.place(p, at[1], "Lever") |
| 236 | test.place(p, at[2], "Piston", "east") |
| 237 | world.set_block(at[3], "Stone") |
| 238 | world.set_block(at[4], "Cobblestone") |
| 239 | test.wait(0.3) |
| 240 | test.same(world.block(at[3]), "Stone", "nothing moves without power") |
| 241 | test.use_block(p, at[1]) |
| 242 | test.wait(0.3) |
| 243 | test.same(world.block(at[3]), "Piston head", "the head is out") |
| 244 | test.same(world.facing(at[3]), "east", "facing the way the piston does") |
| 245 | test.same(world.block(at[4]), "Stone", "the stone pushed one along") |
| 246 | test.same(world.block(at[5]), "Cobblestone", "and the cobblestone past it") |
| 247 | test.same(test.heard("push"), 1, "a push") |
| 248 | test.use_block(p, at[1]) |
| 249 | test.wait(0.3) |
| 250 | test.same(world.block(at[3]), "Air", "the head back in") |
| 251 | test.same(world.block(at[4]), "Stone", "what it pushed stays") |
| 252 | test.same(test.heard("pull"), 1, "a pull") |
| 253 | end) |
| 254 | |
| 255 | test.case("a piston can't push what keeps things, and takes its head when it's broken", function() |
| 256 | local p = test.player("Lu", { survival = true, carrying = { Lever = 1, Piston = 1 } }) |
| 257 | local at = row(p, 4) |
| 258 | test.place(p, at[1], "Lever") |
| 259 | test.place(p, at[2], "Piston", "east") |
| 260 | world.set_block(at[3], "Crate") |
| 261 | test.use_block(p, at[1]) |
| 262 | test.wait(0.3) |
| 263 | test.same(world.block(at[3]), "Crate", "the crate stays") |
| 264 | test.same(test.heard("push"), 0, "no push") |
| 265 | world.set_block(at[3], "Air") |
| 266 | test.use_block(p, at[1]) |
| 267 | test.wait(0.3) |
| 268 | test.use_block(p, at[1]) |
| 269 | test.wait(0.3) |
| 270 | test.same(world.block(at[3]), "Piston head", "with room, it pushes") |
| 271 | test.check(test.dig(p, at[2]), "the piston breaks") |
| 272 | test.same(world.block(at[3]), "Air", "and its head goes with it") |
| 273 | test.same(world.has(p, "Piston"), 1, "it's given back") |
| 274 | test.same(world.has(p, "Piston head"), 0, "but no head") |
| 275 | end) |
| 276 | |
| 277 | test.case("an up piston lifts what's on it", function() |
| 278 | local p = test.player("Mo") |
| 279 | local at = row(p, 2) |
| 280 | for h = 1, 3 do |
| 281 | world.set_block(at[1] + UP * h, "Air") |
| 282 | end |
| 283 | test.place(p, at[2], "Lever") |
| 284 | test.place(p, at[1], "Up piston") |
| 285 | world.set_block(at[1] + UP, "Stone") |
| 286 | test.use_block(p, at[2]) |
| 287 | test.wait(0.3) |
| 288 | test.same(world.block(at[1] + UP), "Up piston head") |
| 289 | test.same(world.block(at[1] + UP * 2), "Stone", "lifted") |
| 290 | test.use_block(p, at[2]) |
| 291 | test.wait(0.3) |
| 292 | test.same(world.block(at[1] + UP), "Air", "the head back down") |
| 293 | end) |
| 294 | |
| 295 | test.case("a note block plays as power reaches it, a step higher each use, in a voice from what it stands on", function() |
| 296 | local p = test.player("Ned") |
| 297 | local at = row(p, 2) |
| 298 | world.set_block(at[2] - UP, "Stone") |
| 299 | test.place(p, at[1], "Lever") |
| 300 | test.place(p, at[2], "Note block") |
| 301 | test.wait(0.3) |
| 302 | test.same(test.heard("bell"), 0, "quiet with no power") |
| 303 | test.use_block(p, at[2]) |
| 304 | test.same(test.shown(p, "note"), "Tuned to G4", "a step up from F#4") |
| 305 | test.same(test.heard("bell"), 1, "a bell on stone") |
| 306 | test.use_block(p, at[1]) |
| 307 | test.wait(0.5) |
| 308 | test.same(test.heard("bell"), 2, "power plays it once") |
| 309 | test.use_block(p, at[1]) |
| 310 | test.wait(0.3) |
| 311 | test.same(test.heard("bell"), 2, "and not as power goes") |
| 312 | world.set_block(at[2] - UP, "Planks") |
| 313 | test.use_block(p, at[2]) |
| 314 | test.same(test.heard("bass"), 1, "a bass on wood") |
| 315 | world.set_block(at[2] - UP, "Glass") |
| 316 | test.use_block(p, at[2]) |
| 317 | test.same(test.heard("chime"), 1, "a chime on glass") |
| 318 | for _ = 1, 10 do |
| 319 | test.use_block(p, at[2]) |
| 320 | end |
| 321 | test.same(test.shown(p, "note"), "Tuned to F#3", "around to the bottom after F#5") |
| 322 | end) |
| 323 | |
| 324 | test.case("a live grid tap powers the wire and lamp beside it", function() |
| 325 | local p = test.player("Ada") |
| 326 | local at = row(p, 4) |
| 327 | test.place(p, at[1], "Grid tap") |
| 328 | test.place(p, at[2], "Copper wire") |
| 329 | test.place(p, at[3], "Copper wire") |
| 330 | test.place(p, at[4], "Copper lamp") |
| 331 | test.wait(0.5) |
| 332 | test.check(not lit(at[4]), "a tap with no power gives none") |
| 333 | -- the Power creation does this when its grid has enough |
| 334 | world.set_block(at[1], "Live grid tap") |
| 335 | test.wait(0.5) |
| 336 | test.same(world.block(at[3]), "Live wire", "the wire is live") |
| 337 | test.check(lit(at[4]), "and the lamp lit") |
| 338 | world.set_block(at[1], "Grid tap") |
| 339 | test.wait(0.5) |
| 340 | test.check(not lit(at[4]), "out when the grid is") |
| 341 | end) |