Sign in
world

Mole Runs

by Potluck · version 1 · Checked for all ages

Out in the meadows, far from the Green, a line of fresh molehills marks a mole run under the grass. Dig down through one and follow the run to the den at its end, where the old moles keep a hoard in a nest of hay: things they've turned up: seeds, flint, glass, old coins and, once in a while, a lost ring for the Curator. The den's moles are friendly. Offer one some berries and it'll go and fetch you something.

1--!strict
2-- Mole Runs: out in the meadows, far from the Green, a line of fresh
3-- molehills turns up on the grass near a player now and then, marking a
4-- mole run a few blocks under it. Under each hill the earth is loose (a
5-- block of dirt, then the run), so digging down through one drops you in.
6-- The run winds from hill to hill and ends at a den: a round room in the
7-- earth, lit by a few glowcaps, where the old moles keep their hoard.
8--
9-- The hoard, in a nest of hay in the middle of the den (a plain hay block,
10-- known by where it is), gives what the moles turned up
11-- once: seeds, flint, glass, berries, an apple, raw gold, old coins (the
12-- Curator's) and now and then a lost ring, a relic (an item of this
13-- creation's) that the Curator on the Green collects. Two moles live in
14-- each den while someone's near; use one with berries in hand and it takes
15-- one, goes off, and comes back with something small, once every few
16-- minutes.
17--
18-- Where each run is, its hills and whether its hoard still holds something
19-- are kept with @potluck/store, so each is dug once. Only grass, earth and
20-- stone are dug.
21
22local world = require("@potluck/world@0.33")
23local tweak = require("@potluck/tweak")
24local store = require("@potluck/store")
25local ui = require("@potluck/ui@0.2")
26local sound = require("@potluck/sound@0.2")
27
28local knobs = tweak {
29 apart = { 160, min = 50, max = 800, label = "How far apart mole runs are", order = 1 },
30 hills = { 6, min = 3, max = 10, label = "Molehills on each", order = 2 },
31 prizes = { 1, min = 0.5, max = 3, label = "How much is in the hoards", order = 3 },
32 fur = { "#4b4038", label = "Fur color", order = 4 },
33}
34
35local HILL, HOARD = "Molehill", "Hay"
36local MOST = 64 -- runs a world keeps track of
37local DEPTH = 3 -- how far under the grass the run goes
38local DEN = 2 -- the den's half width
39local SPACING = 3.5 -- between hills
40
41-- what may be dug, and what a hill may stand on
42local EARTH: { [string]: boolean } = { Dirt = true, Grass = true, Stone = true, Gravel = true, Sand = true, Slate = true, ["Coal ore"] = true, ["Copper ore"] = true, ["Iron ore"] = true }
43local PLANTS: { [string]: boolean } = { Air = true, ["Tall grass"] = true, Fern = true, ["Red flower"] = true, ["White flower"] = true, ["Blue flower"] = true, ["Violet flower"] = true }
44local UP = vector.create(0, 1, 0)
45
46-- ---- what's in the hoards ----
47
48type Prize = { thing: string, chance: number, least: number, most: number }
49local PRIZES: { Prize } = {
50 { thing = "Seeds", chance = 0.7, least = 2, most = 6 },
51 { thing = "Flint", chance = 0.5, least = 1, most = 3 },
52 { thing = "Glass", chance = 0.4, least = 1, most = 3 },
53 { thing = "Berries", chance = 0.4, least = 2, most = 5 },
54 { thing = "Apple", chance = 0.3, least = 1, most = 2 },
55 { thing = "Raw gold", chance = 0.3, least = 1, most = 2 },
56 { thing = "Old coin", chance = 0.5, least = 1, most = 3 },
57 { thing = "Lost ring", chance = 0.35, least = 1, most = 1 },
58}
59-- what a mole fetches for a berry
60local FETCH: { Prize } = {
61 { thing = "Seeds", chance = 0.45, least = 1, most = 3 },
62 { thing = "Flint", chance = 0.25, least = 1, most = 1 },
63 { thing = "Old coin", chance = 0.2, least = 1, most = 1 },
64 { thing = "Raw gold", chance = 0.1, least = 1, most = 1 },
65}
66local HOARD_XP, FETCH_XP = 5, 1
67local FETCH_EVERY = 180 -- seconds between a mole's fetches
68
69-- ---- what's kept ----
70
71-- each run: its den's middle on the floor, its hills, whether it's dug and
72-- its hoard still full, and while it's being dug, the run's cells
73type Run = { at: vector, hills: { vector }, built: boolean, hoard: boolean, cells: { vector }? }
74local runs: { Run } = store.get("runs") or {}
75
76local function save()
77 store.set("runs", runs)
78end
79
80local function key(at: vector): string
81 return string.format("%d,%d,%d", math.floor(at.x), math.floor(at.y), math.floor(at.z))
82end
83
84-- Whether a thing is in this world: old coins are the Curator's.
85local here: { [string]: boolean } = {}
86local function exists(name: string): boolean
87 if here[name] == nil then
88 here[name] = (pcall(world.kind, name))
89 end
90 return here[name] == true
91end
92
93local shown: { [number]: number } = {}
94local function tell(player, text: string)
95 ui.show(player, "moles", text, { at = "bottom" })
96 local mine = (shown[player.id] or 0) + 1
97 shown[player.id] = mine
98 world.after(5, function()
99 if shown[player.id] == mine then
100 shown[player.id] = nil
101 ui.hide(player, "moles")
102 end
103 end)
104end
105
106local function flat(a: vector, b: vector): number
107 local dx, dz = a.x - b.x, a.z - b.z
108 return math.sqrt(dx * dx + dz * dz)
109end
110
111local function v(x: number, y: number, z: number): vector
112 return vector.create(x, y, z)
113end
114
115local function cell(at: vector): vector
116 return v(math.floor(at.x), math.floor(at.y), math.floor(at.z))
117end
118
119-- ---- finding a place ----
120
121-- Where a hill can go at (x, z): the grass on top, with only plants over
122-- it and earth under it down past the run. Says the grass's height.
123local function grass_at(x: number, z: number): number?
124 local y = world.top(x, z)
125 if world.block(v(x, y, z)) ~= "Grass" or not PLANTS[world.block(v(x, y + 1, z))] then
126 return nil
127 end
128 for d = 1, DEPTH + 2 do
129 if not EARTH[world.block(v(x, y - d, z))] then
130 return nil
131 end
132 end
133 return y
134end
135
136-- A line of hills from near a player, wandering, and the den past its end;
137-- nil if the meadow isn't open grass all the way.
138local function lay_out(p): (vector?, { vector }?)
139 local a = world.random(0, 2 * math.pi)
140 local r = world.random(8, 14)
141 local x, z = p.position.x + math.cos(a) * r, p.position.z + math.sin(a) * r
142 local heading = world.random(0, 2 * math.pi)
143 local hills: { vector } = {}
144 for _ = 1, math.floor(knobs.hills) do
145 local gx, gz = math.floor(x), math.floor(z)
146 local y = grass_at(gx, gz)
147 if not y then
148 return nil, nil
149 end
150 table.insert(hills, v(gx, y, gz))
151 heading += world.random(-0.6, 0.6)
152 x += math.cos(heading) * SPACING
153 z += math.sin(heading) * SPACING
154 end
155 -- the den, a little past the last hill, a step deeper than the run
156 x += math.cos(heading) * 2
157 z += math.sin(heading) * 2
158 local dx, dz = math.floor(x), math.floor(z)
159 local y = grass_at(dx, dz)
160 if not y then
161 return nil, nil
162 end
163 local den = v(dx, y - DEPTH - 2, dz)
164 for ox = -DEN - 1, DEN + 1 do
165 for oz = -DEN - 1, DEN + 1 do
166 for oy = -1, 3 do
167 if not EARTH[world.block(den + v(ox, oy, oz))] then
168 return nil, nil
169 end
170 end
171 end
172 end
173 return den, hills
174end
175
176local function far_from_others(pos: vector, apart: number): boolean
177 for _, r in runs do
178 if flat(r.at, pos) < apart then
179 return false
180 end
181 end
182 return true
183end
184
185-- The run's cells (two high): from under each hill to the next, and on to
186-- the den, a step at a time.
187local function run_cells(den: vector, hills: { vector }): { vector }
188 local out: { vector } = {}
189 local seen: { [string]: boolean } = {}
190 local function add(c: vector)
191 local k = key(c)
192 if not seen[k] then
193 seen[k] = true
194 table.insert(out, c)
195 end
196 end
197 local points = {}
198 for _, h in hills do
199 table.insert(points, v(h.x, h.y - DEPTH, h.z))
200 end
201 table.insert(points, den)
202 local last: vector? = nil
203 for i = 1, #points - 1 do
204 local a, b = points[i], points[i + 1]
205 local n = math.max(1, math.ceil(math.max(math.abs(b.x - a.x), math.abs(b.z - a.z))))
206 for t = 0, n do
207 local c = cell(a + (b - a) * (t / n) + v(0.5, 0.5, 0.5))
208 -- a step across a corner goes around it, so the way stays open
209 local prev = last
210 if prev and c.x ~= prev.x and c.z ~= prev.z then
211 add(v(c.x, prev.y, prev.z))
212 end
213 add(c)
214 last = c
215 end
216 end
217 return out
218end
219
220-- ---- digging it ----
221
222type Work = { at: vector, name: string }
223local work: { Work } = {}
224local done = 0
225local digging: Run? = nil
226
227local function plan(r: Run)
228 work, done = {}, 0
229 local function put(at: vector, name: string)
230 table.insert(work, { at = at, name = name })
231 end
232 for _, c in r.cells or {} do
233 put(c, "")
234 put(c + UP, "")
235 end
236 -- under each hill the earth is loose: a block of dirt, then down to the run
237 for _, h in r.hills do
238 put(h, "Dirt")
239 for d = 1, DEPTH - 1 do
240 put(h - UP * d, "")
241 end
242 put(h + UP, HILL)
243 end
244 -- the den: a round room in the earth, with glowcaps and the hoard's nest
245 for ox = -DEN, DEN do
246 for oz = -DEN, DEN do
247 if ox * ox + oz * oz <= DEN * DEN + 1 then
248 for oy = 0, 2 do
249 put(r.at + v(ox, oy, oz), "")
250 end
251 put(r.at + v(ox, -1, oz), "Dirt")
252 end
253 end
254 end
255 put(r.at + v(0, 0, 0), HOARD)
256 put(r.at + v(DEN, 0, 0), "Glowcap")
257 put(r.at + v(-DEN, 0, 1), "Glowcap")
258end
259
260local function step()
261 local last = math.min(done + 200, #work)
262 for i = done + 1, last do
263 local w = work[i]
264 local b = world.block(w.at)
265 if w.name == "" then
266 if EARTH[b] or b == "Glowcap" then
267 world.set_block(w.at, "Air")
268 end
269 elseif w.name == HILL then
270 if PLANTS[b] then
271 world.set_block(w.at, HILL)
272 end
273 elseif EARTH[b] or b == "Air" then
274 world.set_block(w.at, w.name)
275 end
276 end
277 done = last
278 if done >= #work then
279 work, done = {}, 0
280 local r = digging
281 if r then
282 r.built = true
283 r.cells = nil
284 digging = nil
285 save()
286 end
287 end
288end
289
290-- ---- the den's moles ----
291
292local Mole = world.creature {
293 name = "Den mole",
294 speed = 1.2,
295 count = 0,
296 home = "here",
297 size = { 0.5, 0.4, 0.7 },
298 behaviors = {
299 chatter = { "*sniff sniff*", "*snuffle*", "*scratches at the earth*" },
300 sound = "rustle",
301 follow_food = "Berries",
302 },
303 body = {
304 { "body", size = { 0.5, 0.34, 0.66 }, at = { 0, 0.22, 0.04 }, color = knobs.fur },
305 { "head", size = { 0.32, 0.26, 0.26 }, at = { 0, 0.24, -0.38 }, color = knobs.fur, head = true },
306 { "nose", size = { 0.12, 0.1, 0.1 }, at = { 0, 0.24, -0.55 }, color = "#e7a0a8", head = true },
307 { "whiskers", size = { 0.34, 0.02, 0.02 }, at = { 0, 0.22, -0.52 }, color = "#d8d0c8", head = true },
308 { "paw_left", size = { 0.18, 0.08, 0.2 }, at = { -0.3, 0.06, -0.24 }, color = "#e7a0a8", leg = true },
309 { "paw_right", size = { 0.18, 0.08, 0.2 }, at = { 0.3, 0.06, -0.24 }, color = "#e7a0a8", leg = true },
310 },
311}
312
313-- the den each mole lives in, by creature, and when each last fetched
314local den_of: { [number]: number } = {}
315local fetched: { [number]: number } = {}
316
317local function den_near(pos: vector, reach: number): number?
318 for i, r in runs do
319 if vector.magnitude(pos - r.at) < reach then
320 return i
321 end
322 end
323 return nil
324end
325
326function Mole:spawned()
327 self.den = den_near(self.position, 6)
328 self.look_in = world.random(0, 1)
329end
330
331function Mole:tick(dt: number)
332 if self.den then
333 den_of[self.id] = self.den
334 end
335 self.look_in -= dt
336 if self.look_in > 0 then
337 return
338 end
339 self.look_in = 2
340 -- nobody near: it goes, and comes back when someone does
341 if not world.nearest_player(self.position, 60) then
342 den_of[self.id] = nil
343 self:remove()
344 end
345end
346
347local function roll(list: { Prize }, scale: number): { { thing: string, n: number } }
348 local out = {}
349 for _, p in list do
350 if exists(p.thing) and world.random() < math.min(1, p.chance * scale) then
351 local n = math.floor(world.random(p.least, p.most + 0.999) * scale)
352 table.insert(out, { thing = p.thing, n = math.clamp(n, 1, if p.most == 1 then 1 else 64) })
353 end
354 end
355 return out
356end
357
358function Mole:used(player)
359 if world.pockets(player) == nil or world.held(player) ~= "Berries" then
360 return
361 end
362 local last = fetched[self.id] or -math.huge
363 if world.time() - last < FETCH_EVERY then
364 tell(player, "The mole's full of berries for now.")
365 return
366 end
367 if not world.take(player, "Berries", 1) then
368 return
369 end
370 fetched[self.id] = world.time()
371 self:say("*munch*")
372 sound.play("rustle", { at = self, caption = "A mole goes off digging" })
373 local me = self
374 world.after(3, function()
375 local finds = roll(FETCH, 1)
376 local f = finds[1] or { thing = "Seeds", n = 1 }
377 local n = world.give(player, f.thing, f.n)
378 if n > 0 then
379 world.reward(player, FETCH_XP)
380 tell(player, "The mole brings you " .. n .. " " .. string.lower(f.thing) .. ".")
381 pcall(function()
382 me:say("*proud snuffle*")
383 end)
384 end
385 end)
386end
387
388-- ---- the hoard ----
389
390-- what's still in hoards players' pockets were too full for, by run
391local leftover: { [string]: { { thing: string, n: number } } } = {}
392
393local function say(finds): string
394 local parts = {}
395 for _, f in finds do
396 table.insert(parts, f.n .. " " .. string.lower(f.thing))
397 end
398 return table.concat(parts, ", ")
399end
400
401local function rummage(player, r: Run)
402 local k = key(r.at)
403 local finds = leftover[k] or roll(PRIZES, knobs.prizes)
404 if #finds == 0 then
405 finds = { { thing = "Seeds", n = 3 } }
406 end
407 local got, still = {}, {}
408 for _, f in finds do
409 local n = world.give(player, f.thing, f.n)
410 if n > 0 then
411 table.insert(got, { thing = f.thing, n = n })
412 end
413 if n < f.n then
414 table.insert(still, { thing = f.thing, n = f.n - n })
415 end
416 end
417 if #got > 0 then
418 ui.ask(player, { title = "The mole hoard", text = "You found " .. say(got) .. "." }, function() end)
419 end
420 if #still > 0 then
421 leftover[k] = still
422 tell(player, "Your pockets are full.")
423 return
424 end
425 leftover[k] = nil
426 r.hoard = false
427 save()
428 world.reward(player, HOARD_XP)
429 sound.play("sparkle", { at = r.at, caption = "Treasure in the mole hoard" })
430 tell(player, "You've found the moles' hoard.")
431end
432
433world.drops(HILL, { gives = "Dirt" })
434
435-- the run whose den's nest is at at, if it's there
436local function nest_at(at: vector): Run?
437 local i = den_near(at, 3)
438 local r = i and runs[i]
439 if r and key(r.at) == key(at) then
440 return r
441 end
442 return nil
443end
444
445world.on("block_used", function(player, at: vector, block: string)
446 local r = if block == HOARD then nest_at(at) else nil
447 if not r then
448 return
449 end
450 if world.pockets(player) == nil then
451 tell(player, "Mole hoards hold prizes for survival.")
452 elseif r.hoard then
453 rummage(player, r)
454 else
455 tell(player, "Just old hay now.")
456 end
457end)
458
459world.on("block_broken", function(player, at: vector, block: string)
460 local r = if block == HOARD then nest_at(at) else nil
461 if r and r.hoard and world.pockets(player) ~= nil then
462 rummage(player, r)
463 r.hoard = false
464 leftover[key(r.at)] = nil
465 save()
466 end
467end)
468
469-- ---- watching players ----
470
471local told: { [string]: boolean } = {}
472local turn = 0
473world.every(0.1, function()
474 if #work > 0 then
475 step()
476 return
477 end
478 turn += 1
479 -- a word to whoever comes on a run's hills or its den, and moles in the
480 -- dens somebody's near
481 if turn % 10 == 0 then
482 local live: { [number]: number } = {}
483 for id, i in den_of do
484 live[i] = (live[i] or 0) + 1
485 end
486 den_of = {}
487 for i, r in runs do
488 if r.built then
489 local near = nil
490 for _, p in world.players() do
491 if vector.magnitude(p.position - r.at) < 40 then
492 near = p
493 end
494 local k = p.name .. "/" .. i
495 if not told[k] then
496 for _, h in r.hills do
497 if flat(p.position, h) < 4 and math.abs(p.position.y - h.y) < 4 then
498 told[k] = true
499 tell(p, "Fresh molehills. There's a mole run under here.")
500 break
501 end
502 end
503 end
504 end
505 if near and (live[i] or 0) == 0 then
506 for n = 1, 2 do
507 pcall(world.spawn, Mole, r.at + v(if n == 1 then 1.5 else -0.5, 0.05, 0.5))
508 end
509 end
510 end
511 end
512 end
513 if turn % 30 ~= 0 then
514 return
515 end
516 local players = world.players()
517 if #players == 0 or #runs >= MOST then
518 return
519 end
520 local p = players[(turn // 30) % #players + 1]
521 local pos = p.position
522 -- the Long Dusk has finds of its own
523 if world.place(pos) == "dusk" then
524 return
525 end
526 -- only out on the grass, well away from the Green
527 if world.top(pos.x, pos.z) > pos.y + 1 then
528 return
529 end
530 for _, name in { "green", "start" } do
531 local at = world.spot(name)
532 if at and flat(pos, at) < 90 then
533 return
534 end
535 end
536 if not far_from_others(pos, knobs.apart) then
537 return
538 end
539 local den, hills = nil, nil
540 for _ = 1, 6 do
541 den, hills = lay_out(p)
542 if den then
543 break
544 end
545 end
546 if den and hills then
547 local r: Run = { at = den, hills = hills, built = false, hoard = true, cells = run_cells(den, hills) }
548 table.insert(runs, r)
549 digging = r
550 plan(r)
551 save()
552 end
553end)
554
555-- a run left half dug (the world stopped mid-way) is dug on
556for _, r in runs do
557 if not r.built then
558 digging = r
559 plan(r)
560 break
561 end
562end
563
564world.on("player_left", function(p)
565 shown[p.id] = nil
566end)