Sign in
creature

The Gloam

by Potluck · version 1 · Checked for all ages

The Gloam, a great beast of the dark that drinks the light, sleeps in a hollow under the moonsilver vaults: open a vault's moon seal and a stair behind its coffer goes down to it. Make a Moon bell at a workbench from moonsilver and what the night's creatures leave, ring it at the altar and the Gloam wakes. It slams, then snuffs the lamps and sends its shades (light them again to reach it), then rages. Beat it for its heart, a dawn cloak and a dawn stone on the Green. Never on Peaceful.

1--!strict
2-- The Gloam: a great beast of the dark that drinks the light, bound long
3-- ago by the old stone people in a hollow under their moonsilver vaults.
4--
5-- When a player stands in a vault's sanctum (the Moonsilver Vaults
6-- creation's, behind its moon seal), the wall behind the coffer gives way
7-- to a stair down to the Gloam's hollow: a round cave floored with
8-- gloamstone, eight pillars round it each with a moon lamp, and the Gloam
9-- altar in the middle. In a world without the vaults a hollow opens on its
10-- own near a player far down instead.
11--
12-- The Gloam sleeps at the hollow's north end. Ring a Moon bell (made at a
13-- workbench from moonsilver and what the night's creatures leave) at the
14-- altar and it wakes, and fights in three parts:
15-- 1. It slams: it swipes at whoever's close and now and then rears up
16-- and slams down (get back when it rears).
17-- 2. At 62% it drinks the light: the lamps go out, shades come for you
18-- and shards of night fly. While fewer than half the lamps are lit it
19-- takes only a quarter of every blow, so light them again (use them).
20-- 3. At 32% it rages: quicker, harder, slamming more, and at 15% it
21-- drinks the light once more.
22-- Beat it and everyone who fought gets its heart (a relic for the
23-- Curator), moonsilver, XP, and the first time each, a dawn cloak; the
24-- first time in a world a dawn stone goes up on the Green, with the names
25-- of whoever beat it. The bell stays in the altar until the Gloam is
26-- beaten, so a fight that goes badly can be tried again. After a minute it
27-- gathers in the dark again, for another bell.
28--
29-- How tough it is and how hard it hits go by the world's danger (and grow
30-- with every player who fights it). It never wakes on a Peaceful world,
31-- and fights only players in survival.
32
33local world = require("@potluck/world@0.33")
34local tweak = require("@potluck/tweak")
35local store = require("@potluck/store")
36local ui = require("@potluck/ui@0.2")
37local sound = require("@potluck/sound@0.2")
38
39local knobs = tweak {
40 on = { true, label = "The Gloam sleeps under the vaults", order = 1 },
41 health = { 240, min = 40, max = 900, label = "How much it takes to beat (Normal, one player)", order = 2 },
42 hits = { 4, min = 0, max = 12, label = "How hard it swipes", order = 3 },
43 shades = { 3, min = 0, max = 8, label = "Shades it sends", order = 4 },
44 apart = { 300, min = 100, max = 1200, label = "How far apart hollows are without vaults", order = 5 },
45 eyes = { "#e4d8ff", label = "Eye color", order = 6 },
46 hide = { "#1c1826", label = "Hide color", order = 7 },
47}
48
49-- How tough it is, how hard it hits and how many shades, by the danger.
50local DANGER: { [string]: { health: number, hurt: number, shades: number, speed: number } } = {
51 peaceful = { health = 0, hurt = 0, shades = 0, speed = 0 },
52 gentle = { health = 0.6, hurt = 0.5, shades = 0.67, speed = 0.85 },
53 normal = { health = 1, hurt = 1, shades = 1, speed = 1 },
54 hard = { health = 1.4, hurt = 1.5, shades = 1.34, speed = 1.2 },
55}
56local function danger()
57 return DANGER[world.danger()] or DANGER.gentle
58end
59
60local SWORDS: { [string]: number } = {
61 ["Wooden sword"] = 2,
62 ["Stone sword"] = 3,
63 ["Copper sword"] = 3,
64 ["Iron sword"] = 4,
65 ["Moonsilver sword"] = 5,
66}
67
68-- What's in hand bops this hard: a sword, another creation's weapon by its
69-- power, or a hand.
70local function bops(held: string?): number
71 if not held then
72 return 1
73 end
74 if SWORDS[held] then
75 return SWORDS[held]
76 end
77 local it = world.item(held)
78 return if it and it.kind == "weapon" then it.power or 1 else 1
79end
80
81local STONE, ALTAR, LAMP, SNUFFED = "Gloamstone", "Gloam altar", "Moon lamp", "Snuffed moon lamp"
82local BELL, HEART, CLOAK, INGOT = "Moon bell", "Gloam's heart", "Dawn cloak", "Moonsilver ingot"
83local MOONSTONE, WALL, PILLAR, STAIR = "Moonstone", "Slate", "Polished basalt", "Slate stairs"
84local LIGHTS: { [string]: boolean } = { Torch = true, Lantern = true, Candle = true, Brazier = true }
85
86local R, H = 11, 9 -- the hollow's radius and its height in the middle
87local LAMPS = 8 -- pillars round it, each with a lamp
88local MOST = 24 -- hollows a world keeps track of
89local BELOW = -82 -- vaults (and hollows) are only below this
90local DEEP = 8 -- with this much rock over them
91local LOWEST = -114 -- no hollow floor lower than this
92local NEAR, GONE = 40, 56 -- the sleeper comes with a player this close, and goes with none
93local REST = 60 -- seconds before a beaten Gloam gathers again
94local SETTLE = 15 -- seconds with nobody fighting before it settles
95local COOL = 0.45 -- a player's blows count at most this often
96local RAGE, DRINK, BURST = 0.32, 0.62, 0.15
97local METER = "#b48cff"
98local UP = vector.create(0, 1, 0)
99
100-- what may be dug
101local NATURAL: { [string]: boolean } = {
102 Stone = true, Dirt = true, Gravel = true, Sand = true, Slate = true, Sandstone = true, Cobblestone = true,
103 Granite = true, Marble = true, Basalt = true, Water = true,
104 ["Coal ore"] = true, ["Copper ore"] = true, ["Iron ore"] = true, ["Gold ore"] = true, ["Crystal ore"] = true, ["Moonsilver ore"] = true,
105 Glowcap = true,
106}
107-- and what it may put its own blocks over while it builds
108local OURS: { [string]: boolean } = {
109 Air = true, [STONE] = true, [ALTAR] = true, [LAMP] = true, [SNUFFED] = true, [PILLAR] = true, [STAIR] = true,
110}
111
112local function v(x: number, y: number, z: number): vector
113 return vector.create(x, y, z)
114end
115
116local function flat(a: vector, b: vector): number
117 local dx, dz = a.x - b.x, a.z - b.z
118 return math.sqrt(dx * dx + dz * dz)
119end
120
121local function cell(at: vector): vector
122 return v(math.floor(at.x), math.floor(at.y), math.floor(at.z))
123end
124
125-- Whether a block is in this world (the vaults' are the Moonsilver Vaults
126-- creation's): only a yes is kept, since that creation may start later.
127local here: { [string]: boolean } = {}
128local function exists(name: string): boolean
129 if not here[name] and pcall(world.kind, name) then
130 here[name] = true
131 end
132 return here[name] == true
133end
134
135local shown: { [number]: number } = {}
136local function tell(player, text: string)
137 ui.show(player, "gloam", text, { at = "bottom" })
138 local mine = (shown[player.id] or 0) + 1
139 shown[player.id] = mine
140 world.after(5, function()
141 if shown[player.id] == mine then
142 shown[player.id] = nil
143 ui.hide(player, "gloam")
144 end
145 end)
146end
147
148-- ---- what's kept ----
149
150-- each hollow: the altar's cell (its middle on the floor), where the way in
151-- starts, how many stairs it has, whether it's built, whether a bell is in
152-- its altar, and how many times its Gloam was beaten
153type Hollow = { at: vector, from: vector, stairs: number, sanctum: boolean, built: boolean, bell: boolean, beaten: number }
154local hollows: { Hollow } = store.get("hollows") or {}
155-- who has had a dawn cloak, by name
156local cloaks: { [string]: boolean } = store.get("cloaks") or {}
157-- players told of the Gloam on their first dark night, by name
158local heard: { [string]: boolean } = store.get("heard") or {}
159-- the dawn stone on the Green: where, and who beat the Gloam
160type Dawn = { at: vector?, names: { string } }
161local dawn: Dawn = store.get("dawn") or { names = {} }
162
163local function save()
164 store.set("hollows", hollows)
165end
166
167-- ---- where things are in a hollow ----
168
169-- how high the hollow is (in blocks of air) this far out, by the square
170local function height(d2: number): number
171 return H - math.floor(d2 / ((R + 0.5) * (R + 0.5)) * 4)
172end
173
174local function lamp_cells(h: Hollow): { vector }
175 local out = {}
176 for k = 0, LAMPS - 1 do
177 local a = (k + 0.5) * 2 * math.pi / LAMPS
178 table.insert(out, h.at + v(math.floor(math.cos(a) * 8 + 0.5), 2, math.floor(math.sin(a) * 8 + 0.5)))
179 end
180 return out
181end
182
183-- where the Gloam sleeps: the hollow's north end
184local function bed(h: Hollow): vector
185 return h.at + v(0.5, 0, -4.5)
186end
187
188local function nearest_hollow(at: vector, within: number): number?
189 local best, best_d = nil, within
190 for i, h in hollows do
191 local d = vector.magnitude(at - h.at)
192 if d < best_d then
193 best, best_d = i, d
194 end
195 end
196 return best
197end
198
199local function in_hollow(h: Hollow, at: vector): boolean
200 return flat(at, h.at + v(0.5, 0, 0.5)) < R + 1 and at.y > h.at.y - 2 and at.y < h.at.y + H
201end
202
203-- the players in survival in a hollow
204local function fighters(h: Hollow)
205 local out = {}
206 for _, p in world.players() do
207 if world.pockets(p) ~= nil and in_hollow(h, p.position) then
208 table.insert(out, p)
209 end
210 end
211 return out
212end
213
214-- ---- building it ----
215
216-- what's left to put down: a place, a block ("" to dig), the way a stair
217-- faces, and whether it may go through the vault's moonstone
218type Work = { at: vector, name: string, facing: string?, force: boolean? }
219local work: { Work } = {}
220local done = 0
221local building: number? = nil
222
223local function plan(i: number)
224 local h = hollows[i]
225 local c = h.at
226 work, done = {}, 0
227 local function put(at: vector, name: string, facing: string?, force: boolean?)
228 table.insert(work, { at = at, name = name, facing = facing, force = force })
229 end
230 -- the hollow: a gloamstone floor under a dome of slate
231 local inner, outer = (R + 0.5) * (R + 0.5), (R + 1.5) * (R + 1.5)
232 for dx = -R - 1, R + 1 do
233 for dz = -R - 1, R + 1 do
234 local d2 = dx * dx + dz * dz
235 if d2 <= outer then
236 local within = d2 <= inner
237 local up = if within then height(d2) else 0
238 put(c + v(dx, -1, dz), if within then STONE else WALL)
239 for dy = 0, H do
240 put(c + v(dx, dy, dz), if dy < up then "" else WALL)
241 end
242 end
243 end
244 end
245 -- the pillars, each with its lamp
246 for _, lamp in lamp_cells(h) do
247 local base = lamp - v(0, 2, 0)
248 local d = base - c
249 for dy = 0, height(d.x * d.x + d.z * d.z) - 1 do
250 put(base + v(0, dy, 0), if dy == 2 then LAMP else PILLAR)
251 end
252 end
253 put(c, ALTAR)
254 -- the way in, from its start north to the hollow: a stair, then two
255 -- level steps; its first row (through the vault's wall) last of all,
256 -- so nobody walks in before it's done
257 local s, n = h.from, h.stairs
258 local function row(j: number, floor: number, stair: boolean)
259 local z = s.z - j
260 for x = -2, 2 do
261 local at = v(s.x + x, floor, z)
262 if math.abs(x) == 2 then
263 for dy = -1, 3 do
264 put(at + v(0, dy, 0), WALL)
265 end
266 else
267 put(at - UP, WALL)
268 -- a stair's high side toward the start, so it goes down north
269 put(at, if stair then STAIR else "", if stair then "south" else nil)
270 put(at + UP, "")
271 put(at + UP * 2, "")
272 if stair then
273 put(at + UP * 3, "")
274 end
275 put(at + UP * (if stair then 4 else 3), WALL)
276 end
277 end
278 end
279 for j = 1, n do
280 row(j, s.y - j, true)
281 end
282 for j = n + 1, n + 2 do
283 row(j, s.y - n, false)
284 end
285 if h.sanctum then
286 -- through the sanctum's back wall, keeping its moonstone floor
287 for x = -1, 1 do
288 for dy = 0, 2 do
289 put(s + v(x, dy, 0), "", nil, true)
290 end
291 end
292 else
293 row(0, s.y, false)
294 end
295end
296
297-- puts down a few hundred blocks a tick
298local function step()
299 local last = math.min(done + 250, #work)
300 for k = done + 1, last do
301 local w = work[k]
302 local b = world.block(w.at)
303 if w.force or NATURAL[b] or OURS[b] or b == WALL then
304 local name = if w.name == "" then "Air" else w.name
305 if b ~= name then
306 if w.facing then
307 world.set_block(w.at, name, w.facing)
308 else
309 world.set_block(w.at, name)
310 end
311 end
312 end
313 end
314 done = last
315 if done >= #work then
316 work, done = {}, 0
317 local i = building
318 building = nil
319 if i then
320 local h = hollows[i]
321 h.built = true
322 save()
323 for _, p in world.players() do
324 if vector.magnitude(p.position - h.from) < 10 then
325 tell(p, if h.sanctum then "Behind the coffer, the stone gives way to a stair." else "Something vast breathes, just north of here.")
326 end
327 end
328 end
329 end
330end
331
332-- nothing cuts the hollow's floor, altar or lamps
333for _, b in { STONE, ALTAR, LAMP, SNUFFED } do
334 world.drops(b, { gives = false, level = 9 })
335end
336
337-- ---- finding a place ----
338
339-- Where the way down starts if a player stands in a vault's sanctum: the
340-- middle of its back wall, at the floor. A sanctum is floored and roofed in
341-- moonstone three blocks over the floor, with walls two either side of its
342-- middle and its back wall north.
343local function sanctum_at(p): vector?
344 local at = cell(p.position)
345 local function stone(dx: number, dy: number, dz: number): boolean
346 return world.block(at + v(dx, dy, dz)) == MOONSTONE
347 end
348 if not stone(0, -1, 0) or not stone(0, 3, 0) or stone(0, 0, 0) then
349 return nil
350 end
351 local back = nil
352 for dz = 1, 3 do
353 if stone(0, 0, -dz) then
354 back = dz
355 break
356 end
357 end
358 local west, east = nil, nil
359 for dx = 1, 3 do
360 if not west and stone(-dx, 0, 0) then
361 west = dx
362 end
363 if not east and stone(dx, 0, 0) then
364 east = dx
365 end
366 end
367 if not back or not west or not east or west + east ~= 4 then
368 return nil
369 end
370 return at + v(east - 2, 0, -back)
371end
372
373local function far_from_others(at: vector, apart: number): boolean
374 for _, h in hollows do
375 if flat(h.at, at) < apart then
376 return false
377 end
378 end
379 return true
380end
381
382local function open(from: vector, stairs: number, sanctum: boolean)
383 local floor = from.y - stairs
384 local h: Hollow = {
385 at = v(from.x, floor, from.z - stairs - 2 - R),
386 from = from,
387 stairs = stairs,
388 sanctum = sanctum,
389 built = false,
390 bell = false,
391 beaten = 0,
392 }
393 table.insert(hollows, h)
394 building = #hollows
395 plan(#hollows)
396 save()
397end
398
399-- Whether a hollow should open for a player: behind a vault's sanctum, or
400-- where there are no vaults, near anyone far enough down.
401local function look(p)
402 local pos = p.position
403 -- the Long Dusk has finds of its own
404 if world.place(pos) == "dusk" then
405 return
406 end
407 if pos.y > BELOW or #hollows >= MOST then
408 return
409 end
410 if exists("Moonsilver coffer") then
411 local s = sanctum_at(p)
412 if s and far_from_others(s, 40) then
413 open(s, math.clamp(s.y - LOWEST, 0, 8), true)
414 end
415 elseif world.top(pos.x, pos.z) >= pos.y + DEEP and pos.y > LOWEST and far_from_others(pos, knobs.apart) then
416 open(cell(pos), 0, false)
417 end
418end
419
420-- ---- the lamps ----
421
422local function lit(h: Hollow): number
423 local n = 0
424 for _, at in lamp_cells(h) do
425 if world.block(at) == LAMP then
426 n += 1
427 end
428 end
429 return n
430end
431
432local function relight_all(h: Hollow)
433 for _, at in lamp_cells(h) do
434 if world.block(at) == SNUFFED then
435 world.set_block(at, LAMP)
436 end
437 end
438end
439
440local function snuff(h: Hollow)
441 for _, at in lamp_cells(h) do
442 if world.block(at) == LAMP then
443 world.set_block(at, SNUFFED)
444 end
445 end
446 sound.play("snuff", { at = h.at, volume = 1 })
447end
448
449-- ---- the Gloam ----
450
451local HIDE, DARK, VEIN = knobs.hide, "#0c0a12", "#7a58c8"
452local EYE, PUPIL, MAW, BONE = knobs.eyes, "#120a1e", "#5a3a9a", "#d8d0e8"
453
454local function awake_body(rear: boolean)
455 local lift = if rear then 0.3 else 0
456 local arm_y = if rear then 3.4 else 1.25
457 local claw_y = if rear then 4.35 else 0.2
458 local arm_z = if rear then -0.95 else -0.6
459 return {
460 { "leg_left", size = { 0.7, 1.0, 0.8 }, at = { -0.8, 0.5, 0.7 }, color = DARK, leg = true },
461 { "leg_right", size = { 0.7, 1.0, 0.8 }, at = { 0.8, 0.5, 0.7 }, color = DARK, leg = true },
462 { "body", size = { 2.4, 1.6, 2.4 }, at = { 0, 1.7, 0.3 }, color = HIDE },
463 { "veins", size = { 2.44, 0.1, 1.7 }, at = { 0, 1.95, 0.4 }, color = VEIN, glow = true },
464 { "mane", size = { 2.7, 0.9, 1.4 }, at = { 0, 2.6 + lift, -0.3 }, color = DARK },
465 { "tail", size = { 0.5, 0.5, 1.5 }, at = { 0, 1.2, 2.1 }, color = DARK },
466 { "head", size = { 1.5, 1.1, 1.2 }, at = { 0, 2.7 + lift, -1.15 }, color = HIDE, head = true },
467 { "brow", size = { 1.6, 0.22, 0.3 }, at = { 0, 3.2 + lift, -1.65 }, color = DARK, head = true },
468 { "eye_left", size = { 0.44, 0.44, 0.06 }, at = { -0.38, 2.86 + lift, -1.77 }, color = EYE, glow = true, head = true },
469 { "pupil_left", size = { 0.2, 0.26, 0.05 }, at = { -0.34, 2.84 + lift, -1.82 }, color = PUPIL, head = true },
470 { "eye_right", size = { 0.44, 0.44, 0.06 }, at = { 0.38, 2.86 + lift, -1.77 }, color = EYE, glow = true, head = true },
471 { "pupil_right", size = { 0.2, 0.26, 0.05 }, at = { 0.34, 2.84 + lift, -1.82 }, color = PUPIL, head = true },
472 { "maw", size = { 1.0, 0.24, 0.06 }, at = { 0, 2.38 + lift, -1.77 }, color = MAW, glow = true, head = true },
473 { "fang_left", size = { 0.1, 0.2, 0.05 }, at = { -0.3, 2.26 + lift, -1.8 }, color = BONE, head = true },
474 { "fang_right", size = { 0.1, 0.2, 0.05 }, at = { 0.3, 2.26 + lift, -1.8 }, color = BONE, head = true },
475 { "horn_left", size = { 0.2, 0.7, 0.2 }, at = { -0.62, 3.6 + lift, -0.95 }, color = BONE, head = true },
476 { "horn_right", size = { 0.2, 0.7, 0.2 }, at = { 0.62, 3.6 + lift, -0.95 }, color = BONE, head = true },
477 { "horn_tip_left", size = { 0.14, 0.34, 0.14 }, at = { -0.76, 4.05 + lift, -0.85 }, color = VEIN, glow = true, head = true },
478 { "horn_tip_right", size = { 0.14, 0.34, 0.14 }, at = { 0.76, 4.05 + lift, -0.85 }, color = VEIN, glow = true, head = true },
479 { "arm_left", size = { 0.6, 1.6, 0.6 }, at = { -1.5, arm_y, arm_z }, color = DARK, leg = not rear },
480 { "arm_right", size = { 0.6, 1.6, 0.6 }, at = { 1.5, arm_y, arm_z }, color = DARK, leg = not rear },
481 { "claws_left", size = { 0.72, 0.32, 0.72 }, at = { -1.5, claw_y, arm_z - 0.12 }, color = BONE, leg = not rear },
482 { "claws_right", size = { 0.72, 0.32, 0.72 }, at = { 1.5, claw_y, arm_z - 0.12 }, color = BONE, leg = not rear },
483 }
484end
485
486local Asleep = world.creature {
487 name = "The Gloam (asleep)",
488 speed = 0.5,
489 count = 0,
490 home = "here",
491 size = { 2.8, 1.6, 2.8 },
492 behaviors = { wander = false },
493 body = {
494 { "body", size = { 2.8, 1.3, 2.6 }, at = { 0, 0.65, 0.2 }, color = HIDE },
495 { "veins", size = { 2.84, 0.1, 1.9 }, at = { 0, 0.95, 0.3 }, color = VEIN, glow = true },
496 { "mane", size = { 2.4, 0.5, 1.4 }, at = { 0, 1.45, -0.2 }, color = DARK },
497 { "tail", size = { 2.2, 0.45, 0.5 }, at = { 0.2, 0.23, 1.6 }, color = DARK },
498 { "head", size = { 1.4, 0.9, 1.1 }, at = { -0.3, 0.45, -1.45 }, color = HIDE },
499 { "lid_left", size = { 0.4, 0.06, 0.05 }, at = { -0.66, 0.6, -2.02 }, color = MAW, glow = true },
500 { "lid_right", size = { 0.4, 0.06, 0.05 }, at = { 0.06, 0.6, -2.02 }, color = MAW, glow = true },
501 { "horn_left", size = { 0.2, 0.2, 0.7 }, at = { -0.9, 0.9, -1.2 }, color = BONE },
502 { "horn_right", size = { 0.2, 0.2, 0.7 }, at = { 0.3, 0.9, -1.2 }, color = BONE },
503 { "claws", size = { 1.8, 0.3, 0.6 }, at = { 0.4, 0.15, -1.6 }, color = BONE },
504 },
505}
506
507local Gloam = world.creature {
508 name = "The Gloam",
509 speed = 1.4,
510 count = 0,
511 home = "here",
512 size = { 2.8, 3.4, 2.8 },
513 behaviors = { wander = false },
514 body = awake_body(false),
515}
516
517-- rearing up to slam: the same Gloam with its arms up
518local Rearing = world.creature {
519 name = "The Gloam (rearing)",
520 speed = 0.1,
521 count = 0,
522 home = "here",
523 size = { 2.8, 3.4, 2.8 },
524 behaviors = { wander = false },
525 body = awake_body(true),
526}
527
528local Shade = world.creature {
529 name = "Gloam shade",
530 speed = 2.4,
531 count = 0,
532 home = "here",
533 flies = true,
534 size = { 0.6, 0.9, 0.6 },
535 behaviors = { wander = false },
536 body = {
537 { "wisp", size = { 0.52, 0.6, 0.52 }, at = { 0, 0.55, 0 }, color = "#120e1a", head = true },
538 { "tail", size = { 0.3, 0.32, 0.3 }, at = { 0, 0.16, 0.06 }, color = "#241c34" },
539 { "eye_left", size = { 0.17, 0.19, 0.03 }, at = { -0.12, 0.66, -0.27 }, color = EYE, glow = true, head = true },
540 { "pupil_left", size = { 0.08, 0.1, 0.03 }, at = { -0.11, 0.65, -0.29 }, color = PUPIL, head = true },
541 { "eye_right", size = { 0.17, 0.19, 0.03 }, at = { 0.12, 0.66, -0.27 }, color = EYE, glow = true, head = true },
542 { "pupil_right", size = { 0.08, 0.1, 0.03 }, at = { 0.11, 0.65, -0.29 }, color = PUPIL, head = true },
543 },
544}
545
546-- the fight in each hollow, outside the creatures so their kinds can share
547-- it: when the Gloam last stirred, a count that changes when a fight ends
548-- (so its shades go), a bell just rung, who hit it, and when it may come
549-- back after it was beaten
550local stirred: { [number]: number } = {}
551local awake: { [number]: boolean } = {}
552local fights: { [number]: number } = {}
553local waking: { [number]: boolean } = {}
554local hitters: { [number]: { [string]: boolean } } = {}
555local resting: { [number]: number } = {}
556local metered: { [number]: boolean } = {}
557local last_hit: { [number]: number } = {}
558
559local function stale(i: number): boolean
560 return world.time() - (stirred[i] or -10) > 2
561end
562
563local function show_meter(p, self)
564 ui.meter(p, "gloam_meter", math.clamp(self.health / self.most, 0, 1), { color = METER })
565 metered[p.id] = true
566end
567
568local function hide_meters(h: Hollow)
569 for _, p in world.players() do
570 if metered[p.id] and vector.magnitude(p.position - h.at) < 80 then
571 metered[p.id] = nil
572 ui.meter(p, "gloam_meter", nil)
573 end
574 end
575end
576
577local function by_id(id: number?)
578 for _, q in world.players() do
579 if q.id == id then
580 return q
581 end
582 end
583 return nil
584end
585
586function Asleep:spawned()
587 self.hollow = nearest_hollow(self.position, 30)
588 self.breath_in = world.random(2, 5)
589end
590
591local function wake(self, h: Hollow)
592 local d = danger()
593 local crew = math.max(#fighters(h), 1)
594 self:become(Gloam)
595 self.phase = 1
596 self.crew = crew
597 self.most = knobs.health * d.health * (1 + 0.6 * (crew - 1))
598 self.health = self.most
599 self.slam_in, self.swipe_in, self.throw_in, self.look_in = 5, 1, 3, 0
600 self.alone = 0
601 self.rearing = 0
602 self.burst = false
603 fights[self.hollow] = (fights[self.hollow] or 0) + 1
604 awake[self.hollow] = true
605 hitters[self.hollow] = {}
606 relight_all(h)
607 sound.play("roar", { at = self, volume = 1 })
608 for _, p in fighters(h) do
609 tell(p, "The Gloam wakes! The violet bar is its strength.")
610 show_meter(p, self)
611 end
612end
613
614function Asleep:tick(dt: number)
615 local i = self.hollow
616 local h = if i then hollows[i] else nil
617 if not i or not h or not knobs.on or danger().health == 0 or not world.nearest_player(self.position, GONE) then
618 self:remove()
619 return
620 end
621 stirred[i] = world.time()
622 self:stop()
623 if waking[i] then
624 waking[i] = nil
625 wake(self, h)
626 return
627 end
628 self.breath_in -= dt
629 if self.breath_in <= 0 then
630 self.breath_in = world.random(5, 8)
631 sound.play("breath", { at = self, volume = 0.7 })
632 end
633end
634
635-- shards of night, at whoever keeps back
636local function shard(self, p, d)
637 self:throw({ at = p.position + v(0, 1.2, 0), look = "stone", speed = 14, drop = 0.3, power = 3 }, function(_at, _block, _creature, player)
638 if player and world.pockets(player) ~= nil then
639 world.hurt(player, 3 * d.hurt)
640 end
641 end)
642end
643
644local function send_shades(self, h: Hollow)
645 local d = danger()
646 local n = math.min(8, math.floor(knobs.shades * d.shades + 0.5) + (self.crew or 1) - 1)
647 local lamps = lamp_cells(h)
648 for k = 1, n do
649 local lamp = lamps[(k * 3) % #lamps + 1]
650 world.spawn(Shade, h.at + (lamp - h.at) * 0.6 + v(0.5, 0.5, 0.5))
651 end
652end
653
654local function settle(self, h: Hollow)
655 fights[self.hollow] = (fights[self.hollow] or 0) + 1
656 awake[self.hollow] = nil
657 relight_all(h)
658 hide_meters(h)
659 h.bell = true
660 save()
661 for _, p in world.players() do
662 if vector.magnitude(p.position - h.at) < 30 then
663 tell(p, "The Gloam settles back to sleep. The bell stays.")
664 end
665 end
666 self:become(Asleep)
667 self.breath_in = 3
668end
669
670-- the slam landing: everyone close is hurt, so step back when it rears
671local function slam(self, h: Hollow, d)
672 self:become(Gloam)
673 sound.play("slam", { at = self, volume = 1 })
674 for _, p in fighters(h) do
675 if flat(p.position, self.position) < 5.5 and math.abs(p.position.y - self.position.y) < 3 and knobs.hits > 0 then
676 world.hurt(p, knobs.hits * 1.6 * d.hurt * (if self.phase == 3 then 1.25 else 1))
677 end
678 end
679 self.slam_in = if self.phase == 3 then 4.5 else 7
680end
681
682function Gloam:tick(dt: number)
683 local i = self.hollow
684 local h = if i then hollows[i] else nil
685 local d = danger()
686 if not i or not h or not knobs.on or d.health == 0 then
687 if h then
688 hide_meters(h)
689 relight_all(h)
690 end
691 if i then
692 awake[i] = nil
693 end
694 self:remove()
695 return
696 end
697 stirred[i] = world.time()
698 awake[i] = true
699 -- who's fighting, every half second
700 self.look_in -= dt
701 if self.look_in <= 0 then
702 self.look_in = 0.5
703 local all = fighters(h)
704 if #all == 0 then
705 self.alone += 0.5
706 if self.alone >= SETTLE then
707 settle(self, h)
708 return
709 end
710 else
711 self.alone = 0
712 end
713 -- more players coming to the fight make it tougher
714 if #all > self.crew then
715 local more = knobs.health * d.health * 0.6 * (#all - self.crew)
716 self.most += more
717 self.health += more
718 self.crew = #all
719 end
720 local best, best_d = nil, math.huge
721 for _, p in all do
722 show_meter(p, self)
723 local dist = flat(p.position, self.position)
724 if dist < best_d then
725 best, best_d = p, dist
726 end
727 end
728 self.target = if best then best.id else nil
729 self.lit = lit(h)
730 end
731 local p = by_id(self.target)
732 if not p then
733 self:stop()
734 return
735 end
736 local speed = 1.4 * d.speed * (if self.phase == 3 then 1.5 else 1)
737 -- rearing up: it slams when it lands
738 if self.rearing > 0 then
739 self.rearing -= dt
740 self:stop()
741 if self.rearing <= 0 then
742 slam(self, h, d)
743 end
744 return
745 end
746 local veiled = self.phase >= 2 and (self.lit or LAMPS) * 2 < LAMPS
747 local dist = flat(p.position, self.position)
748 -- shards of night once it has drunk the light
749 if self.phase >= 2 then
750 self.throw_in -= dt
751 if self.throw_in <= 0 then
752 self.throw_in = if self.phase == 3 then 2 else 3
753 local all = fighters(h)
754 local q = all[math.floor(world.random(1, #all + 0.999))]
755 if q and flat(q.position, self.position) > 4 then
756 self:face(q.position)
757 shard(self, q, d)
758 end
759 end
760 end
761 if veiled then
762 -- wrapped in the dark, it keeps to its bed and throws
763 local home = bed(h)
764 if flat(self.position, home) > 1 then
765 self:walk_to(home, speed)
766 else
767 self:stop()
768 self:face(p.position)
769 end
770 return
771 end
772 self.slam_in -= dt
773 if self.slam_in <= 0 and dist < 7 then
774 self.rearing = 1.1
775 self:stop()
776 self:face(p.position)
777 self:become(Rearing)
778 sound.play("rumble", { at = self, volume = 1 })
779 return
780 end
781 if dist < 4 then
782 self:stop()
783 self:face(p.position)
784 self.swipe_in -= dt
785 if self.swipe_in <= 0 then
786 self.swipe_in = if self.phase == 3 then 1.1 else 1.6
787 sound.play("swipe", { at = self, volume = 1 })
788 if knobs.hits > 0 and math.abs(p.position.y - self.position.y) < 3 then
789 world.hurt(p, knobs.hits * d.hurt * (if self.phase == 3 then 1.25 else 1))
790 end
791 end
792 else
793 -- after them, but never out of its hollow
794 local to = p.position
795 local c = h.at + v(0.5, 0, 0.5)
796 if flat(to, c) > R - 2 then
797 local off = vector.create(to.x - c.x, 0, to.z - c.z)
798 to = c + vector.normalize(off) * (R - 2)
799 end
800 self:walk_to(to, speed)
801 end
802end
803
804-- beaten: the dark breaks into light, and everyone who fought is rewarded
805local function beaten(self, h: Hollow)
806 local i = self.hollow
807 fights[i] = (fights[i] or 0) + 1
808 awake[i] = nil
809 sound.play("dawn", { at = self, volume = 1 })
810 relight_all(h)
811 hide_meters(h)
812 h.bell = false
813 h.beaten += 1
814 resting[i] = world.time() + REST
815 save()
816 -- who fought: in the hollow now, or hit it and still near
817 local who = {}
818 local names = hitters[i] or {}
819 for _, p in world.players() do
820 if world.pockets(p) ~= nil and (in_hollow(h, p.position) or (names[p.name] and vector.magnitude(p.position - h.at) < 40)) then
821 table.insert(who, p)
822 end
823 end
824 local first = dawn.at == nil
825 for _, p in who do
826 world.reward(p, 100)
827 world.give(p, HEART, 1)
828 local ingots = math.floor(world.random(3, 5.999))
829 world.give(p, INGOT, ingots)
830 local got = string.format("the Gloam's heart and %d moonsilver ingots", ingots)
831 if not cloaks[p.name] then
832 cloaks[p.name] = true
833 world.give(p, CLOAK, 1)
834 got = string.format("the Gloam's heart, %d moonsilver ingots and a dawn cloak", ingots)
835 end
836 local text = "The dark breaks into light, and the hollow is still. You won " .. got .. "."
837 if first then
838 text ..= "\nA dawn stone now stands on the Green with your name on it."
839 end
840 text ..= "\nIn time it gathers in the dark again, for another moon bell."
841 ui.ask(p, { title = "The Gloam is beaten", text = text }, function() end)
842 if #dawn.names < 12 and not table.find(dawn.names, p.name) then
843 table.insert(dawn.names, p.name)
844 end
845 end
846 store.set("cloaks", cloaks)
847 if first and #who > 0 then
848 local green = world.spot("green")
849 if green then
850 local x, z = math.floor(green.x) + 5, math.floor(green.z) + 5
851 local y = world.top(x, z) + 1
852 -- only over air and plants, so nothing built is lost
853 local function free(at: vector, name: string)
854 local b = world.block(at)
855 if b == "Air" or world.kind(b) == "plant" then
856 world.set_block(at, name)
857 end
858 end
859 for dx = -1, 1 do
860 for dz = -1, 1 do
861 free(v(x + dx, y, z + dz), STONE)
862 end
863 end
864 free(v(x, y + 1, z), STONE)
865 free(v(x, y + 2, z), STONE)
866 free(v(x, y + 3, z), LAMP)
867 dawn.at = v(x, y, z)
868 local all = {}
869 for k, p in who do
870 if k <= 3 then
871 table.insert(all, p.name)
872 end
873 end
874 local by = table.concat(all, ", ") .. (if #who > 3 then " and friends" else "")
875 world.say("The Gloam is beaten, by " .. by .. ". A dawn stone stands on the Green.")
876 end
877 end
878 store.set("dawn", dawn)
879 self:remove()
880end
881
882function Gloam:hit(player, shot)
883 local i = self.hollow
884 local h = if i then hollows[i] else nil
885 if not h or not self.most then
886 return
887 end
888 if world.pockets(player) == nil then
889 tell(player, "The Gloam fights only players in survival.")
890 return
891 end
892 local now = world.time()
893 if now - (last_hit[player.id] or -10) < COOL then
894 return
895 end
896 last_hit[player.id] = now
897 local bop = if shot then shot.power else bops(world.held(player)) * world.might(player)
898 local veiled = self.phase >= 2 and lit(h) * 2 < LAMPS
899 if veiled then
900 bop *= 0.25
901 if not self.told_veil then
902 self.told_veil = true
903 for _, p in fighters(h) do
904 tell(p, "The dark shields it. Light the lamps again!")
905 end
906 end
907 end
908 self.health -= bop
909 hitters[i] = hitters[i] or {}
910 hitters[i][player.name] = true
911 show_meter(player, self)
912 if self.health <= 0 then
913 beaten(self, h)
914 return
915 end
916 local share = self.health / self.most
917 if self.phase == 1 and share <= DRINK then
918 self.phase = 2
919 self.told_veil = false
920 self.rearing = 0
921 self:become(Gloam)
922 snuff(h)
923 send_shades(self, h)
924 sound.play("roar", { at = self, volume = 1 })
925 for _, p in fighters(h) do
926 tell(p, "It drinks the light! Use the lamps to light them.")
927 end
928 elseif self.phase == 2 and share <= RAGE then
929 self.phase = 3
930 sound.play("roar", { at = self, volume = 1, pitch = 0.8 })
931 for _, p in fighters(h) do
932 tell(p, "The Gloam rages!")
933 end
934 elseif self.phase == 3 and not self.burst and share <= BURST then
935 self.burst = true
936 self.told_veil = false
937 snuff(h)
938 send_shades(self, h)
939 for _, p in fighters(h) do
940 tell(p, "It drinks the light again!")
941 end
942 else
943 sound.play("swipe", { at = self, volume = 0.5, pitch = 0.6, caption = "The Gloam is struck" })
944 end
945end
946
947Rearing.tick = Gloam.tick
948Rearing.hit = Gloam.hit
949
950-- ---- its shades ----
951
952function Shade:spawned()
953 self.hollow = nearest_hollow(self.position, 30)
954 self.fight = if self.hollow then fights[self.hollow] else nil
955 self.nip_in = 1
956 self.look_in = 0
957 sound.play("whisper", { at = self, volume = 0.8 })
958end
959
960local function fade(self)
961 sound.play("relight", { at = self, volume = 0.5, pitch = 0.7, caption = "A shade fades" })
962 self:remove()
963end
964
965function Shade:tick(dt: number)
966 local i = self.hollow
967 local h = if i then hollows[i] else nil
968 if not i or not h or fights[i] ~= self.fight or stale(i) then
969 self:remove()
970 return
971 end
972 self.look_in -= dt
973 if self.look_in <= 0 then
974 self.look_in = 0.4
975 -- light drives them off: a lit lamp, or one in hand
976 if world.find_light(self.position, 2.5) then
977 fade(self)
978 return
979 end
980 local best, best_d = nil, math.huge
981 for _, p in fighters(h) do
982 local dist = vector.magnitude(p.position - self.position)
983 local held = world.held(p)
984 if held and LIGHTS[held] and dist < 3 then
985 fade(self)
986 return
987 end
988 if dist < best_d then
989 best, best_d = p, dist
990 end
991 end
992 self.target = if best then best.id else nil
993 if world.random() < 0.1 then
994 sound.play("whisper", { at = self, volume = 0.6 })
995 end
996 end
997 local p = by_id(self.target)
998 if not p then
999 self:stop()
1000 self:climb(0)
1001 return
1002 end
1003 local head = p.position + v(0, 1.4, 0)
1004 self:climb(math.clamp((head.y - self.position.y) * 1.5, -2, 2))
1005 self.nip_in -= dt
1006 if vector.magnitude(head - self.position) < 1.5 then
1007 self:stop()
1008 if self.nip_in <= 0 then
1009 self.nip_in = 1.5
1010 sound.play("whisper", { at = self, volume = 1, pitch = 1.3 })
1011 world.hurt(p, 2 * danger().hurt)
1012 end
1013 else
1014 self:walk_to(v(p.position.x, self.position.y, p.position.z), 2.4 * danger().speed)
1015 end
1016end
1017
1018function Shade:hit(player, shot)
1019 self.bopped = (self.bopped or 0) + 1
1020 if self.bopped >= 2 then
1021 world.reward(player, 1)
1022 fade(self)
1023 else
1024 sound.play("whisper", { at = self, volume = 0.8, pitch = 1.6 })
1025 end
1026end
1027
1028-- ---- the altar, the lamps and the dawn stone ----
1029
1030world.on("block_used", function(player, at: vector, block: string)
1031 if block == ALTAR then
1032 local i = nearest_hollow(at, 4)
1033 local h = if i then hollows[i] else nil
1034 if not i or not h then
1035 return
1036 end
1037 if world.pockets(player) == nil then
1038 tell(player, "The Gloam wakes only for players in survival.")
1039 elseif danger().health == 0 or not knobs.on then
1040 tell(player, "The Gloam sleeps on. It never wakes on Peaceful.")
1041 elseif (awake[i] and not stale(i)) or waking[i] then
1042 return
1043 elseif (resting[i] or 0) > world.time() then
1044 tell(player, "The hollow is still. Nothing stirs yet.")
1045 elseif h.bell or (world.held(player) == BELL and world.take(player, BELL, 1)) then
1046 h.bell = true
1047 save()
1048 sound.play("bell", { at = at, volume = 1 })
1049 waking[i] = true
1050 if stale(i) then
1051 world.spawn(Asleep, bed(h))
1052 stirred[i] = world.time()
1053 end
1054 else
1055 tell(player, "A bell-shaped hollow. A Moon bell rings here.")
1056 end
1057 elseif block == SNUFFED then
1058 local i = nearest_hollow(at, R + 4)
1059 if i then
1060 world.set_block(at, LAMP)
1061 sound.play("relight", { at = at, volume = 1 })
1062 end
1063 elseif (block == STONE or block == LAMP) and dawn.at and vector.magnitude(at - (dawn.at :: vector)) < 4 then
1064 local names = dawn.names
1065 local by = if #names > 0 then table.concat(names, ", ") else "someone"
1066 ui.ask(player, { title = "The dawn stone", text = "Here the Gloam was beaten, by " .. by .. "." }, function() end)
1067 end
1068end)
1069
1070-- the bell hums toward the nearest hollow
1071world.on("item_used", function(player, item: string?, _aim)
1072 if item ~= BELL then
1073 return
1074 end
1075 local best, best_d = nil, math.huge
1076 for _, h in hollows do
1077 local d = vector.magnitude(h.at - player.position)
1078 if d < best_d then
1079 best, best_d = h, d
1080 end
1081 end
1082 sound.play_for(player, "bell", { volume = 0.5 })
1083 if not best then
1084 tell(player, "The bell hums toward the deep. Find a moonsilver vault.")
1085 return
1086 end
1087 local to = best.at - player.position
1088 local ways = { "east", "south-east", "south", "south-west", "west", "north-west", "north", "north-east" }
1089 local a = math.atan2(to.z, to.x)
1090 local way = ways[(math.floor(a / (math.pi / 4) + 0.5) % 8) + 1]
1091 if best_d < R + 2 then
1092 tell(player, "The bell rings clear. Ring it at the altar.")
1093 elseif to.y < -20 then
1094 tell(player, "The bell hums " .. way .. ", far below.")
1095 else
1096 tell(player, "The bell hums " .. way .. ".")
1097 end
1098end)
1099
1100world.on("player_left", function(p)
1101 shown[p.id] = nil
1102 metered[p.id] = nil
1103 last_hit[p.id] = nil
1104end)
1105
1106-- ---- watching players ----
1107
1108local turn = 0
1109world.every(0.1, function()
1110 if #work > 0 then
1111 step()
1112 return
1113 end
1114 turn += 1
1115 if turn % 10 ~= 0 then
1116 return
1117 end
1118 local players = world.players()
1119 if #players == 0 then
1120 return
1121 end
1122 -- a way down for one player a second
1123 look(players[(turn // 10) % #players + 1])
1124 if not knobs.on or danger().health == 0 then
1125 return
1126 end
1127 -- the sleeper, wherever someone's near a hollow it isn't in
1128 for i, h in hollows do
1129 if h.built and stale(i) and (resting[i] or 0) <= world.time() and world.nearest_player(h.at, NEAR) then
1130 world.spawn(Asleep, bed(h))
1131 stirred[i] = world.time()
1132 end
1133 end
1134 -- on their first dark night, players hear it
1135 if turn % 100 == 0 and world.daylight() < 0.15 then
1136 for _, p in players do
1137 if world.pockets(p) ~= nil and not heard[p.name] and not shown[p.id] then
1138 heard[p.name] = true
1139 store.set("heard", heard)
1140 sound.play_for(p, "breath", { volume = 0.6 })
1141 tell(p, "Far under the island, something vast breathes.")
1142 end
1143 end
1144 end
1145end)
1146
1147-- a hollow left half built (the world stopped mid-way) is built on
1148for i, h in hollows do
1149 if not h.built then
1150 building = i
1151 plan(i)
1152 break
1153 end
1154end