BlueGit

timfa questward

Keep spell auto-cast alive through disarm and re-arm.

Cancel-arm no longer wipes an active repeat (fixes the cast/disarm race), and re-arming the same spell leaves auto-cast running so favourite clicks stop forcing melee fallback.

timfa Tim Falken · pushed by timfa

Authored 2026-08-31 · 2 months ago

Hash
60d4e770cbb824297f70d20186e05d968b570446
Parent
52f80ccc
2 files changed +125 −2
server/src/spells/service.js +15 −2
@@ -225,7 +225,15 @@ function armSpell (world, sessionId, command, now)
225 225 }
226 226 const casting = world.ecs.get(handle, Component.spellcasting);
227 227 casting.armedSpellId = command.spellId;
228 clearSpellAutoRepeat(casting);
228 casting.pendingCast = null;
229 /** Re-arming the active auto-repeat spell must not cancel it (favourite bar clicks). */
230 if (casting.repeatSpellId != null && casting.repeatSpellId !== command.spellId)
231 {
232 casting.repeatSpellId = null;
233 casting.repeatTargetEntityId = null;
234 casting.repeatTileX = null;
235 casting.repeatTileY = null;
236 }
229 237 return {
230 238 events: [{
231 239 type: "spell-armed",
@@ -243,7 +251,12 @@ function cancelArmSpell (world, sessionId, command)
243 251 if (!casting)
244 252 { return { events: [] }; }
245 253 casting.armedSpellId = null;
246 clearSpellAutoRepeat(casting);
254 casting.pendingCast = null;
255 /**
256 * Do not clear auto-repeat here. Cast already disarms then sets repeat; a late
257 * cancel-arm (right-click while the client still thought it was armed) used to
258 * wipe that repeat and leave the player falling back to melee.
259 */
247 260 return {
248 261 events: [{
249 262 type: "spell-disarmed",
@@ -225,7 +225,15 @@ function armSpell (world, sessionId, command, now)
225 }
225 }
226 const casting = world.ecs.get(handle, Component.spellcasting);
226 const casting = world.ecs.get(handle, Component.spellcasting);
227 casting.armedSpellId = command.spellId;
227 casting.armedSpellId = command.spellId;
228 clearSpellAutoRepeat(casting);
228 casting.pendingCast = null;
229 /** Re-arming the active auto-repeat spell must not cancel it (favourite bar clicks). */
230 if (casting.repeatSpellId != null && casting.repeatSpellId !== command.spellId)
231 {
232 casting.repeatSpellId = null;
233 casting.repeatTargetEntityId = null;
234 casting.repeatTileX = null;
235 casting.repeatTileY = null;
236 }
229 return {
237 return {
230 events: [{
238 events: [{
231 type: "spell-armed",
239 type: "spell-armed",
@@ -243,7 +251,12 @@ function cancelArmSpell (world, sessionId, command)
243 if (!casting)
251 if (!casting)
244 { return { events: [] }; }
252 { return { events: [] }; }
245 casting.armedSpellId = null;
253 casting.armedSpellId = null;
246 clearSpellAutoRepeat(casting);
254 casting.pendingCast = null;
255 /**
256 * Do not clear auto-repeat here. Cast already disarms then sets repeat; a late
257 * cancel-arm (right-click while the client still thought it was armed) used to
258 * wipe that repeat and leave the player falling back to melee.
259 */
247 return {
260 return {
248 events: [{
261 events: [{
249 type: "spell-disarmed",
262 type: "spell-disarmed",
server/tests/spells.test.js +110 −0
@@ -391,6 +391,116 @@ test("physical attack cancels spell auto-cast", () =>
391 391 }
392 392 });
393 393
394 test("cancel-arm after a cast does not clear auto-repeat", () =>
395 {
396 const harness = makeSimulation({ workerCount: 1 });
397 const random = Math.random;
398 Math.random = () => 0.999;
399 try
400 {
401 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
402 const playerId = harness.simulation.spawnCharacter(1, character);
403 const deerId = harness.simulation.spawnEntity(mobComponents({
404 tileX: 10,
405 tileY: 8,
406 mobTypeId: MobId.deer,
407 level: 40
408 }));
409 const worker = workerOf(harness.simulation, playerId);
410 const handle = worker.world.ecs.handleOf(playerId);
411 const deerHandle = worker.world.ecs.handleOf(deerId);
412 worker.world.ecs.get(deerHandle, Component.health).max = 500;
413 worker.world.ecs.get(deerHandle, Component.health).current = 500;
414 const spells = worker.world.ecs.get(handle, Component.spells);
415 spells.known.push(SpellId.manaBullet);
416 spells.autoCast = true;
417 const skills = worker.world.ecs.get(handle, Component.skills);
418 skills.xp[SkillId.magic] = totalXpForLevel(20);
419 skills.xp[SkillId.mind] = totalXpForLevel(20);
420
421 harness.simulation.enqueue(1, {
422 type: "cast-spell-entity",
423 characterId: character.character_id,
424 spellId: SpellId.manaBullet,
425 targetEntityId: deerId
426 });
427 harness.simulation.step();
428 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet);
429
430 /** Late right-click cancel while the client still thought the spell was armed. */
431 harness.simulation.enqueue(1, {
432 type: "cancel-arm-spell",
433 characterId: character.character_id
434 });
435 harness.simulation.step();
436 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet);
437
438 harness.advance(SPELL_COOLDOWN_MS + 10);
439 const again = harness.simulation.step();
440 assert.ok(
441 again.events.some((e) => e.type === "spell-projectile" || e.type === "spell-fizzle"),
442 "auto-cast must continue after cancel-arm"
443 );
444 }
445 finally
446 {
447 Math.random = random;
448 harness.close();
449 }
450 });
451
452 test("re-arming the same auto-cast spell keeps repeating", () =>
453 {
454 const harness = makeSimulation({ workerCount: 1 });
455 const random = Math.random;
456 Math.random = () => 0.999;
457 try
458 {
459 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
460 const playerId = harness.simulation.spawnCharacter(1, character);
461 const deerId = harness.simulation.spawnEntity(mobComponents({
462 tileX: 10,
463 tileY: 8,
464 mobTypeId: MobId.deer,
465 level: 40
466 }));
467 const worker = workerOf(harness.simulation, playerId);
468 const handle = worker.world.ecs.handleOf(playerId);
469 const deerHandle = worker.world.ecs.handleOf(deerId);
470 worker.world.ecs.get(deerHandle, Component.health).max = 500;
471 worker.world.ecs.get(deerHandle, Component.health).current = 500;
472 const spells = worker.world.ecs.get(handle, Component.spells);
473 spells.known.push(SpellId.manaBullet);
474 spells.autoCast = true;
475 const skills = worker.world.ecs.get(handle, Component.skills);
476 skills.xp[SkillId.magic] = totalXpForLevel(20);
477 skills.xp[SkillId.mind] = totalXpForLevel(20);
478
479 harness.simulation.enqueue(1, {
480 type: "cast-spell-entity",
481 characterId: character.character_id,
482 spellId: SpellId.manaBullet,
483 targetEntityId: deerId
484 });
485 harness.simulation.step();
486 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet);
487
488 harness.simulation.enqueue(1, {
489 type: "arm-spell",
490 characterId: character.character_id,
491 spellId: SpellId.manaBullet
492 });
493 harness.simulation.step();
494 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet);
495 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).armedSpellId, SpellId.manaBullet);
496 }
497 finally
498 {
499 Math.random = random;
500 harness.close();
501 }
502 });
503
394 504 test("auto-retaliate does not engage while auto-casting an attack spell", () =>
395 505 {
396 506 const harness = makeSimulation({ workerCount: 1 });
@@ -391,6 +391,116 @@ test("physical attack cancels spell auto-cast", () =>
391 }
391 }
392});
392});
393
393
394test("cancel-arm after a cast does not clear auto-repeat", () =>
395{
396 const harness = makeSimulation({ workerCount: 1 });
397 const random = Math.random;
398 Math.random = () => 0.999;
399 try
400 {
401 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
402 const playerId = harness.simulation.spawnCharacter(1, character);
403 const deerId = harness.simulation.spawnEntity(mobComponents({
404 tileX: 10,
405 tileY: 8,
406 mobTypeId: MobId.deer,
407 level: 40
408 }));
409 const worker = workerOf(harness.simulation, playerId);
410 const handle = worker.world.ecs.handleOf(playerId);
411 const deerHandle = worker.world.ecs.handleOf(deerId);
412 worker.world.ecs.get(deerHandle, Component.health).max = 500;
413 worker.world.ecs.get(deerHandle, Component.health).current = 500;
414 const spells = worker.world.ecs.get(handle, Component.spells);
415 spells.known.push(SpellId.manaBullet);
416 spells.autoCast = true;
417 const skills = worker.world.ecs.get(handle, Component.skills);
418 skills.xp[SkillId.magic] = totalXpForLevel(20);
419 skills.xp[SkillId.mind] = totalXpForLevel(20);
420
421 harness.simulation.enqueue(1, {
422 type: "cast-spell-entity",
423 characterId: character.character_id,
424 spellId: SpellId.manaBullet,
425 targetEntityId: deerId
426 });
427 harness.simulation.step();
428 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet);
429
430 /** Late right-click cancel while the client still thought the spell was armed. */
431 harness.simulation.enqueue(1, {
432 type: "cancel-arm-spell",
433 characterId: character.character_id
434 });
435 harness.simulation.step();
436 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet);
437
438 harness.advance(SPELL_COOLDOWN_MS + 10);
439 const again = harness.simulation.step();
440 assert.ok(
441 again.events.some((e) => e.type === "spell-projectile" || e.type === "spell-fizzle"),
442 "auto-cast must continue after cancel-arm"
443 );
444 }
445 finally
446 {
447 Math.random = random;
448 harness.close();
449 }
450});
451
452test("re-arming the same auto-cast spell keeps repeating", () =>
453{
454 const harness = makeSimulation({ workerCount: 1 });
455 const random = Math.random;
456 Math.random = () => 0.999;
457 try
458 {
459 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
460 const playerId = harness.simulation.spawnCharacter(1, character);
461 const deerId = harness.simulation.spawnEntity(mobComponents({
462 tileX: 10,
463 tileY: 8,
464 mobTypeId: MobId.deer,
465 level: 40
466 }));
467 const worker = workerOf(harness.simulation, playerId);
468 const handle = worker.world.ecs.handleOf(playerId);
469 const deerHandle = worker.world.ecs.handleOf(deerId);
470 worker.world.ecs.get(deerHandle, Component.health).max = 500;
471 worker.world.ecs.get(deerHandle, Component.health).current = 500;
472 const spells = worker.world.ecs.get(handle, Component.spells);
473 spells.known.push(SpellId.manaBullet);
474 spells.autoCast = true;
475 const skills = worker.world.ecs.get(handle, Component.skills);
476 skills.xp[SkillId.magic] = totalXpForLevel(20);
477 skills.xp[SkillId.mind] = totalXpForLevel(20);
478
479 harness.simulation.enqueue(1, {
480 type: "cast-spell-entity",
481 characterId: character.character_id,
482 spellId: SpellId.manaBullet,
483 targetEntityId: deerId
484 });
485 harness.simulation.step();
486 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet);
487
488 harness.simulation.enqueue(1, {
489 type: "arm-spell",
490 characterId: character.character_id,
491 spellId: SpellId.manaBullet
492 });
493 harness.simulation.step();
494 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet);
495 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).armedSpellId, SpellId.manaBullet);
496 }
497 finally
498 {
499 Math.random = random;
500 harness.close();
501 }
502});
503
394test("auto-retaliate does not engage while auto-casting an attack spell", () =>
504test("auto-retaliate does not engage while auto-casting an attack spell", () =>
395{
505{
396 const harness = makeSimulation({ workerCount: 1 });
506 const harness = makeSimulation({ workerCount: 1 });