import { rollSpellDamage, rollSkill } from "./rolls.js"; import { damageTypeMultiplier, MagicElement } from "../definitions/attackTypes.js"; import { spellDifficultyFromTier } from "../spells/constants.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 {import("../definitions/attackTypes.js").MagicElement[keyof import("../definitions/attackTypes.js").MagicElement]|"holy"|"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 }; } /** * Exact P(Magic roll ≥ difficulty roll) as a percent (0–100). * Magic and difficulty dice are uniform integers in [0..level] / [0..difficulty]. * @param {number} magicSkillLevel * @param {number} spellTier */ export function spellCastSuccessChancePercent (magicSkillLevel, spellTier) { const magic = Math.max(0, Math.floor(Number(magicSkillLevel) || 0)); const difficulty = spellDifficultyFromTier(spellTier); let successes = 0; const outcomes = (magic + 1) * (difficulty + 1); for (let d = 0; d <= difficulty; d++) { if (d <= magic) { successes += magic - d + 1; } } return (successes / outcomes) * 100; } function normalizeSpellElement (element) { if (element === "holy") { return MagicElement.light; } return element ?? "none"; } function elementalMultiplier (element, targetAffinities = {}) { const key = normalizeSpellElement(element); if (key === "none") { return ElementMultiplier.neutral; } return damageTypeMultiplier(key, targetAffinities); } /** * Full magic attack resolution (shared rules). */ export function resolveMagicAttack ({ magicSkillLevel, spellDifficulty, mindSkillLevel, spellTier = 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 = rollSpellDamage(mindSkillLevel, spellTier, 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 ?? {}; } /** Physical + elemental affinities for damage multipliers. */ export function mobDamageAffinities (mobDef) { return { ...(mobDef?.attackAffinities ?? {}), ...(mobDef?.elementAffinities ?? {}) }; }