Keep only one combat form active; staff melees as clubs.
Auto-cast blocks retaliate and physical swings; attack clears casting. Staves swing like quarterstaves for clubs XP while magic stays an explicit cast.
@@ -163,6 +163,9 @@ export function engageTarget (ecs, attackerId, targetId, now = Date.now())
163
163
const handle = ecs.handleOf(attackerId);
164
164
if (handle == null)
165
165
{ return false; }
166
// Spell auto-cast / pending cast is already the active combat form.
167
if (isSpellCombatBusy(ecs, handle))
168
{ return false; }
166
169
let combat = ecs.get(handle, Component.combat);
167
170
if (!combat)
168
171
{ combat = ecs.add(handle, Component.combat, { targetEntityId: null, nextSwingAt: 0 }); }
@@ -179,6 +182,33 @@ export function engageTarget (ecs, attackerId, targetId, now = Date.now())
179
182
return true;
180
183
}
181
184
185
/** Pending cast or entity-spell auto-repeat — physical combat must not run. */
186
export function isSpellCombatBusy (ecs, handle)
187
{
188
const casting = ecs.get(handle, Component.spellcasting);
189
if (!casting)
190
{ return false; }
191
if (casting.pendingCast)
192
{ return true; }
193
const spells = ecs.get(handle, Component.spells);
194
if (!spells?.autoCast || !casting.repeatSpellId)
195
{ return false; }
196
const def = spellDefinition(casting.repeatSpellId);
197
return def?.target === "entity";
198
}
199
200
function clearSpellCombatState (ecs, handle)
201
{
202
const casting = ecs.get(handle, Component.spellcasting);
203
if (!casting)
204
{ return; }
205
casting.repeatSpellId = null;
206
casting.repeatTargetEntityId = null;
207
casting.repeatTileX = null;
208
casting.repeatTileY = null;
209
casting.pendingCast = null;
210
}
211
182
212
export function clearEngage (ecs, entityId)
183
213
{
184
214
const handle = ecs.handleOf(entityId);
@@ -527,12 +557,14 @@ export function stepCombat (world, now = Date.now())
527
557
continue;
528
558
}
529
559
560
// One combat form at a time: spell cast / auto-cast wins over melee & archery.
561
if (isSpellCombatBusy(world.ecs, entity.handle))
562
{ continue; }
563
530
564
const profile = attackProfileOf(world.ecs, entity.handle);
531
565
const targetPos = target.pos;
532
566
const isCreature = !!world.ecs.get(entity.handle, Component.creature)
533
567
&& !world.ecs.get(entity.handle, Component.character);
534
const weaponKind = weaponEquipmentKind(world.ecs, entity.handle);
535
const isStaff = weaponKind === EquipmentKind.staff;
536
568
537
569
if (profile.mode === "ranged")
538
570
{
@@ -568,21 +600,21 @@ export function stepCombat (world, now = Date.now())
568
600
if (now < (combat.nextSwingAt || 0))
569
601
{ continue; }
570
602
combat.nextSwingAt = now + (profile.cooldownMs | 0 || 1200);
603
clearSpellCombatState(world.ecs, entity.handle);
571
604
events.push(...resolveRangedShot(world, entity.entityId, targetId, profile, now, targetPos));
572
605
continue;
573
606
}
574
607
575
if (profile.mode !== "melee" && !isStaff)
608
if (profile.mode !== "melee")
576
609
{ continue; }
577
610
578
611
const dist = chebyshev(entity.position.tileX, entity.position.tileY, targetPos.tileX, targetPos.tileY);
579
const canSwing = !isStaff
580
&& dist === 1
612
const canSwing = dist === 1
581
613
&& world.isClearStanding?.(entity.entityId, entity.position.tileX, entity.position.tileY);
582
614
const attackSpells = isCreature ? eligibleMobAttackSpells(world.ecs, entity.handle) : [];
583
615
const preferSpell = isCreature
584
616
&& !!mobDefinition(world.ecs.get(entity.handle, Component.creature)?.mobTypeId)?.preferSpellAttack;
585
const shouldCast = isCreature && attackSpells.length > 0 && (isStaff || !canSwing || preferSpell);
617
const shouldCast = isCreature && attackSpells.length > 0 && (!canSwing || preferSpell);
586
618
587
619
if (shouldCast)
588
620
{
@@ -624,21 +656,7 @@ export function stepCombat (world, now = Date.now())
624
656
625
657
if (!canSwing)
626
658
{
627
if (isStaff)
628
{
629
queueStandInRangeApproach(
630
world,
631
entity.entityId,
632
entity.handle,
633
entity.position,
634
targetPos.tileX,
635
targetPos.tileY,
636
COMBAT_RANGE_TILES,
637
1
638
);
639
}
640
else
641
{ queueMeleeApproach(world, entity.entityId, entity.handle, entity.position, targetPos); }
659
queueMeleeApproach(world, entity.entityId, entity.handle, entity.position, targetPos);
642
660
continue;
643
661
}
644
662
clearCombatApproachPath(world.ecs, entity.handle);
@@ -649,6 +667,7 @@ export function stepCombat (world, now = Date.now())
649
667
{ continue; }
650
668
651
669
combat.nextSwingAt = now + (profile.cooldownMs || 800);
670
clearSpellCombatState(world.ecs, entity.handle);
652
671
events.push(...resolveMeleeSwing(world, entity.entityId, targetId, profile, now));
653
672
}
654
673
return events;
@@ -163,6 +163,9 @@ export function engageTarget (ecs, attackerId, targetId, now = Date.now())163
const handle = ecs.handleOf(attackerId);163
const handle = ecs.handleOf(attackerId);164
if (handle == null)164
if (handle == null)165
{ return false; }165
{ return false; }166
// Spell auto-cast / pending cast is already the active combat form.167
if (isSpellCombatBusy(ecs, handle))168
{ return false; }166
let combat = ecs.get(handle, Component.combat);169
let combat = ecs.get(handle, Component.combat);167
if (!combat)170
if (!combat)168
{ combat = ecs.add(handle, Component.combat, { targetEntityId: null, nextSwingAt: 0 }); }171
{ combat = ecs.add(handle, Component.combat, { targetEntityId: null, nextSwingAt: 0 }); }@@ -179,6 +182,33 @@ export function engageTarget (ecs, attackerId, targetId, now = Date.now())179
return true;182
return true;180
}183
}181
184
185
/** Pending cast or entity-spell auto-repeat — physical combat must not run. */186
export function isSpellCombatBusy (ecs, handle)187
{188
const casting = ecs.get(handle, Component.spellcasting);189
if (!casting)190
{ return false; }191
if (casting.pendingCast)192
{ return true; }193
const spells = ecs.get(handle, Component.spells);194
if (!spells?.autoCast || !casting.repeatSpellId)195
{ return false; }196
const def = spellDefinition(casting.repeatSpellId);197
return def?.target === "entity";198
}199
200
function clearSpellCombatState (ecs, handle)201
{202
const casting = ecs.get(handle, Component.spellcasting);203
if (!casting)204
{ return; }205
casting.repeatSpellId = null;206
casting.repeatTargetEntityId = null;207
casting.repeatTileX = null;208
casting.repeatTileY = null;209
casting.pendingCast = null;210
}211
182
export function clearEngage (ecs, entityId)212
export function clearEngage (ecs, entityId)183
{213
{184
const handle = ecs.handleOf(entityId);214
const handle = ecs.handleOf(entityId);@@ -527,12 +557,14 @@ export function stepCombat (world, now = Date.now())527
continue;557
continue;528
}558
}529
559
560
// One combat form at a time: spell cast / auto-cast wins over melee & archery.561
if (isSpellCombatBusy(world.ecs, entity.handle))562
{ continue; }563
530
const profile = attackProfileOf(world.ecs, entity.handle);564
const profile = attackProfileOf(world.ecs, entity.handle);531
const targetPos = target.pos;565
const targetPos = target.pos;532
const isCreature = !!world.ecs.get(entity.handle, Component.creature)566
const isCreature = !!world.ecs.get(entity.handle, Component.creature)533
&& !world.ecs.get(entity.handle, Component.character);567
&& !world.ecs.get(entity.handle, Component.character);534
const weaponKind = weaponEquipmentKind(world.ecs, entity.handle);535
const isStaff = weaponKind === EquipmentKind.staff;536
568
537
if (profile.mode === "ranged")569
if (profile.mode === "ranged")538
{570
{@@ -568,21 +600,21 @@ export function stepCombat (world, now = Date.now())568
if (now < (combat.nextSwingAt || 0))600
if (now < (combat.nextSwingAt || 0))569
{ continue; }601
{ continue; }570
combat.nextSwingAt = now + (profile.cooldownMs | 0 || 1200);602
combat.nextSwingAt = now + (profile.cooldownMs | 0 || 1200);603
clearSpellCombatState(world.ecs, entity.handle);571
events.push(...resolveRangedShot(world, entity.entityId, targetId, profile, now, targetPos));604
events.push(...resolveRangedShot(world, entity.entityId, targetId, profile, now, targetPos));572
continue;605
continue;573
}606
}574
607
575
if (profile.mode !== "melee" && !isStaff)608
if (profile.mode !== "melee")576
{ continue; }609
{ continue; }577
610
578
const dist = chebyshev(entity.position.tileX, entity.position.tileY, targetPos.tileX, targetPos.tileY);611
const dist = chebyshev(entity.position.tileX, entity.position.tileY, targetPos.tileX, targetPos.tileY);579
const canSwing = !isStaff612
const canSwing = dist === 1580
&& dist === 1581
&& world.isClearStanding?.(entity.entityId, entity.position.tileX, entity.position.tileY);613
&& world.isClearStanding?.(entity.entityId, entity.position.tileX, entity.position.tileY);582
const attackSpells = isCreature ? eligibleMobAttackSpells(world.ecs, entity.handle) : [];614
const attackSpells = isCreature ? eligibleMobAttackSpells(world.ecs, entity.handle) : [];583
const preferSpell = isCreature615
const preferSpell = isCreature584
&& !!mobDefinition(world.ecs.get(entity.handle, Component.creature)?.mobTypeId)?.preferSpellAttack;616
&& !!mobDefinition(world.ecs.get(entity.handle, Component.creature)?.mobTypeId)?.preferSpellAttack;585
const shouldCast = isCreature && attackSpells.length > 0 && (isStaff || !canSwing || preferSpell);617
const shouldCast = isCreature && attackSpells.length > 0 && (!canSwing || preferSpell);586
618
587
if (shouldCast)619
if (shouldCast)588
{620
{@@ -624,21 +656,7 @@ export function stepCombat (world, now = Date.now())624
656
625
if (!canSwing)657
if (!canSwing)626
{658
{627
if (isStaff)659
queueMeleeApproach(world, entity.entityId, entity.handle, entity.position, targetPos);628
{629
queueStandInRangeApproach(630
world,631
entity.entityId,632
entity.handle,633
entity.position,634
targetPos.tileX,635
targetPos.tileY,636
COMBAT_RANGE_TILES,637
1638
);639
}640
else641
{ queueMeleeApproach(world, entity.entityId, entity.handle, entity.position, targetPos); }642
continue;660
continue;643
}661
}644
clearCombatApproachPath(world.ecs, entity.handle);662
clearCombatApproachPath(world.ecs, entity.handle);@@ -649,6 +667,7 @@ export function stepCombat (world, now = Date.now())649
{ continue; }667
{ continue; }650
668
651
combat.nextSwingAt = now + (profile.cooldownMs || 800);669
combat.nextSwingAt = now + (profile.cooldownMs || 800);670
clearSpellCombatState(world.ecs, entity.handle);652
events.push(...resolveMeleeSwing(world, entity.entityId, targetId, profile, now));671
events.push(...resolveMeleeSwing(world, entity.entityId, targetId, profile, now));653
}672
}654
return events;673
return events;@@ -4,6 +4,7 @@ import { mobComponents } from "../src/simulation/spawners.js";
4
4
import { MobId } from "../../shared/definitions/mobs.js";
5
5
import { ItemId, EQUIPMENT_SLOT } from "../../shared/definitions/items.js";
6
6
import { WoodItemId } from "../../shared/definitions/woodItems.js";
7
import { GemItemId } from "../../shared/definitions/gemItems.js";
7
8
import { tierStatBonus, WoodTier } from "../../shared/definitions/materialTiers.js";
8
9
import { BlockType, blockBlocksRangedAttacks } from "../../shared/definitions/tiles.js";
9
10
import { Component } from "../src/ecs/components.js";
@@ -1161,3 +1162,47 @@ test("humanoid attackers add equipment bonuses on top of spawner level", () =>
1161
1162
harness.close();
1162
1163
}
1163
1164
});
1165
1166
test("staff melees like a quarterstaff and awards clubs XP", () =>
1167
{
1168
const harness = makeSimulation({ workerCount: 1 });
1169
const random = Math.random;
1170
Math.random = () => 0.999999;
1171
try
1172
{
1173
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
1174
const playerId = harness.simulation.spawnCharacter(1, character);
1175
const deerId = harness.simulation.spawnEntity(mobComponents({
1176
tileX: 9,
1177
tileY: 8,
1178
mobTypeId: MobId.deer
1179
}));
1180
const worker = workerOf(harness.simulation, playerId);
1181
const handle = worker.world.ecs.handleOf(playerId);
1182
const equipment = worker.world.ecs.get(handle, Component.equipment);
1183
equipment.slots.weapon = { itemId: GemItemId.pineStaff, quantity: 1 };
1184
const skills = worker.world.ecs.get(handle, Component.skills);
1185
skills.xp[SkillId.clubfighting] = totalXpForLevel(10);
1186
skills.xp[SkillId.magic] = totalXpForLevel(10);
1187
const clubsBefore = skills.xp[SkillId.clubfighting];
1188
const magicBefore = skills.xp[SkillId.magic];
1189
1190
const profile = attackProfileOf(worker.world.ecs, handle);
1191
assert.equal(profile.mode, "melee");
1192
assert.equal(profile.hitSkillId, SkillId.clubfighting);
1193
assert.equal(profile.damageSkillId, SkillId.strength);
1194
1195
const events = resolveMeleeSwing(worker.world, playerId, deerId, profile, Date.now());
1196
const hit = events.find((event) => event.type === "combat-hit");
1197
assert.ok(hit, "staff should land a melee hit");
1198
const updated = events.find((event) => event.type === "skills-updated" && event.entityId === playerId);
1199
assert.ok(updated);
1200
assert.ok(updated.stored[SkillId.clubfighting] > clubsBefore, "melee staff awards clubs XP");
1201
assert.equal(updated.stored[SkillId.magic], magicBefore, "melee staff does not award magic XP");
1202
}
1203
finally
1204
{
1205
Math.random = random;
1206
harness.close();
1207
}
1208
});
@@ -4,6 +4,7 @@ import { mobComponents } from "../src/simulation/spawners.js";4
import { MobId } from "../../shared/definitions/mobs.js";4
import { MobId } from "../../shared/definitions/mobs.js";5
import { ItemId, EQUIPMENT_SLOT } from "../../shared/definitions/items.js";5
import { ItemId, EQUIPMENT_SLOT } from "../../shared/definitions/items.js";6
import { WoodItemId } from "../../shared/definitions/woodItems.js";6
import { WoodItemId } from "../../shared/definitions/woodItems.js";7
import { GemItemId } from "../../shared/definitions/gemItems.js";7
import { tierStatBonus, WoodTier } from "../../shared/definitions/materialTiers.js";8
import { tierStatBonus, WoodTier } from "../../shared/definitions/materialTiers.js";8
import { BlockType, blockBlocksRangedAttacks } from "../../shared/definitions/tiles.js";9
import { BlockType, blockBlocksRangedAttacks } from "../../shared/definitions/tiles.js";9
import { Component } from "../src/ecs/components.js";10
import { Component } from "../src/ecs/components.js";@@ -1161,3 +1162,47 @@ test("humanoid attackers add equipment bonuses on top of spawner level", () =>1161
harness.close();1162
harness.close();1162
}1163
}1163
});1164
});1165
1166
test("staff melees like a quarterstaff and awards clubs XP", () =>1167
{1168
const harness = makeSimulation({ workerCount: 1 });1169
const random = Math.random;1170
Math.random = () => 0.999999;1171
try1172
{1173
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });1174
const playerId = harness.simulation.spawnCharacter(1, character);1175
const deerId = harness.simulation.spawnEntity(mobComponents({1176
tileX: 9,1177
tileY: 8,1178
mobTypeId: MobId.deer1179
}));1180
const worker = workerOf(harness.simulation, playerId);1181
const handle = worker.world.ecs.handleOf(playerId);1182
const equipment = worker.world.ecs.get(handle, Component.equipment);1183
equipment.slots.weapon = { itemId: GemItemId.pineStaff, quantity: 1 };1184
const skills = worker.world.ecs.get(handle, Component.skills);1185
skills.xp[SkillId.clubfighting] = totalXpForLevel(10);1186
skills.xp[SkillId.magic] = totalXpForLevel(10);1187
const clubsBefore = skills.xp[SkillId.clubfighting];1188
const magicBefore = skills.xp[SkillId.magic];1189
1190
const profile = attackProfileOf(worker.world.ecs, handle);1191
assert.equal(profile.mode, "melee");1192
assert.equal(profile.hitSkillId, SkillId.clubfighting);1193
assert.equal(profile.damageSkillId, SkillId.strength);1194
1195
const events = resolveMeleeSwing(worker.world, playerId, deerId, profile, Date.now());1196
const hit = events.find((event) => event.type === "combat-hit");1197
assert.ok(hit, "staff should land a melee hit");1198
const updated = events.find((event) => event.type === "skills-updated" && event.entityId === playerId);1199
assert.ok(updated);1200
assert.ok(updated.stored[SkillId.clubfighting] > clubsBefore, "melee staff awards clubs XP");1201
assert.equal(updated.stored[SkillId.magic], magicBefore, "melee staff does not award magic XP");1202
}1203
finally1204
{1205
Math.random = random;1206
harness.close();1207
}1208
});@@ -390,3 +390,75 @@ test("physical attack cancels spell auto-cast", () =>
390
390
harness.close();
391
391
}
392
392
});
393
394
test("auto-retaliate does not engage while auto-casting an attack spell", () =>
395
{
396
const harness = makeSimulation({ workerCount: 1 });
397
const random = Math.random;
398
Math.random = () => 0.999;
399
try
400
{
401
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
402
const playerId = harness.simulation.spawnCharacter(1, character);
403
const deerId = harness.simulation.spawnEntity(mobComponents({
404
tileX: 10,
405
tileY: 8,
406
mobTypeId: MobId.deer,
407
level: 40
408
}));
409
const ratId = harness.simulation.spawnEntity(mobComponents({
410
tileX: 9,
411
tileY: 8,
412
mobTypeId: MobId.rat,
413
spawnerId: null
414
}));
415
const worker = workerOf(harness.simulation, playerId);
416
const handle = worker.world.ecs.handleOf(playerId);
417
const deerHandle = worker.world.ecs.handleOf(deerId);
418
const deerHealth = worker.world.ecs.get(deerHandle, Component.health);
419
deerHealth.max = 500;
420
deerHealth.current = 500;
421
const spells = worker.world.ecs.get(handle, Component.spells);
422
spells.known.push(SpellId.manaBullet);
423
spells.autoCast = true;
424
const skills = worker.world.ecs.get(handle, Component.skills);
425
skills.xp[SkillId.magic] = totalXpForLevel(20);
426
skills.xp[SkillId.mind] = totalXpForLevel(20);
427
428
harness.simulation.enqueue(1, {
429
type: "cast-spell-entity",
430
characterId: character.character_id,
431
spellId: SpellId.manaBullet,
432
targetEntityId: deerId
433
});
434
harness.simulation.step();
435
assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet);
436
assert.equal(worker.world.ecs.get(handle, Component.combat)?.targetEntityId ?? null, null);
437
438
let ratSwung = false;
439
for (let i = 0; i < 80; i++)
440
{
441
harness.advance(50);
442
const events = harness.simulation.step().events;
443
if (events.some((event) =>
444
event.type === "combat-swing" && event.attackerId === ratId && event.targetId === playerId
445
))
446
{
447
ratSwung = true;
448
break;
449
}
450
}
451
assert.ok(ratSwung, "expected the rat to swing at the player");
452
assert.equal(
453
worker.world.ecs.get(handle, Component.combat)?.targetEntityId ?? null,
454
null,
455
"auto-cast is the attack — no melee retaliate engage"
456
);
457
assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet);
458
}
459
finally
460
{
461
Math.random = random;
462
harness.close();
463
}
464
});
@@ -390,3 +390,75 @@ test("physical attack cancels spell auto-cast", () =>390
harness.close();390
harness.close();391
}391
}392
});392
});393
394
test("auto-retaliate does not engage while auto-casting an attack spell", () =>395
{396
const harness = makeSimulation({ workerCount: 1 });397
const random = Math.random;398
Math.random = () => 0.999;399
try400
{401
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });402
const playerId = harness.simulation.spawnCharacter(1, character);403
const deerId = harness.simulation.spawnEntity(mobComponents({404
tileX: 10,405
tileY: 8,406
mobTypeId: MobId.deer,407
level: 40408
}));409
const ratId = harness.simulation.spawnEntity(mobComponents({410
tileX: 9,411
tileY: 8,412
mobTypeId: MobId.rat,413
spawnerId: null414
}));415
const worker = workerOf(harness.simulation, playerId);416
const handle = worker.world.ecs.handleOf(playerId);417
const deerHandle = worker.world.ecs.handleOf(deerId);418
const deerHealth = worker.world.ecs.get(deerHandle, Component.health);419
deerHealth.max = 500;420
deerHealth.current = 500;421
const spells = worker.world.ecs.get(handle, Component.spells);422
spells.known.push(SpellId.manaBullet);423
spells.autoCast = true;424
const skills = worker.world.ecs.get(handle, Component.skills);425
skills.xp[SkillId.magic] = totalXpForLevel(20);426
skills.xp[SkillId.mind] = totalXpForLevel(20);427
428
harness.simulation.enqueue(1, {429
type: "cast-spell-entity",430
characterId: character.character_id,431
spellId: SpellId.manaBullet,432
targetEntityId: deerId433
});434
harness.simulation.step();435
assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet);436
assert.equal(worker.world.ecs.get(handle, Component.combat)?.targetEntityId ?? null, null);437
438
let ratSwung = false;439
for (let i = 0; i < 80; i++)440
{441
harness.advance(50);442
const events = harness.simulation.step().events;443
if (events.some((event) =>444
event.type === "combat-swing" && event.attackerId === ratId && event.targetId === playerId445
))446
{447
ratSwung = true;448
break;449
}450
}451
assert.ok(ratSwung, "expected the rat to swing at the player");452
assert.equal(453
worker.world.ecs.get(handle, Component.combat)?.targetEntityId ?? null,454
null,455
"auto-cast is the attack — no melee retaliate engage"456
);457
assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet);458
}459
finally460
{461
Math.random = random;462
harness.close();463
}464
});