Geodes lie in the rock beside the caves: crack one's gray shell open and it's lined with crystal that shines in the dark, with crystal buds to break for crystal and, now and then, glimmer seeds that grow into flowers that glow. Far deeper, whole grottos of crystal wait, and somewhere in them a singing crystal for the Curator.
| 1 | --!strict |
| 2 | -- Crystal Grottos: geodes beside the caves, and grottos of crystal deep |
| 3 | -- down. |
| 4 | -- |
| 5 | -- When a player is underground, now and then a geode forms in the rock |
| 6 | -- beside a cave near them, its shell of slate just showing in the cave wall. |
| 7 | -- Break in and it's hollow, lined with grotto crystal that shines a little |
| 8 | -- in the dark, with crystal buds on its floor. A bud breaks into crystal |
| 9 | -- (with an iron pickaxe, as crystal ore does), and sometimes glimmer seeds |
| 10 | -- or, rarely, a singing crystal: a relic (world 0.26) that the Curator on |
| 11 | -- the Green collects. |
| 12 | -- |
| 13 | -- Far deeper (below DEEP_GROTTO), a whole grotto opens near a player, with |
| 14 | -- a low passage to where they stand: a great round cave walled with grotto |
| 15 | -- crystal and its floor thick with buds. |
| 16 | -- |
| 17 | -- Glimmer seeds grow where they're planted, on grass, dirt or farmland, |
| 18 | -- into a little green shoot and then a glimmer bloom: a pale flower that |
| 19 | -- glows like a lantern. Break a shoot or a bloom and it gives a seed back. |
| 20 | -- The shell is plain slate and the shoot a plain fern, known by where they |
| 21 | -- are, so the base game's blocks leave room for players' own. |
| 22 | -- |
| 23 | -- Geodes and grottos are kept with @potluck/store, so each forms once, and |
| 24 | -- only natural rock is used. |
| 25 | |
| 26 | local world = require("@potluck/world@0.33") |
| 27 | local tweak = require("@potluck/tweak") |
| 28 | local store = require("@potluck/store") |
| 29 | local ui = require("@potluck/ui@0.2") |
| 30 | local sound = require("@potluck/sound@0.2") |
| 31 | |
| 32 | local knobs = tweak { |
| 33 | geodes = { 64, min = 16, max = 300, label = "How far apart geodes are", order = 1 }, |
| 34 | grottos = { 260, min = 80, max = 1000, label = "How far apart grottos are", order = 2 }, |
| 35 | seeds = { 0.15, min = 0, max = 1, label = "Chance a bud has glimmer seeds", order = 3 }, |
| 36 | grow = { 4, min = 0.5, max = 30, label = "Minutes a glimmer bloom takes to grow", order = 4 }, |
| 37 | } |
| 38 | |
| 39 | local SHELL, CRYSTAL, BUD = "Slate", "Grotto crystal", "Crystal bud" |
| 40 | local SPROUT, BLOOM, SEEDS, RELIC = "Fern", "Glimmer bloom", "Glimmer seeds", "Singing crystal" |
| 41 | local MOST_GEODES, MOST_GROTTOS = 200, 32 |
| 42 | local DEEP_GROTTO = -40 -- grottos only open below here |
| 43 | local RELIC_CHANCE = 0.03 -- a singing crystal in a bud |
| 44 | local COCOON, COCOON_CHANCE = "Glimmer cocoon", 0.04 -- the Glimmerflies', in a bud |
| 45 | |
| 46 | -- what may be used |
| 47 | local NATURAL: { [string]: boolean } = { |
| 48 | Stone = true, Dirt = true, Gravel = true, Sand = true, Slate = true, Sandstone = true, |
| 49 | ["Coal ore"] = true, ["Copper ore"] = true, ["Iron ore"] = true, ["Gold ore"] = true, ["Crystal ore"] = true, ["Moonsilver ore"] = true, |
| 50 | } |
| 51 | -- what glimmer seeds grow on |
| 52 | local SOIL: { [string]: boolean } = { Grass = true, Dirt = true, Farmland = true } |
| 53 | |
| 54 | -- ---- what's kept ---- |
| 55 | |
| 56 | -- each geode's middle; each grotto's middle, whether it's dug, and the way in |
| 57 | type Grotto = { at: vector, built: boolean, way: { vector }? } |
| 58 | local geodes: { vector } = store.get("geodes") or {} |
| 59 | local grottos: { Grotto } = store.get("grottos") or {} |
| 60 | -- what glimmer seeds were planted, by place: seconds they've grown |
| 61 | local sprouts: { [string]: number } = store.get("sprouts") or {} |
| 62 | |
| 63 | local function save() |
| 64 | store.set("geodes", geodes) |
| 65 | store.set("grottos", grottos) |
| 66 | store.set("sprouts", sprouts) |
| 67 | end |
| 68 | |
| 69 | local function key(at: vector): string |
| 70 | return string.format("%d,%d,%d", math.floor(at.x), math.floor(at.y), math.floor(at.z)) |
| 71 | end |
| 72 | |
| 73 | local function from_key(k: string): vector |
| 74 | local x, y, z = string.match(k, "(-?%d+),(-?%d+),(-?%d+)") |
| 75 | return vector.create(tonumber(x) :: number, tonumber(y) :: number, tonumber(z) :: number) |
| 76 | end |
| 77 | |
| 78 | -- the same number every time, for a place |
| 79 | local function hash(x: number, y: number, z: number): number |
| 80 | local h = (math.floor(x) * 73856093 + math.floor(y) * 19349663 + math.floor(z) * 83492791) % 1000003 |
| 81 | return (h * 7919 % 1000003) / 1000003 |
| 82 | end |
| 83 | |
| 84 | local shown: { [number]: number } = {} |
| 85 | local function tell(player, text: string) |
| 86 | ui.show(player, "crystal", text, { at = "bottom" }) |
| 87 | local mine = (shown[player.id] or 0) + 1 |
| 88 | shown[player.id] = mine |
| 89 | world.after(4, function() |
| 90 | if shown[player.id] == mine then |
| 91 | shown[player.id] = nil |
| 92 | ui.hide(player, "crystal") |
| 93 | end |
| 94 | end) |
| 95 | end |
| 96 | |
| 97 | local function near_any(list: { any }, pos: vector, apart: number): boolean |
| 98 | for _, g in list do |
| 99 | local at = if type(g) == "vector" then g else g.at |
| 100 | if vector.magnitude(at - pos) < apart then |
| 101 | return true |
| 102 | end |
| 103 | end |
| 104 | return false |
| 105 | end |
| 106 | |
| 107 | -- ---- building, a few hundred blocks a tick ---- |
| 108 | |
| 109 | type Work = { at: vector, name: string } |
| 110 | local work: { Work } = {} |
| 111 | local done = 0 |
| 112 | local digging: Grotto? = nil |
| 113 | -- Plans are fed in a few hundred cells at a time, so that working one out |
| 114 | -- never takes more than a slice of a tick: it adds to the list it's given |
| 115 | -- and says whether there's more to come. |
| 116 | local feeder: ((out: { Work }) -> boolean)? = nil |
| 117 | local FEED = 200 |
| 118 | |
| 119 | local function step() |
| 120 | if done >= #work and feeder then |
| 121 | work, done = {}, 0 |
| 122 | if not feeder(work) then |
| 123 | feeder = nil |
| 124 | end |
| 125 | end |
| 126 | local last = math.min(done + 250, #work) |
| 127 | for i = done + 1, last do |
| 128 | local w = work[i] |
| 129 | local b = world.block(w.at) |
| 130 | if w.name == "" then |
| 131 | if NATURAL[b] then |
| 132 | world.set_block(w.at, "Air") |
| 133 | end |
| 134 | elseif w.name == BUD then |
| 135 | -- on the floor, never in the wall |
| 136 | if b == "Air" and world.block(w.at - vector.create(0, 1, 0)) == CRYSTAL then |
| 137 | world.set_block(w.at, BUD) |
| 138 | end |
| 139 | elseif NATURAL[b] then |
| 140 | world.set_block(w.at, w.name) |
| 141 | end |
| 142 | end |
| 143 | done = last |
| 144 | if done >= #work and not feeder then |
| 145 | work, done = {}, 0 |
| 146 | local g = digging |
| 147 | if g then |
| 148 | g.built = true |
| 149 | g.way = nil |
| 150 | digging = nil |
| 151 | save() |
| 152 | end |
| 153 | end |
| 154 | end |
| 155 | |
| 156 | -- ---- geodes ---- |
| 157 | |
| 158 | local SHELL_R, LINING_R, HOLLOW_R = 3.3, 2.5, 1.6 |
| 159 | |
| 160 | -- A place for a geode near a player: a cave's air cell, and solid rock |
| 161 | -- beyond it in one of six ways, so the shell shows in the cave wall. |
| 162 | local DIRS = { |
| 163 | vector.create(1, 0, 0), vector.create(-1, 0, 0), vector.create(0, 0, 1), |
| 164 | vector.create(0, 0, -1), vector.create(0, 1, 0), vector.create(0, -1, 0), |
| 165 | } |
| 166 | local function geode_near(p): vector? |
| 167 | for _ = 1, 10 do |
| 168 | local at = vector.create( |
| 169 | math.floor(p.position.x + world.random(-12, 12)), |
| 170 | math.floor(p.position.y + world.random(-4, 4)), |
| 171 | math.floor(p.position.z + world.random(-12, 12)) |
| 172 | ) |
| 173 | if world.block(at) == "Air" and world.top(at.x, at.z) > at.y + 6 then |
| 174 | local d = DIRS[math.floor(world.random(1, 6.999))] |
| 175 | local c = at + d * 4 |
| 176 | local ok = true |
| 177 | for _, e in DIRS do |
| 178 | for _, r in { 1, 3 } do |
| 179 | local b = world.block(c + e * r) |
| 180 | if not NATURAL[b] then |
| 181 | ok = false |
| 182 | end |
| 183 | end |
| 184 | end |
| 185 | if ok and NATURAL[world.block(c)] then |
| 186 | return c |
| 187 | end |
| 188 | end |
| 189 | end |
| 190 | return nil |
| 191 | end |
| 192 | |
| 193 | local function plan_geode(c: vector) |
| 194 | local x = -5 |
| 195 | feeder = function(out: { Work }): boolean |
| 196 | x += 1 |
| 197 | for y = -4, 4 do |
| 198 | for z = -4, 4 do |
| 199 | local r = math.sqrt(x * x + y * y + z * z) + hash(c.x + x, c.y + y, c.z + z) * 0.3 |
| 200 | local at = c + vector.create(x, y, z) |
| 201 | if r <= HOLLOW_R then |
| 202 | table.insert(out, { at = at, name = "" }) |
| 203 | elseif r <= LINING_R then |
| 204 | table.insert(out, { at = at, name = CRYSTAL }) |
| 205 | elseif r <= SHELL_R then |
| 206 | table.insert(out, { at = at, name = SHELL }) |
| 207 | end |
| 208 | end |
| 209 | end |
| 210 | if x < 4 then |
| 211 | return true |
| 212 | end |
| 213 | -- buds on the lining's floor |
| 214 | for _, d in { vector.create(0, -1, 0), vector.create(1, -1, 0), vector.create(-1, -1, 0), vector.create(0, -1, 1), vector.create(0, -1, -1) } do |
| 215 | table.insert(out, { at = c + d, name = BUD }) |
| 216 | end |
| 217 | return false |
| 218 | end |
| 219 | end |
| 220 | |
| 221 | -- ---- grottos ---- |
| 222 | |
| 223 | local RX, RY, FLOOR = 8, 5, -3 |
| 224 | |
| 225 | local function inside(x: number, y: number, z: number): boolean |
| 226 | if y < FLOOR or y > RY then |
| 227 | return false |
| 228 | end |
| 229 | local e = (x / RX) ^ 2 + (y / (RY + 0.5)) ^ 2 + (z / RX) ^ 2 |
| 230 | return e <= 1 - hash(x * 5, y, z * 3) * 0.15 |
| 231 | end |
| 232 | |
| 233 | local function fits(c: vector): boolean |
| 234 | if world.top(c.x, c.z) < c.y + RY + 10 then |
| 235 | return false |
| 236 | end |
| 237 | local air, n = 0, 0 |
| 238 | for _, dx in { -RX, -4, 0, 4, RX } do |
| 239 | for _, dz in { -RX, -4, 0, 4, RX } do |
| 240 | for _, dy in { FLOOR - 1, 0, RY } do |
| 241 | if dy == FLOOR - 1 or inside(dx, dy, dz) or (dx == 0 and dz == 0) then |
| 242 | local b = world.block(c + vector.create(dx, dy, dz)) |
| 243 | n += 1 |
| 244 | if b == "Air" then |
| 245 | if dy == FLOOR - 1 then |
| 246 | return false |
| 247 | end |
| 248 | air += 1 |
| 249 | elseif not NATURAL[b] then |
| 250 | return false |
| 251 | end |
| 252 | end |
| 253 | end |
| 254 | end |
| 255 | end |
| 256 | return air * 3 <= n |
| 257 | end |
| 258 | |
| 259 | -- the cells to dig from a grotto's middle to the open air where a player |
| 260 | -- is, or nil |
| 261 | local function passage(c: vector, to: vector): { vector }? |
| 262 | local d = to - c |
| 263 | local len = vector.magnitude(d) |
| 264 | if len < 1 then |
| 265 | return nil |
| 266 | end |
| 267 | local step = d / len |
| 268 | local out = {} |
| 269 | for t = 0, 44 do |
| 270 | local at = c + step * t |
| 271 | local y = c.y + FLOOR + (math.floor(to.y) - (c.y + FLOOR)) * math.clamp((t - RX) / math.max(len - RX, 1), 0, 1) |
| 272 | local cell = vector.create(math.floor(at.x), math.floor(y), math.floor(at.z)) |
| 273 | local rel = cell - c |
| 274 | if not inside(rel.x, rel.y, rel.z) then |
| 275 | local b, above = world.block(cell), world.block(cell + vector.create(0, 1, 0)) |
| 276 | if b == "Air" and above == "Air" then |
| 277 | return out |
| 278 | end |
| 279 | if (b ~= "Air" and not NATURAL[b]) or (above ~= "Air" and not NATURAL[above]) then |
| 280 | return nil |
| 281 | end |
| 282 | table.insert(out, cell) |
| 283 | end |
| 284 | end |
| 285 | return nil |
| 286 | end |
| 287 | |
| 288 | -- A grotto's shape around its middle is the same for every grotto: the |
| 289 | -- hollow, and the wall of crystal around it. It's worked out once, when the |
| 290 | -- creation starts, since working it out cell by cell for each grotto was |
| 291 | -- more than one tick's work on a slow machine. |
| 292 | local HOLLOW: { vector } = {} |
| 293 | local WALL: { vector } = {} |
| 294 | do |
| 295 | local function k(x: number, y: number, z: number): number |
| 296 | return (x + 16) * 1024 + (y + 16) * 32 + (z + 16) |
| 297 | end |
| 298 | local is: { [number]: boolean } = {} |
| 299 | for x = -RX, RX do |
| 300 | for z = -RX, RX do |
| 301 | for y = FLOOR, RY do |
| 302 | if inside(x, y, z) then |
| 303 | is[k(x, y, z)] = true |
| 304 | table.insert(HOLLOW, vector.create(x, y, z)) |
| 305 | end |
| 306 | end |
| 307 | end |
| 308 | end |
| 309 | for x = -RX - 1, RX + 1 do |
| 310 | for z = -RX - 1, RX + 1 do |
| 311 | for y = FLOOR - 1, RY + 1 do |
| 312 | if not is[k(x, y, z)] and (is[k(x + 1, y, z)] or is[k(x - 1, y, z)] or is[k(x, y + 1, z)] or is[k(x, y - 1, z)] or is[k(x, y, z + 1)] or is[k(x, y, z - 1)]) then |
| 313 | table.insert(WALL, vector.create(x, y, z)) |
| 314 | end |
| 315 | end |
| 316 | end |
| 317 | end |
| 318 | end |
| 319 | |
| 320 | local function plan_grotto(g: Grotto) |
| 321 | local c = g.at |
| 322 | local way = g.way or {} |
| 323 | -- the walls first, then the hollow inside them, then the way in, then the buds |
| 324 | local phase, i = 1, 0 |
| 325 | feeder = function(out: { Work }): boolean |
| 326 | for _ = 1, FEED do |
| 327 | i += 1 |
| 328 | if phase == 1 then |
| 329 | local d = WALL[i] |
| 330 | if d then |
| 331 | table.insert(out, { at = c + d, name = CRYSTAL }) |
| 332 | else |
| 333 | phase, i = 2, 0 |
| 334 | end |
| 335 | elseif phase == 2 then |
| 336 | local d = HOLLOW[i] |
| 337 | if d then |
| 338 | table.insert(out, { at = c + d, name = "" }) |
| 339 | else |
| 340 | phase, i = 3, 0 |
| 341 | end |
| 342 | elseif phase == 3 then |
| 343 | local cell = way[i] |
| 344 | if cell then |
| 345 | table.insert(out, { at = cell, name = "" }) |
| 346 | table.insert(out, { at = cell + vector.create(0, 1, 0), name = "" }) |
| 347 | else |
| 348 | phase, i = 4, 0 |
| 349 | end |
| 350 | else |
| 351 | local d = HOLLOW[i] |
| 352 | if not d then |
| 353 | return false |
| 354 | end |
| 355 | if d.y == FLOOR and hash(c.x + d.x, 7, c.z + d.z) < 0.22 then |
| 356 | table.insert(out, { at = c + d, name = BUD }) |
| 357 | end |
| 358 | end |
| 359 | end |
| 360 | return true |
| 361 | end |
| 362 | end |
| 363 | |
| 364 | local function grotto_near(p): (vector?, { vector }?) |
| 365 | -- one try a time: each is dozens of looks at the rock |
| 366 | for _ = 1, 1 do |
| 367 | local a = world.random(0, 2 * math.pi) |
| 368 | local r = world.random(16, 24) |
| 369 | local c = vector.create( |
| 370 | math.floor(p.position.x + math.cos(a) * r), |
| 371 | math.floor(p.position.y + world.random(-3, 2)), |
| 372 | math.floor(p.position.z + math.sin(a) * r) |
| 373 | ) |
| 374 | if fits(c) then |
| 375 | local way = passage(c, p.position) |
| 376 | if way then |
| 377 | return c, way |
| 378 | end |
| 379 | end |
| 380 | end |
| 381 | return nil, nil |
| 382 | end |
| 383 | |
| 384 | -- ---- watching players ---- |
| 385 | |
| 386 | local turn = 0 |
| 387 | world.every(0.1, function() |
| 388 | if #work > 0 or feeder then |
| 389 | step() |
| 390 | return |
| 391 | end |
| 392 | turn += 1 |
| 393 | if turn % 15 ~= 0 then |
| 394 | return |
| 395 | end |
| 396 | local players = world.players() |
| 397 | if #players == 0 then |
| 398 | return |
| 399 | end |
| 400 | local p = players[(turn // 15) % #players + 1] |
| 401 | local pos = p.position |
| 402 | -- the Long Dusk has finds of its own |
| 403 | if world.place(pos) == "dusk" then |
| 404 | return |
| 405 | end |
| 406 | if world.top(pos.x, pos.z) < pos.y + 8 or pos.y > 10 then |
| 407 | return |
| 408 | end |
| 409 | local green = world.spot("green") |
| 410 | if green and vector.magnitude(vector.create(pos.x - green.x, 0, pos.z - green.z)) < 60 then |
| 411 | return |
| 412 | end |
| 413 | -- deep down, a grotto now and then |
| 414 | if pos.y < DEEP_GROTTO and #grottos < MOST_GROTTOS and not near_any(grottos, pos, knobs.grottos) then |
| 415 | local c, way = grotto_near(p) |
| 416 | if c and way then |
| 417 | local g: Grotto = { at = c, built = false, way = way } |
| 418 | table.insert(grottos, g) |
| 419 | digging = g |
| 420 | plan_grotto(g) |
| 421 | save() |
| 422 | end |
| 423 | return |
| 424 | end |
| 425 | -- and geodes beside caves |
| 426 | if #geodes < MOST_GEODES and not near_any(geodes, pos, knobs.geodes) then |
| 427 | local c = geode_near(p) |
| 428 | if c and not near_any(geodes, c, knobs.geodes) then |
| 429 | table.insert(geodes, c) |
| 430 | work, done = {}, 0 |
| 431 | plan_geode(c) |
| 432 | save() |
| 433 | end |
| 434 | end |
| 435 | end) |
| 436 | |
| 437 | -- a grotto left half dug is dug on |
| 438 | for _, g in grottos do |
| 439 | if not g.built then |
| 440 | digging = g |
| 441 | plan_grotto(g) |
| 442 | break |
| 443 | end |
| 444 | end |
| 445 | |
| 446 | -- ---- buds and blooms ---- |
| 447 | |
| 448 | -- a bud breaks into crystal with an iron pickaxe or better, as crystal ore |
| 449 | -- does; now and then seeds or a singing crystal come with it |
| 450 | world.drops(BUD, { gives = "Crystal", count = { 1, 2 }, level = 4 }) |
| 451 | |
| 452 | world.on("block_broken", function(player, at: vector, block: string) |
| 453 | -- a fern is only a shoot where glimmer seeds were planted |
| 454 | local shoot = block == SPROUT and sprouts[key(at)] ~= nil |
| 455 | if (shoot or block == BLOOM) and sprouts[key(at)] then |
| 456 | sprouts[key(at)] = nil |
| 457 | save() |
| 458 | end |
| 459 | if world.pockets(player) == nil then |
| 460 | return |
| 461 | end |
| 462 | if shoot then |
| 463 | world.give(player, SEEDS, 1) |
| 464 | elseif block == BUD then |
| 465 | if world.random() < knobs.seeds then |
| 466 | world.give(player, SEEDS, math.floor(world.random(1, 2.999))) |
| 467 | tell(player, "There were glimmer seeds in the crystal.") |
| 468 | end |
| 469 | if (pcall(world.kind, COCOON)) and world.random() < COCOON_CHANCE and world.give(player, COCOON, 1) > 0 then |
| 470 | tell(player, "A glimmer cocoon was in the crystal.") |
| 471 | end |
| 472 | if world.random() < RELIC_CHANCE and world.give(player, RELIC, 1) > 0 then |
| 473 | sound.play("chime", { at = at, caption = "A crystal hums" }) |
| 474 | tell(player, "A singing crystal! Hazel on the Green would love to see it.") |
| 475 | end |
| 476 | world.reward(player, 1) |
| 477 | elseif block == BLOOM then |
| 478 | world.give(player, SEEDS, 1) |
| 479 | end |
| 480 | end) |
| 481 | |
| 482 | -- planting glimmer seeds: use them on grass, dirt or farmland |
| 483 | world.on("block_used", function(player, at: vector, block: string) |
| 484 | if world.held(player) ~= SEEDS or not SOIL[block] then |
| 485 | return |
| 486 | end |
| 487 | local above = at + vector.create(0, 1, 0) |
| 488 | if world.block(above) ~= "Air" then |
| 489 | return |
| 490 | end |
| 491 | if world.pockets(player) ~= nil and not world.take(player, SEEDS, 1) then |
| 492 | return |
| 493 | end |
| 494 | world.set_block(above, SPROUT) |
| 495 | sprouts[key(above)] = 0 |
| 496 | save() |
| 497 | sound.play("pop", { at = above, volume = 0.4, caption = "Seeds planted" }) |
| 498 | end) |
| 499 | |
| 500 | -- sprouts grow into blooms |
| 501 | local GROW_EVERY = 5 |
| 502 | world.every(GROW_EVERY, function() |
| 503 | local changed = false |
| 504 | for k, grown in sprouts do |
| 505 | local at = from_key(k) |
| 506 | local b = world.block(at) |
| 507 | if b ~= SPROUT then |
| 508 | -- dug up, or the land isn't here just now: a bloom's done |
| 509 | if b == BLOOM then |
| 510 | sprouts[k] = nil |
| 511 | changed = true |
| 512 | end |
| 513 | else |
| 514 | grown += GROW_EVERY |
| 515 | if grown >= knobs.grow * 60 then |
| 516 | world.set_block(at, BLOOM) |
| 517 | sprouts[k] = nil |
| 518 | sound.play("sparkle", { at = at, volume = 0.4, caption = "A glimmer bloom opens" }) |
| 519 | else |
| 520 | sprouts[k] = grown |
| 521 | end |
| 522 | changed = true |
| 523 | end |
| 524 | end |
| 525 | if changed then |
| 526 | save() |
| 527 | end |
| 528 | end) |
| 529 | |
| 530 | world.on("player_left", function(p) |
| 531 | shown[p.id] = nil |
| 532 | end) |