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