Proper cutting board

This commit is contained in:
2026-09-29 00:04:56 -03:00
parent a51adba46f
commit 445148e812
26 changed files with 549 additions and 16 deletions
+36 -1
View File
@@ -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);