Region file format
This commit is contained in:
+28
-37
@@ -14,7 +14,6 @@ import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everyt
|
||||
import { can_take_output, click_slot, Container, ItemData, ItemStack, take_output } from "$/common/inventory.ts";
|
||||
import {
|
||||
AIR_ID,
|
||||
BlockChange,
|
||||
ClientMessage,
|
||||
ContainerKey,
|
||||
CRAFTING_RESULT_SLOT,
|
||||
@@ -45,6 +44,7 @@ import { consume_recipe_items, update_crafting_result } from "./crafting.ts";
|
||||
import { OpenScreen, SavedPlayer, ServerPlayer } from "./player.ts";
|
||||
import { encode_skin, MAX_SKIN_BYTES, skin_problem } from "./skins.ts";
|
||||
import { ServerWorld, Tile } from "./world.ts";
|
||||
import type { WorldSave } from "./saves.ts";
|
||||
import {
|
||||
BLOCK_DROP_PICKUP_DELAY,
|
||||
DESPAWN_TICKS,
|
||||
@@ -91,20 +91,22 @@ export interface GameHost {
|
||||
download(url: string, max_bytes: number): Promise<Uint8Array>;
|
||||
}
|
||||
|
||||
export interface SavedWorld {
|
||||
version: 3;
|
||||
// level.json, everything saved but the blocks, which are in region files (see saves.ts)
|
||||
export interface SavedLevel {
|
||||
version: 4;
|
||||
seed: string;
|
||||
// world time in ticks, 0 in saves from before the day and night cycle
|
||||
time?: number;
|
||||
changes: BlockChange[];
|
||||
// see ServerWorld.worldgen_fingerprint
|
||||
worldgen: string;
|
||||
// world time in ticks
|
||||
time: number;
|
||||
tiles: SavedTile[];
|
||||
// every ctx.containers container, by id
|
||||
containers?: Record<string, (ItemData | null)[]>;
|
||||
containers: Record<string, (ItemData | null)[]>;
|
||||
players: Record<string, SavedPlayer>;
|
||||
// items on the ground
|
||||
entities?: SavedItemEntity[];
|
||||
entities: SavedItemEntity[];
|
||||
// ctx.storage of every mod, by mod id
|
||||
mod_storage?: Record<string, Record<string, unknown>>;
|
||||
mod_storage: Record<string, Record<string, unknown>>;
|
||||
}
|
||||
|
||||
interface SavedTile {
|
||||
@@ -114,9 +116,6 @@ interface SavedTile {
|
||||
z: number;
|
||||
// what BlockRef.data holds
|
||||
mod_data?: unknown;
|
||||
// version 2, when chests and furnaces were engine code: their fields and their items
|
||||
data?: Record<string, unknown>;
|
||||
containers?: Record<string, (ItemData | null)[]>;
|
||||
}
|
||||
|
||||
// the loaded mods, see load_mods.ts
|
||||
@@ -148,17 +147,21 @@ export class GameServer {
|
||||
// what runs tick(), for /tps. tests call tick() themselves
|
||||
loop?: GameLoop;
|
||||
|
||||
constructor(host: GameHost, save: string | undefined, default_seed: string, mods: GameMods) {
|
||||
constructor(host: GameHost, save: WorldSave | undefined, default_seed: string, mods: GameMods) {
|
||||
this.#host = host;
|
||||
this.#mods = mods;
|
||||
this.mods = mods.runtime;
|
||||
this.recipes = mods.recipes;
|
||||
|
||||
// version 1 saves only had the seed and block changes
|
||||
const saved: Partial<SavedWorld> | undefined = save ? JSON.parse(save) : undefined;
|
||||
const saved: SavedLevel | undefined = save?.level ? JSON.parse(save.level) : undefined;
|
||||
if (saved && saved.version !== 4) {
|
||||
throw new Error(`The world was saved in format ${saved.version}, this server only loads format 4`);
|
||||
}
|
||||
this.world = new ServerWorld(saved?.seed ?? default_seed, mods.worldgen);
|
||||
this.mods.storage = saved?.mod_storage ?? {};
|
||||
this.world.load_changes(saved?.changes ?? []);
|
||||
if (saved) {
|
||||
this.world.load_chunks(save?.chunks ?? [], saved.worldgen);
|
||||
}
|
||||
this.world.time = saved?.time ?? 0;
|
||||
this.#day = this.world.day;
|
||||
for (const [id, items] of Object.entries(saved?.containers ?? {})) {
|
||||
@@ -167,14 +170,15 @@ 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: this.#upgrade_tile(tile) });
|
||||
this.world.add_tile({ id: tile.id, x: tile.x, y: tile.y, z: tile.z, mod_data: tile.mod_data });
|
||||
}
|
||||
for (const entity of saved?.entities ?? []) {
|
||||
const item = ItemEntity.load(this.#new_entity_id(), entity);
|
||||
this.#entities.set(item.id, item);
|
||||
}
|
||||
this.#saved_players = saved?.players ?? {};
|
||||
this.world.dirty = saved?.version !== 3;
|
||||
// a new world is saved straight away, and a loaded one when load_chunks had to save chunks again
|
||||
this.world.dirty = !saved || this.world.has_unsaved_chunks();
|
||||
this.#register_commands();
|
||||
|
||||
this.world.on_block_change = (x, y, z, id, state) =>
|
||||
@@ -273,15 +277,16 @@ export class GameServer {
|
||||
}
|
||||
}
|
||||
|
||||
save(): string {
|
||||
// level.json and the chunks changed since the last save
|
||||
save(): Required<WorldSave> {
|
||||
for (const player of this.#players.values()) {
|
||||
this.#saved_players[player.name] = player.save();
|
||||
}
|
||||
const saved: SavedWorld = {
|
||||
version: 3,
|
||||
const level: SavedLevel = {
|
||||
version: 4,
|
||||
seed: this.world.seed,
|
||||
worldgen: this.world.worldgen_fingerprint(),
|
||||
time: this.world.time,
|
||||
changes: this.world.all_changes(),
|
||||
tiles: [...this.world.tiles.values()].map((tile) => ({
|
||||
id: tile.id,
|
||||
x: tile.x,
|
||||
@@ -297,7 +302,7 @@ export class GameServer {
|
||||
mod_storage: this.mods.storage,
|
||||
};
|
||||
this.world.dirty = false;
|
||||
return JSON.stringify(saved);
|
||||
return { level: JSON.stringify(level), chunks: this.world.save_chunks() };
|
||||
}
|
||||
|
||||
// used by block behaviors and mods
|
||||
@@ -540,20 +545,6 @@ export class GameServer {
|
||||
|
||||
// version 2 saves had chests and furnaces in the engine, with their fields in data and their items in
|
||||
// containers.main. the components keep those fields and put the items in a ctx.containers container
|
||||
#upgrade_tile(tile: SavedTile): unknown {
|
||||
if (tile.mod_data !== undefined || (!tile.data && !tile.containers)) {
|
||||
return tile.mod_data;
|
||||
}
|
||||
const data: Record<string, unknown> = { ...tile.data };
|
||||
const items = tile.containers?.main;
|
||||
if (items) {
|
||||
const { id, container } = this.create_container(items.length);
|
||||
container.load(items);
|
||||
data.container = id;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
// messages
|
||||
|
||||
// hello -> welcome, then ready -> join. see "Delivery to clients" in MODS.md
|
||||
|
||||
Reference in New Issue
Block a user