Basin
This commit is contained in:
@@ -3,8 +3,18 @@
|
||||
|
||||
// how create's moving blocks are drawn: kinetic blocks turn by the speed the server puts in their "speed" client data,
|
||||
// as rotating instances, so a block turning steadily costs nothing per frame. they're still while their network is
|
||||
// overstressed
|
||||
import type { ClientContext, Instances, ModelRef, Visual, VisualBlock } from "bworld/client";
|
||||
// overstressed. and what's in a basin
|
||||
import { mat4 } from "gl-matrix";
|
||||
import type {
|
||||
ClientContext,
|
||||
Instance,
|
||||
Instances,
|
||||
ItemStack,
|
||||
ModelRef,
|
||||
TransformedInstance,
|
||||
Visual,
|
||||
VisualBlock,
|
||||
} from "bworld/client";
|
||||
import type { Face } from "bworld/shared";
|
||||
import {
|
||||
actual_speed,
|
||||
@@ -18,6 +28,7 @@ import {
|
||||
OPPOSITE,
|
||||
} from "./kinetics/connections.ts";
|
||||
import { head_offset, type PressShown } from "./processing/press.ts";
|
||||
import { BASIN_TANK_CAPACITY, BASIN_TANKS, type BasinTank } from "./processing/basin.ts";
|
||||
|
||||
// a model reaching north turned to reach each way
|
||||
const facing_turns = (model: string): Record<Face, ModelRef> => ({
|
||||
@@ -167,6 +178,8 @@ export function setup(ctx: ClientContext) {
|
||||
};
|
||||
});
|
||||
|
||||
ctx.render.register_visual("create:basin", basin_visual);
|
||||
|
||||
// the half shaft turns with the speed like any kinetic block, and the handle catches up with it and slows down
|
||||
// with it, like HandCrankVisual's independent angle
|
||||
ctx.render.register_visual("create:hand_crank", (block, instances) => {
|
||||
@@ -214,3 +227,82 @@ function should_offset(axis: Axis, { x, y, z }: VisualBlock) {
|
||||
const across = (axis === "x" ? 0 : x) + (axis === "y" ? 0 : y) + (axis === "z" ? 0 : z);
|
||||
return across % 2 === 0;
|
||||
}
|
||||
|
||||
// a basin's items are a copy more for every this many
|
||||
const BASIN_ITEMS_A_COPY = 8;
|
||||
|
||||
// what's in a basin, by its "tanks" and "items" client data (see register_basin in server.ts), like BasinRenderer. its
|
||||
// fluids fill the 12 pixels inside its walls side by side, as wide as their share of it, and as high as all of them
|
||||
// make it, filling faster at first. its items lie in a ring over the fluid, tipped over and turned toward the middle,
|
||||
// one in the middle when it's alone, and bob up and down while there's fluid
|
||||
function basin_visual(block: VisualBlock, instances: Instances): Visual {
|
||||
let shown: Instance[] = [];
|
||||
// each copy of an item, placed in the basin but for how high it bobs, and where in its bobbing it is
|
||||
let floating: { instance: TransformedInstance; placed: mat4; phase: number }[] = [];
|
||||
const update = () => {
|
||||
for (const instance of shown) instance.delete();
|
||||
shown = [];
|
||||
floating = [];
|
||||
const tanks = block.get_client_data<BasinTank[]>("tanks") ?? [];
|
||||
const total = tanks.reduce((sum, tank) => sum + tank.amount, 0);
|
||||
const full = 1 - (1 - Math.min(1, total / (BASIN_TANKS * BASIN_TANK_CAPACITY))) ** 2;
|
||||
const top = total > 0 ? 2 + 12 * full : 0;
|
||||
let x = 2;
|
||||
for (const tank of tanks) {
|
||||
const width = 12 * tank.amount / total;
|
||||
const fluid = instances.fluid(tank.fluid);
|
||||
fluid.set_box([x, 2, 2], [x + width, top, 14]);
|
||||
shown.push(fluid);
|
||||
x += width;
|
||||
}
|
||||
|
||||
const items = block.get_client_data<ItemStack[]>("items") ?? [];
|
||||
const level = Math.min(0.6, Math.max(0.125, top / 16 - 0.3));
|
||||
const radius = items.length === 1 ? 0 : 0.125;
|
||||
const random = seeded(block.x * 961 + block.y * 31 + block.z);
|
||||
items.forEach((stack, i) => {
|
||||
const angle = 360 / items.length * (items.length - i) * Math.PI / 180;
|
||||
for (let copy = 0; copy <= Math.floor(stack.count / BASIN_ITEMS_A_COPY); copy++) {
|
||||
const placed = mat4.fromTranslation(mat4.create(), [
|
||||
radius * Math.cos(angle),
|
||||
level,
|
||||
-radius * Math.sin(angle),
|
||||
]);
|
||||
mat4.rotateY(placed, placed, angle + 35 * Math.PI / 180);
|
||||
mat4.rotateX(placed, placed, 65 * Math.PI / 180);
|
||||
const jitter = () => (random() - 0.5) / 8;
|
||||
mat4.translate(placed, placed, [jitter() - 0.5, jitter() - 0.5, jitter() - 0.5]);
|
||||
const instance = instances.item(stack);
|
||||
shown.push(instance);
|
||||
floating.push({ instance, placed, phase: angle * 180 / Math.PI });
|
||||
place(instance, placed, 0);
|
||||
}
|
||||
});
|
||||
if (top === 0) floating = [];
|
||||
};
|
||||
update();
|
||||
return {
|
||||
update,
|
||||
on_frame(time) {
|
||||
for (const { instance, placed, phase } of floating) {
|
||||
place(instance, placed, (Math.sin(time / 12 + phase) + 1.5) / 32);
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// an item copy in a basin, bob higher than where it was placed
|
||||
function place(instance: TransformedInstance, placed: mat4, bob: number) {
|
||||
const matrix = mat4.fromTranslation(mat4.create(), [0.5, 0.2 + bob, 0.5]);
|
||||
instance.set_transform(mat4.multiply(matrix, matrix, placed));
|
||||
}
|
||||
|
||||
// the same numbers from 0 to 1 every time for the same seed, so a basin's items lie the same way however often they're
|
||||
// drawn again
|
||||
function seeded(seed: number) {
|
||||
let state = seed >>> 0;
|
||||
return () => {
|
||||
state = (Math.imul(state, 1664525) + 1013904223) >>> 0;
|
||||
return state / 2 ** 32;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// what a basin holds, like create's BasinBlockEntity's input inventory and tanks: up to BASIN_SLOTS kinds of item, a
|
||||
// stack of each, and BASIN_TANKS tanks with a fluid each. a kind of item or fluid only ever goes where there's already
|
||||
// some of it, so there's never two stacks or two tanks of one. no ctx, so the server keeps it and the client draws it
|
||||
// from the same shapes
|
||||
import type { FluidVariant, ItemStack } from "bworld/shared";
|
||||
|
||||
export const BASIN_SLOTS = 9;
|
||||
export const BASIN_TANKS = 2;
|
||||
export const BASIN_TANK_CAPACITY = 1000;
|
||||
|
||||
export interface BasinTank {
|
||||
fluid: FluidVariant;
|
||||
amount: number;
|
||||
}
|
||||
|
||||
// how many of stack go into items, and what items is after: into the stack of the same item, or a new one while
|
||||
// there's room for another kind, up to max a stack. like BasinInventory.insertItem
|
||||
export function insert_item(items: readonly ItemStack[], stack: ItemStack, max: number) {
|
||||
const index = items.findIndex((item) => same_item(item, stack));
|
||||
if (index === -1 && items.length >= BASIN_SLOTS) return { items, inserted: 0 };
|
||||
const had = index === -1 ? 0 : items[index].count;
|
||||
const inserted = Math.max(0, Math.min(stack.count, max - had));
|
||||
if (inserted === 0) return { items, inserted };
|
||||
const into = { ...stack, count: had + inserted };
|
||||
return { items: index === -1 ? [...items, into] : items.with(index, into), inserted };
|
||||
}
|
||||
|
||||
// tanks with amount of fluid poured in, or undefined when it doesn't all fit: into the tank that has that fluid, or else
|
||||
// an empty one, like SmartFluidTankBehaviour's tanks that each keep to one fluid
|
||||
export function fill(
|
||||
tanks: readonly BasinTank[],
|
||||
fluid: FluidVariant,
|
||||
amount: number,
|
||||
same: (a: FluidVariant, b: FluidVariant) => boolean,
|
||||
): BasinTank[] | undefined {
|
||||
const index = tanks.findIndex((tank) => same(tank.fluid, fluid));
|
||||
if (index === -1) {
|
||||
return tanks.length < BASIN_TANKS && amount <= BASIN_TANK_CAPACITY ? [...tanks, { fluid, amount }] : undefined;
|
||||
}
|
||||
const total = tanks[index].amount + amount;
|
||||
return total <= BASIN_TANK_CAPACITY ? tanks.with(index, { fluid, amount: total }) : undefined;
|
||||
}
|
||||
|
||||
// amount of the first tank's fluid taken out, and the tanks left, or undefined when it has less than that. like
|
||||
// filling an item from CombinedTankWrapper, which drains the first tank with any
|
||||
export function drain(tanks: readonly BasinTank[], amount: number) {
|
||||
const [first, ...rest] = tanks;
|
||||
if (!first || first.amount < amount) return undefined;
|
||||
const left = first.amount - amount;
|
||||
return { fluid: first.fluid, tanks: left > 0 ? [{ ...first, amount: left }, ...rest] : rest };
|
||||
}
|
||||
|
||||
// whether two stacks are of one item: the same id, and the same data whatever order its keys are in
|
||||
export function same_item(a: ItemStack, b: ItemStack): boolean {
|
||||
return a.id === b.id && canonical(a.data) === canonical(b.data);
|
||||
}
|
||||
|
||||
function canonical(value: unknown): string {
|
||||
if (Array.isArray(value)) return `[${value.map(canonical).join(",")}]`;
|
||||
if (value && typeof value === "object") {
|
||||
const entries = Object.entries(value).sort(([a], [b]) => a < b ? -1 : 1);
|
||||
return `{${entries.map(([key, v]) => `${JSON.stringify(key)}:${canonical(v)}`).join(",")}}`;
|
||||
}
|
||||
return JSON.stringify(value) ?? "";
|
||||
}
|
||||
@@ -2,8 +2,8 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// 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";
|
||||
// the creative motor and the water wheel), the mechanical press and the basin
|
||||
import type { BlockRef, EntityRef, FluidVariant, Id, ItemStack, ServerContext } from "bworld/server";
|
||||
import {
|
||||
actual_speed,
|
||||
along,
|
||||
@@ -27,6 +27,7 @@ import {
|
||||
roll_results,
|
||||
} from "./recipes/processing.ts";
|
||||
import { CYCLE, cycle_rate, PRESS_AT, type PressShown, SCAN_TICKS } from "./processing/press.ts";
|
||||
import { type BasinTank, drain, fill, insert_item } from "./processing/basin.ts";
|
||||
|
||||
interface Config {
|
||||
max_speed: number;
|
||||
@@ -194,6 +195,61 @@ export function setup(ctx: ServerContext) {
|
||||
});
|
||||
|
||||
register_press(ctx, pressing);
|
||||
register_basin(ctx);
|
||||
}
|
||||
|
||||
// holds items dropped into it and fluid poured in, like create's BasinBlock: an item that lands in it goes in, a full
|
||||
// fluid container used on it empties into it and an empty one fills from it, and an empty hand takes every item out.
|
||||
// what's in it is its "items" and "tanks" client data, kept nowhere else, which its visual draws. breaking it drops
|
||||
// the items, and the fluid is lost
|
||||
function register_basin(ctx: ServerContext) {
|
||||
const items_of = (block: BlockRef) => block.get_client_data<ItemStack[]>("items") ?? [];
|
||||
const tanks_of = (block: BlockRef) => block.get_client_data<BasinTank[]>("tanks") ?? [];
|
||||
const set_items = (block: BlockRef, items: readonly ItemStack[]) =>
|
||||
block.set_client_data("items", items.length > 0 ? items : undefined);
|
||||
const set_tanks = (block: BlockRef, tanks: BasinTank[]) =>
|
||||
block.set_client_data("tanks", tanks.length > 0 ? tanks : undefined);
|
||||
|
||||
ctx.components.register_block("create:basin", {
|
||||
on_entity_stand(block, _params, entity) {
|
||||
const stack = entity.get_item();
|
||||
if (!stack) return;
|
||||
const { items, inserted } = insert_item(items_of(block), stack, ctx.items.max_stack(stack.id, stack.data));
|
||||
if (inserted === 0) return;
|
||||
set_items(block, items);
|
||||
entity.set_item({ ...stack, count: stack.count - inserted });
|
||||
},
|
||||
on_interact(block, _params, player) {
|
||||
const held = player.held_item;
|
||||
if (!held) {
|
||||
for (const item of items_of(block)) player.give_item(item.id, item.count, item.data);
|
||||
set_items(block, []);
|
||||
return true;
|
||||
}
|
||||
const capacity = ctx.items.fluid_capacity(held.id);
|
||||
// anything else is used or placed as if the basin weren't there
|
||||
if (capacity === undefined) return false;
|
||||
const fluid = (held.data as { fluid?: FluidVariant } | undefined)?.fluid;
|
||||
if (fluid) {
|
||||
const tanks = fill(tanks_of(block), fluid, capacity, (a, b) => ctx.fluids.same(a, b));
|
||||
if (tanks) {
|
||||
set_tanks(block, tanks);
|
||||
player.exchange_held({ id: held.id, count: 1 });
|
||||
}
|
||||
} else {
|
||||
const drained = drain(tanks_of(block), capacity);
|
||||
if (drained) {
|
||||
set_tanks(block, drained.tanks);
|
||||
player.exchange_held({ id: held.id, count: 1, data: { fluid: drained.fluid } });
|
||||
}
|
||||
}
|
||||
// a fluid container is used up on the basin even when nothing fits, like create's
|
||||
return true;
|
||||
},
|
||||
on_break(block) {
|
||||
for (const item of items_of(block)) ctx.world.drop_item(block.x, block.y, block.z, item);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// presses items lying on the ground under it while it turns, like create's MechanicalPressBlockEntity pressing in the
|
||||
|
||||
Reference in New Issue
Block a user