Engine entity work
This commit is contained in:
+118
-7
@@ -46,6 +46,7 @@ import type {
|
||||
MenuBuilder,
|
||||
MenuHandle,
|
||||
MenuType,
|
||||
MobRef,
|
||||
Player,
|
||||
PlayerMenuBuilder,
|
||||
Position,
|
||||
@@ -86,7 +87,9 @@ import type { ServerPlayer } from "./player.ts";
|
||||
import { Menu, type MenuFluidSlot, type MenuSlot } from "./menu.ts";
|
||||
import { damage_source, type DamageSource, type Entity } from "./entity/entity.ts";
|
||||
import type { LivingEntity } from "./entity/living_entity.ts";
|
||||
import type { EffectJson } from "@/common/mod_data.ts";
|
||||
import type { EffectJson, EntityJson } from "@/common/mod_data.ts";
|
||||
import { Mob } from "./entity/mob.ts";
|
||||
import { BUILTIN_GOALS, mob_goals } from "./mob_behaviors.ts";
|
||||
import { create_entity } from "./entity/entity_types.ts";
|
||||
import { BLOCK_DROP_PICKUP_DELAY, ItemEntity } from "./entity/item_entity.ts";
|
||||
import { attribute_type, type AttributeType, MODIFIER_OPERATIONS } from "./entity/attributes.ts";
|
||||
@@ -178,6 +181,8 @@ export class ModRuntime {
|
||||
fluid_rules = new FluidRules(run_guarded);
|
||||
item_components = new Map<string, { mod: string; component: ItemComponent }>();
|
||||
effect_components = new Map<string, { mod: string; component: EffectComponent }>();
|
||||
// mods' mob behaviors by id, each adding its goals to a mob, see mod_entity.ts
|
||||
entity_behaviors = new Map<string, (mob: Mob) => void>();
|
||||
// every command, the engine's and mods', see common/commands.ts
|
||||
commands = new CommandDispatcher<ServerCommandSource>();
|
||||
// who registered each command name: "cubi" or a mod's id
|
||||
@@ -247,6 +252,9 @@ export class ModRuntime {
|
||||
|
||||
// the other way, for apis that take a player
|
||||
#server_players = new WeakMap<Player, ServerPlayer>();
|
||||
// mob refs, one for each mob so scripts can compare them, and the other way
|
||||
#mob_refs = new WeakMap<Mob, MobRef>();
|
||||
#mobs = new WeakMap<MobRef, Mob>();
|
||||
// menu types by id, every mod's
|
||||
#menu_types = new Set<string>();
|
||||
// what each mod adds to the player's own menu, in load order
|
||||
@@ -390,6 +398,16 @@ export class ModRuntime {
|
||||
|
||||
this.loot.check();
|
||||
|
||||
for (const [id, entity] of EverythingRegistry.entries<EntityJson>("entities")) {
|
||||
const behavior = entity.behavior;
|
||||
if (behavior?.includes(":") && !this.entity_behaviors.has(behavior)) {
|
||||
throw new ModLoadError(
|
||||
id.split(":")[0],
|
||||
`entity ${id} has behavior ${behavior}, which nothing registered`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
for (const [id, block] of EverythingRegistry.entries<{ components?: Record<string, unknown> }>("blocks")) {
|
||||
for (const component of Object.keys(block.components ?? {})) {
|
||||
if (!this.block_components.has(component) && !this.rules.has(component)) {
|
||||
@@ -651,6 +669,84 @@ export class ModRuntime {
|
||||
return api;
|
||||
}
|
||||
|
||||
mob(mob: Mob): MobRef {
|
||||
let ref = this.#mob_refs.get(mob);
|
||||
if (!ref) {
|
||||
ref = this.#mob_ref(mob);
|
||||
this.#mob_refs.set(mob, ref);
|
||||
this.#mobs.set(ref, mob);
|
||||
}
|
||||
return ref;
|
||||
}
|
||||
|
||||
// a player or a mob the way scripts see them, undefined once they're dead or gone
|
||||
#living_ref(entity: LivingEntity | undefined): Player | MobRef | undefined {
|
||||
if (!entity?.alive) return undefined;
|
||||
return entity instanceof Mob ? this.mob(entity) : this.player(entity as ServerPlayer);
|
||||
}
|
||||
|
||||
// the other way, for apis that take a player or a mob
|
||||
#living_entity(ref: Player | MobRef, what: string): LivingEntity {
|
||||
const entity = this.#server_players.get(ref as Player) ?? this.#mobs.get(ref as MobRef);
|
||||
if (!entity) throw new Error(`${what} must be a player or a mob`);
|
||||
return entity;
|
||||
}
|
||||
|
||||
#mob_ref(mob: Mob): MobRef {
|
||||
const runtime = this;
|
||||
const extra: Omit<MobRef, keyof LivingRef> = {
|
||||
type: mob.type,
|
||||
get position() {
|
||||
return { x: mob.x, y: mob.y, z: mob.z };
|
||||
},
|
||||
get eye_position() {
|
||||
return { x: mob.x, y: mob.y + mob.eye_height, z: mob.z };
|
||||
},
|
||||
get look_direction() {
|
||||
return look_direction(mob.head_yaw, mob.pitch);
|
||||
},
|
||||
get velocity() {
|
||||
return { x: mob.vx, y: mob.vy, z: mob.vz };
|
||||
},
|
||||
get on_ground() {
|
||||
return mob.on_ground;
|
||||
},
|
||||
get in_water() {
|
||||
return mob.in_water;
|
||||
},
|
||||
get target() {
|
||||
return runtime.#living_ref(mob.target);
|
||||
},
|
||||
set target(target) {
|
||||
mob.target = target === undefined ? undefined : runtime.#living_entity(target, "a target");
|
||||
},
|
||||
get last_attacker() {
|
||||
return runtime.#living_ref(mob.last_attacker);
|
||||
},
|
||||
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);
|
||||
},
|
||||
stop() {
|
||||
mob.move_control.stop();
|
||||
},
|
||||
look_at(x, y, z) {
|
||||
if (![x, y, z].every(Number.isFinite)) throw new Error("look_at takes a position");
|
||||
mob.look_control.look_at(x, y, z);
|
||||
},
|
||||
jump() {
|
||||
mob.jump_control.jump();
|
||||
},
|
||||
attack(target) {
|
||||
const entity = runtime.#living_entity(target, "what a mob attacks");
|
||||
mob.level.entity_event(mob, "swing");
|
||||
mob.do_hurt_target(entity);
|
||||
},
|
||||
};
|
||||
return Object.defineProperties(living_ref(mob), Object.getOwnPropertyDescriptors(extra)) as MobRef;
|
||||
}
|
||||
|
||||
block_ref(x: number, y: number, z: number, id: string): BlockRef {
|
||||
const game = this.game;
|
||||
return {
|
||||
@@ -718,6 +814,20 @@ export class ModRuntime {
|
||||
this.effect_components.set(id, { mod, component: component as EffectComponent });
|
||||
},
|
||||
},
|
||||
entities: {
|
||||
register_behavior: (id, behavior) => {
|
||||
register.new_id("entity behavior", id, this.entity_behaviors.has(id));
|
||||
if (typeof behavior !== "function") register.fail(`entity behavior ${id} must be a function`);
|
||||
this.entity_behaviors.set(id, (mob) => {
|
||||
run_guarded(
|
||||
mod,
|
||||
`entity behavior ${id}`,
|
||||
() => behavior(this.mob(mob), mob_goals(mod, id, mob)),
|
||||
);
|
||||
});
|
||||
},
|
||||
goals: BUILTIN_GOALS,
|
||||
},
|
||||
commands: {
|
||||
register: (name_or_node: string | CommandNode<CommandSource>, command?: Command) => {
|
||||
register.running("command");
|
||||
@@ -1486,12 +1596,7 @@ function player_api(game: GameServer, player: ServerPlayer): Player {
|
||||
},
|
||||
// the same way the client aims, see ClientLevel.pick
|
||||
get look_direction() {
|
||||
const cos_pitch = Math.cos(player.pitch);
|
||||
return {
|
||||
x: -Math.sin(player.yaw) * cos_pitch,
|
||||
y: Math.sin(player.pitch),
|
||||
z: -Math.cos(player.yaw) * cos_pitch,
|
||||
};
|
||||
return look_direction(player.yaw, player.pitch);
|
||||
},
|
||||
get biome() {
|
||||
return game.world.biome_at(Math.floor(player.x), Math.floor(player.y), Math.floor(player.z));
|
||||
@@ -1624,6 +1729,12 @@ function effect_of(entity: LivingEntity, id: string) {
|
||||
return effect && { amplifier: effect.amplifier, duration: effect.duration };
|
||||
}
|
||||
|
||||
// which way yaw and pitch face, a vector 1 long
|
||||
function look_direction(yaw: number, pitch: number): Position {
|
||||
const cos_pitch = Math.cos(pitch);
|
||||
return { x: -Math.sin(yaw) * cos_pitch, y: Math.sin(pitch), z: -Math.cos(yaw) * cos_pitch };
|
||||
}
|
||||
|
||||
export function living_ref(entity: LivingEntity): LivingRef {
|
||||
return {
|
||||
id: entity.id,
|
||||
|
||||
Reference in New Issue
Block a user