Goals
This commit is contained in:
@@ -27,7 +27,7 @@ and players. Mods define new kinds in their `entities/` folder, and draw them wi
|
||||
| `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. |
|
||||
| `behavior` | `"animal"` | `"animal"`, `"monster"`, `"scripted"`, or a [custom behavior](#custom-behaviors)'s id. |
|
||||
| `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. |
|
||||
@@ -51,8 +51,9 @@ heart) whatever they hold. The hit pushes them away, and like any damage it can'
|
||||
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:
|
||||
They find their way to it (see [Getting Around](#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:
|
||||
|
||||
```json
|
||||
{
|
||||
@@ -90,6 +91,120 @@ the world, so a script never finds one it doesn't know about after a restart. Th
|
||||
}
|
||||
```
|
||||
|
||||
## 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:
|
||||
|
||||
```json
|
||||
"behavior": "my_mod:sentry"
|
||||
```
|
||||
|
||||
```ts
|
||||
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](effects.md) with:
|
||||
|
||||
```ts
|
||||
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 `jumps` is on, and drops up to `max_fall` blocks. With `max_fall` 0
|
||||
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 and `move_to` still 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_to` doesn'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`](../resources/tags.md#the-engines-tags) 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:
|
||||
|
||||
```ts
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user