Goals
This commit is contained in:
@@ -51,7 +51,8 @@ deno task build && deno task server # packs the mods and starts a se
|
||||
|
||||
**Entities**
|
||||
|
||||
- [Entities](docs/modding/entities/index.md): animals, monsters, natural spawning, scripted entities and entity models
|
||||
- [Entities](docs/modding/entities/index.md): animals, monsters, custom behaviors and their goals, how mobs get around,
|
||||
natural spawning, scripted entities and entity models
|
||||
- [Players](docs/modding/entities/players.md): the player API, health and hunger
|
||||
|
||||
**Inventories**
|
||||
|
||||
@@ -266,9 +266,17 @@ export interface MobRef extends LivingRef {
|
||||
readonly last_attacker: Player | MobRef | undefined;
|
||||
// a number from 0 to 1, from the mob's own random, which tests replace
|
||||
random(): number;
|
||||
// walks toward x y z until it's there or stop is called. speed multiplies its movement_speed
|
||||
move_to(x: number, y: number, z: number, speed?: number): void;
|
||||
// finds a way to x y z around what's in between and walks it, see PATHFINDING.md. when the whole way isn't there it
|
||||
// walks as close as it can get. false when it can't get any closer, or when it searched in vain less than a second
|
||||
// ago, keeping the way it had. speed multiplies its movement_speed
|
||||
move_to(x: number, y: number, z: number, speed?: number): boolean;
|
||||
// stops walking its way
|
||||
stop(): void;
|
||||
// whether it's still on its way
|
||||
readonly moving: boolean;
|
||||
// a solid block in its way that its way goes through, tagged #cubi:mobs_path_through, which it's pressed against.
|
||||
// it waits there for as long as the block is solid and in its way, instead of giving up
|
||||
readonly blocked_by: { readonly x: number; readonly y: number; readonly z: number; readonly id: Id } | undefined;
|
||||
// turns its head, and its body after it, toward a point. it stops after a couple of ticks, so a goal that keeps
|
||||
// looking at something calls this every tick
|
||||
look_at(x: number, y: number, z: number): void;
|
||||
|
||||
@@ -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
|
||||
|
||||
+25
-20
@@ -11,6 +11,9 @@ import type { GoalFlag } from "@/common/mod_api/server.ts";
|
||||
|
||||
export type { GoalFlag };
|
||||
|
||||
// how often a mob going after its target searches for a new way to it, at most
|
||||
const RECALCULATE_TICKS = 10;
|
||||
|
||||
export abstract class Goal {
|
||||
flags: GoalFlag[] = [];
|
||||
|
||||
@@ -119,11 +122,9 @@ export class FloatGoal extends Goal {
|
||||
}
|
||||
}
|
||||
|
||||
// wanders to a random spot nearby every now and then, staying near home
|
||||
// wanders to a random spot nearby every now and then, staying near home, along a path to it
|
||||
export class RandomStrollGoal extends Goal {
|
||||
override flags: GoalFlag[] = ["move"];
|
||||
// there's no pathfinding, so it gives up on a spot it can't get to
|
||||
#ticks = 0;
|
||||
#wanted: { x: number; y: number; z: number } | undefined;
|
||||
|
||||
// interval is how many ticks it waits between strolls on average
|
||||
@@ -137,25 +138,19 @@ export class RandomStrollGoal extends Goal {
|
||||
return this.#wanted !== undefined;
|
||||
}
|
||||
|
||||
// until it gets there, or as close as it can
|
||||
override can_continue_to_use() {
|
||||
const wanted = this.#wanted;
|
||||
return wanted !== undefined && this.#ticks < 200 &&
|
||||
(wanted.x - this.mob.x) ** 2 + (wanted.z - this.mob.z) ** 2 > 0.25;
|
||||
return this.mob.navigation.moving;
|
||||
}
|
||||
|
||||
override start() {
|
||||
this.#ticks = 0;
|
||||
const { x, y, z } = this.#wanted!;
|
||||
this.mob.move_control.move_to(x, y, z, this.speed_modifier);
|
||||
}
|
||||
|
||||
override tick() {
|
||||
this.#ticks += 1;
|
||||
this.mob.navigation.move_to(x, y, z, this.speed_modifier);
|
||||
}
|
||||
|
||||
override stop() {
|
||||
this.#wanted = undefined;
|
||||
this.mob.move_control.stop();
|
||||
this.mob.navigation.stop();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -180,18 +175,17 @@ export class PanicGoal extends Goal {
|
||||
}
|
||||
|
||||
override can_continue_to_use() {
|
||||
const wanted = this.#wanted;
|
||||
return wanted !== undefined && (wanted.x - this.mob.x) ** 2 + (wanted.z - this.mob.z) ** 2 > 0.25;
|
||||
return this.mob.navigation.moving;
|
||||
}
|
||||
|
||||
override start() {
|
||||
const { x, y, z } = this.#wanted!;
|
||||
this.mob.move_control.move_to(x, y, z, this.speed_modifier);
|
||||
this.mob.navigation.move_to(x, y, z, this.speed_modifier);
|
||||
}
|
||||
|
||||
override stop() {
|
||||
this.#wanted = undefined;
|
||||
this.mob.move_control.stop();
|
||||
this.mob.navigation.stop();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -264,10 +258,13 @@ export class RandomLookAroundGoal extends Goal {
|
||||
}
|
||||
}
|
||||
|
||||
// walks to its target and hits it when close, like minecraft's MeleeAttackGoal without the pathfinding
|
||||
// follows a path to its target and hits it when close, like minecraft's MeleeAttackGoal. it searches again at most
|
||||
// every RECALCULATE_TICKS, and only once the target is in another block than the one it last searched for
|
||||
export class MeleeAttackGoal extends Goal {
|
||||
override flags: GoalFlag[] = ["move", "look"];
|
||||
#cooldown = 0;
|
||||
#recalculate = 0;
|
||||
#searched_for: string | undefined;
|
||||
|
||||
constructor(readonly mob: Mob, readonly speed_modifier = 1, readonly attack_interval = 20) {
|
||||
super();
|
||||
@@ -280,17 +277,25 @@ export class MeleeAttackGoal extends Goal {
|
||||
|
||||
override start() {
|
||||
this.#cooldown = 0;
|
||||
this.#recalculate = 0;
|
||||
this.#searched_for = undefined;
|
||||
}
|
||||
|
||||
override stop() {
|
||||
this.mob.move_control.stop();
|
||||
this.mob.navigation.stop();
|
||||
}
|
||||
|
||||
override tick() {
|
||||
const mob = this.mob;
|
||||
const target = mob.target!;
|
||||
mob.look_control.look_at_entity(target);
|
||||
mob.move_control.move_to(target.x, target.y, target.z, this.speed_modifier);
|
||||
this.#recalculate -= 1;
|
||||
const block = `${Math.floor(target.x)},${Math.floor(target.y)},${Math.floor(target.z)}`;
|
||||
if (this.#recalculate <= 0 && block !== this.#searched_for) {
|
||||
this.#searched_for = block;
|
||||
this.#recalculate = RECALCULATE_TICKS;
|
||||
mob.navigation.move_to(target.x, target.y, target.z, this.speed_modifier);
|
||||
}
|
||||
this.#cooldown = Math.max(0, this.#cooldown - 1);
|
||||
// minecraft's reach: its width doubled, squared, plus the target's width
|
||||
const reach = (mob.width * 2) ** 2 + target.width;
|
||||
|
||||
@@ -99,11 +99,18 @@ export class PathNavigation {
|
||||
const next = points[this.#index];
|
||||
mob.move_control.move_to(next.x, next.y, next.z, this.#speed);
|
||||
|
||||
// it stays blocked for as long as the block is solid and in its way, even when its path is searched again, so a
|
||||
// goal can count how long it's been there
|
||||
if (this.blocked_by) {
|
||||
this.blocked_by = this.#solid_block_toward(next);
|
||||
if (this.blocked_by) return;
|
||||
this.#closest = Infinity;
|
||||
this.#stuck = 0;
|
||||
}
|
||||
const distance = Math.hypot(next.x - mob.x, next.z - mob.z);
|
||||
if (distance < this.#closest - 0.01) {
|
||||
this.#closest = distance;
|
||||
this.#stuck = 0;
|
||||
this.blocked_by = undefined;
|
||||
return;
|
||||
}
|
||||
this.#stuck += 1;
|
||||
@@ -143,7 +150,6 @@ export class PathNavigation {
|
||||
this.#closest = Infinity;
|
||||
this.#stuck = 0;
|
||||
this.#searched_again = searched_again;
|
||||
this.blocked_by = undefined;
|
||||
}
|
||||
|
||||
// close enough to a waypoint to go on to the next, like minecraft's maxDistanceToWaypoint
|
||||
|
||||
@@ -726,10 +726,17 @@ export class ModRuntime {
|
||||
random: () => mob.random(),
|
||||
move_to(x, y, z, speed = 1) {
|
||||
if (![x, y, z, speed].every(Number.isFinite)) throw new Error("move_to takes a position and a speed");
|
||||
mob.move_control.move_to(x, y, z, speed);
|
||||
return mob.navigation.move_to(x, y, z, speed);
|
||||
},
|
||||
stop() {
|
||||
mob.move_control.stop();
|
||||
mob.navigation.stop();
|
||||
},
|
||||
get moving() {
|
||||
return mob.navigation.moving;
|
||||
},
|
||||
get blocked_by() {
|
||||
const blocked = mob.navigation.blocked_by;
|
||||
return blocked && { ...blocked };
|
||||
},
|
||||
look_at(x, y, z) {
|
||||
if (![x, y, z].every(Number.isFinite)) throw new Error("look_at takes a position");
|
||||
|
||||
@@ -58,3 +58,26 @@ Deno.test("a mob whose way gets walled up searches again, and gives up when ther
|
||||
assert(!mob.navigation.moving);
|
||||
assert(mob.x < 3);
|
||||
});
|
||||
|
||||
Deno.test("a monster finds its way around a wall to the player it's after", async () => {
|
||||
const { game } = await rooms();
|
||||
const alice = game.players()[0];
|
||||
alice.set_position(8.5, 100, 0.5);
|
||||
const monster = test_mob(game, { behavior: "monster" }, 0.5, 100, 0.5);
|
||||
for (let i = 0; i < 400 && Math.hypot(monster.x - 8.5, monster.z - 0.5) > 2; i++) game.tick();
|
||||
assert(Math.hypot(monster.x - 8.5, monster.z - 0.5) <= 2, `it got to ${monster.x} ${monster.z}`);
|
||||
});
|
||||
|
||||
Deno.test("a script's mob walks its way, and stops when told", async () => {
|
||||
const { game, mob } = await rooms();
|
||||
const ref = game.mods.mob(mob);
|
||||
assert(ref.move_to(6.5, 100, 0.5));
|
||||
assert(ref.moving);
|
||||
for (let i = 0; i < 20; i++) game.tick();
|
||||
ref.stop();
|
||||
assert(!ref.moving);
|
||||
const { x, z } = ref.position;
|
||||
for (let i = 0; i < 20; i++) game.tick();
|
||||
assert(Math.hypot(ref.position.x - x, ref.position.z - z) < 0.5);
|
||||
assertEquals(ref.blocked_by, undefined);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user