This commit is contained in:
2026-11-22 13:21:27 -03:00
parent e751d22c6e
commit ac791b886e
23 changed files with 494 additions and 91 deletions
+13 -2
View File
@@ -16,7 +16,9 @@ import { sound_range, sound_to_play } from "@/common/mod_data.ts";
import type { ServerPlayer } from "../player.ts";
import type { ServerWorld } from "../world.ts";
import type { DamageSource, Entity, EntityLevel, SavedEntity } from "./entity.ts";
import { load_entity } from "./entity_types.ts";
import { create_entity, load_entity } from "./entity_types.ts";
import { ObjectEntity } from "./mod_entity.ts";
import { EverythingRegistry, type ItemRegistry } from "@/common/everything_registry.ts";
import { ItemEntity } from "./item_entity.ts";
import { MOBS_PATH_THROUGH } from "./pathfinding.ts";
import type { LivingEntity } from "./living_entity.ts";
@@ -180,7 +182,16 @@ export class EntityManager implements EntityLevel {
this.#host.after_hurt(entity, amount, source);
}
spawn_item(x: number, y: number, z: number, stack: ItemStack, pickup_delay = 0): ItemEntity {
// a cubi:item, or the entity the item lies around as instead, holding it (see ItemRegistry.entity)
spawn_item(x: number, y: number, z: number, stack: ItemStack, pickup_delay = 0): Entity {
const type = EverythingRegistry.get<ItemRegistry>("items", stack.type_id)?.entity;
const object = type === undefined ? undefined : create_entity(this, type);
if (object instanceof ObjectEntity) {
object.item = stack;
object.set_position(x, y, z);
this.add_entity(object);
return object;
}
const entity = new ItemEntity(this, stack, x, y, z, pickup_delay);
this.add_entity(entity);
return entity;
+32 -27
View File
@@ -43,6 +43,36 @@ export interface SavedItemEntity extends SavedEntity {
health?: number;
}
// one tick of falling, sliding and floating like an item lying around, for an entity whose own gravity is 0 (see
// ItemEntity.gravity)
export function move_like_item(entity: Entity) {
// covered by a block placed on it, float out the top instead of being stuck inside. only inside what it bumps
// into, like minecraft's noCollision, so it can lie in a block with a hollow, like a basin
const level = entity.level;
if (!physics.box_is_free(entity, 0, 0, 0, (x, y, z) => level.collision_shape(x, y, z))) {
entity.vy = UNSTICK_SPEED;
}
// like minecraft's ItemEntity.tick, gravity goes on its speed before it moves, not along the way like
// move_body's. the floating numbers are made for that: an item bobbing up out of the water drops straight back
// in, instead of hanging above it a tick longer and falling twice as fast
if (physics.fluid_depth(entity, (x, y, z) => level.fluid_at(x, y, z)) > FLOAT_DEPTH) {
entity.vx *= WATER_DRAG;
entity.vz *= WATER_DRAG;
if (entity.vy < FLOAT_SPEED) entity.vy += FLOAT_ACCELERATION;
} else if (!entity.no_gravity) {
entity.vy += GRAVITY * TICK_DELTA;
}
entity.move();
const friction = entity.on_ground ? GROUND_FRICTION : AIR_DRAG;
entity.vx *= friction;
entity.vy *= AIR_DRAG;
entity.vz *= friction;
if (Math.abs(entity.vx) < MIN_SPEED) entity.vx = 0;
if (Math.abs(entity.vz) < MIN_SPEED) entity.vz = 0;
}
// a stack of items lying in the world, like minecraft's ItemEntity. only the server simulates it,
// clients draw where they're told it is
export class ItemEntity extends Entity {
@@ -51,7 +81,7 @@ export class ItemEntity extends Entity {
override width = ITEM_SIZE;
override height = ITEM_SIZE;
override eye_height = ITEM_SIZE / 2;
// it falls in tick() instead, see there
// it falls in move_like_item instead, see there
override gravity = 0;
// ticks until a player can pick it up
@@ -79,32 +109,7 @@ export class ItemEntity extends Entity {
if (this.pickup_delay > 0) {
this.pickup_delay -= 1;
}
// covered by a block placed on it, float out the top instead of being stuck inside. only inside what it bumps
// into, like minecraft's noCollision, so it can lie in a block with a hollow, like a basin
const level = this.level;
if (!physics.box_is_free(this, 0, 0, 0, (x, y, z) => level.collision_shape(x, y, z))) {
this.vy = UNSTICK_SPEED;
}
// like minecraft's ItemEntity.tick, gravity goes on its speed before it moves, not along the way like
// move_body's. the floating numbers are made for that: an item bobbing up out of the water drops straight back
// in, instead of hanging above it a tick longer and falling twice as fast
if (physics.fluid_depth(this, (x, y, z) => level.fluid_at(x, y, z)) > FLOAT_DEPTH) {
this.vx *= WATER_DRAG;
this.vz *= WATER_DRAG;
if (this.vy < FLOAT_SPEED) this.vy += FLOAT_ACCELERATION;
} else if (!this.no_gravity) {
this.vy += GRAVITY * TICK_DELTA;
}
this.move();
const friction = this.on_ground ? GROUND_FRICTION : AIR_DRAG;
this.vx *= friction;
this.vy *= AIR_DRAG;
this.vz *= friction;
if (Math.abs(this.vx) < MIN_SPEED) this.vx = 0;
if (Math.abs(this.vz) < MIN_SPEED) this.vz = 0;
move_like_item(this);
if (this.age >= DESPAWN_TICKS) {
this.remove();
+56 -3
View File
@@ -2,11 +2,14 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
// the kinds of entity mods define in entities/<name>.json (see EntityJson): animals that wander around, panic when hurt
// and look at players now and then, like minecraft's simpler animals, monsters that go after players, and scripted
// ones that stay where scripts put them
// and look at players now and then, like minecraft's simpler animals, monsters that go after players, scripted ones
// that stay where scripts put them, and objects that lie around holding an item
import { type ItemData, ItemStack } from "@/common/inventory.ts";
import { entity_category, type EntityJson } from "@/common/mod_data.ts";
import type { EntityData } from "@/common/protocol.ts";
import { ATTRIBUTES, type AttributeType } from "./attributes.ts";
import { Entity } from "./entity.ts";
import { Entity, type SavedEntity } from "./entity.ts";
import { move_like_item } from "./item_entity.ts";
import type { EntityFactory } from "./entity_types.ts";
import {
FloatGoal,
@@ -30,6 +33,9 @@ export function mod_entity_factory(
if (json.behavior === "scripted") {
return (level) => new ScriptedEntity(level, json);
}
if (json.behavior === "object") {
return (level) => new ObjectEntity(level, json);
}
const monster = json.behavior === "monster";
const behavior = json.behavior?.includes(":") ? json.behavior : undefined;
@@ -110,3 +116,50 @@ class ScriptedEntity extends Entity {
return false;
}
}
// a thing lying around, holding an item, like neoforge's custom item entities (see ItemRegistry.entity): it falls,
// slides and floats like a dropped item, but nobody picks it up by walking into it, it never despawns or merges, and it
// can't be hurt. what happens when it's clicked is up to mods
export class ObjectEntity extends Entity {
readonly type: string;
// it falls in move_like_item instead
override gravity = 0;
item: ItemStack | undefined;
constructor(level: Entity["level"], json: EntityJson) {
super(level);
this.type = json.id;
this.width = json.width;
this.height = json.height;
this.eye_height = json.height * 0.85;
}
override tick() {
this.base_tick();
if (this.removed || this.vehicle) return;
move_like_item(this);
}
// it isn't hurt by falling either
override update_fall_distance() {}
override synced_data(): EntityData {
const data = super.synced_data();
if (this.item) data.item = this.item.to_synced_data();
return data;
}
override save_additional(saved: SavedObjectEntity) {
super.save_additional(saved);
if (this.item) saved.item = this.item.to_data();
}
override load_additional(saved: SavedObjectEntity) {
super.load_additional(saved);
this.item = saved.item ? ItemStack.from_data(saved.item) : undefined;
}
}
interface SavedObjectEntity extends SavedEntity {
item?: ItemData;
}
+22 -2
View File
@@ -549,7 +549,8 @@ export class GameServer {
}
}
spawn_item(x: number, y: number, z: number, stack: ItemStack, pickup_delay = 0): ItemEntity {
// a cubi:item, or the entity the item lies around as, see EntityManager.spawn_item
spawn_item(x: number, y: number, z: number, stack: ItemStack, pickup_delay = 0): Entity {
const entity = this.entities.spawn_item(x, y, z, stack, pickup_delay);
this.world.dirty = true;
return entity;
@@ -588,6 +589,8 @@ export class GameServer {
entity.vx = -Math.sin(player.yaw) * cos_pitch * speed + Math.cos(spread_angle) * spread;
entity.vy = Math.sin(player.pitch) * speed + (0.1 + (Math.random() - Math.random()) * 0.1) * TICKS_PER_SECOND;
entity.vz = -Math.cos(player.yaw) * cos_pitch * speed + Math.sin(spread_angle) * spread;
// one drawn with a model lands facing the way they threw it
entity.yaw = player.yaw;
return entity;
}
@@ -1224,6 +1227,10 @@ export class GameServer {
this.#attack_entity(player, message.id);
}
break;
case "interact_entity":
// a client that hadn't heard it's using an item yet, still clicking
if (typeof message.id === "string" && !player.using) this.#interact_entity(player, message.id);
break;
case "swing":
player.attack_strength_ticker = 0;
break;
@@ -1380,6 +1387,19 @@ export class GameServer {
}
// right click at nothing: eating, or the item's on_use
// a right click on an entity, like minecraft's ServerboundInteractPacket: mods' entity_interact, then the held item
// is used like on nothing, unless one of them stopped it. out of reach, or gone already, it's only that use
#interact_entity(player: ServerPlayer, id: string) {
const entity = this.entities.get_entity(id);
if (entity && !entity.removed && player.alive && this.#entity_in_reach(player, entity)) {
const player_api = this.mods.player(player);
const event = { player: player_api, entity: entity_ref(entity), item: player_api.held_item, cancel: false };
this.mods.before.entity_interact.emit(event);
if (event.cancel) return;
}
this.#use_item(player);
}
#use_item(player: ServerPlayer): boolean {
return this.#eat(player) || this.#use_item_components(player, undefined);
}
@@ -2639,7 +2659,7 @@ export class GameServer {
}
// from the eyes to the closest point of its box
#entity_in_reach(player: ServerPlayer, entity: LivingEntity) {
#entity_in_reach(player: ServerPlayer, entity: Entity) {
const eye_y = player.y + player.eye_height;
const half = entity.width / 2;
const dx = Math.max(entity.x - half - player.x, 0, player.x - entity.x - half);
+10 -1
View File
@@ -92,6 +92,7 @@ import { Mob } from "./entity/mob.ts";
import { BUILTIN_GOALS, mob_goals } from "./mob_behaviors.ts";
import { create_entity } from "./entity/entity_types.ts";
import { BLOCK_DROP_PICKUP_DELAY, ItemEntity } from "./entity/item_entity.ts";
import { ObjectEntity } from "./entity/mod_entity.ts";
import { attribute_type, type AttributeType, MODIFIER_OPERATIONS } from "./entity/attributes.ts";
import { effect_type, EffectInstance, type EffectType } from "./entity/effects.ts";
import { biome_season } from "@/common/worldgen/biomes.ts";
@@ -192,6 +193,7 @@ export class ModRuntime {
block_break: new Signal(),
block_place: new Signal(),
block_interact: new Signal(),
entity_interact: new Signal(),
chat_send: new Signal(),
entity_damage: new Signal(),
};
@@ -1853,8 +1855,15 @@ export function entity_ref(entity: Entity): EntityRef {
kill(cause = "script") {
entity.kill(String(cause));
},
get_item: () => entity instanceof ItemEntity ? item_copy(entity.item) : undefined,
get_item() {
const item = entity instanceof ItemEntity || entity instanceof ObjectEntity ? entity.item : undefined;
return item ? item_copy(item) : undefined;
},
set_item(item) {
if (entity instanceof ObjectEntity) {
if (!entity.removed) entity.item = item && item.count > 0 ? new_stack(item) : undefined;
return;
}
if (!(entity instanceof ItemEntity)) throw new Error(`${entity.type} isn't an item lying around`);
if (entity.removed) return;
if (!item || item.count <= 0) entity.remove();