Fluid containers

This commit is contained in:
2026-10-25 15:02:31 -03:00
parent fc27771a92
commit 26b59a1899
24 changed files with 486 additions and 82 deletions
+13
View File
@@ -0,0 +1,13 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// the fluids' rules from the mods' shared scripts on this client, the same as the server's (see common/fluids.ts), for
// drawing fluids and naming them in tooltips. its own module so those don't import client/mods.ts, which loads the mods
// and makes it again for each server's
import { FluidRules } from "$/common/fluids.ts";
export let fluid_rules = new FluidRules(() => undefined);
export function set_fluid_rules(rules: FluidRules) {
fluid_rules = rules;
}
+1 -2
View File
@@ -161,8 +161,7 @@ export class ModGraphics implements Graphics {
if (!EverythingRegistry.get<ItemRegistry>("items", id)) throw new Error(`unknown item ${id}`);
const stack = new ItemStack(id, count);
// the count only shows when it's more than one
if (count === 1) stack.max_amount = 1;
draw_item(stack, this.#x(x), this.#y(y));
draw_item(stack, this.#x(x), this.#y(y), count !== 1 && stack.max_amount !== 1);
}
clip(x: number, y: number, w: number, h: number, draw: () => void) {
+11 -3
View File
@@ -1,10 +1,12 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// the box next to the mouse saying what an item is, like minecraft's: its name, what it's worth, then its lore
// the box next to the mouse saying what an item is, like minecraft's: its name, the fluid in it, what it's worth, then
// its lore
import { EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
import type { ItemStack } from "$/common/inventory.ts";
import { contained_fluid, type ItemStack } from "$/common/inventory.ts";
import { display_name } from "$/common/utils.ts";
import { fluid_rules } from "$/client/fluid_rules.ts";
import { canvas, draw_text, draw_texture_region, measure_text, type Texture } from "$/client/renderer/mod.ts";
import { AssetManager } from "$/client/assets.ts";
import { get_sprite_region } from "$/client/sprites.ts";
@@ -16,6 +18,7 @@ type Color = [number, number, number, number];
const NAME_COLOR: Color = [1, 1, 1, 1];
const LORE_COLOR: Color = [0.66, 0.66, 0.66, 1];
const FOOD_COLOR: Color = [0.85, 0.6, 0.35, 1];
const FLUID_COLOR: Color = [0.55, 0.75, 0.95, 1];
const VALUE_COLOR: Color = [0.95, 0.8, 0.3, 1];
// from the edge of the tooltip sprite to the text
const PADDING = 4 * GUI_SCALE;
@@ -34,10 +37,15 @@ export interface TooltipLine {
icon?: string;
}
// its name, the item's lore from its json, then what the server's scripts said about this stack
// its name, the fluid a full container holds, the item's lore from its json, then what the server's scripts said about
// this stack
export function item_tooltip(item: ItemStack): TooltipLine[] {
const lines: TooltipLine[] = [{ text: display_name(item.type_id), color: NAME_COLOR }];
const info = EverythingRegistry.get<ItemRegistry>("items", item.type_id);
const fluid = contained_fluid(item.type_id, item.data);
if (fluid) {
lines.push({ text: `${fluid_rules.name(fluid)}, ${info!.fluid_container!.capacity} mB`, color: FLUID_COLOR });
}
if (info?.food) {
lines.push({ text: `Restores ${info.food.nutrition} hunger`, color: FOOD_COLOR });
}
+2 -3
View File
@@ -11,6 +11,7 @@ import { type Bmod, read_bmod } from "$/common/bmod.ts";
import { get_state_value, set_state_value, shared_items_api } from "$/common/utils.ts";
import { BlockRules } from "$/common/block_rules.ts";
import { FluidRules } from "$/common/fluids.ts";
import { fluid_rules, set_fluid_rules } from "./fluid_rules.ts";
import { covers_face, EMPTY_SHAPE, FULL_SHAPE, is_solid } from "$/common/block_shapes.ts";
import type { SharedContext, SharedWorld } from "$/common/mod_api/shared.ts";
import { tag_api } from "$/common/tags.ts";
@@ -91,8 +92,6 @@ const rules_world: SharedWorld = {
// what the mods' shared scripts registered, run to guess ahead of the server, see common/block_rules.ts
export let block_rules = new BlockRules(run_mod, rules_world.get_block);
// the fluids' rules from shared scripts, the same as the server's, see common/fluids.ts
export let fluid_rules = new FluidRules(run_mod);
// whether shared scripts are being set up, the only time they can register rules
let rules_setup = false;
// whether client scripts are being set up, the only time they can register visuals, since chunk workers are told
@@ -166,7 +165,7 @@ export async function load_client_mods(listings: ModListing[], bmods: Bmod[]) {
// every mod's shared rules first, so client scripts can count on any mod's being there
block_rules = new BlockRules(run_mod, rules_world.get_block);
fluid_rules = new FluidRules(run_mod);
set_fluid_rules(new FluidRules(run_mod));
rules_setup = true;
for (const [i, bmod] of bmods.entries()) {
const code = bmod.scripts.shared;
+48 -8
View File
@@ -3,11 +3,11 @@
import { mat4, vec3 } from "gl-matrix";
import type { SpriteRegion } from "$/common/constants.ts";
import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
import { BlockRegistry, EverythingRegistry, type ItemLayer, ItemRegistry } from "$/common/everything_registry.ts";
import { AssetManager } from "$/client/assets.ts";
import { get_sprite_region } from "$/client/sprites.ts";
import type { ItemEntity } from "$/client/entity/item_entity.ts";
import { type ItemData, ItemStack } from "$/common/inventory.ts";
import { contained_fluid, type ItemData, ItemStack } from "$/common/inventory.ts";
import { light_color, push_vertex, Texture } from "$/client/renderer/mod.ts";
import {
bake_model,
@@ -23,12 +23,14 @@ import {
type ModelDisplay,
type ModelJson,
} from "$/common/block_models.ts";
import { item_model } from "./item_model.ts";
import { item_model, type ItemQuad } from "./item_model.ts";
import { get_state_value, item_texture } from "$/common/utils.ts";
import { ID_MASK, TEXTURE_SIZE } from "$/common/constants.ts";
import type { ClientLevel } from "$/client/level/client_level.ts";
import { entity_tint } from "./entity_light.ts";
import type { Vec3 } from "./player_renderer.ts";
import { fluid_rules } from "$/client/fluid_rules.ts";
import { color_rgb, tint_without_biome, type TintColor } from "$/common/worldgen/biome_colors.ts";
// how big items on the ground are drawn, minecraft's ground transform
const BLOCK_SCALE = 0.25;
@@ -50,6 +52,9 @@ const CORNER_UVS = [[0, 1], [1, 1], [1, 0], [0, 0]] as const;
// where the extra copies of a bigger stack sit, as fractions of the model's size
const COPY_OFFSETS = [[0, 0, 0], [0.35, 0.2, -0.25], [-0.3, 0.4, 0.2], [0.15, 0.6, 0.35], [-0.2, 0.8, -0.3]];
// how far each of a flat item's layers sits out from the one under it, so it's drawn over it
const LAYER_GAP = 0.001;
// minecraft draws more copies of the model the bigger the stack is
function copies(amount: number) {
if (amount > 48) return 5;
@@ -320,19 +325,54 @@ export function push_item(atlas: Texture, item: ItemStack, transform: ItemTransf
return;
}
// its sprite, then its layers over it: only their fronts and backs, the first two quads, each a little further out
const texture_id = item_texture(item);
push_sprite(atlas, texture_id, item_model(texture_id), [tr, tg, tb], 0, transform);
item_layers(item).forEach(({ texture, color: [r, g, b] }, i) => {
const color: Vec3 = [r * tr, g * tg, b * tb];
push_sprite(atlas, texture, item_model(texture).slice(0, 2), color, (i + 1) * LAYER_GAP, transform);
});
}
// a flat item's sprite made 3d (see item_model.ts), its front and back gap further out than the item's
function push_sprite(
atlas: Texture,
texture_id: string,
quads: ItemQuad[],
[r, g, b]: Vec3,
gap: number,
transform: ItemTransform,
) {
const region = get_sprite_region(texture_id);
const u = (pixels: number) => (region.x + pixels) / atlas.width;
const v = (pixels: number) => (region.y + pixels) / atlas.height;
for (const quad of item_model(texture_id)) {
for (const quad of quads) {
for (const i of [0, 1, 2, 0, 2, 3]) {
const [px, py, pz] = transform(...quad.corners[i]);
const [cx, cy, cz] = quad.corners[i];
const [px, py, pz] = transform(cx, cy, cz + Math.sign(cz) * gap);
const [pu, pv] = quad.uvs[i];
push_vertex(px, py, pz, u(pu), v(pv), quad.shade * tr, quad.shade * tg, quad.shade * tb, 1);
const u = (region.x + pu) / atlas.width;
const v = (region.y + pv) / atlas.height;
push_vertex(px, py, pz, u, v, quad.shade * r, quad.shade * g, quad.shade * b, 1);
}
}
}
// the layers drawn over a flat item's texture (see ItemLayer), each in its tint's color, without the ones whose tint
// has no color for this stack
export function item_layers(item: ItemStack): { texture: string; color: Vec3 }[] {
const layers = EverythingRegistry.get<ItemRegistry>("items", item.type_id)?.layers ?? [];
return layers.flatMap(({ texture, tint }) => {
const color = layer_color(item, tint);
return color ? [{ texture, color }] : [];
});
}
function layer_color(item: ItemStack, tint: ItemLayer["tint"]): Vec3 | undefined {
if (tint === undefined) return [1, 1, 1];
if (tint !== "fluid") return color_rgb(tint_without_biome(tint));
const fluid = contained_fluid(item.type_id, item.data);
return fluid && color_rgb(tint_without_biome(fluid_rules.color(fluid) as TintColor));
}
// where a model's uv in pixels (0-16) is in the atlas. the atlas's border around each sprite (see ATLAS_BORDER) keeps
// lookups that round past its edge on its own pixels
export function sprite_u(atlas: Texture, region: SpriteRegion, pixels: number) {
+33 -16
View File
@@ -15,7 +15,15 @@ import {
ITEM_FACE_SHADE,
item_tint,
} from "$/common/block_models.ts";
import { display_matrix, item_block, item_model_of, type ItemModel, sprite_u, sprite_v } from "./item_renderer.ts";
import {
display_matrix,
item_block,
item_layers,
item_model_of,
type ItemModel,
sprite_u,
sprite_v,
} from "./item_renderer.ts";
import {
draw_rect,
draw_text,
@@ -77,7 +85,8 @@ export function draw_nine_slice(
}
}
export function draw_item(item: ItemStack, x: number, y: number) {
// show_count is for drawing an item where its count means nothing, like one shown on a screen
export function draw_item(item: ItemStack, x: number, y: number, show_count = item.max_amount !== 1) {
const item_info = EverythingRegistry.get<ItemRegistry>("items", item.type_id);
if (!item_info) {
@@ -95,7 +104,7 @@ export function draw_item(item: ItemStack, x: number, y: number) {
draw_item_item(item, item_info, x, y);
}
if (item.max_amount !== 1) {
if (show_count) {
draw_text(
String(item.amount),
x + SLOT_SIZE + 2 - 11 * 2,
@@ -128,19 +137,27 @@ export function draw_item(item: ItemStack, x: number, y: number) {
// ey its not a bad name
function draw_item_item(item: ItemStack, item_info: ItemRegistry, x: number, y: number) {
const texture_id = item_texture(item, item_info);
const sprite_region = get_sprite_region(texture_id);
draw_texture_region(
AssetManager.instance.get("bworld:textures"),
sprite_region.x,
sprite_region.y,
sprite_region.width,
sprite_region.height,
x + 3,
y + 3,
SLOT_SIZE - 6,
SLOT_SIZE - 6,
);
const draw = (texture_id: string, [r, g, b] = [1, 1, 1]) => {
const sprite_region = get_sprite_region(texture_id);
draw_texture_region(
AssetManager.instance.get("bworld:textures"),
sprite_region.x,
sprite_region.y,
sprite_region.width,
sprite_region.height,
x + 3,
y + 3,
SLOT_SIZE - 6,
SLOT_SIZE - 6,
false,
false,
r,
g,
b,
);
};
draw(item_texture(item, item_info));
for (const layer of item_layers(item)) draw(layer.texture, layer.color);
}
// from a model's points to a slot, as screen x and y and depth toward the viewer. with a gui transform, the transform