Improve a buuuunch of textures and some reworks

This commit is contained in:
2026-09-27 18:05:20 -03:00
parent 9f47c709f1
commit b7ded817d9
186 changed files with 1486 additions and 124 deletions
+47 -17
View File
@@ -1,5 +1,6 @@
// the json formats from MODS.md, and converting them to and from the engine's registry entries.
// the mod loader uses the from_json direction, tests use both to check nothing is lost
import { is_tint_color, type TintColor } from "./worldgen/biome_colors.ts";
import {
type BlockRegistry,
type BlockStateDefinition,
@@ -38,6 +39,7 @@ export interface BlockJson {
variants?: Record<string, BlockVariant>;
render_layer?: RenderLayer;
cull_same?: boolean;
tint?: { color: TintColor; textures?: string[] };
light_emission?: number;
light_opacity?: number;
// replaced by render_layer, true means translucent
@@ -76,15 +78,22 @@ export interface ItemJson {
components?: Record<string, unknown>;
}
// an item a recipe takes: its id, or a list of ids any of which fits, like every kind of planks
export type ItemChoice = string | string[];
export function choice_ids(choice: ItemChoice): string[] {
return typeof choice === "string" ? [choice] : choice;
}
export type RecipeJson =
| {
type: "shaped";
pattern: string[];
key: Record<string, string>;
key: Record<string, ItemChoice>;
result: { id: string; count: number };
}
| { type: "furnace"; input: string; output: { id: string; count: number }; cook_time: number }
| { type: "fuel"; item: string; burn_time: number }
| { type: "furnace"; input: ItemChoice; output: { id: string; count: number }; cook_time: number }
| { type: "fuel"; item: ItemChoice; burn_time: number }
// upgrades a tool at a smithing table, with some of a material and maybe one more item
| {
type: "smithing";
@@ -111,7 +120,7 @@ export function is_mod_recipe(recipe: RecipeJson): recipe is ModRecipeJson {
export interface GridRecipe {
width: number;
height: number;
pattern: (string | undefined)[];
pattern: (ItemChoice | undefined)[];
result: { id: string; count: number };
}
@@ -124,6 +133,7 @@ export function block_to_json(block: BlockRegistry, has_item: boolean): BlockJso
if (block.variants !== undefined) json.variants = block.variants;
if (block.render_layer && block.render_layer !== "solid") json.render_layer = block.render_layer;
if (block.cull_same !== undefined) json.cull_same = block.cull_same;
if (block.tint !== undefined) json.tint = block.tint;
if (block.light_emission !== undefined) json.light_emission = block.light_emission;
if (block.light_opacity !== undefined) json.light_opacity = block.light_opacity;
if (block.alpha !== undefined) json.alpha = block.alpha;
@@ -156,6 +166,7 @@ export function block_from_json(json: BlockJson): { block: BlockRegistry; has_it
const render_layer = json.render_layer ?? (json.transparent ? "translucent" : undefined);
if (render_layer && render_layer !== "solid") block.render_layer = render_layer;
if (json.cull_same !== undefined) block.cull_same = json.cull_same;
if (json.tint !== undefined) block.tint = json.tint;
if (json.light_emission !== undefined) block.light_emission = json.light_emission;
if (json.light_opacity !== undefined) block.light_opacity = json.light_opacity;
if (json.alpha !== undefined) block.alpha = json.alpha;
@@ -206,21 +217,22 @@ export function item_from_json(json: ItemJson): ItemRegistry {
// shaped recipes <-> the grid format
export function grid_recipe_to_json(recipe: GridRecipe): RecipeJson {
const key: Record<string, string> = {};
const key: Record<string, ItemChoice> = {};
// by the choice as json, so equal lists share a letter
const letters = new Map<string, string>();
for (const id of recipe.pattern) {
if (id === undefined || letters.has(id)) continue;
const letter = pick_letter(id, new Set(letters.values()));
letters.set(id, letter);
key[letter] = id;
for (const choice of recipe.pattern) {
if (choice === undefined || letters.has(JSON.stringify(choice))) continue;
const letter = pick_letter(choice_ids(choice)[0], new Set(letters.values()));
letters.set(JSON.stringify(choice), letter);
key[letter] = choice;
}
const pattern: string[] = [];
for (let y = 0; y < recipe.height; y++) {
let row = "";
for (let x = 0; x < recipe.width; x++) {
const id = recipe.pattern[y * recipe.width + x];
row += id === undefined ? " " : letters.get(id);
const choice = recipe.pattern[y * recipe.width + x];
row += choice === undefined ? " " : letters.get(JSON.stringify(choice));
}
pattern.push(row);
}
@@ -231,7 +243,7 @@ export function grid_recipe_to_json(recipe: GridRecipe): RecipeJson {
export function grid_recipe_from_json(json: Extract<RecipeJson, { type: "shaped" }>): GridRecipe {
const height = json.pattern.length;
const width = Math.max(...json.pattern.map((row) => row.length));
const pattern: (string | undefined)[] = [];
const pattern: (ItemChoice | undefined)[] = [];
for (const row of json.pattern) {
for (let x = 0; x < width; x++) {
const letter = row[x] ?? " ";
@@ -351,6 +363,17 @@ export function validate_block(json: unknown): Problems {
for (const key of ["cull_same", "transparent", "collision", "item", "interactive", "replaceable"]) {
if (json[key] !== undefined && typeof json[key] !== "boolean") problems.push(`${key} must be true or false`);
}
if (json.tint !== undefined) {
const tint = json.tint;
if (!is_object(tint) || !is_tint_color(tint.color)) {
problems.push('tint.color must be "grass", "foliage" or a color like "#80a755"');
} else if (
tint.textures !== undefined &&
!(Array.isArray(tint.textures) && tint.textures.every((name) => typeof name === "string"))
) {
problems.push('tint.textures must be a list of texture variables, like ["top"]');
}
}
if (json.alpha !== undefined && (typeof json.alpha !== "number" || json.alpha < 0 || json.alpha > 1)) {
problems.push("alpha must be between 0 and 1");
}
@@ -493,21 +516,21 @@ export function validate_recipe(json: unknown): Problems {
}
}
if (!is_object(json.key)) {
problems.push("key must map letters to item ids");
problems.push("key must map letters to item ids, or lists of them");
} else {
for (const id of Object.values(json.key)) problems.push(...validate_id(id, "key"));
for (const choice of Object.values(json.key)) problems.push(...validate_choice(choice, "key"));
}
problems.push(...validate_stack(json.result, "result"));
break;
}
case "furnace":
problems.push(...validate_id(json.input, "input"), ...validate_stack(json.output, "output"));
problems.push(...validate_choice(json.input, "input"), ...validate_stack(json.output, "output"));
if (!Number.isInteger(json.cook_time) || (json.cook_time as number) < 1) {
problems.push("cook_time must be a positive number of ticks");
}
break;
case "fuel":
problems.push(...validate_id(json.item, "item"));
problems.push(...validate_choice(json.item, "item"));
if (!Number.isInteger(json.burn_time) || (json.burn_time as number) < 1) {
problems.push("burn_time must be a positive number of ticks");
}
@@ -544,6 +567,13 @@ export function validate_ore(json: unknown): Problems {
return problems;
}
// an item id, or a list of them any of which fits
function validate_choice(value: unknown, field: string): Problems {
if (!Array.isArray(value)) return validate_id(value, field);
if (value.length === 0) return [`${field} must list at least one item`];
return value.flatMap((id) => validate_id(id, field));
}
// optional text players see
function validate_text(value: unknown, field: string): Problems {
return value === undefined || (typeof value === "string" && value.length > 0) ? [] : [`${field} must be text`];