17 KiB
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:
{
"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. Without one it's drawn as its texture, facing the camera. |
behavior |
"animal" |
"animal", "monster", "scripted", or a custom behavior's id. |
category |
see below | "monster" or "creature", see 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. |
jumps |
true |
Whether it jumps onto blocks in its way. Without, it still steps up 0.6 blocks and swims up in water. |
max_fall |
3 |
How many blocks it drops on its way. 0 never walks off an edge, to keep monsters out of pits. |
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 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.
Monsters go after the nearest player they can see within 16 blocks, and whoever last hurt them, before anyone else.
They find their way to it (see Getting Around) and hit it 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:
{
"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 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:
{
"format_version": 1,
"entity": {
"id": "bworld:fishing_hook",
"texture": "bworld:fishing_hook",
"behavior": "scripted",
"width": 0.25,
"height": 0.25
}
}
Custom Behaviors
A mob can get its goals from a script instead, like NeoForge's registerGoals. Its json names a behavior id, and the
server phase registers a function under that id, which runs once for each mob of that kind before it first thinks:
"behavior": "my_mod:sentry"
export function server(ctx: ServerContext) {
const chase = ctx.entities.goals.melee_attack({ speed: 1.25 });
ctx.entities.register_behavior("my_mod:sentry", (mob, goals) => {
// stands still at night, taking every control from the goals after it
goals.add(0, {
flags: ["move", "look", "jump"],
can_use: () => ctx.world.hour < 6 || ctx.world.hour >= 18,
start: () => mob.stop(),
});
goals.add(1, chase);
goals.add_target(0, ctx.entities.goals.nearest_attackable_player());
});
}
A server won't start with an entity whose behavior nothing registered.
Every tick, running goals that can't continue stop, then the ones that can start do, most important (lowest priority
number) first, then every running goal ticks. A goal only starts once each of its flags is free or held by a less
important goal, which then stops: "move", "look", "jump", and "target" for the goals in add_target that pick
who the mob goes after.
| Goal field | Meaning |
|---|---|
flags |
The controls it needs while it runs. None when not given. |
can_use() |
Whether it wants to start. The only one it needs. |
can_continue_to_use() |
Whether it wants to keep going. can_use when not given. |
is_interruptable() |
Whether a more important goal can take its flags. True when not given. |
start(), stop() |
When it starts and stops running. |
tick() |
Every tick while it runs. |
A goal that throws is logged once and never runs again, and the mob's other goals go on. What a mob's goals have to remember goes in the behavior function's closures, which each mob gets its own of.
ctx.entities.goals has the goals animals and monsters are made of, to add next to your own. One can be added to many
mobs:
| Goal | What it does |
|---|---|
float() |
Swims up in water. |
random_stroll({ speed?, interval?, range? }) |
Walks to a random spot within range every interval ticks or so. |
panic({ speed? }) |
Runs around after being hurt or while burning. |
look_at_player({ range?, probability? }) |
Looks at a nearby player for a couple of seconds now and then. |
random_look_around() |
Looks around at nothing in particular. |
melee_attack({ speed?, interval? }) |
Goes after its target and hits it, at most every interval ticks. |
hurt_by_target(), nearest_attackable_player() |
Target goals: whoever hurt it last, the nearest player. |
Speeds multiply its movement_speed. The mob a behavior gets is a MobRef, a living entity with:
interface MobRef extends LivingRef {
readonly type: Id;
readonly position: Readonly<Position>;
readonly eye_position: Readonly<Position>; // where it sees from
readonly look_direction: Readonly<Position>; // a vector 1 long
readonly velocity: Readonly<Position>; // in blocks a second
readonly on_ground: boolean;
readonly in_water: boolean;
target: Player | MobRef | undefined; // who it's after, undefined once they're dead or gone
readonly last_attacker: Player | MobRef | undefined;
random(): number; // 0 to 1, from the mob's own random
move_to(x: number, y: number, z: number, speed?: number): boolean; // see Getting Around
stop(): void;
readonly moving: boolean;
readonly blocked_by: { x: number; y: number; z: number; id: Id } | undefined;
look_at(x: number, y: number, z: number): void; // for a couple of ticks, so call it every tick
jump(): void; // this tick, or swims up in water
attack(target: Player | MobRef): void; // swings and hits with its attack_damage
}
Each mob has one MobRef, so mob.target === player works.
Getting Around
Mobs find their way around walls and through gaps, like Minecraft's pathfinding. move_to searches for a way and the
mob walks it, and it's how strolling, panicking and going after a target get anywhere:
- A way only goes where the mob's box fits, so a 2 tall mob doesn't fit under a 2 high gap. It steps onto anything up to
0.6 high, like a slab, jumps up 1 block when its
jumpsis on, and drops up tomax_fallblocks. Withmax_fall0 it never walks off an edge either, like a sneaking player, though something that knocks it into the air can still knock it off. - It looks up to its
follow_range(16 blocks unless an attribute changes it) from where it is. When it can't get to the spot, it goes as close as it can andmove_tostill returns true. It returns false when it can't get any closer. - A search that didn't get the whole way is the slow kind, so for a second after one,
move_todoesn't search again. - Stuck with no closer to its way for two seconds, it searches once more, then stops.
Blocks in the #cubi:mobs_path_through tag count as empty, solid or not, so a
way can go through them. A mob pressed against a solid one on its way stops there with blocked_by set to it, and waits
for as long as it stays solid and in the way. It's up to the behavior what happens then, like a goal without flags, so
the mob keeps its way, that breaks the block after two seconds:
let waited = 0;
goals.add(0, {
can_use: () => mob.blocked_by !== undefined,
start: () => waited = 0,
tick: () => {
const { x, y, z } = mob.blocked_by!;
if (++waited === 40) ctx.world.break_block(x, y, z);
},
});
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:
{
"format_version": 1,
"spawns": [
{
"entity": "undergarden:rotwalker",
"weight": 100,
"min_count": 4,
"max_count": 4,
"chance": 0.1
}
]
}
| Field | Default | Meaning |
|---|---|---|
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. |
biomes |
every biome | Biome ids, like ["bworld:dark_forest", "bworld:swamp"]. See World Generation. |
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:
- 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.
- 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
weightfrom the ones for its biome, and the group tries to spawn betweenmin_countandmax_countof it, at most 4 from one try. - 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.
- 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
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:
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'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.
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:
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_widthandtexture_height, - each bone's
name,parent,pivotandrotation, - each cube's
origin,size,uv,inflate,pivotandrotation.
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:
headturns to where the entity looks.right_arm,left_arm,right_legandleft_legswing as it walks, each arm with the opposite leg.right_armalso 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.