Make tool upgrades cost money

This commit is contained in:
2026-11-01 22:48:49 -03:00
parent 12702520dd
commit c1871a7bf7
37 changed files with 185 additions and 23 deletions
+3 -3
View File
@@ -68,9 +68,9 @@ For the smithing table, which upgrades a tool. It's the base game's own type, re
} }
``` ```
An optional `addition` is a third item it also takes. Upgrading uses up the tool, `count` of the material and the An optional `addition` is a third item it also takes, as `{ "id", "count" }` like the material. Upgrading uses up the
addition, and the result keeps the tool's `data`, like its [durability](../items/tools.md#durability). Two smithing tool and `count` of the material and the addition, and the result keeps the tool's `data`, like its
recipes with the same tool, material and addition are an error. [durability](../items/tools.md#durability). Two smithing recipes with the same tool, material and addition are an error.
## Stonecutting: `bworld:stonecutting` ## Stonecutting: `bworld:stonecutting`
@@ -7,6 +7,10 @@
"id": "bworld:cobalt_ingot", "id": "bworld:cobalt_ingot",
"count": 5 "count": 5
}, },
"addition": {
"id": "bworld:cobalt_coin",
"count": 2
},
"result": "bworld:cobalt_axe" "result": "bworld:cobalt_axe"
} }
} }
@@ -7,6 +7,10 @@
"id": "bworld:cobalt_ingot", "id": "bworld:cobalt_ingot",
"count": 5 "count": 5
}, },
"addition": {
"id": "bworld:cobalt_coin",
"count": 2
},
"result": "bworld:cobalt_hoe" "result": "bworld:cobalt_hoe"
} }
} }
@@ -7,6 +7,10 @@
"id": "bworld:cobalt_ingot", "id": "bworld:cobalt_ingot",
"count": 5 "count": 5
}, },
"addition": {
"id": "bworld:cobalt_coin",
"count": 2
},
"result": "bworld:cobalt_knife" "result": "bworld:cobalt_knife"
} }
} }
@@ -7,6 +7,10 @@
"id": "bworld:cobalt_ingot", "id": "bworld:cobalt_ingot",
"count": 5 "count": 5
}, },
"addition": {
"id": "bworld:cobalt_coin",
"count": 2
},
"result": "bworld:cobalt_pickaxe" "result": "bworld:cobalt_pickaxe"
} }
} }
@@ -7,6 +7,10 @@
"id": "bworld:cobalt_ingot", "id": "bworld:cobalt_ingot",
"count": 5 "count": 5
}, },
"addition": {
"id": "bworld:cobalt_coin",
"count": 2
},
"result": "bworld:cobalt_shovel" "result": "bworld:cobalt_shovel"
} }
} }
@@ -7,6 +7,10 @@
"id": "bworld:cobalt_ingot", "id": "bworld:cobalt_ingot",
"count": 5 "count": 5
}, },
"addition": {
"id": "bworld:cobalt_coin",
"count": 2
},
"result": "bworld:cobalt_watering_can" "result": "bworld:cobalt_watering_can"
} }
} }
@@ -7,6 +7,10 @@
"id": "bworld:copper_ingot", "id": "bworld:copper_ingot",
"count": 5 "count": 5
}, },
"addition": {
"id": "bworld:brass_coin",
"count": 5
},
"result": "bworld:copper_axe" "result": "bworld:copper_axe"
} }
} }
@@ -7,6 +7,10 @@
"id": "bworld:copper_ingot", "id": "bworld:copper_ingot",
"count": 5 "count": 5
}, },
"addition": {
"id": "bworld:brass_coin",
"count": 5
},
"result": "bworld:copper_hoe" "result": "bworld:copper_hoe"
} }
} }
@@ -7,6 +7,10 @@
"id": "bworld:copper_ingot", "id": "bworld:copper_ingot",
"count": 5 "count": 5
}, },
"addition": {
"id": "bworld:brass_coin",
"count": 5
},
"result": "bworld:copper_knife" "result": "bworld:copper_knife"
} }
} }
@@ -7,6 +7,10 @@
"id": "bworld:copper_ingot", "id": "bworld:copper_ingot",
"count": 5 "count": 5
}, },
"addition": {
"id": "bworld:brass_coin",
"count": 5
},
"result": "bworld:copper_pickaxe" "result": "bworld:copper_pickaxe"
} }
} }
@@ -7,6 +7,10 @@
"id": "bworld:copper_ingot", "id": "bworld:copper_ingot",
"count": 5 "count": 5
}, },
"addition": {
"id": "bworld:brass_coin",
"count": 5
},
"result": "bworld:copper_shovel" "result": "bworld:copper_shovel"
} }
} }
@@ -7,6 +7,10 @@
"id": "bworld:copper_ingot", "id": "bworld:copper_ingot",
"count": 5 "count": 5
}, },
"addition": {
"id": "bworld:brass_coin",
"count": 5
},
"result": "bworld:copper_watering_can" "result": "bworld:copper_watering_can"
} }
} }
@@ -7,6 +7,10 @@
"id": "bworld:gold_ingot", "id": "bworld:gold_ingot",
"count": 5 "count": 5
}, },
"addition": {
"id": "bworld:gold_coin",
"count": 1
},
"result": "bworld:gold_axe" "result": "bworld:gold_axe"
} }
} }
@@ -7,6 +7,10 @@
"id": "bworld:gold_ingot", "id": "bworld:gold_ingot",
"count": 5 "count": 5
}, },
"addition": {
"id": "bworld:gold_coin",
"count": 1
},
"result": "bworld:gold_hoe" "result": "bworld:gold_hoe"
} }
} }
@@ -7,6 +7,10 @@
"id": "bworld:gold_ingot", "id": "bworld:gold_ingot",
"count": 5 "count": 5
}, },
"addition": {
"id": "bworld:gold_coin",
"count": 1
},
"result": "bworld:gold_knife" "result": "bworld:gold_knife"
} }
} }
@@ -7,6 +7,10 @@
"id": "bworld:gold_ingot", "id": "bworld:gold_ingot",
"count": 5 "count": 5
}, },
"addition": {
"id": "bworld:gold_coin",
"count": 1
},
"result": "bworld:gold_pickaxe" "result": "bworld:gold_pickaxe"
} }
} }
@@ -7,6 +7,10 @@
"id": "bworld:gold_ingot", "id": "bworld:gold_ingot",
"count": 5 "count": 5
}, },
"addition": {
"id": "bworld:gold_coin",
"count": 1
},
"result": "bworld:gold_shovel" "result": "bworld:gold_shovel"
} }
} }
@@ -7,6 +7,10 @@
"id": "bworld:gold_ingot", "id": "bworld:gold_ingot",
"count": 5 "count": 5
}, },
"addition": {
"id": "bworld:gold_coin",
"count": 1
},
"result": "bworld:gold_watering_can" "result": "bworld:gold_watering_can"
} }
} }
@@ -7,6 +7,10 @@
"id": "bworld:iron_ingot", "id": "bworld:iron_ingot",
"count": 5 "count": 5
}, },
"addition": {
"id": "bworld:iron_coin",
"count": 2
},
"result": "bworld:iron_axe" "result": "bworld:iron_axe"
} }
} }
@@ -7,6 +7,10 @@
"id": "bworld:iron_ingot", "id": "bworld:iron_ingot",
"count": 5 "count": 5
}, },
"addition": {
"id": "bworld:iron_coin",
"count": 2
},
"result": "bworld:iron_hoe" "result": "bworld:iron_hoe"
} }
} }
@@ -7,6 +7,10 @@
"id": "bworld:iron_ingot", "id": "bworld:iron_ingot",
"count": 5 "count": 5
}, },
"addition": {
"id": "bworld:iron_coin",
"count": 2
},
"result": "bworld:iron_knife" "result": "bworld:iron_knife"
} }
} }
@@ -7,6 +7,10 @@
"id": "bworld:iron_ingot", "id": "bworld:iron_ingot",
"count": 5 "count": 5
}, },
"addition": {
"id": "bworld:iron_coin",
"count": 2
},
"result": "bworld:iron_pickaxe" "result": "bworld:iron_pickaxe"
} }
} }
@@ -7,6 +7,10 @@
"id": "bworld:iron_ingot", "id": "bworld:iron_ingot",
"count": 5 "count": 5
}, },
"addition": {
"id": "bworld:iron_coin",
"count": 2
},
"result": "bworld:iron_shovel" "result": "bworld:iron_shovel"
} }
} }
@@ -7,6 +7,10 @@
"id": "bworld:iron_ingot", "id": "bworld:iron_ingot",
"count": 5 "count": 5
}, },
"addition": {
"id": "bworld:iron_coin",
"count": 2
},
"result": "bworld:iron_watering_can" "result": "bworld:iron_watering_can"
} }
} }
@@ -7,6 +7,10 @@
"id": "bworld:stone", "id": "bworld:stone",
"count": 5 "count": 5
}, },
"addition": {
"id": "bworld:brass_coin",
"count": 1
},
"result": "bworld:stone_axe" "result": "bworld:stone_axe"
} }
} }
@@ -7,6 +7,10 @@
"id": "bworld:stone", "id": "bworld:stone",
"count": 5 "count": 5
}, },
"addition": {
"id": "bworld:brass_coin",
"count": 1
},
"result": "bworld:stone_hoe" "result": "bworld:stone_hoe"
} }
} }
@@ -7,6 +7,10 @@
"id": "bworld:stone", "id": "bworld:stone",
"count": 5 "count": 5
}, },
"addition": {
"id": "bworld:brass_coin",
"count": 1
},
"result": "bworld:stone_knife" "result": "bworld:stone_knife"
} }
} }
@@ -7,6 +7,10 @@
"id": "bworld:stone", "id": "bworld:stone",
"count": 5 "count": 5
}, },
"addition": {
"id": "bworld:brass_coin",
"count": 1
},
"result": "bworld:stone_pickaxe" "result": "bworld:stone_pickaxe"
} }
} }
@@ -7,6 +7,10 @@
"id": "bworld:stone", "id": "bworld:stone",
"count": 5 "count": 5
}, },
"addition": {
"id": "bworld:brass_coin",
"count": 1
},
"result": "bworld:stone_shovel" "result": "bworld:stone_shovel"
} }
} }
@@ -7,6 +7,10 @@
"id": "bworld:stone", "id": "bworld:stone",
"count": 5 "count": 5
}, },
"addition": {
"id": "bworld:brass_coin",
"count": 1
},
"result": "bworld:stone_watering_can" "result": "bworld:stone_watering_can"
} }
} }
+10 -3
View File
@@ -295,7 +295,7 @@ function register_furnace(ctx: ServerContext) {
}); });
} }
// smithing table: a tool, some of a material and maybe a third item make a better tool, see smithing.ts. // smithing table: a tool, some of a material and maybe some of a third item make a better tool, see smithing.ts.
// every player gets their own slots, and what's left in them goes back to the player when they close it // every player gets their own slots, and what's left in them goes back to the player when they close it
const TOOL = 0; const TOOL = 0;
@@ -439,7 +439,14 @@ function register_smithing_table(ctx: ServerContext) {
const addition = container.get(ADDITION); const addition = container.get(ADDITION);
if (!tool || !material) return undefined; if (!tool || !material) return undefined;
const recipe = ctx.recipes.find<SmithingRecipe>(SMITHING, { tool, material, addition }); const recipe = ctx.recipes.find<SmithingRecipe>(SMITHING, { tool, material, addition });
return recipe && { result: recipe.result, material_count: recipe.material.count, tool, material, addition }; return recipe && {
result: recipe.result,
material_count: recipe.material.count,
addition_count: recipe.addition?.count ?? 0,
tool,
material,
addition,
};
}; };
// the result slot shows what taking it would give, the upgraded tool keeps the old one's data // the result slot shows what taking it would give, the upgraded tool keeps the old one's data
@@ -470,7 +477,7 @@ function register_smithing_table(ctx: ServerContext) {
if (!recipe || recipe.result !== item.id) return false; if (!recipe || recipe.result !== item.id) return false;
recipe.tool.count -= 1; recipe.tool.count -= 1;
recipe.material.count -= recipe.material_count; recipe.material.count -= recipe.material_count;
if (recipe.addition) recipe.addition.count -= 1; if (recipe.addition) recipe.addition.count -= recipe.addition_count;
return true; return true;
}, },
}); });
+8 -7
View File
@@ -1,8 +1,8 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later // SPDX-License-Identifier: AGPL-3.0-or-later
// smithing recipes: the smithing table upgrades a tool with some of a material and maybe one more item. the type is // smithing recipes: the smithing table upgrades a tool with some of a material and maybe some of one more item. the
// registered by the shared script, so the server's menu and anything on a client find them the same way // type is registered by the shared script, so the server's menu and anything on a client find them the same way
import type { ItemStack, SharedContext } from "bworld/shared"; import type { ItemStack, SharedContext } from "bworld/shared";
export const SMITHING = "bworld:smithing"; export const SMITHING = "bworld:smithing";
@@ -10,7 +10,7 @@ export const SMITHING = "bworld:smithing";
export interface SmithingRecipe { export interface SmithingRecipe {
tool: string; tool: string;
material: { id: string; count: number }; material: { id: string; count: number };
addition?: string; addition?: { id: string; count: number };
result: string; result: string;
} }
@@ -31,15 +31,16 @@ export function register_smithing(ctx: SharedContext) {
material: read.stack(json.material, "material"), material: read.stack(json.material, "material"),
result: read.item(json.result, "result"), result: read.item(json.result, "result"),
}; };
if (json.addition !== undefined) recipe.addition = read.item(json.addition, "addition"); if (json.addition !== undefined) recipe.addition = read.stack(json.addition, "addition");
const inputs = `${recipe.tool} ${recipe.material.id} ${recipe.addition}`; const inputs = `${recipe.tool} ${recipe.material.id} ${recipe.addition?.id}`;
if (seen.has(inputs)) throw new Error(`there's already one for ${recipe.tool} with ${recipe.material.id}`); if (seen.has(inputs)) throw new Error(`there's already one for ${recipe.tool} with ${recipe.material.id}`);
seen.add(inputs); seen.add(inputs);
return recipe; return recipe;
}, },
// with enough of the material // with enough of the material and the addition
matches: (recipe, input) => matches: (recipe, input) =>
recipe.tool === input.tool.id && recipe.material.id === input.material.id && recipe.tool === input.tool.id && recipe.material.id === input.material.id &&
input.material.count >= recipe.material.count && recipe.addition === input.addition?.id, input.material.count >= recipe.material.count && recipe.addition?.id === input.addition?.id &&
(input.addition?.count ?? 0) >= (recipe.addition?.count ?? 0),
}); });
} }
+1 -1
View File
@@ -100,7 +100,7 @@ function base_categories(tags: TagApi): RecipeCategory[] {
draw(recipe, layout) { draw(recipe, layout) {
layout.slot({ ids: [recipe.tool], count: 1 }, 0, 0); layout.slot({ ids: [recipe.tool], count: 1 }, 0, 0);
layout.slot(stack(recipe.material), SLOT, 0); layout.slot(stack(recipe.material), SLOT, 0);
layout.slot(recipe.addition ? { ids: [recipe.addition], count: 1 } : undefined, SLOT * 2, 0); layout.slot(recipe.addition && stack(recipe.addition), SLOT * 2, 0);
layout.texture("bworld:arrow_full", 61, 1, ARROW, ARROW); layout.texture("bworld:arrow_full", 61, 1, ARROW, ARROW);
layout.slot({ ids: [recipe.result], count: 1 }, 90, 0); layout.slot({ ids: [recipe.result], count: 1 }, 90, 0);
}, },
+1 -1
View File
@@ -60,7 +60,7 @@ export function inputs(recipe: Recipe, tags: TagApi): Ingredient[] {
return [{ ids: tags.expand(recipe.item), count: 1 }]; return [{ ids: tags.expand(recipe.item), count: 1 }];
case "bworld:smithing": case "bworld:smithing":
return [{ ids: [recipe.tool], count: 1 }, stack(recipe.material)].concat( return [{ ids: [recipe.tool], count: 1 }, stack(recipe.material)].concat(
recipe.addition ? [{ ids: [recipe.addition], count: 1 }] : [], recipe.addition ? [stack(recipe.addition)] : [],
); );
default: { default: {
const { grid, extras } = mod_recipe_inputs(recipe, tags); const { grid, extras } = mod_recipe_inputs(recipe, tags);
+37 -7
View File
@@ -251,12 +251,13 @@ Deno.test("items are called by their name, or by one made from their id", async
assertEquals(display_name("test:thing"), "A Thing"); assertEquals(display_name("test:thing"), "A Thing");
}); });
Deno.test("a smithing table upgrades a wooden pickaxe with 5 stone, using up exactly that", async () => { Deno.test("a smithing table upgrades a wooden pickaxe with 5 stone and a brass coin, using up exactly that", async () => {
const t = await setup([["bworld:wood_pickaxe", 1], ["bworld:stone", 7]]); const t = await setup([["bworld:wood_pickaxe", 1], ["bworld:stone", 7], ["bworld:brass_coin", 1]]);
t.game.set_block(1, 100, 0, "bworld:smithing_table"); t.game.set_block(1, 100, 0, "bworld:smithing_table");
let messages = t.take(1); let messages = t.take(1);
const pickaxe = slot_of(messages, "bworld:wood_pickaxe"); const pickaxe = slot_of(messages, "bworld:wood_pickaxe");
const stone = slot_of(messages, "bworld:stone"); const stone = slot_of(messages, "bworld:stone");
const coin = slot_of(messages, "bworld:brass_coin");
// mods' own, like the player's curios // mods' own, like the player's curios
const containers = t.game.containers.size; const containers = t.game.containers.size;
@@ -270,9 +271,14 @@ Deno.test("a smithing table upgrades a wooden pickaxe with 5 stone, using up exa
click(t, opened, 3); click(t, opened, 3);
assertEquals(t.take(1).filter((m) => m.type === "cursor").at(-1)?.item ?? null, null); assertEquals(t.take(1).filter((m) => m.type === "cursor").at(-1)?.item ?? null, null);
// the result shows up once there's enough stone, and taking it uses up the pickaxe and 5 stone // nor with the stone but not the coin
move_to_menu(t, opened, stone, 1); move_to_menu(t, opened, stone, 1);
t.game.tick(); t.game.tick();
assertEquals(menu_items(t.take(1))[3], null);
// the result shows up once the coin is in, and taking it uses up the pickaxe, 5 stone and the coin
move_to_menu(t, opened, coin, 2);
t.game.tick();
assertEquals(menu_items(t.take(1))[3], { id: "bworld:stone_pickaxe", count: 1 }); assertEquals(menu_items(t.take(1))[3], { id: "bworld:stone_pickaxe", count: 1 });
click(t, opened, 3); click(t, opened, 3);
messages = t.take(1); messages = t.take(1);
@@ -289,14 +295,38 @@ Deno.test("a smithing table upgrades a wooden pickaxe with 5 stone, using up exa
assertEquals(t.game.containers.size, containers, "its slots are gone"); assertEquals(t.game.containers.size, containers, "its slots are gone");
}); });
Deno.test("a smithing result can't be taken once its inputs are gone, even before it updates", async () => { Deno.test("a smithing addition needs its whole count, and all of it is used up", async () => {
const t = await setup([["bworld:wood_pickaxe", 1], ["bworld:stone", 5]]); const t = await setup([["bworld:stone_pickaxe", 1], ["bworld:copper_ingot", 5], ["bworld:brass_coin", 6]]);
t.game.set_block(1, 100, 0, "bworld:smithing_table"); t.game.set_block(1, 100, 0, "bworld:smithing_table");
const messages = t.take(1); const messages = t.take(1);
t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" }); t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" });
const opened = opened_menu(t.take(1)); const opened = opened_menu(t.take(1));
move_to_menu(t, opened, slot_of(messages, "bworld:wood_pickaxe"), 0); move_to_menu(t, opened, slot_of(messages, "bworld:stone_pickaxe"), 0);
move_to_menu(t, opened, slot_of(messages, "bworld:stone"), 1); move_to_menu(t, opened, slot_of(messages, "bworld:copper_ingot"), 1);
// right-clicking the coins onto the slot puts down one at a time
const coins = inventory_slot(opened, slot_of(messages, "bworld:brass_coin"));
click(t, opened, coins);
for (let i = 0; i < 4; i++) t.send(1, { type: "click", menu: opened.id, slot: 2, button: 2 });
t.game.tick();
assertEquals(menu_items(t.take(1))[3], null);
t.send(1, { type: "click", menu: opened.id, slot: 2, button: 2 });
t.game.tick();
assertEquals(menu_items(t.take(1))[3], { id: "bworld:copper_pickaxe", count: 1 });
click(t, opened, coins);
click(t, opened, 3);
assertEquals(menu_items(t.take(1)).slice(0, 4), [null, null, null, null]);
});
Deno.test("a smithing result can't be taken once its inputs are gone, even before it updates", async () => {
const t = await setup([["bworld:copper_pickaxe", 1], ["bworld:iron_ingot", 5]]);
t.game.set_block(1, 100, 0, "bworld:smithing_table");
const messages = t.take(1);
t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" });
const opened = opened_menu(t.take(1));
move_to_menu(t, opened, slot_of(messages, "bworld:copper_pickaxe"), 0);
move_to_menu(t, opened, slot_of(messages, "bworld:iron_ingot"), 1);
t.game.tick(); t.game.tick();
// take the pickaxe back out and try the result in the same tick // take the pickaxe back out and try the result in the same tick
+5 -1
View File
@@ -88,7 +88,11 @@ Deno.test("the recipe viewer finds what makes and uses an item, in any recipe ty
r.type === "bworld:smelting" && outputs(r)[0].ids[0] === "bworld:iron_ingot" r.type === "bworld:smelting" && outputs(r)[0].ids[0] === "bworld:iron_ingot"
), ),
); );
assertEquals(ids(inputs(making("bworld:stone_pickaxe")[0])), ["bworld:wood_pickaxe", "bworld:stone"]); assertEquals(ids(inputs(making("bworld:stone_pickaxe")[0])), [
"bworld:wood_pickaxe",
"bworld:stone",
"bworld:brass_coin",
]);
assertEquals(category_name("bworld:smithing"), "Smithing"); assertEquals(category_name("bworld:smithing"), "Smithing");
}); });