Blocks interacting on the world api
This commit is contained in:
@@ -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));
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,8 @@ import { Container as EngineContainer, ItemStack as EngineItemStack } from "$/co
|
||||
import { block_states, get_state_value, is_waterlogged, liquid_block, set_state_value } from "$/common/utils.ts";
|
||||
import { LootTables } from "./loot.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 { AIR, faces, ID_MASK } from "$/common/constants.ts";
|
||||
import { physics } from "$/common/physics.ts";
|
||||
import { clock } from "$/common/time.ts";
|
||||
import { BlockRules } from "$/common/block_rules.ts";
|
||||
import type { SharedContext } from "$/common/mod_api/shared.ts";
|
||||
@@ -62,6 +63,7 @@ import { damage_source, type Entity } from "./entity/entity.ts";
|
||||
import type { LivingEntity } from "./entity/living_entity.ts";
|
||||
import type { EffectJson } from "$/common/mod_data.ts";
|
||||
import { create_entity } from "./entity/entity_types.ts";
|
||||
import { BLOCK_DROP_PICKUP_DELAY, ItemEntity } from "./entity/item_entity.ts";
|
||||
import { attribute_type, type AttributeType, MODIFIER_OPERATIONS } from "./entity/attributes.ts";
|
||||
import { effect_type, EffectInstance, type EffectType } from "./entity/effects.ts";
|
||||
import { biome_season } from "$/common/worldgen/biomes.ts";
|
||||
@@ -673,10 +675,18 @@ export class ModRuntime {
|
||||
game().set_block(x, y, z, id);
|
||||
return true;
|
||||
},
|
||||
break_block: (x, y, z) => {
|
||||
if (game().world.get_block_id(x, y, z) === AIR_ID) return false;
|
||||
game().destroy_block(x, y, z);
|
||||
return true;
|
||||
break_block: (x, y, z, options = {}) => {
|
||||
if (game().world.get_block_id(x, y, z) === AIR_ID) return undefined;
|
||||
const how = { harvest: options.harvest === true, collect: options.collect === true };
|
||||
return game().destroy_block(x, y, z, undefined, how)?.map(item_copy);
|
||||
},
|
||||
set_break_progress: (x, y, z, progress) => {
|
||||
if (progress !== undefined && !Number.isFinite(progress)) {
|
||||
throw new Error(
|
||||
`break progress is 0 to 1, or undefined to take the cracks off, not ${progress}`,
|
||||
);
|
||||
}
|
||||
game().set_break_progress(x, y, z, progress);
|
||||
},
|
||||
get_state: (x, y, z, name) => {
|
||||
const id = game().world.get_block_id(x, y, z);
|
||||
@@ -710,6 +720,28 @@ export class ModRuntime {
|
||||
get_block_item: (x, y, z) =>
|
||||
structuredClone(game().world.get_tile(x, y, z)?.client_data?.item as ItemStack),
|
||||
drop_item: (x, y, z, item) => void game().pop_item(x, y, z, new_stack(item)),
|
||||
spawn_item: (x, y, z, item) => {
|
||||
if (!(item.count > 0)) throw new Error("an item lying around needs a count of 1 or more");
|
||||
return entity_ref(game().spawn_item(x, y, z, new_stack(item), BLOCK_DROP_PICKUP_DELAY));
|
||||
},
|
||||
entities_in: (from, to, type) => {
|
||||
const [x0, x1] = [Math.min(from.x, to.x), Math.max(from.x, to.x)];
|
||||
const [y0, y1] = [Math.min(from.y, to.y), Math.max(from.y, to.y)];
|
||||
const [z0, z1] = [Math.min(from.z, to.z), Math.max(from.z, to.z)];
|
||||
return game().entities.all().filter((entity) => {
|
||||
const half = entity.width / 2;
|
||||
return (type === undefined || entity.type === type) &&
|
||||
entity.x - half < x1 && entity.x + half > x0 && entity.y < y1 &&
|
||||
entity.y + entity.height > y0 &&
|
||||
entity.z - half < z1 && entity.z + half > z0;
|
||||
}).map(entity_ref);
|
||||
},
|
||||
use_item_on: (item, x, y, z, face) => {
|
||||
if (!faces.includes(face)) throw new Error(`a face is one of ${faces.join(", ")}, not ${face}`);
|
||||
if (!(item.count > 0)) throw new Error("using an item needs a count of 1 or more");
|
||||
const { held, given } = game().use_item_on(new_stack(item), x, y, z, face);
|
||||
return { item: held && item_copy(held), given: given.map(item_copy) };
|
||||
},
|
||||
spawn_entity: (id, x, y, z) => {
|
||||
const entity = EverythingRegistry.get("entities", id)
|
||||
? create_entity(game().entities, id)
|
||||
@@ -754,6 +786,17 @@ export class ModRuntime {
|
||||
},
|
||||
};
|
||||
},
|
||||
liquid_flow: (x, y, z) => {
|
||||
const [fx, fy, fz] = physics.liquid_flow(
|
||||
Math.floor(x),
|
||||
Math.floor(y),
|
||||
Math.floor(z),
|
||||
(x, y, z) => game().entities.liquid_at(x, y, z),
|
||||
(x, y, z) => game().world.collision_shape(x, y, z),
|
||||
);
|
||||
// without any -0
|
||||
return { x: fx + 0, y: fy + 0, z: fz + 0 };
|
||||
},
|
||||
is_loaded: () => true,
|
||||
biome_at: (x, z) => game().world.biome_at(Math.floor(x), Math.floor(z)),
|
||||
season_at: (x, z) =>
|
||||
@@ -815,6 +858,11 @@ export class ModRuntime {
|
||||
},
|
||||
blocks: {
|
||||
exists: (id) => EverythingRegistry.get("blocks", id) !== undefined,
|
||||
toughness: (id) => {
|
||||
const block = EverythingRegistry.get<BlockRegistry>("blocks", id);
|
||||
if (!block) throw new Error(`unknown block ${id}`);
|
||||
return block.toughness ?? Infinity;
|
||||
},
|
||||
components: (id) => {
|
||||
const block = EverythingRegistry.get<BlockRegistry>("blocks", id);
|
||||
if (!block) throw new Error(`unknown block ${id}`);
|
||||
@@ -1129,6 +1177,11 @@ export function item_api(stack: EngineItemStack): ItemStack {
|
||||
};
|
||||
}
|
||||
|
||||
// an engine stack as a script's item, a copy
|
||||
export function item_copy(stack: EngineItemStack): ItemStack {
|
||||
return structuredClone(stack.to_data());
|
||||
}
|
||||
|
||||
export function new_stack(item: ItemStack): EngineItemStack {
|
||||
if (!EverythingRegistry.get("items", item.id)) throw new Error(`unknown item ${item.id}`);
|
||||
const stack = new EngineItemStack(item.id, item.count);
|
||||
@@ -1347,6 +1400,10 @@ function player_api(game: GameServer, player: ServerPlayer): Player {
|
||||
remove_effect: (id) => player.remove_effect(known_effect(id).id),
|
||||
is_swimming: () => player.swimming,
|
||||
is_sneaking: () => player.sneaking,
|
||||
push(x, y, z) {
|
||||
if (![x, y, z].every(Number.isFinite)) throw new Error("a push is three numbers, in blocks a second");
|
||||
game.push_player(player, x, y, z);
|
||||
},
|
||||
start_using(ticks) {
|
||||
if (!Number.isInteger(ticks) || (ticks < 1 && ticks !== -1)) {
|
||||
throw new Error("start_using takes a whole number of ticks, or -1 until they let go");
|
||||
@@ -1408,11 +1465,27 @@ export function entity_ref(entity: Entity): EntityRef {
|
||||
get removed() {
|
||||
return entity.removed;
|
||||
},
|
||||
get velocity() {
|
||||
return { x: entity.vx, y: entity.vy, z: entity.vz };
|
||||
},
|
||||
teleport(x, y, z) {
|
||||
if (!entity.removed) entity.set_position(x, y, z);
|
||||
},
|
||||
push(x, y, z) {
|
||||
if (![x, y, z].every(Number.isFinite)) throw new Error("a push is three numbers, in blocks a second");
|
||||
entity.vx += x;
|
||||
entity.vy += y;
|
||||
entity.vz += z;
|
||||
},
|
||||
remove() {
|
||||
entity.remove();
|
||||
},
|
||||
get_item: () => entity instanceof ItemEntity ? item_copy(entity.item) : undefined,
|
||||
set_item(item) {
|
||||
if (!(entity instanceof ItemEntity)) throw new Error(`${entity.type} isn't an item lying around`);
|
||||
if (entity.removed) return;
|
||||
if (!item || item.count <= 0) entity.remove();
|
||||
else entity.item = new_stack(item);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -40,6 +40,9 @@ export interface SavedPlayer {
|
||||
flying?: boolean;
|
||||
}
|
||||
|
||||
// the connection of the player machines use items as, see GameServer.use_item_on. nothing is ever sent to it
|
||||
export const MACHINE_CONN = -1;
|
||||
|
||||
// the most ticks the server moves a knocked back player, in case they never land, like in water
|
||||
const MAX_KNOCKBACK_TICKS = 20;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user