Apply equipment bonuses when humanoids resolve melee attacks.
Spawner level alone undercounted armed NPCs; hit/damage/agility/defence now add slot bonuses like players.
@@ -11,7 +11,7 @@ import
11
11
mobIsAggressive,
12
12
mobAggroRangeTiles
13
13
} from "../../../shared/definitions/mobCombat.js";
14
import { spellDefinition } from "../../../shared/definitions/spells.js";
14
import { spellDefinition, spellMinMagicLevel } from "../../../shared/definitions/spells.js";
15
15
import { resolvePhysicalAttack } from "../../../shared/combat/rolls.js";
16
16
import { mobDamageAffinities } from "../../../shared/combat/magic.js";
17
17
import { AttackType } from "../../../shared/definitions/attackTypes.js";
@@ -490,11 +490,17 @@ function attackerCombatLevels (ecs, attackerHandle, profile, settings = null)
490
490
if (attackerCharacter)
491
491
{ return playerCombatLevels(ecs, attackerHandle, profile, settings); }
492
492
const skills = ecs.get(attackerHandle, Component.skills);
493
const equipment = ecs.get(attackerHandle, Component.equipment);
494
const bonuses = equipment ? equipmentBonusesFromSlots(equipment.slots, settings) : {};
495
const hitSkillId = profile.hitSkillId ?? SkillId.unarmed;
496
const damageSkillId = profile.damageSkillId ?? SkillId.strength;
497
const baseHit = attackerSkillLevel(skills, profile, hitSkillId);
498
const baseDamage = attackerSkillLevel(skills, profile, damageSkillId);
493
499
return {
494
hitLevel: attackerSkillLevel(skills, profile, profile.hitSkillId),
495
damageLevel: attackerSkillLevel(skills, profile, profile.damageSkillId),
496
agilityLevel: skillLevel(skills, SkillId.agility),
497
defenceLevel: skillLevel(skills, SkillId.defence)
500
hitLevel: Math.max(1, baseHit + (bonuses[hitSkillId] ?? 0)),
501
damageLevel: Math.max(1, baseDamage + (bonuses[damageSkillId] ?? 0)),
502
agilityLevel: effectiveSkillLevel(skills, SkillId.agility, bonuses),
503
defenceLevel: effectiveSkillLevel(skills, SkillId.defence, bonuses)
498
504
};
499
505
}
500
506
@@ -661,7 +667,7 @@ function eligibleMobAttackSpells (ecs, handle)
661
667
const def = spellDefinition(spellId);
662
668
if (!def || def.target !== "entity")
663
669
{ continue; }
664
const minLevel = Math.max(1, def.minMagicLevel | 0 || 1);
670
const minLevel = spellMinMagicLevel(def);
665
671
if (level < minLevel)
666
672
{ continue; }
667
673
const chance = Math.max(0, Math.min(100, Number(chances[spellId]) || 0)) | 0;
@@ -11,7 +11,7 @@ import11
mobIsAggressive,11
mobIsAggressive,12
mobAggroRangeTiles12
mobAggroRangeTiles13
} from "../../../shared/definitions/mobCombat.js";13
} from "../../../shared/definitions/mobCombat.js";14
import { spellDefinition } from "../../../shared/definitions/spells.js";14
import { spellDefinition, spellMinMagicLevel } from "../../../shared/definitions/spells.js";15
import { resolvePhysicalAttack } from "../../../shared/combat/rolls.js";15
import { resolvePhysicalAttack } from "../../../shared/combat/rolls.js";16
import { mobDamageAffinities } from "../../../shared/combat/magic.js";16
import { mobDamageAffinities } from "../../../shared/combat/magic.js";17
import { AttackType } from "../../../shared/definitions/attackTypes.js";17
import { AttackType } from "../../../shared/definitions/attackTypes.js";@@ -490,11 +490,17 @@ function attackerCombatLevels (ecs, attackerHandle, profile, settings = null)490
if (attackerCharacter)490
if (attackerCharacter)491
{ return playerCombatLevels(ecs, attackerHandle, profile, settings); }491
{ return playerCombatLevels(ecs, attackerHandle, profile, settings); }492
const skills = ecs.get(attackerHandle, Component.skills);492
const skills = ecs.get(attackerHandle, Component.skills);493
const equipment = ecs.get(attackerHandle, Component.equipment);494
const bonuses = equipment ? equipmentBonusesFromSlots(equipment.slots, settings) : {};495
const hitSkillId = profile.hitSkillId ?? SkillId.unarmed;496
const damageSkillId = profile.damageSkillId ?? SkillId.strength;497
const baseHit = attackerSkillLevel(skills, profile, hitSkillId);498
const baseDamage = attackerSkillLevel(skills, profile, damageSkillId);493
return {499
return {494
hitLevel: attackerSkillLevel(skills, profile, profile.hitSkillId),500
hitLevel: Math.max(1, baseHit + (bonuses[hitSkillId] ?? 0)),495
damageLevel: attackerSkillLevel(skills, profile, profile.damageSkillId),501
damageLevel: Math.max(1, baseDamage + (bonuses[damageSkillId] ?? 0)),496
agilityLevel: skillLevel(skills, SkillId.agility),502
agilityLevel: effectiveSkillLevel(skills, SkillId.agility, bonuses),497
defenceLevel: skillLevel(skills, SkillId.defence)503
defenceLevel: effectiveSkillLevel(skills, SkillId.defence, bonuses)498
};504
};499
}505
}500
506
@@ -661,7 +667,7 @@ function eligibleMobAttackSpells (ecs, handle)661
const def = spellDefinition(spellId);667
const def = spellDefinition(spellId);662
if (!def || def.target !== "entity")668
if (!def || def.target !== "entity")663
{ continue; }669
{ continue; }664
const minLevel = Math.max(1, def.minMagicLevel | 0 || 1);670
const minLevel = spellMinMagicLevel(def);665
if (level < minLevel)671
if (level < minLevel)666
{ continue; }672
{ continue; }667
const chance = Math.max(0, Math.min(100, Number(chances[spellId]) || 0)) | 0;673
const chance = Math.max(0, Math.min(100, Number(chances[spellId]) || 0)) | 0;@@ -3,6 +3,8 @@ import assert from "node:assert/strict";
3
3
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
import { WoodItemId } from "../../shared/definitions/woodItems.js";
7
import { tierStatBonus, WoodTier } from "../../shared/definitions/materialTiers.js";
6
8
import { BlockType, blockBlocksRangedAttacks } from "../../shared/definitions/tiles.js";
7
9
import { Component } from "../src/ecs/components.js";
8
10
import { tileKey } from "../src/world/coordinates.js";
@@ -1116,3 +1118,46 @@ test("approaching combatants pick different adjacent goals", () =>
1116
1118
finally
1117
1119
{ harness.close(); }
1118
1120
});
1121
1122
test("humanoid attackers add equipment bonuses on top of spawner level", () =>
1123
{
1124
const harness = makeSimulation({ workerCount: 1 });
1125
const random = Math.random;
1126
/** Max every skill roll so damage equals effective damage skill. */
1127
Math.random = () => 0.999999;
1128
try
1129
{
1130
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
1131
const playerId = harness.simulation.spawnCharacter(1, character);
1132
const player = componentsOf(harness.simulation, playerId);
1133
player.skills.xp[SkillId.agility] = 0;
1134
player.skills.xp[SkillId.defence] = 0;
1135
1136
const level = 5;
1137
const npcId = harness.simulation.spawnEntity(mobComponents({
1138
tileX: 9,
1139
tileY: 8,
1140
mobTypeId: MobId.humanoid,
1141
level,
1142
displayName: "Guard",
1143
sex: "male",
1144
equipment: {
1145
weapon: WoodItemId.oakQuarterstaff
1146
}
1147
}));
1148
const worker = workerOf(harness.simulation, npcId);
1149
const profile = attackProfileOf(worker.world.ecs, worker.world.ecs.handleOf(npcId));
1150
assert.equal(profile.mobAttackSkill, level);
1151
assert.equal(profile.damageSkillId, SkillId.strength);
1152
1153
const events = resolveMeleeSwing(worker.world, npcId, playerId, profile, Date.now());
1154
const hit = events.find((event) => event.type === "combat-hit");
1155
assert.ok(hit, "expected a hit");
1156
assert.equal(hit.damage, level + tierStatBonus(WoodTier.oak));
1157
}
1158
finally
1159
{
1160
Math.random = random;
1161
harness.close();
1162
}
1163
});
@@ -3,6 +3,8 @@ import assert from "node:assert/strict";3
import { mobComponents } from "../src/simulation/spawners.js";3
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";7
import { tierStatBonus, WoodTier } from "../../shared/definitions/materialTiers.js";6
import { BlockType, blockBlocksRangedAttacks } from "../../shared/definitions/tiles.js";8
import { BlockType, blockBlocksRangedAttacks } from "../../shared/definitions/tiles.js";7
import { Component } from "../src/ecs/components.js";9
import { Component } from "../src/ecs/components.js";8
import { tileKey } from "../src/world/coordinates.js";10
import { tileKey } from "../src/world/coordinates.js";@@ -1116,3 +1118,46 @@ test("approaching combatants pick different adjacent goals", () =>1116
finally1118
finally1117
{ harness.close(); }1119
{ harness.close(); }1118
});1120
});1121
1122
test("humanoid attackers add equipment bonuses on top of spawner level", () =>1123
{1124
const harness = makeSimulation({ workerCount: 1 });1125
const random = Math.random;1126
/** Max every skill roll so damage equals effective damage skill. */1127
Math.random = () => 0.999999;1128
try1129
{1130
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });1131
const playerId = harness.simulation.spawnCharacter(1, character);1132
const player = componentsOf(harness.simulation, playerId);1133
player.skills.xp[SkillId.agility] = 0;1134
player.skills.xp[SkillId.defence] = 0;1135
1136
const level = 5;1137
const npcId = harness.simulation.spawnEntity(mobComponents({1138
tileX: 9,1139
tileY: 8,1140
mobTypeId: MobId.humanoid,1141
level,1142
displayName: "Guard",1143
sex: "male",1144
equipment: {1145
weapon: WoodItemId.oakQuarterstaff1146
}1147
}));1148
const worker = workerOf(harness.simulation, npcId);1149
const profile = attackProfileOf(worker.world.ecs, worker.world.ecs.handleOf(npcId));1150
assert.equal(profile.mobAttackSkill, level);1151
assert.equal(profile.damageSkillId, SkillId.strength);1152
1153
const events = resolveMeleeSwing(worker.world, npcId, playerId, profile, Date.now());1154
const hit = events.find((event) => event.type === "combat-hit");1155
assert.ok(hit, "expected a hit");1156
assert.equal(hit.damage, level + tierStatBonus(WoodTier.oak));1157
}1158
finally1159
{1160
Math.random = random;1161
harness.close();1162
}1163
});