Juices on the mechanical press !
This commit is contained in:
@@ -20,6 +20,7 @@ import {
|
||||
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,
|
||||
@@ -27,7 +28,14 @@ import {
|
||||
roll_results,
|
||||
} from "./recipes/processing.ts";
|
||||
import { CYCLE, cycle_rate, PRESS_AT, type PressShown, SCAN_TICKS } from "./processing/press.ts";
|
||||
import { type BasinTank, drain, fill, insert_item } from "./processing/basin.ts";
|
||||
import {
|
||||
apply_basin_recipe,
|
||||
type BasinContents,
|
||||
type BasinTank,
|
||||
drain,
|
||||
fill,
|
||||
insert_item,
|
||||
} from "./processing/basin.ts";
|
||||
|
||||
interface Config {
|
||||
max_speed: number;
|
||||
@@ -73,6 +81,8 @@ interface WheelData extends KineticData {
|
||||
interface PressData extends KineticData {
|
||||
// steps through the cycle while it's pressing, see processing/press.ts
|
||||
pressing?: number;
|
||||
// it's pressing into the basin under it, not on an item on the ground
|
||||
basin?: boolean;
|
||||
// ticks until it looks for something to press again
|
||||
scan?: number;
|
||||
}
|
||||
@@ -82,10 +92,16 @@ export function setup(ctx: ServerContext) {
|
||||
const world = kinetic_world(ctx, config);
|
||||
|
||||
// checked now, so a broken recipe stops the server rather than a press later
|
||||
const pressing = processing_recipes(ctx.recipes.list(PRESSING.id), PRESSING, {
|
||||
expand: (choice) => ctx.tags.expand(choice),
|
||||
item_exists: (id) => ctx.items.exists(id),
|
||||
});
|
||||
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)
|
||||
.sort((a, b) => b.ingredients.length - a.ingredients.length);
|
||||
|
||||
ctx.components.register_block<KineticParams>("create:kinetic", {
|
||||
on_create(block) {
|
||||
@@ -194,7 +210,7 @@ export function setup(ctx: ServerContext) {
|
||||
on_second: update_flow,
|
||||
});
|
||||
|
||||
register_press(ctx, pressing);
|
||||
register_press(ctx, pressing, compacting);
|
||||
register_basin(ctx);
|
||||
}
|
||||
|
||||
@@ -203,12 +219,10 @@ export function setup(ctx: ServerContext) {
|
||||
// what's in it is its "items" and "tanks" client data, kept nowhere else, which its visual draws. breaking it drops
|
||||
// the items, and the fluid is lost
|
||||
function register_basin(ctx: ServerContext) {
|
||||
const items_of = (block: BlockRef) => block.get_client_data<ItemStack[]>("items") ?? [];
|
||||
const tanks_of = (block: BlockRef) => block.get_client_data<BasinTank[]>("tanks") ?? [];
|
||||
const set_items = (block: BlockRef, items: readonly ItemStack[]) =>
|
||||
block.set_client_data("items", items.length > 0 ? items : undefined);
|
||||
const set_tanks = (block: BlockRef, tanks: BasinTank[]) =>
|
||||
block.set_client_data("tanks", tanks.length > 0 ? tanks : undefined);
|
||||
const items_of = (block: BlockRef) => basin_contents(block).items;
|
||||
const tanks_of = (block: BlockRef) => basin_contents(block).tanks;
|
||||
const set_items = (block: BlockRef, items: readonly ItemStack[]) => set_basin(block, { items });
|
||||
const set_tanks = (block: BlockRef, tanks: readonly BasinTank[]) => set_basin(block, { tanks });
|
||||
|
||||
ctx.components.register_block("create:basin", {
|
||||
on_entity_stand(block, _params, entity) {
|
||||
@@ -222,8 +236,9 @@ function register_basin(ctx: ServerContext) {
|
||||
on_interact(block, _params, player) {
|
||||
const held = player.held_item;
|
||||
if (!held) {
|
||||
for (const item of items_of(block)) player.give_item(item.id, item.count, item.data);
|
||||
set_items(block, []);
|
||||
const { items, outputs } = basin_contents(block);
|
||||
for (const item of [...items, ...outputs]) player.give_item(item.id, item.count, item.data);
|
||||
set_basin(block, { items: [], outputs: [] });
|
||||
return true;
|
||||
}
|
||||
const capacity = ctx.items.fluid_capacity(held.id);
|
||||
@@ -247,16 +262,55 @@ function register_basin(ctx: ServerContext) {
|
||||
return true;
|
||||
},
|
||||
on_break(block) {
|
||||
for (const item of items_of(block)) ctx.world.drop_item(block.x, block.y, block.z, item);
|
||||
const { items, outputs } = basin_contents(block);
|
||||
for (const item of [...items, ...outputs]) ctx.world.drop_item(block.x, block.y, block.z, item);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// what's in a basin, its "items", "outputs" and "tanks" client data, which its visual draws, and nowhere else
|
||||
function basin_contents(block: BlockRef): BasinContents {
|
||||
return {
|
||||
items: block.get_client_data<ItemStack[]>("items") ?? [],
|
||||
outputs: block.get_client_data<ItemStack[]>("outputs") ?? [],
|
||||
tanks: block.get_client_data<BasinTank[]>("tanks") ?? [],
|
||||
};
|
||||
}
|
||||
|
||||
// changes the parts of what's in a basin given, an empty one taking its field away
|
||||
function set_basin(block: BlockRef, contents: Partial<BasinContents>) {
|
||||
for (const [field, list] of Object.entries(contents)) {
|
||||
block.set_client_data(field, list.length > 0 ? list : undefined);
|
||||
}
|
||||
}
|
||||
|
||||
// presses items lying on the ground under it while it turns, like create's MechanicalPressBlockEntity pressing in the
|
||||
// world: it looks for one it has a recipe for every SCAN_TICKS, then comes down on it, faster the faster it turns. a
|
||||
// lone item turns into what it's pressed into, and from a stack one at a time is, landing beside the rest. it only
|
||||
// ticks while it turns
|
||||
function register_press(ctx: ServerContext, recipes: ProcessingRecipe[]) {
|
||||
// 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[]) {
|
||||
// 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) => {
|
||||
const basin = ctx.world.get_block_ref(block.x, block.y - 2, block.z);
|
||||
return basin && "create:basin" in ctx.blocks.components(basin.id) ? basin : undefined;
|
||||
};
|
||||
// the basin once the first compacting recipe that can be is made in it, undefined when none can. its results are
|
||||
// rolled when it's made for real, and all of them have to fit for it to be made at all
|
||||
const compact = (basin: BlockRef, roll: boolean) => {
|
||||
const contents = basin_contents(basin);
|
||||
for (const recipe of compacting) {
|
||||
const made = apply_basin_recipe(
|
||||
contents,
|
||||
{ ...recipe, results: roll ? roll_results(recipe) : recipe.results },
|
||||
(item) => ctx.items.max_stack(item.id, item.data),
|
||||
(a, b) => ctx.fluids.same(a, b),
|
||||
);
|
||||
if (made) return made;
|
||||
}
|
||||
};
|
||||
|
||||
// the items lying in the block under it, a bit in from its sides when looking for one, like create's
|
||||
const items_under = (block: BlockRef, inset: number) =>
|
||||
ctx.world.entities_in(
|
||||
@@ -293,20 +347,30 @@ function register_press(ctx: ServerContext, recipes: ProcessingRecipe[]) {
|
||||
block.set_ticking(false);
|
||||
return;
|
||||
}
|
||||
// over a basin it only presses into that
|
||||
const scan = (data.scan ?? 1) - 1;
|
||||
const found = scan <= 0 && items_under(block, 0.125).length > 0;
|
||||
block.data = { ...data, scan: scan <= 0 ? SCAN_TICKS : scan, ...(found && { pressing: 0 }) };
|
||||
if (found) block.set_client_data("press", { steps: 0, rate } satisfies PressShown);
|
||||
const basin = scan <= 0 ? basin_under(block) : undefined;
|
||||
const found = scan <= 0 &&
|
||||
(basin ? compact(basin, false) !== undefined : items_under(block, 0.125).length > 0);
|
||||
const mode = basin ? { basin: true } : {};
|
||||
block.data = { ...data, scan: scan <= 0 ? SCAN_TICKS : scan, ...(found && { pressing: 0, ...mode }) };
|
||||
if (found) block.set_client_data("press", { steps: 0, rate, ...mode } satisfies PressShown);
|
||||
return;
|
||||
}
|
||||
const steps = data.pressing;
|
||||
if (steps === PRESS_AT && rate > 0) {
|
||||
const [item] = items_under(block, 0);
|
||||
if (item) press(item);
|
||||
// what's in the basin could have changed since it started
|
||||
const basin = data.basin ? basin_under(block) : undefined;
|
||||
const made = basin && compact(basin, true);
|
||||
if (basin && made) set_basin(basin, made);
|
||||
else if (!data.basin) {
|
||||
const [item] = items_under(block, 0);
|
||||
if (item) press(item);
|
||||
}
|
||||
}
|
||||
// done, and it looks again straight away, for the rest of a stack
|
||||
if (steps > CYCLE) {
|
||||
const { pressing: _, scan: __, ...rest } = data;
|
||||
const { pressing: _, scan: __, basin: ___, ...rest } = data;
|
||||
block.data = rest;
|
||||
block.set_client_data("press", undefined);
|
||||
return;
|
||||
@@ -315,7 +379,8 @@ function register_press(ctx: ServerContext, recipes: ProcessingRecipe[]) {
|
||||
const next = steps < PRESS_AT && steps + rate >= PRESS_AT ? PRESS_AT : steps + rate;
|
||||
block.data = { ...data, pressing: next };
|
||||
if (block.get_client_data<PressShown>("press")?.rate !== rate) {
|
||||
block.set_client_data("press", { steps: next, rate } satisfies PressShown);
|
||||
const mode = data.basin ? { basin: true } : {};
|
||||
block.set_client_data("press", { steps: next, rate, ...mode } satisfies PressShown);
|
||||
}
|
||||
if (rate === 0) block.set_ticking(false);
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user