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.difficulty. * 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, not wired to server combat yet). * @param {object} params * @param {number} params.magicSkillLevel * @param {number} params.spellDifficulty * @param {number} params.mindSkillLevel * @param {number} [params.baseDamage] * @param {SpellElement} [params.element] * @param {Partial>} [params.targetAffinities] * @param {() => number} [params.rng] */ export function resolveMagicAttack ({ magicSkillLevel, spellDifficulty, mindSkillLevel, baseDamage = 1, 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 rawDamage = Math.max(1, (baseDamage | 0) + mindRoll - 1); const multiplier = elementalMultiplier(element, targetAffinities); const damage = Math.max(1, Math.floor(rawDamage * multiplier)); return { outcome: "hit", magicRoll: cast.magicRoll, difficultyRoll: cast.difficultyRoll, mindRoll, damage, multiplier }; } /** * 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 }; }