BlueGit

timfa questward

Include dimensionId on mob entity-despawn events.

Without it, interest after destroy fell back to overworld, so clients in
other dimensions never received the despawn and corpses stayed visible.

timfa Tim Falken · pushed by timfa

Authored 2026-09-13 · 2 months ago

Hash
dac1a92c9d9053f768cbbdd24e1e236597b9d372
Parent
80d76649
5 files changed +62 −6
server/src/combat/service.js +1 −0
@@ -2568,6 +2568,7 @@ export function finalizeMobKill (world, attackerId, targetId, targetHandle, tile
2568 2568 entityId: targetId,
2569 2569 tileX: view.tileX,
2570 2570 tileY: view.tileY,
2571 dimensionId: view.dimensionId,
2571 2572 cause: "death"
2572 2573 });
2573 2574 }
@@ -2568,6 +2568,7 @@ export function finalizeMobKill (world, attackerId, targetId, targetHandle, tile
2568 entityId: targetId,
2568 entityId: targetId,
2569 tileX: view.tileX,
2569 tileX: view.tileX,
2570 tileY: view.tileY,
2570 tileY: view.tileY,
2571 dimensionId: view.dimensionId,
2571 cause: "death"
2572 cause: "death"
2572 });
2573 });
2573 }
2574 }
server/src/ecs/serialize.js +2 −1
@@ -225,6 +225,7 @@ export function replicationDespawn (view)
225 225 type: "entity-despawn",
226 226 entityId: view.entityId,
227 227 tileX: view.tileX,
228 tileY: view.tileY
228 tileY: view.tileY,
229 dimensionId: view.dimensionId
229 230 };
230 231 }
@@ -225,6 +225,7 @@ export function replicationDespawn (view)
225 type: "entity-despawn",
225 type: "entity-despawn",
226 entityId: view.entityId,
226 entityId: view.entityId,
227 tileX: view.tileX,
227 tileX: view.tileX,
228 tileY: view.tileY
228 tileY: view.tileY,
229 dimensionId: view.dimensionId
229 };
230 };
230}
231}
server/src/simulation/workerWorld.js +18 −2
@@ -1578,7 +1578,15 @@ export class WorkerWorld
1578 1578 {
1579 1579 const dropped = this.#destroyEntity(entityId);
1580 1580 if (dropped)
1581 { events.push({ type: "entity-despawn", entityId }); }
1581 {
1582 events.push({
1583 type: "entity-despawn",
1584 entityId,
1585 tileX: dropped.tileX,
1586 tileY: dropped.tileY,
1587 dimensionId: dropped.dimensionId
1588 });
1589 }
1582 1590 }
1583 1591 const fairies = stepJadeiteFairies(this, now);
1584 1592 events.push(...(fairies.events ?? []));
@@ -1586,7 +1594,15 @@ export class WorkerWorld
1586 1594 {
1587 1595 const dropped = this.#destroyEntity(entityId);
1588 1596 if (dropped)
1589 { events.push({ type: "entity-despawn", entityId }); }
1597 {
1598 events.push({
1599 type: "entity-despawn",
1600 entityId,
1601 tileX: dropped.tileX,
1602 tileY: dropped.tileY,
1603 dimensionId: dropped.dimensionId
1604 });
1605 }
1590 1606 }
1591 1607 events.push(...stepJewelryTeleports(this, now).events);
1592 1608 stepFollow(this);
@@ -1578,7 +1578,15 @@ export class WorkerWorld
1578 {
1578 {
1579 const dropped = this.#destroyEntity(entityId);
1579 const dropped = this.#destroyEntity(entityId);
1580 if (dropped)
1580 if (dropped)
1581 { events.push({ type: "entity-despawn", entityId }); }
1581 {
1582 events.push({
1583 type: "entity-despawn",
1584 entityId,
1585 tileX: dropped.tileX,
1586 tileY: dropped.tileY,
1587 dimensionId: dropped.dimensionId
1588 });
1589 }
1582 }
1590 }
1583 const fairies = stepJadeiteFairies(this, now);
1591 const fairies = stepJadeiteFairies(this, now);
1584 events.push(...(fairies.events ?? []));
1592 events.push(...(fairies.events ?? []));
@@ -1586,7 +1594,15 @@ export class WorkerWorld
1586 {
1594 {
1587 const dropped = this.#destroyEntity(entityId);
1595 const dropped = this.#destroyEntity(entityId);
1588 if (dropped)
1596 if (dropped)
1589 { events.push({ type: "entity-despawn", entityId }); }
1597 {
1598 events.push({
1599 type: "entity-despawn",
1600 entityId,
1601 tileX: dropped.tileX,
1602 tileY: dropped.tileY,
1603 dimensionId: dropped.dimensionId
1604 });
1605 }
1590 }
1606 }
1591 events.push(...stepJewelryTeleports(this, now).events);
1607 events.push(...stepJewelryTeleports(this, now).events);
1592 stepFollow(this);
1608 stepFollow(this);
server/tests/death.test.js +4 −2
@@ -391,11 +391,13 @@ test("mob kill entity-despawn is marked as combat death", () =>
391 391 const health = worker.world.ecs.get(handle, Component.health);
392 392 health.current = 1;
393 393 const events = applyDamageAndKill(worker.world, playerId, ratId, 1, harness.now());
394 assert.ok(events.some((event) =>
394 const despawn = events.find((event) =>
395 395 event.type === "entity-despawn"
396 396 && event.entityId === ratId
397 397 && event.cause === "death"
398 ));
398 );
399 assert.ok(despawn);
400 assert.ok(despawn.dimensionId);
399 401 }
400 402 finally
401 403 { harness.close(); }
@@ -391,11 +391,13 @@ test("mob kill entity-despawn is marked as combat death", () =>
391 const health = worker.world.ecs.get(handle, Component.health);
391 const health = worker.world.ecs.get(handle, Component.health);
392 health.current = 1;
392 health.current = 1;
393 const events = applyDamageAndKill(worker.world, playerId, ratId, 1, harness.now());
393 const events = applyDamageAndKill(worker.world, playerId, ratId, 1, harness.now());
394 assert.ok(events.some((event) =>
394 const despawn = events.find((event) =>
395 event.type === "entity-despawn"
395 event.type === "entity-despawn"
396 && event.entityId === ratId
396 && event.entityId === ratId
397 && event.cause === "death"
397 && event.cause === "death"
398 ));
398 );
399 assert.ok(despawn);
400 assert.ok(despawn.dimensionId);
399 }
401 }
400 finally
402 finally
401 { harness.close(); }
403 { harness.close(); }
server/tests/dimension-interactions.test.js +37 −1
@@ -12,8 +12,10 @@ import { StateDatabase } from "../src/persistence/stateDb.js";
12 12 import { Simulation } from "../src/simulation/simulation.js";
13 13 import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js";
14 14 import { mobComponents } from "../src/simulation/spawners.js";
15 import { engageTarget, ensureMobCombat, stepCombat, stepAggression } from "../src/combat/service.js";
15 import { engageTarget, ensureMobCombat, stepCombat, stepAggression, applyDamageAndKill } from "../src/combat/service.js";
16 16 import { Component } from "../src/ecs/components.js";
17 import { eventInterestKeys } from "../src/gateway/eventInterest.js";
18 import { chunkKey, tileToChunk } from "../src/world/coordinates.js";
17 19 import { makeCharacter, openChunkStrip, writeOpenChunk } from "./helpers/simulationHarness.js";
18 20
19 21 const CAVE_ID = "test-cave";
@@ -147,3 +149,37 @@ test("melee combat ticks in a non-overworld dimension", () =>
147 149 finally
148 150 { harness.close(); }
149 151 });
152
153 test("mob kill entity-despawn carries cave dimensionId for interest", () =>
154 {
155 const harness = makeCaveHarness();
156 try
157 {
158 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
159 harness.stateDb.saveCharacterPosition(character.character_id, 8, 8, "walk", CAVE_ID);
160 const playerId = harness.simulation.spawnCharacter(1, character);
161 const ratId = harness.simulation.spawnEntity(mobComponents({
162 tileX: 8,
163 tileY: 9,
164 mobTypeId: MobId.rat,
165 spawnerId: null,
166 dimensionId: CAVE_ID
167 }));
168 const worker = workerOf(harness.simulation, ratId);
169 const handle = worker.world.ecs.handleOf(ratId);
170 worker.world.ecs.get(handle, Component.health).current = 1;
171 const events = applyDamageAndKill(worker.world, playerId, ratId, 1, harness.now());
172 const despawn = events.find((event) =>
173 event.type === "entity-despawn" && event.entityId === ratId && event.cause === "death"
174 );
175 assert.ok(despawn, "expected combat death despawn");
176 assert.equal(despawn.dimensionId, CAVE_ID);
177 // Entity is already gone — interest must not fall back to overworld.
178 const keys = eventInterestKeys(despawn, () => null);
179 assert.equal(keys.dimensionId, CAVE_ID);
180 assert.equal(keys.toKey, chunkKey(tileToChunk(8), tileToChunk(9), CAVE_ID));
181 assert.notEqual(keys.toKey, chunkKey(tileToChunk(8), tileToChunk(9), OVERWORLD_ID));
182 }
183 finally
184 { harness.close(); }
185 });
@@ -12,8 +12,10 @@ import { StateDatabase } from "../src/persistence/stateDb.js";
12import { Simulation } from "../src/simulation/simulation.js";
12import { Simulation } from "../src/simulation/simulation.js";
13import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js";
13import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js";
14import { mobComponents } from "../src/simulation/spawners.js";
14import { mobComponents } from "../src/simulation/spawners.js";
15import { engageTarget, ensureMobCombat, stepCombat, stepAggression } from "../src/combat/service.js";
15import { engageTarget, ensureMobCombat, stepCombat, stepAggression, applyDamageAndKill } from "../src/combat/service.js";
16import { Component } from "../src/ecs/components.js";
16import { Component } from "../src/ecs/components.js";
17import { eventInterestKeys } from "../src/gateway/eventInterest.js";
18import { chunkKey, tileToChunk } from "../src/world/coordinates.js";
17import { makeCharacter, openChunkStrip, writeOpenChunk } from "./helpers/simulationHarness.js";
19import { makeCharacter, openChunkStrip, writeOpenChunk } from "./helpers/simulationHarness.js";
18
20
19const CAVE_ID = "test-cave";
21const CAVE_ID = "test-cave";
@@ -147,3 +149,37 @@ test("melee combat ticks in a non-overworld dimension", () =>
147 finally
149 finally
148 { harness.close(); }
150 { harness.close(); }
149});
151});
152
153test("mob kill entity-despawn carries cave dimensionId for interest", () =>
154{
155 const harness = makeCaveHarness();
156 try
157 {
158 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
159 harness.stateDb.saveCharacterPosition(character.character_id, 8, 8, "walk", CAVE_ID);
160 const playerId = harness.simulation.spawnCharacter(1, character);
161 const ratId = harness.simulation.spawnEntity(mobComponents({
162 tileX: 8,
163 tileY: 9,
164 mobTypeId: MobId.rat,
165 spawnerId: null,
166 dimensionId: CAVE_ID
167 }));
168 const worker = workerOf(harness.simulation, ratId);
169 const handle = worker.world.ecs.handleOf(ratId);
170 worker.world.ecs.get(handle, Component.health).current = 1;
171 const events = applyDamageAndKill(worker.world, playerId, ratId, 1, harness.now());
172 const despawn = events.find((event) =>
173 event.type === "entity-despawn" && event.entityId === ratId && event.cause === "death"
174 );
175 assert.ok(despawn, "expected combat death despawn");
176 assert.equal(despawn.dimensionId, CAVE_ID);
177 // Entity is already gone — interest must not fall back to overworld.
178 const keys = eventInterestKeys(despawn, () => null);
179 assert.equal(keys.dimensionId, CAVE_ID);
180 assert.equal(keys.toKey, chunkKey(tileToChunk(8), tileToChunk(9), CAVE_ID));
181 assert.notEqual(keys.toKey, chunkKey(tileToChunk(8), tileToChunk(9), OVERWORLD_ID));
182 }
183 finally
184 { harness.close(); }
185});