Fences
This commit is contained in:
@@ -32,7 +32,7 @@ import {
|
||||
placement_target,
|
||||
set_state_value,
|
||||
} from "$/common/utils.ts";
|
||||
import { type Shape, shape_at } from "$/common/block_shapes.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";
|
||||
import {
|
||||
@@ -544,8 +544,31 @@ export class GameServer {
|
||||
// changes a block's state bits without anything else happening, see ServerWorld.get_block_value
|
||||
set_block_state(x: number, y: number, z: number, state: number) {
|
||||
const id = this.world.get_block_id(x, y, z);
|
||||
if (id !== AIR_ID) {
|
||||
if (id !== AIR_ID && this.world.get_block_value(x, y, z) >>> 16 !== state) {
|
||||
this.world.set_block(x, y, z, id, state);
|
||||
this.#notify_neighbors(x, y, z);
|
||||
}
|
||||
}
|
||||
|
||||
// tells the blocks around one that changed, so they can change with it like a fence connecting to it: their
|
||||
// components' on_neighbor_change, like minecraft's updateShape
|
||||
#notify_neighbors(x: number, y: number, z: number) {
|
||||
const neighbor = { x, y, z, id: this.world.get_block_id(x, y, z) };
|
||||
for (const [dx, dy, dz] of NEIGHBORS) {
|
||||
const [nx, ny, nz] = [x + dx, y + dy, z + dz];
|
||||
if (ny < 0 || ny >= CHUNK_HEIGHT) continue;
|
||||
const id = this.world.get_block_id(nx, ny, nz);
|
||||
if (id === AIR_ID) continue;
|
||||
const components = this.mods.components_of(id).filter(({ component }) => component.on_neighbor_change);
|
||||
if (components.length === 0) continue;
|
||||
const block = this.mods.block_ref(nx, ny, nz, id);
|
||||
for (const { mod, id: component_id, component, params } of components) {
|
||||
run_guarded(
|
||||
mod,
|
||||
`${component_id} on_neighbor_change`,
|
||||
() => component.on_neighbor_change!(block, params, neighbor),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -573,6 +596,7 @@ export class GameServer {
|
||||
}
|
||||
}
|
||||
}
|
||||
this.#notify_neighbors(x, y, z);
|
||||
|
||||
// a block standing on this one that can't stand on what it is now breaks, like a crop on farmland that was dug
|
||||
if (y + 1 < CHUNK_HEIGHT) {
|
||||
@@ -1012,7 +1036,7 @@ export class GameServer {
|
||||
const state = placed_state(held.block_id, player);
|
||||
const placing = EverythingRegistry.get<BlockRegistry>("blocks", held.block_id);
|
||||
const value = state === undefined ? default_block_value(0, placing) : block_value(0, state);
|
||||
if (placing?.has_collision && this.#occupied(tx, ty, tz, shape_at(placing, value))) {
|
||||
if (placing?.has_collision && this.#occupied(tx, ty, tz, collision_shape_at(placing, value))) {
|
||||
this.#correct(player, tx, ty, tz);
|
||||
return;
|
||||
}
|
||||
@@ -1729,7 +1753,10 @@ export class GameServer {
|
||||
if (y < 0 || y >= CHUNK_HEIGHT || !player.chunks.has_block(x, z)) {
|
||||
return;
|
||||
}
|
||||
this.#send(player, { type: "set_block", x, y, z, id: this.world.get_block_id(x, y, z) });
|
||||
// with its states, or the client would show it at its defaults, like a fence that lost its connections
|
||||
const id = this.world.get_block_id(x, y, z);
|
||||
const state = this.world.get_block_value(x, y, z) >>> 16;
|
||||
this.#send(player, state ? { type: "set_block", x, y, z, id, state } : { type: "set_block", x, y, z, id });
|
||||
}
|
||||
|
||||
// chunks
|
||||
@@ -1925,6 +1952,9 @@ function death_message(name: string, source: DamageSource) {
|
||||
// minecraft's getStateForPlacement with getHorizontalDirection().getOpposite(). facing is 0 north, 1 east, 2 south,
|
||||
// 3 west, and variants turn the model with it (its north side is the front, like engine:cube's). they look along
|
||||
// (-sin yaw, -cos yaw), so the way back to them is (sin yaw, cos yaw)
|
||||
// the 6 blocks beside one
|
||||
const NEIGHBORS = [[-1, 0, 0], [1, 0, 0], [0, -1, 0], [0, 1, 0], [0, 0, -1], [0, 0, 1]] as const;
|
||||
|
||||
function placed_state(block_id: string, player: ServerPlayer): number | undefined {
|
||||
const info = EverythingRegistry.get<BlockRegistry>("blocks", block_id);
|
||||
if (!info?.states?.some((state) => state.name === "facing" && state.bits >= 2)) return undefined;
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
import { BlockRegistry, EverythingRegistry, ItemRegistry } 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 { covers_face } from "$/common/block_shapes.ts";
|
||||
import { tag_api } from "$/common/tags.ts";
|
||||
import type {
|
||||
BlockComponent,
|
||||
@@ -562,6 +563,7 @@ export class ModRuntime {
|
||||
game().set_block_state(x, y, z, new_value >>> 16);
|
||||
return true;
|
||||
},
|
||||
is_face_sturdy: (x, y, z, face) => covers_face(game().world.collision_shape(x, y, z), face),
|
||||
get_block_data: (x, y, z) => game().world.get_tile(x, y, z)?.mod_data as never,
|
||||
set_block_item: (x, y, z, item) => {
|
||||
if (game().world.get_block_id(x, y, z) === AIR_ID) return;
|
||||
|
||||
Reference in New Issue
Block a user