BlueGit

timfa questward

Files

questward / shared / combat / magic.js

magic.js

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