diff --git a/common/mod_api/server.ts b/common/mod_api/server.ts index 8c70090..ff219a7 100644 --- a/common/mod_api/server.ts +++ b/common/mod_api/server.ts @@ -117,6 +117,10 @@ export interface BlockComponent

{ // an entity is standing on it, every tick the entity moves, like minecraft's updateEntityAfterFallOn: an item // dropped into a basin going in. items lying still move every tick too on_entity_stand?(block: BlockRef, params: P, entity: EntityRef): void; + // an entity's box is in it, every tick the entity moves, like neoforge's entityInside: a block that kills or slows + // what walks into it. players' after each move their client sends. any block, whatever its shape, so one that + // cares where in it the entity is checks the entity's position + on_entity_inside?(block: BlockRef, params: P, entity: EntityRef): void; // what pipes, buckets and other blocks put fluid in and take it out by, reached from face, or from no side in // particular when it's undefined, like neoforge's Capabilities.FluidHandler.BLOCK. undefined when it has none // there. see ServerFluidApi.block_handler @@ -447,6 +451,9 @@ export interface EntityRef { // adds to how fast it's moving, in blocks a second, like minecraft's Entity.push: a fan blowing it along push(x: number, y: number, z: number): void; remove(): void; + // like neoforge's Entity.kill: a player or a mob dies, even when invulnerable, and anything else is removed. cause + // is for events and the death message, like LivingRef.damage's. mods' entity_damage can still cancel it + kill(cause?: string): void; // what an item lying around is, a copy. undefined for every other kind of entity get_item(): ItemStack | undefined; // changes what an item lying around is, like ItemEntity.setItem. undefined or a count of 0 removes it. throws for diff --git a/common/physics.ts b/common/physics.ts index da224b6..732b520 100644 --- a/common/physics.ts +++ b/common/physics.ts @@ -231,6 +231,19 @@ function overlaps_solid(min: number[], max: number[], is_solid: BlockCollision) return false; } +// every block the body's box is in, whatever's there, like minecraft's checkInsideBlocks. a box resting exactly on +// a face isn't in the block on the other side +export function blocks_inside(body: Body, visit: (x: number, y: number, z: number) => void) { + const half = body.width / 2; + for (let x = Math.floor(body.x - half + EPSILON); x <= Math.floor(body.x + half - EPSILON); x++) { + for (let y = Math.floor(body.y + EPSILON); y <= Math.floor(body.y + body.height - EPSILON); y++) { + for (let z = Math.floor(body.z - half + EPSILON); z <= Math.floor(body.z + half - EPSILON); z++) { + visit(x, y, z); + } + } + } +} + // in water like minecraft's wasTouchingWater, close enough: its feet or the middle of its box are in fluid export function in_water(body: Body, is_fluid: (x: number, y: number, z: number) => boolean): boolean { const x = Math.floor(body.x); @@ -390,6 +403,7 @@ export const physics = { move_body, back_off_from_edge, box_is_free, + blocks_inside, in_water, eyes_in_water, fluid_flow, diff --git a/docs/modding/blocks/components.md b/docs/modding/blocks/components.md index 0fd1ca6..cf5aa85 100644 --- a/docs/modding/blocks/components.md +++ b/docs/modding/blocks/components.md @@ -73,6 +73,7 @@ the block and the component. | `on_state_change(block, params, before)` | Its own states change without it being replaced, by `set_state` or a rule. See [Changes](#changes). | | | `on_scheduled_tick(block, params)` | A tick it asked for comes. See [Scheduled Ticks](#scheduled-ticks). | | | `on_entity_stand(block, params, entity)` | An entity standing on it moves, every tick it does. See [Entities on It](#entities-on-it). | | +| `on_entity_inside(block, params, entity)` | An entity whose box is in it moves, every tick it does. See [Entities on It](#entities-on-it). | | | `fluid_handler(block, params, face?)` | Something reaches for its tanks from `face`. See [Fluid Handlers](../resources/fluids.md#fluid-handlers). | a `FluidHandler`, or `undefined` | | `export_data(block, params)` | It goes into a schematic. See [Schematics](../misc/schematics.md#block-data). | its data for the schematic | @@ -98,6 +99,21 @@ ctx.components.register_block("create:basin", { }); ``` +`on_entity_inside` runs every tick an entity moves while its box is in the block, like NeoForge's `entityInside`, for +each block its box is in. Players move on their own clients, so for them it runs after each move their client sends, and +they come as an `EntityRef` of type `cubi:player`: `ctx.players.get(entity.id)` gives the +[`Player`](../entities/players.md). Being against a block's side isn't being in it. It runs whatever the block's shape, +so a block that only cares about part of itself checks the entity's `position`. Dangerous Business's death block kills +whatever walks or falls into it: + +```ts +ctx.components.register_block("dangerous_business:death", { + on_entity_inside(_block, _params, entity) { + entity.kill("death_block"); + }, +}); +``` + ### Changes `on_neighbor_change` is the server's side of the [rule](#neighbors) with the same name, like NeoForge's diff --git a/docs/modding/entities/index.md b/docs/modding/entities/index.md index a29e8d7..73f3de1 100644 --- a/docs/modding/entities/index.md +++ b/docs/modding/entities/index.md @@ -155,6 +155,7 @@ interface EntityRef { teleport(x: number, y: number, z: number): void; push(x: number, y: number, z: number): void; // adds to its velocity remove(): void; + kill(cause?: string): void; // players and mobs die, anything else is removed get_item(): ItemStack | undefined; // for an item lying around, a copy set_item(item: ItemStack | undefined): void; // for an item lying around, undefined takes it away } @@ -163,6 +164,10 @@ interface EntityRef { Once an entity is removed, by a script or anything else, it stays removed, and teleporting it does nothing. Check `removed` before relying on one you kept from earlier. +`kill` is like NeoForge's `Entity.kill`: a player or a mob dies even when it's invulnerable, the way falling out of the +world hurts it, and `cause` goes to `entity_damage` and the death message like [`damage`](players.md)'s does. Mods' +`before.entity_damage` can still cancel it. Anything that can't die, like an item lying around, is removed instead. + `push` adds to how fast it's moving, like Minecraft's `Entity.push`: a fan blowing it along does it every tick. Players have a `push` too, see [Players](players.md#pushing). diff --git a/mods/dangerous_business/blocks/death.json b/mods/dangerous_business/blocks/death.json new file mode 100644 index 0000000..f28faba --- /dev/null +++ b/mods/dangerous_business/blocks/death.json @@ -0,0 +1,15 @@ +{ + "format_version": 1, + "block": { + "id": "dangerous_business:death", + "textures": "cubi:missing", + "model": "dangerous_business:empty", + "item_model": "cubi:cube", + "render_layer": "cutout", + "shape": [[0, 0, 0, 16, 16, 16]], + "collision": false, + "components": { + "dangerous_business:death": {} + } + } +} diff --git a/mods/dangerous_business/models/empty.json b/mods/dangerous_business/models/empty.json new file mode 100644 index 0000000..8befa4f --- /dev/null +++ b/mods/dangerous_business/models/empty.json @@ -0,0 +1,7 @@ +{ + "format_version": 1, + "model": { + "id": "dangerous_business:empty", + "elements": [] + } +} diff --git a/mods/dangerous_business/scripts/main.ts b/mods/dangerous_business/scripts/main.ts index 7aca94c..33fc0e1 100644 --- a/mods/dangerous_business/scripts/main.ts +++ b/mods/dangerous_business/scripts/main.ts @@ -9,6 +9,7 @@ export { shared } from "./shared.ts"; export function server(ctx: ServerContext) { register_facility_door(ctx); register_door(ctx); + register_death(ctx); register_commands(ctx); } @@ -103,6 +104,15 @@ function register_door(ctx: ServerContext) { }); } +// an invisible block that kills whatever walks or falls into it, for the bottoms of pits and the like +function register_death(ctx: ServerContext) { + ctx.components.register_block("dangerous_business:death", { + on_entity_inside(_block, _params, entity) { + entity.kill("death_block"); + }, + }); +} + function register_commands(ctx: ServerContext) { const { literal, argument, types } = ctx.commands; ctx.commands.register( diff --git a/server/game/entity/entity.ts b/server/game/entity/entity.ts index a290193..622bc8f 100644 --- a/server/game/entity/entity.ts +++ b/server/game/entity/entity.ts @@ -104,6 +104,8 @@ export interface EntityLevel { entity_loot(entity: Entity, source: DamageSource): ItemStack[]; // it's standing on the block at x y z after moving, for the block's components, see on_entity_stand stands_on(entity: Entity, x: number, y: number, z: number): void; + // it moved, for the components of the blocks its box is in, see on_entity_inside + inside_blocks(entity: Entity): void; } // an entity as level.json keeps it, see Entity.save. each kind adds its own fields @@ -308,6 +310,7 @@ export abstract class Entity { if (this.on_ground) { this.level.stands_on(this, Math.floor(this.x), Math.floor(this.y - STANDING_DEPTH), Math.floor(this.z)); } + this.level.inside_blocks(this); this.update_fall_distance(this.y - from_y); return collisions; } @@ -362,6 +365,12 @@ export abstract class Entity { this.fire = 0; } + // like minecraft's Entity.kill: ones that can die do, even when invulnerable, the rest go away. cause is for events + // and the death message + kill(_cause: string) { + this.remove(); + } + // takes it out of the world. the entity manager tells clients and forgets it remove() { if (this.removed) return; diff --git a/server/game/entity/entity_manager.ts b/server/game/entity/entity_manager.ts index 246277e..fd9c95f 100644 --- a/server/game/entity/entity_manager.ts +++ b/server/game/entity/entity_manager.ts @@ -30,6 +30,8 @@ export interface EntityHost { entity_loot?(entity: Entity, source: DamageSource): ItemStack[]; // see EntityLevel.stands_on stands_on?(entity: Entity, x: number, y: number, z: number): void; + // see EntityLevel.inside_blocks + inside_blocks?(entity: Entity): void; } // what a client was last told about an entity, so only changes get sent @@ -188,6 +190,10 @@ export class EntityManager implements EntityLevel { this.#host.stands_on?.(entity, x, y, z); } + inside_blocks(entity: Entity) { + this.#host.inside_blocks?.(entity); + } + // ticking // every entity active says should move, with their passengers. entities removed along the way are forgotten diff --git a/server/game/entity/living_entity.ts b/server/game/entity/living_entity.ts index c471e71..7d20569 100644 --- a/server/game/entity/living_entity.ts +++ b/server/game/entity/living_entity.ts @@ -313,6 +313,11 @@ export abstract class LivingEntity extends Entity { return true; } + // mods can still cancel it, like any damage + override kill(cause: string) { + this.hurt(Number.MAX_VALUE, { cause, type: "absolute", bypasses_invulnerability: true }); + } + // after mods and the hurt cooldown actually_hurt(amount: number, source: DamageSource) { // absolute damage, take it and theres no counterplay diff --git a/server/game/game_server.ts b/server/game/game_server.ts index ac3be73..5dd54b2 100644 --- a/server/game/game_server.ts +++ b/server/game/game_server.ts @@ -274,6 +274,7 @@ export class GameServer { ); } }, + inside_blocks: (entity) => this.#inside_blocks(entity), entity_loot: (entity, source) => { const table = EverythingRegistry.get("entities", entity.type)?.loot_table; if (!table) return []; @@ -1153,6 +1154,7 @@ export class GameServer { player.sneaking = message.sneaking === true; this.#broadcast(player_move(player), player); this.#update_view(player); + this.#inside_blocks(player); break; } case "view_distance": @@ -2197,6 +2199,28 @@ export class GameServer { } } + // the components of every block an entity's box is in see it, after it moved. players move on their clients, so + // theirs is after each move they send + #inside_blocks(entity: Entity) { + const { x, y, z } = entity; + physics.blocks_inside(entity, (bx, by, bz) => { + // a block before this one killed it, took it away or moved it somewhere else + if (entity.removed || (entity instanceof LivingEntity && entity.dead)) return; + if (entity.x !== x || entity.y !== y || entity.z !== z) return; + const id = this.world.get_block_id(bx, by, bz); + const components = this.mods.components_with(id, "on_entity_inside"); + if (components.length === 0) return; + const block = this.mods.block_ref(bx, by, bz, id); + for (const { mod, id: component_id, component, params } of components) { + run_guarded( + mod, + `${component_id} on_entity_inside`, + () => component.on_entity_inside!(block, params, entity_ref(entity)), + ); + } + }); + } + // feet on top of something solid, anywhere under the player's box: a thin box just under their feet touches a // block's collision shape. shapes, not whole blocks, so standing on a fence, a slab or a path counts too #on_ground(x: number, y: number, z: number) { diff --git a/server/game/mod_runtime.ts b/server/game/mod_runtime.ts index d20ba43..af592cb 100644 --- a/server/game/mod_runtime.ts +++ b/server/game/mod_runtime.ts @@ -1685,6 +1685,9 @@ export function entity_ref(entity: Entity): EntityRef { remove() { entity.remove(); }, + kill(cause = "script") { + entity.kill(String(cause)); + }, get_item: () => entity instanceof ItemEntity ? item_copy(entity.item) : undefined, set_item(item) { if (!(entity instanceof ItemEntity)) throw new Error(`${entity.type} isn't an item lying around`); diff --git a/tests/dangerous_business_test.ts b/tests/dangerous_business_test.ts index 0f0f484..4134a34 100644 --- a/tests/dangerous_business_test.ts +++ b/tests/dangerous_business_test.ts @@ -2,8 +2,9 @@ // SPDX-License-Identifier: AGPL-3.0-or-later // dangerous business's placement components: slabs and other blocks that go in the top or bottom half, blocks that -// join the same block beside them, and blocks that pick some of their looks when placed -import { assertEquals } from "@std/assert"; +// join the same block beside them, and blocks that pick some of their looks when placed. and the death block +import { assert, assertEquals, assertNotEquals } from "@std/assert"; +import { ItemStack } from "@/common/inventory.ts"; import { get_state_value } from "@/common/utils.ts"; import { copy_dir, test_game } from "./helpers.ts"; @@ -11,6 +12,7 @@ const HALF = "dangerous_business:concrete_wall_slab"; const CONNECTS = "dangerous_business:floor_grate"; const RANDOM_LOOK = "dangerous_business:water_puddle"; const FLOOR = "dangerous_business:concrete_floor"; +const DEATH = "dangerous_business:death"; // alice on a concrete floor at y 100, south of 0 100 0 and looking north, holding each of the blocks. it's a game of // its own, which tests playing mods/ leave out @@ -81,3 +83,26 @@ Deno.test("a block with a random look picks the same one wherever it's placed ag for (const look of first) assertEquals(Number(look.split(" ")[0]) < 3, true); assertEquals(new Set(first).size > 1, true); }); + +Deno.test("the death block kills a player who walks into it, but not one just beside it", async () => { + const { t } = await setup(); + t.game.set_block(0, 101, 1, DEATH); + // touching its side isn't being in it + t.send(1, { type: "move", x: 0.5, y: 100, z: 2.3, yaw: 0, pitch: 0 }); + assert(!t.take(1).some((m) => m.type === "chat" && m.text === "alice died")); + + // its head goes into it + t.send(1, { type: "move", x: 0.5, y: 100, z: 1.9, yaw: 0, pitch: 0 }); + assert(t.take(1).some((m) => m.type === "chat" && m.text === "alice died")); + const player = t.game.players().find((p) => p.name === "alice")!; + assertEquals(player.health, player.max_health); + assertNotEquals([player.x, player.y, player.z], [0.5, 100, 1.9]); +}); + +Deno.test("the death block takes away an item falling into it", async () => { + const { t } = await setup(); + t.game.set_block(2, 100, 0, DEATH); + const item = t.game.spawn_item(2.5, 101.5, 0.5, new ItemStack(FLOOR)); + for (let i = 0; i < 40 && !item.removed; i++) t.game.tick(); + assert(item.removed); +});