Rename all "liquid" mentions to "fluid"
This commit is contained in:
@@ -327,8 +327,8 @@ pressing.
|
||||
slots, and simulate. Chests and furnaces get them in `mods/bworld`. Funnels, chutes, arms, deployers, belts and vaults
|
||||
all move items only through this.
|
||||
|
||||
**S2. Fluids** (P5). A fluid registry (bworld's liquids are fluids, plus mods' own like Create's honey and chocolate),
|
||||
fluid stacks with amounts in millibuckets, a `"fluids"` capability, and fluid containers in items (buckets, bottles).
|
||||
**S2. Fluids** (P5). A fluid registry (bworld's water and lava, plus mods' own like Create's honey and chocolate), fluid
|
||||
stacks with amounts in millibuckets, a `"fluids"` capability, and fluid containers in items (buckets, bottles).
|
||||
|
||||
**S3. Redstone** (P6, or earlier for clutches). A signal API in the engine (each block side has a power level 0 to 15,
|
||||
`get_signal`, `has_neighbor_signal`, updates to neighbours) and the basic blocks in `mods/bworld`: lever, button, dust,
|
||||
@@ -337,13 +337,13 @@ today.
|
||||
|
||||
**S4. Machines acting on the world** (P3, built).
|
||||
|
||||
- `liquid_flow(x, y, z)` (wraps `common/physics.ts`), for water wheels and fans.
|
||||
- `fluid_flow(x, y, z)` (wraps `common/physics.ts`), for water wheels and fans.
|
||||
- Item entities in a box, which can be changed and removed, for the press, fans and belts picking items up.
|
||||
- Pushing entities, for fans.
|
||||
- Breaking a block over time with progress shown, and its drops, for drills and saws.
|
||||
- A fake player using an item on a block, for the deployer (`DeployerFakePlayer`).
|
||||
|
||||
As built, all in `ctx.world` (see the modding docs for each): `liquid_flow`, `entities_in(from, to, type?)` with
|
||||
As built, all in `ctx.world` (see the modding docs for each): `fluid_flow`, `entities_in(from, to, type?)` with
|
||||
`EntityRef.get_item`, `set_item`, `velocity` and `push`, `spawn_item` for an item lying still where it's put (the
|
||||
press's extra results), and `player.push`, which is sent to their client since it moves them. `set_break_progress` shows
|
||||
cracks to the players who have the chunk, sending only a change of stage, and `ctx.blocks.toughness` gives what breaking
|
||||
|
||||
+1
-1
@@ -304,7 +304,7 @@ export class Client {
|
||||
player.yaw,
|
||||
player.pitch,
|
||||
picked?.distance ?? player.block_interaction_range,
|
||||
this.game_mode.held_item()?.targets_liquids,
|
||||
this.game_mode.held_item()?.targets_fluids,
|
||||
);
|
||||
this.entity_hit = picked && !this.hit_result ? picked.entity : undefined;
|
||||
}
|
||||
|
||||
@@ -64,12 +64,12 @@ export abstract class Entity {
|
||||
|
||||
// in water, like the server's Entity.in_water
|
||||
get in_water() {
|
||||
return physics.in_water(this, (x, y, z) => this.level.is_liquid(x, y, z));
|
||||
return physics.in_water(this, (x, y, z) => this.level.is_fluid(x, y, z));
|
||||
}
|
||||
|
||||
// its eyes are in water
|
||||
get eyes_in_water() {
|
||||
return physics.eyes_in_water(this, this.eye_height, (x, y, z) => this.level.is_liquid(x, y, z));
|
||||
return physics.eyes_in_water(this, this.eye_height, (x, y, z) => this.level.is_fluid(x, y, z));
|
||||
}
|
||||
|
||||
// after moving each tick. full speed is 5 blocks per second, a bit faster than walking
|
||||
|
||||
@@ -159,9 +159,9 @@ export class LocalPlayer extends Player {
|
||||
this.drag = this.flying ? FLY_VERTICAL_DRAG : this.#in_water ? WATER_DRAG : LAND_DRAG;
|
||||
if (this.swimming && this.#in_water) this.#steer(input);
|
||||
if (!this.flying) {
|
||||
physics.push_by_liquid(
|
||||
physics.push_by_fluid(
|
||||
this,
|
||||
(x, y, z) => this.level.liquid_at(x, y, z),
|
||||
(x, y, z) => this.level.fluid_at(x, y, z),
|
||||
(x, y, z) => this.level.collision_shape(x, y, z),
|
||||
true,
|
||||
);
|
||||
@@ -229,7 +229,7 @@ export class LocalPlayer extends Player {
|
||||
// of leaping out of the water, unless it's holding jump
|
||||
#steer(input: MoveInput) {
|
||||
const look_y = Math.sin(this.pitch);
|
||||
const head_in_water = this.level.is_liquid(
|
||||
const head_in_water = this.level.is_fluid(
|
||||
Math.floor(this.x),
|
||||
Math.floor(this.y + this.height),
|
||||
Math.floor(this.z),
|
||||
@@ -259,7 +259,7 @@ export class LocalPlayer extends Player {
|
||||
#hop_out_of_water(from_y: number) {
|
||||
if (!this.colliding_x && !this.colliding_z) return;
|
||||
const blocked = (x: number, y: number, z: number) =>
|
||||
this.level.is_liquid(x, y, z) || this.level.collision_shape(x, y, z);
|
||||
this.level.is_fluid(x, y, z) || this.level.collision_shape(x, y, z);
|
||||
const dy = from_y - this.y + this.vy * TICK_DELTA + WATER_HOP_HEIGHT;
|
||||
if (physics.box_is_free(this, this.vx * TICK_DELTA, dy, this.vz * TICK_DELTA, blocked)) this.vy = WATER_HOP;
|
||||
}
|
||||
|
||||
+4
-4
@@ -7,7 +7,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, liquid_block, placement_target, waterlog_placement } from "$/common/utils.ts";
|
||||
import { box_overlaps_block, can_place, fluid_block, 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";
|
||||
@@ -83,11 +83,11 @@ export class MultiPlayerGameMode {
|
||||
|
||||
// the shared rules' on_interact, like a trapdoor opening, shows right away. the server runs them too and corrects
|
||||
// this if it disagrees. when they do something nothing is placed, like on the server
|
||||
// an item aimed at liquids, like a bucket, uses the liquid in a waterlogged block instead, like on the server
|
||||
// an item aimed at fluids, like a bucket, uses the fluid in a waterlogged block instead, like on the server
|
||||
const block_info = EverythingRegistry.get_by_id<BlockRegistry>("blocks", hit.block)!;
|
||||
const held = this.held_item();
|
||||
const value = level.get_block_value(hit.x, hit.y, hit.z);
|
||||
const used = (held?.targets_liquids && liquid_block(value, block_info)) || block_info;
|
||||
const used = (held?.targets_fluids && fluid_block(value, block_info)) || block_info;
|
||||
// sneaking with an item skips the block, like on the server
|
||||
const sneaking = this.client.player.holding_sneak;
|
||||
const skips_block = sneaking && held !== undefined;
|
||||
@@ -107,7 +107,7 @@ 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.
|
||||
// with the states the server will give it, see BlockRules.placement_value, and holding the fluid 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, {
|
||||
|
||||
@@ -5,9 +5,9 @@ import {
|
||||
block_value,
|
||||
chunk_key,
|
||||
default_block_value,
|
||||
fluid_block,
|
||||
fluid_in_cell,
|
||||
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";
|
||||
@@ -17,7 +17,7 @@ import {
|
||||
block_light_opacity,
|
||||
BlockRegistry,
|
||||
EverythingRegistry,
|
||||
is_liquid_block,
|
||||
is_fluid_block,
|
||||
RENDER_LAYERS,
|
||||
RenderLayer,
|
||||
} from "$/common/everything_registry.ts";
|
||||
@@ -376,18 +376,18 @@ export class ClientLevel {
|
||||
.biome_colors[CHUNK_AREA * 2 + (z - chunk_z * CHUNK_SIZE) * CHUNK_SIZE + (x - chunk_x * CHUNK_SIZE)];
|
||||
}
|
||||
|
||||
// the liquid there and how high it is, for liquids pushing things along. unloaded chunks have none
|
||||
liquid_at(x: number, y: number, z: number) {
|
||||
// the fluid there and how high it is, for fluids pushing things along. unloaded chunks have none
|
||||
fluid_at(x: number, y: number, z: number) {
|
||||
const value = this.get_block_value(x, y, z);
|
||||
const nid = value & ID_MASK;
|
||||
return nid === VOID || nid === AIR ? undefined : liquid_in_cell(value, this.#blocks[nid]);
|
||||
return nid === VOID || nid === AIR ? undefined : fluid_in_cell(value, this.#blocks[nid]);
|
||||
}
|
||||
|
||||
// water, or a block waterlogged with it, see liquid_block. unloaded chunks aren't
|
||||
is_liquid(x: number, y: number, z: number) {
|
||||
// water, or a block waterlogged with it, see fluid_block. unloaded chunks aren't
|
||||
is_fluid(x: number, y: number, z: number) {
|
||||
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;
|
||||
return block !== VOID && block !== AIR && fluid_block(value, this.#blocks[block]) !== undefined;
|
||||
}
|
||||
|
||||
// only changes what this client shows, drops and everything else happen on the server
|
||||
@@ -409,13 +409,13 @@ export class ClientLevel {
|
||||
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)
|
||||
// a waterlogged block leaves its fluid, like the server's destroy_block
|
||||
const fluid = is_waterlogged(old_value, old_block)
|
||||
? EverythingRegistry.get_id("blocks", old_block!.waterloggable!) ?? AIR
|
||||
: AIR;
|
||||
chunk.blocks[index] = liquid;
|
||||
chunk.blocks[index] = fluid;
|
||||
this.#mark_dirty(chunk);
|
||||
this.#mark_neighbors_dirty(block_chunk_x, block_chunk_z, lx, lz, old_nid, liquid);
|
||||
this.#mark_neighbors_dirty(block_chunk_x, block_chunk_z, lx, lz, old_nid, fluid);
|
||||
}
|
||||
|
||||
// a block that lets through or gives off a different amount of light changes the light up to 15 blocks
|
||||
@@ -774,7 +774,7 @@ export class ClientLevel {
|
||||
|
||||
// the first block along the view of something at x/y/z looking at yaw/pitch, like minecraft's pick. it has to go
|
||||
// into one of the block's boxes (see common/block_shapes.ts), so it looks past the empty half of a slab. it looks
|
||||
// through liquids unless liquids is set, see ItemRegistry.targets_liquids
|
||||
// through fluids unless fluids is set, see ItemRegistry.targets_fluids
|
||||
pick(
|
||||
x: number,
|
||||
y: number,
|
||||
@@ -782,7 +782,7 @@ export class ClientLevel {
|
||||
yaw: number,
|
||||
pitch: number,
|
||||
max_distance = 6,
|
||||
liquids = false,
|
||||
fluids = false,
|
||||
step = 0.05,
|
||||
): BlockHitResult | undefined {
|
||||
const cos_pitch = Math.cos(pitch);
|
||||
@@ -812,11 +812,11 @@ export class ClientLevel {
|
||||
continue;
|
||||
}
|
||||
const info = this.#blocks[block];
|
||||
if (!liquids && is_liquid_block(info)) {
|
||||
if (!fluids && is_fluid_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);
|
||||
// aimed at fluids, the source in a waterlogged block covers it, like the server's raycast_shape
|
||||
const covered = fluids && is_waterlogged(value, info);
|
||||
const hit = clip_shape(
|
||||
!info || covered ? FULL_SHAPE : shape_at(info, value),
|
||||
bx,
|
||||
|
||||
@@ -53,7 +53,7 @@ export class GameRenderer {
|
||||
#render_underwater(client: Client) {
|
||||
const { x, y, z } = client.camera;
|
||||
const [bx, by, bz] = [Math.floor(x), Math.floor(y), Math.floor(z)];
|
||||
if (!client.level.is_liquid(bx, by, bz)) return;
|
||||
if (!client.level.is_fluid(bx, by, bz)) return;
|
||||
const color = client.level.water_color(bx, bz);
|
||||
draw_rect(0, 0, canvas.width, canvas.height, [
|
||||
(color >> 16) / 255,
|
||||
|
||||
@@ -55,8 +55,8 @@ import {
|
||||
region_sky,
|
||||
REGION_VOID,
|
||||
} from "./lighting.ts";
|
||||
import { cell_index, liquid_quads, type LiquidCell } from "./liquid_mesh.ts";
|
||||
import { liquid_height, liquid_textures } from "$/common/everything_registry.ts";
|
||||
import { cell_index, fluid_quads, type FluidCell } from "./fluid_mesh.ts";
|
||||
import { fluid_height, fluid_textures } from "$/common/everything_registry.ts";
|
||||
|
||||
type TexturesInfo = Record<string, SpriteRegion>;
|
||||
|
||||
@@ -94,11 +94,11 @@ const block_occludes = new Uint8Array(TABLE_SIZE);
|
||||
// light can come around a corner past it
|
||||
const block_lets_light_by = new Uint8Array(TABLE_SIZE);
|
||||
const light_tables: LightTables = { opacity: new Uint8Array(TABLE_SIZE), emission: new Uint8Array(TABLE_SIZE) };
|
||||
// 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);
|
||||
// fluids: their range, 0 for blocks that aren't one, and where their level state is in the state bits
|
||||
const fluid_range = new Uint8Array(TABLE_SIZE);
|
||||
const fluid_level_shift = new Uint8Array(TABLE_SIZE);
|
||||
// waterloggable blocks: the numeric id of the fluid they hold, 0 for other blocks, and where their waterlogged state is
|
||||
const waterlog_fluid = new Uint16Array(TABLE_SIZE);
|
||||
const waterlogged_shift = new Uint8Array(TABLE_SIZE);
|
||||
// blocks with visuals: HAS_VISUAL, or REPLACED_MODEL when the visual draws all of it, see ctx.render.register_visual
|
||||
const block_visual = new Uint8Array(TABLE_SIZE);
|
||||
@@ -275,7 +275,7 @@ 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);
|
||||
waterlog_fluid.fill(0);
|
||||
blocks_registry.forEach((block, nid) => {
|
||||
if (!block || nid === AIR) return;
|
||||
const layer = LAYER_IDS[block.render_layer ?? "solid"];
|
||||
@@ -286,13 +286,13 @@ function build_block_tables() {
|
||||
block_lets_light_by[nid] = layer !== SOLID || opacity === 0 ? 1 : 0;
|
||||
light_tables.opacity[nid] = opacity;
|
||||
light_tables.emission[nid] = block_light_emission(block);
|
||||
liquid_range[nid] = block.liquid?.range ?? 0;
|
||||
if (block.liquid) {
|
||||
fluid_range[nid] = block.fluid?.range ?? 0;
|
||||
if (block.fluid) {
|
||||
// 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);
|
||||
fluid_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;
|
||||
waterlog_fluid[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);
|
||||
}
|
||||
@@ -538,8 +538,8 @@ function light_quad(quad: MeshQuad, index: number, y: number, face_offsets: numb
|
||||
corner_ao.fill(1);
|
||||
}
|
||||
|
||||
// whether a block hides the face of a liquid against it, like show_face. air's layer isn't set, so it's checked first
|
||||
function hides_liquid(nid: number) {
|
||||
// whether a block hides the face of a fluid against it, like show_face. air's layer isn't set, so it's checked first
|
||||
function hides_fluid(nid: number) {
|
||||
return nid !== AIR && block_layers[nid] === SOLID;
|
||||
}
|
||||
|
||||
@@ -553,62 +553,62 @@ function region_value(chunks: (Uint32Array | null)[], index: number, y: number)
|
||||
return chunk ? chunk[y * CHUNK_AREA + (z % CHUNK_SIZE) * CHUNK_SIZE + (x % CHUNK_SIZE)] : AIR;
|
||||
}
|
||||
|
||||
// the surface of the liquid with this numeric id in a cell, if the cell is that liquid
|
||||
function liquid_cell_height(nid: number, value: number) {
|
||||
const level = ((value >>> 16) >>> liquid_level_shift[nid]) & 7;
|
||||
return liquid_height(level, liquid_range[nid]);
|
||||
// the surface of the fluid with this numeric id in a cell, if the cell is that fluid
|
||||
function fluid_cell_height(nid: number, value: number) {
|
||||
const level = ((value >>> 16) >>> fluid_level_shift[nid]) & 7;
|
||||
return fluid_height(level, fluid_range[nid]);
|
||||
}
|
||||
|
||||
const liquid_cells: LiquidCell[] = Array.from(
|
||||
const fluid_cells: FluidCell[] = Array.from(
|
||||
{ length: 9 },
|
||||
() => ({ same: false, height: 0, air: false, solid: false, top_same: false }),
|
||||
);
|
||||
|
||||
// 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) {
|
||||
// the numeric id of the fluid in a cell: the block, or what it's waterlogged with. 0 for none
|
||||
function cell_fluid(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;
|
||||
if (fluid_range[id]) return id;
|
||||
const fluid = waterlog_fluid[id];
|
||||
if (!fluid) return 0;
|
||||
return ((region_value(chunks, index, y) >>> 16) >>> waterlogged_shift[id]) & 1 ? fluid : 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) {
|
||||
// the 3x3 cells around the fluid at index and y, see FluidCell. a waterlogged block is a source of its fluid
|
||||
function fill_fluid_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 cell = fluid_cells[cell_index(dx, dz)];
|
||||
const at = index + dx + dz * REGION_SIZE;
|
||||
const id = region_block(region, at, y);
|
||||
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.same = cell_fluid(chunks, at, y) === nid;
|
||||
cell.height = !cell.same ? 0 : id === nid ? fluid_cell_height(nid, region_value(chunks, at, y)) : 1;
|
||||
cell.air = id === AIR;
|
||||
cell.solid = hides_liquid(id);
|
||||
cell.top_same = cell_liquid(chunks, at + REGION_LAYER, y + 1) === nid;
|
||||
cell.solid = hides_fluid(id);
|
||||
cell.top_same = cell_fluid(chunks, at + REGION_LAYER, y + 1) === nid;
|
||||
}
|
||||
}
|
||||
return liquid_cells;
|
||||
return fluid_cells;
|
||||
}
|
||||
|
||||
// for a liquid with a flowing texture of its own, how much of it a block shows: as many pixels across as the still one,
|
||||
// so minecraft's 32×32 flowing water next to its 16×16 still water shows half. see liquid_quads
|
||||
// for a fluid with a flowing texture of its own, how much of it a block shows: as many pixels across as the still one,
|
||||
// so minecraft's 32×32 flowing water next to its 16×16 still water shows half. see fluid_quads
|
||||
function flowing_scale(block: BlockRegistry): number | undefined {
|
||||
const textures = liquid_textures(block.textures);
|
||||
const textures = fluid_textures(block.textures);
|
||||
if (textures.flowing === textures.still) return undefined;
|
||||
const still = textures_info[textures.still] ?? textures_info["engine:missing"];
|
||||
const flowing = textures_info[textures.flowing] ?? textures_info["engine:missing"];
|
||||
return Math.min(1, still.width / flowing.width);
|
||||
}
|
||||
|
||||
// a liquid's quad for the mesher, see liquid_mesh.ts. it's lit like a full block's face on that side
|
||||
function liquid_mesh_quad(
|
||||
// a fluid's quad for the mesher, see fluid_mesh.ts. it's lit like a full block's face on that side
|
||||
function fluid_mesh_quad(
|
||||
block: BlockRegistry,
|
||||
face: number,
|
||||
positions: number[],
|
||||
uvs: number[],
|
||||
still: boolean,
|
||||
): MeshQuad {
|
||||
const textures = liquid_textures(block.textures);
|
||||
const textures = fluid_textures(block.textures);
|
||||
const flat = FACE_AXIS[face] !== 1 || (positions[1] === positions[4] && positions[4] === positions[7] &&
|
||||
positions[7] === positions[10]);
|
||||
return {
|
||||
@@ -625,7 +625,7 @@ function liquid_mesh_quad(
|
||||
};
|
||||
}
|
||||
|
||||
// the liquid's surface seen from below, so it shows from under water: the top quad facing down, lit and shaded like
|
||||
// the fluid's surface seen from below, so it shows from under water: the top quad facing down, lit and shaded like
|
||||
// the top. its corners go the other way round, the lighting the mesher just worked out with them
|
||||
const UNDERSIDE_CORNERS = [0, 3, 2, 1];
|
||||
|
||||
@@ -641,7 +641,7 @@ function underside(top: MeshQuad): MeshQuad {
|
||||
|
||||
// region has to be filled and lit first
|
||||
// chunks are the 3x3 chunks' blocks with their states, the middle one's for models that change with them and the
|
||||
// ones around it for liquids, whose surface follows their neighbors'
|
||||
// ones around it for fluids, whose surface follows their neighbors'
|
||||
function make_chunk_mesh(chunk_x: number, chunk_z: number, chunks: (Uint32Array | null)[], camera: number[]) {
|
||||
const values = chunks[4]!;
|
||||
// solid and cutout get a buffer per face group, translucent one for everything since it's sorted instead
|
||||
@@ -707,36 +707,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;
|
||||
|
||||
// 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);
|
||||
// the fluid here, the block's own or what it's waterlogged with, then the block in it
|
||||
const fluid_nid = cell_fluid(chunks, index, y);
|
||||
if (fluid_nid) {
|
||||
const fluid_info = blocks_registry[fluid_nid];
|
||||
const fluid_layer = block_layers[fluid_nid];
|
||||
const fluid_alpha = fluid_layer === TRANSLUCENT ? fluid_info.alpha ?? 1 : 1;
|
||||
const cells = fill_fluid_cells(chunks, fluid_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(
|
||||
const quads = fluid_quads(
|
||||
cells,
|
||||
{
|
||||
same: cell_liquid(chunks, index + REGION_LAYER, y + 1) === liquid_nid,
|
||||
solid: hides_liquid(above),
|
||||
same: cell_fluid(chunks, index + REGION_LAYER, y + 1) === fluid_nid,
|
||||
solid: hides_fluid(above),
|
||||
},
|
||||
{
|
||||
same: cell_liquid(chunks, index - REGION_LAYER, y - 1) === liquid_nid,
|
||||
solid: hides_liquid(below),
|
||||
same: cell_fluid(chunks, index - REGION_LAYER, y - 1) === fluid_nid,
|
||||
solid: hides_fluid(below),
|
||||
},
|
||||
flowing_scale(liquid_info),
|
||||
flowing_scale(fluid_info),
|
||||
);
|
||||
for (const { face, positions, uvs, still } of quads) {
|
||||
const quad = liquid_mesh_quad(liquid_info, face, positions, uvs, still);
|
||||
const quad = fluid_mesh_quad(fluid_info, face, positions, uvs, still);
|
||||
light_quad(quad, index, y, face_offsets);
|
||||
// like minecraft's liquids, no ambient occlusion
|
||||
// like minecraft's fluids, no ambient occlusion
|
||||
corner_ao.fill(1);
|
||||
emit(quad, liquid_layer, x, y, z, liquid_alpha);
|
||||
if (face === 0) emit(underside(quad), liquid_layer, x, y, z, liquid_alpha);
|
||||
emit(quad, fluid_layer, x, y, z, fluid_alpha);
|
||||
if (face === 0) emit(underside(quad), fluid_layer, x, y, z, fluid_alpha);
|
||||
}
|
||||
if (liquid_nid === block_nid) continue;
|
||||
if (fluid_nid === block_nid) continue;
|
||||
}
|
||||
|
||||
const value = values[y * CHUNK_AREA + z * CHUNK_SIZE + x];
|
||||
|
||||
@@ -1,25 +1,25 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// liquids drawn like luanti's flowing liquids (MapblockMeshGenerator::drawLiquidNode in its content_mapblock.cpp): each
|
||||
// corner of the surface is as high as the liquid around it, so it slopes down where it flows away from a source. the
|
||||
// fluids drawn like luanti's flowing fluids (MapblockMeshGenerator::drawLiquidNode in its content_mapblock.cpp): each
|
||||
// corner of the surface is as high as the fluid around it, so it slopes down where it flows away from a source. the
|
||||
// sides between two cells of it are left out, except where it pours down past the surface of the one beside it
|
||||
import { FACE_CORNERS } from "$/common/block_models.ts";
|
||||
|
||||
// one of the 3x3 cells around a liquid and level with it, and the cell above that one
|
||||
export interface LiquidCell {
|
||||
// the same liquid, at any level
|
||||
// one of the 3x3 cells around a fluid and level with it, and the cell above that one
|
||||
export interface FluidCell {
|
||||
// the same fluid, at any level
|
||||
same: boolean;
|
||||
// its surface, 0 to 1, when it's the same liquid. 1 is a source
|
||||
// its surface, 0 to 1, when it's the same fluid. 1 is a source
|
||||
height: number;
|
||||
air: boolean;
|
||||
// hides the side of the liquid facing it, like a full block of stone
|
||||
// hides the side of the fluid facing it, like a full block of stone
|
||||
solid: boolean;
|
||||
// the cell above it is the same liquid, so the liquid here goes right up to the top
|
||||
// the cell above it is the same fluid, so the fluid here goes right up to the top
|
||||
top_same: boolean;
|
||||
}
|
||||
|
||||
export interface LiquidQuad {
|
||||
export interface FluidQuad {
|
||||
// the mesher's face index, see FACE_CORNERS
|
||||
face: number;
|
||||
// 4 corners in FACE_CORNERS' order, x y z in block space
|
||||
@@ -36,15 +36,15 @@ const SHORE = 0.02;
|
||||
// the horizontal neighbor each side faces, as dx dz, for faces 2 to 5: south (+z), north (-z), west (-x), east (+x)
|
||||
const SIDE_DIRECTIONS = [[0, 1], [0, -1], [-1, 0], [1, 0]];
|
||||
|
||||
// cells indexed by (dz + 1) * 3 + (dx + 1), so 4 is the liquid itself
|
||||
// cells indexed by (dz + 1) * 3 + (dx + 1), so 4 is the fluid itself
|
||||
export function cell_index(dx: number, dz: number) {
|
||||
return (dz + 1) * 3 + dx + 1;
|
||||
}
|
||||
|
||||
// like luanti's getCornerLevel: the height of the surface at a corner (cx and cz are 0 or 1, which end of the cell),
|
||||
// from the 4 cells that share it. a source or liquid above any of them makes it full, two of air pull it down to the
|
||||
// floor, otherwise it's the average of the liquid among them
|
||||
export function corner_height(cells: LiquidCell[], cx: number, cz: number) {
|
||||
// from the 4 cells that share it. a source or fluid above any of them makes it full, two of air pull it down to the
|
||||
// floor, otherwise it's the average of the fluid among them
|
||||
export function corner_height(cells: FluidCell[], cx: number, cz: number) {
|
||||
let sum = 0;
|
||||
let count = 0;
|
||||
let air = 0;
|
||||
@@ -92,7 +92,7 @@ function quad(
|
||||
still: boolean,
|
||||
y_at: (x: number, top: number, z: number) => number,
|
||||
uv_scale = 1,
|
||||
): LiquidQuad {
|
||||
): FluidQuad {
|
||||
const positions: number[] = [];
|
||||
const uvs: number[] = [];
|
||||
for (const [x, top, z] of FACE_CORNERS[face]) {
|
||||
@@ -104,9 +104,9 @@ function quad(
|
||||
return { face, positions, uvs, still };
|
||||
}
|
||||
|
||||
// the top of liquid flowing towards (flow_x, flow_z), like minecraft's LiquidBlockRenderer: the flowing texture turned
|
||||
// the top of fluid flowing towards (flow_x, flow_z), like minecraft's LiquidBlockRenderer: the flowing texture turned
|
||||
// so it runs downstream, a scale-sized square from its middle
|
||||
function flowing_top(heights: number[], flow_x: number, flow_z: number, scale: number): LiquidQuad {
|
||||
function flowing_top(heights: number[], flow_x: number, flow_z: number, scale: number): FluidQuad {
|
||||
const angle = Math.atan2(flow_z, flow_x) - Math.PI / 2;
|
||||
const cos = Math.cos(angle) * 16 * scale;
|
||||
const sin = Math.sin(angle) * 16 * scale;
|
||||
@@ -121,18 +121,18 @@ function flowing_top(heights: number[], flow_x: number, flow_z: number, scale: n
|
||||
return { face: 0, positions, uvs, still: false };
|
||||
}
|
||||
|
||||
// the quads of the liquid in the middle cell. above and below are the cells over and under it: whether they're the
|
||||
// same liquid, and whether they're solid. flowing_scale is for a liquid with a flowing texture of its own, how much of
|
||||
// the quads of the fluid in the middle cell. above and below are the cells over and under it: whether they're the
|
||||
// same fluid, and whether they're solid. flowing_scale is for a fluid with a flowing texture of its own, how much of
|
||||
// it a block shows, so it's as sharp as the still one (a 32×32 one next to a 16×16 still one shows half, like
|
||||
// minecraft's water). with it, the sides show that much and a sloping top flows. without, the sides show all of the
|
||||
// texture and the top is always still
|
||||
export function liquid_quads(
|
||||
cells: LiquidCell[],
|
||||
export function fluid_quads(
|
||||
cells: FluidCell[],
|
||||
above: { same: boolean; solid: boolean },
|
||||
below: { same: boolean; solid: boolean },
|
||||
flowing_scale?: number,
|
||||
): LiquidQuad[] {
|
||||
const quads: LiquidQuad[] = [];
|
||||
): FluidQuad[] {
|
||||
const quads: FluidQuad[] = [];
|
||||
// by corner, x then z
|
||||
const heights = [
|
||||
corner_height(cells, 0, 0),
|
||||
@@ -152,7 +152,7 @@ export function liquid_quads(
|
||||
if (top) return above.same ? 1 : height(x, z);
|
||||
return neighbor.same ? height(x, z) : 0;
|
||||
}, flowing_scale);
|
||||
// a side with no height, where the liquid next to it is level with it
|
||||
// a side with no height, where the fluid next to it is level with it
|
||||
const p = side.positions;
|
||||
if (p[1] === p[4] && p[4] === p[7] && p[7] === p[10]) return;
|
||||
quads.push(side);
|
||||
@@ -4,7 +4,7 @@
|
||||
// block models, in the shape of minecraft's block model json: boxes ("elements") with a texture per face.
|
||||
// a block picks one with "model" and fills in its texture variables with "textures". baking turns a model
|
||||
// into quads in block space (0 to 1) that the chunk mesher and the item renderers draw
|
||||
import { type BlockRegistry, type BlockTextures, liquid_textures } from "./everything_registry.ts";
|
||||
import { type BlockRegistry, type BlockTextures, fluid_textures } from "./everything_registry.ts";
|
||||
import { tint_without_biome } from "./worldgen/biome_colors.ts";
|
||||
import { get_state_value } from "./utils.ts";
|
||||
import { type DisplayContext, MODEL_FACES, type ModelFace } from "./mod_data.ts";
|
||||
@@ -365,12 +365,12 @@ export function cube_face_texture(block: BlockRegistry, face: "top" | "bottom" |
|
||||
return resolve_texture(`#${face}`, cube_textures(block), BUILTIN_MODELS[DEFAULT_MODEL]);
|
||||
}
|
||||
|
||||
// the block's textures as engine:cube's. a liquid's { still, flowing } are its top and bottom, and its sides, for its
|
||||
// item and its particles. the chunk mesher draws liquids itself
|
||||
// the block's textures as engine:cube's. a fluid's { still, flowing } are its top and bottom, and its sides, for its
|
||||
// item and its particles. the chunk mesher draws fluids itself
|
||||
export function cube_textures(block: BlockRegistry): BlockTextures {
|
||||
if (!block.liquid) return block.textures;
|
||||
if (!block.fluid) return block.textures;
|
||||
// through the variables, so a "tint" on still or flowing still finds them
|
||||
return { ...liquid_textures(block.textures), top: "#still", bottom: "#still", side: "#flowing" };
|
||||
return { ...fluid_textures(block.textures), top: "#still", bottom: "#still", side: "#flowing" };
|
||||
}
|
||||
|
||||
// the color to draw a face of a block's item in, as r g b from 0 to 1. items aren't in a biome, so a biome "tint"
|
||||
|
||||
@@ -160,20 +160,20 @@ export interface BlockRegistry {
|
||||
// draws the item a server script gives it (ServerWorld.set_block_item) on top of it, like farmer's delight's
|
||||
// cutting board. surface is the height in pixels it lies on. turned with the variant's y
|
||||
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
|
||||
// a fluid like water, drawn like luanti's: its top slopes down towards where it flows. see FluidProperties
|
||||
fluid?: FluidProperties;
|
||||
// the fluid 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
|
||||
// it can only be placed into a source of that fluid, so it's always placed holding it, like farmer's delight's rice
|
||||
waterlogged_only?: boolean;
|
||||
|
||||
compiled_states?: CompiledStateDefinition[];
|
||||
}
|
||||
|
||||
// a liquid's "level" state is 0 for a source, the full block, and 1 to range as it flows away from one, each lower,
|
||||
// like minecraft's. liquids get that state without listing it. their textures are one texture, or { still, flowing }:
|
||||
// a fluid's "level" state is 0 for a source, the full block, and 1 to range as it flows away from one, each lower,
|
||||
// like minecraft's. fluids get that state without listing it. their textures are one texture, or { still, flowing }:
|
||||
// still on the top and bottom, flowing on the sides, like luanti's special tiles
|
||||
export interface LiquidProperties {
|
||||
export interface FluidProperties {
|
||||
// how many blocks it flows from a source, 1 to 7, like luanti's liquid_range
|
||||
range: number;
|
||||
// ticks between each step it flows, like minecraft's 5 for water and 30 for lava
|
||||
@@ -183,28 +183,28 @@ export interface LiquidProperties {
|
||||
renewable: boolean;
|
||||
}
|
||||
|
||||
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
|
||||
export const FLUID_LEVEL: BlockStateDefinition = { name: "level", bits: 3, default: 0 };
|
||||
// a waterloggable block's state, 1 while it holds a source of its fluid. 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 };
|
||||
export const MAX_FLUID_RANGE = 7;
|
||||
// what a fluid that doesn't say has
|
||||
export const FLUID_DEFAULTS: FluidProperties = { range: MAX_FLUID_RANGE, ticks: 5, renewable: true };
|
||||
|
||||
// what entities swim in, and where falling doesn't hurt
|
||||
export function is_liquid_block(block: BlockRegistry | undefined): boolean {
|
||||
return block?.liquid !== undefined;
|
||||
export function is_fluid_block(block: BlockRegistry | undefined): boolean {
|
||||
return block?.fluid !== undefined;
|
||||
}
|
||||
|
||||
// a liquid's textures as the top, bottom and sides of a cube, for its item and its particles
|
||||
export function liquid_textures(textures: BlockTextures): { still: string; flowing: string } {
|
||||
// a fluid's textures as the top, bottom and sides of a cube, for its item and its particles
|
||||
export function fluid_textures(textures: BlockTextures): { still: string; flowing: string } {
|
||||
if (typeof textures === "string") return { still: textures, flowing: textures };
|
||||
const still = textures.still ?? "engine:missing";
|
||||
return { still, flowing: textures.flowing ?? still };
|
||||
}
|
||||
|
||||
// how high a liquid's surface is in a block of it at this level, 0 to 1, like luanti's flowing levels
|
||||
export function liquid_height(level: number, range: number) {
|
||||
// how high a fluid's surface is in a block of it at this level, 0 to 1, like luanti's flowing levels
|
||||
export function fluid_height(level: number, range: number) {
|
||||
if (level === 0) return 1;
|
||||
return (range - Math.min(level, range) + 0.5) / range;
|
||||
}
|
||||
@@ -264,9 +264,9 @@ export interface ItemRegistry<T = unknown | undefined> {
|
||||
lore?: string;
|
||||
food?: FoodProperties;
|
||||
worn?: WornProperties;
|
||||
// whether players holding it aim at liquids, like buckets and fishing rods. others look through them, so they can
|
||||
// whether players holding it aim at fluids, like buckets and fishing rods. others look through them, so they can
|
||||
// break and place blocks under water
|
||||
targets_liquids?: boolean;
|
||||
targets_fluids?: boolean;
|
||||
components?: Record<string, unknown>;
|
||||
|
||||
on_create?(item: ItemStack<T>): void;
|
||||
|
||||
@@ -301,9 +301,9 @@ export interface ServerWorld {
|
||||
// the first block on the line from one point to another, like minecraft's Level.clip. it hits what entities bump
|
||||
// into, so it goes through plants and other blocks without collision. undefined when nothing is in the way
|
||||
raycast(from: Position, to: Position, options?: RaycastOptions): RaycastHit | undefined;
|
||||
// which way the liquid at x y z flows, a vector 1 long, or 0 0 0 where it's still or there's none, like minecraft's
|
||||
// which way the fluid at x y z flows, a vector 1 long, or 0 0 0 where it's still or there's none, like minecraft's
|
||||
// FluidState.getFlow: toward lower cells of it beside it, and down where it falls. for what it pushes, like a wheel
|
||||
liquid_flow(x: number, y: number, z: number): Position;
|
||||
fluid_flow(x: number, y: number, z: number): Position;
|
||||
// whether the chunk the column is in was ever generated, so reading its blocks generates nothing. for code that
|
||||
// reaches far, like a walk along something long, which can stop at the rest: nobody has built anything there
|
||||
is_loaded(x: number, z: number): boolean;
|
||||
@@ -350,9 +350,9 @@ export interface BreakOptions {
|
||||
}
|
||||
|
||||
export interface RaycastOptions {
|
||||
// like minecraft's ClipContext.Fluid: "none" (the default) goes through liquids, "source_only" hits their sources
|
||||
// and goes through where they flow, "any" hits all of them. a liquid is hit at its surface
|
||||
liquids?: "none" | "source_only" | "any";
|
||||
// like minecraft's ClipContext.Fluid: "none" (the default) goes through fluids, "source_only" hits their sources
|
||||
// and goes through where they flow, "any" hits all of them. a fluid is hit at its surface
|
||||
fluids?: "none" | "source_only" | "any";
|
||||
}
|
||||
|
||||
export interface RaycastHit {
|
||||
|
||||
+4
-4
@@ -160,7 +160,7 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
|
||||
const entity_model_jsons = new Map<string, EntityModelJson>();
|
||||
const effect_owners = new Map<string, string[]>();
|
||||
const tag_owners = new Map<TagRegistry, Set<string>>();
|
||||
const liquids = new Set<string>();
|
||||
const fluids = new Set<string>();
|
||||
const loot_tables = new Set<string>();
|
||||
const biome_owners = new Map<string, string[]>();
|
||||
const add = (map: Map<string, string[]>, id: string, mod: string) => map.set(id, [...(map.get(id) ?? []), mod]);
|
||||
@@ -181,7 +181,7 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
|
||||
for (const { json } of mod.blocks) {
|
||||
add(block_owners, json.id, mod.id);
|
||||
if (json.item !== false) add(item_owners, json.id, mod.id);
|
||||
if (json.liquid) liquids.add(json.id);
|
||||
if (json.fluid) fluids.add(json.id);
|
||||
}
|
||||
// one without a texture is the item of the mod's block, which is already counted
|
||||
for (const { json } of mod.items) if (json.texture !== undefined) add(item_owners, json.id, mod.id);
|
||||
@@ -279,8 +279,8 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
|
||||
if (json.loot_table) uses(file, json.loot_table, "loot table");
|
||||
if (json.waterloggable) {
|
||||
uses(file, json.waterloggable, "block");
|
||||
if (block_owners.has(json.waterloggable) && !liquids.has(json.waterloggable)) {
|
||||
errors.push(`${file}: waterloggable ${json.waterloggable} isn't a liquid`);
|
||||
if (block_owners.has(json.waterloggable) && !fluids.has(json.waterloggable)) {
|
||||
errors.push(`${file}: waterloggable ${json.waterloggable} isn't a fluid`);
|
||||
}
|
||||
}
|
||||
for (const id of [...(json.place_on ?? []), ...(json.place_in ?? [])]) {
|
||||
|
||||
+41
-41
@@ -8,12 +8,12 @@ import { is_tint_color, type TintColor } from "./worldgen/biome_colors.ts";
|
||||
import {
|
||||
type BlockRegistry,
|
||||
type BlockStateDefinition,
|
||||
FLUID_DEFAULTS,
|
||||
FLUID_LEVEL,
|
||||
type FluidProperties,
|
||||
type FoodProperties,
|
||||
type ItemRegistry,
|
||||
LIQUID_DEFAULTS,
|
||||
LIQUID_LEVEL,
|
||||
type LiquidProperties,
|
||||
MAX_LIQUID_RANGE,
|
||||
MAX_FLUID_RANGE,
|
||||
RENDER_LAYERS,
|
||||
type RenderLayer,
|
||||
type ToolProperties,
|
||||
@@ -119,11 +119,11 @@ export interface BlockJson {
|
||||
states?: BlockStateDefinition[];
|
||||
components?: Record<string, unknown>;
|
||||
item_display?: { surface: number };
|
||||
// makes it a liquid, see LiquidProperties. range is 7 when not set
|
||||
liquid?: Partial<LiquidProperties>;
|
||||
// makes it a fluid, see FluidProperties. range is 7 when not set
|
||||
fluid?: Partial<FluidProperties>;
|
||||
// what it drops when broken, see common/loot.ts. drops is the shorthand for one of an item
|
||||
loot_table?: string;
|
||||
// the liquid it can hold, see BlockRegistry.waterloggable
|
||||
// the fluid it can hold, see BlockRegistry.waterloggable
|
||||
waterloggable?: string;
|
||||
// only placed into a source of it, see BlockRegistry.waterlogged_only
|
||||
waterlogged_only?: boolean;
|
||||
@@ -198,8 +198,8 @@ const block_fields = z.object({
|
||||
components: json_object.optional(),
|
||||
// the height in pixels items lie on
|
||||
item_display: z.object({ surface: z.number().min(0).max(16) }).optional(),
|
||||
liquid: z.strictObject({
|
||||
range: z.int().min(1).max(MAX_LIQUID_RANGE).optional(),
|
||||
fluid: z.strictObject({
|
||||
range: z.int().min(1).max(MAX_FLUID_RANGE).optional(),
|
||||
ticks: z.int().min(1).optional(),
|
||||
renewable: z.boolean().optional(),
|
||||
}, "must be { range, ticks, renewable }, or {}").optional(),
|
||||
@@ -210,8 +210,8 @@ const block_fields = z.object({
|
||||
|
||||
const block_schema: z.ZodType<BlockJson> = block_fields.superRefine(check_block);
|
||||
|
||||
// what a liquid's and a plain cube's textures can be, past being ids
|
||||
const liquid_textures = z.union([z.string(), z.strictObject({ still: z.string(), flowing: z.string().optional() })]);
|
||||
// what a fluid's and a plain cube's textures can be, past being ids
|
||||
const fluid_textures = z.union([z.string(), z.strictObject({ still: z.string(), flowing: z.string().optional() })]);
|
||||
const cube_textures = z.union([
|
||||
z.string(),
|
||||
z.object({ top: z.string(), bottom: z.string(), side: z.string() }),
|
||||
@@ -237,18 +237,18 @@ function check_block(block: z.infer<typeof block_fields>, ctx: z.RefinementCtx)
|
||||
if (part.when !== undefined) check_condition(["parts", i, "when"], part.when);
|
||||
});
|
||||
|
||||
if (block.liquid) {
|
||||
// a liquid is drawn by the engine, so it has no model of its own, and its level is a state it gets
|
||||
if (!liquid_textures.safeParse(block.textures).success) {
|
||||
problem(["textures"], "must be a texture id or { still, flowing } for a liquid");
|
||||
if (block.fluid) {
|
||||
// a fluid is drawn by the engine, so it has no model of its own, and its level is a state it gets
|
||||
if (!fluid_textures.safeParse(block.textures).success) {
|
||||
problem(["textures"], "must be a texture id or { still, flowing } for a fluid");
|
||||
}
|
||||
for (const key of ["model", "variants", "parts", "shape", "collision_shape", "item_model"] as const) {
|
||||
if (block[key] !== undefined) problem([key], "isn't for liquids, the engine draws them");
|
||||
if (block[key] !== undefined) problem([key], "isn't for fluids, the engine draws them");
|
||||
}
|
||||
if (names.has(LIQUID_LEVEL.name)) {
|
||||
problem(["states"], `can't have "${LIQUID_LEVEL.name}", a liquid gets that state of its own`);
|
||||
if (names.has(FLUID_LEVEL.name)) {
|
||||
problem(["states"], `can't have "${FLUID_LEVEL.name}", a fluid gets that state of its own`);
|
||||
}
|
||||
if (block.waterloggable !== undefined) problem(["waterloggable"], "isn't for liquids");
|
||||
if (block.waterloggable !== undefined) problem(["waterloggable"], "isn't for fluids");
|
||||
} else if (block.parts === undefined && (block.model ?? "engine:cube") === "engine:cube") {
|
||||
if (!cube_textures.safeParse(block.textures).success) {
|
||||
problem(["textures"], "must be a texture id, { top, bottom, side } or { front, side }");
|
||||
@@ -259,10 +259,10 @@ function check_block(block: z.infer<typeof block_fields>, ctx: z.RefinementCtx)
|
||||
problem(["states"], `can't have "${WATERLOGGED.name}", a waterloggable block gets that state of its own`);
|
||||
}
|
||||
if (block.waterlogged_only && block.waterloggable === undefined) {
|
||||
problem(["waterlogged_only"], "needs waterloggable, the liquid it's placed into");
|
||||
problem(["waterlogged_only"], "needs waterloggable, the fluid it's placed into");
|
||||
}
|
||||
// a block's states fit in 16 bits, with the one it gets for being a liquid or waterloggable
|
||||
const added = block.liquid ? LIQUID_LEVEL : block.waterloggable !== undefined ? WATERLOGGED : undefined;
|
||||
// a block's states fit in 16 bits, with the one it gets for being a fluid or waterloggable
|
||||
const added = block.fluid ? FLUID_LEVEL : block.waterloggable !== undefined ? WATERLOGGED : undefined;
|
||||
const bits = states.reduce((sum, state) => sum + state.bits, added?.bits ?? 0);
|
||||
if (bits > 16) problem(["states"], `use ${bits} bits${added ? ` with ${added.name}` : ""}, the most is 16`);
|
||||
if (block.drops !== undefined && block.loot_table !== undefined) {
|
||||
@@ -365,8 +365,8 @@ export interface ItemJson {
|
||||
food?: FoodProperties;
|
||||
// how it looks worn on a player
|
||||
worn?: WornProperties;
|
||||
// see ItemRegistry.targets_liquids
|
||||
targets_liquids?: boolean;
|
||||
// see ItemRegistry.targets_fluids
|
||||
targets_fluids?: boolean;
|
||||
name?: string;
|
||||
lore?: string;
|
||||
components?: Record<string, unknown>;
|
||||
@@ -399,7 +399,7 @@ const item_schema: z.ZodType<ItemJson> = z.object({
|
||||
slim_model: id.optional(),
|
||||
slim_texture: id.optional(),
|
||||
}, "must be { model, texture }, an entity model and a texture").optional(),
|
||||
targets_liquids: z.boolean().optional(),
|
||||
targets_fluids: z.boolean().optional(),
|
||||
name: text.optional(),
|
||||
lore: text.optional(),
|
||||
components: json_object.optional(),
|
||||
@@ -629,8 +629,8 @@ export function block_to_json(block: BlockRegistry, has_item: boolean): BlockJso
|
||||
if (block.light_emission !== undefined) json.light_emission = block.light_emission;
|
||||
if (block.light_opacity !== undefined) json.light_opacity = block.light_opacity;
|
||||
if (block.alpha !== undefined) json.alpha = block.alpha;
|
||||
// liquids are without collision and replaceable unless they say otherwise
|
||||
if (block.has_collision === !!block.liquid) json.collision = block.has_collision;
|
||||
// fluids are without collision and replaceable unless they say otherwise
|
||||
if (block.has_collision === !!block.fluid) json.collision = block.has_collision;
|
||||
if (block.shape !== undefined) json.shape = block.shape;
|
||||
if (block.collision_shape !== undefined) json.collision_shape = block.collision_shape;
|
||||
if (block.parts !== undefined) json.parts = block.parts;
|
||||
@@ -646,22 +646,22 @@ export function block_to_json(block: BlockRegistry, has_item: boolean): BlockJso
|
||||
if (!has_item) json.item = false;
|
||||
if (block.interactive) json.interactive = true;
|
||||
if (block.scrollable) json.scrollable = true;
|
||||
if (!!block.replaceable !== !!block.liquid) json.replaceable = !!block.replaceable;
|
||||
if (!!block.replaceable !== !!block.fluid) 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 and a waterloggable block's waterlogged state are its own, added when it loads
|
||||
const added = block.liquid || block.waterloggable;
|
||||
// a fluid's level state and a waterloggable block's waterlogged state are its own, added when it loads
|
||||
const added = block.fluid || 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) {
|
||||
if (block.fluid) {
|
||||
// only what isn't the default
|
||||
const liquid = block.liquid;
|
||||
json.liquid = Object.fromEntries(
|
||||
Object.entries(liquid).filter(([key, value]) => LIQUID_DEFAULTS[key as keyof LiquidProperties] !== value),
|
||||
const fluid = block.fluid;
|
||||
json.fluid = Object.fromEntries(
|
||||
Object.entries(fluid).filter(([key, value]) => FLUID_DEFAULTS[key as keyof FluidProperties] !== value),
|
||||
);
|
||||
}
|
||||
return json;
|
||||
@@ -671,7 +671,7 @@ export function block_from_json(json: BlockJson): { block: BlockRegistry; has_it
|
||||
const block: BlockRegistry = {
|
||||
id: json.id,
|
||||
textures: json.textures,
|
||||
has_collision: json.collision ?? !json.liquid,
|
||||
has_collision: json.collision ?? !json.fluid,
|
||||
};
|
||||
if (json.name !== undefined) block.name = json.name;
|
||||
if (json.model !== undefined) block.model = json.model;
|
||||
@@ -697,16 +697,16 @@ export function block_from_json(json: BlockJson): { block: BlockRegistry; has_it
|
||||
if (json.loot_table !== undefined) block.loot_table = json.loot_table;
|
||||
if (json.interactive) block.interactive = true;
|
||||
if (json.scrollable) block.scrollable = true;
|
||||
if (json.replaceable ?? json.liquid) block.replaceable = true;
|
||||
if (json.replaceable ?? json.fluid) block.replaceable = true;
|
||||
if (json.place_on) block.place_on = json.place_on;
|
||||
if (json.place_in) block.place_in = json.place_in;
|
||||
if (json.states) block.states = json.states;
|
||||
if (json.components) block.components = json.components;
|
||||
if (json.item_display) block.item_display = json.item_display;
|
||||
if (json.liquid) {
|
||||
block.liquid = { ...LIQUID_DEFAULTS, ...json.liquid };
|
||||
if (json.fluid) {
|
||||
block.fluid = { ...FLUID_DEFAULTS, ...json.fluid };
|
||||
// last, so the block's own states keep their bits
|
||||
block.states = [...block.states ?? [], { ...LIQUID_LEVEL }];
|
||||
block.states = [...block.states ?? [], { ...FLUID_LEVEL }];
|
||||
}
|
||||
if (json.waterlogged_only) block.waterlogged_only = true;
|
||||
if (json.waterloggable) {
|
||||
@@ -732,7 +732,7 @@ export function item_to_json(id: string, item: ItemRegistry): ItemJson {
|
||||
if (item.base_sell_value !== undefined) json.base_sell_value = item.base_sell_value;
|
||||
if (item.food !== undefined) json.food = item.food;
|
||||
if (item.worn !== undefined) json.worn = item.worn;
|
||||
if (item.targets_liquids !== undefined) json.targets_liquids = item.targets_liquids;
|
||||
if (item.targets_fluids !== undefined) json.targets_fluids = item.targets_fluids;
|
||||
if (item.name !== undefined) json.name = item.name;
|
||||
if (item.lore !== undefined) json.lore = item.lore;
|
||||
if (item.components) json.components = item.components;
|
||||
@@ -760,7 +760,7 @@ function item_fields(json: ItemJson): Partial<ItemRegistry> {
|
||||
if (json.base_sell_value !== undefined) item.base_sell_value = json.base_sell_value;
|
||||
if (json.food !== undefined) item.food = json.food;
|
||||
if (json.worn !== undefined) item.worn = json.worn;
|
||||
if (json.targets_liquids !== undefined) item.targets_liquids = json.targets_liquids;
|
||||
if (json.targets_fluids !== undefined) item.targets_fluids = json.targets_fluids;
|
||||
if (json.name !== undefined) item.name = json.name;
|
||||
if (json.lore !== undefined) item.lore = json.lore;
|
||||
if (json.components) item.components = json.components;
|
||||
|
||||
+44
-44
@@ -231,39 +231,39 @@ function overlaps_solid(min: number[], max: number[], is_solid: BlockCollision)
|
||||
return false;
|
||||
}
|
||||
|
||||
// in water like minecraft's wasTouchingWater, close enough: its feet or the middle of its box are in liquid
|
||||
export function in_water(body: Body, is_liquid: (x: number, y: number, z: number) => boolean): boolean {
|
||||
// in water like minecraft's wasTouchingWater, close enough: its feet or the middle of its box are in fluid
|
||||
export function in_water(body: Body, is_fluid: (x: number, y: number, z: number) => boolean): boolean {
|
||||
const x = Math.floor(body.x);
|
||||
const z = Math.floor(body.z);
|
||||
return is_liquid(x, Math.floor(body.y), z) || is_liquid(x, Math.floor(body.y + body.height / 2), z);
|
||||
return is_fluid(x, Math.floor(body.y), z) || is_fluid(x, Math.floor(body.y + body.height / 2), z);
|
||||
}
|
||||
|
||||
// its eyes are in liquid, so it can't breathe
|
||||
// its eyes are in fluid, so it can't breathe
|
||||
export function eyes_in_water(
|
||||
body: Body,
|
||||
eye_height: number,
|
||||
is_liquid: (x: number, y: number, z: number) => boolean,
|
||||
is_fluid: (x: number, y: number, z: number) => boolean,
|
||||
): boolean {
|
||||
return is_liquid(Math.floor(body.x), Math.floor(body.y + eye_height), Math.floor(body.z));
|
||||
return is_fluid(Math.floor(body.x), Math.floor(body.y + eye_height), Math.floor(body.z));
|
||||
}
|
||||
|
||||
// a liquid in a cell, for liquids pushing things along
|
||||
export interface LiquidInCell {
|
||||
// which liquid, its block's id. a liquid's flow only goes by cells of the same one
|
||||
// a fluid in a cell, for fluids pushing things along
|
||||
export interface FluidInCell {
|
||||
// which fluid, its block's id. a fluid'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
|
||||
// its surface, 0 to 1 (see fluid_height), not counting more of it above
|
||||
height: number;
|
||||
}
|
||||
|
||||
// the liquid in a cell, undefined for none
|
||||
export type LiquidAt = (x: number, y: number, z: number) => LiquidInCell | undefined;
|
||||
// the fluid in a cell, undefined for none
|
||||
export type FluidAt = (x: number, y: number, z: number) => FluidInCell | undefined;
|
||||
|
||||
const HORIZONTAL = [[1, 0], [-1, 0], [0, 1], [0, -1]];
|
||||
// blocks per second added each tick, minecraft's water 0.014 blocks per tick per tick
|
||||
const LIQUID_PUSH = 0.014 * 20;
|
||||
// moving slower than this sideways, a push is at least LIQUID_MIN_PUSH, so things still drift off flat-ish water
|
||||
const LIQUID_STILL_SPEED = 0.003 * 20;
|
||||
const LIQUID_MIN_PUSH = 0.0045 * 20;
|
||||
const FLUID_PUSH = 0.014 * 20;
|
||||
// moving slower than this sideways, a push is at least FLUID_MIN_PUSH, so things still drift off flat-ish water
|
||||
const FLUID_STILL_SPEED = 0.003 * 20;
|
||||
const FLUID_MIN_PUSH = 0.0045 * 20;
|
||||
|
||||
function blocks_motion(collision: boolean | Shape) {
|
||||
return shape_of(collision).length > 0;
|
||||
@@ -274,29 +274,29 @@ function normalized(x: number, y: number, z: number): [number, number, number] {
|
||||
return length < 1e-5 ? [0, 0, 0] : [x / length, y / length, z / length];
|
||||
}
|
||||
|
||||
// which way the liquid at x y z flows, a length 1 vector or nothing, like minecraft's FlowingFluid.getFlow: towards
|
||||
// which way the fluid at x y z flows, a length 1 vector or nothing, like minecraft's FlowingFluid.getFlow: towards
|
||||
// lower cells of it beside it, and towards drops where it pours over an edge. falling down a wall it goes down too
|
||||
export function liquid_flow(
|
||||
export function fluid_flow(
|
||||
x: number,
|
||||
y: number,
|
||||
z: number,
|
||||
liquid_at: LiquidAt,
|
||||
fluid_at: FluidAt,
|
||||
is_solid: BlockCollision,
|
||||
): [number, number, number] {
|
||||
const here = liquid_at(x, y, z);
|
||||
const here = fluid_at(x, y, z);
|
||||
if (!here) return [0, 0, 0];
|
||||
let fx = 0;
|
||||
let fz = 0;
|
||||
for (const [dx, dz] of HORIZONTAL) {
|
||||
const neighbor = liquid_at(x + dx, y, z + dz);
|
||||
// another liquid doesn't change this one's flow
|
||||
const neighbor = fluid_at(x + dx, y, z + dz);
|
||||
// another fluid doesn't change this one's flow
|
||||
if (neighbor && neighbor.id !== here.id) continue;
|
||||
let drop = 0;
|
||||
if (neighbor) {
|
||||
drop = here.height - neighbor.height;
|
||||
} else if (!blocks_motion(is_solid(x + dx, y, z + dz))) {
|
||||
// an edge it pours over, onto more of itself
|
||||
const below = liquid_at(x + dx, y - 1, z + dz);
|
||||
const below = fluid_at(x + dx, y - 1, z + dz);
|
||||
if (below?.id === here.id) drop = here.height - (below.height - 1);
|
||||
}
|
||||
fx += dx * drop;
|
||||
@@ -306,7 +306,7 @@ export function liquid_flow(
|
||||
|
||||
// falling: flowing, not a source, with more of it above. a deep pool of sources isn't
|
||||
let fy = 0;
|
||||
if (here.height < 1 && liquid_at(x, y + 1, z)?.id === here.id) {
|
||||
if (here.height < 1 && fluid_at(x, y + 1, z)?.id === here.id) {
|
||||
const against_wall = HORIZONTAL.some(([dx, dz]) =>
|
||||
blocks_motion(is_solid(x + dx, y, z + dz)) || blocks_motion(is_solid(x + dx, y + 1, z + dz))
|
||||
);
|
||||
@@ -315,10 +315,10 @@ export function liquid_flow(
|
||||
return normalized(fx, fy, fz);
|
||||
}
|
||||
|
||||
// liquid the body is in pushes it the way it flows, like minecraft's Entity.updateFluidHeightAndDoFluidPushing: the
|
||||
// fluid the body is in pushes it the way it flows, like minecraft's Entity.updateFluidHeightAndDoFluidPushing: the
|
||||
// flow of every cell of it the box reaches, averaged, weaker where it's shallow. players get that average as it is,
|
||||
// other things are pushed at full strength whichever way it points. changes the body's velocity
|
||||
export function push_by_liquid(body: Body, liquid_at: LiquidAt, is_solid: BlockCollision, is_player: boolean) {
|
||||
export function push_by_fluid(body: Body, fluid_at: FluidAt, is_solid: BlockCollision, is_player: boolean) {
|
||||
const half = body.width / 2 - 0.001;
|
||||
const min_y = body.y + 0.001;
|
||||
const max_y = body.y + body.height - 0.001;
|
||||
@@ -330,12 +330,12 @@ export function push_by_liquid(body: Body, liquid_at: LiquidAt, is_solid: BlockC
|
||||
for (let x = Math.floor(body.x - half); x <= Math.floor(body.x + half); x++) {
|
||||
for (let y = Math.floor(min_y); y <= Math.floor(max_y); y++) {
|
||||
for (let z = Math.floor(body.z - half); z <= Math.floor(body.z + half); z++) {
|
||||
const liquid = liquid_at(x, y, z);
|
||||
if (!liquid) continue;
|
||||
const surface = y + (liquid_at(x, y + 1, z)?.id === liquid.id ? 1 : liquid.height);
|
||||
const fluid = fluid_at(x, y, z);
|
||||
if (!fluid) continue;
|
||||
const surface = y + (fluid_at(x, y + 1, z)?.id === fluid.id ? 1 : fluid.height);
|
||||
if (surface < min_y) continue;
|
||||
depth = Math.max(depth, surface - min_y);
|
||||
let [fx, fy, fz] = liquid_flow(x, y, z, liquid_at, is_solid);
|
||||
let [fx, fy, fz] = fluid_flow(x, y, z, fluid_at, is_solid);
|
||||
// minecraft's, which goes by the deepest cell so far
|
||||
if (depth < 0.4) {
|
||||
fx *= depth;
|
||||
@@ -352,31 +352,31 @@ export function push_by_liquid(body: Body, liquid_at: LiquidAt, is_solid: BlockC
|
||||
if (Math.hypot(px, py, pz) === 0) return;
|
||||
|
||||
[px, py, pz] = is_player ? [px / count, py / count, pz / count] : normalized(px, py, pz);
|
||||
px *= LIQUID_PUSH;
|
||||
py *= LIQUID_PUSH;
|
||||
pz *= LIQUID_PUSH;
|
||||
px *= FLUID_PUSH;
|
||||
py *= FLUID_PUSH;
|
||||
pz *= FLUID_PUSH;
|
||||
if (
|
||||
Math.abs(body.vx) < LIQUID_STILL_SPEED && Math.abs(body.vz) < LIQUID_STILL_SPEED &&
|
||||
Math.hypot(px, py, pz) < LIQUID_MIN_PUSH
|
||||
Math.abs(body.vx) < FLUID_STILL_SPEED && Math.abs(body.vz) < FLUID_STILL_SPEED &&
|
||||
Math.hypot(px, py, pz) < FLUID_MIN_PUSH
|
||||
) {
|
||||
[px, py, pz] = normalized(px, py, pz).map((n) => n * LIQUID_MIN_PUSH);
|
||||
[px, py, pz] = normalized(px, py, pz).map((n) => n * FLUID_MIN_PUSH);
|
||||
}
|
||||
body.vx += px;
|
||||
body.vy += py;
|
||||
body.vz += pz;
|
||||
}
|
||||
|
||||
// how far the body's feet are under the surface of liquid it's in, 0 for none, like minecraft's getFluidHeight
|
||||
export function liquid_depth(body: Body, liquid_at: LiquidAt): number {
|
||||
// how far the body's feet are under the surface of fluid it's in, 0 for none, like minecraft's getFluidHeight
|
||||
export function fluid_depth(body: Body, fluid_at: FluidAt): number {
|
||||
const half = body.width / 2 - 0.001;
|
||||
const min_y = body.y + 0.001;
|
||||
let depth = 0;
|
||||
for (let x = Math.floor(body.x - half); x <= Math.floor(body.x + half); x++) {
|
||||
for (let y = Math.floor(min_y); y <= Math.floor(body.y + body.height - 0.001); y++) {
|
||||
for (let z = Math.floor(body.z - half); z <= Math.floor(body.z + half); z++) {
|
||||
const liquid = liquid_at(x, y, z);
|
||||
if (!liquid) continue;
|
||||
const surface = y + (liquid_at(x, y + 1, z)?.id === liquid.id ? 1 : liquid.height);
|
||||
const fluid = fluid_at(x, y, z);
|
||||
if (!fluid) continue;
|
||||
const surface = y + (fluid_at(x, y + 1, z)?.id === fluid.id ? 1 : fluid.height);
|
||||
depth = Math.max(depth, surface - min_y);
|
||||
}
|
||||
}
|
||||
@@ -392,7 +392,7 @@ export const physics = {
|
||||
box_is_free,
|
||||
in_water,
|
||||
eyes_in_water,
|
||||
liquid_flow,
|
||||
push_by_liquid,
|
||||
liquid_depth,
|
||||
fluid_flow,
|
||||
push_by_fluid,
|
||||
fluid_depth,
|
||||
};
|
||||
|
||||
+14
-14
@@ -2,11 +2,11 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import { ID_MASK, STATE_SHIFT } from "./constants.ts";
|
||||
import { BlockRegistry, EverythingRegistry, ItemRegistry, liquid_height } from "./everything_registry.ts";
|
||||
import { BlockRegistry, EverythingRegistry, fluid_height, ItemRegistry } from "./everything_registry.ts";
|
||||
import { block_item_texture, cube_face_texture } from "./block_models.ts";
|
||||
import { FULL_SHAPE, type Shape } from "./block_shapes.ts";
|
||||
import type { ItemStack } from "./inventory.ts";
|
||||
import type { LiquidInCell } from "./physics.ts";
|
||||
import type { FluidInCell } from "./physics.ts";
|
||||
import { list_matches } from "./tags.ts";
|
||||
|
||||
export function point_inside_rec(
|
||||
@@ -191,7 +191,7 @@ export function can_place(
|
||||
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;
|
||||
// one that's only ever waterlogged goes into a source of its liquid and nothing else
|
||||
// one that's only ever waterlogged goes into a source of its fluid and nothing else
|
||||
const source_ok = !block.waterlogged_only ||
|
||||
(current !== undefined && current.id === block.waterloggable &&
|
||||
(get_state_value(current_value, current, "level") ?? 0) === 0);
|
||||
@@ -220,31 +220,31 @@ export function chunk_key(x: number, z: number) {
|
||||
return (x + 32768) * 65536 + (z + 32768);
|
||||
}
|
||||
|
||||
// whether a block value is a waterloggable block holding its liquid
|
||||
// whether a block value is a waterloggable block holding its fluid
|
||||
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
|
||||
// the fluid in a cell with this block value: the block itself, or the fluid 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;
|
||||
export function fluid_block(value: number, block: BlockRegistry | undefined): BlockRegistry | undefined {
|
||||
if (block?.fluid) return block;
|
||||
if (!is_waterlogged(value, block)) return undefined;
|
||||
return EverythingRegistry.get<BlockRegistry>("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
|
||||
// the fluid in a cell and how high it is, for fluids pushing things (see push_by_fluid in physics.ts). a waterlogged
|
||||
// block holds a source
|
||||
export function liquid_in_cell(value: number, block: BlockRegistry | undefined): LiquidInCell | undefined {
|
||||
if (block?.liquid) {
|
||||
export function fluid_in_cell(value: number, block: BlockRegistry | undefined): FluidInCell | undefined {
|
||||
if (block?.fluid) {
|
||||
const level = get_state_value(value, block, "level") ?? 0;
|
||||
return { id: block.id, height: liquid_height(level, block.liquid.range) };
|
||||
return { id: block.id, height: fluid_height(level, block.fluid.range) };
|
||||
}
|
||||
const liquid = liquid_block(value, block);
|
||||
return liquid && { id: liquid.id, height: 1 };
|
||||
const fluid = fluid_block(value, block);
|
||||
return fluid && { id: fluid.id, height: 1 };
|
||||
}
|
||||
|
||||
// a waterloggable block placed where a source of its liquid is holds it, like minecraft's getStateForPlacement.
|
||||
// a waterloggable block placed where a source of its fluid 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,
|
||||
|
||||
@@ -362,7 +362,7 @@ const MODIFIERS: Record<string, Builtin<Modifier>> = {
|
||||
return (chunk, positions) => positions.map((p) => ({ x: p.x, y: height(chunk), z: p.z }));
|
||||
},
|
||||
},
|
||||
// on top of the column: WORLD_SURFACE on its highest block, OCEAN_FLOOR on its highest that isn't a liquid.
|
||||
// on top of the column: WORLD_SURFACE on its highest block, OCEAN_FLOOR on its highest that isn't a fluid.
|
||||
// positions outside the chunk are dropped
|
||||
heightmap: {
|
||||
schema: z.object({ heightmap: z.enum(HEIGHTMAPS) }),
|
||||
@@ -392,7 +392,7 @@ const MODIFIERS: Record<string, Builtin<Modifier>> = {
|
||||
|
||||
function counts_for_heightmap(id: string | undefined, floor: boolean) {
|
||||
if (id === undefined || id === AIR_ID) return false;
|
||||
return !floor || !EverythingRegistry.get<BlockRegistry>("blocks", id)?.liquid;
|
||||
return !floor || !EverythingRegistry.get<BlockRegistry>("blocks", id)?.fluid;
|
||||
}
|
||||
|
||||
// block predicates, like minecraft's
|
||||
|
||||
@@ -87,7 +87,7 @@ when what it hangs from is gone. A block whose states follow from its neighbors,
|
||||
clients see it change right away.
|
||||
|
||||
`on_state_change` runs when the block's states change and it stays the same block: a script's `ctx.world.set_state`, a
|
||||
rule, a liquid's level. `before` is every state as it was, by name, and `ctx.world.get_state` gives what they are now.
|
||||
rule, a fluid's level. `before` is every state as it was, by name, and `ctx.world.get_state` gives what they are now.
|
||||
It's how a block reacts to a state something else sets on it, like a machine another mod's script turns off. It runs
|
||||
right away, before the blocks beside it are told, like `on_create`. Replacing the block with `set_block` runs `on_break`
|
||||
and `on_create` instead, and setting a state to what it already is runs nothing.
|
||||
@@ -296,20 +296,20 @@ catch up. If it's further behind than that, it skips the rest and logs a warning
|
||||
|
||||
`mods/bworld/scripts/server.ts` registers these, and any mod that depends on `bworld` can use them:
|
||||
|
||||
| Component | Params | What it does |
|
||||
| ----------------------- | --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `bworld:storage` | `rows` | A chest: `rows` × 9 slots, dropped when it's broken. |
|
||||
| `bworld:shipping_bin` | `rows` | Like `bworld:storage`, and every morning at 06:00 sells what's in it with a `base_sell_value` for coins. |
|
||||
| `bworld:furnace` | none | Smelts with `furnace` and `fuel` [recipes](../resources/recipes.md). Needs `lit` and `facing` states, see `blocks/furnace.json`. |
|
||||
| `bworld:smithing_table` | none | Upgrades tools with `smithing` recipes. |
|
||||
| `bworld:stonecutter` | none | Cuts one item into another with `bworld:stonecutting` recipes, picked from a list. |
|
||||
| `bworld:hoeable` | `tool`, `into` | Right clicking with a tool of type `tool` turns it into `into`, like a hoe on grass. |
|
||||
| `bworld:farmland` | none | Watered with the watering can, dries out every morning. |
|
||||
| `bworld:crop` | see [Crops](crops.md) | Grows a stage every morning its farmland was watered. |
|
||||
| `bworld:crop_top` | see [Crops](crops.md) | The top half of a two block crop, like rice. |
|
||||
| `bworld:water_source` | none | Refills the watering can. |
|
||||
| `bworld:fishable` | `loot_table`? | The fishing rod's hook floats on it and fish bite there when it lands in it. What's caught is `loot_table`, `bworld:gameplay/fishing` when not given, built from the fish whose `liquid` is this block. |
|
||||
| `bworld:trapdoor` | none | Opens and closes when right clicked, and is placed like Minecraft's (both are rules, in the shared script). Needs `facing` (2 bits), `half` and `open` states, see `blocks/oak_trapdoor.json`. |
|
||||
| `bworld:fence` | `connects_to` | Joins the blocks in the tag `connects_to` and sturdy sides of other blocks, except leaves. Needs the `north`, `east`, `south` and `west` states. |
|
||||
| Component | Params | What it does |
|
||||
| ----------------------- | --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `bworld:storage` | `rows` | A chest: `rows` × 9 slots, dropped when it's broken. |
|
||||
| `bworld:shipping_bin` | `rows` | Like `bworld:storage`, and every morning at 06:00 sells what's in it with a `base_sell_value` for coins. |
|
||||
| `bworld:furnace` | none | Smelts with `furnace` and `fuel` [recipes](../resources/recipes.md). Needs `lit` and `facing` states, see `blocks/furnace.json`. |
|
||||
| `bworld:smithing_table` | none | Upgrades tools with `smithing` recipes. |
|
||||
| `bworld:stonecutter` | none | Cuts one item into another with `bworld:stonecutting` recipes, picked from a list. |
|
||||
| `bworld:hoeable` | `tool`, `into` | Right clicking with a tool of type `tool` turns it into `into`, like a hoe on grass. |
|
||||
| `bworld:farmland` | none | Watered with the watering can, dries out every morning. |
|
||||
| `bworld:crop` | see [Crops](crops.md) | Grows a stage every morning its farmland was watered. |
|
||||
| `bworld:crop_top` | see [Crops](crops.md) | The top half of a two block crop, like rice. |
|
||||
| `bworld:water_source` | none | Refills the watering can. |
|
||||
| `bworld:fishable` | `loot_table`? | The fishing rod's hook floats on it and fish bite there when it lands in it. What's caught is `loot_table`, `bworld:gameplay/fishing` when not given, built from the fish whose `fluid` is this block. |
|
||||
| `bworld:trapdoor` | none | Opens and closes when right clicked, and is placed like Minecraft's (both are rules, in the shared script). Needs `facing` (2 bits), `half` and `open` states, see `blocks/oak_trapdoor.json`. |
|
||||
| `bworld:fence` | `connects_to` | Joins the blocks in the tag `connects_to` and sturdy sides of other blocks, except leaves. Needs the `north`, `east`, `south` and `west` states. |
|
||||
|
||||
Item components work the same way, see [Items](../items/index.md#item-components).
|
||||
|
||||
@@ -53,7 +53,7 @@ there will be holes in the world behind them.
|
||||
|
||||
| Field | Default | Meaning |
|
||||
| ---------------------- | ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `collision` | `true` | Whether entities bump into it. Plants set `false`, and liquids default to it. |
|
||||
| `collision` | `true` | Whether entities bump into it. Plants set `false`, and fluids default to it. |
|
||||
| `shape` | its model | The boxes players point at and entities bump into, in pixels. See [Shapes](../resources/models.md#shapes). |
|
||||
| `collision_shape` | `shape` | What entities bump into when it's different, up to 24 pixels high. See [Collision Shapes](../resources/models.md#collision-shapes). |
|
||||
| `mining.toughness` | unbreakable | Seconds to break by hand. Without `mining`, a block can't be broken. |
|
||||
@@ -65,13 +65,13 @@ there will be holes in the world behind them.
|
||||
| `item` | `true` | Also register an item that places it, with the same id. To give that item more, see [Block Items](../items/index.md#block-items). |
|
||||
| `interactive` | `false` | Right clicking it does something, like opening a screen, so clients don't guess that it places a block. |
|
||||
| `scrollable` | `false` | The mouse wheel turned while looking at it goes to its components' `on_scroll` instead of the hotbar, see [Scrolling](components.md#scrolling). |
|
||||
| `replaceable` | `false` | Placing a block into it replaces it, like water. Liquids default to `true`. |
|
||||
| `replaceable` | `false` | Placing a block into it replaces it, like water. Fluids default to `true`. |
|
||||
| `place_in` | see meaning | Block ids or [tags](../resources/tags.md) it can be placed into, replacing them, like `["bworld:water"]` for rice. Air and `replaceable` blocks when not set. |
|
||||
| `place_on` | anywhere | Block ids or [tags](../resources/tags.md) it can stand on, like `["bworld:hoed_dirt"]` for crops or `["#bworld:logs"]`. See [below](#placement). |
|
||||
| `states` | none | Named [state](states.md) bits, like a crop's age. |
|
||||
| `components` | none | [Components](components.md) and their parameters. |
|
||||
| `item_display` | none | Shows an item on top of it, see [Tile Data](tiledata.md#showing-an-item). |
|
||||
| `liquid` | none | Makes it a liquid, like water. See [Liquids](#liquids). |
|
||||
| `fluid` | none | Makes it a fluid, like water. See [Fluids](#fluids). |
|
||||
|
||||
A block's definition goes to every player, since clients need it to draw the block, time breaking and collide with it.
|
||||
It can't hold functions.
|
||||
@@ -129,11 +129,10 @@ crop when its farmland is dug up.
|
||||
|
||||
Food that places a block, like potatoes, is planted when clicked somewhere it can go and eaten otherwise.
|
||||
|
||||
## Liquids
|
||||
## Fluids
|
||||
|
||||
A block with `liquid` is a liquid, like water. The engine draws it like Luanti's liquids: each corner of its surface is
|
||||
as high as the liquid around it, so it slopes down from a source to where it flows, and it thins out to a film at the
|
||||
shore.
|
||||
A block with `fluid` is a fluid, like water. The engine draws it like Luanti's fluids: each corner of its surface is as
|
||||
high as the fluid around it, so it slopes down from a source to where it flows, and it thins out to a film at the shore.
|
||||
|
||||
`mods/oily/blocks/oil.json`:
|
||||
|
||||
@@ -145,7 +144,7 @@ shore.
|
||||
"textures": { "still": "oily:oil_still", "flowing": "oily:oil_flowing" },
|
||||
"render_layer": "translucent",
|
||||
"alpha": 0.9,
|
||||
"liquid": { "range": 4 }
|
||||
"fluid": { "range": 4 }
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -154,9 +153,9 @@ shore.
|
||||
top. `flowing` is on its sides and on a sloping top, turned to run downhill. A block shows as many of `flowing`'s
|
||||
pixels across as `still` has, so a 32×32 `flowing` next to a 16×16 `still` spreads over two blocks. With one texture,
|
||||
it's all of that texture on every side. Either can be [animated](../resources/textures.md#animated-textures).
|
||||
- `liquid.range` is how many blocks it flows from a source, 1 to 7, and `7` when not set.
|
||||
- `liquid.ticks` is how many ticks each step of its flow takes, `5` when not set, like Minecraft's water. Lava's is 30.
|
||||
- `liquid.renewable`, `true` when not set: a spot with two sources beside it and something solid or a source under it
|
||||
- `fluid.range` is how many blocks it flows from a source, 1 to 7, and `7` when not set.
|
||||
- `fluid.ticks` is how many ticks each step of its flow takes, `5` when not set, like Minecraft's water. Lava's is 30.
|
||||
- `fluid.renewable`, `true` when not set: a spot with two sources beside it and something solid or a source under it
|
||||
becomes a source too, like Minecraft's infinite water.
|
||||
- It gets a `level` [state](states.md) of its own, after any it lists: `0` is a source, a full block, and `1` up to
|
||||
`range` as it flows away from one, each lower.
|
||||
@@ -168,43 +167,43 @@ shore.
|
||||
|
||||
### Flowing
|
||||
|
||||
Liquids flow on the server like Minecraft's water. When a block next to a liquid changes, the cells around it are looked
|
||||
Fluids flow on the server like Minecraft's water. When a block next to a fluid changes, the cells around it are looked
|
||||
at again after its `ticks`, and each works out what it should be:
|
||||
|
||||
- A source stays a source.
|
||||
- With the same liquid above, it's falling, and drawn full.
|
||||
- Otherwise it's one level more than the lowest of the liquid beside it that spreads sideways, and gone once that's past
|
||||
`range`. Sources spread sideways, and flowing liquid only does when it's on something solid: over air or more of
|
||||
- With the same fluid above, it's falling, and drawn full.
|
||||
- Otherwise it's one level more than the lowest of the fluid beside it that spreads sideways, and gone once that's past
|
||||
`range`. Sources spread sideways, and flowing fluid only does when it's on something solid: over air or more of
|
||||
itself, it falls instead.
|
||||
|
||||
So it spreads one block a step, falls off edges and spreads again where it lands, and drains away a step at a time once
|
||||
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.
|
||||
|
||||
`ctx.world.liquid_flow(x, y, z)` says which way the liquid in a block flows, like Minecraft's `FluidState.getFlow`: a
|
||||
vector 1 long toward the lower liquid beside it, and down where it falls against a wall, or `{ x: 0, y: 0, z: 0 }` where
|
||||
`ctx.world.fluid_flow(x, y, z)` says which way the fluid in a block flows, like Minecraft's `FluidState.getFlow`: a
|
||||
vector 1 long toward the lower fluid beside it, and down where it falls against a wall, or `{ x: 0, y: 0, z: 0 }` where
|
||||
it's still or there's none. It's the same flow that pushes players and items along, for machines it turns, like a water
|
||||
wheel.
|
||||
|
||||
### Waterlogging
|
||||
|
||||
A block that isn't a liquid can hold one, like Minecraft's waterlogged fences. `waterloggable` names the liquid:
|
||||
A block that isn't a fluid can hold one, like Minecraft's waterlogged fences. `waterloggable` names the fluid:
|
||||
|
||||
```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 gets a `waterlogged` [state](states.md) of its own, after any it lists: `1` while it holds a source of the fluid.
|
||||
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
|
||||
- Placed into a source of the fluid, it holds it. Placed into flowing fluid 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.
|
||||
- Using the fluid's item on it, like a water bucket, fills it. An item that aims at fluids, like a bucket, uses the
|
||||
fluid in it instead of the block, so a bucket takes the water back out and the block stays.
|
||||
- It's a source of the fluid that never drains: the fluid flows out of it, and entities swim and are pushed in it.
|
||||
Flowing fluid doesn't flow into it.
|
||||
- Broken, it leaves a source of the fluid behind.
|
||||
- Raycasts with `fluids` other than `"none"` hit the fluid in it, filling the block, and say the fluid's id.
|
||||
|
||||
The base game's wooden fences are waterloggable, and Farmer's Delight's rice is `waterlogged_only`.
|
||||
|
||||
@@ -228,22 +227,22 @@ hook uses it to see where it lands, and with a player's
|
||||
[`eye_position` and `look_direction`](../entities/players.md#the-player-api) it finds what they're looking at:
|
||||
|
||||
```ts
|
||||
const hit = ctx.world.raycast(from, to, { liquids: "any" });
|
||||
const hit = ctx.world.raycast(from, to, { fluids: "any" });
|
||||
if (hit && "bworld:fishable" in ctx.blocks.components(hit.id)) {
|
||||
// hit.x, hit.y, hit.z is the block, hit.face the side it went in by, hit.position the exact point
|
||||
}
|
||||
```
|
||||
|
||||
It hits what entities bump into, so it goes through plants and other blocks with `"collision": false`. `liquids` is like
|
||||
It hits what entities bump into, so it goes through plants and other blocks with `"collision": false`. `fluids` is like
|
||||
Minecraft's `ClipContext.Fluid`:
|
||||
|
||||
| `liquids` | Hits |
|
||||
| ------------------ | ------------------------------------------- |
|
||||
| `"none"` (default) | No liquids, it goes through them. |
|
||||
| `"source_only"` | Liquid sources, going through flowing ones. |
|
||||
| `"any"` | Every liquid. |
|
||||
| `fluids` | Hits |
|
||||
| ------------------ | ------------------------------------------ |
|
||||
| `"none"` (default) | No fluids, it goes through them. |
|
||||
| `"source_only"` | Fluid sources, going through flowing ones. |
|
||||
| `"any"` | Every fluid. |
|
||||
|
||||
A liquid is hit at its surface, which is lower where it flows. It returns `undefined` when nothing is in the way.
|
||||
A fluid is hit at its surface, which is lower where it flows. It returns `undefined` when nothing is in the way.
|
||||
|
||||
`ctx.blocks.components(id)` gives the components a block's json lists and their params, like `ctx.items.components`, and
|
||||
`ctx.blocks.exists(id)` whether a block exists.
|
||||
|
||||
@@ -71,7 +71,7 @@ const eye = player.eye_position;
|
||||
const look = player.look_direction;
|
||||
const reach = player.attribute("bworld:block_interaction_range");
|
||||
const to = { x: eye.x + look.x * reach, y: eye.y + look.y * reach, z: eye.z + look.z * reach };
|
||||
const hit = ctx.world.raycast(eye, to, { liquids: "source_only" });
|
||||
const hit = ctx.world.raycast(eye, to, { fluids: "source_only" });
|
||||
```
|
||||
|
||||
## Health and Hunger
|
||||
|
||||
@@ -53,7 +53,7 @@ down to 0 is gone.
|
||||
| `data_textures` | none | Other textures, picked by the stack's `data`, see below. |
|
||||
| `model` | none | A [model](../resources/models.md#item-models) it's drawn with instead of its texture. |
|
||||
| `worn` | none | How it looks worn on a player, see [below](#worn-models). |
|
||||
| `targets_liquids` | `false` | Players holding it aim at liquids, like buckets and fishing rods. Others aim through. |
|
||||
| `targets_fluids` | `false` | Players holding it aim at fluids, like buckets and fishing rods. Others aim through. |
|
||||
| `components` | none | [Item components](#item-components) and their parameters. |
|
||||
|
||||
### Textures From Data
|
||||
|
||||
@@ -134,9 +134,9 @@ does nothing unless they hold.
|
||||
## Fishing
|
||||
|
||||
The fishing tables aren't files: the base game builds them from every item with the `bworld:fish` component, so a mod
|
||||
adds a fish by giving its item the component. A fish lives in one liquid, water unless its `liquid` says otherwise, and
|
||||
is in that liquid's table: the `loot_table` of its `bworld:fishable` component, `bworld:gameplay/fishing` when it
|
||||
doesn't name one. Water's is `bworld:gameplay/fishing` and lava's is `bworld:gameplay/lava_fishing`:
|
||||
adds a fish by giving its item the component. A fish lives in one fluid, water unless its `fluid` says otherwise, and is
|
||||
in that fluid's table: the `loot_table` of its `bworld:fishable` component, `bworld:gameplay/fishing` when it doesn't
|
||||
name one. Water's is `bworld:gameplay/fishing` and lava's is `bworld:gameplay/lava_fishing`:
|
||||
|
||||
```json
|
||||
"components": {
|
||||
@@ -153,7 +153,7 @@ doesn't name one. Water's is `bworld:gameplay/fishing` and lava's is `bworld:gam
|
||||
| ------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `weight` | How often it's caught against the other fish, 1 when not given. |
|
||||
| `seasons` | The [seasons](../worldgen/biomes.md#seasons) it's caught in where the hook is, every one when not given. |
|
||||
| `liquid` | The `bworld:fishable` block it's caught in, `bworld:water` when not given, like `"bworld:lava"`. |
|
||||
| `fluid` | The `bworld:fishable` block it's caught in, `bworld:water` when not given, like `"bworld:lava"`. |
|
||||
| `biomes` | The [biomes](../worldgen/biomes.md#biome-ids) and [biome tags](../worldgen/biomes.md#biome-tags) it's caught in where the hook is, every one when not given. |
|
||||
| `time` | The hours it's caught at on a 24 hour clock, from `from` up to `to`, like `6.5` for 06:30. When `from` is past `to` it wraps around midnight. All day when not given. |
|
||||
| `minigame` | What the player does to land it once it's reeled in, in a screen that holds the fish until they win. Closing it lets the fish get away. Caught right away when not given. The base game's are `"test"`, a Catch button, `"hold"`, a bar that fills while the mouse is held and empties while it isn't: full lands the fish, empty loses it, `"valley"`, Stardew Valley's, where holding the mouse lifts a bar to keep the fish in it, `"ring"`, where a green ring grows out toward gray ones and clicking while it's inside each lands the fish, `"maze"`, where the mouse goes from the start to the end of a maze without touching its walls, in 8 seconds, and `"chess"`, a mate in one from Lichess's puzzles to find in 8 seconds. |
|
||||
|
||||
@@ -161,15 +161,15 @@ Built in names work with or without `minecraft:`.
|
||||
| `simple_block` | `to_place` | One block, where it can stand (its `place_on`). |
|
||||
| `block_column` | `layers` (`height`, `provider`), `direction`, `allowed_placement`, `prioritize_tip` | Layers stacked `up` (or `down`), cut short where `allowed_placement` doesn't hold (air when not set). |
|
||||
|
||||
| Placement modifier | Fields | Does |
|
||||
| ------------------------ | ----------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `count` | `count` | Each position becomes `count` of them. |
|
||||
| `rarity_filter` | `chance` | Keeps one in `chance`. |
|
||||
| `in_square` | | Moves each to a random spot in the chunk, across. |
|
||||
| `random_offset` | `xz_spread`, `y_spread` | Moves each by those amounts. |
|
||||
| `height_range` | `height` | Sets the height: an anchor or `constant`/`uniform` of anchors. |
|
||||
| `heightmap` | `heightmap` | On top of the column: `WORLD_SURFACE_WG`, `WORLD_SURFACE` and `MOTION_BLOCKING` on its highest block, `OCEAN_FLOOR_WG` and `OCEAN_FLOOR` on its highest that isn't a liquid. |
|
||||
| `block_predicate_filter` | `predicate` | Keeps the positions where a block predicate holds. |
|
||||
| Placement modifier | Fields | Does |
|
||||
| ------------------------ | ----------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `count` | `count` | Each position becomes `count` of them. |
|
||||
| `rarity_filter` | `chance` | Keeps one in `chance`. |
|
||||
| `in_square` | | Moves each to a random spot in the chunk, across. |
|
||||
| `random_offset` | `xz_spread`, `y_spread` | Moves each by those amounts. |
|
||||
| `height_range` | `height` | Sets the height: an anchor or `constant`/`uniform` of anchors. |
|
||||
| `heightmap` | `heightmap` | On top of the column: `WORLD_SURFACE_WG`, `WORLD_SURFACE` and `MOTION_BLOCKING` on its highest block, `OCEAN_FLOOR_WG` and `OCEAN_FLOOR` on its highest that isn't a fluid. |
|
||||
| `block_predicate_filter` | `predicate` | Keeps the positions where a block predicate holds. |
|
||||
|
||||
- **Block predicates:** `matching_blocks` (`blocks`: ids and #tags), `solid`, `replaceable`, `would_survive` (`state`),
|
||||
`not`, `all_of`, `any_of` and `true`. Each takes an `offset`.
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
"render_layer": "cutout",
|
||||
"light_emission": 15,
|
||||
"item": false,
|
||||
"liquid": { "range": 3, "ticks": 30, "renewable": false },
|
||||
"fluid": { "range": 3, "ticks": 30, "renewable": false },
|
||||
"components": {
|
||||
"bworld:bucketable": {
|
||||
"into": "bworld:lava_bucket"
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
"light_opacity": 1,
|
||||
"alpha": 0.8,
|
||||
"item": false,
|
||||
"liquid": {},
|
||||
"fluid": {},
|
||||
"components": {
|
||||
"bworld:water_source": {},
|
||||
"bworld:fishable": {},
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
"item": {
|
||||
"id": "bworld:bucket",
|
||||
"texture": "bworld:bucket",
|
||||
"targets_liquids": true,
|
||||
"targets_fluids": true,
|
||||
"max_stack": 16,
|
||||
"base_sell_value": 180
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
"item": {
|
||||
"id": "bworld:copper_watering_can",
|
||||
"texture": "bworld:copper_watering_can",
|
||||
"targets_liquids": true,
|
||||
"targets_fluids": true,
|
||||
"tool": {
|
||||
"type": "watering_can",
|
||||
"tier": 1,
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
"item": {
|
||||
"id": "bworld:gold_watering_can",
|
||||
"texture": "bworld:gold_watering_can",
|
||||
"targets_liquids": true,
|
||||
"targets_fluids": true,
|
||||
"tool": {
|
||||
"type": "watering_can",
|
||||
"tier": 0,
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
"item": {
|
||||
"id": "bworld:iron_watering_can",
|
||||
"texture": "bworld:iron_watering_can",
|
||||
"targets_liquids": true,
|
||||
"targets_fluids": true,
|
||||
"tool": {
|
||||
"type": "watering_can",
|
||||
"tier": 2,
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
},
|
||||
"components": {
|
||||
"bworld:fish": {
|
||||
"liquid": "bworld:lava",
|
||||
"fluid": "bworld:lava",
|
||||
"weight": 2,
|
||||
"minigame": "valley",
|
||||
"difficulty": 90
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
},
|
||||
"components": {
|
||||
"bworld:fish": {
|
||||
"liquid": "bworld:lava",
|
||||
"fluid": "bworld:lava",
|
||||
"weight": 8,
|
||||
"minigame": "ring",
|
||||
"difficulty": 61
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
},
|
||||
"components": {
|
||||
"bworld:fish": {
|
||||
"liquid": "bworld:lava",
|
||||
"fluid": "bworld:lava",
|
||||
"weight": 10,
|
||||
"minigame": "valley",
|
||||
"difficulty": 50
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
},
|
||||
"components": {
|
||||
"bworld:fish": {
|
||||
"liquid": "bworld:lava",
|
||||
"fluid": "bworld:lava",
|
||||
"weight": 1,
|
||||
"minigame": "maze",
|
||||
"difficulty": 110
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
"item": {
|
||||
"id": "bworld:stone_watering_can",
|
||||
"texture": "bworld:stone_watering_can",
|
||||
"targets_liquids": true,
|
||||
"targets_fluids": true,
|
||||
"tool": {
|
||||
"type": "watering_can",
|
||||
"tier": 1,
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
},
|
||||
"components": {
|
||||
"bworld:fish": {
|
||||
"liquid": "bworld:lava",
|
||||
"fluid": "bworld:lava",
|
||||
"weight": 4,
|
||||
"minigame": "ring",
|
||||
"difficulty": 81
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
},
|
||||
"components": {
|
||||
"bworld:fish": {
|
||||
"liquid": "bworld:lava",
|
||||
"fluid": "bworld:lava",
|
||||
"weight": 0.1,
|
||||
"minigame": "hold",
|
||||
"difficulty": 50
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
},
|
||||
"components": {
|
||||
"bworld:fish": {
|
||||
"liquid": "bworld:lava",
|
||||
"fluid": "bworld:lava",
|
||||
"weight": 10,
|
||||
"minigame": "chess",
|
||||
"difficulty": 70
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
"item": {
|
||||
"id": "bworld:wood_watering_can",
|
||||
"texture": "bworld:wood_watering_can",
|
||||
"targets_liquids": true,
|
||||
"targets_fluids": true,
|
||||
"tool": {
|
||||
"type": "watering_can",
|
||||
"tier": 0,
|
||||
|
||||
@@ -15,9 +15,9 @@ const FISH = "bworld:fish";
|
||||
// the menu a fish's minigame is played in. client.ts gives it the minigame's screen, see minigames/mod.ts
|
||||
const MINIGAME_MENU = "bworld:fishing_minigame";
|
||||
// what's caught in a fishable block whose bworld:fishable doesn't name a table, like water. each table is built from
|
||||
// the items with bworld:fish that live in its liquid, see register_fishing
|
||||
// the items with bworld:fish that live in its fluid, see register_fishing
|
||||
const FISHING_TABLE = "bworld:gameplay/fishing";
|
||||
// the liquid a fish lives in when it doesn't say
|
||||
// the fluid a fish lives in when it doesn't say
|
||||
const WATER = "bworld:water";
|
||||
// further than this from the hook, the line snaps
|
||||
const LINE_LENGTH = 16;
|
||||
@@ -65,7 +65,7 @@ interface FishParams {
|
||||
// how hard its minigame is, 10 when not given. what it changes is up to the minigame
|
||||
difficulty?: number;
|
||||
// the fishable block it's caught in, bworld:water when not given. it's in that block's loot table
|
||||
liquid?: string;
|
||||
fluid?: string;
|
||||
}
|
||||
|
||||
interface Cast {
|
||||
@@ -111,7 +111,7 @@ export function register_fishing(ctx: ServerContext) {
|
||||
const fly = (cast: Cast, velocity: Position, now: number) => {
|
||||
const from = cast.position;
|
||||
const to = { x: from.x + velocity.x, y: from.y + velocity.y, z: from.z + velocity.z };
|
||||
const hit = ctx.world.raycast(from, to, { liquids: "any" });
|
||||
const hit = ctx.world.raycast(from, to, { fluids: "any" });
|
||||
if (!hit) {
|
||||
cast.position = to;
|
||||
velocity.x *= DRAG;
|
||||
@@ -167,7 +167,7 @@ export function register_fishing(ctx: ServerContext) {
|
||||
return from <= to ? hour >= from && hour < to : hour >= from || hour < to;
|
||||
});
|
||||
|
||||
// every item with bworld:fish is in its liquid's fishing table, caught in its seasons, at its hours and in its
|
||||
// every item with bworld:fish is in its fluid's fishing table, caught in its seasons, at its hours and in its
|
||||
// biomes
|
||||
ctx.components.register_item<FishParams>(FISH, {});
|
||||
const tables = new Map<string, { item: string; weight: number; conditions: { condition: string }[] }[]>([
|
||||
@@ -186,12 +186,12 @@ export function register_fishing(ctx: ServerContext) {
|
||||
if (fish.seasons) conditions.push({ condition: "bworld:season", seasons: fish.seasons });
|
||||
if (fish.time) conditions.push({ condition: "bworld:time", ...fish.time });
|
||||
if (fish.biomes) conditions.push({ condition: "bworld:biome", biomes: fish.biomes });
|
||||
const liquid = fish.liquid ?? WATER;
|
||||
if (!ctx.blocks.exists(liquid) || !(FISHABLE in ctx.blocks.components(liquid))) {
|
||||
throw new Error(`${id} is caught in ${liquid}, which isn't a block with ${FISHABLE}`);
|
||||
const fluid = fish.fluid ?? WATER;
|
||||
if (!ctx.blocks.exists(fluid) || !(FISHABLE in ctx.blocks.components(fluid))) {
|
||||
throw new Error(`${id} is caught in ${fluid}, which isn't a block with ${FISHABLE}`);
|
||||
}
|
||||
const entry = { item: id, weight: fish.weight ?? 1, conditions };
|
||||
const table = table_of(liquid);
|
||||
const table = table_of(fluid);
|
||||
tables.set(table, [...tables.get(table) ?? [], entry]);
|
||||
return [entry];
|
||||
});
|
||||
|
||||
@@ -815,11 +815,11 @@ function register_buckets(ctx: ServerContext) {
|
||||
if (bucket?.id !== BUCKET) return false;
|
||||
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
|
||||
// only a fluid'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
|
||||
// the fluid in a waterlogged block, like a fence in water, which stays
|
||||
ctx.world.set_state(x, y, z, "waterlogged", 0);
|
||||
}
|
||||
bucket.count -= 1;
|
||||
|
||||
@@ -12,7 +12,7 @@ made down its left side, the category and the page between their arrows, then th
|
||||
them, Backspace goes back, and Escape or the inventory key (E) returns to the inventory.
|
||||
|
||||
It reads every mod's recipes with `ctx.recipes.list()`. Fish, the items with the base game's `bworld:fish` component,
|
||||
get a Fishing category too: clicking one shows its seasons, biomes, hours and the liquid it's caught in, read with
|
||||
get a Fishing category too: clicking one shows its seasons, biomes, hours and the fluid it's caught in, read with
|
||||
`ctx.items.components`, and the fishing rod's uses are every fish. The crab pot gets one too: a recipe for each bait in
|
||||
its `bworld:crabpot` component and each kind of water, showing everything that water's loot table can catch and how
|
||||
likely each is, read with `ctx.blocks.components` and `ctx.loot.table`.
|
||||
|
||||
@@ -131,7 +131,7 @@ function base_categories(tags: TagApi): RecipeCategory[] {
|
||||
}`,
|
||||
`Biomes: ${fish.biomes?.map(biome_name).join(", ") ?? "all"}`,
|
||||
`Time: ${fish.time ? `${clock(fish.time.from)} to ${clock(fish.time.to)}` : "all day"}`,
|
||||
`In: ${biome_name(fish.liquid ?? "bworld:water")}`,
|
||||
`In: ${biome_name(fish.fluid ?? "bworld:water")}`,
|
||||
];
|
||||
lines.forEach((line, i) => layout.text(line, 24, 1 + i * 13));
|
||||
},
|
||||
@@ -169,7 +169,7 @@ interface FishParams {
|
||||
seasons?: string[];
|
||||
biomes?: string[];
|
||||
time?: { from: number; to: number };
|
||||
liquid?: string;
|
||||
fluid?: string;
|
||||
}
|
||||
|
||||
export function fishing_recipes(ctx: ClientContext): Recipe[] {
|
||||
@@ -244,7 +244,7 @@ function title_case(name: string): string {
|
||||
return name.split("_").map((word) => word[0].toUpperCase() + word.slice(1)).join(" ");
|
||||
}
|
||||
|
||||
// a biome, liquid or #biome tag by its name: "bworld:swamp" is "Swamp", "#bworld:is_ocean" is "Ocean"
|
||||
// a biome, fluid or #biome tag by its name: "bworld:swamp" is "Swamp", "#bworld:is_ocean" is "Ocean"
|
||||
function biome_name(id: string): string {
|
||||
return title_case(id.replace(/^#/, "").split(":")[1].replace(/^is_/, ""));
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ import type { BlockRegistry } from "$/common/everything_registry.ts";
|
||||
import type { ItemStack } from "$/common/inventory.ts";
|
||||
import type { Shape } from "$/common/block_shapes.ts";
|
||||
import type { Season } from "$/common/worldgen/biomes.ts";
|
||||
import { type Collisions, type LiquidInCell, physics } from "$/common/physics.ts";
|
||||
import { type Collisions, type FluidInCell, physics } from "$/common/physics.ts";
|
||||
import type { EntityData, EntityEvent, EntityInfo } from "$/common/protocol.ts";
|
||||
import type { ServerPlayer } from "../player.ts";
|
||||
|
||||
@@ -70,9 +70,9 @@ export interface EntityLevel {
|
||||
// what it bumps into at a block, see common/physics.ts
|
||||
collision_shape(x: number, y: number, z: number): boolean | Shape;
|
||||
// water, and other blocks you're in instead of on
|
||||
is_liquid(x: number, y: number, z: number): boolean;
|
||||
// the liquid there and how high it is, for liquids pushing things along
|
||||
liquid_at(x: number, y: number, z: number): LiquidInCell | undefined;
|
||||
is_fluid(x: number, y: number, z: number): boolean;
|
||||
// the fluid there and how high it is, for fluids pushing things along
|
||||
fluid_at(x: number, y: number, z: number): FluidInCell | undefined;
|
||||
get_block_info(x: number, y: number, z: number): BlockRegistry | undefined;
|
||||
// the column's biome, like "bworld:plains"
|
||||
biome_at(x: number, z: number): string;
|
||||
@@ -241,10 +241,10 @@ export abstract class Entity {
|
||||
this.fall_distance = 0;
|
||||
this.fire = 0;
|
||||
}
|
||||
if (this.pushed_by_liquid) {
|
||||
physics.push_by_liquid(
|
||||
if (this.pushed_by_fluid) {
|
||||
physics.push_by_fluid(
|
||||
this,
|
||||
(x, y, z) => this.level.liquid_at(x, y, z),
|
||||
(x, y, z) => this.level.fluid_at(x, y, z),
|
||||
(x, y, z) => this.level.collision_shape(x, y, z),
|
||||
false,
|
||||
);
|
||||
@@ -266,18 +266,18 @@ export abstract class Entity {
|
||||
}
|
||||
}
|
||||
|
||||
// flowing liquid pushes it along, like minecraft's isPushedByFluid
|
||||
get pushed_by_liquid() {
|
||||
// flowing fluid pushes it along, like minecraft's isPushedByFluid
|
||||
get pushed_by_fluid() {
|
||||
return true;
|
||||
}
|
||||
|
||||
update_in_water() {
|
||||
this.in_water = physics.in_water(this, (x, y, z) => this.level.is_liquid(x, y, z));
|
||||
this.in_water = physics.in_water(this, (x, y, z) => this.level.is_fluid(x, y, z));
|
||||
}
|
||||
|
||||
// its eyes are in water, so it can't breathe
|
||||
get eyes_in_water() {
|
||||
return physics.eyes_in_water(this, this.eye_height, (x, y, z) => this.level.is_liquid(x, y, z));
|
||||
return physics.eyes_in_water(this, this.eye_height, (x, y, z) => this.level.is_fluid(x, y, z));
|
||||
}
|
||||
|
||||
// falls and moves by its velocity for one tick (see move_body), and counts how far it fell. returns what it hit
|
||||
|
||||
@@ -6,7 +6,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 { liquid_block, liquid_in_cell } from "$/common/utils.ts";
|
||||
import { fluid_block, fluid_in_cell } from "$/common/utils.ts";
|
||||
import { biome_season } from "$/common/worldgen/biomes.ts";
|
||||
import type { ItemStack } from "$/common/inventory.ts";
|
||||
import type { EntityEvent, EntityInfo, ServerMessage } from "$/common/protocol.ts";
|
||||
@@ -99,9 +99,9 @@ export class EntityManager implements EntityLevel {
|
||||
return this.#host.world.collision_shape(x, y, z);
|
||||
}
|
||||
|
||||
is_liquid(x: number, y: number, z: number) {
|
||||
is_fluid(x: number, y: number, z: number) {
|
||||
const world = this.#host.world;
|
||||
return liquid_block(world.get_block_value(x, y, z), world.get_block_info(x, y, z)) !== undefined;
|
||||
return fluid_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) {
|
||||
@@ -116,9 +116,9 @@ export class EntityManager implements EntityLevel {
|
||||
return biome_season(this.biome_at(x, z), this.#host.world.time);
|
||||
}
|
||||
|
||||
liquid_at(x: number, y: number, z: number) {
|
||||
fluid_at(x: number, y: number, z: number) {
|
||||
const world = this.#host.world;
|
||||
return liquid_in_cell(world.get_block_value(x, y, z), world.get_block_info(x, y, z));
|
||||
return fluid_in_cell(world.get_block_value(x, y, z), world.get_block_info(x, y, z));
|
||||
}
|
||||
|
||||
nearest_player(x: number, y: number, z: number, max_distance: number): ServerPlayer | undefined {
|
||||
|
||||
@@ -363,7 +363,7 @@ function is_alive(target: LivingEntity) {
|
||||
// undefined when the tries all fail
|
||||
export function random_stand_position(mob: Mob, range: number, vertical: number, tries = 10) {
|
||||
const level = mob.level;
|
||||
const free = (x: number, y: number, z: number) => !level.has_collision(x, y, z) && !level.is_liquid(x, y, z);
|
||||
const free = (x: number, y: number, z: number) => !level.has_collision(x, y, z) && !level.is_fluid(x, y, z);
|
||||
for (let i = 0; i < tries; i++) {
|
||||
const x = Math.floor(mob.x + (mob.random() * 2 - 1) * range);
|
||||
const z = Math.floor(mob.z + (mob.random() * 2 - 1) * range);
|
||||
|
||||
@@ -89,7 +89,7 @@ export class ItemEntity extends Entity {
|
||||
// like minecraft's ItemEntity.tick, gravity goes on its speed before it moves, not along the way like
|
||||
// move_body's. the floating numbers are made for that: an item bobbing up out of the water drops straight back
|
||||
// in, instead of hanging above it a tick longer and falling twice as fast
|
||||
if (physics.liquid_depth(this, (x, y, z) => level.liquid_at(x, y, z)) > FLOAT_DEPTH) {
|
||||
if (physics.fluid_depth(this, (x, y, z) => level.fluid_at(x, y, z)) > FLOAT_DEPTH) {
|
||||
this.vx *= WATER_DRAG;
|
||||
this.vz *= WATER_DRAG;
|
||||
if (this.vy < FLOAT_SPEED) this.vy += FLOAT_ACCELERATION;
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// liquids flowing, like minecraft's FlowingFluid: a change next to a liquid gets the cells around it looked at again
|
||||
// after the liquid's ticks, and each works out what it should be from its neighbors (see next_level). that changes
|
||||
// fluids flowing, like minecraft's FlowingFluid: a change next to a fluid gets the cells around it looked at again
|
||||
// after the fluid's ticks, and each works out what it should be from its neighbors (see next_level). that changes
|
||||
// more cells, and so it spreads one block a step, and drains away the same way once its source is gone
|
||||
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, liquid_block, set_state_value } from "$/common/utils.ts";
|
||||
import { fluid_block, get_state_value, set_state_value } from "$/common/utils.ts";
|
||||
|
||||
// what the flow needs of the world
|
||||
export interface LiquidWorld {
|
||||
// undefined where the chunk isn't loaded, so liquid stops at the edge of the world instead of making new chunks
|
||||
export interface FluidWorld {
|
||||
// undefined where the chunk isn't loaded, so fluid stops at the edge of the world instead of making new chunks
|
||||
value_at(x: number, y: number, z: number): number | undefined;
|
||||
// by numeric id
|
||||
block(nid: number): BlockRegistry | undefined;
|
||||
@@ -26,8 +26,8 @@ const MAX_PER_TICK = 4096;
|
||||
const HORIZONTAL = [[1, 0], [-1, 0], [0, 1], [0, -1]];
|
||||
const NEIGHBORS = [[0, 0, 0], [0, 1, 0], [0, -1, 0], [1, 0, 0], [-1, 0, 0], [0, 0, 1], [0, 0, -1]];
|
||||
|
||||
// a liquid in a cell: which, and its level
|
||||
interface Liquid {
|
||||
// a fluid in a cell: which, and its level
|
||||
interface Fluid {
|
||||
nid: number;
|
||||
block: BlockRegistry;
|
||||
level: number;
|
||||
@@ -35,12 +35,12 @@ interface Liquid {
|
||||
|
||||
const key = (x: number, y: number, z: number) => `${x},${y},${z}`;
|
||||
|
||||
export class LiquidFlow {
|
||||
#world: LiquidWorld;
|
||||
export class FluidFlow {
|
||||
#world: FluidWorld;
|
||||
// cells to look at, by key, with the tick they're due
|
||||
#due = new Map<string, { x: number; y: number; z: number; tick: number }>();
|
||||
|
||||
constructor(world: LiquidWorld) {
|
||||
constructor(world: FluidWorld) {
|
||||
this.#world = world;
|
||||
}
|
||||
|
||||
@@ -49,13 +49,13 @@ export class LiquidFlow {
|
||||
return this.#due.size;
|
||||
}
|
||||
|
||||
// a block changed at x y z: if there's liquid there or beside it, it and its neighbors are looked at again after
|
||||
// that liquid's ticks
|
||||
// a block changed at x y z: if there's fluid there or beside it, it and its neighbors are looked at again after
|
||||
// that fluid's ticks
|
||||
changed(x: number, y: number, z: number, now: number) {
|
||||
let ticks = Infinity;
|
||||
for (const [dx, dy, dz] of NEIGHBORS) {
|
||||
const liquid = this.#liquid(x + dx, y + dy, z + dz);
|
||||
if (liquid) ticks = Math.min(ticks, liquid.block.liquid!.ticks);
|
||||
const fluid = this.#fluid(x + dx, y + dy, z + dz);
|
||||
if (fluid) ticks = Math.min(ticks, fluid.block.fluid!.ticks);
|
||||
}
|
||||
if (ticks === Infinity) return;
|
||||
for (const [dx, dy, dz] of NEIGHBORS) {
|
||||
@@ -82,28 +82,28 @@ export class LiquidFlow {
|
||||
const value = this.#world.value_at(x, y, z);
|
||||
if (value === undefined) return;
|
||||
const nid = value & ID_MASK;
|
||||
const here = this.#liquid(x, y, z);
|
||||
const here = this.#fluid(x, y, z);
|
||||
if (here?.level === 0) return;
|
||||
// only air and the liquid's own flowing cells are flowed into
|
||||
// only air and the fluid's own flowing cells are flowed into
|
||||
if (!here && nid !== AIR) return;
|
||||
|
||||
// the liquid that would be here: what's here, or one from above or beside
|
||||
const candidates = here ? [here] : this.#liquids_around(x, y, z);
|
||||
for (const liquid of candidates) {
|
||||
const level = this.next_level(liquid, x, y, z);
|
||||
// the fluid that would be here: what's here, or one from above or beside
|
||||
const candidates = here ? [here] : this.#fluids_around(x, y, z);
|
||||
for (const fluid of candidates) {
|
||||
const level = this.next_level(fluid, x, y, z);
|
||||
if (level === undefined) continue;
|
||||
if (!here || here.level !== level) this.#set(liquid.block, x, y, z, level);
|
||||
if (!here || here.level !== level) this.#set(fluid.block, x, y, z, level);
|
||||
return;
|
||||
}
|
||||
if (here) this.#world.set_block(x, y, z, AIR_ID, 0);
|
||||
}
|
||||
|
||||
// what level the liquid would be at x y z, from the cells around it, or undefined for none there
|
||||
next_level(liquid: Liquid, x: number, y: number, z: number): number | undefined {
|
||||
const props = liquid.block.liquid!;
|
||||
// what level the fluid would be at x y z, from the cells around it, or undefined for none there
|
||||
next_level(fluid: Fluid, x: number, y: number, z: number): number | undefined {
|
||||
const props = fluid.block.fluid!;
|
||||
const same = (cx: number, cy: number, cz: number) => {
|
||||
const other = this.#liquid(cx, cy, cz);
|
||||
return other?.nid === liquid.nid ? other : undefined;
|
||||
const other = this.#fluid(cx, cy, cz);
|
||||
return other?.nid === fluid.nid ? other : undefined;
|
||||
};
|
||||
|
||||
// two sources beside it over something solid or a source make another, like minecraft's infinite water
|
||||
@@ -114,7 +114,7 @@ export class LiquidFlow {
|
||||
if (sources >= 2 && (below_block?.has_collision || same(x, y - 1, z)?.level === 0)) return 0;
|
||||
}
|
||||
|
||||
// falling, with more of it above. it's drawn full whatever its level, see liquid_mesh.ts
|
||||
// falling, with more of it above. it's drawn full whatever its level, see fluid_mesh.ts
|
||||
if (same(x, y + 1, z)) return 1;
|
||||
|
||||
let lowest = Infinity;
|
||||
@@ -123,7 +123,7 @@ export class LiquidFlow {
|
||||
const nz = z + dz;
|
||||
const neighbor = same(nx, y, nz);
|
||||
if (!neighbor || !this.#spreads_sideways(neighbor, nx, y, nz)) continue;
|
||||
// falling liquid spreads like a source once it lands
|
||||
// falling fluid spreads like a source once it lands
|
||||
const level = same(nx, y + 1, nz) ? 0 : neighbor.level;
|
||||
lowest = Math.min(lowest, level);
|
||||
}
|
||||
@@ -131,36 +131,36 @@ export class LiquidFlow {
|
||||
return level <= props.range ? level : undefined;
|
||||
}
|
||||
|
||||
// sources spread sideways, and flowing liquid that can't fall any further. over air or more of itself it falls
|
||||
// sources spread sideways, and flowing fluid that can't fall any further. over air or more of itself it falls
|
||||
// instead, like minecraft's isWaterHole
|
||||
#spreads_sideways(liquid: Liquid, x: number, y: number, z: number) {
|
||||
if (liquid.level === 0) return true;
|
||||
#spreads_sideways(fluid: Fluid, x: number, y: number, z: number) {
|
||||
if (fluid.level === 0) return true;
|
||||
const below = this.#world.value_at(x, y - 1, z);
|
||||
if (below === undefined) return false;
|
||||
const nid = below & ID_MASK;
|
||||
return nid !== AIR && nid !== liquid.nid;
|
||||
return nid !== AIR && nid !== fluid.nid;
|
||||
}
|
||||
|
||||
// the liquids above and beside x y z that could flow into it, above first
|
||||
#liquids_around(x: number, y: number, z: number): Liquid[] {
|
||||
const found: Liquid[] = [];
|
||||
// the fluids above and beside x y z that could flow into it, above first
|
||||
#fluids_around(x: number, y: number, z: number): Fluid[] {
|
||||
const found: Fluid[] = [];
|
||||
for (const [dx, dy, dz] of [[0, 1, 0], ...HORIZONTAL.map(([dx, dz]) => [dx, 0, dz])]) {
|
||||
const liquid = this.#liquid(x + dx, y + dy, z + dz);
|
||||
if (liquid && !found.some((other) => other.nid === liquid.nid)) found.push(liquid);
|
||||
const fluid = this.#fluid(x + dx, y + dy, z + dz);
|
||||
if (fluid && !found.some((other) => other.nid === fluid.nid)) found.push(fluid);
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
#liquid(x: number, y: number, z: number): Liquid | undefined {
|
||||
#fluid(x: number, y: number, z: number): Fluid | undefined {
|
||||
if (y < 0 || y >= CHUNK_HEIGHT) return undefined;
|
||||
const value = this.#world.value_at(x, y, z);
|
||||
if (value === undefined) return undefined;
|
||||
const nid = value & ID_MASK;
|
||||
const block = this.#world.block(nid);
|
||||
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 };
|
||||
if (block?.fluid) return { nid, block, level: get_state_value(value, block, "level")! };
|
||||
// a waterlogged block is a source of its fluid that never flows away, like minecraft's
|
||||
const fluid = fluid_block(value, block);
|
||||
return fluid && { nid: this.#world.nid(fluid.id), block: fluid, level: 0 };
|
||||
}
|
||||
|
||||
#set(block: BlockRegistry, x: number, y: number, z: number, level: number) {
|
||||
+22
-22
@@ -15,7 +15,7 @@ import {
|
||||
TICKS_PER_SECOND,
|
||||
} from "$/common/constants.ts";
|
||||
import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
|
||||
import { LiquidFlow } from "./liquids.ts";
|
||||
import { FluidFlow } from "./fluids.ts";
|
||||
import { Container, ItemData, ItemStack } from "$/common/inventory.ts";
|
||||
import {
|
||||
AIR_ID,
|
||||
@@ -38,8 +38,8 @@ import {
|
||||
can_stand_on,
|
||||
chunk_key,
|
||||
display_name,
|
||||
fluid_block,
|
||||
is_waterlogged,
|
||||
liquid_block,
|
||||
placement_target,
|
||||
set_state_value,
|
||||
waterlog_placement,
|
||||
@@ -182,8 +182,8 @@ export class GameServer {
|
||||
recipes: RecipeBook;
|
||||
// monsters spawning on their own near players, see natural_spawner.ts
|
||||
spawner: NaturalSpawner;
|
||||
// water and other liquids flowing, see liquids.ts
|
||||
liquids: LiquidFlow;
|
||||
// water and other fluids flowing, see fluids.ts
|
||||
fluids: FluidFlow;
|
||||
// minecraft's doMobSpawning game rule. tests turn it off so nothing turns up they didn't put there
|
||||
do_mob_spawning = true;
|
||||
// what runs tick(), for /tps. tests call tick() themselves
|
||||
@@ -215,7 +215,7 @@ export class GameServer {
|
||||
);
|
||||
}
|
||||
this.world = new ServerWorld(saved?.seed ?? default_seed, mods.worldgen);
|
||||
this.liquids = new LiquidFlow({
|
||||
this.fluids = new FluidFlow({
|
||||
value_at: (x, y, z) => {
|
||||
if (y < 0 || y >= CHUNK_HEIGHT) return undefined;
|
||||
const loaded = this.world.has_chunk(Math.floor(x / CHUNK_SIZE), Math.floor(z / CHUNK_SIZE));
|
||||
@@ -280,7 +280,7 @@ export class GameServer {
|
||||
|
||||
// players without the chunk get the block with it when they do, like minecraft
|
||||
this.world.on_block_change = (x, y, z, id, state) => {
|
||||
this.liquids.changed(x, y, z, this.#tick);
|
||||
this.fluids.changed(x, y, z, this.#tick);
|
||||
const data = JSON.stringify(
|
||||
(state
|
||||
? { type: "set_block", x, y, z, id, state }
|
||||
@@ -384,7 +384,7 @@ export class GameServer {
|
||||
}
|
||||
|
||||
this.#new_days();
|
||||
this.liquids.tick(this.#tick);
|
||||
this.fluids.tick(this.#tick);
|
||||
this.#tick_vitals();
|
||||
for (const player of this.#players.values()) this.#tick_using(player);
|
||||
this.#tick_entities();
|
||||
@@ -805,9 +805,9 @@ export class GameServer {
|
||||
: drops && info.drop_table
|
||||
? [new ItemStack(info.drop_table)]
|
||||
: [];
|
||||
// a waterlogged block leaves its liquid behind, a source
|
||||
const liquid = is_waterlogged(value, info) ? liquid_block(value, info) : undefined;
|
||||
this.set_block(x, y, z, liquid?.id ?? AIR_ID, player);
|
||||
// a waterlogged block leaves its fluid behind, a source
|
||||
const fluid = is_waterlogged(value, info) ? fluid_block(value, info) : undefined;
|
||||
this.set_block(x, y, z, fluid?.id ?? AIR_ID, player);
|
||||
if (!how.collect) { for (const stack of loot) this.pop_item(x, y, z, stack); }
|
||||
return loot;
|
||||
}
|
||||
@@ -1323,8 +1323,8 @@ export class GameServer {
|
||||
const value = this.world.get_block_value(x, y, z);
|
||||
const held_slot = player.inventory.get_slot(player.selected_slot);
|
||||
const held = EverythingRegistry.get<ItemRegistry>("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;
|
||||
// an item aimed at fluids, like a bucket, uses the fluid in a waterlogged block instead of the block
|
||||
const used = (held?.targets_fluids && fluid_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 };
|
||||
@@ -1364,7 +1364,7 @@ export class GameServer {
|
||||
return;
|
||||
}
|
||||
|
||||
// placing a liquid, like a water bucket, into a block that can hold it fills it, like minecraft's
|
||||
// placing a fluid, 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;
|
||||
@@ -1399,7 +1399,7 @@ export class GameServer {
|
||||
this.#correct(player, tx, ty, tz);
|
||||
return;
|
||||
}
|
||||
// put into a source of the liquid it can hold, it holds it
|
||||
// put into a source of the fluid it can hold, it holds it
|
||||
const placed_value = waterlog_placement(
|
||||
ruled_value,
|
||||
placing,
|
||||
@@ -1420,7 +1420,7 @@ 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,
|
||||
// the held fluid poured into the block at x y z, which can hold it. it's placing the fluid 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);
|
||||
@@ -1984,19 +1984,19 @@ export class GameServer {
|
||||
}
|
||||
|
||||
const vitals = player.vitals;
|
||||
const in_liquid = this.#in_liquid(x, y, z);
|
||||
const in_fluid = this.#in_fluid(x, y, z);
|
||||
const sprinting = horizontal / (ticks * TICK_DELTA) > SPRINT_SPEED;
|
||||
if (in_liquid) {
|
||||
if (in_fluid) {
|
||||
vitals.add_exhaustion(EXHAUSTION.swim_per_block * (horizontal + Math.abs(dy)));
|
||||
player.fall_distance = 0;
|
||||
} else if (sprinting) {
|
||||
vitals.add_exhaustion(EXHAUSTION.sprint_per_block * horizontal);
|
||||
}
|
||||
if (dy > 0 && !in_liquid && this.#on_ground(player.x, player.y, player.z)) {
|
||||
if (dy > 0 && !in_fluid && this.#on_ground(player.x, player.y, player.z)) {
|
||||
vitals.add_exhaustion(sprinting ? EXHAUSTION.sprint_jump : EXHAUSTION.jump);
|
||||
}
|
||||
|
||||
if (dy < 0 && !in_liquid) {
|
||||
if (dy < 0 && !in_fluid) {
|
||||
player.fall_distance -= dy;
|
||||
}
|
||||
if (player.on_ground) {
|
||||
@@ -2024,10 +2024,10 @@ export class GameServer {
|
||||
return !physics.box_is_free(under_feet, 0, 0, 0, (bx, by, bz) => this.world.collision_shape(bx, by, bz));
|
||||
}
|
||||
|
||||
// in liquid, a waterlogged block's too
|
||||
#in_liquid(x: number, y: number, z: number) {
|
||||
// in fluid, a waterlogged block's too
|
||||
#in_fluid(x: number, y: number, z: number) {
|
||||
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)) !==
|
||||
return fluid_block(this.world.get_block_value(bx, by, bz), this.world.get_block_info(bx, by, bz)) !==
|
||||
undefined;
|
||||
}
|
||||
|
||||
|
||||
+18
-18
@@ -3,9 +3,9 @@
|
||||
|
||||
// runs mods' server scripts: builds each one's ServerContext and keeps what they register.
|
||||
// see docs/modding/. parts not built yet throw when used, saying so
|
||||
import { BlockRegistry, EverythingRegistry, ItemRegistry, liquid_height } from "$/common/everything_registry.ts";
|
||||
import { BlockRegistry, EverythingRegistry, fluid_height, ItemRegistry } from "$/common/everything_registry.ts";
|
||||
import { Container as EngineContainer, ItemStack as EngineItemStack } from "$/common/inventory.ts";
|
||||
import { block_states, get_state_value, is_waterlogged, liquid_block, set_state_value } from "$/common/utils.ts";
|
||||
import { block_states, fluid_block, get_state_value, is_waterlogged, set_state_value } from "$/common/utils.ts";
|
||||
import { LootTables } from "./loot.ts";
|
||||
import { collision_shape_at, covers_face, is_solid, raycast_blocks, type Shape } from "$/common/block_shapes.ts";
|
||||
import { AIR, CHUNK_SIZE, faces, ID_MASK } from "$/common/constants.ts";
|
||||
@@ -803,9 +803,9 @@ export class ModRuntime {
|
||||
return entity_ref(entity);
|
||||
},
|
||||
raycast: (from, to, options) => {
|
||||
const liquids = options?.liquids ?? "none";
|
||||
if (!["none", "source_only", "any"].includes(liquids)) {
|
||||
throw new Error(`liquids must be "none", "source_only" or "any", not ${liquids}`);
|
||||
const fluids = options?.fluids ?? "none";
|
||||
if (!["none", "source_only", "any"].includes(fluids)) {
|
||||
throw new Error(`fluids must be "none", "source_only" or "any", not ${fluids}`);
|
||||
}
|
||||
const origin = [from.x, from.y, from.z];
|
||||
const length = Math.hypot(to.x - from.x, to.y - from.y, to.z - from.z);
|
||||
@@ -817,15 +817,15 @@ export class ModRuntime {
|
||||
origin,
|
||||
direction,
|
||||
length,
|
||||
(x, y, z) => raycast_shape(world, x, y, z, liquids),
|
||||
(x, y, z) => raycast_shape(world, x, y, z, fluids),
|
||||
);
|
||||
if (!hit) return undefined;
|
||||
// what was hit in a waterlogged block with liquids on is its liquid, see raycast_shape
|
||||
// what was hit in a waterlogged block with fluids on is its fluid, 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);
|
||||
const fluid = fluids === "none" ? undefined : fluid_block(value, block);
|
||||
return {
|
||||
id: liquid?.id ?? world.get_block_id(hit.x, hit.y, hit.z),
|
||||
id: fluid?.id ?? world.get_block_id(hit.x, hit.y, hit.z),
|
||||
x: hit.x,
|
||||
y: hit.y,
|
||||
z: hit.z,
|
||||
@@ -837,12 +837,12 @@ export class ModRuntime {
|
||||
},
|
||||
};
|
||||
},
|
||||
liquid_flow: (x, y, z) => {
|
||||
const [fx, fy, fz] = physics.liquid_flow(
|
||||
fluid_flow: (x, y, z) => {
|
||||
const [fx, fy, fz] = physics.fluid_flow(
|
||||
Math.floor(x),
|
||||
Math.floor(y),
|
||||
Math.floor(z),
|
||||
(x, y, z) => game().entities.liquid_at(x, y, z),
|
||||
(x, y, z) => game().entities.fluid_at(x, y, z),
|
||||
(x, y, z) => game().world.collision_shape(x, y, z),
|
||||
);
|
||||
// without any -0
|
||||
@@ -1315,27 +1315,27 @@ function container_api(game: GameServer, id: string, container: EngineContainer)
|
||||
return api;
|
||||
}
|
||||
|
||||
// what ServerWorld.raycast hits in a block: what entities bump into, and liquids up to their surface when it's
|
||||
// what ServerWorld.raycast hits in a block: what entities bump into, and fluids up to their surface when it's
|
||||
// looking for them
|
||||
function raycast_shape(
|
||||
world: GameServer["world"],
|
||||
x: number,
|
||||
y: number,
|
||||
z: number,
|
||||
liquids: "none" | "source_only" | "any",
|
||||
fluids: "none" | "source_only" | "any",
|
||||
): Shape | undefined {
|
||||
const value = world.get_block_value(x, y, z);
|
||||
const nid = value & ID_MASK;
|
||||
if (nid === AIR) return undefined;
|
||||
const block = EverythingRegistry.get_by_id<BlockRegistry>("blocks", nid);
|
||||
if (!block) return undefined;
|
||||
if (block.liquid) {
|
||||
if (block.fluid) {
|
||||
const level = get_state_value(value, block, "level") ?? 0;
|
||||
if (liquids === "none" || (liquids === "source_only" && level !== 0)) return undefined;
|
||||
return [[0, 0, 0, 1, liquid_height(level, block.liquid.range), 1]];
|
||||
if (fluids === "none" || (fluids === "source_only" && level !== 0)) return undefined;
|
||||
return [[0, 0, 0, 1, fluid_height(level, block.fluid.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]];
|
||||
if (fluids !== "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;
|
||||
}
|
||||
|
||||
@@ -205,7 +205,7 @@ function has_room(entities: EntityManager, mob: Mob) {
|
||||
for (let x = Math.floor(mob.x - half); x < mob.x + half; x++) {
|
||||
for (let y = Math.floor(mob.y); y < mob.y + mob.height; y++) {
|
||||
for (let z = Math.floor(mob.z - half); z < mob.z + half; z++) {
|
||||
if (entities.has_collision(x, y, z) || entities.is_liquid(x, y, z)) return false;
|
||||
if (entities.has_collision(x, y, z) || entities.is_fluid(x, y, z)) return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,8 +147,8 @@ export class ServerPlayer extends LivingEntity {
|
||||
|
||||
// their client moves them, this only burns, falls out of the world and counts effects and hurt time down. while
|
||||
// knocked back it moves them like a mob that isn't walking anywhere, landing hands them back to their client
|
||||
// its client moves it, liquids included, see LocalPlayer
|
||||
override get pushed_by_liquid() {
|
||||
// its client moves it, fluids included, see LocalPlayer
|
||||
override get pushed_by_fluid() {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// liquids flowing on the server, like minecraft's water: out from a source, down past edges, and draining away
|
||||
// fluids flowing on the server, like minecraft's water: out from a source, down past edges, and draining away
|
||||
import { assert, assertEquals } from "@std/assert";
|
||||
import { type BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
|
||||
import { get_state_value } from "$/common/utils.ts";
|
||||
import { LIQUID_DEFAULTS } from "$/common/everything_registry.ts";
|
||||
import { LiquidFlow } from "$/server/game/liquids.ts";
|
||||
import { FLUID_DEFAULTS } from "$/common/everything_registry.ts";
|
||||
import { FluidFlow } from "$/server/game/fluids.ts";
|
||||
import { ItemStack } from "$/common/inventory.ts";
|
||||
import { test_game } from "./helpers.ts";
|
||||
|
||||
@@ -50,7 +50,7 @@ Deno.test("water flows out from a source one block a step, lower each block, up
|
||||
// it spreads in a diamond, by how far it walked
|
||||
assertEquals(level(game, 3, Y, 3), 6);
|
||||
// and doesn't flow on its own after that
|
||||
assertEquals(game.liquids.pending, 0);
|
||||
assertEquals(game.fluids.pending, 0);
|
||||
});
|
||||
|
||||
// lava's level there, undefined for no lava
|
||||
@@ -112,7 +112,7 @@ Deno.test("taking the source away drains what flowed from it", async () => {
|
||||
for (let x = -8; x <= 8; x++) {
|
||||
for (let z = -8; z <= 8; z++) assertEquals(level(game, x, Y, z), undefined, `at ${x}, ${z}`);
|
||||
}
|
||||
assertEquals(game.liquids.pending, 0);
|
||||
assertEquals(game.fluids.pending, 0);
|
||||
});
|
||||
|
||||
Deno.test("two sources beside a spot on solid ground make it a source, like infinite water", async () => {
|
||||
@@ -158,12 +158,12 @@ Deno.test("water stops at chunks that aren't loaded instead of making them", ()
|
||||
id: "a:water",
|
||||
textures: "a:water",
|
||||
has_collision: false,
|
||||
liquid: LIQUID_DEFAULTS,
|
||||
fluid: FLUID_DEFAULTS,
|
||||
} as BlockRegistry;
|
||||
water.states = [{ name: "level", bits: 3, default: 0 }];
|
||||
const blocks = new Map<string, number>([["0,1,0", 1]]);
|
||||
const asked: number[] = [];
|
||||
const flow = new LiquidFlow({
|
||||
const flow = new FluidFlow({
|
||||
value_at(x, y, z) {
|
||||
asked.push(x);
|
||||
if (x > 0) return undefined;
|
||||
@@ -1,48 +1,48 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// which way liquid flows and how it pushes what's in it, like minecraft's FlowingFluid.getFlow
|
||||
// which way fluid flows and how it pushes what's in it, like minecraft's FlowingFluid.getFlow
|
||||
import { assert, assertAlmostEquals, assertEquals } from "@std/assert";
|
||||
import { type Body, liquid_flow, type LiquidInCell, push_by_liquid } from "$/common/physics.ts";
|
||||
import { type Body, fluid_flow, type FluidInCell, push_by_fluid } from "$/common/physics.ts";
|
||||
|
||||
const WATER = "a:water";
|
||||
|
||||
// cells by "x,y,z", and solid blocks
|
||||
function world(liquids: Record<string, number>, solid: string[] = []) {
|
||||
const liquid_at = (x: number, y: number, z: number): LiquidInCell | undefined => {
|
||||
const height = liquids[`${x},${y},${z}`];
|
||||
function world(fluids: Record<string, number>, solid: string[] = []) {
|
||||
const fluid_at = (x: number, y: number, z: number): FluidInCell | undefined => {
|
||||
const height = fluids[`${x},${y},${z}`];
|
||||
return height === undefined ? undefined : { id: WATER, height };
|
||||
};
|
||||
const is_solid = (x: number, y: number, z: number) => solid.includes(`${x},${y},${z}`);
|
||||
return { liquid_at, is_solid };
|
||||
return { fluid_at, is_solid };
|
||||
}
|
||||
|
||||
const rounded = (v: number[]) => v.map((n) => Math.round(n * 1000) / 1000 + 0);
|
||||
|
||||
Deno.test("liquid flows towards lower cells of it and over edges, not into walls or air beside it", () => {
|
||||
Deno.test("fluid flows towards lower cells of it and over edges, not into walls or air beside it", () => {
|
||||
// a source with lower water to +x
|
||||
const stream = world({ "0,0,0": 1, "1,0,0": 0.8 });
|
||||
assertEquals(rounded(liquid_flow(0, 0, 0, stream.liquid_at, stream.is_solid)), [1, 0, 0]);
|
||||
assertEquals(rounded(fluid_flow(0, 0, 0, stream.fluid_at, stream.is_solid)), [1, 0, 0]);
|
||||
// level, nothing
|
||||
const pool = world({ "0,0,0": 1, "1,0,0": 1, "-1,0,0": 1 });
|
||||
assertEquals(rounded(liquid_flow(0, 0, 0, pool.liquid_at, pool.is_solid)), [0, 0, 0]);
|
||||
assertEquals(rounded(fluid_flow(0, 0, 0, pool.fluid_at, pool.is_solid)), [0, 0, 0]);
|
||||
// air beside it with nothing below doesn't pull, air with more of it below does
|
||||
assertEquals(rounded(liquid_flow(0, 0, 0, world({ "0,0,0": 1 }).liquid_at, () => false)), [0, 0, 0]);
|
||||
assertEquals(rounded(fluid_flow(0, 0, 0, world({ "0,0,0": 1 }).fluid_at, () => false)), [0, 0, 0]);
|
||||
const edge = world({ "0,0,0": 0.5, "0,-1,1": 1 });
|
||||
assertEquals(rounded(liquid_flow(0, 0, 0, edge.liquid_at, edge.is_solid)), [0, 0, 1]);
|
||||
assertEquals(rounded(fluid_flow(0, 0, 0, edge.fluid_at, edge.is_solid)), [0, 0, 1]);
|
||||
// falling down beside a wall, down
|
||||
const fall = world({ "0,0,0": 0.9, "0,1,0": 1 }, ["1,0,0"]);
|
||||
assertEquals(rounded(liquid_flow(0, 0, 0, fall.liquid_at, fall.is_solid)), [0, -1, 0]);
|
||||
assertEquals(rounded(fluid_flow(0, 0, 0, fall.fluid_at, fall.is_solid)), [0, -1, 0]);
|
||||
// a source under more water beside a wall is still, like the bottom of a pool
|
||||
const deep = world({ "0,0,0": 1, "0,1,0": 1 }, ["1,0,0"]);
|
||||
assertEquals(rounded(liquid_flow(0, 0, 0, deep.liquid_at, deep.is_solid)), [0, 0, 0]);
|
||||
assertEquals(rounded(fluid_flow(0, 0, 0, deep.fluid_at, deep.is_solid)), [0, 0, 0]);
|
||||
});
|
||||
|
||||
Deno.test("a body in flowing liquid is pushed along it, a player only by the average", () => {
|
||||
Deno.test("a body in flowing fluid is pushed along it, a player only by the average", () => {
|
||||
const stream = world({ "0,0,0": 1, "1,0,0": 0.8 });
|
||||
const body = (): Body => ({ x: 0.5, y: 0, z: 0.5, vx: 0, vy: 0, vz: 0, width: 0.25, height: 0.25 });
|
||||
const item = body();
|
||||
push_by_liquid(item, stream.liquid_at, stream.is_solid, false);
|
||||
push_by_fluid(item, stream.fluid_at, stream.is_solid, false);
|
||||
assertAlmostEquals(item.vx, 0.28);
|
||||
assertEquals([item.vy, item.vz], [0, 0]);
|
||||
|
||||
@@ -50,13 +50,13 @@ Deno.test("a body in flowing liquid is pushed along it, a player only by the ave
|
||||
const wide: Body = { ...body(), x: 0, width: 0.5 };
|
||||
const half = world({ "-1,0,0": 1, "0,0,0": 1, "1,0,0": 0.8 });
|
||||
const player = { ...wide };
|
||||
push_by_liquid(player, half.liquid_at, half.is_solid, true);
|
||||
push_by_fluid(player, half.fluid_at, half.is_solid, true);
|
||||
const other = { ...wide };
|
||||
push_by_liquid(other, half.liquid_at, half.is_solid, false);
|
||||
push_by_fluid(other, half.fluid_at, half.is_solid, false);
|
||||
assert(player.vx > 0 && player.vx < other.vx, `${player.vx} < ${other.vx}`);
|
||||
|
||||
// out of the liquid, nothing
|
||||
// out of the fluid, nothing
|
||||
const dry = { ...body(), y: 3 };
|
||||
push_by_liquid(dry, stream.liquid_at, stream.is_solid, false);
|
||||
push_by_fluid(dry, stream.fluid_at, stream.is_solid, false);
|
||||
assertEquals([dry.vx, dry.vy, dry.vz], [0, 0, 0]);
|
||||
});
|
||||
@@ -1,20 +1,20 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// liquids: blocks with "liquid", drawn like luanti's with surfaces that slope down to where they flow
|
||||
// fluids: blocks with "fluid", drawn like luanti's with surfaces that slope down to where they flow
|
||||
import { assert, assertAlmostEquals, assertEquals } from "@std/assert";
|
||||
import {
|
||||
type BlockRegistry,
|
||||
EverythingRegistry,
|
||||
is_liquid_block,
|
||||
liquid_height,
|
||||
liquid_textures,
|
||||
fluid_height,
|
||||
fluid_textures,
|
||||
is_fluid_block,
|
||||
} from "$/common/everything_registry.ts";
|
||||
import { block_from_json, block_to_json, type BlockJson, validate_block } from "$/common/mod_data.ts";
|
||||
import { cube_face_texture } from "$/common/block_models.ts";
|
||||
import { CHUNK_AREA, CHUNK_HEIGHT, CHUNK_SIZE } from "$/common/constants.ts";
|
||||
import { get_state_value, set_state_value } from "$/common/utils.ts";
|
||||
import { cell_index, corner_height, liquid_quads, type LiquidCell } from "$/client/workers/liquid_mesh.ts";
|
||||
import { cell_index, corner_height, fluid_quads, type FluidCell } from "$/client/workers/fluid_mesh.ts";
|
||||
import { TERRAIN_VERTEX_BYTES } from "$/client/workers/chunk_messages.ts";
|
||||
import { test_game } from "./helpers.ts";
|
||||
|
||||
@@ -22,13 +22,13 @@ const OIL: BlockJson = {
|
||||
id: "oily:oil",
|
||||
textures: { still: "oily:oil_still", flowing: "oily:oil_flow" },
|
||||
render_layer: "translucent",
|
||||
liquid: { range: 4 },
|
||||
fluid: { range: 4 },
|
||||
};
|
||||
|
||||
Deno.test("a liquid gets a level state, no collision and can be placed into", () => {
|
||||
Deno.test("a fluid gets a level state, no collision and can be placed into", () => {
|
||||
const { block } = block_from_json(OIL);
|
||||
assert(is_liquid_block(block));
|
||||
assertEquals(block.liquid, { range: 4, ticks: 5, renewable: true });
|
||||
assert(is_fluid_block(block));
|
||||
assertEquals(block.fluid, { range: 4, ticks: 5, renewable: true });
|
||||
assertEquals(block.states, [{ name: "level", bits: 3, default: 0 }]);
|
||||
assertEquals(block.has_collision, false);
|
||||
assertEquals(block.replaceable, true);
|
||||
@@ -43,48 +43,48 @@ Deno.test("a liquid gets a level state, no collision and can be placed into", ()
|
||||
assertEquals(block_to_json(hot, true).states, [{ name: "hot", bits: 1, default: 0 }]);
|
||||
|
||||
// a range of 7 is the default
|
||||
const water = block_from_json({ id: "a:water", textures: "a:water", liquid: {} }).block;
|
||||
assertEquals(water.liquid, { range: 7, ticks: 5, renewable: true });
|
||||
assertEquals(block_to_json(water, true).liquid, {});
|
||||
const water = block_from_json({ id: "a:water", textures: "a:water", fluid: {} }).block;
|
||||
assertEquals(water.fluid, { range: 7, ticks: 5, renewable: true });
|
||||
assertEquals(block_to_json(water, true).fluid, {});
|
||||
});
|
||||
|
||||
Deno.test("a liquid's still texture is its top and bottom, the flowing one its sides", () => {
|
||||
Deno.test("a fluid's still texture is its top and bottom, the flowing one its sides", () => {
|
||||
const { block } = block_from_json(OIL);
|
||||
assertEquals(liquid_textures(block.textures), { still: "oily:oil_still", flowing: "oily:oil_flow" });
|
||||
assertEquals(fluid_textures(block.textures), { still: "oily:oil_still", flowing: "oily:oil_flow" });
|
||||
assertEquals(cube_face_texture(block, "top"), "oily:oil_still");
|
||||
assertEquals(cube_face_texture(block, "side"), "oily:oil_flow");
|
||||
// one texture is both
|
||||
assertEquals(liquid_textures("a:water"), { still: "a:water", flowing: "a:water" });
|
||||
assertEquals(liquid_textures({ still: "a:water" }), { still: "a:water", flowing: "a:water" });
|
||||
assertEquals(fluid_textures("a:water"), { still: "a:water", flowing: "a:water" });
|
||||
assertEquals(fluid_textures({ still: "a:water" }), { still: "a:water", flowing: "a:water" });
|
||||
});
|
||||
|
||||
Deno.test("check-mods catches liquids that can't work", () => {
|
||||
Deno.test("check-mods catches fluids that can't work", () => {
|
||||
assertEquals(validate_block(OIL), []);
|
||||
const problems = (json: Record<string, unknown>) => validate_block({ ...OIL, ...json });
|
||||
assertEquals(problems({ liquid: { range: 8 } }), ["liquid.range must be at most 7"]);
|
||||
assertEquals(problems({ liquid: true }), ["liquid must be { range, ticks, renewable }, or {}"]);
|
||||
assertEquals(problems({ liquid: { ticks: 0 } }), ["liquid.ticks must be 1 or more"]);
|
||||
assertEquals(problems({ liquid: { speed: 2 } }), ['liquid can\'t have "speed"']);
|
||||
assertEquals(problems({ fluid: { range: 8 } }), ["fluid.range must be at most 7"]);
|
||||
assertEquals(problems({ fluid: true }), ["fluid must be { range, ticks, renewable }, or {}"]);
|
||||
assertEquals(problems({ fluid: { ticks: 0 } }), ["fluid.ticks must be 1 or more"]);
|
||||
assertEquals(problems({ fluid: { speed: 2 } }), ['fluid can\'t have "speed"']);
|
||||
assertEquals(problems({ textures: { top: "a:b" } }), [
|
||||
"textures must be a texture id or { still, flowing } for a liquid",
|
||||
"textures must be a texture id or { still, flowing } for a fluid",
|
||||
]);
|
||||
assertEquals(problems({ model: "engine:cross" }), ["model isn't for liquids, the engine draws them"]);
|
||||
assertEquals(problems({ model: "engine:cross" }), ["model isn't for fluids, the engine draws them"]);
|
||||
assertEquals(problems({ states: [{ name: "level", bits: 2, default: 0 }] }), [
|
||||
`states can't have "level", a liquid gets that state of its own`,
|
||||
`states can't have "level", a fluid gets that state of its own`,
|
||||
]);
|
||||
});
|
||||
|
||||
Deno.test("a liquid's surface is full at a source and lower each block it flows", () => {
|
||||
assertEquals(liquid_height(0, 7), 1);
|
||||
assertAlmostEquals(liquid_height(1, 7), 6.5 / 7);
|
||||
assertAlmostEquals(liquid_height(7, 7), 0.5 / 7);
|
||||
Deno.test("a fluid's surface is full at a source and lower each block it flows", () => {
|
||||
assertEquals(fluid_height(0, 7), 1);
|
||||
assertAlmostEquals(fluid_height(1, 7), 6.5 / 7);
|
||||
assertAlmostEquals(fluid_height(7, 7), 0.5 / 7);
|
||||
// past its range it's as low as it goes
|
||||
assertEquals(liquid_height(5, 4), liquid_height(4, 4));
|
||||
assertEquals(fluid_height(5, 4), fluid_height(4, 4));
|
||||
});
|
||||
|
||||
// the 3x3 cells around a liquid, all air unless set
|
||||
function cells(set: Record<string, Partial<LiquidCell>> = {}) {
|
||||
const list: LiquidCell[] = Array.from({ length: 9 }, () => ({
|
||||
// the 3x3 cells around a fluid, all air unless set
|
||||
function cells(set: Record<string, Partial<FluidCell>> = {}) {
|
||||
const list: FluidCell[] = Array.from({ length: 9 }, () => ({
|
||||
same: false,
|
||||
height: 0,
|
||||
air: true,
|
||||
@@ -97,12 +97,12 @@ function cells(set: Record<string, Partial<LiquidCell>> = {}) {
|
||||
}
|
||||
return list;
|
||||
}
|
||||
const liquid = (height: number): Partial<LiquidCell> => ({ same: true, height });
|
||||
const fluid = (height: number): Partial<FluidCell> => ({ same: true, height });
|
||||
const OPEN = { same: false, solid: false };
|
||||
const SOLID = { same: false, solid: true };
|
||||
|
||||
// the highest and lowest y of each quad, by face
|
||||
function extents(quads: ReturnType<typeof liquid_quads>) {
|
||||
function extents(quads: ReturnType<typeof fluid_quads>) {
|
||||
return quads.map((q) => {
|
||||
const ys = [q.positions[1], q.positions[4], q.positions[7], q.positions[10]];
|
||||
return { face: q.face, min: Math.min(...ys), max: Math.max(...ys) };
|
||||
@@ -110,7 +110,7 @@ function extents(quads: ReturnType<typeof liquid_quads>) {
|
||||
}
|
||||
|
||||
Deno.test("a lone source is a full block, on the ground it has no bottom", () => {
|
||||
const quads = liquid_quads(cells({ "0,0": liquid(1) }), OPEN, SOLID);
|
||||
const quads = fluid_quads(cells({ "0,0": fluid(1) }), OPEN, SOLID);
|
||||
assertEquals(quads.map((q) => q.face).sort(), [0, 2, 3, 4, 5]);
|
||||
for (const { min, max, face } of extents(quads)) {
|
||||
if (face === 0) assertEquals([min, max], [1, 1]);
|
||||
@@ -119,25 +119,25 @@ Deno.test("a lone source is a full block, on the ground it has no bottom", () =>
|
||||
});
|
||||
|
||||
Deno.test("the middle of a lake is only its surface", () => {
|
||||
const lake: Record<string, Partial<LiquidCell>> = {};
|
||||
for (let dx = -1; dx <= 1; dx++) for (let dz = -1; dz <= 1; dz++) lake[`${dx},${dz}`] = liquid(1);
|
||||
assertEquals(liquid_quads(cells(lake), OPEN, SOLID).map((q) => q.face), [0]);
|
||||
const lake: Record<string, Partial<FluidCell>> = {};
|
||||
for (let dx = -1; dx <= 1; dx++) for (let dz = -1; dz <= 1; dz++) lake[`${dx},${dz}`] = fluid(1);
|
||||
assertEquals(fluid_quads(cells(lake), OPEN, SOLID).map((q) => q.face), [0]);
|
||||
// deeper down, nothing at all
|
||||
assertEquals(liquid_quads(cells(lake), { same: true, solid: false }, { same: true, solid: false }), []);
|
||||
assertEquals(fluid_quads(cells(lake), { same: true, solid: false }, { same: true, solid: false }), []);
|
||||
// a full surface right under a solid block can't be seen
|
||||
assertEquals(liquid_quads(cells(lake), SOLID, SOLID), []);
|
||||
assertEquals(fluid_quads(cells(lake), SOLID, SOLID), []);
|
||||
});
|
||||
|
||||
Deno.test("flowing liquid slopes down from the source beside it, and thins out to a film at the shore", () => {
|
||||
const flowing = liquid_height(1, 7);
|
||||
Deno.test("flowing fluid slopes down from the source beside it, and thins out to a film at the shore", () => {
|
||||
const flowing = fluid_height(1, 7);
|
||||
// a source to the -x side
|
||||
const set = cells({ "0,0": liquid(flowing), "-1,0": liquid(1) });
|
||||
const set = cells({ "0,0": fluid(flowing), "-1,0": fluid(1) });
|
||||
assertEquals(corner_height(set, 0, 0), 1);
|
||||
assertEquals(corner_height(set, 0, 1), 1);
|
||||
// the +x corners have it and air around them
|
||||
assertAlmostEquals(corner_height(set, 1, 0), 0.02);
|
||||
|
||||
const quads = liquid_quads(set, OPEN, SOLID);
|
||||
const quads = fluid_quads(set, OPEN, SOLID);
|
||||
// no side facing the source
|
||||
assert(!quads.some((q) => q.face === 4));
|
||||
const top = extents(quads).find((q) => q.face === 0)!;
|
||||
@@ -145,18 +145,18 @@ Deno.test("flowing liquid slopes down from the source beside it, and thins out t
|
||||
assertAlmostEquals(top.min, 0.02);
|
||||
|
||||
// between two flowing cells, the average
|
||||
const two = cells({ "0,0": liquid(0.5), "1,0": liquid(0.3), "1,1": liquid(0.1), "0,1": { solid: true } });
|
||||
const two = cells({ "0,0": fluid(0.5), "1,0": fluid(0.3), "1,1": fluid(0.1), "0,1": { solid: true } });
|
||||
assertAlmostEquals(corner_height(two, 1, 1), 0.3);
|
||||
});
|
||||
|
||||
Deno.test("a lower neighbor's surface rises to meet a column of liquid, so there's no gap", () => {
|
||||
// liquid with more of itself above: every corner is full, so the side toward the lower one has no height
|
||||
const column = cells({ "0,0": { ...liquid(1), top_same: true }, "0,1": liquid(0.3) });
|
||||
const quads = liquid_quads(column, { same: true, solid: false }, SOLID);
|
||||
Deno.test("a lower neighbor's surface rises to meet a column of fluid, so there's no gap", () => {
|
||||
// fluid with more of itself above: every corner is full, so the side toward the lower one has no height
|
||||
const column = cells({ "0,0": { ...fluid(1), top_same: true }, "0,1": fluid(0.3) });
|
||||
const quads = fluid_quads(column, { same: true, solid: false }, SOLID);
|
||||
assert(!quads.some((q) => q.face === 2 || q.face === 0));
|
||||
// the lower one, beside the column at -z: its top goes up to the top of the cell there
|
||||
const low = cells({ "0,0": liquid(0.3), "0,-1": { ...liquid(1), top_same: true } });
|
||||
const top = liquid_quads(low, OPEN, SOLID).find((q) => q.face === 0)!;
|
||||
const low = cells({ "0,0": fluid(0.3), "0,-1": { ...fluid(1), top_same: true } });
|
||||
const top = fluid_quads(low, OPEN, SOLID).find((q) => q.face === 0)!;
|
||||
const ys_at_z = (z: number) =>
|
||||
[0, 1, 2, 3].filter((k) => top.positions[k * 3 + 2] === z).map((k) => top.positions[k * 3 + 1]);
|
||||
assertEquals(ys_at_z(0), [1, 1]);
|
||||
@@ -232,7 +232,7 @@ Deno.test("the chunk worker draws water sloping down from a source, across a chu
|
||||
// full where it meets the source, then down
|
||||
assertEquals(height_at(0), 1);
|
||||
// the average of the levels around it, between the rows
|
||||
assertAlmostEquals(height_at(1), (liquid_height(1, 7) + liquid_height(2, 7)) / 2, 1e-6);
|
||||
assertAlmostEquals(height_at(1), (fluid_height(1, 7) + fluid_height(2, 7)) / 2, 1e-6);
|
||||
assertAlmostEquals(height_at(2), 0.02, 1e-6);
|
||||
// no sides between cells of it, only on the outside
|
||||
assert(!quads.some((corners) => corners.every(([x]) => x === 1)));
|
||||
@@ -241,13 +241,13 @@ Deno.test("the chunk worker draws water sloping down from a source, across a chu
|
||||
|
||||
Deno.test("with a flowing texture of its own, a sloping top flows downhill and the sides show part of it", () => {
|
||||
// level water stays still, whatever the flowing texture
|
||||
const lake: Record<string, Partial<LiquidCell>> = {};
|
||||
for (let dx = -1; dx <= 1; dx++) for (let dz = -1; dz <= 1; dz++) lake[`${dx},${dz}`] = liquid(1);
|
||||
assert(liquid_quads(cells(lake), OPEN, SOLID, 0.5).every((q) => q.still));
|
||||
const lake: Record<string, Partial<FluidCell>> = {};
|
||||
for (let dx = -1; dx <= 1; dx++) for (let dz = -1; dz <= 1; dz++) lake[`${dx},${dz}`] = fluid(1);
|
||||
assert(fluid_quads(cells(lake), OPEN, SOLID, 0.5).every((q) => q.still));
|
||||
|
||||
// a source to the -x side, so it flows towards +x
|
||||
const set = cells({ "0,0": liquid(liquid_height(1, 7)), "-1,0": liquid(1) });
|
||||
const quads = liquid_quads(set, OPEN, SOLID, 0.5);
|
||||
const set = cells({ "0,0": fluid(fluid_height(1, 7)), "-1,0": fluid(1) });
|
||||
const quads = fluid_quads(set, OPEN, SOLID, 0.5);
|
||||
const top = quads.find((q) => q.face === 0)!;
|
||||
assertEquals(top.still, false);
|
||||
// the middle half of the texture, turned so v (down the texture) runs with x, downstream
|
||||
@@ -267,12 +267,12 @@ Deno.test("with a flowing texture of its own, a sloping top flows downhill and t
|
||||
}
|
||||
|
||||
// with only one texture, the top stays still and the sides show all of it
|
||||
const plain = liquid_quads(set, OPEN, SOLID);
|
||||
const plain = fluid_quads(set, OPEN, SOLID);
|
||||
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", () => {
|
||||
Deno.test("a waterloggable block gets a waterlogged state after its own, and can't be a fluid", () => {
|
||||
const json: BlockJson = {
|
||||
id: "a:post",
|
||||
textures: "a:post",
|
||||
@@ -285,7 +285,7 @@ Deno.test("a waterloggable block gets a waterlogged state after its own, and can
|
||||
|
||||
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({ id: "a:w", textures: "a:w", fluid: {}, waterloggable: "a:water" }).length > 0);
|
||||
assert(
|
||||
validate_block({ ...json, states: [{ name: "waterlogged", bits: 1, default: 0 }] })
|
||||
.some((p) => p.includes("of its own")),
|
||||
@@ -16,9 +16,9 @@ function setup(ceiling = Infinity, world?: { solid: Blocks; water: Blocks }) {
|
||||
const client = {
|
||||
level: {
|
||||
collision_shape: world?.solid ?? ((_x: number, y: number) => y < 0 || y >= ceiling),
|
||||
is_liquid: world?.water ?? (() => false),
|
||||
is_fluid: world?.water ?? (() => false),
|
||||
// its water is all still sources
|
||||
liquid_at: (x: number, y: number, z: number) =>
|
||||
fluid_at: (x: number, y: number, z: number) =>
|
||||
world?.water(x, y, z) ? { id: "a:water", height: 1 } : undefined,
|
||||
},
|
||||
screen: undefined,
|
||||
|
||||
@@ -74,11 +74,11 @@ Deno.test("which way water flows, from where it's higher to where it's lower", a
|
||||
const { game, ctx } = await setup();
|
||||
game.set_block(0, 100, 0, "bworld:water");
|
||||
for (let i = 0; i < 40; i++) game.tick();
|
||||
assertEquals(ctx.world.liquid_flow(2, 100, 0), { x: 1, y: 0, z: 0 });
|
||||
assertEquals(ctx.world.liquid_flow(0, 100, -2), { x: 0, y: 0, z: -1 });
|
||||
assertEquals(ctx.world.fluid_flow(2, 100, 0), { x: 1, y: 0, z: 0 });
|
||||
assertEquals(ctx.world.fluid_flow(0, 100, -2), { x: 0, y: 0, z: -1 });
|
||||
// a source with water all around is still, and so is air
|
||||
assertEquals(ctx.world.liquid_flow(0, 100, 0), { x: 0, y: 0, z: 0 });
|
||||
assertEquals(ctx.world.liquid_flow(0, 102, 0), { x: 0, y: 0, z: 0 });
|
||||
assertEquals(ctx.world.fluid_flow(0, 100, 0), { x: 0, y: 0, z: 0 });
|
||||
assertEquals(ctx.world.fluid_flow(0, 102, 0), { x: 0, y: 0, z: 0 });
|
||||
});
|
||||
|
||||
Deno.test("items lying in a box are found, changed and taken away", async () => {
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
import { assert, assertAlmostEquals, assertEquals } from "@std/assert";
|
||||
import { eyes_in_water, in_water, move_body } from "$/common/physics.ts";
|
||||
import { type BlockRegistry, is_liquid_block, LIQUID_DEFAULTS } from "$/common/everything_registry.ts";
|
||||
import { type BlockRegistry, FLUID_DEFAULTS, is_fluid_block } from "$/common/everything_registry.ts";
|
||||
|
||||
const body = (x: number, z: number, vx: number, vz: number) => ({ x, y: 0, z, vx, vy: 0, vz, width: 0.6, height: 1.8 });
|
||||
// a block at x 0 to 1, z 1 to 2, on a floor
|
||||
@@ -46,12 +46,12 @@ Deno.test("a body on the ground steps up onto something low, not onto a full blo
|
||||
assertAlmostEquals(wall.y, 0);
|
||||
});
|
||||
|
||||
Deno.test("liquids are the blocks that say they are one", () => {
|
||||
Deno.test("fluids are the blocks that say they are one", () => {
|
||||
const block = (fields: Partial<BlockRegistry>) => ({ has_collision: false, ...fields }) as BlockRegistry;
|
||||
assertEquals(is_liquid_block(block({ liquid: LIQUID_DEFAULTS })), true);
|
||||
assertEquals(is_fluid_block(block({ fluid: FLUID_DEFAULTS })), true);
|
||||
// no collision and replaceable isn't enough any more, like a plant you can place into
|
||||
assertEquals(is_liquid_block(block({ replaceable: true })), false);
|
||||
assertEquals(is_liquid_block(undefined), false);
|
||||
assertEquals(is_fluid_block(block({ replaceable: true })), false);
|
||||
assertEquals(is_fluid_block(undefined), false);
|
||||
});
|
||||
|
||||
Deno.test("a body is in water by its feet or the middle of its box, and its eyes by its eye height", () => {
|
||||
|
||||
@@ -68,7 +68,7 @@ Deno.test("water flows out of a waterlogged fence, which never drains, and break
|
||||
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");
|
||||
assert(t.game.entities.is_fluid(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");
|
||||
@@ -94,7 +94,7 @@ Deno.test("rice is only planted in a water source, holds it, and leaves it behin
|
||||
assertEquals(t.game.world.get_block_id(2, 100, 0), "farmersdelight:rice");
|
||||
const rice = EverythingRegistry.get<BlockRegistry>("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));
|
||||
assert(t.game.entities.is_fluid(2, 100, 0));
|
||||
|
||||
t.game.destroy_block(2, 100, 0);
|
||||
ticks(t.game, 1);
|
||||
|
||||
Reference in New Issue
Block a user