Sign in
world

Far Ruins

by Potluck · version 1 · Checked for all ages

Six kinds of ruin wait out in the far lands, each in a land of its own: sun shrines on the Glare's salt, lamplighters' rests in the Gold, starwatches in the Gloaming, frozen cairns in the Long Night, vine shrines in the rainforest and bog huts in the swamps. Each keeps a ruin cache with what its people left: ore and ingots, starglass, food, a lamp, and a relic for the Curator.

1--!strict
2-- Far Ruins: finds out in the far lands. Walk far enough in a land and
3-- you'll come upon what its people left, built where you can see it:
4-- the Glare a sun shrine: four pillars of duskstone round a sun lamp
5-- the Gold a lamplighter's rest: a duskwood hut with a lantern lit
6-- the Gloaming a starwatch: a tower of duskstone with starglass on top
7-- the Long Night a frozen cairn: a snow brick hut under a pile of snow
8-- rainforest a vine shrine: mossy stone pillars and a roof of moss
9-- swamp a bog hut: a cypress hut on stilts
10-- Each keeps a ruin cache. Use it for what's in it (by the land: ore and
11-- ingots, starglass, food, lamps) and a relic for the Curator, once.
12-- Ruins are built a little way from a player in survival, a long way from
13-- each other, and only built over natural ground, plants and leaves.
14
15local world = require("@potluck/world@0.33")
16local tweak = require("@potluck/tweak")
17local store = require("@potluck/store")
18local ui = require("@potluck/ui@0.2")
19local sound = require("@potluck/sound@0.2")
20
21local knobs = tweak {
22 on = { true, label = "Ruins in the far lands", order = 1 },
23 apart = { 260, min = 80, max = 1000, label = "How far apart ruins are", order = 2 },
24 prizes = { 1, min = 0.5, max = 3, label = "How generous the caches are", order = 3 },
25}
26
27local CACHE = "Ruin cache"
28local MOST = 60 -- ruins a world keeps track of
29local EDGE = 29000
30
31local function v(x: number, y: number, z: number): vector
32 return vector.create(x, y, z)
33end
34
35local function flat(a: vector, b: vector): number
36 local dx, dz = a.x - b.x, a.z - b.z
37 return math.sqrt(dx * dx + dz * dz)
38end
39
40local function key(at: vector): string
41 return string.format("%d,%d,%d", math.floor(at.x), math.floor(at.y), math.floor(at.z))
42end
43
44local here: { [string]: boolean } = {}
45local function exists(name: string): boolean
46 if not here[name] and pcall(world.kind, name) then
47 here[name] = true
48 end
49 return here[name] == true
50end
51
52local shown: { [number]: number } = {}
53local function tell(player, text: string)
54 ui.show(player, "ruins", text, { at = "bottom" })
55 local mine = (shown[player.id] or 0) + 1
56 shown[player.id] = mine
57 world.after(5, function()
58 if shown[player.id] == mine then
59 shown[player.id] = nil
60 ui.hide(player, "ruins")
61 end
62 end)
63end
64
65world.on("player_left", function(p)
66 shown[p.id] = nil
67end)
68
69-- ---- what's in the caches ----
70
71type Prize = { thing: string, chance: number, least: number, most: number }
72type Land = { biome: string, name: string, xp: number, relic: string, prizes: { Prize } }
73local LANDS: { [string]: Land } = {
74 glare = {
75 biome = "glare", name = "sun shrine", xp = 10, relic = "Salt idol",
76 prizes = {
77 { thing = "Sunstone", chance = 0.7, least = 1, most = 3 },
78 { thing = "Gold ingot", chance = 0.7, least = 1, most = 3 },
79 { thing = "Iron ingot", chance = 0.6, least = 2, most = 5 },
80 { thing = "Sun lamp", chance = 0.6, least = 1, most = 2 },
81 { thing = "Glint scale", chance = 0.5, least = 1, most = 3 },
82 { thing = "Bread", chance = 0.5, least = 1, most = 3 },
83 { thing = "Crystal", chance = 0.3, least = 1, most = 2 },
84 },
85 },
86 gold = {
87 biome = "gold", name = "lamplighter's rest", xp = 8, relic = "Lamplighter's pin",
88 prizes = {
89 { thing = "Ember fruit", chance = 0.9, least = 2, most = 5 },
90 { thing = "Torch", chance = 0.8, least = 3, most = 8 },
91 { thing = "Gold ingot", chance = 0.5, least = 1, most = 3 },
92 { thing = "Sun lamp", chance = 0.5, least = 1, most = 3 },
93 { thing = "Lantern", chance = 0.5, least = 1, most = 2 },
94 { thing = "Sunstone", chance = 0.25, least = 1, most = 2 },
95 { thing = "Bread", chance = 0.6, least = 1, most = 3 },
96 },
97 },
98 gloaming = {
99 biome = "gloaming", name = "starwatch", xp = 12, relic = "Star plate",
100 prizes = {
101 { thing = "Starglass shard", chance = 0.8, least = 2, most = 5 },
102 { thing = "Crystal", chance = 0.6, least = 1, most = 3 },
103 { thing = "Moonsilver ingot", chance = 0.35, least = 1, most = 2 },
104 { thing = "Sunstone", chance = 0.4, least = 1, most = 2 },
105 { thing = "Hush silk", chance = 0.3, least = 1, most = 2 },
106 { thing = "Brass compass", chance = 0.25, least = 1, most = 1 },
107 { thing = "Evensteel ingot", chance = 0.1, least = 1, most = 1 },
108 },
109 },
110 long_night = {
111 biome = "long_night", name = "frozen cairn", xp = 14, relic = "Frozen bell",
112 prizes = {
113 { thing = "Hush silk", chance = 0.7, least = 1, most = 3 },
114 { thing = "Starglass shard", chance = 0.7, least = 2, most = 4 },
115 { thing = "Moonsilver ingot", chance = 0.4, least = 1, most = 2 },
116 { thing = "Iron ingot", chance = 0.5, least = 2, most = 4 },
117 { thing = "Torch", chance = 0.6, least = 3, most = 6 },
118 { thing = "Evensteel ingot", chance = 0.12, least = 1, most = 1 },
119 },
120 },
121 rainforest = {
122 biome = "rainforest", name = "vine shrine", xp = 9, relic = "Vine idol",
123 prizes = {
124 { thing = "Seeds", chance = 0.8, least = 3, most = 8 },
125 { thing = "Gold ingot", chance = 0.6, least = 1, most = 3 },
126 { thing = "Crystal", chance = 0.5, least = 1, most = 3 },
127 { thing = "Iron ingot", chance = 0.5, least = 2, most = 4 },
128 { thing = "Bread", chance = 0.5, least = 1, most = 3 },
129 { thing = "Moonsilver ingot", chance = 0.1, least = 1, most = 1 },
130 { thing = "Gnome music box", chance = 0.1, least = 1, most = 1 },
131 },
132 },
133 swamp = {
134 biome = "swamp", name = "bog hut", xp = 8, relic = "Bog lantern",
135 prizes = {
136 { thing = "Brown mushroom", chance = 0.8, least = 2, most = 6 },
137 { thing = "Coal", chance = 0.7, least = 2, most = 6 },
138 { thing = "Raw copper", chance = 0.5, least = 1, most = 4 },
139 { thing = "Iron ingot", chance = 0.4, least = 1, most = 3 },
140 { thing = "Bucket", chance = 0.3, least = 1, most = 1 },
141 { thing = "Potatoes", chance = 0.4, least = 1, most = 3 },
142 { thing = "Gold ingot", chance = 0.3, least = 1, most = 2 },
143 },
144 },
145}
146
147-- ---- what's kept ----
148
149-- each ruin: its land, the cache's cell, whether it's built, and who has
150-- opened it (the cache goes when it's opened, so this is only the built
151-- flag and where)
152type Ruin = { land: string, at: vector, built: boolean }
153local ruins: { Ruin } = store.get("ruins") or {}
154-- caches still shut, by place: their land
155local caches: { [string]: string } = store.get("caches") or {}
156-- who has found each land's relic, by name and land
157local relics: { [string]: boolean } = store.get("relics") or {}
158
159local function save()
160 store.set("ruins", ruins)
161 store.set("caches", caches)
162end
163
164-- ---- building them ----
165
166type Work = { at: vector, name: string, force: boolean? }
167local work: { Work } = {}
168local done = 0
169local building: Ruin? = nil
170
171-- what a ruin may be put over
172local GROUND: { [string]: boolean } = {
173 Air = true, Grass = true, Dirt = true, Sand = true, Stone = true, Gravel = true, Snow = true, Rime = true,
174 ["Salt crust"] = true, Duskstone = true, Glarestone = true, ["Dusk grass"] = true, ["Gloam grass"] = true,
175 Mud = true, ["Red sand"] = true, Vines = true, ["Hanging moss"] = true, ["Dry bush"] = true,
176}
177local function free(b: string): boolean
178 if GROUND[b] then
179 return true
180 end
181 if string.find(b, "leaves") or string.find(b, "fronds") or string.find(b, "needles") or string.find(b, "blossom") or string.find(b, " log") then
182 return true
183 end
184 return world.kind(b) == "plant"
185end
186
187local function lamp_block(): string
188 return if exists("Sun lamp") then "Sun lamp" else "Lantern"
189end
190
191-- the ground under a column: down through the canopy, trunks and plants
192local function ground(x: number, z: number): number
193 local y = world.top(x, z)
194 for _ = 1, 40 do
195 local b = world.block(v(x, y, z))
196 if b == "Air" or string.find(b, "leaves") or string.find(b, "log") or string.find(b, "fronds") or string.find(b, "needles")
197 or string.find(b, "blossom") or b == "Vines" or b == "Hanging moss" or world.kind(b) == "plant" then
198 y -= 1
199 else
200 break
201 end
202 end
203 return y
204end
205
206-- the plans: each puts blocks relative to the cache (0, 0, 0), standing on
207-- a platform at y = -1
208local function plan(r: Ruin)
209 work, done = {}, 0
210 local c = r.at
211 local function put(dx: number, dy: number, dz: number, name: string, force: boolean?)
212 table.insert(work, { at = c + v(dx, dy, dz), name = name, force = force })
213 end
214 local function box(x0: number, y0: number, z0: number, x1: number, y1: number, z1: number, name: string)
215 -- a doorway's air is forced: it clears the plan's own walls too
216 for x = x0, x1 do
217 for y = y0, y1 do
218 for z = z0, z1 do
219 put(x, y, z, name, name == "Air")
220 end
221 end
222 end
223 end
224 -- a square of floor, with its ground under it and air over it
225 local function platform(half: number, floor: string, air: number)
226 for x = -half, half do
227 for z = -half, half do
228 local under = ground(c.x + x, c.z + z)
229 for y = math.max(under + 1, c.y - 8), c.y - 2 do
230 put(x, y - c.y, z, floor)
231 end
232 put(x, -1, z, floor)
233 for y = 0, air do
234 put(x, y, z, "Air")
235 end
236 end
237 end
238 end
239 local land = r.land
240 if land == "glare" then
241 platform(3, "Duskstone bricks", 6)
242 for _, s in { -3, 3 } do
243 for _, t in { -3, 3 } do
244 box(s, 0, t, s, 3, t, "Duskstone")
245 put(s, 4, t, lamp_block())
246 end
247 end
248 box(-1, 0, -1, 1, 0, 1, "Salt crust")
249 put(0, 0, 0, CACHE)
250 put(0, 1, 0, lamp_block())
251 elseif land == "gold" then
252 platform(3, "Duskwood planks", 5)
253 for _, s in { -3, 3 } do
254 for _, t in { -3, 3 } do
255 box(s, 0, t, s, 3, t, "Duskwood log")
256 end
257 end
258 for x = -2, 2 do
259 for y = 0, 2 do
260 put(x, y, -3, "Duskwood planks")
261 put(x, y, 3, "Duskwood planks")
262 put(-3, y, x, "Duskwood planks")
263 put(3, y, x, "Duskwood planks")
264 end
265 end
266 box(0, 0, 3, 0, 1, 3, "Air") -- the doorway
267 box(-3, 3, -3, 3, 3, 3, "Duskwood planks")
268 put(0, 0, 0, CACHE)
269 put(-2, 0, -2, "Lantern")
270 put(2, 0, 4, lamp_block())
271 elseif land == "gloaming" then
272 platform(3, "Duskstone bricks", 12)
273 for x = -2, 2 do
274 for z = -2, 2 do
275 if math.abs(x) == 2 or math.abs(z) == 2 then
276 box(x, 0, z, x, 8, z, "Duskstone bricks")
277 end
278 end
279 end
280 box(0, 0, 2, 0, 1, 2, "Air") -- the doorway
281 box(-2, 9, -2, 2, 9, 2, "Duskstone bricks")
282 for _, s in { -2, 2 } do
283 for _, t in { -2, 2 } do
284 put(s, 10, t, "Starglass")
285 end
286 end
287 put(0, 10, 0, lamp_block())
288 put(0, 0, 0, CACHE)
289 elseif land == "long_night" then
290 platform(3, "Snow bricks", 5)
291 for x = -1, 1 do
292 for z = -1, 1 do
293 if x ~= 0 or z ~= 0 then
294 box(x, 0, z, x, 1, z, "Snow bricks")
295 end
296 put(x, 2, z, "Snow bricks")
297 end
298 end
299 box(0, 0, 1, 0, 1, 1, "Air") -- the way in
300 for x = -2, 2 do
301 for z = -2, 2 do
302 if (math.abs(x) == 2 or math.abs(z) == 2) and not (x == 0 and z == 2) then
303 put(x, 0, z, "Snow")
304 end
305 end
306 end
307 put(0, 0, 0, CACHE)
308 put(1, 1, 2, "Lantern")
309 put(0, 3, 0, "Starglass")
310 elseif land == "rainforest" then
311 platform(3, "Mossy cobblestone", 6)
312 for _, s in { -3, 3 } do
313 for _, t in { -3, 3 } do
314 box(s, 0, t, s, 3, t, "Mossy cobblestone")
315 end
316 end
317 box(-3, 4, -3, 3, 4, 3, "Mossy cobblestone")
318 for x = -3, 3, 2 do
319 put(x, 3, -3 + 6 * ((x + 3) // 2 % 2), "Hanging moss")
320 end
321 for x = -2, 2 do
322 for z = -2, 2 do
323 if x ~= 0 or z ~= 0 then
324 put(x, 0, z, "Mossy brick")
325 end
326 end
327 end
328 put(0, 0, 0, CACHE)
329 put(2, 1, 2, "Orchid")
330 else
331 -- a bog hut on stilts
332 platform(3, "Cypress planks", 5)
333 for _, s in { -3, 3 } do
334 for _, t in { -3, 3 } do
335 box(s, -4, t, s, 2, t, "Cypress log")
336 end
337 end
338 for x = -2, 2 do
339 for y = 0, 2 do
340 put(x, y, -3, "Cypress planks")
341 put(x, y, 3, "Cypress planks")
342 put(-3, y, x, "Cypress planks")
343 put(3, y, x, "Cypress planks")
344 end
345 end
346 box(0, 0, 3, 0, 1, 3, "Air")
347 box(-3, 3, -3, 3, 3, 3, "Cypress planks")
348 put(0, 0, 0, CACHE)
349 put(2, 0, -2, "Lantern")
350 put(-2, 0, 2, "Brown mushroom")
351 end
352end
353
354-- puts down a few hundred blocks a tick
355local function step()
356 local last = math.min(done + 250, #work)
357 for k = done + 1, last do
358 local w = work[k]
359 local b = world.block(w.at)
360 -- a plan's air only clears; its blocks go over ground and plants
361 if b ~= w.name and (w.force or free(b) or b == "Water" and w.name ~= "Air") then
362 if w.name == CACHE then
363 caches[key(w.at)] = building and building.land or "glare"
364 end
365 world.set_block(w.at, w.name)
366 end
367 end
368 done = last
369 if done >= #work then
370 work, done = {}, 0
371 local r = building
372 building = nil
373 if r then
374 r.built = true
375 save()
376 end
377 end
378end
379
380-- ---- finding places ----
381
382local function far_from_others(at: vector, apart: number): boolean
383 for _, r in ruins do
384 if flat(r.at, at) < apart then
385 return false
386 end
387 end
388 return true
389end
390
391-- level dry ground near a player, in the same land, or nil
392local function site_near(p, land: string): vector?
393 local a = world.random() * math.pi * 2
394 local d = world.random(22, 42)
395 local x, z = math.floor(p.position.x + math.cos(a) * d), math.floor(p.position.z + math.sin(a) * d)
396 if math.abs(x) > EDGE or math.abs(z) > EDGE then
397 return nil
398 end
399 local lo, hi = math.huge, -math.huge
400 -- the whole floor and its lamps must be dry; a swamp's hut stands on stilts
401 local reach, gap = 4, 2
402 if land == "swamp" then
403 reach, gap = 3, 3
404 end
405 for dx = -reach, reach, gap do
406 for dz = -reach, reach, gap do
407 local top = ground(x + dx, z + dz)
408 local b = world.block(v(x + dx, top, z + dz))
409 if b == "Water" or b == "Ice" or (dx == 0 and dz == 0 and world.biome(v(x, 0, z)) ~= land) then
410 return nil
411 end
412 lo, hi = math.min(lo, top), math.max(hi, top)
413 end
414 end
415 if hi - lo > 3 or hi < 10 then
416 return nil
417 end
418 return v(x, hi + 1, z)
419end
420
421local function look(p)
422 if #ruins >= MOST or world.pockets(p) == nil then
423 return
424 end
425 local land = world.biome(p.position)
426 if not LANDS[land] then
427 return
428 end
429 if not far_from_others(p.position, knobs.apart) then
430 return
431 end
432 local at = site_near(p, land)
433 if at and far_from_others(at, knobs.apart) then
434 local r: Ruin = { land = land, at = at, built = false }
435 table.insert(ruins, r)
436 building = r
437 plan(r)
438 save()
439 end
440end
441
442-- ---- the caches ----
443
444type Find = { thing: string, n: number }
445local left: { [string]: { Find } } = {}
446
447local function roll(land: string): { Find }
448 local out: { Find } = {}
449 for _, p in LANDS[land].prizes do
450 if exists(p.thing) and world.random() < math.min(1, p.chance * knobs.prizes) then
451 local n = math.floor(world.random(p.least, p.most + 0.999) * knobs.prizes)
452 table.insert(out, { thing = p.thing, n = math.clamp(n, 1, 64) })
453 end
454 end
455 if #out == 0 then
456 table.insert(out, { thing = "Coal", n = 3 })
457 end
458 return out
459end
460
461-- as many as fit on a line (60 letters), then "and more"
462local function found(finds: { Find }): string
463 local parts = {}
464 for _, f in finds do
465 table.insert(parts, f.n .. " " .. string.lower(f.thing))
466 end
467 for n = #parts, 1, -1 do
468 local line = if n < #parts
469 then table.concat(parts, ", ", 1, n) .. " and more"
470 elseif n == 1 then parts[1]
471 else table.concat(parts, ", ", 1, n - 1) .. " and " .. parts[n]
472 line = "You found " .. line .. "."
473 if #line <= 60 or n == 1 then
474 return line
475 end
476 end
477 return "You found something."
478end
479
480local function open(player, at: vector)
481 local k = key(at)
482 local land = caches[k]
483 if not land or not LANDS[land] then
484 tell(player, "It's empty.")
485 return
486 end
487 if world.pockets(player) == nil then
488 tell(player, "Ruin caches hold prizes for survival.")
489 return
490 end
491 local finds = left[k]
492 if not finds then
493 finds = roll(land)
494 local relic = LANDS[land].relic
495 if not relics[player.name .. "/" .. land] and exists(relic) then
496 table.insert(finds, { thing = relic, n = 1 })
497 end
498 end
499 local still: { Find } = {}
500 local got: { Find } = {}
501 for _, f in finds do
502 local n = world.give(player, f.thing, f.n)
503 if n > 0 then
504 table.insert(got, { thing = f.thing, n = n })
505 if f.thing == LANDS[land].relic then
506 relics[player.name .. "/" .. land] = true
507 end
508 end
509 if n < f.n then
510 table.insert(still, { thing = f.thing, n = f.n - n })
511 end
512 end
513 if #still > 0 then
514 left[k] = still
515 tell(player, "Your pockets are full.")
516 return
517 end
518 left[k] = nil
519 caches[k] = nil
520 save()
521 store.set("relics", relics)
522 world.reward(player, LANDS[land].xp)
523 world.set_block(at, "Air")
524 sound.play("sparkle", { at = at, caption = "A ruin cache opens" })
525 tell(player, found(got))
526end
527
528world.on("block_used", function(player, at: vector, block: string)
529 if block == CACHE then
530 open(player, at)
531 end
532end)
533
534-- a cache broken open gives nothing
535world.drops(CACHE, { gives = false })
536world.on("block_broken", function(_player, at: vector, block: string)
537 if block == CACHE and caches[key(at)] then
538 caches[key(at)] = nil
539 left[key(at)] = nil
540 save()
541 end
542end)
543
544-- ---- watching players ----
545
546local turn = 0
547world.every(0.1, function()
548 if #work > 0 then
549 step()
550 return
551 end
552 turn += 1
553 if turn % 20 ~= 0 or not knobs.on then
554 return
555 end
556 local players = world.players()
557 if #players == 0 then
558 return
559 end
560 look(players[(turn // 20) % #players + 1])
561end)
562
563-- a ruin left half built (the world stopped mid-way) is built on
564for _, r in ruins do
565 if not r.built then
566 building = r
567 plan(r)
568 break
569 end
570end