BlueGit

timfa questward

Keep auto-cast preference across death and respawn.

Clear only the active armed/repeat cast on death so the toggle stays in sync with the client; after respawn a new cast can resume repeating without a refresh.

timfa Tim Falken · pushed by timfa

Authored 2026-08-31 · 2 months ago

Hash
5f42d1c62b4c04dbf20927c76eb69843f889b349
Parent
7d596f0c
3 files changed +111 −6
server/src/combat/service.js +3 −4
@@ -163,7 +163,8 @@ export function isUntargetable (ecs, handle)
163 163 return !!mobDefinition(creature.mobTypeId)?.untargetable;
164 164 }
165 165
166 /** Stop all combat / cast / path activity when a player dies. */
166 /** Stop active combat / cast / path activity when a player dies.
167 * Auto-cast preference is kept so respawn + re-cast can resume repeating. */
167 168 export function silenceDeadPlayer (ecs, handle)
168 169 {
169 170 if (handle == null)
@@ -183,15 +184,13 @@ export function silenceDeadPlayer (ecs, handle)
183 184 const casting = ecs.get(handle, Component.spellcasting);
184 185 if (casting)
185 186 {
187 casting.armedSpellId = null;
186 188 casting.repeatSpellId = null;
187 189 casting.repeatTargetEntityId = null;
188 190 casting.repeatTileX = null;
189 191 casting.repeatTileY = null;
190 192 casting.pendingCast = null;
191 193 }
192 const spells = ecs.get(handle, Component.spells);
193 if (spells)
194 { spells.autoCast = false; }
195 194 }
196 195
197 196 export function engageTarget (ecs, attackerId, targetId, now = Date.now())
@@ -163,7 +163,8 @@ export function isUntargetable (ecs, handle)
163 return !!mobDefinition(creature.mobTypeId)?.untargetable;
163 return !!mobDefinition(creature.mobTypeId)?.untargetable;
164}
164}
165
165
166/** Stop all combat / cast / path activity when a player dies. */
166/** Stop active combat / cast / path activity when a player dies.
167 * Auto-cast preference is kept so respawn + re-cast can resume repeating. */
167export function silenceDeadPlayer (ecs, handle)
168export function silenceDeadPlayer (ecs, handle)
168{
169{
169 if (handle == null)
170 if (handle == null)
@@ -183,15 +184,13 @@ export function silenceDeadPlayer (ecs, handle)
183 const casting = ecs.get(handle, Component.spellcasting);
184 const casting = ecs.get(handle, Component.spellcasting);
184 if (casting)
185 if (casting)
185 {
186 {
187 casting.armedSpellId = null;
186 casting.repeatSpellId = null;
188 casting.repeatSpellId = null;
187 casting.repeatTargetEntityId = null;
189 casting.repeatTargetEntityId = null;
188 casting.repeatTileX = null;
190 casting.repeatTileX = null;
189 casting.repeatTileY = null;
191 casting.repeatTileY = null;
190 casting.pendingCast = null;
192 casting.pendingCast = null;
191 }
193 }
192 const spells = ecs.get(handle, Component.spells);
193 if (spells)
194 { spells.autoCast = false; }
195}
194}
196
195
197export function engageTarget (ecs, attackerId, targetId, now = Date.now())
196export function engageTarget (ecs, attackerId, targetId, now = Date.now())
server/tests/death.test.js +4 −2
@@ -194,7 +194,7 @@ test("respawn without an altar falls back to the death tile", () =>
194 194 });
195 195 });
196 196
197 test("dead players stop combat and auto-cast and cannot be targeted", () =>
197 test("dead players stop combat and active auto-repeat and cannot be targeted", () =>
198 198 {
199 199 withDeterministicCombat(() =>
200 200 {
@@ -213,6 +213,7 @@ test("dead players stop combat and auto-cast and cannot be targeted", () =>
213 213 seekCover: false
214 214 });
215 215 worker.world.ecs.add(handle, Component.spellcasting, {
216 armedSpellId: "firebolt",
216 217 repeatSpellId: "firebolt",
217 218 repeatTargetEntityId: 99,
218 219 repeatTileX: null,
@@ -234,7 +235,8 @@ test("dead players stop combat and auto-cast and cannot be targeted", () =>
234 235 const player = componentsOf(harness.simulation, playerId);
235 236 assert.ok(player[Component.dead]);
236 237 assert.equal(player.combat?.targetEntityId ?? null, null);
237 assert.equal(player.spells?.autoCast, false);
238 assert.equal(player.spells?.autoCast, true, "auto-cast preference survives death");
239 assert.equal(player.spellcasting?.armedSpellId ?? null, null);
238 240 assert.equal(player.spellcasting?.repeatSpellId ?? null, null);
239 241 assert.equal(player.spellcasting?.pendingCast ?? null, null);
240 242 assert.deepEqual(player.path?.tiles ?? [], []);
@@ -194,7 +194,7 @@ test("respawn without an altar falls back to the death tile", () =>
194 });
194 });
195});
195});
196
196
197test("dead players stop combat and auto-cast and cannot be targeted", () =>
197test("dead players stop combat and active auto-repeat and cannot be targeted", () =>
198{
198{
199 withDeterministicCombat(() =>
199 withDeterministicCombat(() =>
200 {
200 {
@@ -213,6 +213,7 @@ test("dead players stop combat and auto-cast and cannot be targeted", () =>
213 seekCover: false
213 seekCover: false
214 });
214 });
215 worker.world.ecs.add(handle, Component.spellcasting, {
215 worker.world.ecs.add(handle, Component.spellcasting, {
216 armedSpellId: "firebolt",
216 repeatSpellId: "firebolt",
217 repeatSpellId: "firebolt",
217 repeatTargetEntityId: 99,
218 repeatTargetEntityId: 99,
218 repeatTileX: null,
219 repeatTileX: null,
@@ -234,7 +235,8 @@ test("dead players stop combat and auto-cast and cannot be targeted", () =>
234 const player = componentsOf(harness.simulation, playerId);
235 const player = componentsOf(harness.simulation, playerId);
235 assert.ok(player[Component.dead]);
236 assert.ok(player[Component.dead]);
236 assert.equal(player.combat?.targetEntityId ?? null, null);
237 assert.equal(player.combat?.targetEntityId ?? null, null);
237 assert.equal(player.spells?.autoCast, false);
238 assert.equal(player.spells?.autoCast, true, "auto-cast preference survives death");
239 assert.equal(player.spellcasting?.armedSpellId ?? null, null);
238 assert.equal(player.spellcasting?.repeatSpellId ?? null, null);
240 assert.equal(player.spellcasting?.repeatSpellId ?? null, null);
239 assert.equal(player.spellcasting?.pendingCast ?? null, null);
241 assert.equal(player.spellcasting?.pendingCast ?? null, null);
240 assert.deepEqual(player.path?.tiles ?? [], []);
242 assert.deepEqual(player.path?.tiles ?? [], []);
server/tests/spells.test.js +104 −0
@@ -572,3 +572,107 @@ test("auto-retaliate does not engage while auto-casting an attack spell", () =>
572 572 harness.close();
573 573 }
574 574 });
575
576 test("death clears active auto-repeat but keeps auto-cast for respawn", () =>
577 {
578 const harness = makeSimulation({ workerCount: 1 });
579 const random = Math.random;
580 Math.random = () => 0.999;
581 try
582 {
583 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
584 const playerId = harness.simulation.spawnCharacter(1, character);
585 const deerId = harness.simulation.spawnEntity(mobComponents({
586 tileX: 10,
587 tileY: 8,
588 mobTypeId: MobId.deer,
589 level: 40
590 }));
591 const worker = workerOf(harness.simulation, playerId);
592 const handle = worker.world.ecs.handleOf(playerId);
593 const deerHandle = worker.world.ecs.handleOf(deerId);
594 worker.world.ecs.get(deerHandle, Component.health).max = 500;
595 worker.world.ecs.get(deerHandle, Component.health).current = 500;
596 const spells = worker.world.ecs.get(handle, Component.spells);
597 spells.known.push(SpellId.manaBullet);
598 spells.autoCast = true;
599 const skills = worker.world.ecs.get(handle, Component.skills);
600 skills.xp[SkillId.magic] = totalXpForLevel(20);
601 skills.xp[SkillId.mind] = totalXpForLevel(20);
602
603 harness.simulation.enqueue(1, {
604 type: "cast-spell-entity",
605 characterId: character.character_id,
606 spellId: SpellId.manaBullet,
607 targetEntityId: deerId
608 });
609 harness.simulation.step();
610 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet);
611
612 const killerId = harness.simulation.spawnEntity(mobComponents({
613 tileX: 9,
614 tileY: 8,
615 mobTypeId: MobId.deer,
616 spawnerId: null
617 }));
618 worker.world.ecs.get(worker.world.ecs.handleOf(killerId), Component.health).current = 1_000_000;
619 worker.world.ecs.get(worker.world.ecs.handleOf(killerId), Component.health).max = 1_000_000;
620 worker.world.ecs.get(handle, Component.health).current = 1;
621 harness.simulation.enqueue(1, {
622 type: "attack",
623 characterId: character.character_id,
624 targetEntityId: killerId
625 });
626 let died = false;
627 for (let i = 0; i < 100; i++)
628 {
629 harness.advance(1000);
630 const events = harness.simulation.step().events;
631 if (events.some((event) => event.type === "player-died" && event.entityId === playerId))
632 {
633 died = true;
634 break;
635 }
636 }
637 assert.ok(died, "player should die from retaliation");
638 assert.equal(worker.world.ecs.get(handle, Component.spells).autoCast, true);
639 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, null);
640 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).armedSpellId, null);
641
642 harness.simulation.enqueue(1, { type: "respawn", characterId: character.character_id });
643 harness.simulation.step();
644 assert.equal(worker.world.ecs.has(handle, Component.dead), false);
645 assert.equal(worker.world.ecs.get(handle, Component.spells).autoCast, true);
646
647 const casting = worker.world.ecs.get(handle, Component.spellcasting);
648 casting.nextCastAt = 0;
649 const pos = worker.world.ecs.get(handle, Component.position);
650 pos.tileX = 8;
651 pos.tileY = 8;
652 worker.world.ecs.get(deerHandle, Component.health).current = 500;
653
654 harness.simulation.enqueue(1, {
655 type: "cast-spell-entity",
656 characterId: character.character_id,
657 spellId: SpellId.manaBullet,
658 targetEntityId: deerId
659 });
660 const castResult = harness.simulation.step();
661 assert.equal(
662 castResult.events.some((event) => event.type === "command-error"),
663 false,
664 castResult.events.find((event) => event.type === "command-error")?.message
665 ?? "cast after respawn should succeed"
666 );
667 assert.equal(
668 worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId,
669 SpellId.manaBullet,
670 "auto-cast preference still arms repeat after respawn"
671 );
672 }
673 finally
674 {
675 Math.random = random;
676 harness.close();
677 }
678 });
@@ -572,3 +572,107 @@ test("auto-retaliate does not engage while auto-casting an attack spell", () =>
572 harness.close();
572 harness.close();
573 }
573 }
574});
574});
575
576test("death clears active auto-repeat but keeps auto-cast for respawn", () =>
577{
578 const harness = makeSimulation({ workerCount: 1 });
579 const random = Math.random;
580 Math.random = () => 0.999;
581 try
582 {
583 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
584 const playerId = harness.simulation.spawnCharacter(1, character);
585 const deerId = harness.simulation.spawnEntity(mobComponents({
586 tileX: 10,
587 tileY: 8,
588 mobTypeId: MobId.deer,
589 level: 40
590 }));
591 const worker = workerOf(harness.simulation, playerId);
592 const handle = worker.world.ecs.handleOf(playerId);
593 const deerHandle = worker.world.ecs.handleOf(deerId);
594 worker.world.ecs.get(deerHandle, Component.health).max = 500;
595 worker.world.ecs.get(deerHandle, Component.health).current = 500;
596 const spells = worker.world.ecs.get(handle, Component.spells);
597 spells.known.push(SpellId.manaBullet);
598 spells.autoCast = true;
599 const skills = worker.world.ecs.get(handle, Component.skills);
600 skills.xp[SkillId.magic] = totalXpForLevel(20);
601 skills.xp[SkillId.mind] = totalXpForLevel(20);
602
603 harness.simulation.enqueue(1, {
604 type: "cast-spell-entity",
605 characterId: character.character_id,
606 spellId: SpellId.manaBullet,
607 targetEntityId: deerId
608 });
609 harness.simulation.step();
610 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet);
611
612 const killerId = harness.simulation.spawnEntity(mobComponents({
613 tileX: 9,
614 tileY: 8,
615 mobTypeId: MobId.deer,
616 spawnerId: null
617 }));
618 worker.world.ecs.get(worker.world.ecs.handleOf(killerId), Component.health).current = 1_000_000;
619 worker.world.ecs.get(worker.world.ecs.handleOf(killerId), Component.health).max = 1_000_000;
620 worker.world.ecs.get(handle, Component.health).current = 1;
621 harness.simulation.enqueue(1, {
622 type: "attack",
623 characterId: character.character_id,
624 targetEntityId: killerId
625 });
626 let died = false;
627 for (let i = 0; i < 100; i++)
628 {
629 harness.advance(1000);
630 const events = harness.simulation.step().events;
631 if (events.some((event) => event.type === "player-died" && event.entityId === playerId))
632 {
633 died = true;
634 break;
635 }
636 }
637 assert.ok(died, "player should die from retaliation");
638 assert.equal(worker.world.ecs.get(handle, Component.spells).autoCast, true);
639 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, null);
640 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).armedSpellId, null);
641
642 harness.simulation.enqueue(1, { type: "respawn", characterId: character.character_id });
643 harness.simulation.step();
644 assert.equal(worker.world.ecs.has(handle, Component.dead), false);
645 assert.equal(worker.world.ecs.get(handle, Component.spells).autoCast, true);
646
647 const casting = worker.world.ecs.get(handle, Component.spellcasting);
648 casting.nextCastAt = 0;
649 const pos = worker.world.ecs.get(handle, Component.position);
650 pos.tileX = 8;
651 pos.tileY = 8;
652 worker.world.ecs.get(deerHandle, Component.health).current = 500;
653
654 harness.simulation.enqueue(1, {
655 type: "cast-spell-entity",
656 characterId: character.character_id,
657 spellId: SpellId.manaBullet,
658 targetEntityId: deerId
659 });
660 const castResult = harness.simulation.step();
661 assert.equal(
662 castResult.events.some((event) => event.type === "command-error"),
663 false,
664 castResult.events.find((event) => event.type === "command-error")?.message
665 ?? "cast after respawn should succeed"
666 );
667 assert.equal(
668 worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId,
669 SpellId.manaBullet,
670 "auto-cast preference still arms repeat after respawn"
671 );
672 }
673 finally
674 {
675 Math.random = random;
676 harness.close();
677 }
678});