Set a clay pot on a brazier, a campfire or a lit furnace and it brews teas, tonics and drafts, each in a glass vial from the workbench: some mend you, some make you dig faster, hit harder or shrug off hurts, and some give you a gift for a while: seeing in the dark, a higher jump, a quicker step, breathing under water, a slow fall, fire resistance, warmth or luck. Drink one (use it on nothing) and a picture on the left says what it's doing and for how long.
| 1 | --!strict |
| 2 | -- Brewing: a clay pot set over something hot (a brazier, a campfire, a lit |
| 3 | -- furnace or a fire, under it or right beside it) brews teas and tonics |
| 4 | -- into glass vials. Drinking one (Interact with it in hand) heals, or for a |
| 5 | -- while changes how you dig, hit, heal, take hurts, see, jump, walk, fall, |
| 6 | -- breathe, burn or keep warm, through the same world.healing, haste, might |
| 7 | -- and guard the rest of the game uses (so they stack with food, charms and |
| 8 | -- armor) and world.perk (0.33). A picture and a line on the left of the |
| 9 | -- screen say what's working and for how long. A cold clay pot is left |
| 10 | -- alone: the Old Greens' hearths and the workshops' pots are clay pots too. |
| 11 | |
| 12 | local world = require("@potluck/world@0.34") |
| 13 | local tweak = require("@potluck/tweak") |
| 14 | local ui = require("@potluck/ui@0.3") |
| 15 | local sound = require("@potluck/sound@0.2") |
| 16 | local store = require("@potluck/store") |
| 17 | |
| 18 | local knobs = tweak { |
| 19 | minutes = { 1, min = 0.25, max = 4, label = "How long a brew lasts (x)", order = 1 }, |
| 20 | xp = { 30, min = 0, max = 100, label = "XP for brewing a vial", order = 2 }, |
| 21 | } |
| 22 | |
| 23 | local POT = "Clay pot" |
| 24 | local VIAL = "Glass vial" |
| 25 | -- what's hot enough to brew over, under the pot or beside it |
| 26 | local HEAT: { [string]: boolean } = { Brazier = true, Campfire = true, ["Lit furnace"] = true, Fire = true } |
| 27 | local NEAR = { vector.create(0, -1, 0), vector.create(1, 0, 0), vector.create(-1, 0, 0), vector.create(0, 0, 1), vector.create(0, 0, -1) } |
| 28 | |
| 29 | -- What a brew does while it lasts: a channel at a level. The first four |
| 30 | -- channels are shares the game multiplies (guard is the share of a hurt |
| 31 | -- that gets through, so less is stronger); the rest are world.perk's. |
| 32 | type Fx = { channel: string, level: number } |
| 33 | |
| 34 | -- A brew: what it takes (and a vial), and what drinking it does at once |
| 35 | -- (`heal`, `xp`) and for `seconds` (`fx`). `chip` is its name on the |
| 36 | -- screen while it works. |
| 37 | type Brew = { |
| 38 | give: string, |
| 39 | want: { [string]: number }, |
| 40 | shelf: string, |
| 41 | heal: number?, |
| 42 | xp: number?, |
| 43 | fx: { Fx }?, |
| 44 | seconds: number?, |
| 45 | chip: string?, |
| 46 | says: string, |
| 47 | } |
| 48 | |
| 49 | local function fx(channel: string, level: number): Fx |
| 50 | return { channel = channel, level = level } |
| 51 | end |
| 52 | |
| 53 | local BREWS: { Brew } = { |
| 54 | -- mending |
| 55 | { give = "Mending tea", shelf = "Mending", want = { Berries = 2, Honey = 1 }, heal = 8, says = "Warm and sweet. You feel mended." }, |
| 56 | { give = "Berry cordial", shelf = "Mending", want = { Berries = 3, Apple = 1 }, heal = 4, fx = { fx("healing", 2) }, seconds = 90, chip = "Mending", says = "Sweet and tart. You feel better, and you'll mend faster." }, |
| 57 | { give = "Mender's draft", shelf = "Mending", want = { Berries = 2, Honey = 2, ["Gloom dust"] = 1 }, heal = 14, says = "A deep, slow warmth runs through you." }, |
| 58 | { give = "Hearth broth", shelf = "Mending", want = { Carrots = 2, Glowcap = 1 }, fx = { fx("healing", 3) }, seconds = 120, chip = "Mending", says = "You'll mend three times as fast." }, |
| 59 | { give = "Hearth cider", shelf = "Mending", want = { Apple = 2, Honey = 1 }, fx = { fx("warm", 1) }, seconds = 300, chip = "Warmth", says = "Spiced and warm. The winter cold can't get at you." }, |
| 60 | -- strength |
| 61 | { give = "Digger's tonic", shelf = "Strength", want = { Coal = 2, Glowcap = 1 }, fx = { fx("haste", 1.6) }, seconds = 180, chip = "Digging", says = "Your hands itch to dig." }, |
| 62 | { give = "Delver's draft", shelf = "Strength", want = { Coal = 3, Glowcap = 2, Crystal = 1 }, fx = { fx("haste", 2.5) }, seconds = 90, chip = "Digging fast", says = "Stone feels like cheese. Dig!" }, |
| 63 | { give = "Fireheart draft", shelf = "Strength", want = { Ember = 1, ["Raw meat"] = 1 }, fx = { fx("might", 1.5) }, seconds = 120, chip = "Might", says = "It burns all the way down. You hit harder." }, |
| 64 | { give = "Bruiser's brew", shelf = "Strength", want = { Ember = 2, ["Raw meat"] = 2, Bristles = 1 }, fx = { fx("might", 2) }, seconds = 90, chip = "Great might", says = "Your fists feel like hammers." }, |
| 65 | { give = "Stoneheart tonic", shelf = "Strength", want = { Bristles = 2, ["Raw iron"] = 1 }, fx = { fx("guard", 0.6) }, seconds = 180, chip = "Stoneheart", says = "You feel as tough as old stone." }, |
| 66 | { give = "Ironbark tonic", shelf = "Strength", want = { Bristles = 2, ["Iron ingot"] = 1, Leather = 1 }, fx = { fx("guard", 0.4) }, seconds = 120, chip = "Ironbark", says = "Your skin feels like bark and iron." }, |
| 67 | -- eyes and breath |
| 68 | { give = "Cat's-eye tonic", shelf = "Eyes and breath", want = { Glowcap = 2, ["Gloom dust"] = 1 }, fx = { fx("night_sight", 1) }, seconds = 300, chip = "Night sight", says = "The dark thins. You can see." }, |
| 69 | { give = "Owl's brew", shelf = "Eyes and breath", want = { Glowcap = 2, ["Gloom dust"] = 2, Crystal = 1 }, fx = { fx("night_sight", 2) }, seconds = 180, chip = "Owl's eyes", says = "Night is nearly day to you now." }, |
| 70 | { give = "Tidewater tonic", shelf = "Eyes and breath", want = { Reeds = 2, ["Raw fish"] = 1 }, fx = { fx("gills", 1) }, seconds = 240, chip = "Water breath", says = "You could stay under all day." }, |
| 71 | { give = "Diver's brew", shelf = "Eyes and breath", want = { Reeds = 1, Glowcap = 1, Crystal = 1 }, fx = { fx("gills", 1), fx("night_sight", 1) }, seconds = 180, chip = "Diving", says = "You breathe easy, and the deep looks bright." }, |
| 72 | { give = "Starlight tea", shelf = "Eyes and breath", want = { ["Gloom dust"] = 1, Crystal = 1, ["White flower"] = 1 }, xp = 40, says = "You see a little further into things." }, |
| 73 | { give = "Scholar's tea", shelf = "Eyes and breath", want = { ["Gloom dust"] = 2, Crystal = 2, ["White flower"] = 2 }, xp = 90, says = "Things click into place." }, |
| 74 | -- feet |
| 75 | { give = "Springheel tonic", shelf = "Feet", want = { Feather = 2, ["Yellow flower"] = 1 }, fx = { fx("leap", 1) }, seconds = 180, chip = "High jump", says = "Your legs are springs." }, |
| 76 | { give = "Featherfall tea", shelf = "Feet", want = { Feather = 1, Silk = 1 }, fx = { fx("feather", 1) }, seconds = 180, chip = "Slow fall", says = "You feel light as a feather." }, |
| 77 | { give = "Quickstep tea", shelf = "Feet", want = { Honey = 1, Lavender = 2 }, fx = { fx("stride", 1) }, seconds = 180, chip = "Quick step", says = "Your feet want to run." }, |
| 78 | { give = "Mountain goat's brew", shelf = "Feet", want = { Feather = 1, Honey = 1, Lavender = 1 }, fx = { fx("leap", 1), fx("stride", 0.6) }, seconds = 150, chip = "Goat's feet", says = "Hills hold no fear for you." }, |
| 79 | -- luck and fire |
| 80 | { give = "Clover tea", shelf = "Luck and fire", want = { Clover = 3, Honey = 1 }, fx = { fx("luck", 1) }, seconds = 300, chip = "Luck", says = "You feel lucky." }, |
| 81 | { give = "Gilded clover tea", shelf = "Luck and fire", want = { Clover = 3, ["Gold ingot"] = 1 }, fx = { fx("luck", 2) }, seconds = 180, chip = "Great luck", says = "Gold dust glitters in the cup. You feel very lucky." }, |
| 82 | { give = "Cinderward tonic", shelf = "Luck and fire", want = { Cactus = 2, Ember = 1 }, fx = { fx("fireproof", 1) }, seconds = 240, chip = "Fire ward", says = "Flames lick at you and can't hurt." }, |
| 83 | { give = "Hero's cordial", shelf = "Luck and fire", want = { ["Gold ingot"] = 1, Honey = 2, Ember = 1, Crystal = 1 }, fx = { fx("might", 1.3), fx("guard", 0.8), fx("haste", 1.3), fx("healing", 2) }, seconds = 180, chip = "Hero", says = "You feel ready for anything." }, |
| 84 | } |
| 85 | local SHELVES = { "Mending", "Strength", "Eyes and breath", "Feet", "Luck and fire" } |
| 86 | local BY_NAME: { [string]: Brew } = {} |
| 87 | for _, b in BREWS do |
| 88 | BY_NAME[b.give] = b |
| 89 | world.aims(b.give) |
| 90 | end |
| 91 | |
| 92 | -- the channels the game multiplies, with what they are when nothing works |
| 93 | local SHARES: { [string]: number } = { healing = 1, haste = 1, might = 1, guard = 1 } |
| 94 | local PERKS = { "night_sight", "leap", "gills", "feather", "fireproof", "luck", "stride", "warm" } |
| 95 | |
| 96 | local function exists(name: string): boolean |
| 97 | return (pcall(world.kind, name)) |
| 98 | end |
| 99 | |
| 100 | local function hot(at: vector): boolean |
| 101 | for _, d in NEAR do |
| 102 | if HEAT[world.block(at + d)] then |
| 103 | return true |
| 104 | end |
| 105 | end |
| 106 | return false |
| 107 | end |
| 108 | |
| 109 | -- "2 Berries, Honey" |
| 110 | local function short(b: Brew): string |
| 111 | local parts = {} |
| 112 | for name, n in b.want do |
| 113 | table.insert(parts, if n == 1 then name else n .. " " .. name) |
| 114 | end |
| 115 | table.sort(parts) |
| 116 | return table.concat(parts, ", ") |
| 117 | end |
| 118 | |
| 119 | local function takes(b: Brew): string |
| 120 | return short(b) .. " and a glass vial" |
| 121 | end |
| 122 | |
| 123 | -- whether this world has everything a brew is made of |
| 124 | local function makeable(b: Brew): boolean |
| 125 | local ok = exists(b.give) |
| 126 | for name in b.want do |
| 127 | ok = ok and exists(name) |
| 128 | end |
| 129 | return ok |
| 130 | end |
| 131 | |
| 132 | -- ---- what's drunk, and said ---- |
| 133 | |
| 134 | -- one line above the hotbar for a few seconds, for each player |
| 135 | local said: { [number]: number } = {} |
| 136 | local function tell(player, text: string) |
| 137 | ui.show(player, "brewing_said", text, { at = "bottom" }) |
| 138 | local mine = (said[player.id] or 0) + 1 |
| 139 | said[player.id] = mine |
| 140 | world.after(5, function() |
| 141 | if said[player.id] == mine then |
| 142 | said[player.id] = nil |
| 143 | ui.hide(player, "brewing_said") |
| 144 | end |
| 145 | end) |
| 146 | end |
| 147 | |
| 148 | -- ---- brewing ---- |
| 149 | |
| 150 | local function brew(player, b: Brew): (boolean, string) |
| 151 | if world.has(player, VIAL) < 1 then |
| 152 | return false, "You'll need a glass vial. A workbench makes three from a block of glass." |
| 153 | end |
| 154 | for name, n in b.want do |
| 155 | if world.has(player, name) < n then |
| 156 | return false, "You'll need " .. takes(b) .. "." |
| 157 | end |
| 158 | end |
| 159 | for name, n in b.want do |
| 160 | world.take(player, name, n) |
| 161 | end |
| 162 | world.take(player, VIAL, 1) |
| 163 | if world.give(player, b.give, 1) == 0 then |
| 164 | for name, n in b.want do |
| 165 | world.give(player, name, n) |
| 166 | end |
| 167 | world.give(player, VIAL, 1) |
| 168 | return false, "Your pockets are full. Make some room first." |
| 169 | end |
| 170 | world.reward(player, math.floor(knobs.xp)) |
| 171 | return true, "You brewed a " .. b.give .. "." |
| 172 | end |
| 173 | |
| 174 | -- the brews on a shelf this world can make |
| 175 | local function on_shelf(shelf: string): { Brew } |
| 176 | local can: { Brew } = {} |
| 177 | for _, b in BREWS do |
| 178 | if b.shelf == shelf and makeable(b) then |
| 179 | table.insert(can, b) |
| 180 | end |
| 181 | end |
| 182 | return can |
| 183 | end |
| 184 | |
| 185 | -- What a shelf's card says: each brew and what it takes. A card holds 300 |
| 186 | -- letters, so the shelves are kept to what fits (a test checks). |
| 187 | local function shelf_text(can: { Brew }): string |
| 188 | local lines = {} |
| 189 | for _, b in can do |
| 190 | table.insert(lines, b.give .. ": " .. short(b)) |
| 191 | end |
| 192 | return table.concat(lines, "\n") |
| 193 | end |
| 194 | |
| 195 | local stir |
| 196 | |
| 197 | -- one shelf of the pot: its brews to choose from |
| 198 | local function shelf(player, at: vector, name: string) |
| 199 | local can = on_shelf(name) |
| 200 | if #can == 0 then |
| 201 | stir(player, at) |
| 202 | return |
| 203 | end |
| 204 | local choices = {} |
| 205 | for _, b in can do |
| 206 | table.insert(choices, b.give) |
| 207 | end |
| 208 | table.insert(choices, "Back") |
| 209 | ui.ask(player, { title = "Brewing pot: " .. name, text = shelf_text(can), choices = choices }, function(who, choice) |
| 210 | local b = choice and can[choice] |
| 211 | if not b then |
| 212 | if choice then |
| 213 | stir(who, at) |
| 214 | end |
| 215 | return |
| 216 | end |
| 217 | local brewed, how = brew(who, b) |
| 218 | if brewed then |
| 219 | sound.play("splash", { at = at, volume = 0.4, caption = "Something's brewing" }) |
| 220 | end |
| 221 | tell(who, how) |
| 222 | -- ask again, so brewing several is quick |
| 223 | shelf(who, at, name) |
| 224 | end) |
| 225 | end |
| 226 | |
| 227 | function stir(player, at: vector) |
| 228 | if world.pockets(player) == nil then |
| 229 | ui.ask(player, { title = "Brewing pot", text = "In survival, a clay pot over a brazier or a campfire brews teas and tonics.", choices = { "OK" } }, function() end) |
| 230 | return |
| 231 | end |
| 232 | local shelves, choices = {}, {} |
| 233 | for _, name in SHELVES do |
| 234 | if #on_shelf(name) > 0 then |
| 235 | table.insert(shelves, name) |
| 236 | table.insert(choices, name) |
| 237 | end |
| 238 | end |
| 239 | if #shelves == 0 then |
| 240 | ui.ask(player, { title = "Brewing pot", text = "There's nothing to brew in this world yet.", choices = { "OK" } }, function() end) |
| 241 | return |
| 242 | end |
| 243 | table.insert(choices, "Close") |
| 244 | ui.ask(player, { title = "Brewing pot", text = "What will you brew? Each takes a glass vial too.\n\nMending, strength, eyes and breath, feet, and luck and fire.", choices = choices }, function(who, choice) |
| 245 | local name = choice and shelves[choice] |
| 246 | if name then |
| 247 | shelf(who, at, name) |
| 248 | end |
| 249 | end) |
| 250 | end |
| 251 | |
| 252 | world.on("block_used", function(player, at: vector, block: string) |
| 253 | if block == POT and hot(at) then |
| 254 | stir(player, at) |
| 255 | end |
| 256 | end) |
| 257 | |
| 258 | -- ---- drinking ---- |
| 259 | |
| 260 | -- what's working on whom, by name (so it lasts through leaving and coming |
| 261 | -- back): brew to the seconds left |
| 262 | local working: { [string]: { [string]: number } } = store.get("working") or {} |
| 263 | -- what this creation last told the game for each player, by id: channel to |
| 264 | -- level |
| 265 | local told: { [number]: { [string]: number } } = {} |
| 266 | -- the words on each player's screen, by id |
| 267 | local shown: { [number]: { string } } = {} |
| 268 | |
| 269 | world.on("item_used", function(player, item: string?) |
| 270 | local b = item and BY_NAME[item] |
| 271 | if not b then |
| 272 | return |
| 273 | end |
| 274 | if not world.take(player, b.give, 1) then |
| 275 | return |
| 276 | end |
| 277 | world.give(player, VIAL, 1) |
| 278 | sound.play("pop", { at = player.position, volume = 0.5, caption = "Gulp" }) |
| 279 | if b.heal then |
| 280 | world.heal(player, b.heal) |
| 281 | end |
| 282 | if b.xp then |
| 283 | world.reward(player, b.xp) |
| 284 | end |
| 285 | if b.fx then |
| 286 | local mine = working[player.name] or {} |
| 287 | working[player.name] = mine |
| 288 | mine[b.give] = (b.seconds or 60) * knobs.minutes |
| 289 | end |
| 290 | tell(player, b.says) |
| 291 | end) |
| 292 | |
| 293 | -- "4:30" |
| 294 | local function clock(s: number): string |
| 295 | local m = math.floor(s / 60) |
| 296 | return m .. ":" .. string.format("%02d", math.floor(s % 60)) |
| 297 | end |
| 298 | |
| 299 | -- the best of each channel among what's working: more for most, less for |
| 300 | -- guard (the share of a hurt that gets through) |
| 301 | local function best(mine: { [string]: number }): { [string]: number } |
| 302 | local now: { [string]: number } = {} |
| 303 | for name in mine do |
| 304 | local b = BY_NAME[name] |
| 305 | for _, e in (b and b.fx) or {} do |
| 306 | local was = now[e.channel] |
| 307 | if was == nil or (if e.channel == "guard" then e.level < was else e.level > was) then |
| 308 | now[e.channel] = e.level |
| 309 | end |
| 310 | end |
| 311 | end |
| 312 | return now |
| 313 | end |
| 314 | |
| 315 | local function apply(player, channel: string, level: number) |
| 316 | if SHARES[channel] then |
| 317 | if channel == "healing" then |
| 318 | world.healing(player, level) |
| 319 | elseif channel == "haste" then |
| 320 | world.haste(player, level) |
| 321 | elseif channel == "might" then |
| 322 | world.might(player, level) |
| 323 | else |
| 324 | world.guard(player, level) |
| 325 | end |
| 326 | else |
| 327 | world.perk(player, channel, level) |
| 328 | end |
| 329 | end |
| 330 | |
| 331 | local CHANNELS: { string } = { "healing", "haste", "might", "guard" } |
| 332 | for _, p in PERKS do |
| 333 | table.insert(CHANNELS, p) |
| 334 | end |
| 335 | |
| 336 | local beat = 0 |
| 337 | world.every(1, function() |
| 338 | beat += 1 |
| 339 | for _, p in world.players() do |
| 340 | local mine = working[p.name] |
| 341 | -- the seconds tick down; what's done goes |
| 342 | if mine then |
| 343 | for name, left in mine do |
| 344 | if left <= 1 or not BY_NAME[name] then |
| 345 | mine[name] = nil |
| 346 | else |
| 347 | mine[name] = left - 1 |
| 348 | end |
| 349 | end |
| 350 | if next(mine) == nil then |
| 351 | working[p.name] = nil |
| 352 | mine = nil |
| 353 | end |
| 354 | end |
| 355 | local now = if mine then best(mine) else {} |
| 356 | local was = told[p.id] or {} |
| 357 | for _, c in CHANNELS do |
| 358 | local level = now[c] or SHARES[c] or 0 |
| 359 | if (was[c] or SHARES[c] or 0) ~= level then |
| 360 | apply(p, c, level) |
| 361 | end |
| 362 | end |
| 363 | told[p.id] = now |
| 364 | -- and a picture and a line for each brew that's working, soonest to |
| 365 | -- end last, as far as the screen has room |
| 366 | local lines: { string } = {} |
| 367 | local pics: { string } = {} |
| 368 | if mine then |
| 369 | local names: { string } = {} |
| 370 | for name in mine do |
| 371 | table.insert(names, name) |
| 372 | end |
| 373 | table.sort(names, function(a, b) |
| 374 | return (mine :: { [string]: number })[a] > (mine :: { [string]: number })[b] |
| 375 | end) |
| 376 | for _, name in names do |
| 377 | local b = BY_NAME[name] |
| 378 | table.insert(pics, name) |
| 379 | table.insert(lines, ((b and b.chip) or name) .. "\n" .. clock((mine :: { [string]: number })[name])) |
| 380 | end |
| 381 | end |
| 382 | local before = shown[p.id] or {} |
| 383 | for i = 1, 6 do |
| 384 | local key = "fx" .. i |
| 385 | if lines[i] then |
| 386 | local line = pics[i] .. "|" .. lines[i] |
| 387 | if before[i] ~= line then |
| 388 | ui.effect(p, key, pics[i], lines[i]) |
| 389 | end |
| 390 | before[i] = line |
| 391 | elseif before[i] then |
| 392 | ui.hide(p, key) |
| 393 | before[i] = nil |
| 394 | end |
| 395 | end |
| 396 | shown[p.id] = before |
| 397 | end |
| 398 | if beat % 10 == 0 then |
| 399 | store.set("working", working) |
| 400 | end |
| 401 | end) |
| 402 | |
| 403 | world.on("player_left", function(player) |
| 404 | told[player.id] = nil |
| 405 | shown[player.id] = nil |
| 406 | said[player.id] = nil |
| 407 | store.set("working", working) |
| 408 | end) |
| 409 | |
| 410 | -- Told to the item guide: what the pot brews. |
| 411 | local book = {} |
| 412 | for _, b in BREWS do |
| 413 | local want = table.clone(b.want) |
| 414 | want[VIAL] = 1 |
| 415 | table.insert(book, { give = b.give, n = 1, want = want, note = "over a brazier or a campfire" }) |
| 416 | end |
| 417 | world.recipes("Brewing pot", book) |