Every recipe type registered from shared scripts
This commit is contained in:
@@ -2,9 +2,10 @@
|
||||
// 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, and for the types that can, fluids out as { fluid, amount }. 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 { FluidStack, Id } from "bworld/shared";
|
||||
// stacks out, each with a chance, and for the types that can, fluids out as { fluid, amount }. the shared script
|
||||
// registers each type, so a broken recipe stops the mod loading instead of a machine later. no ctx, tests give what it
|
||||
// needs
|
||||
import type { FluidStack, Id, RecipeType } from "bworld/shared";
|
||||
|
||||
// how many ingredients, item results and fluid results a type takes at most, like getMaxInputCount,
|
||||
// getMaxOutputCount and getMaxFluidOutputCount
|
||||
@@ -42,14 +43,17 @@ export interface RecipeLookup {
|
||||
fluid_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[];
|
||||
// the recipe type for ctx.recipes.register_type, saying everything wrong with a broken recipe. it's found by the item
|
||||
// it takes first, like a machine's getRecipe
|
||||
export function processing_type(type: ProcessingType, lookup: RecipeLookup): RecipeType<ProcessingRecipe, Id> {
|
||||
return {
|
||||
parse(json) {
|
||||
const parsed = parse_processing(json, type, lookup);
|
||||
if ("problems" in parsed) throw new Error(parsed.problems.join("; "));
|
||||
return parsed;
|
||||
},
|
||||
matches: (recipe, item) => recipe.ingredients[0]?.includes(item) ?? false,
|
||||
};
|
||||
}
|
||||
|
||||
// one recipe, or what's wrong with it, like ProcessingRecipe.validate
|
||||
@@ -120,11 +124,6 @@ export function parse_processing(
|
||||
return problems.length > 0 ? { problems } : { ingredients, results, fluid_results };
|
||||
}
|
||||
|
||||
// the first recipe that takes the item, like a machine's getRecipe
|
||||
export function processing_for(recipes: ProcessingRecipe[], item: Id): ProcessingRecipe | undefined {
|
||||
return recipes.find((recipe) => recipe.ingredients[0]?.includes(item));
|
||||
}
|
||||
|
||||
// what one go of a recipe makes: each result, with each of its count kept by its chance, like rollResults
|
||||
export function roll_results(recipe: ProcessingRecipe, random = Math.random): { id: Id; count: number }[] {
|
||||
return recipe.results.flatMap(({ id, count, chance }) => {
|
||||
|
||||
@@ -19,14 +19,7 @@ import {
|
||||
} from "./kinetics/connections.ts";
|
||||
import { flow_score, motor_step } from "./kinetics/generators.ts";
|
||||
import { handle_added, handle_removed, type KineticWorld, update_generated } from "./kinetics/spreading.ts";
|
||||
import {
|
||||
COMPACTING,
|
||||
PRESSING,
|
||||
processing_for,
|
||||
processing_recipes,
|
||||
type ProcessingRecipe,
|
||||
roll_results,
|
||||
} from "./recipes/processing.ts";
|
||||
import { COMPACTING, PRESSING, type ProcessingRecipe, roll_results } from "./recipes/processing.ts";
|
||||
import { CYCLE, cycle_rate, PRESS_AT, type PressShown, SCAN_TICKS } from "./processing/press.ts";
|
||||
import {
|
||||
apply_basin_recipe,
|
||||
@@ -90,16 +83,9 @@ 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
|
||||
const lookup = {
|
||||
expand: (choice: Id | Id[]) => ctx.tags.expand(choice),
|
||||
item_exists: (id: Id) => ctx.items.exists(id),
|
||||
fluid_exists: (id: Id) => ctx.fluids.exists(id),
|
||||
};
|
||||
const pressing = processing_recipes(ctx.recipes.list(PRESSING.id), PRESSING, lookup);
|
||||
// the ones that take more ingredients first, so a recipe isn't beaten by one that takes part of what it does, like
|
||||
// BasinOperatingBlockEntity.getMatchingRecipes
|
||||
const compacting = processing_recipes(ctx.recipes.list(COMPACTING.id), COMPACTING, lookup)
|
||||
const compacting = [...ctx.recipes.all<ProcessingRecipe>(COMPACTING.id)]
|
||||
.sort((a, b) => b.ingredients.length - a.ingredients.length);
|
||||
|
||||
ctx.components.register_block<KineticParams>("create:kinetic", {
|
||||
@@ -209,7 +195,7 @@ export function setup(ctx: ServerContext) {
|
||||
on_second: update_flow,
|
||||
});
|
||||
|
||||
register_press(ctx, pressing, compacting);
|
||||
register_press(ctx, compacting);
|
||||
register_basin(ctx);
|
||||
}
|
||||
|
||||
@@ -280,7 +266,8 @@ function set_basin(block: BlockRef, contents: Partial<BasinContents>) {
|
||||
// lone item turns into what it's pressed into, and from a stack one at a time is, landing beside the rest. with a
|
||||
// basin two blocks under it, it presses into that instead, making a compacting recipe in it each time, like
|
||||
// MechanicalPressBlockEntity as a BasinOperatingBlockEntity. it only ticks while it turns
|
||||
function register_press(ctx: ServerContext, recipes: ProcessingRecipe[], compacting: ProcessingRecipe[]) {
|
||||
function register_press(ctx: ServerContext, compacting: ProcessingRecipe[]) {
|
||||
const pressing_for = (item: Id) => ctx.recipes.find<ProcessingRecipe>(PRESSING.id, item);
|
||||
// the basin two blocks under it, with a block between for the head to come down through, like
|
||||
// BasinOperatingBlockEntity.getBasin
|
||||
const basin_under = (block: BlockRef) => {
|
||||
@@ -308,11 +295,11 @@ function register_press(ctx: ServerContext, recipes: ProcessingRecipe[], compact
|
||||
{ x: block.x + inset, y: block.y - 1 + inset, z: block.z + inset },
|
||||
{ x: block.x + 1 - inset, y: block.y - inset, z: block.z + 1 - inset },
|
||||
"bworld:item",
|
||||
).filter((item) => item.on_ground && processing_for(recipes, item.get_item()!.id));
|
||||
).filter((item) => item.on_ground && pressing_for(item.get_item()!.id));
|
||||
|
||||
const press = (item: EntityRef) => {
|
||||
const stack = item.get_item()!;
|
||||
const results = roll_results(processing_for(recipes, stack.id)!);
|
||||
const results = roll_results(pressing_for(stack.id)!);
|
||||
const { x, y, z } = item.position;
|
||||
if (stack.count === 1) {
|
||||
const [first, ...rest] = results;
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
// placed against, and chain drives join into lines
|
||||
import type { BlockPlacement, Face, Id, SharedContext } from "bworld/shared";
|
||||
import { chain_update, type ChainState } from "./kinetics/chains.ts";
|
||||
import { COMPACTING, PRESSING, processing_type } from "./recipes/processing.ts";
|
||||
import {
|
||||
AXES,
|
||||
type Axis,
|
||||
@@ -20,6 +21,10 @@ import {
|
||||
} from "./kinetics/connections.ts";
|
||||
|
||||
export function setup(ctx: SharedContext) {
|
||||
const lookup = { expand: ctx.tags.expand, item_exists: ctx.items.exists, fluid_exists: ctx.fluids.exists };
|
||||
ctx.recipes.register_type(PRESSING.id, processing_type(PRESSING, lookup));
|
||||
ctx.recipes.register_type(COMPACTING.id, processing_type(COMPACTING, lookup));
|
||||
|
||||
// what turns at x y z, undefined when nothing does
|
||||
const kinetic_at = (x: number, y: number, z: number): KineticShape | undefined => {
|
||||
const id = ctx.world.get_block(x, y, z);
|
||||
|
||||
Reference in New Issue
Block a user