BlueGit

timfa questward

Goblin slashing melee without equipment; fix deterministic combat tests.

Goblins use a baked-in sword attack profile. Combat integration tests now use roll sequences that deal damage under current rules. Push requires a green npm test run.

timfa Tim Falken · pushed by timfa

Authored 2026-09-01 · 2 months ago

Hash
bc9e7272ba731d484b5fb5649a89191d1f59114b
Parent
f50d79ab
8 files changed +128 −42
.cursor/rules/git-feature-branch-workflow.mdc +1 −0
@@ -15,6 +15,7 @@ alwaysApply: true
15 15
16 16 “Push” / “naar origin” means this sequence:
17 17
18 0. Run the **full** test suite: `npm test`. **Every test must pass.** Do not merge or push with failing tests — fix failures first (update outdated tests or remove obsolete one-offs; keep generic system coverage).
18 19 1. Merge the feature branch into `master` (fast-forward when possible).
19 20 2. Push `master` to `origin`.
20 21 3. Delete the local (and remote, if it existed) feature branch — it is obsolete after merge.
@@ -15,6 +15,7 @@ alwaysApply: true
15
15
16“Push” / “naar origin” means this sequence:
16“Push” / “naar origin” means this sequence:
17
17
180. Run the **full** test suite: `npm test`. **Every test must pass.** Do not merge or push with failing tests — fix failures first (update outdated tests or remove obsolete one-offs; keep generic system coverage).
181. Merge the feature branch into `master` (fast-forward when possible).
191. Merge the feature branch into `master` (fast-forward when possible).
192. Push `master` to `origin`.
202. Push `master` to `origin`.
203. Delete the local (and remote, if it existed) feature branch — it is obsolete after merge.
213. Delete the local (and remote, if it existed) feature branch — it is obsolete after merge.
server/tests/combat.test.js +10 −22
@@ -15,6 +15,7 @@ import { SkillId } from "../../shared/definitions/skills.js";
15 15 import { totalXpForLevel } from "../../shared/skills/progression.js";
16 16 import { resolveMeleeSwing, attackProfileOf, hasCombatLineOfSight, queueMeleeApproach, markSeekCover, engageTarget, applyDamageAndKill, markFleeOnHit, stepCombat, chebyshev } from "../src/combat/service.js";
17 17 import { makeCharacter, makeSimulation, pinEastSplit, stepTimes } from "./helpers/simulationHarness.js";
18 import { withDamagingCombatRng, installDamagingCombatRng, installMissDodgeRng, installBlockRng } from "./helpers/combatRng.js";
18 19
19 20 function workerOf (simulation, entityId)
20 21 {
@@ -102,12 +103,7 @@ function runCombatUntilOutcome (harness, playerId, deerId, maxSteps = 600)
102 103
103 104 function withDeterministicCombat (fn)
104 105 {
105 const random = Math.random;
106 Math.random = () => 0;
107 try
108 { fn(); }
109 finally
110 { Math.random = random; }
106 withDamagingCombatRng(fn);
111 107 }
112 108
113 109 test("immortal deer auto-attacks until the player dies", () =>
@@ -997,8 +993,7 @@ test("mobs heal to full when their region unloads", () =>
997 993 test("taking damage awards Health XP equal to damage dealt", () =>
998 994 {
999 995 const harness = makeSimulation({ workerCount: 1 });
1000 const random = Math.random;
1001 Math.random = () => 0;
996 const restore = installDamagingCombatRng();
1002 997 try
1003 998 {
1004 999 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
@@ -1021,7 +1016,7 @@ test("taking damage awards Health XP equal to damage dealt", () =>
1021 1016 }
1022 1017 finally
1023 1018 {
1024 Math.random = random;
1019 restore();
1025 1020 harness.close();
1026 1021 }
1027 1022 });
@@ -1029,8 +1024,7 @@ test("taking damage awards Health XP equal to damage dealt", () =>
1029 1024 test("lethal damage awards no Health XP", () =>
1030 1025 {
1031 1026 const harness = makeSimulation({ workerCount: 1 });
1032 const random = Math.random;
1033 Math.random = () => 0;
1027 const restore = installDamagingCombatRng();
1034 1028 try
1035 1029 {
1036 1030 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
@@ -1057,7 +1051,7 @@ test("lethal damage awards no Health XP", () =>
1057 1051 }
1058 1052 finally
1059 1053 {
1060 Math.random = random;
1054 restore();
1061 1055 harness.close();
1062 1056 }
1063 1057 });
@@ -1065,10 +1059,7 @@ test("lethal damage awards no Health XP", () =>
1065 1059 test("dodging a hit awards Agility XP equal to avoided damage", () =>
1066 1060 {
1067 1061 const harness = makeSimulation({ workerCount: 1 });
1068 let roll = 0;
1069 const seq = [0, 0.5, 0];
1070 const random = Math.random;
1071 Math.random = () => seq[roll++ % seq.length];
1062 const restore = installMissDodgeRng(1);
1072 1063 try
1073 1064 {
1074 1065 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
@@ -1092,7 +1083,7 @@ test("dodging a hit awards Agility XP equal to avoided damage", () =>
1092 1083 }
1093 1084 finally
1094 1085 {
1095 Math.random = random;
1086 restore();
1096 1087 harness.close();
1097 1088 }
1098 1089 });
@@ -1100,10 +1091,7 @@ test("dodging a hit awards Agility XP equal to avoided damage", () =>
1100 1091 test("blocking a hit awards Defence XP equal to avoided damage", () =>
1101 1092 {
1102 1093 const harness = makeSimulation({ workerCount: 1 });
1103 let roll = 0;
1104 const seq = [0.9, 0, 0, 0.9];
1105 const random = Math.random;
1106 Math.random = () => seq[roll++ % seq.length];
1094 const restore = installBlockRng(1);
1107 1095 try
1108 1096 {
1109 1097 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
@@ -1127,7 +1115,7 @@ test("blocking a hit awards Defence XP equal to avoided damage", () =>
1127 1115 }
1128 1116 finally
1129 1117 {
1130 Math.random = random;
1118 restore();
1131 1119 harness.close();
1132 1120 }
1133 1121 });
@@ -15,6 +15,7 @@ import { SkillId } from "../../shared/definitions/skills.js";
15import { totalXpForLevel } from "../../shared/skills/progression.js";
15import { totalXpForLevel } from "../../shared/skills/progression.js";
16import { resolveMeleeSwing, attackProfileOf, hasCombatLineOfSight, queueMeleeApproach, markSeekCover, engageTarget, applyDamageAndKill, markFleeOnHit, stepCombat, chebyshev } from "../src/combat/service.js";
16import { resolveMeleeSwing, attackProfileOf, hasCombatLineOfSight, queueMeleeApproach, markSeekCover, engageTarget, applyDamageAndKill, markFleeOnHit, stepCombat, chebyshev } from "../src/combat/service.js";
17import { makeCharacter, makeSimulation, pinEastSplit, stepTimes } from "./helpers/simulationHarness.js";
17import { makeCharacter, makeSimulation, pinEastSplit, stepTimes } from "./helpers/simulationHarness.js";
18import { withDamagingCombatRng, installDamagingCombatRng, installMissDodgeRng, installBlockRng } from "./helpers/combatRng.js";
18
19
19function workerOf (simulation, entityId)
20function workerOf (simulation, entityId)
20{
21{
@@ -102,12 +103,7 @@ function runCombatUntilOutcome (harness, playerId, deerId, maxSteps = 600)
102
103
103function withDeterministicCombat (fn)
104function withDeterministicCombat (fn)
104{
105{
105 const random = Math.random;
106 withDamagingCombatRng(fn);
106 Math.random = () => 0;
107 try
108 { fn(); }
109 finally
110 { Math.random = random; }
111}
107}
112
108
113test("immortal deer auto-attacks until the player dies", () =>
109test("immortal deer auto-attacks until the player dies", () =>
@@ -997,8 +993,7 @@ test("mobs heal to full when their region unloads", () =>
997test("taking damage awards Health XP equal to damage dealt", () =>
993test("taking damage awards Health XP equal to damage dealt", () =>
998{
994{
999 const harness = makeSimulation({ workerCount: 1 });
995 const harness = makeSimulation({ workerCount: 1 });
1000 const random = Math.random;
996 const restore = installDamagingCombatRng();
1001 Math.random = () => 0;
1002 try
997 try
1003 {
998 {
1004 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
999 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
@@ -1021,7 +1016,7 @@ test("taking damage awards Health XP equal to damage dealt", () =>
1021 }
1016 }
1022 finally
1017 finally
1023 {
1018 {
1024 Math.random = random;
1019 restore();
1025 harness.close();
1020 harness.close();
1026 }
1021 }
1027});
1022});
@@ -1029,8 +1024,7 @@ test("taking damage awards Health XP equal to damage dealt", () =>
1029test("lethal damage awards no Health XP", () =>
1024test("lethal damage awards no Health XP", () =>
1030{
1025{
1031 const harness = makeSimulation({ workerCount: 1 });
1026 const harness = makeSimulation({ workerCount: 1 });
1032 const random = Math.random;
1027 const restore = installDamagingCombatRng();
1033 Math.random = () => 0;
1034 try
1028 try
1035 {
1029 {
1036 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
1030 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
@@ -1057,7 +1051,7 @@ test("lethal damage awards no Health XP", () =>
1057 }
1051 }
1058 finally
1052 finally
1059 {
1053 {
1060 Math.random = random;
1054 restore();
1061 harness.close();
1055 harness.close();
1062 }
1056 }
1063});
1057});
@@ -1065,10 +1059,7 @@ test("lethal damage awards no Health XP", () =>
1065test("dodging a hit awards Agility XP equal to avoided damage", () =>
1059test("dodging a hit awards Agility XP equal to avoided damage", () =>
1066{
1060{
1067 const harness = makeSimulation({ workerCount: 1 });
1061 const harness = makeSimulation({ workerCount: 1 });
1068 let roll = 0;
1062 const restore = installMissDodgeRng(1);
1069 const seq = [0, 0.5, 0];
1070 const random = Math.random;
1071 Math.random = () => seq[roll++ % seq.length];
1072 try
1063 try
1073 {
1064 {
1074 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
1065 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
@@ -1092,7 +1083,7 @@ test("dodging a hit awards Agility XP equal to avoided damage", () =>
1092 }
1083 }
1093 finally
1084 finally
1094 {
1085 {
1095 Math.random = random;
1086 restore();
1096 harness.close();
1087 harness.close();
1097 }
1088 }
1098});
1089});
@@ -1100,10 +1091,7 @@ test("dodging a hit awards Agility XP equal to avoided damage", () =>
1100test("blocking a hit awards Defence XP equal to avoided damage", () =>
1091test("blocking a hit awards Defence XP equal to avoided damage", () =>
1101{
1092{
1102 const harness = makeSimulation({ workerCount: 1 });
1093 const harness = makeSimulation({ workerCount: 1 });
1103 let roll = 0;
1094 const restore = installBlockRng(1);
1104 const seq = [0.9, 0, 0, 0.9];
1105 const random = Math.random;
1106 Math.random = () => seq[roll++ % seq.length];
1107 try
1095 try
1108 {
1096 {
1109 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
1097 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
@@ -1127,7 +1115,7 @@ test("blocking a hit awards Defence XP equal to avoided damage", () =>
1127 }
1115 }
1128 finally
1116 finally
1129 {
1117 {
1130 Math.random = random;
1118 restore();
1131 harness.close();
1119 harness.close();
1132 }
1120 }
1133});
1121});
server/tests/helpers/combatRng.js added +45 −0
@@ -0,0 +1,45 @@
1 /** Four-roll hit pattern: high hit, low agility, high damage, low defence. */
2 const DAMAGING_PATTERN = Object.freeze([0.99, 0, 0.99, 0]);
3
4 /**
5 * Deterministic Math.random for integration tests: melee hits land with damage.
6 * Matches {@link shared/combat/rolls.js} — damage equals the damage skill roll only.
7 */
8 export function installDamagingCombatRng ()
9 {
10 const random = Math.random;
11 let i = 0;
12 Math.random = () => DAMAGING_PATTERN[i++ % DAMAGING_PATTERN.length];
13 return () => { Math.random = random; };
14 }
15
16 export function withDamagingCombatRng (fn)
17 {
18 const restore = installDamagingCombatRng();
19 try
20 { fn(); }
21 finally
22 { restore(); }
23 }
24
25 /** Miss (agility wins) but attacker rolled non-zero damage for dodge XP. */
26 export function installMissDodgeRng (attackerDamageSkill = 1)
27 {
28 const damageSample = attackerDamageSkill <= 1 ? 0.51 : 0.99;
29 const pattern = Object.freeze([0, 0.99, damageSample]);
30 let i = 0;
31 const random = Math.random;
32 Math.random = () => pattern[i++ % pattern.length];
33 return () => { Math.random = random; };
34 }
35
36 /** Block after a hit contest; damage roll is non-zero for Defence XP. */
37 export function installBlockRng (attackerDamageSkill = 1)
38 {
39 const damageSample = attackerDamageSkill <= 1 ? 0.51 : 0.99;
40 const pattern = Object.freeze([0.99, 0, damageSample, 0.99]);
41 let i = 0;
42 const random = Math.random;
43 Math.random = () => pattern[i++ % pattern.length];
44 return () => { Math.random = random; };
45 }
@@ -0,0 +1,45 @@
1/** Four-roll hit pattern: high hit, low agility, high damage, low defence. */
2const DAMAGING_PATTERN = Object.freeze([0.99, 0, 0.99, 0]);
3
4/**
5 * Deterministic Math.random for integration tests: melee hits land with damage.
6 * Matches {@link shared/combat/rolls.js} — damage equals the damage skill roll only.
7 */
8export function installDamagingCombatRng ()
9{
10 const random = Math.random;
11 let i = 0;
12 Math.random = () => DAMAGING_PATTERN[i++ % DAMAGING_PATTERN.length];
13 return () => { Math.random = random; };
14}
15
16export function withDamagingCombatRng (fn)
17{
18 const restore = installDamagingCombatRng();
19 try
20 { fn(); }
21 finally
22 { restore(); }
23}
24
25/** Miss (agility wins) but attacker rolled non-zero damage for dodge XP. */
26export function installMissDodgeRng (attackerDamageSkill = 1)
27{
28 const damageSample = attackerDamageSkill <= 1 ? 0.51 : 0.99;
29 const pattern = Object.freeze([0, 0.99, damageSample]);
30 let i = 0;
31 const random = Math.random;
32 Math.random = () => pattern[i++ % pattern.length];
33 return () => { Math.random = random; };
34}
35
36/** Block after a hit contest; damage roll is non-zero for Defence XP. */
37export function installBlockRng (attackerDamageSkill = 1)
38{
39 const damageSample = attackerDamageSkill <= 1 ? 0.51 : 0.99;
40 const pattern = Object.freeze([0.99, 0, damageSample, 0.99]);
41 let i = 0;
42 const random = Math.random;
43 Math.random = () => pattern[i++ % pattern.length];
44 return () => { Math.random = random; };
45}
server/tests/mobCombat.test.js +12 −1
@@ -2,6 +2,7 @@ import test from "node:test";
2 2 import assert from "node:assert/strict";
3 3 import { mobCanFight, mobMeleeAttackProfile } from "../../shared/definitions/mobCombat.js";
4 4 import { MobId, mobDefinition } from "../../shared/definitions/mobs.js";
5 import { SkillId } from "../../shared/definitions/skills.js";
5 6
6 7 test("living targetable mobs can fight except pigeons", () =>
7 8 {
@@ -13,6 +14,16 @@ test("living targetable mobs can fight except pigeons", () =>
13 14 test("mob attack profile uses one attackSkill for hit and damage rolls", () =>
14 15 {
15 16 const profile = mobMeleeAttackProfile(mobDefinition(MobId.rat));
16 assert.equal(profile.mobAttackSkill, 3);
17 assert.equal(profile.mobAttackSkill, 1);
17 18 assert.equal(profile.baseDamage, undefined);
18 19 });
20
21 test("goblins can fight and default to level 5 with slashing melee", () =>
22 {
23 assert.equal(mobCanFight(mobDefinition(MobId.goblin)), true);
24 const profile = mobMeleeAttackProfile(mobDefinition(MobId.goblin), 5);
25 assert.equal(profile.mobAttackSkill, 5);
26 assert.equal(profile.mode, "melee");
27 assert.equal(profile.attackType, "slashing");
28 assert.equal(profile.hitSkillId, SkillId.swordfighting);
29 });
@@ -2,6 +2,7 @@ import test from "node:test";
2import assert from "node:assert/strict";
2import assert from "node:assert/strict";
3import { mobCanFight, mobMeleeAttackProfile } from "../../shared/definitions/mobCombat.js";
3import { mobCanFight, mobMeleeAttackProfile } from "../../shared/definitions/mobCombat.js";
4import { MobId, mobDefinition } from "../../shared/definitions/mobs.js";
4import { MobId, mobDefinition } from "../../shared/definitions/mobs.js";
5import { SkillId } from "../../shared/definitions/skills.js";
5
6
6test("living targetable mobs can fight except pigeons", () =>
7test("living targetable mobs can fight except pigeons", () =>
7{
8{
@@ -13,6 +14,16 @@ test("living targetable mobs can fight except pigeons", () =>
13test("mob attack profile uses one attackSkill for hit and damage rolls", () =>
14test("mob attack profile uses one attackSkill for hit and damage rolls", () =>
14{
15{
15 const profile = mobMeleeAttackProfile(mobDefinition(MobId.rat));
16 const profile = mobMeleeAttackProfile(mobDefinition(MobId.rat));
16 assert.equal(profile.mobAttackSkill, 3);
17 assert.equal(profile.mobAttackSkill, 1);
17 assert.equal(profile.baseDamage, undefined);
18 assert.equal(profile.baseDamage, undefined);
18});
19});
20
21test("goblins can fight and default to level 5 with slashing melee", () =>
22{
23 assert.equal(mobCanFight(mobDefinition(MobId.goblin)), true);
24 const profile = mobMeleeAttackProfile(mobDefinition(MobId.goblin), 5);
25 assert.equal(profile.mobAttackSkill, 5);
26 assert.equal(profile.mode, "melee");
27 assert.equal(profile.attackType, "slashing");
28 assert.equal(profile.hitSkillId, SkillId.swordfighting);
29});
server/tests/skills.test.js +3 −3
@@ -24,6 +24,7 @@ import { resolveMeleeSwing, attackProfileOf } from "../src/combat/service.js";
24 24 import { mobComponents } from "../src/simulation/spawners.js";
25 25 import { MobId } from "../../shared/definitions/mobs.js";
26 26 import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
27 import { installDamagingCombatRng } from "./helpers/combatRng.js";
27 28
28 29 test("enforceMinimumSkillLevels raises Health to 10 and leaves higher XP alone", () =>
29 30 {
@@ -180,8 +181,7 @@ test("combat xp equals damage dealt to hit and damage skills", () =>
180 181 test("successful player melee hits award xp equal to damage", () =>
181 182 {
182 183 const harness = makeSimulation({ workerCount: 1 });
183 const random = Math.random;
184 Math.random = () => 0;
184 const restore = installDamagingCombatRng();
185 185 try
186 186 {
187 187 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
@@ -204,7 +204,7 @@ test("successful player melee hits award xp equal to damage", () =>
204 204 }
205 205 finally
206 206 {
207 Math.random = random;
207 restore();
208 208 harness.close();
209 209 }
210 210 });
@@ -24,6 +24,7 @@ import { resolveMeleeSwing, attackProfileOf } from "../src/combat/service.js";
24import { mobComponents } from "../src/simulation/spawners.js";
24import { mobComponents } from "../src/simulation/spawners.js";
25import { MobId } from "../../shared/definitions/mobs.js";
25import { MobId } from "../../shared/definitions/mobs.js";
26import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
26import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
27import { installDamagingCombatRng } from "./helpers/combatRng.js";
27
28
28test("enforceMinimumSkillLevels raises Health to 10 and leaves higher XP alone", () =>
29test("enforceMinimumSkillLevels raises Health to 10 and leaves higher XP alone", () =>
29{
30{
@@ -180,8 +181,7 @@ test("combat xp equals damage dealt to hit and damage skills", () =>
180test("successful player melee hits award xp equal to damage", () =>
181test("successful player melee hits award xp equal to damage", () =>
181{
182{
182 const harness = makeSimulation({ workerCount: 1 });
183 const harness = makeSimulation({ workerCount: 1 });
183 const random = Math.random;
184 const restore = installDamagingCombatRng();
184 Math.random = () => 0;
185 try
185 try
186 {
186 {
187 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
187 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
@@ -204,7 +204,7 @@ test("successful player melee hits award xp equal to damage", () =>
204 }
204 }
205 finally
205 finally
206 {
206 {
207 Math.random = random;
207 restore();
208 harness.close();
208 harness.close();
209 }
209 }
210});
210});
server/tests/spawner-bounty.test.js +35 −3
@@ -15,6 +15,8 @@ import { mobComponents } from "../src/simulation/spawners.js";
15 15 import { Component } from "../src/ecs/components.js";
16 16 import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
17 17 import { ItemId } from "../../shared/definitions/items.js";
18 import { attackProfileOf } from "../src/combat/service.js";
19 import { installDamagingCombatRng } from "./helpers/combatRng.js";
18 20
19 21 test("spawner config defaults bounty 0 and level 1", () =>
20 22 {
@@ -36,6 +38,37 @@ test("town guard spawner defaults level, bounty, and equipment", () =>
36 38 assert.equal(config.equipment.weapon, 245);
37 39 });
38 40
41 test("goblin spawner defaults level 5 without equipment", () =>
42 {
43 const config = normalizeSpawnerConfig({ mobTypeId: MobId.goblin }, "goblin-1");
44 assert.equal(config.level, 5);
45 assert.equal(config.bounty, 0);
46 assert.equal(config.equipment.weapon, null);
47 });
48
49 test("goblin spawns with slashing melee profile from mob definition", () =>
50 {
51 const harness = makeSimulation({ workerCount: 1 });
52 try
53 {
54 const config = normalizeSpawnerConfig({ mobTypeId: MobId.goblin }, "goblin-1");
55 const goblinId = harness.simulation.spawnEntity(mobComponents({
56 mobTypeId: MobId.goblin,
57 tileX: 8,
58 tileY: 8,
59 level: config.level
60 }));
61 const worker = harness.simulation.workers[0];
62 const profile = attackProfileOf(worker.world.ecs, worker.world.ecs.handleOf(goblinId));
63 assert.equal(profile.mode, "melee");
64 assert.equal(profile.attackType, "slashing");
65 assert.equal(profile.hitSkillId, SkillId.swordfighting);
66 assert.equal(profile.mobAttackSkill, 5);
67 }
68 finally
69 { harness.close(); }
70 });
71
39 72 test("humanoid spawners do not buy from players by default", () =>
40 73 {
41 74 const config = normalizeSpawnerConfig({ mobTypeId: MobId.humanoid }, "shop-1");
@@ -168,8 +201,7 @@ test("aggressive humanoid stores aggro flag and zero bounty", () =>
168 201 test("killing a bounty mob awards player bounty and can wake town guards", () =>
169 202 {
170 203 const harness = makeSimulation({ workerCount: 1 });
171 const random = Math.random;
172 Math.random = () => 0;
204 const restore = installDamagingCombatRng();
173 205 try
174 206 {
175 207 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
@@ -229,7 +261,7 @@ test("killing a bounty mob awards player bounty and can wake town guards", () =>
229 261 }
230 262 finally
231 263 {
232 Math.random = random;
264 restore();
233 265 harness.close();
234 266 }
235 267 });
@@ -15,6 +15,8 @@ import { mobComponents } from "../src/simulation/spawners.js";
15import { Component } from "../src/ecs/components.js";
15import { Component } from "../src/ecs/components.js";
16import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
16import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
17import { ItemId } from "../../shared/definitions/items.js";
17import { ItemId } from "../../shared/definitions/items.js";
18import { attackProfileOf } from "../src/combat/service.js";
19import { installDamagingCombatRng } from "./helpers/combatRng.js";
18
20
19test("spawner config defaults bounty 0 and level 1", () =>
21test("spawner config defaults bounty 0 and level 1", () =>
20{
22{
@@ -36,6 +38,37 @@ test("town guard spawner defaults level, bounty, and equipment", () =>
36 assert.equal(config.equipment.weapon, 245);
38 assert.equal(config.equipment.weapon, 245);
37});
39});
38
40
41test("goblin spawner defaults level 5 without equipment", () =>
42{
43 const config = normalizeSpawnerConfig({ mobTypeId: MobId.goblin }, "goblin-1");
44 assert.equal(config.level, 5);
45 assert.equal(config.bounty, 0);
46 assert.equal(config.equipment.weapon, null);
47});
48
49test("goblin spawns with slashing melee profile from mob definition", () =>
50{
51 const harness = makeSimulation({ workerCount: 1 });
52 try
53 {
54 const config = normalizeSpawnerConfig({ mobTypeId: MobId.goblin }, "goblin-1");
55 const goblinId = harness.simulation.spawnEntity(mobComponents({
56 mobTypeId: MobId.goblin,
57 tileX: 8,
58 tileY: 8,
59 level: config.level
60 }));
61 const worker = harness.simulation.workers[0];
62 const profile = attackProfileOf(worker.world.ecs, worker.world.ecs.handleOf(goblinId));
63 assert.equal(profile.mode, "melee");
64 assert.equal(profile.attackType, "slashing");
65 assert.equal(profile.hitSkillId, SkillId.swordfighting);
66 assert.equal(profile.mobAttackSkill, 5);
67 }
68 finally
69 { harness.close(); }
70});
71
39test("humanoid spawners do not buy from players by default", () =>
72test("humanoid spawners do not buy from players by default", () =>
40{
73{
41 const config = normalizeSpawnerConfig({ mobTypeId: MobId.humanoid }, "shop-1");
74 const config = normalizeSpawnerConfig({ mobTypeId: MobId.humanoid }, "shop-1");
@@ -168,8 +201,7 @@ test("aggressive humanoid stores aggro flag and zero bounty", () =>
168test("killing a bounty mob awards player bounty and can wake town guards", () =>
201test("killing a bounty mob awards player bounty and can wake town guards", () =>
169{
202{
170 const harness = makeSimulation({ workerCount: 1 });
203 const harness = makeSimulation({ workerCount: 1 });
171 const random = Math.random;
204 const restore = installDamagingCombatRng();
172 Math.random = () => 0;
173 try
205 try
174 {
206 {
175 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
207 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
@@ -229,7 +261,7 @@ test("killing a bounty mob awards player bounty and can wake town guards", () =>
229 }
261 }
230 finally
262 finally
231 {
263 {
232 Math.random = random;
264 restore();
233 harness.close();
265 harness.close();
234 }
266 }
235});
267});
shared/definitions/mobCombat.js +12 −1
@@ -1,5 +1,6 @@
1 1 import { mobDefinition, mobMaxHealthForLevel } from "./mobs.js";
2 2 import { SkillId, UNARMED_ATTACK, ALL_SKILL_IDS } from "./skills.js";
3 import { AttackType } from "./attackTypes.js";
3 4
4 5 export const DEFAULT_AGGRO_RANGE_TILES = 5;
5 6
@@ -37,10 +38,20 @@ export function mobMeleeAttackProfile (def, level = null)
37 38 1,
38 39 level != null ? (level | 0) : (def?.attackSkill | 0 || 1)
39 40 );
41 const custom = def?.meleeAttack;
42 if (custom)
43 {
44 return {
45 ...custom,
46 cooldownMs: custom.cooldownMs ?? def?.attackCooldownMs ?? 900,
47 mobAttackSkill: attackSkill
48 };
49 }
40 50 return {
41 51 mode: "melee",
42 52 hitSkillId: SkillId.unarmed,
43 damageSkillId: SkillId.unarmed,
53 damageSkillId: SkillId.strength,
54 attackType: AttackType.blunt,
44 55 cooldownMs: def?.attackCooldownMs ?? 900,
45 56 mobAttackSkill: attackSkill
46 57 };
@@ -1,5 +1,6 @@
1import { mobDefinition, mobMaxHealthForLevel } from "./mobs.js";
1import { mobDefinition, mobMaxHealthForLevel } from "./mobs.js";
2import { SkillId, UNARMED_ATTACK, ALL_SKILL_IDS } from "./skills.js";
2import { SkillId, UNARMED_ATTACK, ALL_SKILL_IDS } from "./skills.js";
3import { AttackType } from "./attackTypes.js";
3
4
4export const DEFAULT_AGGRO_RANGE_TILES = 5;
5export const DEFAULT_AGGRO_RANGE_TILES = 5;
5
6
@@ -37,10 +38,20 @@ export function mobMeleeAttackProfile (def, level = null)
37 1,
38 1,
38 level != null ? (level | 0) : (def?.attackSkill | 0 || 1)
39 level != null ? (level | 0) : (def?.attackSkill | 0 || 1)
39 );
40 );
41 const custom = def?.meleeAttack;
42 if (custom)
43 {
44 return {
45 ...custom,
46 cooldownMs: custom.cooldownMs ?? def?.attackCooldownMs ?? 900,
47 mobAttackSkill: attackSkill
48 };
49 }
40 return {
50 return {
41 mode: "melee",
51 mode: "melee",
42 hitSkillId: SkillId.unarmed,
52 hitSkillId: SkillId.unarmed,
43 damageSkillId: SkillId.unarmed,
53 damageSkillId: SkillId.strength,
54 attackType: AttackType.blunt,
44 cooldownMs: def?.attackCooldownMs ?? 900,
55 cooldownMs: def?.attackCooldownMs ?? 900,
45 mobAttackSkill: attackSkill
56 mobAttackSkill: attackSkill
46 };
57 };
shared/definitions/mobs.js +10 −12
@@ -10,8 +10,8 @@ import { spellDefinition } from "./spells.js";
10 10 import { ClothTier } from "./materialTiers.js";
11 11 import { ClothItemId } from "./clothItems.js";
12 12 import { EssenceItemId } from "./essenceItems.js";
13 import { SwordItemId } from "./swordItems.js";
14 import { MagicElement } from "./attackTypes.js";
13 import { AttackType, MagicElement } from "./attackTypes.js";
14 import { SkillId } from "./skills.js";
15 15
16 16 export const MobId = Object.freeze({
17 17 deer: "deer",
@@ -151,6 +151,13 @@ export const MOB_DEFINITIONS = Object.freeze({
151 151 untargetable: false,
152 152 aggressive: true,
153 153 aggroRangeTiles: 5,
154 meleeAttack: Object.freeze({
155 mode: "melee",
156 rangeTiles: 1,
157 hitSkillId: SkillId.swordfighting,
158 damageSkillId: SkillId.strength,
159 attackType: AttackType.slashing
160 }),
154 161 gait: "walk",
155 162 sex: "male",
156 163 frames: 1,
@@ -509,16 +516,7 @@ export function defaultSpawnerConfig (spawnerId, mobTypeId = MobId.deer)
509 516 weapon: WoodItemId.oakQuarterstaff,
510 517 offhand: null
511 518 }
512 : (mobTypeId === MobId.goblin
513 ? {
514 head: null,
515 chest: null,
516 legs: null,
517 boots: null,
518 weapon: SwordItemId.bronzeSword,
519 offhand: null
520 }
521 : emptySpawnerEquipment()),
519 : emptySpawnerEquipment(),
522 520 sells: [],
523 521 drops: [],
524 522 spells: [],
@@ -10,8 +10,8 @@ import { spellDefinition } from "./spells.js";
10import { ClothTier } from "./materialTiers.js";
10import { ClothTier } from "./materialTiers.js";
11import { ClothItemId } from "./clothItems.js";
11import { ClothItemId } from "./clothItems.js";
12import { EssenceItemId } from "./essenceItems.js";
12import { EssenceItemId } from "./essenceItems.js";
13import { SwordItemId } from "./swordItems.js";
13import { AttackType, MagicElement } from "./attackTypes.js";
14import { MagicElement } from "./attackTypes.js";
14import { SkillId } from "./skills.js";
15
15
16export const MobId = Object.freeze({
16export const MobId = Object.freeze({
17 deer: "deer",
17 deer: "deer",
@@ -151,6 +151,13 @@ export const MOB_DEFINITIONS = Object.freeze({
151 untargetable: false,
151 untargetable: false,
152 aggressive: true,
152 aggressive: true,
153 aggroRangeTiles: 5,
153 aggroRangeTiles: 5,
154 meleeAttack: Object.freeze({
155 mode: "melee",
156 rangeTiles: 1,
157 hitSkillId: SkillId.swordfighting,
158 damageSkillId: SkillId.strength,
159 attackType: AttackType.slashing
160 }),
154 gait: "walk",
161 gait: "walk",
155 sex: "male",
162 sex: "male",
156 frames: 1,
163 frames: 1,
@@ -509,16 +516,7 @@ export function defaultSpawnerConfig (spawnerId, mobTypeId = MobId.deer)
509 weapon: WoodItemId.oakQuarterstaff,
516 weapon: WoodItemId.oakQuarterstaff,
510 offhand: null
517 offhand: null
511 }
518 }
512 : (mobTypeId === MobId.goblin
519 : emptySpawnerEquipment(),
513 ? {
514 head: null,
515 chest: null,
516 legs: null,
517 boots: null,
518 weapon: SwordItemId.bronzeSword,
519 offhand: null
520 }
521 : emptySpawnerEquipment()),
522 sells: [],
520 sells: [],
523 drops: [],
521 drops: [],
524 spells: [],
522 spells: [],