Move effects out of cubi
This commit is contained in:
@@ -15,6 +15,7 @@ import type { Season } from "@/common/worldgen/biomes.ts";
|
||||
import { type Collisions, type FluidInCell, physics } from "@/common/physics.ts";
|
||||
import type { EntityData, EntityEvent, EntityInfo } from "@/common/protocol.ts";
|
||||
import type { ServerPlayer } from "../player.ts";
|
||||
import type { LivingEntity } from "./living_entity.ts";
|
||||
|
||||
// below this, entities have fallen out of the world
|
||||
export const VOID_Y = -64;
|
||||
@@ -85,8 +86,11 @@ export interface EntityLevel {
|
||||
// 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): 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;
|
||||
// mods' before.entity_damage, see LivingEntity.hurt: what's left of amount once they had their say, 0 when one
|
||||
// canceled it
|
||||
before_hurt(entity: LivingEntity, amount: number, source: DamageSource): number;
|
||||
// mods' after.entity_damage, it was hurt by amount
|
||||
after_hurt(entity: LivingEntity, amount: number, source: DamageSource): void;
|
||||
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;
|
||||
|
||||
@@ -15,6 +15,7 @@ import type { ServerWorld } from "../world.ts";
|
||||
import type { DamageSource, Entity, EntityLevel, SavedEntity } from "./entity.ts";
|
||||
import { load_entity } from "./entity_types.ts";
|
||||
import { ItemEntity } from "./item_entity.ts";
|
||||
import type { LivingEntity } from "./living_entity.ts";
|
||||
|
||||
// everything the entities need from the game server
|
||||
export interface EntityHost {
|
||||
@@ -22,7 +23,8 @@ export interface EntityHost {
|
||||
readonly current_tick: number;
|
||||
players(): ServerPlayer[];
|
||||
send(player: ServerPlayer, message: ServerMessage): void;
|
||||
hurt_player(player: ServerPlayer, amount: number, source: DamageSource): boolean;
|
||||
before_hurt(entity: LivingEntity, amount: number, source: DamageSource): number;
|
||||
after_hurt(entity: LivingEntity, amount: number, source: DamageSource): void;
|
||||
on_entity_death?(entity: Entity, source: DamageSource): void;
|
||||
// what it drops when it dies, from its loot table
|
||||
entity_loot?(entity: Entity, source: DamageSource): ItemStack[];
|
||||
@@ -136,8 +138,12 @@ export class EntityManager implements EntityLevel {
|
||||
return nearest;
|
||||
}
|
||||
|
||||
hurt_player(player: ServerPlayer, amount: number, source: DamageSource) {
|
||||
return this.#host.hurt_player(player, amount, source);
|
||||
before_hurt(entity: LivingEntity, amount: number, source: DamageSource) {
|
||||
return this.#host.before_hurt(entity, amount, source);
|
||||
}
|
||||
|
||||
after_hurt(entity: LivingEntity, amount: number, source: DamageSource) {
|
||||
this.#host.after_hurt(entity, amount, source);
|
||||
}
|
||||
|
||||
spawn_item(x: number, y: number, z: number, stack: ItemStack, pickup_delay = 0): ItemEntity {
|
||||
|
||||
@@ -31,9 +31,6 @@ const SWIM_UP = 0.04;
|
||||
// a jump can't start more often than this while jump is held
|
||||
const JUMP_DELAY = 10;
|
||||
const KNOCKBACK = 0.4;
|
||||
// the base game's effects that work where what they change happens, like minecraft's: in damage and air
|
||||
const RESISTANCE = "bworld:resistance";
|
||||
const WATER_BREATHING = "bworld:water_breathing";
|
||||
|
||||
export interface SavedLivingEntity extends SavedEntity {
|
||||
health?: number;
|
||||
@@ -141,7 +138,7 @@ export abstract class LivingEntity extends Entity {
|
||||
override base_tick() {
|
||||
super.base_tick();
|
||||
if (this.alive) {
|
||||
if (this.eyes_in_water && !this.can_breathe_underwater && !this.has_effect(WATER_BREATHING)) {
|
||||
if (this.eyes_in_water && !this.can_breathe_underwater) {
|
||||
this.air -= 1;
|
||||
if (this.air <= -20) {
|
||||
this.air = 0;
|
||||
@@ -270,11 +267,14 @@ export abstract class LivingEntity extends Entity {
|
||||
|
||||
// health
|
||||
|
||||
// like minecraft's LivingEntity.hurt. returns whether it was hurt
|
||||
// like minecraft's LivingEntity.hurt. mods can change or cancel it first. returns whether it was hurt
|
||||
override hurt(amount: number, source: DamageSource): boolean {
|
||||
if (this.is_invulnerable_to(source) || this.dead || !(amount > 0)) {
|
||||
return false;
|
||||
}
|
||||
amount = this.level.before_hurt(this, amount, source);
|
||||
if (!(amount > 0)) return false;
|
||||
const taken = amount;
|
||||
let fresh = true;
|
||||
if (this.#hurt_cooldown > HURT_COOLDOWN / 2) {
|
||||
if (amount <= this.#last_hurt_amount) return false;
|
||||
@@ -306,24 +306,20 @@ export abstract class LivingEntity extends Entity {
|
||||
}
|
||||
}
|
||||
this.on_hurt(source);
|
||||
this.level.after_hurt(this, taken, source);
|
||||
if (this.dead) {
|
||||
this.die(source);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// after resistance
|
||||
// after mods and the hurt cooldown
|
||||
actually_hurt(amount: number, source: DamageSource) {
|
||||
// absolute damage, take it and theres no counterplay
|
||||
if (source.type === "absolute") {
|
||||
this.health -= amount;
|
||||
return;
|
||||
}
|
||||
|
||||
const resistance = this.get_effect(RESISTANCE);
|
||||
if (resistance && !source.bypasses_invulnerability) {
|
||||
amount = Math.max(0, amount * (25 - (resistance.amplifier + 1) * 5) / 25);
|
||||
}
|
||||
const shielding = this.attribute(
|
||||
source.type === "physical" ? ATTRIBUTES.physical_shielding : ATTRIBUTES.arcane_shielding,
|
||||
);
|
||||
|
||||
@@ -242,7 +242,10 @@ export class GameServer {
|
||||
},
|
||||
players: () => this.players(),
|
||||
send: (player, message) => this.#send(player, message),
|
||||
hurt_player: (player, amount, source) => this.hurt_player(player, amount, source),
|
||||
before_hurt: (entity, amount, source) =>
|
||||
this.mods.before_damage(entity, entity instanceof ServerPlayer ? entity : undefined, amount, source),
|
||||
after_hurt: (entity, amount, source) =>
|
||||
this.mods.after_damage(entity, entity instanceof ServerPlayer ? entity : undefined, amount, source),
|
||||
on_entity_death: (entity, source) => {
|
||||
if (entity instanceof ServerPlayer) this.#kill(entity, source);
|
||||
},
|
||||
@@ -1959,22 +1962,6 @@ export class GameServer {
|
||||
player.hurt(amount, damage_source(cause));
|
||||
}
|
||||
|
||||
// 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: source.cause, cancel: false };
|
||||
this.mods.before.player_damage.emit(before);
|
||||
const final = Number(before.amount);
|
||||
if (before.cancel || !(final > 0)) {
|
||||
return false;
|
||||
}
|
||||
if (!player.apply_hurt(final, source)) return false;
|
||||
this.mods.after.player_damage.emit({ player: before.player, amount: final, cause: source.cause });
|
||||
return true;
|
||||
}
|
||||
|
||||
heal_player(player: ServerPlayer, amount: number) {
|
||||
player.heal(amount);
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ import type { GameServer } from "./game_server.ts";
|
||||
import { MAX_PRIORITY, MIN_PRIORITY } from "./scheduled_ticks.ts";
|
||||
import type { ServerPlayer } from "./player.ts";
|
||||
import { Menu, type MenuFluidSlot, type MenuSlot } from "./menu.ts";
|
||||
import { damage_source, type Entity } from "./entity/entity.ts";
|
||||
import { damage_source, type DamageSource, type Entity } from "./entity/entity.ts";
|
||||
import type { LivingEntity } from "./entity/living_entity.ts";
|
||||
import type { EffectJson } from "@/common/mod_data.ts";
|
||||
import { create_entity } from "./entity/entity_types.ts";
|
||||
@@ -184,7 +184,7 @@ export class ModRuntime {
|
||||
block_place: new Signal(),
|
||||
block_interact: new Signal(),
|
||||
chat_send: new Signal(),
|
||||
player_damage: new Signal(),
|
||||
entity_damage: new Signal(),
|
||||
};
|
||||
after: After = {
|
||||
block_break: new Signal(),
|
||||
@@ -193,7 +193,7 @@ export class ModRuntime {
|
||||
chat_send: new Signal(),
|
||||
player_join: new Signal(),
|
||||
player_leave: new Signal(),
|
||||
player_damage: new Signal(),
|
||||
entity_damage: new Signal(),
|
||||
player_death: new Signal(),
|
||||
server_start: new Signal(),
|
||||
tick: new Signal(),
|
||||
@@ -624,6 +624,32 @@ export class ModRuntime {
|
||||
}
|
||||
}
|
||||
|
||||
// everything that hurts a player or a mob, see LivingEntity.hurt. what's left of amount once mods had their say, 0
|
||||
// when one canceled it
|
||||
before_damage(entity: LivingEntity, player: ServerPlayer | undefined, amount: number, source: DamageSource) {
|
||||
if (this.before.entity_damage.empty) return amount;
|
||||
const event = { ...this.#damage_event(entity, player, amount, source), cancel: false };
|
||||
this.before.entity_damage.emit(event);
|
||||
const final = Number(event.amount);
|
||||
return event.cancel || !(final > 0) ? 0 : final;
|
||||
}
|
||||
|
||||
after_damage(entity: LivingEntity, player: ServerPlayer | undefined, amount: number, source: DamageSource) {
|
||||
if (!this.after.entity_damage.empty) {
|
||||
this.after.entity_damage.emit(this.#damage_event(entity, player, amount, source));
|
||||
}
|
||||
}
|
||||
|
||||
#damage_event(entity: LivingEntity, player: ServerPlayer | undefined, amount: number, source: DamageSource) {
|
||||
return {
|
||||
entity: living_ref(entity),
|
||||
player: player && this.player(player),
|
||||
amount,
|
||||
cause: source.cause,
|
||||
bypasses_invulnerability: source.bypasses_invulnerability === true,
|
||||
};
|
||||
}
|
||||
|
||||
player(player: ServerPlayer): Player {
|
||||
let api = this.#players.get(player);
|
||||
if (!api) {
|
||||
@@ -1575,10 +1601,7 @@ function player_api(game: GameServer, player: ServerPlayer): Player {
|
||||
}
|
||||
player.add_effect(new EffectInstance(type, duration, { ...options, amplifier }));
|
||||
},
|
||||
effect(id) {
|
||||
const effect = player.get_effect(known_effect(id).id);
|
||||
return effect && { amplifier: effect.amplifier, duration: effect.duration };
|
||||
},
|
||||
effect: (id) => effect_of(player, id),
|
||||
remove_effect: (id) => player.remove_effect(known_effect(id).id),
|
||||
is_swimming: () => player.swimming,
|
||||
is_sneaking: () => player.sneaking,
|
||||
@@ -1611,7 +1634,12 @@ function known_attribute(id: string): AttributeType {
|
||||
return type;
|
||||
}
|
||||
|
||||
function living_ref(entity: LivingEntity): LivingRef {
|
||||
function effect_of(entity: LivingEntity, id: string) {
|
||||
const effect = entity.get_effect(known_effect(id).id);
|
||||
return effect && { amplifier: effect.amplifier, duration: effect.duration };
|
||||
}
|
||||
|
||||
export function living_ref(entity: LivingEntity): LivingRef {
|
||||
return {
|
||||
id: entity.id,
|
||||
get health() {
|
||||
@@ -1620,6 +1648,17 @@ function living_ref(entity: LivingEntity): LivingRef {
|
||||
get max_health() {
|
||||
return entity.max_health;
|
||||
},
|
||||
get air() {
|
||||
return entity.air;
|
||||
},
|
||||
set air(value) {
|
||||
if (typeof value !== "number" || !Number.isFinite(value)) throw new Error(`air must be a number`);
|
||||
entity.air = Math.min(value, entity.max_air);
|
||||
},
|
||||
get max_air() {
|
||||
return entity.max_air;
|
||||
},
|
||||
effect: (id) => effect_of(entity, id),
|
||||
damage(amount, cause = "script") {
|
||||
if (typeof amount !== "number" || !Number.isFinite(amount)) throw new Error(`damage must be a number`);
|
||||
entity.hurt(amount, damage_source(String(cause)));
|
||||
|
||||
@@ -178,16 +178,6 @@ export class ServerPlayer extends LivingEntity {
|
||||
this.vx = this.vy = this.vz = 0;
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user