Ores for survival: coal, copper, iron, gold, crystal and moonsilver. Each breaks into what it holds once you have a good enough pickaxe, old chests left underground hold prizes, better the deeper they are, gravel turns up flint now and then, and deep stone a fossil shell, a relic for the Curator.
| 1 | --!strict |
| 2 | -- Mining, for survival. Each ore breaks into what it holds (world.drops), |
| 3 | -- once you hold a pickaxe of its level or better: wooden pickaxes are |
| 4 | -- level 1, stone 2, copper 3, iron 4 and moonsilver 5 (the tools creation |
| 5 | -- says so). So coal needs a wooden pickaxe, copper a stone one, iron a |
| 6 | -- copper one, and gold, crystal and moonsilver an iron one. ORES below |
| 7 | -- says what each gives and needs. |
| 8 | -- |
| 9 | -- Old chests are left in old workings underground. Use one and it gives |
| 10 | -- you its prizes and falls apart; the deeper it was, the better they are. |
| 11 | -- PRIZES below says what can be in one. |
| 12 | -- |
| 13 | -- Breaking gravel turns up flint now and then, for a flint and steel, and |
| 14 | -- breaking stone or slate deep down a fossil shell now and then: a relic |
| 15 | -- (an item of this creation's, world 0.26) that the Curator on the Green |
| 16 | -- collects. Old chests sometimes hold the Curator's old coins. |
| 17 | -- |
| 18 | -- Mining ore and opening old chests give XP (world.reward), the rarer the |
| 19 | -- more. |
| 20 | |
| 21 | local world = require("@potluck/world@0.26") |
| 22 | local tweak = require("@potluck/tweak") |
| 23 | local ui = require("@potluck/ui@0.2") |
| 24 | local sound = require("@potluck/sound@0.1") |
| 25 | |
| 26 | local knobs = tweak { |
| 27 | levels = { true, label = "Ores need the right pickaxe", order = 1 }, |
| 28 | more = { 1, min = 1, max = 3, label = "How much each ore gives", order = 2 }, |
| 29 | prizes = { 1, min = 0.5, max = 3, label = "How generous old chests are", order = 3 }, |
| 30 | flint = { 0.15, min = 0, max = 1, label = "Chance gravel turns up flint", order = 4 }, |
| 31 | fossils = { 0.004, min = 0, max = 0.1, label = "Chance deep stone turns up a fossil", order = 5 }, |
| 32 | } |
| 33 | |
| 34 | -- where fossils are: stone and slate below this height |
| 35 | local FOSSILS_BELOW = 0 |
| 36 | |
| 37 | type Ore = { gives: string, least: number, most: number, level: number, xp: number } |
| 38 | |
| 39 | -- What each ore gives, from least to most of it, the level of pickaxe it |
| 40 | -- needs, and the XP for mining it. |
| 41 | local ORES: { [string]: Ore } = { |
| 42 | ["Coal ore"] = { gives = "Coal", least = 1, most = 2, level = 1, xp = 1 }, |
| 43 | ["Copper ore"] = { gives = "Raw copper", least = 1, most = 3, level = 2, xp = 2 }, |
| 44 | ["Iron ore"] = { gives = "Raw iron", least = 1, most = 1, level = 3, xp = 3 }, |
| 45 | ["Gold ore"] = { gives = "Raw gold", least = 1, most = 1, level = 4, xp = 5 }, |
| 46 | ["Crystal ore"] = { gives = "Crystal", least = 1, most = 2, level = 4, xp = 4 }, |
| 47 | ["Moonsilver ore"] = { gives = "Raw moonsilver", least = 1, most = 1, level = 4, xp = 8 }, |
| 48 | } |
| 49 | |
| 50 | for name, o in ORES do |
| 51 | world.drops(name, { |
| 52 | gives = o.gives, |
| 53 | count = { math.floor(o.least * knobs.more), math.floor(o.most * knobs.more) }, |
| 54 | level = if knobs.levels then o.level else 0, |
| 55 | }) |
| 56 | end |
| 57 | |
| 58 | type Prize = { thing: string, least: number, most: number, chance: number, below: number? } |
| 59 | |
| 60 | -- What an old chest can hold: each prize is in a chest by its chance, |
| 61 | -- from least to most of it, but only in chests deeper than `below` (a |
| 62 | -- height; the sea is at 19). |
| 63 | local PRIZES: { Prize } = { |
| 64 | { thing = "Coal", least = 2, most = 6, chance = 0.6 }, |
| 65 | { thing = "Lantern", least = 1, most = 2, chance = 0.45 }, |
| 66 | { thing = "Candle", least = 1, most = 3, chance = 0.3 }, |
| 67 | { thing = "Carrots", least = 2, most = 5, chance = 0.3 }, |
| 68 | { thing = "Glowcap", least = 1, most = 3, chance = 0.25 }, |
| 69 | { thing = "Stone pickaxe", least = 1, most = 1, chance = 0.2 }, |
| 70 | { thing = "Stone sword", least = 1, most = 1, chance = 0.15 }, |
| 71 | { thing = "Flint", least = 1, most = 3, chance = 0.3 }, |
| 72 | { thing = "Bread", least = 1, most = 3, chance = 0.25 }, |
| 73 | { thing = "Flint and steel", least = 1, most = 1, chance = 0.1 }, |
| 74 | { thing = "Raw copper", least = 1, most = 4, chance = 0.5, below = 60 }, |
| 75 | { thing = "Copper pickaxe", least = 1, most = 1, chance = 0.15, below = 20 }, |
| 76 | { thing = "Raw iron", least = 1, most = 4, chance = 0.45, below = 20 }, |
| 77 | { thing = "Iron ingot", least = 1, most = 3, chance = 0.25, below = 0 }, |
| 78 | { thing = "Raw gold", least = 1, most = 3, chance = 0.35, below = -10 }, |
| 79 | { thing = "Iron pickaxe", least = 1, most = 1, chance = 0.12, below = -20 }, |
| 80 | { thing = "Iron sword", least = 1, most = 1, chance = 0.1, below = -40 }, |
| 81 | { thing = "Crystal", least = 1, most = 3, chance = 0.3, below = -40 }, |
| 82 | { thing = "Raw moonsilver", least = 1, most = 2, chance = 0.15, below = -80 }, |
| 83 | -- the Curator's, when she's in this world |
| 84 | { thing = "Old coin", least = 1, most = 2, chance = 0.15 }, |
| 85 | { thing = "Wooden music box", least = 1, most = 1, chance = 0.06, below = 0 }, |
| 86 | } |
| 87 | |
| 88 | type Find = { thing: string, n: number } |
| 89 | |
| 90 | -- what's still in chests players' pockets were too full for, by place |
| 91 | local left: { [string]: { Find } } = {} |
| 92 | |
| 93 | -- XP for opening an old chest |
| 94 | local CHEST_XP = 5 |
| 95 | |
| 96 | local function key(at: vector): string |
| 97 | return string.format("%d,%d,%d", math.floor(at.x), math.floor(at.y), math.floor(at.z)) |
| 98 | end |
| 99 | |
| 100 | -- Whether a thing is in this world: the Curator's coins are hers. |
| 101 | local here: { [string]: boolean } = {} |
| 102 | local function exists(name: string): boolean |
| 103 | if here[name] == nil then |
| 104 | here[name] = (pcall(world.kind, name)) |
| 105 | end |
| 106 | return here[name] == true |
| 107 | end |
| 108 | |
| 109 | -- A chest's prizes at height y: each by its chance, and always something. |
| 110 | local function roll(y: number): { Find } |
| 111 | local out: { Find } = {} |
| 112 | for _, p in PRIZES do |
| 113 | if (p.below == nil or y < p.below) and exists(p.thing) and world.random() < math.min(1, p.chance * knobs.prizes) then |
| 114 | local n = math.floor(world.random(p.least, p.most + 0.999) * knobs.prizes) |
| 115 | table.insert(out, { thing = p.thing, n = math.clamp(n, 1, 64) }) |
| 116 | end |
| 117 | end |
| 118 | if #out == 0 then |
| 119 | table.insert(out, { thing = "Coal", n = 3 }) |
| 120 | end |
| 121 | return out |
| 122 | end |
| 123 | |
| 124 | -- "4 coal, 1 lantern and 1 stone pickaxe" |
| 125 | -- "You found 2 raw gold, 1 crystal and 3 glass.": as many as fit on a line |
| 126 | -- (60 letters), then "and more" |
| 127 | local function found(finds: { Find }): string |
| 128 | local parts = {} |
| 129 | for _, f in finds do |
| 130 | table.insert(parts, f.n .. " " .. string.lower(f.thing)) |
| 131 | end |
| 132 | for n = #parts, 1, -1 do |
| 133 | local line = if n < #parts |
| 134 | then table.concat(parts, ", ", 1, n) .. " and more" |
| 135 | elseif n == 1 then parts[1] |
| 136 | else table.concat(parts, ", ", 1, n - 1) .. " and " .. parts[n] |
| 137 | line = "You found " .. line .. "." |
| 138 | if #line <= 60 or n == 1 then |
| 139 | return line |
| 140 | end |
| 141 | end |
| 142 | return "You found something." |
| 143 | end |
| 144 | |
| 145 | local shown: { [number]: number } = {} |
| 146 | |
| 147 | -- one line above the hotbar for a few seconds, for each player |
| 148 | local function tell(player, text: string) |
| 149 | ui.show(player, "found", text, { at = "bottom" }) |
| 150 | local mine = (shown[player.id] or 0) + 1 |
| 151 | shown[player.id] = mine |
| 152 | world.after(4, function() |
| 153 | if shown[player.id] == mine then |
| 154 | shown[player.id] = nil |
| 155 | ui.hide(player, "found") |
| 156 | end |
| 157 | end) |
| 158 | end |
| 159 | |
| 160 | world.on("player_left", function(p) |
| 161 | shown[p.id] = nil |
| 162 | end) |
| 163 | |
| 164 | -- flint, now and then, as well as the gravel, a fossil from deep stone, |
| 165 | -- and XP for ore |
| 166 | world.on("block_broken", function(player, at: vector, block: string) |
| 167 | if block == "Gravel" and world.pockets(player) ~= nil and world.random() < knobs.flint then |
| 168 | world.give(player, "Flint", 1) |
| 169 | end |
| 170 | if (block == "Stone" or block == "Slate") and at.y < FOSSILS_BELOW and world.pockets(player) ~= nil and world.random() < knobs.fossils then |
| 171 | if world.give(player, "Fossil shell", 1) > 0 then |
| 172 | tell(player, "There's a fossil shell in the stone!") |
| 173 | sound.play("sparkle", { at = at, volume = 0.5, caption = "Something in the stone" }) |
| 174 | end |
| 175 | end |
| 176 | local ore = ORES[block] |
| 177 | if ore then |
| 178 | world.reward(player, ore.xp) |
| 179 | end |
| 180 | end) |
| 181 | |
| 182 | world.on("block_used", function(player, at: vector, block: string) |
| 183 | if block ~= "Old chest" then |
| 184 | return |
| 185 | end |
| 186 | if world.pockets(player) == nil then |
| 187 | tell(player, "Old chests hold prizes for survival.") |
| 188 | return |
| 189 | end |
| 190 | local k = key(at) |
| 191 | local finds = left[k] or roll(at.y) |
| 192 | local got: { Find } = {} |
| 193 | local still: { Find } = {} |
| 194 | for _, f in finds do |
| 195 | local n = world.give(player, f.thing, f.n) |
| 196 | if n > 0 then |
| 197 | table.insert(got, { thing = f.thing, n = n }) |
| 198 | end |
| 199 | if n < f.n then |
| 200 | table.insert(still, { thing = f.thing, n = f.n - n }) |
| 201 | end |
| 202 | end |
| 203 | if #still > 0 then |
| 204 | left[k] = still |
| 205 | tell(player, "Your pockets are full.") |
| 206 | return |
| 207 | end |
| 208 | left[k] = nil |
| 209 | world.reward(player, CHEST_XP) |
| 210 | world.set_block(at, "Air") |
| 211 | sound.play("sparkle", { at = at, caption = "An old chest opens" }) |
| 212 | tell(player, found(got)) |
| 213 | end) |