The sea keeps what it takes. Out over deep water, shipwrecks lie broken on the bottom and sunken ruins stand in the dark, each with a sunken chest of pearls, metal and old things for the Curator. A pirate map from a chest leads to treasure buried on the land: use it for the way, and dig where you're standing when you get there.
| 1 | --!strict |
| 2 | -- Sea wrecks: what the sea keeps. |
| 3 | -- |
| 4 | -- Round a player out over deep water, now and then a shipwreck settles on |
| 5 | -- the sea floor (a broken hull of planks with its mast stump, barrels and a |
| 6 | -- stern cabin) or a sunken ruin appears (a mossy platform with broken |
| 7 | -- columns and coral). Each holds a sunken chest: Interact to open it, in |
| 8 | -- survival, for pearls, metal and old things, some of them relics for the |
| 9 | -- Curator. A pirate map from a chest leads to treasure buried on the land: |
| 10 | -- use it on nothing to see the way, and at the cross a chest is buried |
| 11 | -- under your feet. Everything is kept with @potluck/store, so each site is |
| 12 | -- built once and its chest is opened once. |
| 13 | |
| 14 | local world = require("@potluck/world@0.34") |
| 15 | local tweak = require("@potluck/tweak") |
| 16 | local store = require("@potluck/store") |
| 17 | local ui = require("@potluck/ui@0.2") |
| 18 | local sound = require("@potluck/sound@0.2") |
| 19 | |
| 20 | local knobs = tweak { |
| 21 | apart = { 140, min = 40, max = 600, label = "How far apart wrecks and ruins are", order = 1 }, |
| 22 | prizes = { 1, min = 0.25, max = 3, label = "How much a chest holds", order = 2 }, |
| 23 | maps = { true, label = "Pirate maps lead to buried chests", order = 3 }, |
| 24 | } |
| 25 | |
| 26 | local CHEST = "Sunken chest" |
| 27 | local MOST = 64 |
| 28 | local SEA = 19 -- the sea's level; ground above SEA + 2 is land |
| 29 | local MIN_DEPTH, MAX_DEPTH = 6, 30 |
| 30 | |
| 31 | type Prize = { thing: string, chance: number, least: number, most: number } |
| 32 | local PRIZES: { [string]: { Prize } } = { |
| 33 | wreck = { |
| 34 | { thing = "Iron ingot", chance = 0.7, least = 1, most = 4 }, |
| 35 | { thing = "Copper ingot", chance = 0.7, least = 2, most = 5 }, |
| 36 | { thing = "Gold ingot", chance = 0.5, least = 1, most = 3 }, |
| 37 | { thing = "Coal", chance = 0.6, least = 2, most = 6 }, |
| 38 | { thing = "Bread", chance = 0.4, least = 1, most = 3 }, |
| 39 | { thing = "Pearl", chance = 0.5, least = 1, most = 3 }, |
| 40 | { thing = "Pirate map", chance = 0.6, least = 1, most = 1 }, |
| 41 | { thing = "Hammer", chance = 0.2, least = 1, most = 1 }, |
| 42 | { thing = "Ship's bell", chance = 0.15, least = 1, most = 1 }, |
| 43 | }, |
| 44 | ruin = { |
| 45 | { thing = "Pearl", chance = 0.9, least = 2, most = 5 }, |
| 46 | { thing = "Gold ingot", chance = 0.8, least = 2, most = 5 }, |
| 47 | { thing = "Crystal", chance = 0.7, least = 1, most = 3 }, |
| 48 | { thing = "Moonsilver ingot", chance = 0.25, least = 1, most = 2 }, |
| 49 | { thing = "Pirate map", chance = 0.4, least = 1, most = 1 }, |
| 50 | { thing = "Tide glass", chance = 0.3, least = 1, most = 1 }, |
| 51 | }, |
| 52 | buried = { |
| 53 | { thing = "Gold ingot", chance = 0.8, least = 2, most = 5 }, |
| 54 | { thing = "Iron ingot", chance = 0.7, least = 2, most = 5 }, |
| 55 | { thing = "Pearl", chance = 0.7, least = 2, most = 4 }, |
| 56 | { thing = "Crystal", chance = 0.5, least = 1, most = 3 }, |
| 57 | { thing = "Moonsilver ingot", chance = 0.2, least = 1, most = 2 }, |
| 58 | { thing = "Ship's bell", chance = 0.12, least = 1, most = 1 }, |
| 59 | { thing = "Pirate map", chance = 0.25, least = 1, most = 1 }, |
| 60 | }, |
| 61 | } |
| 62 | local XP: { [string]: number } = { wreck = 6, ruin = 10, buried = 10 } |
| 63 | |
| 64 | -- ---- what's kept ---- |
| 65 | |
| 66 | -- each site: its kind, where its floor is, which way it lies and whether it's built |
| 67 | type Site = { kind: string, at: vector, dir: number, built: boolean } |
| 68 | local sites: { Site } = store.get("sites") or {} |
| 69 | -- chests that are still shut, by place: which PRIZES they hold |
| 70 | local chests: { [string]: string } = store.get("chests") or {} |
| 71 | -- each player's map, by their name: where its cross is |
| 72 | local crosses: { [string]: vector } = store.get("crosses") or {} |
| 73 | |
| 74 | local function save() |
| 75 | store.set("sites", sites) |
| 76 | store.set("chests", chests) |
| 77 | store.set("crosses", crosses) |
| 78 | end |
| 79 | |
| 80 | local function key(at: vector): string |
| 81 | return string.format("%d,%d,%d", math.floor(at.x), math.floor(at.y), math.floor(at.z)) |
| 82 | end |
| 83 | |
| 84 | local here: { [string]: boolean } = {} |
| 85 | local function exists(name: string): boolean |
| 86 | if here[name] == nil then |
| 87 | here[name] = (pcall(world.kind, name)) |
| 88 | end |
| 89 | return here[name] == true |
| 90 | end |
| 91 | |
| 92 | local function hash(x: number, y: number, z: number): number |
| 93 | local h = (math.floor(x) * 73856093 + math.floor(y) * 19349663 + math.floor(z) * 83492791) % 1000003 |
| 94 | return (h * 7919 % 1000003) / 1000003 |
| 95 | end |
| 96 | |
| 97 | local shown: { [number]: number } = {} |
| 98 | local function tell(player, text: string) |
| 99 | ui.show(player, "sea-wrecks", text, { at = "bottom" }) |
| 100 | local mine = (shown[player.id] or 0) + 1 |
| 101 | shown[player.id] = mine |
| 102 | world.after(4, function() |
| 103 | if shown[player.id] == mine then |
| 104 | shown[player.id] = nil |
| 105 | ui.hide(player, "sea-wrecks") |
| 106 | end |
| 107 | end) |
| 108 | end |
| 109 | |
| 110 | local function way(dx: number, dz: number): string |
| 111 | local a = math.deg(math.atan2(dx, -dz)) % 360 |
| 112 | local names = { "north", "north-east", "east", "south-east", "south", "south-west", "west", "north-west" } |
| 113 | return names[math.floor((a + 22.5) / 45) % 8 + 1] |
| 114 | end |
| 115 | |
| 116 | -- ---- building, a few hundred blocks a tick ---- |
| 117 | |
| 118 | -- what a build may replace: the sea itself and what grows in it |
| 119 | local SOFT: { [string]: boolean } = { |
| 120 | Water = true, Kelp = true, ["Sea grass"] = true, ["Pink coral branch"] = true, ["Orange coral branch"] = true, |
| 121 | ["Purple coral branch"] = true, ["Yellow coral branch"] = true, Air = true, |
| 122 | } |
| 123 | |
| 124 | type Work = { at: vector, name: string } |
| 125 | local work: { Work } = {} |
| 126 | local done = 0 |
| 127 | |
| 128 | -- where a site's own cells go: `u` along its length, `v` across, `h` up |
| 129 | -- from the first block over the floor |
| 130 | local function cell(s: Site, u: number, v: number, h: number): vector |
| 131 | if s.dir == 0 then |
| 132 | return s.at + vector.create(u, h, v) |
| 133 | end |
| 134 | return s.at + vector.create(v, h, u) |
| 135 | end |
| 136 | |
| 137 | local function plan_wreck(s: Site, depth: number, out: { Work }) |
| 138 | local wood = ({ "Planks", "Pine planks", "Maple planks", "Mahogany planks", "Redwood planks" })[math.floor(hash(s.at.x, 1, s.at.z) * 5) + 1] |
| 139 | local log = if wood == "Planks" then "Oak log" elseif wood == "Pine planks" then "Pine log" elseif wood == "Maple planks" then "Maple log" elseif wood == "Mahogany planks" then "Mahogany log" else "Redwood log" |
| 140 | local function put(u: number, v: number, h: number, name: string) |
| 141 | table.insert(out, { at = cell(s, u, v, h), name = name }) |
| 142 | end |
| 143 | for u = 0, 8 do |
| 144 | local w = if u == 0 then 0 elseif u <= 2 then 1 else 2 |
| 145 | for v = -w, w do |
| 146 | local at = cell(s, u, v, 1) |
| 147 | -- holes in the bottom, more toward the bow |
| 148 | if hash(at.x, at.y, at.z) > (if u < 3 then 0.25 else 0.12) then |
| 149 | put(u, v, 1, wood) |
| 150 | end |
| 151 | if math.abs(v) == w then |
| 152 | for h = 2, 3 do |
| 153 | local c = cell(s, u, v, h) |
| 154 | if hash(c.x, c.y + h, c.z) > 0.28 + 0.2 * (h - 2) then |
| 155 | put(u, v, h, wood) |
| 156 | end |
| 157 | end |
| 158 | end |
| 159 | end |
| 160 | end |
| 161 | put(0, 0, 2, log) |
| 162 | put(0, 0, 3, log) |
| 163 | -- the stern cabin's wall |
| 164 | for v = -2, 2 do |
| 165 | local c = cell(s, 8, v, 2) |
| 166 | if hash(c.x, c.y, c.z) > 0.2 then |
| 167 | put(8, v, 2, wood) |
| 168 | end |
| 169 | if hash(c.x, c.y + 7, c.z) > 0.5 then |
| 170 | put(8, v, 3, wood) |
| 171 | end |
| 172 | end |
| 173 | -- the mast stump, never up through the surface |
| 174 | for h = 2, math.min(5, depth - 2) do |
| 175 | put(3, 0, h, log) |
| 176 | end |
| 177 | if hash(s.at.x, 2, s.at.z) > 0.4 and depth > 5 then |
| 178 | put(5, 1, 2, "Barrel") |
| 179 | end |
| 180 | put(5, -1, 2, "Cobblestone") |
| 181 | table.insert(out, { at = cell(s, 7, 0, 2), name = CHEST }) |
| 182 | end |
| 183 | |
| 184 | local function plan_ruin(s: Site, depth: number, out: { Work }) |
| 185 | local function put(u: number, v: number, h: number, name: string) |
| 186 | table.insert(out, { at = cell(s, u, v, h), name = name }) |
| 187 | end |
| 188 | for u = -3, 3 do |
| 189 | for v = -3, 3 do |
| 190 | local c = cell(s, u, v, 1) |
| 191 | local r = hash(c.x, c.y, c.z) |
| 192 | if r > 0.15 then |
| 193 | put(u, v, 1, if r > 0.6 then "Stone bricks" elseif r > 0.35 then "Mossy brick" else "Mossy cobblestone") |
| 194 | end |
| 195 | end |
| 196 | end |
| 197 | for _, uv in { { -3, -3 }, { 3, -3 }, { -3, 3 }, { 3, 3 } } do |
| 198 | local c = cell(s, uv[1], uv[2], 2) |
| 199 | local height = 1 + math.floor(hash(c.x, 5, c.z) * 4) |
| 200 | for h = 2, math.min(1 + height, depth - 2) do |
| 201 | put(uv[1], uv[2], h, "Column") |
| 202 | end |
| 203 | end |
| 204 | put(0, 0, 2, "Marble bricks") |
| 205 | put(0, 0, 3, CHEST) |
| 206 | table.insert(out, { at = cell(s, 0, 0, 3), name = CHEST }) |
| 207 | for _, uv in { { -2, 0 }, { 2, 0 }, { 0, -2 }, { 0, 2 }, { 1, 1 }, { -1, -1 } } do |
| 208 | local c = cell(s, uv[1], uv[2], 2) |
| 209 | local r = hash(c.x, c.y, c.z) |
| 210 | put(uv[1], uv[2], 2, if r > 0.75 then "Pink coral branch" elseif r > 0.5 then "Orange coral branch" elseif r > 0.25 then "Purple coral branch" else "Kelp") |
| 211 | end |
| 212 | end |
| 213 | |
| 214 | local function depth_at(x: number, z: number): (number, number) |
| 215 | local top = world.top(x, z) |
| 216 | local n = 0 |
| 217 | while n < MAX_DEPTH + 2 and world.block(vector.create(x, top + 1 + n, z)) == "Water" do |
| 218 | n += 1 |
| 219 | end |
| 220 | return top, n |
| 221 | end |
| 222 | |
| 223 | local function queue(s: Site) |
| 224 | local _, depth = depth_at(s.at.x, s.at.z) |
| 225 | if s.kind == "wreck" then |
| 226 | plan_wreck(s, math.max(depth, MIN_DEPTH), work) |
| 227 | else |
| 228 | plan_ruin(s, math.max(depth, MIN_DEPTH), work) |
| 229 | end |
| 230 | end |
| 231 | |
| 232 | local function step() |
| 233 | if #work == 0 then |
| 234 | return |
| 235 | end |
| 236 | local last = math.min(done + 250, #work) |
| 237 | for i = done + 1, last do |
| 238 | local w = work[i] |
| 239 | local b = world.block(w.at) |
| 240 | if w.name == CHEST then |
| 241 | local k = key(w.at) |
| 242 | if not chests[k] and SOFT[b] or b == "Sand" or b == "Gravel" then |
| 243 | world.set_block(w.at, CHEST) |
| 244 | local site = nil |
| 245 | for _, s in sites do |
| 246 | if vector.magnitude(s.at - w.at) < 8 then |
| 247 | site = s |
| 248 | end |
| 249 | end |
| 250 | chests[k] = if site then site.kind else "wreck" |
| 251 | end |
| 252 | elseif SOFT[b] or b == "Sand" or b == "Gravel" then |
| 253 | world.set_block(w.at, w.name) |
| 254 | end |
| 255 | end |
| 256 | done = last |
| 257 | if done >= #work then |
| 258 | work, done = {}, 0 |
| 259 | for _, s in sites do |
| 260 | s.built = true |
| 261 | end |
| 262 | save() |
| 263 | end |
| 264 | end |
| 265 | |
| 266 | -- ---- finding a place ---- |
| 267 | |
| 268 | -- Whether a site can go at this floor spot: deep enough, not too deep, a |
| 269 | -- level floor and clear water over it, and away from the others. |
| 270 | local function fits(x: number, z: number): vector? |
| 271 | local top, depth = depth_at(x, z) |
| 272 | if depth < MIN_DEPTH or depth > MAX_DEPTH then |
| 273 | return nil |
| 274 | end |
| 275 | for _, d in { { 5, 0 }, { -5, 0 }, { 0, 5 }, { 0, -5 } } do |
| 276 | local t, n = depth_at(x + d[1], z + d[2]) |
| 277 | if n < MIN_DEPTH - 1 or math.abs(t - top) > 2 then |
| 278 | return nil |
| 279 | end |
| 280 | end |
| 281 | local at = vector.create(math.floor(x), top, math.floor(z)) |
| 282 | for _, s in sites do |
| 283 | if vector.magnitude(vector.create(s.at.x - at.x, 0, s.at.z - at.z)) < knobs.apart then |
| 284 | return nil |
| 285 | end |
| 286 | end |
| 287 | return at |
| 288 | end |
| 289 | |
| 290 | local function add(kind: string, at: vector) |
| 291 | local s: Site = { kind = kind, at = at, dir = if hash(at.x, 9, at.z) > 0.5 then 1 else 0, built = false } |
| 292 | table.insert(sites, s) |
| 293 | save() |
| 294 | queue(s) |
| 295 | end |
| 296 | |
| 297 | world.every(8, function() |
| 298 | if #sites >= MOST or #work > 0 then |
| 299 | return |
| 300 | end |
| 301 | for _, p in world.players() do |
| 302 | -- the Long Dusk has places of its own |
| 303 | if world.place(p.position) == "dusk" or world.random() > 0.5 then |
| 304 | continue |
| 305 | end |
| 306 | for _ = 1, 4 do |
| 307 | local a = world.random(0, 2 * math.pi) |
| 308 | local r = world.random(14, 40) |
| 309 | local at = fits(p.position.x + math.cos(a) * r, p.position.z + math.sin(a) * r) |
| 310 | if at then |
| 311 | add(if world.random() < 0.6 then "wreck" else "ruin", at) |
| 312 | return |
| 313 | end |
| 314 | end |
| 315 | end |
| 316 | end) |
| 317 | |
| 318 | -- a site that's left unbuilt carries on after a restart; and a plan can be |
| 319 | -- left for it in the store (`plan`: { kind, at }), as the tests do |
| 320 | world.after(0.2, function() |
| 321 | for _, s in sites do |
| 322 | if not s.built then |
| 323 | queue(s) |
| 324 | end |
| 325 | end |
| 326 | end) |
| 327 | |
| 328 | world.every(1, function() |
| 329 | local plan = store.get("plan") |
| 330 | if plan and #work == 0 then |
| 331 | store.set("plan", nil) |
| 332 | add(plan.kind, plan.at) |
| 333 | end |
| 334 | step() |
| 335 | end) |
| 336 | world.every(0.1, step) |
| 337 | |
| 338 | -- ---- chests ---- |
| 339 | |
| 340 | type Find = { thing: string, n: number } |
| 341 | local left: { [string]: { Find } } = {} |
| 342 | |
| 343 | local function roll(kind: string): { Find } |
| 344 | local out: { Find } = {} |
| 345 | for _, p in PRIZES[kind] or PRIZES.wreck do |
| 346 | if exists(p.thing) and world.random() < math.min(1, p.chance * knobs.prizes) then |
| 347 | local n = math.floor(world.random(p.least, p.most + 0.999) * (if p.most > 1 then knobs.prizes else 1)) |
| 348 | table.insert(out, { thing = p.thing, n = math.clamp(n, 1, 64) }) |
| 349 | end |
| 350 | end |
| 351 | if #out == 0 then |
| 352 | table.insert(out, { thing = "Coal", n = 3 }) |
| 353 | end |
| 354 | return out |
| 355 | end |
| 356 | |
| 357 | -- "You found 2 pearls, 1 gold ingot and more.": as many as fit on a line |
| 358 | local function found(finds: { Find }): string |
| 359 | local parts = {} |
| 360 | for _, f in finds do |
| 361 | table.insert(parts, f.n .. " " .. string.lower(f.thing)) |
| 362 | end |
| 363 | for n = #parts, 1, -1 do |
| 364 | local line = if n < #parts |
| 365 | then table.concat(parts, ", ", 1, n) .. " and more" |
| 366 | elseif n == 1 then parts[1] |
| 367 | else table.concat(parts, ", ", 1, n - 1) .. " and " .. parts[n] |
| 368 | line = "You found " .. line .. "." |
| 369 | if #line <= 60 or n == 1 then |
| 370 | return line |
| 371 | end |
| 372 | end |
| 373 | return "You found something." |
| 374 | end |
| 375 | |
| 376 | local function open(player, at: vector) |
| 377 | local k = key(at) |
| 378 | local kind = chests[k] |
| 379 | if not kind then |
| 380 | tell(player, "It's empty.") |
| 381 | return |
| 382 | end |
| 383 | if world.pockets(player) == nil then |
| 384 | tell(player, "Sunken chests hold prizes for survival.") |
| 385 | return |
| 386 | end |
| 387 | local finds = left[k] or roll(kind) |
| 388 | local got: { Find } = {} |
| 389 | local still: { Find } = {} |
| 390 | for _, f in finds do |
| 391 | local n = world.give(player, f.thing, f.n) |
| 392 | if n > 0 then |
| 393 | table.insert(got, { thing = f.thing, n = n }) |
| 394 | end |
| 395 | if n < f.n then |
| 396 | table.insert(still, { thing = f.thing, n = f.n - n }) |
| 397 | end |
| 398 | end |
| 399 | if #still > 0 then |
| 400 | left[k] = still |
| 401 | tell(player, "Your pockets are full.") |
| 402 | return |
| 403 | end |
| 404 | left[k] = nil |
| 405 | chests[k] = nil |
| 406 | save() |
| 407 | world.reward(player, XP[kind] or 6) |
| 408 | world.set_block(at, "Water") |
| 409 | sound.play("sparkle", { at = at, caption = "A sunken chest opens" }) |
| 410 | tell(player, found(got)) |
| 411 | end |
| 412 | |
| 413 | world.drops(CHEST, { gives = false }) |
| 414 | world.on("block_broken", function(player, at: vector, block: string) |
| 415 | if block == CHEST and chests[key(at)] then |
| 416 | chests[key(at)] = nil |
| 417 | left[key(at)] = nil |
| 418 | save() |
| 419 | end |
| 420 | end) |
| 421 | |
| 422 | -- ---- pirate maps ---- |
| 423 | |
| 424 | local function read_map(player) |
| 425 | if not knobs.maps then |
| 426 | tell(player, "The map's gone blank.") |
| 427 | return |
| 428 | end |
| 429 | local cross = crosses[player.name] |
| 430 | if not cross then |
| 431 | -- somewhere on dry land, a walk away |
| 432 | for _ = 1, 30 do |
| 433 | local a = world.random() * math.pi * 2 |
| 434 | local d = world.random(70, 160) |
| 435 | local x = math.floor(player.position.x + math.cos(a) * d) |
| 436 | local z = math.floor(player.position.z + math.sin(a) * d) |
| 437 | if world.top(x, z) >= SEA + 3 then |
| 438 | cross = vector.create(x, 0, z) |
| 439 | break |
| 440 | end |
| 441 | end |
| 442 | if not cross then |
| 443 | tell(player, "The map shows only water here. Try it on the shore.") |
| 444 | return |
| 445 | end |
| 446 | crosses[player.name] = cross |
| 447 | save() |
| 448 | end |
| 449 | local dx, dz = cross.x - player.position.x, cross.z - player.position.z |
| 450 | local d = math.floor(math.sqrt(dx * dx + dz * dz)) |
| 451 | if d <= 6 then |
| 452 | tell(player, "The cross is right about here.") |
| 453 | else |
| 454 | tell(player, "The map shows a cross about " .. d .. " blocks to the " .. way(dx, dz) .. ".") |
| 455 | end |
| 456 | end |
| 457 | |
| 458 | world.on("item_used", function(player, item: string?, aim: vector) |
| 459 | if item == "Pirate map" then |
| 460 | read_map(player) |
| 461 | end |
| 462 | end) |
| 463 | |
| 464 | -- world.on("block_used", ...) for chests |
| 465 | world.on("block_used", function(player, at: vector, block: string) |
| 466 | if block == CHEST then |
| 467 | open(player, at) |
| 468 | end |
| 469 | end) |
| 470 | |
| 471 | -- At the cross, a chest is buried under their feet (and the map's used up). |
| 472 | world.every(1, function() |
| 473 | for _, p in world.players() do |
| 474 | local cross = crosses[p.name] |
| 475 | if cross then |
| 476 | local dx, dz = cross.x - p.position.x, cross.z - p.position.z |
| 477 | if dx * dx + dz * dz <= 16 then |
| 478 | local x, z = math.floor(p.position.x), math.floor(p.position.z) |
| 479 | local y = world.top(x, z) |
| 480 | crosses[p.name] = nil |
| 481 | world.take(p, "Pirate map", 1) |
| 482 | local at = vector.create(x, y - 2, z) |
| 483 | world.set_block(at, CHEST) |
| 484 | chests[key(at)] = "buried" |
| 485 | save() |
| 486 | sound.play("chime", { at = p.position, caption = "This is the place on the map" }) |
| 487 | tell(p, "This is the place on the map. Dig down!") |
| 488 | end |
| 489 | end |
| 490 | end |
| 491 | end) |
| 492 | |
| 493 | world.on("player_left", function(player) |
| 494 | shown[player.id] = nil |
| 495 | end) |