BlueGit

timfa questward

Fix staff Attack click to hold spell range via auto-cast.

Clicking attack with a staff and a favorite entity attack spell now arms the same auto-cast path as retaliate instead of melee-approaching adjacent.

timfa Tim Falken · pushed by timfa

Authored 2026-09-02 · 2 months ago

Hash
b482642198d92441b6f9509303638297272cdfdb
Parent
c8fd6334
3 files changed +100 −27
server/src/combat/service.js +42 −26
@@ -246,6 +246,46 @@ export function playerBusyWithClickOrder (ecs, handle)
246 246 * Staff with a favorited attack spell → entity auto-cast at the attacker;
247 247 * otherwise melee engage. Returns true when a response was started.
248 248 */
249 /**
250 * Staff + favorite entity attack spell: arm auto-cast at the target instead of melee engage.
251 * Used by click-Attack and auto-retaliate so casters hold spell range.
252 */
253 export function tryArmStaffSpellAttack (ecs, playerId, targetEntityId)
254 {
255 const handle = ecs.handleOf(playerId);
256 if (handle == null)
257 { return false; }
258 const equipment = ecs.get(handle, Component.equipment);
259 const spells = ecs.get(handle, Component.spells);
260 const skills = ecs.get(handle, Component.skills);
261 const casting = ecs.get(handle, Component.spellcasting);
262 if (!(isMagicStaffEquipped(equipment?.slots) && casting && spells && skills))
263 { return false; }
264 const spellId = firstFavoriteAttackSpellId(spells);
265 const def = spellId ? spellDefinition(spellId) : null;
266 if (
267 !(
268 spellId
269 && def?.target === "entity"
270 && def.autoRepeat
271 && canCastSpell(skills, spells, spellId).ok
272 )
273 )
274 { return false; }
275 casting.repeatSpellId = spellId;
276 casting.repeatTargetEntityId = targetEntityId | 0;
277 casting.repeatTileX = null;
278 casting.repeatTileY = null;
279 casting.pendingCast = null;
280 const combat = ecs.get(handle, Component.combat);
281 if (combat)
282 { combat.targetEntityId = null; }
283 const path = ecs.get(handle, Component.path);
284 if (path)
285 { path.tiles = []; }
286 return true;
287 }
288
249 289 export function maybePlayerRetaliate (world, playerId, attackerId, now = Date.now())
250 290 {
251 291 const ecs = world.ecs;
@@ -266,32 +306,8 @@ export function maybePlayerRetaliate (world, playerId, attackerId, now = Date.no
266 306 if (ecs.has(handle, Component.gather))
267 307 { ecs.remove(handle, Component.gather); }
268 308
269 const equipment = ecs.get(handle, Component.equipment);
270 const spells = ecs.get(handle, Component.spells);
271 const skills = ecs.get(handle, Component.skills);
272 const casting = ecs.get(handle, Component.spellcasting);
273 if (isMagicStaffEquipped(equipment?.slots) && casting && spells && skills)
274 {
275 const spellId = firstFavoriteAttackSpellId(spells);
276 const def = spellId ? spellDefinition(spellId) : null;
277 if (
278 spellId
279 && def?.target === "entity"
280 && def.autoRepeat
281 && canCastSpell(skills, spells, spellId).ok
282 )
283 {
284 casting.repeatSpellId = spellId;
285 casting.repeatTargetEntityId = attackerId | 0;
286 casting.repeatTileX = null;
287 casting.repeatTileY = null;
288 casting.pendingCast = null;
289 const combat = ecs.get(handle, Component.combat);
290 if (combat)
291 { combat.targetEntityId = null; }
292 return true;
293 }
294 }
309 if (tryArmStaffSpellAttack(ecs, playerId, attackerId))
310 { return true; }
295 311 return engageTarget(ecs, playerId, attackerId, now);
296 312 }
297 313
@@ -246,6 +246,46 @@ export function playerBusyWithClickOrder (ecs, handle)
246 * Staff with a favorited attack spell → entity auto-cast at the attacker;
246 * Staff with a favorited attack spell → entity auto-cast at the attacker;
247 * otherwise melee engage. Returns true when a response was started.
247 * otherwise melee engage. Returns true when a response was started.
248 */
248 */
249/**
250 * Staff + favorite entity attack spell: arm auto-cast at the target instead of melee engage.
251 * Used by click-Attack and auto-retaliate so casters hold spell range.
252 */
253export function tryArmStaffSpellAttack (ecs, playerId, targetEntityId)
254{
255 const handle = ecs.handleOf(playerId);
256 if (handle == null)
257 { return false; }
258 const equipment = ecs.get(handle, Component.equipment);
259 const spells = ecs.get(handle, Component.spells);
260 const skills = ecs.get(handle, Component.skills);
261 const casting = ecs.get(handle, Component.spellcasting);
262 if (!(isMagicStaffEquipped(equipment?.slots) && casting && spells && skills))
263 { return false; }
264 const spellId = firstFavoriteAttackSpellId(spells);
265 const def = spellId ? spellDefinition(spellId) : null;
266 if (
267 !(
268 spellId
269 && def?.target === "entity"
270 && def.autoRepeat
271 && canCastSpell(skills, spells, spellId).ok
272 )
273 )
274 { return false; }
275 casting.repeatSpellId = spellId;
276 casting.repeatTargetEntityId = targetEntityId | 0;
277 casting.repeatTileX = null;
278 casting.repeatTileY = null;
279 casting.pendingCast = null;
280 const combat = ecs.get(handle, Component.combat);
281 if (combat)
282 { combat.targetEntityId = null; }
283 const path = ecs.get(handle, Component.path);
284 if (path)
285 { path.tiles = []; }
286 return true;
287}
288
249export function maybePlayerRetaliate (world, playerId, attackerId, now = Date.now())
289export function maybePlayerRetaliate (world, playerId, attackerId, now = Date.now())
250{
290{
251 const ecs = world.ecs;
291 const ecs = world.ecs;
@@ -266,32 +306,8 @@ export function maybePlayerRetaliate (world, playerId, attackerId, now = Date.no
266 if (ecs.has(handle, Component.gather))
306 if (ecs.has(handle, Component.gather))
267 { ecs.remove(handle, Component.gather); }
307 { ecs.remove(handle, Component.gather); }
268
308
269 const equipment = ecs.get(handle, Component.equipment);
309 if (tryArmStaffSpellAttack(ecs, playerId, attackerId))
270 const spells = ecs.get(handle, Component.spells);
310 { return true; }
271 const skills = ecs.get(handle, Component.skills);
272 const casting = ecs.get(handle, Component.spellcasting);
273 if (isMagicStaffEquipped(equipment?.slots) && casting && spells && skills)
274 {
275 const spellId = firstFavoriteAttackSpellId(spells);
276 const def = spellId ? spellDefinition(spellId) : null;
277 if (
278 spellId
279 && def?.target === "entity"
280 && def.autoRepeat
281 && canCastSpell(skills, spells, spellId).ok
282 )
283 {
284 casting.repeatSpellId = spellId;
285 casting.repeatTargetEntityId = attackerId | 0;
286 casting.repeatTileX = null;
287 casting.repeatTileY = null;
288 casting.pendingCast = null;
289 const combat = ecs.get(handle, Component.combat);
290 if (combat)
291 { combat.targetEntityId = null; }
292 return true;
293 }
294 }
295 return engageTarget(ecs, playerId, attackerId, now);
311 return engageTarget(ecs, playerId, attackerId, now);
296}
312}
297
313
server/src/simulation/workerWorld.js +5 −1
@@ -72,7 +72,8 @@ import {
72 72 stepAggression,
73 73 stepCombat,
74 74 stepPassiveHealthRegen,
75 chebyshev
75 chebyshev,
76 tryArmStaffSpellAttack
76 77 } from "../combat/service.js";
77 78 import { awardCombatHitXp, syncPlayerStamina } from "../skills/service.js";
78 79 import { SkillId } from "../../../shared/definitions/skills.js";
@@ -968,6 +969,9 @@ export class WorkerWorld
968 969 { this.ecs.add(handle, Component.combat, { targetEntityId: null, nextSwingAt: 0 }); }
969 970 clearFollow(this.ecs, entityId);
970 971 cancelGather(this.ecs, handle);
972 // Staff + favorite attack spell: hold spell range via auto-cast (same as retaliate).
973 if (tryArmStaffSpellAttack(this.ecs, entityId, targetEntityId))
974 { return { events: [] }; }
971 975 clearSpellAutoRepeat(this.ecs.get(handle, Component.spellcasting));
972 976 engageTarget(this.ecs, entityId, targetEntityId, now);
973 977 const position = this.ecs.get(handle, Component.position);
@@ -72,7 +72,8 @@ import {
72 stepAggression,
72 stepAggression,
73 stepCombat,
73 stepCombat,
74 stepPassiveHealthRegen,
74 stepPassiveHealthRegen,
75 chebyshev
75 chebyshev,
76 tryArmStaffSpellAttack
76} from "../combat/service.js";
77} from "../combat/service.js";
77import { awardCombatHitXp, syncPlayerStamina } from "../skills/service.js";
78import { awardCombatHitXp, syncPlayerStamina } from "../skills/service.js";
78import { SkillId } from "../../../shared/definitions/skills.js";
79import { SkillId } from "../../../shared/definitions/skills.js";
@@ -968,6 +969,9 @@ export class WorkerWorld
968 { this.ecs.add(handle, Component.combat, { targetEntityId: null, nextSwingAt: 0 }); }
969 { this.ecs.add(handle, Component.combat, { targetEntityId: null, nextSwingAt: 0 }); }
969 clearFollow(this.ecs, entityId);
970 clearFollow(this.ecs, entityId);
970 cancelGather(this.ecs, handle);
971 cancelGather(this.ecs, handle);
972 // Staff + favorite attack spell: hold spell range via auto-cast (same as retaliate).
973 if (tryArmStaffSpellAttack(this.ecs, entityId, targetEntityId))
974 { return { events: [] }; }
971 clearSpellAutoRepeat(this.ecs.get(handle, Component.spellcasting));
975 clearSpellAutoRepeat(this.ecs.get(handle, Component.spellcasting));
972 engageTarget(this.ecs, entityId, targetEntityId, now);
976 engageTarget(this.ecs, entityId, targetEntityId, now);
973 const position = this.ecs.get(handle, Component.position);
977 const position = this.ecs.get(handle, Component.position);
server/tests/combat.test.js +53 −0
@@ -757,6 +757,59 @@ test("staff without favorite attack spell retaliates with melee", () =>
757 757 { harness.close(); }
758 758 });
759 759
760 test("staff click-attack arms favorite spell instead of melee approach", () =>
761 {
762 const harness = makeSimulation({ workerCount: 1 });
763 try
764 {
765 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
766 const playerId = harness.simulation.spawnCharacter(1, character);
767 // Beyond spell range so auto-cast queues a stand-in-range path (not melee adjacency).
768 const ratId = harness.simulation.spawnEntity(mobComponents({
769 tileX: 28,
770 tileY: 8,
771 mobTypeId: MobId.rat,
772 spawnerId: null
773 }));
774 const worker = workerOf(harness.simulation, playerId);
775 const handle = worker.world.ecs.handleOf(playerId);
776 const equipment = worker.world.ecs.get(handle, Component.equipment);
777 equipment.slots.weapon = { itemId: GemItemId.pineStaff, quantity: 1 };
778 const spells = worker.world.ecs.get(handle, Component.spells);
779 spells.known.push(SpellId.manaBullet);
780 spells.favorites = [SpellId.manaBullet];
781 spells.autoCast = false;
782
783 harness.simulation.enqueue(1, {
784 type: "attack",
785 characterId: character.character_id,
786 targetEntityId: ratId
787 });
788 harness.simulation.step();
789
790 const casting = worker.world.ecs.get(handle, Component.spellcasting);
791 assert.equal(casting.repeatSpellId, SpellId.manaBullet);
792 assert.equal(casting.repeatTargetEntityId, ratId);
793 assert.equal(
794 componentsOf(harness.simulation, playerId).combat?.targetEntityId ?? null,
795 null,
796 "staff click-attack should not melee-engage"
797 );
798
799 const path = componentsOf(harness.simulation, playerId).path;
800 const tiles = path?.tiles ?? [];
801 assert.ok(tiles.length > 0, "should approach into spell range");
802 const last = tiles[tiles.length - 1];
803 const standDist = chebyshev(last.tileX, last.tileY, 28, 8);
804 assert.ok(
805 standDist > 1,
806 `spell approach should stop outside melee adjacency, got dist=${standDist}`
807 );
808 }
809 finally
810 { harness.close(); }
811 });
812
760 813 test("players do not auto-retaliate while walking from a click order", () =>
761 814 {
762 815 const harness = makeSimulation({ workerCount: 1 });
@@ -757,6 +757,59 @@ test("staff without favorite attack spell retaliates with melee", () =>
757 { harness.close(); }
757 { harness.close(); }
758});
758});
759
759
760test("staff click-attack arms favorite spell instead of melee approach", () =>
761{
762 const harness = makeSimulation({ workerCount: 1 });
763 try
764 {
765 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
766 const playerId = harness.simulation.spawnCharacter(1, character);
767 // Beyond spell range so auto-cast queues a stand-in-range path (not melee adjacency).
768 const ratId = harness.simulation.spawnEntity(mobComponents({
769 tileX: 28,
770 tileY: 8,
771 mobTypeId: MobId.rat,
772 spawnerId: null
773 }));
774 const worker = workerOf(harness.simulation, playerId);
775 const handle = worker.world.ecs.handleOf(playerId);
776 const equipment = worker.world.ecs.get(handle, Component.equipment);
777 equipment.slots.weapon = { itemId: GemItemId.pineStaff, quantity: 1 };
778 const spells = worker.world.ecs.get(handle, Component.spells);
779 spells.known.push(SpellId.manaBullet);
780 spells.favorites = [SpellId.manaBullet];
781 spells.autoCast = false;
782
783 harness.simulation.enqueue(1, {
784 type: "attack",
785 characterId: character.character_id,
786 targetEntityId: ratId
787 });
788 harness.simulation.step();
789
790 const casting = worker.world.ecs.get(handle, Component.spellcasting);
791 assert.equal(casting.repeatSpellId, SpellId.manaBullet);
792 assert.equal(casting.repeatTargetEntityId, ratId);
793 assert.equal(
794 componentsOf(harness.simulation, playerId).combat?.targetEntityId ?? null,
795 null,
796 "staff click-attack should not melee-engage"
797 );
798
799 const path = componentsOf(harness.simulation, playerId).path;
800 const tiles = path?.tiles ?? [];
801 assert.ok(tiles.length > 0, "should approach into spell range");
802 const last = tiles[tiles.length - 1];
803 const standDist = chebyshev(last.tileX, last.tileY, 28, 8);
804 assert.ok(
805 standDist > 1,
806 `spell approach should stop outside melee adjacency, got dist=${standDist}`
807 );
808 }
809 finally
810 { harness.close(); }
811});
812
760test("players do not auto-retaliate while walking from a click order", () =>
813test("players do not auto-retaliate while walking from a click order", () =>
761{
814{
762 const harness = makeSimulation({ workerCount: 1 });
815 const harness = makeSimulation({ workerCount: 1 });