BlueGit

timfa questward

Files

questward / shared / combat / magic.js

magic.js

magic.js 3.0 KiB Download Raw
1import { 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
13export 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 tier.
24 * Succeeds on tie or higher (same rule as physical hit vs Agility).
25 */
26export 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
42function 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).
54 */
55export function resolveMagicAttack ({
56 magicSkillLevel,
57 spellDifficulty,
58 mindSkillLevel,
59 element = "none",
60 targetAffinities = {},
61 rng = Math.random
62})
63{
64 const cast = resolveSpellCast({ magicSkillLevel, spellDifficulty, rng });
65 if (!cast.success)
66 {
67 return {
68 outcome: "fizzle",
69 magicRoll: cast.magicRoll,
70 difficultyRoll: cast.difficultyRoll,
71 damage: 0,
72 multiplier: 1
73 };
74 }
75
76 const mindRoll = rollSkill(mindSkillLevel, rng);
77 const multiplier = elementalMultiplier(element, targetAffinities);
78 const damage = Math.max(1, Math.floor(mindRoll * multiplier));
79
80 return {
81 outcome: "hit",
82 magicRoll: cast.magicRoll,
83 difficultyRoll: cast.difficultyRoll,
84 mindRoll,
85 damage,
86 multiplier
87 };
88}
89
90/** Potency roll for non-damage spells (e.g. Light duration = mindRoll × 5 seconds). */
91export function resolveSpellPotency ({ mindSkillLevel, rng = Math.random })
92{
93 const mindRoll = rollSkill(mindSkillLevel, rng);
94 return { mindRoll };
95}
96
97/**
98 * Non-damage spells: Magic vs spell difficulty, then apply effectId.
99 */
100export function resolveMagicEffect ({
101 magicSkillLevel,
102 spellDifficulty,
103 effectId,
104 rng = Math.random
105})
106{
107 const cast = resolveSpellCast({ magicSkillLevel, spellDifficulty, rng });
108 if (!cast.success)
109 {
110 return {
111 outcome: "fizzle",
112 magicRoll: cast.magicRoll,
113 difficultyRoll: cast.difficultyRoll,
114 effectId: null
115 };
116 }
117 return {
118 outcome: "effect",
119 magicRoll: cast.magicRoll,
120 difficultyRoll: cast.difficultyRoll,
121 effectId: effectId ?? null
122 };
123}
124
125export function mobElementAffinities (mobDef)
126{
127 return mobDef?.elementAffinities ?? {};
128}