Generators in four tiers (Pity, Basic, Intermediate, Superior) burn coal, charcoal or logs, only as fast as their grid wants; bigger ones need a chimney. Solar panels, batteries and a breaker join them through power cable. They run lights, an electric furnace, a heater, a sprinkler, a door, a lift and heavy machines; a meter shows the grid. Lights go last. Specification by Phqen1x.
| 1 | --!strict |
| 2 | -- Power's own tests: `potluck-server --test=power`. |
| 3 | |
| 4 | local test = require("@potluck/test@0.9") |
| 5 | local world = require("@potluck/world@0.37") |
| 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 | -- a survival player carrying what a case builds with |
| 21 | local function builder(coal: number, lamps: number) |
| 22 | return test.player("Ada", { |
| 23 | survival = true, |
| 24 | carrying = { Coal = coal, ["Pity generator"] = 1, ["Power cable"] = 4, ["Street lamp"] = lamps, ["Power meter"] = 1 }, |
| 25 | }) |
| 26 | end |
| 27 | |
| 28 | local function lit_lamps(at: { vector }): number |
| 29 | local n = 0 |
| 30 | for _, a in at do |
| 31 | if world.block(a) == "Lit street lamp" then |
| 32 | n += 1 |
| 33 | end |
| 34 | end |
| 35 | return n |
| 36 | end |
| 37 | |
| 38 | test.case("its blocks are in the world", function() |
| 39 | for _, name in { "Power cable", "Live cable", "Pity generator", "Lit pity generator", "Street lamp", "Lit street lamp", "Power meter" } do |
| 40 | test.check(pcall(world.kind, name), name) |
| 41 | end |
| 42 | end) |
| 43 | |
| 44 | test.case("a generator with coal lights a lamp along cable in the dark", function() |
| 45 | test.daytime(0) |
| 46 | local p = builder(4, 8) |
| 47 | local at = row(p, 5) |
| 48 | test.place(p, at[1], "Pity generator") |
| 49 | test.place(p, at[2], "Power cable") |
| 50 | test.place(p, at[3], "Power cable") |
| 51 | test.place(p, at[4], "Street lamp") |
| 52 | test.wait(0.5) |
| 53 | test.check(world.block(at[4]) == "Street lamp", "no fuel, so dark") |
| 54 | test.use_block(p, at[1]) |
| 55 | test.wait(0.5) |
| 56 | test.same(world.has(p, "Coal"), 0, "it takes the coal") |
| 57 | test.same(world.block(at[1]), "Lit pity generator", "burning") |
| 58 | test.same(world.block(at[2]), "Live cable", "cable is live") |
| 59 | test.same(world.block(at[4]), "Lit street lamp", "and the lamp is lit") |
| 60 | end) |
| 61 | |
| 62 | test.case("lamps keep dark by day", function() |
| 63 | test.daytime(0.5) |
| 64 | local p = builder(4, 8) |
| 65 | local at = row(p, 3) |
| 66 | test.place(p, at[1], "Pity generator") |
| 67 | test.place(p, at[2], "Street lamp") |
| 68 | test.use_block(p, at[1]) |
| 69 | test.wait(0.5) |
| 70 | test.same(world.block(at[2]), "Street lamp", "daylight, so no need") |
| 71 | test.same(world.block(at[1]), "Pity generator", "and the generator is idle") |
| 72 | test.daytime(0) |
| 73 | test.wait(0.5) |
| 74 | test.same(world.block(at[2]), "Lit street lamp", "dark now") |
| 75 | end) |
| 76 | |
| 77 | test.case("when there isn't enough, the last lamps go without", function() |
| 78 | test.daytime(0) |
| 79 | local p = builder(4, 8) |
| 80 | local at = row(p, 7) |
| 81 | test.place(p, at[1], "Pity generator") |
| 82 | for i = 2, 7 do |
| 83 | test.place(p, at[i], "Street lamp") |
| 84 | end |
| 85 | test.use_block(p, at[1]) |
| 86 | test.wait(0.5) |
| 87 | -- 20 watts, 4 each: five of the six |
| 88 | local lamps = {} |
| 89 | for i = 2, 7 do |
| 90 | table.insert(lamps, at[i]) |
| 91 | end |
| 92 | test.same(lit_lamps(lamps), 5) |
| 93 | end) |
| 94 | |
| 95 | test.case("it burns out", function() |
| 96 | test.daytime(0) |
| 97 | local p = builder(1, 8) |
| 98 | local at = row(p, 6) |
| 99 | test.place(p, at[1], "Pity generator") |
| 100 | for i = 2, 6 do |
| 101 | test.place(p, at[i], "Street lamp") |
| 102 | end |
| 103 | test.use_block(p, at[1]) |
| 104 | test.wait(2) |
| 105 | test.same(world.block(at[6]), "Lit street lamp", "lit at first") |
| 106 | -- a coal makes 20 watts for a minute |
| 107 | test.wait(70) |
| 108 | test.same(world.block(at[6]), "Street lamp", "dark when the coal's gone") |
| 109 | test.same(world.block(at[1]), "Pity generator") |
| 110 | end) |
| 111 | |
| 112 | test.case("the meter says what a grid makes and uses", function() |
| 113 | test.daytime(0) |
| 114 | local p = builder(2, 8) |
| 115 | local at = row(p, 4) |
| 116 | test.place(p, at[1], "Pity generator") |
| 117 | test.place(p, at[2], "Power meter") |
| 118 | test.place(p, at[3], "Street lamp") |
| 119 | test.use_block(p, at[1]) |
| 120 | test.wait(0.5) |
| 121 | test.use_block(p, at[2]) |
| 122 | local shown = test.shown(p, "power") |
| 123 | test.check(shown ~= nil and string.find(shown, "Making 4 W of 20 W", 1, true) ~= nil, "the meter: " .. tostring(shown)) |
| 124 | end) |
| 125 | |
| 126 | -- a survival player carrying one of everything a tier test needs |
| 127 | local function tester(coal: number, extra: { [string]: number }) |
| 128 | local c: { [string]: number } = { Coal = coal, ["Power cable"] = 8, ["Street lamp"] = 20, ["Power meter"] = 1 } |
| 129 | for k, v in extra do |
| 130 | c[k] = v |
| 131 | end |
| 132 | return test.player("Ada", { survival = true, carrying = c }) |
| 133 | end |
| 134 | |
| 135 | test.case("a basic generator without a chimney makes half", function() |
| 136 | test.daytime(0) |
| 137 | local p = tester(4, { ["Basic generator"] = 1, Chimney = 4 }) |
| 138 | local at = row(p, 14) |
| 139 | test.place(p, at[1], "Basic generator") |
| 140 | for i = 2, 14 do |
| 141 | test.place(p, at[i], "Street lamp") |
| 142 | end |
| 143 | test.use_block(p, at[1]) |
| 144 | test.wait(0.5) |
| 145 | -- 60 W: half is 30 W, seven lamps of 4 |
| 146 | local lamps = {} |
| 147 | for i = 2, 14 do |
| 148 | table.insert(lamps, at[i]) |
| 149 | end |
| 150 | test.same(lit_lamps(lamps), 7, "half power") |
| 151 | end) |
| 152 | |
| 153 | test.case("a basic generator with its chimney makes all of it", function() |
| 154 | test.daytime(0) |
| 155 | local p = tester(4, { ["Basic generator"] = 1, Chimney = 4 }) |
| 156 | local x, z = math.floor(p.position.x) + 3, math.floor(p.position.z) + 6 |
| 157 | local y = world.top(x, z) + 6 |
| 158 | local g = vector.create(x, y, z) |
| 159 | world.set_block(g, "Air") |
| 160 | for i = 1, 4 do |
| 161 | world.set_block(g + vector.create(0, i, 0), "Air") |
| 162 | end |
| 163 | local lamps = {} |
| 164 | for i = 1, 13 do |
| 165 | local a = vector.create(x + i, y, z) |
| 166 | world.set_block(a, "Air") |
| 167 | table.insert(lamps, a) |
| 168 | end |
| 169 | test.place(p, g, "Basic generator") |
| 170 | test.place(p, g + vector.create(0, 1, 0), "Chimney") |
| 171 | test.place(p, g + vector.create(0, 2, 0), "Chimney") |
| 172 | for _, a in lamps do |
| 173 | test.place(p, a, "Street lamp") |
| 174 | end |
| 175 | test.use_block(p, g) |
| 176 | test.wait(0.5) |
| 177 | test.check(lit_lamps(lamps) >= 13, "full power with its chimney") |
| 178 | end) |
| 179 | |
| 180 | test.case("a battery keeps the night's light going", function() |
| 181 | test.daytime(0.5) |
| 182 | local p = tester(4, { ["Pity generator"] = 1, Battery = 1 }) |
| 183 | local at = row(p, 4) |
| 184 | test.place(p, at[1], "Pity generator") |
| 185 | test.place(p, at[2], "Battery") |
| 186 | test.place(p, at[3], "Street lamp") |
| 187 | test.use_block(p, at[1]) |
| 188 | test.wait(0.5) |
| 189 | test.same(world.block(at[3]), "Street lamp", "day, so dark") |
| 190 | test.wait(12) |
| 191 | test.check(world.block(at[2]) ~= "Battery", "it charged") |
| 192 | test.daytime(0) |
| 193 | test.wait(0.5) |
| 194 | test.same(world.block(at[3]), "Lit street lamp", "lit") |
| 195 | end) |
| 196 | |
| 197 | test.case("an open breaker splits a grid", function() |
| 198 | test.daytime(0) |
| 199 | local p = tester(4, { ["Pity generator"] = 1, Breaker = 1 }) |
| 200 | local at = row(p, 4) |
| 201 | test.place(p, at[1], "Pity generator") |
| 202 | test.place(p, at[2], "Breaker") |
| 203 | test.place(p, at[3], "Street lamp") |
| 204 | test.use_block(p, at[1]) |
| 205 | test.wait(0.5) |
| 206 | test.same(world.block(at[3]), "Lit street lamp", "closed passes power") |
| 207 | test.use_block(p, at[2]) |
| 208 | test.wait(0.5) |
| 209 | test.same(world.block(at[2]), "Open breaker", "open") |
| 210 | test.same(world.block(at[3]), "Street lamp", "cut off") |
| 211 | test.use_block(p, at[2]) |
| 212 | test.wait(0.5) |
| 213 | test.same(world.block(at[3]), "Lit street lamp", "closed again") |
| 214 | end) |
| 215 | |
| 216 | test.case("an upgrade kit turns a generator up a tier and keeps its fuel", function() |
| 217 | test.daytime(0) |
| 218 | local p = tester(4, { ["Pity generator"] = 1, ["Basic upgrade kit"] = 1 }) |
| 219 | local at = row(p, 2) |
| 220 | test.place(p, at[1], "Pity generator") |
| 221 | test.use_block(p, at[1]) |
| 222 | test.hold(p, "Basic upgrade kit") |
| 223 | test.use_block(p, at[1]) |
| 224 | test.wait(0.5) |
| 225 | local b = world.block(at[1]) |
| 226 | test.check(b == "Basic generator" or b == "Lit basic generator", "now basic: " .. b) |
| 227 | test.same(world.has(p, "Basic upgrade kit"), 0, "the kit is used") |
| 228 | end) |
| 229 | |
| 230 | -- a 3x3 of ground-level spots in the air east of a player, cleared |
| 231 | local function square(p) |
| 232 | local x, z = math.floor(p.position.x) + 4, math.floor(p.position.z) + 4 |
| 233 | local y = world.top(x, z) + 5 |
| 234 | local out = {} |
| 235 | for dx = -1, 1 do |
| 236 | for dz = -1, 1 do |
| 237 | local at = vector.create(x + dx, y, z + dz) |
| 238 | world.set_block(at, "Air") |
| 239 | world.set_block(at + vector.create(0, 1, 0), "Air") |
| 240 | out[dx * 3 + dz] = at |
| 241 | end |
| 242 | end |
| 243 | return out, vector.create(x, y, z) |
| 244 | end |
| 245 | |
| 246 | local function array(p) |
| 247 | local at, mid = square(p) |
| 248 | for dx = -1, 1 do |
| 249 | for dz = -1, 1 do |
| 250 | if dx ~= 0 or dz ~= 0 then |
| 251 | test.place(p, at[dx * 3 + dz], "Solar panel") |
| 252 | end |
| 253 | end |
| 254 | end |
| 255 | test.place(p, at[0], "Tracker mast") |
| 256 | local meter = mid + vector.create(2, 0, 0) |
| 257 | world.set_block(meter, "Air") |
| 258 | test.place(p, meter, "Power meter") |
| 259 | return mid, meter |
| 260 | end |
| 261 | |
| 262 | local function sun(extra: { [string]: number }) |
| 263 | local c: { [string]: number } = { ["Solar panel"] = 8, ["Tracker mast"] = 1, ["Power meter"] = 1 } |
| 264 | for k, v in extra do |
| 265 | c[k] = v |
| 266 | end |
| 267 | return test.player("Ada", { survival = true, carrying = c }) |
| 268 | end |
| 269 | |
| 270 | test.case("a tracker array makes 96 W at noon and nothing at night", function() |
| 271 | test.daytime(0.5) |
| 272 | local p = sun({}) |
| 273 | local _, meter = array(p) |
| 274 | test.wait(1) |
| 275 | test.use_block(p, meter) |
| 276 | local shown = test.shown(p, "power") |
| 277 | test.check(shown ~= nil and string.find(shown, "Solar 96 W", 1, true) ~= nil, "noon: " .. tostring(shown)) |
| 278 | test.daytime(0) |
| 279 | test.wait(1) |
| 280 | test.use_block(p, meter) |
| 281 | shown = test.shown(p, "power") |
| 282 | test.check(shown ~= nil and string.find(shown, "Solar 0 W", 1, true) ~= nil, "night: " .. tostring(shown)) |
| 283 | end) |
| 284 | |
| 285 | test.case("rain thins the sun and a roof shuts it out", function() |
| 286 | test.daytime(0.5) |
| 287 | local p = sun({ Stone = 4 }) |
| 288 | local mid, meter = array(p) |
| 289 | local sky = require("@potluck/sky@0.2") |
| 290 | sky.set_weather("rain", { strength = 0.5, over = 0 }) |
| 291 | test.wait(1) |
| 292 | test.use_block(p, meter) |
| 293 | local shown = test.shown(p, "power") |
| 294 | test.check(shown ~= nil and string.find(shown, "Solar 29 W", 1, true) ~= nil, "rain: " .. tostring(shown)) |
| 295 | sky.set_weather("clear", { over = 0 }) |
| 296 | for dx = -2, 2 do |
| 297 | for dz = -2, 2 do |
| 298 | local a = mid + vector.create(dx, 2, dz) |
| 299 | world.set_block(a, "Stone") |
| 300 | end |
| 301 | end |
| 302 | test.wait(2.5) |
| 303 | test.use_block(p, meter) |
| 304 | shown = test.shown(p, "power") |
| 305 | test.check(shown ~= nil and string.find(shown, "Solar 0 W", 1, true) ~= nil, "roofed: " .. tostring(shown)) |
| 306 | end) |
| 307 | |
| 308 | test.case("lights come on in the dark and a grow lamp feeds the crops", function() |
| 309 | test.daytime(0) |
| 310 | local p = builder(4, 0) |
| 311 | local at = row(p, 7) |
| 312 | local extra = { "Wall lantern", "Floodlight", "Red neon tube", "Grow lamp" } |
| 313 | world.give(p, "Basic generator", 1) |
| 314 | test.place(p, at[1], "Basic generator") |
| 315 | for i, n in extra do |
| 316 | world.give(p, n, 1) |
| 317 | test.place(p, at[i + 1], n) |
| 318 | end |
| 319 | test.use_block(p, at[1]) |
| 320 | test.wait(0.5) |
| 321 | test.same(world.block(at[2]), "Lit wall lantern") |
| 322 | test.same(world.block(at[3]), "Lit floodlight") |
| 323 | test.same(world.block(at[4]), "Lit red neon tube") |
| 324 | test.same(world.block(at[5]), "Lit grow lamp") |
| 325 | end) |
| 326 | |
| 327 | test.case("a heater and a sprinkler run on a grid", function() |
| 328 | test.daytime(0) |
| 329 | local p = builder(4, 0) |
| 330 | local at = row(p, 4) |
| 331 | world.give(p, "Basic generator", 1) |
| 332 | world.give(p, "Heater", 1) |
| 333 | test.place(p, at[1], "Basic generator") |
| 334 | test.place(p, at[2], "Heater") |
| 335 | test.use_block(p, at[1]) |
| 336 | test.wait(0.5) |
| 337 | test.same(world.block(at[2]), "Lit heater", "night, so it warms") |
| 338 | test.daytime(0.5) |
| 339 | test.wait(0.5) |
| 340 | test.same(world.block(at[2]), "Heater", "day, so it rests") |
| 341 | end) |
| 342 | |
| 343 | test.case("an electric furnace comes on with power (Furnaces smelts in it)", function() |
| 344 | test.daytime(0) |
| 345 | local p = builder(4, 0) |
| 346 | local at = row(p, 3) |
| 347 | world.give(p, "Basic generator", 1) |
| 348 | world.give(p, "Electric furnace", 1) |
| 349 | world.give(p, "Raw copper", 1) |
| 350 | test.place(p, at[1], "Basic generator") |
| 351 | test.place(p, at[2], "Electric furnace") |
| 352 | test.use_block(p, at[1]) |
| 353 | test.wait(0.5) |
| 354 | test.same(world.block(at[2]), "Lit electric furnace", "powered") |
| 355 | end) |
| 356 | |
| 357 | test.case("a beacon tower is the last light to go and takes 250 W", function() |
| 358 | test.daytime(0) |
| 359 | local p = builder(4, 0) |
| 360 | local at = row(p, 4) |
| 361 | world.give(p, "Superior generator", 1) |
| 362 | world.give(p, "Beacon tower", 1) |
| 363 | world.give(p, "Power meter", 1) |
| 364 | test.place(p, at[1], "Superior generator") |
| 365 | test.place(p, at[2], "Beacon tower") |
| 366 | test.place(p, at[3], "Power meter") |
| 367 | test.use_block(p, at[1]) |
| 368 | test.wait(0.5) |
| 369 | test.same(world.block(at[2]), "Lit beacon tower", "lit") |
| 370 | test.use_block(p, at[3]) |
| 371 | local shown = test.shown(p, "power") |
| 372 | test.check(shown ~= nil and string.find(shown, "Using 250 W", 1, true) ~= nil, "uses 250: " .. tostring(shown)) |
| 373 | end) |
| 374 | |
| 375 | -- balance: each tier's coal per hour at full tilt, and what a coal is worth |
| 376 | test.case("balance: coal per hour rises with the tier, and every tier gives the same energy per coal", function() |
| 377 | test.daytime(0) |
| 378 | for _, name in { "Pity generator", "Basic generator", "Intermediate generator", "Superior generator" } do |
| 379 | test.check(pcall(world.kind, name), name) |
| 380 | end |
| 381 | -- a Pity burns one coal a minute for 20 W; Superior 20 times the power |
| 382 | -- for 20 times the coal, so a coal is always 1200 watt seconds |
| 383 | local p = builder(1, 8) |
| 384 | local at = row(p, 7) |
| 385 | test.place(p, at[1], "Pity generator") |
| 386 | for i = 2, 7 do |
| 387 | test.place(p, at[i], "Street lamp") |
| 388 | end |
| 389 | test.use_block(p, at[1]) |
| 390 | test.wait(2) |
| 391 | local lamps = {} |
| 392 | for i = 2, 7 do |
| 393 | table.insert(lamps, at[i]) |
| 394 | end |
| 395 | test.same(lit_lamps(lamps), 5, "20 W runs five 4 W lamps") |
| 396 | end) |
| 397 | |
| 398 | -- a Superior generator and a machine beside it |
| 399 | local function machine(name: string, held: { [string]: number }) |
| 400 | test.daytime(0) |
| 401 | local p = builder(4, 0) |
| 402 | world.give(p, "Superior generator", 1) |
| 403 | world.give(p, name, 1) |
| 404 | for k, v in held do |
| 405 | world.give(p, k, v) |
| 406 | end |
| 407 | local at = row(p, 3) |
| 408 | test.place(p, at[1], "Superior generator") |
| 409 | test.place(p, at[2], name) |
| 410 | test.use_block(p, at[1]) |
| 411 | return p, at |
| 412 | end |
| 413 | |
| 414 | test.case("a crusher wakes when used and crushes stone", function() |
| 415 | local p, at = machine("Rock crusher", { Stone = 4 }) |
| 416 | test.hold(p, "Stone") |
| 417 | test.use_block(p, at[2]) |
| 418 | test.wait(0.5) |
| 419 | test.use_block(p, at[2]) |
| 420 | test.same(world.has(p, "Gravel"), 4, "gravel") |
| 421 | end) |
| 422 | |
| 423 | test.case("an arc furnace smelts raw ore at once", function() |
| 424 | local p, at = machine("Arc furnace", { ["Raw copper"] = 3 }) |
| 425 | test.hold(p, "Raw copper") |
| 426 | test.use_block(p, at[2]) |
| 427 | test.wait(0.5) |
| 428 | test.use_block(p, at[2]) |
| 429 | test.same(world.has(p, "Copper ingot"), 3, "ingots") |
| 430 | end) |
| 431 | |
| 432 | test.case("a pump fills a bucket", function() |
| 433 | local p, at = machine("Pump", { Bucket = 1 }) |
| 434 | test.hold(p, "Bucket") |
| 435 | test.use_block(p, at[2]) |
| 436 | test.wait(0.5) |
| 437 | test.use_block(p, at[2]) |
| 438 | test.same(world.has(p, "Water bucket"), 1, "water") |
| 439 | end) |
| 440 | |
| 441 | test.case("a powered door opens for someone close and shuts when they leave", function() |
| 442 | local p, at = machine("Powered door", {}) |
| 443 | test.wait(0.5) |
| 444 | test.same(world.block(at[2]), "Open powered door", "open") |
| 445 | test.move(p, p.position + vector.create(30, 0, 0)) |
| 446 | test.wait(0.5) |
| 447 | test.same(world.block(at[2]), "Powered door", "shut") |
| 448 | end) |
| 449 | |
| 450 | test.case("a quarry digs a shaft and hands over what it digs", function() |
| 451 | local p, at = machine("Quarry drill", {}) |
| 452 | test.use_block(p, at[2]) |
| 453 | test.wait(0.5) |
| 454 | test.use_block(p, at[2]) |
| 455 | test.wait(6) |
| 456 | local below = at[2] - vector.create(0, 1, 0) |
| 457 | test.same(world.block(below), "Air", "dug") |
| 458 | end) |
| 459 | |
| 460 | test.case("a lift pad carries you to the next one", function() |
| 461 | local p, at = machine("Lift pad", {}) |
| 462 | local up = at[2] + vector.create(0, 6, 0) |
| 463 | world.set_block(up, "Air") |
| 464 | world.give(p, "Lift pad", 1) |
| 465 | test.place(p, up, "Lift pad") |
| 466 | test.use_block(p, at[2]) |
| 467 | test.wait(0.5) |
| 468 | test.use_block(p, at[2]) |
| 469 | local shown = test.shown(p, "power") |
| 470 | test.check(shown == "Lift: going up", "lift: " .. tostring(shown)) |
| 471 | end) |
| 472 | |
| 473 | test.case("another creation's load takes watts from a grid and is told what it got", function() |
| 474 | local pw = require("@potluck/power") |
| 475 | test.daytime(0) |
| 476 | local p = builder(4, 0) |
| 477 | local at = row(p, 3) |
| 478 | test.place(p, at[1], "Pity generator") |
| 479 | test.use_block(p, at[1]) |
| 480 | pw.consume(at[2], 15, 3) |
| 481 | test.wait(0.5) |
| 482 | test.same(pw.got(at[2]), 15, "it got what it asked") |
| 483 | pw.consume(at[2], 60, 3) |
| 484 | test.wait(0.5) |
| 485 | test.same(pw.got(at[2]), 0, "too much, so nothing") |
| 486 | pw.release(at[2]) |
| 487 | end) |