Chests, crates and barrels keep what you put in them, in survival. A chest holds more kinds of thing, and two chests side by side are one double chest with twice the room. A dispenser gives out what it holds each time power reaches it: it puts a block down, pours water, fills a bucket or hands the thing to someone near. A chute moves things down from what is over it into what is under it, unless it has power. Make them at a workbench.
| 1 | --!strict |
| 2 | -- Storage: chests, crates and barrels keep what you put in them, in survival. Use |
| 3 | -- one (Interact while aiming at it) holding something to put it in, or pick |
| 4 | -- something to take out. What each one holds is kept in the store by where |
| 5 | -- it stands, so a crate keeps its things across a reload, and on a server |
| 6 | -- across restarts. A chest holds more than a crate, and two chests side by |
| 7 | -- side are one double chest that holds twice as much (the lower one, by x |
| 8 | -- then z, keeps it). |
| 9 | -- |
| 10 | -- A dispenser holds things too, and gives one out each time power reaches |
| 11 | -- it (from the Circuits creation: live wire, a pulled lever, or a lit |
| 12 | -- inverter or delay facing it): a block is put down in front of it, a |
| 13 | -- water bucket poured (the bucket stays in it), a bucket filled from water |
| 14 | -- in front, and anything else handed to the nearest player. A chute holds |
| 15 | -- one kind of thing, and moves things down from the crate, barrel, |
| 16 | -- dispenser or chute over it into the one under it, until power reaches |
| 17 | -- it. |
| 18 | |
| 19 | local world = require("@potluck/world@0.29") |
| 20 | local tweak = require("@potluck/tweak") |
| 21 | local ui = require("@potluck/ui@0.1") |
| 22 | local sound = require("@potluck/sound@0.1") |
| 23 | local store = require("@potluck/store@0.1") |
| 24 | |
| 25 | local knobs = tweak { |
| 26 | kinds = { 6, min = 1, max = 7, label = "Kinds of thing a crate or barrel holds", order = 1 }, |
| 27 | chest = { 12, min = 1, max = 24, label = "Kinds of thing a chest holds (a double chest, twice)", order = 4 }, |
| 28 | moves = { 2, min = 0.5, max = 8, label = "Things a chute moves a second", order = 2 }, |
| 29 | reach = { 3, min = 1, max = 8, label = "How near a dispenser hands things to someone", order = 3 }, |
| 30 | } |
| 31 | |
| 32 | -- the blocks that hold things |
| 33 | local HOLDERS: { [string]: boolean } = { Chest = true, Crate = true, Barrel = true, Dispenser = true, Chute = true } |
| 34 | -- kinds of thing a chute holds (the others hold the knob's) |
| 35 | local CHUTE_KINDS = 1 |
| 36 | -- how many can hold things in one world (the store has 64 KB) |
| 37 | local MOST = 200 |
| 38 | -- the most moved at a time (world.give and world.take move 64 at most) |
| 39 | local LOAD = 64 |
| 40 | |
| 41 | -- what each holds, by where it is: { ["10,20,30"] = { Planks = 12 } } |
| 42 | type Box = { [string]: number } |
| 43 | local boxes: { [string]: Box } = store.get("boxes") or {} |
| 44 | |
| 45 | local function save() |
| 46 | store.set("boxes", boxes) |
| 47 | end |
| 48 | |
| 49 | local function key(at: vector): string |
| 50 | return math.floor(at.x) .. "," .. math.floor(at.y) .. "," .. math.floor(at.z) |
| 51 | end |
| 52 | |
| 53 | -- the four ways a chest can have a neighbor, in the order they're tried |
| 54 | local SIDES = { vector.create(-1, 0, 0), vector.create(0, 0, -1), vector.create(1, 0, 0), vector.create(0, 0, 1) } |
| 55 | |
| 56 | -- the chest beside a chest that it's one double chest with: each takes the |
| 57 | -- first chest it finds beside it, and they're a pair when each took the |
| 58 | -- other (so of three in a row, the first two are a double and the third |
| 59 | -- stands alone) |
| 60 | local function first_chest(at: vector): vector? |
| 61 | for _, d in SIDES do |
| 62 | local q = at + d |
| 63 | if world.block(q) == "Chest" then |
| 64 | return q |
| 65 | end |
| 66 | end |
| 67 | return nil |
| 68 | end |
| 69 | |
| 70 | local function partner(at: vector): vector? |
| 71 | if world.block(at) ~= "Chest" then |
| 72 | return nil |
| 73 | end |
| 74 | local q = first_chest(at) |
| 75 | if q == nil then |
| 76 | return nil |
| 77 | end |
| 78 | local back = first_chest(q) |
| 79 | if back ~= nil and key(back) == key(at) then |
| 80 | return q |
| 81 | end |
| 82 | return nil |
| 83 | end |
| 84 | |
| 85 | local function lower(a: vector, b: vector): boolean |
| 86 | return a.x < b.x or (a.x == b.x and a.z < b.z) |
| 87 | end |
| 88 | |
| 89 | -- where what's held at `at` is kept: where it stands, or for a double |
| 90 | -- chest at its lower half, taking in what the other half held on its own |
| 91 | local function slot(at: vector): string |
| 92 | local q = partner(at) |
| 93 | if q == nil then |
| 94 | return key(at) |
| 95 | end |
| 96 | local home, other = at, q |
| 97 | if lower(q, at) then |
| 98 | home, other = q, at |
| 99 | end |
| 100 | local ok, hk = key(other), key(home) |
| 101 | local extra = boxes[ok] |
| 102 | if extra then |
| 103 | local box = boxes[hk] or {} |
| 104 | for name, n in extra do |
| 105 | box[name] = (box[name] or 0) + n |
| 106 | end |
| 107 | boxes[hk] = box |
| 108 | boxes[ok] = nil |
| 109 | end |
| 110 | return hk |
| 111 | end |
| 112 | |
| 113 | local function names(box: Box): { string } |
| 114 | local list = {} |
| 115 | for name in box do |
| 116 | table.insert(list, name) |
| 117 | end |
| 118 | table.sort(list) |
| 119 | return list |
| 120 | end |
| 121 | |
| 122 | local function filled(): number |
| 123 | local n = 0 |
| 124 | for _ in boxes do |
| 125 | n += 1 |
| 126 | end |
| 127 | return n |
| 128 | end |
| 129 | |
| 130 | -- "12 Planks, 4 Stone and 1 Lantern" |
| 131 | local function holds(box: Box): string |
| 132 | local parts = {} |
| 133 | for _, name in names(box) do |
| 134 | table.insert(parts, box[name] .. " " .. name) |
| 135 | end |
| 136 | if #parts == 0 then |
| 137 | return "nothing" |
| 138 | end |
| 139 | local last = table.remove(parts) |
| 140 | return if #parts == 0 then last else table.concat(parts, ", ") .. " and " .. last |
| 141 | end |
| 142 | |
| 143 | local function cut(text: string): string |
| 144 | return if #text <= 30 then text else string.sub(text, 1, 29) .. "…" |
| 145 | end |
| 146 | |
| 147 | local open |
| 148 | |
| 149 | -- How many kinds of thing a block holds, and how many of each (nil for |
| 150 | -- as many as you like). |
| 151 | local function room(block: string, at: vector): (number, number?) |
| 152 | if block == "Chute" then |
| 153 | return CHUTE_KINDS, LOAD |
| 154 | end |
| 155 | if block == "Chest" then |
| 156 | return math.floor(knobs.chest) * (if partner(at) then 2 else 1), nil |
| 157 | end |
| 158 | return math.floor(knobs.kinds), nil |
| 159 | end |
| 160 | |
| 161 | -- How many more of `thing` the holder at `at`, a `block`, takes. |
| 162 | local function takes(at: vector, block: string, thing: string): number |
| 163 | local box = boxes[slot(at)] or {} |
| 164 | local kinds, each = room(block, at) |
| 165 | if box[thing] == nil and #names(box) >= kinds then |
| 166 | return 0 |
| 167 | end |
| 168 | if boxes[slot(at)] == nil and filled() >= MOST then |
| 169 | return 0 |
| 170 | end |
| 171 | return if each then math.max(each - (box[thing] or 0), 0) else LOAD |
| 172 | end |
| 173 | |
| 174 | -- Puts what's in their hand in, or says why not. |
| 175 | local function put_in(player, at: vector, block: string, thing: string): string |
| 176 | local k = slot(at) |
| 177 | local box = boxes[k] or {} |
| 178 | local kinds = room(block, at) |
| 179 | if box[thing] == nil and #names(box) >= kinds then |
| 180 | return if kinds == 1 then "It holds one kind of thing, and has some " .. names(box)[1] .. " in it." else "It's full: it holds " .. #names(box) .. " kinds of thing already." |
| 181 | end |
| 182 | if boxes[k] == nil and filled() >= MOST then |
| 183 | return "There's no more room in this world's crates and barrels." |
| 184 | end |
| 185 | if takes(at, block, thing) <= 0 then |
| 186 | return "It's full." |
| 187 | end |
| 188 | local n = math.min(world.has(player, thing), takes(at, block, thing)) |
| 189 | if n <= 0 or not world.take(player, thing, n) then |
| 190 | return "You've no " .. thing .. " to put in." |
| 191 | end |
| 192 | box[thing] = (box[thing] or 0) + n |
| 193 | boxes[k] = box |
| 194 | save() |
| 195 | sound.play("click", { at = at, caption = "Something's put away" }) |
| 196 | return "Put in " .. n .. " " .. thing .. "." |
| 197 | end |
| 198 | |
| 199 | -- Takes one kind of thing out, as much as their hotbar has room for. |
| 200 | local function take_out(player, at: vector, thing: string): string |
| 201 | local k = slot(at) |
| 202 | local box = boxes[k] |
| 203 | if box == nil or box[thing] == nil then |
| 204 | return "There's no " .. thing .. " in it now." |
| 205 | end |
| 206 | local given = world.give(player, thing, math.min(box[thing], LOAD)) |
| 207 | if given == 0 then |
| 208 | return "Your hotbar is full. Make some room first." |
| 209 | end |
| 210 | box[thing] -= given |
| 211 | if box[thing] <= 0 then |
| 212 | box[thing] = nil |
| 213 | end |
| 214 | if next(box) == nil then |
| 215 | boxes[k] = nil |
| 216 | end |
| 217 | save() |
| 218 | sound.play("click", { at = at, caption = "Something's taken out" }) |
| 219 | return "Took out " .. given .. " " .. thing .. "." |
| 220 | end |
| 221 | |
| 222 | function open(player, at: vector, block: string, said: string?) |
| 223 | local title = if block == "Chest" and partner(at) then "Double chest" else block |
| 224 | if world.pockets(player) == nil then |
| 225 | ui.ask(player, { |
| 226 | title = title, |
| 227 | text = "In creative you've every block already. In survival, chests, crates, barrels, dispensers and chutes keep what you put in them; two chests side by side are a double chest.", |
| 228 | choices = { "OK" }, |
| 229 | }, function() end) |
| 230 | return |
| 231 | end |
| 232 | local box = boxes[slot(at)] or {} |
| 233 | local choices: { string } = {} |
| 234 | local does: { () -> string } = {} |
| 235 | local hand = world.held(player) |
| 236 | if hand and world.has(player, hand) > 0 then |
| 237 | table.insert(choices, cut("Put in your " .. hand)) |
| 238 | table.insert(does, function() |
| 239 | return put_in(player, at, block, hand) |
| 240 | end) |
| 241 | end |
| 242 | for _, name in names(box) do |
| 243 | if #choices >= 8 then |
| 244 | break |
| 245 | end |
| 246 | table.insert(choices, cut("Take out " .. name)) |
| 247 | table.insert(does, function() |
| 248 | return take_out(player, at, name) |
| 249 | end) |
| 250 | end |
| 251 | local text = (if said then said .. "\n\n" else "") |
| 252 | .. (if next(box) then "It holds " .. holds(box) .. "." else "It's empty. Use it while you hold something to put that in.") |
| 253 | if #choices == 0 then |
| 254 | ui.ask(player, { title = title, text = text, choices = { "OK" } }, function() end) |
| 255 | return |
| 256 | end |
| 257 | ui.ask(player, { title = title, text = text, choices = choices }, function(who, choice) |
| 258 | local act = choice and does[choice] |
| 259 | if act then |
| 260 | -- ask again, so moving several things is quick |
| 261 | open(who, at, block, act()) |
| 262 | end |
| 263 | end) |
| 264 | end |
| 265 | |
| 266 | -- ---- dispensers and chutes ---- |
| 267 | |
| 268 | local AHEAD: { [string]: vector } = { |
| 269 | north = vector.create(0, 0, -1), |
| 270 | east = vector.create(1, 0, 0), |
| 271 | south = vector.create(0, 0, 1), |
| 272 | west = vector.create(-1, 0, 0), |
| 273 | } |
| 274 | local BACK = { north = "south", east = "west", south = "north", west = "east" } |
| 275 | local UP = vector.create(0, 1, 0) |
| 276 | -- what a dispenser can put a block down into |
| 277 | local OPEN: { [string]: boolean } = { Air = true, Water = true, ["Tall grass"] = true, Fern = true, Bush = true } |
| 278 | |
| 279 | -- dispensers and chutes, by where they are, and whether power reaches them |
| 280 | type Machine = { at: vector, block: string, on: boolean } |
| 281 | local machines: { [string]: Machine } = {} |
| 282 | for _, t in (store.get("machines") or {}) :: { { any } } do |
| 283 | local at = vector.create(t[1], t[2], t[3]) |
| 284 | machines[key(at)] = { at = at, block = t[4], on = t[5] == 1 } |
| 285 | end |
| 286 | local dirty = false |
| 287 | |
| 288 | local function keep() |
| 289 | local list = {} |
| 290 | for _, m in machines do |
| 291 | table.insert(list, { m.at.x, m.at.y, m.at.z, m.block, if m.on then 1 else 0 }) |
| 292 | end |
| 293 | store.set("machines", list) |
| 294 | end |
| 295 | |
| 296 | local function corner(at: vector): vector |
| 297 | return vector.create(math.floor(at.x), math.floor(at.y), math.floor(at.z)) |
| 298 | end |
| 299 | |
| 300 | -- Whether power reaches c: live wire or a pulled lever beside, under or |
| 301 | -- over it, or a lit inverter or delay beside it facing it. |
| 302 | local function powered(c: vector): boolean |
| 303 | for side, d in AHEAD do |
| 304 | local q = c + d |
| 305 | local b = world.block(q) |
| 306 | if b == "Live wire" or b == "Pulled lever" then |
| 307 | return true |
| 308 | end |
| 309 | if (b == "Lit inverter" or b == "Lit delay") and world.facing(q) == BACK[side] then |
| 310 | return true |
| 311 | end |
| 312 | end |
| 313 | for _, q in { c + UP, c - UP } do |
| 314 | local b = world.block(q) |
| 315 | if b == "Live wire" or b == "Pulled lever" then |
| 316 | return true |
| 317 | end |
| 318 | end |
| 319 | return false |
| 320 | end |
| 321 | |
| 322 | -- Takes n of a thing out of what the holder at k holds. |
| 323 | local function remove(k: string, thing: string, n: number) |
| 324 | local box = boxes[k] |
| 325 | if not box or not box[thing] then |
| 326 | return |
| 327 | end |
| 328 | box[thing] -= n |
| 329 | if box[thing] <= 0 then |
| 330 | box[thing] = nil |
| 331 | end |
| 332 | if next(box) == nil then |
| 333 | boxes[k] = nil |
| 334 | end |
| 335 | end |
| 336 | |
| 337 | local function add(k: string, thing: string, n: number) |
| 338 | local box = boxes[k] or {} |
| 339 | box[thing] = (box[thing] or 0) + n |
| 340 | boxes[k] = box |
| 341 | end |
| 342 | |
| 343 | -- Gives out one thing: the first it holds, by name. |
| 344 | local function dispense(m: Machine) |
| 345 | local k = key(m.at) |
| 346 | local box = boxes[k] |
| 347 | local front = m.at + AHEAD[world.facing(m.at) or "north"] |
| 348 | local mid = front + vector.create(0.5, 0.5, 0.5) |
| 349 | if box == nil then |
| 350 | sound.play("click", { at = m.at, pitch = 0.6, caption = "A dispenser clicks, empty" }) |
| 351 | return |
| 352 | end |
| 353 | local thing = names(box)[1] |
| 354 | local b = world.block(front) |
| 355 | local done = false |
| 356 | if thing == "Water bucket" and OPEN[b] and b ~= "Water" then |
| 357 | world.set_block(front, "Water") |
| 358 | remove(k, thing, 1) |
| 359 | add(k, "Bucket", 1) |
| 360 | done = true |
| 361 | elseif thing == "Bucket" and b == "Water" then |
| 362 | remove(k, thing, 1) |
| 363 | add(k, "Water bucket", 1) |
| 364 | done = true |
| 365 | elseif OPEN[b] and pcall(function() |
| 366 | world.set_block(front, thing) |
| 367 | end) then |
| 368 | -- a block: put down in front |
| 369 | remove(k, thing, 1) |
| 370 | done = true |
| 371 | else |
| 372 | -- anything else is handed to whoever's near |
| 373 | local p = world.nearest_player(mid, knobs.reach) |
| 374 | if p and world.pockets(p) ~= nil and world.give(p, thing, 1) > 0 then |
| 375 | remove(k, thing, 1) |
| 376 | done = true |
| 377 | end |
| 378 | end |
| 379 | dirty = true |
| 380 | if done then |
| 381 | sound.play("click", { at = m.at, pitch = 1.3, caption = "A dispenser gives something out" }) |
| 382 | else |
| 383 | sound.play("click", { at = m.at, pitch = 0.6, caption = "A dispenser clicks" }) |
| 384 | end |
| 385 | end |
| 386 | |
| 387 | -- Moves one thing from the holder at `from` (if any) into the one at `to`. |
| 388 | local function move(from: vector, to: vector): boolean |
| 389 | local fk = slot(from) |
| 390 | local box = boxes[fk] |
| 391 | local into = world.block(to) |
| 392 | if box == nil or not HOLDERS[into] or not HOLDERS[world.block(from)] then |
| 393 | return false |
| 394 | end |
| 395 | for _, thing in names(box) do |
| 396 | if takes(to, into, thing) > 0 then |
| 397 | remove(fk, thing, 1) |
| 398 | add(slot(to), thing, 1) |
| 399 | dirty = true |
| 400 | return true |
| 401 | end |
| 402 | end |
| 403 | return false |
| 404 | end |
| 405 | |
| 406 | world.every(0.1, function() |
| 407 | for k, m in machines do |
| 408 | if m.block == "Dispenser" then |
| 409 | local on = powered(m.at) |
| 410 | if on ~= m.on then |
| 411 | m.on = on |
| 412 | dirty = true |
| 413 | if on then |
| 414 | dispense(m) |
| 415 | end |
| 416 | end |
| 417 | end |
| 418 | end |
| 419 | end) |
| 420 | |
| 421 | -- chutes move a thing down, then take one from over them, so things go |
| 422 | -- down a line of them a block at a time |
| 423 | local since = 0 |
| 424 | world.every(0.1, function() |
| 425 | since += 0.1 |
| 426 | if since < 1 / knobs.moves then |
| 427 | return |
| 428 | end |
| 429 | since = 0 |
| 430 | for _, m in machines do |
| 431 | if m.block == "Chute" then |
| 432 | m.on = powered(m.at) |
| 433 | if not m.on then |
| 434 | move(m.at, m.at - UP) |
| 435 | move(m.at + UP, m.at) |
| 436 | end |
| 437 | end |
| 438 | end |
| 439 | end) |
| 440 | |
| 441 | world.every(2, function() |
| 442 | if dirty then |
| 443 | dirty = false |
| 444 | save() |
| 445 | keep() |
| 446 | end |
| 447 | end) |
| 448 | |
| 449 | world.on("block_placed", function(_player, middle: vector, block: string) |
| 450 | if block == "Dispenser" or block == "Chute" then |
| 451 | local at = corner(middle) |
| 452 | machines[key(at)] = { at = at, block = block, on = false } |
| 453 | keep() |
| 454 | end |
| 455 | end) |
| 456 | |
| 457 | world.on("block_used", function(player, at: vector, block: string) |
| 458 | if HOLDERS[block] then |
| 459 | if (block == "Dispenser" or block == "Chute") and not machines[key(at)] then |
| 460 | -- one put down before this creation was here |
| 461 | local c = corner(at) |
| 462 | machines[key(c)] = { at = c, block = block, on = false } |
| 463 | keep() |
| 464 | end |
| 465 | open(player, at, block) |
| 466 | end |
| 467 | end) |
| 468 | |
| 469 | -- Broken, a crate gives what it held to whoever broke it, as much as fits; |
| 470 | -- the rest stays there, for a crate put back in its place. |
| 471 | world.on("block_broken", function(player, at: vector, block: string) |
| 472 | local k = key(at) |
| 473 | local box = boxes[k] |
| 474 | if machines[k] then |
| 475 | machines[k] = nil |
| 476 | keep() |
| 477 | end |
| 478 | if not HOLDERS[block] or box == nil or world.pockets(player) == nil then |
| 479 | return |
| 480 | end |
| 481 | for _, name in names(box) do |
| 482 | while box[name] and box[name] > 0 do |
| 483 | local given = world.give(player, name, math.min(box[name], LOAD)) |
| 484 | if given == 0 then |
| 485 | break |
| 486 | end |
| 487 | box[name] -= given |
| 488 | if box[name] <= 0 then |
| 489 | box[name] = nil |
| 490 | end |
| 491 | end |
| 492 | end |
| 493 | if next(box) == nil then |
| 494 | boxes[k] = nil |
| 495 | else |
| 496 | ui.show(player, "storage", "Your hotbar's full: the rest waits here", { at = "left" }) |
| 497 | world.after(6, function() |
| 498 | ui.hide(player, "storage") |
| 499 | end) |
| 500 | end |
| 501 | save() |
| 502 | end) |
| 503 | |
| 504 | -- machines kept from before that are gone (the world was changed while |
| 505 | -- this creation was off) are forgotten |
| 506 | world.after(0.2, function() |
| 507 | local gone = false |
| 508 | for k, m in machines do |
| 509 | if world.block(m.at) ~= m.block then |
| 510 | machines[k] = nil |
| 511 | gone = true |
| 512 | end |
| 513 | end |
| 514 | if gone then |
| 515 | keep() |
| 516 | end |
| 517 | end) |
| 518 | |
| 519 | -- The item guide's + (recipe_fetch): what a recipe takes and the player is |
| 520 | -- short of comes to their pockets from chests, crates and barrels near them, |
| 521 | -- so the bench can take it as usual. Only holders near the player are |
| 522 | -- touched, and only the player's own pockets are given to: never another |
| 523 | -- player's. |
| 524 | local FETCH_REACH = 8 |
| 525 | world.on("recipe_fetch", function(player, wants: { [string]: number }, at: vector) |
| 526 | if world.pockets(player) == nil then |
| 527 | return |
| 528 | end |
| 529 | local near: { string } = {} |
| 530 | for k in boxes do |
| 531 | local x, y, z = string.match(k, "^(-?%d+),(-?%d+),(-?%d+)$") |
| 532 | if x and math.abs(tonumber(x) :: number - at.x) <= FETCH_REACH and math.abs(tonumber(y) :: number - at.y) <= FETCH_REACH and math.abs(tonumber(z) :: number - at.z) <= FETCH_REACH then |
| 533 | table.insert(near, k) |
| 534 | end |
| 535 | end |
| 536 | table.sort(near) |
| 537 | local moved = false |
| 538 | for thing, n in wants do |
| 539 | local ok, have = pcall(world.has, player, thing) |
| 540 | local short = if ok then n - (have :: number) else 0 |
| 541 | for _, k in near do |
| 542 | local box = boxes[k] |
| 543 | while short > 0 and box and box[thing] and box[thing] > 0 do |
| 544 | local given = world.give(player, thing, math.min(box[thing], short, LOAD)) |
| 545 | if given == 0 then |
| 546 | break |
| 547 | end |
| 548 | remove(k, thing, given) |
| 549 | box = boxes[k] |
| 550 | short -= given |
| 551 | moved = true |
| 552 | end |
| 553 | end |
| 554 | end |
| 555 | if moved then |
| 556 | save() |
| 557 | sound.play("click", { at = at, caption = "Something's taken out" }) |
| 558 | end |
| 559 | end) |