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: generators, cables and things that need watts, next to Circuits. |
| 3 | -- Specification by Phqen1x; plan in docs/power.md. |
| 4 | -- |
| 5 | -- Circuits carries a signal, on or off. Power carries watts, with a budget. |
| 6 | -- Power cable joins into grids (with anything that gives or uses power |
| 7 | -- touching it). A grid's generators burn fuel only as fast as its loads |
| 8 | -- want; when they want more than there is, the least important loads go |
| 9 | -- without (lights are last to go). A Grid tap, which Circuits owns, turns on |
| 10 | -- when its grid has enough, so a lever and copper wire can read the grid. |
| 11 | -- |
| 12 | -- Everything here is worked out ten times a second, and the grids only again |
| 13 | -- when something is put down or taken away. What the generators hold is |
| 14 | -- kept in the world's store. |
| 15 | |
| 16 | local world = require("@potluck/world@0.37") |
| 17 | local tweak = require("@potluck/tweak") |
| 18 | local store = require("@potluck/store") |
| 19 | local ui = require("@potluck/ui@0.2") |
| 20 | local sky = require("@potluck/sky@0.2") |
| 21 | local power = require("@potluck/power") |
| 22 | |
| 23 | local knobs = tweak { |
| 24 | dusk = { 0.3, min = 0.05, max = 0.9, label = "How dark it is when lamps come on", order = 1 }, |
| 25 | burn = { 1, min = 0.25, max = 4, label = "How fast generators burn fuel", order = 2 }, |
| 26 | lamp = { 4, min = 1, max = 20, label = "Watts a street lamp takes", order = 3 }, |
| 27 | solar = { 8, min = 1, max = 40, label = "Watts a solar panel makes at noon", order = 4 }, |
| 28 | lantern = { 2, min = 1, max = 10, label = "Watts a wall lantern takes", order = 5 }, |
| 29 | flood = { 12, min = 2, max = 50, label = "Watts a floodlight takes", order = 6 }, |
| 30 | neon = { 3, min = 1, max = 20, label = "Watts a neon tube takes", order = 7 }, |
| 31 | grow = { 10, min = 2, max = 50, label = "Watts a grow lamp takes", order = 8 }, |
| 32 | heater = { 20, min = 5, max = 100, label = "Watts a heater takes", order = 9 }, |
| 33 | crusher_w = { 120, min = 1, max = 2000, label = "Watts a rock crusher takes", order = 20 }, |
| 34 | arc_w = { 300, min = 1, max = 2000, label = "Watts a arc furnace takes", order = 21 }, |
| 35 | door_w = { 5, min = 1, max = 2000, label = "Watts a powered door takes", order = 22 }, |
| 36 | lift_w = { 40, min = 1, max = 2000, label = "Watts a lift pad takes", order = 23 }, |
| 37 | pump_w = { 25, min = 1, max = 2000, label = "Watts a pump takes", order = 24 }, |
| 38 | quarry_w = { 400, min = 1, max = 2000, label = "Watts a quarry drill takes", order = 25 }, |
| 39 | greenhouse_w = { 350, min = 1, max = 2000, label = "Watts a greenhouse array takes", order = 26 }, |
| 40 | tele_w = { 600, min = 1, max = 2000, label = "Watts a teleport pad takes", order = 27 }, |
| 41 | beacon = { 250, min = 50, max = 1000, label = "Watts a beacon tower takes", order = 12 }, |
| 42 | furnace = { 30, min = 5, max = 100, label = "Watts an electric furnace takes", order = 11 }, |
| 43 | sprinkler = { 8, min = 2, max = 40, label = "Watts a sprinkler takes", order = 10 }, |
| 44 | } |
| 45 | |
| 46 | -- Generators by tier. `watts` is what it makes at full tilt, `seconds` how |
| 47 | -- long one coal lasts at that, and `held` how many coal's worth it holds. |
| 48 | -- `chimney` is how many Chimney blocks it needs stacked on top, open to the |
| 49 | -- sky, or it makes half as much. |
| 50 | type Tier = { name: string, lit: string, watts: number, seconds: number, held: number, chimney: number } |
| 51 | local TIERS: { Tier } = { |
| 52 | { name = "Pity generator", lit = "Lit pity generator", watts = 20, seconds = 60, held = 16, chimney = 0 }, |
| 53 | { name = "Basic generator", lit = "Lit basic generator", watts = 60, seconds = 60, held = 32, chimney = 2 }, |
| 54 | { name = "Intermediate generator", lit = "Lit mid generator", watts = 160, seconds = 60, held = 64, chimney = 3 }, |
| 55 | { name = "Superior generator", lit = "Lit superior generator", watts = 400, seconds = 60, held = 128, chimney = 6 }, |
| 56 | } |
| 57 | -- Upgrade kits, by the tier they make. |
| 58 | local KITS: { [string]: number } = { |
| 59 | ["Basic upgrade kit"] = 2, |
| 60 | ["Intermediate upgrade kit"] = 3, |
| 61 | ["Superior upgrade kit"] = 4, |
| 62 | } |
| 63 | -- A battery holds this many watt-seconds, takes at most CHARGE_RATE watts and |
| 64 | -- gives at most GIVE_RATE. |
| 65 | local BATTERY_MAX, CHARGE_RATE, GIVE_RATE = 1200, 30, 60 |
| 66 | local BATTERY_LOOKS = { "Battery", "Charged battery", "Full battery" } |
| 67 | |
| 68 | -- What each part is, by its blocks' names: [off, on]. |
| 69 | local BLOCKS: { [string]: { string } } = { |
| 70 | cable = { "Power cable", "Live cable" }, |
| 71 | lamp = { "Street lamp", "Lit street lamp" }, |
| 72 | meter = { "Power meter" }, |
| 73 | tap = { "Grid tap", "Live grid tap" }, |
| 74 | breaker = { "Open breaker", "Breaker" }, |
| 75 | solar = { "Dark solar panel", "Solar panel" }, |
| 76 | mast = { "Tracker mast" }, |
| 77 | lantern = { "Wall lantern", "Lit wall lantern" }, |
| 78 | flood = { "Floodlight", "Lit floodlight" }, |
| 79 | neonr = { "Red neon tube", "Lit red neon tube" }, |
| 80 | neong = { "Green neon tube", "Lit green neon tube" }, |
| 81 | neonb = { "Blue neon tube", "Lit blue neon tube" }, |
| 82 | grow = { "Grow lamp", "Lit grow lamp" }, |
| 83 | heater = { "Heater", "Lit heater" }, |
| 84 | sprinkler = { "Sprinkler", "Lit sprinkler" }, |
| 85 | efurnace = { "Electric furnace", "Lit electric furnace" }, |
| 86 | beacon = { "Beacon tower", "Lit beacon tower" }, |
| 87 | crusher = { "Rock crusher", "Lit rock crusher" }, |
| 88 | arc = { "Arc furnace", "Lit arc furnace" }, |
| 89 | door = { "Powered door", "Open powered door" }, |
| 90 | lift = { "Lift pad", "Lit lift pad" }, |
| 91 | pump = { "Pump", "Lit pump" }, |
| 92 | quarry = { "Quarry drill", "Lit quarry drill" }, |
| 93 | greenhouse = { "Greenhouse array", "Lit greenhouse array" }, |
| 94 | tele = { "Teleport pad", "Lit teleport pad" }, |
| 95 | battery = { "Battery", "Charged battery", "Full battery" }, |
| 96 | } |
| 97 | local KIND: { [string]: { kind: string, on: boolean, tier: number? } } = {} |
| 98 | for kind, names in BLOCKS do |
| 99 | for i, name in names do |
| 100 | KIND[name] = { kind = kind, on = i > 1 } |
| 101 | end |
| 102 | end |
| 103 | for i, t in TIERS do |
| 104 | KIND[t.name] = { kind = "gen", on = false, tier = i } |
| 105 | KIND[t.lit] = { kind = "gen", on = true, tier = i } |
| 106 | end |
| 107 | |
| 108 | -- What burns, and how much of a coal each is worth. |
| 109 | local FUELS: { { name: string, worth: number } } = { |
| 110 | { name = "Coal", worth = 1 }, |
| 111 | { name = "Charcoal", worth = 1 }, |
| 112 | { name = "Ember", worth = 0.75 }, |
| 113 | { name = "Peat", worth = 0.6 }, |
| 114 | { name = "Oak log", worth = 0.25 }, |
| 115 | { name = "Birch log", worth = 0.25 }, |
| 116 | { name = "Pine log", worth = 0.25 }, |
| 117 | { name = "Maple log", worth = 0.25 }, |
| 118 | { name = "Willow log", worth = 0.25 }, |
| 119 | { name = "Palm log", worth = 0.25 }, |
| 120 | { name = "Redwood log", worth = 0.25 }, |
| 121 | { name = "Planks", worth = 0.125 }, |
| 122 | } |
| 123 | |
| 124 | -- Loads shed last-class first: lights, farm, machines, heavy. |
| 125 | local CLASSES = 4 |
| 126 | local LOADS: { [string]: { class: number } } = { |
| 127 | lamp = { class = 1 }, |
| 128 | lantern = { class = 1 }, |
| 129 | flood = { class = 1 }, |
| 130 | neonr = { class = 1 }, |
| 131 | neong = { class = 1 }, |
| 132 | neonb = { class = 1 }, |
| 133 | grow = { class = 2 }, |
| 134 | sprinkler = { class = 2 }, |
| 135 | heater = { class = 3 }, |
| 136 | efurnace = { class = 3 }, |
| 137 | beacon = { class = 4 }, |
| 138 | crusher = { class = 4 }, |
| 139 | arc = { class = 4 }, |
| 140 | door = { class = 3 }, |
| 141 | lift = { class = 3 }, |
| 142 | pump = { class = 3 }, |
| 143 | quarry = { class = 4 }, |
| 144 | greenhouse = { class = 2 }, |
| 145 | tele = { class = 4 }, |
| 146 | mast = { class = 2 }, |
| 147 | } |
| 148 | local MAST_W = 3 |
| 149 | |
| 150 | local SIDES = { |
| 151 | vector.create(1, 0, 0), |
| 152 | vector.create(-1, 0, 0), |
| 153 | vector.create(0, 1, 0), |
| 154 | vector.create(0, -1, 0), |
| 155 | vector.create(0, 0, 1), |
| 156 | vector.create(0, 0, -1), |
| 157 | } |
| 158 | -- Cable also joins cable a step up or down. |
| 159 | local STEPS = {} |
| 160 | for _, d in { vector.create(1, 0, 0), vector.create(-1, 0, 0), vector.create(0, 0, 1), vector.create(0, 0, -1) } do |
| 161 | table.insert(STEPS, d + vector.create(0, 1, 0)) |
| 162 | table.insert(STEPS, d - vector.create(0, 1, 0)) |
| 163 | end |
| 164 | |
| 165 | -- One part: what it is, where, whether it's on, and for a generator its |
| 166 | -- tier, the fuel it holds (in coal) and the watts it makes now; for a load |
| 167 | -- the watts it gets (0 to its draw). |
| 168 | type Part = { |
| 169 | at: vector, |
| 170 | kind: string, |
| 171 | on: boolean, |
| 172 | tier: number, |
| 173 | fuel: number, |
| 174 | out: number, |
| 175 | wants: number, |
| 176 | got: number, |
| 177 | class: number, |
| 178 | flue: number, |
| 179 | awake: number, |
| 180 | run: boolean, |
| 181 | idx: number, |
| 182 | owner: string, |
| 183 | } |
| 184 | local parts: { [string]: Part } = {} |
| 185 | -- A grid: everything one piece of cable can reach, and what's on it. |
| 186 | type Grid = { nodes: { Part }, making: number, using: number, cap: number, fuel: number, devices: number, stored: number, room: number, solar: number, extra: { Part } } |
| 187 | local grids: { Grid } = {} |
| 188 | local grid_of: { [string]: Grid } = {} |
| 189 | local shaped = false |
| 190 | local changed = false |
| 191 | |
| 192 | local function key(at: vector): string |
| 193 | return math.floor(at.x) .. "," .. math.floor(at.y) .. "," .. math.floor(at.z) |
| 194 | end |
| 195 | |
| 196 | local function middle(at: vector): vector |
| 197 | return vector.create(math.floor(at.x) + 0.5, math.floor(at.y) + 0.5, math.floor(at.z) + 0.5) |
| 198 | end |
| 199 | |
| 200 | local function add(at: vector, block: string): Part? |
| 201 | local k = KIND[block] |
| 202 | if not k then |
| 203 | return nil |
| 204 | end |
| 205 | local p: Part = { |
| 206 | at = middle(at), |
| 207 | kind = k.kind, |
| 208 | on = k.on, |
| 209 | tier = k.tier or 1, |
| 210 | fuel = 0, |
| 211 | out = 0, |
| 212 | wants = 0, |
| 213 | got = 0, |
| 214 | class = if LOADS[k.kind] then LOADS[k.kind].class else 1, |
| 215 | flue = 1, |
| 216 | awake = 0, |
| 217 | run = false, |
| 218 | idx = 0, |
| 219 | owner = "", |
| 220 | } |
| 221 | parts[key(at)] = p |
| 222 | shaped, changed = true, true |
| 223 | return p |
| 224 | end |
| 225 | |
| 226 | -- Picks up the parts beside one that was just put down, and theirs. |
| 227 | local function join(from: vector) |
| 228 | local todo = { from } |
| 229 | local n = 0 |
| 230 | while #todo > 0 and n < 512 do |
| 231 | local at = table.remove(todo) :: vector |
| 232 | local p = parts[key(at)] |
| 233 | for _, list in { SIDES, STEPS } do |
| 234 | for _, d in list do |
| 235 | local q = at + d |
| 236 | if not parts[key(q)] then |
| 237 | local b = world.block(q) |
| 238 | local k = KIND[b] |
| 239 | if k and (list == SIDES or (k.kind == "cable" and p ~= nil and p.kind == "cable")) and add(q, b) then |
| 240 | n += 1 |
| 241 | table.insert(todo, q) |
| 242 | end |
| 243 | end |
| 244 | end |
| 245 | end |
| 246 | end |
| 247 | end |
| 248 | |
| 249 | -- Parts into grids: anything touching cable or another part is on its grid. |
| 250 | local function shape() |
| 251 | shaped = false |
| 252 | grids, grid_of = {}, {} |
| 253 | for k, p in parts do |
| 254 | if grid_of[k] then |
| 255 | continue |
| 256 | end |
| 257 | local g: Grid = { nodes = {}, making = 0, using = 0, cap = 0, fuel = 0, devices = 0, stored = 0, room = 0, solar = 0, extra = {} } |
| 258 | table.insert(grids, g) |
| 259 | local todo = { p } |
| 260 | grid_of[k] = g |
| 261 | while #todo > 0 do |
| 262 | local q = table.remove(todo) :: Part |
| 263 | table.insert(g.nodes, q) |
| 264 | for _, list in { SIDES, STEPS } do |
| 265 | for _, d in list do |
| 266 | local o = q.at + d |
| 267 | local ok = key(o) |
| 268 | local other = parts[ok] |
| 269 | local cut = (q.kind == "breaker" and not q.on) or (other ~= nil and other.kind == "breaker" and not other.on) |
| 270 | if other and not cut and not grid_of[ok] and (list == SIDES or (q.kind == "cable" and other.kind == "cable")) then |
| 271 | grid_of[ok] = g |
| 272 | table.insert(todo, other) |
| 273 | end |
| 274 | end |
| 275 | end |
| 276 | end |
| 277 | end |
| 278 | end |
| 279 | |
| 280 | -- Shows a part's state with its own block. |
| 281 | local function show(p: Part, on: boolean) |
| 282 | local want: string |
| 283 | if p.kind == "gen" then |
| 284 | local t = TIERS[p.tier] |
| 285 | want = if on then t.lit else t.name |
| 286 | elseif p.kind == "battery" then |
| 287 | local f = p.fuel / BATTERY_MAX |
| 288 | want = BATTERY_LOOKS[if f < 0.1 then 1 elseif f < 0.9 then 2 else 3] |
| 289 | else |
| 290 | local names = BLOCKS[p.kind] |
| 291 | want = names[if on and #names > 1 then 2 else 1] |
| 292 | end |
| 293 | if world.block(p.at) ~= want then |
| 294 | world.set_block(p.at, want) |
| 295 | end |
| 296 | end |
| 297 | |
| 298 | -- What each kind of light takes. |
| 299 | local function draw(kind: string): number |
| 300 | if kind == "lamp" then |
| 301 | return knobs.lamp |
| 302 | elseif kind == "lantern" then |
| 303 | return knobs.lantern |
| 304 | elseif kind == "flood" then |
| 305 | return knobs.flood |
| 306 | end |
| 307 | return knobs.neon |
| 308 | end |
| 309 | |
| 310 | -- Whether a lamp wants to be on: it's dark, or a live wire beside it says so. |
| 311 | local function wants_on(p: Part): boolean |
| 312 | if world.daylight(p.at) < knobs.dusk then |
| 313 | return true |
| 314 | end |
| 315 | for _, d in SIDES do |
| 316 | if world.block(p.at + d) == "Live wire" then |
| 317 | return true |
| 318 | end |
| 319 | end |
| 320 | return false |
| 321 | end |
| 322 | |
| 323 | -- How much of its power a generator gets: half without its chimney open to the sky. |
| 324 | local function flue(p: Part): number |
| 325 | local need = TIERS[p.tier].chimney |
| 326 | if need == 0 then |
| 327 | return 1 |
| 328 | end |
| 329 | local x, y, z = p.at.x, math.floor(p.at.y), p.at.z |
| 330 | local n = 0 |
| 331 | while n < need and world.block(vector.create(x, y + 1 + n, z)) == "Chimney" do |
| 332 | n += 1 |
| 333 | end |
| 334 | if n < need or world.top(x, z) > y + n then |
| 335 | return 0.5 |
| 336 | end |
| 337 | return 1 |
| 338 | end |
| 339 | |
| 340 | -- Machines wake when used and sleep after a while, so they draw power only |
| 341 | -- when they're being used. A door draws while someone's near it, a |
| 342 | -- greenhouse in the dark, a teleport pad while it charges. |
| 343 | local MACHINE: { [string]: boolean } = { crusher = true, arc = true, door = true, lift = true, pump = true, quarry = true, greenhouse = true, tele = true } |
| 344 | local TELE_COST = 18000 |
| 345 | |
| 346 | local function machine_wants(p: Part): number |
| 347 | local w = (knobs :: any)[p.kind .. "_w"] :: number |
| 348 | if p.kind == "door" then |
| 349 | return if world.nearest_player(p.at, 6) then w else 0 |
| 350 | elseif p.kind == "greenhouse" then |
| 351 | return if world.daylight(p.at) < 0.6 then w else 0 |
| 352 | elseif p.kind == "tele" then |
| 353 | return if p.fuel < TELE_COST then w else 0 |
| 354 | end |
| 355 | return if world.time() < p.awake then w else 0 |
| 356 | end |
| 357 | |
| 358 | local ticks = 0 |
| 359 | local external: { [string]: Part } = {} |
| 360 | |
| 361 | -- How much sky a panel sees: all of it, half through leaves, none under a roof. |
| 362 | local function sky_open(p: Part): number |
| 363 | local x, y, z = p.at.x, math.floor(p.at.y), p.at.z |
| 364 | local top = world.top(x, z) |
| 365 | if top <= y then |
| 366 | return 1 |
| 367 | end |
| 368 | local seen = 1 |
| 369 | for h = y + 1, math.min(top, y + 24) do |
| 370 | local b = world.block(vector.create(x, h, z)) |
| 371 | if b ~= "Air" then |
| 372 | if string.find(b, "leaves", 1, true) or b == "Water" then |
| 373 | seen = 0.5 |
| 374 | else |
| 375 | return 0 |
| 376 | end |
| 377 | end |
| 378 | end |
| 379 | return seen |
| 380 | end |
| 381 | |
| 382 | -- What the weather lets through. |
| 383 | local WEATHER: { [string]: number } = { clear = 1, cloudy = 0.8, rain = 0.3, storm = 0.15, snow = 0.5, fog = 0.6 } |
| 384 | |
| 385 | -- One tick of a grid: what's wanted, what the generators and batteries make, |
| 386 | -- who gets it, and what the surplus charges. |
| 387 | local function work(g: Grid, dt: number) |
| 388 | local loads: { { Part } } = {} |
| 389 | for c = 1, CLASSES do |
| 390 | loads[c] = {} |
| 391 | end |
| 392 | local cap, devices, give, solar = 0, 0, 0, 0 |
| 393 | local weather = WEATHER[sky.weather().kind] or 1 |
| 394 | local batteries: { Part } = {} |
| 395 | for _, p in g.nodes do |
| 396 | if p.kind == "gen" then |
| 397 | if ticks % 20 == 0 then |
| 398 | p.flue = flue(p) |
| 399 | end |
| 400 | if p.fuel > 0 then |
| 401 | cap += TIERS[p.tier].watts * p.flue |
| 402 | end |
| 403 | devices += 1 |
| 404 | elseif p.kind == "solar" then |
| 405 | if ticks % 20 == 0 then |
| 406 | p.flue = sky_open(p) |
| 407 | end |
| 408 | local bonus = 1 |
| 409 | for dx = -1, 1 do |
| 410 | for dz = -1, 1 do |
| 411 | local m = parts[key(p.at + vector.create(dx, 0, dz))] |
| 412 | if m and m.kind == "mast" and m.got > 0 then |
| 413 | bonus = 1.5 |
| 414 | end |
| 415 | end |
| 416 | end |
| 417 | p.out = knobs.solar * world.daylight(p.at) * weather * p.flue * bonus |
| 418 | solar += p.out |
| 419 | devices += 1 |
| 420 | elseif p.kind == "battery" then |
| 421 | table.insert(batteries, p) |
| 422 | give += math.min(GIVE_RATE, p.fuel / dt) |
| 423 | devices += 1 |
| 424 | elseif LOADS[p.kind] then |
| 425 | if p.kind == "mast" then |
| 426 | p.wants = if world.daylight(p.at) > 0.05 then MAST_W else 0 |
| 427 | elseif p.kind == "heater" then |
| 428 | local winter = sky.season() == "winter" |
| 429 | p.wants = if winter or world.daylight(p.at) < 0.3 then knobs.heater else 0 |
| 430 | elseif MACHINE[p.kind] then |
| 431 | p.wants = machine_wants(p) |
| 432 | elseif p.kind == "beacon" then |
| 433 | p.wants = if wants_on(p) then knobs.beacon else 0 |
| 434 | elseif p.kind == "efurnace" then |
| 435 | p.wants = knobs.furnace |
| 436 | elseif p.kind == "sprinkler" then |
| 437 | p.wants = if world.daylight(p.at) > 0.2 then knobs.sprinkler else 0 |
| 438 | elseif p.kind == "grow" then |
| 439 | p.wants = if world.daylight(p.at) < 0.6 then knobs.grow else 0 |
| 440 | else |
| 441 | p.wants = if wants_on(p) then draw(p.kind) else 0 |
| 442 | end |
| 443 | table.insert(loads[p.class], p) |
| 444 | devices += 1 |
| 445 | end |
| 446 | end |
| 447 | for _, x in g.extra do |
| 448 | table.insert(loads[x.class], x) |
| 449 | devices += 1 |
| 450 | end |
| 451 | -- shed from the last class: each load gets all it wants, or what's left |
| 452 | local wanted, left = 0, cap + give + solar |
| 453 | for c = 1, CLASSES do |
| 454 | table.sort(loads[c], function(a: Part, b: Part) |
| 455 | return key(a.at) < key(b.at) |
| 456 | end) |
| 457 | for _, p in loads[c] do |
| 458 | wanted += p.wants |
| 459 | if p.wants > 0 and left >= p.wants then |
| 460 | p.got = p.wants |
| 461 | left -= p.wants |
| 462 | else |
| 463 | p.got = 0 |
| 464 | end |
| 465 | end |
| 466 | end |
| 467 | local used = cap + give + solar - left |
| 468 | local from_solar = math.min(used, solar) |
| 469 | local from_gen = math.min(used - from_solar, cap) |
| 470 | local from_bat = used - from_solar - from_gen |
| 471 | -- batteries give what the rest can't, and take what they have over |
| 472 | local solar_spare = solar - from_solar |
| 473 | local surplus = cap - from_gen + solar_spare |
| 474 | local stored, room, charged = 0, 0, 0 |
| 475 | for _, b in batteries do |
| 476 | if from_bat > 0 and give > 0 then |
| 477 | b.fuel = math.max(0, b.fuel - from_bat * math.min(GIVE_RATE, b.fuel / dt) / give * dt) |
| 478 | elseif surplus > 0 then |
| 479 | local take = math.min(CHARGE_RATE, (BATTERY_MAX - b.fuel) / dt, surplus) |
| 480 | b.fuel += take * dt |
| 481 | surplus -= take |
| 482 | charged += take |
| 483 | end |
| 484 | stored += b.fuel |
| 485 | room += BATTERY_MAX - b.fuel |
| 486 | show(b, true) |
| 487 | end |
| 488 | local making = from_solar + from_gen + from_bat + charged |
| 489 | -- the generators share the work by their size and burn what they make; |
| 490 | -- sun that's going spare charges batteries first |
| 491 | local gen_made = from_gen + math.max(0, charged - solar_spare) |
| 492 | for _, p in g.nodes do |
| 493 | if p.kind == "gen" then |
| 494 | local t = TIERS[p.tier] |
| 495 | p.out = if cap > 0 and p.fuel > 0 then gen_made * t.watts * p.flue / cap else 0 |
| 496 | p.fuel = math.max(0, p.fuel - p.out * dt * knobs.burn / (t.watts * t.seconds)) |
| 497 | local on = p.out > 0 |
| 498 | if on ~= p.on then |
| 499 | p.on = on |
| 500 | show(p, on) |
| 501 | end |
| 502 | changed = changed or on |
| 503 | elseif LOADS[p.kind] then |
| 504 | local on = p.got > 0 |
| 505 | if p.kind == "tele" then |
| 506 | p.fuel = math.min(TELE_COST, p.fuel + p.got * dt) |
| 507 | on = p.fuel >= TELE_COST |
| 508 | changed = true |
| 509 | end |
| 510 | if on ~= p.on then |
| 511 | p.on = on |
| 512 | show(p, on) |
| 513 | end |
| 514 | elseif p.kind == "solar" then |
| 515 | local on = p.out > 0.05 |
| 516 | if on ~= p.on then |
| 517 | p.on = on |
| 518 | show(p, on) |
| 519 | end |
| 520 | elseif p.kind == "cable" then |
| 521 | local on = making > 0 |
| 522 | if on ~= p.on then |
| 523 | p.on = on |
| 524 | show(p, on) |
| 525 | end |
| 526 | elseif p.kind == "tap" then |
| 527 | -- live when the grid is making all it's asked |
| 528 | local on = cap + give + solar > 0 and wanted <= cap + give + solar |
| 529 | if on ~= p.on then |
| 530 | p.on = on |
| 531 | show(p, on) |
| 532 | end |
| 533 | end |
| 534 | end |
| 535 | local fuel = 0 |
| 536 | for _, p in g.nodes do |
| 537 | if p.kind == "gen" then |
| 538 | fuel += p.fuel |
| 539 | end |
| 540 | end |
| 541 | if from_bat > 0 or charged > 0 then |
| 542 | changed = true |
| 543 | end |
| 544 | g.making, g.using, g.cap, g.fuel, g.devices = making, wanted, cap + give + solar, fuel, devices |
| 545 | g.solar = solar |
| 546 | g.stored, g.room = stored, room |
| 547 | end |
| 548 | |
| 549 | world.every(0.1, function() |
| 550 | if shaped then |
| 551 | shape() |
| 552 | end |
| 553 | ticks += 1 |
| 554 | for _, g in grids do |
| 555 | g.extra = {} |
| 556 | end |
| 557 | -- loads other creations registered (@potluck/power), on the grid they touch |
| 558 | local seen: { [string]: boolean } = {} |
| 559 | for _, l in power.loads() do |
| 560 | local k = key(l.at) |
| 561 | seen[k] = true |
| 562 | local x = external[k] |
| 563 | if not x then |
| 564 | x = { at = l.at, kind = "ext", on = false, tier = 1, fuel = 0, out = 0, wants = 0, got = 0, class = l.class, flue = 1, awake = 0, run = false, idx = 0, owner = "" } :: Part |
| 565 | external[k] = x |
| 566 | end |
| 567 | x.wants, x.class = l.watts, l.class |
| 568 | for _, d in SIDES do |
| 569 | local g = grid_of[key(l.at + d)] |
| 570 | if g then |
| 571 | table.insert(g.extra, x) |
| 572 | break |
| 573 | end |
| 574 | end |
| 575 | end |
| 576 | for k in external do |
| 577 | if not seen[k] then |
| 578 | external[k] = nil |
| 579 | end |
| 580 | end |
| 581 | for _, g in grids do |
| 582 | work(g, 0.1) |
| 583 | for _, x in g.extra do |
| 584 | power.deliver(x.at, x.got) |
| 585 | end |
| 586 | end |
| 587 | end) |
| 588 | |
| 589 | -- Loads what burns from a player's pockets into a generator, to what it holds. |
| 590 | local function stoke(player, p: Part): number |
| 591 | local t = TIERS[p.tier] |
| 592 | local took = 0 |
| 593 | for _, f in FUELS do |
| 594 | if (pcall(world.kind, f.name)) then |
| 595 | while p.fuel + f.worth <= t.held and world.has(player, f.name) >= 1 do |
| 596 | world.take(player, f.name, 1) |
| 597 | p.fuel += f.worth |
| 598 | took += 1 |
| 599 | end |
| 600 | end |
| 601 | end |
| 602 | if took > 0 then |
| 603 | changed = true |
| 604 | end |
| 605 | return took |
| 606 | end |
| 607 | |
| 608 | local function say(player, id: string, text: string) |
| 609 | ui.show(player, id, text, { at = "bottom" }) |
| 610 | world.after(3, function() |
| 611 | ui.hide(player, id) |
| 612 | end) |
| 613 | end |
| 614 | |
| 615 | local function watts(n: number): string |
| 616 | return string.format("%d W", math.floor(n + 0.5)) |
| 617 | end |
| 618 | |
| 619 | world.on("block_placed", function(_player, at: vector, block: string) |
| 620 | if parts[key(at)] then |
| 621 | parts[key(at)] = nil |
| 622 | shaped, changed = true, true |
| 623 | end |
| 624 | if add(at, block) then |
| 625 | join(at) |
| 626 | end |
| 627 | end) |
| 628 | |
| 629 | world.on("block_broken", function(_player, at: vector, _block: string) |
| 630 | if parts[key(at)] then |
| 631 | parts[key(at)] = nil |
| 632 | shaped, changed = true, true |
| 633 | end |
| 634 | end) |
| 635 | |
| 636 | -- Things machines take and give. |
| 637 | local CRUSH: { [string]: string } = { Stone = "Gravel", Cobblestone = "Gravel", Gravel = "Sand" } |
| 638 | local ORES: { [string]: string } = { |
| 639 | ["Raw copper"] = "Copper ingot", |
| 640 | ["Raw iron"] = "Iron ingot", |
| 641 | ["Raw gold"] = "Gold ingot", |
| 642 | ["Raw moonsilver"] = "Moonsilver ingot", |
| 643 | } |
| 644 | |
| 645 | local function exists(name: string): boolean |
| 646 | return (pcall(world.kind, name)) |
| 647 | end |
| 648 | |
| 649 | local function use_machine(player, p: Part) |
| 650 | local held = world.held(player) or "" |
| 651 | if p.kind == "tele" then |
| 652 | if p.fuel < TELE_COST then |
| 653 | say(player, "power", string.format("Teleport pad: charging, %d%%", math.floor(p.fuel / TELE_COST * 100))) |
| 654 | return |
| 655 | end |
| 656 | local mine = key(p.at) |
| 657 | local keys: { string } = {} |
| 658 | for k, q in parts do |
| 659 | if q.kind == "tele" and world.place(q.at) == world.place(p.at) then |
| 660 | table.insert(keys, k) |
| 661 | end |
| 662 | end |
| 663 | table.sort(keys) |
| 664 | local at = table.find(keys, mine) |
| 665 | if #keys < 2 or not at then |
| 666 | say(player, "power", "Teleport pad: build a second one to go to.") |
| 667 | return |
| 668 | end |
| 669 | local to = parts[keys[at % #keys + 1]] |
| 670 | if to and world.send(player, to.at + vector.create(0, 1, 0), "the teleport pad") then |
| 671 | p.fuel = 0 |
| 672 | changed = true |
| 673 | end |
| 674 | return |
| 675 | end |
| 676 | p.awake = world.time() + 15 |
| 677 | if not p.on then |
| 678 | say(player, "power", "Powering up: use it again in a moment. It needs a grid with power.") |
| 679 | return |
| 680 | end |
| 681 | if p.kind == "crusher" then |
| 682 | local from = CRUSH[held] |
| 683 | if from and exists(from) then |
| 684 | local n = math.min(16, world.has(player, held)) |
| 685 | if world.take(player, held, n) then |
| 686 | world.give(player, from, n) |
| 687 | say(player, "power", string.format("Crushed %d %s into %s", n, held, from)) |
| 688 | end |
| 689 | elseif ORES[held] then |
| 690 | local n = math.floor(math.min(16, world.has(player, held)) / 2) * 2 |
| 691 | if n >= 2 and world.take(player, held, n) then |
| 692 | world.give(player, held, n * 3 / 2) |
| 693 | say(player, "power", string.format("Crushed %d into %d", n, n * 3 / 2)) |
| 694 | end |
| 695 | else |
| 696 | say(player, "power", "Crusher: hold stone, cobblestone, gravel or raw ore.") |
| 697 | end |
| 698 | elseif p.kind == "arc" then |
| 699 | local out = ORES[held] |
| 700 | if out and exists(out) then |
| 701 | local n = math.min(16, world.has(player, held)) |
| 702 | if world.take(player, held, n) then |
| 703 | world.give(player, out, n) |
| 704 | say(player, "power", string.format("Smelted %d %s", n, out)) |
| 705 | end |
| 706 | else |
| 707 | say(player, "power", "Arc furnace: hold raw copper, iron, gold or moonsilver.") |
| 708 | end |
| 709 | elseif p.kind == "pump" then |
| 710 | if held == "Bucket" and world.take(player, "Bucket", 1) then |
| 711 | world.give(player, "Water bucket", 1) |
| 712 | say(player, "power", "Filled a bucket") |
| 713 | else |
| 714 | say(player, "power", "Pump: use it with a Bucket in hand.") |
| 715 | end |
| 716 | elseif p.kind == "lift" then |
| 717 | local best: Part? = nil |
| 718 | local down: Part? = nil |
| 719 | for _, q in parts do |
| 720 | if q.kind == "lift" and q ~= p and q.at.x == p.at.x and q.at.z == p.at.z then |
| 721 | if q.at.y > p.at.y and (best == nil or q.at.y < best.at.y) then |
| 722 | best = q |
| 723 | elseif q.at.y < p.at.y and (down == nil or q.at.y < down.at.y) then |
| 724 | down = q |
| 725 | end |
| 726 | end |
| 727 | end |
| 728 | local to = best or down |
| 729 | if to then |
| 730 | say(player, "power", if to.at.y > p.at.y then "Lift: going up" else "Lift: going down") |
| 731 | world.send(player, to.at + vector.create(0, 1, 0), "the lift") |
| 732 | else |
| 733 | say(player, "power", "Lift: put another lift pad above or below it.") |
| 734 | end |
| 735 | elseif p.kind == "quarry" then |
| 736 | p.run = not p.run |
| 737 | p.idx = 0 |
| 738 | p.owner = player.name |
| 739 | say(player, "power", if p.run then "Quarry started: it digs a 5 by 5 shaft down." else "Quarry stopped.") |
| 740 | end |
| 741 | end |
| 742 | |
| 743 | -- A running quarry digs a block a tick, layer by layer, and hands what it |
| 744 | -- digs to whoever started it. It only breaks natural soft and hard blocks. |
| 745 | world.every(0.5, function() |
| 746 | for _, p in parts do |
| 747 | if p.kind ~= "quarry" or not p.run then |
| 748 | continue |
| 749 | end |
| 750 | p.awake = world.time() + 2 |
| 751 | if not p.on then |
| 752 | continue |
| 753 | end |
| 754 | local who |
| 755 | for _, pl in world.players() do |
| 756 | if pl.name == p.owner then |
| 757 | who = pl |
| 758 | end |
| 759 | end |
| 760 | if not who then |
| 761 | continue |
| 762 | end |
| 763 | for _ = 1, 3 do |
| 764 | local layer = math.floor(p.idx / 25) |
| 765 | local cell = p.idx % 25 |
| 766 | local at = vector.create(p.at.x + cell % 5 - 2, math.floor(p.at.y) - 1 - layer, p.at.z + math.floor(cell / 5) - 2) |
| 767 | if layer > 60 or at.y < -120 then |
| 768 | p.run = false |
| 769 | break |
| 770 | end |
| 771 | local b = world.block(at) |
| 772 | local kind = world.kind(b) |
| 773 | if b ~= "Air" and b ~= "Bedrock" and not KIND[b] and (kind == "soft" or kind == "hard") then |
| 774 | world.set_block(at, "Air") |
| 775 | world.give(who, b, 1) |
| 776 | end |
| 777 | p.idx += 1 |
| 778 | end |
| 779 | end |
| 780 | end) |
| 781 | |
| 782 | world.on("block_used", function(player, at: vector, block: string) |
| 783 | local p = parts[key(at)] |
| 784 | if not p then |
| 785 | if not KIND[block] then |
| 786 | return |
| 787 | end |
| 788 | p = add(at, block) |
| 789 | join(at) |
| 790 | end |
| 791 | if not p then |
| 792 | return |
| 793 | end |
| 794 | local g = grid_of[key(at)] |
| 795 | if p.kind == "breaker" then |
| 796 | p.on = not p.on |
| 797 | show(p, p.on) |
| 798 | shaped = true |
| 799 | say(player, "power", if p.on then "Breaker closed: the grids are joined." else "Breaker open: the grids are apart.") |
| 800 | elseif p.kind == "gen" then |
| 801 | local held = world.held(player) or "" |
| 802 | local up = KITS[held] |
| 803 | if up and p.tier == up - 1 then |
| 804 | if world.take(player, held, 1) then |
| 805 | p.tier = up |
| 806 | show(p, p.on) |
| 807 | changed = true |
| 808 | say(player, "power", string.format("Upgraded to %s", TIERS[up].name)) |
| 809 | end |
| 810 | return |
| 811 | end |
| 812 | local took = stoke(player, p) |
| 813 | local t = TIERS[p.tier] |
| 814 | local text = string.format("%s: %.1f coal's worth of fuel", t.name, p.fuel) |
| 815 | if took > 0 then |
| 816 | text ..= ", filled" |
| 817 | elseif p.fuel <= 0 then |
| 818 | text ..= ". Use it with coal, charcoal or logs in your pockets." |
| 819 | end |
| 820 | say(player, "power", text) |
| 821 | elseif MACHINE[p.kind] and p.kind ~= "door" and p.kind ~= "greenhouse" then |
| 822 | use_machine(player, p) |
| 823 | elseif p.kind == "meter" and g then |
| 824 | say( |
| 825 | player, |
| 826 | "power", |
| 827 | string.format( |
| 828 | "Making %s of %s · Using %s · Fuel %.1f coal · Solar %s · Stored %d%% · %d devices", |
| 829 | watts(g.making), |
| 830 | watts(g.cap), |
| 831 | watts(g.using), |
| 832 | g.fuel, |
| 833 | watts(g.solar), |
| 834 | if g.stored + g.room > 0 then math.floor(g.stored / (g.stored + g.room) * 100 + 0.5) else 0, |
| 835 | g.devices |
| 836 | ) |
| 837 | ) |
| 838 | end |
| 839 | end) |
| 840 | |
| 841 | -- What's lit breaks into what it is. |
| 842 | -- what's lit breaks into what it is |
| 843 | for kind, names in BLOCKS do |
| 844 | if #names > 1 and kind ~= "tap" and kind ~= "solar" and kind ~= "breaker" and kind ~= "battery" then |
| 845 | world.drops(names[2], { gives = names[1] }) |
| 846 | end |
| 847 | end |
| 848 | world.drops("Dark solar panel", { gives = "Solar panel" }) |
| 849 | world.drops("Open breaker", { gives = "Breaker" }) |
| 850 | for _, n in { "Charged battery", "Full battery" } do |
| 851 | world.drops(n, { gives = "Battery" }) |
| 852 | end |
| 853 | for _, t in TIERS do |
| 854 | world.drops(t.lit, { gives = t.name }) |
| 855 | end |
| 856 | |
| 857 | -- Forget parts that aren't there any more, a few at a time. |
| 858 | local checking: { string } = {} |
| 859 | world.every(0.5, function() |
| 860 | if #checking == 0 then |
| 861 | for k in parts do |
| 862 | table.insert(checking, k) |
| 863 | end |
| 864 | return |
| 865 | end |
| 866 | for _ = 1, 32 do |
| 867 | local k = table.remove(checking) |
| 868 | if not k then |
| 869 | break |
| 870 | end |
| 871 | local p = parts[k] |
| 872 | local k2 = p and KIND[world.block(p.at)] |
| 873 | if p and not (k2 and k2.kind == p.kind) then |
| 874 | parts[k] = nil |
| 875 | shaped, changed = true, true |
| 876 | end |
| 877 | end |
| 878 | end) |
| 879 | |
| 880 | -- The generators' fuel is kept in the world's store. |
| 881 | world.every(2, function() |
| 882 | if not changed then |
| 883 | return |
| 884 | end |
| 885 | changed = false |
| 886 | local kept = {} |
| 887 | for _, p in parts do |
| 888 | if p.kind == "gen" or p.kind == "battery" or p.kind == "tele" then |
| 889 | table.insert(kept, { p.at.x, p.at.y, p.at.z, p.tier, math.floor(p.fuel * 1000 + 0.5) / 1000 }) |
| 890 | end |
| 891 | end |
| 892 | store.set("gens", kept) |
| 893 | end) |
| 894 | |
| 895 | do |
| 896 | for _, s in (store.get("gens") or {}) :: { { number } } do |
| 897 | local at = vector.create(s[1], s[2], s[3]) |
| 898 | local k = KIND[world.block(at)] |
| 899 | if k and (k.kind == "gen" or k.kind == "battery" or k.kind == "tele") then |
| 900 | local p = add(at, world.block(at)) :: Part |
| 901 | p.tier = if k.kind == "gen" then (s[4] or 1) else 1 |
| 902 | p.fuel = s[5] or 0 |
| 903 | join(at) |
| 904 | end |
| 905 | end |
| 906 | changed = false |
| 907 | end |