Add various generic models

This commit is contained in:
2026-10-12 17:56:47 -03:00
parent e5baeab193
commit d4eff11f3e
34 changed files with 786 additions and 251 deletions
+66 -1
View File
@@ -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;