Make players mobs too !
This commit is contained in:
+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":
|
||||
|
||||
Reference in New Issue
Block a user