import { rollSkill } from "./rolls.js"; /** * Magic attack resolution — cast step is implemented; full combat wiring is later. * * Flow: * 1. Roll Magic vs per-spell difficulty (contest) — fizzle if magicRoll < difficultyRoll * 2. On success with damage: roll Mind for spell damage * 3. Compare spell element to target weaknesses/resistances (×2 / ×0.5 / ×1) * 4. Apply damage, or apply a non-damage effect when the spell defines one */ export const ElementMultiplier = Object.freeze({ weak: 2, neutral: 1, resistant: 0.5 }); /** @typedef {"fire"|"water"|"earth"|"air"|"holy"|"dark"|"none"} SpellElement */ /** * Whether the caster successfully channels the spell. * Magic roll vs a difficulty roll sized by spell tier. * Succeeds on tie or higher (same rule as physical hit vs Agility). */ export function resolveSpellCast ({ magicSkillLevel, spellDifficulty, rng = Math.random }) { const magicRoll = rollSkill(magicSkillLevel, rng); const difficultyRoll = rollSkill(spellDifficulty, rng); const success = magicRoll >= difficultyRoll; return { success, magicRoll, difficultyRoll }; } function elementalMultiplier (element, targetAffinities = {}) { const affinity = targetAffinities[element]; if (affinity === "weak") { return ElementMultiplier.weak; } if (affinity === "resistant") { return ElementMultiplier.resistant; } return ElementMultiplier.neutral; } /** * Full magic attack resolution (shared rules). */ export function resolveMagicAttack ({ magicSkillLevel, spellDifficulty, mindSkillLevel, element = "none", targetAffinities = {}, rng = Math.random }) { const cast = resolveSpellCast({ magicSkillLevel, spellDifficulty, rng }); if (!cast.success) { return { outcome: "fizzle", magicRoll: cast.magicRoll, difficultyRoll: cast.difficultyRoll, damage: 0, multiplier: 1 }; } const mindRoll = rollSkill(mindSkillLevel, rng); const multiplier = elementalMultiplier(element, targetAffinities); const damage = Math.max(1, Math.floor(mindRoll * multiplier)); return { outcome: "hit", magicRoll: cast.magicRoll, difficultyRoll: cast.difficultyRoll, mindRoll, damage, multiplier }; } /** Potency roll for non-damage spells (e.g. Light duration = mindRoll × 5 seconds). */ export function resolveSpellPotency ({ mindSkillLevel, rng = Math.random }) { const mindRoll = rollSkill(mindSkillLevel, rng); return { mindRoll }; } /** * Non-damage spells: Magic vs spell difficulty, then apply effectId. */ export function resolveMagicEffect ({ magicSkillLevel, spellDifficulty, effectId, rng = Math.random }) { const cast = resolveSpellCast({ magicSkillLevel, spellDifficulty, rng }); if (!cast.success) { return { outcome: "fizzle", magicRoll: cast.magicRoll, difficultyRoll: cast.difficultyRoll, effectId: null }; } return { outcome: "effect", magicRoll: cast.magicRoll, difficultyRoll: cast.difficultyRoll, effectId: effectId ?? null }; } export function mobElementAffinities (mobDef) { return mobDef?.elementAffinities ?? {}; }