BlueGit

timfa questward

Files

questward / server / tests / helpers / combatRng.js

combatRng.js

combatRng.js 1.4 KiB Download Raw
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}