From 60a7cae86eba80dacbdb5e76c5801f9d0baa6968 Mon Sep 17 00:00:00 2001 From: Paula Date: Sat, 21 Nov 2026 10:53:33 -0300 Subject: [PATCH] Start working on entities --- common/mod_check.ts | 9 +- common/mod_data.ts | 16 +++- docs/modding/entities/index.md | 7 +- .../dangerous_business/entities/coilhead.json | 15 +++ .../models/coilhead.geo.json | 91 +++++++++++++++++++ server/game/entity/mod_entity.ts | 9 +- tests/mod_entity_test.ts | 14 ++- 7 files changed, 150 insertions(+), 11 deletions(-) create mode 100644 mods/dangerous_business/entities/coilhead.json create mode 100644 mods/dangerous_business/models/coilhead.geo.json diff --git a/common/mod_check.ts b/common/mod_check.ts index 54d6c76..9e83750 100644 --- a/common/mod_check.ts +++ b/common/mod_check.ts @@ -7,6 +7,7 @@ import { type BlockJson, choice_list, type EffectJson, + entity_category, type EntityJson, type EntityModelJson, type FluidJson, @@ -380,6 +381,8 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable( + (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 width: 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; +// 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 const effect_category = z.enum(["beneficial", "harmful", "neutral"]); export type EffectCategory = z.infer; diff --git a/docs/modding/entities/index.md b/docs/modding/entities/index.md index 73f3de1..f9c3c8c 100644 --- a/docs/modding/entities/index.md +++ b/docs/modding/entities/index.md @@ -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`. | | `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. | +| `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. | | `max_health` | `10` | In half hearts. | | `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 +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 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 | | ----------- | ----------- | --------------------------------------------------------------------------------------------------------------- | -| `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. | | `min_count` | required | How many spawn together, at least. | | `max_count` | required | And at most. | diff --git a/mods/dangerous_business/entities/coilhead.json b/mods/dangerous_business/entities/coilhead.json new file mode 100644 index 0000000..0732a6e --- /dev/null +++ b/mods/dangerous_business/entities/coilhead.json @@ -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 + } +} diff --git a/mods/dangerous_business/models/coilhead.geo.json b/mods/dangerous_business/models/coilhead.geo.json new file mode 100644 index 0000000..3b6776a --- /dev/null +++ b/mods/dangerous_business/models/coilhead.geo.json @@ -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]} + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/server/game/entity/mod_entity.ts b/server/game/entity/mod_entity.ts index 0c31944..89ed35e 100644 --- a/server/game/entity/mod_entity.ts +++ b/server/game/entity/mod_entity.ts @@ -4,7 +4,7 @@ // the kinds of entity mods define in entities/.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 // 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 { Entity } from "./entity.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 category = entity_category(json); class ModEntity extends Mob { readonly type = json.id; @@ -36,7 +37,7 @@ export function mod_entity_factory(json: EntityJson): EntityFactory { override eye_height = json.height * 0.85; override get category(): MobCategory { - return monster ? "monster" : "creature"; + return category; } 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)); } - // 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() { - return monster; + return category === "monster"; } } return (level) => new ModEntity(level); diff --git a/tests/mod_entity_test.ts b/tests/mod_entity_test.ts index c7586fa..f01e44f 100644 --- a/tests/mod_entity_test.ts +++ b/tests/mod_entity_test.ts @@ -3,7 +3,7 @@ // mobs mods define in entities/.json, drawn with a bedrock model from models/.geo.json 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 { IDENTITY } from "@/client/rendering/model_math.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", () => { 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 }), [ - '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 () => {