Sign in
mod

Brickworks

by Potluck · version 1 · Checked for all ages

Blocks of fired clay: red bricks, terracotta (the game's own, from the badlands), roof tiles, clay slabs and paper lanterns, made in a kiln. Each block is drawn in letters in potluck.toml, so change a color or a row there and the world shows it.

1--!strict
2-- Brickworks: blocks of fired clay, made in a kiln. The blocks themselves
3-- are in potluck.toml, each drawn in letters ([blocks.red_bricks] and the
4-- rest): change a color or a row there and the world shows it. This file
5-- is the kiln: use one (Interact while aiming at it) in survival to fire
6-- what you carry into bricks, terracotta, roof tiles, slabs and glass. Make
7-- a kiln, and paper lanterns, at a workbench.
8
9local world = require("@potluck/world@0.29")
10local tweak = require("@potluck/tweak")
11local ui = require("@potluck/ui@0.1")
12local sound = require("@potluck/sound@0.2")
13
14local knobs = tweak {
15 generous = { 1, min = 1, max = 3, label = "Fires this many times as much", order = 1 },
16}
17
18local KILN = "Kiln"
19
20-- A firing: what goes in, what comes out and how many.
21type Firing = { want: { [string]: number }, give: string, n: number }
22
23-- Up to eight (a question has eight choices at most).
24local FIRINGS: { Firing } = {
25 { want = { Dirt = 4 }, give = "Red bricks", n = 4 },
26 { want = { Gravel = 4 }, give = "Terracotta", n = 4 },
27 { want = { Terracotta = 2 }, give = "Roof tiles", n = 4 },
28 { want = { Terracotta = 1 }, give = "Clay slab", n = 2 },
29 { want = { Sand = 2 }, give = "Glass", n = 1 },
30}
31
32local function makes(f: Firing): number
33 return math.floor(f.n * knobs.generous)
34end
35
36-- what it takes: "2 Terracotta"
37local function needs(f: Firing): string
38 local parts = {}
39 for block, n in f.want do
40 table.insert(parts, n .. " " .. block)
41 end
42 table.sort(parts)
43 return table.concat(parts, " and ")
44end
45
46-- Fires one lot, or says why not.
47local function fire(player, f: Firing): string
48 for block, n in f.want do
49 if world.has(player, block) < n then
50 return "You'll need " .. needs(f) .. " for that."
51 end
52 end
53 for block, n in f.want do
54 world.take(player, block, n)
55 end
56 local given = world.give(player, f.give, makes(f))
57 if given == 0 then
58 for block, n in f.want do
59 world.give(player, block, n)
60 end
61 return "Your hotbar is full. Make some room first."
62 end
63 return "Fired " .. given .. " " .. f.give .. "."
64end
65
66-- `said` is how the last firing went
67local function choose(player, at: vector, said: string?)
68 if world.pockets(player) == nil then
69 ui.ask(player, {
70 title = "Kiln",
71 text = "In survival, use a kiln to fire what you carry: dirt into red bricks, gravel into terracotta, terracotta into roof tiles and slabs, and sand into glass.",
72 choices = { "OK" },
73 }, function() end)
74 return
75 end
76 local choices = {}
77 for _, f in FIRINGS do
78 table.insert(choices, makes(f) .. " " .. f.give .. " for " .. needs(f))
79 end
80 local text = (if said then said .. "\n\n" else "") .. "What would you like to fire?"
81 ui.ask(player, { title = "Kiln", text = text, choices = choices }, function(who, choice)
82 local f = choice and FIRINGS[choice]
83 if not f then
84 return
85 end
86 local result = fire(who, f)
87 if string.sub(result, 1, 5) == "Fired" then
88 sound.play("roar", { at = at })
89 end
90 -- ask again, so firing several lots is quick
91 choose(who, at, result)
92 end)
93end
94
95world.on("block_used", function(player, at: vector, block: string)
96 if block == KILN then
97 choose(player, at)
98 end
99end)
100
101-- Told to the item guide: what the kiln fires (world.recipes).
102world.recipes("Kiln", FIRINGS)