diablo2-web/tests/parity-1.13c.test.ts

554 lines
18 KiB
TypeScript

import { describe, it, expect, beforeAll } from 'vitest'
import { ACT_BOSSES, BOSS_LIST, type BossDefinition } from '../src/boss.ts'
import { getEmbeddedDropTables } from '../src/game/embedded-drop-tables.ts'
import {
executeDropPipeline,
resolveItemTypeFlags,
createDroppedItem,
type DropTables,
} from '../src/game/drop-pipeline.ts'
import {
NON_RANDOM_RANGE_CODES,
rollMagicAffixes,
rollRareAffixes,
} from '../src/game/affix-generator.ts'
import { formatItemTooltip } from '../src/game/item-tooltip.ts'
import { D2Rng } from '../src/game/d2-rng.ts'
import type { MagicAffix, AffixTable } from '../src/game/affixes.ts'
import type { RareNameTable } from '../src/game/rare-names.ts'
import type { UpgradableItemBase } from '../src/game/item-upgrade.ts'
import type { Item, ItemBase } from '../src/game/items.ts'
import { runParityAudit } from '../scripts/audit-parity.ts'
export type BaseTier = 'normal' | 'exceptional' | 'elite'
export function classifyItemBaseTier(item: Item): BaseTier | null {
if (item.base.kind !== 'weapon' && item.base.kind !== 'armor') {
return null
}
const b = item.base as UpgradableItemBase
const id = (b.id ?? '').trim().toLowerCase()
const uber = (b.ubercode ?? '').trim().toLowerCase()
const ultra = (b.ultracode ?? '').trim().toLowerCase()
if (ultra && ultra !== 'xxx' && id === ultra) {
return 'elite'
}
if (uber && uber !== 'xxx' && id === uber) {
return 'exceptional'
}
return 'normal'
}
describe('Diablo II 1.13c Parity Baseline & Boss monsterType=4 Upgrade Odds (Issue #126)', () => {
let dropTables: DropTables
beforeAll(() => {
dropTables = getEmbeddedDropTables()
})
it('1. BOSS_LIST and ACT_BOSSES define monsterType === 4 for all 5 Act Bosses', () => {
expect(BOSS_LIST).toBeDefined()
expect(BOSS_LIST).toHaveLength(5)
expect(BOSS_LIST).toBe(ACT_BOSSES)
for (const boss of BOSS_LIST) {
const def: BossDefinition = boss
expect(def.monsterType, `Boss ${def.id} must explicitly set monsterType: 4`).toBe(4)
}
})
it('2. Difficultylevels.txt Good odds (UberCodeOddsGood / UltraCodeOddsGood) exceed Normal odds in Nightmare and Hell', () => {
const nmOdds = dropTables.difficultyLevels.get('Nightmare')!
const hellOdds = dropTables.difficultyLevels.get('Hell')!
expect(nmOdds.uberCodeOddsGood).toBeGreaterThan(nmOdds.uberCodeOddsNormal)
expect(hellOdds.uberCodeOddsGood).toBeGreaterThan(hellOdds.uberCodeOddsNormal)
expect(hellOdds.ultraCodeOddsGood).toBeGreaterThan(hellOdds.ultraCodeOddsNormal)
})
it('3. monsterType=4 (Boss) yields significantly higher Exceptional/Elite base upgrades than monsterType=1 (Normal) over identical seeds', () => {
const sampleKills = 400
let normalMonsterUpgraded = 0
let normalMonsterEquipCount = 0
let bossMonsterUpgraded = 0
let bossMonsterEquipCount = 0
for (let i = 0; i < sampleKills; i++) {
const seed = (0x12600000 + i * 7919) >>> 0
// Run with monsterType = 1 (Normal monster: uses uberCodeOddsNormal=20, ultraCodeOddsNormal=30)
const dropsNormalType = executeDropPipeline({
tcName: 'Mephistoq (H)',
nLevel: 87,
monsterType: 1,
dropTables,
difficulty: 'hell',
playerMf: 250,
gamePlayers: 1,
partyPlayers: 1,
monsterRng: seed,
})
for (const item of dropsNormalType) {
const tier = classifyItemBaseTier(item)
if (tier !== null) {
normalMonsterEquipCount++
if (tier === 'exceptional' || tier === 'elite') {
normalMonsterUpgraded++
}
}
}
// Run with monsterType = 4 (Act Boss: uses uberCodeOddsGood=40, ultraCodeOddsGood=40)
const dropsBossType = executeDropPipeline({
tcName: 'Mephistoq (H)',
nLevel: 87,
monsterType: 4,
dropTables,
difficulty: 'hell',
playerMf: 250,
gamePlayers: 1,
partyPlayers: 1,
monsterRng: seed,
})
for (const item of dropsBossType) {
const tier = classifyItemBaseTier(item)
if (tier !== null) {
bossMonsterEquipCount++
if (tier === 'exceptional' || tier === 'elite') {
bossMonsterUpgraded++
}
}
}
}
expect(normalMonsterEquipCount).toBeGreaterThan(500)
expect(bossMonsterEquipCount).toBe(normalMonsterEquipCount)
const normalUpgradeRate = normalMonsterUpgraded / normalMonsterEquipCount
const bossUpgradeRate = bossMonsterUpgraded / bossMonsterEquipCount
// Boss (Good odds: 40/1024 + 40/1024) must strictly exceed Normal (20/1024 + 30/1024)
expect(bossMonsterUpgraded).toBeGreaterThan(normalMonsterUpgraded)
expect(bossUpgradeRate).toBeGreaterThan(normalUpgradeRate)
})
it('4. executeDropPipeline works properly with monsterType: 4 across all 5 Act Bosses in Hell', () => {
for (const boss of BOSS_LIST) {
const drops = executeDropPipeline({
tcName: boss.questTc.hell,
nLevel: boss.mlvl.hell,
monsterType: boss.monsterType ?? 4,
dropTables,
difficulty: 'hell',
playerMf: 250,
gamePlayers: 1,
partyPlayers: 1,
monsterRng: 1337 + boss.act,
})
expect(drops.length).toBeGreaterThan(0)
for (const item of drops) {
expect(item.ilvl).toBe(boss.mlvl.hell)
}
}
})
it('5. runParityAudit generates structured JSON summary of quality, base upgrade, and affix distributions', () => {
const summary = runParityAudit({
kills: 100,
bossId: 'mephisto',
difficulty: 'hell',
playerMf: 250,
gamePlayers: 1,
})
expect(summary.config.boss).toBe('mephisto')
expect(summary.config.monsterType).toBe(4)
expect(summary.totalItems).toBeGreaterThan(0)
expect(summary.baseTierUpgrade.totalEquipment).toBeGreaterThan(0)
expect(summary.baseTierUpgrade.upgradedRatio).toBeGreaterThan(0)
})
})
describe('ItemTypes.txt Quality Flags & classSpecific Propagation (Issue #127 / A2)', () => {
let dropTables: DropTables
beforeAll(() => {
dropTables = getEmbeddedDropTables()
})
it('1. resolveItemTypeFlags returns isAlwaysMagic: true for rin, amu, jew, cm1, cm2, cm3', () => {
const alwaysMagicCodes = ['rin', 'amu', 'jew', 'cm1', 'cm2', 'cm3']
for (const code of alwaysMagicCodes) {
const base = dropTables.getBase(code)
expect(base, `Base ${code} must exist`).toBeDefined()
const flags = resolveItemTypeFlags(base!, dropTables.itemTypes, dropTables.isA)
expect(flags.isAlwaysMagic, `${code} must have isAlwaysMagic === true`).toBe(true)
expect(flags.isNormalOnly, `${code} must have isNormalOnly === false`).toBe(false)
if (code === 'cm1' || code === 'cm2' || code === 'cm3') {
expect(flags.canBeRare, `${code} charm must have canBeRare === false`).toBe(false)
} else {
expect(flags.canBeRare, `${code} must have canBeRare === true`).toBe(true)
}
}
})
it('2. 200 rolls of Ring / Amulet / Small Charm at playerMf: -100 and 0, nLevel: 1 NEVER produce normal/superior/low (and cm1 NEVER produces rare)', () => {
const validQualities = new Set(['magic', 'rare', 'set', 'unique'])
const forbiddenQualities = new Set(['normal', 'superior', 'low'])
for (const mf of [-100, 0]) {
for (const code of ['rin', 'amu', 'cm1']) {
let countNormalOrSuperiorOrLow = 0
let countRare = 0
let totalGenerated = 0
for (let i = 0; i < 200; i++) {
const drops = executeDropPipeline({
tcName: code,
nLevel: 1,
monsterType: 1,
dropTables,
difficulty: 'normal',
playerMf: mf,
gamePlayers: 1,
partyPlayers: 1,
monsterRng: (0x12700000 + i * 1009 + (mf === 0 ? 500000 : 0)) >>> 0,
})
expect(drops.length).toBe(1)
const item = drops[0]!
totalGenerated++
const q = item.rarity ?? ''
if (forbiddenQualities.has(q)) {
countNormalOrSuperiorOrLow++
}
if (q === 'rare') {
countRare++
}
expect(validQualities.has(q), `Item ${item.name} (${code}) rolled invalid quality ${q} at MF=${mf}`).toBe(true)
expect(item.name).not.toMatch(/^Cracked /)
expect(item.name).not.toMatch(/^Superior /)
}
expect(totalGenerated).toBe(200)
expect(countNormalOrSuperiorOrLow).toBe(0)
if (code === 'cm1') {
expect(countRare).toBe(0)
}
}
}
})
it('3. resolveItemTypeFlags identifies class-specific equipment bases (Sorceress Orb, Paladin Shield, Amazon Bow, etc.) with isClassSpecific === true', () => {
// Sorceress Orb: ob1 (Eagle Orb), obc (Eldritch Orb), obf (Dimensional Shard)
// Paladin Auric Shield: pa1 (Targe), paa (Akaran Targe), paf (Vortex Shield)
// Amazon Bow: am1 (Stag Bow), am6 (Ashwood Bow), amb (Matriarchal Bow)
// Barbarian Primal Helm: ba1 (Jawbone Cap)
// Druid Pelt: dr1 (Wolf Head)
// Necromancer Voodoo Head: ne1 (Preserved Head)
// Assassin Katar: ktr (Katar)
const classSpecificCodes = ['ob1', 'obf', 'pa1', 'paf', 'am1', 'amb', 'ba1', 'dr1', 'ne1', 'ktr']
for (const code of classSpecificCodes) {
const base = dropTables.getBase(code)
expect(base, `Class-specific base ${code} must exist`).toBeDefined()
const flags = resolveItemTypeFlags(base!, dropTables.itemTypes, dropTables.isA)
expect(flags.isClassSpecific, `${code} (${base!.name}) must have isClassSpecific === true`).toBe(true)
}
// Generic equipment bases must have isClassSpecific === false
const genericCodes = ['cap', 'uap', 'uit', 'lsd', 'crn', 'rin', 'amu', 'cm1']
for (const code of genericCodes) {
const base = dropTables.getBase(code)
expect(base, `Generic base ${code} must exist`).toBeDefined()
const flags = resolveItemTypeFlags(base!, dropTables.itemTypes, dropTables.isA)
expect(flags.isClassSpecific, `${code} (${base!.name}) must have isClassSpecific === false`).toBe(false)
}
})
})
describe('Magic & Rare Affix NON_RANDOM_RANGE_CODES Range & Skill Parameter Protection (Issue #128)', () => {
const swordBase: ItemBase = {
id: 'crs',
name: 'Crystal Sword',
kind: 'weapon',
tags: ['swor', 'weap'],
damage: 15,
defense: 0,
value: 1200,
invWidth: 2,
invHeight: 3,
maxStack: 1,
level: 11,
}
const mockIsA = (type: string, target: string): boolean => {
if (type === target) return true
if (type === 'swor' && (target === 'weap' || target === 'mele')) return true
return false
}
const mockRareNames: { prefixes: RareNameTable; suffixes: RareNameTable } = {
prefixes: {
entries: [],
getEligible: () => [{ index: 0, name: 'Fiend', version: 100, itypes: ['weap'], etypes: [], add: 0, multiply: 0, divide: 0 }],
} as unknown as RareNameTable,
suffixes: {
entries: [],
getEligible: () => [{ index: 0, name: 'Fang', version: 100, itypes: ['weap'], etypes: [], add: 0, multiply: 0, divide: 0 }],
} as unknown as RareNameTable,
}
function makeAffix(
name: string,
isPrefix: boolean,
group: number,
mods: { code: string; param?: string; min: number; max: number }[],
): MagicAffix {
return {
name,
isPrefix,
version: 100,
spawnable: true,
rare: true,
level: 1,
maxlevel: 0,
levelreq: 12,
frequency: 10,
group,
mods,
transform: false,
itypes: ['swor'],
etypes: [],
}
}
it('1. exports NON_RANDOM_RANGE_CODES covering all elemental damage ranges, event procs, and charged skills', () => {
const expectedCodes = [
'dmg-fire',
'dmg-cold',
'dmg-ltng',
'dmg-mag',
'dmg-pois',
'dmg-norm',
'dmg-elem',
'hit-skill',
'att-skill',
'gethit-skill',
'kill-skill',
'death-skill',
'levelup-skill',
'charged',
'skill-rand',
]
for (const c of expectedCodes) {
expect(NON_RANDOM_RANGE_CODES.has(c), `NON_RANDOM_RANGE_CODES must include ${c}`).toBe(true)
}
})
it('2. preserves dmg-fire (min=1, max=6) across rollMagicAffixes / rollRareAffixes -> createDroppedItem -> formatItemTooltip as "Adds 1-6 Fire Damage"', () => {
const fieryPrefix = makeAffix('Fiery', true, 101, [{ code: 'dmg-fire', min: 1, max: 6 }])
const auxPrefix1 = makeAffix('Bronze', true, 102, [{ code: 'att', min: 15, max: 15 }])
const auxPrefix2 = makeAffix('Sharp', true, 103, [{ code: 'dmg%', min: 20, max: 20 }])
const auxSuffix1 = makeAffix('of Craftsmanship', false, 201, [{ code: 'dmg-max', min: 1, max: 1 }])
const auxSuffix2 = makeAffix('of Worth', false, 202, [{ code: 'dmg-min', min: 2, max: 2 }])
const auxSuffix3 = makeAffix('of Strength', false, 203, [{ code: 'str', min: 5, max: 5 }])
// Magic roll with only Fiery prefix
const magicAffixes = rollMagicAffixes(
swordBase,
30,
{
prefixes: { all: [fieryPrefix] } as unknown as AffixTable,
suffixes: { all: [] } as unknown as AffixTable,
},
mockIsA,
new D2Rng(12345),
)
// Find a seed that selects prefix (slotChoice 0 or 2)
let magicResult = magicAffixes
for (let s = 1; s <= 20; s++) {
const candidate = rollMagicAffixes(
swordBase,
30,
{
prefixes: { all: [fieryPrefix] } as unknown as AffixTable,
suffixes: { all: [] } as unknown as AffixTable,
},
mockIsA,
new D2Rng(s),
)
if (candidate.prefix) {
magicResult = candidate
break
}
}
expect(magicResult.prefix).toBeDefined()
expect(magicResult.prefix!.mods[0]).toEqual({
code: 'dmg-fire',
min: 1,
max: 6,
value: 1,
})
const magicItem = createDroppedItem(swordBase, 'magic', {
magicAffixes: magicResult,
ilvl: 30,
dwInitSeed: 777,
})
expect(magicItem.rolledProps).toContainEqual({
code: 'dmg-fire',
param: undefined,
min: 1,
max: 6,
value: 1,
})
const magicTooltip = formatItemTooltip(magicItem)
const magicLines = magicTooltip.lines.map(l => l.text)
expect(magicLines).toContain('Adds 1-6 Fire Damage')
// Rare roll where Fiery is guaranteed to be chosen (3 prefixes, 3 suffixes)
const rareResult = rollRareAffixes(
swordBase,
30,
{
prefixes: { all: [fieryPrefix] } as unknown as AffixTable,
suffixes: { all: [auxSuffix1, auxSuffix2] } as unknown as AffixTable,
},
mockRareNames,
mockIsA,
new D2Rng(98765),
)
const fieryRolled = rareResult.affixes.find(a => a.name === 'Fiery')
expect(fieryRolled).toBeDefined()
expect(fieryRolled!.mods[0]).toEqual({
code: 'dmg-fire',
min: 1,
max: 6,
value: 1,
})
const rareItem = createDroppedItem(swordBase, 'rare', {
rareAffixes: rareResult,
ilvl: 30,
dwInitSeed: 888,
})
expect(rareItem.rolledProps).toContainEqual({
code: 'dmg-fire',
param: undefined,
min: 1,
max: 6,
value: 1,
})
const rareTooltip = formatItemTooltip(rareItem)
const rareLines = rareTooltip.lines.map(l => l.text)
expect(rareLines).toContain('Adds 1-6 Fire Damage')
// Also verify when only rolledProps is present (without rolledMagicAffixes / rolledRareAffixes)
const rarePropsOnlyItem = { ...rareItem, rolledRareAffixes: undefined } as Item
expect(formatItemTooltip(rarePropsOnlyItem).lines.map(l => l.text)).toContain('Adds 1-6 Fire Damage')
})
it('3. preserves hit-skill (param="Amplify Damage"/66, min=5, max=1) and charged (param="Teleport"/54, min=20, max=2) across Magic and Rare generation and tooltip rendering', () => {
const ampSuffix = makeAffix('of Amplify Damage', false, 301, [
{ code: 'hit-skill', param: '66', min: 5, max: 1 },
])
const teleSuffix = makeAffix('of Teleportation', false, 302, [
{ code: 'charged', param: '54', min: 20, max: 2 },
])
const bronzePrefix = makeAffix('Bronze', true, 101, [{ code: 'att', min: 10, max: 10 }])
// Magic item with prefix=Bronze (or hit-skill) and suffix=charged
const ampPrefix = makeAffix('Cursing', true, 102, [
{ code: 'hit-skill', param: 'Amplify Damage', min: 5, max: 1 },
])
let magicResult = rollMagicAffixes(
swordBase,
40,
{
prefixes: { all: [ampPrefix] } as unknown as AffixTable,
suffixes: { all: [teleSuffix] } as unknown as AffixTable,
},
mockIsA,
new D2Rng(1),
)
for (let s = 1; s <= 30; s++) {
const candidate = rollMagicAffixes(
swordBase,
40,
{
prefixes: { all: [ampPrefix] } as unknown as AffixTable,
suffixes: { all: [teleSuffix] } as unknown as AffixTable,
},
mockIsA,
new D2Rng(s),
)
if (candidate.prefix && candidate.suffix) {
magicResult = candidate
break
}
}
expect(magicResult.prefix?.mods[0]).toEqual({
code: 'hit-skill',
param: 'Amplify Damage',
min: 5,
max: 1,
value: 5,
})
expect(magicResult.suffix?.mods[0]).toEqual({
code: 'charged',
param: '54',
min: 20,
max: 2,
value: 20,
})
const magicItem = createDroppedItem(swordBase, 'magic', {
magicAffixes: magicResult,
ilvl: 40,
dwInitSeed: 555,
})
const magicTooltip = formatItemTooltip(magicItem)
const magicLines = magicTooltip.lines.map(l => l.text)
expect(magicLines.some(l => /^5% Chance to cast Level 1 Amplify Damage on striking$/i.test(l))).toBe(true)
expect(magicLines).toContain('Level 2 Teleport (20/20 Charges)')
// Rare item with both suffixes (66 and 54) + prefix
const rareResult = rollRareAffixes(
swordBase,
40,
{
prefixes: { all: [bronzePrefix] } as unknown as AffixTable,
suffixes: { all: [ampSuffix, teleSuffix] } as unknown as AffixTable,
},
mockRareNames,
mockIsA,
new D2Rng(42),
)
const rareItem = createDroppedItem(swordBase, 'rare', {
rareAffixes: rareResult,
ilvl: 40,
dwInitSeed: 666,
})
const rareTooltip = formatItemTooltip(rareItem)
const rareLines = rareTooltip.lines.map(l => l.text)
expect(rareLines.some(l => /^5% Chance to cast Level 1 Amplify Damage on striking$/i.test(l))).toBe(true)
expect(rareLines).toContain('Level 2 Teleport (20/20 Charges)')
})
})