215 lines
10 KiB
Markdown
215 lines
10 KiB
Markdown
# Entities
|
|
|
|
Entities are the things in the world that aren't blocks and move on their own: animals, dropped items, the fishing hook,
|
|
and players. Mods define new kinds in their `entities/` folder, and draw them with a model from `models/`.
|
|
|
|
## Defining an Entity
|
|
|
|
`entities/chicken.json`:
|
|
|
|
```json
|
|
{
|
|
"format_version": 1,
|
|
"entity": {
|
|
"id": "bworld:chicken",
|
|
"model": "bworld:chicken",
|
|
"texture": "bworld:chicken",
|
|
"width": 0.3,
|
|
"height": 0.4,
|
|
"max_health": 4,
|
|
"movement_speed": 0.25
|
|
}
|
|
}
|
|
```
|
|
|
|
| Field | Default | Meaning |
|
|
| -------------------- | ---------- | ------------------------------------------------------------------------------------------------------ |
|
|
| `id` | required | The entity's id. |
|
|
| `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. |
|
|
| `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. |
|
|
| `attack_damage` | `2` | What a monster's hits do, in half hearts. |
|
|
| `physical_shielding` | `0` | Protection from physical damage. Not used yet. |
|
|
| `arcane_shielding` | `0` | Protection from arcane damage. Not used yet. |
|
|
| `loot_table` | none | A [loot table](../resources/loot_tables.md) it drops from when it dies. |
|
|
|
|
`/summon <entity>` spawns one where you stand.
|
|
|
|
## Behaviors
|
|
|
|
**Animals** (the default) are mobs: they wander around, panic when hurt, look at players now and then, swim up in water,
|
|
and never despawn. They're saved with the world, and players can't place solid blocks where one is standing.
|
|
|
|
Players hit animals, and each other, by left clicking them from up to 3 blocks away, for 1 physical damage (half a
|
|
heart) whatever they hold. The hit pushes them away, and like any damage it can't hurt them again for half a second
|
|
unless it's harder. See [Players](players.md#health-and-hunger).
|
|
|
|
**Monsters** go after the nearest player they can see within 16 blocks, and whoever last hurt them, before anyone else.
|
|
They walk up and hit their target for `attack_damage` once a second, swinging their right arm. Otherwise they wander
|
|
around and look about. Unlike animals, they go away when no player is near. The Rotwalker in `mods/undergarden` is one:
|
|
|
|
```json
|
|
{
|
|
"format_version": 1,
|
|
"entity": {
|
|
"id": "undergarden:rotwalker",
|
|
"model": "undergarden:rotwalker",
|
|
"texture": "undergarden:rotwalker",
|
|
"behavior": "monster",
|
|
"width": 0.8,
|
|
"height": 2.5,
|
|
"max_health": 40,
|
|
"movement_speed": 0.23,
|
|
"attack_damage": 5,
|
|
"physical_shielding": 3
|
|
}
|
|
}
|
|
```
|
|
|
|
Monsters can also [spawn on their own](#natural-spawning) in the dark.
|
|
|
|
**Scripted** entities stay exactly where a script puts them. They don't fall, think or get hurt, and aren't saved with
|
|
the world, so a script never finds one it doesn't know about after a restart. The fishing hook is one:
|
|
|
|
```json
|
|
{
|
|
"format_version": 1,
|
|
"entity": {
|
|
"id": "bworld:fishing_hook",
|
|
"texture": "bworld:fishing_hook",
|
|
"behavior": "scripted",
|
|
"width": 0.25,
|
|
"height": 0.25
|
|
}
|
|
}
|
|
```
|
|
|
|
## Natural Spawning
|
|
|
|
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:
|
|
|
|
```json
|
|
{
|
|
"format_version": 1,
|
|
"spawns": [
|
|
{
|
|
"entity": "undergarden:rotwalker",
|
|
"weight": 100,
|
|
"min_count": 4,
|
|
"max_count": 4,
|
|
"chance": 0.1
|
|
}
|
|
]
|
|
}
|
|
```
|
|
|
|
| Field | Default | Meaning |
|
|
| ----------- | ----------- | --------------------------------------------------------------------------------------------------------------- |
|
|
| `entity` | required | A `"monster"` entity. 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. |
|
|
| `biomes` | every biome | Biome ids, like `["bworld:dark_forest", "bworld:swamp"]`. See [World Generation](../worldgen/index.md#terrain). |
|
|
| `chance` | `1` | The chance a spawn that passed every other check goes ahead. The Undergarden gives its rotspawn 1 in 10. |
|
|
|
|
Every tick, the server makes one try in each chunk within 6 chunks of a player that their client has loaded:
|
|
|
|
1. It picks a random spot in the chunk, anywhere from the bottom of the world to just above the surface, so most spawns
|
|
are in caves.
|
|
2. From there, up to 3 groups wander up to 5 blocks a step at that height. The first spot of a group picks a spawn by
|
|
`weight` from the ones for its biome, and the group tries to spawn between `min_count` and `max_count` of it, at most
|
|
4 from one try.
|
|
3. Each spot has to be at least 24 blocks from every player and from the world spawn, on top of a whole solid block (not
|
|
leaves, glass or a block that gives off light 14 or more), with room for the entity's box and no water in it.
|
|
4. It has to be dark: no block light at all, and less likely the more sky light there is. Under the open sky monsters
|
|
only spawn at night.
|
|
|
|
There are at most 70 monsters for the 17x17 chunks around a player, fewer when fewer chunks are loaded. Monsters that
|
|
never despawn (named, riding or ridden, or that picked up an item) don't count. Like any monster, they despawn when no
|
|
player is near.
|
|
|
|
## Spawning From Scripts
|
|
|
|
```ts
|
|
const hook = ctx.world.spawn_entity("bworld:fishing_hook", x, y, z); // feet at x, y, z
|
|
hook.teleport(x, y + 0.1, z);
|
|
if (!hook.removed) hook.remove();
|
|
```
|
|
|
|
`spawn_entity` throws for a kind that doesn't exist, and returns an `EntityRef`:
|
|
|
|
```ts
|
|
interface EntityRef {
|
|
readonly id: string; // this entity's own id
|
|
readonly type: string; // its kind, like "bworld:fishing_hook"
|
|
readonly position: { x: number; y: number; z: number };
|
|
readonly biome: string; // where its feet are, see ../worldgen/biomes.md#seasons
|
|
readonly season: Season; // that biome's
|
|
readonly removed: boolean;
|
|
readonly velocity: { x: number; y: number; z: number }; // in blocks a second
|
|
readonly on_ground: boolean; // standing or lying on something
|
|
teleport(x: number, y: number, z: number): void;
|
|
push(x: number, y: number, z: number): void; // adds to its velocity
|
|
remove(): void;
|
|
kill(cause?: string): void; // players and mobs die, anything else is removed
|
|
get_item(): ItemStack | undefined; // for an item lying around, a copy
|
|
set_item(item: ItemStack | undefined): void; // for an item lying around, undefined takes it away
|
|
}
|
|
```
|
|
|
|
Once an entity is removed, by a script or anything else, it stays removed, and teleporting it does nothing. Check
|
|
`removed` before relying on one you kept from earlier.
|
|
|
|
`kill` is like NeoForge's `Entity.kill`: a player or a mob dies even when it's invulnerable, the way falling out of the
|
|
world hurts it, and `cause` goes to `entity_damage` and the death message like [`damage`](players.md)'s does. Mods'
|
|
`before.entity_damage` can still cancel it. Anything that can't die, like an item lying around, is removed instead.
|
|
|
|
`push` adds to how fast it's moving, like Minecraft's `Entity.push`: a fan blowing it along does it every tick. Players
|
|
have a `push` too, see [Players](players.md#pushing).
|
|
|
|
## Finding Entities
|
|
|
|
`ctx.world.entities_in(from, to, type?)` finds the entities whose boxes overlap the box between two corners, like
|
|
Minecraft's `getEntitiesOfClass`, only the ones of `type` when it's given. Players aren't among them, they're in
|
|
`ctx.players`.
|
|
|
|
Items lying around are entities of type `cubi:item`. A machine that works on them, like a press, finds them, changes
|
|
them with `set_item`, and adds what it made with `ctx.world.spawn_item`, which lays an item down still where it's put,
|
|
unlike `drop_item`, which pops it out of a block:
|
|
|
|
```ts
|
|
const [item] = ctx.world.entities_in({ x, y: y - 1, z }, { x: x + 1, y, z: z + 1 }, "cubi:item");
|
|
const stack = item?.get_item();
|
|
if (stack?.id === "bworld:iron_ingot") {
|
|
item.set_item(stack.count > 1 ? { ...stack, count: stack.count - 1 } : undefined);
|
|
const { x: ix, y: iy, z: iz } = item.position;
|
|
ctx.world.spawn_item(ix, iy, iz, { id: "create:iron_sheet", count: 1 });
|
|
}
|
|
```
|
|
|
|
## Entity Models
|
|
|
|
Entity models are `models/<name>.geo.json`, as Blockbench exports them for Bedrock (the "Bedrock Entity" format), and
|
|
get the id `<mod id>:<name>`. From the file's first geometry, bworld uses:
|
|
|
|
- the description's `texture_width` and `texture_height`,
|
|
- each bone's `name`, `parent`, `pivot` and `rotation`,
|
|
- each cube's `origin`, `size`, `uv`, `inflate`, `pivot` and `rotation`.
|
|
|
|
Cubes need box UV: per face UV isn't supported. A bone's parent has to come before it. The model faces -z, 16 pixels to
|
|
a block with its feet at 0, and shows the way Blockbench shows it.
|
|
|
|
There are no animation files yet. The whole model turns with the entity, and bones with Minecraft's humanoid names move
|
|
on their own:
|
|
|
|
- `head` turns to where the entity looks.
|
|
- `right_arm`, `left_arm`, `right_leg` and `left_leg` swing as it walks, each arm with the opposite leg.
|
|
- `right_arm` also comes down when a monster attacks.
|
|
|
|
Without a model, the whole texture is drawn facing the camera, 16 pixels to a block, centered on the middle of the
|
|
entity's box.
|