BlueGit

timfa questward

Route staff LMB attack through cast-spell at favourite range.

Client sends arm+cast instead of melee Attack when a staff and castable favourite attack spell are ready; server keeps entity auto-repeat for that staff combat cast without requiring the auto-cast toggle.

timfa Tim Falken · pushed by timfa

Authored 2026-09-02 · 2 months ago

Hash
cdef12b48d06186eb10fef497d6e31bdb95308b9
Parent
85950896
5 files changed +130 −4
client/src/play/main.js +13 −0
@@ -996,6 +996,19 @@ function requestAttack (targetEntityId)
996 996 {
997 997 clearAllPendingActions();
998 998 setAutorunFocusEntity(targetEntityId);
999 // Staff + castable favourite attack spell: use the spell cast/approach path (spell range),
1000 // not melee Attack. Server keeps auto-repeat for that staff combat target.
1001 const staffSpellId = spells?.getStaffAttackSpellId?.();
1002 if (staffSpellId)
1003 {
1004 socket.send({ type: MessageType.armSpell, spellId: staffSpellId });
1005 socket.send({
1006 type: MessageType.castSpellEntity,
1007 spellId: staffSpellId,
1008 targetEntityId
1009 });
1010 return;
1011 }
999 1012 socket.send({ type: MessageType.attack, targetEntityId });
1000 1013 }
1001 1014
@@ -996,6 +996,19 @@ function requestAttack (targetEntityId)
996{
996{
997 clearAllPendingActions();
997 clearAllPendingActions();
998 setAutorunFocusEntity(targetEntityId);
998 setAutorunFocusEntity(targetEntityId);
999 // Staff + castable favourite attack spell: use the spell cast/approach path (spell range),
1000 // not melee Attack. Server keeps auto-repeat for that staff combat target.
1001 const staffSpellId = spells?.getStaffAttackSpellId?.();
1002 if (staffSpellId)
1003 {
1004 socket.send({ type: MessageType.armSpell, spellId: staffSpellId });
1005 socket.send({
1006 type: MessageType.castSpellEntity,
1007 spellId: staffSpellId,
1008 targetEntityId
1009 });
1010 return;
1011 }
999 socket.send({ type: MessageType.attack, targetEntityId });
1012 socket.send({ type: MessageType.attack, targetEntityId });
1000}
1013}
1001
1014
client/src/play/spells.js +17 −0
@@ -72,6 +72,22 @@ export function initSpells ({
72 72 renderBar();
73 73 }
74 74
75 /**
76 * Favourite attack spell used for staff LMB / retaliate, or null if unequipped / unavailable.
77 */
78 function getStaffAttackSpellId ()
79 {
80 if (!isMagicStaffEquipped(getEquipmentSlots?.() ?? {}))
81 { return null; }
82 const spellId = firstFavoriteAttackSpellId({ favorites: favoriteOrder });
83 if (!spellId)
84 { return null; }
85 const spell = spells.find((entry) => entry.spellId === spellId);
86 if (!spell?.canCast || spell.target !== "entity")
87 { return null; }
88 return spellId;
89 }
90
75 91 function disarmLocal ()
76 92 {
77 93 if (!armedSpellId)
@@ -403,6 +419,7 @@ export function initSpells ({
403 419 return spells.map((spell) => spell.spellId);
404 420 },
405 421 refreshMaxHitDisplay,
422 getStaffAttackSpellId,
406 423 handleSpellDisarmed ()
407 424 {
408 425 disarmLocal();
@@ -72,6 +72,22 @@ export function initSpells ({
72 renderBar();
72 renderBar();
73 }
73 }
74
74
75 /**
76 * Favourite attack spell used for staff LMB / retaliate, or null if unequipped / unavailable.
77 */
78 function getStaffAttackSpellId ()
79 {
80 if (!isMagicStaffEquipped(getEquipmentSlots?.() ?? {}))
81 { return null; }
82 const spellId = firstFavoriteAttackSpellId({ favorites: favoriteOrder });
83 if (!spellId)
84 { return null; }
85 const spell = spells.find((entry) => entry.spellId === spellId);
86 if (!spell?.canCast || spell.target !== "entity")
87 { return null; }
88 return spellId;
89 }
90
75 function disarmLocal ()
91 function disarmLocal ()
76 {
92 {
77 if (!armedSpellId)
93 if (!armedSpellId)
@@ -403,6 +419,7 @@ export function initSpells ({
403 return spells.map((spell) => spell.spellId);
419 return spells.map((spell) => spell.spellId);
404 },
420 },
405 refreshMaxHitDisplay,
421 refreshMaxHitDisplay,
422 getStaffAttackSpellId,
406 handleSpellDisarmed ()
423 handleSpellDisarmed ()
407 {
424 {
408 disarmLocal();
425 disarmLocal();
server/src/combat/service.js +13 −1
@@ -258,7 +258,19 @@ export function tryArmStaffSpellAttack (ecs, playerId, targetEntityId)
258 258 const equipment = ecs.get(handle, Component.equipment);
259 259 const spells = ecs.get(handle, Component.spells);
260 260 const skills = ecs.get(handle, Component.skills);
261 const casting = ecs.get(handle, Component.spellcasting);
261 let casting = ecs.get(handle, Component.spellcasting);
262 if (!casting && spells)
263 {
264 casting = ecs.add(handle, Component.spellcasting, {
265 armedSpellId: null,
266 repeatSpellId: null,
267 repeatTargetEntityId: null,
268 repeatTileX: null,
269 repeatTileY: null,
270 nextCastAt: 0,
271 pendingCast: null
272 });
273 }
262 274 if (!(isMagicStaffEquipped(equipment?.slots) && casting && spells && skills))
263 275 { return false; }
264 276 const spellId = firstFavoriteAttackSpellId(spells);
@@ -258,7 +258,19 @@ export function tryArmStaffSpellAttack (ecs, playerId, targetEntityId)
258 const equipment = ecs.get(handle, Component.equipment);
258 const equipment = ecs.get(handle, Component.equipment);
259 const spells = ecs.get(handle, Component.spells);
259 const spells = ecs.get(handle, Component.spells);
260 const skills = ecs.get(handle, Component.skills);
260 const skills = ecs.get(handle, Component.skills);
261 const casting = ecs.get(handle, Component.spellcasting);
261 let casting = ecs.get(handle, Component.spellcasting);
262 if (!casting && spells)
263 {
264 casting = ecs.add(handle, Component.spellcasting, {
265 armedSpellId: null,
266 repeatSpellId: null,
267 repeatTargetEntityId: null,
268 repeatTileX: null,
269 repeatTileY: null,
270 nextCastAt: 0,
271 pendingCast: null
272 });
273 }
262 if (!(isMagicStaffEquipped(equipment?.slots) && casting && spells && skills))
274 if (!(isMagicStaffEquipped(equipment?.slots) && casting && spells && skills))
263 { return false; }
275 { return false; }
264 const spellId = firstFavoriteAttackSpellId(spells);
276 const spellId = firstFavoriteAttackSpellId(spells);
server/src/spells/service.js +37 −3
@@ -36,6 +36,11 @@ import { mobDefinition } from "../../../shared/definitions/mobs.js";
36 36 import { awardCombatHitXp, awardDamageHealthXp, awardSpellCastXp, syncPlayerHealth } from "../skills/service.js";
37 37 import { equipmentBonusesFromSlots, effectiveSkillLevel } from "../../../shared/equipment/stats.js";
38 38 import
39 {
40 isMagicStaffEquipped,
41 firstFavoriteAttackSpellId
42 } from "../../../shared/combat/maxHit.js";
43 import
39 44 {
40 45 attackerSpellCastRng,
41 46 hitSkillBonus,
@@ -369,6 +374,7 @@ function beginCastOrApproach (world, sessionId, characterId, spellId, target, no
369 374 ensureSpellComponents(world.ecs, handle);
370 375 const casting = world.ecs.get(handle, Component.spellcasting);
371 376 stopCasterMovement(world, handle, entityId);
377 armStaffCombatRepeatIfApplicable(world.ecs, handle, spellId, target);
372 378 const rangeCheck = validateCastRange(world, handle, target.tileX, target.tileY, now);
373 379 if (!rangeCheck.ok)
374 380 {
@@ -385,6 +391,33 @@ function beginCastOrApproach (world, sessionId, characterId, spellId, target, no
385 391 return executeCast(world, sessionId, characterId, spellId, target, now);
386 392 }
387 393
394 /** Staff LMB / favourite attack cast: keep entity auto-repeat without the auto-cast toggle. */
395 function armStaffCombatRepeatIfApplicable (ecs, handle, spellId, target)
396 {
397 const equipment = ecs.get(handle, Component.equipment);
398 const spells = ecs.get(handle, Component.spells);
399 const casting = ecs.get(handle, Component.spellcasting);
400 const def = spellDefinition(spellId);
401 if (
402 !casting
403 || !spells
404 || !def?.autoRepeat
405 || def.target !== "entity"
406 || target?.targetEntityId == null
407 || !isMagicStaffEquipped(equipment?.slots)
408 || firstFavoriteAttackSpellId(spells) !== spellId
409 )
410 { return false; }
411 casting.repeatSpellId = spellId;
412 casting.repeatTargetEntityId = target.targetEntityId | 0;
413 casting.repeatTileX = null;
414 casting.repeatTileY = null;
415 const combat = ecs.get(handle, Component.combat);
416 if (combat)
417 { combat.targetEntityId = null; }
418 return true;
419 }
420
388 421 function castSpellAtTile (world, sessionId, command, now)
389 422 {
390 423 const spellId = command.spellId;
@@ -479,7 +512,8 @@ function executeCast (world, sessionId, characterId, spellId, target, now)
479 512 });
480 513
481 514 // Keep / refresh auto-repeat even on fizzle so we keep trying until cancelled.
482 rememberAutoRepeat(casting, spells, def, spellId, target);
515 const staffCombat = armStaffCombatRepeatIfApplicable(world.ecs, handle, spellId, target);
516 rememberAutoRepeat(casting, spells, def, spellId, target, { staffCombat });
483 517
484 518 if (!cast.success)
485 519 {
@@ -547,7 +581,7 @@ function executeCast (world, sessionId, characterId, spellId, target, now)
547 581 return { events };
548 582 }
549 583
550 function rememberAutoRepeat (casting, spells, def, spellId, target)
584 function rememberAutoRepeat (casting, spells, def, spellId, target, { staffCombat = false } = {})
551 585 {
552 586 if (!casting || !def?.autoRepeat)
553 587 {
@@ -560,7 +594,7 @@ function rememberAutoRepeat (casting, spells, def, spellId, target)
560 594 && target?.targetEntityId != null
561 595 && def.target === "entity"
562 596 );
563 if (!spells?.autoCast && !continuingEntityRepeat)
597 if (!spells?.autoCast && !continuingEntityRepeat && !staffCombat)
564 598 {
565 599 clearSpellAutoRepeat(casting);
566 600 return;
@@ -36,6 +36,11 @@ import { mobDefinition } from "../../../shared/definitions/mobs.js";
36import { awardCombatHitXp, awardDamageHealthXp, awardSpellCastXp, syncPlayerHealth } from "../skills/service.js";
36import { awardCombatHitXp, awardDamageHealthXp, awardSpellCastXp, syncPlayerHealth } from "../skills/service.js";
37import { equipmentBonusesFromSlots, effectiveSkillLevel } from "../../../shared/equipment/stats.js";
37import { equipmentBonusesFromSlots, effectiveSkillLevel } from "../../../shared/equipment/stats.js";
38import
38import
39{
40 isMagicStaffEquipped,
41 firstFavoriteAttackSpellId
42} from "../../../shared/combat/maxHit.js";
43import
39{
44{
40 attackerSpellCastRng,
45 attackerSpellCastRng,
41 hitSkillBonus,
46 hitSkillBonus,
@@ -369,6 +374,7 @@ function beginCastOrApproach (world, sessionId, characterId, spellId, target, no
369 ensureSpellComponents(world.ecs, handle);
374 ensureSpellComponents(world.ecs, handle);
370 const casting = world.ecs.get(handle, Component.spellcasting);
375 const casting = world.ecs.get(handle, Component.spellcasting);
371 stopCasterMovement(world, handle, entityId);
376 stopCasterMovement(world, handle, entityId);
377 armStaffCombatRepeatIfApplicable(world.ecs, handle, spellId, target);
372 const rangeCheck = validateCastRange(world, handle, target.tileX, target.tileY, now);
378 const rangeCheck = validateCastRange(world, handle, target.tileX, target.tileY, now);
373 if (!rangeCheck.ok)
379 if (!rangeCheck.ok)
374 {
380 {
@@ -385,6 +391,33 @@ function beginCastOrApproach (world, sessionId, characterId, spellId, target, no
385 return executeCast(world, sessionId, characterId, spellId, target, now);
391 return executeCast(world, sessionId, characterId, spellId, target, now);
386}
392}
387
393
394/** Staff LMB / favourite attack cast: keep entity auto-repeat without the auto-cast toggle. */
395function armStaffCombatRepeatIfApplicable (ecs, handle, spellId, target)
396{
397 const equipment = ecs.get(handle, Component.equipment);
398 const spells = ecs.get(handle, Component.spells);
399 const casting = ecs.get(handle, Component.spellcasting);
400 const def = spellDefinition(spellId);
401 if (
402 !casting
403 || !spells
404 || !def?.autoRepeat
405 || def.target !== "entity"
406 || target?.targetEntityId == null
407 || !isMagicStaffEquipped(equipment?.slots)
408 || firstFavoriteAttackSpellId(spells) !== spellId
409 )
410 { return false; }
411 casting.repeatSpellId = spellId;
412 casting.repeatTargetEntityId = target.targetEntityId | 0;
413 casting.repeatTileX = null;
414 casting.repeatTileY = null;
415 const combat = ecs.get(handle, Component.combat);
416 if (combat)
417 { combat.targetEntityId = null; }
418 return true;
419}
420
388function castSpellAtTile (world, sessionId, command, now)
421function castSpellAtTile (world, sessionId, command, now)
389{
422{
390 const spellId = command.spellId;
423 const spellId = command.spellId;
@@ -479,7 +512,8 @@ function executeCast (world, sessionId, characterId, spellId, target, now)
479 });
512 });
480
513
481 // Keep / refresh auto-repeat even on fizzle so we keep trying until cancelled.
514 // Keep / refresh auto-repeat even on fizzle so we keep trying until cancelled.
482 rememberAutoRepeat(casting, spells, def, spellId, target);
515 const staffCombat = armStaffCombatRepeatIfApplicable(world.ecs, handle, spellId, target);
516 rememberAutoRepeat(casting, spells, def, spellId, target, { staffCombat });
483
517
484 if (!cast.success)
518 if (!cast.success)
485 {
519 {
@@ -547,7 +581,7 @@ function executeCast (world, sessionId, characterId, spellId, target, now)
547 return { events };
581 return { events };
548}
582}
549
583
550function rememberAutoRepeat (casting, spells, def, spellId, target)
584function rememberAutoRepeat (casting, spells, def, spellId, target, { staffCombat = false } = {})
551{
585{
552 if (!casting || !def?.autoRepeat)
586 if (!casting || !def?.autoRepeat)
553 {
587 {
@@ -560,7 +594,7 @@ function rememberAutoRepeat (casting, spells, def, spellId, target)
560 && target?.targetEntityId != null
594 && target?.targetEntityId != null
561 && def.target === "entity"
595 && def.target === "entity"
562 );
596 );
563 if (!spells?.autoCast && !continuingEntityRepeat)
597 if (!spells?.autoCast && !continuingEntityRepeat && !staffCombat)
564 {
598 {
565 clearSpellAutoRepeat(casting);
599 clearSpellAutoRepeat(casting);
566 return;
600 return;
server/tests/combat.test.js +50 −0
@@ -810,6 +810,56 @@ test("staff click-attack arms favorite spell instead of melee approach", () =>
810 810 { harness.close(); }
811 811 });
812 812
813 test("staff cast-spell-entity approaches spell range and keeps auto-repeat without autoCast", () =>
814 {
815 const harness = makeSimulation({ workerCount: 1 });
816 try
817 {
818 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
819 const playerId = harness.simulation.spawnCharacter(1, character);
820 const ratId = harness.simulation.spawnEntity(mobComponents({
821 tileX: 28,
822 tileY: 8,
823 mobTypeId: MobId.rat,
824 spawnerId: null
825 }));
826 const worker = workerOf(harness.simulation, playerId);
827 const handle = worker.world.ecs.handleOf(playerId);
828 const equipment = worker.world.ecs.get(handle, Component.equipment);
829 equipment.slots.weapon = { itemId: GemItemId.pineStaff, quantity: 1 };
830 const spells = worker.world.ecs.get(handle, Component.spells);
831 spells.known.push(SpellId.manaBullet);
832 spells.favorites = [SpellId.manaBullet];
833 spells.autoCast = false;
834 const skills = worker.world.ecs.get(handle, Component.skills);
835 skills.xp[SkillId.magic] = totalXpForLevel(20);
836
837 harness.simulation.enqueue(1, {
838 type: "cast-spell-entity",
839 characterId: character.character_id,
840 spellId: SpellId.manaBullet,
841 targetEntityId: ratId
842 });
843 harness.simulation.step();
844
845 const casting = worker.world.ecs.get(handle, Component.spellcasting);
846 assert.equal(casting.repeatSpellId, SpellId.manaBullet);
847 assert.equal(casting.repeatTargetEntityId, ratId);
848 assert.equal(casting.pendingCast?.spellId, SpellId.manaBullet);
849 assert.equal(
850 componentsOf(harness.simulation, playerId).combat?.targetEntityId ?? null,
851 null
852 );
853 const path = componentsOf(harness.simulation, playerId).path;
854 const tiles = path?.tiles ?? [];
855 assert.ok(tiles.length > 0, "should approach into spell range");
856 const last = tiles[tiles.length - 1];
857 assert.ok(chebyshev(last.x, last.y, 28, 8) > 1);
858 }
859 finally
860 { harness.close(); }
861 });
862
813 863 test("players do not auto-retaliate while walking from a click order", () =>
814 864 {
815 865 const harness = makeSimulation({ workerCount: 1 });
@@ -810,6 +810,56 @@ test("staff click-attack arms favorite spell instead of melee approach", () =>
810 { harness.close(); }
810 { harness.close(); }
811});
811});
812
812
813test("staff cast-spell-entity approaches spell range and keeps auto-repeat without autoCast", () =>
814{
815 const harness = makeSimulation({ workerCount: 1 });
816 try
817 {
818 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
819 const playerId = harness.simulation.spawnCharacter(1, character);
820 const ratId = harness.simulation.spawnEntity(mobComponents({
821 tileX: 28,
822 tileY: 8,
823 mobTypeId: MobId.rat,
824 spawnerId: null
825 }));
826 const worker = workerOf(harness.simulation, playerId);
827 const handle = worker.world.ecs.handleOf(playerId);
828 const equipment = worker.world.ecs.get(handle, Component.equipment);
829 equipment.slots.weapon = { itemId: GemItemId.pineStaff, quantity: 1 };
830 const spells = worker.world.ecs.get(handle, Component.spells);
831 spells.known.push(SpellId.manaBullet);
832 spells.favorites = [SpellId.manaBullet];
833 spells.autoCast = false;
834 const skills = worker.world.ecs.get(handle, Component.skills);
835 skills.xp[SkillId.magic] = totalXpForLevel(20);
836
837 harness.simulation.enqueue(1, {
838 type: "cast-spell-entity",
839 characterId: character.character_id,
840 spellId: SpellId.manaBullet,
841 targetEntityId: ratId
842 });
843 harness.simulation.step();
844
845 const casting = worker.world.ecs.get(handle, Component.spellcasting);
846 assert.equal(casting.repeatSpellId, SpellId.manaBullet);
847 assert.equal(casting.repeatTargetEntityId, ratId);
848 assert.equal(casting.pendingCast?.spellId, SpellId.manaBullet);
849 assert.equal(
850 componentsOf(harness.simulation, playerId).combat?.targetEntityId ?? null,
851 null
852 );
853 const path = componentsOf(harness.simulation, playerId).path;
854 const tiles = path?.tiles ?? [];
855 assert.ok(tiles.length > 0, "should approach into spell range");
856 const last = tiles[tiles.length - 1];
857 assert.ok(chebyshev(last.x, last.y, 28, 8) > 1);
858 }
859 finally
860 { harness.close(); }
861});
862
813test("players do not auto-retaliate while walking from a click order", () =>
863test("players do not auto-retaliate while walking from a click order", () =>
814{
864{
815 const harness = makeSimulation({ workerCount: 1 });
865 const harness = makeSimulation({ workerCount: 1 });