Stonecutter
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"block": {
|
||||
"id": "bworld:stonecutter",
|
||||
"textures": {
|
||||
"top": "bworld:stonecutter_top",
|
||||
"side": "bworld:stonecutter_side",
|
||||
"bottom": "bworld:stonecutter_bottom",
|
||||
"saw": "bworld:stonecutter_saw"
|
||||
},
|
||||
"model": "bworld:stonecutter",
|
||||
"item_model": "bworld:stonecutter",
|
||||
"render_layer": "cutout",
|
||||
"states": [
|
||||
{
|
||||
"name": "facing",
|
||||
"bits": 2,
|
||||
"default": 0
|
||||
}
|
||||
],
|
||||
"variants": {
|
||||
"facing=1": { "y": 90 },
|
||||
"facing=2": { "y": 180 },
|
||||
"facing=3": { "y": 270 }
|
||||
},
|
||||
"mining": {
|
||||
"toughness": 5,
|
||||
"tool": "pickaxe",
|
||||
"requires_tool": true
|
||||
},
|
||||
"drops": "bworld:stonecutter",
|
||||
"interactive": true,
|
||||
"components": {
|
||||
"bworld:stonecutter": {}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"model": {
|
||||
"id": "bworld:stonecutter",
|
||||
"elements": [
|
||||
{
|
||||
"from": [0, 0, 0],
|
||||
"to": [16, 9, 16],
|
||||
"faces": {
|
||||
"bottom": { "texture": "#bottom", "cullface": "bottom" },
|
||||
"top": { "texture": "#top" },
|
||||
"north": { "texture": "#side", "uv": [0, 7, 16, 16], "cullface": "north" },
|
||||
"south": { "texture": "#side", "uv": [0, 7, 16, 16], "cullface": "south" },
|
||||
"west": { "texture": "#side", "uv": [0, 7, 16, 16], "cullface": "west" },
|
||||
"east": { "texture": "#side", "uv": [0, 7, 16, 16], "cullface": "east" }
|
||||
}
|
||||
},
|
||||
{
|
||||
"from": [1, 9, 8],
|
||||
"to": [15, 16, 8],
|
||||
"faces": {
|
||||
"north": { "texture": "#saw", "uv": [1, 9, 15, 16] },
|
||||
"south": { "texture": "#saw", "uv": [15, 9, 1, 16] }
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"recipe": {
|
||||
"type": "shaped",
|
||||
"pattern": [
|
||||
"III",
|
||||
"SSS"
|
||||
],
|
||||
"key": {
|
||||
"I": "bworld:iron_ingot",
|
||||
"S": "bworld:smooth_stone"
|
||||
},
|
||||
"result": {
|
||||
"id": "bworld:stonecutter",
|
||||
"count": 1
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"recipe": {
|
||||
"type": "bworld:stonecutting",
|
||||
"input": "bworld:cobblestone",
|
||||
"output": { "id": "bworld:stone_bricks", "count": 1 }
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,9 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// the screens of the base game's menus, see scripts/server.ts for their slots. in gui pixels, like minecraft's
|
||||
import type { ClientContext, ClientMenu, Graphics, ItemStack, MenuScreen } from "bworld/client";
|
||||
import type { ClientContext, ClientMenu, Color, Graphics, ItemStack, MenuScreen } from "bworld/client";
|
||||
import { MINIGAMES } from "./minigames/mod.ts";
|
||||
import { stonecutting_options, stonecutting_recipes } from "./stonecutting.ts";
|
||||
|
||||
const WIDTH = 176;
|
||||
// like minecraft's 176x166 container screens
|
||||
@@ -15,6 +16,7 @@ export function setup(ctx: ClientContext) {
|
||||
ctx.ui.register_menu_screen("bworld:crabpot", (menu) => container_screen(menu, HEIGHT));
|
||||
ctx.ui.register_menu_screen("bworld:worm_bin", worm_bin_screen);
|
||||
ctx.ui.register_menu_screen("bworld:smithing_table", smithing_screen);
|
||||
ctx.ui.register_menu_screen("bworld:stonecutter", (menu) => stonecutter_screen(ctx, menu));
|
||||
ctx.ui.register_menu_screen("bworld:fishing_minigame", fishing_minigame_screen);
|
||||
}
|
||||
|
||||
@@ -60,6 +62,69 @@ function smithing_screen(menu: ClientMenu): MenuScreen {
|
||||
});
|
||||
}
|
||||
|
||||
// what the input can be cut into, as a grid of buttons that scrolls when there are more than fit. a button is the
|
||||
// recipe's place in that list, which the server's menu reads the same way, see stonecutting.ts
|
||||
const CUT_X = 52;
|
||||
const CUT_Y = 14;
|
||||
const CUT_COLUMNS = 4;
|
||||
const CUT_ROWS = 3;
|
||||
const CUT_WIDTH = 16;
|
||||
const CUT_HEIGHT = 18;
|
||||
const SCROLLBAR_X = 119;
|
||||
const SCROLLBAR_WIDTH = 12;
|
||||
const SCROLLER_HEIGHT = 15;
|
||||
const TRACK_COLOR: Color = [0.55, 0.55, 0.55, 1];
|
||||
const SCROLLER_COLOR: Color = [1, 1, 1, 1];
|
||||
const SCROLLER_OFF_COLOR: Color = [0.78, 0.78, 0.78, 1];
|
||||
const SELECTED_COLOR: Color = [1, 1, 1, 0.45];
|
||||
|
||||
function stonecutter_screen(ctx: ClientContext, menu: ClientMenu): MenuScreen {
|
||||
const recipes = stonecutting_recipes(ctx);
|
||||
// the first row shown, and whether the scroller is being dragged
|
||||
let row = 0;
|
||||
let dragging = false;
|
||||
return container_screen(menu, HEIGHT, (g) => {
|
||||
const options = stonecutting_options(recipes, menu.item(0)?.id);
|
||||
const rows_hidden = Math.max(0, Math.ceil(options.length / CUT_COLUMNS) - CUT_ROWS);
|
||||
const track_height = CUT_ROWS * CUT_HEIGHT;
|
||||
const { mouse } = g;
|
||||
const over = (x: number, y: number, w: number, h: number) =>
|
||||
mouse.x >= x && mouse.y >= y && mouse.x < x + w && mouse.y < y + h;
|
||||
|
||||
if (mouse.pressed && rows_hidden > 0 && over(SCROLLBAR_X, CUT_Y, SCROLLBAR_WIDTH, track_height)) {
|
||||
dragging = true;
|
||||
}
|
||||
if (!mouse.down) dragging = false;
|
||||
if (dragging) {
|
||||
const along = (mouse.y - CUT_Y - SCROLLER_HEIGHT / 2) / (track_height - SCROLLER_HEIGHT);
|
||||
row = Math.round(Math.max(0, Math.min(1, along)) * rows_hidden);
|
||||
}
|
||||
row = Math.min(row, rows_hidden);
|
||||
const scroller_y = CUT_Y + (rows_hidden > 0 ? (row / rows_hidden) * (track_height - SCROLLER_HEIGHT) : 0);
|
||||
g.rect(SCROLLBAR_X, CUT_Y, SCROLLBAR_WIDTH, track_height, TRACK_COLOR);
|
||||
g.rect(
|
||||
SCROLLBAR_X,
|
||||
scroller_y,
|
||||
SCROLLBAR_WIDTH,
|
||||
SCROLLER_HEIGHT,
|
||||
rows_hidden > 0 ? SCROLLER_COLOR : SCROLLER_OFF_COLOR,
|
||||
);
|
||||
|
||||
for (let i = 0; i < CUT_COLUMNS * CUT_ROWS; i++) {
|
||||
const index = row * CUT_COLUMNS + i;
|
||||
const recipe = options[index];
|
||||
if (!recipe) break;
|
||||
const x = CUT_X + (i % CUT_COLUMNS) * CUT_WIDTH;
|
||||
const y = CUT_Y + Math.floor(i / CUT_COLUMNS) * CUT_HEIGHT;
|
||||
if (g.button("", x, y, CUT_WIDTH, CUT_HEIGHT)) menu.click_button(index);
|
||||
if (index === menu.data.selected) g.rect(x, y, CUT_WIDTH, CUT_HEIGHT, SELECTED_COLOR);
|
||||
// an item is drawn in an 18 pixel slot, the button is 16 wide
|
||||
g.item(recipe.output.id, x - 1, y, recipe.output.count);
|
||||
if (over(x, y, CUT_WIDTH, CUT_HEIGHT)) g.item_tooltip(recipe.output.id, mouse.x, mouse.y);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// an 18 pixel square progress bar, value / max of full drawn over empty
|
||||
function bar(
|
||||
g: Graphics,
|
||||
|
||||
@@ -6,6 +6,7 @@ import type { BlockRef, Container, Player, Season, ServerContext } from "bworld/
|
||||
import { register_curios } from "./curios.ts";
|
||||
import { register_effects } from "./effects.ts";
|
||||
import { register_fishing } from "./fishing.ts";
|
||||
import { stonecutting_options, stonecutting_recipes } from "./stonecutting.ts";
|
||||
|
||||
export function setup(ctx: ServerContext) {
|
||||
register_hoeable(ctx);
|
||||
@@ -16,6 +17,7 @@ export function setup(ctx: ServerContext) {
|
||||
register_crabpot(ctx);
|
||||
register_worm_bin(ctx);
|
||||
register_smithing_table(ctx);
|
||||
register_stonecutter(ctx);
|
||||
register_farming(ctx);
|
||||
register_fishing(ctx);
|
||||
register_door(ctx);
|
||||
@@ -478,6 +480,85 @@ function register_smithing_table(ctx: ServerContext) {
|
||||
});
|
||||
}
|
||||
|
||||
// stonecutter: one item in, cut into whichever of its stonecutting recipes the player picks with the screen's buttons,
|
||||
// see stonecutting.ts. like the smithing table, every player gets their own slots, and what's left in the input goes
|
||||
// back to the player when they close it
|
||||
|
||||
const STONECUTTER_INPUT = 0;
|
||||
const STONECUTTER_RESULT = 1;
|
||||
|
||||
function register_stonecutter(ctx: ServerContext) {
|
||||
const recipes = stonecutting_recipes(ctx);
|
||||
|
||||
const menu_type = ctx.menus.register<BlockRef>("bworld:stonecutter", (menu, block, player) => {
|
||||
const container = ctx.containers.create(2);
|
||||
// the picked recipe, by its place in what the input can be cut into. -1 for none
|
||||
let selected = -1;
|
||||
// the item it was picked for, since another item has other recipes
|
||||
let picked_for: string | undefined;
|
||||
const options = () => stonecutting_options(recipes, container.get(STONECUTTER_INPUT)?.id);
|
||||
|
||||
// the result slot shows what taking it would give
|
||||
const show_result = () => {
|
||||
const recipe = options()[selected];
|
||||
const shown = container.get(STONECUTTER_RESULT);
|
||||
if (!recipe) {
|
||||
if (shown) container.set(STONECUTTER_RESULT, undefined);
|
||||
} else if (shown?.id !== recipe.output.id || shown.count !== recipe.output.count) {
|
||||
container.set(STONECUTTER_RESULT, { ...recipe.output });
|
||||
}
|
||||
};
|
||||
const stop = container.on_change((slot) => {
|
||||
if (slot !== STONECUTTER_INPUT) return;
|
||||
const id = container.get(STONECUTTER_INPUT)?.id;
|
||||
if (id !== picked_for) {
|
||||
picked_for = id;
|
||||
selected = -1;
|
||||
}
|
||||
show_result();
|
||||
});
|
||||
|
||||
menu.title = "Stonecutter";
|
||||
menu.add_slot(container, STONECUTTER_INPUT, { x: 19, y: 32 });
|
||||
menu.add_slot(container, STONECUTTER_RESULT, {
|
||||
x: 142,
|
||||
y: 32,
|
||||
output_only: true,
|
||||
// the input is used up in the same step, so it can't be taken out after
|
||||
on_take(item) {
|
||||
const recipe = options()[selected];
|
||||
const input = container.get(STONECUTTER_INPUT);
|
||||
if (!recipe || !input || recipe.output.id !== item.id) return false;
|
||||
input.count -= 1;
|
||||
return true;
|
||||
},
|
||||
});
|
||||
menu.add_player_inventory(7, INVENTORY_Y);
|
||||
menu.add_data("selected", () => selected);
|
||||
// a button is a recipe, by its place in options
|
||||
menu.on_button((_player, id) => {
|
||||
if (!Number.isInteger(id) || id < 0 || id >= options().length) return;
|
||||
selected = id;
|
||||
picked_for = container.get(STONECUTTER_INPUT)?.id;
|
||||
show_result();
|
||||
});
|
||||
menu.still_valid_near(block);
|
||||
menu.on_close(() => {
|
||||
stop();
|
||||
const item = container.get(STONECUTTER_INPUT);
|
||||
if (item) player.give_item(item.id, item.count, item.data);
|
||||
ctx.containers.delete(container.id);
|
||||
});
|
||||
});
|
||||
|
||||
ctx.components.register_block("bworld:stonecutter", {
|
||||
on_interact(block, _params, player) {
|
||||
menu_type.open(player, block);
|
||||
return true;
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// farming: java's crop rules on stardew valley's clock. every morning at 06:00 a crop grows one stage if its farmland
|
||||
// was watered the day before, then all farmland dries. crops only stand on farmland (their place_on), and pop off when
|
||||
// it's dug up
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// stonecutting recipes: the stonecutter cuts one item into another, picked from every recipe that takes it. the
|
||||
// server's menu and the client's screen both list them from here, in the same order, so a button's number means the
|
||||
// same recipe to both
|
||||
import type { TagApi } from "bworld/shared";
|
||||
|
||||
export const STONECUTTING = "bworld:stonecutting";
|
||||
|
||||
export interface StonecuttingRecipe {
|
||||
// the items it cuts
|
||||
input: string[];
|
||||
output: { id: string; count: number };
|
||||
}
|
||||
|
||||
// what reading them needs, which server and client scripts both have
|
||||
interface RecipeReader {
|
||||
// deno-lint-ignore no-explicit-any
|
||||
recipes: { list(type: string): any[] };
|
||||
tags: TagApi;
|
||||
items: { exists(id: string): boolean };
|
||||
}
|
||||
|
||||
// every stonecutting recipe, in load order. a broken one throws, which stops the server from starting
|
||||
export function stonecutting_recipes(ctx: RecipeReader): StonecuttingRecipe[] {
|
||||
return ctx.recipes.list(STONECUTTING).map((json, i) => {
|
||||
const where = `${STONECUTTING} recipe ${i + 1}`;
|
||||
const choice = json.input;
|
||||
const input = typeof choice === "string" || Array.isArray(choice) ? ctx.tags.expand(choice) : [];
|
||||
if (input.length === 0 || !input.every((id) => ctx.items.exists(id))) {
|
||||
throw new Error(`${where}: input must be an item, a #tag or a list of them, and they must exist`);
|
||||
}
|
||||
const { id, count } = json.output ?? {};
|
||||
if (typeof id !== "string" || !ctx.items.exists(id) || !Number.isInteger(count) || count < 1) {
|
||||
throw new Error(`${where}: output must be { id, count } of an item that exists`);
|
||||
}
|
||||
return { input, output: { id, count } };
|
||||
});
|
||||
}
|
||||
|
||||
// what an item can be cut into, in the order the stonecutter shows them
|
||||
export function stonecutting_options(recipes: StonecuttingRecipe[], id: string | undefined): StonecuttingRecipe[] {
|
||||
return id === undefined ? [] : recipes.filter((recipe) => recipe.input.includes(id));
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 452 B |
Binary file not shown.
|
After Width: | Height: | Size: 508 B |
@@ -101,6 +101,18 @@ function base_categories(tags: TagApi): RecipeCategory[] {
|
||||
layout.slot({ ids: [recipe.result], count: 1 }, 90, 0);
|
||||
},
|
||||
},
|
||||
{
|
||||
type: "bworld:stonecutting",
|
||||
title: "Stonecutting",
|
||||
width: 78,
|
||||
height: 18,
|
||||
catalysts: ["bworld:stonecutter"],
|
||||
draw(recipe, layout) {
|
||||
layout.slot({ ids: tags.expand(recipe.input), count: 1 }, 0, 0);
|
||||
layout.texture("bworld:arrow_full", 31, 1, ARROW, ARROW);
|
||||
layout.slot(stack(recipe.output), 60, 0);
|
||||
},
|
||||
},
|
||||
{
|
||||
// when and where a fish bites, see fishing_recipes
|
||||
type: FISHING,
|
||||
|
||||
Reference in New Issue
Block a user