Where do Gloomlings go by day? Deep in the rock, wander far enough underground and you may find a hollow where they sleep, curled up on gloom moss around their nests of shiny things. Bring a light and they fade away; creep in without one and they wake and nip. Their nests hold gold, crystal, old coins and, now and then, a gloom pearl for the Curator.
| 1 | --!strict |
| 2 | -- Gloom Hollow: where Gloomlings go by day. Deep in the rock, when a player |
| 3 | -- wanders far enough underground, a round hollow opens in the stone near |
| 4 | -- them with a low passage to where they are: a floor of gloom moss that |
| 5 | -- shines a little in the dark, a few glowcaps, nests of shiny things, and |
| 6 | -- gloomlings curled up asleep on the moss. |
| 7 | -- |
| 8 | -- Bring a light (a lantern, a torch or a candle in hand, or one put down |
| 9 | -- near) and the sleepers fade away. Creep in without one and they wake |
| 10 | -- and nip, once each, before they run off into the dark; so does opening a |
| 11 | -- nest while they're asleep. A sleeper bopped pops for gloom dust. Nests |
| 12 | -- hold what gloomlings hoard (PRIZES below): gold, crystal, glass, old |
| 13 | -- coins and now and then a gloom pearl, a relic (an item of this |
| 14 | -- creation's, world 0.26) that the Curator on the Green collects. |
| 15 | -- |
| 16 | -- Where each hollow is, how many still sleep in it and its nests are kept |
| 17 | -- with @potluck/store, so each is dug once. Sleepers are only there while |
| 18 | -- someone is near. Nobody wakes on a Peaceful world, and creative players |
| 19 | -- can look around in peace. Only natural stone is dug. |
| 20 | |
| 21 | local world = require("@potluck/world@0.33") |
| 22 | local tweak = require("@potluck/tweak") |
| 23 | local store = require("@potluck/store") |
| 24 | local ui = require("@potluck/ui@0.2") |
| 25 | local sound = require("@potluck/sound@0.2") |
| 26 | |
| 27 | local knobs = tweak { |
| 28 | color = { "#221d36", label = "Gloomling color", order = 1 }, |
| 29 | apart = { 160, min = 60, max = 800, label = "How far apart hollows are", order = 2 }, |
| 30 | sleepers = { 4, min = 0, max = 8, label = "Gloomlings asleep in each", order = 3 }, |
| 31 | prizes = { 1, min = 0.5, max = 3, label = "How much is in the nests", order = 4 }, |
| 32 | nip = { 1, min = 0, max = 4, label = "How hard a woken one nips", order = 5 }, |
| 33 | } |
| 34 | |
| 35 | local MOSS, NEST = "Gloom moss", "Gloom nest" |
| 36 | local MOST = 48 -- hollows a world keeps track of |
| 37 | local RX, RY, FLOOR = 6, 3, -2 -- the hollow's half width, its top, and its floor, from its middle |
| 38 | local DEEP = 8 -- how much rock is over a player before a hollow opens near them |
| 39 | local WAKE = 3 -- a sleeper wakes when someone in the dark comes this close |
| 40 | local SEE = 7 -- and fades when a light comes this close |
| 41 | local GONE = 80 -- and goes, to come back later, with nobody this close |
| 42 | |
| 43 | -- what may be dug |
| 44 | local NATURAL: { [string]: boolean } = { |
| 45 | Stone = true, Dirt = true, Gravel = true, Sand = true, Slate = true, Sandstone = true, Cobblestone = true, |
| 46 | ["Coal ore"] = true, ["Copper ore"] = true, ["Iron ore"] = true, ["Gold ore"] = true, ["Crystal ore"] = true, ["Moonsilver ore"] = true, |
| 47 | Glowcap = true, |
| 48 | } |
| 49 | -- what in hand sends sleepers off |
| 50 | local LIGHTS: { [string]: boolean } = { Lantern = true, Torch = true, Candle = true, ["Paper lantern"] = true } |
| 51 | |
| 52 | -- How much a woken one nips, by the world's danger: nothing on Peaceful. |
| 53 | local DANGER: { [string]: number } = { peaceful = 0, gentle = 0.5, normal = 1, hard = 1.5 } |
| 54 | |
| 55 | -- ---- what's in the nests ---- |
| 56 | |
| 57 | type Prize = { thing: string, chance: number, least: number, most: number } |
| 58 | local PRIZES: { Prize } = { |
| 59 | { thing = "Gold ingot", chance = 0.6, least = 1, most = 2 }, |
| 60 | { thing = "Raw gold", chance = 0.5, least = 1, most = 3 }, |
| 61 | { thing = "Crystal", chance = 0.5, least = 1, most = 3 }, |
| 62 | { thing = "Glass", chance = 0.4, least = 2, most = 5 }, |
| 63 | { thing = "Copper ingot", chance = 0.4, least = 1, most = 3 }, |
| 64 | { thing = "Gloom dust", chance = 0.5, least = 1, most = 3 }, |
| 65 | { thing = "Old coin", chance = 0.35, least = 1, most = 2 }, |
| 66 | { thing = "Gloom pearl", chance = 0.3, least = 1, most = 1 }, |
| 67 | { thing = "Glimmer cocoon", chance = 0.2, least = 1, most = 1 }, |
| 68 | } |
| 69 | local NEST_XP = 6 |
| 70 | |
| 71 | -- ---- what's kept ---- |
| 72 | |
| 73 | -- each hollow: its middle, whether it's dug, how many still sleep there, |
| 74 | -- and while it's being dug, the way in |
| 75 | type Hollow = { at: vector, built: boolean, sleeping: number, way: { vector }? } |
| 76 | local hollows: { Hollow } = store.get("hollows") or {} |
| 77 | -- nests still holding something, by place: which hollow |
| 78 | local nests: { [string]: number } = store.get("nests") or {} |
| 79 | |
| 80 | local function save() |
| 81 | store.set("hollows", hollows) |
| 82 | store.set("nests", nests) |
| 83 | end |
| 84 | |
| 85 | local function key(at: vector): string |
| 86 | return string.format("%d,%d,%d", math.floor(at.x), math.floor(at.y), math.floor(at.z)) |
| 87 | end |
| 88 | |
| 89 | -- Whether a thing is in this world: old coins are the Curator's. |
| 90 | local here: { [string]: boolean } = {} |
| 91 | local function exists(name: string): boolean |
| 92 | if here[name] == nil then |
| 93 | here[name] = (pcall(world.kind, name)) |
| 94 | end |
| 95 | return here[name] == true |
| 96 | end |
| 97 | |
| 98 | -- the same number every time, for a place |
| 99 | local function hash(x: number, y: number, z: number): number |
| 100 | local h = (math.floor(x) * 73856093 + math.floor(y) * 19349663 + math.floor(z) * 83492791) % 1000003 |
| 101 | return (h * 7919 % 1000003) / 1000003 |
| 102 | end |
| 103 | |
| 104 | local shown: { [number]: number } = {} |
| 105 | local function tell(player, text: string) |
| 106 | ui.show(player, "hollow", text, { at = "bottom" }) |
| 107 | local mine = (shown[player.id] or 0) + 1 |
| 108 | shown[player.id] = mine |
| 109 | world.after(4, function() |
| 110 | if shown[player.id] == mine then |
| 111 | shown[player.id] = nil |
| 112 | ui.hide(player, "hollow") |
| 113 | end |
| 114 | end) |
| 115 | end |
| 116 | |
| 117 | local function flat(a: vector, b: vector): number |
| 118 | local dx, dz = a.x - b.x, a.z - b.z |
| 119 | return math.sqrt(dx * dx + dz * dz) |
| 120 | end |
| 121 | |
| 122 | -- the hollow a place is in (or nearest, within reach) |
| 123 | local function hollow_at(pos: vector, reach: number): number? |
| 124 | local best, best_d = nil, reach |
| 125 | for i, h in hollows do |
| 126 | local d = vector.magnitude(pos - h.at) |
| 127 | if d < best_d then |
| 128 | best, best_d = i, d |
| 129 | end |
| 130 | end |
| 131 | return best |
| 132 | end |
| 133 | |
| 134 | -- ---- finding a place ---- |
| 135 | |
| 136 | -- inside the hollow, by how far from its middle |
| 137 | local function inside(x: number, y: number, z: number): boolean |
| 138 | if y < FLOOR or y > RY then |
| 139 | return false |
| 140 | end |
| 141 | local e = (x / RX) ^ 2 + (y / (RY + 0.5)) ^ 2 + (z / RX) ^ 2 |
| 142 | return e <= 1 - hash(x * 3, y, z * 5) * 0.2 |
| 143 | end |
| 144 | |
| 145 | -- Whether a hollow fits around `c`: deep in natural rock (a cave through it |
| 146 | -- is fine), with nothing built and no water. |
| 147 | local function fits(c: vector): boolean |
| 148 | if world.top(c.x, c.z) < c.y + RY + DEEP then |
| 149 | return false |
| 150 | end |
| 151 | local air, n = 0, 0 |
| 152 | for _, dx in { -RX, -3, 0, 3, RX } do |
| 153 | for _, dz in { -RX, -3, 0, 3, RX } do |
| 154 | for _, dy in { FLOOR - 1, 0, RY } do |
| 155 | if dy == FLOOR - 1 or inside(dx, dy, dz) or (dx == 0 and dz == 0) then |
| 156 | local b = world.block(c + vector.create(dx, dy, dz)) |
| 157 | n += 1 |
| 158 | if b == "Air" then |
| 159 | if dy == FLOOR - 1 then |
| 160 | return false -- a floor to stand on |
| 161 | end |
| 162 | air += 1 |
| 163 | elseif not NATURAL[b] then |
| 164 | return false |
| 165 | end |
| 166 | end |
| 167 | end |
| 168 | end |
| 169 | end |
| 170 | return air * 3 <= n |
| 171 | end |
| 172 | |
| 173 | -- The way from a hollow's middle out to where a player is: the cells to |
| 174 | -- dig, up to the first open air outside it, or nil if it's built or wet |
| 175 | -- in between, or too far. |
| 176 | local function passage(c: vector, to: vector): { vector }? |
| 177 | local d = to - c |
| 178 | local len = vector.magnitude(d) |
| 179 | if len < 1 then |
| 180 | return nil |
| 181 | end |
| 182 | local step = d / len |
| 183 | local out = {} |
| 184 | for t = 0, 40 do |
| 185 | local at = c + step * t |
| 186 | local cell = vector.create(math.floor(at.x), math.floor(c.y + FLOOR + (to.y - (c.y + FLOOR)) * math.clamp((t - RX) / math.max(len - RX, 1), 0, 1)), math.floor(at.z)) |
| 187 | local rel = cell - c |
| 188 | if not inside(rel.x, rel.y, rel.z) then |
| 189 | local b, above = world.block(cell), world.block(cell + vector.create(0, 1, 0)) |
| 190 | if b == "Air" and above == "Air" then |
| 191 | return out |
| 192 | end |
| 193 | if (b ~= "Air" and not NATURAL[b]) or (above ~= "Air" and not NATURAL[above]) then |
| 194 | return nil |
| 195 | end |
| 196 | table.insert(out, cell) |
| 197 | end |
| 198 | end |
| 199 | return nil |
| 200 | end |
| 201 | |
| 202 | -- A place for a hollow near a player deep underground, and the way to it. |
| 203 | local function look_near(p): (vector?, { vector }?) |
| 204 | for _ = 1, 8 do |
| 205 | local a = world.random(0, 2 * math.pi) |
| 206 | local r = world.random(14, 22) |
| 207 | local c = vector.create( |
| 208 | math.floor(p.position.x + math.cos(a) * r), |
| 209 | math.floor(p.position.y + world.random(-3, 1)), |
| 210 | math.floor(p.position.z + math.sin(a) * r) |
| 211 | ) |
| 212 | if fits(c) then |
| 213 | local way = passage(c, p.position) |
| 214 | if way then |
| 215 | return c, way |
| 216 | end |
| 217 | end |
| 218 | end |
| 219 | return nil, nil |
| 220 | end |
| 221 | |
| 222 | local function far_from_others(pos: vector, apart: number): boolean |
| 223 | for _, h in hollows do |
| 224 | if flat(h.at, pos) < apart then |
| 225 | return false |
| 226 | end |
| 227 | end |
| 228 | return true |
| 229 | end |
| 230 | |
| 231 | -- ---- digging it ---- |
| 232 | |
| 233 | -- what's left to put down: a place and a block ("" to dig) |
| 234 | type Work = { at: vector, name: string } |
| 235 | local work: { Work } = {} |
| 236 | local done = 0 |
| 237 | local digging: Hollow? = nil |
| 238 | |
| 239 | -- the floor spots of a hollow, the same each time: where nests, glowcaps |
| 240 | -- and sleepers go |
| 241 | local function floor_spots(c: vector, from: number, to: number, n: number, seed: number): { vector } |
| 242 | local out = {} |
| 243 | for i = 1, n do |
| 244 | local a = hash(c.x + i * 7, seed, c.z) * 2 * math.pi |
| 245 | local r = from + hash(c.x, seed + i, c.z - i) * (to - from) |
| 246 | table.insert(out, vector.create(math.floor(c.x + math.cos(a) * r), c.y + FLOOR, math.floor(c.z + math.sin(a) * r))) |
| 247 | end |
| 248 | return out |
| 249 | end |
| 250 | |
| 251 | local function plan(h: Hollow) |
| 252 | local c = h.at |
| 253 | local way = h.way or {} |
| 254 | work, done = {}, 0 |
| 255 | for x = -RX, RX do |
| 256 | for z = -RX, RX do |
| 257 | for y = FLOOR, RY do |
| 258 | if inside(x, y, z) then |
| 259 | table.insert(work, { at = c + vector.create(x, y, z), name = "" }) |
| 260 | end |
| 261 | end |
| 262 | if inside(x, FLOOR, z) then |
| 263 | table.insert(work, { at = c + vector.create(x, FLOOR - 1, z), name = MOSS }) |
| 264 | end |
| 265 | end |
| 266 | end |
| 267 | for i, cell in way do |
| 268 | table.insert(work, { at = cell, name = "" }) |
| 269 | table.insert(work, { at = cell + vector.create(0, 1, 0), name = "" }) |
| 270 | if i <= 4 then |
| 271 | table.insert(work, { at = cell - vector.create(0, 1, 0), name = MOSS }) |
| 272 | end |
| 273 | end |
| 274 | for _, at in floor_spots(c, 1.5, 4, 3, 1) do |
| 275 | table.insert(work, { at = at, name = NEST }) |
| 276 | end |
| 277 | for _, at in floor_spots(c, 3, 4, 3, 2) do |
| 278 | table.insert(work, { at = at, name = "Glowcap" }) |
| 279 | end |
| 280 | end |
| 281 | |
| 282 | -- puts down a few hundred blocks a tick |
| 283 | local function step() |
| 284 | local last = math.min(done + 250, #work) |
| 285 | for i = done + 1, last do |
| 286 | local w = work[i] |
| 287 | local b = world.block(w.at) |
| 288 | if w.name == "" then |
| 289 | if NATURAL[b] then |
| 290 | world.set_block(w.at, "Air") |
| 291 | end |
| 292 | elseif w.name == NEST or w.name == "Glowcap" then |
| 293 | -- on the floor, not in the wall |
| 294 | if b == "Air" and world.block(w.at - vector.create(0, 1, 0)) == MOSS then |
| 295 | world.set_block(w.at, w.name) |
| 296 | if w.name == NEST then |
| 297 | nests[key(w.at)] = hollow_at(w.at, RX + 2) or 0 |
| 298 | end |
| 299 | end |
| 300 | elseif NATURAL[b] or b == "Air" then |
| 301 | world.set_block(w.at, w.name) |
| 302 | end |
| 303 | end |
| 304 | done = last |
| 305 | if done >= #work then |
| 306 | work, done = {}, 0 |
| 307 | local h = digging |
| 308 | if h then |
| 309 | h.built = true |
| 310 | h.sleeping = math.floor(knobs.sleepers) |
| 311 | h.way = nil |
| 312 | digging = nil |
| 313 | save() |
| 314 | end |
| 315 | end |
| 316 | end |
| 317 | |
| 318 | -- ---- the sleepers ---- |
| 319 | |
| 320 | local function sleeper_body(eyes: string, open: boolean) |
| 321 | return { |
| 322 | { "body", size = { 0.66, 0.34, 0.6 }, at = { 0, 0.17, 0 }, color = knobs.color }, |
| 323 | { "tuft", size = { 0.3, 0.12, 0.3 }, at = { 0, 0.38, 0.1 }, color = knobs.color, head = true }, |
| 324 | { "eye_left", size = { 0.13, if open then 0.1 else 0.03, 0.04 }, at = { -0.14, 0.24, -0.31 }, color = eyes, glow = open, head = true }, |
| 325 | { "eye_right", size = { 0.13, if open then 0.1 else 0.03, 0.04 }, at = { 0.14, 0.24, -0.31 }, color = eyes, glow = open, head = true }, |
| 326 | } |
| 327 | end |
| 328 | |
| 329 | local Sleeper = world.creature { |
| 330 | name = "Sleeping gloomling", |
| 331 | speed = 0.5, |
| 332 | count = 0, |
| 333 | home = "here", |
| 334 | size = { 0.66, 0.4, 0.62 }, |
| 335 | body = sleeper_body("#5a4c2a", false), |
| 336 | } |
| 337 | |
| 338 | local Woken = world.creature { |
| 339 | name = "Woken gloomling", |
| 340 | speed = 2.8, |
| 341 | count = 0, |
| 342 | home = "here", |
| 343 | size = { 0.66, 0.5, 0.62 }, |
| 344 | body = { |
| 345 | { "body", size = { 0.62, 0.5, 0.62 }, at = { 0, 0.32, 0 }, color = knobs.color }, |
| 346 | { "tuft", size = { 0.3, 0.14, 0.3 }, at = { 0, 0.63, 0.04 }, color = knobs.color, head = true }, |
| 347 | { "eye_left", size = { 0.13, 0.1, 0.04 }, at = { -0.14, 0.42, -0.32 }, color = "#ffe27a", glow = true, head = true }, |
| 348 | { "eye_right", size = { 0.13, 0.1, 0.04 }, at = { 0.14, 0.42, -0.32 }, color = "#ffe27a", glow = true, head = true }, |
| 349 | { "foot_left", size = { 0.16, 0.1, 0.22 }, at = { -0.16, 0.05, 0 }, color = knobs.color, leg = true }, |
| 350 | { "foot_right", size = { 0.16, 0.1, 0.22 }, at = { 0.16, 0.05, 0 }, color = knobs.color, leg = true }, |
| 351 | }, |
| 352 | } |
| 353 | |
| 354 | -- each sleeper's hollow, by creature (each one says, every tick) |
| 355 | local home_of: { [number]: number } = {} |
| 356 | -- when each hollow was last woken by a nest being opened |
| 357 | local alarm: { [number]: number } = {} |
| 358 | -- who's been told about each hollow |
| 359 | local told: { [string]: boolean } = {} |
| 360 | |
| 361 | -- one fewer sleeping in its hollow |
| 362 | local function left(g) |
| 363 | local i = g.hollow |
| 364 | home_of[g.id] = nil |
| 365 | if i and hollows[i] then |
| 366 | hollows[i].sleeping = math.max(0, hollows[i].sleeping - 1) |
| 367 | save() |
| 368 | end |
| 369 | end |
| 370 | |
| 371 | -- a light near it, or in a near player's hand |
| 372 | local function lit(pos: vector): boolean |
| 373 | if world.find_light(pos, 5) then |
| 374 | return true |
| 375 | end |
| 376 | for _, p in world.players() do |
| 377 | local held = world.held(p) |
| 378 | if held and LIGHTS[held] and vector.magnitude(p.position - pos) < SEE then |
| 379 | return true |
| 380 | end |
| 381 | end |
| 382 | return false |
| 383 | end |
| 384 | |
| 385 | local function fade(g, player) |
| 386 | sound.play("whoosh", { at = g, volume = 0.3, pitch = 0.6, caption = "A gloomling fades in the light" }) |
| 387 | if player then |
| 388 | tell(player, "The gloomlings fade away in the light.") |
| 389 | end |
| 390 | left(g) |
| 391 | g:remove() |
| 392 | end |
| 393 | |
| 394 | function Sleeper:spawned() |
| 395 | self.hollow = hollow_at(self.position, RX + 3) |
| 396 | self.since = world.time() |
| 397 | self.look_in = world.random(0, 0.5) |
| 398 | end |
| 399 | |
| 400 | function Sleeper:tick(dt: number) |
| 401 | if self.hollow then |
| 402 | home_of[self.id] = self.hollow |
| 403 | end |
| 404 | self.look_in -= dt |
| 405 | if self.look_in > 0 then |
| 406 | return |
| 407 | end |
| 408 | self.look_in = 0.4 |
| 409 | local p = world.nearest_player(self.position, GONE) |
| 410 | if not p then |
| 411 | -- nobody near: it goes, and comes back when someone does |
| 412 | home_of[self.id] = nil |
| 413 | self:remove() |
| 414 | return |
| 415 | end |
| 416 | local d = self:distance_to(p.position) |
| 417 | if d < SEE and lit(self.position) then |
| 418 | fade(self, p) |
| 419 | return |
| 420 | end |
| 421 | local woke = self.hollow and (alarm[self.hollow] or -1) > self.since |
| 422 | if (woke or d < WAKE) and world.pockets(p) ~= nil and DANGER[world.danger()] ~= 0 then |
| 423 | self.prey = p.id |
| 424 | self.nip_in = 0.5 |
| 425 | self.away_in = 8 |
| 426 | self:become(Woken) |
| 427 | sound.play("grumble", { at = self, volume = 0.7, pitch = 1.8, caption = "A gloomling wakes" }) |
| 428 | tell(p, "A gloomling wakes! A light would send them off.") |
| 429 | end |
| 430 | end |
| 431 | |
| 432 | function Woken:tick(dt: number) |
| 433 | if self.hollow then |
| 434 | home_of[self.id] = self.hollow |
| 435 | end |
| 436 | self.away_in = (self.away_in or 8) - dt |
| 437 | if lit(self.position) then |
| 438 | fade(self, nil) |
| 439 | return |
| 440 | end |
| 441 | if self.away_in <= 0 then |
| 442 | -- off into the dark, for good |
| 443 | left(self) |
| 444 | self:remove() |
| 445 | return |
| 446 | end |
| 447 | local p = nil |
| 448 | for _, q in world.players() do |
| 449 | if q.id == self.prey then |
| 450 | p = q |
| 451 | end |
| 452 | end |
| 453 | if not p or self.nipped then |
| 454 | if p then |
| 455 | local d = self.position - p.position |
| 456 | self:walk_to(self.position + d * (4 / math.max(vector.magnitude(d), 0.5)), 3) |
| 457 | end |
| 458 | return |
| 459 | end |
| 460 | if self:distance_to(p.position) < 1.6 then |
| 461 | self:face(p.position) |
| 462 | self.nip_in = (self.nip_in or 0) - dt |
| 463 | if self.nip_in <= 0 then |
| 464 | self.nipped = true |
| 465 | self.away_in = math.min(self.away_in, 3) |
| 466 | self:hop(0.5) |
| 467 | local hurt = knobs.nip * (DANGER[world.danger()] or 0.5) |
| 468 | if hurt > 0 and world.hurt(p, hurt) then |
| 469 | sound.play("grumble", { at = self, volume = 0.7, pitch = 1.9, caption = "A gloomling nips" }) |
| 470 | end |
| 471 | end |
| 472 | else |
| 473 | self:walk_to(p.position, 2.8) |
| 474 | end |
| 475 | end |
| 476 | |
| 477 | local function popped(self, player) |
| 478 | sound.play("pop", { at = self, pitch = 0.7, caption = "A gloomling pops" }) |
| 479 | if exists("Gloom dust") then |
| 480 | world.give(player, "Gloom dust", 1) |
| 481 | end |
| 482 | world.reward(player, 2) |
| 483 | left(self) |
| 484 | self:remove() |
| 485 | end |
| 486 | |
| 487 | Sleeper.hit = popped |
| 488 | Woken.hit = popped |
| 489 | |
| 490 | -- ---- the nests ---- |
| 491 | |
| 492 | type Find = { thing: string, n: number } |
| 493 | -- what's still in nests players' pockets were too full for, by place |
| 494 | local leftover: { [string]: { Find } } = {} |
| 495 | |
| 496 | local function roll(): { Find } |
| 497 | local out: { Find } = {} |
| 498 | for _, p in PRIZES do |
| 499 | if exists(p.thing) and world.random() < math.min(1, p.chance * knobs.prizes) then |
| 500 | local n = math.floor(world.random(p.least, p.most + 0.999) * knobs.prizes) |
| 501 | table.insert(out, { thing = p.thing, n = math.clamp(n, 1, 64) }) |
| 502 | end |
| 503 | end |
| 504 | if #out == 0 then |
| 505 | table.insert(out, { thing = "Glass", n = 2 }) |
| 506 | end |
| 507 | return out |
| 508 | end |
| 509 | |
| 510 | -- "You found 2 raw gold, 1 crystal and 3 glass.": as many as fit on a line |
| 511 | -- (60 letters), then "and more" |
| 512 | local function found(finds: { Find }): string |
| 513 | local parts = {} |
| 514 | for _, f in finds do |
| 515 | table.insert(parts, f.n .. " " .. string.lower(f.thing)) |
| 516 | end |
| 517 | for n = #parts, 1, -1 do |
| 518 | local line = if n < #parts |
| 519 | then table.concat(parts, ", ", 1, n) .. " and more" |
| 520 | elseif n == 1 then parts[1] |
| 521 | else table.concat(parts, ", ", 1, n - 1) .. " and " .. parts[n] |
| 522 | line = "You found " .. line .. "." |
| 523 | if #line <= 60 or n == 1 then |
| 524 | return line |
| 525 | end |
| 526 | end |
| 527 | return "You found something." |
| 528 | end |
| 529 | |
| 530 | -- Gives what's in a nest; true when it's all given. |
| 531 | local function rummage(player, at: vector): boolean |
| 532 | local k = key(at) |
| 533 | local finds = leftover[k] or roll() |
| 534 | local got, still = {}, {} |
| 535 | for _, f in finds do |
| 536 | local n = world.give(player, f.thing, f.n) |
| 537 | if n > 0 then |
| 538 | table.insert(got, { thing = f.thing, n = n }) |
| 539 | end |
| 540 | if n < f.n then |
| 541 | table.insert(still, { thing = f.thing, n = f.n - n }) |
| 542 | end |
| 543 | end |
| 544 | -- the noise wakes whoever's asleep in its hollow |
| 545 | local i = nests[k] |
| 546 | if i and i > 0 then |
| 547 | alarm[i] = world.time() |
| 548 | end |
| 549 | if #still > 0 then |
| 550 | leftover[k] = still |
| 551 | tell(player, "Your pockets are full.") |
| 552 | return false |
| 553 | end |
| 554 | leftover[k] = nil |
| 555 | nests[k] = nil |
| 556 | save() |
| 557 | world.reward(player, NEST_XP) |
| 558 | sound.play("sparkle", { at = at, caption = "Shiny things in a nest" }) |
| 559 | tell(player, found(got)) |
| 560 | return true |
| 561 | end |
| 562 | |
| 563 | -- breaking a nest gives nothing of itself: what's in it comes out instead |
| 564 | world.drops(NEST, { gives = false }) |
| 565 | |
| 566 | world.on("block_used", function(player, at: vector, block: string) |
| 567 | if block ~= NEST then |
| 568 | return |
| 569 | end |
| 570 | if nests[key(at)] == nil then |
| 571 | tell(player, "It's an empty nest of twigs.") |
| 572 | return |
| 573 | end |
| 574 | if world.pockets(player) == nil then |
| 575 | tell(player, "Gloom nests hold prizes for survival.") |
| 576 | return |
| 577 | end |
| 578 | if rummage(player, at) then |
| 579 | world.set_block(at, "Air") |
| 580 | end |
| 581 | end) |
| 582 | |
| 583 | world.on("block_broken", function(player, at: vector, block: string) |
| 584 | if block == NEST and nests[key(at)] ~= nil then |
| 585 | if world.pockets(player) ~= nil then |
| 586 | rummage(player, at) |
| 587 | end |
| 588 | nests[key(at)] = nil |
| 589 | leftover[key(at)] = nil |
| 590 | save() |
| 591 | end |
| 592 | end) |
| 593 | |
| 594 | -- ---- watching players ---- |
| 595 | |
| 596 | local turn = 0 |
| 597 | world.every(0.1, function() |
| 598 | if #work > 0 then |
| 599 | step() |
| 600 | return |
| 601 | end |
| 602 | turn += 1 |
| 603 | if turn % 20 ~= 0 then |
| 604 | return |
| 605 | end |
| 606 | local players = world.players() |
| 607 | if #players == 0 or #hollows >= MOST then |
| 608 | return |
| 609 | end |
| 610 | local p = players[(turn // 20) % #players + 1] |
| 611 | local pos = p.position |
| 612 | -- the Long Dusk has finds of its own |
| 613 | if world.place(pos) == "dusk" then |
| 614 | return |
| 615 | end |
| 616 | -- only deep underground, well away from the Green |
| 617 | if world.top(pos.x, pos.z) < pos.y + DEEP or pos.y > 12 then |
| 618 | return |
| 619 | end |
| 620 | for _, name in { "green", "start" } do |
| 621 | local at = world.spot(name) |
| 622 | if at and flat(pos, at) < 90 then |
| 623 | return |
| 624 | end |
| 625 | end |
| 626 | if not far_from_others(pos, knobs.apart) then |
| 627 | return |
| 628 | end |
| 629 | local c, way = look_near(p) |
| 630 | if c and way then |
| 631 | local h: Hollow = { at = c, built = false, sleeping = 0, way = way } |
| 632 | table.insert(hollows, h) |
| 633 | digging = h |
| 634 | plan(h) |
| 635 | save() |
| 636 | end |
| 637 | end) |
| 638 | |
| 639 | -- Every few seconds: sleepers back in the hollows somebody's near, and a |
| 640 | -- word to whoever comes on one. |
| 641 | world.every(3, function() |
| 642 | local live: { [number]: number } = {} |
| 643 | local alive = {} |
| 644 | for _, kind in { Sleeper, Woken } do |
| 645 | for _, id in world.creatures(kind) do |
| 646 | alive[id] = true |
| 647 | end |
| 648 | end |
| 649 | for id, i in home_of do |
| 650 | if alive[id] then |
| 651 | live[i] = (live[i] or 0) + 1 |
| 652 | else |
| 653 | home_of[id] = nil |
| 654 | end |
| 655 | end |
| 656 | for i, h in hollows do |
| 657 | if h.built then |
| 658 | local near = nil |
| 659 | for _, p in world.players() do |
| 660 | if vector.magnitude(p.position - h.at) < 48 then |
| 661 | near = p |
| 662 | end |
| 663 | if vector.magnitude(p.position - h.at) < RX + 4 and not told[p.name .. "/" .. i] then |
| 664 | told[p.name .. "/" .. i] = true |
| 665 | tell(p, if h.sleeping > 0 then "Gloomlings sleep here, by day. A light would send them off." else "A gloom hollow, empty now.") |
| 666 | end |
| 667 | end |
| 668 | if near and h.sleeping > 0 and (live[i] or 0) == 0 then |
| 669 | -- on the moss, never in the rock (where a spawn would go up |
| 670 | -- to the grass) |
| 671 | local put = 0 |
| 672 | for _, at in floor_spots(h.at, 1.5, 3.5, h.sleeping * 2, 3) do |
| 673 | local b = world.block(at) |
| 674 | if put < h.sleeping and (b == "Air" or b == NEST) and world.block(at - vector.create(0, 1, 0)) == MOSS then |
| 675 | if pcall(world.spawn, Sleeper, at + vector.create(0.5, 0.05, 0.5)) then |
| 676 | put += 1 |
| 677 | end |
| 678 | end |
| 679 | end |
| 680 | if put < h.sleeping then |
| 681 | h.sleeping = put |
| 682 | save() |
| 683 | end |
| 684 | live[i] = put |
| 685 | end |
| 686 | end |
| 687 | end |
| 688 | end) |
| 689 | |
| 690 | -- a hollow left half dug (the world stopped mid-way) is dug on |
| 691 | for _, h in hollows do |
| 692 | if not h.built then |
| 693 | digging = h |
| 694 | plan(h) |
| 695 | break |
| 696 | end |
| 697 | end |
| 698 | |
| 699 | world.on("player_left", function(p) |
| 700 | shown[p.id] = nil |
| 701 | end) |