Sign in
mod

Fire

by Potluck · version 1 · Checked for all ages

Real fire: light it with a flint and steel or an ember, and it burns for a while and goes out. Beside wood, leaves, plants, hay or wool it spreads, slowly, and may burn them away. Water and rain put it out, lightning can start one, and standing in it hurts. Knobs turn spreading off.

1--!strict
2-- Fire. A flint and steel lights it (Interact while aiming at a block holding
3-- one, or an Ember Imp's ember, which is used up), and it burns for a
4-- while, then goes out. Beside wood, leaves, plants, hay or wool it spreads,
5-- slowly: at most a block every few seconds from each fire. When a fire
6-- goes out the wood or wool it was eating may burn away. Water puts it
7-- out, and so does breaking it, and so does rain on a fire out under the
8-- sky; lightning lights one where it strikes (@potluck/sky). Standing in
9-- it hurts.
10--
11-- The fire block is in potluck.toml, drawn in four pictures it shows in turn
12-- (frames); with Reduce flashing on, players see the first one only. A
13-- knob turns spreading off, and so does a world whose rules say fire
14-- doesn't spread (world.rule, world 0.28: a server's fire_spreads, or the
15-- world's own Change page).
16
17local world = require("@potluck/world@0.34")
18local tweak = require("@potluck/tweak")
19local store = require("@potluck/store")
20local sound = require("@potluck/sound@0.2")
21local sky = require("@potluck/sky@0.1")
22
23local knobs = tweak {
24 spreads = { true, label = "Fire spreads", order = 1 },
25 burns = { 30, min = 5, max = 120, label = "Seconds a fire burns", order = 2 },
26 spread = { 4, min = 1, max = 30, label = "Seconds between spreading, at quickest", order = 3 },
27 eats = { true, label = "Burns away wood and wool", order = 4 },
28 hurts = { true, label = "Standing in fire hurts", order = 5 },
29 lighter = { 64, min = 8, max = 256, label = "Uses of a flint and steel", order = 6 },
30 rain = { true, label = "Rain puts out fires", order = 7 },
31 lightning = { true, label = "Lightning lights fires", order = 8 },
32}
33
34local FIRE = "Fire"
35local LIGHTER = "Flint and steel"
36local EMBER = "Ember" -- lights one fire, and is gone
37local MOST = 256 -- fires burning at once, at most
38
39-- What burns: wood and plants by their kind, and these by name. A remix can
40-- add its own.
41local BURNS: { [string]: boolean } = {
42 ["Red wool"] = true,
43 ["Cream wool"] = true,
44 ["Blue wool"] = true,
45 ["Yellow wool"] = true,
46 Hay = true,
47}
48-- Never: what other creations light themselves, and fire itself.
49local NEVER: { [string]: boolean } = { [FIRE] = true, Campfire = true, Furnace = true, ["Lit furnace"] = true }
50
51local function burns(name: string): boolean
52 if NEVER[name] then
53 return false
54 end
55 if BURNS[name] then
56 return true
57 end
58 local ok, kind = pcall(world.kind, name)
59 return ok and (kind == "wood" or kind == "plant")
60end
61
62local SIDES = {
63 vector.create(1, 0, 0),
64 vector.create(-1, 0, 0),
65 vector.create(0, 1, 0),
66 vector.create(0, -1, 0),
67 vector.create(0, 0, 1),
68 vector.create(0, 0, -1),
69}
70
71-- Burning fires, by where they are: when each goes out, and when it may
72-- next spread.
73type Burning = { at: vector, out: number, next: number, rained: number? }
74local fires: { [string]: Burning } = {}
75local count = 0
76local changed = false
77
78local function key(at: vector): string
79 return math.floor(at.x) .. "," .. math.floor(at.y) .. "," .. math.floor(at.z)
80end
81
82local function middle(at: vector): vector
83 return vector.create(math.floor(at.x) + 0.5, math.floor(at.y) + 0.5, math.floor(at.z) + 0.5)
84end
85
86-- whether anything beside `at` burns
87local function fuel_by(at: vector): boolean
88 for _, d in SIDES do
89 if burns(world.block(at + d)) then
90 return true
91 end
92 end
93 return false
94end
95
96local function watched(at: vector)
97 local k = key(at)
98 if fires[k] then
99 return
100 end
101 local now = world.time()
102 -- with nothing to eat, a fire dies down three times as fast
103 local lasts = knobs.burns * world.random(0.7, 1.3) / (if fuel_by(at) then 1 else 3)
104 fires[k] = { at = middle(at), out = now + lasts, next = now + knobs.spread * world.random(1, 3) }
105 count += 1
106 changed = true
107end
108
109-- Starts a fire at `at` if there's room for one: air, or a plant it burns up.
110local function light(at: vector): boolean
111 if count >= MOST then
112 return false
113 end
114 local here = world.block(at)
115 if here ~= "Air" and not (burns(here) and world.kind(here) == "plant") then
116 return false
117 end
118 for _, d in SIDES do
119 if world.block(at + d) == "Water" then
120 return false
121 end
122 end
123 world.set_block(at, FIRE)
124 watched(at)
125 sound.play("crackle", { at = middle(at), volume = 0.5 })
126 return true
127end
128
129local function forget(k: string)
130 if fires[k] then
131 fires[k] = nil
132 count -= 1
133 changed = true
134 end
135end
136
137local function put_out(k: string, f: Burning)
138 if world.block(f.at) == FIRE then
139 world.set_block(f.at, "Air")
140 end
141 forget(k)
142end
143
144-- An air block beside `at`, next to something that burns, picked at random.
145local function catch(at: vector): vector?
146 local tries = {}
147 for _, d in SIDES do
148 local p = at + d
149 for _, e in SIDES do
150 local q = p + e
151 if world.block(q) == "Air" and fuel_by(q) then
152 table.insert(tries, q)
153 end
154 end
155 end
156 if #tries == 0 then
157 return nil
158 end
159 return tries[math.floor(world.random(1, #tries + 0.999))]
160end
161
162-- whether rain's falling on `at`: rain or a storm, and nothing over it
163-- but leaves (which it drips through)
164local function rained_on(at: vector, wet: number): boolean
165 return wet > 0 and world.top(at.x, at.z) <= math.floor(at.y)
166end
167
168-- how hard it's raining, 0 to 1 (snow puts nothing out)
169local function raining(): number
170 if not knobs.rain then
171 return 0
172 end
173 local w = sky.weather()
174 return if w.kind == "rain" or w.kind == "storm" then math.max(w.strength, 0.3) else 0
175end
176
177world.every(0.5, function()
178 local now = world.time()
179 local rain = raining()
180 local spreading: { vector } = {}
181 local spreads = knobs.spreads and world.rule("fire_spreads")
182 local eaten: { vector } = {}
183 for k, f in fires do
184 if world.block(f.at) ~= FIRE then
185 -- broken, built over or put out by something else
186 forget(k)
187 continue
188 end
189 local wet = false
190 for _, d in SIDES do
191 if world.block(f.at + d) == "Water" then
192 wet = true
193 end
194 end
195 -- rain puts it out within a few seconds, sooner the harder it falls
196 local in_rain = rained_on(f.at, rain)
197 f.rained = if in_rain then (f.rained or 0) + 0.5 else nil
198 if wet or (in_rain and (f.rained or 0) >= 3 / rain) then
199 put_out(k, f)
200 sound.play("hiss", { at = f.at, volume = 0.6 })
201 continue
202 end
203 if now >= f.out then
204 put_out(k, f)
205 if knobs.eats then
206 for _, d in SIDES do
207 local p = f.at + d
208 local name = world.block(p)
209 if burns(name) and world.kind(name) ~= "plant" and world.random() < 0.4 then
210 table.insert(eaten, p)
211 end
212 end
213 end
214 continue
215 end
216 if spreads and now >= f.next and not in_rain then
217 f.next = now + knobs.spread * world.random(1, 3)
218 local q = catch(f.at)
219 if q then
220 table.insert(spreading, q)
221 end
222 end
223 end
224 -- what burnt away may catch, so fire moves through a wooden house
225 for _, p in eaten do
226 world.set_block(p, "Air")
227 if spreads then
228 light(p)
229 end
230 end
231 for _, q in spreading do
232 light(q)
233 end
234end)
235
236-- Keeps the fires in the world's store, so they burn on after a restart.
237world.every(2, function()
238 if not changed then
239 return
240 end
241 changed = false
242 local now = world.time()
243 local kept = {}
244 for _, f in fires do
245 table.insert(kept, { f.at.x, f.at.y, f.at.z, math.max(1, f.out - now) })
246 end
247 store.set("fires", kept)
248end)
249
250do
251 local now = world.time()
252 for _, f in (store.get("fires") or {}) :: { { number } } do
253 local at = vector.create(f[1], f[2], f[3])
254 if world.block(at) == FIRE then
255 fires[key(at)] = { at = at, out = now + f[4], next = now + knobs.spread }
256 count += 1
257 end
258 end
259end
260
261-- Standing in fire hurts, and armor doesn't help (guarded = false).
262world.every(1, function()
263 if not knobs.hurts then
264 return
265 end
266 for _, p in world.players() do
267 local feet = p.position
268 if world.block(feet) == FIRE or world.block(feet + vector.create(0, 1, 0)) == FIRE then
269 -- a Cinderward tonic (the fireproof perk) walks through it
270 if world.perk(p, "fireproof") == 0 and world.hurt(p, 1, { guarded = false }) then
271 sound.play_for(p, "crackle", { caption = "You're burning" })
272 end
273 end
274 end
275end)
276
277-- Flint and steel, or an ember: lights the side of the block nearest the
278-- player, or its top.
279local used: { [number]: number } = {}
280
281world.on("block_used", function(player, at: vector, block: string)
282 local held = world.held(player)
283 if (held ~= LIGHTER and held ~= EMBER) or NEVER[block] then
284 return
285 end
286 local eye = player.position + vector.create(0, 1.6, 0)
287 local d = eye - at
288 local side
289 if math.abs(d.y) >= math.abs(d.x) and math.abs(d.y) >= math.abs(d.z) then
290 side = vector.create(0, if d.y > 0 then 1 else -1, 0)
291 elseif math.abs(d.x) >= math.abs(d.z) then
292 side = vector.create(if d.x > 0 then 1 else -1, 0, 0)
293 else
294 side = vector.create(0, 0, if d.z > 0 then 1 else -1)
295 end
296 if held == LIGHTER then
297 sound.play("strike", { at = at })
298 end
299 if not (light(at + side) or light(at + vector.create(0, 1, 0))) then
300 return
301 end
302 if held == EMBER then
303 world.take(player, EMBER, 1)
304 return
305 end
306 -- it wears out, in survival
307 if world.pockets(player) ~= nil then
308 used[player.id] = (used[player.id] or 0) + 1
309 if used[player.id] >= knobs.lighter then
310 used[player.id] = 0
311 world.take(player, LIGHTER, 1)
312 sound.play_for(player, "crack", { caption = "Flint and steel wore out" })
313 end
314 end
315end)
316
317-- Lightning lights a fire where it strikes, or just under it (the top of
318-- a tree).
319sky.on("strike", function(at: vector)
320 if knobs.lightning then
321 local _ = light(at) or light(at - vector.create(0, 1, 0))
322 end
323end)
324
325-- Fire put down from the Blocks sheet in creative burns like any other.
326world.on("block_placed", function(_player, at: vector, block: string)
327 if block == FIRE then
328 watched(at)
329 end
330end)
331
332world.on("player_left", function(player)
333 used[player.id] = nil
334end)
335
336-- Breaking fire puts it out, and gives nothing.
337world.drops(FIRE, { gives = false })