A charm table puts charms on what's in your hand, for XP and gloom dust. Tools and weapons: Swift, Sturdy, Lucky, Keen, Thrifty, Hewing, Reaping and Sifting. Armor: Warm, Stout, Hearty, and Owl-eyed, Gilled, Springy, Feathery, Ashen, Fleet and Fortunate, which give sight in the dark, breathing under water, a higher jump, a slow fall, fire resistance, a quicker step and luck. Make the table at a workbench from gold, a crystal and planks.
| 1 | --!strict |
| 2 | -- Charms, for survival. Use a charm table (Interact while aiming at it) |
| 3 | -- with a tool, a weapon or a piece of armor in your hand, and it puts a |
| 4 | -- charm on it for XP |
| 5 | -- (world.spend_xp) and a little of what wild creatures and the deep give: |
| 6 | -- Swift: a tool digs faster (world.haste, while it's in your hand). |
| 7 | -- Sturdy: a tool wears half as fast (the Tools creation reads it). |
| 8 | -- Keen: a sword, bow, sling or spear hits harder (world.might, which |
| 9 | -- wild creatures and shots read). |
| 10 | -- Lucky: a pickaxe sometimes brings up more ore than the ore gives. |
| 11 | -- Thrifty: a bow or sling sometimes shoots without using up its ammo |
| 12 | -- (the Ranged creation reads it). |
| 13 | -- Warm: a piece of armor keeps the winter's cold off, Stout makes it |
| 14 | -- keep more of every hurt off, and Hearty helps you heal faster (the |
| 15 | -- Armor creation reads all three). |
| 16 | -- Hewing: an axe sometimes brings down an extra log, Reaping: a hoe or a |
| 17 | -- sickle sometimes brings in an extra crop and Sifting: a shovel |
| 18 | -- sometimes finds flint in gravel. |
| 19 | -- Owl-eyed (sight in the dark), Gilled (breathing under water), Springy |
| 20 | -- (a higher jump), Feathery (a slow fall), Ashen (fire can't hurt), Fleet |
| 21 | -- (a quicker step) and Fortunate (luck) go on armor, and the Armor |
| 22 | -- creation gives each as a world.perk while the piece is worn. |
| 23 | -- Charms go on that kind of thing (world.charm), so every iron pickaxe |
| 24 | -- you carry is Swift once one is, and they're kept with the world. A tool |
| 25 | -- that breaks takes its charms with it, unless you've another. |
| 26 | -- CHARMS below says what each fits, costs and does, so a remix can add its |
| 27 | -- own. The table is in potluck.toml, and the workbench makes it. |
| 28 | |
| 29 | local world = require("@potluck/world@0.34") |
| 30 | local tweak = require("@potluck/tweak") |
| 31 | local ui = require("@potluck/ui@0.2") |
| 32 | local sound = require("@potluck/sound@0.2") |
| 33 | |
| 34 | local knobs = tweak { |
| 35 | swift = { 1.5, min = 1, max = 4, label = "How much faster Swift digs", order = 1 }, |
| 36 | keen = { 1.5, min = 1, max = 4, label = "How much harder Keen hits", order = 2 }, |
| 37 | cost = { 1, min = 0, max = 3, label = "How much XP charms take", order = 3 }, |
| 38 | lucky = { 0.35, min = 0, max = 1, label = "How often Lucky finds more", order = 4 }, |
| 39 | hewing = { 0.3, min = 0, max = 1, label = "How often Hewing brings down more", order = 5 }, |
| 40 | reaping = { 0.3, min = 0, max = 1, label = "How often Reaping brings in more", order = 6 }, |
| 41 | sifting = { 0.25, min = 0, max = 1, label = "How often Sifting finds flint", order = 7 }, |
| 42 | } |
| 43 | |
| 44 | local TABLE = "Charm table" |
| 45 | |
| 46 | -- What digs and what hits, by name: a name that starts with a space is the |
| 47 | -- end of one ("Iron pickaxe"), any other the whole of it. |
| 48 | local DIGS = { " pickaxe", " axe", " shovel", " hoe", "Shears", "Sickle", "Hammer" } |
| 49 | local HITS = { " sword", "Bow", "Sling", "Spear" } |
| 50 | local WEARS = { " cap", " helmet", " coat", " leggings", " boots" } |
| 51 | local GUARDS = { " cap", " helmet", " coat", " leggings", " boots", "Shield" } |
| 52 | local PICKS = { " pickaxe", "Gnome's pick" } |
| 53 | local SHOOTS = { "Bow", "Sling" } |
| 54 | local AXES = { " axe" } |
| 55 | local REAPS = { " hoe", "Sickle" } |
| 56 | local SHOVELS = { " shovel" } |
| 57 | local HEAD = { " cap", " helmet" } |
| 58 | local BODY = { " coat" } |
| 59 | local LEGS = { " leggings" } |
| 60 | local FEET = { " boots" } |
| 61 | local function join(...: { string }): { string } |
| 62 | local out = {} |
| 63 | for _, list in { ... } do |
| 64 | for _, e in list do |
| 65 | table.insert(out, e) |
| 66 | end |
| 67 | end |
| 68 | return out |
| 69 | end |
| 70 | |
| 71 | -- A charm: its name, what it does, what it fits, the XP it takes and what |
| 72 | -- else. |
| 73 | type Charm = { name: string, does: string, fits: { string }, xp: number, wants: { [string]: number } } |
| 74 | local CHARMS: { Charm } = { |
| 75 | { name = "Swift", does = "digs faster", fits = DIGS, xp = 40, wants = { ["Gloom dust"] = 2, Crystal = 1 } }, |
| 76 | { name = "Sturdy", does = "wears half as fast", fits = DIGS, xp = 30, wants = { ["Gloom dust"] = 2, ["Iron ingot"] = 2 } }, |
| 77 | { name = "Keen", does = "hits harder", fits = HITS, xp = 50, wants = { ["Gloom dust"] = 3, ["Gold ingot"] = 1 } }, |
| 78 | { name = "Lucky", does = "finds more ore", fits = PICKS, xp = 45, wants = { ["Gloom dust"] = 2, ["Gold ingot"] = 1 } }, |
| 79 | { name = "Thrifty", does = "saves ammo", fits = SHOOTS, xp = 30, wants = { ["Gloom dust"] = 1, Feather = 3 } }, |
| 80 | { name = "Warm", does = "keeps the cold off", fits = WEARS, xp = 20, wants = { ["Gloom dust"] = 1, Ember = 1 } }, |
| 81 | { name = "Stout", does = "keeps more off", fits = GUARDS, xp = 30, wants = { ["Gloom dust"] = 2, ["Iron ingot"] = 1 } }, |
| 82 | { name = "Hearty", does = "heals you faster", fits = GUARDS, xp = 35, wants = { ["Gloom dust"] = 1, Honey = 2 } }, |
| 83 | { name = "Hewing", does = "brings down more logs", fits = AXES, xp = 30, wants = { ["Gloom dust"] = 1, ["Iron ingot"] = 1 } }, |
| 84 | { name = "Reaping", does = "brings in more crops", fits = REAPS, xp = 30, wants = { ["Gloom dust"] = 1, Honey = 1 } }, |
| 85 | { name = "Sifting", does = "finds flint in gravel", fits = SHOVELS, xp = 25, wants = { ["Gloom dust"] = 1, Flint = 3 } }, |
| 86 | { name = "Owl-eyed", does = "sees in the dark", fits = HEAD, xp = 35, wants = { ["Gloom dust"] = 2, Glowcap = 1 } }, |
| 87 | { name = "Gilled", does = "breathes under water", fits = HEAD, xp = 35, wants = { ["Gloom dust"] = 2, Reeds = 2 } }, |
| 88 | { name = "Springy", does = "jumps higher", fits = FEET, xp = 35, wants = { ["Gloom dust"] = 2, Feather = 2 } }, |
| 89 | { name = "Feathery", does = "falls slowly", fits = FEET, xp = 30, wants = { ["Gloom dust"] = 1, Feather = 3 } }, |
| 90 | { name = "Ashen", does = "can't be burned", fits = join(BODY, LEGS), xp = 40, wants = { ["Gloom dust"] = 2, Ember = 2 } }, |
| 91 | { name = "Fleet", does = "walks quicker", fits = join(LEGS, FEET), xp = 35, wants = { ["Gloom dust"] = 1, Silk = 2 } }, |
| 92 | { name = "Fortunate", does = "brings luck", fits = join(HEAD, BODY), xp = 45, wants = { ["Gloom dust"] = 2, Clover = 3 } }, |
| 93 | } |
| 94 | |
| 95 | local function fits(name: string, list: { string }): boolean |
| 96 | for _, e in list do |
| 97 | if name == e or (string.sub(e, 1, 1) == " " and string.sub(name, -#e) == e) then |
| 98 | return true |
| 99 | end |
| 100 | end |
| 101 | return false |
| 102 | end |
| 103 | |
| 104 | local function cost(c: Charm): number |
| 105 | return math.floor(c.xp * knobs.cost) |
| 106 | end |
| 107 | |
| 108 | -- "2 Gloom dust and 1 Crystal" |
| 109 | local function needs(c: Charm): string |
| 110 | local parts = {} |
| 111 | for name, n in c.wants do |
| 112 | table.insert(parts, n .. " " .. name) |
| 113 | end |
| 114 | table.sort(parts) |
| 115 | return table.concat(parts, " and ") |
| 116 | end |
| 117 | |
| 118 | -- "Swift", "Swift and Keen", "Swift, Sturdy and Keen" |
| 119 | local function list(names: { string }): string |
| 120 | if #names == 1 then |
| 121 | return names[1] |
| 122 | end |
| 123 | return table.concat(names, ", ", 1, #names - 1) .. " and " .. names[#names] |
| 124 | end |
| 125 | |
| 126 | -- What a player's charms make them, kept so it's only said when it changes: |
| 127 | -- what's in their hand and its charms. |
| 128 | local was: { [number]: string } = {} |
| 129 | |
| 130 | -- Digging and hitting by the charms on what's in a player's hand now. |
| 131 | local function apply(p) |
| 132 | local held = world.held(p) |
| 133 | local on: { string } = if held and world.pockets(p) ~= nil then world.charms(p, held) else {} |
| 134 | local now = (held or "") .. ":" .. table.concat(on, ",") |
| 135 | if was[p.id] == now then |
| 136 | return |
| 137 | end |
| 138 | was[p.id] = now |
| 139 | local swift = held ~= nil and table.find(on, "Swift") ~= nil and fits(held, DIGS) |
| 140 | local keen = held ~= nil and table.find(on, "Keen") ~= nil and fits(held, HITS) |
| 141 | world.haste(p, if swift then knobs.swift else 1) |
| 142 | world.might(p, if keen then knobs.keen else 1) |
| 143 | -- and a word of what it carries, as it comes into their hand |
| 144 | if held and #on > 0 then |
| 145 | ui.show(p, "charmed", held .. ": " .. list(on), { at = "bottom" }) |
| 146 | world.after(2.5, function() |
| 147 | if was[p.id] == now then |
| 148 | ui.hide(p, "charmed") |
| 149 | end |
| 150 | end) |
| 151 | else |
| 152 | ui.hide(p, "charmed") |
| 153 | end |
| 154 | end |
| 155 | |
| 156 | world.every(0.25, function() |
| 157 | for _, p in world.players() do |
| 158 | apply(p) |
| 159 | end |
| 160 | end) |
| 161 | |
| 162 | world.on("player_left", function(p) |
| 163 | was[p.id] = nil |
| 164 | end) |
| 165 | |
| 166 | local function close() end |
| 167 | |
| 168 | -- The card holds 300 letters, so a long list of charms is split into pages |
| 169 | -- of whatever fits, with More to turn the page. |
| 170 | local CARD = 300 |
| 171 | |
| 172 | -- The table's question: the charms that fit what's in their hand. |
| 173 | local function offer(player, at: vector, said: string?, page: number?) |
| 174 | if world.pockets(player) == nil then |
| 175 | ui.ask(player, { |
| 176 | title = TABLE, |
| 177 | text = "In survival, a charm table puts charms on tools, weapons and armor, for XP.", |
| 178 | choices = { "OK" }, |
| 179 | }, close) |
| 180 | return |
| 181 | end |
| 182 | local held = world.held(player) |
| 183 | local xp, level = world.xp(player) |
| 184 | local text = (if said then said .. "\n\n" else "") .. "You have " .. xp .. " XP, level " .. level .. "." |
| 185 | local on: { string } = if held then world.charms(player, held) else {} |
| 186 | local can: { Charm } = {} |
| 187 | local any = false |
| 188 | for _, c in CHARMS do |
| 189 | if held and fits(held, c.fits) then |
| 190 | any = true |
| 191 | if not table.find(on, c.name) then |
| 192 | table.insert(can, c) |
| 193 | end |
| 194 | end |
| 195 | end |
| 196 | if not held or not any then |
| 197 | ui.ask(player, { title = TABLE, text = text .. " Hold a tool, a weapon or a piece of armor to charm it.", choices = { "Close" } }, close) |
| 198 | return |
| 199 | end |
| 200 | local name = string.lower(held) |
| 201 | if #on > 0 then |
| 202 | text ..= " Your " .. name .. " is " .. list(on) .. "." |
| 203 | end |
| 204 | if #can == 0 then |
| 205 | ui.ask(player, { title = TABLE, text = text .. " It has every charm it can take.", choices = { "Close" } }, close) |
| 206 | return |
| 207 | end |
| 208 | -- each charm's line, and which go on which page |
| 209 | local lines: { string } = {} |
| 210 | for _, c in can do |
| 211 | table.insert(lines, c.name .. " " .. c.does .. ": " .. cost(c) .. " XP, " .. needs(c) .. ".") |
| 212 | end |
| 213 | local pages: { { number } } = { {} } |
| 214 | local used = #text |
| 215 | for i, line in lines do |
| 216 | local last = pages[#pages] |
| 217 | if #last >= 6 or (#last > 0 and used + 1 + #line > CARD) then |
| 218 | table.insert(pages, {}) |
| 219 | last = pages[#pages] |
| 220 | used = #text |
| 221 | end |
| 222 | table.insert(last, i) |
| 223 | used += 1 + #line |
| 224 | end |
| 225 | local at_page = math.clamp(page or 1, 1, #pages) |
| 226 | local shown = pages[at_page] |
| 227 | local choices: { string } = {} |
| 228 | for _, i in shown do |
| 229 | text ..= "\n" .. lines[i] |
| 230 | table.insert(choices, can[i].name) |
| 231 | end |
| 232 | local more = #pages > 1 |
| 233 | if more then |
| 234 | table.insert(choices, "More") |
| 235 | end |
| 236 | table.insert(choices, "Close") |
| 237 | ui.ask(player, { title = TABLE .. ": " .. held, text = text, choices = choices }, function(who, choice) |
| 238 | if not choice then |
| 239 | return |
| 240 | end |
| 241 | if more and choice == #shown + 1 then |
| 242 | offer(who, at, said, at_page % #pages + 1) |
| 243 | return |
| 244 | end |
| 245 | local c = shown[choice] and can[shown[choice]] |
| 246 | if not c then |
| 247 | return |
| 248 | end |
| 249 | -- they may have changed what's in hand since |
| 250 | if world.held(who) ~= held then |
| 251 | offer(who, at, "Hold your " .. name .. " to charm it.") |
| 252 | return |
| 253 | end |
| 254 | for thing, n in c.wants do |
| 255 | if world.has(who, thing) < n then |
| 256 | offer(who, at, "You'll need " .. needs(c) .. " for that.", at_page) |
| 257 | return |
| 258 | end |
| 259 | end |
| 260 | if not world.spend_xp(who, cost(c)) then |
| 261 | offer(who, at, "You'll need " .. cost(c) .. " XP for that.", at_page) |
| 262 | return |
| 263 | end |
| 264 | for thing, n in c.wants do |
| 265 | world.take(who, thing, n) |
| 266 | end |
| 267 | local now = world.charms(who, held) |
| 268 | table.insert(now, c.name) |
| 269 | world.charm(who, held, now) |
| 270 | sound.play("sparkle", { at = at, caption = "A charm takes" }) |
| 271 | apply(who) |
| 272 | offer(who, at, "Your " .. name .. " is " .. c.name .. " now.") |
| 273 | end) |
| 274 | end |
| 275 | |
| 276 | world.on("block_used", function(player, at: vector, block: string) |
| 277 | if block == TABLE then |
| 278 | offer(player, at) |
| 279 | end |
| 280 | end) |
| 281 | |
| 282 | -- What a Lucky pickaxe sometimes brings up more of, as Mining's ores give. |
| 283 | local ORE_GIVES: { [string]: string } = { |
| 284 | ["Coal ore"] = "Coal", |
| 285 | ["Copper ore"] = "Raw copper", |
| 286 | ["Iron ore"] = "Raw iron", |
| 287 | ["Gold ore"] = "Raw gold", |
| 288 | ["Crystal ore"] = "Crystal", |
| 289 | ["Moonsilver ore"] = "Raw moonsilver", |
| 290 | } |
| 291 | -- the crops a Reaping hoe or sickle brings in more of |
| 292 | local CROPS: { [string]: boolean } = { Wheat = true, Carrots = true, Potatoes = true, Beans = true, Pumpkin = true } |
| 293 | |
| 294 | -- an extra one, with a sparkle |
| 295 | local function extra(player, at: vector, thing: string, caption: string) |
| 296 | if world.give(player, thing, 1) > 0 then |
| 297 | sound.play("sparkle", { at = at, volume = 0.5, caption = caption }) |
| 298 | end |
| 299 | end |
| 300 | |
| 301 | world.on("block_broken", function(player, at: vector, block: string) |
| 302 | local held = world.held(player) |
| 303 | if not held or world.pockets(player) == nil then |
| 304 | return |
| 305 | end |
| 306 | local on = world.charms(player, held) |
| 307 | local gives = ORE_GIVES[block] |
| 308 | if gives and fits(held, PICKS) then |
| 309 | -- Lucky, and the luck perk (a Clover tea or a Fortunate piece) on any pickaxe |
| 310 | local chance = if table.find(on, "Lucky") then knobs.lucky else 0 |
| 311 | local perk = math.min(0.15 * world.perk(player, "luck"), 0.6) |
| 312 | chance = 1 - (1 - chance) * (1 - perk) |
| 313 | if chance > 0 and world.random() < chance then |
| 314 | extra(player, at, gives, "Lucky!") |
| 315 | end |
| 316 | elseif string.sub(block, -4) == " log" and fits(held, AXES) and table.find(on, "Hewing") then |
| 317 | if world.random() < knobs.hewing then |
| 318 | extra(player, at, block, "A good cut") |
| 319 | end |
| 320 | elseif CROPS[block] and fits(held, REAPS) and table.find(on, "Reaping") then |
| 321 | if world.random() < knobs.reaping then |
| 322 | extra(player, at, block, "A good harvest") |
| 323 | end |
| 324 | elseif block == "Gravel" and fits(held, SHOVELS) and table.find(on, "Sifting") then |
| 325 | if world.random() < knobs.sifting then |
| 326 | extra(player, at, "Flint", "Something in the gravel") |
| 327 | end |
| 328 | end |
| 329 | end) |