Pickaxes, axes, shovels and hoes for survival, in wood, stone, copper, iron and moonsilver. Hold one and its kind of block breaks faster, and better ones reach harder ores. They wear as they work and a worn out one breaks. Shears cut leaves and plants, a sickle reaps crops and a hammer breaks stone three by three. Make wooden, stone and copper tools, shears, sickles and hammers at a workbench, and iron and moonsilver ones at an anvil, which mends them all and makes iron and moonsilver armor too.
| 1 | --!strict |
| 2 | -- Tools, for survival. Holding a pickaxe, stone and brick break faster; an |
| 3 | -- axe, wood; a shovel, earth, sand and gravel; a hoe, plants (world.digging, |
| 4 | -- by the kind of block). A tool's level is how hard a block it reaches: |
| 5 | -- ores need a pickaxe of their level or better (the mining creation says |
| 6 | -- which). Wood is level 1, stone 2, copper 3, iron 4 and moonsilver 5. |
| 7 | -- Every block a tool breaks wears it a little (a hoe, every patch it |
| 8 | -- tills), a bar beside your health shows how much it has left, and a worn |
| 9 | -- out tool breaks. |
| 10 | -- Shears cut leaves and plants fast (and get more wool from a Woolly), a |
| 11 | -- sickle cuts plants (and harvests a square of ripe crops for the farming |
| 12 | -- creation), and a hammer breaks stone three by three, leaving ores for a |
| 13 | -- pickaxe (HAMMERED below says what it breaks). |
| 14 | -- The anvil (made at a workbench from iron) makes iron and moonsilver |
| 15 | -- tools and armor (for the Armor creation), and mends a worn tool with one |
| 16 | -- more of what it's made of, or with XP (world.spend_xp). |
| 17 | -- A tool with the Sturdy charm (the Charms creation's, world.charms) wears |
| 18 | -- half as fast. |
| 19 | -- TOOLS below says what each does, so a remix can change them. Swords are |
| 20 | -- for Gloomlings, who say what a sword does to them, and hoes till for the |
| 21 | -- farming creation. |
| 22 | |
| 23 | local world = require("@potluck/world@0.29") |
| 24 | local tweak = require("@potluck/tweak") |
| 25 | local ui = require("@potluck/ui@0.2") |
| 26 | local sound = require("@potluck/sound@0.2") |
| 27 | |
| 28 | local knobs = tweak { |
| 29 | faster = { 1, min = 0.5, max = 3, label = "How much faster tools dig", order = 1 }, |
| 30 | wear = { true, label = "Tools wear out", order = 2 }, |
| 31 | last = { 1, min = 0.5, max = 4, label = "How long tools last", order = 3 }, |
| 32 | color = { "#a8b0b8", label = "Wear bar", order = 4 }, |
| 33 | mend = { 1, min = 1, max = 3, label = "What mending takes", order = 5 }, |
| 34 | mend_xp = { 20, min = 0, max = 100, label = "XP to mend a worn out tool (0 for none)", order = 7 }, |
| 35 | hammer = { true, label = "A hammer breaks three by three", order = 6 }, |
| 36 | } |
| 37 | |
| 38 | type Tool = { digs: string, speed: number, level: number, uses: number, made: string } |
| 39 | |
| 40 | local ANVIL = "Anvil" |
| 41 | |
| 42 | -- What each tool digs faster (soft, hard, wood, plant or glass), how much |
| 43 | -- faster, its level, how many blocks it breaks before it wears out, and |
| 44 | -- what it's made of (which mends it). |
| 45 | local TOOLS: { [string]: Tool } = {} |
| 46 | |
| 47 | -- what each tool needs to dig faster |
| 48 | local KINDS: { [string]: string } = { pickaxe = "hard", axe = "wood", shovel = "soft", hoe = "plant" } |
| 49 | |
| 50 | -- each material: its name in a tool's, what mends it, speed, level and uses |
| 51 | local MATERIALS = { |
| 52 | { name = "Wooden", made = "Planks", speed = 2.5, level = 1, uses = 60 }, |
| 53 | { name = "Stone", made = "Cobblestone", speed = 4, level = 2, uses = 130 }, |
| 54 | { name = "Copper", made = "Copper ingot", speed = 5, level = 3, uses = 200 }, |
| 55 | { name = "Iron", made = "Iron ingot", speed = 6.5, level = 4, uses = 320 }, |
| 56 | { name = "Moonsilver", made = "Moonsilver ingot", speed = 8.5, level = 5, uses = 640 }, |
| 57 | } |
| 58 | |
| 59 | for _, m in MATERIALS do |
| 60 | for kind, digs in KINDS do |
| 61 | -- there are no wooden or stone hoes: a hand tills a block as well |
| 62 | if kind ~= "hoe" or m.level >= 3 then |
| 63 | TOOLS[m.name .. " " .. kind] = { digs = digs, speed = m.speed, level = m.level, uses = m.uses, made = m.made } |
| 64 | end |
| 65 | end |
| 66 | end |
| 67 | |
| 68 | -- and the tools that aren't in every material |
| 69 | TOOLS.Shears = { digs = "plant", speed = 5, level = 1, uses = 240, made = "Iron ingot" } |
| 70 | TOOLS.Sickle = { digs = "plant", speed = 4, level = 1, uses = 200, made = "Copper ingot" } |
| 71 | TOOLS.Hammer = { digs = "hard", speed = 2.5, level = 2, uses = 250, made = "Iron ingot" } |
| 72 | |
| 73 | -- What a hammer breaks around the block it hits (ores and what creations |
| 74 | -- make are left alone). |
| 75 | local HAMMERED: { [string]: boolean } = { |
| 76 | Stone = true, Cobblestone = true, Slate = true, Sandstone = true, ["Mossy brick"] = true, |
| 77 | } |
| 78 | |
| 79 | for name, t in TOOLS do |
| 80 | world.digging(name, { [t.digs] = math.min(t.speed * knobs.faster, 10), level = t.level }) |
| 81 | end |
| 82 | |
| 83 | -- What the anvil makes, for each of its metals: tools, and armor for the |
| 84 | -- Armor creation, and what each takes. |
| 85 | type Recipe = { tool: string, metal: number, planks: number } |
| 86 | local ANVIL_MAKES: { [string]: { Recipe } } = { |
| 87 | tools = { |
| 88 | { tool = "pickaxe", metal = 3, planks = 2 }, |
| 89 | { tool = "axe", metal = 3, planks = 2 }, |
| 90 | { tool = "shovel", metal = 1, planks = 2 }, |
| 91 | { tool = "hoe", metal = 2, planks = 2 }, |
| 92 | { tool = "sword", metal = 2, planks = 1 }, |
| 93 | }, |
| 94 | armor = { |
| 95 | { tool = "helmet", metal = 5, planks = 0 }, |
| 96 | { tool = "coat", metal = 8, planks = 0 }, |
| 97 | { tool = "leggings", metal = 7, planks = 0 }, |
| 98 | { tool = "boots", metal = 4, planks = 0 }, |
| 99 | }, |
| 100 | } |
| 101 | local ANVIL_METALS = { "Iron", "Moonsilver" } |
| 102 | local ANVIL_KINDS = { "tools", "armor" } |
| 103 | |
| 104 | local function uses(t: Tool): number |
| 105 | return math.max(1, math.floor(t.uses * knobs.last)) |
| 106 | end |
| 107 | |
| 108 | -- how many blocks each player's tools have broken, by tool |
| 109 | local worn: { [number]: { [string]: number } } = {} |
| 110 | |
| 111 | -- One more use of what's in a survival player's hand, if it's a tool: |
| 112 | -- it breaks when it's worn out. |
| 113 | local function wear(player) |
| 114 | if not knobs.wear or world.pockets(player) == nil then |
| 115 | return |
| 116 | end |
| 117 | local held = world.held(player) |
| 118 | local t = held and TOOLS[held] |
| 119 | if not held or not t then |
| 120 | return |
| 121 | end |
| 122 | -- a Sturdy one wears half as fast |
| 123 | if table.find(world.charms(player, held), "Sturdy") and world.random() < 0.5 then |
| 124 | return |
| 125 | end |
| 126 | local mine = worn[player.id] or {} |
| 127 | worn[player.id] = mine |
| 128 | mine[held] = (mine[held] or 0) + 1 |
| 129 | if mine[held] >= uses(t) then |
| 130 | mine[held] = 0 |
| 131 | if world.take(player, held, 1) then |
| 132 | -- its charms go with it, unless they've another |
| 133 | if world.has(player, held) == 0 then |
| 134 | world.charm(player, held, {}) |
| 135 | end |
| 136 | ui.show(player, "broke", "Your " .. string.lower(held) .. " broke.", { at = "bottom" }) |
| 137 | world.after(3, function() |
| 138 | ui.hide(player, "broke") |
| 139 | end) |
| 140 | sound.play_for(player, "crack", { pitch = 1.3, caption = "A tool breaks" }) |
| 141 | end |
| 142 | end |
| 143 | end |
| 144 | |
| 145 | -- A hammer's blow: the eight blocks around the one it broke, across the |
| 146 | -- way the player looks at it, that HAMMERED says it breaks. |
| 147 | local function blow(player, at: vector) |
| 148 | local d = at - (player.position + vector.create(0, 1.6, 0)) |
| 149 | local ax, ay, az = math.abs(d.x), math.abs(d.y), math.abs(d.z) |
| 150 | for i = -1, 1 do |
| 151 | for j = -1, 1 do |
| 152 | if i ~= 0 or j ~= 0 then |
| 153 | local off = if ax >= ay and ax >= az |
| 154 | then vector.create(0, i, j) |
| 155 | elseif ay >= az then vector.create(i, 0, j) |
| 156 | else vector.create(i, j, 0) |
| 157 | local q = at + off |
| 158 | local b = world.block(q) |
| 159 | if HAMMERED[b] then |
| 160 | world.set_block(q, "Air") |
| 161 | world.give(player, b, 1) |
| 162 | end |
| 163 | end |
| 164 | end |
| 165 | end |
| 166 | end |
| 167 | |
| 168 | world.on("block_broken", function(player, at: vector, block: string) |
| 169 | -- a hoe wears as it tills, not as it breaks things |
| 170 | local held = world.held(player) |
| 171 | if held and string.sub(held, -4) == " hoe" then |
| 172 | return |
| 173 | end |
| 174 | if held == "Hammer" and knobs.hammer and HAMMERED[block] and world.pockets(player) ~= nil then |
| 175 | blow(player, at) |
| 176 | end |
| 177 | wear(player) |
| 178 | end) |
| 179 | |
| 180 | -- a hoe wears a little each time it tills (the farming creation tills |
| 181 | -- grass or dirt with air over it) |
| 182 | world.on("block_used", function(player, at: vector, block: string) |
| 183 | local held = world.held(player) |
| 184 | if held and string.sub(held, -4) == " hoe" and (block == "Grass" or block == "Dirt") and world.block(at + vector.create(0, 1, 0)) == "Air" then |
| 185 | wear(player) |
| 186 | end |
| 187 | end) |
| 188 | |
| 189 | -- who's been told what the bar means |
| 190 | local told: { [number]: boolean } = {} |
| 191 | |
| 192 | -- the wear bar, while a tool is in hand |
| 193 | world.every(0.5, function() |
| 194 | for _, p in world.players() do |
| 195 | local held = world.held(p) |
| 196 | local t = held and TOOLS[held] |
| 197 | if knobs.wear and held and t and world.pockets(p) ~= nil then |
| 198 | local used = (worn[p.id] or {})[held] or 0 |
| 199 | -- in twentieths, so it isn't sent for every block |
| 200 | local left = math.ceil((1 - used / uses(t)) * 20) / 20 |
| 201 | ui.meter(p, "wear", left, { color = knobs.color }) |
| 202 | if not told[p.id] then |
| 203 | told[p.id] = true |
| 204 | ui.show(p, "wear_says", "The new bar is your tool's wear. It breaks when empty.", { at = "left" }) |
| 205 | world.after(6, function() |
| 206 | ui.hide(p, "wear_says") |
| 207 | end) |
| 208 | end |
| 209 | else |
| 210 | ui.meter(p, "wear", nil) |
| 211 | end |
| 212 | end |
| 213 | end) |
| 214 | |
| 215 | -- The anvil. |
| 216 | |
| 217 | -- "3 Iron ingot and 2 Planks" |
| 218 | local function needs(metal: string, r: Recipe): string |
| 219 | return r.metal .. " " .. metal .. " ingot" .. (if r.planks > 0 then " and " .. r.planks .. " Planks" else "") |
| 220 | end |
| 221 | |
| 222 | local function title(s: string): string |
| 223 | return string.upper(string.sub(s, 1, 1)) .. string.sub(s, 2) |
| 224 | end |
| 225 | |
| 226 | local anvil |
| 227 | |
| 228 | -- The tools or armor of one metal: pick one to make. |
| 229 | local function make(player, at: vector, metal: string, kind: string, said: string?) |
| 230 | local list = ANVIL_MAKES[kind] |
| 231 | local choices = {} |
| 232 | local lines = {} |
| 233 | for _, r in list do |
| 234 | table.insert(choices, title(r.tool)) |
| 235 | table.insert(lines, title(r.tool) .. ": " .. needs(metal, r)) |
| 236 | end |
| 237 | table.insert(choices, "Back") |
| 238 | local text = (if said then said .. "\n\n" else "") .. table.concat(lines, "\n") |
| 239 | ui.ask(player, { title = "Anvil: " .. metal .. " " .. kind, text = text, choices = choices }, function(who, choice) |
| 240 | local r = choice and list[choice] |
| 241 | if choice == #list + 1 then |
| 242 | anvil(who, at) |
| 243 | return |
| 244 | end |
| 245 | if not r then |
| 246 | return |
| 247 | end |
| 248 | local ingot = metal .. " ingot" |
| 249 | if world.has(who, ingot) < r.metal or world.has(who, "Planks") < r.planks then |
| 250 | make(who, at, metal, kind, "You'll need " .. needs(metal, r) .. " for that.") |
| 251 | return |
| 252 | end |
| 253 | local tool = metal .. " " .. r.tool |
| 254 | world.take(who, ingot, r.metal) |
| 255 | if r.planks > 0 then |
| 256 | world.take(who, "Planks", r.planks) |
| 257 | end |
| 258 | if world.give(who, tool, 1) == 0 then |
| 259 | world.give(who, ingot, r.metal) |
| 260 | if r.planks > 0 then |
| 261 | world.give(who, "Planks", r.planks) |
| 262 | end |
| 263 | make(who, at, metal, kind, "Your pockets are full. Make some room first.") |
| 264 | return |
| 265 | end |
| 266 | sound.play("clink", { at = at }) |
| 267 | -- "Made an iron pickaxe." |
| 268 | local a = if string.find(string.lower(tool), "^[aeiou]") then "an " else "a " |
| 269 | local made = if r.tool == "leggings" or r.tool == "boots" then "Made " .. string.lower(tool) .. "." else "Made " .. a .. string.lower(tool) .. "." |
| 270 | make(who, at, metal, kind, made) |
| 271 | end) |
| 272 | end |
| 273 | |
| 274 | -- XP to mend a tool this worn: the knob's for a worn out one, less for |
| 275 | -- one that's only a little worn, and never less than 1. |
| 276 | local function mend_cost(t: Tool, used: number): number |
| 277 | return math.max(1, math.ceil(knobs.mend_xp * used / uses(t))) |
| 278 | end |
| 279 | |
| 280 | -- Mends what's in their hand with XP, or says why not. |
| 281 | local function mend_with_xp(player): string |
| 282 | local held = world.held(player) |
| 283 | local t = held and TOOLS[held] |
| 284 | if not held or not t then |
| 285 | return "Hold the tool you'd like mended." |
| 286 | end |
| 287 | local used = (worn[player.id] or {})[held] or 0 |
| 288 | if used == 0 then |
| 289 | return "Your " .. string.lower(held) .. " is as good as new." |
| 290 | end |
| 291 | local cost = mend_cost(t, used) |
| 292 | if not world.spend_xp(player, cost) then |
| 293 | return "Mending your " .. string.lower(held) .. " takes " .. cost .. " XP." |
| 294 | end |
| 295 | worn[player.id][held] = 0 |
| 296 | return "Mended your " .. string.lower(held) .. "." |
| 297 | end |
| 298 | |
| 299 | -- Mends what's in their hand, or says why not. |
| 300 | local function mend(player): string |
| 301 | local held = world.held(player) |
| 302 | local t = held and TOOLS[held] |
| 303 | if not held or not t then |
| 304 | return "Hold the tool you'd like mended." |
| 305 | end |
| 306 | local used = (worn[player.id] or {})[held] or 0 |
| 307 | if used == 0 then |
| 308 | return "Your " .. string.lower(held) .. " is as good as new." |
| 309 | end |
| 310 | local n = math.floor(knobs.mend) |
| 311 | if world.has(player, t.made) < n then |
| 312 | return "Mending your " .. string.lower(held) .. " takes " .. n .. " " .. t.made .. "." |
| 313 | end |
| 314 | world.take(player, t.made, n) |
| 315 | worn[player.id][held] = 0 |
| 316 | return "Mended your " .. string.lower(held) .. "." |
| 317 | end |
| 318 | |
| 319 | function anvil(player, at: vector, said: string?) |
| 320 | if world.pockets(player) == nil then |
| 321 | ui.ask(player, { |
| 322 | title = "Anvil", |
| 323 | text = "In survival, an anvil makes iron and moonsilver tools and armor, and mends worn tools.", |
| 324 | choices = { "OK" }, |
| 325 | }, function() end) |
| 326 | return |
| 327 | end |
| 328 | local held = world.held(player) |
| 329 | local t = held and TOOLS[held] |
| 330 | local choices = {} |
| 331 | local text = if said then said .. "\n\n" else "" |
| 332 | -- with XP to mend with, a second way to mend |
| 333 | local by_xp = false |
| 334 | if held and t then |
| 335 | local used = (worn[player.id] or {})[held] or 0 |
| 336 | local left = uses(t) - used |
| 337 | text ..= "Your " .. string.lower(held) .. " has " .. left .. " of " .. uses(t) .. " uses left. Mending it takes " .. math.floor(knobs.mend) .. " " .. t.made |
| 338 | table.insert(choices, "Mend your " .. string.lower(held)) |
| 339 | if knobs.mend_xp > 0 and used > 0 then |
| 340 | by_xp = true |
| 341 | text ..= ", or " .. mend_cost(t, used) .. " XP" |
| 342 | table.insert(choices, "Mend it with XP") |
| 343 | end |
| 344 | text ..= "." |
| 345 | else |
| 346 | text ..= "Hold a worn tool to mend it, or make a new one." |
| 347 | end |
| 348 | local makes = {} |
| 349 | for _, metal in ANVIL_METALS do |
| 350 | for _, kind in ANVIL_KINDS do |
| 351 | table.insert(choices, "Make " .. string.lower(metal) .. " " .. kind) |
| 352 | table.insert(makes, { metal, kind }) |
| 353 | end |
| 354 | end |
| 355 | ui.ask(player, { title = "Anvil", text = text, choices = choices }, function(who, choice) |
| 356 | if not choice then |
| 357 | return |
| 358 | end |
| 359 | if held and t and (choice == 1 or (by_xp and choice == 2)) then |
| 360 | local result = if choice == 1 then mend(who) else mend_with_xp(who) |
| 361 | if string.sub(result, 1, 6) == "Mended" then |
| 362 | sound.play("clink", { at = at }) |
| 363 | end |
| 364 | anvil(who, at, result) |
| 365 | return |
| 366 | end |
| 367 | local m = makes[choice - (if held and t then 1 else 0) - (if by_xp then 1 else 0)] |
| 368 | if m then |
| 369 | make(who, at, m[1], m[2]) |
| 370 | end |
| 371 | end) |
| 372 | end |
| 373 | |
| 374 | world.on("block_used", function(player, at: vector, block: string) |
| 375 | if block == ANVIL then |
| 376 | anvil(player, at) |
| 377 | end |
| 378 | end) |
| 379 | |
| 380 | world.on("player_left", function(p) |
| 381 | worn[p.id] = nil |
| 382 | told[p.id] = nil |
| 383 | end) |
| 384 | |
| 385 | -- Told to the item guide: what the anvil makes (world.recipes). |
| 386 | do |
| 387 | local book = {} |
| 388 | for _, metal in ANVIL_METALS do |
| 389 | for _, kind in ANVIL_KINDS do |
| 390 | for _, m in ANVIL_MAKES[kind] do |
| 391 | local want = { [metal .. " ingot"] = m.metal } |
| 392 | if m.planks > 0 then |
| 393 | want.Planks = m.planks |
| 394 | end |
| 395 | table.insert(book, { give = metal .. " " .. m.tool, n = 1, want = want }) |
| 396 | end |
| 397 | end |
| 398 | end |
| 399 | world.recipes("Anvil", book) |
| 400 | end |