Long ago the Gnome Gardeners' people dug into the hills. Walk far from the Green and you'll come upon their diggings in a steep hillside: a timbered tunnel with old cart tracks, a slope down into the rock, a forge room, a store room and a long dark drift beyond. Bring a light. Burnt-out lanterns light again with a piece of coal, and gnome crates hold what the diggers left behind: tools, ore, ingots, seeds, a brass compass for collectors, and torn maps that lead to a crate buried out on the land.
| 1 | --!strict |
| 2 | -- Gnome Diggings: long ago the Gnome Gardeners' people dug into the hills. |
| 3 | -- When a player comes near a steep hillside far from the Green, and far |
| 4 | -- from any other diggings, a timbered tunnel opens in it: a level gallery |
| 5 | -- with old cart tracks, a slope down into the rock, a hall with a forge |
| 6 | -- room and a store room off it, and a long dark drift beyond. Where it's |
| 7 | -- dug is kept with @potluck/store, so each is dug once. |
| 8 | -- |
| 9 | -- Burnt-out lanterns hang along the way; use one with a piece of coal and |
| 10 | -- it lights again. Gnome crates hold what the diggers left (PRIZES below, |
| 11 | -- by room), better the deeper in: tools, ore, ingots, seeds, the diggers' |
| 12 | -- own pick (an item of this creation's, world 0.25, that digs as a copper |
| 13 | -- pickaxe and never wears out), a brass compass whose needle points to the |
| 14 | -- nearest diggings, and torn maps. Use a torn map on nothing to see where |
| 15 | -- its cross is; walk there and a crate is buried under your feet. The |
| 16 | -- compass, a gnome's teapot and a carved gnome are relics (world 0.26): |
| 17 | -- the Curator on the Green collects them and tells their stories. |
| 18 | -- |
| 19 | -- Only natural ground is dug: a hillside with anything built in it, or |
| 20 | -- water, is left alone. |
| 21 | |
| 22 | local world = require("@potluck/world@0.33") |
| 23 | local tweak = require("@potluck/tweak") |
| 24 | local store = require("@potluck/store") |
| 25 | local ui = require("@potluck/ui@0.2") |
| 26 | local sound = require("@potluck/sound@0.2") |
| 27 | |
| 28 | local knobs = tweak { |
| 29 | apart = { 300, min = 120, max = 1000, label = "How far apart diggings are", order = 1 }, |
| 30 | away = { 90, min = 40, max = 300, label = "How far from the Green the nearest is", order = 2 }, |
| 31 | prizes = { 1, min = 0.5, max = 3, label = "How generous the crates are", order = 3 }, |
| 32 | maps = { true, label = "Torn maps lead to buried crates", order = 4 }, |
| 33 | } |
| 34 | |
| 35 | local CRATE = "Gnome crate" |
| 36 | local DARK = "Burnt-out lantern" |
| 37 | local SEA = 19 |
| 38 | local MOST = 64 -- diggings a world keeps track of |
| 39 | |
| 40 | -- the four ways a tunnel can run into a hill: east, south, west, north |
| 41 | local DIRS = { vector.create(1, 0, 0), vector.create(0, 0, 1), vector.create(-1, 0, 0), vector.create(0, 0, -1) } |
| 42 | |
| 43 | -- what a tunnel may be dug through |
| 44 | local NATURAL: { [string]: boolean } = { |
| 45 | Stone = true, Dirt = true, Grass = true, Gravel = true, Sand = true, Snow = true, Slate = true, Sandstone = true, |
| 46 | ["Coal ore"] = true, ["Copper ore"] = true, ["Iron ore"] = true, ["Gold ore"] = true, ["Crystal ore"] = true, ["Moonsilver ore"] = true, |
| 47 | Glowcap = true, ["Tall grass"] = true, Fern = true, Bush = true, ["Dry bush"] = true, |
| 48 | ["Red flower"] = true, ["Blue flower"] = true, ["White flower"] = true, ["Violet flower"] = true, |
| 49 | } |
| 50 | |
| 51 | -- ---- what's in the crates ---- |
| 52 | |
| 53 | -- A prize: the thing, its chance in a crate, and from least to most of it. |
| 54 | type Prize = { thing: string, chance: number, least: number, most: number } |
| 55 | local PRIZES: { [string]: { Prize } } = { |
| 56 | -- the store room: what a digger lived on |
| 57 | store = { |
| 58 | { thing = "Seeds", chance = 1, least = 3, most = 8 }, |
| 59 | { thing = "Coal", chance = 0.8, least = 2, most = 6 }, |
| 60 | { thing = "Torch", chance = 0.7, least = 2, most = 6 }, |
| 61 | { thing = "Bread", chance = 0.4, least = 1, most = 2 }, |
| 62 | { thing = "Potatoes", chance = 0.3, least = 1, most = 3 }, |
| 63 | { thing = "Raw copper", chance = 0.5, least = 1, most = 4 }, |
| 64 | { thing = "Raw iron", chance = 0.35, least = 1, most = 3 }, |
| 65 | { thing = "Torn map", chance = 0.5, least = 1, most = 1 }, |
| 66 | { thing = "Gnome's teapot", chance = 0.25, least = 1, most = 1 }, |
| 67 | }, |
| 68 | -- the forge: metal and tools |
| 69 | forge = { |
| 70 | { thing = "Copper ingot", chance = 0.8, least = 1, most = 4 }, |
| 71 | { thing = "Iron ingot", chance = 0.6, least = 1, most = 3 }, |
| 72 | { thing = "Copper pickaxe", chance = 0.35, least = 1, most = 1 }, |
| 73 | { thing = "Hammer", chance = 0.3, least = 1, most = 1 }, |
| 74 | { thing = "Bucket", chance = 0.3, least = 1, most = 1 }, |
| 75 | { thing = "Iron pickaxe", chance = 0.12, least = 1, most = 1 }, |
| 76 | { thing = "Gnome's pick", chance = 0.2, least = 1, most = 1 }, |
| 77 | { thing = "Carved gnome", chance = 0.15, least = 1, most = 1 }, |
| 78 | }, |
| 79 | -- the end of the drift, deepest in: the best of it |
| 80 | deep = { |
| 81 | { thing = "Brass compass", chance = 1, least = 1, most = 1 }, |
| 82 | { thing = "Gold ingot", chance = 0.7, least = 1, most = 3 }, |
| 83 | { thing = "Crystal", chance = 0.6, least = 1, most = 3 }, |
| 84 | { thing = "Iron sword", chance = 0.3, least = 1, most = 1 }, |
| 85 | { thing = "Gnome's pick", chance = 0.3, least = 1, most = 1 }, |
| 86 | { thing = "Moonsilver ingot", chance = 0.12, least = 1, most = 1 }, |
| 87 | { thing = "Torn map", chance = 0.6, least = 1, most = 1 }, |
| 88 | { thing = "Carved gnome", chance = 0.35, least = 1, most = 1 }, |
| 89 | { thing = "Gnome music box", chance = 0.25, least = 1, most = 1 }, |
| 90 | }, |
| 91 | -- buried where a torn map's cross is |
| 92 | buried = { |
| 93 | { thing = "Gold ingot", chance = 0.8, least = 2, most = 5 }, |
| 94 | { thing = "Iron ingot", chance = 0.7, least = 2, most = 5 }, |
| 95 | { thing = "Crystal", chance = 0.5, least = 1, most = 3 }, |
| 96 | { thing = "Moonsilver ingot", chance = 0.25, least = 1, most = 2 }, |
| 97 | { thing = "Brass compass", chance = 0.3, least = 1, most = 1 }, |
| 98 | { thing = "Gnome music box", chance = 0.2, least = 1, most = 1 }, |
| 99 | { thing = "Torn map", chance = 0.35, least = 1, most = 1 }, |
| 100 | }, |
| 101 | } |
| 102 | -- XP for opening a crate, by where it was |
| 103 | local XP: { [string]: number } = { store = 3, forge = 5, deep = 12, buried = 10 } |
| 104 | |
| 105 | -- ---- what's kept ---- |
| 106 | |
| 107 | -- Each diggings: its mouth (the ground block at the foot of the hill), |
| 108 | -- which way it runs in (DIRS) and whether it's all dug yet. |
| 109 | type Site = { at: vector, dir: number, built: boolean } |
| 110 | local sites: { Site } = store.get("sites") or {} |
| 111 | -- crates this creation put down that are still shut, by place: which |
| 112 | -- PRIZES they hold |
| 113 | local crates: { [string]: string } = store.get("crates") or {} |
| 114 | -- each player's torn map, by their name: where its cross is |
| 115 | local crosses: { [string]: vector } = store.get("crosses") or {} |
| 116 | |
| 117 | local function save() |
| 118 | store.set("sites", sites) |
| 119 | store.set("crates", crates) |
| 120 | store.set("crosses", crosses) |
| 121 | end |
| 122 | |
| 123 | local function key(at: vector): string |
| 124 | return string.format("%d,%d,%d", math.floor(at.x), math.floor(at.y), math.floor(at.z)) |
| 125 | end |
| 126 | |
| 127 | -- Whether a thing is in this world: rails, furnaces and anvils come from |
| 128 | -- other creations. |
| 129 | local here: { [string]: boolean } = {} |
| 130 | local function exists(name: string): boolean |
| 131 | if here[name] == nil then |
| 132 | here[name] = (pcall(world.kind, name)) |
| 133 | end |
| 134 | return here[name] == true |
| 135 | end |
| 136 | |
| 137 | -- the same pattern every time, for a place |
| 138 | local function hash(x: number, z: number, n: number): number |
| 139 | local h = (math.floor(x) * 73856093 + math.floor(z) * 19349663 + n * 83492791) % 1000003 |
| 140 | return (h * 7919 % 1000003) / 1000003 |
| 141 | end |
| 142 | |
| 143 | -- one line above the hotbar for a few seconds |
| 144 | local shown: { [number]: number } = {} |
| 145 | local function tell(player, text: string) |
| 146 | ui.show(player, "diggings", text, { at = "bottom" }) |
| 147 | local mine = (shown[player.id] or 0) + 1 |
| 148 | shown[player.id] = mine |
| 149 | world.after(4, function() |
| 150 | if shown[player.id] == mine then |
| 151 | shown[player.id] = nil |
| 152 | ui.hide(player, "diggings") |
| 153 | end |
| 154 | end) |
| 155 | end |
| 156 | |
| 157 | -- "north-east", from a step across the ground (north is -z) |
| 158 | local function way(dx: number, dz: number): string |
| 159 | local a = math.deg(math.atan2(dx, -dz)) % 360 |
| 160 | local names = { "north", "north-east", "east", "south-east", "south", "south-west", "west", "north-west" } |
| 161 | return names[math.floor((a + 22.5) / 45) % 8 + 1] |
| 162 | end |
| 163 | |
| 164 | -- ---- a place in a diggings ---- |
| 165 | |
| 166 | -- by how far across (u, to the right going in), up (h) and in (v) |
| 167 | local function spot(s: Site, u: number, h: number, v: number): vector |
| 168 | local f = DIRS[s.dir] |
| 169 | local r = vector.create(-f.z, 0, f.x) |
| 170 | return s.at + r * u + f * v + vector.create(0, h, 0) |
| 171 | end |
| 172 | |
| 173 | -- The floor's height going in: level for the gallery, then down a block |
| 174 | -- every two to the hall, eight below the mouth. |
| 175 | local GALLERY, SLOPE_TO, HALL_TO, DRIFT_TO = 10, 26, 34, 52 |
| 176 | local function floor_at(v: number): number |
| 177 | if v <= GALLERY then |
| 178 | return 0 |
| 179 | end |
| 180 | return -math.min(math.floor((v - GALLERY + 1) / 2), 8) |
| 181 | end |
| 182 | |
| 183 | -- what grows on the ground rather than being it |
| 184 | local PLANT: { [string]: boolean } = { |
| 185 | Glowcap = true, ["Tall grass"] = true, Fern = true, Bush = true, ["Dry bush"] = true, |
| 186 | ["Red flower"] = true, ["Blue flower"] = true, ["White flower"] = true, ["Violet flower"] = true, |
| 187 | } |
| 188 | |
| 189 | -- The ground at a column, under trees and plants (or the water's top). |
| 190 | local function ground(x: number, z: number): number |
| 191 | local top = world.top(x, z) |
| 192 | for y = top, top - 16, -1 do |
| 193 | local b = world.block(vector.create(x, y, z)) |
| 194 | if b == "Water" or (NATURAL[b] and not PLANT[b]) then |
| 195 | return y |
| 196 | end |
| 197 | end |
| 198 | return top |
| 199 | end |
| 200 | |
| 201 | -- ---- finding a hillside ---- |
| 202 | |
| 203 | -- Whether a diggings can go here, running in `dir`: a steep hill ahead, |
| 204 | -- and only natural rock where it'll be dug (a few cave holes are fine). |
| 205 | local function fits(at: vector, dir: number): boolean |
| 206 | local s: Site = { at = at, dir = dir, built = false } |
| 207 | local f = DIRS[dir] |
| 208 | local rise = ground(at.x + f.x * 10, at.z + f.z * 10) - at.y |
| 209 | local beyond = ground(at.x + f.x * 20, at.z + f.z * 20) - at.y |
| 210 | if rise < 7 or beyond < 5 then |
| 211 | return false |
| 212 | end |
| 213 | local holes = 0 |
| 214 | local checks = { |
| 215 | { 0, 2, 7 }, { 0, 2, 10 }, { 0, -1, 16 }, { 0, -4, 22 }, { 0, -6, 28 }, { -3, -6, 30 }, { 3, -6, 30 }, |
| 216 | { -8, -6, 30 }, { 8, -6, 30 }, { 0, -6, 40 }, { 0, -6, DRIFT_TO }, { 0, 0, 36 }, |
| 217 | } |
| 218 | for _, c in checks do |
| 219 | local b = world.block(spot(s, c[1], c[2], c[3])) |
| 220 | if b == "Air" then |
| 221 | holes += 1 |
| 222 | elseif not NATURAL[b] then |
| 223 | return false |
| 224 | end |
| 225 | end |
| 226 | return holes <= 3 |
| 227 | end |
| 228 | |
| 229 | -- A foot of a hill near a player, or nil. |
| 230 | local function look_near(p: vector): Site? |
| 231 | for _ = 1, 6 do |
| 232 | local a = world.random() * math.pi * 2 |
| 233 | local d = world.random(24, 56) |
| 234 | local x, z = math.floor(p.x + math.cos(a) * d), math.floor(p.z + math.sin(a) * d) |
| 235 | local y = ground(x, z) |
| 236 | local b = world.block(vector.create(x, y, z)) |
| 237 | if y > SEA + 1 and NATURAL[b] and world.block(vector.create(x, y + 1, z)) ~= "Water" then |
| 238 | local first = math.floor(world.random(1, 4.999)) |
| 239 | for i = 0, 3 do |
| 240 | local dir = (first + i - 1) % 4 + 1 |
| 241 | if fits(vector.create(x, y, z), dir) then |
| 242 | return { at = vector.create(x, y, z), dir = dir, built = false } |
| 243 | end |
| 244 | end |
| 245 | end |
| 246 | end |
| 247 | return nil |
| 248 | end |
| 249 | |
| 250 | local function far_from_others(p: vector, apart: number): boolean |
| 251 | for _, s in sites do |
| 252 | if vector.magnitude(vector.create(p.x - s.at.x, 0, p.z - s.at.z)) < apart then |
| 253 | return false |
| 254 | end |
| 255 | end |
| 256 | return true |
| 257 | end |
| 258 | |
| 259 | -- ---- digging one ---- |
| 260 | |
| 261 | -- what's left to put down, a few hundred a tick: { place, block, facing } |
| 262 | local work: { { any } } = {} |
| 263 | local done = 0 -- how many of work are down |
| 264 | local digging: Site? = nil |
| 265 | |
| 266 | local function put(s: Site, u: number, h: number, v: number, name: string, facing: string?) |
| 267 | table.insert(work, { spot(s, u, h, v), name, facing }) |
| 268 | end |
| 269 | |
| 270 | -- air, but only where it's natural ground (a cave stays a cave) |
| 271 | local function carve(s: Site, u: number, h: number, v: number) |
| 272 | table.insert(work, { spot(s, u, h, v), "" }) |
| 273 | end |
| 274 | |
| 275 | -- a crate that holds a room's prizes |
| 276 | local function crate(s: Site, u: number, h: number, v: number, room: string) |
| 277 | local at = spot(s, u, h, v) |
| 278 | table.insert(work, { at, CRATE }) |
| 279 | crates[key(at)] = room |
| 280 | end |
| 281 | |
| 282 | -- Posts on each side, a beam across, and now and then a lantern under it. |
| 283 | local function timbers(s: Site, v: number, half: number, tall: number, lantern: boolean) |
| 284 | local y = floor_at(v) |
| 285 | for _, u in { -half, half } do |
| 286 | for h = 1, tall do |
| 287 | put(s, u, y + h, v, "Oak log") |
| 288 | end |
| 289 | end |
| 290 | for u = -half, half do |
| 291 | put(s, u, y + tall + 1, v, "Planks") |
| 292 | end |
| 293 | if lantern then |
| 294 | put(s, 0, y + tall, v, DARK) |
| 295 | end |
| 296 | end |
| 297 | |
| 298 | -- a room's air, from u0 to u1 and v0 to v1, `tall` high over floor y |
| 299 | local function room(s: Site, u0: number, u1: number, v0: number, v1: number, y: number, tall: number) |
| 300 | for v = v0, v1 do |
| 301 | for u = u0, u1 do |
| 302 | for h = 1, tall do |
| 303 | carve(s, u, y + h, v) |
| 304 | end |
| 305 | end |
| 306 | end |
| 307 | end |
| 308 | |
| 309 | -- Lays out everything one diggings is, to be put down a tick at a time. |
| 310 | local function plan(s: Site) |
| 311 | -- the mouth: a frame of logs in the hillside |
| 312 | room(s, -2, 2, 0, 0, 0, 4) |
| 313 | for _, u in { -2, 2 } do |
| 314 | for h = 1, 4 do |
| 315 | put(s, u, h, 0, "Oak log") |
| 316 | end |
| 317 | end |
| 318 | for u = -2, 2 do |
| 319 | put(s, u, 5, 0, "Planks") |
| 320 | end |
| 321 | -- the gallery and the slope: five wide and four high, timbered every four |
| 322 | for v = 1, SLOPE_TO do |
| 323 | local y = floor_at(v) |
| 324 | room(s, -2, 2, v, v, y, 4) |
| 325 | -- a boardwalk over any cave it crosses |
| 326 | for u = -1, 1 do |
| 327 | table.insert(work, { spot(s, u, y, v), "?Planks" }) |
| 328 | end |
| 329 | if v % 4 == 0 then |
| 330 | timbers(s, v, 2, 4, v % 8 == 0) |
| 331 | end |
| 332 | -- old cart tracks along the gallery, a few rails gone |
| 333 | local along = if s.dir % 2 == 1 then "east" else "north" |
| 334 | if v <= GALLERY and exists("Rail") and hash(spot(s, 0, 0, v).x, spot(s, 0, 0, v).z, 3) > 0.25 then |
| 335 | put(s, 0, 1, v, "Rail", along) |
| 336 | end |
| 337 | end |
| 338 | -- the hall: nine wide and five high, with a lantern |
| 339 | local y = floor_at(SLOPE_TO + 1) |
| 340 | room(s, -4, 4, SLOPE_TO + 1, HALL_TO, y, 5) |
| 341 | for _, c in { { -4, SLOPE_TO + 1 }, { 4, SLOPE_TO + 1 }, { -4, HALL_TO }, { 4, HALL_TO } } do |
| 342 | for h = 1, 5 do |
| 343 | put(s, c[1], y + h, c[2], "Oak log") |
| 344 | end |
| 345 | end |
| 346 | for u = -4, 4 do |
| 347 | put(s, u, y + 6, SLOPE_TO + 4, "Planks") |
| 348 | end |
| 349 | put(s, 0, y + 5, SLOPE_TO + 4, DARK) |
| 350 | put(s, 3, y + 1, HALL_TO - 1, "Barrel") |
| 351 | put(s, -3, y + 1, SLOPE_TO + 2, "Gravel") |
| 352 | put(s, -3, y + 2, SLOPE_TO + 2, "Gravel") |
| 353 | -- the forge, off to the left |
| 354 | local mid = SLOPE_TO + 4 |
| 355 | room(s, -5, -5, mid - 1, mid + 1, y, 3) |
| 356 | room(s, -11, -6, mid - 3, mid + 3, y, 4) |
| 357 | put(s, -11, y + 1, mid, if exists("Furnace") then "Furnace" else "Cobblestone") |
| 358 | put(s, -11, y + 1, mid - 1, "Cobblestone") |
| 359 | put(s, -11, y + 1, mid + 1, "Cobblestone") |
| 360 | put(s, -9, y + 1, mid + 2, if exists("Anvil") then "Anvil" else "Cobblestone") |
| 361 | put(s, -7, y + 1, mid - 3, "Table") |
| 362 | put(s, -6, y + 1, mid - 3, "Stool") |
| 363 | put(s, -8, y + 4, mid, DARK) |
| 364 | crate(s, -11, y + 1, mid - 3, "forge") |
| 365 | crate(s, -11, y + 1, mid + 3, "forge") |
| 366 | -- the store room, off to the right |
| 367 | room(s, 5, 5, mid - 1, mid + 1, y, 3) |
| 368 | room(s, 6, 11, mid - 3, mid + 3, y, 4) |
| 369 | for i, v in { mid - 2, mid, mid + 2 } do |
| 370 | crate(s, 11, y + 1, v, "store") |
| 371 | put(s, 11, y + 1, v + 1, if i == 2 then "Barrel" else "Crate") |
| 372 | end |
| 373 | put(s, 7, y + 1, mid + 3, "Bookshelf") |
| 374 | put(s, 8, y + 1, mid + 3, "Barrel") |
| 375 | put(s, 6, y + 1, mid - 3, "Clay pot") |
| 376 | put(s, 8, y + 4, mid, DARK) |
| 377 | -- the drift: three wide, three high, dark all the way to its end |
| 378 | for v = HALL_TO + 1, DRIFT_TO do |
| 379 | room(s, -1, 1, v, v, y, 3) |
| 380 | for u = -1, 1 do |
| 381 | table.insert(work, { spot(s, u, y, v), "?Planks" }) |
| 382 | end |
| 383 | if v % 5 == 0 then |
| 384 | timbers(s, v, 1, 3, false) |
| 385 | end |
| 386 | -- a fall of rock to climb over |
| 387 | if v == HALL_TO + 9 then |
| 388 | put(s, -1, y + 1, v, "Gravel") |
| 389 | put(s, 1, y + 1, v, "Gravel") |
| 390 | end |
| 391 | end |
| 392 | room(s, -2, 2, DRIFT_TO - 3, DRIFT_TO, y, 3) |
| 393 | crate(s, 0, y + 1, DRIFT_TO, "deep") |
| 394 | put(s, -2, y + 1, DRIFT_TO, "Crate") |
| 395 | put(s, 2, y + 1, DRIFT_TO, "Barrel") |
| 396 | end |
| 397 | |
| 398 | -- Puts down what's planned, a few hundred blocks a tick. |
| 399 | local function step() |
| 400 | local last = math.min(done + 250, #work) |
| 401 | for i = done + 1, last do |
| 402 | local w = work[i] |
| 403 | local at, name, facing = w[1] :: vector, w[2] :: string, w[3] :: string? |
| 404 | local b = world.block(at) |
| 405 | if name == "" then |
| 406 | if NATURAL[b] then |
| 407 | world.set_block(at, "Air") |
| 408 | end |
| 409 | elseif string.sub(name, 1, 1) == "?" then |
| 410 | -- only over a hole |
| 411 | if b == "Air" or b == "Water" then |
| 412 | world.set_block(at, string.sub(name, 2)) |
| 413 | end |
| 414 | elseif NATURAL[b] or b == "Air" then |
| 415 | world.set_block(at, name, facing) |
| 416 | end |
| 417 | end |
| 418 | done = last |
| 419 | if done >= #work then |
| 420 | work, done = {}, 0 |
| 421 | local d = digging |
| 422 | if d then |
| 423 | d.built = true |
| 424 | digging = nil |
| 425 | save() |
| 426 | end |
| 427 | end |
| 428 | end |
| 429 | |
| 430 | -- ---- watching players ---- |
| 431 | |
| 432 | local turn = 0 |
| 433 | world.every(0.1, function() |
| 434 | if #work > 0 then |
| 435 | step() |
| 436 | return |
| 437 | end |
| 438 | -- one player a few times a second, to keep the looking light |
| 439 | turn += 1 |
| 440 | if turn % 20 ~= 0 then |
| 441 | return |
| 442 | end |
| 443 | local players = world.players() |
| 444 | if #players == 0 or #sites >= MOST then |
| 445 | return |
| 446 | end |
| 447 | local p = players[(turn // 20) % #players + 1] |
| 448 | -- the Long Dusk has finds of its own |
| 449 | if world.place(p.position) == "dusk" then |
| 450 | return |
| 451 | end |
| 452 | for _, name in { "green", "start" } do |
| 453 | local at = world.spot(name) |
| 454 | if at and vector.magnitude(vector.create(p.position.x - at.x, 0, p.position.z - at.z)) < knobs.away then |
| 455 | return |
| 456 | end |
| 457 | end |
| 458 | if not far_from_others(p.position, knobs.apart) then |
| 459 | return |
| 460 | end |
| 461 | local s = look_near(p.position) |
| 462 | if s and far_from_others(s.at, knobs.apart) then |
| 463 | table.insert(sites, s) |
| 464 | digging = s |
| 465 | plan(s) |
| 466 | save() |
| 467 | end |
| 468 | end) |
| 469 | |
| 470 | -- a diggings left half dug (the world stopped mid-way) is dug on |
| 471 | for _, s in sites do |
| 472 | if not s.built then |
| 473 | digging = s |
| 474 | plan(s) |
| 475 | break |
| 476 | end |
| 477 | end |
| 478 | |
| 479 | -- ---- the crates ---- |
| 480 | |
| 481 | type Find = { thing: string, n: number } |
| 482 | -- what's still in crates players' pockets were too full for, by place |
| 483 | local left: { [string]: { Find } } = {} |
| 484 | |
| 485 | local function roll(room: string): { Find } |
| 486 | local out: { Find } = {} |
| 487 | for _, p in PRIZES[room] or {} do |
| 488 | if exists(p.thing) and world.random() < math.min(1, p.chance * knobs.prizes) then |
| 489 | local n = math.floor(world.random(p.least, p.most + 0.999) * knobs.prizes) |
| 490 | table.insert(out, { thing = p.thing, n = math.clamp(n, 1, 64) }) |
| 491 | end |
| 492 | end |
| 493 | if #out == 0 then |
| 494 | table.insert(out, { thing = "Coal", n = 3 }) |
| 495 | end |
| 496 | return out |
| 497 | end |
| 498 | |
| 499 | -- "You found 2 raw gold, 1 crystal and 3 glass.": as many as fit on a line |
| 500 | -- (60 letters), then "and more" |
| 501 | local function found(finds: { Find }): string |
| 502 | local parts = {} |
| 503 | for _, f in finds do |
| 504 | table.insert(parts, f.n .. " " .. string.lower(f.thing)) |
| 505 | end |
| 506 | for n = #parts, 1, -1 do |
| 507 | local line = if n < #parts |
| 508 | then table.concat(parts, ", ", 1, n) .. " and more" |
| 509 | elseif n == 1 then parts[1] |
| 510 | else table.concat(parts, ", ", 1, n - 1) .. " and " .. parts[n] |
| 511 | line = "You found " .. line .. "." |
| 512 | if #line <= 60 or n == 1 then |
| 513 | return line |
| 514 | end |
| 515 | end |
| 516 | return "You found something." |
| 517 | end |
| 518 | |
| 519 | local function open(player, at: vector) |
| 520 | local k = key(at) |
| 521 | local room = crates[k] |
| 522 | if not room then |
| 523 | tell(player, "It's empty.") |
| 524 | return |
| 525 | end |
| 526 | if world.pockets(player) == nil then |
| 527 | tell(player, "Gnome crates hold prizes for survival.") |
| 528 | return |
| 529 | end |
| 530 | local finds = left[k] or roll(room) |
| 531 | local got: { Find } = {} |
| 532 | local still: { Find } = {} |
| 533 | for _, f in finds do |
| 534 | local n = world.give(player, f.thing, f.n) |
| 535 | if n > 0 then |
| 536 | table.insert(got, { thing = f.thing, n = n }) |
| 537 | end |
| 538 | if n < f.n then |
| 539 | table.insert(still, { thing = f.thing, n = f.n - n }) |
| 540 | end |
| 541 | end |
| 542 | if #still > 0 then |
| 543 | left[k] = still |
| 544 | tell(player, "Your pockets are full.") |
| 545 | return |
| 546 | end |
| 547 | left[k] = nil |
| 548 | crates[k] = nil |
| 549 | save() |
| 550 | world.reward(player, XP[room] or 3) |
| 551 | world.set_block(at, "Air") |
| 552 | sound.play("sparkle", { at = at, caption = "A gnome crate opens" }) |
| 553 | tell(player, found(got)) |
| 554 | end |
| 555 | |
| 556 | -- a burnt-out lantern lights again with a piece of coal |
| 557 | local function relight(player, at: vector) |
| 558 | if world.pockets(player) ~= nil then |
| 559 | if world.has(player, "Coal") < 1 then |
| 560 | tell(player, "A piece of coal would light it again.") |
| 561 | return |
| 562 | end |
| 563 | world.take(player, "Coal", 1) |
| 564 | end |
| 565 | world.set_block(at, "Lantern") |
| 566 | sound.play("click", { at = at, caption = "A lantern's lit" }) |
| 567 | end |
| 568 | |
| 569 | world.on("block_used", function(player, at: vector, block: string) |
| 570 | if block == CRATE then |
| 571 | open(player, at) |
| 572 | elseif block == DARK then |
| 573 | relight(player, at) |
| 574 | end |
| 575 | end) |
| 576 | |
| 577 | -- a crate broken open gives nothing, and isn't a crate to put down again |
| 578 | world.drops(CRATE, { gives = false }) |
| 579 | world.on("block_broken", function(player, at: vector, block: string) |
| 580 | if block == CRATE and crates[key(at)] then |
| 581 | crates[key(at)] = nil |
| 582 | left[key(at)] = nil |
| 583 | save() |
| 584 | end |
| 585 | end) |
| 586 | |
| 587 | -- ---- torn maps and the compass ---- |
| 588 | |
| 589 | local function read_map(player) |
| 590 | if not knobs.maps then |
| 591 | tell(player, "The map's too torn to read.") |
| 592 | return |
| 593 | end |
| 594 | local cross = crosses[player.name] |
| 595 | if not cross then |
| 596 | -- a cross somewhere out on the land, a walk away |
| 597 | local a = world.random() * math.pi * 2 |
| 598 | local d = world.random(60, 140) |
| 599 | cross = vector.create(math.floor(player.position.x + math.cos(a) * d), 0, math.floor(player.position.z + math.sin(a) * d)) |
| 600 | crosses[player.name] = cross |
| 601 | save() |
| 602 | end |
| 603 | local dx, dz = cross.x - player.position.x, cross.z - player.position.z |
| 604 | local d = math.floor(math.sqrt(dx * dx + dz * dz)) |
| 605 | if d <= 6 then |
| 606 | tell(player, "The cross is right about here.") |
| 607 | else |
| 608 | tell(player, "The map shows a cross about " .. d .. " blocks to the " .. way(dx, dz) .. ".") |
| 609 | end |
| 610 | end |
| 611 | |
| 612 | local function read_compass(player) |
| 613 | local best: Site? = nil |
| 614 | local best_d = math.huge |
| 615 | for _, s in sites do |
| 616 | local d = vector.magnitude(vector.create(s.at.x - player.position.x, 0, s.at.z - player.position.z)) |
| 617 | if d < best_d then |
| 618 | best, best_d = s, d |
| 619 | end |
| 620 | end |
| 621 | if not best then |
| 622 | tell(player, "The needle spins. There are no diggings yet.") |
| 623 | elseif best_d < 12 then |
| 624 | tell(player, "The needle points straight down. You're at the diggings.") |
| 625 | else |
| 626 | local b = best :: Site |
| 627 | tell(player, "The needle points " .. way(b.at.x - player.position.x, b.at.z - player.position.z) .. ", about " .. math.floor(best_d) .. " blocks.") |
| 628 | end |
| 629 | end |
| 630 | |
| 631 | world.on("item_used", function(player, item: string?, aim: vector) |
| 632 | if item == "Torn map" then |
| 633 | read_map(player) |
| 634 | elseif item == "Brass compass" then |
| 635 | read_compass(player) |
| 636 | end |
| 637 | end) |
| 638 | |
| 639 | -- At the cross, a crate is buried under their feet (and the map's used up). |
| 640 | world.every(1, function() |
| 641 | for _, p in world.players() do |
| 642 | local cross = crosses[p.name] |
| 643 | if cross then |
| 644 | local dx, dz = cross.x - p.position.x, cross.z - p.position.z |
| 645 | if dx * dx + dz * dz <= 16 then |
| 646 | local x, z = math.floor(p.position.x), math.floor(p.position.z) |
| 647 | local y = ground(x, z) |
| 648 | crosses[p.name] = nil |
| 649 | world.take(p, "Torn map", 1) |
| 650 | if world.block(vector.create(x, y + 1, z)) == "Water" or y <= SEA - 1 then |
| 651 | save() |
| 652 | tell(p, "The cross is in the water. The map's older than the shore.") |
| 653 | continue |
| 654 | end |
| 655 | local at = vector.create(x, y - 2, z) |
| 656 | world.set_block(vector.create(x, y - 1, z), "Dirt") |
| 657 | world.set_block(at, CRATE) |
| 658 | crates[key(at)] = "buried" |
| 659 | save() |
| 660 | sound.play("chime", { at = p.position, caption = "This is the place on the map" }) |
| 661 | tell(p, "This is the place on the map. Dig down!") |
| 662 | end |
| 663 | end |
| 664 | end |
| 665 | end) |
| 666 | |
| 667 | world.on("player_left", function(player) |
| 668 | shown[player.id] = nil |
| 669 | end) |