Files
bworld/server/game/vitals.ts
T
2026-09-30 10:42:52 -03:00

114 lines
3.3 KiB
TypeScript

// 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;
export const MAX_FOOD = 20;
// what a new or respawned player starts with, like minecraft
export const START_SATURATION = 5;
// every this much exhaustion costs a point of saturation, or of hunger once saturation is gone
const EXHAUSTION_PER_POINT = 4;
const MAX_EXHAUSTION = 40;
// healing from a full stomach: fast while there's saturation, slow while hunger is at least SLOW_REGEN_FOOD
const FAST_REGEN_TICKS = 10;
const SLOW_REGEN_TICKS = 4 * TICKS_PER_SECOND;
const SLOW_REGEN_FOOD = 18;
const STARVE_TICKS = 4 * TICKS_PER_SECOND;
// starving stops here, like minecraft on normal difficulty
const STARVE_MIN_HEALTH = 1;
// how tiring things are, from minecraft
export const EXHAUSTION = {
sprint_per_block: 0.1,
swim_per_block: 0.01,
jump: 0.05,
sprint_jump: 0.2,
break_block: 0.005,
attack: 0.1,
damage: 0.1,
heal_per_health: 6,
};
export interface SavedVitals {
health: number;
food: number;
saturation: number;
exhaustion: number;
}
export class Vitals {
food = MAX_FOOD;
saturation = START_SATURATION;
exhaustion = 0;
#timer = 0;
add_exhaustion(amount: number) {
this.exhaustion = Math.min(this.exhaustion + amount, MAX_EXHAUSTION);
}
// eating: saturation_modifier works like minecraft's, bread's is 0.6. saturation never goes above hunger
eat(nutrition: number, saturation_modifier: number) {
this.food = Math.min(this.food + nutrition, MAX_FOOD);
this.saturation = Math.min(this.saturation + nutrition * saturation_modifier * 2, this.food);
}
can_eat() {
return this.food < MAX_FOOD;
}
// 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) {
this.saturation = Math.max(this.saturation - 1, 0);
} else {
this.food = Math.max(this.food - 1, 0);
}
}
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);
heal(amount / 6);
this.add_exhaustion(amount);
this.#timer = 0;
}
} else if (this.food >= SLOW_REGEN_FOOD && hurt_some) {
if (++this.#timer >= SLOW_REGEN_TICKS) {
heal(1);
this.add_exhaustion(EXHAUSTION.heal_per_health);
this.#timer = 0;
}
} else if (this.food <= 0) {
if (++this.#timer >= STARVE_TICKS) {
if (health > STARVE_MIN_HEALTH) hurt(1);
this.#timer = 0;
}
} else {
this.#timer = 0;
}
}
reset() {
this.food = MAX_FOOD;
this.saturation = START_SATURATION;
this.exhaustion = 0;
this.#timer = 0;
}
// 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.food = saved.food;
this.saturation = saved.saturation;
this.exhaustion = saved.exhaustion;
}
}