Start working on entities
This commit is contained in:
+7
-2
@@ -7,6 +7,7 @@ import {
|
|||||||
type BlockJson,
|
type BlockJson,
|
||||||
choice_list,
|
choice_list,
|
||||||
type EffectJson,
|
type EffectJson,
|
||||||
|
entity_category,
|
||||||
type EntityJson,
|
type EntityJson,
|
||||||
type EntityModelJson,
|
type EntityModelJson,
|
||||||
type FluidJson,
|
type FluidJson,
|
||||||
@@ -380,6 +381,8 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
|
|||||||
if (json.model !== undefined) uses(file, json.model, "entity model");
|
if (json.model !== undefined) uses(file, json.model, "entity model");
|
||||||
uses(file, json.texture, "texture");
|
uses(file, json.texture, "texture");
|
||||||
if (json.loot_table) uses(file, json.loot_table, "loot table");
|
if (json.loot_table) uses(file, json.loot_table, "loot table");
|
||||||
|
// scripts register behaviors, the server checks them when it starts
|
||||||
|
if (json.behavior?.includes(":")) check_namespace(file, json.behavior);
|
||||||
}
|
}
|
||||||
for (const { file, json } of mod.loot_tables ?? []) {
|
for (const { file, json } of mod.loot_tables ?? []) {
|
||||||
const refs = loot_references(json);
|
const refs = loot_references(json);
|
||||||
@@ -527,8 +530,10 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
|
|||||||
check_namespace(file, json.entity);
|
check_namespace(file, json.entity);
|
||||||
const entity = entity_jsons.get(json.entity);
|
const entity = entity_jsons.get(json.entity);
|
||||||
if (!entity) errors.push(`${file}: entity ${json.entity} doesn't exist`);
|
if (!entity) errors.push(`${file}: entity ${json.entity} doesn't exist`);
|
||||||
else if (entity.behavior !== "monster") {
|
else if (entity_category(entity) !== "monster") {
|
||||||
errors.push(`${file}: ${json.entity} isn't a monster, only monsters spawn on their own for now`);
|
errors.push(
|
||||||
|
`${file}: ${json.entity} isn't in the monster category, only monsters spawn on their own for now`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
for (const biome of json.biomes ?? []) {
|
for (const biome of json.biomes ?? []) {
|
||||||
check_namespace(file, biome);
|
check_namespace(file, biome);
|
||||||
|
|||||||
+14
-2
@@ -430,8 +430,14 @@ const entity_schema = z.object({
|
|||||||
texture: id,
|
texture: id,
|
||||||
// "animal" (the default) wanders around, panics when hurt and looks at players. "monster" goes after players it
|
// "animal" (the default) wanders around, panics when hurt and looks at players. "monster" goes after players it
|
||||||
// sees and whoever hurts it, and despawns far from players, like minecraft's zombies. "scripted" stays where
|
// sees and whoever hurts it, and despawns far from players, like minecraft's zombies. "scripted" stays where
|
||||||
// scripts put it: no gravity, ai or health, and it isn't saved with the world, like the fishing hook
|
// scripts put it: no gravity, ai or health, and it isn't saved with the world, like the fishing hook. any other id
|
||||||
behavior: z.enum(["animal", "monster", "scripted"]).optional(),
|
// is a behavior a mod's script registers
|
||||||
|
behavior: z.custom<string>(
|
||||||
|
(value) => ["animal", "monster", "scripted"].includes(value as string) || id.safeParse(value).success,
|
||||||
|
'must be "animal", "monster", "scripted" or a behavior id like "my_mod:thing"',
|
||||||
|
).optional(),
|
||||||
|
// what natural spawning counts it as, see entity_category
|
||||||
|
category: z.enum(["monster", "creature"]).optional(),
|
||||||
// its collision box in blocks, width is for both x and z
|
// its collision box in blocks, width is for both x and z
|
||||||
width: z.number().positive().max(16),
|
width: z.number().positive().max(16),
|
||||||
height: z.number().positive().max(16),
|
height: z.number().positive().max(16),
|
||||||
@@ -449,6 +455,12 @@ const entity_schema = z.object({
|
|||||||
|
|
||||||
export type EntityJson = z.infer<typeof entity_schema>;
|
export type EntityJson = z.infer<typeof entity_schema>;
|
||||||
|
|
||||||
|
// like minecraft's MobCategory: monsters can spawn on their own and go away when no player is near, creatures stay.
|
||||||
|
// monster behavior makes one a monster unless it says otherwise
|
||||||
|
export function entity_category(json: EntityJson): "monster" | "creature" {
|
||||||
|
return json.category ?? (json.behavior === "monster" ? "monster" : "creature");
|
||||||
|
}
|
||||||
|
|
||||||
// like minecraft's MobEffectCategory, "neutral" when not set
|
// like minecraft's MobEffectCategory, "neutral" when not set
|
||||||
const effect_category = z.enum(["beneficial", "harmful", "neutral"]);
|
const effect_category = z.enum(["beneficial", "harmful", "neutral"]);
|
||||||
export type EffectCategory = z.infer<typeof effect_category>;
|
export type EffectCategory = z.infer<typeof effect_category>;
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ and players. Mods define new kinds in their `entities/` folder, and draw them wi
|
|||||||
| `texture` | required | Any texture, stretched over the model's `texture_width` and `texture_height`. |
|
| `texture` | required | Any texture, stretched over the model's `texture_width` and `texture_height`. |
|
||||||
| `model` | none | An [entity model](#entity-models). Without one it's drawn as its texture, facing the camera. |
|
| `model` | none | An [entity model](#entity-models). Without one it's drawn as its texture, facing the camera. |
|
||||||
| `behavior` | `"animal"` | `"animal"`, `"monster"` or `"scripted"`, see below. |
|
| `behavior` | `"animal"` | `"animal"`, `"monster"` or `"scripted"`, see below. |
|
||||||
|
| `category` | see below | `"monster"` or `"creature"`, see [Natural Spawning](#natural-spawning). |
|
||||||
| `width`, `height` | required | Its collision box in blocks, up to 16. `width` is for both x and z. Its eyes are at 85% of the height. |
|
| `width`, `height` | required | Its collision box in blocks, up to 16. `width` is for both x and z. Its eyes are at 85% of the height. |
|
||||||
| `max_health` | `10` | In half hearts. |
|
| `max_health` | `10` | In half hearts. |
|
||||||
| `movement_speed` | `0.25` | Minecraft's per tick speed, 0.25 for most animals. |
|
| `movement_speed` | `0.25` | Minecraft's per tick speed, 0.25 for most animals. |
|
||||||
@@ -89,6 +90,10 @@ the world, so a script never finds one it doesn't know about after a restart. Th
|
|||||||
|
|
||||||
## Natural Spawning
|
## Natural Spawning
|
||||||
|
|
||||||
|
An entity's `category` is what natural spawning counts it as, like Minecraft's mob categories. Monsters can spawn on
|
||||||
|
their own and go away when no player is near, creatures stay. Without one, a `"monster"` is a monster and anything else
|
||||||
|
is a creature.
|
||||||
|
|
||||||
Monsters spawn on their own in the dark around players, the way Minecraft's do. A mod lists which ones, how often and in
|
Monsters spawn on their own in the dark around players, the way Minecraft's do. A mod lists which ones, how often and in
|
||||||
which biomes in `worldgen/spawns.json`, like NeoForge's `add_spawns` biome modifier. The Undergarden's rotwalkers:
|
which biomes in `worldgen/spawns.json`, like NeoForge's `add_spawns` biome modifier. The Undergarden's rotwalkers:
|
||||||
|
|
||||||
@@ -109,7 +114,7 @@ which biomes in `worldgen/spawns.json`, like NeoForge's `add_spawns` biome modif
|
|||||||
|
|
||||||
| Field | Default | Meaning |
|
| Field | Default | Meaning |
|
||||||
| ----------- | ----------- | --------------------------------------------------------------------------------------------------------------- |
|
| ----------- | ----------- | --------------------------------------------------------------------------------------------------------------- |
|
||||||
| `entity` | required | A `"monster"` entity. Only monsters spawn on their own for now. |
|
| `entity` | required | An entity in the `"monster"` category. Only monsters spawn on their own for now. |
|
||||||
| `weight` | required | How often this one is picked over the others that can spawn in the same biome. |
|
| `weight` | required | How often this one is picked over the others that can spawn in the same biome. |
|
||||||
| `min_count` | required | How many spawn together, at least. |
|
| `min_count` | required | How many spawn together, at least. |
|
||||||
| `max_count` | required | And at most. |
|
| `max_count` | required | And at most. |
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"format_version": 1,
|
||||||
|
"entity": {
|
||||||
|
"id": "dangerous_business:coilhead",
|
||||||
|
"model": "dangerous_business:coilhead",
|
||||||
|
"texture": "dangerous_business:coilhead",
|
||||||
|
"behavior": "dangerous_business:coilhead",
|
||||||
|
"width": 0.8,
|
||||||
|
"height": 2.5,
|
||||||
|
"max_health": 40,
|
||||||
|
"movement_speed": 0.23,
|
||||||
|
"attack_damage": 5,
|
||||||
|
"physical_shielding": 3
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
{
|
||||||
|
"format_version": "1.12.0",
|
||||||
|
"minecraft:geometry": [
|
||||||
|
{
|
||||||
|
"description": {
|
||||||
|
"identifier": "geometry.unknown",
|
||||||
|
"texture_width": 128,
|
||||||
|
"texture_height": 128,
|
||||||
|
"visible_bounds_width": 3,
|
||||||
|
"visible_bounds_height": 4.5,
|
||||||
|
"visible_bounds_offset": [0, 1.75, 0]
|
||||||
|
},
|
||||||
|
"bones": [
|
||||||
|
{
|
||||||
|
"name": "root",
|
||||||
|
"pivot": [0, 0, 0]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Torso",
|
||||||
|
"parent": "root",
|
||||||
|
"pivot": [0.5, 17, 0],
|
||||||
|
"cubes": [
|
||||||
|
{"origin": [-4.5, 17, -3.5], "size": [9, 14, 5], "uv": [0, 0]},
|
||||||
|
{"origin": [5.5, 20, -5.499], "size": [0, 1, 1], "pivot": [4.5, 17.5, -4.499], "rotation": [0, -90, 0], "uv": [1, 1]},
|
||||||
|
{"origin": [10.5, 23, -5.499], "size": [0, 1, 1], "pivot": [4.5, 17.5, -4.499], "rotation": [0, -90, 0], "uv": [1, 1]},
|
||||||
|
{"origin": [-1.5, 20.05952, -5.5], "size": [0, 1, 2], "uv": [0, 0]},
|
||||||
|
{"origin": [-0.5, 26.05952, -5.5], "size": [0, 1, 2], "uv": [0, 0]},
|
||||||
|
{"origin": [-0.5, 24.05952, 1.5], "size": [0, 1, 2], "uv": [0, 0]},
|
||||||
|
{"origin": [-2.5, 19.05952, 1.5], "size": [0, 1, 2], "uv": [0, 0]}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "RightArm",
|
||||||
|
"parent": "Torso",
|
||||||
|
"pivot": [-5, 28, -1.5],
|
||||||
|
"cubes": [
|
||||||
|
{"origin": [-8.5, 20, -3], "size": [4, 10, 4], "uv": [0, 34]},
|
||||||
|
{"origin": [-5.5, 28.05952, -5], "size": [0, 1, 2], "uv": [0, 0]},
|
||||||
|
{"origin": [-5.5, 28.05952, 1], "size": [0, 1, 2], "uv": [0, 0]}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "LeftArm",
|
||||||
|
"parent": "Torso",
|
||||||
|
"pivot": [5, 28.66032, -0.66633],
|
||||||
|
"cubes": [
|
||||||
|
{"origin": [4.5, 22, -3], "size": [4, 8, 4], "uv": [40, 38]},
|
||||||
|
{"origin": [6.5, 27.05952, -4.5], "size": [0, 1, 2], "uv": [0, 0]},
|
||||||
|
{"origin": [8.5, 27, -10.499], "size": [0, 1, 2], "pivot": [4.5, 17.5, -4.499], "rotation": [0, -90, 0], "uv": [0, 0]}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Neck",
|
||||||
|
"parent": "Torso",
|
||||||
|
"pivot": [-1.5, 31, 0],
|
||||||
|
"cubes": [
|
||||||
|
{"origin": [-2, 30.75, -3], "size": [4, 9, 4], "uv": [16, 38]}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Head",
|
||||||
|
"parent": "Neck",
|
||||||
|
"pivot": [0, 39.75, -1],
|
||||||
|
"cubes": [
|
||||||
|
{"origin": [-3.5, 39.75, -4.5], "size": [7, 8, 7], "uv": [0, 19]}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "RightLeg",
|
||||||
|
"parent": "root",
|
||||||
|
"pivot": [-1, 16, -0.833],
|
||||||
|
"cubes": [
|
||||||
|
{"origin": [-4, 0, -3], "size": [4, 17, 4], "uv": [28, 21]},
|
||||||
|
{"origin": [-1.5, 10.05952, -5], "size": [0, 1, 2], "uv": [0, 0]},
|
||||||
|
{"origin": [8.5, 10, 4], "size": [0, 1, 2], "pivot": [4.5, 17.5, -4.499], "rotation": [0, -90, 0], "uv": [0, 0]}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "LeftLeg",
|
||||||
|
"parent": "root",
|
||||||
|
"pivot": [0.167, 16, -0.833],
|
||||||
|
"cubes": [
|
||||||
|
{"origin": [0, 0, -3], "size": [4, 17, 4], "uv": [28, 0]},
|
||||||
|
{"origin": [2.5, 8.05952, -5], "size": [0, 1, 2], "uv": [0, 0]},
|
||||||
|
{"origin": [8.5, 12, -6], "size": [0, 1, 2], "pivot": [4.5, 17.5, -4.499], "rotation": [0, -90, 0], "uv": [0, 0]}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -4,7 +4,7 @@
|
|||||||
// the kinds of entity mods define in entities/<name>.json (see EntityJson): animals that wander around, panic when hurt
|
// the kinds of entity mods define in entities/<name>.json (see EntityJson): animals that wander around, panic when hurt
|
||||||
// and look at players now and then, like minecraft's simpler animals, monsters that go after players, and scripted
|
// and look at players now and then, like minecraft's simpler animals, monsters that go after players, and scripted
|
||||||
// ones that stay where scripts put them
|
// ones that stay where scripts put them
|
||||||
import type { EntityJson } from "@/common/mod_data.ts";
|
import { entity_category, type EntityJson } from "@/common/mod_data.ts";
|
||||||
import { ATTRIBUTES, type AttributeType } from "./attributes.ts";
|
import { ATTRIBUTES, type AttributeType } from "./attributes.ts";
|
||||||
import { Entity } from "./entity.ts";
|
import { Entity } from "./entity.ts";
|
||||||
import type { EntityFactory } from "./entity_types.ts";
|
import type { EntityFactory } from "./entity_types.ts";
|
||||||
@@ -28,6 +28,7 @@ export function mod_entity_factory(json: EntityJson): EntityFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const monster = json.behavior === "monster";
|
const monster = json.behavior === "monster";
|
||||||
|
const category = entity_category(json);
|
||||||
|
|
||||||
class ModEntity extends Mob {
|
class ModEntity extends Mob {
|
||||||
readonly type = json.id;
|
readonly type = json.id;
|
||||||
@@ -36,7 +37,7 @@ export function mod_entity_factory(json: EntityJson): EntityFactory {
|
|||||||
override eye_height = json.height * 0.85;
|
override eye_height = json.height * 0.85;
|
||||||
|
|
||||||
override get category(): MobCategory {
|
override get category(): MobCategory {
|
||||||
return monster ? "monster" : "creature";
|
return category;
|
||||||
}
|
}
|
||||||
|
|
||||||
override default_attributes(): [AttributeType, number?][] {
|
override default_attributes(): [AttributeType, number?][] {
|
||||||
@@ -68,9 +69,9 @@ export function mod_entity_factory(json: EntityJson): EntityFactory {
|
|||||||
this.goals.add_goal(7, new RandomLookAroundGoal(this));
|
this.goals.add_goal(7, new RandomLookAroundGoal(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
// animals stay, like minecraft's farm animals. monsters go away when no player is near
|
// creatures stay, like minecraft's farm animals. monsters go away when no player is near
|
||||||
override remove_when_far_away() {
|
override remove_when_far_away() {
|
||||||
return monster;
|
return category === "monster";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return (level) => new ModEntity(level);
|
return (level) => new ModEntity(level);
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
// mobs mods define in entities/<name>.json, drawn with a bedrock model from models/<name>.geo.json
|
// mobs mods define in entities/<name>.json, drawn with a bedrock model from models/<name>.geo.json
|
||||||
import { assert, assertAlmostEquals, assertEquals, assertNotEquals, assertStringIncludes } from "@std/assert";
|
import { assert, assertAlmostEquals, assertEquals, assertNotEquals, assertStringIncludes } from "@std/assert";
|
||||||
import { entity_model_from_geo, type EntityBoneJson, validate_entity } from "@/common/mod_data.ts";
|
import { entity_category, entity_model_from_geo, type EntityBoneJson, validate_entity } from "@/common/mod_data.ts";
|
||||||
import type { Texture } from "@/client/renderer/mod.ts";
|
import type { Texture } from "@/client/renderer/mod.ts";
|
||||||
import { IDENTITY } from "@/client/rendering/model_math.ts";
|
import { IDENTITY } from "@/client/rendering/model_math.ts";
|
||||||
import { push_model } from "@/client/rendering/model_renderer.ts";
|
import { push_model } from "@/client/rendering/model_renderer.ts";
|
||||||
@@ -82,8 +82,18 @@ Deno.test("an entity needs a texture and a size", () => {
|
|||||||
Deno.test("an entity without a model is drawn as its texture, and a scripted one has no ai", () => {
|
Deno.test("an entity without a model is drawn as its texture, and a scripted one has no ai", () => {
|
||||||
assertEquals(validate_entity({ id: "a:b", texture: "a:b", behavior: "scripted", width: 0.25, height: 0.25 }), []);
|
assertEquals(validate_entity({ id: "a:b", texture: "a:b", behavior: "scripted", width: 0.25, height: 0.25 }), []);
|
||||||
assertEquals(validate_entity({ id: "a:b", texture: "a:b", behavior: "flying", width: 0.25, height: 0.25 }), [
|
assertEquals(validate_entity({ id: "a:b", texture: "a:b", behavior: "flying", width: 0.25, height: 0.25 }), [
|
||||||
'behavior must be "animal", "monster" or "scripted"',
|
'behavior must be "animal", "monster", "scripted" or a behavior id like "my_mod:thing"',
|
||||||
]);
|
]);
|
||||||
|
assertEquals(validate_entity({ id: "a:b", texture: "a:b", behavior: "a:flying", width: 0.25, height: 0.25 }), []);
|
||||||
|
});
|
||||||
|
|
||||||
|
Deno.test("an entity's category is monster for monsters unless it says otherwise", () => {
|
||||||
|
const entity = { id: "a:b", texture: "a:b", width: 1, height: 1 };
|
||||||
|
assertEquals(entity_category(entity), "creature");
|
||||||
|
assertEquals(entity_category({ ...entity, behavior: "monster" }), "monster");
|
||||||
|
assertEquals(entity_category({ ...entity, behavior: "a:flying", category: "monster" }), "monster");
|
||||||
|
assertEquals(entity_category({ ...entity, behavior: "monster", category: "creature" }), "creature");
|
||||||
|
assertEquals(validate_entity({ ...entity, category: "boss" }), ['category must be "monster" or "creature"']);
|
||||||
});
|
});
|
||||||
|
|
||||||
Deno.test("scripted entities stay put and aren't saved with the world", async () => {
|
Deno.test("scripted entities stay put and aren't saved with the world", async () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user