Sign in
world

Old Greens

by Potluck · version 1 · Checked for all ages

Three old greens lie in ruins around the island: one by the shore, one deep in the woods and one up on the heights. Each hearth has gone cold. Bring it a lantern and something from nearby (sand, logs or stone) and use it to light it again, and it gives you something back. Light all three and the whole island hears of it.

1--!strict
2-- Old Greens: three ruined greens around the island, a reason to go far.
3-- When a world starts it looks for a place for each (a beach, a wood and a
4-- mountainside, far from the Green), lays a broken plaza with an arch and a
5-- cold hearth there, and remembers where with @potluck/store so they're
6-- built only once. Use a hearth with what it wants (a lantern and something
7-- from nearby) and it's lit again: braziers burn, the arch is mended,
8-- and it gives you something back. Every Old Green is in GREENS below, so a
9-- remix can add its own, move them or change what they want.
10
11local world = require("@potluck/world@0.22")
12local tweak = require("@potluck/tweak")
13local ui = require("@potluck/ui@0.1")
14local sound = require("@potluck/sound@0.2")
15local store = require("@potluck/store")
16
17local knobs = tweak {
18 far = { 90, min = 40, max = 160, label = "How far from the Green they lie", order = 1 },
19 hint = { true, label = "Say what a hearth wants near it", order = 2 },
20}
21
22type Green = {
23 id: string,
24 name: string,
25 -- what its hearth wants, besides the lantern
26 block: string,
27 count: number,
28 -- what it gives back when it's lit
29 gift: string,
30 gifts: number,
31 -- what its stones are
32 stone: string,
33 -- whether a place will do, and how good it is
34 fits: (x: number, y: number, z: number) -> boolean,
35}
36
37local SEA = 19
38local HEARTH = "Clay pot" -- a cold hearth
39local FIRE = "Brazier" -- a lit one
40
41local function block(x: number, y: number, z: number): string
42 return world.block(vector.create(x, y, z))
43end
44
45-- the ground, under any tree trunk or bush standing on it
46local STANDING = { ["Oak log"] = true, ["Birch log"] = true, ["Pine log"] = true, ["Maple log"] = true, ["Willow log"] = true, ["Palm log"] = true, ["Redwood log"] = true, Bush = true }
47local function ground(x: number, z: number): number
48 local y = world.top(x, z)
49 while y > 1 and STANDING[block(x, y, z)] do
50 y -= 1
51 end
52 return y
53end
54
55local LEAVES = {
56 ["Oak leaves"] = true, ["Birch leaves"] = true, ["Pine needles"] = true, ["Maple leaves"] = true, ["Willow leaves"] = true,
57 ["Palm fronds"] = true, ["Redwood needles"] = true, ["Oak log"] = true, ["Birch log"] = true, ["Pine log"] = true,
58 ["Maple log"] = true, ["Willow log"] = true, ["Palm log"] = true, ["Redwood log"] = true,
59}
60
61local GREENS: { Green } = {
62 {
63 id = "heights",
64 name = "the Old Green on the heights",
65 block = "Stone",
66 count = 6,
67 gift = "Stone pickaxe",
68 gifts = 1,
69 stone = "Cobblestone",
70 fits = function(x, y, z)
71 local b = block(x, y, z)
72 return y >= 52 and (b == "Stone" or b == "Snow" or b == "Grass")
73 end,
74 },
75 {
76 id = "shore",
77 name = "the Old Green by the shore",
78 block = "Sand",
79 count = 6,
80 gift = "Balloon basket",
81 gifts = 1,
82 stone = "Sandstone bricks",
83 fits = function(x, y, z)
84 if block(x, y, z) ~= "Sand" or y < SEA or block(x, y + 1, z) ~= "Air" then
85 return false
86 end
87 -- the sea's near
88 for _, d in { { 9, 0 }, { -9, 0 }, { 0, 9 }, { 0, -9 } } do
89 if block(x + d[1], SEA, z + d[2]) == "Water" then
90 return true
91 end
92 end
93 return false
94 end,
95 },
96 {
97 id = "woods",
98 name = "the Old Green in the woods",
99 block = "Oak log",
100 count = 4,
101 gift = "Stone axe",
102 gifts = 1,
103 stone = "Mossy brick",
104 fits = function(x, y, z)
105 if block(x, y, z) ~= "Grass" then
106 return false
107 end
108 -- trees all around
109 local trees = 0
110 for i = 0, 7 do
111 local a = i * math.pi / 4
112 local tx, tz = math.floor(x + math.cos(a) * 7), math.floor(z + math.sin(a) * 7)
113 local g = ground(tx, tz)
114 if LEAVES[block(tx, g + 1, tz)] or LEAVES[block(tx, g + 4, tz)] or LEAVES[block(tx, g + 6, tz)] then
115 trees += 1
116 end
117 end
118 return trees >= 4
119 end,
120 },
121}
122
123local BY_ID: { [string]: Green } = {}
124for _, g in GREENS do
125 BY_ID[g.id] = g
126end
127
128-- what a hearth wants, said plainly
129local function wants(g: Green): string
130 return "1 Lantern and " .. g.count .. " " .. g.block
131end
132
133-- the same pattern of broken stones every time, for a place
134local function hash(x: number, z: number, n: number): number
135 local h = (x * 73856093 + z * 19349663 + n * 83492791) % 1000003
136 return (h * 7919 % 1000003) / 1000003
137end
138
139-- ---- finding places ----
140
141-- Each found one: its kind, its middle, which way its arch faces, whether
142-- it's built yet and whether it's lit.
143type Site = { id: string, at: vector, along_x: boolean, built: boolean, lit: boolean }
144
145local sites: { Site } = store.get("sites") or {}
146
147local function save()
148 store.set("sites", sites)
149end
150
151local NATURAL = { Grass = true, Sand = true, Stone = true, Snow = true, Dirt = true, Gravel = true }
152
153-- how level a 9 by 9 is around x, z, and whether it's all natural ground
154local function level(x: number, z: number): (number?, number)
155 local lo, hi = math.huge, -math.huge
156 for dz = -4, 4, 4 do
157 for dx = -4, 4, 4 do
158 local y = ground(x + dx, z + dz)
159 if not NATURAL[block(x + dx, y, z + dz)] then
160 return nil, 0
161 end
162 lo, hi = math.min(lo, y), math.max(hi, y)
163 end
164 end
165 return hi - lo, ground(x, z)
166end
167
168local STEP = 6
169local EDGE = 26
170local SIZE = 384
171local points: { { number } } = {}
172for z = EDGE, SIZE - EDGE, STEP do
173 for x = EDGE, SIZE - EDGE, STEP do
174 table.insert(points, { x, z })
175 end
176end
177-- the best few of each kind so far: { score, x, y, z }
178local found: { [string]: { { number } } } = {}
179for _, g in GREENS do
180 found[g.id] = {}
181end
182
183local function consider(x: number, z: number)
184 local green = world.spot("green")
185 local start = world.spot("start")
186 local away = vector.magnitude(vector.create(x - green.x, 0, z - green.z))
187 if away < knobs.far or vector.magnitude(vector.create(x - start.x, 0, z - start.z)) < 40 then
188 return
189 end
190 local spread, y = level(x, z)
191 if spread == nil then
192 return
193 end
194 for _, g in GREENS do
195 local most = if g.id == "heights" then 7 else 3
196 if spread <= most and g.fits(x, y, z) then
197 local list = found[g.id]
198 table.insert(list, { math.min(away, 160) - spread * 8, x, y, z })
199 table.sort(list, function(a, b)
200 return a[1] > b[1]
201 end)
202 if #list > 24 then
203 table.remove(list)
204 end
205 end
206 end
207end
208
209-- the best of each kind, well apart from each other
210local function choose()
211 local chosen: { Site } = {}
212 for _, g in GREENS do
213 for _, c in found[g.id] do
214 -- a step up out of the sea, by the shore
215 local at = vector.create(c[2], math.max(c[3], SEA + 1), c[4])
216 local apart = true
217 for _, s in chosen do
218 if vector.magnitude(vector.create(at.x - s.at.x, 0, at.z - s.at.z)) < 70 then
219 apart = false
220 end
221 end
222 if apart then
223 -- the arch stands across the way back to the Green
224 local green = world.spot("green")
225 local along_x = math.abs(green.z - at.z) > math.abs(green.x - at.x)
226 table.insert(chosen, { id = g.id, at = at, along_x = along_x, built = false, lit = false })
227 break
228 end
229 end
230 end
231 return chosen
232end
233
234-- ---- building them ----
235
236-- a place in a site, by how far across (u), up (h) and along to the arch (v)
237local function spot(s: Site, u: number, h: number, v: number): vector
238 if s.along_x then
239 return vector.create(s.at.x + u, s.at.y + h, s.at.z + v)
240 end
241 return vector.create(s.at.x + v, s.at.y + h, s.at.z + u)
242end
243
244local function put(s: Site, u: number, h: number, v: number, name: string)
245 world.set_block(spot(s, u, h, v), name)
246end
247
248local function hearth(s: Site): vector
249 return spot(s, 0, 1, 0)
250end
251
252-- The arch's lintel: broken on one side while it's cold, whole once lit.
253local function lintel(s: Site, g: Green)
254 for u = -2, 2 do
255 if s.lit or u < 1 then
256 put(s, u, 6, -3, g.stone)
257 end
258 end
259end
260
261-- A clearing around it: no trunk stands on the plaza, and no branches hang over it
262-- (a trunk just outside keeps its own side of the tree).
263local function clearing(s: Site, v0: number, v1: number)
264 for v = v0, v1 do
265 for u = -7, 7 do
266 local inside = math.abs(u) <= 4 and math.abs(v) <= 4
267 for h = 1, 24 do
268 local p = spot(s, u, h, v)
269 local b = world.block(p)
270 if (LEAVES[b] and not STANDING[b]) or (inside and STANDING[b]) then
271 world.set_block(p, "Air")
272 end
273 end
274 end
275 end
276end
277
278-- the plaza a few rows at a time (v0 to v1), then the arch and the hearth
279local function build(s: Site, v0: number, v1: number)
280 local g = BY_ID[s.id]
281 for v = v0, math.min(v1, 4) do
282 for u = -4, 4 do
283 local p = spot(s, u, 0, v)
284 -- level ground under it, and nothing standing in it
285 local y = ground(p.x, p.z)
286 -- (built up at its edges, so it stands like a terrace)
287 local edge = math.abs(u) == 4 or math.abs(v) == 4
288 for h = y + 1, s.at.y - 1 do
289 world.set_block(vector.create(p.x, h, p.z), if edge then g.stone else "Dirt")
290 end
291 for h = 1, 8 do
292 put(s, u, h, v, "Air")
293 end
294 -- an old floor with stones missing
295 local r = hash(p.x, p.z, 1)
296 local floor = if r < 0.6 then g.stone elseif r < 0.8 then "Cobblestone" elseif r < 0.9 then "Gravel" else block(p.x, y, p.z)
297 put(s, u, 0, v, if NATURAL[floor] or floor == g.stone or floor == "Cobblestone" then floor else "Dirt")
298 -- what's left of a low wall around it
299 if math.abs(u) == 4 or math.abs(v) == 4 then
300 local w = hash(p.x, p.z, 2)
301 if w < 0.45 then
302 put(s, u, 1, v, g.stone)
303 end
304 if w < 0.12 then
305 put(s, u, 2, v, g.stone)
306 end
307 end
308 end
309 end
310 if v1 < 5 then
311 return
312 end
313 -- the arch: two columns and a lintel, with a cold pot on each
314 for _, u in { -2, 2 } do
315 for h = 1, 4 do
316 put(s, u, h, -3, "Column")
317 end
318 put(s, u, 5, -3, g.stone)
319 end
320 lintel(s, g)
321 -- the hearth, on a step
322 put(s, 0, 0, 0, g.stone)
323 put(s, 0, 1, 0, if s.lit then FIRE else HEARTH)
324 if s.lit then
325 put(s, -2, 7, -3, "Lantern")
326 put(s, 2, 7, -3, "Lantern")
327 end
328end
329
330-- Lights one: the hearth burns, the arch is mended, lanterns on top.
331local function light(s: Site)
332 local g = BY_ID[s.id]
333 s.lit = true
334 put(s, 0, 1, 0, FIRE)
335 lintel(s, g)
336 put(s, -2, 7, -3, "Lantern")
337 put(s, 2, 7, -3, "Lantern")
338 save()
339 sound.play("relight", { at = hearth(s) })
340 local all = true
341 for _, o in sites do
342 all = all and o.lit
343 end
344 if all then
345 world.after(1.5, function()
346 world.say("Every Old Green burns again!")
347 sound.play("fanfare", { caption = "Every Old Green is lit" })
348 end)
349 end
350end
351
352-- Looks for places a hundred at a tick, so the world doesn't stutter, then
353-- clears around one a few rows at a tick and builds it the same way. Found
354-- places are kept straight away and each is marked built once it is, so a
355-- reload carries on where it was.
356local ROWS = 3
357local next_point = 1
358local searched = #sites > 0
359-- how far along clearing (from -7 to 7) and building (from -4 to 5) each is
360local cleared: { [Site]: number } = {}
361local built: { [Site]: number } = {}
362local work: number? = nil
363work = world.every(0.1, function()
364 for _, s in sites do
365 if not s.built then
366 local c = cleared[s] or -7
367 if c <= 7 then
368 clearing(s, c, c + ROWS - 1)
369 cleared[s] = c + ROWS
370 return
371 end
372 local b = built[s] or -4
373 build(s, b, b + ROWS - 1)
374 built[s] = b + ROWS
375 if b + ROWS - 1 >= 5 then
376 s.built = true
377 save()
378 end
379 return
380 end
381 end
382 if searched then
383 world.cancel(work)
384 return
385 end
386 local stop = math.min(next_point + 100, #points)
387 for i = next_point, stop do
388 consider(points[i][1], points[i][2])
389 end
390 next_point = stop + 1
391 if next_point > #points then
392 searched = true
393 sites = choose()
394 save()
395 end
396end)
397
398-- ---- using a hearth ----
399
400local function site_at(at: vector): Site?
401 for _, s in sites do
402 local h = hearth(s)
403 if math.floor(h.x) == math.floor(at.x) and math.floor(h.y) == math.floor(at.y) and math.floor(h.z) == math.floor(at.z) then
404 return s
405 end
406 end
407 return nil
408end
409
410local function tell(player, text: string)
411 ui.show(player, "old_green", text, { at = "left" })
412 world.after(6, function()
413 ui.hide(player, "old_green")
414 end)
415end
416
417world.on("block_used", function(player, at: vector, used: string)
418 if used ~= HEARTH then
419 return
420 end
421 local s = site_at(at)
422 if s == nil or s.lit then
423 return
424 end
425 local g = BY_ID[s.id]
426 if world.pockets(player) ~= nil then
427 if world.has(player, "Lantern") < 1 or world.has(player, g.block) < g.count then
428 sound.play("cold", { at = at, volume = 0.6 })
429 tell(player, "This hearth wants " .. wants(g))
430 return
431 end
432 world.take(player, "Lantern", 1)
433 world.take(player, g.block, g.count)
434 if world.give(player, g.gift, g.gifts) == 0 then
435 -- no room for what it gives back: give it all back
436 world.give(player, "Lantern", 1)
437 world.give(player, g.block, g.count)
438 tell(player, "Your hotbar is full. Make some room first")
439 return
440 end
441 tell(player, "It's lit! It gives you " .. g.gifts .. " " .. g.gift)
442 end
443 light(s)
444 -- and a good deal of XP for it (world.reward)
445 world.reward(player, 30)
446 world.say(player.name .. " lit " .. g.name .. ".")
447end)
448
449-- a word to anyone who comes near a cold one, once each time
450local near: { [number]: string? } = {}
451world.every(1, function()
452 if not knobs.hint then
453 return
454 end
455 for _, p in world.players() do
456 local close: Site? = nil
457 for _, s in sites do
458 if not s.lit and vector.magnitude(p.position - hearth(s)) < 12 then
459 close = s
460 end
461 end
462 local id = if close then close.id else nil
463 if close and near[p.id] ~= id then
464 local g = BY_ID[close.id]
465 if world.pockets(p) ~= nil then
466 tell(p, "An Old Green, gone cold. It wants " .. wants(g))
467 else
468 tell(p, "An Old Green, gone cold. Use its hearth to light it")
469 end
470 end
471 near[p.id] = id
472 end
473end)
474
475world.on("player_left", function(player)
476 near[player.id] = nil
477end)