Proper cutting board
This commit is contained in:
@@ -123,6 +123,8 @@ interface SavedTile {
|
||||
z: number;
|
||||
// what BlockRef.data holds
|
||||
mod_data?: unknown;
|
||||
// what it shows, see ServerWorld.set_block_item in the mod api
|
||||
item?: ItemData;
|
||||
}
|
||||
|
||||
// the loaded mods, see load_mods.ts
|
||||
@@ -197,7 +199,14 @@ export class GameServer {
|
||||
this.containers.set(id, container);
|
||||
}
|
||||
for (const tile of saved?.tiles ?? []) {
|
||||
this.world.add_tile({ id: tile.id, x: tile.x, y: tile.y, z: tile.z, mod_data: tile.mod_data });
|
||||
this.world.add_tile({
|
||||
id: tile.id,
|
||||
x: tile.x,
|
||||
y: tile.y,
|
||||
z: tile.z,
|
||||
mod_data: tile.mod_data,
|
||||
item: tile.item,
|
||||
});
|
||||
}
|
||||
this.entities.load(saved?.version === 4 ? upgrade_v4_entities(saved.entities) : saved?.entities ?? []);
|
||||
this.#saved_players = saved?.players ?? {};
|
||||
@@ -218,6 +227,16 @@ export class GameServer {
|
||||
}
|
||||
}
|
||||
};
|
||||
this.world.on_tile_item_change = (x, y, z, item) => {
|
||||
const data = JSON.stringify(
|
||||
{ type: "set_block_item", x, y, z, item: item ?? null } satisfies ServerMessage,
|
||||
);
|
||||
for (const player of this.#players.values()) {
|
||||
if (player.chunks.has_block(x, z)) {
|
||||
this.#host.send(player.conn, data);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// connections
|
||||
@@ -331,6 +350,7 @@ export class GameServer {
|
||||
y: tile.y,
|
||||
z: tile.z,
|
||||
mod_data: tile.mod_data,
|
||||
item: tile.item,
|
||||
})),
|
||||
containers: Object.fromEntries(
|
||||
[...this.containers].map(([id, container]) => [id, container.to_data()]),
|
||||
@@ -1458,6 +1478,9 @@ export class GameServer {
|
||||
this.#send(player, { type: "chunk_batch_start" });
|
||||
for (const [x, z] of batch) {
|
||||
this.#host.send(player.conn, this.world.level_chunk(x, z));
|
||||
for (const tile of this.world.item_tiles(x, z)) {
|
||||
this.#send(player, { type: "set_block_item", x: tile.x, y: tile.y, z: tile.z, item: tile.item! });
|
||||
}
|
||||
}
|
||||
this.#send(player, { type: "chunk_batch_finished", size: batch.length });
|
||||
}
|
||||
|
||||
@@ -434,6 +434,19 @@ export class ModRuntime {
|
||||
return true;
|
||||
},
|
||||
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;
|
||||
if (!item || item.count <= 0) {
|
||||
const tile = game().world.get_tile(x, y, z);
|
||||
if (tile?.item) game().world.set_tile_item(tile, undefined);
|
||||
return;
|
||||
}
|
||||
game().world.set_tile_item(game().get_or_create_tile(x, y, z), new_stack(item).to_data());
|
||||
},
|
||||
get_block_item: (x, y, z) => {
|
||||
const item = game().world.get_tile(x, y, z)?.item;
|
||||
return item && structuredClone(item);
|
||||
},
|
||||
drop_item: (x, y, z, item) => void game().pop_item(x, y, z, new_stack(item)),
|
||||
spawn_entity: (id, x, y, z) => {
|
||||
const entity = EverythingRegistry.get("entities", id)
|
||||
@@ -750,6 +763,9 @@ function player_api(game: GameServer, player: ServerPlayer): Player {
|
||||
get position() {
|
||||
return { x: player.x, y: player.y, z: player.z };
|
||||
},
|
||||
get yaw() {
|
||||
return player.yaw;
|
||||
},
|
||||
inventory,
|
||||
get selected_slot() {
|
||||
return player.selected_slot;
|
||||
|
||||
+36
-1
@@ -6,6 +6,7 @@ import { block_value, chunk_key, default_block_value } from "$/common/utils.ts";
|
||||
import { LruMap } from "./lru.ts";
|
||||
import { CYCLE_TICKS } from "$/common/time.ts";
|
||||
import { encode_level_chunk } from "$/common/chunk_streaming.ts";
|
||||
import type { ItemData } from "$/common/inventory.ts";
|
||||
import { CHUNK_VOLUME, ChunkBlocks, decode_chunk, encode_chunk, is_changed, SavedChunk } from "./saves.ts";
|
||||
|
||||
// generation only, rebuilt when needed. 256 KB each
|
||||
@@ -16,7 +17,7 @@ const SPAWN_SEARCH_CHUNKS = 32;
|
||||
// generated for worldgen_fingerprint, spread out so most changes to world generation touch at least one
|
||||
const FINGERPRINT_CHUNKS = [[0, 0], [7, -3], [-45, 18], [130, -80], [-310, 260], [1000, -1000]];
|
||||
|
||||
// a block with data the server keeps, like where a chest's items are. never sent to clients
|
||||
// a block with data the server keeps, like where a chest's items are. never sent to clients, but for item
|
||||
export interface Tile {
|
||||
id: string;
|
||||
x: number;
|
||||
@@ -24,6 +25,8 @@ export interface Tile {
|
||||
z: number;
|
||||
// what BlockRef.data holds
|
||||
mod_data?: unknown;
|
||||
// the item it shows, sent to clients, see BlockRegistry.item_display. only set through set_tile_item
|
||||
item?: ItemData;
|
||||
}
|
||||
|
||||
export function position_key(x: number, y: number, z: number) {
|
||||
@@ -47,6 +50,8 @@ export class ServerWorld {
|
||||
// chunks changed since they were last saved, by chunk_key
|
||||
#unsaved = new Map<number, [number, number]>();
|
||||
tiles = new Map<string, Tile>();
|
||||
// the tiles showing an item, by chunk_key, to send with their chunk
|
||||
#item_tiles = new Map<number, Set<Tile>>();
|
||||
|
||||
// ticks since 06:00 of day 1, see common/time.ts
|
||||
time = 0;
|
||||
@@ -60,6 +65,7 @@ export class ServerWorld {
|
||||
dirty = false;
|
||||
|
||||
on_block_change?: (x: number, y: number, z: number, id: string, state: number) => void;
|
||||
on_tile_item_change?: (x: number, y: number, z: number, item: ItemData | undefined) => void;
|
||||
|
||||
constructor(seed: string, worldgen?: WorldgenSetup) {
|
||||
this.seed = seed;
|
||||
@@ -298,15 +304,44 @@ export class ServerWorld {
|
||||
}
|
||||
|
||||
add_tile(tile: Tile) {
|
||||
const item = tile.item;
|
||||
delete tile.item;
|
||||
this.tiles.set(position_key(tile.x, tile.y, tile.z), tile);
|
||||
if (item) this.set_tile_item(tile, item);
|
||||
this.dirty = true;
|
||||
}
|
||||
|
||||
remove_tile(x: number, y: number, z: number) {
|
||||
const tile = this.get_tile(x, y, z);
|
||||
if (tile?.item) this.set_tile_item(tile, undefined);
|
||||
this.tiles.delete(position_key(x, y, z));
|
||||
this.dirty = true;
|
||||
}
|
||||
|
||||
set_tile_item(tile: Tile, item: ItemData | undefined) {
|
||||
const key = chunk_key(Math.floor(tile.x / CHUNK_SIZE), Math.floor(tile.z / CHUNK_SIZE));
|
||||
let tiles = this.#item_tiles.get(key);
|
||||
if (item) {
|
||||
tile.item = item;
|
||||
if (!tiles) {
|
||||
tiles = new Set();
|
||||
this.#item_tiles.set(key, tiles);
|
||||
}
|
||||
tiles.add(tile);
|
||||
} else {
|
||||
delete tile.item;
|
||||
tiles?.delete(tile);
|
||||
if (tiles?.size === 0) this.#item_tiles.delete(key);
|
||||
}
|
||||
this.dirty = true;
|
||||
this.on_tile_item_change?.(tile.x, tile.y, tile.z, item);
|
||||
}
|
||||
|
||||
// the tiles in a chunk that show an item
|
||||
item_tiles(chunk_x: number, chunk_z: number): Iterable<Tile> {
|
||||
return this.#item_tiles.get(chunk_key(chunk_x, chunk_z)) ?? [];
|
||||
}
|
||||
|
||||
#record_change(x: number, y: number, z: number, id: string, state: number) {
|
||||
const key = chunk_key(Math.floor(x / CHUNK_SIZE), Math.floor(z / CHUNK_SIZE));
|
||||
let chunk_changes = this.#changes.get(key);
|
||||
|
||||
Reference in New Issue
Block a user