Retaliate with staff favorite attack spells.
When a staff is equipped, auto-retaliate casts the first favorited attack spell instead of melee, without requiring the UI auto-cast toggle.
@@ -23,6 +23,12 @@ import { spellDefinition, spellMinMagicLevel } from "../../../shared/definitions
23
23
import { resolvePhysicalAttack } from "../../../shared/combat/rolls.js";
24
24
import { mobDamageAffinities } from "../../../shared/combat/magic.js";
25
25
import { AttackType } from "../../../shared/definitions/attackTypes.js";
26
import
27
{
28
isMagicStaffEquipped,
29
firstFavoriteAttackSpellId
30
} from "../../../shared/combat/maxHit.js";
31
import { canCastSpell } from "../../../shared/spells/state.js";
26
32
import
27
33
{
28
34
effectiveHitSkillInDarkness,
@@ -222,6 +228,59 @@ export function playerBusyWithClickOrder (ecs, handle)
222
228
return !!(path?.tiles?.length);
223
229
}
224
230
231
/**
232
* Staff with a favorited attack spell → entity auto-cast at the attacker;
233
* otherwise melee engage. Returns true when a response was started.
234
*/
235
export function maybePlayerRetaliate (world, playerId, attackerId, now = Date.now())
236
{
237
const ecs = world.ecs;
238
const handle = ecs.handleOf(playerId);
239
if (handle == null || ecs.has(handle, Component.dead))
240
{ return false; }
241
if (playerBusyWithClickOrder(ecs, handle))
242
{ return false; }
243
if (isSpellCombatBusy(ecs, handle))
244
{ return false; }
245
246
const follow = ecs.get(handle, Component.follow);
247
if (follow)
248
{
249
follow.targetEntityId = null;
250
follow.trail = [];
251
}
252
if (ecs.has(handle, Component.gather))
253
{ ecs.remove(handle, Component.gather); }
254
255
const equipment = ecs.get(handle, Component.equipment);
256
const spells = ecs.get(handle, Component.spells);
257
const skills = ecs.get(handle, Component.skills);
258
const casting = ecs.get(handle, Component.spellcasting);
259
if (isMagicStaffEquipped(equipment?.slots) && casting && spells && skills)
260
{
261
const spellId = firstFavoriteAttackSpellId(spells);
262
const def = spellId ? spellDefinition(spellId) : null;
263
if (
264
spellId
265
&& def?.target === "entity"
266
&& def.autoRepeat
267
&& canCastSpell(skills, spells, spellId).ok
268
)
269
{
270
casting.repeatSpellId = spellId;
271
casting.repeatTargetEntityId = attackerId | 0;
272
casting.repeatTileX = null;
273
casting.repeatTileY = null;
274
casting.pendingCast = null;
275
const combat = ecs.get(handle, Component.combat);
276
if (combat)
277
{ combat.targetEntityId = null; }
278
return true;
279
}
280
}
281
return engageTarget(ecs, playerId, attackerId, now);
282
}
283
225
284
export function engageTarget (ecs, attackerId, targetId, now = Date.now())
226
285
{
227
286
const handle = ecs.handleOf(attackerId);
@@ -307,8 +366,7 @@ export function isSpellCombatBusy (ecs, handle)
307
366
{ return false; }
308
367
if (casting.pendingCast)
309
368
{ return true; }
310
const spells = ecs.get(handle, Component.spells);
311
if (!spells?.autoCast || !casting.repeatSpellId)
369
if (!casting.repeatSpellId)
312
370
{ return false; }
313
371
const def = spellDefinition(casting.repeatSpellId);
314
372
return def?.target === "entity";
@@ -1865,18 +1923,8 @@ function applyLocalMeleeHit (
1865
1923
if (attackerIsPlayer === false)
1866
1924
{
1867
1925
const targetCharacter = world.ecs.get(targetHandle, Component.character);
1868
if (targetCharacter && !playerBusyWithClickOrder(world.ecs, targetHandle))
1869
{
1870
engageTarget(world.ecs, targetId, attackerId, now);
1871
const follow = world.ecs.get(targetHandle, Component.follow);
1872
if (follow)
1873
{
1874
follow.targetEntityId = null;
1875
follow.trail = [];
1876
}
1877
if (world.ecs.has(targetHandle, Component.gather))
1878
{ world.ecs.remove(targetHandle, Component.gather); }
1879
}
1926
if (targetCharacter)
1927
{ maybePlayerRetaliate(world, targetId, attackerId, now); }
1880
1928
}
1881
1929
1882
1930
const defenderSkills = world.ecs.get(targetHandle, Component.skills);
@@ -2184,10 +2232,8 @@ export function applyDamageAndKill (world, attackerId, targetId, damage, now = D
2184
2232
if (attackerHandle != null && world.ecs.get(targetHandle, Component.character))
2185
2233
{
2186
2234
const attackerCreature = world.ecs.get(attackerHandle, Component.creature);
2187
if (attackerCreature && !playerBusyWithClickOrder(world.ecs, targetHandle))
2188
{
2189
engageTarget(world.ecs, targetId, attackerId, now);
2190
}
2235
if (attackerCreature)
2236
{ maybePlayerRetaliate(world, targetId, attackerId, now); }
2191
2237
}
2192
2238
2193
2239
const healthXp = awardDamageHealthXp(world.ecs, targetHandle, damage);
@@ -23,6 +23,12 @@ import { spellDefinition, spellMinMagicLevel } from "../../../shared/definitions23
import { resolvePhysicalAttack } from "../../../shared/combat/rolls.js";23
import { resolvePhysicalAttack } from "../../../shared/combat/rolls.js";24
import { mobDamageAffinities } from "../../../shared/combat/magic.js";24
import { mobDamageAffinities } from "../../../shared/combat/magic.js";25
import { AttackType } from "../../../shared/definitions/attackTypes.js";25
import { AttackType } from "../../../shared/definitions/attackTypes.js";26
import27
{28
isMagicStaffEquipped,29
firstFavoriteAttackSpellId30
} from "../../../shared/combat/maxHit.js";31
import { canCastSpell } from "../../../shared/spells/state.js";26
import32
import27
{33
{28
effectiveHitSkillInDarkness,34
effectiveHitSkillInDarkness,@@ -222,6 +228,59 @@ export function playerBusyWithClickOrder (ecs, handle)222
return !!(path?.tiles?.length);228
return !!(path?.tiles?.length);223
}229
}224
230
231
/**232
* Staff with a favorited attack spell → entity auto-cast at the attacker;233
* otherwise melee engage. Returns true when a response was started.234
*/235
export function maybePlayerRetaliate (world, playerId, attackerId, now = Date.now())236
{237
const ecs = world.ecs;238
const handle = ecs.handleOf(playerId);239
if (handle == null || ecs.has(handle, Component.dead))240
{ return false; }241
if (playerBusyWithClickOrder(ecs, handle))242
{ return false; }243
if (isSpellCombatBusy(ecs, handle))244
{ return false; }245
246
const follow = ecs.get(handle, Component.follow);247
if (follow)248
{249
follow.targetEntityId = null;250
follow.trail = [];251
}252
if (ecs.has(handle, Component.gather))253
{ ecs.remove(handle, Component.gather); }254
255
const equipment = ecs.get(handle, Component.equipment);256
const spells = ecs.get(handle, Component.spells);257
const skills = ecs.get(handle, Component.skills);258
const casting = ecs.get(handle, Component.spellcasting);259
if (isMagicStaffEquipped(equipment?.slots) && casting && spells && skills)260
{261
const spellId = firstFavoriteAttackSpellId(spells);262
const def = spellId ? spellDefinition(spellId) : null;263
if (264
spellId265
&& def?.target === "entity"266
&& def.autoRepeat267
&& canCastSpell(skills, spells, spellId).ok268
)269
{270
casting.repeatSpellId = spellId;271
casting.repeatTargetEntityId = attackerId | 0;272
casting.repeatTileX = null;273
casting.repeatTileY = null;274
casting.pendingCast = null;275
const combat = ecs.get(handle, Component.combat);276
if (combat)277
{ combat.targetEntityId = null; }278
return true;279
}280
}281
return engageTarget(ecs, playerId, attackerId, now);282
}283
225
export function engageTarget (ecs, attackerId, targetId, now = Date.now())284
export function engageTarget (ecs, attackerId, targetId, now = Date.now())226
{285
{227
const handle = ecs.handleOf(attackerId);286
const handle = ecs.handleOf(attackerId);@@ -307,8 +366,7 @@ export function isSpellCombatBusy (ecs, handle)307
{ return false; }366
{ return false; }308
if (casting.pendingCast)367
if (casting.pendingCast)309
{ return true; }368
{ return true; }310
const spells = ecs.get(handle, Component.spells);369
if (!casting.repeatSpellId)311
if (!spells?.autoCast || !casting.repeatSpellId)312
{ return false; }370
{ return false; }313
const def = spellDefinition(casting.repeatSpellId);371
const def = spellDefinition(casting.repeatSpellId);314
return def?.target === "entity";372
return def?.target === "entity";@@ -1865,18 +1923,8 @@ function applyLocalMeleeHit (1865
if (attackerIsPlayer === false)1923
if (attackerIsPlayer === false)1866
{1924
{1867
const targetCharacter = world.ecs.get(targetHandle, Component.character);1925
const targetCharacter = world.ecs.get(targetHandle, Component.character);1868
if (targetCharacter && !playerBusyWithClickOrder(world.ecs, targetHandle))1926
if (targetCharacter)1869
{1927
{ maybePlayerRetaliate(world, targetId, attackerId, now); }1870
engageTarget(world.ecs, targetId, attackerId, now);1871
const follow = world.ecs.get(targetHandle, Component.follow);1872
if (follow)1873
{1874
follow.targetEntityId = null;1875
follow.trail = [];1876
}1877
if (world.ecs.has(targetHandle, Component.gather))1878
{ world.ecs.remove(targetHandle, Component.gather); }1879
}1880
}1928
}1881
1929
1882
const defenderSkills = world.ecs.get(targetHandle, Component.skills);1930
const defenderSkills = world.ecs.get(targetHandle, Component.skills);@@ -2184,10 +2232,8 @@ export function applyDamageAndKill (world, attackerId, targetId, damage, now = D2184
if (attackerHandle != null && world.ecs.get(targetHandle, Component.character))2232
if (attackerHandle != null && world.ecs.get(targetHandle, Component.character))2185
{2233
{2186
const attackerCreature = world.ecs.get(attackerHandle, Component.creature);2234
const attackerCreature = world.ecs.get(attackerHandle, Component.creature);2187
if (attackerCreature && !playerBusyWithClickOrder(world.ecs, targetHandle))2235
if (attackerCreature)2188
{2236
{ maybePlayerRetaliate(world, targetId, attackerId, now); }2189
engageTarget(world.ecs, targetId, attackerId, now);2190
}2191
}2237
}2192
2238
2193
const healthXp = awardDamageHealthXp(world.ecs, targetHandle, damage);2239
const healthXp = awardDamageHealthXp(world.ecs, targetHandle, damage);@@ -514,7 +514,18 @@ function executeCast (world, sessionId, characterId, spellId, target, now)
514
514
515
515
function rememberAutoRepeat (casting, spells, def, spellId, target)
516
516
{
517
if (!casting || !spells?.autoCast || !def?.autoRepeat)
517
if (!casting || !def?.autoRepeat)
518
{
519
clearSpellAutoRepeat(casting);
520
return;
521
}
522
const continuingEntityRepeat = !!(
523
casting.repeatSpellId === spellId
524
&& casting.repeatTargetEntityId != null
525
&& target?.targetEntityId != null
526
&& def.target === "entity"
527
);
528
if (!spells?.autoCast && !continuingEntityRepeat)
518
529
{
519
530
clearSpellAutoRepeat(casting);
520
531
return;
@@ -530,6 +541,11 @@ function rememberAutoRepeat (casting, spells, def, spellId, target)
530
541
}
531
542
if (def.target === "tile")
532
543
{
544
if (!spells?.autoCast)
545
{
546
clearSpellAutoRepeat(casting);
547
return;
548
}
533
549
casting.repeatSpellId = spellId;
534
550
casting.repeatTargetEntityId = null;
535
551
casting.repeatTileX = target.tileX | 0;
@@ -920,7 +936,7 @@ export function stepSpellAutoCast (world, now = Date.now())
920
936
const casting = entity.spellcasting;
921
937
const spells = entity.spells;
922
938
normalizeSpellcasting(casting);
923
if (!spells.autoCast || !casting.repeatSpellId)
939
if (!casting.repeatSpellId)
924
940
{ continue; }
925
941
if (now < (casting.nextCastAt || 0))
926
942
{ continue; }
@@ -930,6 +946,12 @@ export function stepSpellAutoCast (world, now = Date.now())
930
946
clearSpellAutoRepeat(casting);
931
947
continue;
932
948
}
949
/** UI auto-cast toggle gates starting repeats; combat retaliate may set repeat without it. */
950
if (!spells.autoCast && casting.repeatTargetEntityId == null)
951
{
952
clearSpellAutoRepeat(casting);
953
continue;
954
}
933
955
934
956
const sessionId = entity.networkIdentity.sessionId;
935
957
if (!sessionId)
@@ -514,7 +514,18 @@ function executeCast (world, sessionId, characterId, spellId, target, now)514
514
515
function rememberAutoRepeat (casting, spells, def, spellId, target)515
function rememberAutoRepeat (casting, spells, def, spellId, target)516
{516
{517
if (!casting || !spells?.autoCast || !def?.autoRepeat)517
if (!casting || !def?.autoRepeat)518
{519
clearSpellAutoRepeat(casting);520
return;521
}522
const continuingEntityRepeat = !!(523
casting.repeatSpellId === spellId524
&& casting.repeatTargetEntityId != null525
&& target?.targetEntityId != null526
&& def.target === "entity"527
);528
if (!spells?.autoCast && !continuingEntityRepeat)518
{529
{519
clearSpellAutoRepeat(casting);530
clearSpellAutoRepeat(casting);520
return;531
return;@@ -530,6 +541,11 @@ function rememberAutoRepeat (casting, spells, def, spellId, target)530
}541
}531
if (def.target === "tile")542
if (def.target === "tile")532
{543
{544
if (!spells?.autoCast)545
{546
clearSpellAutoRepeat(casting);547
return;548
}533
casting.repeatSpellId = spellId;549
casting.repeatSpellId = spellId;534
casting.repeatTargetEntityId = null;550
casting.repeatTargetEntityId = null;535
casting.repeatTileX = target.tileX | 0;551
casting.repeatTileX = target.tileX | 0;@@ -920,7 +936,7 @@ export function stepSpellAutoCast (world, now = Date.now())920
const casting = entity.spellcasting;936
const casting = entity.spellcasting;921
const spells = entity.spells;937
const spells = entity.spells;922
normalizeSpellcasting(casting);938
normalizeSpellcasting(casting);923
if (!spells.autoCast || !casting.repeatSpellId)939
if (!casting.repeatSpellId)924
{ continue; }940
{ continue; }925
if (now < (casting.nextCastAt || 0))941
if (now < (casting.nextCastAt || 0))926
{ continue; }942
{ continue; }@@ -930,6 +946,12 @@ export function stepSpellAutoCast (world, now = Date.now())930
clearSpellAutoRepeat(casting);946
clearSpellAutoRepeat(casting);931
continue;947
continue;932
}948
}949
/** UI auto-cast toggle gates starting repeats; combat retaliate may set repeat without it. */950
if (!spells.autoCast && casting.repeatTargetEntityId == null)951
{952
clearSpellAutoRepeat(casting);953
continue;954
}933
955
934
const sessionId = entity.networkIdentity.sessionId;956
const sessionId = entity.networkIdentity.sessionId;935
if (!sessionId)957
if (!sessionId)@@ -12,6 +12,7 @@ import { tileKey } from "../src/world/coordinates.js";
12
12
import { WorkerMessage } from "../src/simulation/protocol.js";
13
13
import { tileToRegion } from "../src/world/coordinates.js";
14
14
import { SkillId } from "../../shared/definitions/skills.js";
15
import { SpellId } from "../../shared/definitions/spells.js";
15
16
import { totalXpForLevel } from "../../shared/skills/progression.js";
16
17
import { resolveMeleeSwing, attackProfileOf, hasCombatLineOfSight, queueMeleeApproach, markSeekCover, engageTarget, applyDamageAndKill, markFleeOnHit, stepCombat, chebyshev } from "../src/combat/service.js";
17
18
import { combatDamageRollLevel } from "../../shared/combat/rolls.js";
@@ -681,6 +682,81 @@ test("players retaliate when a mob swings at them", () =>
681
682
{ harness.close(); }
682
683
});
683
684
685
test("staff with favorite attack spell retaliates by auto-casting it", () =>
686
{
687
const harness = makeSimulation({ workerCount: 1 });
688
try
689
{
690
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
691
const playerId = harness.simulation.spawnCharacter(1, character);
692
const ratId = harness.simulation.spawnEntity(mobComponents({
693
tileX: 9,
694
tileY: 8,
695
mobTypeId: MobId.rat,
696
spawnerId: null
697
}));
698
const worker = workerOf(harness.simulation, playerId);
699
const handle = worker.world.ecs.handleOf(playerId);
700
const equipment = worker.world.ecs.get(handle, Component.equipment);
701
equipment.slots.weapon = { itemId: GemItemId.pineStaff, quantity: 1 };
702
const spells = worker.world.ecs.get(handle, Component.spells);
703
spells.known.push(SpellId.manaBullet);
704
spells.favorites = [SpellId.light, SpellId.manaBullet];
705
spells.autoCast = false;
706
707
const profile = attackProfileOf(worker.world.ecs, worker.world.ecs.handleOf(ratId));
708
resolveMeleeSwing(worker.world, ratId, playerId, profile, Date.now());
709
710
const casting = worker.world.ecs.get(handle, Component.spellcasting);
711
assert.equal(casting.repeatSpellId, SpellId.manaBullet);
712
assert.equal(casting.repeatTargetEntityId, ratId);
713
assert.equal(
714
componentsOf(harness.simulation, playerId).combat?.targetEntityId ?? null,
715
null,
716
"staff spell retaliate should not melee-engage"
717
);
718
}
719
finally
720
{ harness.close(); }
721
});
722
723
test("staff without favorite attack spell retaliates with melee", () =>
724
{
725
const harness = makeSimulation({ workerCount: 1 });
726
try
727
{
728
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
729
const playerId = harness.simulation.spawnCharacter(1, character);
730
const ratId = harness.simulation.spawnEntity(mobComponents({
731
tileX: 9,
732
tileY: 8,
733
mobTypeId: MobId.rat,
734
spawnerId: null
735
}));
736
const worker = workerOf(harness.simulation, playerId);
737
const handle = worker.world.ecs.handleOf(playerId);
738
const equipment = worker.world.ecs.get(handle, Component.equipment);
739
equipment.slots.weapon = { itemId: GemItemId.pineStaff, quantity: 1 };
740
const spells = worker.world.ecs.get(handle, Component.spells);
741
spells.favorites = [SpellId.light];
742
743
const profile = attackProfileOf(worker.world.ecs, worker.world.ecs.handleOf(ratId));
744
resolveMeleeSwing(worker.world, ratId, playerId, profile, Date.now());
745
746
assert.equal(
747
componentsOf(harness.simulation, playerId).combat?.targetEntityId,
748
ratId,
749
"staff without attack favorites should melee-retaliate"
750
);
751
assert.equal(
752
worker.world.ecs.get(handle, Component.spellcasting)?.repeatSpellId ?? null,
753
null
754
);
755
}
756
finally
757
{ harness.close(); }
758
});
759
684
760
test("players do not auto-retaliate while walking from a click order", () =>
685
761
{
686
762
const harness = makeSimulation({ workerCount: 1 });
@@ -12,6 +12,7 @@ import { tileKey } from "../src/world/coordinates.js";12
import { WorkerMessage } from "../src/simulation/protocol.js";12
import { WorkerMessage } from "../src/simulation/protocol.js";13
import { tileToRegion } from "../src/world/coordinates.js";13
import { tileToRegion } from "../src/world/coordinates.js";14
import { SkillId } from "../../shared/definitions/skills.js";14
import { SkillId } from "../../shared/definitions/skills.js";15
import { SpellId } from "../../shared/definitions/spells.js";15
import { totalXpForLevel } from "../../shared/skills/progression.js";16
import { totalXpForLevel } from "../../shared/skills/progression.js";16
import { resolveMeleeSwing, attackProfileOf, hasCombatLineOfSight, queueMeleeApproach, markSeekCover, engageTarget, applyDamageAndKill, markFleeOnHit, stepCombat, chebyshev } from "../src/combat/service.js";17
import { resolveMeleeSwing, attackProfileOf, hasCombatLineOfSight, queueMeleeApproach, markSeekCover, engageTarget, applyDamageAndKill, markFleeOnHit, stepCombat, chebyshev } from "../src/combat/service.js";17
import { combatDamageRollLevel } from "../../shared/combat/rolls.js";18
import { combatDamageRollLevel } from "../../shared/combat/rolls.js";@@ -681,6 +682,81 @@ test("players retaliate when a mob swings at them", () =>681
{ harness.close(); }682
{ harness.close(); }682
});683
});683
684
685
test("staff with favorite attack spell retaliates by auto-casting it", () =>686
{687
const harness = makeSimulation({ workerCount: 1 });688
try689
{690
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });691
const playerId = harness.simulation.spawnCharacter(1, character);692
const ratId = harness.simulation.spawnEntity(mobComponents({693
tileX: 9,694
tileY: 8,695
mobTypeId: MobId.rat,696
spawnerId: null697
}));698
const worker = workerOf(harness.simulation, playerId);699
const handle = worker.world.ecs.handleOf(playerId);700
const equipment = worker.world.ecs.get(handle, Component.equipment);701
equipment.slots.weapon = { itemId: GemItemId.pineStaff, quantity: 1 };702
const spells = worker.world.ecs.get(handle, Component.spells);703
spells.known.push(SpellId.manaBullet);704
spells.favorites = [SpellId.light, SpellId.manaBullet];705
spells.autoCast = false;706
707
const profile = attackProfileOf(worker.world.ecs, worker.world.ecs.handleOf(ratId));708
resolveMeleeSwing(worker.world, ratId, playerId, profile, Date.now());709
710
const casting = worker.world.ecs.get(handle, Component.spellcasting);711
assert.equal(casting.repeatSpellId, SpellId.manaBullet);712
assert.equal(casting.repeatTargetEntityId, ratId);713
assert.equal(714
componentsOf(harness.simulation, playerId).combat?.targetEntityId ?? null,715
null,716
"staff spell retaliate should not melee-engage"717
);718
}719
finally720
{ harness.close(); }721
});722
723
test("staff without favorite attack spell retaliates with melee", () =>724
{725
const harness = makeSimulation({ workerCount: 1 });726
try727
{728
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });729
const playerId = harness.simulation.spawnCharacter(1, character);730
const ratId = harness.simulation.spawnEntity(mobComponents({731
tileX: 9,732
tileY: 8,733
mobTypeId: MobId.rat,734
spawnerId: null735
}));736
const worker = workerOf(harness.simulation, playerId);737
const handle = worker.world.ecs.handleOf(playerId);738
const equipment = worker.world.ecs.get(handle, Component.equipment);739
equipment.slots.weapon = { itemId: GemItemId.pineStaff, quantity: 1 };740
const spells = worker.world.ecs.get(handle, Component.spells);741
spells.favorites = [SpellId.light];742
743
const profile = attackProfileOf(worker.world.ecs, worker.world.ecs.handleOf(ratId));744
resolveMeleeSwing(worker.world, ratId, playerId, profile, Date.now());745
746
assert.equal(747
componentsOf(harness.simulation, playerId).combat?.targetEntityId,748
ratId,749
"staff without attack favorites should melee-retaliate"750
);751
assert.equal(752
worker.world.ecs.get(handle, Component.spellcasting)?.repeatSpellId ?? null,753
null754
);755
}756
finally757
{ harness.close(); }758
});759
684
test("players do not auto-retaliate while walking from a click order", () =>760
test("players do not auto-retaliate while walking from a click order", () =>685
{761
{686
const harness = makeSimulation({ workerCount: 1 });762
const harness = makeSimulation({ workerCount: 1 });