Pressing recipes
This commit is contained in:
@@ -18,8 +18,8 @@ deno task check-mods
|
||||
|
||||
The order matters: tags and recipes are checked against what's already in the mods folder, so blocks and items go first,
|
||||
then tags, then recipes. Forge's shared tags, like `forge:ingots/brass`, become bworld's `c:` ones. Recipes are only
|
||||
Create's stonecutting ones so far, and the ones that cut a stone bworld doesn't have yet (granite, diorite, deepslate,
|
||||
calcite, tuff, dripstone) are left out until it does.
|
||||
Create's stonecutting and pressing ones so far. The stonecutting ones that cut a stone bworld doesn't have yet (granite,
|
||||
diorite, deepslate, calcite, tuff, dripstone) are left out until it does, and so is pressing paper and dirt paths.
|
||||
|
||||
Java sets what blocks do in code, so every block gets placeholders (a second to mine with anything, dropping itself).
|
||||
`blocks.json` gives blocks what they have instead, by id, like the components that make a shaft turn and a hand crank
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"recipe": {
|
||||
"type": "create:pressing",
|
||||
"ingredients": ["#c:ingots/brass"],
|
||||
"results": [
|
||||
{ "id": "create:brass_sheet", "count": 1 }
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"recipe": {
|
||||
"type": "create:pressing",
|
||||
"ingredients": ["bworld:copper_ingot"],
|
||||
"results": [
|
||||
{ "id": "create:copper_sheet", "count": 1 }
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"recipe": {
|
||||
"type": "create:pressing",
|
||||
"ingredients": ["#c:ingots/gold"],
|
||||
"results": [
|
||||
{ "id": "create:golden_sheet", "count": 1 }
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"recipe": {
|
||||
"type": "create:pressing",
|
||||
"ingredients": ["#c:ingots/iron"],
|
||||
"results": [
|
||||
{ "id": "create:iron_sheet", "count": 1 }
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// create's processing recipes, the shape its machines' recipe types share, like ProcessingRecipe: item choices in, and
|
||||
// stacks out, each with a chance. read and checked when the server starts, so a broken one stops it there instead of
|
||||
// in a machine. no ctx, tests give what it needs
|
||||
import type { Id } from "bworld/shared";
|
||||
|
||||
// how many ingredients and results a type takes at most, like getMaxInputCount and getMaxOutputCount
|
||||
export interface ProcessingType {
|
||||
readonly id: Id;
|
||||
readonly inputs: number;
|
||||
readonly outputs: number;
|
||||
}
|
||||
|
||||
export const PRESSING: ProcessingType = { id: "create:pressing", inputs: 1, outputs: 2 };
|
||||
|
||||
export interface ProcessingResult {
|
||||
id: Id;
|
||||
count: number;
|
||||
// how likely each of count is to come out, 1 for always, like ProcessingOutput's
|
||||
chance: number;
|
||||
}
|
||||
|
||||
export interface ProcessingRecipe {
|
||||
// each ingredient as every item that fits it
|
||||
ingredients: Id[][];
|
||||
results: ProcessingResult[];
|
||||
}
|
||||
|
||||
// what reading recipes needs from the game
|
||||
export interface RecipeLookup {
|
||||
// an item choice as its items, like ctx.tags.expand
|
||||
expand(choice: Id | Id[]): Id[];
|
||||
item_exists(id: Id): boolean;
|
||||
}
|
||||
|
||||
// every recipe of type, from their json, or an error saying what's wrong with each broken one
|
||||
export function processing_recipes(jsons: unknown[], type: ProcessingType, lookup: RecipeLookup): ProcessingRecipe[] {
|
||||
const parsed = jsons.map((json) => parse_processing(json, type, lookup));
|
||||
const problems = parsed.flatMap((recipe, i) =>
|
||||
"problems" in recipe ? [`${type.id} recipe ${JSON.stringify(jsons[i])}: ${recipe.problems.join("; ")}`] : []
|
||||
);
|
||||
if (problems.length > 0) throw new Error(problems.join("\n"));
|
||||
return parsed as ProcessingRecipe[];
|
||||
}
|
||||
|
||||
// one recipe, or what's wrong with it, like ProcessingRecipe.validate
|
||||
export function parse_processing(
|
||||
// deno-lint-ignore no-explicit-any
|
||||
json: any,
|
||||
type: ProcessingType,
|
||||
lookup: RecipeLookup,
|
||||
): ProcessingRecipe | { problems: string[] } {
|
||||
const problems: string[] = [];
|
||||
const ingredients: Id[][] = [];
|
||||
const results: ProcessingResult[] = [];
|
||||
if (!Array.isArray(json?.ingredients) || json.ingredients.length === 0) {
|
||||
problems.push("ingredients must be a list of item choices");
|
||||
} else {
|
||||
if (json.ingredients.length > type.inputs) {
|
||||
problems.push(`it takes ${json.ingredients.length} ingredients, and ${type.id} takes ${type.inputs}`);
|
||||
}
|
||||
for (const choice of json.ingredients) {
|
||||
const choices = [choice].flat();
|
||||
const valid = choices.length > 0 && choices.every((id) => typeof id === "string");
|
||||
const unknown = valid ? choices.filter((id: string) => !id.startsWith("#") && !lookup.item_exists(id)) : [];
|
||||
if (!valid) problems.push(`${JSON.stringify(choice)} isn't an item, a #tag or a list of them`);
|
||||
else if (unknown.length > 0) problems.push(`it takes ${unknown.join(", ")}, which doesn't exist`);
|
||||
else ingredients.push(lookup.expand(choice));
|
||||
}
|
||||
}
|
||||
if (!Array.isArray(json?.results) || json.results.length === 0) {
|
||||
problems.push("results must be a list of { id, count, chance }");
|
||||
} else {
|
||||
if (json.results.length > type.outputs) {
|
||||
problems.push(`it makes ${json.results.length} results, and ${type.id} makes ${type.outputs}`);
|
||||
}
|
||||
for (const result of json.results) {
|
||||
const { id, count = 1, chance = 1 } = result ?? {};
|
||||
if (typeof id !== "string" || !lookup.item_exists(id)) problems.push(`it makes ${id}, which doesn't exist`);
|
||||
else if (!Number.isInteger(count) || count < 1 || count > 99) {
|
||||
problems.push(`${id}'s count must be 1 to 99`);
|
||||
} else if (typeof chance !== "number" || chance <= 0 || chance > 1) {
|
||||
problems.push(`${id}'s chance must be above 0 and at most 1`);
|
||||
} else results.push({ id, count, chance });
|
||||
}
|
||||
}
|
||||
// none of the types so far can say how long they take or how hot they have to be, like canSpecifyDuration and
|
||||
// canRequireHeat
|
||||
if (json?.processing_time !== undefined) problems.push(`${type.id} can't have a processing_time`);
|
||||
if (json?.heat_requirement !== undefined) problems.push(`${type.id} can't have a heat_requirement`);
|
||||
return problems.length > 0 ? { problems } : { ingredients, results };
|
||||
}
|
||||
@@ -1,10 +1,12 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// create's blocks that do something: so far what turns (create:kinetic) and the hand crank that turns it
|
||||
// create's blocks that do something: so far what turns (create:kinetic) and the hand crank that turns it. and its
|
||||
// recipe types' checks
|
||||
import type { BlockRef, Id, ServerContext } from "bworld/server";
|
||||
import { along, kinetic_shape, type KineticParams } from "./kinetics/connections.ts";
|
||||
import { handle_added, handle_removed, type KineticWorld, type Pos, update_generated } from "./kinetics/spreading.ts";
|
||||
import { PRESSING, processing_recipes } from "./recipes/processing.ts";
|
||||
|
||||
interface Config {
|
||||
max_speed: number;
|
||||
@@ -36,6 +38,12 @@ export function setup(ctx: ServerContext) {
|
||||
const config: Config = ctx.config;
|
||||
const world = kinetic_world(ctx, config);
|
||||
|
||||
// checked now, so a broken recipe stops the server rather than a press later
|
||||
processing_recipes(ctx.recipes.list(PRESSING.id), PRESSING, {
|
||||
expand: (choice) => ctx.tags.expand(choice),
|
||||
item_exists: (id) => ctx.items.exists(id),
|
||||
});
|
||||
|
||||
ctx.components.register_block<KineticParams>("create:kinetic", {
|
||||
on_create(block) {
|
||||
handle_added(world, [block.x, block.y, block.z]);
|
||||
|
||||
Reference in New Issue
Block a user