Sign in
mod

Furnaces and campfires

by Potluck · version 1 · Checked for all ages

A furnace smelts raw metal into ingots, logs into charcoal, sand into glass and cobblestone into stone, and cooks bread, roasted carrots, baked potatoes, fried eggs, fish and meat, burning coal, charcoal, embers, logs or planks. A campfire cooks with no fuel, and its light keeps Gloomlings away. A cooking pot over a fire cooks pies, stews, porridge and honey cake, and new dishes from recipe cards. Make them all at a workbench.

1--!strict
2-- Furnaces and campfires, for survival. Use a furnace (Interact while aiming
3-- at it) to smelt what you carry: raw metal into ingots, logs into charcoal,
4-- sand into glass and cobblestone back into stone. It cooks food too. It
5-- takes its fuel from your pockets (coal, charcoal, logs or planks), lights
6-- up and works through the lot a few seconds at a time, and what it makes
7-- waits inside until you, or anyone, use it again.
8--
9-- A campfire cooks food with no fuel, and its light keeps Gloomlings back.
10--
11-- A cooking pot set over a campfire, a fire or a lit furnace (under it or
12-- beside it) cooks dishes of several things at once: pies, stews, porridge
13-- and honey cake (DISHES below), and any item another creation makes with a
14-- `cook` (world 0.27), for a cook carrying its `card` if it has one, as the
15-- Deep Kitchen's recipe cards are. Use it and pick one.
16--
17-- SMELTS, COOKS, DISHES and FUELS below say what goes in, what comes out
18-- and how far fuel goes, so a remix can add its own. The blocks are in potluck.toml;
19-- the lit furnace and the campfire flicker through pictures of their own.
20
21local world = require("@potluck/world@0.29")
22local tweak = require("@potluck/tweak")
23local store = require("@potluck/store")
24local ui = require("@potluck/ui@0.2")
25local sound = require("@potluck/sound@0.2")
26
27local knobs = tweak {
28 seconds = { 5, min = 1, max = 20, label = "Seconds to smelt or cook one", order = 1 },
29 fuel = { 1, min = 0.5, max = 4, label = "How far fuel goes", order = 2 },
30 most = { 16, min = 1, max = 64, label = "Most it takes in at once", order = 3 },
31}
32
33local FURNACE, LIT, CAMPFIRE = "Furnace", "Lit furnace", "Campfire"
34-- The Power creation's furnace smelts the same, with no fuel while its grid has power.
35local ELEC, ELEC_ON = "Electric furnace", "Lit electric furnace"
36
37-- A recipe: `n` of `from` make `count` of `gives`.
38type Recipe = { from: string, n: number, gives: string, count: number }
39
40-- Only a furnace is hot enough for these.
41local SMELTS: { Recipe } = {
42 { from = "Raw copper", n = 1, gives = "Copper ingot", count = 1 },
43 { from = "Raw iron", n = 1, gives = "Iron ingot", count = 1 },
44 { from = "Raw gold", n = 1, gives = "Gold ingot", count = 1 },
45 { from = "Raw moonsilver", n = 1, gives = "Moonsilver ingot", count = 1 },
46 { from = "Oak log", n = 1, gives = "Charcoal", count = 1 },
47 { from = "Birch log", n = 1, gives = "Charcoal", count = 1 },
48 { from = "Pine log", n = 1, gives = "Charcoal", count = 1 },
49 { from = "Maple log", n = 1, gives = "Charcoal", count = 1 },
50 { from = "Willow log", n = 1, gives = "Charcoal", count = 1 },
51 { from = "Palm log", n = 1, gives = "Charcoal", count = 1 },
52 { from = "Redwood log", n = 1, gives = "Charcoal", count = 1 },
53 { from = "Sand", n = 1, gives = "Glass", count = 1 },
54 { from = "Cobblestone", n = 1, gives = "Stone", count = 1 },
55}
56
57-- A furnace or a campfire cooks these.
58local COOKS: { Recipe } = {
59 { from = "Carrots", n = 1, gives = "Roasted carrots", count = 1 },
60 { from = "Raw fish", n = 1, gives = "Cooked fish", count = 1 },
61 { from = "Wheat", n = 3, gives = "Bread", count = 1 },
62 { from = "Potatoes", n = 1, gives = "Baked potato", count = 1 },
63 { from = "Egg", n = 1, gives = "Fried egg", count = 1 },
64 { from = "Raw meat", n = 1, gives = "Cooked meat", count = 1 },
65 -- the Fish creation's fish, the Snapjaw eels' eels
66 { from = "Perch", n = 1, gives = "Cooked fish", count = 1 },
67 { from = "Trout", n = 1, gives = "Cooked fish", count = 1 },
68 { from = "Mackerel", n = 1, gives = "Cooked fish", count = 1 },
69 { from = "Flounder", n = 1, gives = "Cooked fish", count = 1 },
70 { from = "Eel", n = 1, gives = "Cooked fish", count = 1 },
71}
72
73-- XP for each one taken out of a furnace or campfire (world.reward), and
74-- for anything else made there; a fraction is a chance of one more.
75local XP: { [string]: number } = {
76 ["Copper ingot"] = 0.7,
77 ["Iron ingot"] = 1,
78 ["Gold ingot"] = 1.5,
79 ["Moonsilver ingot"] = 2,
80}
81local XP_ELSE = 0.25
82local XP_DISH = 2
83
84-- Some XP for `n` of `made`, rounding the fraction by chance.
85local function reward(player, made: string, n: number)
86 local xp = n * (XP[made] or XP_ELSE)
87 world.reward(player, math.floor(xp + world.random()))
88end
89
90-- How many lots each fuel smelts, best first: the furnace burns the first
91-- it finds in your pockets.
92local FUELS: { { name: string, lots: number } } = {
93 { name = "Coal", lots = 8 },
94 { name = "Charcoal", lots = 8 },
95 -- an Ember Imp's ember burns long and hot
96 { name = "Ember", lots = 6 },
97 -- a Bogle's peat smolders long
98 { name = "Peat", lots = 5 },
99 { name = "Oak log", lots = 2 },
100 { name = "Birch log", lots = 2 },
101 { name = "Pine log", lots = 2 },
102 { name = "Maple log", lots = 2 },
103 { name = "Willow log", lots = 2 },
104 { name = "Palm log", lots = 2 },
105 { name = "Redwood log", lots = 2 },
106 { name = "Planks", lots = 1 },
107 { name = "Birch planks", lots = 1 },
108 { name = "Pine planks", lots = 1 },
109 { name = "Maple planks", lots = 1 },
110 { name = "Willow planks", lots = 1 },
111 { name = "Palm planks", lots = 1 },
112 { name = "Redwood planks", lots = 1 },
113 -- what fishing rods bring up
114 { name = "Driftwood", lots = 1 },
115}
116
117-- What a furnace or campfire has in it: the recipe it's working through,
118-- how many lots are left, what's made and waiting, how much fuel is left
119-- (in lots) and when the next lot is done.
120type Cooker = { at: vector, recipe: number?, smelt: boolean?, left: number, made: string?, ready: number, fuel: number, next: number }
121local cookers: { [string]: Cooker } = {}
122local changed = false
123
124local function key(at: vector): string
125 return math.floor(at.x) .. "," .. math.floor(at.y) .. "," .. math.floor(at.z)
126end
127
128local function cooker(at: vector): Cooker
129 local k = key(at)
130 local c = cookers[k]
131 if not c then
132 c = { at = at, left = 0, ready = 0, fuel = 0, next = 0 }
133 cookers[k] = c
134 end
135 return c
136end
137
138local function recipe_of(c: Cooker): Recipe?
139 if not c.recipe then
140 return nil
141 end
142 return if c.smelt then SMELTS[c.recipe] else COOKS[c.recipe]
143end
144
145-- Whether a block or item is in this world: eggs, potatoes and the like
146-- come from other creations.
147local function exists(name: string): boolean
148 return (pcall(world.kind, name))
149end
150
151-- "3 Raw iron"
152local function some(n: number, name: string): string
153 return n .. " " .. name
154end
155
156-- Gives a player what's waiting, as much as fits.
157local function collect(player, c: Cooker): string?
158 if not c.made or c.ready == 0 then
159 return nil
160 end
161 local given = world.give(player, c.made, c.ready)
162 local made = c.made
163 c.ready -= given
164 if c.ready == 0 and c.left == 0 then
165 c.made = nil
166 end
167 changed = true
168 if given == 0 then
169 return "Your pockets are full, so the " .. made .. " waits here."
170 end
171 reward(player, made, given)
172 return "You took " .. some(given, made) .. "."
173end
174
175-- Burns fuel from a player's pockets until there's enough for `lots`, and
176-- says how many lots it can do.
177local function stoke(player, c: Cooker, lots: number): number
178 if world.block(c.at) == ELEC_ON then
179 c.fuel = math.max(c.fuel, lots)
180 return lots
181 end
182 local per = function(f: { name: string, lots: number }): number
183 return f.lots * knobs.fuel
184 end
185 for _, f in FUELS do
186 -- (fuels other creations make are only here with them)
187 while c.fuel < lots and exists(f.name) and world.has(player, f.name) >= 1 do
188 world.take(player, f.name, 1)
189 c.fuel += per(f)
190 end
191 end
192 return math.min(lots, math.floor(c.fuel))
193end
194
195-- Puts a lot of something in, if the cooker can take it.
196local function load(player, c: Cooker, list: { Recipe }, i: number, furnace: boolean): string
197 local r = list[i]
198 local smelt = list == SMELTS
199 if c.left > 0 and (c.recipe ~= i or c.smelt ~= smelt) then
200 return "It's busy with " .. (recipe_of(c) :: Recipe).from .. ". Come back when it's done."
201 end
202 if c.made and c.made ~= r.gives and c.ready > 0 then
203 return "Take out the " .. c.made .. " first."
204 end
205 local lots = math.min(math.floor(world.has(player, r.from) / r.n), math.max(1, math.floor(knobs.most / r.n)))
206 if lots < 1 then
207 return "You'll need " .. some(r.n, r.from) .. " for that."
208 end
209 -- in first, so the furnace doesn't burn what it's to smelt
210 world.take(player, r.from, lots * r.n)
211 if furnace then
212 local fueled = stoke(player, c, lots)
213 if fueled < lots then
214 world.give(player, r.from, (lots - fueled) * r.n)
215 end
216 lots = fueled
217 if lots < 1 then
218 if world.block(c.at) == ELEC then
219 return "It needs power: join it to a grid that has some."
220 end
221 return "It needs fuel: coal, charcoal, logs or planks."
222 end
223 end
224 if c.left == 0 then
225 c.next = world.time() + knobs.seconds
226 end
227 c.recipe, c.smelt, c.made = i, smelt, r.gives
228 c.left += lots
229 changed = true
230 if furnace and world.block(c.at) == FURNACE then
231 world.set_block(c.at, LIT)
232 end
233 sound.play(if furnace then "roar" else "crackle", { at = c.at, volume = 0.6 })
234 return (if furnace and smelt then "Smelting " else "Cooking ") .. some(lots * r.n, r.from) .. "."
235end
236
237-- What it's doing, in a line or two.
238local function doing(c: Cooker): string
239 local r = recipe_of(c)
240 local lines = {}
241 if r and c.left > 0 then
242 table.insert(lines, some(c.left * r.n, r.from) .. " to go, " .. math.ceil(c.left * knobs.seconds) .. " seconds.")
243 end
244 if c.made and c.ready > 0 then
245 table.insert(lines, some(c.ready, c.made) .. " ready.")
246 end
247 return table.concat(lines, " ")
248end
249
250local choose
251
252-- `said` is how the last thing went
253function choose(player, at: vector, block: string, said: string?)
254 local furnace = block ~= CAMPFIRE
255 local title = if furnace then "Furnace" else "Campfire"
256 if world.pockets(player) == nil then
257 ui.ask(player, {
258 title = title,
259 text = if furnace
260 then "In survival, a furnace smelts raw metal into ingots, logs into charcoal and sand into glass, and cooks food, burning coal, charcoal, logs or planks."
261 else "In survival, a campfire cooks food with no fuel. Its light keeps Gloomlings away.",
262 choices = { "OK" },
263 }, function() end)
264 return
265 end
266 local c = cooker(at)
267 local took = collect(player, c)
268 -- what they carry that it can take: smelting first, then cooking
269 local options: { { list: { Recipe }, i: number } } = {}
270 local choices = {}
271 local lists = if furnace then { SMELTS, COOKS } else { COOKS }
272 for _, list in lists do
273 for i, r in list do
274 if exists(r.from) and world.has(player, r.from) >= r.n and #choices < 8 then
275 table.insert(options, { list = list, i = i })
276 local verb = if list == SMELTS then "Smelt " else "Cook "
277 table.insert(choices, verb .. r.from)
278 end
279 end
280 end
281 local parts = {}
282 for _, s in { said, took, doing(c) } do
283 if s and s ~= "" then
284 table.insert(parts, s)
285 end
286 end
287 if #choices == 0 then
288 table.insert(parts, if furnace then "Bring raw metal, logs, sand, cobblestone, or food to cook: carrots, potatoes, eggs, fish or wheat." else "Bring food to cook: carrots, potatoes, eggs, fish or wheat.")
289 else
290 table.insert(parts, "What would you like to put in?")
291 end
292 ui.ask(player, { title = title, text = table.concat(parts, "\n\n"), choices = if #choices > 0 then choices else { "OK" } }, function(who, choice)
293 local o = choice and options[choice]
294 if not o then
295 return
296 end
297 -- what's in it now: the one asked about may have been tidied away
298 choose(who, at, world.block(at), load(who, cooker(at), o.list, o.i, furnace))
299 end)
300end
301
302-- ---- the cooking pot ----
303
304local POT = "Cooking pot"
305-- what's hot enough to cook over, under the pot or beside it
306local HEAT: { [string]: boolean } = { [CAMPFIRE] = true, [LIT] = true, [ELEC_ON] = true, Fire = true }
307local NEAR = { vector.create(0, -1, 0), vector.create(1, 0, 0), vector.create(-1, 0, 0), vector.create(0, 0, 1), vector.create(0, 0, -1) }
308
309-- A dish: what it takes, what it makes and how many, what comes back (the
310-- bucket the milk was in) and a card its cook carries, if it needs one.
311-- Eggs come from the Hens creation, honey from Bees and the rest from
312-- Farming, and a dish shows only when all it takes is in the world.
313type Dish = { want: { [string]: number }, give: string, n: number, back: string?, card: string? }
314local DISHES: { Dish } = {
315 { want = { Pumpkin = 1, Egg = 1, Wheat = 2 }, give = "Pumpkin pie", n = 2 },
316 { want = { Berries = 4, Egg = 1, Wheat = 2 }, give = "Berry pie", n = 2 },
317 { want = { Beans = 2, Potatoes = 1, Carrots = 1 }, give = "Bean stew", n = 2 },
318 { want = { ["Raw fish"] = 1, Potatoes = 1, ["Milk bucket"] = 1 }, give = "Fish stew", n = 2, back = "Bucket" },
319 { want = { Wheat = 2, ["Milk bucket"] = 1 }, give = "Porridge", n = 2, back = "Bucket" },
320 { want = { Wheat = 3, Egg = 1, Honey = 1, ["Milk bucket"] = 1 }, give = "Honey cake", n = 2, back = "Bucket" },
321}
322
323-- Dishes other creations' items say a pot cooks: `cook` is what it takes,
324-- `makes` how many and `card` what its cook carries (world 0.27).
325local function made_dishes(): { Dish }
326 local out: { Dish } = {}
327 for _, name in world.items() do
328 local it = world.item(name)
329 if it and it.cook then
330 local back: string? = nil
331 for thing in it.cook do
332 if thing == "Milk bucket" then
333 back = "Bucket"
334 end
335 end
336 table.insert(out, { want = it.cook, give = name, n = it.makes or 1, back = back, card = it.card })
337 end
338 end
339 table.sort(out, function(a, b)
340 return a.give < b.give
341 end)
342 return out
343end
344
345local function hot(at: vector): boolean
346 for _, d in NEAR do
347 if HEAT[world.block(at + d)] then
348 return true
349 end
350 end
351 return false
352end
353
354-- what a dish takes: "1 Egg and 1 Pumpkin and 2 Wheat"
355local function takes(d: Dish): string
356 local parts = {}
357 for name, n in d.want do
358 table.insert(parts, n .. " " .. name)
359 end
360 table.sort(parts)
361 return table.concat(parts, " and ")
362end
363
364-- what a dish takes, shorter, for the list: "Egg, Pumpkin, 2 Wheat"
365local function short(d: Dish): string
366 local parts = {}
367 for name, n in d.want do
368 table.insert(parts, if n == 1 then name else n .. " " .. name)
369 end
370 table.sort(parts)
371 return table.concat(parts, ", ")
372end
373
374-- Cooks one, or says why not.
375local function cook(player, d: Dish): (boolean, string)
376 if d.card and world.has(player, d.card) < 1 then
377 return false, "You'll need the " .. d.card .. " with you."
378 end
379 for name, n in d.want do
380 if world.has(player, name) < n then
381 return false, "You'll need " .. takes(d) .. " for that."
382 end
383 end
384 for name, n in d.want do
385 world.take(player, name, n)
386 end
387 local given = world.give(player, d.give, d.n)
388 if given == 0 then
389 for name, n in d.want do
390 world.give(player, name, n)
391 end
392 return false, "Your pockets are full. Make some room first."
393 end
394 if d.back then
395 world.give(player, d.back, 1)
396 end
397 world.reward(player, XP_DISH)
398 return true, "You cooked " .. some(given, d.give) .. "."
399end
400
401-- dishes on a page of the pot's card, when there are more than fit
402local PAGE = 6
403
404local function stir(player, at: vector, said: string?, page: number?)
405 if world.pockets(player) == nil then
406 ui.ask(player, { title = "Cooking pot", text = "In survival, a cooking pot over a campfire cooks pies, stews, porridge and honey cake.", choices = { "OK" } }, function() end)
407 return
408 end
409 if not hot(at) then
410 ui.ask(player, { title = "Cooking pot", text = "It's cold. Set it over a campfire, a fire or a lit furnace, or right beside one.", choices = { "OK" } }, function() end)
411 return
412 end
413 -- every dish this world has, for this cook (a dish with a card only
414 -- with the card in their pockets)
415 local all: { Dish } = {}
416 for _, d in DISHES do
417 table.insert(all, d)
418 end
419 for _, d in made_dishes() do
420 table.insert(all, d)
421 end
422 local can: { Dish } = {}
423 for _, d in all do
424 local ok = exists(d.give) and (d.card == nil or world.has(player, d.card) > 0)
425 for name in d.want do
426 ok = ok and exists(name)
427 end
428 if ok then
429 table.insert(can, d)
430 end
431 end
432 if #can == 0 then
433 ui.ask(player, { title = "Cooking pot", text = "There's nothing to cook in this world yet.", choices = { "OK" } }, function() end)
434 return
435 end
436 -- up to eight on one card, or pages of six and a way to the next
437 local pages = if #can <= 8 then 1 else math.ceil(#can / PAGE)
438 local this = if pages == 1 then 1 else ((page or 1) - 1) % pages + 1
439 local per = if pages == 1 then #can else PAGE
440 local list, choices, lines = {}, {}, {}
441 for i = (this - 1) * per + 1, math.min(this * per, #can) do
442 local d = can[i]
443 table.insert(list, d)
444 table.insert(choices, some(d.n, d.give))
445 table.insert(lines, d.give .. ": " .. short(d))
446 end
447 if pages > 1 then
448 table.insert(choices, "More dishes")
449 end
450 local ask = if pages > 1 then "What would you like to cook? (" .. this .. " of " .. pages .. ")" else "What would you like to cook?"
451 local text = (if said then said .. "\n\n" else "") .. ask .. "\n\n" .. table.concat(lines, "\n")
452 ui.ask(player, { title = "Cooking pot", text = text, choices = choices }, function(who, choice)
453 if pages > 1 and choice == #choices then
454 stir(who, at, nil, this + 1)
455 return
456 end
457 local d = choice and list[choice]
458 if not d then
459 return
460 end
461 local cooked, said = cook(who, d)
462 if cooked then
463 sound.play("crackle", { at = at, volume = 0.5, caption = "Something's cooking" })
464 end
465 -- ask again, on the same page, so cooking several is quick
466 stir(who, at, said, this)
467 end)
468end
469
470world.on("block_used", function(player, at: vector, block: string)
471 if block == FURNACE or block == LIT or block == CAMPFIRE or block == ELEC or block == ELEC_ON then
472 choose(player, at, block)
473 elseif block == POT then
474 stir(player, at)
475 end
476end)
477
478-- Breaking one gives back what was in it, as much as fits.
479world.on("block_broken", function(player, at: vector, block: string)
480 if block ~= FURNACE and block ~= LIT and block ~= CAMPFIRE and block ~= ELEC and block ~= ELEC_ON then
481 return
482 end
483 local k = key(at)
484 local c = cookers[k]
485 if not c then
486 return
487 end
488 local r = recipe_of(c)
489 if r and c.left > 0 then
490 world.give(player, r.from, c.left * r.n)
491 end
492 if c.made and c.ready > 0 then
493 world.give(player, c.made, c.ready)
494 end
495 cookers[k] = nil
496 changed = true
497end)
498
499-- Works through each lot, and puts the furnace out when it's done.
500world.every(0.5, function()
501 local now = world.time()
502 for k, c in cookers do
503 local here = world.block(c.at)
504 if here ~= FURNACE and here ~= LIT and here ~= CAMPFIRE and here ~= ELEC and here ~= ELEC_ON then
505 cookers[k] = nil
506 changed = true
507 continue
508 end
509 local r = recipe_of(c)
510 while r and c.left > 0 and now >= c.next do
511 c.left -= 1
512 if here ~= CAMPFIRE then
513 c.fuel = math.max(0, c.fuel - 1)
514 end
515 c.ready += r.count
516 c.next += knobs.seconds
517 changed = true
518 end
519 if c.left == 0 and here == LIT then
520 world.set_block(c.at, FURNACE)
521 -- a tiny bit of fuel left over stays for next time
522 end
523 if c.left == 0 and c.ready == 0 and c.fuel < 1 then
524 cookers[k] = nil
525 changed = true
526 end
527 end
528end)
529
530-- Keeps what's in them in the world's store, so it's there after a restart.
531world.every(2, function()
532 if not changed then
533 return
534 end
535 changed = false
536 local now = world.time()
537 local kept = {}
538 for _, c in cookers do
539 table.insert(kept, {
540 x = c.at.x,
541 y = c.at.y,
542 z = c.at.z,
543 recipe = c.recipe,
544 smelt = c.smelt,
545 left = c.left,
546 made = c.made,
547 ready = c.ready,
548 fuel = c.fuel,
549 wait = math.max(0, c.next - now),
550 })
551 end
552 store.set("cookers", kept)
553end)
554
555do
556 local now = world.time()
557 for _, s in (store.get("cookers") or {}) :: { any } do
558 local at = vector.create(s.x, s.y, s.z)
559 cookers[key(at)] = { at = at, recipe = s.recipe, smelt = s.smelt, left = s.left, made = s.made, ready = s.ready, fuel = s.fuel, next = now + s.wait }
560 end
561end
562
563-- A lit furnace breaks into a furnace.
564world.drops(LIT, { gives = FURNACE })
565
566-- Told to the item guide: what the furnace smelts and cooks and what the pot
567-- makes (world.recipes).
568do
569 local function from(list: { Recipe })
570 local book = {}
571 for _, r in list do
572 table.insert(book, { give = r.gives, n = r.count, want = { [r.from] = r.n } })
573 end
574 return book
575 end
576 world.recipes("Furnace", from(SMELTS))
577 world.recipes("Cooking", from(COOKS))
578 local pot = {}
579 for _, d in DISHES do
580 table.insert(pot, { give = d.give, n = d.n, want = d.want, note = if d.back then "gives the " .. string.lower(d.back) .. " back" else nil })
581 end
582 for _, d in made_dishes() do
583 table.insert(pot, { give = d.give, n = d.n, want = d.want, note = if d.card then "needs the " .. d.card else nil })
584 end
585 world.recipes("Cooking pot", pot)
586end