Juices on the mechanical press !
This commit is contained in:
@@ -2,18 +2,22 @@
|
||||
// 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
|
||||
// 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 { Id } from "bworld/shared";
|
||||
import type { FluidVariant, Id } from "bworld/shared";
|
||||
|
||||
// how many ingredients and results a type takes at most, like getMaxInputCount and getMaxOutputCount
|
||||
// how many ingredients, item results and fluid results a type takes at most, like getMaxInputCount,
|
||||
// getMaxOutputCount and getMaxFluidOutputCount
|
||||
export interface ProcessingType {
|
||||
readonly id: Id;
|
||||
readonly inputs: number;
|
||||
readonly outputs: number;
|
||||
readonly fluid_outputs: number;
|
||||
}
|
||||
|
||||
export const PRESSING: ProcessingType = { id: "create:pressing", inputs: 1, outputs: 2 };
|
||||
export const PRESSING: ProcessingType = { id: "create:pressing", inputs: 1, outputs: 2, fluid_outputs: 0 };
|
||||
// what a press does to what's in a basin under it, like BasinRecipe's limits
|
||||
export const COMPACTING: ProcessingType = { id: "create:compacting", inputs: 64, outputs: 4, fluid_outputs: 2 };
|
||||
|
||||
export interface ProcessingResult {
|
||||
id: Id;
|
||||
@@ -23,9 +27,11 @@ export interface ProcessingResult {
|
||||
}
|
||||
|
||||
export interface ProcessingRecipe {
|
||||
// each ingredient as every item that fits it
|
||||
// each ingredient as every item that fits it, one of it each
|
||||
ingredients: Id[][];
|
||||
results: ProcessingResult[];
|
||||
// in mB
|
||||
fluid_results: { fluid: FluidVariant; amount: number }[];
|
||||
}
|
||||
|
||||
// what reading recipes needs from the game
|
||||
@@ -33,6 +39,7 @@ export interface RecipeLookup {
|
||||
// an item choice as its items, like ctx.tags.expand
|
||||
expand(choice: Id | Id[]): Id[];
|
||||
item_exists(id: Id): boolean;
|
||||
fluid_exists(id: Id): boolean;
|
||||
}
|
||||
|
||||
// every recipe of type, from their json, or an error saying what's wrong with each broken one
|
||||
@@ -55,6 +62,7 @@ export function parse_processing(
|
||||
const problems: string[] = [];
|
||||
const ingredients: Id[][] = [];
|
||||
const results: ProcessingResult[] = [];
|
||||
const fluid_results: ProcessingRecipe["fluid_results"] = [];
|
||||
if (!Array.isArray(json?.ingredients) || json.ingredients.length === 0) {
|
||||
problems.push("ingredients must be a list of item choices");
|
||||
} else {
|
||||
@@ -62,6 +70,10 @@ export function parse_processing(
|
||||
problems.push(`it takes ${json.ingredients.length} ingredients, and ${type.id} takes ${type.inputs}`);
|
||||
}
|
||||
for (const choice of json.ingredients) {
|
||||
if (typeof choice === "object" && choice !== null && "fluid" in choice) {
|
||||
problems.push("fluid ingredients aren't supported yet");
|
||||
continue;
|
||||
}
|
||||
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)) : [];
|
||||
@@ -70,13 +82,28 @@ export function parse_processing(
|
||||
else ingredients.push(lookup.expand(choice));
|
||||
}
|
||||
}
|
||||
const fluids = type.fluid_outputs > 0 ? " or { fluid, amount }" : "";
|
||||
if (!Array.isArray(json?.results) || json.results.length === 0) {
|
||||
problems.push("results must be a list of { id, count, chance }");
|
||||
problems.push(`results must be a list of { id, count, chance }${fluids}`);
|
||||
} else {
|
||||
if (json.results.length > type.outputs) {
|
||||
problems.push(`it makes ${json.results.length} results, and ${type.id} makes ${type.outputs}`);
|
||||
// deno-lint-ignore no-explicit-any
|
||||
const is_fluid = (result: any) => typeof result === "object" && result !== null && "fluid" in result;
|
||||
const items = json.results.filter((result: unknown) => !is_fluid(result)).length;
|
||||
if (items > type.outputs) problems.push(`it makes ${items} items, and ${type.id} makes ${type.outputs}`);
|
||||
if (json.results.length - items > type.fluid_outputs) {
|
||||
problems.push(`it makes ${json.results.length - items} fluids, and ${type.id} makes ${type.fluid_outputs}`);
|
||||
}
|
||||
for (const result of json.results) {
|
||||
// deno-lint-ignore no-explicit-any
|
||||
for (const result of json.results as any[]) {
|
||||
if (is_fluid(result)) {
|
||||
const { fluid, amount } = result;
|
||||
if (typeof fluid?.id !== "string" || !lookup.fluid_exists(fluid.id)) {
|
||||
problems.push(`it makes the fluid ${JSON.stringify(fluid)}, which doesn't exist`);
|
||||
} else if (!Number.isInteger(amount) || amount < 1) {
|
||||
problems.push(`${fluid.id}'s amount must be a whole number of mB above 0`);
|
||||
} else fluid_results.push({ fluid, amount });
|
||||
continue;
|
||||
}
|
||||
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) {
|
||||
@@ -90,7 +117,7 @@ export function parse_processing(
|
||||
// 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 };
|
||||
return problems.length > 0 ? { problems } : { ingredients, results, fluid_results };
|
||||
}
|
||||
|
||||
// the first recipe that takes the item, like a machine's getRecipe
|
||||
|
||||
Reference in New Issue
Block a user