Mechanical press !
This commit is contained in:
@@ -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