Make players mobs too !
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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 });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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) {}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
+60
-41
@@ -44,7 +44,7 @@ import {
|
||||
placement_target,
|
||||
set_state_value,
|
||||
} from "$/common/utils.ts";
|
||||
import { EXHAUSTION, MAX_HEALTH } from "./vitals.ts";
|
||||
import { EXHAUSTION } from "./vitals.ts";
|
||||
import { ModRuntime, run_guarded, type ServerCommandSource } from "./mod_runtime.ts";
|
||||
import {
|
||||
argument,
|
||||
@@ -75,12 +75,11 @@ import { Mob } from "./entity/mob.ts";
|
||||
import { LivingEntity } from "./entity/living_entity.ts";
|
||||
import { EntityManager } from "./entity/entity_manager.ts";
|
||||
import { create_entity, entity_type_ids } from "./entity/entity_types.ts";
|
||||
import type { SavedEntity } from "./entity/entity.ts";
|
||||
import { DAMAGE, type DamageSource, type SavedEntity } from "./entity/entity.ts";
|
||||
import { ATTRIBUTES } from "./entity/attributes.ts";
|
||||
|
||||
// how far from a player's eyes a block can be changed, a bit more than the client's reach
|
||||
const MAX_REACH = 8;
|
||||
// how hard a player hits with their fist, whatever they're holding. half a heart, like minecraft
|
||||
const PLAYER_ATTACK_DAMAGE = 1;
|
||||
// how far past a player's box items get picked up from, sideways and up or down, like minecraft
|
||||
const PICKUP_REACH_XZ = 1;
|
||||
const PICKUP_REACH_Y = 0.5;
|
||||
@@ -97,10 +96,6 @@ const SPRINT_SPEED = 5;
|
||||
const TELEPORT_DISTANCE = 20;
|
||||
// how close feet have to be to a block's top to stand on it
|
||||
const GROUND_EPSILON = 0.02;
|
||||
// below the world, players take VOID_DAMAGE every VOID_DAMAGE_TICKS until they die
|
||||
const VOID_Y = -64;
|
||||
const VOID_DAMAGE = 4;
|
||||
const VOID_DAMAGE_TICKS = 10;
|
||||
// the most mornings run at once after time jumps forward, the rest are skipped
|
||||
const MAX_CATCH_UP_DAYS = 64;
|
||||
// how long a client gets between welcome and ready to download and load everything
|
||||
@@ -206,7 +201,10 @@ export class GameServer {
|
||||
},
|
||||
players: () => this.players(),
|
||||
send: (player, message) => this.#send(player, message),
|
||||
damage_player: (player, amount, cause) => this.damage_player(player, amount, cause),
|
||||
hurt_player: (player, amount, source) => this.hurt_player(player, amount, source),
|
||||
on_entity_death: (entity, source) => {
|
||||
if (entity instanceof ServerPlayer) this.#kill(entity, source);
|
||||
},
|
||||
});
|
||||
this.mods.storage = saved?.mod_storage ?? {};
|
||||
if (saved) {
|
||||
@@ -271,9 +269,11 @@ export class GameServer {
|
||||
return;
|
||||
}
|
||||
this.#close_screen(player);
|
||||
this.#saved_players[player.name] = player.save();
|
||||
this.#saved_players[player.name] = player.save_player();
|
||||
this.world.dirty = true;
|
||||
this.#players.delete(conn);
|
||||
// mobs after them give up
|
||||
player.remove();
|
||||
this.entities.stop_tracking(player);
|
||||
this.mods.after.player_leave.emit({ player: this.mods.player(player) });
|
||||
this.#broadcast({ type: "player_leave", id: player.id });
|
||||
@@ -358,7 +358,7 @@ export class GameServer {
|
||||
// level.json and the chunks changed since the last save
|
||||
save(): Required<WorldSave> {
|
||||
for (const player of this.#players.values()) {
|
||||
this.#saved_players[player.name] = player.save();
|
||||
this.#saved_players[player.name] = player.save_player();
|
||||
}
|
||||
const level: SavedLevel = {
|
||||
version: 5,
|
||||
@@ -672,10 +672,10 @@ export class GameServer {
|
||||
}
|
||||
|
||||
#join(conn: number, raw_name: unknown, view_distance: number) {
|
||||
const player = new ServerPlayer(conn, this.#unique_name(raw_name), view_distance);
|
||||
const player = new ServerPlayer(this.entities, conn, this.#unique_name(raw_name), view_distance);
|
||||
const saved = this.#saved_players[player.name];
|
||||
if (saved) {
|
||||
player.load(saved);
|
||||
player.load_player(saved);
|
||||
} else {
|
||||
Object.assign(player, this.world.spawn_point());
|
||||
}
|
||||
@@ -684,7 +684,7 @@ export class GameServer {
|
||||
type: "join",
|
||||
id: player.id,
|
||||
name: player.name,
|
||||
players: [...this.#players.values()].map((p) => p.info()),
|
||||
players: [...this.#players.values()].map((p) => p.player_info()),
|
||||
entities: this.entities.start_tracking(player),
|
||||
blocks: this.world.block_table(),
|
||||
spawn: { x: player.x, y: player.y, z: player.z, yaw: player.yaw, pitch: player.pitch },
|
||||
@@ -696,7 +696,7 @@ export class GameServer {
|
||||
this.#update_view(player);
|
||||
this.#sync(player);
|
||||
|
||||
this.#broadcast({ type: "player_join", player: player.info() }, player);
|
||||
this.#broadcast({ type: "player_join", player: player.player_info() }, player);
|
||||
this.#broadcast({ type: "chat", text: `${player.name} joined` });
|
||||
this.mods.after.player_join.emit({ player: this.mods.player(player) });
|
||||
console.log(`${player.name} joined (${this.#players.size} online)`);
|
||||
@@ -813,14 +813,18 @@ export class GameServer {
|
||||
}
|
||||
}
|
||||
|
||||
// left click on an entity: hurts it and pushes it away. it can't be hurt again for a moment, see LivingEntity.hurt
|
||||
// left click on an entity or another player: hurts them with the player's attack_damage and pushes them away.
|
||||
// they can't be hurt again for a moment, see LivingEntity.hurt
|
||||
#attack_entity(player: ServerPlayer, id: string) {
|
||||
const entity = this.entities.get_entity(id);
|
||||
if (!(entity instanceof LivingEntity) || !entity.alive || !this.#entity_in_reach(player, entity)) {
|
||||
const entity = this.entities.get_entity(id) ?? this.players().find((other) => other.id === id);
|
||||
if (
|
||||
!(entity instanceof LivingEntity) || entity === player || !entity.alive || !player.alive ||
|
||||
!this.#entity_in_reach(player, entity)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
const source = { type: "physical", cause: "player_attack", attacker: player, by_player: true } as const;
|
||||
if (entity.hurt(PLAYER_ATTACK_DAMAGE, source)) {
|
||||
if (entity.hurt(player.attribute(ATTRIBUTES.attack_damage), source)) {
|
||||
player.vitals.add_exhaustion(EXHAUSTION.attack);
|
||||
}
|
||||
}
|
||||
@@ -1330,31 +1334,32 @@ export class GameServer {
|
||||
|
||||
// hurts a player, killing them at 0. cause is for mods and the death message, like "fall"
|
||||
damage_player(player: ServerPlayer, amount: number, cause: string) {
|
||||
if (amount <= 0 || player.vitals.health <= 0) {
|
||||
return;
|
||||
const known: Record<string, DamageSource> = DAMAGE;
|
||||
player.hurt(amount, known[cause] ?? { cause, type: "physical" });
|
||||
}
|
||||
|
||||
// everything that hurts a player ends up here, see ServerPlayer.hurt. mods can change or cancel it first
|
||||
hurt_player(player: ServerPlayer, amount: number, source: DamageSource): boolean {
|
||||
if (player.is_invulnerable_to(source) || !player.alive || !(amount > 0)) {
|
||||
return false;
|
||||
}
|
||||
const before = { player: this.mods.player(player), amount, cause, cancel: false };
|
||||
const before = { player: this.mods.player(player), amount, cause: source.cause, cancel: false };
|
||||
this.mods.before.player_damage.emit(before);
|
||||
const final = Number(before.amount);
|
||||
if (before.cancel || !(final > 0)) {
|
||||
return;
|
||||
}
|
||||
player.vitals.health = Math.max(0, player.vitals.health - final);
|
||||
player.vitals.add_exhaustion(EXHAUSTION.damage);
|
||||
if (player.vitals.health <= 0) {
|
||||
this.#kill(player, cause);
|
||||
return false;
|
||||
}
|
||||
return player.apply_hurt(final, source);
|
||||
}
|
||||
|
||||
heal_player(player: ServerPlayer, amount: number) {
|
||||
if (amount > 0 && player.vitals.health > 0) {
|
||||
player.vitals.health = Math.min(MAX_HEALTH, player.vitals.health + amount);
|
||||
}
|
||||
player.heal(amount);
|
||||
}
|
||||
|
||||
// like minecraft: everything they carried falls out where they died, and they start over at spawn. tools lose 1
|
||||
// durability first, and ones that run out break instead of dropping
|
||||
#kill(player: ServerPlayer, cause: string) {
|
||||
#kill(player: ServerPlayer, source: DamageSource) {
|
||||
const cause = source.cause;
|
||||
const { x, y, z } = player;
|
||||
this.close_screen(player);
|
||||
for (let i = 0; i < player.inventory.size; i++) {
|
||||
@@ -1366,8 +1371,9 @@ export class GameServer {
|
||||
}
|
||||
}
|
||||
this.mods.after.player_death.emit({ player: this.mods.player(player), cause });
|
||||
this.#broadcast({ type: "chat", text: death_message(player.name, cause) });
|
||||
this.#broadcast({ type: "chat", text: death_message(player.name, source) });
|
||||
player.vitals.reset();
|
||||
player.revive();
|
||||
const spawn = this.world.spawn_point();
|
||||
this.teleport(player, spawn.x, spawn.y, spawn.z);
|
||||
this.world.dirty = true;
|
||||
@@ -1375,14 +1381,13 @@ export class GameServer {
|
||||
|
||||
#tick_vitals() {
|
||||
for (const player of this.#players.values()) {
|
||||
// burning, effects, and falling out of the world
|
||||
player.tick();
|
||||
player.vitals.tick(
|
||||
player.health,
|
||||
(amount) => this.heal_player(player, amount),
|
||||
(amount) => this.damage_player(player, amount, "starve"),
|
||||
);
|
||||
// out of the world, like minecraft's void
|
||||
if (player.y < VOID_Y && this.#tick % VOID_DAMAGE_TICKS === 0) {
|
||||
this.damage_player(player, VOID_DAMAGE, "void");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1416,7 +1421,9 @@ export class GameServer {
|
||||
if (dy < 0 && !in_liquid) {
|
||||
player.fall_distance -= dy;
|
||||
}
|
||||
if (this.#on_ground(x, y, z)) {
|
||||
// knockback only lifts them off the ground
|
||||
player.on_ground = this.#on_ground(x, y, z);
|
||||
if (player.on_ground) {
|
||||
const damage = Math.ceil(player.fall_distance - SAFE_FALL);
|
||||
player.fall_distance = 0;
|
||||
if (damage > 0) {
|
||||
@@ -1523,6 +1530,13 @@ export class GameServer {
|
||||
}
|
||||
};
|
||||
|
||||
// knockback, their client moves them
|
||||
const motion = player.take_motion();
|
||||
if (motion) {
|
||||
const [vx, vy, vz] = motion;
|
||||
this.#send(player, { type: "set_motion", vx, vy, vz });
|
||||
}
|
||||
|
||||
// with lore, so it's resent when only the lore changed
|
||||
const inventory = player.inventory.to_synced_data();
|
||||
sync("inventory", inventory, () => ({ type: "container", container: "inventory", items: inventory }));
|
||||
@@ -1531,7 +1545,8 @@ export class GameServer {
|
||||
const cursor = player.cursor.item?.to_synced_data() ?? null;
|
||||
sync("cursor", cursor, () => ({ type: "cursor", item: cursor }));
|
||||
|
||||
const { health, food, saturation } = player.vitals;
|
||||
const { food, saturation } = player.vitals;
|
||||
const health = player.health;
|
||||
sync("vitals", [health, food, saturation], () => ({ type: "vitals", health, food, saturation }));
|
||||
|
||||
if (player.screen) {
|
||||
@@ -1708,8 +1723,12 @@ function upgrade_v4_entities(entities: SavedLevelV4["entities"]): SavedEntity[]
|
||||
}
|
||||
|
||||
// what everyone's told when someone dies
|
||||
function death_message(name: string, cause: string) {
|
||||
switch (cause) {
|
||||
function death_message(name: string, source: DamageSource) {
|
||||
switch (source.cause) {
|
||||
case "player_attack":
|
||||
return source.attacker instanceof ServerPlayer
|
||||
? `${name} was slain by ${source.attacker.name}`
|
||||
: `${name} died`;
|
||||
case "fall":
|
||||
return `${name} hit the ground too hard`;
|
||||
case "void":
|
||||
|
||||
@@ -782,7 +782,7 @@ function player_api(game: GameServer, player: ServerPlayer): Player {
|
||||
game.teleport(player, x, y, z);
|
||||
},
|
||||
get health() {
|
||||
return player.vitals.health;
|
||||
return player.health;
|
||||
},
|
||||
max_health: MAX_HEALTH,
|
||||
get food() {
|
||||
|
||||
+77
-15
@@ -1,7 +1,11 @@
|
||||
import { PLAYER_EYE_HEIGHT, PLAYER_HEIGHT, PLAYER_WIDTH } from "$/common/constants.ts";
|
||||
import { Container, Cursor, ItemData, ItemStack } from "$/common/inventory.ts";
|
||||
import { PlayerInfo, ScreenLayout } from "$/common/protocol.ts";
|
||||
import { type SavedVitals, Vitals } from "./vitals.ts";
|
||||
import { EXHAUSTION, type SavedVitals, Vitals } from "./vitals.ts";
|
||||
import { PlayerChunks } from "./player_chunks.ts";
|
||||
import { ATTRIBUTES, type AttributeType } from "./entity/attributes.ts";
|
||||
import type { DamageSource, EntityLevel } from "./entity/entity.ts";
|
||||
import { LivingEntity } from "./entity/living_entity.ts";
|
||||
|
||||
export const INVENTORY_SIZE = 9 * 4;
|
||||
export const CRAFTING_SIZE = 10;
|
||||
@@ -35,21 +39,22 @@ export interface SavedPlayer {
|
||||
slim?: boolean;
|
||||
}
|
||||
|
||||
export class ServerPlayer {
|
||||
// a player is a living entity, like minecraft's Player: they have health, can be hurt and pushed around by mobs and
|
||||
// other players, and have effects. their client moves them though, so they don't walk, fall or think here. they're
|
||||
// kept apart from the entity manager's entities, clients hear about players through their own messages
|
||||
export class ServerPlayer extends LivingEntity {
|
||||
readonly type = "bworld:player";
|
||||
readonly conn: number;
|
||||
readonly id = crypto.randomUUID();
|
||||
readonly name: string;
|
||||
|
||||
x = 0;
|
||||
y = 100;
|
||||
z = 0;
|
||||
yaw = 0;
|
||||
pitch = 0;
|
||||
override y = 100;
|
||||
override width = PLAYER_WIDTH;
|
||||
override height = PLAYER_HEIGHT;
|
||||
override eye_height = PLAYER_EYE_HEIGHT;
|
||||
|
||||
inventory = new Container(INVENTORY_SIZE);
|
||||
// hunger and saturation, health is the entity's
|
||||
vitals = new Vitals();
|
||||
// blocks fallen since last standing on something, for fall damage
|
||||
fall_distance = 0;
|
||||
// when the last move came, to tell walking from sprinting
|
||||
last_move_tick = 0;
|
||||
crafting = new Container(CRAFTING_SIZE);
|
||||
@@ -68,17 +73,73 @@ export class ServerPlayer {
|
||||
// the chunks their client has and still needs
|
||||
chunks: PlayerChunks;
|
||||
|
||||
constructor(conn: number, name: string, view_distance: number) {
|
||||
constructor(level: EntityLevel, conn: number, name: string, view_distance: number) {
|
||||
super(level);
|
||||
this.conn = conn;
|
||||
this.name = name;
|
||||
this.chunks = new PlayerChunks(view_distance);
|
||||
}
|
||||
|
||||
override default_attributes(): [AttributeType, number?][] {
|
||||
return [
|
||||
...super.default_attributes(),
|
||||
// a fist does half a heart, like minecraft
|
||||
[ATTRIBUTES.attack_damage, 1],
|
||||
[ATTRIBUTES.physical_shielding],
|
||||
[ATTRIBUTES.arcane_shielding],
|
||||
];
|
||||
}
|
||||
|
||||
// they're saved by name with the players, not with the world's entities
|
||||
override get persistent() {
|
||||
return false;
|
||||
}
|
||||
|
||||
// there's no air bar to show yet, so players don't drown
|
||||
override get can_breathe_underwater() {
|
||||
return true;
|
||||
}
|
||||
|
||||
// their client moves them, this only burns, falls out of the world and counts effects and hurt time down
|
||||
override tick() {
|
||||
this.base_tick();
|
||||
if (!this.dead) {
|
||||
this.tick_effects();
|
||||
}
|
||||
}
|
||||
|
||||
// everything that hurts a player goes through the game server first, which lets mods change or cancel it
|
||||
override hurt(amount: number, source: DamageSource): boolean {
|
||||
return this.level.hurt_player(this, amount, source);
|
||||
}
|
||||
|
||||
// hurts them for real, once mods have had their say
|
||||
apply_hurt(amount: number, source: DamageSource): boolean {
|
||||
return super.hurt(amount, source);
|
||||
}
|
||||
|
||||
override on_hurt(_source: DamageSource) {
|
||||
this.vitals.add_exhaustion(EXHAUSTION.damage);
|
||||
}
|
||||
|
||||
// they don't drop loot or fall over, the game server drops what they carry and respawns them
|
||||
override die(source: DamageSource) {
|
||||
this.level.on_death(this, source);
|
||||
}
|
||||
|
||||
// knockback is how fast their client should move them, sent with the next sync and cleared
|
||||
take_motion(): [number, number, number] | undefined {
|
||||
if (!this.vx && !this.vy && !this.vz) return undefined;
|
||||
const motion: [number, number, number] = [this.vx, this.vy, this.vz];
|
||||
this.vx = this.vy = this.vz = 0;
|
||||
return motion;
|
||||
}
|
||||
|
||||
get held_item(): ItemStack | undefined {
|
||||
return this.inventory.get_item(this.selected_slot);
|
||||
}
|
||||
|
||||
info(): PlayerInfo {
|
||||
player_info(): PlayerInfo {
|
||||
return {
|
||||
id: this.id,
|
||||
name: this.name,
|
||||
@@ -92,7 +153,7 @@ export class ServerPlayer {
|
||||
};
|
||||
}
|
||||
|
||||
save(): SavedPlayer {
|
||||
save_player(): SavedPlayer {
|
||||
return {
|
||||
x: this.x,
|
||||
y: this.y,
|
||||
@@ -101,13 +162,13 @@ export class ServerPlayer {
|
||||
pitch: this.pitch,
|
||||
selected_slot: this.selected_slot,
|
||||
inventory: this.inventory.to_data(),
|
||||
vitals: this.vitals.save(),
|
||||
vitals: { health: this.health, ...this.vitals.save() },
|
||||
skin: this.skin,
|
||||
slim: this.slim || undefined,
|
||||
};
|
||||
}
|
||||
|
||||
load(saved: SavedPlayer) {
|
||||
load_player(saved: SavedPlayer) {
|
||||
this.x = saved.x;
|
||||
this.y = saved.y;
|
||||
this.z = saved.z;
|
||||
@@ -116,6 +177,7 @@ export class ServerPlayer {
|
||||
this.selected_slot = saved.selected_slot;
|
||||
this.inventory.load(saved.inventory);
|
||||
this.vitals.load(saved.vitals);
|
||||
this.health = saved.vitals?.health ?? this.max_health;
|
||||
this.skin = saved.skin;
|
||||
this.slim = saved.slim ?? false;
|
||||
}
|
||||
|
||||
+10
-11
@@ -1,5 +1,5 @@
|
||||
// a player's health, hunger and saturation, with minecraft's rules (its FoodData). the server owns them, clients only
|
||||
// draw them
|
||||
// a player's hunger and saturation, with minecraft's rules (its FoodData), and what they do to health. the server
|
||||
// owns them, clients only draw them
|
||||
import { TICKS_PER_SECOND } from "$/common/constants.ts";
|
||||
|
||||
export const MAX_HEALTH = 20;
|
||||
@@ -37,7 +37,6 @@ export interface SavedVitals {
|
||||
}
|
||||
|
||||
export class Vitals {
|
||||
health = MAX_HEALTH;
|
||||
food = MAX_FOOD;
|
||||
saturation = START_SATURATION;
|
||||
exhaustion = 0;
|
||||
@@ -57,8 +56,9 @@ export class Vitals {
|
||||
return this.food < MAX_FOOD;
|
||||
}
|
||||
|
||||
// one tick. heal and hurt go through the game server, which fires events and handles dying
|
||||
tick(heal: (amount: number) => void, hurt: (amount: number) => void) {
|
||||
// one tick, for someone with that much health. heal and hurt go through the game server, which fires events and
|
||||
// handles dying
|
||||
tick(health: number, heal: (amount: number) => void, hurt: (amount: number) => void) {
|
||||
if (this.exhaustion > EXHAUSTION_PER_POINT) {
|
||||
this.exhaustion -= EXHAUSTION_PER_POINT;
|
||||
if (this.saturation > 0) {
|
||||
@@ -68,7 +68,7 @@ export class Vitals {
|
||||
}
|
||||
}
|
||||
|
||||
const hurt_some = this.health < MAX_HEALTH;
|
||||
const hurt_some = health < MAX_HEALTH;
|
||||
if (this.saturation > 0 && hurt_some && this.food >= MAX_FOOD) {
|
||||
if (++this.#timer >= FAST_REGEN_TICKS) {
|
||||
const amount = Math.min(this.saturation, 6);
|
||||
@@ -84,7 +84,7 @@ export class Vitals {
|
||||
}
|
||||
} else if (this.food <= 0) {
|
||||
if (++this.#timer >= STARVE_TICKS) {
|
||||
if (this.health > STARVE_MIN_HEALTH) hurt(1);
|
||||
if (health > STARVE_MIN_HEALTH) hurt(1);
|
||||
this.#timer = 0;
|
||||
}
|
||||
} else {
|
||||
@@ -93,20 +93,19 @@ export class Vitals {
|
||||
}
|
||||
|
||||
reset() {
|
||||
this.health = MAX_HEALTH;
|
||||
this.food = MAX_FOOD;
|
||||
this.saturation = START_SATURATION;
|
||||
this.exhaustion = 0;
|
||||
this.#timer = 0;
|
||||
}
|
||||
|
||||
save(): SavedVitals {
|
||||
return { health: this.health, food: this.food, saturation: this.saturation, exhaustion: this.exhaustion };
|
||||
// health is the player's, they add it
|
||||
save(): Omit<SavedVitals, "health"> {
|
||||
return { food: this.food, saturation: this.saturation, exhaustion: this.exhaustion };
|
||||
}
|
||||
|
||||
load(saved: SavedVitals | undefined) {
|
||||
if (!saved) return;
|
||||
this.health = saved.health;
|
||||
this.food = saved.food;
|
||||
this.saturation = saved.saturation;
|
||||
this.exhaustion = saved.exhaustion;
|
||||
|
||||
Reference in New Issue
Block a user