From b8cccb86ffab630b8075d08b7ec8bccad705f235 Mon Sep 17 00:00:00 2001 From: Paula Date: Wed, 28 Oct 2026 22:10:48 -0300 Subject: [PATCH] Some more kinetic blocks --- CREATE.md | 25 ++- client/game_mode.ts | 1 + common/mod_api/server.ts | 4 +- common/mod_api/shared.ts | 3 + docs/modding/blocks/components.md | 7 +- docs/modding/blocks/states.md | 9 + docs/modding/blocks/verdigris.md | 5 +- mods/bworld/scripts/verdigris/server.ts | 10 +- mods/bworld/scripts/verdigris/signals.ts | 2 + .../blocks/adjustable_chain_gearshift.json | 14 +- .../blocks/andesite_encased_cogwheel.json | 7 +- .../andesite_encased_large_cogwheel.json | 7 +- .../create/blocks/andesite_encased_shaft.json | 7 +- .../create/blocks/brass_encased_cogwheel.json | 7 +- .../blocks/brass_encased_large_cogwheel.json | 7 +- mods/create/blocks/brass_encased_shaft.json | 7 +- mods/create/blocks/clutch.json | 6 +- mods/create/blocks/cogwheel.json | 6 +- mods/create/blocks/encased_chain_drive.json | 7 +- mods/create/blocks/gearbox.json | 8 +- mods/create/blocks/gearshift.json | 6 +- mods/create/blocks/large_cogwheel.json | 6 +- mods/create/blocks/shaft.json | 6 +- mods/create/items/vertical_gearbox.json | 3 +- mods/create/loot_tables/blocks/gearbox.json | 21 +++ mods/create/scripts/client.ts | 89 ++++++++-- mods/create/scripts/kinetics/chains.ts | 44 +++++ mods/create/scripts/kinetics/connections.ts | 108 +++++++++++- mods/create/scripts/kinetics/spreading.ts | 5 +- mods/create/scripts/server.ts | 52 +++++- mods/create/scripts/shared.ts | 75 ++++++-- server/game/game_server.ts | 1 + server/game/mod_runtime.ts | 15 +- tests/create/transmission_test.ts | 161 ++++++++++++++++++ tests/helpers.ts | 2 +- 35 files changed, 670 insertions(+), 73 deletions(-) create mode 100644 mods/create/loot_tables/blocks/gearbox.json create mode 100644 mods/create/scripts/kinetics/chains.ts create mode 100644 tests/create/transmission_test.ts diff --git a/CREATE.md b/CREATE.md index 1b1be18..3d6a110 100644 --- a/CREATE.md +++ b/CREATE.md @@ -5,10 +5,10 @@ redstone, contraptions, trains, equipment, decoration and schematics. It aims to possible, and to stay fast on both the server and the client when a world has thousands of moving parts. Built so far: the importer and Create's imported blocks and items, R1, R2, R3 (`transformed`, `rotating`, `fluid` and -items), R4, R5, R8, S0, S4, S7, S12, stonecutting from S9, the start of P1's kinetics (shafts and cogwheels turned by a -hand crank, creative motor or water wheel, with stress and overstress), Create's pressing recipes, and the mechanical -press pressing items on the ground. Facts about Create come from reading `ports/create1.21.1` and `ports/create1.19.3`, -and facts about bworld from its source. +items), R4, R5, R8, S0, S4, S7, S12, stonecutting from S9, the start of P1's kinetics (shafts, cogwheels, gearboxes, +encased shafts and cogs, clutches, gearshifts and chain drives turned by a hand crank, creative motor or water wheel, +with stress and overstress), Create's pressing recipes, and the mechanical press pressing items on the ground. Facts +about Create come from reading `ports/create1.21.1` and `ports/create1.19.3`, and facts about bworld from its source. ## Sources @@ -529,6 +529,23 @@ says. It scores again a tick after a neighbour changes, and every second for flo it every 3 seconds), so a wheel ticks once a second. It can't be placed beside another wheel unless they share an axle, like `canSurvive`. The large water wheel isn't built, and neither is changing a wheel's planks. +**Transmission as built** (`connections.ts`, `chains.ts`). All of it is `create:kinetic` params, so the rules are data +and one function, `speed_ratio`, which takes the table's gearbox and chain drive rows now. `gearbox` puts an axle on the +4 sides around its axis, and `split` (`clutch` or `gearshift`) with `powered` names a 1 bit state. Both turn their axles +by `axis_modifier`, from the side their speed comes from (`getAxisModifier`), which the server keeps in their `source` +client data for their visuals' half shafts. A gearbox reverses what goes straight through it, like Create's. The clutch +and gearshift are powered by verdigris through `bworld:verdigris_input`, and any change to a kinetic block's states that +changes how it turns takes it out of its network and puts it back, like `areStatesKineticallyEquivalent` with +`detachKinetics` (straight away rather than a tick later). The vertical gearbox is an item that `places` the gearbox, +whose placing rule stands it on its side when `placement.item` is the vertical gearbox, and a loot table gives back the +item it was. Encased shafts are the shaft's rule with a casing model. Encased cogs are cogs with `shaft_states`, the 1 +bit states for an axle out of each end. They have no item, like Create's: `create:encasable` on shafts and cogs turns +one right clicked with a casing into the encased one, keeping its axis, with an axle toward each axle beside it, and +both break into what was encased. Turning one back with a wrench waits for the wrench (P9). `chain` names a chain +drive's `part` and `axis_along_first` states, which `chain_update` keeps up to date on placing and on neighbour changes, +like `updateShape`. The adjustable chain gearshift also names a 4 bit `power` state, which the verdigris input fills +with how much power goes into it, for `chain_ratio`. The metal girder encased shaft waits for girders (P10). + **Chunk borders.** Tile data keeps speeds across unloading. bworld keeps every tile in memory and reloads a chunk when a block in it is read, so the walk sees a whole network, and Create's separate count of unloaded members' stress (`KineticNetwork`'s unloaded stress) isn't needed. diff --git a/client/game_mode.ts b/client/game_mode.ts index 33a25a1..50430c9 100644 --- a/client/game_mode.ts +++ b/client/game_mode.ts @@ -115,6 +115,7 @@ export class MultiPlayerGameMode { face: hit.face, hit: hit.hit, sneaking, + item: this.#held_slot().type_id!, }); const place_value = ruled_value !== undefined ? waterlog_placement( diff --git a/common/mod_api/server.ts b/common/mod_api/server.ts index d76927d..515431e 100644 --- a/common/mod_api/server.ts +++ b/common/mod_api/server.ts @@ -263,7 +263,9 @@ export interface ServerAfterEvents { export interface ServerWorld { // generates the chunk if it never was, which takes a while, see is_loaded get_block(x: number, y: number, z: number): Id | undefined; - set_block(x: number, y: number, z: number, id: Id): boolean; // runs on_break / on_create, synced to everyone + // runs on_break / on_create, synced to everyone. states are the ones it's placed with, the rest keep their defaults, + // like minecraft's setBlock with a block state: a shaft turned into an encased one keeps its axis + set_block(x: number, y: number, z: number, id: Id, states?: Record): boolean; // like breaking it by hand without a tool: on_break, then its drops, which it returns (copies). undefined when there // was nothing to break. see BreakOptions for machines breaking it break_block(x: number, y: number, z: number, options?: BreakOptions): ItemStack[] | undefined; diff --git a/common/mod_api/shared.ts b/common/mod_api/shared.ts index 423a068..215a27e 100644 --- a/common/mod_api/shared.ts +++ b/common/mod_api/shared.ts @@ -154,6 +154,9 @@ export interface SharedBlock extends Position { // of the six they look most toward, up and down too, like getNearestLookingDirection. sneaking is whether they hold the // sneak key, for blocks placed another way round when they do export interface BlockPlacement extends Position { + // the item placing it, like minecraft's getItemInHand: usually the block's own, or another that places it, like a + // vertical gearbox placing a gearbox on its side + readonly item: Id; readonly face: Face; readonly hit: readonly [number, number, number]; readonly facing: "north" | "east" | "south" | "west"; diff --git a/docs/modding/blocks/components.md b/docs/modding/blocks/components.md index fa30175..888271f 100644 --- a/docs/modding/blocks/components.md +++ b/docs/modding/blocks/components.md @@ -169,9 +169,10 @@ a slab's sides and plants aren't. ### Placing `state_for_placement` picks the states a block is placed with, like Minecraft's `getStateForPlacement`. `placement` is -`{ x, y, z, face, hit, facing, looking, sneaking }`: where it's going, the face of the block that was clicked, where on -that block the click was (from 0 to 1 on each axis), the way the player looks along the ground, the face of the six they -look most toward, `"top"` or `"bottom"` too, like `getNearestLookingDirection`, and whether they're +`{ x, y, z, item, face, hit, facing, looking, sneaking }`: where it's going, the item placing it (the block's own, or +another whose `places` is this block, like Minecraft's `getItemInHand`), the face of the block that was clicked, where +on that block the click was (from 0 to 1 on each axis), the way the player looks along the ground, the face of the six +they look most toward, `"top"` or `"bottom"` too, like `getNearestLookingDirection`, and whether they're [sneaking](../entities/players.md#sneaking), for blocks that sneaking places the other way round. It returns states by name, and the rest keep their defaults, except that a 2 bit `facing` already faces the player. Returning `false` means it can't go there at all, like a large cogwheel right beside another cogwheel, and nothing is placed, like diff --git a/docs/modding/blocks/states.md b/docs/modding/blocks/states.md index 86f3f71..8780e1e 100644 --- a/docs/modding/blocks/states.md +++ b/docs/modding/blocks/states.md @@ -34,6 +34,15 @@ everyone who can see the block. Unlike `set_block`, it doesn't run `on_break` or tile data. It runs the block's [`on_state_change`](components.md#changes) instead, and tells the blocks beside it, unless it's given `{ update_neighbors: false }`. +`set_block` puts a block there with its states at their defaults, or with the ones it's given, like Minecraft's +`setBlock` with a block state. Turning one block into another that keeps its direction looks like this, and `on_create` +already sees the new block turned the right way: + +```ts +const axis = ctx.world.get_state(x, y, z, "axis")!; +ctx.world.set_block(x, y, z, "create:andesite_encased_shaft", { axis }); +``` + ## Variants `variants` gives a block a different `model`, `textures` or rotation depending on its states. `blocks/wheat.json` shows diff --git a/docs/modding/blocks/verdigris.md b/docs/modding/blocks/verdigris.md index 1930938..3c065c4 100644 --- a/docs/modding/blocks/verdigris.md +++ b/docs/modding/blocks/verdigris.md @@ -82,12 +82,15 @@ The torch gives power out of every side but the block it's on, and strongly into | Param | Default | Meaning | | ---------- | ---------- | ------------------------------------------------------------- | | `state` | | A 1 bit state it keeps whether it's powered in: 1 while it is | +| `power` | | A 4 bit state it keeps how much power comes in in, 0 to 15 | | `facing` | | Which way it points, see [Sides](#sides) | | `in` | every side | The sides it takes power from | | `connects` | `in` | The sides wire joins | The component works out its input when the block is placed and whenever a block beside it changes, and sets the state, -which does nothing when it's already right. It's on or off: a state for how much power comes in isn't supported yet. +which does nothing when it's already right. A block that does more the more power it gets, like Create's adjustable +chain gearshift, also names a 4 bit `power` state, which gets the most power coming into any of its sides. It's set +before `state`, so `on_state_change` sees both up to date when `state` changes. A trapdoor lists it with a `powered` state: diff --git a/mods/bworld/scripts/verdigris/server.ts b/mods/bworld/scripts/verdigris/server.ts index bcd98ab..1ce55f6 100644 --- a/mods/bworld/scripts/verdigris/server.ts +++ b/mods/bworld/scripts/verdigris/server.ts @@ -69,8 +69,9 @@ export function register_verdigris(ctx: ServerContext) { }, }); - // keeps whether the block is powered in its state, for the block's own component to react to in on_state_change. - // setting it to what it is does nothing, so most changes around it cost a few reads + // keeps whether the block is powered in its state, and how much power comes in in another if it says, for the + // block's own component to react to in on_state_change. setting it to what it is does nothing, so most changes + // around it cost a few reads ctx.components.register_block("bworld:verdigris_input", { on_create: (block, params) => set_input(ctx, world, block, params), on_neighbor_change: (block, params) => set_input(ctx, world, block, params), @@ -82,8 +83,9 @@ export function register_verdigris(ctx: ServerContext) { } function set_input(ctx: ServerContext, world: SignalReader, block: BlockRef, params: InputParams) { - const powered = input_power(world, block.x, block.y, block.z, params) > 0 ? 1 : 0; - ctx.world.set_state(block.x, block.y, block.z, params.state, powered); + const power = input_power(world, block.x, block.y, block.z, params); + if (params.power) ctx.world.set_state(block.x, block.y, block.z, params.power, power); + ctx.world.set_state(block.x, block.y, block.z, params.state, power > 0 ? 1 : 0); } // the wires on the next faces of the blocks a wire lies on, which it joins or could join around their edges. they diff --git a/mods/bworld/scripts/verdigris/signals.ts b/mods/bworld/scripts/verdigris/signals.ts index ef0b8ce..e0e91b1 100644 --- a/mods/bworld/scripts/verdigris/signals.ts +++ b/mods/bworld/scripts/verdigris/signals.ts @@ -29,6 +29,8 @@ export interface SourceParams { export interface InputParams { // a 1 bit state, 1 while it's powered state: string; + // a 4 bit state it keeps how much power comes in in, 0 to 15, for a block that does more the more it gets + power?: string; facing?: FacingParam; // the sides it takes power from, every side when not set in?: SideName[]; diff --git a/mods/create/blocks/adjustable_chain_gearshift.json b/mods/create/blocks/adjustable_chain_gearshift.json index 924f9c3..5449a20 100644 --- a/mods/create/blocks/adjustable_chain_gearshift.json +++ b/mods/create/blocks/adjustable_chain_gearshift.json @@ -191,12 +191,20 @@ "states": [ { "name": "axis", "bits": 2, "default": 1 }, { "name": "axis_along_first", "bits": 1, "default": 0 }, - { "name": "part", "bits": 2, "default": 0 }, - { "name": "powered", "bits": 1, "default": 0 } + { "name": "part", "bits": 2, "default": 2 }, + { "name": "powered", "bits": 1, "default": 0 }, + { "name": "power", "bits": 4, "default": 0 } ], "render_layer": "cutout", "mining": { "toughness": 1 }, "drops": "create:adjustable_chain_gearshift", - "item_model": "create:item_adjustable_chain_gearshift" + "item_model": "create:item_adjustable_chain_gearshift", + "components": { + "create:kinetic": { + "axis": "axis", + "chain": { "part": "part", "along_first": "axis_along_first", "power": "power" } + }, + "bworld:verdigris_input": { "state": "powered", "power": "power" } + } } } diff --git a/mods/create/blocks/andesite_encased_cogwheel.json b/mods/create/blocks/andesite_encased_cogwheel.json index 7886531..7588837 100644 --- a/mods/create/blocks/andesite_encased_cogwheel.json +++ b/mods/create/blocks/andesite_encased_cogwheel.json @@ -57,7 +57,10 @@ ], "render_layer": "cutout", "mining": { "toughness": 1 }, - "drops": "create:andesite_encased_cogwheel", - "item_model": "create:item_andesite_encased_cogwheel" + "drops": "create:cogwheel", + "item": false, + "components": { + "create:kinetic": { "axis": "axis", "cog": "small", "shaft_states": ["top_shaft", "bottom_shaft"] } + } } } diff --git a/mods/create/blocks/andesite_encased_large_cogwheel.json b/mods/create/blocks/andesite_encased_large_cogwheel.json index e1b60ec..e05adb4 100644 --- a/mods/create/blocks/andesite_encased_large_cogwheel.json +++ b/mods/create/blocks/andesite_encased_large_cogwheel.json @@ -57,7 +57,10 @@ ], "render_layer": "cutout", "mining": { "toughness": 1 }, - "drops": "create:andesite_encased_large_cogwheel", - "item_model": "create:item_andesite_encased_large_cogwheel" + "drops": "create:large_cogwheel", + "item": false, + "components": { + "create:kinetic": { "axis": "axis", "cog": "large", "shaft_states": ["top_shaft", "bottom_shaft"] } + } } } diff --git a/mods/create/blocks/andesite_encased_shaft.json b/mods/create/blocks/andesite_encased_shaft.json index 2afab07..7f80095 100644 --- a/mods/create/blocks/andesite_encased_shaft.json +++ b/mods/create/blocks/andesite_encased_shaft.json @@ -14,7 +14,10 @@ ], "render_layer": "cutout", "mining": { "toughness": 1 }, - "drops": "create:andesite_encased_shaft", - "item_model": "create:item_andesite_encased_shaft" + "drops": "create:shaft", + "item_model": "create:item_andesite_encased_shaft", + "components": { + "create:kinetic": { "axis": "axis" } + } } } diff --git a/mods/create/blocks/brass_encased_cogwheel.json b/mods/create/blocks/brass_encased_cogwheel.json index 71e3177..83f82b4 100644 --- a/mods/create/blocks/brass_encased_cogwheel.json +++ b/mods/create/blocks/brass_encased_cogwheel.json @@ -49,7 +49,10 @@ ], "render_layer": "cutout", "mining": { "toughness": 1 }, - "drops": "create:brass_encased_cogwheel", - "item_model": "create:item_brass_encased_cogwheel" + "drops": "create:cogwheel", + "item": false, + "components": { + "create:kinetic": { "axis": "axis", "cog": "small", "shaft_states": ["top_shaft", "bottom_shaft"] } + } } } diff --git a/mods/create/blocks/brass_encased_large_cogwheel.json b/mods/create/blocks/brass_encased_large_cogwheel.json index 56c060e..c87cbec 100644 --- a/mods/create/blocks/brass_encased_large_cogwheel.json +++ b/mods/create/blocks/brass_encased_large_cogwheel.json @@ -57,7 +57,10 @@ ], "render_layer": "cutout", "mining": { "toughness": 1 }, - "drops": "create:brass_encased_large_cogwheel", - "item_model": "create:item_brass_encased_large_cogwheel" + "drops": "create:large_cogwheel", + "item": false, + "components": { + "create:kinetic": { "axis": "axis", "cog": "large", "shaft_states": ["top_shaft", "bottom_shaft"] } + } } } diff --git a/mods/create/blocks/brass_encased_shaft.json b/mods/create/blocks/brass_encased_shaft.json index a7d96ea..bb86733 100644 --- a/mods/create/blocks/brass_encased_shaft.json +++ b/mods/create/blocks/brass_encased_shaft.json @@ -14,7 +14,10 @@ ], "render_layer": "cutout", "mining": { "toughness": 1 }, - "drops": "create:brass_encased_shaft", - "item_model": "create:item_brass_encased_shaft" + "drops": "create:shaft", + "item_model": "create:item_brass_encased_shaft", + "components": { + "create:kinetic": { "axis": "axis" } + } } } diff --git a/mods/create/blocks/clutch.json b/mods/create/blocks/clutch.json index d32ed89..6ce3cb8 100644 --- a/mods/create/blocks/clutch.json +++ b/mods/create/blocks/clutch.json @@ -19,6 +19,10 @@ "render_layer": "cutout", "mining": { "toughness": 1 }, "drops": "create:clutch", - "item_model": "create:item_clutch" + "item_model": "create:item_clutch", + "components": { + "create:kinetic": { "axis": "axis", "split": "clutch", "powered": "powered" }, + "bworld:verdigris_input": { "state": "powered" } + } } } diff --git a/mods/create/blocks/cogwheel.json b/mods/create/blocks/cogwheel.json index 1161ee8..2b9e763 100644 --- a/mods/create/blocks/cogwheel.json +++ b/mods/create/blocks/cogwheel.json @@ -17,7 +17,11 @@ "drops": "create:cogwheel", "item_model": "create:item_cogwheel", "components": { - "create:kinetic": { "axis": "axis", "cog": "small" } + "create:kinetic": { "axis": "axis", "cog": "small" }, + "create:encasable": { + "create:andesite_casing": "create:andesite_encased_cogwheel", + "create:brass_casing": "create:brass_encased_cogwheel" + } } } } diff --git a/mods/create/blocks/encased_chain_drive.json b/mods/create/blocks/encased_chain_drive.json index 7ecad86..683adc3 100644 --- a/mods/create/blocks/encased_chain_drive.json +++ b/mods/create/blocks/encased_chain_drive.json @@ -63,11 +63,14 @@ "states": [ { "name": "axis", "bits": 2, "default": 1 }, { "name": "axis_along_first", "bits": 1, "default": 0 }, - { "name": "part", "bits": 2, "default": 0 } + { "name": "part", "bits": 2, "default": 2 } ], "render_layer": "cutout", "mining": { "toughness": 1 }, "drops": "create:encased_chain_drive", - "item_model": "create:item_encased_chain_drive" + "item_model": "create:item_encased_chain_drive", + "components": { + "create:kinetic": { "axis": "axis", "chain": { "part": "part", "along_first": "axis_along_first" } } + } } } diff --git a/mods/create/blocks/gearbox.json b/mods/create/blocks/gearbox.json index a16bc6e..ccd237a 100644 --- a/mods/create/blocks/gearbox.json +++ b/mods/create/blocks/gearbox.json @@ -14,7 +14,11 @@ ], "render_layer": "cutout", "mining": { "toughness": 1 }, - "drops": "create:gearbox", - "item_model": "create:item_gearbox" + "loot_table": "create:blocks/gearbox", + "item_model": "create:item_gearbox", + "components": { + "create:kinetic": { "axis": "axis", "gearbox": true }, + "create:gearbox": { "side_item": "create:vertical_gearbox" } + } } } diff --git a/mods/create/blocks/gearshift.json b/mods/create/blocks/gearshift.json index 806cb08..7c2ec30 100644 --- a/mods/create/blocks/gearshift.json +++ b/mods/create/blocks/gearshift.json @@ -19,6 +19,10 @@ "render_layer": "cutout", "mining": { "toughness": 1 }, "drops": "create:gearshift", - "item_model": "create:item_gearshift" + "item_model": "create:item_gearshift", + "components": { + "create:kinetic": { "axis": "axis", "split": "gearshift", "powered": "powered" }, + "bworld:verdigris_input": { "state": "powered" } + } } } diff --git a/mods/create/blocks/large_cogwheel.json b/mods/create/blocks/large_cogwheel.json index a7b6a17..6e5ae34 100644 --- a/mods/create/blocks/large_cogwheel.json +++ b/mods/create/blocks/large_cogwheel.json @@ -17,7 +17,11 @@ "drops": "create:large_cogwheel", "item_model": "create:item_large_cogwheel", "components": { - "create:kinetic": { "axis": "axis", "cog": "large" } + "create:kinetic": { "axis": "axis", "cog": "large" }, + "create:encasable": { + "create:andesite_casing": "create:andesite_encased_large_cogwheel", + "create:brass_casing": "create:brass_encased_large_cogwheel" + } } } } diff --git a/mods/create/blocks/shaft.json b/mods/create/blocks/shaft.json index c1572c4..e96d210 100644 --- a/mods/create/blocks/shaft.json +++ b/mods/create/blocks/shaft.json @@ -17,7 +17,11 @@ "drops": "create:shaft", "item_model": "create:item_shaft", "components": { - "create:kinetic": { "axis": "axis" } + "create:kinetic": { "axis": "axis" }, + "create:encasable": { + "create:andesite_casing": "create:andesite_encased_shaft", + "create:brass_casing": "create:brass_encased_shaft" + } } } } diff --git a/mods/create/items/vertical_gearbox.json b/mods/create/items/vertical_gearbox.json index aad4668..8c36471 100644 --- a/mods/create/items/vertical_gearbox.json +++ b/mods/create/items/vertical_gearbox.json @@ -3,6 +3,7 @@ "item": { "id": "create:vertical_gearbox", "texture": "create:block_axis", - "model": "create:item_vertical_gearbox" + "model": "create:item_vertical_gearbox", + "places": "create:gearbox" } } diff --git a/mods/create/loot_tables/blocks/gearbox.json b/mods/create/loot_tables/blocks/gearbox.json new file mode 100644 index 0000000..ed41c5e --- /dev/null +++ b/mods/create/loot_tables/blocks/gearbox.json @@ -0,0 +1,21 @@ +{ + "format_version": 1, + "loot_table": { + "pools": [ + { + "entries": [ + { + "item": "create:gearbox", + "conditions": [{ "condition": "block_state", "states": { "axis": 1 } }] + }, + { + "item": "create:vertical_gearbox", + "conditions": [ + { "condition": "inverted", "term": { "condition": "block_state", "states": { "axis": 1 } } } + ] + } + ] + } + ] + } +} diff --git a/mods/create/scripts/client.ts b/mods/create/scripts/client.ts index 36ff480..47bb25c 100644 --- a/mods/create/scripts/client.ts +++ b/mods/create/scripts/client.ts @@ -9,6 +9,8 @@ import type { Face } from "bworld/shared"; import { actual_speed, type Axis, + axis_modifier, + face_axis, FACINGS, kinetic_shape, type KineticParams, @@ -43,17 +45,73 @@ export function setup(ctx: ClientContext) { }; const plain = (block: VisualBlock, instances: Instances): Visual => ({ - update: turning(block, instances, shape_of(block)), + 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, half_shaft(shape.facing!)) }; + 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 @@ -65,11 +123,11 @@ export function setup(ctx: ClientContext) { const cog = turning( block, instances, - shape, + shape.axis, { model: "create:block_large_cogwheel_shaftless", ...turns }, 11.25, ); - const shaft = turning(block, instances, shape, { model: "create:block_cogwheel_shaft", ...turns }); + const shaft = turning(block, instances, shape.axis, { model: "create:block_cogwheel_shaft", ...turns }); return { update() { cog(); @@ -82,7 +140,7 @@ export function setup(ctx: ClientContext) { // 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, { model: "create:block_shaft", ...AXIS_TURNS[shape.axis] }); + 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, @@ -113,7 +171,7 @@ export function setup(ctx: ClientContext) { // 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); + const turn = turning(block, instances, shape.axis); const handle = instances.rotating(HANDLE_TURNS[shape.facing!]); // in degrees, and degrees a tick let angle = 0; @@ -133,13 +191,20 @@ export function setup(ctx: ClientContext) { }, { replaces_model: true }); } -// a model turning around the block's axis at its speed, the block's own 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, shape: KineticShape, model?: ModelRef, offset = 0) { +// 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(shape.axis, block) ? 22.5 : offset; - const turn = () => instance.set_rotation(shape.axis, actual_speed(block), angle); + const angle = should_offset(axis, block) ? 22.5 : offset; + const turn = () => instance.set_rotation(axis, actual_speed(block) * modifier(), angle); turn(); return turn; } diff --git a/mods/create/scripts/kinetics/chains.ts b/mods/create/scripts/kinetics/chains.ts new file mode 100644 index 0000000..7c564ff --- /dev/null +++ b/mods/create/scripts/kinetics/chains.ts @@ -0,0 +1,44 @@ +// SPDX-FileCopyrightText: 2026 Paula Wisnievski +// SPDX-License-Identifier: AGPL-3.0-or-later + +// how chain drives join into lines: each keeps where it is in its line (start, middle, end, or none on its own) and +// which way the line goes, and changes them as chain drives beside it come and go, like ChainDriveBlock.updateShape. +// no ctx, so placing and neighbor rules both use it +import type { Face } from "bworld/shared"; +import { type Axis, chain_axis, type ChainPart, face_axis, positive } from "./connections.ts"; + +// a chain drive's states that matter for its line +export interface ChainState { + axis: Axis; + part: ChainPart; + // its line goes along the first of the axes that aren't its own, see chain_axis + along_first: boolean; +} + +// a chain drive's line once the block toward face is other, undefined when that isn't a chain drive. one on its own +// starts a line with a chain drive beside it across its axis, and one in a line only joins more along that line, and +// is the end of it again when the one past it goes +export function chain_update(state: ChainState, face: Face, other: ChainState | undefined): ChainState { + const { axis } = state; + const toward = face_axis(face); + if (axis === toward) return state; + const facing_along_first = axis === "x" ? toward === "y" : toward === "x"; + const ahead = positive(face); + if (!other) { + if (facing_along_first !== state.along_first || state.part === "none") return state; + if (state.part === "middle") return { ...state, part: ahead ? "end" : "start" }; + if ((state.part === "start") === ahead) return { ...state, part: "none" }; + return state; + } + if (other.axis === toward) return state; + if (other.part !== "none" && chain_axis(other.axis, other.along_first) !== toward) return state; + let { part, along_first } = state; + if (part === "none") { + part = ahead ? "start" : "end"; + along_first = facing_along_first; + } else if (chain_axis(axis, along_first) !== toward) { + return state; + } + if ((part === "start") !== ahead) part = "middle"; + return { axis, part, along_first }; +} diff --git a/mods/create/scripts/kinetics/connections.ts b/mods/create/scripts/kinetics/connections.ts index ed71948..8d6660d 100644 --- a/mods/create/scripts/kinetics/connections.ts +++ b/mods/create/scripts/kinetics/connections.ts @@ -24,8 +24,27 @@ export interface KineticParams { // a cogwheel: a small one turns the small ones beside it on the same axis, a large one turns small ones on the same // axis at its corners, and large ones on other axes across its edges, like create's ICogWheel cog?: "small" | "large"; + // the 1 bit states that say whether there's an axle at the positive end of its axis and at the negative end, like an + // encased cog's top_shaft and bottom_shaft. an axle at both when not given + shaft_states?: [string, string]; + // a gearbox: an axle on each of the 4 sides around its axis instead of along it, see axis_modifier + gearbox?: boolean; + // a clutch or a gearshift: while the 1 bit state powered names is 1, the half of its axle its speed doesn't come from + // is cut off from it, or turns the other way, see axis_modifier + split?: "clutch" | "gearshift"; + powered?: string; + // a chain drive, which turns the chain drives in a line with it across its axis, like ChainDriveBlock. part and + // along_first name its states for where it is in the line (CHAIN_PARTS) and which way the line goes (chain_axis). + // power names an adjustable chain gearshift's 4 bit state with the power going into it, which makes the rest of + // the line turn faster, see chain_ratio + chain?: { part: string; along_first: string; power?: string }; } +// a chain drive's part state's values, in the order the importer numbers them: the last of a line, one between two +// others, one on its own, and the first +export const CHAIN_PARTS = ["end", "middle", "none", "start"] as const; +export type ChainPart = typeof CHAIN_PARTS[number]; + // a 3 bit facing state's values, in the order the importer numbers java's facing export const FACINGS: readonly Face[] = ["north", "east", "south", "west", "top", "bottom"]; // a 2 bit axis state's values @@ -73,6 +92,12 @@ export interface KineticShape { // the way it faces, for a block with a facing facing?: Face; cog?: "small" | "large"; + // a gearbox, clutch or gearshift, whose axles turn depending on where its speed comes from, see axis_modifier + halves?: "gearbox" | "clutch" | "gearshift"; + powered?: boolean; + // a chain drive's line: the axis it goes along, where in it this one is, and how much slower this one turns than + // the rest, see chain_ratio + chain?: { axis: Axis; part: ChainPart; ratio: number }; } // a kinetic block where it is @@ -80,6 +105,8 @@ export interface Kinetic extends KineticShape { readonly x: number; readonly y: number; readonly z: number; + // the block next to it it gets its speed from. undefined for a still block, and for a generator turning it + readonly source?: Pos; } // a block's shape from its params and its states, read by name @@ -95,7 +122,76 @@ export function kinetic_shape(params: KineticParams, state: (name: string) => nu return { axis: FACE_AXIS[facing], shafts, facing }; } const axis = AXES[state(params.axis ?? "axis") ?? 1]; - return { axis, shafts: AXIS_FACES[axis], ...(params.cog && { cog: params.cog }) }; + const ends = params.shaft_states; + const shafts = params.gearbox + ? FACINGS.filter((face) => FACE_AXIS[face] !== axis) + : ends + ? AXIS_FACES[axis].filter((_, i) => state(ends[i]) === 1) + : AXIS_FACES[axis]; + const halves = params.gearbox ? "gearbox" : params.split; + const chain = params.chain; + return { + axis, + shafts, + ...(params.cog && { cog: params.cog }), + ...(halves && { halves }), + ...(params.powered && { powered: state(params.powered) === 1 }), + ...(chain && { + chain: { + axis: chain_axis(axis, state(chain.along_first) === 1), + part: CHAIN_PARTS[state(chain.part) ?? 2], + ratio: chain_ratio(chain.power ? state(chain.power) ?? 0 : 0), + }, + }), + }; +} + +// the axis a line of chain drives on axis goes along: the first of the other two, or the second, like +// ChainDriveBlock.getConnectionAxis +export function chain_axis(axis: Axis, along_first: boolean): Axis { + return along_first ? (axis === "x" ? "y" : "x") : (axis === "z" ? "y" : "z"); +} + +// how much faster an adjustable chain gearshift with power going into it turns the rest of its line than it turns +// itself, like ChainGearshiftBlockEntity.getModifierForSignal +export function chain_ratio(power: number): number { + return power === 0 ? 1 : 1 + (power + 1) / 16; +} + +// how much faster the axle at face turns than the block, like RotationPropagator.getAxisModifier. a gearbox's axle +// toward where its speed comes from turns with it and the one across from that the other way, and of the 4 others +// the ones on the same side of it as that one turn the other way. a powered clutch's axle away from its speed is cut +// off, and a powered gearshift's turns the other way. source is the side its speed comes from, undefined when it +// isn't turned +export function axis_modifier(shape: KineticShape, source: Face | undefined, face: Face): number { + if (!shape.halves || !source) return 1; + if (shape.halves === "gearbox") { + if (FACE_AXIS[face] === FACE_AXIS[source]) return face === source ? 1 : -1; + return positive(face) === positive(source) ? -1 : 1; + } + if (face === source || !shape.powered) return 1; + return shape.halves === "clutch" ? 0 : -1; +} + +// the side of a block its speed comes from, undefined when it isn't turned or it makes its own +export function source_face(block: Pick): Face | undefined { + return block.source && + face_toward([block.source[0] - block.x, block.source[1] - block.y, block.source[2] - block.z]); +} + +// whether face is at the positive end of its axis: east, top or south +export function positive(face: Face): boolean { + return AXIS_FACES[FACE_AXIS[face]][0] === face; +} + +// whether a chain drive turns the one beside it toward face: they're on the same line, and it's on the side of this +// one the line goes on, like ChainDriveBlock.areBlocksConnected +function chained(from: KineticShape, to: KineticShape, face: Face): boolean { + if (!from.chain || !to.chain || from.chain.axis !== to.chain.axis || FACE_AXIS[face] !== from.chain.axis) { + return false; + } + const part = from.chain.part; + return part === "middle" || part === (positive(face) ? "start" : "end"); } export function face_axis(face: Face): Axis { @@ -117,13 +213,17 @@ function face_toward([dx, dy, dz]: readonly number[]): Face | undefined { } // how much faster to turns than from when from turns it, 0 when from doesn't turn it, like -// RotationPropagator.getRotationSpeedModifier: 1 for axles that meet, and the other way round for cogs, at the ratio of -// their sizes +// RotationPropagator.getRotationSpeedModifier: 1 for axles that meet unless one's halves turn differently, the +// chain drives' ratio along a line of them, and the other way round for cogs, at the ratio of their sizes export function speed_ratio(from: Kinetic, to: Kinetic): number { const diff = [to.x - from.x, to.y - from.y, to.z - from.z]; const along = (axis: Axis) => diff[AXES.indexOf(axis)]; const face = face_toward(diff); - if (face && from.shafts.includes(face) && to.shafts.includes(OPPOSITE[face])) return 1; + if (face && from.shafts.includes(face) && to.shafts.includes(OPPOSITE[face])) { + const into = axis_modifier(to, source_face(to), OPPOSITE[face]); + return into === 0 ? 0 : axis_modifier(from, source_face(from), face) / into; + } + if (face && chained(from, to, face)) return from.chain!.ratio / to.chain!.ratio; // large cogs on different axes meet across an edge, off along both their axes. whether they turn the same way // around their own axes depends on which side of each other they're on diff --git a/mods/create/scripts/kinetics/spreading.ts b/mods/create/scripts/kinetics/spreading.ts index 117de2e..8557b2a 100644 --- a/mods/create/scripts/kinetics/spreading.ts +++ b/mods/create/scripts/kinetics/spreading.ts @@ -10,10 +10,7 @@ import { connected as connected_in, type Kinetic, neighbour_positions, type Pos, import { type StressNode, type StressWorld, update_stress } from "./stress.ts"; // a kinetic block as the world keeps it -export interface KineticNode extends StressNode { - // the block next to it it gets its speed from. undefined for a still block, and for a generator turning it - readonly source?: Pos; -} +export type KineticNode = StressNode; export interface KineticWorld extends StressWorld { // undefined when what's there isn't kinetic diff --git a/mods/create/scripts/server.ts b/mods/create/scripts/server.ts index b4c3057..f152e18 100644 --- a/mods/create/scripts/server.ts +++ b/mods/create/scripts/server.ts @@ -4,7 +4,19 @@ // create's blocks that do something: so far what turns (create:kinetic), the generators that turn it (the hand crank, // the creative motor and the water wheel), and the mechanical press import type { BlockRef, EntityRef, Id, ServerContext } from "bworld/server"; -import { actual_speed, along, kinetic_shape, type KineticParams, type Pos } from "./kinetics/connections.ts"; +import { + actual_speed, + along, + face_axis, + FACE_OFFSETS, + FACINGS, + kinetic_shape, + type KineticParams, + OPPOSITE, + type Pos, + positive, + source_face, +} from "./kinetics/connections.ts"; import { flow_score, motor_step } from "./kinetics/generators.ts"; import { handle_added, handle_removed, type KineticWorld, update_generated } from "./kinetics/spreading.ts"; import { @@ -81,6 +93,39 @@ export function setup(ctx: ServerContext) { on_break(block) { handle_removed(world, [block.x, block.y, block.z]); }, + // states that change how it turns things, like a clutch being powered or a chain drive joining a line, take it + // out and put it back, like Create's detachKinetics and attachKinetics on a change that isn't + // areStatesKineticallyEquivalent + on_state_change(block, params, before) { + const now = kinetic_shape(params, (name) => ctx.world.get_state(block.x, block.y, block.z, name)); + if (JSON.stringify(now) === JSON.stringify(kinetic_shape(params, (name) => before[name]))) return; + handle_removed(world, [block.x, block.y, block.z]); + handle_added(world, [block.x, block.y, block.z]); + }, + }); + + // a shaft or cog right clicked with a casing becomes the encased one, by the casing's id, turned the same way. an + // encased cog gets an axle out of its casing toward each axle beside it. the casing isn't used up. like + // EncasableBlock.tryEncase and handleEncasing + ctx.components.register_block>("create:encasable", { + on_interact(block, params, player) { + const into = params[player.held_item?.id ?? ""]; + const node = world.get(block.x, block.y, block.z); + if (!into || !node) return false; + const own = ctx.blocks.components(block.id)["create:kinetic"] as KineticParams; + const encased = ctx.blocks.components(into)["create:kinetic"] as KineticParams; + const states = { + [encased.axis ?? "axis"]: ctx.world.get_state(block.x, block.y, block.z, own.axis ?? "axis")!, + }; + for (const side of FACINGS) { + if (!encased.shaft_states || face_axis(side) !== node.axis) continue; + const [dx, dy, dz] = FACE_OFFSETS[side]; + const axle = world.get(block.x + dx, block.y + dy, block.z + dz)?.shafts.includes(OPPOSITE[side]); + states[encased.shaft_states[positive(side) ? 0 : 1]] = axle ? 1 : 0; + } + ctx.world.set_block(block.x, block.y, block.z, into, states); + return true; + }, }); // turns while it's being clicked, a little longer each click, and the other way while sneaking, like create's @@ -285,6 +330,11 @@ function kinetic_world(ctx: ServerContext, config: Config): KineticWorld { // machines stop ticking while they're still, like a press, and start again once they turn if (speed !== 0 && !block.get_client_data("speed")) block.set_ticking(true); block.set_client_data("speed", speed === 0 ? undefined : speed); + // the side its speed comes from decides how a gearbox's or clutch's axles turn, which its visual shows + const { gearbox, split } = params_of(block.id).kinetic!; + if (gearbox || split) { + block.set_client_data("source", source_face({ x: at[0], y: at[1], z: at[2], source })); + } const { source: _, network: __, ...rest }: KineticData = block.data ?? {}; block.data = { ...rest, ...(source && { source }), ...(network !== undefined && { network }) }; }, diff --git a/mods/create/scripts/shared.ts b/mods/create/scripts/shared.ts index 3f1441e..a5fe96b 100644 --- a/mods/create/scripts/shared.ts +++ b/mods/create/scripts/shared.ts @@ -2,11 +2,14 @@ // SPDX-License-Identifier: AGPL-3.0-or-later // how create's blocks are placed, on the server and on every client: kinetic blocks line up with the axles they're -// placed against -import type { BlockPlacement, Face, SharedContext } from "bworld/shared"; +// placed against, and chain drives join into lines +import type { BlockPlacement, Face, Id, SharedContext } from "bworld/shared"; +import { chain_update, type ChainState } from "./kinetics/chains.ts"; import { AXES, type Axis, + chain_axis, + CHAIN_PARTS, face_axis, FACE_OFFSETS, FACINGS, @@ -72,8 +75,27 @@ export function setup(ctx: SharedContext) { }; // a block with an axis lines up with the axle it's placed against, or sneaking, with the face it's clicked onto. - // otherwise it points the way the player looks, like RotatedPillarKineticBlock. a block with a facing faces as - // facing_for says. cogs are placed their own way, see cog_axis + // otherwise it points the way the player looks, like RotatedPillarKineticBlock + const axis_for = (placement: BlockPlacement): Axis => { + const side = axle_beside(placement.x, placement.y, placement.z); + if (side && !placement.sneaking) return face_axis(side); + return face_axis(side ? placement.face : placement.looking); + }; + + // a chain drive's line, undefined for a block that isn't one + const chain_of = (shape: KineticShape | undefined): ChainState | undefined => + shape?.chain && { + axis: shape.axis, + part: shape.chain.part, + along_first: shape.chain.axis === chain_axis(shape.axis, true), + }; + const chain_states = (params: KineticParams, chain: ChainState) => ({ + [params.chain!.part]: CHAIN_PARTS.indexOf(chain.part), + [params.chain!.along_first]: chain.along_first ? 1 : 0, + }); + + // a block with an axis is placed as axis_for says, and a chain drive joins the lines beside it. a block with a + // facing faces as facing_for says. cogs are placed their own way, see cog_axis ctx.components.register_block("create:kinetic", { state_for_placement(placement, params) { if (params.cog) { @@ -89,13 +111,46 @@ export function setup(ctx: SharedContext) { if (params.facing !== undefined) { return { [params.facing]: FACINGS.indexOf(facing_for(placement, params.shaft)) }; } - const side = axle_beside(placement.x, placement.y, placement.z); - const axis = side && !placement.sneaking + const axis = axis_for(placement); + const states = { [params.axis ?? "axis"]: AXES.indexOf(axis) }; + if (!params.chain) return states; + const { x, y, z } = placement; + const alone: ChainState = { axis, part: "none", along_first: false }; + const chain = FACINGS.reduce( + (chain, side) => chain_update(chain, side, chain_of(beside(x, y, z, side))), + alone, + ); + return { ...states, ...chain_states(params, chain) }; + }, + // a chain drive joins one placed beside it, and ends its line where one goes, like ChainDriveBlock.updateShape + on_neighbor_change(block, params, neighbor) { + if (!params.chain) return; + const own = chain_of(kinetic_at(block.x, block.y, block.z)); + const side = FACINGS.find((side) => { + const [dx, dy, dz] = FACE_OFFSETS[side]; + return block.x + dx === neighbor.x && block.y + dy === neighbor.y && block.z + dz === neighbor.z; + }); + if (!own || !side) return; + const chain = chain_update(own, side, chain_of(kinetic_at(neighbor.x, neighbor.y, neighbor.z))); + if (chain === own) return; + for (const [name, value] of Object.entries(chain_states(params, chain))) { + ctx.world.set_state(block.x, block.y, block.z, name, value); + } + }, + }); + + // a gearbox lies flat with its axles level, and side_item, the vertical gearbox, stands it on its side: across the + // axle level with it, or else across the way the player looks, like GearboxBlock and VerticalGearboxItem + ctx.components.register_block<{ side_item: Id }>("create:gearbox", { + state_for_placement(placement, params) { + if (placement.item !== params.side_item) return { axis: AXES.indexOf("y") }; + const side = axle_beside(placement.x, placement.y, placement.z, FACINGS.slice(0, 4)); + const across = side ? face_axis(side) - : side - ? face_axis(placement.face) - : face_axis(placement.looking); - return { [params.axis ?? "axis"]: AXES.indexOf(axis) }; + : placement.facing === "north" || placement.facing === "south" + ? "z" + : "x"; + return { axis: AXES.indexOf(across === "x" ? "z" : "x") }; }, }); diff --git a/server/game/game_server.ts b/server/game/game_server.ts index 4c700cf..17074c4 100644 --- a/server/game/game_server.ts +++ b/server/game/game_server.ts @@ -1396,6 +1396,7 @@ export class GameServer { face, hit: [hit[0], hit[1], hit[2]], sneaking: player.sneaking, + item: held_slot.type_id!, }); // its rules say it can't go there if (ruled_value === undefined) { diff --git a/server/game/mod_runtime.ts b/server/game/mod_runtime.ts index 692d024..9dbbcfe 100644 --- a/server/game/mod_runtime.ts +++ b/server/game/mod_runtime.ts @@ -735,9 +735,18 @@ export class ModRuntime { }, world: { get_block: (x, y, z) => game().world.get_block_id(x, y, z), - set_block: (x, y, z, id) => { - if (id !== AIR_ID && !EverythingRegistry.get("blocks", id)) throw new Error(`unknown block ${id}`); - game().set_block(x, y, z, id); + set_block: (x, y, z, id, states) => { + const info = id === AIR_ID ? undefined : EverythingRegistry.get("blocks", id); + if (id !== AIR_ID && !info) throw new Error(`unknown block ${id}`); + let value = info && game().world.default_values[game().world.block_ids[id]]; + for (const [name, state] of Object.entries(states ?? {})) { + const bits = info?.states?.find((s) => s.name === name)?.bits; + if (bits === undefined || !Number.isInteger(state) || state < 0 || state >= 2 ** bits) { + throw new Error(`${id} has no state ${name} that fits ${state}`); + } + value = set_state_value(value!, info!, name, state)!; + } + game().set_block(x, y, z, id, undefined, states && value !== undefined ? value >>> 16 : undefined); return true; }, break_block: (x, y, z, options = {}) => { diff --git a/tests/create/transmission_test.ts b/tests/create/transmission_test.ts new file mode 100644 index 0000000..202d939 --- /dev/null +++ b/tests/create/transmission_test.ts @@ -0,0 +1,161 @@ +// 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); +}); diff --git a/tests/helpers.ts b/tests/helpers.ts index de2cea6..66bf568 100644 --- a/tests/helpers.ts +++ b/tests/helpers.ts @@ -269,7 +269,7 @@ export function client_of(game: GameServer, area: Area) { return { // what game_mode does when this client places held against face of x y z, at hit place(id: string, x: number, y: number, z: number, yaw: number, face: Face, hit: [number, number, number]) { - const value = rules.placement_value(info(id)!, yaw, 0, { x, y, z, face, hit, sneaking: false }); + const value = rules.placement_value(info(id)!, yaw, 0, { x, y, z, face, hit, sneaking: false, item: id }); // its rules say it can't go there if (value === undefined) return; copy.set(`${x} ${y} ${z}`, [id, value >>> 16]);