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 | -- The Charms creation's own tests: `potluck-server --test=charms`. |
| 3 | |
| 4 | local test = require("@potluck/test@0.9") |
| 5 | local world = require("@potluck/world@0.34") |
| 6 | |
| 7 | -- a survival player carrying `carrying`, with `xp` XP, and a charm table |
| 8 | -- beside them |
| 9 | local function at_a_table(carrying: { [string]: number }, xp: number): (any, vector) |
| 10 | local p = test.player("Ada", { survival = true, carrying = carrying }) |
| 11 | test.same(world.reward(p, xp), xp, "the XP to spend") |
| 12 | local x, z = p.position.x + 2, p.position.z |
| 13 | local at = vector.create(x, world.top(x, z) + 1, z) |
| 14 | world.set_block(at, "Charm table") |
| 15 | return p, at |
| 16 | end |
| 17 | |
| 18 | -- what the question in front of them says |
| 19 | local function asked(p): (string, string, { string }) |
| 20 | local q = test.asked(p) |
| 21 | test.check(q ~= nil, "a question") |
| 22 | local a = q :: any |
| 23 | return a.title, a.text, a.choices |
| 24 | end |
| 25 | |
| 26 | test.case("a charm table makes the pickaxe in hand Swift, for XP and gloom dust", function() |
| 27 | local p, at = at_a_table({ ["Iron pickaxe"] = 1, ["Gloom dust"] = 4, Crystal = 1 }, 100) |
| 28 | test.hold(p, "Iron pickaxe") |
| 29 | test.use_block(p, at) |
| 30 | local title, text, choices = asked(p) |
| 31 | test.same(title, "Charm table: Iron pickaxe") |
| 32 | test.check(string.find(text, "You have 100 XP, level 4.", 1, true) == 1, text) |
| 33 | test.same(choices[1], "Swift") |
| 34 | test.same(choices[2], "Sturdy") |
| 35 | test.same(choices[3], "Lucky") |
| 36 | test.same(choices[4], "Close") |
| 37 | test.answer(p, 1) |
| 38 | local _, now = asked(p) |
| 39 | test.check(string.find(now, "Your iron pickaxe is Swift now.", 1, true) == 1, now) |
| 40 | test.same(world.charms(p, "Iron pickaxe")[1], "Swift", "the charm") |
| 41 | test.same(world.xp(p), 60, "the XP it took") |
| 42 | test.same(world.has(p, "Gloom dust"), 2, "the gloom dust it took") |
| 43 | test.same(world.has(p, "Crystal"), 0, "the crystal it took") |
| 44 | test.same(test.heard("sparkle"), 1, "a sparkle") |
| 45 | test.wait(0.5) |
| 46 | test.same(world.haste(p), 1.5, "digs faster with it in hand") |
| 47 | test.same(test.shown(p, "charmed"), "Iron pickaxe: Swift", "says so") |
| 48 | test.hold(p, nil) |
| 49 | test.wait(0.5) |
| 50 | test.same(world.haste(p), 1, "and as usual without") |
| 51 | end) |
| 52 | |
| 53 | test.case("without the XP, it charms nothing and takes nothing", function() |
| 54 | local p, at = at_a_table({ ["Stone shovel"] = 1, ["Gloom dust"] = 4, Crystal = 1 }, 10) |
| 55 | test.hold(p, "Stone shovel") |
| 56 | test.use_block(p, at) |
| 57 | test.answer(p, 1) |
| 58 | local _, text = asked(p) |
| 59 | test.check(string.find(text, "You'll need 40 XP for that.", 1, true) == 1, text) |
| 60 | test.same(#world.charms(p, "Stone shovel"), 0, "no charm") |
| 61 | test.same(world.xp(p), 10, "the XP still there") |
| 62 | test.same(world.has(p, "Gloom dust"), 4, "the gloom dust still there") |
| 63 | end) |
| 64 | |
| 65 | test.case("a Keen sword hits harder, and a charmed tool takes no charm twice", function() |
| 66 | local p, at = at_a_table({ ["Iron sword"] = 1, ["Gloom dust"] = 3, ["Gold ingot"] = 1 }, 100) |
| 67 | test.hold(p, "Iron sword") |
| 68 | test.use_block(p, at) |
| 69 | local _, _, choices = asked(p) |
| 70 | test.same(choices[1], "Keen") |
| 71 | test.same(#choices, 2, "only Keen fits a sword") |
| 72 | test.answer(p, 1) |
| 73 | test.wait(0.5) |
| 74 | test.same(world.might(p), 1.5, "hits harder") |
| 75 | test.use_block(p, at) |
| 76 | local _, text = asked(p) |
| 77 | test.check(string.find(text, "It has every charm it can take.", 1, true) ~= nil, text) |
| 78 | end) |
| 79 | |
| 80 | test.case("an empty hand has nothing to charm", function() |
| 81 | local p, at = at_a_table({}, 20) |
| 82 | test.hold(p, nil) |
| 83 | test.use_block(p, at) |
| 84 | local _, text = asked(p) |
| 85 | test.check(string.find(text, "Hold a tool, a weapon or a piece of armor", 1, true) ~= nil, text) |
| 86 | end) |
| 87 | |
| 88 | test.case("in creative, the table says what it's for", function() |
| 89 | local p = test.player("Bo") |
| 90 | local x, z = p.position.x + 2, p.position.z |
| 91 | local at = vector.create(x, world.top(x, z) + 1, z) |
| 92 | world.set_block(at, "Charm table") |
| 93 | test.use_block(p, at) |
| 94 | local _, text = asked(p) |
| 95 | test.check(string.find(text, "survival", 1, true) ~= nil, text) |
| 96 | end) |
| 97 | |
| 98 | test.case("a Warm charm goes on armor", function() |
| 99 | local p, at = at_a_table({ ["Wool coat"] = 1, ["Gloom dust"] = 1, Ember = 1 }, 30) |
| 100 | test.hold(p, "Wool coat") |
| 101 | test.use_block(p, at) |
| 102 | local _, _, choices = asked(p) |
| 103 | test.same(choices[1], "Warm") |
| 104 | test.same(choices[2], "Stout") |
| 105 | test.same(choices[3], "Hearty") |
| 106 | test.check(table.find(choices, "More") ~= nil, "five charms fit a coat, over two pages") |
| 107 | test.answer(p, 1) |
| 108 | test.same(world.charms(p, "Wool coat")[1], "Warm", "the charm") |
| 109 | test.same(world.has(p, "Ember"), 0, "the ember it took") |
| 110 | test.same(world.xp(p), 10, "and the XP") |
| 111 | end) |
| 112 | |
| 113 | test.case("a Lucky pickaxe now and then brings up more ore", function() |
| 114 | local p = test.player("Cy", { survival = true, carrying = { ["Iron pickaxe"] = 1 } }) |
| 115 | world.charm(p, "Iron pickaxe", { "Lucky" }) |
| 116 | test.hold(p, "Iron pickaxe") |
| 117 | local at = p.position + vector.create(2, 0, 0) |
| 118 | local x, z = math.floor(at.x), math.floor(at.z) |
| 119 | local y = world.top(x, z) + 1 |
| 120 | for _ = 1, 30 do |
| 121 | local ore = vector.create(x + 0.5, y + 0.5, z + 0.5) |
| 122 | world.set_block(ore, "Coal ore") |
| 123 | test.dig(p, ore) |
| 124 | end |
| 125 | local extra = world.has(p, "Coal") |
| 126 | test.check(extra > 0 and extra < 30, "some extra coal: " .. extra) |
| 127 | end) |
| 128 | |
| 129 | test.case("a sling can be Keen or Thrifty", function() |
| 130 | local p, at = at_a_table({ Sling = 1, ["Gloom dust"] = 1, Feather = 3 }, 40) |
| 131 | test.hold(p, "Sling") |
| 132 | test.use_block(p, at) |
| 133 | local _, _, choices = asked(p) |
| 134 | test.same(choices[1], "Keen") |
| 135 | test.same(choices[2], "Thrifty") |
| 136 | test.same(#choices, 3, "two charms fit a sling") |
| 137 | test.answer(p, 2) |
| 138 | test.same(world.charms(p, "Sling")[1], "Thrifty", "the charm") |
| 139 | end) |
| 140 | |
| 141 | |
| 142 | -- the charms on armor give the Armor creation's perks, one piece at a time |
| 143 | test.case("boots take Springy, Feathery or Fleet, over two pages of the card", function() |
| 144 | local p, at = at_a_table({ ["Iron boots"] = 1, ["Gloom dust"] = 4, Feather = 6, Silk = 2 }, 100) |
| 145 | test.hold(p, "Iron boots") |
| 146 | test.use_block(p, at) |
| 147 | local title, text, choices = asked(p) |
| 148 | test.same(title, "Charm table: Iron boots") |
| 149 | test.check(#text <= 300, "the card fits: " .. #text) |
| 150 | -- six charms fit boots (Warm, Stout, Hearty, Springy, Feathery, Fleet) |
| 151 | -- and they don't all fit one card: More turns the page |
| 152 | local seen: { [string]: boolean } = {} |
| 153 | local first = table.concat(choices, ",") |
| 154 | local pages = 0 |
| 155 | repeat |
| 156 | pages += 1 |
| 157 | for _, name in choices do |
| 158 | if name ~= "More" and name ~= "Close" then |
| 159 | seen[name] = true |
| 160 | end |
| 161 | end |
| 162 | local more = table.find(choices, "More") |
| 163 | test.check(more ~= nil, "More is on every page") |
| 164 | test.answer(p, more :: number) |
| 165 | local _, now_text, now = asked(p) |
| 166 | test.check(#now_text <= 300, "the next card fits too: " .. #now_text) |
| 167 | choices = now |
| 168 | until table.concat(choices, ",") == first or pages > 4 |
| 169 | local count = 0 |
| 170 | for _ in seen do |
| 171 | count += 1 |
| 172 | end |
| 173 | test.same(count, 6, "all six turn up") |
| 174 | test.same(pages, 2, "over two pages") |
| 175 | if not table.find(choices, "Springy") then |
| 176 | test.answer(p, table.find(choices, "More") :: number) |
| 177 | local _, _, now = asked(p) |
| 178 | choices = now |
| 179 | end |
| 180 | test.answer(p, table.find(choices, "Springy") :: number) |
| 181 | test.same(world.charms(p, "Iron boots")[1], "Springy", "the charm") |
| 182 | test.same(world.has(p, "Feather"), 4, "two feathers it took") |
| 183 | end) |
| 184 | |
| 185 | test.case("a helmet can be Owl-eyed or Gilled", function() |
| 186 | local p, at = at_a_table({ ["Iron helmet"] = 1, ["Gloom dust"] = 2, Glowcap = 1 }, 100) |
| 187 | test.hold(p, "Iron helmet") |
| 188 | test.use_block(p, at) |
| 189 | local _, _, choices = asked(p) |
| 190 | test.check(table.find(choices, "Owl-eyed") ~= nil, "Owl-eyed fits a helmet") |
| 191 | local more = table.find(choices, "More") |
| 192 | test.check(more ~= nil, "and the rest are on the next page") |
| 193 | test.answer(p, more :: number) |
| 194 | local _, _, next_page = asked(p) |
| 195 | test.check(table.find(next_page, "Gilled") ~= nil, "Gilled is there") |
| 196 | test.answer(p, table.find(next_page, "More") :: number) |
| 197 | local _, _, back = asked(p) |
| 198 | choices = back |
| 199 | test.answer(p, table.find(choices, "Owl-eyed") :: number) |
| 200 | test.same(world.charms(p, "Iron helmet")[1], "Owl-eyed", "the charm") |
| 201 | test.same(world.has(p, "Glowcap"), 0, "the glowcap it took") |
| 202 | end) |
| 203 | |
| 204 | test.case("Hewing brings down extra logs, Reaping extra crops and Sifting flint", function() |
| 205 | local p = test.player("Dee", { survival = true, carrying = { ["Iron axe"] = 1, ["Iron hoe"] = 1, ["Iron shovel"] = 1 } }) |
| 206 | world.charm(p, "Iron axe", { "Hewing" }) |
| 207 | world.charm(p, "Iron shovel", { "Sifting" }) |
| 208 | local at = p.position + vector.create(2, 0, 0) |
| 209 | local x, z = math.floor(at.x), math.floor(at.z) |
| 210 | local y = world.top(x, z) + 1 |
| 211 | local spot = vector.create(x + 0.5, y + 0.5, z + 0.5) |
| 212 | test.hold(p, "Iron axe") |
| 213 | for _ = 1, 40 do |
| 214 | world.set_block(spot, "Oak log") |
| 215 | test.dig(p, spot) |
| 216 | end |
| 217 | local logs = world.has(p, "Oak log") |
| 218 | test.check(logs > 40 and logs < 80, "some extra logs: " .. logs) |
| 219 | -- an axe without it brings no extra |
| 220 | world.charm(p, "Iron axe", {}) |
| 221 | local before = world.has(p, "Oak log") |
| 222 | for _ = 1, 20 do |
| 223 | world.set_block(spot, "Oak log") |
| 224 | test.dig(p, spot) |
| 225 | end |
| 226 | test.same(world.has(p, "Oak log") - before, 20, "no extras without the charm") |
| 227 | test.hold(p, "Iron shovel") |
| 228 | for _ = 1, 40 do |
| 229 | world.set_block(spot, "Gravel") |
| 230 | test.dig(p, spot) |
| 231 | end |
| 232 | test.check(world.has(p, "Flint") > 0, "flint turned up in the gravel: " .. world.has(p, "Flint")) |
| 233 | end) |
| 234 | |
| 235 | test.case("luck makes any pickaxe find more ore", function() |
| 236 | local p = test.player("Em", { survival = true, carrying = { ["Iron pickaxe"] = 1 } }) |
| 237 | test.hold(p, "Iron pickaxe") |
| 238 | local at = p.position + vector.create(2, 0, 0) |
| 239 | local x, z = math.floor(at.x), math.floor(at.z) |
| 240 | local y = world.top(x, z) + 1 |
| 241 | local function mine(n: number): number |
| 242 | local before = world.has(p, "Coal") |
| 243 | for _ = 1, n do |
| 244 | local ore = vector.create(x + 0.5, y + 0.5, z + 0.5) |
| 245 | world.set_block(ore, "Coal ore") |
| 246 | test.dig(p, ore) |
| 247 | end |
| 248 | return world.has(p, "Coal") - before |
| 249 | end |
| 250 | local plain = mine(60) |
| 251 | world.perk(p, "luck", 4) |
| 252 | local lucky = mine(60) |
| 253 | test.check(lucky > plain + 20, "luck finds more: " .. plain .. " then " .. lucky .. " (ores give 1 to 2 each)") |
| 254 | end) |