// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // create's blocks that pass rotation on: gearboxes, clutches and gearshifts worked by verdigris, encased shafts and // cogs, and chain drives import { assert, assertEquals } from "@std/assert"; import { AIR_ID } from "@/common/protocol.ts"; import { chain_ratio } from "@/mods/create/scripts/kinetics/connections.ts"; import { AXIS, FACING, setup, visual_for } from "./helpers.ts"; // a crank at 0 100 0 facing west, turning a shaft at 1 100 0 along x, and the speed it turns that line at async function crank_and_shaft(config?: Record) { const t = await setup(config); t.place(0, 100, 0, "create:hand_crank", "facing", FACING.west); t.place(1, 100, 0, "create:shaft", "axis", AXIS.x); return { ...t, s: -t.crank }; } // gives alice some of id and has her hold them const give = (t: Awaited>, id: string) => { t.send(1, { type: "chat", text: `/give ${id} 64` }); const { inventory } = t.game.players()[0]; const slot = Array.from({ length: 9 }, (_, i) => i).find((i) => inventory.get_slot(i).type_id === id)!; t.send(1, { type: "select_slot", slot }); }; Deno.test("a gearbox turns the axles on its 4 sides, the one across from what turns it the other way", async () => { const t = await crank_and_shaft(); t.place(2, 100, 0, "create:gearbox", "axis", AXIS.y); t.place(3, 100, 0, "create:shaft", "axis", AXIS.x); t.place(2, 100, 1, "create:shaft", "axis", AXIS.z); t.place(2, 100, -1, "create:shaft", "axis", AXIS.z); // along its own axis there's no axle t.place(2, 101, 0, "create:shaft", "axis", AXIS.y); t.use(0, 100, 0); assertEquals(t.speed(3, 100, 0), -t.s); // the two to the sides turn opposite ways around z, at the same speed assertEquals(Math.abs(t.speed(2, 100, 1)), t.crank); assertEquals(t.speed(2, 100, -1), -t.speed(2, 100, 1)); assertEquals(t.speed(2, 101, 0), 0); }); Deno.test("a gearbox lies flat, the vertical gearbox stands it on its side, and it breaks into the one it was", async () => { const t = await setup(); give(t, "create:gearbox"); t.use(0, 99, 0, "top"); assertEquals(t.state(0, 100, 0, "axis"), AXIS.y); give(t, "create:vertical_gearbox"); t.use(3, 99, 0, "top"); assertEquals(t.game.world.get_block_id(3, 100, 0), "create:gearbox"); // looking north, its axles point north and south, and up and down assertEquals(t.state(3, 100, 0, "axis"), AXIS.x); // beside the first's axle, it lines up across it t.use(1, 99, 0, "top"); assertEquals(t.state(1, 100, 0, "axis"), AXIS.z); t.game.destroy_block(1, 100, 0); t.game.destroy_block(3, 100, 0); t.game.destroy_block(0, 100, 0); const dropped = t.game.item_entities().map((e) => e.item.type_id).sort(); assertEquals(dropped, ["create:gearbox", "create:vertical_gearbox", "create:vertical_gearbox"]); }); Deno.test("a powered clutch cuts off what's past it, and a powered gearshift turns it the other way", async () => { for (const [id, powered] of [["create:clutch", 0], ["create:gearshift", 1]] as const) { const t = await crank_and_shaft(); t.place(2, 100, 0, id, "axis", AXIS.x); t.place(3, 100, 0, "create:shaft", "axis", AXIS.x); t.use(0, 100, 0); assertEquals(t.speed(3, 100, 0), t.s); // a verdigris block beside it powers it t.game.set_block(2, 101, 0, "bworld:verdigris_block"); assertEquals(t.state(2, 100, 0, "powered"), 1); assertEquals([t.speed(2, 100, 0), t.speed(3, 100, 0)], [t.s, powered * -t.s]); t.game.set_block(2, 101, 0, AIR_ID); assertEquals(t.speed(3, 100, 0), t.s); } }); Deno.test("a gearbox's and a clutch's half shafts are drawn turning the way their sides do", async () => { const gearbox = await visual_for("create:gearbox", { axis: AXIS.y }, { speed: 16, source: "west" }); const turns = (visual: typeof gearbox) => [...visual.turns.values()].map(([axis, rpm]) => `${axis} ${rpm}`).sort(); assertEquals(turns(gearbox), ["x -16", "x 16", "z -16", "z 16"]); const clutch = await visual_for("create:clutch", { axis: AXIS.x, powered: 1 }, { speed: 16, source: "west" }); assertEquals(turns(clutch), ["x 0", "x 16"]); clutch.data.source = undefined; clutch.visual.update!(); assertEquals(turns(clutch), ["x 16", "x 16"]); }); Deno.test("casing used on a turning shaft or cog encases it, and it goes on turning", async () => { const t = await crank_and_shaft(); t.place(2, 100, 0, "create:cogwheel", "axis", AXIS.x); t.place(3, 100, 0, "create:shaft", "axis", AXIS.x); t.place(2, 101, 0, "create:cogwheel", "axis", AXIS.x); t.use(0, 100, 0); give(t, "create:andesite_casing"); t.use(1, 100, 0); assertEquals(t.game.world.get_block_id(1, 100, 0), "create:andesite_encased_shaft"); assertEquals(t.state(1, 100, 0, "axis"), AXIS.x); // an encased cog gets an axle out of each end with an axle beside it, and still turns the cogs beside it t.use(2, 100, 0); assertEquals(t.game.world.get_block_id(2, 100, 0), "create:andesite_encased_cogwheel"); assertEquals([t.state(2, 100, 0, "top_shaft"), t.state(2, 100, 0, "bottom_shaft")], [1, 1]); assertEquals([t.speed(1, 100, 0), t.speed(3, 100, 0), t.speed(2, 101, 0)], [t.s, t.s, -t.s]); // the casing isn't used up assertEquals(t.game.players()[0].held_item?.amount, 64); // broken, they give back what was encased t.game.destroy_block(1, 100, 0); t.game.destroy_block(2, 100, 0); const dropped = t.game.item_entities().map((e) => e.item.type_id).sort(); assertEquals(dropped, ["create:cogwheel", "create:shaft"]); }); // chain drives placed by a player looking east, so their axles go along x, in a line along z from 1 100 0, the first // turned by the crank, and a shaft past the last async function chain_line(first = "create:encased_chain_drive") { const t = await crank_and_shaft(); t.game.set_block(1, 100, 0, AIR_ID); t.send(1, { type: "move", x: 0.5, y: 100, z: 2.5, yaw: -Math.PI / 2, pitch: 0 }); give(t, first); t.use(1, 99, 0, "top"); give(t, "create:encased_chain_drive"); t.use(1, 99, 1, "top"); t.use(1, 99, 2, "top"); t.place(2, 100, 2, "create:shaft", "axis", AXIS.x); return t; } Deno.test("chain drives placed in a line turn each other, and one taken out ends the line", async () => { const t = await chain_line(); for (let z = 0; z <= 2; z++) assertEquals(t.state(1, 100, z, "axis"), AXIS.x); t.use(0, 100, 0); assertEquals([t.speed(1, 100, 1), t.speed(1, 100, 2), t.speed(2, 100, 2)], [t.s, t.s, t.s]); t.game.destroy_block(1, 100, 1); assertEquals(t.speed(2, 100, 2), 0); // put back, it joins them again t.use(1, 99, 1, "top"); assertEquals(t.speed(2, 100, 2), t.s); }); Deno.test("an adjustable chain gearshift turns the rest of its line faster the more power goes into it", async () => { const t = await chain_line("create:adjustable_chain_gearshift"); assertEquals([1, 2].map((z) => t.game.world.get_block_id(1, 100, z)), Array(2).fill("create:encased_chain_drive")); t.use(0, 100, 0); assertEquals(t.speed(1, 100, 2), t.s); t.game.set_block(1, 101, 0, "bworld:verdigris_block"); assertEquals(t.state(1, 100, 0, "powered"), 1); assert(chain_ratio(15) > 1); assertEquals([t.speed(1, 100, 0), t.speed(1, 100, 2)], [t.s, t.s * chain_ratio(15)]); t.game.set_block(1, 101, 0, AIR_ID); assertEquals(t.speed(2, 100, 2), t.s); });