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 { 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; // a container screen a mod opened for this player, see ctx.ui.open_container export interface OpenScreen { container: Container; layout: ScreenLayout; // what the bars show, synced when they change properties: Record; // what can go into each slot, checked when the player clicks filters: Map boolean>; // asked before a player takes from an output slot on_take: Map boolean>; on_close: (() => void)[]; } // what's saved about a player between sessions, by name export interface SavedPlayer { x: number; y: number; z: number; yaw: number; pitch: number; selected_slot: number; inventory: (ItemData | null)[]; // players saved before health and hunger start full vitals?: SavedVitals; // base64 png, see /skin skin?: string; slim?: boolean; } // 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 name: string; 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(); // when the last move came, to tell walking from sprinting last_move_tick = 0; crafting = new Container(CRAFTING_SIZE); cursor: Cursor = { item: undefined }; selected_slot = 0; screen: OpenScreen | undefined; // base64 png, see /skin. undefined is the default skin skin: string | undefined; // the skin is for the model with 3 pixel wide arms slim = false; // a /skin download is on its way, one at a time downloading_skin = false; // last json sent for each synced thing, so only changes get sent sent = new Map(); // the chunks their client has and still needs chunks: PlayerChunks; 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); } player_info(): PlayerInfo { return { id: this.id, name: this.name, x: this.x, y: this.y, z: this.z, yaw: this.yaw, pitch: this.pitch, skin: this.skin, slim: this.slim || undefined, }; } save_player(): SavedPlayer { return { x: this.x, y: this.y, z: this.z, yaw: this.yaw, pitch: this.pitch, selected_slot: this.selected_slot, inventory: this.inventory.to_data(), vitals: { health: this.health, ...this.vitals.save() }, skin: this.skin, slim: this.slim || undefined, }; } load_player(saved: SavedPlayer) { this.x = saved.x; this.y = saved.y; this.z = saved.z; this.yaw = saved.yaw; this.pitch = saved.pitch; 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; } }