Cog wheels

This commit is contained in:
2026-10-15 12:01:46 -03:00
parent a0596ed0ef
commit 74c315bdfc
14 changed files with 331 additions and 88 deletions
+115 -2
View File
@@ -201,9 +201,100 @@ Deno.test("a shaft lines up with the axle it's placed against, and otherwise poi
assertEquals(t.state(2, 100, 0, "facing"), FACING.east);
});
Deno.test("small cogs side by side on the same axis turn each other the other way", async () => {
const t = await setup();
t.place(0, 100, 0, "create:hand_crank", "facing", FACING.west);
// a square of them across the crank's axle, and one on another axis beside it
for (const [y, z] of [[100, 0], [101, 0], [100, 1], [101, 1]]) t.place(1, y, z, "create:cogwheel", "axis", AXIS.x);
t.place(1, 100, -1, "create:cogwheel", "axis", AXIS.z);
t.use(0, 100, 0);
const c = t.crank;
assertEquals([t.speed(1, 100, 0), t.speed(1, 101, 0), t.speed(1, 100, 1), t.speed(1, 101, 1)], [-c, c, c, -c]);
assertEquals(t.speed(1, 100, -1), 0);
// the square is a loop that agrees with itself, so it stops cleanly too
t.ticks(40);
assertEquals([t.speed(1, 100, 0), t.speed(1, 101, 0), t.speed(1, 100, 1), t.speed(1, 101, 1)], [0, 0, 0, 0]);
assertEquals(t.game.world.get_block_id(1, 101, 1), "create:cogwheel");
});
Deno.test("a large cog turns a small one at its corner twice as fast, and a small one turns it half as fast", async () => {
const t = await setup();
t.place(0, 100, 0, "create:hand_crank", "facing", FACING.west);
t.place(1, 100, 0, "create:large_cogwheel", "axis", AXIS.x);
t.place(1, 101, 1, "create:cogwheel", "axis", AXIS.x);
// at another corner, but on another axis
t.place(1, 101, -1, "create:cogwheel", "axis", AXIS.y);
t.use(0, 100, 0);
assertEquals([t.speed(1, 100, 0), t.speed(1, 101, 1), t.speed(1, 101, -1)], [-t.crank, 2 * t.crank, 0]);
const u = await setup();
u.place(0, 100, 0, "create:hand_crank", "facing", FACING.west);
u.place(1, 100, 0, "create:cogwheel", "axis", AXIS.x);
u.place(1, 101, 1, "create:large_cogwheel", "axis", AXIS.x);
u.use(0, 100, 0);
assertEquals([u.speed(1, 100, 0), u.speed(1, 101, 1)], [-u.crank, u.crank / 2]);
});
Deno.test("large cogs on different axes turn each other across an edge", async () => {
const t = await setup();
t.place(0, 100, 0, "create:hand_crank", "facing", FACING.west);
t.place(1, 100, 0, "create:large_cogwheel", "axis", AXIS.x);
t.place(2, 101, 0, "create:large_cogwheel", "axis", AXIS.y);
// on the same axis they don't
t.place(1, 101, 1, "create:large_cogwheel", "axis", AXIS.x);
t.use(0, 100, 0);
assertEquals(Math.abs(t.speed(2, 101, 0)), t.crank);
assertEquals(t.speed(1, 101, 1), 0);
});
Deno.test("cogs that would turn something faster than create's max_speed break", async () => {
const t = await setup({ max_speed: 48 });
t.place(0, 100, 0, "create:hand_crank", "facing", FACING.west);
t.place(1, 100, 0, "create:large_cogwheel", "axis", AXIS.x);
t.place(1, 101, 1, "create:cogwheel", "axis", AXIS.x);
t.use(0, 100, 0);
const blocks = [[0, 100, 0], [1, 100, 0], [1, 101, 1]] as const;
assert(blocks.some(([x, y, z]) => t.game.world.get_block_id(x, y, z) === AIR_ID));
for (const [x, y, z] of blocks) assert(Math.abs(t.speed(x, y, z)) <= 48);
});
Deno.test("a cog lines up with the cog it's placed against, and can't go where it would cut into another", async () => {
const t = await setup();
t.send(1, { type: "chat", text: "/give create:cogwheel 64" });
t.send(1, { type: "chat", text: "/give create:large_cogwheel 64" });
t.send(1, { type: "select_slot", slot: 0 });
const held = () => t.game.players()[0].held_item?.amount;
// on the floor it lies flat, and one placed against its side lies flat with it
t.use(0, 99, 0, "top");
assertEquals(t.state(0, 100, 0, "axis"), AXIS.y);
t.use(0, 100, 0, "west");
assertEquals(t.state(-1, 100, 0, "axis"), AXIS.y);
// sneaking, it takes the axis of the face it's clicked onto
t.move(true);
t.use(0, 100, 0, "north");
assertEquals(t.state(0, 100, -1, "axis"), AXIS.z);
t.move();
// a large cog's teeth would cut into the small one beside it, but at its corner they mesh
t.send(1, { type: "select_slot", slot: 1 });
t.use(1, 99, 0, "top");
assertEquals(t.game.world.get_block_id(1, 100, 0), AIR_ID);
assertEquals(held(), 64);
t.use(1, 99, 1, "top");
assertEquals(t.game.world.get_block_id(1, 100, 1), "create:large_cogwheel");
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>) {
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({
@@ -219,7 +310,7 @@ async function visual_for(id: string, states: Record<string, number>, data: Reco
turns.set(model === undefined ? "own" : JSON.stringify(model), [axis, rpm, offset]),
delete() {},
});
const block = { id, x: 0, y: 100, z: 0, states, get_client_data: (field: string) => data[field] as never };
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 };
}
@@ -258,3 +349,25 @@ Deno.test("a crank's handle catches up with the speed it turns at, and slows to
for (let i = 0; i < 100; i++) step();
assert(Math.abs(step()) < 0.01);
});
Deno.test("small cogs beside each other are turned half a tooth apart, so they mesh", async () => {
const offset = async (x: number) =>
(await visual_for("create:cogwheel", { axis: AXIS.y }, {}, [x, 100, 0])).turns.get("own")![2];
assertEquals(Math.abs(await offset(0) - await offset(1)), 22.5);
});
Deno.test("a large cog is drawn as its cog and its shaft, both turning at its speed", async () => {
const { visual, turns, data } = await visual_for("create:large_cogwheel", { axis: AXIS.z }, { speed: 8 }, [
1,
0,
0,
]);
const [cog, shaft] = ["shaftless", 'shaft"'].map((part) => [...turns].find(([model]) => model.includes(part))![1]);
assertEquals([cog.slice(0, 2), shaft.slice(0, 2)], [["z", 8], ["z", 8]]);
// where a small cog wouldn't be offset, it's offset by half its own tooth, and its shaft lines up with other shafts
assertEquals([cog[2], shaft[2]], [11.25, 0]);
data.speed = -8;
visual.update!();
assertEquals(turns.size, 2);
assert([...turns.values()].every(([, rpm]) => rpm === -8));
});