genshin-calc-core 0.5.12

Genshin Impact damage calculation engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
use serde::{Deserialize, Serialize};

use crate::types::Element;

/// Elemental reactions in Genshin Impact.
///
/// Reactions are grouped into four categories: amplifying, catalyze,
/// transformative, and lunar. Use [`Reaction::category`] to check
/// which category a reaction belongs to.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Reaction {
    /// Vaporize (Hydro + Pyro amplifying reaction).
    Vaporize,
    /// Melt (Pyro + Cryo amplifying reaction).
    Melt,
    /// Aggravate (Electro catalyze reaction).
    Aggravate,
    /// Spread (Dendro catalyze reaction).
    Spread,
    /// Overloaded (Pyro + Electro transformative reaction).
    Overloaded,
    /// Superconduct (Cryo + Electro transformative reaction).
    Superconduct,
    /// Electro-Charged (Hydro + Electro transformative reaction).
    ElectroCharged,
    /// Shattered (Cryo + physical heavy attack transformative reaction).
    Shattered,
    /// Swirl (Anemo + element transformative reaction, carries the swirled element).
    Swirl(Element),
    /// Bloom (Hydro + Dendro transformative reaction).
    Bloom,
    /// Hyperbloom (Electro + Bloom core transformative reaction).
    Hyperbloom,
    /// Burgeon (Pyro + Bloom core transformative reaction).
    Burgeon,
    /// Burning (Pyro + Dendro transformative reaction).
    Burning,
    /// Lunar Electro-Charged (Nod-Krai lunar reaction).
    LunarElectroCharged,
    /// Lunar Bloom (Nod-Krai lunar reaction).
    LunarBloom,
    /// Lunar Crystallize primary hit (Nod-Krai lunar reaction).
    LunarCrystallize,
    /// Lunar Crystallize secondary hit (Nod-Krai lunar reaction).
    LunarCrystallizeSecondary,
}

/// Reaction category classification.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ReactionCategory {
    /// Amplifying reactions (vaporize, melt) — multiply damage by a coefficient.
    Amplifying,
    /// Catalyze reactions (aggravate, spread) — add flat damage based on EM.
    Catalyze,
    /// Transformative reactions — deal separate instance of elemental damage.
    Transformative,
    /// Lunar reactions — Nod-Krai exclusive crittable reaction damage.
    Lunar,
}

impl Reaction {
    /// Returns the category of this reaction.
    pub fn category(&self) -> ReactionCategory {
        match self {
            Reaction::Vaporize | Reaction::Melt => ReactionCategory::Amplifying,
            Reaction::Aggravate | Reaction::Spread => ReactionCategory::Catalyze,
            Reaction::Overloaded
            | Reaction::Superconduct
            | Reaction::ElectroCharged
            | Reaction::Shattered
            | Reaction::Swirl(_)
            | Reaction::Bloom
            | Reaction::Hyperbloom
            | Reaction::Burgeon
            | Reaction::Burning => ReactionCategory::Transformative,
            Reaction::LunarElectroCharged
            | Reaction::LunarBloom
            | Reaction::LunarCrystallize
            | Reaction::LunarCrystallizeSecondary => ReactionCategory::Lunar,
        }
    }
}

/// Determines the elemental reaction from a trigger and aura element.
///
/// Returns `None` if no reaction occurs (e.g. same element,
/// or Geo/Anemo as aura).
pub fn determine_reaction(trigger: Element, aura: Element) -> Option<Reaction> {
    match (trigger, aura) {
        // Vaporize
        (Element::Pyro, Element::Hydro) => Some(Reaction::Vaporize),
        (Element::Hydro, Element::Pyro) => Some(Reaction::Vaporize),
        // Melt
        (Element::Pyro, Element::Cryo) => Some(Reaction::Melt),
        (Element::Cryo, Element::Pyro) => Some(Reaction::Melt),
        // Overloaded
        (Element::Pyro, Element::Electro) | (Element::Electro, Element::Pyro) => {
            Some(Reaction::Overloaded)
        }
        // Superconduct
        (Element::Cryo, Element::Electro) | (Element::Electro, Element::Cryo) => {
            Some(Reaction::Superconduct)
        }
        // Electro-Charged
        (Element::Hydro, Element::Electro) | (Element::Electro, Element::Hydro) => {
            Some(Reaction::ElectroCharged)
        }
        // Swirl
        (
            Element::Anemo,
            aura @ (Element::Pyro | Element::Hydro | Element::Electro | Element::Cryo),
        ) => Some(Reaction::Swirl(aura)),
        // Bloom
        (Element::Hydro, Element::Dendro) | (Element::Dendro, Element::Hydro) => {
            Some(Reaction::Bloom)
        }
        // Burning
        (Element::Pyro, Element::Dendro) | (Element::Dendro, Element::Pyro) => {
            Some(Reaction::Burning)
        }
        // Shattered is triggered by heavy attacks on frozen enemies, not by element combination
        // Hyperbloom/Burgeon are triggered on Bloom cores, not by direct element combination
        // Catalyze (Aggravate/Spread) requires Quicken state, not representable here
        // Lunar reactions have special triggers not representable here
        _ => None,
    }
}

/// Returns the amplifying reaction multiplier.
///
/// Vaporize (Hydro→Pyro) = 2.0, Vaporize (Pyro→Hydro) = 1.5,
/// Melt (Pyro→Cryo) = 2.0, Melt (Cryo→Pyro) = 1.5.
pub fn amplifying_multiplier(trigger: Element, aura: Element) -> Option<f64> {
    match (trigger, aura) {
        (Element::Pyro, Element::Hydro) => Some(1.5), // Reverse Vaporize
        (Element::Hydro, Element::Pyro) => Some(2.0), // Forward Vaporize
        (Element::Pyro, Element::Cryo) => Some(2.0),  // Forward Melt
        (Element::Cryo, Element::Pyro) => Some(1.5),  // Reverse Melt
        _ => None,
    }
}

/// Returns the catalyze flat damage coefficient.
///
/// Aggravate = 1.15, Spread = 1.25.
pub fn catalyze_coefficient(reaction: Reaction) -> Option<f64> {
    match reaction {
        Reaction::Aggravate => Some(1.15),
        Reaction::Spread => Some(1.25),
        _ => None,
    }
}

/// Returns the transformative reaction damage multiplier.
pub fn transformative_multiplier(reaction: Reaction) -> Option<f64> {
    match reaction {
        Reaction::Overloaded => Some(2.75),
        Reaction::Superconduct => Some(1.5),
        Reaction::ElectroCharged => Some(2.0),
        Reaction::Shattered => Some(3.0),
        Reaction::Swirl(_) => Some(0.6),
        Reaction::Bloom => Some(2.0),
        Reaction::Hyperbloom => Some(3.0),
        Reaction::Burgeon => Some(3.0),
        Reaction::Burning => Some(0.25),
        _ => None,
    }
}

/// Returns the lunar reaction damage multiplier.
pub fn lunar_multiplier(reaction: Reaction) -> Option<f64> {
    match reaction {
        Reaction::LunarElectroCharged => Some(1.8),
        Reaction::LunarBloom => Some(1.0),
        Reaction::LunarCrystallize => Some(0.96),
        Reaction::LunarCrystallizeSecondary => Some(1.6),
        _ => None,
    }
}

/// Returns the damage element of a transformative reaction.
///
/// Returns `None` if the reaction is not transformative.
/// Returns `Some(None)` for physical damage (e.g. shattered).
pub fn transformative_element(reaction: Reaction) -> Option<Option<Element>> {
    match reaction {
        Reaction::Overloaded => Some(Some(Element::Pyro)),
        Reaction::Superconduct => Some(Some(Element::Cryo)),
        Reaction::ElectroCharged => Some(Some(Element::Electro)),
        Reaction::Shattered => Some(None), // Physical
        Reaction::Swirl(elem) => Some(Some(elem)),
        Reaction::Bloom | Reaction::Hyperbloom | Reaction::Burgeon => Some(Some(Element::Dendro)),
        Reaction::Burning => Some(Some(Element::Pyro)),
        _ => None,
    }
}

/// Returns the damage element of a lunar reaction.
pub fn lunar_element(reaction: Reaction) -> Option<Option<Element>> {
    match reaction {
        Reaction::LunarElectroCharged => Some(Some(Element::Electro)),
        Reaction::LunarBloom => Some(Some(Element::Dendro)),
        Reaction::LunarCrystallize | Reaction::LunarCrystallizeSecondary => {
            Some(Some(Element::Geo))
        }
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_category_amplifying() {
        assert_eq!(Reaction::Vaporize.category(), ReactionCategory::Amplifying);
        assert_eq!(Reaction::Melt.category(), ReactionCategory::Amplifying);
    }

    #[test]
    fn test_category_catalyze() {
        assert_eq!(Reaction::Aggravate.category(), ReactionCategory::Catalyze);
        assert_eq!(Reaction::Spread.category(), ReactionCategory::Catalyze);
    }

    #[test]
    fn test_category_transformative() {
        assert_eq!(
            Reaction::Overloaded.category(),
            ReactionCategory::Transformative
        );
        assert_eq!(
            Reaction::Swirl(Element::Pyro).category(),
            ReactionCategory::Transformative
        );
    }

    #[test]
    fn test_category_lunar() {
        assert_eq!(
            Reaction::LunarElectroCharged.category(),
            ReactionCategory::Lunar
        );
        assert_eq!(Reaction::LunarBloom.category(), ReactionCategory::Lunar);
    }

    #[test]
    fn test_determine_vaporize() {
        assert_eq!(
            determine_reaction(Element::Pyro, Element::Hydro),
            Some(Reaction::Vaporize)
        );
        assert_eq!(
            determine_reaction(Element::Hydro, Element::Pyro),
            Some(Reaction::Vaporize)
        );
    }

    #[test]
    fn test_determine_melt() {
        assert_eq!(
            determine_reaction(Element::Pyro, Element::Cryo),
            Some(Reaction::Melt)
        );
        assert_eq!(
            determine_reaction(Element::Cryo, Element::Pyro),
            Some(Reaction::Melt)
        );
    }

    #[test]
    fn test_determine_overloaded() {
        assert_eq!(
            determine_reaction(Element::Pyro, Element::Electro),
            Some(Reaction::Overloaded)
        );
        assert_eq!(
            determine_reaction(Element::Electro, Element::Pyro),
            Some(Reaction::Overloaded)
        );
    }

    #[test]
    fn test_determine_superconduct() {
        assert_eq!(
            determine_reaction(Element::Cryo, Element::Electro),
            Some(Reaction::Superconduct)
        );
    }

    #[test]
    fn test_determine_electro_charged() {
        assert_eq!(
            determine_reaction(Element::Hydro, Element::Electro),
            Some(Reaction::ElectroCharged)
        );
    }

    #[test]
    fn test_determine_swirl() {
        assert_eq!(
            determine_reaction(Element::Anemo, Element::Pyro),
            Some(Reaction::Swirl(Element::Pyro))
        );
        assert_eq!(
            determine_reaction(Element::Anemo, Element::Hydro),
            Some(Reaction::Swirl(Element::Hydro))
        );
        assert_eq!(
            determine_reaction(Element::Anemo, Element::Electro),
            Some(Reaction::Swirl(Element::Electro))
        );
        assert_eq!(
            determine_reaction(Element::Anemo, Element::Cryo),
            Some(Reaction::Swirl(Element::Cryo))
        );
    }

    #[test]
    fn test_determine_swirl_invalid_elements() {
        assert_eq!(determine_reaction(Element::Anemo, Element::Dendro), None);
        assert_eq!(determine_reaction(Element::Anemo, Element::Geo), None);
        assert_eq!(determine_reaction(Element::Anemo, Element::Anemo), None);
    }

    #[test]
    fn test_determine_bloom() {
        assert_eq!(
            determine_reaction(Element::Hydro, Element::Dendro),
            Some(Reaction::Bloom)
        );
        assert_eq!(
            determine_reaction(Element::Dendro, Element::Hydro),
            Some(Reaction::Bloom)
        );
    }

    #[test]
    fn test_determine_burning() {
        assert_eq!(
            determine_reaction(Element::Pyro, Element::Dendro),
            Some(Reaction::Burning)
        );
    }

    #[test]
    fn test_determine_no_reaction() {
        assert_eq!(determine_reaction(Element::Geo, Element::Geo), None);
        assert_eq!(determine_reaction(Element::Hydro, Element::Hydro), None);
    }

    #[test]
    fn test_amplifying_multiplier_vaporize() {
        assert_eq!(
            amplifying_multiplier(Element::Pyro, Element::Hydro),
            Some(1.5)
        );
        assert_eq!(
            amplifying_multiplier(Element::Hydro, Element::Pyro),
            Some(2.0)
        );
    }

    #[test]
    fn test_amplifying_multiplier_melt() {
        assert_eq!(
            amplifying_multiplier(Element::Pyro, Element::Cryo),
            Some(2.0)
        );
        assert_eq!(
            amplifying_multiplier(Element::Cryo, Element::Pyro),
            Some(1.5)
        );
    }

    #[test]
    fn test_amplifying_multiplier_non_amplifying() {
        assert_eq!(amplifying_multiplier(Element::Pyro, Element::Electro), None);
    }

    #[test]
    fn test_catalyze_coefficient_values() {
        assert_eq!(catalyze_coefficient(Reaction::Aggravate), Some(1.15));
        assert_eq!(catalyze_coefficient(Reaction::Spread), Some(1.25));
        assert_eq!(catalyze_coefficient(Reaction::Vaporize), None);
    }

    #[test]
    fn test_transformative_multiplier_values() {
        assert_eq!(transformative_multiplier(Reaction::Overloaded), Some(2.75));
        assert_eq!(transformative_multiplier(Reaction::Superconduct), Some(1.5));
        assert_eq!(
            transformative_multiplier(Reaction::ElectroCharged),
            Some(2.0)
        );
        assert_eq!(transformative_multiplier(Reaction::Shattered), Some(3.0));
        assert_eq!(
            transformative_multiplier(Reaction::Swirl(Element::Pyro)),
            Some(0.6)
        );
        assert_eq!(transformative_multiplier(Reaction::Bloom), Some(2.0));
        assert_eq!(transformative_multiplier(Reaction::Hyperbloom), Some(3.0));
        assert_eq!(transformative_multiplier(Reaction::Burgeon), Some(3.0));
        assert_eq!(transformative_multiplier(Reaction::Burning), Some(0.25));
    }

    #[test]
    fn test_lunar_multiplier_values() {
        assert_eq!(lunar_multiplier(Reaction::LunarElectroCharged), Some(1.8));
        assert_eq!(lunar_multiplier(Reaction::LunarBloom), Some(1.0));
        assert_eq!(lunar_multiplier(Reaction::LunarCrystallize), Some(0.96));
        assert_eq!(
            lunar_multiplier(Reaction::LunarCrystallizeSecondary),
            Some(1.6)
        );
    }

    #[test]
    fn test_transformative_element_values() {
        assert_eq!(
            transformative_element(Reaction::Overloaded),
            Some(Some(Element::Pyro))
        );
        assert_eq!(
            transformative_element(Reaction::Superconduct),
            Some(Some(Element::Cryo))
        );
        assert_eq!(transformative_element(Reaction::Shattered), Some(None));
        assert_eq!(
            transformative_element(Reaction::Swirl(Element::Hydro)),
            Some(Some(Element::Hydro))
        );
        assert_eq!(
            transformative_element(Reaction::Bloom),
            Some(Some(Element::Dendro))
        );
        assert_eq!(transformative_element(Reaction::Vaporize), None);
    }
}