diff --git a/client/game_mode.ts b/client/game_mode.ts index ea53db4..bc79332 100644 --- a/client/game_mode.ts +++ b/client/game_mode.ts @@ -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("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("blocks", current), + ); const placeable = place_id !== undefined && can_place( place_block, current === AIR ? undefined : EverythingRegistry.get_by_id("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) { diff --git a/client/level/client_level.ts b/client/level/client_level.ts index df79f0c..09d7b21 100644 --- a/client/level/client_level.ts +++ b/client/level/client_level.ts @@ -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, diff --git a/client/workers/chunk_worker.ts b/client/workers/chunk_worker.ts index b8b01b9..e718864 100644 --- a/client/workers/chunk_worker.ts +++ b/client/workers/chunk_worker.ts @@ -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(); + 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])) { diff --git a/common/everything_registry.ts b/common/everything_registry.ts index 3443cde..c1d3877 100644 --- a/common/everything_registry.ts +++ b/common/everything_registry.ts @@ -155,6 +155,10 @@ export interface BlockRegistry { item_display?: { surface: number }; // a liquid like water, drawn like luanti's: its top slopes down towards where it flows. see LiquidProperties liquid?: LiquidProperties; + // the liquid it can hold, like minecraft's waterlogged fences. it gets a "waterlogged" state, see WATERLOGGED + waterloggable?: string; + // it can only be placed into a source of that liquid, so it's always placed holding it, like farmer's delight's rice + waterlogged_only?: boolean; compiled_states?: CompiledStateDefinition[]; } @@ -173,6 +177,9 @@ export interface LiquidProperties { } export const LIQUID_LEVEL: BlockStateDefinition = { name: "level", bits: 3, default: 0 }; +// a waterloggable block's state, 1 while it holds a source of its liquid. blocks get it without listing it, after +// their own states +export const WATERLOGGED: BlockStateDefinition = { name: "waterlogged", bits: 1, default: 0 }; export const MAX_LIQUID_RANGE = 7; // what a liquid that doesn't say has export const LIQUID_DEFAULTS: LiquidProperties = { range: MAX_LIQUID_RANGE, ticks: 5, renewable: true }; diff --git a/common/mod_check.ts b/common/mod_check.ts index 2339965..b72273e 100644 --- a/common/mod_check.ts +++ b/common/mod_check.ts @@ -86,6 +86,7 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable(); const effect_owners = new Map(); const tag_owners = new Map>(); + const liquids = new Set(); const add = (map: Map, id: string, mod: string) => map.set(id, [...(map.get(id) ?? []), mod]); const textures = new Set(engine_textures); @@ -94,6 +95,7 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable; + // the liquid it can hold, see BlockRegistry.waterloggable + waterloggable?: string; + // only placed into a source of it, see BlockRegistry.waterlogged_only + waterlogged_only?: boolean; } export interface OreJson { @@ -301,9 +306,12 @@ export function block_to_json(block: BlockRegistry, has_item: boolean): BlockJso if (!!block.replaceable !== !!block.liquid) json.replaceable = !!block.replaceable; if (block.place_on) json.place_on = block.place_on; if (block.place_in) json.place_in = block.place_in; - // a liquid's level state is its own, added when it loads - const states = block.liquid ? block.states?.slice(0, -1) : block.states; - if (states && (states.length > 0 || !block.liquid)) json.states = states; + // a liquid's level state and a waterloggable block's waterlogged state are its own, added when it loads + const added = block.liquid || block.waterloggable; + const states = added ? block.states?.slice(0, -1) : block.states; + if (states && (states.length > 0 || !added)) json.states = states; + if (block.waterloggable) json.waterloggable = block.waterloggable; + if (block.waterlogged_only) json.waterlogged_only = true; if (block.components) json.components = block.components; if (block.item_display) json.item_display = block.item_display; if (block.liquid) { @@ -355,6 +363,12 @@ export function block_from_json(json: BlockJson): { block: BlockRegistry; has_it // last, so the block's own states keep their bits block.states = [...block.states ?? [], { ...LIQUID_LEVEL }]; } + if (json.waterlogged_only) block.waterlogged_only = true; + if (json.waterloggable) { + block.waterloggable = json.waterloggable; + // last too, see WATERLOGGED + block.states = [...block.states ?? [], { ...WATERLOGGED }]; + } return { block, has_item: json.item ?? true }; } @@ -669,6 +683,23 @@ export function validate_block(json: unknown): Problems { } else { const bits = states.reduce((sum, s) => sum + (is_object(s) && typeof s.bits === "number" ? s.bits : 0), 0); if (bits > 16) problems.push(`states use ${bits} bits, the most is 16`); + if (json.waterloggable !== undefined && bits + WATERLOGGED.bits > 16) { + problems.push(`its states and waterlogged use more than 16 bits`); + } + } + } + if (json.waterloggable !== undefined) { + problems.push(...validate_id(json.waterloggable, "waterloggable")); + if (json.liquid !== undefined) problems.push("a liquid can't be waterloggable"); + const states = Array.isArray(json.states) ? json.states : []; + if (states.some((state) => is_object(state) && state.name === WATERLOGGED.name)) { + problems.push(`a waterloggable block gets a "${WATERLOGGED.name}" state of its own`); + } + } + if (json.waterlogged_only !== undefined) { + if (typeof json.waterlogged_only !== "boolean") problems.push("waterlogged_only must be true or false"); + else if (json.waterlogged_only && json.waterloggable === undefined) { + problems.push("waterlogged_only needs waterloggable, the liquid it's placed into"); } } return problems; diff --git a/common/physics.ts b/common/physics.ts index 215a600..ac1dc6e 100644 --- a/common/physics.ts +++ b/common/physics.ts @@ -246,8 +246,8 @@ export function eyes_in_water( // a liquid in a cell, for liquids pushing things along export interface LiquidInCell { - // which liquid, its block's numeric id. a liquid's flow only goes by cells of the same one - id: number; + // which liquid, its block's id. a liquid's flow only goes by cells of the same one + id: string; // its surface, 0 to 1 (see liquid_height), not counting more of it above height: number; } diff --git a/common/utils.ts b/common/utils.ts index c2b6b85..cf167c4 100644 --- a/common/utils.ts +++ b/common/utils.ts @@ -152,17 +152,23 @@ export function placement_target( return clicked?.replaceable ? { x, y, z } : { x: x + offset.x, y: y + offset.y, z: z + offset.z }; } -// whether a player can place block where current is now, with below under it. current is undefined for air +// whether a player can place block where current is now, with below under it. current is undefined for air, +// current_value is its value with its states export function can_place( block: BlockRegistry | undefined, current: BlockRegistry | undefined, + current_value: number, below: string, ): boolean { if (!block) return false; const into = current?.id ?? "bworld:air"; const replaceable = !current || current.replaceable === true; const in_ok = block.place_in ? list_matches("blocks", block.place_in, into) : replaceable; - return in_ok && can_stand_on(block, below); + // one that's only ever waterlogged goes into a source of its liquid and nothing else + const source_ok = !block.waterlogged_only || + (current !== undefined && current.id === block.waterloggable && + (get_state_value(current_value, current, "level") ?? 0) === 0); + return in_ok && source_ok && can_stand_on(block, below); } // whether an entity's collision box, centered on x and z with its feet at y, overlaps the block at bx, by, bz. only @@ -187,10 +193,39 @@ export function chunk_key(x: number, z: number) { return (x + 32768) * 65536 + (z + 32768); } -// the liquid a block value is, for liquids pushing things (see push_by_liquid in physics.ts), or undefined for blocks -// that aren't liquids -export function liquid_in_cell(value: number, block: BlockRegistry | undefined): LiquidInCell | undefined { - if (!block?.liquid) return undefined; - const level = get_state_value(value, block, "level") ?? 0; - return { id: value & ID_MASK, height: liquid_height(level, block.liquid.range) }; +// whether a block value is a waterloggable block holding its liquid +export function is_waterlogged(value: number, block: BlockRegistry | undefined): boolean { + return block?.waterloggable !== undefined && get_state_value(value, block, "waterlogged") === 1; +} + +// the liquid in a cell with this block value: the block itself, or the liquid it's waterlogged with. undefined for +// none. what entities swim in, see in_water in physics.ts +export function liquid_block(value: number, block: BlockRegistry | undefined): BlockRegistry | undefined { + if (block?.liquid) return block; + if (!is_waterlogged(value, block)) return undefined; + return EverythingRegistry.get("blocks", block!.waterloggable!); +} + +// the liquid in a cell and how high it is, for liquids pushing things (see push_by_liquid in physics.ts). a waterlogged +// block holds a source +export function liquid_in_cell(value: number, block: BlockRegistry | undefined): LiquidInCell | undefined { + if (block?.liquid) { + const level = get_state_value(value, block, "level") ?? 0; + return { id: block.id, height: liquid_height(level, block.liquid.range) }; + } + const liquid = liquid_block(value, block); + return liquid && { id: liquid.id, height: 1 }; +} + +// a waterloggable block placed where a source of its liquid is holds it, like minecraft's getStateForPlacement. +// value is what it's placed as, current_value and current what's there now +export function waterlog_placement( + value: number, + block: BlockRegistry, + current_value: number, + current: BlockRegistry | undefined, +): number { + if (!block.waterloggable || current?.id !== block.waterloggable) return value; + if ((get_state_value(current_value, current, "level") ?? 0) !== 0) return value; + return set_state_value(value, block, "waterlogged", 1)!; } diff --git a/docs/modding/blocks/index.md b/docs/modding/blocks/index.md index 29da269..50d3e36 100644 --- a/docs/modding/blocks/index.md +++ b/docs/modding/blocks/index.md @@ -150,6 +150,28 @@ So it spreads one block a step, falls off edges and spreads again where it lands its source is gone. It only flows into air, and stops at chunks that aren't loaded. Buckets only pick up sources. Flows still going when the server stops don't carry on after it starts again until something next to them changes. +### Waterlogging + +A block that isn't a liquid can hold one, like Minecraft's waterlogged fences. `waterloggable` names the liquid: + +```json +"waterloggable": "bworld:water" +``` + +- It gets a `waterlogged` [state](states.md) of its own, after any it lists: `1` while it holds a source of the liquid. + It uses one of the block's 16 bits. +- Placed into a source of the liquid, it holds it. Placed into flowing liquid or anything else, it doesn't. With + `"waterlogged_only": true` it can only be placed into a source, so it's always placed holding it, like Farmer's + Delight's rice. +- Using the liquid's item on it, like a water bucket, fills it. An item that aims at liquids, like a bucket, uses the + liquid in it instead of the block, so a bucket takes the water back out and the block stays. +- It's a source of the liquid that never drains: the liquid flows out of it, and entities swim and are pushed in it. + Flowing liquid doesn't flow into it. +- Broken, it leaves a source of the liquid behind. +- Raycasts with `liquids` other than `"none"` hit the liquid in it, filling the block, and say the liquid's id. + +The base game's wooden fences are waterloggable, and Farmer's Delight's rice is `waterlogged_only`. + ## Tinting `tint` draws a block's textures in a color, for textures painted in greys: diff --git a/mods/bworld/blocks/acacia_fence.json b/mods/bworld/blocks/acacia_fence.json index 7389396..d5cf7b0 100644 --- a/mods/bworld/blocks/acacia_fence.json +++ b/mods/bworld/blocks/acacia_fence.json @@ -9,6 +9,7 @@ }, "item_model": "bworld:fence_inventory", "render_layer": "cutout", + "waterloggable": "bworld:water", "states": [ { "name": "north", diff --git a/mods/bworld/blocks/birch_fence.json b/mods/bworld/blocks/birch_fence.json index 7444ec8..00e04d4 100644 --- a/mods/bworld/blocks/birch_fence.json +++ b/mods/bworld/blocks/birch_fence.json @@ -9,6 +9,7 @@ }, "item_model": "bworld:fence_inventory", "render_layer": "cutout", + "waterloggable": "bworld:water", "states": [ { "name": "north", diff --git a/mods/bworld/blocks/cherry_fence.json b/mods/bworld/blocks/cherry_fence.json index 323a895..10f84ef 100644 --- a/mods/bworld/blocks/cherry_fence.json +++ b/mods/bworld/blocks/cherry_fence.json @@ -9,6 +9,7 @@ }, "item_model": "bworld:fence_inventory", "render_layer": "cutout", + "waterloggable": "bworld:water", "states": [ { "name": "north", diff --git a/mods/bworld/blocks/dark_oak_fence.json b/mods/bworld/blocks/dark_oak_fence.json index 6effa16..9e3f5ee 100644 --- a/mods/bworld/blocks/dark_oak_fence.json +++ b/mods/bworld/blocks/dark_oak_fence.json @@ -9,6 +9,7 @@ }, "item_model": "bworld:fence_inventory", "render_layer": "cutout", + "waterloggable": "bworld:water", "states": [ { "name": "north", diff --git a/mods/bworld/blocks/jungle_fence.json b/mods/bworld/blocks/jungle_fence.json index a9bb990..477f12e 100644 --- a/mods/bworld/blocks/jungle_fence.json +++ b/mods/bworld/blocks/jungle_fence.json @@ -9,6 +9,7 @@ }, "item_model": "bworld:fence_inventory", "render_layer": "cutout", + "waterloggable": "bworld:water", "states": [ { "name": "north", diff --git a/mods/bworld/blocks/oak_fence.json b/mods/bworld/blocks/oak_fence.json index a61ea20..e5eeec1 100644 --- a/mods/bworld/blocks/oak_fence.json +++ b/mods/bworld/blocks/oak_fence.json @@ -9,6 +9,7 @@ }, "item_model": "bworld:fence_inventory", "render_layer": "cutout", + "waterloggable": "bworld:water", "states": [ { "name": "north", diff --git a/mods/bworld/blocks/spruce_fence.json b/mods/bworld/blocks/spruce_fence.json index 3d58b4d..13f7a12 100644 --- a/mods/bworld/blocks/spruce_fence.json +++ b/mods/bworld/blocks/spruce_fence.json @@ -9,6 +9,7 @@ }, "item_model": "bworld:fence_inventory", "render_layer": "cutout", + "waterloggable": "bworld:water", "states": [ { "name": "north", diff --git a/mods/bworld/scripts/server.ts b/mods/bworld/scripts/server.ts index 3e75eef..1bca76a 100644 --- a/mods/bworld/scripts/server.ts +++ b/mods/bworld/scripts/server.ts @@ -595,9 +595,15 @@ function register_buckets(ctx: ServerContext) { on_interact(block, params, player) { const bucket = player.held_item; if (bucket?.id !== BUCKET) return false; - // only a liquid's source fills it, not where it's flowing - if ((ctx.world.get_state(block.x, block.y, block.z, "level") ?? 0) !== 0) return false; - ctx.world.set_block(block.x, block.y, block.z, "bworld:air"); + const { x, y, z } = block; + if (ctx.world.get_block(x, y, z) === block.id) { + // only a liquid's source fills it, not where it's flowing + if ((ctx.world.get_state(x, y, z, "level") ?? 0) !== 0) return false; + ctx.world.set_block(x, y, z, "bworld:air"); + } else { + // the liquid in a waterlogged block, like a fence in water, which stays + ctx.world.set_state(x, y, z, "waterlogged", 0); + } bucket.count -= 1; // the last one becomes the filled bucket in the hand, from a stack it goes in the inventory if (bucket.count <= 0) player.inventory.set(player.selected_slot, { id: params.into, count: 1 }); diff --git a/mods/farmersdelight/blocks/rice.json b/mods/farmersdelight/blocks/rice.json index b622888..5b4aec9 100644 --- a/mods/farmersdelight/blocks/rice.json +++ b/mods/farmersdelight/blocks/rice.json @@ -34,6 +34,8 @@ } }, "render_layer": "cutout", + "waterloggable": "bworld:water", + "waterlogged_only": true, "collision": false, "mining": { "toughness": 0 diff --git a/server/game/entity/entity_manager.ts b/server/game/entity/entity_manager.ts index a6c6d9b..de07ef7 100644 --- a/server/game/entity/entity_manager.ts +++ b/server/game/entity/entity_manager.ts @@ -3,8 +3,7 @@ // when those move or change, and forgets them when they leave it import { CHUNK_SIZE } from "$/common/constants.ts"; import { is_within_view } from "$/common/chunk_streaming.ts"; -import { is_liquid_block } from "$/common/everything_registry.ts"; -import { liquid_in_cell } from "$/common/utils.ts"; +import { liquid_block, liquid_in_cell } from "$/common/utils.ts"; import type { ItemStack } from "$/common/inventory.ts"; import type { EntityEvent, EntityInfo, ServerMessage } from "$/common/protocol.ts"; import type { ServerPlayer } from "../player.ts"; @@ -95,7 +94,8 @@ export class EntityManager implements EntityLevel { } is_liquid(x: number, y: number, z: number) { - return is_liquid_block(this.#host.world.get_block_info(x, y, z)); + const world = this.#host.world; + return liquid_block(world.get_block_value(x, y, z), world.get_block_info(x, y, z)) !== undefined; } get_block_info(x: number, y: number, z: number) { diff --git a/server/game/game_server.ts b/server/game/game_server.ts index f55c1fb..1c67109 100644 --- a/server/game/game_server.ts +++ b/server/game/game_server.ts @@ -8,7 +8,7 @@ import { TICK_DELTA, TICKS_PER_SECOND, } from "$/common/constants.ts"; -import { BlockRegistry, EverythingRegistry, is_liquid_block, ItemRegistry } from "$/common/everything_registry.ts"; +import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts"; import { LiquidFlow } from "./liquids.ts"; import { Container, ItemData, ItemStack } from "$/common/inventory.ts"; import { @@ -23,7 +23,17 @@ import type { ModListing, RecipeBook } from "$/common/mod_loader.ts"; import type { WorldgenSetup } from "$/common/generation.ts"; import { MAX_VIEW_DISTANCE, MIN_VIEW_DISTANCE } from "$/common/chunk_streaming.ts"; import { CYCLE_TICKS, format_clock, time_of_day, TIMES_OF_DAY } from "$/common/time.ts"; -import { box_overlaps_block, can_place, can_stand_on, display_name, placement_target } from "$/common/utils.ts"; +import { + box_overlaps_block, + can_place, + can_stand_on, + display_name, + is_waterlogged, + liquid_block, + placement_target, + set_state_value, + waterlog_placement, +} from "$/common/utils.ts"; import { collision_shape_at, type Shape } from "$/common/block_shapes.ts"; import { EXHAUSTION } from "./vitals.ts"; import { ModRuntime, run_guarded, type ServerCommandSource } from "./mod_runtime.ts"; @@ -611,7 +621,10 @@ export class GameServer { const tool = held?.tool; const right_tool = tool !== undefined && tool.type === info.tool_to_break && tool.tier >= (info.tool_tier ?? 0); const drops = info.drop_table && (!info.requires_tool || right_tool); - this.set_block(x, y, z, AIR_ID, player); + // a waterlogged block leaves its liquid behind, a source + const value = this.world.get_block_value(x, y, z); + const liquid = is_waterlogged(value, info) ? liquid_block(value, info) : undefined; + this.set_block(x, y, z, liquid?.id ?? AIR_ID, player); if (drops) { this.pop_item(x, y, z, new ItemStack(info.drop_table!)); } @@ -985,7 +998,12 @@ export class GameServer { } const player_api = this.mods.player(player); - const block = this.mods.block_ref(x, y, z, info.id); + const value = this.world.get_block_value(x, y, z); + const held_slot = player.inventory.get_slot(player.selected_slot); + const held = EverythingRegistry.get("items", held_slot.type_id ?? ""); + // an item aimed at liquids, like a bucket, uses the liquid in a waterlogged block instead of the block + const used = (held?.targets_liquids && liquid_block(value, info)) || info; + const block = this.mods.block_ref(x, y, z, used.id); const interact = { player: player_api, block, item: player_api.held_item }; const before_interact = { ...interact, cancel: false }; this.mods.before.block_interact.emit(before_interact); @@ -995,7 +1013,7 @@ export class GameServer { } let handled = false; - for (const { mod, id: component_id, component, params } of this.mods.components_of(info.id)) { + for (const { mod, id: component_id, component, params } of this.mods.components_of(used.id)) { if (component.on_interact) { handled = run_guarded(mod, `${component_id} on_interact`, () => component.on_interact!(block, params, player_api)) === @@ -1015,14 +1033,19 @@ export class GameServer { return; } + // placing a liquid, like a water bucket, into a block that can hold it fills it, like minecraft's + if (held?.block_id && info.waterloggable === held.block_id && !is_waterlogged(value, info)) { + this.#waterlog(player, x, y, z, face, hit); + return; + } + // place the held block against the face, or into the clicked block if it's replaceable. food that can be planted, // like a potato on farmland, is planted, and eaten when it can't be - const held_slot = player.inventory.get_slot(player.selected_slot); - const held = EverythingRegistry.get("items", held_slot.type_id ?? ""); const placeable = held?.block_id !== undefined && ty >= 0 && ty < CHUNK_HEIGHT && can_place( EverythingRegistry.get("blocks", held.block_id), this.world.get_block_info(tx, ty, tz), + this.world.get_block_value(tx, ty, tz), this.world.get_block_id(tx, ty - 1, tz), ); if (!placeable || !held?.block_id) { @@ -1032,15 +1055,21 @@ 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("blocks", held.block_id)!; - const value = this.mods.rules.placement_value(placing, player.yaw, { - x: tx, - y: ty, - z: tz, - face, - hit: [hit[0], hit[1], hit[2]], - }); - const state = value >>> 16; - if (placing?.has_collision && this.#occupied(tx, ty, tz, collision_shape_at(placing, value))) { + // 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, { + x: tx, + y: ty, + z: tz, + face, + hit: [hit[0], hit[1], hit[2]], + }), + placing, + this.world.get_block_value(tx, ty, tz), + this.world.get_block_info(tx, ty, tz), + ); + const state = placed_value >>> 16; + if (placing?.has_collision && this.#occupied(tx, ty, tz, collision_shape_at(placing, placed_value))) { this.#correct(player, tx, ty, tz); return; } @@ -1064,6 +1093,35 @@ export class GameServer { this.mods.after.block_place.emit(place); } + // the held liquid poured into the block at x y z, which can hold it. it's placing the liquid as far as mods can tell, + // so a bucket empties like it does placing water anywhere else + #waterlog(player: ServerPlayer, x: number, y: number, z: number, face: Faces, hit: number[]) { + const held_slot = player.inventory.get_slot(player.selected_slot); + const player_api = this.mods.player(player); + const place = { + player: player_api, + block: { id: this.world.get_block_info(x, y, z)!.waterloggable!, x, y, z }, + face, + hit: [hit[0], hit[1], hit[2]] as const, + item: player_api.held_item, + }; + const before_place = { ...place, cancel: false }; + this.mods.before.block_place.emit(before_place); + if (before_place.cancel) { + this.#correct(player, x, y, z); + return; + } + const info = this.world.get_block_info(x, y, z)!; + this.set_block_state( + x, + y, + z, + set_state_value(this.world.get_block_value(x, y, z), info, "waterlogged", 1)! >>> 16, + ); + held_slot.amount = held_slot.amount! - 1; + this.mods.after.block_place.emit(place); + } + // whether a mob's or player's collision box overlaps a block with that shape at x, y, z #occupied(x: number, y: number, z: number, shape: Shape) { return this.players().some((player) => @@ -1614,9 +1672,11 @@ export class GameServer { return false; } - // in a block entities pass through and players can place into, like water + // in liquid, a waterlogged block's too #in_liquid(x: number, y: number, z: number) { - return is_liquid_block(this.world.get_block_info(Math.floor(x), Math.floor(y), Math.floor(z))); + const [bx, by, bz] = [Math.floor(x), Math.floor(y), Math.floor(z)]; + return liquid_block(this.world.get_block_value(bx, by, bz), this.world.get_block_info(bx, by, bz)) !== + undefined; } // right click with food: it's eaten if they're hungry, or it's always edible diff --git a/server/game/liquids.ts b/server/game/liquids.ts index b10e9b4..c4e913e 100644 --- a/server/game/liquids.ts +++ b/server/game/liquids.ts @@ -4,7 +4,7 @@ import { AIR, CHUNK_HEIGHT, ID_MASK } from "$/common/constants.ts"; import type { BlockRegistry } from "$/common/everything_registry.ts"; import { AIR_ID } from "$/common/protocol.ts"; -import { get_state_value, set_state_value } from "$/common/utils.ts"; +import { get_state_value, liquid_block, set_state_value } from "$/common/utils.ts"; // what the flow needs of the world export interface LiquidWorld { @@ -154,8 +154,10 @@ export class LiquidFlow { if (value === undefined) return undefined; const nid = value & ID_MASK; const block = this.#world.block(nid); - if (!block?.liquid) return undefined; - return { nid, block, level: get_state_value(value, block, "level")! }; + if (block?.liquid) return { nid, block, level: get_state_value(value, block, "level")! }; + // a waterlogged block is a source of its liquid that never flows away, like minecraft's + const liquid = liquid_block(value, block); + return liquid && { nid: this.#world.nid(liquid.id), block: liquid, level: 0 }; } #set(block: BlockRegistry, x: number, y: number, z: number, level: number) { diff --git a/server/game/mod_runtime.ts b/server/game/mod_runtime.ts index 20870cc..96110fa 100644 --- a/server/game/mod_runtime.ts +++ b/server/game/mod_runtime.ts @@ -2,7 +2,7 @@ // see docs/modding/. parts not built yet throw when used, saying so import { BlockRegistry, EverythingRegistry, ItemRegistry, liquid_height } from "$/common/everything_registry.ts"; import { Container as EngineContainer, ItemStack as EngineItemStack } from "$/common/inventory.ts"; -import { get_state_value, set_state_value } from "$/common/utils.ts"; +import { get_state_value, is_waterlogged, liquid_block, set_state_value } from "$/common/utils.ts"; import { collision_shape_at, covers_face, raycast_blocks, type Shape } from "$/common/block_shapes.ts"; import { AIR, ID_MASK } from "$/common/constants.ts"; import { BlockRules } from "$/common/block_rules.ts"; @@ -666,8 +666,12 @@ export class ModRuntime { (x, y, z) => raycast_shape(world, x, y, z, liquids), ); if (!hit) return undefined; + // what was hit in a waterlogged block with liquids on is its liquid, see raycast_shape + const value = world.get_block_value(hit.x, hit.y, hit.z); + const block = world.get_block_info(hit.x, hit.y, hit.z); + const liquid = liquids === "none" ? undefined : liquid_block(value, block); return { - id: world.get_block_id(hit.x, hit.y, hit.z), + id: liquid?.id ?? world.get_block_id(hit.x, hit.y, hit.z), x: hit.x, y: hit.y, z: hit.z, @@ -1099,6 +1103,8 @@ function raycast_shape( if (liquids === "none" || (liquids === "source_only" && level !== 0)) return undefined; return [[0, 0, 0, 1, liquid_height(level, block.liquid.range), 1]]; } + // the source in a waterlogged block covers the block, like minecraft's fluid clip + if (liquids !== "none" && is_waterlogged(value, block)) return [[0, 0, 0, 1, 1, 1]]; const shape = collision_shape_at(block, value); return shape.length > 0 ? shape : undefined; } diff --git a/tests/liquid_push_test.ts b/tests/liquid_push_test.ts index 769a52e..6f9af2a 100644 --- a/tests/liquid_push_test.ts +++ b/tests/liquid_push_test.ts @@ -2,7 +2,7 @@ import { assert, assertAlmostEquals, assertEquals } from "@std/assert"; import { type Body, liquid_flow, type LiquidInCell, push_by_liquid } from "$/common/physics.ts"; -const WATER = 1; +const WATER = "a:water"; // cells by "x,y,z", and solid blocks function world(liquids: Record, solid: string[] = []) { diff --git a/tests/liquid_test.ts b/tests/liquid_test.ts index 53d34ce..5347960 100644 --- a/tests/liquid_test.ts +++ b/tests/liquid_test.ts @@ -169,7 +169,8 @@ async function mesh(blocks: [number, number, number, string, number?][]) { const worker = new Worker(new URL("../client/workers/chunk_worker.ts", import.meta.url).href, { type: "module" }); worker.postMessage({ type: "init", - blocks_registry: JSON.parse(JSON.stringify(registry)), + // with the registry's gaps, like the client sends it + blocks_registry: registry.map((block) => JSON.parse(JSON.stringify(block))), models: {}, textures_info: { "engine:missing": { x: 0, y: 0, width: 16, height: 16 } }, image: { width: 1024, height: 1024 }, @@ -266,3 +267,47 @@ Deno.test("with a flowing texture of its own, a sloping top flows downhill and t assert(plain.find((q) => q.face === 0)!.still); assert(plain.some((q) => q.face !== 0 && q.uvs.some((n) => n > 8))); }); + +Deno.test("a waterloggable block gets a waterlogged state after its own, and can't be a liquid", () => { + const json: BlockJson = { + id: "a:post", + textures: "a:post", + states: [{ name: "lit", bits: 1, default: 0 }], + waterloggable: "a:water", + }; + const { block } = block_from_json(json); + assertEquals(block.states!.map((s) => s.name), ["lit", "waterlogged"]); + assertEquals(block_to_json(block, true), json); + + assertEquals(validate_block(json), []); + assert(validate_block({ ...json, waterloggable: "water" }).some((p) => p.includes("waterloggable"))); + assert(validate_block({ id: "a:w", textures: "a:w", liquid: {}, waterloggable: "a:water" }).length > 0); + assert( + validate_block({ ...json, states: [{ name: "waterlogged", bits: 1, default: 0 }] }) + .some((p) => p.includes("of its own")), + ); + // only ever placed waterlogged, which needs something to be waterlogged with + const only: BlockJson = { ...json, waterlogged_only: true }; + assertEquals(block_to_json(block_from_json(only).block, true), only); + assertEquals(validate_block(only), []); + assert(validate_block({ id: "a:post", textures: "a:post", waterlogged_only: true }).length > 0); +}); + +Deno.test("the chunk worker draws the water in a waterlogged fence, joined to the water beside it", async () => { + await test_game("mods"); + const fence = EverythingRegistry.get("blocks", "bworld:oak_fence")!; + const waterlogged = set_state_value(0, fence, "waterlogged", 1)! >>> 16; + // a fence in water at x 1 with water at x 2, and a dry fence at x 5 + const quads = await mesh([ + [1, 10, 8, "bworld:oak_fence", waterlogged], + [2, 10, 8, "bworld:water", 0], + [5, 10, 8, "bworld:oak_fence", 0], + ]); + const water_in = (x: number) => + quads.filter((corners) => corners.every(([cx]) => cx >= x - 1e-6 && cx <= x + 1 + 1e-6)); + // a full top, facing up and down, and no side between it and the water next to it + const tops = water_in(1).filter((corners) => corners.every(([, y]) => y === 11)); + assertEquals(tops.length, 2); + assert(!water_in(1).some((corners) => corners.every(([cx]) => Math.abs(cx - 2) < 1e-6))); + assertEquals(water_in(5).length, 0); +}); diff --git a/tests/local_player_test.ts b/tests/local_player_test.ts index 4ffa446..baa1e21 100644 --- a/tests/local_player_test.ts +++ b/tests/local_player_test.ts @@ -15,7 +15,8 @@ function setup(ceiling = Infinity, world?: { solid: Blocks; water: Blocks }) { collision_shape: world?.solid ?? ((_x: number, y: number) => y < 0 || y >= ceiling), is_liquid: world?.water ?? (() => false), // its water is all still sources - liquid_at: (x: number, y: number, z: number) => world?.water(x, y, z) ? { id: 1, height: 1 } : undefined, + liquid_at: (x: number, y: number, z: number) => + world?.water(x, y, z) ? { id: "a:water", height: 1 } : undefined, }, screen: undefined, options: new Options(), diff --git a/tests/waterlog_test.ts b/tests/waterlog_test.ts new file mode 100644 index 0000000..446e539 --- /dev/null +++ b/tests/waterlog_test.ts @@ -0,0 +1,100 @@ +// waterlogging, like minecraft's: a fence holds a water source, which flows out of it, can be bucketed in and out, and +// is left behind when the fence is broken +import { assert, assertEquals } from "@std/assert"; +import { type BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts"; +import { get_state_value, set_state_value } from "$/common/utils.ts"; +import { test_game } from "./helpers.ts"; + +// a player at 0.5 100 0.5 on stone, holding what they're given +async function setup(items: [string, number][] = []) { + const t = await test_game("mods"); + t.join(1, "alice"); + for (let x = -6; x <= 6; x++) { + for (let z = -6; z <= 6; z++) t.game.set_block(x, 99, z, "bworld:stone"); + } + t.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0 }); + for (const [id, count] of items) t.send(1, { type: "chat", text: `/give ${id} ${count}` }); + return t; +} + +type Game = Awaited>["game"]; + +function waterlogged(game: Game, x: number, y: number, z: number) { + const fence = EverythingRegistry.get("blocks", "bworld:oak_fence")!; + assertEquals(game.world.get_block_id(x, y, z), "bworld:oak_fence"); + return get_state_value(game.world.get_block_value(x, y, z), fence, "waterlogged"); +} + +function ticks(game: Game, count: number) { + for (let i = 0; i < count; i++) game.tick(); +} + +Deno.test("a fence placed in a water source holds it, one placed in flowing water or air doesn't", async () => { + const t = await setup([["bworld:oak_fence", 3]]); + t.game.set_block(2, 100, 0, "bworld:water"); + t.game.set_block(-2, 100, 0, "bworld:water", undefined, 3); + // water is replaceable, so clicking it places into it + t.send(1, { type: "use_block", x: 2, y: 100, z: 0, face: "top", hit: [0.5, 1, 0.5] }); + assertEquals(waterlogged(t.game, 2, 100, 0), 1); + t.send(1, { type: "use_block", x: -2, y: 100, z: 0, face: "top", hit: [0.5, 1, 0.5] }); + assertEquals(waterlogged(t.game, -2, 100, 0), 0); + t.send(1, { type: "use_block", x: 0, y: 99, z: 2, face: "top", hit: [0.5, 1, 0.5] }); + assertEquals(waterlogged(t.game, 0, 100, 2), 0); +}); + +Deno.test("a water bucket fills a fence and a bucket empties it, the fence stays", async () => { + const t = await setup([["bworld:water_bucket", 1]]); + const held = () => t.game.players()[0].held_item?.type_id; + t.game.set_block(2, 100, 0, "bworld:oak_fence"); + + t.send(1, { type: "use_block", x: 2, y: 100, z: 0, face: "top", hit: [0.5, 1, 0.5] }); + assertEquals(waterlogged(t.game, 2, 100, 0), 1); + assertEquals(held(), "bworld:bucket"); + // nothing went on top of it + assertEquals(t.game.world.get_block_id(2, 101, 0), "bworld:air"); + + t.send(1, { type: "use_block", x: 2, y: 100, z: 0, face: "top", hit: [0.5, 1, 0.5] }); + assertEquals(held(), "bworld:water_bucket"); + assertEquals(waterlogged(t.game, 2, 100, 0), 0); +}); + +Deno.test("water flows out of a waterlogged fence, which never drains, and breaking it leaves a source", async () => { + const t = await setup(); + t.game.set_block(2, 100, 0, "bworld:oak_fence"); + const fence = EverythingRegistry.get("blocks", "bworld:oak_fence")!; + const value = set_state_value(t.game.world.get_block_value(2, 100, 0), fence, "waterlogged", 1)!; + t.game.set_block_state(2, 100, 0, value >>> 16); + assertEquals(waterlogged(t.game, 2, 100, 0), 1); + assert(t.game.entities.is_liquid(2, 100, 0), "entities are in water in it"); + + ticks(t.game, 30); + assertEquals(t.game.world.get_block_id(3, 100, 0), "bworld:water"); + assertEquals(waterlogged(t.game, 2, 100, 0), 1); + + t.game.destroy_block(2, 100, 0); + assertEquals(t.game.world.get_block_id(2, 100, 0), "bworld:water"); + assertEquals(t.game.world.get_block_value(2, 100, 0) >>> 16, 0); +}); + +Deno.test("rice is only planted in a water source, holds it, and leaves it behind when broken", async () => { + const t = await setup([["farmersdelight:rice", 1]]); + for (const x of [-2, 2, 4]) t.game.set_block(x, 99, 0, "bworld:dirt"); + t.game.set_block(2, 100, 0, "bworld:water"); + // not in flowing water, or out of water + t.game.set_block(-2, 100, 0, "bworld:water", undefined, 3); + t.send(1, { type: "use_block", x: -2, y: 100, z: 0, face: "top", hit: [0.5, 1, 0.5] }); + assertEquals(t.game.world.get_block_id(-2, 100, 0), "bworld:water"); + t.send(1, { type: "use_block", x: 4, y: 99, z: 0, face: "top", hit: [0.5, 1, 0.5] }); + assertEquals(t.game.world.get_block_id(4, 100, 0), "bworld:air"); + + t.send(1, { type: "use_block", x: 2, y: 100, z: 0, face: "top", hit: [0.5, 1, 0.5] }); + assertEquals(t.game.world.get_block_id(2, 100, 0), "farmersdelight:rice"); + const rice = EverythingRegistry.get("blocks", "farmersdelight:rice")!; + assertEquals(get_state_value(t.game.world.get_block_value(2, 100, 0), rice, "waterlogged"), 1); + assert(t.game.entities.is_liquid(2, 100, 0)); + + t.game.destroy_block(2, 100, 0); + ticks(t.game, 1); + assertEquals(t.game.world.get_block_id(2, 100, 0), "bworld:water"); + assertEquals(t.game.world.get_block_value(2, 100, 0) >>> 16, 0); +});