Make players mobs too !

This commit is contained in:
2026-09-30 10:42:52 -03:00
parent 65f27d6099
commit 4fbcca6851
23 changed files with 314 additions and 132 deletions
+4 -7
View File
@@ -9,6 +9,7 @@ import type { BlockRegistry } from "$/common/everything_registry.ts";
import type { ItemStack } from "$/common/inventory.ts";
import { type Collisions, move_body } from "$/common/physics.ts";
import type { EntityData, EntityInfo } from "$/common/protocol.ts";
import type { ServerPlayer } from "../player.ts";
// below this, entities have fallen out of the world
export const VOID_Y = -64;
@@ -27,11 +28,6 @@ export interface Attacker {
readonly z: number;
}
// a player as entities see them, ServerPlayer is one
export interface PlayerTarget extends Attacker {
readonly name: string;
}
export type DamageType = "physical" | "arcane" | "absolute";
// what hurt something. cause is for mods and death messages, like "fall", "drown" or "mob_attack"
@@ -68,8 +64,9 @@ export interface EntityLevel {
get_entity(id: string): Entity | undefined;
// entities whose feet are within the box around x, y, z
entities_near(x: number, y: number, z: number, radius: number): Entity[];
nearest_player(x: number, y: number, z: number, max_distance: number): PlayerTarget | undefined;
damage_player(id: string, amount: number, source: DamageSource): void;
nearest_player(x: number, y: number, z: number, max_distance: number): ServerPlayer | undefined;
// what ServerPlayer.hurt does: mods get a say, then they're hurt. returns whether they were
hurt_player(player: ServerPlayer, amount: number, source: DamageSource): boolean;
spawn_item(x: number, y: number, z: number, stack: ItemStack, pickup_delay?: number): Entity;
// an item on the ground was picked up whole by who, clients see it fly to them. it's gone after
take_item(item: Entity, by: string): void;
+8 -5
View File
@@ -17,7 +17,7 @@ export interface EntityHost {
readonly current_tick: number;
players(): ServerPlayer[];
send(player: ServerPlayer, message: ServerMessage): void;
damage_player(player: ServerPlayer, amount: number, cause: string): void;
hurt_player(player: ServerPlayer, amount: number, source: DamageSource): boolean;
on_entity_death?(entity: Entity, source: DamageSource): void;
}
@@ -110,9 +110,8 @@ export class EntityManager implements EntityLevel {
return nearest;
}
damage_player(id: string, amount: number, source: DamageSource) {
const player = this.#host.players().find((player) => player.id === id);
if (player) this.#host.damage_player(player, amount, source.cause);
hurt_player(player: ServerPlayer, amount: number, source: DamageSource) {
return this.#host.hurt_player(player, amount, source);
}
spawn_item(x: number, y: number, z: number, stack: ItemStack, pickup_delay = 0): ItemEntity {
@@ -131,9 +130,13 @@ export class EntityManager implements EntityLevel {
this.#entities.delete(item.id);
}
// players aren't tracked like entities, everyone else knows about them
entity_event(entity: Entity, event: "hurt" | "death") {
const is_player = this.#host.players().includes(entity as ServerPlayer);
for (const [player, tracked] of this.#tracked) {
if (tracked.has(entity.id)) this.#host.send(player, { type: "entity_event", id: entity.id, event });
if (is_player ? player !== entity : tracked.has(entity.id)) {
this.#host.send(player, { type: "entity_event", id: entity.id, event });
}
}
}
+5 -8
View File
@@ -1,10 +1,8 @@
// what mobs decide to do, like minecraft's Goal and GoalSelector. each goal says when it wants to run and which
// controls it needs (its flags), and the most important goals that can run get them. a lower priority number is
// more important, and can take a flag from a less important goal that's running
import { PLAYER_WIDTH } from "$/common/constants.ts";
import { ATTRIBUTES } from "./attributes.ts";
import { Entity, type PlayerTarget } from "./entity.ts";
import { LivingEntity } from "./living_entity.ts";
import type { LivingEntity } from "./living_entity.ts";
import type { Mob } from "./mob.ts";
export type GoalFlag = "move" | "look" | "jump" | "target";
@@ -196,7 +194,7 @@ export class PanicGoal extends Goal {
// looks at a nearby player for a couple of seconds, now and then
export class LookAtPlayerGoal extends Goal {
override flags: GoalFlag[] = ["look"];
#player: PlayerTarget | undefined;
#player: LivingEntity | undefined;
#ticks = 0;
constructor(readonly mob: Mob, readonly range = 8, readonly probability = 0.02) {
@@ -291,7 +289,7 @@ export class MeleeAttackGoal extends Goal {
mob.move_control.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 instanceof Entity ? target.width : PLAYER_WIDTH);
const reach = (mob.width * 2) ** 2 + target.width;
const distance = (target.x - mob.x) ** 2 + (target.y - mob.y) ** 2 + (target.z - mob.z) ** 2;
if (distance <= reach && this.#cooldown === 0) {
this.#cooldown = this.attack_interval;
@@ -333,9 +331,8 @@ export class NearestAttackablePlayerGoal extends Goal {
}
}
// players are always alive to mobs, they respawn straight away
function is_alive(target: LivingEntity | PlayerTarget) {
return !(target instanceof LivingEntity) || target.alive;
function is_alive(target: LivingEntity) {
return target.alive;
}
// somewhere random within range it could stand: solid ground under it, room for its box, and inside its home.
+16
View File
@@ -331,6 +331,22 @@ export abstract class LivingEntity extends Entity {
this.health -= post_mitigation;
}
// back to full health with nothing left over from before: no effects, fire, hurt cooldown or who hurt it. for
// players respawning, minecraft makes them a new entity instead
revive() {
this.clear_effects();
this.health = this.max_health;
this.fire = 0;
this.air = this.max_air;
this.fall_distance = 0;
this.vx = this.vy = this.vz = 0;
this.hurt_time = this.death_time = 0;
this.#hurt_cooldown = 0;
this.#last_hurt_amount = 0;
this.last_hurt_by_mob = this.last_hurt_by_player = undefined;
this.ticks_since_last_hurt_by_mob = this.last_hurt_by_player_memory_time = 0;
}
// it was just hurt, for kinds that react
on_hurt(_source: DamageSource) {}
+3 -7
View File
@@ -1,7 +1,7 @@
// living entities with a mind of their own, like minecraft's Mob: goals decide what to do (see goals.ts) and
// controls turn that into walking, looking and jumping. they can despawn, pick up items and be kept near a home
import { ATTRIBUTES, type AttributeType } from "./attributes.ts";
import { type DamageSource, Entity, finite, type PlayerTarget } from "./entity.ts";
import { type DamageSource, Entity, finite } from "./entity.ts";
import { GoalSelector } from "./goals.ts";
import { ItemEntity } from "./item_entity.ts";
import { EQUIPMENT_SLOTS, type EquipmentSlot, LivingEntity, type SavedLivingEntity } from "./living_entity.ts";
@@ -56,7 +56,7 @@ export abstract class Mob extends LivingEntity {
goals = new GoalSelector();
target_goals = new GoalSelector();
// who it's after, set by target goals
target: LivingEntity | PlayerTarget | undefined;
target: LivingEntity | undefined;
move_control = new MoveControl(this);
look_control = new LookControl(this);
@@ -165,13 +165,9 @@ export abstract class Mob extends LivingEntity {
// fighting
// hits whoever it's attacking with its attack damage, like minecraft's doHurtTarget. returns whether it hurt them
do_hurt_target(target: Entity | PlayerTarget): boolean {
do_hurt_target(target: Entity): boolean {
const damage = this.attribute(ATTRIBUTES.attack_damage);
const source = { cause: "mob_attack", attacker: this, type: "physical" } as const;
if (!(target instanceof Entity)) {
this.level.damage_player(target.id, damage, source);
return true;
}
if (!(target instanceof LivingEntity)) return false;
if (!target.hurt(damage, source)) return false;
const knockback = this.attribute(ATTRIBUTES.attack_knockback);