Fix spell auto-cast and stop movement when casting or shooting.
Auto-cast never resolved sessionId from the ECS query; it now repeats entity spells and Tinders on a tile until the target dies, the log lights, you move, or you start a physical attack. Casting clears the walk path the same way melee/ranged swings already do.
@@ -473,7 +473,10 @@ export class Simulation
473
473
armedSpellId: null,
474
474
repeatSpellId: null,
475
475
repeatTargetEntityId: null,
476
nextCastAt: 0
476
repeatTileX: null,
477
repeatTileY: null,
478
nextCastAt: 0,
479
pendingCast: null
477
480
},
478
481
};
479
482
if (isDead)
@@ -473,7 +473,10 @@ export class Simulation473
armedSpellId: null,473
armedSpellId: null,474
repeatSpellId: null,474
repeatSpellId: null,475
repeatTargetEntityId: null,475
repeatTargetEntityId: null,476
nextCastAt: 0476
repeatTileX: null,477
repeatTileY: null,478
nextCastAt: 0,479
pendingCast: null477
},480
},478
};481
};479
if (isDead)482
if (isDead)@@ -32,7 +32,7 @@ import {
32
32
setGroundItemsForRegion,
33
33
stripCarriedCoinsForDeath
34
34
} from "../items/bags.js";
35
import { handleSpellCommand, processSpellImpacts, stepPendingSpellCast, stepSpellAutoCast, tickSpellLights } from "../spells/service.js";
35
import { handleSpellCommand, processSpellImpacts, stepPendingSpellCast, stepSpellAutoCast, tickSpellLights, clearSpellAutoRepeat } from "../spells/service.js";
36
36
import { stepSummons } from "../spells/summons.js";
37
37
import { publicSpellsView } from "../../../shared/spells/state.js";
38
38
import {
@@ -834,9 +834,7 @@ export class WorkerWorld
834
834
clearEngage(this.ecs, entityId);
835
835
clearFollow(this.ecs, entityId);
836
836
cancelGather(this.ecs, handle);
837
const casting = this.ecs.get(handle, Component.spellcasting);
838
if (casting)
839
{ casting.pendingCast = null; }
837
clearSpellAutoRepeat(this.ecs.get(handle, Component.spellcasting));
840
838
const position = this.ecs.get(handle, Component.position);
841
839
const movement = this.ecs.get(handle, Component.movement);
842
840
let path = this.ecs.get(handle, Component.path);
@@ -884,6 +882,7 @@ export class WorkerWorld
884
882
{ this.ecs.add(handle, Component.combat, { targetEntityId: null, nextSwingAt: 0 }); }
885
883
clearFollow(this.ecs, entityId);
886
884
cancelGather(this.ecs, handle);
885
clearSpellAutoRepeat(this.ecs.get(handle, Component.spellcasting));
887
886
engageTarget(this.ecs, entityId, targetEntityId, now);
888
887
const position = this.ecs.get(handle, Component.position);
889
888
const profile = attackProfileOf(this.ecs, handle);
@@ -998,8 +997,9 @@ export class WorkerWorld
998
997
999
998
#cancelAttack (sessionId, characterId)
1000
999
{
1001
const { entityId } = this.#requireOwnedEntity(sessionId, characterId);
1000
const { handle, entityId } = this.#requireOwnedEntity(sessionId, characterId);
1002
1001
clearEngage(this.ecs, entityId);
1002
clearSpellAutoRepeat(this.ecs.get(handle, Component.spellcasting));
1003
1003
return { events: [] };
1004
1004
}
1005
1005
@@ -32,7 +32,7 @@ import {32
setGroundItemsForRegion,32
setGroundItemsForRegion,33
stripCarriedCoinsForDeath33
stripCarriedCoinsForDeath34
} from "../items/bags.js";34
} from "../items/bags.js";35
import { handleSpellCommand, processSpellImpacts, stepPendingSpellCast, stepSpellAutoCast, tickSpellLights } from "../spells/service.js";35
import { handleSpellCommand, processSpellImpacts, stepPendingSpellCast, stepSpellAutoCast, tickSpellLights, clearSpellAutoRepeat } from "../spells/service.js";36
import { stepSummons } from "../spells/summons.js";36
import { stepSummons } from "../spells/summons.js";37
import { publicSpellsView } from "../../../shared/spells/state.js";37
import { publicSpellsView } from "../../../shared/spells/state.js";38
import {38
import {@@ -834,9 +834,7 @@ export class WorkerWorld834
clearEngage(this.ecs, entityId);834
clearEngage(this.ecs, entityId);835
clearFollow(this.ecs, entityId);835
clearFollow(this.ecs, entityId);836
cancelGather(this.ecs, handle);836
cancelGather(this.ecs, handle);837
const casting = this.ecs.get(handle, Component.spellcasting);837
clearSpellAutoRepeat(this.ecs.get(handle, Component.spellcasting));838
if (casting)839
{ casting.pendingCast = null; }840
const position = this.ecs.get(handle, Component.position);838
const position = this.ecs.get(handle, Component.position);841
const movement = this.ecs.get(handle, Component.movement);839
const movement = this.ecs.get(handle, Component.movement);842
let path = this.ecs.get(handle, Component.path);840
let path = this.ecs.get(handle, Component.path);@@ -884,6 +882,7 @@ export class WorkerWorld884
{ this.ecs.add(handle, Component.combat, { targetEntityId: null, nextSwingAt: 0 }); }882
{ this.ecs.add(handle, Component.combat, { targetEntityId: null, nextSwingAt: 0 }); }885
clearFollow(this.ecs, entityId);883
clearFollow(this.ecs, entityId);886
cancelGather(this.ecs, handle);884
cancelGather(this.ecs, handle);885
clearSpellAutoRepeat(this.ecs.get(handle, Component.spellcasting));887
engageTarget(this.ecs, entityId, targetEntityId, now);886
engageTarget(this.ecs, entityId, targetEntityId, now);888
const position = this.ecs.get(handle, Component.position);887
const position = this.ecs.get(handle, Component.position);889
const profile = attackProfileOf(this.ecs, handle);888
const profile = attackProfileOf(this.ecs, handle);@@ -998,8 +997,9 @@ export class WorkerWorld998
997
999
#cancelAttack (sessionId, characterId)998
#cancelAttack (sessionId, characterId)1000
{999
{1001
const { entityId } = this.#requireOwnedEntity(sessionId, characterId);1000
const { handle, entityId } = this.#requireOwnedEntity(sessionId, characterId);1002
clearEngage(this.ecs, entityId);1001
clearEngage(this.ecs, entityId);1002
clearSpellAutoRepeat(this.ecs.get(handle, Component.spellcasting));1003
return { events: [] };1003
return { events: [] };1004
}1004
}1005
1005
@@ -37,6 +37,7 @@ import { statusSkillBonuses, applyStatusEffect, emptyStatusEffects, StatusEffect
37
37
import
38
38
{
39
39
chebyshev,
40
clearEngage,
40
41
hasCombatLineOfSight,
41
42
queueStandInRangeApproach,
42
43
resolveCombatTarget,
@@ -48,7 +49,7 @@ import { publicSpellLight, SpellLightRegistry } from "./lights.js";
48
49
import { spellbookInstance, spellbookItemIdForSpell, spellIdFromSpellbookStack } from "../../../shared/definitions/spellItems.js";
49
50
import { itemDefinition } from "../../../shared/definitions/items.js";
50
51
import { bagView } from "../items/bags.js";
51
import { applyTindersAtTile } from "./tinders.js";
52
import { applyTindersAtTile, tindersTileStillNeedsCast } from "./tinders.js";
52
53
53
54
const MAGIC_ATTACK_PROFILE = Object.freeze({
54
55
hitSkillId: SkillId.magic,
@@ -63,13 +64,56 @@ export function ensureSpellComponents (ecs, handle, storedSpells = null)
63
64
}
64
65
if (!ecs.get(handle, Component.spellcasting))
65
66
{
66
ecs.add(handle, Component.spellcasting, {
67
armedSpellId: null,
68
repeatSpellId: null,
69
repeatTargetEntityId: null,
70
nextCastAt: 0
71
});
67
ecs.add(handle, Component.spellcasting, emptySpellcasting());
72
68
}
69
else
70
{ normalizeSpellcasting(ecs.get(handle, Component.spellcasting)); }
71
}
72
73
export function emptySpellcasting ()
74
{
75
return {
76
armedSpellId: null,
77
repeatSpellId: null,
78
repeatTargetEntityId: null,
79
repeatTileX: null,
80
repeatTileY: null,
81
nextCastAt: 0,
82
pendingCast: null
83
};
84
}
85
86
function normalizeSpellcasting (casting)
87
{
88
if (!casting)
89
{ return; }
90
if (!("repeatTileX" in casting))
91
{ casting.repeatTileX = null; }
92
if (!("repeatTileY" in casting))
93
{ casting.repeatTileY = null; }
94
}
95
96
/** Clear auto-repeat / pending cast state (move, melee, cancel, …). */
97
export function clearSpellAutoRepeat (casting)
98
{
99
if (!casting)
100
{ return; }
101
casting.repeatSpellId = null;
102
casting.repeatTargetEntityId = null;
103
casting.repeatTileX = null;
104
casting.repeatTileY = null;
105
casting.pendingCast = null;
106
}
107
108
function stopCasterMovement (world, handle, entityId)
109
{
110
clearEngage(world.ecs, entityId);
111
const path = world.ecs.get(handle, Component.path);
112
if (path?.tiles?.length)
113
{ path.tiles = []; }
114
const movement = world.ecs.get(handle, Component.movement);
115
if (movement)
116
{ movement.walkCarry = 0; }
73
117
}
74
118
75
119
export function handleSpellCommand (world, sessionId, command, now = Date.now())
@@ -154,10 +198,7 @@ function setSpellAutoCast (world, sessionId, command, now)
154
198
spells.autoCast = !!command.enabled;
155
199
const casting = world.ecs.get(handle, Component.spellcasting);
156
200
if (!spells.autoCast)
157
{
158
casting.repeatSpellId = null;
159
casting.repeatTargetEntityId = null;
160
}
201
{ clearSpellAutoRepeat(casting); }
161
202
return {
162
203
events: [spellsUpdatedEvent(entityId, character.characterId, spells, skills)]
163
204
};
@@ -178,9 +219,7 @@ function armSpell (world, sessionId, command, now)
178
219
}
179
220
const casting = world.ecs.get(handle, Component.spellcasting);
180
221
casting.armedSpellId = command.spellId;
181
casting.repeatSpellId = null;
182
casting.repeatTargetEntityId = null;
183
casting.pendingCast = null;
222
clearSpellAutoRepeat(casting);
184
223
return {
185
224
events: [{
186
225
type: "spell-armed",
@@ -198,9 +237,7 @@ function cancelArmSpell (world, sessionId, command)
198
237
if (!casting)
199
238
{ return { events: [] }; }
200
239
casting.armedSpellId = null;
201
casting.repeatSpellId = null;
202
casting.repeatTargetEntityId = null;
203
casting.pendingCast = null;
240
clearSpellAutoRepeat(casting);
204
241
return {
205
242
events: [{
206
243
type: "spell-disarmed",
@@ -278,6 +315,7 @@ function beginCastOrApproach (world, sessionId, characterId, spellId, target, no
278
315
const { handle, entityId } = requirePlayer(world, sessionId, characterId);
279
316
ensureSpellComponents(world.ecs, handle);
280
317
const casting = world.ecs.get(handle, Component.spellcasting);
318
stopCasterMovement(world, handle, entityId);
281
319
const rangeCheck = validateCastRange(world, handle, target.tileX, target.tileY, now);
282
320
if (!rangeCheck.ok)
283
321
{
@@ -366,6 +404,8 @@ function executeCast (world, sessionId, characterId, spellId, target, now)
366
404
throw error;
367
405
}
368
406
407
stopCasterMovement(world, handle, entityId);
408
369
409
const levels = casterMagicLevels(world.ecs, handle, world.settings);
370
410
const difficulty = spellDifficulty(def);
371
411
const cast = resolveSpellCast({
@@ -376,8 +416,6 @@ function executeCast (world, sessionId, characterId, spellId, target, now)
376
416
const events = [];
377
417
const from = world.ecs.get(handle, Component.position);
378
418
casting.armedSpellId = null;
379
casting.repeatSpellId = null;
380
casting.repeatTargetEntityId = null;
381
419
casting.nextCastAt = now + SPELL_COOLDOWN_MS;
382
420
events.push({
383
421
type: "spell-disarmed",
@@ -385,6 +423,9 @@ function executeCast (world, sessionId, characterId, spellId, target, now)
385
423
characterId: character.characterId
386
424
});
387
425
426
// Keep / refresh auto-repeat even on fizzle so we keep trying until cancelled.
427
rememberAutoRepeat(casting, spells, def, spellId, target);
428
388
429
if (!cast.success)
389
430
{
390
431
events.push({
@@ -447,18 +488,35 @@ function executeCast (world, sessionId, characterId, spellId, target, now)
447
488
sprite: def.projectileSprite
448
489
});
449
490
450
if (def.autoRepeat && spells.autoCast && target.targetEntityId != null)
491
return { events };
492
}
493
494
function rememberAutoRepeat (casting, spells, def, spellId, target)
495
{
496
if (!casting || !spells?.autoCast || !def?.autoRepeat)
497
{
498
clearSpellAutoRepeat(casting);
499
return;
500
}
501
if (def.target === "entity" && target?.targetEntityId != null)
451
502
{
452
503
casting.repeatSpellId = spellId;
453
casting.repeatTargetEntityId = target.targetEntityId;
504
casting.repeatTargetEntityId = target.targetEntityId | 0;
505
casting.repeatTileX = null;
506
casting.repeatTileY = null;
507
casting.pendingCast = null;
508
return;
454
509
}
455
else
510
if (def.target === "tile")
456
511
{
457
casting.repeatSpellId = null;
512
casting.repeatSpellId = spellId;
458
513
casting.repeatTargetEntityId = null;
514
casting.repeatTileX = target.tileX | 0;
515
casting.repeatTileY = target.tileY | 0;
516
casting.pendingCast = null;
517
return;
459
518
}
460
461
return { events };
519
clearSpellAutoRepeat(casting);
462
520
}
463
521
464
522
function targetAffinitiesForEntity (world, entityId)
@@ -814,40 +872,81 @@ export function stepPendingSpellCast (world, now = Date.now())
814
872
export function stepSpellAutoCast (world, now = Date.now())
815
873
{
816
874
const events = [];
817
for (const entity of world.ecs.query(Component.spellcasting, Component.spells, Component.character))
875
for (const entity of world.ecs.query(
876
Component.spellcasting,
877
Component.spells,
878
Component.character,
879
Component.networkIdentity
880
))
818
881
{
819
882
const casting = entity.spellcasting;
820
883
const spells = entity.spells;
821
if (!spells.autoCast || !casting.repeatSpellId || casting.repeatTargetEntityId == null)
884
normalizeSpellcasting(casting);
885
if (!spells.autoCast || !casting.repeatSpellId)
822
886
{ continue; }
823
887
if (now < (casting.nextCastAt || 0))
824
888
{ continue; }
825
889
const def = spellDefinition(casting.repeatSpellId);
826
890
if (!def?.autoRepeat)
827
{ continue; }
828
const target = resolveCombatTarget(world, casting.repeatTargetEntityId);
829
if (!target || (target.health && target.health.current <= 0))
830
891
{
831
casting.repeatSpellId = null;
832
casting.repeatTargetEntityId = null;
892
clearSpellAutoRepeat(casting);
833
893
continue;
834
894
}
835
const range = validateCastRange(world, entity.handle, target.pos.tileX, target.pos.tileY, now);
836
if (!range.ok)
837
{ continue; }
838
const sessionId = entity.networkIdentity?.sessionId;
895
896
const sessionId = entity.networkIdentity.sessionId;
839
897
if (!sessionId)
840
898
{ continue; }
899
900
let target = null;
901
let minStandDist = 0;
902
if (casting.repeatTargetEntityId != null)
903
{
904
const resolved = resolveCombatTarget(world, casting.repeatTargetEntityId);
905
if (!resolved || (resolved.health && resolved.health.current <= 0))
906
{
907
clearSpellAutoRepeat(casting);
908
continue;
909
}
910
target = {
911
tileX: resolved.pos.tileX,
912
tileY: resolved.pos.tileY,
913
targetEntityId: casting.repeatTargetEntityId
914
};
915
minStandDist = casting.repeatTargetEntityId === entity.entityId ? 0 : 1;
916
}
917
else if (casting.repeatTileX != null && casting.repeatTileY != null)
918
{
919
if (!tileAutoCastStillValid(world, casting.repeatSpellId, casting.repeatTileX, casting.repeatTileY))
920
{
921
clearSpellAutoRepeat(casting);
922
continue;
923
}
924
target = {
925
tileX: casting.repeatTileX | 0,
926
tileY: casting.repeatTileY | 0,
927
targetEntityId: null
928
};
929
minStandDist = 0;
930
}
931
else
932
{
933
clearSpellAutoRepeat(casting);
934
continue;
935
}
936
937
const range = validateCastRange(world, entity.handle, target.tileX, target.tileY, now);
938
if (!range.ok)
939
{
940
queueSpellApproach(world, entity.handle, entity.entityId, target, minStandDist);
941
continue;
942
}
943
841
944
const result = executeCast(
842
945
world,
843
946
sessionId,
844
947
entity.character.characterId,
845
948
casting.repeatSpellId,
846
{
847
tileX: target.pos.tileX,
848
tileY: target.pos.tileY,
849
targetEntityId: casting.repeatTargetEntityId
850
},
949
target,
851
950
now
852
951
);
853
952
events.push(...(result.events ?? []));
@@ -855,6 +954,16 @@ export function stepSpellAutoCast (world, now = Date.now())
855
954
return events;
856
955
}
857
956
957
function tileAutoCastStillValid (world, spellId, tileX, tileY)
958
{
959
const def = spellDefinition(spellId);
960
if (!def)
961
{ return false; }
962
if (def.effect === "tinders")
963
{ return tindersTileStillNeedsCast(world, tileX, tileY); }
964
return true;
965
}
966
858
967
export function tickSpellLights (world, now = Date.now())
859
968
{
860
969
if (!world.spellLights)
@@ -37,6 +37,7 @@ import { statusSkillBonuses, applyStatusEffect, emptyStatusEffects, StatusEffect37
import37
import38
{38
{39
chebyshev,39
chebyshev,40
clearEngage,40
hasCombatLineOfSight,41
hasCombatLineOfSight,41
queueStandInRangeApproach,42
queueStandInRangeApproach,42
resolveCombatTarget,43
resolveCombatTarget,@@ -48,7 +49,7 @@ import { publicSpellLight, SpellLightRegistry } from "./lights.js";48
import { spellbookInstance, spellbookItemIdForSpell, spellIdFromSpellbookStack } from "../../../shared/definitions/spellItems.js";49
import { spellbookInstance, spellbookItemIdForSpell, spellIdFromSpellbookStack } from "../../../shared/definitions/spellItems.js";49
import { itemDefinition } from "../../../shared/definitions/items.js";50
import { itemDefinition } from "../../../shared/definitions/items.js";50
import { bagView } from "../items/bags.js";51
import { bagView } from "../items/bags.js";51
import { applyTindersAtTile } from "./tinders.js";52
import { applyTindersAtTile, tindersTileStillNeedsCast } from "./tinders.js";52
53
53
const MAGIC_ATTACK_PROFILE = Object.freeze({54
const MAGIC_ATTACK_PROFILE = Object.freeze({54
hitSkillId: SkillId.magic,55
hitSkillId: SkillId.magic,@@ -63,13 +64,56 @@ export function ensureSpellComponents (ecs, handle, storedSpells = null)63
}64
}64
if (!ecs.get(handle, Component.spellcasting))65
if (!ecs.get(handle, Component.spellcasting))65
{66
{66
ecs.add(handle, Component.spellcasting, {67
ecs.add(handle, Component.spellcasting, emptySpellcasting());67
armedSpellId: null,68
repeatSpellId: null,69
repeatTargetEntityId: null,70
nextCastAt: 071
});72
}68
}69
else70
{ normalizeSpellcasting(ecs.get(handle, Component.spellcasting)); }71
}72
73
export function emptySpellcasting ()74
{75
return {76
armedSpellId: null,77
repeatSpellId: null,78
repeatTargetEntityId: null,79
repeatTileX: null,80
repeatTileY: null,81
nextCastAt: 0,82
pendingCast: null83
};84
}85
86
function normalizeSpellcasting (casting)87
{88
if (!casting)89
{ return; }90
if (!("repeatTileX" in casting))91
{ casting.repeatTileX = null; }92
if (!("repeatTileY" in casting))93
{ casting.repeatTileY = null; }94
}95
96
/** Clear auto-repeat / pending cast state (move, melee, cancel, …). */97
export function clearSpellAutoRepeat (casting)98
{99
if (!casting)100
{ return; }101
casting.repeatSpellId = null;102
casting.repeatTargetEntityId = null;103
casting.repeatTileX = null;104
casting.repeatTileY = null;105
casting.pendingCast = null;106
}107
108
function stopCasterMovement (world, handle, entityId)109
{110
clearEngage(world.ecs, entityId);111
const path = world.ecs.get(handle, Component.path);112
if (path?.tiles?.length)113
{ path.tiles = []; }114
const movement = world.ecs.get(handle, Component.movement);115
if (movement)116
{ movement.walkCarry = 0; }73
}117
}74
118
75
export function handleSpellCommand (world, sessionId, command, now = Date.now())119
export function handleSpellCommand (world, sessionId, command, now = Date.now())@@ -154,10 +198,7 @@ function setSpellAutoCast (world, sessionId, command, now)154
spells.autoCast = !!command.enabled;198
spells.autoCast = !!command.enabled;155
const casting = world.ecs.get(handle, Component.spellcasting);199
const casting = world.ecs.get(handle, Component.spellcasting);156
if (!spells.autoCast)200
if (!spells.autoCast)157
{201
{ clearSpellAutoRepeat(casting); }158
casting.repeatSpellId = null;159
casting.repeatTargetEntityId = null;160
}161
return {202
return {162
events: [spellsUpdatedEvent(entityId, character.characterId, spells, skills)]203
events: [spellsUpdatedEvent(entityId, character.characterId, spells, skills)]163
};204
};@@ -178,9 +219,7 @@ function armSpell (world, sessionId, command, now)178
}219
}179
const casting = world.ecs.get(handle, Component.spellcasting);220
const casting = world.ecs.get(handle, Component.spellcasting);180
casting.armedSpellId = command.spellId;221
casting.armedSpellId = command.spellId;181
casting.repeatSpellId = null;222
clearSpellAutoRepeat(casting);182
casting.repeatTargetEntityId = null;183
casting.pendingCast = null;184
return {223
return {185
events: [{224
events: [{186
type: "spell-armed",225
type: "spell-armed",@@ -198,9 +237,7 @@ function cancelArmSpell (world, sessionId, command)198
if (!casting)237
if (!casting)199
{ return { events: [] }; }238
{ return { events: [] }; }200
casting.armedSpellId = null;239
casting.armedSpellId = null;201
casting.repeatSpellId = null;240
clearSpellAutoRepeat(casting);202
casting.repeatTargetEntityId = null;203
casting.pendingCast = null;204
return {241
return {205
events: [{242
events: [{206
type: "spell-disarmed",243
type: "spell-disarmed",@@ -278,6 +315,7 @@ function beginCastOrApproach (world, sessionId, characterId, spellId, target, no278
const { handle, entityId } = requirePlayer(world, sessionId, characterId);315
const { handle, entityId } = requirePlayer(world, sessionId, characterId);279
ensureSpellComponents(world.ecs, handle);316
ensureSpellComponents(world.ecs, handle);280
const casting = world.ecs.get(handle, Component.spellcasting);317
const casting = world.ecs.get(handle, Component.spellcasting);318
stopCasterMovement(world, handle, entityId);281
const rangeCheck = validateCastRange(world, handle, target.tileX, target.tileY, now);319
const rangeCheck = validateCastRange(world, handle, target.tileX, target.tileY, now);282
if (!rangeCheck.ok)320
if (!rangeCheck.ok)283
{321
{@@ -366,6 +404,8 @@ function executeCast (world, sessionId, characterId, spellId, target, now)366
throw error;404
throw error;367
}405
}368
406
407
stopCasterMovement(world, handle, entityId);408
369
const levels = casterMagicLevels(world.ecs, handle, world.settings);409
const levels = casterMagicLevels(world.ecs, handle, world.settings);370
const difficulty = spellDifficulty(def);410
const difficulty = spellDifficulty(def);371
const cast = resolveSpellCast({411
const cast = resolveSpellCast({@@ -376,8 +416,6 @@ function executeCast (world, sessionId, characterId, spellId, target, now)376
const events = [];416
const events = [];377
const from = world.ecs.get(handle, Component.position);417
const from = world.ecs.get(handle, Component.position);378
casting.armedSpellId = null;418
casting.armedSpellId = null;379
casting.repeatSpellId = null;380
casting.repeatTargetEntityId = null;381
casting.nextCastAt = now + SPELL_COOLDOWN_MS;419
casting.nextCastAt = now + SPELL_COOLDOWN_MS;382
events.push({420
events.push({383
type: "spell-disarmed",421
type: "spell-disarmed",@@ -385,6 +423,9 @@ function executeCast (world, sessionId, characterId, spellId, target, now)385
characterId: character.characterId423
characterId: character.characterId386
});424
});387
425
426
// Keep / refresh auto-repeat even on fizzle so we keep trying until cancelled.427
rememberAutoRepeat(casting, spells, def, spellId, target);428
388
if (!cast.success)429
if (!cast.success)389
{430
{390
events.push({431
events.push({@@ -447,18 +488,35 @@ function executeCast (world, sessionId, characterId, spellId, target, now)447
sprite: def.projectileSprite488
sprite: def.projectileSprite448
});489
});449
490
450
if (def.autoRepeat && spells.autoCast && target.targetEntityId != null)491
return { events };492
}493
494
function rememberAutoRepeat (casting, spells, def, spellId, target)495
{496
if (!casting || !spells?.autoCast || !def?.autoRepeat)497
{498
clearSpellAutoRepeat(casting);499
return;500
}501
if (def.target === "entity" && target?.targetEntityId != null)451
{502
{452
casting.repeatSpellId = spellId;503
casting.repeatSpellId = spellId;453
casting.repeatTargetEntityId = target.targetEntityId;504
casting.repeatTargetEntityId = target.targetEntityId | 0;505
casting.repeatTileX = null;506
casting.repeatTileY = null;507
casting.pendingCast = null;508
return;454
}509
}455
else510
if (def.target === "tile")456
{511
{457
casting.repeatSpellId = null;512
casting.repeatSpellId = spellId;458
casting.repeatTargetEntityId = null;513
casting.repeatTargetEntityId = null;514
casting.repeatTileX = target.tileX | 0;515
casting.repeatTileY = target.tileY | 0;516
casting.pendingCast = null;517
return;459
}518
}460
519
clearSpellAutoRepeat(casting);461
return { events };462
}520
}463
521
464
function targetAffinitiesForEntity (world, entityId)522
function targetAffinitiesForEntity (world, entityId)@@ -814,40 +872,81 @@ export function stepPendingSpellCast (world, now = Date.now())814
export function stepSpellAutoCast (world, now = Date.now())872
export function stepSpellAutoCast (world, now = Date.now())815
{873
{816
const events = [];874
const events = [];817
for (const entity of world.ecs.query(Component.spellcasting, Component.spells, Component.character))875
for (const entity of world.ecs.query(876
Component.spellcasting,877
Component.spells,878
Component.character,879
Component.networkIdentity880
))818
{881
{819
const casting = entity.spellcasting;882
const casting = entity.spellcasting;820
const spells = entity.spells;883
const spells = entity.spells;821
if (!spells.autoCast || !casting.repeatSpellId || casting.repeatTargetEntityId == null)884
normalizeSpellcasting(casting);885
if (!spells.autoCast || !casting.repeatSpellId)822
{ continue; }886
{ continue; }823
if (now < (casting.nextCastAt || 0))887
if (now < (casting.nextCastAt || 0))824
{ continue; }888
{ continue; }825
const def = spellDefinition(casting.repeatSpellId);889
const def = spellDefinition(casting.repeatSpellId);826
if (!def?.autoRepeat)890
if (!def?.autoRepeat)827
{ continue; }828
const target = resolveCombatTarget(world, casting.repeatTargetEntityId);829
if (!target || (target.health && target.health.current <= 0))830
{891
{831
casting.repeatSpellId = null;892
clearSpellAutoRepeat(casting);832
casting.repeatTargetEntityId = null;833
continue;893
continue;834
}894
}835
const range = validateCastRange(world, entity.handle, target.pos.tileX, target.pos.tileY, now);895
836
if (!range.ok)896
const sessionId = entity.networkIdentity.sessionId;837
{ continue; }838
const sessionId = entity.networkIdentity?.sessionId;839
if (!sessionId)897
if (!sessionId)840
{ continue; }898
{ continue; }899
900
let target = null;901
let minStandDist = 0;902
if (casting.repeatTargetEntityId != null)903
{904
const resolved = resolveCombatTarget(world, casting.repeatTargetEntityId);905
if (!resolved || (resolved.health && resolved.health.current <= 0))906
{907
clearSpellAutoRepeat(casting);908
continue;909
}910
target = {911
tileX: resolved.pos.tileX,912
tileY: resolved.pos.tileY,913
targetEntityId: casting.repeatTargetEntityId914
};915
minStandDist = casting.repeatTargetEntityId === entity.entityId ? 0 : 1;916
}917
else if (casting.repeatTileX != null && casting.repeatTileY != null)918
{919
if (!tileAutoCastStillValid(world, casting.repeatSpellId, casting.repeatTileX, casting.repeatTileY))920
{921
clearSpellAutoRepeat(casting);922
continue;923
}924
target = {925
tileX: casting.repeatTileX | 0,926
tileY: casting.repeatTileY | 0,927
targetEntityId: null928
};929
minStandDist = 0;930
}931
else932
{933
clearSpellAutoRepeat(casting);934
continue;935
}936
937
const range = validateCastRange(world, entity.handle, target.tileX, target.tileY, now);938
if (!range.ok)939
{940
queueSpellApproach(world, entity.handle, entity.entityId, target, minStandDist);941
continue;942
}943
841
const result = executeCast(944
const result = executeCast(842
world,945
world,843
sessionId,946
sessionId,844
entity.character.characterId,947
entity.character.characterId,845
casting.repeatSpellId,948
casting.repeatSpellId,846
{949
target,847
tileX: target.pos.tileX,848
tileY: target.pos.tileY,849
targetEntityId: casting.repeatTargetEntityId850
},851
now950
now852
);951
);853
events.push(...(result.events ?? []));952
events.push(...(result.events ?? []));@@ -855,6 +954,16 @@ export function stepSpellAutoCast (world, now = Date.now())855
return events;954
return events;856
}955
}857
956
957
function tileAutoCastStillValid (world, spellId, tileX, tileY)958
{959
const def = spellDefinition(spellId);960
if (!def)961
{ return false; }962
if (def.effect === "tinders")963
{ return tindersTileStillNeedsCast(world, tileX, tileY); }964
return true;965
}966
858
export function tickSpellLights (world, now = Date.now())967
export function tickSpellLights (world, now = Date.now())859
{968
{860
if (!world.spellLights)969
if (!world.spellLights)@@ -149,6 +149,22 @@ function spawnGroundStack (world, tileX, tileY, stack, now, events)
149
149
});
150
150
}
151
151
152
/**
153
* True while Tinders still has work on this tile (unlit logs or raw food).
154
* Used to stop auto-cast once a campfire is lit or nothing remains.
155
*/
156
export function tindersTileStillNeedsCast (world, tileX, tileY)
157
{
158
if (blockAt(world, tileX, tileY)?.typeId === BlockType.campfire)
159
{ return false; }
160
const items = groundItemsAt(world, tileX, tileY);
161
if (items.some((item) => woodTierFromLogItemId(item.stack?.itemId)))
162
{ return true; }
163
if (items.some((item) => cookableRawDef(item.stack?.itemId)))
164
{ return true; }
165
return false;
166
}
167
152
168
/**
153
169
* Tinders: Mind roll lights ground logs (as firestarting) or cooks raw meat/fish (40% burn).
154
170
*/
@@ -149,6 +149,22 @@ function spawnGroundStack (world, tileX, tileY, stack, now, events)149
});149
});150
}150
}151
151
152
/**153
* True while Tinders still has work on this tile (unlit logs or raw food).154
* Used to stop auto-cast once a campfire is lit or nothing remains.155
*/156
export function tindersTileStillNeedsCast (world, tileX, tileY)157
{158
if (blockAt(world, tileX, tileY)?.typeId === BlockType.campfire)159
{ return false; }160
const items = groundItemsAt(world, tileX, tileY);161
if (items.some((item) => woodTierFromLogItemId(item.stack?.itemId)))162
{ return true; }163
if (items.some((item) => cookableRawDef(item.stack?.itemId)))164
{ return true; }165
return false;166
}167
152
/**168
/**153
* Tinders: Mind roll lights ground logs (as firestarting) or cooks raw meat/fish (40% burn).169
* Tinders: Mind roll lights ground logs (as firestarting) or cooks raw meat/fish (40% burn).154
*/170
*/@@ -1,7 +1,7 @@
1
1
import test from "node:test";
2
2
import assert from "node:assert/strict";
3
3
import { SpellId, spellDefinition } from "../../shared/definitions/spells.js";
4
import { SPELL_PROJECTILE_MS } from "../../shared/spells/constants.js";
4
import { SPELL_PROJECTILE_MS, SPELL_COOLDOWN_MS } from "../../shared/spells/constants.js";
5
5
import { mobComponents } from "../src/simulation/spawners.js";
6
6
import { MobId } from "../../shared/definitions/mobs.js";
7
7
import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
@@ -16,6 +16,8 @@ import
16
16
import { itemDefinition } from "../../shared/definitions/items.js";
17
17
import { normalizePlayerSpells } from "../../shared/spells/state.js";
18
18
import { createSpellbookStack } from "../src/spells/service.js";
19
import { SkillId } from "../../shared/definitions/skills.js";
20
import { totalXpForLevel } from "../../shared/skills/progression.js";
19
21
20
22
function workerOf (simulation, entityId)
21
23
{
@@ -224,3 +226,167 @@ test("read-spellbook with instance stack also works and rejects duplicates", ()
224
226
finally
225
227
{ harness.close(); }
226
228
});
229
230
test("auto-cast repeats an entity damage spell until cancelled by move", () =>
231
{
232
const harness = makeSimulation({ workerCount: 1 });
233
const random = Math.random;
234
Math.random = () => 0.999;
235
try
236
{
237
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
238
const playerId = harness.simulation.spawnCharacter(1, character);
239
const deerId = harness.simulation.spawnEntity(mobComponents({
240
tileX: 10,
241
tileY: 8,
242
mobTypeId: MobId.deer,
243
level: 40
244
}));
245
const worker = workerOf(harness.simulation, playerId);
246
const handle = worker.world.ecs.handleOf(playerId);
247
const deerHandle = worker.world.ecs.handleOf(deerId);
248
const deerHealth = worker.world.ecs.get(deerHandle, Component.health);
249
deerHealth.max = 500;
250
deerHealth.current = 500;
251
const spells = worker.world.ecs.get(handle, Component.spells);
252
spells.known.push(SpellId.manaBullet);
253
spells.autoCast = true;
254
const skills = worker.world.ecs.get(handle, Component.skills);
255
skills.xp[SkillId.magic] = totalXpForLevel(20);
256
skills.xp[SkillId.mind] = totalXpForLevel(20);
257
258
harness.simulation.enqueue(1, {
259
type: "cast-spell-entity",
260
characterId: character.character_id,
261
spellId: SpellId.manaBullet,
262
targetEntityId: deerId
263
});
264
const first = harness.simulation.step();
265
assert.ok(first.events.some((e) => e.type === "spell-projectile" || e.type === "spell-fizzle"));
266
267
const casting = worker.world.ecs.get(handle, Component.spellcasting);
268
assert.equal(casting.repeatSpellId, SpellId.manaBullet);
269
assert.equal(casting.repeatTargetEntityId, deerId);
270
271
harness.advance(SPELL_COOLDOWN_MS + 10);
272
const second = harness.simulation.step();
273
assert.ok(
274
second.events.some((e) => e.type === "spell-projectile" || e.type === "spell-fizzle"),
275
`expected auto-cast to fire again after cooldown; got ${second.events.map((e) => e.type).join(",")}`
276
);
277
assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet);
278
279
harness.simulation.enqueue(1, {
280
type: "move-to",
281
characterId: character.character_id,
282
tileX: 6,
283
tileY: 8
284
});
285
harness.simulation.step();
286
assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, null);
287
288
harness.advance(SPELL_COOLDOWN_MS + 10);
289
const afterMove = harness.simulation.step();
290
assert.equal(
291
afterMove.events.some((e) => e.type === "spell-projectile" && e.spellId === SpellId.manaBullet),
292
false
293
);
294
}
295
finally
296
{
297
Math.random = random;
298
harness.close();
299
}
300
});
301
302
test("casting a spell clears the walk path", () =>
303
{
304
const harness = makeSimulation({ workerCount: 1 });
305
const random = Math.random;
306
Math.random = () => 0.999;
307
try
308
{
309
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
310
const playerId = harness.simulation.spawnCharacter(1, character);
311
const deerId = harness.simulation.spawnEntity(mobComponents({
312
tileX: 10,
313
tileY: 8,
314
mobTypeId: MobId.deer
315
}));
316
const worker = workerOf(harness.simulation, playerId);
317
const handle = worker.world.ecs.handleOf(playerId);
318
const spells = worker.world.ecs.get(handle, Component.spells);
319
spells.known.push(SpellId.manaBullet);
320
const skills = worker.world.ecs.get(handle, Component.skills);
321
skills.xp[SkillId.magic] = totalXpForLevel(20);
322
323
harness.simulation.enqueue(1, {
324
type: "move-to",
325
characterId: character.character_id,
326
tileX: 14,
327
tileY: 8
328
});
329
harness.simulation.step();
330
assert.ok(worker.world.ecs.get(handle, Component.path)?.tiles?.length > 0);
331
332
harness.simulation.enqueue(1, {
333
type: "cast-spell-entity",
334
characterId: character.character_id,
335
spellId: SpellId.manaBullet,
336
targetEntityId: deerId
337
});
338
harness.simulation.step();
339
assert.equal(worker.world.ecs.get(handle, Component.path)?.tiles?.length ?? 0, 0);
340
}
341
finally
342
{
343
Math.random = random;
344
harness.close();
345
}
346
});
347
348
test("physical attack cancels spell auto-cast", () =>
349
{
350
const harness = makeSimulation({ workerCount: 1 });
351
const random = Math.random;
352
Math.random = () => 0.999;
353
try
354
{
355
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
356
const playerId = harness.simulation.spawnCharacter(1, character);
357
const deerId = harness.simulation.spawnEntity(mobComponents({
358
tileX: 9,
359
tileY: 8,
360
mobTypeId: MobId.deer
361
}));
362
const worker = workerOf(harness.simulation, playerId);
363
const handle = worker.world.ecs.handleOf(playerId);
364
const spells = worker.world.ecs.get(handle, Component.spells);
365
spells.known.push(SpellId.manaBullet);
366
spells.autoCast = true;
367
const skills = worker.world.ecs.get(handle, Component.skills);
368
skills.xp[SkillId.magic] = totalXpForLevel(20);
369
370
harness.simulation.enqueue(1, {
371
type: "cast-spell-entity",
372
characterId: character.character_id,
373
spellId: SpellId.manaBullet,
374
targetEntityId: deerId
375
});
376
harness.simulation.step();
377
assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet);
378
379
harness.simulation.enqueue(1, {
380
type: "attack",
381
characterId: character.character_id,
382
targetEntityId: deerId
383
});
384
harness.simulation.step();
385
assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, null);
386
}
387
finally
388
{
389
Math.random = random;
390
harness.close();
391
}
392
});
@@ -1,7 +1,7 @@1
import test from "node:test";1
import test from "node:test";2
import assert from "node:assert/strict";2
import assert from "node:assert/strict";3
import { SpellId, spellDefinition } from "../../shared/definitions/spells.js";3
import { SpellId, spellDefinition } from "../../shared/definitions/spells.js";4
import { SPELL_PROJECTILE_MS } from "../../shared/spells/constants.js";4
import { SPELL_PROJECTILE_MS, SPELL_COOLDOWN_MS } from "../../shared/spells/constants.js";5
import { mobComponents } from "../src/simulation/spawners.js";5
import { mobComponents } from "../src/simulation/spawners.js";6
import { MobId } from "../../shared/definitions/mobs.js";6
import { MobId } from "../../shared/definitions/mobs.js";7
import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";7
import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";@@ -16,6 +16,8 @@ import16
import { itemDefinition } from "../../shared/definitions/items.js";16
import { itemDefinition } from "../../shared/definitions/items.js";17
import { normalizePlayerSpells } from "../../shared/spells/state.js";17
import { normalizePlayerSpells } from "../../shared/spells/state.js";18
import { createSpellbookStack } from "../src/spells/service.js";18
import { createSpellbookStack } from "../src/spells/service.js";19
import { SkillId } from "../../shared/definitions/skills.js";20
import { totalXpForLevel } from "../../shared/skills/progression.js";19
21
20
function workerOf (simulation, entityId)22
function workerOf (simulation, entityId)21
{23
{@@ -224,3 +226,167 @@ test("read-spellbook with instance stack also works and rejects duplicates", ()224
finally226
finally225
{ harness.close(); }227
{ harness.close(); }226
});228
});229
230
test("auto-cast repeats an entity damage spell until cancelled by move", () =>231
{232
const harness = makeSimulation({ workerCount: 1 });233
const random = Math.random;234
Math.random = () => 0.999;235
try236
{237
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });238
const playerId = harness.simulation.spawnCharacter(1, character);239
const deerId = harness.simulation.spawnEntity(mobComponents({240
tileX: 10,241
tileY: 8,242
mobTypeId: MobId.deer,243
level: 40244
}));245
const worker = workerOf(harness.simulation, playerId);246
const handle = worker.world.ecs.handleOf(playerId);247
const deerHandle = worker.world.ecs.handleOf(deerId);248
const deerHealth = worker.world.ecs.get(deerHandle, Component.health);249
deerHealth.max = 500;250
deerHealth.current = 500;251
const spells = worker.world.ecs.get(handle, Component.spells);252
spells.known.push(SpellId.manaBullet);253
spells.autoCast = true;254
const skills = worker.world.ecs.get(handle, Component.skills);255
skills.xp[SkillId.magic] = totalXpForLevel(20);256
skills.xp[SkillId.mind] = totalXpForLevel(20);257
258
harness.simulation.enqueue(1, {259
type: "cast-spell-entity",260
characterId: character.character_id,261
spellId: SpellId.manaBullet,262
targetEntityId: deerId263
});264
const first = harness.simulation.step();265
assert.ok(first.events.some((e) => e.type === "spell-projectile" || e.type === "spell-fizzle"));266
267
const casting = worker.world.ecs.get(handle, Component.spellcasting);268
assert.equal(casting.repeatSpellId, SpellId.manaBullet);269
assert.equal(casting.repeatTargetEntityId, deerId);270
271
harness.advance(SPELL_COOLDOWN_MS + 10);272
const second = harness.simulation.step();273
assert.ok(274
second.events.some((e) => e.type === "spell-projectile" || e.type === "spell-fizzle"),275
`expected auto-cast to fire again after cooldown; got ${second.events.map((e) => e.type).join(",")}`276
);277
assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet);278
279
harness.simulation.enqueue(1, {280
type: "move-to",281
characterId: character.character_id,282
tileX: 6,283
tileY: 8284
});285
harness.simulation.step();286
assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, null);287
288
harness.advance(SPELL_COOLDOWN_MS + 10);289
const afterMove = harness.simulation.step();290
assert.equal(291
afterMove.events.some((e) => e.type === "spell-projectile" && e.spellId === SpellId.manaBullet),292
false293
);294
}295
finally296
{297
Math.random = random;298
harness.close();299
}300
});301
302
test("casting a spell clears the walk path", () =>303
{304
const harness = makeSimulation({ workerCount: 1 });305
const random = Math.random;306
Math.random = () => 0.999;307
try308
{309
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });310
const playerId = harness.simulation.spawnCharacter(1, character);311
const deerId = harness.simulation.spawnEntity(mobComponents({312
tileX: 10,313
tileY: 8,314
mobTypeId: MobId.deer315
}));316
const worker = workerOf(harness.simulation, playerId);317
const handle = worker.world.ecs.handleOf(playerId);318
const spells = worker.world.ecs.get(handle, Component.spells);319
spells.known.push(SpellId.manaBullet);320
const skills = worker.world.ecs.get(handle, Component.skills);321
skills.xp[SkillId.magic] = totalXpForLevel(20);322
323
harness.simulation.enqueue(1, {324
type: "move-to",325
characterId: character.character_id,326
tileX: 14,327
tileY: 8328
});329
harness.simulation.step();330
assert.ok(worker.world.ecs.get(handle, Component.path)?.tiles?.length > 0);331
332
harness.simulation.enqueue(1, {333
type: "cast-spell-entity",334
characterId: character.character_id,335
spellId: SpellId.manaBullet,336
targetEntityId: deerId337
});338
harness.simulation.step();339
assert.equal(worker.world.ecs.get(handle, Component.path)?.tiles?.length ?? 0, 0);340
}341
finally342
{343
Math.random = random;344
harness.close();345
}346
});347
348
test("physical attack cancels spell auto-cast", () =>349
{350
const harness = makeSimulation({ workerCount: 1 });351
const random = Math.random;352
Math.random = () => 0.999;353
try354
{355
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });356
const playerId = harness.simulation.spawnCharacter(1, character);357
const deerId = harness.simulation.spawnEntity(mobComponents({358
tileX: 9,359
tileY: 8,360
mobTypeId: MobId.deer361
}));362
const worker = workerOf(harness.simulation, playerId);363
const handle = worker.world.ecs.handleOf(playerId);364
const spells = worker.world.ecs.get(handle, Component.spells);365
spells.known.push(SpellId.manaBullet);366
spells.autoCast = true;367
const skills = worker.world.ecs.get(handle, Component.skills);368
skills.xp[SkillId.magic] = totalXpForLevel(20);369
370
harness.simulation.enqueue(1, {371
type: "cast-spell-entity",372
characterId: character.character_id,373
spellId: SpellId.manaBullet,374
targetEntityId: deerId375
});376
harness.simulation.step();377
assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet);378
379
harness.simulation.enqueue(1, {380
type: "attack",381
characterId: character.character_id,382
targetEntityId: deerId383
});384
harness.simulation.step();385
assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, null);386
}387
finally388
{389
Math.random = random;390
harness.close();391
}392
});