| 1 | import { rollSkill } from "./rolls.js"; |
| 2 | |
| 3 | /** |
| 4 | * Magic attack resolution — cast step is implemented; full combat wiring is later. |
| 5 | * |
| 6 | * Flow: |
| 7 | * 1. Roll Magic vs per-spell difficulty (contest) — fizzle if magicRoll < difficultyRoll |
| 8 | * 2. On success with damage: roll Mind for spell damage |
| 9 | * 3. Compare spell element to target weaknesses/resistances (×2 / ×0.5 / ×1) |
| 10 | * 4. Apply damage, or apply a non-damage effect when the spell defines one |
| 11 | */ |
| 12 | |
| 13 | export const ElementMultiplier = Object.freeze({ |
| 14 | weak: 2, |
| 15 | neutral: 1, |
| 16 | resistant: 0.5 |
| 17 | }); |
| 18 | |
| 19 | /** @typedef {"fire"|"water"|"earth"|"air"|"holy"|"dark"|"none"} SpellElement */ |
| 20 | |
| 21 | /** |
| 22 | * Whether the caster successfully channels the spell. |
| 23 | * Magic roll vs a difficulty roll sized by spell.difficulty. |
| 24 | * Succeeds on tie or higher (same rule as physical hit vs agility). |
| 25 | */ |
| 26 | export function resolveSpellCast ({ |
| 27 | magicSkillLevel, |
| 28 | spellDifficulty, |
| 29 | rng = Math.random |
| 30 | }) |
| 31 | { |
| 32 | const magicRoll = rollSkill(magicSkillLevel, rng); |
| 33 | const difficultyRoll = rollSkill(spellDifficulty, rng); |
| 34 | const success = magicRoll >= difficultyRoll; |
| 35 | return { |
| 36 | success, |
| 37 | magicRoll, |
| 38 | difficultyRoll |
| 39 | }; |
| 40 | } |
| 41 | |
| 42 | function elementalMultiplier (element, targetAffinities = {}) |
| 43 | { |
| 44 | const affinity = targetAffinities[element]; |
| 45 | if (affinity === "weak") |
| 46 | { return ElementMultiplier.weak; } |
| 47 | if (affinity === "resistant") |
| 48 | { return ElementMultiplier.resistant; } |
| 49 | return ElementMultiplier.neutral; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Full magic attack resolution (shared rules, not wired to server combat yet). |
| 54 | * @param {object} params |
| 55 | * @param {number} params.magicSkillLevel |
| 56 | * @param {number} params.spellDifficulty |
| 57 | * @param {number} params.mindSkillLevel |
| 58 | * @param {number} [params.baseDamage] |
| 59 | * @param {SpellElement} [params.element] |
| 60 | * @param {Partial<Record<SpellElement, "weak"|"resistant"|"neutral">>} [params.targetAffinities] |
| 61 | * @param {() => number} [params.rng] |
| 62 | */ |
| 63 | export function resolveMagicAttack ({ |
| 64 | magicSkillLevel, |
| 65 | spellDifficulty, |
| 66 | mindSkillLevel, |
| 67 | baseDamage = 1, |
| 68 | element = "none", |
| 69 | targetAffinities = {}, |
| 70 | rng = Math.random |
| 71 | }) |
| 72 | { |
| 73 | const cast = resolveSpellCast({ magicSkillLevel, spellDifficulty, rng }); |
| 74 | if (!cast.success) |
| 75 | { |
| 76 | return { |
| 77 | outcome: "fizzle", |
| 78 | magicRoll: cast.magicRoll, |
| 79 | difficultyRoll: cast.difficultyRoll, |
| 80 | damage: 0, |
| 81 | multiplier: 1 |
| 82 | }; |
| 83 | } |
| 84 | |
| 85 | const mindRoll = rollSkill(mindSkillLevel, rng); |
| 86 | const rawDamage = Math.max(1, (baseDamage | 0) + mindRoll - 1); |
| 87 | const multiplier = elementalMultiplier(element, targetAffinities); |
| 88 | const damage = Math.max(1, Math.floor(rawDamage * multiplier)); |
| 89 | |
| 90 | return { |
| 91 | outcome: "hit", |
| 92 | magicRoll: cast.magicRoll, |
| 93 | difficultyRoll: cast.difficultyRoll, |
| 94 | mindRoll, |
| 95 | damage, |
| 96 | multiplier |
| 97 | }; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Non-damage spells: Magic vs spell difficulty, then apply effectId. |
| 102 | */ |
| 103 | export function resolveMagicEffect ({ |
| 104 | magicSkillLevel, |
| 105 | spellDifficulty, |
| 106 | effectId, |
| 107 | rng = Math.random |
| 108 | }) |
| 109 | { |
| 110 | const cast = resolveSpellCast({ magicSkillLevel, spellDifficulty, rng }); |
| 111 | if (!cast.success) |
| 112 | { |
| 113 | return { |
| 114 | outcome: "fizzle", |
| 115 | magicRoll: cast.magicRoll, |
| 116 | difficultyRoll: cast.difficultyRoll, |
| 117 | effectId: null |
| 118 | }; |
| 119 | } |
| 120 | return { |
| 121 | outcome: "effect", |
| 122 | magicRoll: cast.magicRoll, |
| 123 | difficultyRoll: cast.difficultyRoll, |
| 124 | effectId: effectId ?? null |
| 125 | }; |
| 126 | } |