When the rain stops and the sun comes out, a rainbow may rise over the land for a few minutes. Follow it to its foot, and there's a lucky pot: an iron pot full of gold and shiny things. Every player can open it once for a handful of finds, beads, ingots, crystal, old coins and, now and then, a piece of rainbow glass for the Curator. The rainbow fades, and the pot sinks away soon after, so don't dawdle.
| 1 | --!strict |
| 2 | -- Rainbows and the lucky pot (@potluck/sky 0.2). When the rain stops in |
| 3 | -- the daytime a rainbow may come out near a player, for a few minutes, and |
| 4 | -- at its foot stands a lucky pot. Every survival player can open the pot |
| 5 | -- once, for a handful of finds rolled for them alone (PRIZES below). The |
| 6 | -- rainbow fades over its last half minute and the pot sinks away a couple |
| 7 | -- of minutes after, whether or not everyone got to it. The game draws the |
| 8 | -- bow on every game in the world (sky.set_rainbow) and nothing about it |
| 9 | -- moves anyone's camera. |
| 10 | -- |
| 11 | -- The rainbow and the pot are kept with the world (store), so a restart |
| 12 | -- carries on with what was left of them. |
| 13 | |
| 14 | local world = require("@potluck/world@0.35") |
| 15 | local sky = require("@potluck/sky@0.2") |
| 16 | local tweak = require("@potluck/tweak") |
| 17 | local store = require("@potluck/store") |
| 18 | local ui = require("@potluck/ui@0.2") |
| 19 | local sound = require("@potluck/sound@0.2") |
| 20 | |
| 21 | local knobs = tweak { |
| 22 | chance = { 0.6, min = 0, max = 1, label = "Chance of a rainbow when rain ends", order = 1 }, |
| 23 | minutes = { 4, min = 1, max = 20, label = "Minutes a rainbow lasts, about", order = 2 }, |
| 24 | linger = { 2, min = 0.5, max = 10, label = "Minutes the pot stays after it", order = 3 }, |
| 25 | prizes = { 1, min = 0.5, max = 3, label = "How much is in a pot", order = 4 }, |
| 26 | } |
| 27 | |
| 28 | local POT = "Lucky pot" |
| 29 | local XP = 20 |
| 30 | -- how far from a player the pot goes: close enough to be loaded and to |
| 31 | -- walk to before the bow fades |
| 32 | local NEAR, FAR = 36, 56 |
| 33 | -- the bow fades out over this many seconds at the end of its life |
| 34 | local FADE = 30 |
| 35 | -- the time of day a rainbow comes out in: low sun, but up |
| 36 | local FROM, TO = 0.28, 0.72 |
| 37 | |
| 38 | type Prize = { thing: string, chance: number, least: number, most: number } |
| 39 | local PRIZES: { Prize } = { |
| 40 | { thing = "Beads", chance = 1, least = 20, most = 40 }, |
| 41 | { thing = "Gold ingot", chance = 0.7, least = 1, most = 3 }, |
| 42 | { thing = "Moonsilver ingot", chance = 0.4, least = 1, most = 2 }, |
| 43 | { thing = "Grotto crystal", chance = 0.5, least = 2, most = 4 }, |
| 44 | { thing = "Crystal", chance = 0.5, least = 1, most = 3 }, |
| 45 | { thing = "Raw gold", chance = 0.5, least = 1, most = 3 }, |
| 46 | { thing = "Glimmer cocoon", chance = 0.25, least = 1, most = 1 }, |
| 47 | { thing = "Old coin", chance = 0.45, least = 1, most = 2 }, |
| 48 | { thing = "Rainbow glass", chance = 0.15, least = 1, most = 1 }, |
| 49 | } |
| 50 | |
| 51 | -- ---- what's kept ---- |
| 52 | |
| 53 | -- the rainbow now: where its pot is, the heading of its arch, and the |
| 54 | -- seconds left of it and of the pot after; who has opened the pot |
| 55 | type Bow = { at: vector, facing: number, left: number, linger: number, opened: { [string]: boolean } } |
| 56 | local bow: Bow? = store.get("bow") |
| 57 | local last_kind = sky.weather().kind |
| 58 | local cool = 0 |
| 59 | |
| 60 | local function save() |
| 61 | store.set("bow", bow) |
| 62 | end |
| 63 | |
| 64 | local function key(at: vector): string |
| 65 | return string.format("%d,%d,%d", math.floor(at.x), math.floor(at.y), math.floor(at.z)) |
| 66 | end |
| 67 | |
| 68 | -- Whether a thing is in this world. |
| 69 | local here: { [string]: boolean } = {} |
| 70 | local function exists(name: string): boolean |
| 71 | if here[name] == nil then |
| 72 | here[name] = (pcall(world.kind, name)) |
| 73 | end |
| 74 | return here[name] == true |
| 75 | end |
| 76 | |
| 77 | local shown: { [number]: number } = {} |
| 78 | local function tell(player, text: string) |
| 79 | ui.show(player, "rainbow", text, { at = "bottom" }) |
| 80 | local mine = (shown[player.id] or 0) + 1 |
| 81 | shown[player.id] = mine |
| 82 | world.after(5, function() |
| 83 | if shown[player.id] == mine then |
| 84 | shown[player.id] = nil |
| 85 | ui.hide(player, "rainbow") |
| 86 | end |
| 87 | end) |
| 88 | end |
| 89 | |
| 90 | -- ---- a rainbow comes out ---- |
| 91 | |
| 92 | -- Open grass or sand a way off, with sky over it and no water: where the |
| 93 | -- foot of a bow could stand. Nil if the dozen tries found none. |
| 94 | local function foot(from: vector): vector? |
| 95 | for _ = 1, 12 do |
| 96 | local turn = world.random() * 2 * math.pi |
| 97 | local far = world.random(NEAR, FAR) |
| 98 | local x, z = math.floor(from.x + math.cos(turn) * far), math.floor(from.z + math.sin(turn) * far) |
| 99 | local y = world.top(x, z) |
| 100 | local ground = world.block(vector.create(x, y, z)) |
| 101 | local over = world.block(vector.create(x, y + 1, z)) |
| 102 | local roof = world.block(vector.create(x, y + 2, z)) |
| 103 | if (ground == "Grass" or ground == "Sand" or ground == "Dirt" or ground == "Snow") and over == "Air" and roof == "Air" then |
| 104 | return vector.create(x + 0.5, y + 1, z + 0.5) |
| 105 | end |
| 106 | end |
| 107 | return nil |
| 108 | end |
| 109 | |
| 110 | local function strength(b: Bow): number |
| 111 | return math.clamp(math.floor(b.left / FADE * 10) / 10, 0, 1) |
| 112 | end |
| 113 | |
| 114 | local function show() |
| 115 | if bow and bow.left > 0 then |
| 116 | sky.set_rainbow(bow.at, { facing = bow.facing, strength = math.max(0.1, strength(bow)) }) |
| 117 | else |
| 118 | sky.set_rainbow(nil) |
| 119 | end |
| 120 | end |
| 121 | |
| 122 | -- A bow near a player, if there's room for one. True when it came out. |
| 123 | local function rise(): boolean |
| 124 | local players = {} |
| 125 | for _, p in world.players() do |
| 126 | if world.place(p.position) ~= "dusk" then |
| 127 | table.insert(players, p) |
| 128 | end |
| 129 | end |
| 130 | if #players == 0 then |
| 131 | return false |
| 132 | end |
| 133 | local p = players[math.floor(world.random(1, #players + 0.999))] |
| 134 | local at = foot(p.position) |
| 135 | if not at then |
| 136 | return false |
| 137 | end |
| 138 | -- the bow arches across the player's view of the pot |
| 139 | local toward = math.atan2(p.position.z - at.z, p.position.x - at.x) |
| 140 | local facing = toward + (if world.random() < 0.5 then 1 else -1) * math.pi / 2 |
| 141 | world.set_block(at, POT) |
| 142 | bow = { at = at, facing = facing, left = knobs.minutes * 60 * world.random(0.8, 1.25), linger = knobs.linger * 60, opened = {} } |
| 143 | save() |
| 144 | show() |
| 145 | for _, q in world.players() do |
| 146 | tell(q, "A rainbow! There's a lucky pot at its foot.") |
| 147 | end |
| 148 | return true |
| 149 | end |
| 150 | |
| 151 | -- Takes the pot away, and the bow with it. |
| 152 | local function end_bow() |
| 153 | if bow then |
| 154 | if world.block(bow.at) == POT then |
| 155 | world.set_block(bow.at, "Air") |
| 156 | end |
| 157 | bow = nil |
| 158 | save() |
| 159 | end |
| 160 | sky.set_rainbow(nil) |
| 161 | end |
| 162 | |
| 163 | -- ---- opening the pot ---- |
| 164 | |
| 165 | type Find = { thing: string, n: number } |
| 166 | -- what a player's pockets were too full for, by name |
| 167 | local leftover: { [string]: { Find } } = {} |
| 168 | |
| 169 | local function roll(): { Find } |
| 170 | local out: { Find } = {} |
| 171 | for _, p in PRIZES do |
| 172 | if exists(p.thing) and world.random() < p.chance then |
| 173 | local n = math.floor(world.random(p.least, p.most + 0.999) * knobs.prizes) |
| 174 | table.insert(out, { thing = p.thing, n = math.clamp(n, 1, 64) }) |
| 175 | end |
| 176 | end |
| 177 | -- a pot is never poor: three finds at least |
| 178 | if #out < 3 then |
| 179 | table.insert(out, { thing = "Gold ingot", n = 2 }) |
| 180 | end |
| 181 | return out |
| 182 | end |
| 183 | |
| 184 | -- "You found 30 beads, 2 gold ingots and more.": as many as fit on a line |
| 185 | -- (60 letters), then "and more" |
| 186 | local function found(finds: { Find }): string |
| 187 | local parts = {} |
| 188 | for _, f in finds do |
| 189 | table.insert(parts, f.n .. " " .. string.lower(f.thing)) |
| 190 | end |
| 191 | for n = #parts, 1, -1 do |
| 192 | local line = if n < #parts |
| 193 | then table.concat(parts, ", ", 1, n) .. " and more" |
| 194 | elseif n == 1 then parts[1] |
| 195 | else table.concat(parts, ", ", 1, n - 1) .. " and " .. parts[n] |
| 196 | line = "You found " .. line .. "." |
| 197 | if #line <= 60 or n == 1 then |
| 198 | return line |
| 199 | end |
| 200 | end |
| 201 | return "You found something." |
| 202 | end |
| 203 | |
| 204 | -- Gives a player their finds; true when it's all given. |
| 205 | local function rummage(player, b: Bow): boolean |
| 206 | local finds = leftover[player.name] or roll() |
| 207 | local got, still = {}, {} |
| 208 | for _, f in finds do |
| 209 | local n = world.give(player, f.thing, f.n) |
| 210 | if n > 0 then |
| 211 | table.insert(got, { thing = f.thing, n = n }) |
| 212 | end |
| 213 | if n < f.n then |
| 214 | table.insert(still, { thing = f.thing, n = f.n - n }) |
| 215 | end |
| 216 | end |
| 217 | if #still > 0 then |
| 218 | leftover[player.name] = still |
| 219 | tell(player, "Your pockets are full.") |
| 220 | return false |
| 221 | end |
| 222 | leftover[player.name] = nil |
| 223 | b.opened[player.name] = true |
| 224 | save() |
| 225 | world.reward(player, XP) |
| 226 | sound.play("sparkle", { at = b.at, caption = "Shiny things in a pot" }) |
| 227 | tell(player, found(got)) |
| 228 | return true |
| 229 | end |
| 230 | |
| 231 | -- the pot's own finds are the players': breaking it gives nothing of itself |
| 232 | world.drops(POT, { gives = false }) |
| 233 | |
| 234 | world.on("block_used", function(player, at: vector, block: string) |
| 235 | if block ~= POT then |
| 236 | return |
| 237 | end |
| 238 | local b = bow |
| 239 | if not b or key(b.at) ~= key(at) then |
| 240 | tell(player, "It's an old pot, and empty.") |
| 241 | return |
| 242 | end |
| 243 | if world.pockets(player) == nil then |
| 244 | tell(player, "A lucky pot holds finds for survival.") |
| 245 | return |
| 246 | end |
| 247 | if b.opened[player.name] then |
| 248 | tell(player, "You've had your share of this pot.") |
| 249 | return |
| 250 | end |
| 251 | rummage(player, b) |
| 252 | end) |
| 253 | |
| 254 | world.on("block_broken", function(_, at: vector, block: string) |
| 255 | if block == POT and bow and key(bow.at) == key(at) then |
| 256 | bow.linger = 0 |
| 257 | bow.left = 0 |
| 258 | end_bow() |
| 259 | end |
| 260 | end) |
| 261 | |
| 262 | -- ---- the clock ---- |
| 263 | |
| 264 | local ticks = 0 |
| 265 | world.every(1, function() |
| 266 | local kind = sky.weather().kind |
| 267 | local rained = last_kind == "rain" or last_kind == "storm" |
| 268 | local stopped = kind ~= "rain" and kind ~= "storm" and kind ~= "snow" and kind ~= "fog" |
| 269 | last_kind = kind |
| 270 | cool = math.max(0, cool - 1) |
| 271 | local day = world.daytime() |
| 272 | if rained and stopped and bow == nil and cool == 0 and day >= FROM and day <= TO and world.random() < knobs.chance then |
| 273 | if rise() then |
| 274 | cool = 600 |
| 275 | end |
| 276 | end |
| 277 | local b = bow |
| 278 | if b then |
| 279 | if b.left > 0 then |
| 280 | b.left -= 1 |
| 281 | if b.left <= 0 then |
| 282 | sky.set_rainbow(nil) |
| 283 | elseif b.left <= FADE or ticks % 5 == 0 then |
| 284 | show() |
| 285 | end |
| 286 | else |
| 287 | b.linger -= 1 |
| 288 | if b.linger <= 0 then |
| 289 | end_bow() |
| 290 | end |
| 291 | end |
| 292 | ticks += 1 |
| 293 | if ticks % 10 == 0 then |
| 294 | save() |
| 295 | end |
| 296 | end |
| 297 | end) |
| 298 | |
| 299 | -- a restart carries on with what was left of the bow |
| 300 | if bow and bow.left > 0 then |
| 301 | show() |
| 302 | end |