Long ago the island's first potluck was held in a feast hall that's now deep under the ground. Wander far enough down and you may break into it: long tables laid with candles, a hearth with its cooking pot, a larder and a shelf of old recipes. Each recipe card teaches the cooking pot a dish found nowhere else, from glowcap soup to potluck stew, as long as you carry it. And somewhere on the shelf is the menu from that first potluck, for the Curator.
| 1 | --!strict |
| 2 | -- The Deep Kitchen: long ago the island's first potluck was held in a feast |
| 3 | -- hall that's now deep under the ground. When a player wanders far enough |
| 4 | -- down, one opens in the rock near them with a low passage to where they |
| 5 | -- are: sandstone walls under oak beams, a long table laid with candles and |
| 6 | -- stools, a hearth at the east end with a campfire and a cooking pot, a |
| 7 | -- larder of barrels and crates at the west end, and along the north wall a |
| 8 | -- shelf of old recipes. |
| 9 | -- |
| 10 | -- Each of the five low bookshelves on that wall holds a recipe card once: |
| 11 | -- use one (or break it) to take it. A card is an item of this creation's, |
| 12 | -- and each of the five dishes it makes (world 0.27) names its card as |
| 13 | -- `card` and what it takes as `cook`, so the Furnaces' cooking pot offers |
| 14 | -- the dish to any cook carrying its card. Hold a card and Interact looking |
| 15 | -- at the sky to read it. One of the shelves also holds the menu from the |
| 16 | -- first potluck, a relic the Curator on the Green collects. |
| 17 | -- |
| 18 | -- The hall's shelf, table and lights are the game's own blocks (bookshelves, |
| 19 | -- tables, stools, candles, lanterns), known by where they are, so the base |
| 20 | -- game's made blocks leave room for players' own. |
| 21 | -- |
| 22 | -- Where each kitchen is and which shelves still hold their cards are kept |
| 23 | -- with @potluck/store, so each is built once. Only natural rock is dug. |
| 24 | |
| 25 | local world = require("@potluck/world@0.33") |
| 26 | local tweak = require("@potluck/tweak") |
| 27 | local store = require("@potluck/store") |
| 28 | local ui = require("@potluck/ui@0.2") |
| 29 | local sound = require("@potluck/sound@0.2") |
| 30 | |
| 31 | local knobs = tweak { |
| 32 | apart = { 260, min = 80, max = 1000, label = "How far apart kitchens are", order = 1 }, |
| 33 | menus = { 0.6, min = 0, max = 1, label = "Chance a kitchen keeps the first menu", order = 2 }, |
| 34 | } |
| 35 | |
| 36 | local WALL, FLOOR, BEAM, SHELF = "Sandstone bricks", "Planks", "Oak log", "Bookshelf" |
| 37 | local MOST = 48 -- kitchens a world keeps track of |
| 38 | local RX, RZ, TOP = 7, 4, 3 -- the hall's half width and depth, and its top, from its middle on the floor |
| 39 | local DEEP = 6 -- how much rock is over a player before a kitchen opens near them |
| 40 | local HIGHEST = 16 -- kitchens open only below here |
| 41 | local CARD_XP, MENU_XP = 3, 5 |
| 42 | |
| 43 | -- the dishes the cards teach, in the order of the shelves |
| 44 | local DISHES = { "Glowcap soup", "Miner's pasty", "Honey carrots", "Feast pie", "Potluck stew" } |
| 45 | local MENU = "First potluck menu" |
| 46 | |
| 47 | -- what may be dug |
| 48 | local NATURAL: { [string]: boolean } = { |
| 49 | Stone = true, Dirt = true, Gravel = true, Sand = true, Slate = true, Sandstone = true, Cobblestone = true, |
| 50 | ["Coal ore"] = true, ["Copper ore"] = true, ["Iron ore"] = true, ["Gold ore"] = true, ["Crystal ore"] = true, ["Moonsilver ore"] = true, |
| 51 | Glowcap = true, |
| 52 | } |
| 53 | local UP = vector.create(0, 1, 0) |
| 54 | |
| 55 | -- ---- what's kept ---- |
| 56 | |
| 57 | -- each kitchen: its middle on the floor, whether it's built, which shelves |
| 58 | -- still hold a card (by shelf), which shelf has the menu (0 for none), and |
| 59 | -- while it's being built, the way in |
| 60 | type Kitchen = { at: vector, built: boolean, cards: { boolean }, menu: number, way: { vector }? } |
| 61 | local kitchens: { Kitchen } = store.get("kitchens") or {} |
| 62 | |
| 63 | local function save() |
| 64 | store.set("kitchens", kitchens) |
| 65 | end |
| 66 | |
| 67 | local function key(at: vector): string |
| 68 | return string.format("%d,%d,%d", math.floor(at.x), math.floor(at.y), math.floor(at.z)) |
| 69 | end |
| 70 | |
| 71 | -- Whether a thing is in this world: the campfire and pot are the Furnaces'. |
| 72 | local here: { [string]: boolean } = {} |
| 73 | local function exists(name: string): boolean |
| 74 | if here[name] == nil then |
| 75 | here[name] = (pcall(world.kind, name)) |
| 76 | end |
| 77 | return here[name] == true |
| 78 | end |
| 79 | |
| 80 | -- the same number every time, for a place |
| 81 | local function hash(x: number, y: number, z: number): number |
| 82 | local h = (math.floor(x) * 73856093 + math.floor(y) * 19349663 + math.floor(z) * 83492791) % 1000003 |
| 83 | return (h * 7919 % 1000003) / 1000003 |
| 84 | end |
| 85 | |
| 86 | local shown: { [number]: number } = {} |
| 87 | local function tell(player, text: string) |
| 88 | ui.show(player, "kitchen", text, { at = "bottom" }) |
| 89 | local mine = (shown[player.id] or 0) + 1 |
| 90 | shown[player.id] = mine |
| 91 | world.after(5, function() |
| 92 | if shown[player.id] == mine then |
| 93 | shown[player.id] = nil |
| 94 | ui.hide(player, "kitchen") |
| 95 | end |
| 96 | end) |
| 97 | end |
| 98 | |
| 99 | local function flat(a: vector, b: vector): number |
| 100 | local dx, dz = a.x - b.x, a.z - b.z |
| 101 | return math.sqrt(dx * dx + dz * dz) |
| 102 | end |
| 103 | |
| 104 | local function v(x: number, y: number, z: number): vector |
| 105 | return vector.create(x, y, z) |
| 106 | end |
| 107 | |
| 108 | -- ---- where things are in a kitchen ---- |
| 109 | |
| 110 | -- the recipe shelves: five low bookshelves along the north wall |
| 111 | local function shelf_at(k: Kitchen, i: number): vector |
| 112 | return k.at + v(-6 + i * 2, 0, -RZ) |
| 113 | end |
| 114 | |
| 115 | -- which shelf a place is, or nil |
| 116 | local function shelf_of(k: Kitchen, at: vector): number? |
| 117 | for i = 1, #DISHES do |
| 118 | if key(shelf_at(k, i)) == key(at) then |
| 119 | return i |
| 120 | end |
| 121 | end |
| 122 | return nil |
| 123 | end |
| 124 | |
| 125 | -- the dish a kitchen's shelf teaches: each kitchen has them all, in its |
| 126 | -- own order |
| 127 | local function dish_on(k: Kitchen, i: number): string |
| 128 | local shift = math.floor(hash(k.at.x, k.at.y, k.at.z) * #DISHES) |
| 129 | return DISHES[(i - 1 + shift) % #DISHES + 1] |
| 130 | end |
| 131 | |
| 132 | local function card_of(dish: string): string |
| 133 | return dish .. " recipe" |
| 134 | end |
| 135 | |
| 136 | -- inside the hall, from its middle on the floor |
| 137 | local function inside(x: number, y: number, z: number): boolean |
| 138 | return math.abs(x) <= RX and math.abs(z) <= RZ and y >= 0 and y <= TOP |
| 139 | end |
| 140 | |
| 141 | -- ---- finding a place ---- |
| 142 | |
| 143 | -- Whether a kitchen fits around `c`: deep in natural rock (a cave through it |
| 144 | -- is fine), with nothing built and no water. |
| 145 | local function fits(c: vector): boolean |
| 146 | if world.top(c.x, c.z) < c.y + TOP + DEEP then |
| 147 | return false |
| 148 | end |
| 149 | local air, n = 0, 0 |
| 150 | for _, dx in { -RX - 1, -4, 0, 4, RX + 1 } do |
| 151 | for _, dz in { -RZ - 1, 0, RZ + 1 } do |
| 152 | for _, dy in { -1, 1, TOP + 1 } do |
| 153 | local b = world.block(c + v(dx, dy, dz)) |
| 154 | n += 1 |
| 155 | if b == "Air" then |
| 156 | air += 1 |
| 157 | elseif not NATURAL[b] then |
| 158 | return false |
| 159 | end |
| 160 | end |
| 161 | end |
| 162 | end |
| 163 | return air * 3 <= n |
| 164 | end |
| 165 | |
| 166 | -- The way from a kitchen's middle out to where a player is: the cells to |
| 167 | -- dig, up to the first open air outside it, or nil if it's built or wet in |
| 168 | -- between, or too far. |
| 169 | local function passage(c: vector, to: vector): { vector }? |
| 170 | local d = to - c |
| 171 | local len = vector.magnitude(d) |
| 172 | if len < 1 then |
| 173 | return nil |
| 174 | end |
| 175 | local step = d / len |
| 176 | local out = {} |
| 177 | for t = 0, 40 do |
| 178 | local at = c + step * t |
| 179 | local y = c.y + (to.y - c.y) * math.clamp((t - RZ) / math.max(len - RZ, 1), 0, 1) |
| 180 | local cell = v(math.floor(at.x), math.floor(y), math.floor(at.z)) |
| 181 | local rel = cell - c |
| 182 | if not inside(rel.x, rel.y, rel.z) then |
| 183 | local b, above = world.block(cell), world.block(cell + UP) |
| 184 | if b == "Air" and above == "Air" then |
| 185 | return out |
| 186 | end |
| 187 | if (b ~= "Air" and not NATURAL[b]) or (above ~= "Air" and not NATURAL[above]) then |
| 188 | return nil |
| 189 | end |
| 190 | table.insert(out, cell) |
| 191 | end |
| 192 | end |
| 193 | return nil |
| 194 | end |
| 195 | |
| 196 | -- A place for a kitchen near a player deep underground, north of them so |
| 197 | -- the way in comes through the south wall, and the way to it. |
| 198 | local function look_near(p): (vector?, { vector }?) |
| 199 | for _ = 1, 8 do |
| 200 | local a = world.random(math.pi + 0.5, 2 * math.pi - 0.5) |
| 201 | local r = world.random(12, 18) |
| 202 | local c = v( |
| 203 | math.floor(p.position.x + math.cos(a) * r), |
| 204 | math.floor(p.position.y + world.random(-2, 1)), |
| 205 | math.floor(p.position.z + math.sin(a) * r) |
| 206 | ) |
| 207 | if fits(c) then |
| 208 | local way = passage(c, p.position) |
| 209 | if way then |
| 210 | return c, way |
| 211 | end |
| 212 | end |
| 213 | end |
| 214 | return nil, nil |
| 215 | end |
| 216 | |
| 217 | local function far_from_others(pos: vector, apart: number): boolean |
| 218 | for _, k in kitchens do |
| 219 | if flat(k.at, pos) < apart then |
| 220 | return false |
| 221 | end |
| 222 | end |
| 223 | return true |
| 224 | end |
| 225 | |
| 226 | -- ---- building it ---- |
| 227 | |
| 228 | -- what's left to put down: a place and a block ("" to dig) |
| 229 | type Work = { at: vector, name: string } |
| 230 | local work: { Work } = {} |
| 231 | local done = 0 |
| 232 | local building: Kitchen? = nil |
| 233 | -- what it may put its own blocks over while it builds |
| 234 | local OURS: { [string]: boolean } = { Air = true, [WALL] = true, [FLOOR] = true, [BEAM] = true } |
| 235 | |
| 236 | local function plan(k: Kitchen) |
| 237 | local c = k.at |
| 238 | work, done = {}, 0 |
| 239 | local function put(at: vector, name: string) |
| 240 | table.insert(work, { at = at, name = name }) |
| 241 | end |
| 242 | -- the hall, in its shell |
| 243 | for x = -RX - 1, RX + 1 do |
| 244 | for z = -RZ - 1, RZ + 1 do |
| 245 | for y = -1, TOP + 1 do |
| 246 | if inside(x, y, z) then |
| 247 | put(c + v(x, y, z), "") |
| 248 | elseif y == -1 then |
| 249 | put(c + v(x, y, z), FLOOR) |
| 250 | elseif y == TOP + 1 then |
| 251 | put(c + v(x, y, z), if x % 3 == 0 then BEAM else WALL) |
| 252 | else |
| 253 | put(c + v(x, y, z), WALL) |
| 254 | end |
| 255 | end |
| 256 | end |
| 257 | end |
| 258 | -- the way in |
| 259 | for i, cell in k.way or {} do |
| 260 | put(cell, "") |
| 261 | put(cell + UP, "") |
| 262 | if i <= 3 then |
| 263 | put(cell - UP, FLOOR) |
| 264 | end |
| 265 | end |
| 266 | -- the long table, laid, with stools down both sides |
| 267 | for x = -4, 4 do |
| 268 | put(c + v(x, 0, 0), "Table") |
| 269 | if x % 2 == 0 then |
| 270 | put(c + v(x, 0, -1), "Stool") |
| 271 | put(c + v(x, 0, 1), "Stool") |
| 272 | end |
| 273 | end |
| 274 | for _, x in { -3, 0, 3 } do |
| 275 | put(c + v(x, 1, 0), "Candle") |
| 276 | end |
| 277 | -- the recipe shelf along the north wall, low shelves with old books over them |
| 278 | for i = 1, #DISHES do |
| 279 | put(shelf_at(k, i), SHELF) |
| 280 | put(shelf_at(k, i) + UP, SHELF) |
| 281 | end |
| 282 | -- the hearth at the east end: a campfire and the pot beside it, if the |
| 283 | -- Furnaces are in this world, or a brazier |
| 284 | if exists("Campfire") and exists("Cooking pot") then |
| 285 | put(c + v(RX, 0, -1), "Campfire") |
| 286 | put(c + v(RX, 0, 0), "Cooking pot") |
| 287 | else |
| 288 | put(c + v(RX, 0, 0), "Brazier") |
| 289 | end |
| 290 | -- the larder at the west end |
| 291 | put(c + v(-RX, 0, -2), "Barrel") |
| 292 | put(c + v(-RX, 0, -1), "Barrel") |
| 293 | put(c + v(-RX, 1, -2), "Crate") |
| 294 | put(c + v(-RX, 0, 2), "Crate") |
| 295 | put(c + v(-RX, 0, 3), "Clay pot") |
| 296 | put(c + v(-RX + 1, 0, 3), "Hay") |
| 297 | -- lanterns in the corners |
| 298 | for _, x in { -RX, RX } do |
| 299 | for _, z in { -RZ, RZ } do |
| 300 | if not (x == -RX and z == RZ) then |
| 301 | put(c + v(x, 0, z), "Lantern") |
| 302 | end |
| 303 | end |
| 304 | end |
| 305 | put(c + v(-RX + 1, 0, RZ), "Lantern") |
| 306 | end |
| 307 | |
| 308 | -- puts down a few hundred blocks a tick |
| 309 | local function step() |
| 310 | local last = math.min(done + 250, #work) |
| 311 | for i = done + 1, last do |
| 312 | local w = work[i] |
| 313 | local b = world.block(w.at) |
| 314 | if w.name == "" then |
| 315 | if NATURAL[b] or OURS[b] then |
| 316 | world.set_block(w.at, "Air") |
| 317 | end |
| 318 | elseif NATURAL[b] or OURS[b] then |
| 319 | world.set_block(w.at, w.name) |
| 320 | end |
| 321 | end |
| 322 | done = last |
| 323 | if done >= #work then |
| 324 | work, done = {}, 0 |
| 325 | local k = building |
| 326 | if k then |
| 327 | k.built = true |
| 328 | k.way = nil |
| 329 | building = nil |
| 330 | save() |
| 331 | end |
| 332 | end |
| 333 | end |
| 334 | |
| 335 | -- ---- the recipe shelf ---- |
| 336 | |
| 337 | -- the kitchen a place belongs to |
| 338 | local function kitchen_at(pos: vector, reach: number): Kitchen? |
| 339 | for _, k in kitchens do |
| 340 | if vector.magnitude(pos - k.at) < reach then |
| 341 | return k |
| 342 | end |
| 343 | end |
| 344 | return nil |
| 345 | end |
| 346 | |
| 347 | -- Gives what a shelf holds: its card, and the menu on the menu's shelf. |
| 348 | -- False when there was nothing there; what pockets have no room for stays. |
| 349 | local function take_from(player, k: Kitchen, i: number): boolean |
| 350 | local card_here, menu_here = k.cards[i], k.menu == i |
| 351 | if not card_here and not menu_here then |
| 352 | return false |
| 353 | end |
| 354 | local dish = dish_on(k, i) |
| 355 | local card = card_here and world.give(player, card_of(dish), 1) > 0 |
| 356 | local menu = menu_here and world.give(player, MENU, 1) > 0 |
| 357 | if not card and not menu then |
| 358 | tell(player, "Your pockets are full.") |
| 359 | return true |
| 360 | end |
| 361 | if card then |
| 362 | k.cards[i] = false |
| 363 | end |
| 364 | if menu then |
| 365 | k.menu = 0 |
| 366 | end |
| 367 | save() |
| 368 | world.reward(player, (if card then CARD_XP else 0) + (if menu then MENU_XP else 0)) |
| 369 | sound.play("click", { at = shelf_at(k, i), pitch = 1.3, volume = 0.6, caption = "Pages turning" }) |
| 370 | local text = if card then "Between the old cookbooks you find a recipe card for " .. string.lower(dish) .. "." else "" |
| 371 | if menu then |
| 372 | text ..= (if card then " Tucked in with it is" else "You find") .. " the menu from the island's first potluck." |
| 373 | end |
| 374 | if card then |
| 375 | text ..= "\n\nCarry the card and a cooking pot over a fire can cook it. Hold it and Interact looking at the sky to read it." |
| 376 | end |
| 377 | ui.ask(player, { title = "The recipe shelf", text = text }, function() end) |
| 378 | return true |
| 379 | end |
| 380 | |
| 381 | -- the shelves break into a bookshelf as any do, so only their cards matter |
| 382 | world.on("block_used", function(player, at: vector, block: string) |
| 383 | if block ~= SHELF then |
| 384 | return |
| 385 | end |
| 386 | local k = kitchen_at(at, RX + 2) |
| 387 | local i = k and shelf_of(k, at) |
| 388 | if not k or not i then |
| 389 | return |
| 390 | end |
| 391 | if world.pockets(player) == nil then |
| 392 | tell(player, "The recipe shelf holds its cards for survival.") |
| 393 | elseif not take_from(player, k, i) then |
| 394 | tell(player, "Only crumbs and faded pages now.") |
| 395 | end |
| 396 | end) |
| 397 | |
| 398 | world.on("block_broken", function(player, at: vector, block: string) |
| 399 | if block ~= SHELF or world.pockets(player) == nil then |
| 400 | return |
| 401 | end |
| 402 | local k = kitchen_at(at, RX + 2) |
| 403 | local i = k and shelf_of(k, at) |
| 404 | if k and i then |
| 405 | take_from(player, k, i) |
| 406 | end |
| 407 | end) |
| 408 | |
| 409 | -- ---- reading a card ---- |
| 410 | |
| 411 | -- "2 Glowcap, 1 Potatoes, 1 Milk bucket", one to a line |
| 412 | local function wants(dish: string): string |
| 413 | local it = world.item(dish) |
| 414 | local parts = {} |
| 415 | for name, n in (it and it.cook) or {} do |
| 416 | table.insert(parts, n .. " " .. name) |
| 417 | end |
| 418 | table.sort(parts) |
| 419 | return table.concat(parts, "\n") |
| 420 | end |
| 421 | |
| 422 | world.on("item_used", function(player, item: string?) |
| 423 | for _, dish in DISHES do |
| 424 | if item == card_of(dish) then |
| 425 | local it = world.item(dish) |
| 426 | local makes = (it and it.makes) or 1 |
| 427 | local text = "Makes " .. makes .. ", in a cooking pot over a fire, for a cook who has this card with them.\n\n" .. wants(dish) |
| 428 | ui.ask(player, { title = dish, text = text }, function() end) |
| 429 | sound.play("click", { at = player.position, pitch = 1.5, volume = 0.4 }) |
| 430 | return |
| 431 | end |
| 432 | end |
| 433 | end) |
| 434 | |
| 435 | -- ---- watching players ---- |
| 436 | |
| 437 | local told: { [string]: boolean } = {} |
| 438 | local turn = 0 |
| 439 | world.every(0.1, function() |
| 440 | if #work > 0 then |
| 441 | step() |
| 442 | return |
| 443 | end |
| 444 | turn += 1 |
| 445 | -- a word to whoever comes into a kitchen, once |
| 446 | if turn % 5 == 0 then |
| 447 | for i, k in kitchens do |
| 448 | if k.built then |
| 449 | for _, p in world.players() do |
| 450 | local t = p.name .. "/" .. i |
| 451 | if not told[t] and vector.magnitude(p.position - k.at) < RX + 2 and not shown[p.id] then |
| 452 | told[t] = true |
| 453 | tell(p, "A feast hall, buried long ago. Its shelf holds old recipes.") |
| 454 | end |
| 455 | end |
| 456 | end |
| 457 | end |
| 458 | end |
| 459 | if turn % 20 ~= 0 then |
| 460 | return |
| 461 | end |
| 462 | local players = world.players() |
| 463 | if #players == 0 or #kitchens >= MOST then |
| 464 | return |
| 465 | end |
| 466 | local p = players[(turn // 20) % #players + 1] |
| 467 | local pos = p.position |
| 468 | -- the Long Dusk has finds of its own |
| 469 | if world.place(pos) == "dusk" then |
| 470 | return |
| 471 | end |
| 472 | -- only underground, well away from the Green |
| 473 | if world.top(pos.x, pos.z) < pos.y + DEEP or pos.y > HIGHEST then |
| 474 | return |
| 475 | end |
| 476 | for _, name in { "green", "start" } do |
| 477 | local at = world.spot(name) |
| 478 | if at and flat(pos, at) < 90 then |
| 479 | return |
| 480 | end |
| 481 | end |
| 482 | if not far_from_others(pos, knobs.apart) then |
| 483 | return |
| 484 | end |
| 485 | local c, way = look_near(p) |
| 486 | if c and way then |
| 487 | local cards = {} |
| 488 | for _ = 1, #DISHES do |
| 489 | table.insert(cards, true) |
| 490 | end |
| 491 | local k: Kitchen = { |
| 492 | at = c, |
| 493 | built = false, |
| 494 | cards = cards, |
| 495 | menu = if hash(c.x + 7, c.y, c.z) < knobs.menus then 1 + math.floor(hash(c.x, c.y + 3, c.z) * #DISHES) else 0, |
| 496 | way = way, |
| 497 | } |
| 498 | table.insert(kitchens, k) |
| 499 | building = k |
| 500 | plan(k) |
| 501 | save() |
| 502 | end |
| 503 | end) |
| 504 | |
| 505 | -- a kitchen left half built (the world stopped mid-way) is built on |
| 506 | for _, k in kitchens do |
| 507 | if not k.built then |
| 508 | building = k |
| 509 | plan(k) |
| 510 | break |
| 511 | end |
| 512 | end |
| 513 | |
| 514 | world.on("player_left", function(p) |
| 515 | shown[p.id] = nil |
| 516 | end) |