Add various generic models
This commit is contained in:
+66
-1
@@ -39,13 +39,78 @@ export interface ModelElementJson {
|
||||
faces: Partial<Record<ModelFace, ModelFaceJson>>;
|
||||
}
|
||||
|
||||
export interface ModelJson {
|
||||
// a model as a mod writes it in models/. it can start from a parent, like minecraft's: it gets the parent's elements
|
||||
// unless it has its own, and the parent's texture defaults under its own. one with no elements is only there to be a
|
||||
// parent, or draws nothing
|
||||
export interface ModelFileJson {
|
||||
id: string;
|
||||
parent?: string;
|
||||
// defaults for texture variables, can point at other variables ("top": "#side")
|
||||
textures?: Record<string, string>;
|
||||
elements?: ModelElementJson[];
|
||||
}
|
||||
|
||||
// a model with its parents merged in, which is what blocks are drawn with. see resolve_models
|
||||
export interface ModelJson {
|
||||
id: string;
|
||||
textures?: Record<string, string>;
|
||||
elements: ModelElementJson[];
|
||||
}
|
||||
|
||||
// mods' models with their parents merged in. a parent is another of these, or one existing finds (a model of a mod
|
||||
// loaded before, or a built-in one). a model whose parent doesn't exist or whose parents loop keeps only what it has
|
||||
// itself, and gets a problem
|
||||
export function resolve_models(
|
||||
models: ModelFileJson[],
|
||||
existing: (id: string) => ModelJson | undefined,
|
||||
): { models: ModelJson[]; problems: { id: string; problem: string }[] } {
|
||||
const files = new Map(models.map((model) => [model.id, model]));
|
||||
const problems: { id: string; problem: string }[] = [];
|
||||
// a model in a loop is resolved without its parent, so resolving never goes round one
|
||||
const looped = new Set<string>();
|
||||
for (const { id } of models) {
|
||||
const loop = parent_loop(id, (other) => files.get(other)?.parent);
|
||||
if (loop) {
|
||||
looped.add(id);
|
||||
problems.push({ id, problem: `its parents loop: ${loop.join(" -> ")}` });
|
||||
}
|
||||
}
|
||||
const resolved = new Map<string, ModelJson>();
|
||||
const resolve = (id: string): ModelJson | undefined => {
|
||||
const file = files.get(id);
|
||||
if (!file) return existing(id);
|
||||
const done = resolved.get(id);
|
||||
if (done) return done;
|
||||
let parent: ModelJson | undefined;
|
||||
if (file.parent !== undefined && !looped.has(id)) {
|
||||
parent = resolve(file.parent);
|
||||
// a parent of this mod that has a problem of its own still resolves, so this is only for missing ones
|
||||
if (!parent) problems.push({ id, problem: `its parent ${file.parent} doesn't exist` });
|
||||
}
|
||||
const textures = { ...parent?.textures, ...file.textures };
|
||||
const model: ModelJson = {
|
||||
id,
|
||||
...(Object.keys(textures).length > 0 ? { textures } : {}),
|
||||
elements: file.elements ?? parent?.elements ?? [],
|
||||
};
|
||||
resolved.set(id, model);
|
||||
return model;
|
||||
};
|
||||
return { models: models.map(({ id }) => resolve(id)!), problems };
|
||||
}
|
||||
|
||||
// the parents from a model back round to it, when its parents loop. parent_of gives a model's parent, if it has one
|
||||
export function parent_loop(id: string, parent_of: (id: string) => string | undefined): string[] | undefined {
|
||||
const chain = [id];
|
||||
for (let next = parent_of(id); next !== undefined; next = parent_of(next)) {
|
||||
if (next === id) return [...chain, id];
|
||||
// a loop further up, which isn't this model's to report
|
||||
if (chain.includes(next)) return undefined;
|
||||
chain.push(next);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// a model a block uses, and the textures it fills in
|
||||
export interface BlockVariant {
|
||||
model?: string;
|
||||
|
||||
+2
-2
@@ -4,7 +4,7 @@
|
||||
// .bmod files: a built mod in one zip, what servers load from server_mods/ and send to players. see
|
||||
// docs/modding/gettingstarted/modfiles.md. the same code reads them on the server and in the browser, so it only works on bytes
|
||||
import { unzipSync, zipSync } from "fflate";
|
||||
import type { ModelJson } from "./block_models.ts";
|
||||
import type { ModelFileJson } from "./block_models.ts";
|
||||
import { MAX_ANIMATED_TEXTURE_SIZE, MAX_TEXTURE_SIZE } from "./constants.ts";
|
||||
import {
|
||||
type BlockJson,
|
||||
@@ -164,7 +164,7 @@ export function read_bmod(bytes: Uint8Array, file: string): Bmod {
|
||||
|
||||
const data: ModData = {
|
||||
blocks: checked(raw_data.blocks, "blocks", validate_block, problems) as BlockJson[],
|
||||
models: checked(raw_data.models, "models", validate_model, problems) as ModelJson[],
|
||||
models: checked(raw_data.models, "models", validate_model, problems) as ModelFileJson[],
|
||||
items: checked(raw_data.items, "items", validate_item, problems) as ItemJson[],
|
||||
recipes: checked(raw_data.recipes, "recipes", validate_recipe, problems) as RecipeJson[],
|
||||
ores: checked(raw_data.ores, "ores", validate_ore, problems) as OreJson[],
|
||||
|
||||
+13
-4
@@ -18,7 +18,7 @@ import {
|
||||
type TagRegistry,
|
||||
} from "./mod_data.ts";
|
||||
import { PLAYER_PARTS } from "./everything_registry.ts";
|
||||
import { BUILTIN_MODELS, type ModelJson } from "./block_models.ts";
|
||||
import { BUILTIN_MODELS, type ModelFileJson, parent_loop } from "./block_models.ts";
|
||||
import { resolve_tags } from "./tags.ts";
|
||||
import type { TextureJson } from "./texture_animation.ts";
|
||||
import { loot_references, type LootTableJson } from "./loot.ts";
|
||||
@@ -49,7 +49,7 @@ export interface LoadedMod {
|
||||
report: ModReport;
|
||||
manifest?: Record<string, unknown>;
|
||||
blocks: { file: string; json: BlockJson }[];
|
||||
models: { file: string; json: ModelJson }[];
|
||||
models: { file: string; json: ModelFileJson }[];
|
||||
items: { file: string; json: ItemJson }[];
|
||||
recipes: { file: string; json: RecipeJson }[];
|
||||
ores: { file: string; json: OreJson }[];
|
||||
@@ -105,6 +105,7 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
|
||||
const block_owners = new Map<string, string[]>();
|
||||
const item_owners = new Map<string, string[]>();
|
||||
const model_owners = new Map<string, string[]>();
|
||||
const model_parents = new Map<string, string>();
|
||||
const entity_owners = new Map<string, string[]>();
|
||||
const entity_jsons = new Map<string, EntityJson>();
|
||||
const entity_model_owners = new Map<string, string[]>();
|
||||
@@ -136,7 +137,10 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
|
||||
}
|
||||
// one without a texture is the item of the mod's block, which is already counted
|
||||
for (const { json } of mod.items) if (json.texture !== undefined) add(item_owners, json.id, mod.id);
|
||||
for (const { json } of mod.models) add(model_owners, json.id, mod.id);
|
||||
for (const { json } of mod.models) {
|
||||
add(model_owners, json.id, mod.id);
|
||||
if (json.parent !== undefined) model_parents.set(json.id, json.parent);
|
||||
}
|
||||
for (const { json } of mod.entities) {
|
||||
add(entity_owners, json.id, mod.id);
|
||||
entity_jsons.set(json.id, json);
|
||||
@@ -241,7 +245,12 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
|
||||
for (const texture of Object.values(json.textures ?? {})) {
|
||||
if (!texture.startsWith("#")) uses(file, texture, "texture");
|
||||
}
|
||||
for (const element of json.elements) {
|
||||
if (json.parent !== undefined) {
|
||||
uses(file, json.parent, "model");
|
||||
const loop = parent_loop(json.id, (id) => model_parents.get(id));
|
||||
if (loop) errors.push(`${file}: model ${json.id}'s parents loop: ${loop.join(" -> ")}`);
|
||||
}
|
||||
for (const element of json.elements ?? []) {
|
||||
for (const face of Object.values(element.faces)) {
|
||||
if (face && !face.texture.startsWith("#")) uses(file, face.texture, "texture");
|
||||
}
|
||||
|
||||
+9
-1
@@ -775,8 +775,16 @@ function validate_shape(shape: unknown, where: string, top = 16): Problems {
|
||||
export function validate_model(json: unknown): Problems {
|
||||
if (!is_object(json)) return ["model must be an object"];
|
||||
const problems = validate_id(json.id, "id");
|
||||
if (json.parent !== undefined) problems.push(...validate_id(json.parent, "parent"));
|
||||
if (json.textures !== undefined) problems.push(...validate_textures(json.textures, "textures", true));
|
||||
if (!Array.isArray(json.elements) || json.elements.length === 0) {
|
||||
// a model can leave its elements to its parent, or be one that only gives texture defaults to its children
|
||||
if (json.elements === undefined) {
|
||||
if (json.parent === undefined && json.textures === undefined) {
|
||||
problems.push("a model needs elements, a parent, or textures for models that use it as theirs");
|
||||
}
|
||||
return problems;
|
||||
}
|
||||
if (!Array.isArray(json.elements)) {
|
||||
problems.push("elements must be a list of boxes");
|
||||
return problems;
|
||||
}
|
||||
|
||||
+12
-4
@@ -29,13 +29,13 @@ import { type BiomeJson, register_biome } from "./worldgen/biomes.ts";
|
||||
import type { BiomeSourceJson, DensityFunctionJson, NoiseJson, NoiseSettingsJson } from "./worldgen/data.ts";
|
||||
import type { FeatureJson } from "./worldgen/features.ts";
|
||||
import { register_block_item } from "./utils.ts";
|
||||
import type { ModelJson } from "./block_models.ts";
|
||||
import { find_model, type ModelFileJson, type ModelJson, resolve_models } from "./block_models.ts";
|
||||
|
||||
// everything a mod's json files hold, merged into one file by the build (build/mods/<id>/<hash>/data.json)
|
||||
export interface ModData {
|
||||
blocks: BlockJson[];
|
||||
// block models, see common/block_models.ts
|
||||
models: ModelJson[];
|
||||
// block models as mods write them, parents not merged in yet. see common/block_models.ts
|
||||
models: ModelFileJson[];
|
||||
items: ItemJson[];
|
||||
recipes: RecipeJson[];
|
||||
ores: OreJson[];
|
||||
@@ -107,7 +107,15 @@ export function register_mod_data(mods: { id: string; data: ModData }[]): Recipe
|
||||
|
||||
for (const { id, data } of mods) {
|
||||
try {
|
||||
for (const model of data.models) {
|
||||
// parents are this mod's or an earlier one's, which are already registered with theirs merged in
|
||||
const { models, problems } = resolve_models(
|
||||
data.models,
|
||||
(model_id) => find_model(model_id, {}) ?? EverythingRegistry.get<ModelJson>("models", model_id),
|
||||
);
|
||||
if (problems.length > 0) {
|
||||
throw new Error(problems.map(({ id, problem }) => `model ${id}: ${problem}`).join("\n"));
|
||||
}
|
||||
for (const model of models) {
|
||||
EverythingRegistry.register<ModelJson>("models", model.id, model);
|
||||
}
|
||||
for (const json of data.blocks) {
|
||||
|
||||
Reference in New Issue
Block a user