Cog wheels
This commit is contained in:
@@ -5,8 +5,9 @@ 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` and `rotating`), R4, R5,
|
||||
R8, S0, S7, S12, stonecutting from S9, and the start of P1's kinetics: shafts turned by a hand crank. Facts about Create
|
||||
come from reading `ports/create1.21.1` and `ports/create1.19.3`, and facts about bworld from its source.
|
||||
R8, S0, S7, S12, stonecutting from S9, and the start of P1's kinetics: shafts and cogwheels turned by a hand crank.
|
||||
Facts about Create come from reading `ports/create1.21.1` and `ports/create1.19.3`, and facts about bworld from its
|
||||
source.
|
||||
|
||||
## Sources
|
||||
|
||||
@@ -466,19 +467,23 @@ visual makes a `rotating` instance with the axis, rpm and an offset that keeps c
|
||||
when the block's coordinates on the two axes that aren't its axis add up to an even number, otherwise 11.25° for large
|
||||
cogs and 0 for the rest. A speed change updates one instance, and nothing else happens per frame.
|
||||
|
||||
**As built** (`scripts/kinetics/`, the shaft and the hand crank). `create:kinetic` takes `axis` (the state with x, y or
|
||||
z) or `facing` (the state with the 6 faces) and `shaft` (`both`, `front` or `back`), and its shared rules place blocks
|
||||
like `RotatedPillarKineticBlock` and `DirectionalKineticBlock`, lining up with an axle beside them. Connections are only
|
||||
axle to axle so far. Spreading is `RotationPropagator`'s, with queues: a block's speed is its `speed` client data, and
|
||||
the block it gets it from and the generator it comes from (`source` and `network`) are tile data. Two things differ:
|
||||
on_break runs while the block is still there, so the walk after a removal treats its place as empty, and generators that
|
||||
stop with what turned them start again right after the walk instead of a tick later (`reActivateSource`). Create's
|
||||
flicker score and its periodic `validateKinetics` aren't ported, since shafts don't tick. `create:hand_crank` turns
|
||||
while it's clicked, for 10 ticks after each click, the other way while sneaking, and costs hunger by the config's
|
||||
`crank_hunger`. Its visual turns the half shaft as a `rotating` instance and eases the handle in `on_frame`, like
|
||||
`HandCrankVisual`. The crank doesn't pop off when its block is taken away yet. It needed three engine additions:
|
||||
`looking` in `state_for_placement` (`getNearestLookingDirection`), `ctx.blocks` in shared scripts, and
|
||||
`ctx.world.get_block_ref` for changing another block's data (`getBlockEntity`).
|
||||
**As built** (`scripts/kinetics/`: the shaft, both cogwheels and the hand crank). `create:kinetic` takes `axis` (the
|
||||
state with x, y or z) or `facing` (the state with the 6 faces), `shaft` (`both`, `front` or `back`) and `cog` (`small`
|
||||
or `large`), and its shared rules place blocks like `RotatedPillarKineticBlock`, `DirectionalKineticBlock` and
|
||||
`CogWheelBlock`, lining up with an axle or cog beside them. A cog that would cut into another isn't placed, like
|
||||
`isValidCogwheelPosition`, by `state_for_placement` returning `false`. Connections are the table's first four rows.
|
||||
Spreading is `RotationPropagator`'s, with queues: a block's speed is its `speed` client data, and the block it gets it
|
||||
from and the generator it comes from (`source` and `network`) are tile data. Two things differ: on_break runs while the
|
||||
block is still there, so the walk after a removal treats its place as empty, and generators that stop with what turned
|
||||
them start again right after the walk instead of a tick later (`reActivateSource`). Create's flicker score and its
|
||||
periodic `validateKinetics` aren't ported, since shafts don't tick. `create:hand_crank` turns while it's clicked, for 10
|
||||
ticks after each click, the other way while sneaking, and costs hunger by the config's `crank_hunger`. Its visual turns
|
||||
the half shaft as a `rotating` instance and eases the handle in `on_frame`, like `HandCrankVisual`. A large cog is drawn
|
||||
as its shaftless model and a separate shaft, offset apart, like `LargeCogVisual`. The crank doesn't pop off when its
|
||||
block is taken away yet, and cogs have no placement helper (`CogwheelBlockItem`'s ghost offsets). It needed four engine
|
||||
additions: `looking` in `state_for_placement` (`getNearestLookingDirection`), `false` from it to not place
|
||||
(`getStateForPlacement` returning null), `ctx.blocks` in shared scripts, and `ctx.world.get_block_ref` for changing
|
||||
another block's data (`getBlockEntity`).
|
||||
|
||||
**Chunk borders.** Tile data keeps speeds across unloading. Create tracks unloaded members' stress separately
|
||||
(`KineticNetwork`'s unloaded stress). Do the same, so a network across an unloaded chunk counts right.
|
||||
|
||||
+16
-12
@@ -107,20 +107,24 @@ export class MultiPlayerGameMode {
|
||||
const place_block = place_id !== undefined
|
||||
? EverythingRegistry.get<BlockRegistry>("blocks", place_id)
|
||||
: undefined;
|
||||
// with the states the server will give it, see BlockRules.placement_value, and holding the liquid it goes into
|
||||
const place_value = place_block &&
|
||||
waterlog_placement(
|
||||
block_rules.placement_value(place_block, this.client.player.yaw, this.client.player.pitch, {
|
||||
...target,
|
||||
face: hit.face,
|
||||
hit: hit.hit,
|
||||
sneaking,
|
||||
}),
|
||||
place_block,
|
||||
// with the states the server will give it, see BlockRules.placement_value, and holding the liquid it goes into.
|
||||
// undefined when its rules say it can't go there
|
||||
const ruled_value = place_block &&
|
||||
block_rules.placement_value(place_block, this.client.player.yaw, this.client.player.pitch, {
|
||||
...target,
|
||||
face: hit.face,
|
||||
hit: hit.hit,
|
||||
sneaking,
|
||||
});
|
||||
const place_value = ruled_value !== undefined
|
||||
? waterlog_placement(
|
||||
ruled_value,
|
||||
place_block!,
|
||||
level.get_block_value(target.x, target.y, target.z),
|
||||
EverythingRegistry.get_by_id<BlockRegistry>("blocks", current),
|
||||
);
|
||||
const placeable = place_id !== undefined && can_place(
|
||||
)
|
||||
: undefined;
|
||||
const placeable = place_value !== undefined && place_id !== undefined && can_place(
|
||||
place_block,
|
||||
current === AIR ? undefined : EverythingRegistry.get_by_id<BlockRegistry>("blocks", current),
|
||||
level.get_block_value(target.x, target.y, target.z),
|
||||
|
||||
@@ -67,13 +67,13 @@ export class BlockRules {
|
||||
|
||||
// the block's value with the states a player looking at yaw and pitch placing it gets: its defaults facing them
|
||||
// (default_placement_value), then what its rules' state_for_placement say. states that don't exist or don't fit are
|
||||
// left alone
|
||||
// left alone. undefined when a rule says it can't go there
|
||||
placement_value(
|
||||
block: BlockRegistry,
|
||||
yaw: number,
|
||||
pitch: number,
|
||||
where: Omit<BlockPlacement, "facing" | "looking">,
|
||||
): number {
|
||||
): number | undefined {
|
||||
let value = default_placement_value(block, yaw);
|
||||
const placement: BlockPlacement = {
|
||||
...where,
|
||||
@@ -87,6 +87,7 @@ export class BlockRules {
|
||||
`${id} state_for_placement`,
|
||||
() => rules.state_for_placement!(placement, params),
|
||||
);
|
||||
if (states === false) return undefined;
|
||||
for (const [name, state] of Object.entries(states ?? {})) {
|
||||
const known = block.states?.find((s) => s.name === name);
|
||||
if (known && Number.isInteger(state) && state >= 0 && state < 2 ** known.bits) {
|
||||
|
||||
@@ -125,8 +125,9 @@ export interface BlockInteraction {
|
||||
// deno-lint-ignore no-explicit-any
|
||||
export interface BlockRules<P = any> {
|
||||
// the states it's placed with when a player places it, like minecraft's getStateForPlacement. states it doesn't
|
||||
// return keep their defaults, and a 2 bit "facing" faces the player
|
||||
state_for_placement?(placement: BlockPlacement, params: P): Record<string, number> | void;
|
||||
// return keep their defaults, and a 2 bit "facing" faces the player. false when it can't go there, so it isn't
|
||||
// placed, like getStateForPlacement returning null
|
||||
state_for_placement?(placement: BlockPlacement, params: P): Record<string, number> | false | void;
|
||||
// a block next to it (on any of the 6 sides) was placed, broken or changed its states, like minecraft's
|
||||
// updateShape. neighbor is where, and what's there now
|
||||
on_neighbor_change?(block: SharedBlock, params: P, neighbor: SharedBlock): void;
|
||||
|
||||
@@ -93,11 +93,11 @@ export function setup(ctx: SharedContext) {
|
||||
}
|
||||
```
|
||||
|
||||
| Rule | Called when | Returns |
|
||||
| --------------------------------------------- | ---------------------------------------------------------------------- | -------------------------- |
|
||||
| `state_for_placement(placement, params)` | A player is about to place it. | States to place it with |
|
||||
| `on_neighbor_change(block, params, neighbor)` | A block on one of its 6 sides is placed, broken or changes its states. | |
|
||||
| `on_interact(block, params, interaction)` | A player right clicks it. | `true` if it did something |
|
||||
| Rule | Called when | Returns |
|
||||
| --------------------------------------------- | ---------------------------------------------------------------------- | ---------------------------------------------------------- |
|
||||
| `state_for_placement(placement, params)` | A player is about to place it. | States to place it with, or `false` when it can't go there |
|
||||
| `on_neighbor_change(block, params, neighbor)` | A block on one of its 6 sides is placed, broken or changes its states. | |
|
||||
| `on_interact(block, params, interaction)` | A player right clicks it. | `true` if it did something |
|
||||
|
||||
Here `block` is `{ id, x, y, z }`, without tile data, which clients never have. `ctx.world` in a shared script has
|
||||
`get_block`, `get_state`, `set_state`, `is_face_sturdy`, `biome_at` and `season_at` (see
|
||||
@@ -122,9 +122,11 @@ a slab's sides and plants aren't.
|
||||
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. It runs before the server
|
||||
checks that no one is in the way, so that's checked against the shape the block will really have. A trapdoor clicked
|
||||
onto the upper half of a block's side hangs in the top half:
|
||||
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
|
||||
`getStateForPlacement` returning `null`. It runs before the server checks that no one is in the way, so that's checked
|
||||
against the shape the block will really have. A trapdoor clicked onto the upper half of a block's side hangs in the top
|
||||
half:
|
||||
|
||||
```ts
|
||||
state_for_placement(placement): Record<string, number> {
|
||||
|
||||
@@ -15,6 +15,9 @@
|
||||
"render_layer": "cutout",
|
||||
"mining": { "toughness": 1 },
|
||||
"drops": "create:cogwheel",
|
||||
"item_model": "create:item_cogwheel"
|
||||
"item_model": "create:item_cogwheel",
|
||||
"components": {
|
||||
"create:kinetic": { "axis": "axis", "cog": "small" }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,9 @@
|
||||
"render_layer": "cutout",
|
||||
"mining": { "toughness": 1 },
|
||||
"drops": "create:large_cogwheel",
|
||||
"item_model": "create:item_large_cogwheel"
|
||||
"item_model": "create:item_large_cogwheel",
|
||||
"components": {
|
||||
"create:kinetic": { "axis": "axis", "cog": "large" }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,16 @@
|
||||
"create:kinetic": { "axis": "axis" }
|
||||
}
|
||||
},
|
||||
"create:cogwheel": {
|
||||
"components": {
|
||||
"create:kinetic": { "axis": "axis", "cog": "small" }
|
||||
}
|
||||
},
|
||||
"create:large_cogwheel": {
|
||||
"components": {
|
||||
"create:kinetic": { "axis": "axis", "cog": "large" }
|
||||
}
|
||||
},
|
||||
"create:hand_crank": {
|
||||
"interactive": true,
|
||||
"components": {
|
||||
|
||||
@@ -19,32 +19,60 @@ const HANDLE_TURNS: Record<Face, ModelRef> = {
|
||||
bottom: { model: HANDLE, x: 90 },
|
||||
};
|
||||
|
||||
// how a model made along y is turned to an axis, the way the kinetic blocks' variants turn theirs
|
||||
const AXIS_TURNS: Record<Axis, { x?: number; y?: number }> = { 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 shaft = (block: VisualBlock, instances: Instances) => turning(block, instances, shape_of(block)).visual;
|
||||
ctx.render.register_visual("create:shaft", shaft, { replaces_model: true });
|
||||
const plain = (block: VisualBlock, instances: Instances): Visual => ({
|
||||
update: turning(block, instances, shape_of(block)),
|
||||
});
|
||||
ctx.render.register_visual("create:shaft", plain, { replaces_model: true });
|
||||
ctx.render.register_visual("create:cogwheel", plain, { replaces_model: true });
|
||||
|
||||
// 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,
|
||||
{ model: "create:block_large_cogwheel_shaftless", ...turns },
|
||||
11.25,
|
||||
);
|
||||
const shaft = turning(block, instances, shape, { model: "create:block_cogwheel_shaft", ...turns });
|
||||
return {
|
||||
update() {
|
||||
cog();
|
||||
shaft();
|
||||
},
|
||||
};
|
||||
}, { replaces_model: true });
|
||||
|
||||
// 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 { visual, speed } = turning(block, instances, shape);
|
||||
const turn = turning(block, instances, shape);
|
||||
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 {
|
||||
...visual,
|
||||
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 += (speed() * 0.3 - velocity) * Math.min(1, ticks / 4);
|
||||
velocity += (speed_of(block) * 0.3 - velocity) * Math.min(1, ticks / 4);
|
||||
angle = (angle + velocity * ticks) % 360;
|
||||
handle.set_rotation(shape.axis, 0, angle);
|
||||
},
|
||||
@@ -52,16 +80,17 @@ export function setup(ctx: ClientContext) {
|
||||
}, { replaces_model: true });
|
||||
}
|
||||
|
||||
// the block's own model turning around its axis at its speed. offset so cogs mesh with the ones beside them, like
|
||||
// KineticBlockEntityVisual.rotationOffset: turned by half a tooth on every other block
|
||||
function turning(block: VisualBlock, instances: Instances, shape: KineticShape) {
|
||||
const speed = () => block.get_client_data<number>("speed") ?? 0;
|
||||
const instance = instances.rotating();
|
||||
const offset = should_offset(shape.axis, block) ? 22.5 : 0;
|
||||
const turn = () => instance.set_rotation(shape.axis, speed(), offset);
|
||||
const speed_of = (block: VisualBlock) => block.get_client_data<number>("speed") ?? 0;
|
||||
|
||||
// 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) {
|
||||
const instance = instances.rotating(model);
|
||||
const angle = should_offset(shape.axis, block) ? 22.5 : offset;
|
||||
const turn = () => instance.set_rotation(shape.axis, speed_of(block), angle);
|
||||
turn();
|
||||
const visual: Visual = { update: turn };
|
||||
return { visual, speed };
|
||||
return turn;
|
||||
}
|
||||
|
||||
// whether the coordinates across the axis add up to an even number, like KineticBlockEntityVisual.shouldOffset
|
||||
|
||||
@@ -18,11 +18,15 @@ export interface KineticParams {
|
||||
// which ends of its axis have an axle that turns the block there: both, or for one with a facing only the end it
|
||||
// faces ("front") or the other ("back"). both when not given
|
||||
shaft?: "both" | "front" | "back";
|
||||
// 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";
|
||||
}
|
||||
|
||||
// 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"];
|
||||
const AXES: readonly Axis[] = ["x", "y", "z"];
|
||||
// a 2 bit axis state's values
|
||||
export const AXES: readonly Axis[] = ["x", "y", "z"];
|
||||
|
||||
export const FACE_OFFSETS: Readonly<Record<Face, readonly [number, number, number]>> = {
|
||||
west: [-1, 0, 0],
|
||||
@@ -65,6 +69,7 @@ export interface KineticShape {
|
||||
shafts: readonly Face[];
|
||||
// the way it faces, for a block with a facing
|
||||
facing?: Face;
|
||||
cog?: "small" | "large";
|
||||
}
|
||||
|
||||
// a kinetic block where it is
|
||||
@@ -87,7 +92,7 @@ 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] };
|
||||
return { axis, shafts: AXIS_FACES[axis], ...(params.cog && { cog: params.cog }) };
|
||||
}
|
||||
|
||||
export function face_axis(face: Face): Axis {
|
||||
@@ -101,23 +106,52 @@ export function along(face: Face, speed: number): number {
|
||||
}
|
||||
|
||||
// the face of from that to is beside, undefined when they aren't beside each other
|
||||
function face_toward(from: Kinetic, to: Kinetic): Face | undefined {
|
||||
const [dx, dy, dz] = [to.x - from.x, to.y - from.y, to.z - from.z];
|
||||
function face_toward([dx, dy, dz]: readonly number[]): Face | undefined {
|
||||
return (Object.keys(FACE_OFFSETS) as Face[]).find((face) => {
|
||||
const [ox, oy, oz] = FACE_OFFSETS[face];
|
||||
return ox === dx && oy === dy && oz === dz;
|
||||
});
|
||||
}
|
||||
|
||||
// how much faster to turns than from when from turns it, 0 when from doesn't turn it: 1 for axles that meet. like
|
||||
// RotationPropagator.getRotationSpeedModifier
|
||||
// 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
|
||||
export function speed_ratio(from: Kinetic, to: Kinetic): number {
|
||||
const face = face_toward(from, to);
|
||||
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;
|
||||
|
||||
// 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
|
||||
if (from.cog === "large" && to.cog === "large") {
|
||||
if (from.axis === to.axis || along(from.axis) === 0 || along(to.axis) === 0) return 0;
|
||||
if (AXES.some((axis) => axis !== from.axis && axis !== to.axis && along(axis) !== 0)) return 0;
|
||||
return along(from.axis) > 0 !== along(to.axis) > 0 ? -1 : 1;
|
||||
}
|
||||
// a large cog meets a small one on the same axis at one of its corners, level with it along the axis
|
||||
const at_corner = from.axis === to.axis && along(from.axis) === 0 &&
|
||||
AXES.every((axis) => axis === from.axis || Math.abs(along(axis)) === 1);
|
||||
if (from.cog === "large" && to.cog === "small" && at_corner) return -2;
|
||||
if (from.cog === "small" && to.cog === "large" && at_corner) return -0.5;
|
||||
// small cogs side by side on the same axis
|
||||
if (from.cog === "small" && to.cog === "small" && face && face_axis(face) !== from.axis && from.axis === to.axis) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// where the blocks it could turn are, like getPotentialNeighbourLocations
|
||||
// the offsets of the 12 blocks that share an edge with one
|
||||
const EDGES = [-1, 0, 1].flatMap((dx) => [-1, 0, 1].flatMap((dy) => [-1, 0, 1].map((dz) => [dx, dy, dz])))
|
||||
.filter((offset) => offset.filter((d) => d !== 0).length === 2);
|
||||
|
||||
// where the blocks it could turn are, like getPotentialNeighbourLocations: the 6 beside it, and for cogs the ones
|
||||
// across their edges, in their own plane for small ones
|
||||
export function neighbour_positions(block: Kinetic): [number, number, number][] {
|
||||
return Object.values(FACE_OFFSETS).map(([dx, dy, dz]) => [block.x + dx, block.y + dy, block.z + dz]);
|
||||
const offsets: readonly (readonly number[])[] = [
|
||||
...Object.values(FACE_OFFSETS),
|
||||
...block.cog === "large" ? EDGES : [],
|
||||
...block.cog === "small" ? EDGES.filter((offset) => offset[AXES.indexOf(block.axis)] === 0) : [],
|
||||
];
|
||||
return offsets.map(([dx, dy, dz]) => [block.x + dx, block.y + dy, block.z + dz]);
|
||||
}
|
||||
|
||||
@@ -3,44 +3,76 @@
|
||||
|
||||
// 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 { Face, SharedContext } from "bworld/shared";
|
||||
import type { BlockPlacement, Face, SharedContext } from "bworld/shared";
|
||||
import {
|
||||
AXES,
|
||||
type Axis,
|
||||
face_axis,
|
||||
FACE_OFFSETS,
|
||||
FACINGS,
|
||||
kinetic_shape,
|
||||
type KineticParams,
|
||||
type KineticShape,
|
||||
OPPOSITE,
|
||||
} from "./kinetics/connections.ts";
|
||||
|
||||
const AXES = ["x", "y", "z"];
|
||||
|
||||
export function setup(ctx: SharedContext) {
|
||||
// what turns at x y z, undefined when nothing does
|
||||
const kinetic_at = (x: number, y: number, z: number): KineticShape | undefined => {
|
||||
const id = ctx.world.get_block(x, y, z);
|
||||
const params = id && ctx.blocks.exists(id)
|
||||
? ctx.blocks.components(id)["create:kinetic"] as KineticParams | undefined
|
||||
: undefined;
|
||||
return params && kinetic_shape(params, (name) => ctx.world.get_state(x, y, z, name));
|
||||
};
|
||||
const beside = (x: number, y: number, z: number, side: Face) => {
|
||||
const [dx, dy, dz] = FACE_OFFSETS[side];
|
||||
return kinetic_at(x + dx, y + dy, z + dz);
|
||||
};
|
||||
|
||||
// the side of x y z with an axle pointing at it, like getPreferredFacing. undefined when there's none, or when
|
||||
// axles point at it along two axes, since it can't line up with both
|
||||
const axle_beside = (x: number, y: number, z: number): Face | undefined => {
|
||||
let found: Face | undefined;
|
||||
for (const side of FACINGS) {
|
||||
const [dx, dy, dz] = FACE_OFFSETS[side];
|
||||
const [nx, ny, nz] = [x + dx, y + dy, z + dz];
|
||||
const id = ctx.world.get_block(nx, ny, nz);
|
||||
const params = id && ctx.blocks.exists(id)
|
||||
? ctx.blocks.components(id)["create:kinetic"] as KineticParams | undefined
|
||||
: undefined;
|
||||
if (!params) continue;
|
||||
const shape = kinetic_shape(params, (name) => ctx.world.get_state(nx, ny, nz, name));
|
||||
if (!shape.shafts.includes(OPPOSITE[side])) continue;
|
||||
if (!beside(x, y, z, side)?.shafts.includes(OPPOSITE[side])) continue;
|
||||
if (found && face_axis(found) !== face_axis(side)) return undefined;
|
||||
found = side;
|
||||
}
|
||||
return found;
|
||||
};
|
||||
|
||||
// a cog lines up with the small cog it's placed against, or the axle beside it, or else the face it's clicked onto,
|
||||
// which is all sneaking looks at, like CogWheelBlock.getAxisForPlacement
|
||||
const cog_axis = (placement: BlockPlacement): Axis => {
|
||||
const { x, y, z, face } = placement;
|
||||
if (placement.sneaking) return face_axis(face);
|
||||
const against = beside(x, y, z, OPPOSITE[face]);
|
||||
if (against?.cog === "small") return against.axis;
|
||||
const side = axle_beside(x, y, z);
|
||||
return face_axis(side ?? face);
|
||||
};
|
||||
// whether a cog on axis at x y z keeps clear of the cogs beside it: a large one's teeth would cut into any cog
|
||||
// beside it across its axis, and a small one's into a large one, unless that one's axis points at it. like
|
||||
// isValidCogwheelPosition
|
||||
const cog_fits = (x: number, y: number, z: number, axis: Axis, cog: "small" | "large") =>
|
||||
FACINGS.every((side) => {
|
||||
const other = beside(x, y, z, side);
|
||||
if (face_axis(side) === axis || !other?.cog || other.axis === face_axis(side)) return true;
|
||||
return other.cog === "small" && cog === "small";
|
||||
});
|
||||
|
||||
// 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 away
|
||||
// from the axle, or toward the player, or away from them while sneaking, like DirectionalKineticBlock
|
||||
// from the axle, or toward the player, or away from them while sneaking, like DirectionalKineticBlock. cogs are
|
||||
// placed their own way, see cog_axis
|
||||
ctx.components.register_block<KineticParams>("create:kinetic", {
|
||||
state_for_placement(placement, params) {
|
||||
if (params.cog) {
|
||||
const axis = cog_axis(placement);
|
||||
if (!cog_fits(placement.x, placement.y, placement.z, axis, params.cog)) return false;
|
||||
return { [params.axis ?? "axis"]: AXES.indexOf(axis) };
|
||||
}
|
||||
const side = axle_beside(placement.x, placement.y, placement.z);
|
||||
if (params.facing !== undefined) {
|
||||
const facing = side && !placement.sneaking
|
||||
|
||||
@@ -1212,16 +1212,22 @@ export class GameServer {
|
||||
}
|
||||
// solid blocks can't go where a mob or player is, the one placing it included, like minecraft
|
||||
const placing = EverythingRegistry.get<BlockRegistry>("blocks", held.block_id)!;
|
||||
const ruled_value = this.mods.rules.placement_value(placing, player.yaw, player.pitch, {
|
||||
x: tx,
|
||||
y: ty,
|
||||
z: tz,
|
||||
face,
|
||||
hit: [hit[0], hit[1], hit[2]],
|
||||
sneaking: player.sneaking,
|
||||
});
|
||||
// its rules say it can't go there
|
||||
if (ruled_value === undefined) {
|
||||
this.#correct(player, tx, ty, tz);
|
||||
return;
|
||||
}
|
||||
// put into a source of the liquid it can hold, it holds it
|
||||
const placed_value = waterlog_placement(
|
||||
this.mods.rules.placement_value(placing, player.yaw, player.pitch, {
|
||||
x: tx,
|
||||
y: ty,
|
||||
z: tz,
|
||||
face,
|
||||
hit: [hit[0], hit[1], hit[2]],
|
||||
sneaking: player.sneaking,
|
||||
}),
|
||||
ruled_value,
|
||||
placing,
|
||||
this.world.get_block_value(tx, ty, tz),
|
||||
this.world.get_block_info(tx, ty, tz),
|
||||
|
||||
@@ -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));
|
||||
});
|
||||
|
||||
@@ -89,7 +89,7 @@ function client_of(t: Game) {
|
||||
// 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 });
|
||||
copy.set(`${x} ${y} ${z}`, [id, value >>> 16]);
|
||||
copy.set(`${x} ${y} ${z}`, [id, value! >>> 16]);
|
||||
rules.notify_neighbors(world.get_block, x, y, z);
|
||||
},
|
||||
// what game_mode does when this client right clicks face of x y z, at hit. whether the rules did something
|
||||
|
||||
Reference in New Issue
Block a user