Mechanical press !
This commit is contained in:
@@ -5,9 +5,9 @@ redstone, contraptions, trains, equipment, decoration and schematics. It aims to
|
||||
possible, and to stay fast on both the server and the client when a world has thousands of moving parts.
|
||||
|
||||
Built so far: the importer and Create's imported blocks and items, R1, R2, R3 (`transformed` and `rotating`), R4, R5,
|
||||
R8, S0, S4, S7, S12, stonecutting from S9, the start of P1's kinetics (shafts and cogwheels turned by a hand crank), and
|
||||
Create's pressing recipes. Facts about Create come from reading `ports/create1.21.1` and `ports/create1.19.3`, and facts
|
||||
about bworld from its source.
|
||||
R8, S0, S4, S7, S12, stonecutting from S9, the start of P1's kinetics (shafts and cogwheels turned by a hand crank),
|
||||
Create's pressing recipes, and the mechanical press pressing items on the ground. Facts about Create come from reading
|
||||
`ports/create1.21.1` and `ports/create1.19.3`, and facts about bworld from its source.
|
||||
|
||||
## Sources
|
||||
|
||||
@@ -547,6 +547,15 @@ Check: new worlds have zinc and Create's stones, and casings join up.
|
||||
Check: ores go through crushing, washing and smelting into more ingots than a furnace alone gives, and brass is mixed in
|
||||
a heated basin.
|
||||
|
||||
As built so far: the mechanical press, pressing items lying on the ground under it (`PressingBehaviour`'s world mode, in
|
||||
`scripts/processing/press.ts` and the server script). While it turns it looks for an item with a pressing recipe every
|
||||
10 ticks, then runs its 240 step cycle at Create's speed for its rpm and presses at the bottom: a lone item turns into
|
||||
its result, and from a stack one is pressed and its results land beside it, like `bulkPressing` off. It stops ticking
|
||||
while it's still, and a kinetic block that starts turning ticks again. Clients get the step the cycle was at and its
|
||||
rate as `press` client data whenever the rate changes, and the visual moves the head from those, so nothing is sent
|
||||
while it runs at a steady speed. Pressing on belts, depots and basins, its stress impact, sound and particles come with
|
||||
those.
|
||||
|
||||
### P4: Logistics
|
||||
|
||||
- R3's `scrolling` type.
|
||||
|
||||
@@ -331,6 +331,8 @@ export interface EntityRef {
|
||||
readonly removed: boolean;
|
||||
// how fast it's moving, in blocks a second
|
||||
readonly velocity: Readonly<Position>;
|
||||
// standing or lying on something, like an item that's landed, like minecraft's onGround
|
||||
readonly on_ground: boolean;
|
||||
teleport(x: number, y: number, z: number): void;
|
||||
// adds to how fast it's moving, in blocks a second, like minecraft's Entity.push: a fan blowing it along
|
||||
push(x: number, y: number, z: number): void;
|
||||
|
||||
@@ -151,6 +151,7 @@ interface EntityRef {
|
||||
readonly season: Season; // that biome's
|
||||
readonly removed: boolean;
|
||||
readonly velocity: { x: number; y: number; z: number }; // in blocks a second
|
||||
readonly on_ground: boolean; // standing or lying on something
|
||||
teleport(x: number, y: number, z: number): void;
|
||||
push(x: number, y: number, z: number): void; // adds to its velocity
|
||||
remove(): void;
|
||||
|
||||
@@ -16,6 +16,10 @@
|
||||
"render_layer": "cutout",
|
||||
"mining": { "toughness": 1 },
|
||||
"drops": "create:mechanical_press",
|
||||
"item_model": "create:item_mechanical_press"
|
||||
"item_model": "create:item_mechanical_press",
|
||||
"components": {
|
||||
"create:kinetic": { "facing": "facing", "horizontal": true },
|
||||
"create:mechanical_press": {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,12 @@
|
||||
"create:kinetic": { "axis": "axis", "cog": "large" }
|
||||
}
|
||||
},
|
||||
"create:mechanical_press": {
|
||||
"components": {
|
||||
"create:kinetic": { "facing": "facing", "horizontal": true },
|
||||
"create:mechanical_press": {}
|
||||
}
|
||||
},
|
||||
"create:hand_crank": {
|
||||
"interactive": true,
|
||||
"components": {
|
||||
|
||||
@@ -5,7 +5,8 @@
|
||||
// as rotating instances, so a block turning steadily costs nothing per frame
|
||||
import type { ClientContext, Instances, ModelRef, Visual, VisualBlock } from "bworld/client";
|
||||
import type { Face } from "bworld/shared";
|
||||
import { type Axis, kinetic_shape, type KineticParams, type KineticShape } from "./kinetics/connections.ts";
|
||||
import { type Axis, FACINGS, kinetic_shape, type KineticParams, type KineticShape } from "./kinetics/connections.ts";
|
||||
import { head_offset, type PressShown } from "./processing/press.ts";
|
||||
|
||||
// the crank's handle model has its hub on its south side and its grip reaching north, so it's turned to put north
|
||||
// where the crank faces
|
||||
@@ -56,6 +57,37 @@ export function setup(ctx: ClientContext) {
|
||||
};
|
||||
}, { replaces_model: true });
|
||||
|
||||
// its casing is its block's model. the shaft through it turns like any other, and the head comes down and goes back
|
||||
// up through the cycle from where the server last said it was, like PressVisual
|
||||
ctx.render.register_visual("create:mechanical_press", (block, instances) => {
|
||||
const shape = shape_of(block);
|
||||
const turn = turning(block, instances, shape, { model: "create:block_shaft", ...AXIS_TURNS[shape.axis] });
|
||||
const head = instances.transformed({
|
||||
model: "create:block_mechanical_press_head",
|
||||
y: FACINGS.indexOf(shape.facing!) * 90,
|
||||
});
|
||||
let shown = block.get_client_data<PressShown>("press");
|
||||
// when this client heard of it, in the time on_frame gets
|
||||
let since: number | undefined;
|
||||
let offset: number | undefined;
|
||||
return {
|
||||
update() {
|
||||
turn();
|
||||
const press = block.get_client_data<PressShown>("press");
|
||||
if (JSON.stringify(press) === JSON.stringify(shown)) return;
|
||||
shown = press;
|
||||
since = undefined;
|
||||
},
|
||||
on_frame(time) {
|
||||
since ??= time;
|
||||
const now = shown ? head_offset(shown.steps + (time - since) * shown.rate) : 0;
|
||||
if (now === offset) return;
|
||||
offset = now;
|
||||
head.set_transform([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, -offset, 0, 1]);
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
// the half shaft turns with the speed like any kinetic block, and the handle catches up with it and slows down
|
||||
// with it, like HandCrankVisual's independent angle
|
||||
ctx.render.register_visual("create:hand_crank", (block, instances) => {
|
||||
|
||||
@@ -18,6 +18,8 @@ export interface KineticParams {
|
||||
// which ends of its axis have an axle that turns the block there: both, or for one with a facing only the end it
|
||||
// faces ("front") or the other ("back"). both when not given
|
||||
shaft?: "both" | "front" | "back";
|
||||
// its facing is only north, east, south or west, a 2 bit state, like HorizontalKineticBlock's
|
||||
horizontal?: boolean;
|
||||
// a cogwheel: a small one turns the small ones beside it on the same axis, a large one turns small ones on the same
|
||||
// axis at its corners, and large ones on other axes across its edges, like create's ICogWheel
|
||||
cog?: "small" | "large";
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// the mechanical press's cycle, like create's PressingBehaviour: the head comes down, presses at the bottom halfway
|
||||
// through, and goes back up. it counts CYCLE steps, more of them a tick the faster the press turns. no ctx, the server
|
||||
// runs the cycle and visuals draw it from the same numbers
|
||||
|
||||
export const CYCLE = 240;
|
||||
// where it presses, at the bottom
|
||||
export const PRESS_AT = CYCLE / 2;
|
||||
// how often a still press looks for something to press, in ticks
|
||||
export const SCAN_TICKS = 10;
|
||||
|
||||
// a pressing press's "press" client data, what its visual moves the head by: the step of the cycle it was at when this
|
||||
// was set, and how many steps a tick it's going
|
||||
export interface PressShown {
|
||||
steps: number;
|
||||
rate: number;
|
||||
}
|
||||
|
||||
// steps of the cycle a tick at a speed in rpm, like getRunningTickSpeed: 1 almost still, up to 60 at 512 rpm
|
||||
export function cycle_rate(speed: number): number {
|
||||
if (speed === 0) return 0;
|
||||
return Math.floor(1 + 59 * Math.min(1, Math.abs(speed) / 512));
|
||||
}
|
||||
|
||||
// how far down the head is that far through the cycle, 0 up to 1 at the bottom, like getRenderedHeadOffset: it
|
||||
// speeds up on the way down, stays down a moment after pressing, and goes up at a steady speed
|
||||
export function head_offset(steps: number): number {
|
||||
const clamp = (value: number) => Math.max(0, Math.min(1, value));
|
||||
if (steps < CYCLE * 2 / 3) return clamp((steps / CYCLE * 2) ** 3);
|
||||
return clamp((CYCLE - steps) / CYCLE * 3);
|
||||
}
|
||||
@@ -92,3 +92,17 @@ export function parse_processing(
|
||||
if (json?.heat_requirement !== undefined) problems.push(`${type.id} can't have a heat_requirement`);
|
||||
return problems.length > 0 ? { problems } : { ingredients, 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 }) => {
|
||||
let kept = count;
|
||||
if (chance < 1) { for (let i = 0; i < count; i++) if (random() > chance) kept--; }
|
||||
return kept > 0 ? [{ id, count: kept }] : [];
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,12 +1,19 @@
|
||||
// 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. and its
|
||||
// recipe types' checks
|
||||
import type { BlockRef, Id, ServerContext } from "bworld/server";
|
||||
// create's blocks that do something: so far what turns (create:kinetic), the hand crank that turns it, and the
|
||||
// mechanical press
|
||||
import type { BlockRef, EntityRef, 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";
|
||||
import {
|
||||
PRESSING,
|
||||
processing_for,
|
||||
processing_recipes,
|
||||
type ProcessingRecipe,
|
||||
roll_results,
|
||||
} from "./recipes/processing.ts";
|
||||
import { CYCLE, cycle_rate, PRESS_AT, type PressShown, SCAN_TICKS } from "./processing/press.ts";
|
||||
|
||||
interface Config {
|
||||
max_speed: number;
|
||||
@@ -34,12 +41,19 @@ interface CrankData extends KineticData {
|
||||
backwards?: boolean;
|
||||
}
|
||||
|
||||
interface PressData extends KineticData {
|
||||
// steps through the cycle while it's pressing, see processing/press.ts
|
||||
pressing?: number;
|
||||
// ticks until it looks for something to press again
|
||||
scan?: number;
|
||||
}
|
||||
|
||||
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, {
|
||||
const pressing = processing_recipes(ctx.recipes.list(PRESSING.id), PRESSING, {
|
||||
expand: (choice) => ctx.tags.expand(choice),
|
||||
item_exists: (id) => ctx.items.exists(id),
|
||||
});
|
||||
@@ -75,6 +89,78 @@ export function setup(ctx: ServerContext) {
|
||||
update_generated(world, [block.x, block.y, block.z]);
|
||||
},
|
||||
});
|
||||
|
||||
register_press(ctx, pressing);
|
||||
}
|
||||
|
||||
// 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[]) {
|
||||
// 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(
|
||||
{ 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));
|
||||
|
||||
const press = (item: EntityRef) => {
|
||||
const stack = item.get_item()!;
|
||||
const results = roll_results(processing_for(recipes, stack.id)!);
|
||||
const { x, y, z } = item.position;
|
||||
if (stack.count === 1) {
|
||||
const [first, ...rest] = results;
|
||||
item.set_item(first);
|
||||
for (const extra of rest) {
|
||||
const { x: vx, y: vy, z: vz } = item.velocity;
|
||||
ctx.world.spawn_item(x, y, z, extra).push(vx, vy, vz);
|
||||
}
|
||||
return;
|
||||
}
|
||||
const scatter = () => (Math.random() - 0.5) * 2;
|
||||
for (const result of results) ctx.world.spawn_item(x, y, z, result).push(scatter(), scatter(), scatter());
|
||||
item.set_item({ ...stack, count: stack.count - 1 });
|
||||
};
|
||||
|
||||
ctx.components.register_block("create:mechanical_press", {
|
||||
on_tick(block) {
|
||||
const data: PressData = block.data ?? {};
|
||||
const rate = cycle_rate(block.get_client_data<number>("speed") ?? 0);
|
||||
if (data.pressing === undefined) {
|
||||
// a block that starts turning ticks again, see kinetic_world
|
||||
if (rate === 0) {
|
||||
block.set_ticking(false);
|
||||
return;
|
||||
}
|
||||
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);
|
||||
return;
|
||||
}
|
||||
const steps = data.pressing;
|
||||
if (steps === PRESS_AT && rate > 0) {
|
||||
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;
|
||||
block.data = rest;
|
||||
block.set_client_data("press", undefined);
|
||||
return;
|
||||
}
|
||||
// it stops at the bottom to press on the next tick, however fast it's going
|
||||
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);
|
||||
}
|
||||
if (rate === 0) block.set_ticking(false);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// the world as rotation spreading sees it: blocks with create:kinetic, their speed in client data and where it comes
|
||||
@@ -125,6 +211,8 @@ function kinetic_world(ctx: ServerContext, config: Config): KineticWorld {
|
||||
},
|
||||
set(at, speed, source, network) {
|
||||
const block = block_at(at);
|
||||
// machines stop ticking while they're still, like a press, and start again once they turn
|
||||
if (speed !== 0 && !block.get_client_data("speed")) block.set_ticking(true);
|
||||
block.set_client_data("speed", speed === 0 ? undefined : speed);
|
||||
const { source: _, network: __, ...rest }: KineticData = block.data ?? {};
|
||||
block.data = { ...rest, ...(source && { source }), ...(network !== undefined && { network }) };
|
||||
|
||||
@@ -30,11 +30,11 @@ export function setup(ctx: SharedContext) {
|
||||
return kinetic_at(x + dx, y + dy, z + dz);
|
||||
};
|
||||
|
||||
// the side of x y z with an axle pointing at it, like getPreferredFacing. undefined when there's none, or when
|
||||
// axles point at it along two axes, since it can't line up with both
|
||||
const axle_beside = (x: number, y: number, z: number): Face | undefined => {
|
||||
// the side of x y z with an axle pointing at it, like getPreferredFacing, of the sides given. undefined when there's
|
||||
// none, or when axles point at it along two axes, since it can't line up with both
|
||||
const axle_beside = (x: number, y: number, z: number, sides = FACINGS): Face | undefined => {
|
||||
let found: Face | undefined;
|
||||
for (const side of FACINGS) {
|
||||
for (const side of sides) {
|
||||
if (!beside(x, y, z, side)?.shafts.includes(OPPOSITE[side])) continue;
|
||||
if (found && face_axis(found) !== face_axis(side)) return undefined;
|
||||
found = side;
|
||||
@@ -73,6 +73,11 @@ export function setup(ctx: SharedContext) {
|
||||
if (!cog_fits(placement.x, placement.y, placement.z, axis, params.cog)) return false;
|
||||
return { [params.axis ?? "axis"]: AXES.indexOf(axis) };
|
||||
}
|
||||
// along an axle beside it, or else facing the player, like HorizontalKineticBlock and MechanicalPressBlock
|
||||
if (params.facing !== undefined && params.horizontal) {
|
||||
const side = axle_beside(placement.x, placement.y, placement.z, FACINGS.slice(0, 4));
|
||||
return { [params.facing]: FACINGS.indexOf(side ?? OPPOSITE[placement.facing]) };
|
||||
}
|
||||
const side = axle_beside(placement.x, placement.y, placement.z);
|
||||
if (params.facing !== undefined) {
|
||||
const facing = side && !placement.sneaking
|
||||
|
||||
@@ -1468,6 +1468,9 @@ export function entity_ref(entity: Entity): EntityRef {
|
||||
get velocity() {
|
||||
return { x: entity.vx, y: entity.vy, z: entity.vz };
|
||||
},
|
||||
get on_ground() {
|
||||
return entity.on_ground;
|
||||
},
|
||||
teleport(x, y, z) {
|
||||
if (!entity.removed) entity.set_position(x, y, z);
|
||||
},
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import { type BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
|
||||
import { AIR_ID } from "$/common/protocol.ts";
|
||||
import { get_state_value, set_state_value } from "$/common/utils.ts";
|
||||
import type { Face } from "$/common/mod_api/shared.ts";
|
||||
import type { ClientContext, Instances, ModelRef, Visual, VisualBlock } from "$/common/mod_api/client.ts";
|
||||
import { setup as setup_client } from "$/mods/create/scripts/client.ts";
|
||||
import { stone_floor, test_game } from "../helpers.ts";
|
||||
|
||||
// facing and axis states, in the order the importer numbers them
|
||||
export const FACING = { north: 0, east: 1, south: 2, west: 3, up: 4, down: 5 };
|
||||
export const AXIS = { x: 0, y: 1, z: 2 };
|
||||
|
||||
// alice standing south of 0 100 0 on a stone floor, looking north, with nothing in her hands. config changes create's
|
||||
export async function setup(config: Record<string, unknown> = {}) {
|
||||
const t = await test_game("mods", undefined, undefined, { create: config });
|
||||
const { game } = t;
|
||||
t.join(1, "alice");
|
||||
stone_floor(game, 6);
|
||||
for (let x = -6; x <= 6; x++) {
|
||||
for (let z = -6; z <= 6; z++) for (let y = 100; y < 104; y++) game.set_block(x, y, z, AIR_ID);
|
||||
}
|
||||
const move = (sneaking = false, pitch = 0) =>
|
||||
t.send(1, { type: "move", x: 0.5, y: 100, z: 2.5, yaw: 0, pitch, ...(sneaking && { sneaking }) });
|
||||
move();
|
||||
|
||||
// a block with one state set, the way a player would have placed it
|
||||
const place = (x: number, y: number, z: number, id: string, state?: string, value = 0) => {
|
||||
const info = EverythingRegistry.get<BlockRegistry>("blocks", id)!;
|
||||
game.set_block(x, y, z, id, undefined, state ? set_state_value(0, info, state, value)! >>> 16 : 0);
|
||||
};
|
||||
const speed = (x: number, y: number, z: number) => game.world.get_tile(x, y, z)?.client_data?.speed as number ?? 0;
|
||||
const state = (x: number, y: number, z: number, name: string) =>
|
||||
get_state_value(game.world.get_block_value(x, y, z), game.world.get_block_info(x, y, z)!, name);
|
||||
const use = (x: number, y: number, z: number, face: Face = "south") =>
|
||||
t.send(1, { type: "use_block", x, y, z, face, hit: [0.5, 0.5, 1] });
|
||||
const ticks = (n: number) => {
|
||||
for (let i = 0; i < n; i++) game.tick();
|
||||
};
|
||||
// the rpm a crank turns at
|
||||
const crank =
|
||||
(EverythingRegistry.get<BlockRegistry>("blocks", "create:hand_crank")!.components!["create:hand_crank"] as {
|
||||
speed: number;
|
||||
}).speed;
|
||||
return { ...t, move, place, speed, state, use, ticks, crank };
|
||||
}
|
||||
|
||||
// create's client script with only what its visuals need, and a block for one to draw that records what its instances
|
||||
// do, by model ("own" for the block's own model): how rotating ones turn, and where transformed ones are moved
|
||||
export async function visual_for(
|
||||
id: string,
|
||||
states: Record<string, number>,
|
||||
data: Record<string, unknown>,
|
||||
[x, y, z] = [0, 100, 0],
|
||||
) {
|
||||
await test_game("mods");
|
||||
const visuals = new Map<string, (block: VisualBlock, instances: Instances) => Visual>();
|
||||
setup_client({
|
||||
blocks: {
|
||||
components: (id: string) =>
|
||||
structuredClone(EverythingRegistry.get<BlockRegistry>("blocks", id)!.components),
|
||||
},
|
||||
render: { register_visual: (id: string, create: never) => visuals.set(id, create) },
|
||||
} as unknown as ClientContext);
|
||||
const key = (model?: ModelRef) => model === undefined ? "own" : JSON.stringify(model);
|
||||
const turns = new Map<string, [string, number, number]>();
|
||||
const transforms = new Map<string, number[]>();
|
||||
const instances = {
|
||||
rotating: (model?: ModelRef) => ({
|
||||
set_rotation: (axis: string, rpm: number, offset = 0) => turns.set(key(model), [axis, rpm, offset]),
|
||||
delete() {},
|
||||
}),
|
||||
transformed: (model?: ModelRef) => ({
|
||||
set_transform: (matrix: ArrayLike<number>) => transforms.set(key(model), Array.from(matrix)),
|
||||
delete() {},
|
||||
}),
|
||||
} as unknown as Instances;
|
||||
const block = { id, x, y, z, states, get_client_data: (field: string) => structuredClone(data[field]) as never };
|
||||
const visual = visuals.get(id)!(block, instances);
|
||||
return { visual, turns, transforms, data };
|
||||
}
|
||||
@@ -4,51 +4,9 @@
|
||||
// create's kinetics: shafts take the speed of what turns them, a hand crank turns them while it's being clicked, and a
|
||||
// block that would turn two ways at once breaks
|
||||
import { assert, assertEquals } from "@std/assert";
|
||||
import { type BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
|
||||
import { AIR_ID } from "$/common/protocol.ts";
|
||||
import { get_state_value, set_state_value } from "$/common/utils.ts";
|
||||
import type { Face } from "$/common/mod_api/shared.ts";
|
||||
import type { ClientContext, Instances, ModelRef, Visual, VisualBlock } from "$/common/mod_api/client.ts";
|
||||
import { setup as setup_client } from "$/mods/create/scripts/client.ts";
|
||||
import { stone_floor, test_game } from "../helpers.ts";
|
||||
|
||||
// facing and axis states, in the order the importer numbers them
|
||||
const FACING = { north: 0, east: 1, south: 2, west: 3, up: 4, down: 5 };
|
||||
const AXIS = { x: 0, y: 1, z: 2 };
|
||||
|
||||
// alice standing south of 0 100 0 on a stone floor, looking north, with nothing in her hands. config changes create's
|
||||
async function setup(config: Record<string, unknown> = {}) {
|
||||
const t = await test_game("mods", undefined, undefined, { create: config });
|
||||
const { game } = t;
|
||||
t.join(1, "alice");
|
||||
stone_floor(game, 6);
|
||||
for (let x = -6; x <= 6; x++) {
|
||||
for (let z = -6; z <= 6; z++) for (let y = 100; y < 104; y++) game.set_block(x, y, z, AIR_ID);
|
||||
}
|
||||
const move = (sneaking = false, pitch = 0) =>
|
||||
t.send(1, { type: "move", x: 0.5, y: 100, z: 2.5, yaw: 0, pitch, ...(sneaking && { sneaking }) });
|
||||
move();
|
||||
|
||||
// a block with one state set, the way a player would have placed it
|
||||
const place = (x: number, y: number, z: number, id: string, state?: string, value = 0) => {
|
||||
const info = EverythingRegistry.get<BlockRegistry>("blocks", id)!;
|
||||
game.set_block(x, y, z, id, undefined, state ? set_state_value(0, info, state, value)! >>> 16 : 0);
|
||||
};
|
||||
const speed = (x: number, y: number, z: number) => game.world.get_tile(x, y, z)?.client_data?.speed as number ?? 0;
|
||||
const state = (x: number, y: number, z: number, name: string) =>
|
||||
get_state_value(game.world.get_block_value(x, y, z), game.world.get_block_info(x, y, z)!, name);
|
||||
const use = (x: number, y: number, z: number, face: Face = "south") =>
|
||||
t.send(1, { type: "use_block", x, y, z, face, hit: [0.5, 0.5, 1] });
|
||||
const ticks = (n: number) => {
|
||||
for (let i = 0; i < n; i++) game.tick();
|
||||
};
|
||||
// the rpm a crank turns at
|
||||
const crank =
|
||||
(EverythingRegistry.get<BlockRegistry>("blocks", "create:hand_crank")!.components!["create:hand_crank"] as {
|
||||
speed: number;
|
||||
}).speed;
|
||||
return { ...t, move, place, speed, state, use, ticks, crank };
|
||||
}
|
||||
import { test_game } from "../helpers.ts";
|
||||
import { AXIS, FACING, setup, visual_for } from "./helpers.ts";
|
||||
|
||||
// a crank at 0 100 0 facing west, its axle in a line of shafts along x from 1 to length
|
||||
async function crank_and_line(length = 4, config?: Record<string, unknown>) {
|
||||
@@ -287,34 +245,6 @@ Deno.test("a cog lines up with the cog it's placed against, and can't go where i
|
||||
assertEquals(t.state(1, 100, 1, "axis"), AXIS.y);
|
||||
});
|
||||
|
||||
// create's client script with only what its visuals need, and a block for one to draw that records how its instances
|
||||
// turn, by model: "own" for the block's own model
|
||||
async function visual_for(
|
||||
id: string,
|
||||
states: Record<string, number>,
|
||||
data: Record<string, unknown>,
|
||||
[x, y, z] = [0, 100, 0],
|
||||
) {
|
||||
await test_game("mods");
|
||||
const visuals = new Map<string, (block: VisualBlock, instances: Instances) => Visual>();
|
||||
setup_client({
|
||||
blocks: {
|
||||
components: (id: string) =>
|
||||
structuredClone(EverythingRegistry.get<BlockRegistry>("blocks", id)!.components),
|
||||
},
|
||||
render: { register_visual: (id: string, create: never) => visuals.set(id, create) },
|
||||
} as unknown as ClientContext);
|
||||
const turns = new Map<string, [string, number, number]>();
|
||||
const rotating = (model?: ModelRef) => ({
|
||||
set_rotation: (axis: string, rpm: number, offset = 0) =>
|
||||
turns.set(model === undefined ? "own" : JSON.stringify(model), [axis, rpm, offset]),
|
||||
delete() {},
|
||||
});
|
||||
const block = { id, x, y, z, states, get_client_data: (field: string) => data[field] as never };
|
||||
const visual = visuals.get(id)!(block, { rotating, transformed: () => ({}) } as unknown as Instances);
|
||||
return { visual, turns, data };
|
||||
}
|
||||
|
||||
Deno.test("a shaft is drawn turning around its axis at the speed its client data says", async () => {
|
||||
const { visual, turns, data } = await visual_for("create:shaft", { axis: AXIS.z }, { speed: 16 });
|
||||
// its own model, the way its states draw it
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// the mechanical press: turned, it slams down on items lying on the ground under it and presses them with create's
|
||||
// pressing recipes, one at a time, and it only ticks while it turns
|
||||
import { assert, assertEquals } from "@std/assert";
|
||||
import { ItemStack } from "$/common/inventory.ts";
|
||||
import { FACING, setup, visual_for } from "./helpers.ts";
|
||||
|
||||
// a press at 1 101 0 along x, turned by a crank on its west side, with the floor two blocks under it
|
||||
async function press_with_crank() {
|
||||
const t = await setup();
|
||||
t.place(0, 101, 0, "create:hand_crank", "facing", FACING.west);
|
||||
t.place(1, 101, 0, "create:mechanical_press", "facing", FACING.east);
|
||||
// holding use on the crank, which clicks it every 4 ticks
|
||||
const crank = (ticks: number) => {
|
||||
for (let i = 0; i < ticks; i += 4) {
|
||||
t.use(0, 101, 0);
|
||||
t.ticks(4);
|
||||
}
|
||||
};
|
||||
// what's lying around, added up by item
|
||||
const lying = () => {
|
||||
const counts: Record<string, number> = {};
|
||||
for (const { item } of t.game.item_entities()) counts[item.type_id] = (counts[item.type_id] ?? 0) + item.amount;
|
||||
return counts;
|
||||
};
|
||||
const ingots = (count: number) => t.game.spawn_item(1.5, 100, 0.5, new ItemStack("bworld:iron_ingot", count));
|
||||
return { ...t, crank, lying, ingots };
|
||||
}
|
||||
|
||||
Deno.test("a turning press slams down on an ingot lying under it and presses it into a sheet", async () => {
|
||||
const t = await press_with_crank();
|
||||
t.ingots(1);
|
||||
// still, it waits
|
||||
t.ticks(100);
|
||||
assertEquals(t.lying(), { "bworld:iron_ingot": 1 });
|
||||
|
||||
t.crank(200);
|
||||
assertEquals(t.lying(), { "create:iron_sheet": 1 });
|
||||
});
|
||||
|
||||
Deno.test("from a stack, a press presses one at a time, and each lands beside the rest", async () => {
|
||||
const t = await press_with_crank();
|
||||
t.ingots(3);
|
||||
for (let i = 0; i < 100 && !t.lying()["create:iron_sheet"]; i++) t.crank(4);
|
||||
assertEquals(t.lying(), { "bworld:iron_ingot": 2, "create:iron_sheet": 1 });
|
||||
assertEquals(t.game.item_entities().length, 2);
|
||||
|
||||
t.crank(600);
|
||||
assertEquals(t.lying(), { "create:iron_sheet": 3 });
|
||||
});
|
||||
|
||||
Deno.test("a press only ticks while it turns", async () => {
|
||||
const t = await press_with_crank();
|
||||
const ticking = () => {
|
||||
const tile = t.game.world.get_tile(1, 101, 0);
|
||||
return tile !== undefined && t.game.world.ticks(tile);
|
||||
};
|
||||
assertEquals(ticking(), false);
|
||||
t.crank(8);
|
||||
assertEquals(ticking(), true);
|
||||
// the crank lets go and the press stops, nothing under it to press
|
||||
t.ticks(40);
|
||||
assertEquals(ticking(), false);
|
||||
});
|
||||
|
||||
Deno.test("a press lines up with an axle beside it, and otherwise faces the player", async () => {
|
||||
const t = await setup();
|
||||
t.send(1, { type: "chat", text: "/give create:mechanical_press 64" });
|
||||
t.send(1, { type: "select_slot", slot: 0 });
|
||||
// alice looks north
|
||||
t.use(-2, 99, 0, "top");
|
||||
assertEquals(t.state(-2, 100, 0, "facing"), FACING.south);
|
||||
t.place(2, 100, 0, "create:shaft", "axis", 0);
|
||||
t.use(3, 99, 0, "top");
|
||||
assertEquals(t.state(3, 100, 0, "facing"), FACING.west);
|
||||
});
|
||||
|
||||
Deno.test("a press's head comes down to press halfway through its cycle and goes back up, as the server says", async () => {
|
||||
const { visual, turns, transforms, data } = await visual_for(
|
||||
"create:mechanical_press",
|
||||
{ facing: FACING.east },
|
||||
{ speed: 32 },
|
||||
);
|
||||
// the shaft through it turns along its facing
|
||||
const shaft = [...turns].find(([model]) => model.includes("shaft"))![1];
|
||||
assertEquals(shaft.slice(0, 2), ["x", 32]);
|
||||
// how far down the head is, from its transform
|
||||
const down = () => -([...transforms].find(([model]) => model.includes("head"))?.[1][13] ?? 0);
|
||||
|
||||
visual.on_frame!(5);
|
||||
assertEquals(down(), 0);
|
||||
// the server says a cycle started, 4 steps of 240 a tick
|
||||
data.press = { steps: 0, rate: 4 };
|
||||
visual.update!();
|
||||
visual.on_frame!(10);
|
||||
assertEquals(down(), 0);
|
||||
visual.on_frame!(25);
|
||||
assert(down() > 0 && down() < 1, `${down()}`);
|
||||
visual.on_frame!(40);
|
||||
assertEquals(down(), 1);
|
||||
visual.on_frame!(70);
|
||||
assertEquals(down(), 0);
|
||||
});
|
||||
Reference in New Issue
Block a user