Mechanical press !

This commit is contained in:
2026-10-15 13:07:09 -03:00
parent b43e8fe8b8
commit c7a2392591
15 changed files with 403 additions and 86 deletions
+2 -72
View File
@@ -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