BlueGit

timfa questward

Silence dead players: no combat, casts, targeting, or remote body.

Death clears engage/path/auto-cast, combat and spell ticks skip corpses, other clients get an entity despawn (respawn re-spawns), and commands stay rejected until respawn.

timfa Tim Falken · pushed by timfa

Authored 2026-08-31 · 2 months ago

Hash
e8c80cfb32abebaf50dfb8a91a6ad6ca8f1f2bea
Parent
e2b8ded7
7 files changed +193 −7
client/src/play/main.js +2 −0
@@ -2015,7 +2015,9 @@ async function main ()
2015 2015 break;
2016 2016 case MessageType.playerDied:
2017 2017 clearAllPendingActions();
2018 clearAutorunFocus();
2018 2019 contextMenu?.hide();
2020 spells?.handleSpellDisarmed?.();
2019 2021 death?.show();
2020 2022 if (message.entityId === view?.localEntityId)
2021 2023 {
@@ -2015,7 +2015,9 @@ async function main ()
2015 break;
2015 break;
2016 case MessageType.playerDied:
2016 case MessageType.playerDied:
2017 clearAllPendingActions();
2017 clearAllPendingActions();
2018 clearAutorunFocus();
2018 contextMenu?.hide();
2019 contextMenu?.hide();
2020 spells?.handleSpellDisarmed?.();
2019 death?.show();
2021 death?.show();
2020 if (message.entityId === view?.localEntityId)
2022 if (message.entityId === view?.localEntityId)
2021 {
2023 {
server/src/combat/service.js +45 −0
@@ -155,17 +155,55 @@ function playerCombatLevels (ecs, handle, profile, settings = null)
155 155
156 156 export function isUntargetable (ecs, handle)
157 157 {
158 if (ecs.has(handle, Component.dead))
159 { return true; }
158 160 const creature = ecs.get(handle, Component.creature);
159 161 if (!creature)
160 162 { return false; }
161 163 return !!mobDefinition(creature.mobTypeId)?.untargetable;
162 164 }
163 165
166 /** Stop all combat / cast / path activity when a player dies. */
167 export function silenceDeadPlayer (ecs, handle)
168 {
169 if (handle == null)
170 { return; }
171 const entityId = ecs.entityIdOf(handle);
172 if (entityId != null)
173 { clearEngage(ecs, entityId); }
174 const path = ecs.get(handle, Component.path);
175 if (path)
176 { path.tiles = []; }
177 const follow = ecs.get(handle, Component.follow);
178 if (follow)
179 {
180 follow.targetEntityId = null;
181 follow.trail = [];
182 }
183 const casting = ecs.get(handle, Component.spellcasting);
184 if (casting)
185 {
186 casting.repeatSpellId = null;
187 casting.repeatTargetEntityId = null;
188 casting.repeatTileX = null;
189 casting.repeatTileY = null;
190 casting.pendingCast = null;
191 }
192 const spells = ecs.get(handle, Component.spells);
193 if (spells)
194 { spells.autoCast = false; }
195 }
196
164 197 export function engageTarget (ecs, attackerId, targetId, now = Date.now())
165 198 {
166 199 const handle = ecs.handleOf(attackerId);
167 200 if (handle == null)
168 201 { return false; }
202 if (ecs.has(handle, Component.dead))
203 { return false; }
204 const targetHandle = ecs.handleOf(targetId);
205 if (targetHandle != null && ecs.has(targetHandle, Component.dead))
206 { return false; }
169 207 // Spell auto-cast / pending cast is already the active combat form.
170 208 if (isSpellCombatBusy(ecs, handle))
171 209 { return false; }
@@ -573,6 +611,11 @@ export function stepCombat (world, now = Date.now())
573 611 const events = [];
574 612 for (const entity of world.ecs.query(Component.combat, Component.position))
575 613 {
614 if (world.ecs.has(entity.handle, Component.dead))
615 {
616 entity.combat.targetEntityId = null;
617 continue;
618 }
576 619 if (!world.chunkIsSimulating?.(entity.position.tileX, entity.position.tileY))
577 620 { continue; }
578 621 const combat = entity.combat;
@@ -1715,6 +1758,7 @@ function applyLocalMeleeHit (
1715 1758 follow.trail = [];
1716 1759 }
1717 1760 world.ecs.add(targetHandle, Component.dead, {});
1761 silenceDeadPlayer(world.ecs, targetHandle);
1718 1762 const identity = world.ecs.get(targetHandle, Component.networkIdentity);
1719 1763 events.push({
1720 1764 type: "player-died",
@@ -1861,6 +1905,7 @@ export function applyDamageAndKill (world, attackerId, targetId, damage, now = D
1861 1905 if (path)
1862 1906 { path.tiles = []; }
1863 1907 world.ecs.add(targetHandle, Component.dead, {});
1908 silenceDeadPlayer(world.ecs, targetHandle);
1864 1909 const identity = world.ecs.get(targetHandle, Component.networkIdentity);
1865 1910 events.push({
1866 1911 type: "player-died",
@@ -155,17 +155,55 @@ function playerCombatLevels (ecs, handle, profile, settings = null)
155
155
156export function isUntargetable (ecs, handle)
156export function isUntargetable (ecs, handle)
157{
157{
158 if (ecs.has(handle, Component.dead))
159 { return true; }
158 const creature = ecs.get(handle, Component.creature);
160 const creature = ecs.get(handle, Component.creature);
159 if (!creature)
161 if (!creature)
160 { return false; }
162 { return false; }
161 return !!mobDefinition(creature.mobTypeId)?.untargetable;
163 return !!mobDefinition(creature.mobTypeId)?.untargetable;
162}
164}
163
165
166/** Stop all combat / cast / path activity when a player dies. */
167export function silenceDeadPlayer (ecs, handle)
168{
169 if (handle == null)
170 { return; }
171 const entityId = ecs.entityIdOf(handle);
172 if (entityId != null)
173 { clearEngage(ecs, entityId); }
174 const path = ecs.get(handle, Component.path);
175 if (path)
176 { path.tiles = []; }
177 const follow = ecs.get(handle, Component.follow);
178 if (follow)
179 {
180 follow.targetEntityId = null;
181 follow.trail = [];
182 }
183 const casting = ecs.get(handle, Component.spellcasting);
184 if (casting)
185 {
186 casting.repeatSpellId = null;
187 casting.repeatTargetEntityId = null;
188 casting.repeatTileX = null;
189 casting.repeatTileY = null;
190 casting.pendingCast = null;
191 }
192 const spells = ecs.get(handle, Component.spells);
193 if (spells)
194 { spells.autoCast = false; }
195}
196
164export function engageTarget (ecs, attackerId, targetId, now = Date.now())
197export function engageTarget (ecs, attackerId, targetId, now = Date.now())
165{
198{
166 const handle = ecs.handleOf(attackerId);
199 const handle = ecs.handleOf(attackerId);
167 if (handle == null)
200 if (handle == null)
168 { return false; }
201 { return false; }
202 if (ecs.has(handle, Component.dead))
203 { return false; }
204 const targetHandle = ecs.handleOf(targetId);
205 if (targetHandle != null && ecs.has(targetHandle, Component.dead))
206 { return false; }
169 // Spell auto-cast / pending cast is already the active combat form.
207 // Spell auto-cast / pending cast is already the active combat form.
170 if (isSpellCombatBusy(ecs, handle))
208 if (isSpellCombatBusy(ecs, handle))
171 { return false; }
209 { return false; }
@@ -573,6 +611,11 @@ export function stepCombat (world, now = Date.now())
573 const events = [];
611 const events = [];
574 for (const entity of world.ecs.query(Component.combat, Component.position))
612 for (const entity of world.ecs.query(Component.combat, Component.position))
575 {
613 {
614 if (world.ecs.has(entity.handle, Component.dead))
615 {
616 entity.combat.targetEntityId = null;
617 continue;
618 }
576 if (!world.chunkIsSimulating?.(entity.position.tileX, entity.position.tileY))
619 if (!world.chunkIsSimulating?.(entity.position.tileX, entity.position.tileY))
577 { continue; }
620 { continue; }
578 const combat = entity.combat;
621 const combat = entity.combat;
@@ -1715,6 +1758,7 @@ function applyLocalMeleeHit (
1715 follow.trail = [];
1758 follow.trail = [];
1716 }
1759 }
1717 world.ecs.add(targetHandle, Component.dead, {});
1760 world.ecs.add(targetHandle, Component.dead, {});
1761 silenceDeadPlayer(world.ecs, targetHandle);
1718 const identity = world.ecs.get(targetHandle, Component.networkIdentity);
1762 const identity = world.ecs.get(targetHandle, Component.networkIdentity);
1719 events.push({
1763 events.push({
1720 type: "player-died",
1764 type: "player-died",
@@ -1861,6 +1905,7 @@ export function applyDamageAndKill (world, attackerId, targetId, damage, now = D
1861 if (path)
1905 if (path)
1862 { path.tiles = []; }
1906 { path.tiles = []; }
1863 world.ecs.add(targetHandle, Component.dead, {});
1907 world.ecs.add(targetHandle, Component.dead, {});
1908 silenceDeadPlayer(world.ecs, targetHandle);
1864 const identity = world.ecs.get(targetHandle, Component.networkIdentity);
1909 const identity = world.ecs.get(targetHandle, Component.networkIdentity);
1865 events.push({
1910 events.push({
1866 type: "player-died",
1911 type: "player-died",
server/src/ecs/serialize.js +6 −5
@@ -98,12 +98,13 @@ export function publicEntityView (ecs, entityId)
98 98 accountId: character?.accountId ?? null,
99 99 mobTypeId: creature?.mobTypeId ?? null,
100 100 element: creature?.element ?? null,
101 canFight: mob ? mobCanFight(mob) : !!character,
101 canFight: mob ? mobCanFight(mob) : (!!character && !ecs.has(handle, Component.dead)),
102 102 hostile: !!mob?.hostile,
103 talkable: !!mob?.talkable || shopHasInterface(ecs.get(handle, Component.shop)),
104 hasDialogue: !!(ecs.get(handle, Component.spawnOrigin)?.dialogueId),
105 hasShop: shopHasInterface(ecs.get(handle, Component.shop)),
106 untargetable: !!mob?.untargetable,
103 talkable: (!!mob?.talkable || shopHasInterface(ecs.get(handle, Component.shop))) && !ecs.has(handle, Component.dead),
104 hasDialogue: !!(ecs.get(handle, Component.spawnOrigin)?.dialogueId) && !ecs.has(handle, Component.dead),
105 hasShop: shopHasInterface(ecs.get(handle, Component.shop)) && !ecs.has(handle, Component.dead),
106 untargetable: !!mob?.untargetable || ecs.has(handle, Component.dead),
107 dead: ecs.has(handle, Component.dead),
107 108 freeFlight: !!mob?.freeFlight,
108 109 level: creature?.level ?? null,
109 110 health: health ? health.current : undefined,
@@ -98,12 +98,13 @@ export function publicEntityView (ecs, entityId)
98 accountId: character?.accountId ?? null,
98 accountId: character?.accountId ?? null,
99 mobTypeId: creature?.mobTypeId ?? null,
99 mobTypeId: creature?.mobTypeId ?? null,
100 element: creature?.element ?? null,
100 element: creature?.element ?? null,
101 canFight: mob ? mobCanFight(mob) : !!character,
101 canFight: mob ? mobCanFight(mob) : (!!character && !ecs.has(handle, Component.dead)),
102 hostile: !!mob?.hostile,
102 hostile: !!mob?.hostile,
103 talkable: !!mob?.talkable || shopHasInterface(ecs.get(handle, Component.shop)),
103 talkable: (!!mob?.talkable || shopHasInterface(ecs.get(handle, Component.shop))) && !ecs.has(handle, Component.dead),
104 hasDialogue: !!(ecs.get(handle, Component.spawnOrigin)?.dialogueId),
104 hasDialogue: !!(ecs.get(handle, Component.spawnOrigin)?.dialogueId) && !ecs.has(handle, Component.dead),
105 hasShop: shopHasInterface(ecs.get(handle, Component.shop)),
105 hasShop: shopHasInterface(ecs.get(handle, Component.shop)) && !ecs.has(handle, Component.dead),
106 untargetable: !!mob?.untargetable,
106 untargetable: !!mob?.untargetable || ecs.has(handle, Component.dead),
107 dead: ecs.has(handle, Component.dead),
107 freeFlight: !!mob?.freeFlight,
108 freeFlight: !!mob?.freeFlight,
108 level: creature?.level ?? null,
109 level: creature?.level ?? null,
109 health: health ? health.current : undefined,
110 health: health ? health.current : undefined,
server/src/simulation/simulation.js +12 −0
@@ -1777,6 +1777,12 @@ export class Simulation
1777 1777 ]
1778 1778 });
1779 1779 const events = [event];
1780 events.push({
1781 type: "entity-despawn",
1782 entityId: event.entityId,
1783 tileX: event.tileX,
1784 tileY: event.tileY
1785 });
1780 1786 for (const dropEvent of result?.events ?? [])
1781 1787 {
1782 1788 this.#persistGroundEvent(dropEvent);
@@ -1998,8 +2004,14 @@ export class Simulation
1998 2004 );
1999 2005 this.#recalculateActivation(this.now);
2000 2006 this.#syncGhosts();
2007 const spawnView = this.workers[record.workerId].call({
2008 type: WorkerMessage.inspect,
2009 entityId
2010 });
2011 const spawn = spawnView ? replicationSpawn(spawnView) : null;
2001 2012 return [
2002 2013 ...moveEvents,
2014 ...(spawn ? [spawn] : []),
2003 2015 {
2004 2016 type: "player-respawned",
2005 2017 entityId,
@@ -1777,6 +1777,12 @@ export class Simulation
1777 ]
1777 ]
1778 });
1778 });
1779 const events = [event];
1779 const events = [event];
1780 events.push({
1781 type: "entity-despawn",
1782 entityId: event.entityId,
1783 tileX: event.tileX,
1784 tileY: event.tileY
1785 });
1780 for (const dropEvent of result?.events ?? [])
1786 for (const dropEvent of result?.events ?? [])
1781 {
1787 {
1782 this.#persistGroundEvent(dropEvent);
1788 this.#persistGroundEvent(dropEvent);
@@ -1998,8 +2004,14 @@ export class Simulation
1998 );
2004 );
1999 this.#recalculateActivation(this.now);
2005 this.#recalculateActivation(this.now);
2000 this.#syncGhosts();
2006 this.#syncGhosts();
2007 const spawnView = this.workers[record.workerId].call({
2008 type: WorkerMessage.inspect,
2009 entityId
2010 });
2011 const spawn = spawnView ? replicationSpawn(spawnView) : null;
2001 return [
2012 return [
2002 ...moveEvents,
2013 ...moveEvents,
2014 ...(spawn ? [spawn] : []),
2003 {
2015 {
2004 type: "player-respawned",
2016 type: "player-respawned",
2005 entityId,
2017 entityId,
server/src/simulation/workerWorld.js +22 −1
@@ -59,6 +59,7 @@ import {
59 59 queueMeleeApproach,
60 60 queueStandInRangeApproach,
61 61 resolveCombatTarget,
62 silenceDeadPlayer,
62 63 stepAggression,
63 64 stepCombat,
64 65 chebyshev
@@ -191,13 +192,22 @@ export class WorkerWorld
191 192 case WorkerMessage.relocateEntity:
192 193 return this.#relocateEntity(message);
193 194 case WorkerMessage.clearCombat:
195 {
194 196 clearEngage(this.ecs, message.entityId);
197 const handle = this.ecs.handleOf(message.entityId);
198 if (handle != null)
199 {
200 silenceDeadPlayer(this.ecs, handle);
201 cancelGather(this.ecs, handle);
202 clearFollow(this.ecs, message.entityId);
203 }
195 204 for (const entity of this.ecs.query(Component.combat))
196 205 {
197 206 if (entity.combat.targetEntityId === message.targetEntityId)
198 207 { entity.combat.targetEntityId = null; }
199 208 }
200 209 return { ok: true };
210 }
201 211 case WorkerMessage.setItemLastSales:
202 212 {
203 213 this.itemLastSales = new Map();
@@ -1861,7 +1871,16 @@ export class WorkerWorld
1861 1871
1862 1872 #listPublicEntities ()
1863 1873 {
1864 return this.ecs.query(Component.position).map((entity) => publicEntityView(this.ecs, entity.entityId)).filter(Boolean);
1874 const out = [];
1875 for (const entity of this.ecs.query(Component.position))
1876 {
1877 if (this.ecs.has(entity.handle, Component.dead))
1878 { continue; }
1879 const view = publicEntityView(this.ecs, entity.entityId);
1880 if (view)
1881 { out.push(view); }
1882 }
1883 return out;
1865 1884 }
1866 1885
1867 1886 #entitiesInRegion (regionX, regionY, dimensionId = OVERWORLD_ID)
@@ -1907,6 +1926,8 @@ export class WorkerWorld
1907 1926 const entities = [];
1908 1927 for (const entity of this.ecs.query(Component.position, Component.appearance))
1909 1928 {
1929 if (this.ecs.has(entity.handle, Component.dead))
1930 { continue; }
1910 1931 const entityDim = normalizeDimensionId(entity.position.dimensionId ?? OVERWORLD_ID);
1911 1932 if (entityDim !== dim)
1912 1933 { continue; }
@@ -59,6 +59,7 @@ import {
59 queueMeleeApproach,
59 queueMeleeApproach,
60 queueStandInRangeApproach,
60 queueStandInRangeApproach,
61 resolveCombatTarget,
61 resolveCombatTarget,
62 silenceDeadPlayer,
62 stepAggression,
63 stepAggression,
63 stepCombat,
64 stepCombat,
64 chebyshev
65 chebyshev
@@ -191,13 +192,22 @@ export class WorkerWorld
191 case WorkerMessage.relocateEntity:
192 case WorkerMessage.relocateEntity:
192 return this.#relocateEntity(message);
193 return this.#relocateEntity(message);
193 case WorkerMessage.clearCombat:
194 case WorkerMessage.clearCombat:
195 {
194 clearEngage(this.ecs, message.entityId);
196 clearEngage(this.ecs, message.entityId);
197 const handle = this.ecs.handleOf(message.entityId);
198 if (handle != null)
199 {
200 silenceDeadPlayer(this.ecs, handle);
201 cancelGather(this.ecs, handle);
202 clearFollow(this.ecs, message.entityId);
203 }
195 for (const entity of this.ecs.query(Component.combat))
204 for (const entity of this.ecs.query(Component.combat))
196 {
205 {
197 if (entity.combat.targetEntityId === message.targetEntityId)
206 if (entity.combat.targetEntityId === message.targetEntityId)
198 { entity.combat.targetEntityId = null; }
207 { entity.combat.targetEntityId = null; }
199 }
208 }
200 return { ok: true };
209 return { ok: true };
210 }
201 case WorkerMessage.setItemLastSales:
211 case WorkerMessage.setItemLastSales:
202 {
212 {
203 this.itemLastSales = new Map();
213 this.itemLastSales = new Map();
@@ -1861,7 +1871,16 @@ export class WorkerWorld
1861
1871
1862 #listPublicEntities ()
1872 #listPublicEntities ()
1863 {
1873 {
1864 return this.ecs.query(Component.position).map((entity) => publicEntityView(this.ecs, entity.entityId)).filter(Boolean);
1874 const out = [];
1875 for (const entity of this.ecs.query(Component.position))
1876 {
1877 if (this.ecs.has(entity.handle, Component.dead))
1878 { continue; }
1879 const view = publicEntityView(this.ecs, entity.entityId);
1880 if (view)
1881 { out.push(view); }
1882 }
1883 return out;
1865 }
1884 }
1866
1885
1867 #entitiesInRegion (regionX, regionY, dimensionId = OVERWORLD_ID)
1886 #entitiesInRegion (regionX, regionY, dimensionId = OVERWORLD_ID)
@@ -1907,6 +1926,8 @@ export class WorkerWorld
1907 const entities = [];
1926 const entities = [];
1908 for (const entity of this.ecs.query(Component.position, Component.appearance))
1927 for (const entity of this.ecs.query(Component.position, Component.appearance))
1909 {
1928 {
1929 if (this.ecs.has(entity.handle, Component.dead))
1930 { continue; }
1910 const entityDim = normalizeDimensionId(entity.position.dimensionId ?? OVERWORLD_ID);
1931 const entityDim = normalizeDimensionId(entity.position.dimensionId ?? OVERWORLD_ID);
1911 if (entityDim !== dim)
1932 if (entityDim !== dim)
1912 { continue; }
1933 { continue; }
server/src/spells/service.js +17 −0
@@ -147,6 +147,12 @@ function requirePlayer (world, sessionId, characterId)
147 147 { continue; }
148 148 if (entity.character.characterId !== characterId)
149 149 { continue; }
150 if (world.ecs.has(entity.handle, Component.dead))
151 {
152 const error = new Error("You are dead.");
153 error.userFacing = true;
154 throw error;
155 }
150 156 return { handle: entity.handle, entityId: entity.entityId, character: entity.character };
151 157 }
152 158 const error = new Error("Character not found.");
@@ -838,6 +844,12 @@ export function stepPendingSpellCast (world, now = Date.now())
838 844 const events = [];
839 845 for (const entity of world.ecs.query(Component.spellcasting, Component.character, Component.networkIdentity))
840 846 {
847 if (world.ecs.has(entity.handle, Component.dead))
848 {
849 if (entity.spellcasting)
850 { entity.spellcasting.pendingCast = null; }
851 continue;
852 }
841 853 const casting = entity.spellcasting;
842 854 if (!casting?.pendingCast)
843 855 { continue; }
@@ -879,6 +891,11 @@ export function stepSpellAutoCast (world, now = Date.now())
879 891 Component.networkIdentity
880 892 ))
881 893 {
894 if (world.ecs.has(entity.handle, Component.dead))
895 {
896 clearSpellAutoRepeat(entity.spellcasting);
897 continue;
898 }
882 899 const casting = entity.spellcasting;
883 900 const spells = entity.spells;
884 901 normalizeSpellcasting(casting);
@@ -147,6 +147,12 @@ function requirePlayer (world, sessionId, characterId)
147 { continue; }
147 { continue; }
148 if (entity.character.characterId !== characterId)
148 if (entity.character.characterId !== characterId)
149 { continue; }
149 { continue; }
150 if (world.ecs.has(entity.handle, Component.dead))
151 {
152 const error = new Error("You are dead.");
153 error.userFacing = true;
154 throw error;
155 }
150 return { handle: entity.handle, entityId: entity.entityId, character: entity.character };
156 return { handle: entity.handle, entityId: entity.entityId, character: entity.character };
151 }
157 }
152 const error = new Error("Character not found.");
158 const error = new Error("Character not found.");
@@ -838,6 +844,12 @@ export function stepPendingSpellCast (world, now = Date.now())
838 const events = [];
844 const events = [];
839 for (const entity of world.ecs.query(Component.spellcasting, Component.character, Component.networkIdentity))
845 for (const entity of world.ecs.query(Component.spellcasting, Component.character, Component.networkIdentity))
840 {
846 {
847 if (world.ecs.has(entity.handle, Component.dead))
848 {
849 if (entity.spellcasting)
850 { entity.spellcasting.pendingCast = null; }
851 continue;
852 }
841 const casting = entity.spellcasting;
853 const casting = entity.spellcasting;
842 if (!casting?.pendingCast)
854 if (!casting?.pendingCast)
843 { continue; }
855 { continue; }
@@ -879,6 +891,11 @@ export function stepSpellAutoCast (world, now = Date.now())
879 Component.networkIdentity
891 Component.networkIdentity
880 ))
892 ))
881 {
893 {
894 if (world.ecs.has(entity.handle, Component.dead))
895 {
896 clearSpellAutoRepeat(entity.spellcasting);
897 continue;
898 }
882 const casting = entity.spellcasting;
899 const casting = entity.spellcasting;
883 const spells = entity.spells;
900 const spells = entity.spells;
884 normalizeSpellcasting(casting);
901 normalizeSpellcasting(casting);
server/tests/death.test.js +89 −1
@@ -72,7 +72,7 @@ function killPlayer (harness, character, playerId)
72 72 function withDeterministicCombat (fn)
73 73 {
74 74 const random = Math.random;
75 Math.random = () => 0;
75 Math.random = () => 0.5;
76 76 try
77 77 { fn(); }
78 78 finally
@@ -193,3 +193,91 @@ test("respawn without an altar falls back to the death tile", () =>
193 193 { harness.close(); }
194 194 });
195 195 });
196
197 test("dead players stop combat and auto-cast and cannot be targeted", () =>
198 {
199 withDeterministicCombat(() =>
200 {
201 const harness = makeSimulation({ workerCount: 1 });
202 try
203 {
204 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
205 const playerId = harness.simulation.spawnCharacter(1, character);
206 const worker = harness.simulation.workers[
207 harness.simulation.entityRecord(playerId).workerId
208 ];
209 const handle = worker.world.ecs.handleOf(playerId);
210 worker.world.ecs.add(handle, Component.combat, {
211 targetEntityId: 99,
212 nextSwingAt: 0,
213 seekCover: false
214 });
215 worker.world.ecs.add(handle, Component.spellcasting, {
216 repeatSpellId: "firebolt",
217 repeatTargetEntityId: 99,
218 repeatTileX: null,
219 repeatTileY: null,
220 pendingCast: { spellId: "firebolt", tileX: 9, tileY: 8, targetEntityId: 99 },
221 nextCastAt: 0
222 });
223 worker.world.ecs.add(handle, Component.spells, {
224 known: ["firebolt"],
225 favorites: [],
226 autoCast: true,
227 castChances: {}
228 });
229 const path = worker.world.ecs.get(handle, Component.path)
230 ?? worker.world.ecs.add(handle, Component.path, { tiles: [] });
231 path.tiles = [{ x: 9, y: 8 }];
232
233 killPlayer(harness, character, playerId);
234 const player = componentsOf(harness.simulation, playerId);
235 assert.ok(player[Component.dead]);
236 assert.equal(player.combat?.targetEntityId ?? null, null);
237 assert.equal(player.spells?.autoCast, false);
238 assert.equal(player.spellcasting?.repeatSpellId ?? null, null);
239 assert.equal(player.spellcasting?.pendingCast ?? null, null);
240 assert.deepEqual(player.path?.tiles ?? [], []);
241
242 const deerId = harness.simulation.spawnEntity(mobComponents({
243 tileX: 10,
244 tileY: 8,
245 mobTypeId: MobId.deer,
246 spawnerId: null
247 }));
248 harness.simulation.enqueue(1, {
249 type: "attack",
250 characterId: character.character_id,
251 targetEntityId: deerId
252 });
253 const result = harness.simulation.step();
254 assert.ok(
255 result.events.some((event) =>
256 event.type === "command-error" && /dead/i.test(event.message ?? "")
257 ),
258 "dead player commands must be rejected"
259 );
260 }
261 finally
262 { harness.close(); }
263 });
264 });
265
266 test("player death emits entity-despawn for other viewers", () =>
267 {
268 withDeterministicCombat(() =>
269 {
270 const harness = makeSimulation({ workerCount: 1 });
271 try
272 {
273 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
274 const playerId = harness.simulation.spawnCharacter(1, character);
275 const result = killPlayer(harness, character, playerId);
276 assert.ok(result.events.some((event) =>
277 event.type === "entity-despawn" && event.entityId === playerId
278 ));
279 }
280 finally
281 { harness.close(); }
282 });
283 });
@@ -72,7 +72,7 @@ function killPlayer (harness, character, playerId)
72function withDeterministicCombat (fn)
72function withDeterministicCombat (fn)
73{
73{
74 const random = Math.random;
74 const random = Math.random;
75 Math.random = () => 0;
75 Math.random = () => 0.5;
76 try
76 try
77 { fn(); }
77 { fn(); }
78 finally
78 finally
@@ -193,3 +193,91 @@ test("respawn without an altar falls back to the death tile", () =>
193 { harness.close(); }
193 { harness.close(); }
194 });
194 });
195});
195});
196
197test("dead players stop combat and auto-cast and cannot be targeted", () =>
198{
199 withDeterministicCombat(() =>
200 {
201 const harness = makeSimulation({ workerCount: 1 });
202 try
203 {
204 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
205 const playerId = harness.simulation.spawnCharacter(1, character);
206 const worker = harness.simulation.workers[
207 harness.simulation.entityRecord(playerId).workerId
208 ];
209 const handle = worker.world.ecs.handleOf(playerId);
210 worker.world.ecs.add(handle, Component.combat, {
211 targetEntityId: 99,
212 nextSwingAt: 0,
213 seekCover: false
214 });
215 worker.world.ecs.add(handle, Component.spellcasting, {
216 repeatSpellId: "firebolt",
217 repeatTargetEntityId: 99,
218 repeatTileX: null,
219 repeatTileY: null,
220 pendingCast: { spellId: "firebolt", tileX: 9, tileY: 8, targetEntityId: 99 },
221 nextCastAt: 0
222 });
223 worker.world.ecs.add(handle, Component.spells, {
224 known: ["firebolt"],
225 favorites: [],
226 autoCast: true,
227 castChances: {}
228 });
229 const path = worker.world.ecs.get(handle, Component.path)
230 ?? worker.world.ecs.add(handle, Component.path, { tiles: [] });
231 path.tiles = [{ x: 9, y: 8 }];
232
233 killPlayer(harness, character, playerId);
234 const player = componentsOf(harness.simulation, playerId);
235 assert.ok(player[Component.dead]);
236 assert.equal(player.combat?.targetEntityId ?? null, null);
237 assert.equal(player.spells?.autoCast, false);
238 assert.equal(player.spellcasting?.repeatSpellId ?? null, null);
239 assert.equal(player.spellcasting?.pendingCast ?? null, null);
240 assert.deepEqual(player.path?.tiles ?? [], []);
241
242 const deerId = harness.simulation.spawnEntity(mobComponents({
243 tileX: 10,
244 tileY: 8,
245 mobTypeId: MobId.deer,
246 spawnerId: null
247 }));
248 harness.simulation.enqueue(1, {
249 type: "attack",
250 characterId: character.character_id,
251 targetEntityId: deerId
252 });
253 const result = harness.simulation.step();
254 assert.ok(
255 result.events.some((event) =>
256 event.type === "command-error" && /dead/i.test(event.message ?? "")
257 ),
258 "dead player commands must be rejected"
259 );
260 }
261 finally
262 { harness.close(); }
263 });
264});
265
266test("player death emits entity-despawn for other viewers", () =>
267{
268 withDeterministicCombat(() =>
269 {
270 const harness = makeSimulation({ workerCount: 1 });
271 try
272 {
273 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
274 const playerId = harness.simulation.spawnCharacter(1, character);
275 const result = killPlayer(harness, character, playerId);
276 assert.ok(result.events.some((event) =>
277 event.type === "entity-despawn" && event.entityId === playerId
278 ));
279 }
280 finally
281 { harness.close(); }
282 });
283});