Diablo II's math, rebuilt as pure functions

Damage, hit chance, resistances, monster scaling and 54 skills as pure shared functions, pinned by balance tests that print their tables.

Four heroes fight a pack in a red canyon while lightning and ring effects go off around them
Contents
  1. One file for derived stats, one for hits
  2. Monsters scale by formula, not by hand
  3. Three classes, nine trees, 54 skills
  4. Tests that print tables
  5. The early-game taper
  6. Pack pressure as a first-class column
  7. Related posts

All combat math lives in three pure files under shared/combat/, imported by the server sim, the client tooltips and the balance tests. The sheet, the sim and the tests can't disagree because they run the same code.

One file for derived stats, one for hits

Nothing derived is persisted. A save holds level, points and item instances; everything else is recomputed:

shared/combat/derive.tsTypeScript
const lifeMax = Math.max(1, Math.floor((cd.baseLife + vit * cd.lifePerVit + input.level * cd.lifePerLevel + t.life) * (1 + t.lifePct / 100)))
const defense = Math.max(0, Math.floor((armorDefense + t.defense + dex / 4) * (1 + t.defensePct / 100)))
const attackRating = Math.max(1, Math.floor((dex * 5 - 35 + cd.arBonus + t.attackRating + input.level * 2) * (1 + t.attackRatingPct / 100)))
 
const maxRes = Math.min(90, 75 + t.maxRes)

deriveStats (268 lines, no side effects) walks ten slots, counts set pieces, applies +skills only to skills with a hard point (D2's rule), folds in passives and party auras, then clamps:

Cap Value
Block 75%
Dodge 50%
Crit 75%
Physical reduction 50%
Cooldown reduction 40%
Resistances 75% (90% with maxRes)
The character sheet of a level-75 Warden in Emberhold
The character sheet is deriveStats rendered: 694–1285 damage, 1092 attack rating, 6,729 defense and 1,387 life on a level-75 dev Warden. The Endgame group at the bottom shows the ladder rank.

damage.ts resolves one hit:

shared/combat/damage.tsTypeScript
/** D2 hit chance, clamped 5–95%. */
export function hitChance(ar: number, def: number, alvl: number, dlvl: number): number {
  const a = Math.max(1, ar)
  const d = Math.max(0, def)
  return clamp((2 * a) / (a + d) * (alvl / (alvl + dlvl)), 0.05, 0.95)
}
 
/** Resist after penetration. Immunity (≥100) cannot be pierced; others floor at −100. */
export function effectiveResist(res: number, pen: number): number {
  if (res >= 100) return 100
  return clamp(res - pen, -100, 95)
}

rollDamage(attacker, defender, packet, rng) in D2 order:

  1. Attacks roll hit, dodge, block; spells skip all three
  2. Crit multiplies the packet (5% base, ×1.5)
  3. Bonus vs undead, demons, beasts; then shock and weaken
  4. Each type rolls against its own resist: physical takes resist, % and flat reduction; elements take resist minus penetration
  5. Poison is never instant: a stacking DoT over the effect's duration
  6. A connecting hit does at least 1 unless the target is immune

The Rng is a parameter: per-room on the server, seeded in tests, so tables print the same numbers every run.

Monsters scale by formula, not by hand

109 monster definitions (28 in Act 1, 17 in Act 6; 20 boss definitions for 18 encounters, since the Drowned Choir has three voices) store only level-1 stats:

shared/combat/monster.tsTypeScript
export const lifeMult = (l: number): number => (1 + 0.2 * (l - 1)) * Math.pow(1.05, l - 1)
export const MONSTER_DMG_SCALE = 1.4
export const DMG_COMPOUND_CAP = 45
export const dmgMult = (l: number): number =>
  MONSTER_DMG_SCALE * (1 + 0.12 * (l - 1)) * Math.pow(1.035, Math.min(l, DMG_COMPOUND_CAP) - 1)
Tier Life Damage Mods
Champion 1.5× 1.25× none
Unique elite 4× 1.4× 1–3 of 16 (eleven D2-style plus molten, tidal, void touched…)
Party of n (within 25 tiles, cap 8) ×(1 + 0.35·(n − 1)) none none

DMG_COMPOUND_CAP: player life is roughly linear, 1.035^l isn't. Uncapped, Act 6 normals kill a geared hero in 1.4–1.8 hits at levels 50–70. Capped at 45, level 99 is 81× instead of 520×, which matters most in Nightmare and Hell.

Two panels: the early taper over levels 1–20 and the compounding cap over levels 1–99
Left: the shipped curve (orange) starts at 0.4× the old one and joins it at level 16. Right: without the cap, level-99 monsters hit 520× their level-1 damage; with it, 81×. Both curves are computed from the exported constants.

Three classes, nine trees, 54 skills

Three trees per class, six skills per tree at levels 1, 6, 12, 18, 24, 30, 20 points max. Each tree is one build with a capstone: Steel ends in Executioner's Verdict, Venom stacks poison ×10 and traps, Flame's Firebolt and Fireball feed each other up to Starfall.

Class Trees Starts with Base life at L1
Warden Steel, Bulwark, Wrath Sunder, short sword + buckler 79
Stalker Marksman, Venom, Shadow Barbed Arrow, short bow 61
Emberwright Flame, Frost, Storm Firebolt, quarterstaff 52

Behaviors are implemented once (melee, projectile, aoe_point, nova, cone, chain, leap, dash, blink, buff, aura, summon, trap, channel, plus attack and passive); a skill is a record:

shared/data/skills/emberwright.tsTypeScript
{
  id: 'e_starfall',
  name: 'Starfall',
  tier: 5,
  reqLevel: 30,
  prereqs: ['e_ember_sentinel', 'e_pyre_mastery'],
  behavior: { kind: 'aoe_point', range: 14, radius: sc(3.5, 0.08), delayMs: 1100 },
  mana: sc(20, 1),
  castMs: 700,
  damage: { kind: 'spell', type: 'fire', min: sc(60, 18, 24, 32), max: sc(90, 22, 28, 38) },
  effects: [{ status: 'burn', chance: 100, durationMs: 3000, magnitude: 30 }],
  synergies: [
    { skill: 'e_firebolt', perLevel: 6, target: 'damage' },
    { skill: 'e_fireball', perLevel: 6, target: 'damage' },
    { skill: 'e_cinderfield', perLevel: 4, target: 'damage' },
  ],
  description: 'Call down a burning star: after a moment it strikes a {radius} yard area for {min}-{max} fire damage and sets the survivors ablaze.',
}
  • sc(base, per, per9, per17): one slope for points 1–8, 9–16, 17+. Starfall: 60–90 at 1, 506–620 at 20, +320% from maxed synergies.
  • Synergies read hard points only, so +2 Flame Skills gear can't double-dip.
  • skillNumbers() fills {min}/{max}/{radius} from the real packet math; a test fails on any unresolved placeholder.
The skill tree panels for all three classes, with a Fireball tooltip showing synergy bonuses
The skill trees, one per class. The Fireball tooltip shows the current and next level and both synergies: +10% per Firebolt point and +5% per Starfall point.
Rending Gale, the Wrath tree's whirlwind: a `channel` behavior that drains mana per tick and hits everything in a radius. Captured in-engine for the trailer.

Tests that print tables

Each balance file prints console.table then asserts bands. bun test shared/combat: 28 tests, 307 assertions, 5 files, ~2 s.

Same-level fight Warden Stalker Emberwright
L1 time to kill a zombie 1.8 s 1.7 s 2.2 s
L10 time to kill a zombie 1.6 s 2.0 s 1.1 s
L40 time to kill a death knight 2.5 s 3.0 s 2.4 s
L70 time to kill a void stalker 4.2 s 4.4 s 5.2 s
L70: a void stalker kills you in 12 s 6 s 6 s
Solo Unmaker, pure uptime 260 s 250 s 343 s

XP pacing from a simulated pass through every zone: Act 2 at 17, Act 3 at 30, Act 4 at 43, Act 5 at 55, Act 6 at 66, story done around 75. A data change shows up as a table diff, and outside a band as a failure.

A Stalker in the dunes under Rain of Barbs
Rain of Barbs, the Marksman capstone, is an aoe_point with scattered strikes. At level 20 the Stalker's Act 2 trash dies in about a second.

The early-game taper

With 1.4× from level 1, four blightwolves killed a fresh Emberwright in ~1.4 s. The curve is right from Act 2 on, so the fix only touches the start:

shared/combat/monster.tsTypeScript
export const EARLY_DMG_FLOOR = 0.4
export const EARLY_DMG_FULL_LEVEL = 16
export const earlyDmgTaper = (l: number): number =>
  l >= EARLY_DMG_FULL_LEVEL ? 1 : EARLY_DMG_FLOOR + ((1 - EARLY_DMG_FLOOR) * (Math.max(1, l) - 1)) / (EARLY_DMG_FULL_LEVEL - 1)
  • 0.4× at level 1, linear to 1.0× at 16; hostile monsters only (minionStats passes hostile = false so summons don't weaken)
  • +15 base life per class: 79 / 61 / 52 at level 1
  • Belt starts with four healing potions, plus two healing and two mana packed
  • Potion weight ×1.5 at item level ≤ 12 (EARLY_POTION_MULT)

Rejected in decisions.md: lowering MONSTER_DMG_SCALE globally (retunes every act), editing Act 1 monsters (misses the curve's shape), cutting monster life (breaks 3–6 hits to kill).

Same simulation with and without the change, 2,000 fights per cell:

A dumbbell chart: hits to die for each class against four Blighted Fields monsters, before and after
A level-1 hero in the starting kit against the first zone's trash, with and without the taper. The Emberwright goes from 5.8 blightwolf bites to 19.7. The dashed line is the test floor.

balanceEarly.test.ts asserts:

  • At least 15 landed hits to die from Blighted Fields trash (Warden 25)
  • A standing hero beats an average pack with at most one minor potion
  • The starting skill still kills that trash in under 4 s (tanks 7 s)
Class vs pack Pack size Life Damage taken Margin
Warden vs blightwolves 4 79 66 1.81
Stalker vs blightwolves 4 61 84 1.21
Emberwright vs blightwolves 4 52 70 1.31
Emberwright vs zombies 4.5 52 40 2.29

Margin = (life + one potion) / damage taken killing the whole pack one at a time; pass is 1.1. Every Act 1 monster passed the old per-fight bands ("five swings to kill a fresh hero") even untapered; the whole-pack margin is stricter and closer to how new players fight.

A party of four in a canyon: the kind of fight the tables are trying to predict.

Pack pressure as a first-class column

Single-fight numbers undercount danger: a zone is packs back to back with no regen. "Seconds under a full pack" belongs next to time-to-kill in every act's table, with its own band.