// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // how create's moving blocks are drawn: kinetic blocks turn by the speed the server puts in their "speed" client data, // as rotating instances, so a block turning steadily costs nothing per frame. they're still while their network is // overstressed. and what's in a basin import { mat4 } from "gl-matrix"; import type { ClientContext, Instance, Instances, ItemStack, ModelRef, TransformedInstance, Visual, VisualBlock, } from "bworld/client"; import type { Face } from "bworld/shared"; import { actual_speed, type Axis, axis_modifier, face_axis, FACINGS, kinetic_shape, type KineticParams, type KineticShape, OPPOSITE, } from "./kinetics/connections.ts"; import { BASIN_TRAVEL, head_offset, type PressShown } from "./processing/press.ts"; import { BASIN_TANK_CAPACITY, BASIN_TANKS, type BasinTank } from "./processing/basin.ts"; // a model reaching north turned to reach each way const facing_turns = (model: string): Record => ({ north: model, east: { model, y: 90 }, south: { model, y: 180 }, west: { model, y: 270 }, top: { model, x: 270 }, bottom: { model, x: 90 }, }); // 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 const HANDLE_TURNS = facing_turns("create:block_hand_crank_handle"); // a half shaft reaching south, from the middle of the block to its side, turned to reach where a block faces const HALF_SHAFT_TURNS = facing_turns("create:block_shaft_half"); const half_shaft = (facing: Face) => HALF_SHAFT_TURNS[OPPOSITE[facing]]; // how a model made along y is turned to an axis, the way the kinetic blocks' variants turn theirs const AXIS_TURNS: Record = { x: { x: 90, y: 90 }, y: {}, z: { x: 90, y: 180 } }; export function setup(ctx: ClientContext) { const shape_of = (block: VisualBlock): KineticShape => { const params = ctx.blocks.components(block.id)["create:kinetic"] as KineticParams; return kinetic_shape(params, (name) => block.states[name]); }; const plain = (block: VisualBlock, instances: Instances): Visual => ({ update: turning(block, instances, shape_of(block).axis), }); ctx.render.register_visual("create:shaft", plain, { replaces_model: true }); ctx.render.register_visual("create:cogwheel", plain, { replaces_model: true }); // the casing is the block's model, and the shaft through it turns, like the encased shafts' and chain drives' // ShaftRenderer const encased_shaft = (block: VisualBlock, instances: Instances): Visual => { const { axis } = shape_of(block); return { update: turning(block, instances, axis, { model: "create:block_shaft", ...AXIS_TURNS[axis] }) }; }; for ( const id of [ "andesite_encased_shaft", "brass_encased_shaft", "encased_chain_drive", "adjustable_chain_gearshift", ] ) { ctx.render.register_visual(`create:${id}`, encased_shaft); } // a half shaft out of each side with an axle, turning as axis_modifier says from the side its speed comes from, // which the server puts in its "source" client data, like GearboxVisual and SplitShaftVisual const halves = (block: VisualBlock, instances: Instances): Visual => { const shape = shape_of(block); const turns = shape.shafts.map((face) => turning( block, instances, face_axis(face), half_shaft(face), 0, () => axis_modifier(shape, block.get_client_data("source"), face), ) ); return { update: () => turns.forEach((turn) => turn()) }; }; for (const id of ["gearbox", "clutch", "gearshift"]) ctx.render.register_visual(`create:${id}`, halves); // the cog turns in its casing, offset like a large cog's when it's one, and a half shaft out of each end with an // axle, like EncasedCogVisual const encased_cog = (model: string, offset: number) => (block: VisualBlock, instances: Instances): Visual => { const { axis, shafts } = shape_of(block); const turns = [ turning(block, instances, axis, { model, ...AXIS_TURNS[axis] }, offset), ...shafts.map((face) => turning(block, instances, axis, half_shaft(face))), ]; return { update: () => turns.forEach((turn) => turn()) }; }; for (const casing of ["andesite", "brass"]) { ctx.render.register_visual( `create:${casing}_encased_cogwheel`, encased_cog("create:block_cogwheel_shaftless", 0), ); ctx.render.register_visual( `create:${casing}_encased_large_cogwheel`, encased_cog("create:block_large_cogwheel_shaftless", 11.25), ); } // the whole wheel turns, paddles and all, like WaterWheelVisual ctx.render.register_visual("create:water_wheel", plain, { replaces_model: true }); // its casing is its block's model, and the half shaft out of its front turns, like CreativeMotorRenderer ctx.render.register_visual("create:creative_motor", (block, instances) => { const shape = shape_of(block); return { update: turning(block, instances, shape.axis, half_shaft(shape.facing!)) }; }); // a large cog's teeth are half as far apart as a small one's, so on the blocks where small cogs aren't offset it's // offset by half of one of its own teeth. its shaft is apart from it and offset like any other, so it lines up with // the shafts next to it. like BracketedKineticBlockEntityVisual.LargeCogVisual ctx.render.register_visual("create:large_cogwheel", (block, instances) => { const shape = shape_of(block); const turns = AXIS_TURNS[shape.axis]; const cog = turning( block, instances, shape.axis, { model: "create:block_large_cogwheel_shaftless", ...turns }, 11.25, ); const shaft = turning(block, instances, shape.axis, { model: "create:block_cogwheel_shaft", ...turns }); return { update() { cog(); shaft(); }, }; }, { 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.axis, { 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("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("press"); if (JSON.stringify(press) === JSON.stringify(shown)) return; shown = press; since = undefined; }, on_frame(time) { since ??= time; const travel = shown?.basin ? BASIN_TRAVEL : 1; const now = shown ? head_offset(shown.steps + (time - since) * shown.rate) * travel : 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]); }, }; }); ctx.render.register_visual("create:basin", basin_visual); // 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) => { const shape = shape_of(block); const turn = turning(block, instances, shape.axis); const handle = instances.rotating(HANDLE_TURNS[shape.facing!]); // in degrees, and degrees a tick let angle = 0; let velocity = 0; let last: number | undefined; return { update: turn, on_frame(time) { const ticks = last === undefined ? 0 : time - last; last = time; // a quarter of the way to the shaft's speed each tick velocity += (actual_speed(block) * 0.3 - velocity) * Math.min(1, ticks / 4); angle = (angle + velocity * ticks) % 360; handle.set_rotation(shape.axis, 0, angle); }, }; }, { replaces_model: true }); } // a model turning around axis at the block's speed times modifier, the block's own model when not given. returns what // turns it again when the speed changes. on every other block it's offset by half a small cog's tooth, so cogs mesh // with the ones beside them, and on the rest by offset, like KineticBlockEntityVisual.rotationOffset function turning( block: VisualBlock, instances: Instances, axis: Axis, model?: ModelRef, offset = 0, modifier = () => 1, ) { const instance = instances.rotating(model); const angle = should_offset(axis, block) ? 22.5 : offset; const turn = () => instance.set_rotation(axis, actual_speed(block) * modifier(), angle); turn(); return turn; } // whether the coordinates across the axis add up to an even number, like KineticBlockEntityVisual.shouldOffset function should_offset(axis: Axis, { x, y, z }: VisualBlock) { const across = (axis === "x" ? 0 : x) + (axis === "y" ? 0 : y) + (axis === "z" ? 0 : z); return across % 2 === 0; } // a basin's items are a copy more for every this many const BASIN_ITEMS_A_COPY = 8; // what's in a basin, by its "tanks", "items" and "outputs" client data (see register_basin in server.ts), like // BasinRenderer. its fluids fill the 12 pixels inside its walls side by side, as wide as their share of it, and as high // as all of them make it, filling faster at first. its items lie in a ring over the fluid, tipped over and turned toward the middle, // one in the middle when it's alone, and bob up and down while there's fluid function basin_visual(block: VisualBlock, instances: Instances): Visual { let shown: Instance[] = []; // each copy of an item, placed in the basin but for how high it bobs, and where in its bobbing it is let floating: { instance: TransformedInstance; placed: mat4; phase: number }[] = []; const update = () => { for (const instance of shown) instance.delete(); shown = []; floating = []; const tanks = block.get_client_data("tanks") ?? []; const total = tanks.reduce((sum, tank) => sum + tank.amount, 0); const full = 1 - (1 - Math.min(1, total / (BASIN_TANKS * BASIN_TANK_CAPACITY))) ** 2; const top = total > 0 ? 2 + 12 * full : 0; let x = 2; for (const tank of tanks) { const width = 12 * tank.amount / total; const fluid = instances.fluid(tank.fluid); fluid.set_box([x, 2, 2], [x + width, top, 14]); shown.push(fluid); x += width; } // what's going in and what recipes made, together const items = [ ...block.get_client_data("items") ?? [], ...block.get_client_data("outputs") ?? [], ]; const level = Math.min(0.6, Math.max(0.125, top / 16 - 0.3)); const radius = items.length === 1 ? 0 : 0.125; const random = seeded(block.x * 961 + block.y * 31 + block.z); items.forEach((stack, i) => { const angle = 360 / items.length * (items.length - i) * Math.PI / 180; for (let copy = 0; copy <= Math.floor(stack.count / BASIN_ITEMS_A_COPY); copy++) { const placed = mat4.fromTranslation(mat4.create(), [ radius * Math.cos(angle), level, -radius * Math.sin(angle), ]); mat4.rotateY(placed, placed, angle + 35 * Math.PI / 180); mat4.rotateX(placed, placed, 65 * Math.PI / 180); const jitter = () => (random() - 0.5) / 8; mat4.translate(placed, placed, [jitter() - 0.5, jitter() - 0.5, jitter() - 0.5]); const instance = instances.item(stack); shown.push(instance); floating.push({ instance, placed, phase: angle * 180 / Math.PI }); place(instance, placed, 0); } }); if (top === 0) floating = []; }; update(); return { update, on_frame(time) { for (const { instance, placed, phase } of floating) { place(instance, placed, (Math.sin(time / 12 + phase) + 1.5) / 32); } }, }; } // an item copy in a basin, bob higher than where it was placed function place(instance: TransformedInstance, placed: mat4, bob: number) { const matrix = mat4.fromTranslation(mat4.create(), [0.5, 0.2 + bob, 0.5]); instance.set_transform(mat4.multiply(matrix, matrix, placed)); } // the same numbers from 0 to 1 every time for the same seed, so a basin's items lie the same way however often they're // drawn again function seeded(seed: number) { let state = seed >>> 0; return () => { state = (Math.imul(state, 1664525) + 1013904223) >>> 0; return state / 2 ** 32; }; }