Add natural mob spawning for rotwalker
This commit is contained in:
@@ -12,6 +12,7 @@ import {
|
||||
type ManifestJson,
|
||||
type OreJson,
|
||||
type RecipeJson,
|
||||
type SpawnJson,
|
||||
type TagJson,
|
||||
validate_block,
|
||||
validate_entity,
|
||||
@@ -21,6 +22,7 @@ import {
|
||||
validate_model,
|
||||
validate_ore,
|
||||
validate_recipe,
|
||||
validate_spawn,
|
||||
validate_tag,
|
||||
} from "./mod_data.ts";
|
||||
import type { ModData } from "./mod_loader.ts";
|
||||
@@ -139,6 +141,7 @@ export function read_bmod(bytes: Uint8Array, file: string): Bmod {
|
||||
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[],
|
||||
spawns: checked(raw_data.spawns, "spawns", validate_spawn, problems) as SpawnJson[],
|
||||
entities: checked(raw_data.entities, "entities", validate_entity, problems) as EntityJson[],
|
||||
entity_models: checked(
|
||||
raw_data.entity_models,
|
||||
|
||||
+20
-1
@@ -9,11 +9,13 @@ import {
|
||||
type OreJson,
|
||||
type RecipeJson,
|
||||
SHARED_TAG_NAMESPACE,
|
||||
type SpawnJson,
|
||||
type TagJson,
|
||||
type TagRegistry,
|
||||
} from "./mod_data.ts";
|
||||
import { BUILTIN_MODELS, type ModelJson } from "./block_models.ts";
|
||||
import { resolve_tags } from "./tags.ts";
|
||||
import { BIOMES } from "./worldgen/overworld.ts";
|
||||
|
||||
export interface ModReport {
|
||||
id: string;
|
||||
@@ -31,6 +33,7 @@ export interface LoadedMod {
|
||||
items: { file: string; json: ItemJson }[];
|
||||
recipes: { file: string; json: RecipeJson }[];
|
||||
ores: { file: string; json: OreJson }[];
|
||||
spawns: { file: string; json: SpawnJson }[];
|
||||
entities: { file: string; json: EntityJson }[];
|
||||
entity_models: { file: string; json: EntityModelJson }[];
|
||||
tags: { file: string; json: TagJson }[];
|
||||
@@ -72,6 +75,7 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
|
||||
const item_owners = new Map<string, string[]>();
|
||||
const model_owners = 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[]>();
|
||||
const tag_owners = new Map<TagRegistry, Set<string>>();
|
||||
const add = (map: Map<string, string[]>, id: string, mod: string) => map.set(id, [...(map.get(id) ?? []), mod]);
|
||||
@@ -85,7 +89,10 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
|
||||
}
|
||||
for (const { json } of mod.items) 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.entities) add(entity_owners, json.id, mod.id);
|
||||
for (const { json } of mod.entities) {
|
||||
add(entity_owners, json.id, mod.id);
|
||||
entity_jsons.set(json.id, json);
|
||||
}
|
||||
for (const { json } of mod.entity_models) add(entity_model_owners, json.id, mod.id);
|
||||
for (const { json } of mod.tags) {
|
||||
if (!tag_owners.has(json.registry)) tag_owners.set(json.registry, new Set());
|
||||
@@ -203,6 +210,18 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
|
||||
if (json.model !== undefined) uses(file, json.model, "entity model");
|
||||
uses(file, json.texture, "texture");
|
||||
}
|
||||
for (const { file, json } of mod.spawns) {
|
||||
check_namespace(file, json.entity);
|
||||
const entity = entity_jsons.get(json.entity);
|
||||
if (!entity) errors.push(`${file}: entity ${json.entity} doesn't exist`);
|
||||
else if (entity.behavior !== "monster") {
|
||||
errors.push(`${file}: ${json.entity} isn't a monster, only monsters spawn on their own for now`);
|
||||
}
|
||||
// biomes are the engine's, whatever their namespace
|
||||
for (const biome of json.biomes ?? []) {
|
||||
if (!(biome in BIOMES)) errors.push(`${file}: biome ${biome} doesn't exist`);
|
||||
}
|
||||
}
|
||||
for (const { file, json } of mod.entity_models) {
|
||||
if ((entity_model_owners.get(json.id)?.length ?? 0) > 1) {
|
||||
errors.push(`${file}: entity model ${json.id} is defined more than once`);
|
||||
|
||||
@@ -75,6 +75,22 @@ export interface OreJson {
|
||||
threshold: number;
|
||||
}
|
||||
|
||||
// mobs that spawn on their own, like neoforge's add_spawns biome modifier and undergarden's biome spawners. only
|
||||
// monsters for now: in the dark near players, like minecraft's NaturalSpawner
|
||||
export interface SpawnJson {
|
||||
entity: string;
|
||||
// how often it's picked over the other spawns where both can spawn
|
||||
weight: number;
|
||||
// how many spawn together
|
||||
min_count: number;
|
||||
max_count: number;
|
||||
// only in these biomes, every biome when not set
|
||||
biomes?: string[];
|
||||
// the chance each spawn that passed every other check goes ahead, like undergarden's 1 in 10 for its rotspawn. 1
|
||||
// when not set
|
||||
chance?: number;
|
||||
}
|
||||
|
||||
export interface ItemJson {
|
||||
id: string;
|
||||
texture: string;
|
||||
@@ -701,6 +717,31 @@ export function validate_ore(json: unknown): Problems {
|
||||
return problems;
|
||||
}
|
||||
|
||||
export function validate_spawn(json: unknown): Problems {
|
||||
if (!is_object(json)) return ["spawn must be an object"];
|
||||
const problems = validate_id(json.entity, "entity");
|
||||
for (const key of ["weight", "min_count", "max_count"]) {
|
||||
if (!Number.isInteger(json[key]) || (json[key] as number) < (key === "weight" ? 0 : 1)) {
|
||||
problems.push(
|
||||
key === "weight"
|
||||
? "weight must be a whole number, 0 or more"
|
||||
: `${key} must be a whole number, 1 or more`,
|
||||
);
|
||||
}
|
||||
}
|
||||
if (typeof json.min_count === "number" && typeof json.max_count === "number" && json.min_count > json.max_count) {
|
||||
problems.push("min_count must not be above max_count");
|
||||
}
|
||||
if (json.biomes !== undefined) {
|
||||
if (!Array.isArray(json.biomes)) problems.push("biomes must be a list of biome ids");
|
||||
else json.biomes.forEach((biome, i) => problems.push(...validate_id(biome, `biomes[${i}]`)));
|
||||
}
|
||||
if (json.chance !== undefined && (typeof json.chance !== "number" || !(json.chance > 0 && json.chance <= 1))) {
|
||||
problems.push("chance must be a number above 0, at most 1");
|
||||
}
|
||||
return problems;
|
||||
}
|
||||
|
||||
export function validate_entity(json: unknown): Problems {
|
||||
if (!is_object(json)) return ["entity must be an object"];
|
||||
const problems = [
|
||||
|
||||
@@ -14,6 +14,7 @@ import {
|
||||
ModRecipeJson,
|
||||
OreJson,
|
||||
RecipeJson,
|
||||
SpawnJson,
|
||||
TagJson,
|
||||
TagRegistry,
|
||||
} from "./mod_data.ts";
|
||||
@@ -29,6 +30,8 @@ export interface ModData {
|
||||
items: ItemJson[];
|
||||
recipes: RecipeJson[];
|
||||
ores: OreJson[];
|
||||
// natural spawns, see SpawnJson
|
||||
spawns: SpawnJson[];
|
||||
entities: EntityJson[];
|
||||
entity_models: EntityModelJson[];
|
||||
// tag files, see common/tags.ts
|
||||
@@ -57,6 +60,7 @@ export class RecipeBook {
|
||||
// every recipe as its json, in load order, for recipe viewers
|
||||
all: RecipeJson[] = [];
|
||||
ores: OreJson[] = [];
|
||||
spawns: SpawnJson[] = [];
|
||||
}
|
||||
|
||||
export class ModLoadError extends Error {
|
||||
@@ -150,6 +154,7 @@ export function register_mod_data(mods: { id: string; data: ModData }[]): Recipe
|
||||
}
|
||||
}
|
||||
recipes.ores.push(...data.ores);
|
||||
recipes.spawns.push(...data.spawns);
|
||||
}
|
||||
|
||||
return recipes;
|
||||
|
||||
Reference in New Issue
Block a user