Placing schematics

This commit is contained in:
2026-11-15 19:01:40 -03:00
parent 59bfed58bf
commit 6bb2c87373
10 changed files with 464 additions and 50 deletions
+22
View File
@@ -4,6 +4,8 @@
// schematics: blocks and entities from a box of the world, kept to be placed again somewhere else. a .cbsm file is
// one encoded with msgpack. the short keys (i, s, p) are there to keep files small, see STRUCTURES.md
import { decode, encode } from "@std/msgpack";
import * as z from "zod";
import { find_problems, id, json_object, type Problems, vec3 } from "./schema.ts";
export const SCHEMATIC_EXTENSION = ".cbsm";
@@ -62,3 +64,23 @@ export function decode_schematic(bytes: Uint8Array): Schematic {
}
return file.schematic;
}
const int = z.int();
const block_position = z.tuple([int.min(0), int.min(0), int.min(0)], "must be [x, y, z], whole numbers from 0");
const schematic_schema = z.object({
id: z.string().optional(),
size: z.tuple([int.min(1), int.min(1), int.min(1)], "must be [x, y, z], whole numbers from 1"),
palette: z.array(z.object({ id, s: z.record(z.string(), int.min(0)).optional() })),
blocks: z.array(z.object({
i: int.min(0),
p: block_position,
data: z.unknown().optional(),
client_data: json_object.optional(),
})),
entities: z.array(z.object({ p: vec3, id, data: json_object })),
});
// what's wrong with the shape of a schematic, without looking at whether its blocks and entities exist
export function schematic_problems(json: unknown): Problems {
return find_problems(schematic_schema, json);
}