Remove duplicate code
This commit is contained in:
+1
-5
@@ -8,7 +8,7 @@
|
||||
// see docs/modding/resources/loot_tables.md
|
||||
import { EverythingRegistry, type ItemRegistry } from "./everything_registry.ts";
|
||||
import { list_matches, Tags } from "./tags.ts";
|
||||
import { TAG_ID_PATTERN } from "./mod_data.ts";
|
||||
import { is_object, TAG_ID_PATTERN } from "./mod_data.ts";
|
||||
|
||||
// a whole number, a random one from min to max, or the number of n tries at chance p that succeed, like minecraft's
|
||||
// constant, uniform and binomial number providers
|
||||
@@ -450,7 +450,3 @@ export function loot_references(table: LootTableJson) {
|
||||
}
|
||||
return refs;
|
||||
}
|
||||
|
||||
function is_object(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value);
|
||||
}
|
||||
|
||||
@@ -148,6 +148,17 @@ export interface Graphics {
|
||||
measure_text(text: string, scale?: number): number;
|
||||
// part is the part of the texture to draw, [u, v, width, height] from 0 to 1, like a progress bar's filled part
|
||||
texture(id: Id, x: number, y: number, w: number, h: number, part?: [number, number, number, number]): void;
|
||||
// a progress bar: filled (0 to 1) of full drawn over empty, from the left, or from the bottom going "up"
|
||||
progress(
|
||||
empty: Id,
|
||||
full: Id,
|
||||
x: number,
|
||||
y: number,
|
||||
w: number,
|
||||
h: number,
|
||||
filled: number,
|
||||
direction?: "right" | "up",
|
||||
): void;
|
||||
item(id: Id, x: number, y: number, count?: number): void;
|
||||
clip(x: number, y: number, w: number, h: number, draw: () => void): void;
|
||||
button(label: string, x: number, y: number, w: number, h: number): boolean;
|
||||
|
||||
@@ -36,6 +36,7 @@ import {
|
||||
type WorldgenRefs,
|
||||
} from "./worldgen/data.ts";
|
||||
import { feature_refs, type FeatureJson } from "./worldgen/features.ts";
|
||||
import type { ModData } from "./mod_loader.ts";
|
||||
|
||||
export interface ModReport {
|
||||
id: string;
|
||||
@@ -73,6 +74,29 @@ export interface LoadedMod {
|
||||
texture_json: Map<string, TextureJson>;
|
||||
}
|
||||
|
||||
// what a mod's data files hold, as packed into its .bmod
|
||||
export function mod_data(mod: LoadedMod): ModData {
|
||||
return {
|
||||
blocks: mod.blocks.map((b) => b.json),
|
||||
models: mod.models.map((m) => m.json),
|
||||
items: mod.items.map((i) => i.json),
|
||||
recipes: mod.recipes.map((r) => r.json),
|
||||
ores: mod.ores.map((o) => o.json),
|
||||
spawns: mod.spawns.map((s) => s.json),
|
||||
entities: mod.entities.map((e) => e.json),
|
||||
entity_models: mod.entity_models.map((m) => m.json),
|
||||
effects: mod.effects.map((e) => e.json),
|
||||
tags: mod.tags.map((t) => t.json),
|
||||
loot_tables: mod.loot_tables.map((t) => t.json),
|
||||
biomes: mod.biomes.map((b) => b.json),
|
||||
noises: mod.noises.map((n) => n.json),
|
||||
density_functions: mod.density_functions.map((d) => d.json),
|
||||
noise_settings: mod.noise_settings.map((n) => n.json),
|
||||
biome_sources: mod.biome_sources.map((b) => b.json),
|
||||
features: mod.features.map((f) => f.json),
|
||||
};
|
||||
}
|
||||
|
||||
// dependencies before the mods that need them, otherwise alphabetical
|
||||
export function load_order(mods: LoadedMod[]): LoadedMod[] {
|
||||
const by_id = new Map(mods.map((mod) => [mod.id, mod]));
|
||||
|
||||
+2
-1
@@ -1236,6 +1236,7 @@ function validate_stack(value: unknown, field: string): Problems {
|
||||
return problems;
|
||||
}
|
||||
|
||||
function is_object(value: unknown): value is Record<string, unknown> {
|
||||
// a json object, not null or an array
|
||||
export function is_object(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
|
||||
// animated textures, like minecraft's .png.mcmeta and in the same format: textures/<name>.json next to
|
||||
// textures/<name>.png says how to cut the png into frames and how long each one shows. see docs/modding/resources/textures.md
|
||||
import { is_object } from "./mod_data.ts";
|
||||
|
||||
export interface TextureJson {
|
||||
animation?: AnimationJson;
|
||||
}
|
||||
@@ -101,7 +103,3 @@ function frame_size(animation: AnimationJson, size: { width: number; height: num
|
||||
const side = Math.min(size.width, size.height);
|
||||
return { frame_width: animation.width ?? side, frame_height: animation.height ?? side };
|
||||
}
|
||||
|
||||
function is_object(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value);
|
||||
}
|
||||
|
||||
@@ -38,6 +38,20 @@ export function distance_point_rectangle(px: number, py: number, sqx: number, sq
|
||||
return Math.sqrt(dx * dx + dy * dy);
|
||||
}
|
||||
|
||||
// -pi to pi
|
||||
export function wrap_angle(angle: number) {
|
||||
angle %= Math.PI * 2;
|
||||
if (angle >= Math.PI) angle -= Math.PI * 2;
|
||||
if (angle < -Math.PI) angle += Math.PI * 2;
|
||||
return angle;
|
||||
}
|
||||
|
||||
// the hex sha-256 of bytes
|
||||
export async function sha256(bytes: Uint8Array<ArrayBuffer>): Promise<string> {
|
||||
const digest = new Uint8Array(await crypto.subtle.digest("SHA-256", bytes));
|
||||
return Array.from(digest, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
||||
}
|
||||
|
||||
export function distance_point_point(ax: number, ay: number, az: number, bx: number, by: number, bz: number) {
|
||||
const dx = ax - bx;
|
||||
const dy = ay - by;
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
// biomes as data: worldgen/biomes/<name>.json in a mod, see docs/modding/worldgen/biomes.md. which biome a column
|
||||
// gets is the generator's biome rules (worldgen/biome_source), these say what a biome looks like once it's picked
|
||||
import { EverythingRegistry } from "../everything_registry.ts";
|
||||
import { ID_PATTERN } from "../mod_data.ts";
|
||||
import { ID_PATTERN, is_object } from "../mod_data.ts";
|
||||
import { farming_day } from "../time.ts";
|
||||
|
||||
export const SEASONS = ["spring", "summer", "autumn", "winter"] as const;
|
||||
@@ -164,7 +164,3 @@ export function validate_biome(json: unknown): string[] {
|
||||
}
|
||||
return problems;
|
||||
}
|
||||
|
||||
function is_object(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value);
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
// world generation as data: noises, density functions, noise settings (a generator) and biome sources, see
|
||||
// docs/modding/worldgen/terrain.md. checked here, compiled by density.ts and generator.ts. the names follow
|
||||
// minecraft's (density functions, the noise router, surface rules) where the idea is the same
|
||||
import { ID_PATTERN, TAG_ID_PATTERN } from "../mod_data.ts";
|
||||
import { ID_PATTERN, is_object, TAG_ID_PATTERN } from "../mod_data.ts";
|
||||
|
||||
// ids of worldgen data can have folders, like "bworld:overworld/erosion"
|
||||
export const WORLDGEN_ID_PATTERN = TAG_ID_PATTERN;
|
||||
@@ -540,7 +540,3 @@ function block_id(value: unknown, field: string, refs: WorldgenRefs, problems: s
|
||||
if (typeof value === "string" && ID_PATTERN.test(value)) refs.blocks.add(value);
|
||||
else problems.push(`${field} must be a block's id`);
|
||||
}
|
||||
|
||||
function is_object(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value);
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
import { EverythingRegistry } from "../everything_registry.ts";
|
||||
import type { BlockRegistry } from "../everything_registry.ts";
|
||||
import { CHUNK_HEIGHT, CHUNK_SIZE } from "../constants.ts";
|
||||
import { ID_PATTERN, TAG_ID_PATTERN } from "../mod_data.ts";
|
||||
import { ID_PATTERN, is_object, TAG_ID_PATTERN } from "../mod_data.ts";
|
||||
import { list_matches } from "../tags.ts";
|
||||
import { can_stand_on } from "../utils.ts";
|
||||
import { AIR_ID } from "../protocol.ts";
|
||||
@@ -598,7 +598,3 @@ function height_provider(json: unknown): IntProvider {
|
||||
const span = anchor(object.max_inclusive) - min + 1;
|
||||
return (chunk) => min + Math.floor(chunk.rng.next() * span);
|
||||
}
|
||||
|
||||
function is_object(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user