BlueGit

timfa questward

Fix trade approach, sticky invite toasts, and death combat cleanup

Trade requests now walk adjacent before sending, invite toasts stay until
accept/reject/expiry, and attackers drop melee plus spell auto-targets when
a player dies.

Co-authored-by: Cursor <cursoragent@cursor.com>

timfa Tim Falken · pushed by timfa

Authored 2026-09-04 · 2 months ago

Hash
5c6a7c14a2007b2b5d3ef095f7c84b01e83c8cf1
Parent
c9d1a27f
6 files changed +189 −23
client/src/play/main.js +33 −3
@@ -969,19 +969,42 @@ function flushPendingInteract ()
969 969 return;
970 970 }
971 971
972 if (pendingInteract.kind === "trade-request")
973 {
974 const target = view.entities.get(pendingInteract.targetEntityId);
975 if (!target)
976 {
977 clearPendingInteract();
978 return;
979 }
980 if (!isAdjacentTile(local.tileX, local.tileY, target.tileX, target.tileY)
981 && !(local.tileX === target.tileX && local.tileY === target.tileY))
982 {
983 approachAdjacentTile(target.tileX, target.tileY);
984 return;
985 }
986 const targetEntityId = pendingInteract.targetEntityId;
987 clearPendingInteract();
988 trade?.flushPendingRequest?.(targetEntityId);
989 return;
990 }
991
972 992 if (pendingInteract.kind === "trade-accept")
973 993 {
974 994 const target = view.entities.get(pendingInteract.fromEntityId);
975 995 if (!target)
976 996 {
977 pendingInteract = null;
997 clearPendingInteract();
978 998 return;
979 999 }
980 1000 if (!isAdjacentTile(local.tileX, local.tileY, target.tileX, target.tileY)
981 1001 && !(local.tileX === target.tileX && local.tileY === target.tileY))
982 { return; }
1002 {
1003 approachAdjacentTile(target.tileX, target.tileY);
1004 return;
1005 }
983 1006 const invite = pendingInteract.invite;
984 pendingInteract = null;
1007 clearPendingInteract();
985 1008 trade?.flushPendingAccept?.(invite);
986 1009 return;
987 1010 }
@@ -1898,6 +1921,13 @@ async function main ()
1898 1921 },
1899 1922 approachAdjacentTile,
1900 1923 clearPendingActions: clearAllPendingActions,
1924 setPendingTradeRequest (targetEntityId)
1925 {
1926 pendingInteract = {
1927 kind: "trade-request",
1928 targetEntityId: targetEntityId | 0
1929 };
1930 },
1901 1931 setPendingTradeAccept (invite)
1902 1932 {
1903 1933 pendingInteract = {
@@ -969,19 +969,42 @@ function flushPendingInteract ()
969 return;
969 return;
970 }
970 }
971
971
972 if (pendingInteract.kind === "trade-request")
973 {
974 const target = view.entities.get(pendingInteract.targetEntityId);
975 if (!target)
976 {
977 clearPendingInteract();
978 return;
979 }
980 if (!isAdjacentTile(local.tileX, local.tileY, target.tileX, target.tileY)
981 && !(local.tileX === target.tileX && local.tileY === target.tileY))
982 {
983 approachAdjacentTile(target.tileX, target.tileY);
984 return;
985 }
986 const targetEntityId = pendingInteract.targetEntityId;
987 clearPendingInteract();
988 trade?.flushPendingRequest?.(targetEntityId);
989 return;
990 }
991
972 if (pendingInteract.kind === "trade-accept")
992 if (pendingInteract.kind === "trade-accept")
973 {
993 {
974 const target = view.entities.get(pendingInteract.fromEntityId);
994 const target = view.entities.get(pendingInteract.fromEntityId);
975 if (!target)
995 if (!target)
976 {
996 {
977 pendingInteract = null;
997 clearPendingInteract();
978 return;
998 return;
979 }
999 }
980 if (!isAdjacentTile(local.tileX, local.tileY, target.tileX, target.tileY)
1000 if (!isAdjacentTile(local.tileX, local.tileY, target.tileX, target.tileY)
981 && !(local.tileX === target.tileX && local.tileY === target.tileY))
1001 && !(local.tileX === target.tileX && local.tileY === target.tileY))
982 { return; }
1002 {
1003 approachAdjacentTile(target.tileX, target.tileY);
1004 return;
1005 }
983 const invite = pendingInteract.invite;
1006 const invite = pendingInteract.invite;
984 pendingInteract = null;
1007 clearPendingInteract();
985 trade?.flushPendingAccept?.(invite);
1008 trade?.flushPendingAccept?.(invite);
986 return;
1009 return;
987 }
1010 }
@@ -1898,6 +1921,13 @@ async function main ()
1898 },
1921 },
1899 approachAdjacentTile,
1922 approachAdjacentTile,
1900 clearPendingActions: clearAllPendingActions,
1923 clearPendingActions: clearAllPendingActions,
1924 setPendingTradeRequest (targetEntityId)
1925 {
1926 pendingInteract = {
1927 kind: "trade-request",
1928 targetEntityId: targetEntityId | 0
1929 };
1930 },
1901 setPendingTradeAccept (invite)
1931 setPendingTradeAccept (invite)
1902 {
1932 {
1903 pendingInteract = {
1933 pendingInteract = {
client/src/play/toasts.js +12 −2
@@ -32,7 +32,10 @@ export function initToasts ()
32 32 const text = String(message ?? "").trim();
33 33 if (!text)
34 34 { return { dismiss () {} }; }
35 const durationMs = Math.max(800, options.durationMs ?? DEFAULT_DURATION_MS);
35 const sticky = !!options.sticky;
36 const durationMs = sticky
37 ? null
38 : Math.max(800, options.durationMs ?? DEFAULT_DURATION_MS);
36 39 const onClick = typeof options.onClick === "function" ? options.onClick : null;
37 40 const actions = Array.isArray(options.actions) ? options.actions : [];
38 41 const el = document.createElement("div");
@@ -43,11 +46,17 @@ export function initToasts ()
43 46 textEl.textContent = text;
44 47 el.append(textEl);
45 48 let dismissed = false;
49 let timer = null;
46 50 const dismiss = () =>
47 51 {
48 52 if (dismissed)
49 53 { return; }
50 54 dismissed = true;
55 if (timer != null)
56 {
57 window.clearTimeout(timer);
58 timer = null;
59 }
51 60 el.classList.remove("toast-visible");
52 61 el.classList.add("toast-leaving");
53 62 window.setTimeout(() => el.remove(), LEAVE_MS);
@@ -88,7 +97,8 @@ export function initToasts ()
88 97 {
89 98 el.classList.add("toast-visible");
90 99 });
91 window.setTimeout(dismiss, durationMs);
100 if (durationMs != null)
101 { timer = window.setTimeout(dismiss, durationMs); }
92 102 return { dismiss };
93 103 }
94 104
@@ -32,7 +32,10 @@ export function initToasts ()
32 const text = String(message ?? "").trim();
32 const text = String(message ?? "").trim();
33 if (!text)
33 if (!text)
34 { return { dismiss () {} }; }
34 { return { dismiss () {} }; }
35 const durationMs = Math.max(800, options.durationMs ?? DEFAULT_DURATION_MS);
35 const sticky = !!options.sticky;
36 const durationMs = sticky
37 ? null
38 : Math.max(800, options.durationMs ?? DEFAULT_DURATION_MS);
36 const onClick = typeof options.onClick === "function" ? options.onClick : null;
39 const onClick = typeof options.onClick === "function" ? options.onClick : null;
37 const actions = Array.isArray(options.actions) ? options.actions : [];
40 const actions = Array.isArray(options.actions) ? options.actions : [];
38 const el = document.createElement("div");
41 const el = document.createElement("div");
@@ -43,11 +46,17 @@ export function initToasts ()
43 textEl.textContent = text;
46 textEl.textContent = text;
44 el.append(textEl);
47 el.append(textEl);
45 let dismissed = false;
48 let dismissed = false;
49 let timer = null;
46 const dismiss = () =>
50 const dismiss = () =>
47 {
51 {
48 if (dismissed)
52 if (dismissed)
49 { return; }
53 { return; }
50 dismissed = true;
54 dismissed = true;
55 if (timer != null)
56 {
57 window.clearTimeout(timer);
58 timer = null;
59 }
51 el.classList.remove("toast-visible");
60 el.classList.remove("toast-visible");
52 el.classList.add("toast-leaving");
61 el.classList.add("toast-leaving");
53 window.setTimeout(() => el.remove(), LEAVE_MS);
62 window.setTimeout(() => el.remove(), LEAVE_MS);
@@ -88,7 +97,8 @@ export function initToasts ()
88 {
97 {
89 el.classList.add("toast-visible");
98 el.classList.add("toast-visible");
90 });
99 });
91 window.setTimeout(dismiss, durationMs);
100 if (durationMs != null)
101 { timer = window.setTimeout(dismiss, durationMs); }
92 return { dismiss };
102 return { dismiss };
93 }
103 }
94
104
client/src/play/trade.js +29 −3
@@ -19,6 +19,7 @@ export function initTrade ({
19 19 getEntity,
20 20 approachAdjacentTile,
21 21 clearPendingActions,
22 setPendingTradeRequest,
22 23 setPendingTradeAccept,
23 24 resolveSpriteUrl,
24 25 resolveImage,
@@ -209,15 +210,35 @@ export function initTrade ({
209 210 applyState(message);
210 211 }
211 212
212 function requestTrade (targetEntityId)
213 function sendTradeRequest (targetEntityId)
213 214 {
214 clearPendingActions?.();
215 215 socket.send({
216 216 type: MessageType.tradeRequest,
217 217 targetEntityId: targetEntityId | 0
218 218 });
219 219 }
220 220
221 function requestTrade (targetEntityId)
222 {
223 clearPendingActions?.();
224 const id = targetEntityId | 0;
225 const local = getLocalEntity?.();
226 const partner = getEntity?.(id);
227 if (!local || !partner)
228 {
229 sendTradeRequest(id);
230 return;
231 }
232 if (isAdjacentTile(local.tileX, local.tileY, partner.tileX, partner.tileY)
233 || (local.tileX === partner.tileX && local.tileY === partner.tileY))
234 {
235 sendTradeRequest(id);
236 return;
237 }
238 setPendingTradeRequest?.(id);
239 approachAdjacentTile?.(partner.tileX, partner.tileY);
240 }
241
221 242 function respondAccept (invite)
222 243 {
223 244 const local = getLocalEntity?.();
@@ -253,7 +274,8 @@ export function initTrade ({
253 274 const handle = toasts?.show?.(
254 275 `${fromName} wishes to trade with you`,
255 276 {
256 durationMs: Math.max(1000, (message.expiresAt | 0) - Date.now()) || 60_000,
277 // Stay until Accept / Reject / server tradeInviteExpired (clock-safe).
278 sticky: true,
257 279 actions: [
258 280 {
259 281 label: "Accept",
@@ -348,6 +370,10 @@ export function initTrade ({
348 370 requestTrade,
349 371 offerBagSlot,
350 372 inventoryLocked: () => (open && youAccepted) || !!(qtyPanel && !qtyPanel.hidden),
373 flushPendingRequest (targetEntityId)
374 {
375 sendTradeRequest(targetEntityId);
376 },
351 377 flushPendingAccept (invite)
352 378 {
353 379 socket.send({
@@ -19,6 +19,7 @@ export function initTrade ({
19 getEntity,
19 getEntity,
20 approachAdjacentTile,
20 approachAdjacentTile,
21 clearPendingActions,
21 clearPendingActions,
22 setPendingTradeRequest,
22 setPendingTradeAccept,
23 setPendingTradeAccept,
23 resolveSpriteUrl,
24 resolveSpriteUrl,
24 resolveImage,
25 resolveImage,
@@ -209,15 +210,35 @@ export function initTrade ({
209 applyState(message);
210 applyState(message);
210 }
211 }
211
212
212 function requestTrade (targetEntityId)
213 function sendTradeRequest (targetEntityId)
213 {
214 {
214 clearPendingActions?.();
215 socket.send({
215 socket.send({
216 type: MessageType.tradeRequest,
216 type: MessageType.tradeRequest,
217 targetEntityId: targetEntityId | 0
217 targetEntityId: targetEntityId | 0
218 });
218 });
219 }
219 }
220
220
221 function requestTrade (targetEntityId)
222 {
223 clearPendingActions?.();
224 const id = targetEntityId | 0;
225 const local = getLocalEntity?.();
226 const partner = getEntity?.(id);
227 if (!local || !partner)
228 {
229 sendTradeRequest(id);
230 return;
231 }
232 if (isAdjacentTile(local.tileX, local.tileY, partner.tileX, partner.tileY)
233 || (local.tileX === partner.tileX && local.tileY === partner.tileY))
234 {
235 sendTradeRequest(id);
236 return;
237 }
238 setPendingTradeRequest?.(id);
239 approachAdjacentTile?.(partner.tileX, partner.tileY);
240 }
241
221 function respondAccept (invite)
242 function respondAccept (invite)
222 {
243 {
223 const local = getLocalEntity?.();
244 const local = getLocalEntity?.();
@@ -253,7 +274,8 @@ export function initTrade ({
253 const handle = toasts?.show?.(
274 const handle = toasts?.show?.(
254 `${fromName} wishes to trade with you`,
275 `${fromName} wishes to trade with you`,
255 {
276 {
256 durationMs: Math.max(1000, (message.expiresAt | 0) - Date.now()) || 60_000,
277 // Stay until Accept / Reject / server tradeInviteExpired (clock-safe).
278 sticky: true,
257 actions: [
279 actions: [
258 {
280 {
259 label: "Accept",
281 label: "Accept",
@@ -348,6 +370,10 @@ export function initTrade ({
348 requestTrade,
370 requestTrade,
349 offerBagSlot,
371 offerBagSlot,
350 inventoryLocked: () => (open && youAccepted) || !!(qtyPanel && !qtyPanel.hidden),
372 inventoryLocked: () => (open && youAccepted) || !!(qtyPanel && !qtyPanel.hidden),
373 flushPendingRequest (targetEntityId)
374 {
375 sendTradeRequest(targetEntityId);
376 },
351 flushPendingAccept (invite)
377 flushPendingAccept (invite)
352 {
378 {
353 socket.send({
379 socket.send({
server/src/combat/service.js +39 −10
@@ -128,6 +128,43 @@ function dropCombatTarget (world, entity)
128 128 syncCreatureChaseGait(world, entity.handle, 0, false);
129 129 }
130 130
131 /**
132 * Stop everyone who was attacking / auto-casting at `targetEntityId`
133 * (melee engage, spell repeat, pending cast, approach path).
134 */
135 export function clearAttackersTargeting (ecs, targetEntityId)
136 {
137 const targetId = targetEntityId | 0;
138 if (!targetId)
139 { return; }
140 for (const entity of ecs.query(Component.combat))
141 {
142 if (entity.combat.targetEntityId !== targetId)
143 { continue; }
144 entity.combat.targetEntityId = null;
145 entity.combat.seekCover = false;
146 entity.combat.fleeFrom = null;
147 clearCombatApproachPath(ecs, entity.handle);
148 }
149 for (const entity of ecs.query(Component.spellcasting))
150 {
151 const casting = entity.spellcasting;
152 if (!casting)
153 { continue; }
154 const pendingTarget = casting.pendingCast?.targetEntityId;
155 const hitting = casting.repeatTargetEntityId === targetId
156 || (pendingTarget != null && (pendingTarget | 0) === targetId);
157 if (!hitting)
158 { continue; }
159 casting.repeatSpellId = null;
160 casting.repeatTargetEntityId = null;
161 casting.repeatTileX = null;
162 casting.repeatTileY = null;
163 casting.pendingCast = null;
164 clearCombatApproachPath(ecs, entity.handle);
165 }
166 }
167
131 168 export function attackProfileOf (ecs, handle)
132 169 {
133 170 const creature = ecs.get(handle, Component.creature);
@@ -2250,11 +2287,7 @@ function applyLocalMeleeHit (
2250 2287 {
2251 2288 clearEngage(world.ecs, attackerId);
2252 2289 clearEngage(world.ecs, targetId);
2253 for (const entity of world.ecs.query(Component.combat))
2254 {
2255 if (entity.combat.targetEntityId === targetId)
2256 { entity.combat.targetEntityId = null; }
2257 }
2290 clearAttackersTargeting(world.ecs, targetId);
2258 2291 if (targetCharacter && !world.ecs.has(targetHandle, Component.dead))
2259 2292 {
2260 2293 const path = world.ecs.get(targetHandle, Component.path);
@@ -2427,11 +2460,7 @@ export function applyDamageAndKill (world, attackerId, targetId, damage, now = D
2427 2460 {
2428 2461 clearEngage(world.ecs, attackerId);
2429 2462 clearEngage(world.ecs, targetId);
2430 for (const entity of world.ecs.query(Component.combat))
2431 {
2432 if (entity.combat.targetEntityId === targetId)
2433 { entity.combat.targetEntityId = null; }
2434 }
2463 clearAttackersTargeting(world.ecs, targetId);
2435 2464 const targetCharacter = world.ecs.get(targetHandle, Component.character);
2436 2465 if (targetCharacter && !world.ecs.has(targetHandle, Component.dead))
2437 2466 {
@@ -128,6 +128,43 @@ function dropCombatTarget (world, entity)
128 syncCreatureChaseGait(world, entity.handle, 0, false);
128 syncCreatureChaseGait(world, entity.handle, 0, false);
129}
129}
130
130
131/**
132 * Stop everyone who was attacking / auto-casting at `targetEntityId`
133 * (melee engage, spell repeat, pending cast, approach path).
134 */
135export function clearAttackersTargeting (ecs, targetEntityId)
136{
137 const targetId = targetEntityId | 0;
138 if (!targetId)
139 { return; }
140 for (const entity of ecs.query(Component.combat))
141 {
142 if (entity.combat.targetEntityId !== targetId)
143 { continue; }
144 entity.combat.targetEntityId = null;
145 entity.combat.seekCover = false;
146 entity.combat.fleeFrom = null;
147 clearCombatApproachPath(ecs, entity.handle);
148 }
149 for (const entity of ecs.query(Component.spellcasting))
150 {
151 const casting = entity.spellcasting;
152 if (!casting)
153 { continue; }
154 const pendingTarget = casting.pendingCast?.targetEntityId;
155 const hitting = casting.repeatTargetEntityId === targetId
156 || (pendingTarget != null && (pendingTarget | 0) === targetId);
157 if (!hitting)
158 { continue; }
159 casting.repeatSpellId = null;
160 casting.repeatTargetEntityId = null;
161 casting.repeatTileX = null;
162 casting.repeatTileY = null;
163 casting.pendingCast = null;
164 clearCombatApproachPath(ecs, entity.handle);
165 }
166}
167
131export function attackProfileOf (ecs, handle)
168export function attackProfileOf (ecs, handle)
132{
169{
133 const creature = ecs.get(handle, Component.creature);
170 const creature = ecs.get(handle, Component.creature);
@@ -2250,11 +2287,7 @@ function applyLocalMeleeHit (
2250 {
2287 {
2251 clearEngage(world.ecs, attackerId);
2288 clearEngage(world.ecs, attackerId);
2252 clearEngage(world.ecs, targetId);
2289 clearEngage(world.ecs, targetId);
2253 for (const entity of world.ecs.query(Component.combat))
2290 clearAttackersTargeting(world.ecs, targetId);
2254 {
2255 if (entity.combat.targetEntityId === targetId)
2256 { entity.combat.targetEntityId = null; }
2257 }
2258 if (targetCharacter && !world.ecs.has(targetHandle, Component.dead))
2291 if (targetCharacter && !world.ecs.has(targetHandle, Component.dead))
2259 {
2292 {
2260 const path = world.ecs.get(targetHandle, Component.path);
2293 const path = world.ecs.get(targetHandle, Component.path);
@@ -2427,11 +2460,7 @@ export function applyDamageAndKill (world, attackerId, targetId, damage, now = D
2427 {
2460 {
2428 clearEngage(world.ecs, attackerId);
2461 clearEngage(world.ecs, attackerId);
2429 clearEngage(world.ecs, targetId);
2462 clearEngage(world.ecs, targetId);
2430 for (const entity of world.ecs.query(Component.combat))
2463 clearAttackersTargeting(world.ecs, targetId);
2431 {
2432 if (entity.combat.targetEntityId === targetId)
2433 { entity.combat.targetEntityId = null; }
2434 }
2435 const targetCharacter = world.ecs.get(targetHandle, Component.character);
2464 const targetCharacter = world.ecs.get(targetHandle, Component.character);
2436 if (targetCharacter && !world.ecs.has(targetHandle, Component.dead))
2465 if (targetCharacter && !world.ecs.has(targetHandle, Component.dead))
2437 {
2466 {
server/src/simulation/workerWorld.js +2 −5
@@ -61,6 +61,7 @@ import {
61 61 applyRemoteMeleeSwing,
62 62 applyRemoteRangedShot,
63 63 attackProfileOf,
64 clearAttackersTargeting,
64 65 clearEngage,
65 66 engageTarget,
66 67 ensureMobCombat,
@@ -230,11 +231,7 @@ export class WorkerWorld
230 231 cancelGather(this.ecs, handle);
231 232 clearFollow(this.ecs, message.entityId);
232 233 }
233 for (const entity of this.ecs.query(Component.combat))
234 {
235 if (entity.combat.targetEntityId === message.targetEntityId)
236 { entity.combat.targetEntityId = null; }
237 }
234 clearAttackersTargeting(this.ecs, message.targetEntityId ?? message.entityId);
238 235 return { ok: true };
239 236 }
240 237 case WorkerMessage.setItemLastSales:
@@ -61,6 +61,7 @@ import {
61 applyRemoteMeleeSwing,
61 applyRemoteMeleeSwing,
62 applyRemoteRangedShot,
62 applyRemoteRangedShot,
63 attackProfileOf,
63 attackProfileOf,
64 clearAttackersTargeting,
64 clearEngage,
65 clearEngage,
65 engageTarget,
66 engageTarget,
66 ensureMobCombat,
67 ensureMobCombat,
@@ -230,11 +231,7 @@ export class WorkerWorld
230 cancelGather(this.ecs, handle);
231 cancelGather(this.ecs, handle);
231 clearFollow(this.ecs, message.entityId);
232 clearFollow(this.ecs, message.entityId);
232 }
233 }
233 for (const entity of this.ecs.query(Component.combat))
234 clearAttackersTargeting(this.ecs, message.targetEntityId ?? message.entityId);
234 {
235 if (entity.combat.targetEntityId === message.targetEntityId)
236 { entity.combat.targetEntityId = null; }
237 }
238 return { ok: true };
235 return { ok: true };
239 }
236 }
240 case WorkerMessage.setItemLastSales:
237 case WorkerMessage.setItemLastSales:
server/tests/death.test.js +74 −0
@@ -276,6 +276,80 @@ test("dead players stop combat and active auto-repeat and cannot be targeted", (
276 276 });
277 277 });
278 278
279 test("attackers drop melee and spell auto-targets when the victim dies", () =>
280 {
281 withDeterministicCombat(() =>
282 {
283 const harness = makeSimulation({ workerCount: 1 });
284 try
285 {
286 const victim = makeCharacter(harness.stateDb, {
287 tileX: 8,
288 tileY: 8,
289 firstName: "Vic",
290 lastName: "Victim"
291 });
292 const caster = makeCharacter(harness.stateDb, {
293 tileX: 10,
294 tileY: 8,
295 firstName: "Cas",
296 lastName: "Caster"
297 });
298 const victimId = harness.simulation.spawnCharacter(1, victim);
299 const casterId = harness.simulation.spawnCharacter(2, caster);
300 const deerId = harness.simulation.spawnEntity(mobComponents({
301 tileX: 9,
302 tileY: 8,
303 mobTypeId: MobId.deer,
304 spawnerId: null
305 }));
306 const worker = harness.simulation.workers[
307 harness.simulation.entityRecord(victimId).workerId
308 ];
309 const ecs = worker.world.ecs;
310 const deerHandle = ecs.handleOf(deerId);
311 const casterHandle = ecs.handleOf(casterId);
312 const victimHandle = ecs.handleOf(victimId);
313 ecs.get(deerHandle, Component.health).current = 1_000_000;
314 ecs.get(deerHandle, Component.health).max = 1_000_000;
315 ecs.get(victimHandle, Component.health).current = 1;
316 ecs.add(deerHandle, Component.combat, {
317 targetEntityId: victimId,
318 nextSwingAt: 0,
319 seekCover: false,
320 fleeFrom: null
321 });
322 ecs.add(casterHandle, Component.spellcasting, {
323 armedSpellId: null,
324 repeatSpellId: "firebolt",
325 repeatTargetEntityId: victimId,
326 repeatTileX: null,
327 repeatTileY: null,
328 pendingCast: { spellId: "firebolt", tileX: 8, tileY: 8, targetEntityId: victimId },
329 nextCastAt: 0
330 });
331 const casterPath = ecs.get(casterHandle, Component.path)
332 ?? ecs.add(casterHandle, Component.path, { tiles: [] });
333 casterPath.tiles = [{ x: 8, y: 8 }];
334
335 applyDamageAndKill(worker.world, deerId, victimId, 999, Date.now());
336
337 assert.ok(ecs.has(victimHandle, Component.dead));
338 assert.equal(
339 ecs.get(deerHandle, Component.combat)?.targetEntityId ?? null,
340 null,
341 "melee auto-attack must stop on death"
342 );
343 const casting = ecs.get(casterHandle, Component.spellcasting);
344 assert.equal(casting?.repeatTargetEntityId ?? null, null);
345 assert.equal(casting?.pendingCast ?? null, null);
346 assert.deepEqual(ecs.get(casterHandle, Component.path)?.tiles ?? [], []);
347 }
348 finally
349 { harness.close(); }
350 });
351 });
352
279 353 test("player death emits entity-despawn for other viewers", () =>
280 354 {
281 355 withDeterministicCombat(() =>
@@ -276,6 +276,80 @@ test("dead players stop combat and active auto-repeat and cannot be targeted", (
276 });
276 });
277});
277});
278
278
279test("attackers drop melee and spell auto-targets when the victim dies", () =>
280{
281 withDeterministicCombat(() =>
282 {
283 const harness = makeSimulation({ workerCount: 1 });
284 try
285 {
286 const victim = makeCharacter(harness.stateDb, {
287 tileX: 8,
288 tileY: 8,
289 firstName: "Vic",
290 lastName: "Victim"
291 });
292 const caster = makeCharacter(harness.stateDb, {
293 tileX: 10,
294 tileY: 8,
295 firstName: "Cas",
296 lastName: "Caster"
297 });
298 const victimId = harness.simulation.spawnCharacter(1, victim);
299 const casterId = harness.simulation.spawnCharacter(2, caster);
300 const deerId = harness.simulation.spawnEntity(mobComponents({
301 tileX: 9,
302 tileY: 8,
303 mobTypeId: MobId.deer,
304 spawnerId: null
305 }));
306 const worker = harness.simulation.workers[
307 harness.simulation.entityRecord(victimId).workerId
308 ];
309 const ecs = worker.world.ecs;
310 const deerHandle = ecs.handleOf(deerId);
311 const casterHandle = ecs.handleOf(casterId);
312 const victimHandle = ecs.handleOf(victimId);
313 ecs.get(deerHandle, Component.health).current = 1_000_000;
314 ecs.get(deerHandle, Component.health).max = 1_000_000;
315 ecs.get(victimHandle, Component.health).current = 1;
316 ecs.add(deerHandle, Component.combat, {
317 targetEntityId: victimId,
318 nextSwingAt: 0,
319 seekCover: false,
320 fleeFrom: null
321 });
322 ecs.add(casterHandle, Component.spellcasting, {
323 armedSpellId: null,
324 repeatSpellId: "firebolt",
325 repeatTargetEntityId: victimId,
326 repeatTileX: null,
327 repeatTileY: null,
328 pendingCast: { spellId: "firebolt", tileX: 8, tileY: 8, targetEntityId: victimId },
329 nextCastAt: 0
330 });
331 const casterPath = ecs.get(casterHandle, Component.path)
332 ?? ecs.add(casterHandle, Component.path, { tiles: [] });
333 casterPath.tiles = [{ x: 8, y: 8 }];
334
335 applyDamageAndKill(worker.world, deerId, victimId, 999, Date.now());
336
337 assert.ok(ecs.has(victimHandle, Component.dead));
338 assert.equal(
339 ecs.get(deerHandle, Component.combat)?.targetEntityId ?? null,
340 null,
341 "melee auto-attack must stop on death"
342 );
343 const casting = ecs.get(casterHandle, Component.spellcasting);
344 assert.equal(casting?.repeatTargetEntityId ?? null, null);
345 assert.equal(casting?.pendingCast ?? null, null);
346 assert.deepEqual(ecs.get(casterHandle, Component.path)?.tiles ?? [], []);
347 }
348 finally
349 { harness.close(); }
350 });
351});
352
279test("player death emits entity-despawn for other viewers", () =>
353test("player death emits entity-despawn for other viewers", () =>
280{
354{
281 withDeterministicCombat(() =>
355 withDeterministicCombat(() =>