Waterlogging

This commit is contained in:
2026-10-07 20:56:24 -03:00
parent 78117f65c9
commit 757ee74bc7
26 changed files with 459 additions and 73 deletions
+13 -7
View File
@@ -4,7 +4,7 @@ import type { Client } from "./client.ts";
import type { BlockHitResult } from "./level/client_level.ts";
import type { Container } from "$/common/inventory.ts";
import { AIR_ID } from "$/common/protocol.ts";
import { box_overlaps_block, can_place, placement_target } from "$/common/utils.ts";
import { box_overlaps_block, can_place, placement_target, waterlog_placement } from "$/common/utils.ts";
import { block_rules, predict_neighbors } from "./mods.ts";
import { collision_shape_at } from "$/common/block_shapes.ts";
import type { EntityJson } from "$/common/mod_data.ts";
@@ -81,16 +81,22 @@ 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
// with the states the server will give it, see BlockRules.placement_value, and holding the liquid it goes into
const value = place_block &&
block_rules.placement_value(place_block, this.client.player.yaw, {
...target,
face: hit.face,
hit: hit.hit,
});
waterlog_placement(
block_rules.placement_value(place_block, this.client.player.yaw, {
...target,
face: hit.face,
hit: hit.hit,
}),
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(
place_block,
current === AIR ? undefined : EverythingRegistry.get_by_id<BlockRegistry>("blocks", current),
level.get_block_value(target.x, target.y, target.z),
below_id,
) && !(place_block?.has_collision && this.#occupied(target.x, target.y, target.z, place_block, value!));
if (!block_info.interactive && place_id && placeable) {
+24 -8
View File
@@ -1,4 +1,11 @@
import { block_value, chunk_key, default_block_value, liquid_in_cell } from "$/common/utils.ts";
import {
block_value,
chunk_key,
default_block_value,
is_waterlogged,
liquid_block,
liquid_in_cell,
} from "$/common/utils.ts";
import { AIR_ID } from "$/common/protocol.ts";
import { decode_level_chunk } from "$/common/chunk_streaming.ts";
import { DEFAULT_WATER_COLOR, foliage_color, grass_color, water_color } from "$/common/worldgen/biome_colors.ts";
@@ -340,10 +347,11 @@ export class ClientLevel {
return nid === VOID || nid === AIR ? undefined : liquid_in_cell(value, this.#blocks[nid]);
}
// water, see is_liquid_block. unloaded chunks aren't
// water, or a block waterlogged with it, see liquid_block. unloaded chunks aren't
is_liquid(x: number, y: number, z: number) {
const block = this.get_block(x, y, z);
return block !== VOID && block !== AIR && is_liquid_block(this.#blocks[block]);
const value = this.get_block_value(x, y, z);
const block = value & ID_MASK;
return block !== VOID && block !== AIR && liquid_block(value, this.#blocks[block]) !== undefined;
}
// only changes what this client shows, drops and everything else happen on the server
@@ -360,10 +368,16 @@ export class ClientLevel {
const ly = y;
const index = ly * CHUNK_SIZE * CHUNK_SIZE + lz * CHUNK_SIZE + lx;
const old_nid = chunk.blocks[index] & ID_MASK;
chunk.blocks[index] = AIR;
const old_value = chunk.blocks[index];
const old_nid = old_value & ID_MASK;
const old_block = this.#blocks[old_nid];
// a waterlogged block leaves its liquid, like the server's destroy_block
const liquid = is_waterlogged(old_value, old_block)
? EverythingRegistry.get_id("blocks", old_block!.waterloggable!) ?? AIR
: AIR;
chunk.blocks[index] = liquid;
this.#mark_dirty(chunk);
this.#mark_neighbors_dirty(block_chunk_x, block_chunk_z, lx, lz, old_nid, AIR);
this.#mark_neighbors_dirty(block_chunk_x, block_chunk_z, lx, lz, old_nid, liquid);
}
// a block that lets through or gives off a different amount of light changes the light up to 15 blocks
@@ -714,8 +728,10 @@ export class ClientLevel {
if (!liquids && is_liquid_block(info)) {
continue;
}
// aimed at liquids, the source in a waterlogged block covers it, like the server's raycast_shape
const covered = liquids && is_waterlogged(value, info);
const hit = clip_shape(
info ? shape_at(info, value) : FULL_SHAPE,
!info || covered ? FULL_SHAPE : shape_at(info, value),
bx,
by,
bz,
+45 -13
View File
@@ -97,6 +97,9 @@ const light_tables: LightTables = { opacity: new Uint8Array(TABLE_SIZE), emissio
// liquids: their range, 0 for blocks that aren't one, and where their level state is in the state bits
const liquid_range = new Uint8Array(TABLE_SIZE);
const liquid_level_shift = new Uint8Array(TABLE_SIZE);
// waterloggable blocks: the numeric id of the liquid they hold, 0 for other blocks, and where their waterlogged state is
const waterlog_liquid = new Uint16Array(TABLE_SIZE);
const waterlogged_shift = new Uint8Array(TABLE_SIZE);
const region = new LightRegion();
// a model's quad ready for the mesher: uvs in the atlas, and for each corner how much of each of the face's
@@ -259,6 +262,10 @@ function quad_tint(block: BlockRegistry, variables: string[]) {
}
function build_block_tables() {
// the registry has gaps, which forEach skips
const nids = new Map<string, number>();
blocks_registry.forEach((block, nid) => block && nids.set(block.id, nid));
waterlog_liquid.fill(0);
blocks_registry.forEach((block, nid) => {
if (!block || nid === AIR) return;
const layer = LAYER_IDS[block.render_layer ?? "solid"];
@@ -274,6 +281,11 @@ function build_block_tables() {
// the level state is the last one, see block_from_json
liquid_level_shift[nid] = (block.states ?? []).slice(0, -1).reduce((bits, state) => bits + state.bits, 0);
}
if (block.waterloggable) {
waterlog_liquid[nid] = nids.get(block.waterloggable) ?? 0;
// the waterlogged state is the last one, see block_from_json
waterlogged_shift[nid] = (block.states ?? []).slice(0, -1).reduce((bits, state) => bits + state.bits, 0);
}
});
block_lets_light_by[AIR] = 1;
block_layers[REGION_VOID] = SOLID;
@@ -543,18 +555,27 @@ const liquid_cells: LiquidCell[] = Array.from(
() => ({ same: false, height: 0, air: false, solid: false, top_same: false }),
);
// the 3x3 cells around the liquid at index and y, see LiquidCell
// the numeric id of the liquid in a cell: the block, or what it's waterlogged with. 0 for none
function cell_liquid(chunks: (Uint32Array | null)[], index: number, y: number) {
const id = region_block(region, index, y);
if (liquid_range[id]) return id;
const liquid = waterlog_liquid[id];
if (!liquid) return 0;
return ((region_value(chunks, index, y) >>> 16) >>> waterlogged_shift[id]) & 1 ? liquid : 0;
}
// the 3x3 cells around the liquid at index and y, see LiquidCell. a waterlogged block is a source of its liquid
function fill_liquid_cells(chunks: (Uint32Array | null)[], nid: number, index: number, y: number) {
for (let dz = -1; dz <= 1; dz++) {
for (let dx = -1; dx <= 1; dx++) {
const cell = liquid_cells[cell_index(dx, dz)];
const at = index + dx + dz * REGION_SIZE;
const id = region_block(region, at, y);
cell.same = id === nid;
cell.height = cell.same ? liquid_cell_height(nid, region_value(chunks, at, y)) : 0;
cell.same = cell_liquid(chunks, at, y) === nid;
cell.height = !cell.same ? 0 : id === nid ? liquid_cell_height(nid, region_value(chunks, at, y)) : 1;
cell.air = id === AIR;
cell.solid = hides_liquid(id);
cell.top_same = region_block(region, at + REGION_LAYER, y + 1) === nid;
cell.top_same = cell_liquid(chunks, at + REGION_LAYER, y + 1) === nid;
}
}
return liquid_cells;
@@ -675,25 +696,36 @@ function make_chunk_mesh(chunk_x: number, chunk_z: number, chunks: (Uint32Array
const layer_id = block_layers[block_nid];
const alpha = layer_id === TRANSLUCENT ? block_info.alpha ?? 1 : 1;
if (liquid_range[block_nid]) {
const cells = fill_liquid_cells(chunks, block_nid, index, y);
// the liquid here, the block's own or what it's waterlogged with, then the block in it
const liquid_nid = cell_liquid(chunks, index, y);
if (liquid_nid) {
const liquid_info = blocks_registry[liquid_nid];
const liquid_layer = block_layers[liquid_nid];
const liquid_alpha = liquid_layer === TRANSLUCENT ? liquid_info.alpha ?? 1 : 1;
const cells = fill_liquid_cells(chunks, liquid_nid, index, y);
const above = region_block(region, index + REGION_LAYER, y + 1);
const below = region_block(region, index - REGION_LAYER, y - 1);
const quads = liquid_quads(
cells,
{ same: above === block_nid, solid: hides_liquid(above) },
{ same: below === block_nid, solid: hides_liquid(below) },
flowing_scale(block_info),
{
same: cell_liquid(chunks, index + REGION_LAYER, y + 1) === liquid_nid,
solid: hides_liquid(above),
},
{
same: cell_liquid(chunks, index - REGION_LAYER, y - 1) === liquid_nid,
solid: hides_liquid(below),
},
flowing_scale(liquid_info),
);
for (const { face, positions, uvs, still } of quads) {
const quad = liquid_mesh_quad(block_info, face, positions, uvs, still);
const quad = liquid_mesh_quad(liquid_info, face, positions, uvs, still);
light_quad(quad, index, y, face_offsets);
// like minecraft's liquids, no ambient occlusion
corner_ao.fill(1);
emit(quad, layer_id, x, y, z, alpha);
if (face === 0) emit(underside(quad), layer_id, x, y, z, alpha);
emit(quad, liquid_layer, x, y, z, liquid_alpha);
if (face === 0) emit(underside(quad), liquid_layer, x, y, z, liquid_alpha);
}
continue;
if (liquid_nid === block_nid) continue;
}
for (const quad of block_quads(values[y * CHUNK_AREA + z * CHUNK_SIZE + x])) {