Waterlogging
This commit is contained in:
@@ -3,8 +3,7 @@
|
||||
// when those move or change, and forgets them when they leave it
|
||||
import { CHUNK_SIZE } from "$/common/constants.ts";
|
||||
import { is_within_view } from "$/common/chunk_streaming.ts";
|
||||
import { is_liquid_block } from "$/common/everything_registry.ts";
|
||||
import { liquid_in_cell } from "$/common/utils.ts";
|
||||
import { liquid_block, liquid_in_cell } from "$/common/utils.ts";
|
||||
import type { ItemStack } from "$/common/inventory.ts";
|
||||
import type { EntityEvent, EntityInfo, ServerMessage } from "$/common/protocol.ts";
|
||||
import type { ServerPlayer } from "../player.ts";
|
||||
@@ -95,7 +94,8 @@ export class EntityManager implements EntityLevel {
|
||||
}
|
||||
|
||||
is_liquid(x: number, y: number, z: number) {
|
||||
return is_liquid_block(this.#host.world.get_block_info(x, y, z));
|
||||
const world = this.#host.world;
|
||||
return liquid_block(world.get_block_value(x, y, z), world.get_block_info(x, y, z)) !== undefined;
|
||||
}
|
||||
|
||||
get_block_info(x: number, y: number, z: number) {
|
||||
|
||||
+78
-18
@@ -8,7 +8,7 @@ import {
|
||||
TICK_DELTA,
|
||||
TICKS_PER_SECOND,
|
||||
} from "$/common/constants.ts";
|
||||
import { BlockRegistry, EverythingRegistry, is_liquid_block, ItemRegistry } from "$/common/everything_registry.ts";
|
||||
import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
|
||||
import { LiquidFlow } from "./liquids.ts";
|
||||
import { Container, ItemData, ItemStack } from "$/common/inventory.ts";
|
||||
import {
|
||||
@@ -23,7 +23,17 @@ import type { ModListing, RecipeBook } from "$/common/mod_loader.ts";
|
||||
import type { WorldgenSetup } from "$/common/generation.ts";
|
||||
import { MAX_VIEW_DISTANCE, MIN_VIEW_DISTANCE } from "$/common/chunk_streaming.ts";
|
||||
import { CYCLE_TICKS, format_clock, time_of_day, TIMES_OF_DAY } from "$/common/time.ts";
|
||||
import { box_overlaps_block, can_place, can_stand_on, display_name, placement_target } from "$/common/utils.ts";
|
||||
import {
|
||||
box_overlaps_block,
|
||||
can_place,
|
||||
can_stand_on,
|
||||
display_name,
|
||||
is_waterlogged,
|
||||
liquid_block,
|
||||
placement_target,
|
||||
set_state_value,
|
||||
waterlog_placement,
|
||||
} from "$/common/utils.ts";
|
||||
import { collision_shape_at, type Shape } from "$/common/block_shapes.ts";
|
||||
import { EXHAUSTION } from "./vitals.ts";
|
||||
import { ModRuntime, run_guarded, type ServerCommandSource } from "./mod_runtime.ts";
|
||||
@@ -611,7 +621,10 @@ export class GameServer {
|
||||
const tool = held?.tool;
|
||||
const right_tool = tool !== undefined && tool.type === info.tool_to_break && tool.tier >= (info.tool_tier ?? 0);
|
||||
const drops = info.drop_table && (!info.requires_tool || right_tool);
|
||||
this.set_block(x, y, z, AIR_ID, player);
|
||||
// a waterlogged block leaves its liquid behind, a source
|
||||
const value = this.world.get_block_value(x, y, z);
|
||||
const liquid = is_waterlogged(value, info) ? liquid_block(value, info) : undefined;
|
||||
this.set_block(x, y, z, liquid?.id ?? AIR_ID, player);
|
||||
if (drops) {
|
||||
this.pop_item(x, y, z, new ItemStack(info.drop_table!));
|
||||
}
|
||||
@@ -985,7 +998,12 @@ export class GameServer {
|
||||
}
|
||||
|
||||
const player_api = this.mods.player(player);
|
||||
const block = this.mods.block_ref(x, y, z, info.id);
|
||||
const value = this.world.get_block_value(x, y, z);
|
||||
const held_slot = player.inventory.get_slot(player.selected_slot);
|
||||
const held = EverythingRegistry.get<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;
|
||||
const block = this.mods.block_ref(x, y, z, used.id);
|
||||
const interact = { player: player_api, block, item: player_api.held_item };
|
||||
const before_interact = { ...interact, cancel: false };
|
||||
this.mods.before.block_interact.emit(before_interact);
|
||||
@@ -995,7 +1013,7 @@ export class GameServer {
|
||||
}
|
||||
|
||||
let handled = false;
|
||||
for (const { mod, id: component_id, component, params } of this.mods.components_of(info.id)) {
|
||||
for (const { mod, id: component_id, component, params } of this.mods.components_of(used.id)) {
|
||||
if (component.on_interact) {
|
||||
handled = run_guarded(mod, `${component_id} on_interact`, () =>
|
||||
component.on_interact!(block, params, player_api)) ===
|
||||
@@ -1015,14 +1033,19 @@ export class GameServer {
|
||||
return;
|
||||
}
|
||||
|
||||
// placing a liquid, like a water bucket, into a block that can hold it fills it, like minecraft's
|
||||
if (held?.block_id && info.waterloggable === held.block_id && !is_waterlogged(value, info)) {
|
||||
this.#waterlog(player, x, y, z, face, hit);
|
||||
return;
|
||||
}
|
||||
|
||||
// place the held block against the face, or into the clicked block if it's replaceable. food that can be planted,
|
||||
// like a potato on farmland, is planted, and eaten when it can't be
|
||||
const held_slot = player.inventory.get_slot(player.selected_slot);
|
||||
const held = EverythingRegistry.get<ItemRegistry>("items", held_slot.type_id ?? "");
|
||||
const placeable = held?.block_id !== undefined && ty >= 0 && ty < CHUNK_HEIGHT &&
|
||||
can_place(
|
||||
EverythingRegistry.get<BlockRegistry>("blocks", held.block_id),
|
||||
this.world.get_block_info(tx, ty, tz),
|
||||
this.world.get_block_value(tx, ty, tz),
|
||||
this.world.get_block_id(tx, ty - 1, tz),
|
||||
);
|
||||
if (!placeable || !held?.block_id) {
|
||||
@@ -1032,15 +1055,21 @@ export class GameServer {
|
||||
}
|
||||
// solid blocks can't go where a mob or player is, the one placing it included, like minecraft
|
||||
const placing = EverythingRegistry.get<BlockRegistry>("blocks", held.block_id)!;
|
||||
const value = this.mods.rules.placement_value(placing, player.yaw, {
|
||||
x: tx,
|
||||
y: ty,
|
||||
z: tz,
|
||||
face,
|
||||
hit: [hit[0], hit[1], hit[2]],
|
||||
});
|
||||
const state = value >>> 16;
|
||||
if (placing?.has_collision && this.#occupied(tx, ty, tz, collision_shape_at(placing, value))) {
|
||||
// put into a source of the liquid it can hold, it holds it
|
||||
const placed_value = waterlog_placement(
|
||||
this.mods.rules.placement_value(placing, player.yaw, {
|
||||
x: tx,
|
||||
y: ty,
|
||||
z: tz,
|
||||
face,
|
||||
hit: [hit[0], hit[1], hit[2]],
|
||||
}),
|
||||
placing,
|
||||
this.world.get_block_value(tx, ty, tz),
|
||||
this.world.get_block_info(tx, ty, tz),
|
||||
);
|
||||
const state = placed_value >>> 16;
|
||||
if (placing?.has_collision && this.#occupied(tx, ty, tz, collision_shape_at(placing, placed_value))) {
|
||||
this.#correct(player, tx, ty, tz);
|
||||
return;
|
||||
}
|
||||
@@ -1064,6 +1093,35 @@ export class GameServer {
|
||||
this.mods.after.block_place.emit(place);
|
||||
}
|
||||
|
||||
// the held liquid poured into the block at x y z, which can hold it. it's placing the liquid as far as mods can tell,
|
||||
// so a bucket empties like it does placing water anywhere else
|
||||
#waterlog(player: ServerPlayer, x: number, y: number, z: number, face: Faces, hit: number[]) {
|
||||
const held_slot = player.inventory.get_slot(player.selected_slot);
|
||||
const player_api = this.mods.player(player);
|
||||
const place = {
|
||||
player: player_api,
|
||||
block: { id: this.world.get_block_info(x, y, z)!.waterloggable!, x, y, z },
|
||||
face,
|
||||
hit: [hit[0], hit[1], hit[2]] as const,
|
||||
item: player_api.held_item,
|
||||
};
|
||||
const before_place = { ...place, cancel: false };
|
||||
this.mods.before.block_place.emit(before_place);
|
||||
if (before_place.cancel) {
|
||||
this.#correct(player, x, y, z);
|
||||
return;
|
||||
}
|
||||
const info = this.world.get_block_info(x, y, z)!;
|
||||
this.set_block_state(
|
||||
x,
|
||||
y,
|
||||
z,
|
||||
set_state_value(this.world.get_block_value(x, y, z), info, "waterlogged", 1)! >>> 16,
|
||||
);
|
||||
held_slot.amount = held_slot.amount! - 1;
|
||||
this.mods.after.block_place.emit(place);
|
||||
}
|
||||
|
||||
// whether a mob's or player's collision box overlaps a block with that shape at x, y, z
|
||||
#occupied(x: number, y: number, z: number, shape: Shape) {
|
||||
return this.players().some((player) =>
|
||||
@@ -1614,9 +1672,11 @@ export class GameServer {
|
||||
return false;
|
||||
}
|
||||
|
||||
// in a block entities pass through and players can place into, like water
|
||||
// in liquid, a waterlogged block's too
|
||||
#in_liquid(x: number, y: number, z: number) {
|
||||
return is_liquid_block(this.world.get_block_info(Math.floor(x), Math.floor(y), Math.floor(z)));
|
||||
const [bx, by, bz] = [Math.floor(x), Math.floor(y), Math.floor(z)];
|
||||
return liquid_block(this.world.get_block_value(bx, by, bz), this.world.get_block_info(bx, by, bz)) !==
|
||||
undefined;
|
||||
}
|
||||
|
||||
// right click with food: it's eaten if they're hungry, or it's always edible
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import { AIR, CHUNK_HEIGHT, ID_MASK } from "$/common/constants.ts";
|
||||
import type { BlockRegistry } from "$/common/everything_registry.ts";
|
||||
import { AIR_ID } from "$/common/protocol.ts";
|
||||
import { get_state_value, set_state_value } from "$/common/utils.ts";
|
||||
import { get_state_value, liquid_block, set_state_value } from "$/common/utils.ts";
|
||||
|
||||
// what the flow needs of the world
|
||||
export interface LiquidWorld {
|
||||
@@ -154,8 +154,10 @@ export class LiquidFlow {
|
||||
if (value === undefined) return undefined;
|
||||
const nid = value & ID_MASK;
|
||||
const block = this.#world.block(nid);
|
||||
if (!block?.liquid) return undefined;
|
||||
return { nid, block, level: get_state_value(value, block, "level")! };
|
||||
if (block?.liquid) return { nid, block, level: get_state_value(value, block, "level")! };
|
||||
// a waterlogged block is a source of its liquid that never flows away, like minecraft's
|
||||
const liquid = liquid_block(value, block);
|
||||
return liquid && { nid: this.#world.nid(liquid.id), block: liquid, level: 0 };
|
||||
}
|
||||
|
||||
#set(block: BlockRegistry, x: number, y: number, z: number, level: number) {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// see docs/modding/. parts not built yet throw when used, saying so
|
||||
import { BlockRegistry, EverythingRegistry, ItemRegistry, liquid_height } from "$/common/everything_registry.ts";
|
||||
import { Container as EngineContainer, ItemStack as EngineItemStack } from "$/common/inventory.ts";
|
||||
import { get_state_value, set_state_value } from "$/common/utils.ts";
|
||||
import { get_state_value, is_waterlogged, liquid_block, set_state_value } from "$/common/utils.ts";
|
||||
import { collision_shape_at, covers_face, raycast_blocks, type Shape } from "$/common/block_shapes.ts";
|
||||
import { AIR, ID_MASK } from "$/common/constants.ts";
|
||||
import { BlockRules } from "$/common/block_rules.ts";
|
||||
@@ -666,8 +666,12 @@ export class ModRuntime {
|
||||
(x, y, z) => raycast_shape(world, x, y, z, liquids),
|
||||
);
|
||||
if (!hit) return undefined;
|
||||
// what was hit in a waterlogged block with liquids on is its liquid, see raycast_shape
|
||||
const value = world.get_block_value(hit.x, hit.y, hit.z);
|
||||
const block = world.get_block_info(hit.x, hit.y, hit.z);
|
||||
const liquid = liquids === "none" ? undefined : liquid_block(value, block);
|
||||
return {
|
||||
id: world.get_block_id(hit.x, hit.y, hit.z),
|
||||
id: liquid?.id ?? world.get_block_id(hit.x, hit.y, hit.z),
|
||||
x: hit.x,
|
||||
y: hit.y,
|
||||
z: hit.z,
|
||||
@@ -1099,6 +1103,8 @@ function raycast_shape(
|
||||
if (liquids === "none" || (liquids === "source_only" && level !== 0)) return undefined;
|
||||
return [[0, 0, 0, 1, liquid_height(level, block.liquid.range), 1]];
|
||||
}
|
||||
// the source in a waterlogged block covers the block, like minecraft's fluid clip
|
||||
if (liquids !== "none" && is_waterlogged(value, block)) return [[0, 0, 0, 1, 1, 1]];
|
||||
const shape = collision_shape_at(block, value);
|
||||
return shape.length > 0 ? shape : undefined;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user