Blocks interacting on the world api

This commit is contained in:
2026-10-15 12:49:44 -03:00
parent 7faa070d83
commit b43e8fe8b8
15 changed files with 493 additions and 28 deletions
+73 -7
View File
@@ -2,6 +2,7 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import {
BREAK_STAGES,
CHUNK_HEIGHT,
CHUNK_SIZE,
FACE_OFFSETS,
@@ -64,7 +65,7 @@ import { biome_ids, get_biome } from "$/common/worldgen/biomes.ts";
import type { GameLoop } from "./game_loop.ts";
import { update_crafting_result } from "./crafting.ts";
import type { Menu } from "./menu.ts";
import { SavedPlayer, ServerPlayer } from "./player.ts";
import { MACHINE_CONN, SavedPlayer, ServerPlayer } from "./player.ts";
import { encode_skin, MAX_SKIN_BYTES, skin_problem } from "./skins.ts";
import { ServerWorld, SPAWN_AREA_CHUNKS, Tile } from "./world.ts";
import type { WorldSave, WorldSource } from "./saves.ts";
@@ -596,6 +597,7 @@ export class GameServer {
set_block(x: number, y: number, z: number, id: string, player?: ServerPlayer, state?: number) {
const old_id = this.world.get_block_id(x, y, z);
if (this.#break_stages.size > 0) this.set_break_progress(x, y, z, undefined);
if (old_id !== AIR_ID) {
const components = this.mods.components_of(old_id).filter(({ component }) => component.on_break);
if (components.length > 0) {
@@ -629,6 +631,61 @@ export class GameServer {
}
}
// the cracks machines are making on blocks, as the stage clients draw, by position. see set_break_progress
#break_stages = new Map<string, number>();
// cracks on the block at x y z, 0 to 1 of the way to broken, for every player who has its chunk. undefined takes them
// off. only a change of stage is sent, so a drill can set it every tick
set_break_progress(x: number, y: number, z: number, progress: number | undefined) {
const key = `${x} ${y} ${z}`;
const stage = progress === undefined
? undefined
: Math.round(Math.max(0, Math.min(1, progress)) * BREAK_STAGES);
if (this.#break_stages.get(key) === stage) return;
if (stage === undefined) this.#break_stages.delete(key);
else this.#break_stages.set(key, stage);
const message: ServerMessage = { type: "break_progress", x, y, z, ...(stage !== undefined && { stage }) };
for (const player of this.#players.values()) {
if (player.chunks.has_block(x, z)) this.#send(player, message);
}
}
// a script pushing a player, see Player.push. their client moves them, so it's added there, unless the server is
// moving them while they're knocked back
push_player(player: ServerPlayer, x: number, y: number, z: number) {
if (player.knockback_ticks === undefined) {
this.#send(player, { type: "push", motion: [x, y, z] });
return;
}
player.vx += x;
player.vy += y;
player.vz += z;
}
// the player machines use items as, made the first time. it's never online or in the world
#machine_player: ServerPlayer | undefined;
// uses stack on face of the block at x y z like a player holding it would, see ServerWorld.use_item_on: as a player
// standing in the middle of the block in front of that face, looking at it. what it holds after, and anything else
// it was given
use_item_on(stack: ItemStack, x: number, y: number, z: number, face: Faces) {
const player = this.#machine_player ??= new ServerPlayer(this.entities, MACHINE_CONN, "[machine]", 0);
const { x: dx, y: dy, z: dz } = FACE_OFFSETS[face];
player.set_position(x + 0.5 + dx, y + 0.5 + dy - player.eye_height, z + 0.5 + dz);
// facing back the other way, see horizontal_facing
player.yaw = dx || dz ? Math.atan2(dx, dz) : 0;
player.pitch = -dy * Math.PI / 2;
player.sneaking = false;
player.using = undefined;
player.selected_slot = 0;
player.inventory.clear();
player.inventory.set_item(0, stack);
this.#use_block(player, x, y, z, face, [0.5 + dx / 2, 0.5 + dy / 2, 0.5 + dz / 2]);
const items = Array.from({ length: player.inventory.size }, (_, slot) => player.inventory.get_item(slot));
player.inventory.clear();
return { held: items[0], given: items.slice(1).filter((item) => item !== undefined) };
}
// what a loot table gives, ready to drop. a table that can't be rolled is logged and gives nothing, so a broken
// table never stops a block breaking or a mob dying
roll_loot(table: string, context: LootContext): ItemStack[] {
@@ -640,17 +697,24 @@ export class GameServer {
}
}
// breaks a block the way breaking it by hand does: its components' on_break, then what it drops. without the
// right tool of a high enough tier (or without a player) blocks that need one drop nothing
destroy_block(x: number, y: number, z: number, player?: ServerPlayer) {
// breaks a block the way breaking it by hand does: its components' on_break, then what it drops, which it returns.
// without the right tool of a high enough tier (or without a player) blocks that need one drop nothing, unless a
// machine breaks it as if it had one (harvest). collect returns the drops without dropping them
destroy_block(
x: number,
y: number,
z: number,
player?: ServerPlayer,
how: { harvest?: boolean; collect?: boolean } = {},
): ItemStack[] | undefined {
const info = this.world.get_block_info(x, y, z);
if (!info) {
return;
return undefined;
}
const held = EverythingRegistry.get<ItemRegistry>("items", player?.held_item?.type_id ?? "");
const tool = held?.tool;
const right_tool = tool !== undefined && tool.type === info.tool_to_break && tool.tier >= (info.tool_tier ?? 0);
const drops = !info.requires_tool || right_tool;
const drops = how.harvest || !info.requires_tool || right_tool;
const value = this.world.get_block_value(x, y, z);
// rolled while it's still there, for its states
const loot = drops && info.loot_table
@@ -666,7 +730,8 @@ export class GameServer {
// 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);
for (const stack of loot) this.pop_item(x, y, z, stack);
if (!how.collect) { for (const stack of loot) this.pop_item(x, y, z, stack); }
return loot;
}
get_or_create_tile(x: number, y: number, z: number): Tile {
@@ -2219,6 +2284,7 @@ export class GameServer {
}
#send(player: ServerPlayer, message: ServerMessage) {
if (player.conn === MACHINE_CONN) return;
this.#host.send(player.conn, JSON.stringify(message));
}