1use crate::abilities::Abilities;
2use crate::choices::{Choices, MOVES};
3use crate::state::{PokemonIndex, Side, Terrain};
4use crate::{
5 choices::{Choice, MoveCategory},
6 state::{
7 Pokemon, PokemonBoostableStat, PokemonStatus, PokemonType, PokemonVolatileStatus,
8 SideReference, State, Weather,
9 },
10};
11
12#[rustfmt::skip]
13#[cfg(any(feature = "gen9",feature = "gen8",feature = "gen7",feature = "gen6"))]
14const TYPE_MATCHUP_DAMAGE_MULTIPICATION: [[f32; 19]; 19] = [
15[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.5, 0.0, 1.0, 1.0, 0.5, 1.0, 1.0],
17[1.0, 0.5, 0.5, 1.0, 2.0, 2.0, 1.0, 1.0, 1.0, 1.0, 1.0, 2.0, 0.5, 1.0, 0.5, 1.0, 2.0, 1.0, 1.0],
18[1.0, 2.0, 0.5, 1.0, 0.5, 1.0, 1.0, 1.0, 2.0, 1.0, 1.0, 1.0, 2.0, 1.0, 0.5, 1.0, 1.0, 1.0, 1.0],
19[1.0, 1.0, 2.0, 0.5, 0.5, 1.0, 1.0, 1.0, 0.0, 2.0, 1.0, 1.0, 1.0, 1.0, 0.5, 1.0, 1.0, 1.0, 1.0],
20[1.0, 0.5, 2.0, 1.0, 0.5, 1.0, 1.0, 0.5, 2.0, 0.5, 1.0, 0.5, 2.0, 1.0, 0.5, 1.0, 0.5, 1.0, 1.0],
21[1.0, 0.5, 0.5, 1.0, 2.0, 0.5, 1.0, 1.0, 2.0, 2.0, 1.0, 1.0, 1.0, 1.0, 2.0, 1.0, 0.5, 1.0, 1.0],
22[2.0, 1.0, 1.0, 1.0, 1.0, 2.0, 1.0, 0.5, 1.0, 0.5, 0.5, 0.5, 2.0, 0.0, 1.0, 2.0, 2.0, 0.5, 1.0],
23[1.0, 1.0, 1.0, 1.0, 2.0, 1.0, 1.0, 0.5, 0.5, 1.0, 1.0, 1.0, 0.5, 0.5, 1.0, 1.0, 0.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MULTIPLIER: f32 = 2.0;
64
65#[cfg(any(feature = "gen6", feature = "gen7", feature = "gen8", feature = "gen9"))]
66pub const CRIT_MULTIPLIER: f32 = 1.5;
67
68#[allow(dead_code)]
69pub enum DamageRolls {
70 Average,
71 Min,
72 Max,
73}
74
75fn type_enum_to_type_matchup_int(type_enum: &PokemonType) -> usize {
76 match type_enum {
77 PokemonType::NORMAL => 0,
78 PokemonType::FIRE => 1,
79 PokemonType::WATER => 2,
80 PokemonType::ELECTRIC => 3,
81 PokemonType::GRASS => 4,
82 PokemonType::ICE => 5,
83 PokemonType::FIGHTING => 6,
84 PokemonType::POISON => 7,
85 PokemonType::GROUND => 8,
86 PokemonType::FLYING => 9,
87 PokemonType::PSYCHIC => 10,
88 PokemonType::BUG => 11,
89 PokemonType::ROCK => 12,
90 PokemonType::GHOST => 13,
91 PokemonType::DRAGON => 14,
92 PokemonType::DARK => 15,
93 PokemonType::STEEL => 16,
94 PokemonType::FAIRY => 17,
95 PokemonType::TYPELESS => 18,
96 PokemonType::STELLAR => 18, }
98}
99
100pub fn type_effectiveness_modifier(attacking_type: &PokemonType, defender: &Pokemon) -> f32 {
101 #[cfg(not(feature = "terastallization"))]
102 let defending_types = defender.types;
103 #[cfg(feature = "terastallization")]
104 let defending_types = if defender.terastallized {
105 (defender.tera_type, PokemonType::TYPELESS)
106 } else {
107 defender.types
108 };
109 _type_effectiveness_modifier(attacking_type, &defending_types)
110}
111
112fn _type_effectiveness_modifier(
113 attacking_type: &PokemonType,
114 defending_types: &(PokemonType, PokemonType),
115) -> f32 {
116 let mut modifier = 1.0;
117 let attacking_type_index = type_enum_to_type_matchup_int(attacking_type);
118 modifier = modifier
119 * TYPE_MATCHUP_DAMAGE_MULTIPICATION[attacking_type_index]
120 [type_enum_to_type_matchup_int(&defending_types.0)];
121 modifier = modifier
122 * TYPE_MATCHUP_DAMAGE_MULTIPICATION[attacking_type_index]
123 [type_enum_to_type_matchup_int(&defending_types.1)];
124 modifier
125}
126
127fn weather_modifier(attacking_move_type: &PokemonType, weather: &Weather) -> f32 {
128 match weather {
129 Weather::SUN => match attacking_move_type {
130 PokemonType::FIRE => 1.5,
131 PokemonType::WATER => 0.5,
132 _ => 1.0,
133 },
134 Weather::RAIN => match attacking_move_type {
135 PokemonType::WATER => 1.5,
136 PokemonType::FIRE => 0.5,
137 _ => 1.0,
138 },
139 Weather::HARSHSUN => match attacking_move_type {
140 PokemonType::FIRE => 1.5,
141 PokemonType::WATER => 0.0,
142 _ => 1.0,
143 },
144 Weather::HEAVYRAIN => match attacking_move_type {
145 PokemonType::WATER => 1.5,
146 PokemonType::FIRE => 0.0,
147 _ => 1.0,
148 },
149 _ => 1.0,
150 }
151}
152
153fn stab_modifier(attacking_move_type: &PokemonType, active_pkmn: &Pokemon) -> f32 {
154 if attacking_move_type == &PokemonType::TYPELESS {
155 return 1.0;
156 }
157
158 let active_types = active_pkmn.types;
159 let move_has_basic_stab =
160 attacking_move_type == &active_types.0 || attacking_move_type == &active_types.1;
161 if active_pkmn.terastallized {
162 if &active_pkmn.tera_type == attacking_move_type && move_has_basic_stab {
163 return 2.0;
164 } else if &active_pkmn.tera_type == attacking_move_type || move_has_basic_stab {
165 return 1.5;
166 }
167 } else if move_has_basic_stab {
168 return 1.5;
169 }
170 1.0
171}
172
173fn burn_modifier(
174 attacking_move_category: &MoveCategory,
175 attacking_pokemon_status: &PokemonStatus,
176) -> f32 {
177 if attacking_pokemon_status == &PokemonStatus::BURN
178 && attacking_move_category == &MoveCategory::Physical
179 {
180 return 0.5;
181 }
182
183 1.0
184}
185
186fn terrain_modifier(
187 terrain: &Terrain,
188 attacker: &Pokemon,
189 defender: &Pokemon,
190 choice: &Choice,
191) -> f32 {
192 #[cfg(any(feature = "gen9", feature = "gen8"))]
193 let terrain_boost = 1.3;
194
195 #[cfg(not(any(feature = "gen9", feature = "gen8")))]
196 let terrain_boost = 1.5;
197
198 match terrain {
199 Terrain::ELECTRICTERRAIN => {
200 if choice.move_type == PokemonType::ELECTRIC && attacker.is_grounded() {
201 terrain_boost
202 } else {
203 1.0
204 }
205 }
206 Terrain::GRASSYTERRAIN => {
207 if choice.move_type == PokemonType::GRASS && attacker.is_grounded() {
208 terrain_boost
209 } else if choice.move_id == Choices::EARTHQUAKE {
210 0.5
211 } else {
212 1.0
213 }
214 }
215 Terrain::MISTYTERRAIN => {
216 if choice.move_type == PokemonType::DRAGON && defender.is_grounded() {
217 0.5
218 } else {
219 1.0
220 }
221 }
222 Terrain::PSYCHICTERRAIN => {
223 if choice.move_type == PokemonType::PSYCHIC && attacker.is_grounded() {
224 terrain_boost
225 } else {
226 1.0
227 }
228 }
229 Terrain::NONE => 1.0,
230 }
231}
232
233fn volatile_status_modifier(choice: &Choice, attacking_side: &Side, defending_side: &Side) -> f32 {
234 let mut modifier = 1.0;
235 for vs in attacking_side.volatile_statuses.iter() {
236 match vs {
237 PokemonVolatileStatus::FLASHFIRE if choice.move_type == PokemonType::FIRE => {
238 modifier *= 1.5;
239 }
240 PokemonVolatileStatus::SLOWSTART if choice.category == MoveCategory::Physical => {
241 modifier *= 0.5;
242 }
243 PokemonVolatileStatus::CHARGE if choice.move_type == PokemonType::ELECTRIC => {
244 modifier *= 2.0;
245 }
246 PokemonVolatileStatus::PROTOSYNTHESISATK | PokemonVolatileStatus::QUARKDRIVEATK
247 if choice.category == MoveCategory::Physical =>
248 {
249 modifier *= 1.3;
250 }
251 PokemonVolatileStatus::PROTOSYNTHESISSPA | PokemonVolatileStatus::QUARKDRIVESPA
252 if choice.category == MoveCategory::Special =>
253 {
254 modifier *= 1.3;
255 }
256 _ => {}
257 }
258 }
259
260 for vs in defending_side.volatile_statuses.iter() {
261 match vs {
262 PokemonVolatileStatus::MAGNETRISE
263 if choice.move_type == PokemonType::GROUND
264 && choice.move_id != Choices::THOUSANDARROWS =>
265 {
266 return 0.0;
267 }
268 PokemonVolatileStatus::TARSHOT if choice.move_type == PokemonType::FIRE => {
269 modifier *= 2.0;
270 }
271 PokemonVolatileStatus::PHANTOMFORCE
272 | PokemonVolatileStatus::SHADOWFORCE
273 | PokemonVolatileStatus::BOUNCE
274 | PokemonVolatileStatus::DIG
275 | PokemonVolatileStatus::DIVE
276 | PokemonVolatileStatus::FLY => {
277 return 0.0;
278 }
279 PokemonVolatileStatus::GLAIVERUSH => {
280 modifier *= 2.0;
281 }
282 PokemonVolatileStatus::PROTOSYNTHESISDEF | PokemonVolatileStatus::QUARKDRIVEDEF
283 if choice.category == MoveCategory::Physical =>
284 {
285 modifier /= 1.3;
286 }
287 PokemonVolatileStatus::PROTOSYNTHESISSPD | PokemonVolatileStatus::QUARKDRIVESPD
288 if choice.category == MoveCategory::Special =>
289 {
290 modifier /= 1.3;
291 }
292 _ => {}
293 }
294 }
295
296 modifier
297}
298
299fn get_defending_types(
300 side: &Side,
301 defending_pkmn: &Pokemon,
302 attacking_pkmn: &Pokemon,
303 attacking_choice: &Choice,
304) -> (PokemonType, PokemonType) {
305 if defending_pkmn.terastallized && !(defending_pkmn.tera_type == PokemonType::STELLAR) {
306 return (defending_pkmn.tera_type, PokemonType::TYPELESS);
307 }
308 let mut defender_types = defending_pkmn.types;
309 if side
310 .volatile_statuses
311 .contains(&PokemonVolatileStatus::ROOST)
312 {
313 if defender_types.0 == PokemonType::FLYING {
314 defender_types = (PokemonType::TYPELESS, defender_types.1);
315 }
316 if defender_types.1 == PokemonType::FLYING {
317 defender_types = (defender_types.0, PokemonType::TYPELESS);
318 }
319 }
320 if (attacking_pkmn.ability == Abilities::SCRAPPY
321 || attacking_pkmn.ability == Abilities::MINDSEYE)
322 && (attacking_choice.move_type == PokemonType::NORMAL
323 || attacking_choice.move_type == PokemonType::FIGHTING)
324 {
325 if defender_types.0 == PokemonType::GHOST {
326 defender_types = (PokemonType::TYPELESS, defender_types.1);
327 }
328 if defender_types.1 == PokemonType::GHOST {
329 defender_types = (defender_types.0, PokemonType::TYPELESS);
330 }
331 }
332 defender_types
333}
334
335fn get_attacking_and_defending_stats(
336 attacker: &Pokemon,
337 defender: &Pokemon,
338 attacking_side: &Side,
339 defending_side: &Side,
340 state: &State,
341 choice: &Choice,
342) -> (i16, i16, i16, i16) {
343 let mut should_calc_attacker_boost = true;
344 let mut should_calc_defender_boost = true;
345 let defending_stat;
346 let (
347 attacking_final_stat,
348 mut defending_final_stat,
349 mut crit_attacking_stat,
350 mut crit_defending_stat,
351 );
352
353 match choice.category {
354 MoveCategory::Physical => {
355 if attacking_side.attack_boost > 0 {
356 crit_attacking_stat =
357 attacking_side.calculate_boosted_stat(PokemonBoostableStat::Attack);
358 } else {
359 crit_attacking_stat = attacker.attack;
360 }
361 if defending_side.defense_boost <= 0 {
362 crit_defending_stat =
363 defending_side.calculate_boosted_stat(PokemonBoostableStat::Defense);
364 } else {
365 crit_defending_stat = defender.defense;
366 }
367
368 if defender.ability == Abilities::UNAWARE {
370 should_calc_attacker_boost = false;
371 }
372 if attacker.ability == Abilities::UNAWARE {
373 should_calc_defender_boost = false;
374 }
375
376 if choice.move_id == Choices::FOULPLAY {
380 if should_calc_attacker_boost {
381 attacking_final_stat =
382 defending_side.calculate_boosted_stat(PokemonBoostableStat::Attack);
383 } else {
384 attacking_final_stat = defender.attack;
385 }
386 crit_attacking_stat = defending_side.get_active_immutable().attack;
387 } else if choice.move_id == Choices::BODYPRESS {
388 if should_calc_attacker_boost {
389 attacking_final_stat =
390 attacking_side.calculate_boosted_stat(PokemonBoostableStat::Defense);
391 } else {
392 attacking_final_stat = attacker.defense;
393 }
394 crit_attacking_stat = attacking_side.get_active_immutable().defense;
395 } else if should_calc_attacker_boost {
396 attacking_final_stat =
397 attacking_side.calculate_boosted_stat(PokemonBoostableStat::Attack);
398 } else {
399 attacking_final_stat = attacker.attack;
400 }
401
402 defending_stat = PokemonBoostableStat::Defense;
404 if should_calc_defender_boost {
405 defending_final_stat =
406 defending_side.calculate_boosted_stat(PokemonBoostableStat::Defense);
407 } else {
408 defending_final_stat = defender.defense;
409 }
410 }
411 MoveCategory::Special => {
412 if attacking_side.special_attack_boost > 0 {
413 crit_attacking_stat =
414 attacking_side.calculate_boosted_stat(PokemonBoostableStat::SpecialAttack);
415 } else {
416 crit_attacking_stat = attacker.special_attack;
417 }
418 if defending_side.special_defense_boost <= 0 {
419 crit_defending_stat =
420 defending_side.calculate_boosted_stat(PokemonBoostableStat::SpecialDefense);
421 } else {
422 crit_defending_stat = defender.special_defense;
423 }
424
425 if defender.ability == Abilities::UNAWARE {
427 should_calc_attacker_boost = false;
428 }
429 if attacker.ability == Abilities::UNAWARE {
430 should_calc_defender_boost = false;
431 }
432
433 if should_calc_attacker_boost {
435 attacking_final_stat =
436 attacking_side.calculate_boosted_stat(PokemonBoostableStat::SpecialAttack);
437 } else {
438 attacking_final_stat = attacker.special_attack;
439 }
440
441 if choice.move_id == Choices::PSYSHOCK
444 || choice.move_id == Choices::SECRETSWORD
445 || choice.move_id == Choices::PSYSTRIKE
446 {
447 if defending_side.defense_boost <= 0 {
448 crit_defending_stat =
449 defending_side.calculate_boosted_stat(PokemonBoostableStat::Defense);
450 } else {
451 crit_defending_stat = defender.defense;
452 }
453
454 defending_stat = PokemonBoostableStat::Defense;
455 if should_calc_defender_boost {
456 defending_final_stat =
457 defending_side.calculate_boosted_stat(PokemonBoostableStat::Defense);
458 } else {
459 defending_final_stat = defender.defense;
460 }
461 } else {
462 defending_stat = PokemonBoostableStat::SpecialDefense;
463 if should_calc_defender_boost {
464 defending_final_stat =
465 defending_side.calculate_boosted_stat(PokemonBoostableStat::SpecialDefense);
466 } else {
467 defending_final_stat = defender.special_defense;
468 }
469 }
470 }
471 _ => panic!("Can only calculate damage for physical or special moves"),
472 }
473
474 #[cfg(any(
475 feature = "gen4",
476 feature = "gen5",
477 feature = "gen6",
478 feature = "gen7",
479 feature = "gen8",
480 feature = "gen9"
481 ))]
482 if state.weather_is_active(&Weather::SNOW)
483 && defender.has_type(&PokemonType::ICE)
484 && defending_stat == PokemonBoostableStat::Defense
485 {
486 defending_final_stat = (defending_final_stat as f32 * 1.5) as i16;
487 } else if state.weather_is_active(&Weather::SAND)
488 && defender.has_type(&PokemonType::ROCK)
489 && defending_stat == PokemonBoostableStat::SpecialDefense
490 {
491 defending_final_stat = (defending_final_stat as f32 * 1.5) as i16;
492 }
493
494 (
495 attacking_final_stat,
496 defending_final_stat,
497 crit_attacking_stat,
498 crit_defending_stat,
499 )
500}
501
502fn common_pkmn_damage_calc(
503 attacking_side: &Side,
504 attacker: &Pokemon,
505 attacking_stat: i16,
506 defending_side: &Side,
507 defender: &Pokemon,
508 defending_stat: i16,
509 weather: &Weather,
510 terrain: &Terrain,
511 choice: &Choice,
512) -> f32 {
513 let mut damage: f32;
514 damage = 2.0 * attacker.level as f32;
515 damage = damage.floor() / 5.0;
516 damage = damage.floor() + 2.0;
517 damage = damage.floor() * choice.base_power;
518 damage = damage * attacking_stat as f32 / defending_stat as f32;
519 damage = damage.floor() / 50.0;
520 damage = damage.floor() + 2.0;
521
522 let defender_types = get_defending_types(&defending_side, defender, attacker, choice);
523
524 let mut damage_modifier = 1.0;
525 damage_modifier *= _type_effectiveness_modifier(&choice.move_type, &defender_types);
526
527 if attacker.ability != Abilities::CLOUDNINE
528 && attacker.ability != Abilities::AIRLOCK
529 && defender.ability != Abilities::CLOUDNINE
530 && defender.ability != Abilities::AIRLOCK
531 {
532 damage_modifier *= weather_modifier(&choice.move_type, weather);
533 }
534
535 damage_modifier *= stab_modifier(&choice.move_type, &attacker);
536 damage_modifier *= burn_modifier(&choice.category, &attacker.status);
537 damage_modifier *= volatile_status_modifier(&choice, attacking_side, defending_side);
538 damage_modifier *= terrain_modifier(terrain, attacker, defender, &choice);
539
540 damage * damage_modifier
541}
542
543pub fn calculate_damage(
549 state: &State,
550 attacking_side: &SideReference,
551 choice: &Choice,
552 _damage_rolls: DamageRolls,
553) -> Option<(i16, i16)> {
554 if choice.category == MoveCategory::Status || choice.category == MoveCategory::Switch {
555 return None;
556 } else if choice.base_power == 0.0 {
557 return Some((0, 0));
558 }
559 let (attacking_side, defending_side) = state.get_both_sides_immutable(attacking_side);
560 let attacker = attacking_side.get_active_immutable();
561 let defender = defending_side.get_active_immutable();
562 let (attacking_stat, defending_stat, crit_attacking_stat, crit_defending_stat) =
563 get_attacking_and_defending_stats(
564 attacker,
565 defender,
566 attacking_side,
567 defending_side,
568 state,
569 &choice,
570 );
571
572 let mut damage = common_pkmn_damage_calc(
573 attacking_side,
574 attacker,
575 attacking_stat,
576 defending_side,
577 defender,
578 defending_stat,
579 &state.weather.weather_type,
580 &state.terrain.terrain_type,
581 choice,
582 );
583 if attacker.ability != Abilities::INFILTRATOR {
584 if defending_side.side_conditions.aurora_veil > 0 {
585 damage *= 0.5
586 } else if defending_side.side_conditions.reflect > 0
587 && choice.category == MoveCategory::Physical
588 {
589 damage *= 0.5
590 } else if defending_side.side_conditions.light_screen > 0
591 && choice.category == MoveCategory::Special
592 {
593 damage *= 0.5
594 }
595 }
596
597 let mut crit_damage = common_pkmn_damage_calc(
598 attacking_side,
599 attacker,
600 crit_attacking_stat,
601 defending_side,
602 defender,
603 crit_defending_stat,
604 &state.weather.weather_type,
605 &state.terrain.terrain_type,
606 choice,
607 );
608 crit_damage *= CRIT_MULTIPLIER;
609
610 match _damage_rolls {
611 DamageRolls::Average => {
612 damage = damage.floor() * 0.925;
613 crit_damage = crit_damage.floor() * 0.925;
614 }
615 DamageRolls::Min => {
616 damage = damage.floor() * 0.85;
617 crit_damage = crit_damage.floor() * 0.85;
618 }
619 DamageRolls::Max => {
620 damage = damage.floor();
621 crit_damage = crit_damage.floor();
622 }
623 }
624
625 Some((damage as i16, crit_damage as i16))
626}
627
628pub fn calculate_futuresight_damage(
629 attacking_side: &Side,
630 defending_side: &Side,
631 attacking_side_pokemon_index: &PokemonIndex,
632) -> i16 {
633 let attacking_stat = attacking_side.pokemon[attacking_side_pokemon_index].special_attack;
634 let defending_stat = defending_side.get_active_immutable().special_defense;
635 let attacker = attacking_side.get_active_immutable();
636 let mut damage = common_pkmn_damage_calc(
637 attacking_side,
638 attacker,
639 attacking_stat,
640 defending_side,
641 defending_side.get_active_immutable(),
642 defending_stat,
643 &Weather::NONE,
644 &Terrain::NONE,
645 MOVES.get(&Choices::FUTURESIGHT).unwrap(),
646 );
647 if attacker.ability != Abilities::INFILTRATOR {
648 if defending_side.side_conditions.light_screen > 0 {
649 damage *= 0.5
650 }
651 }
652
653 (damage * 0.925) as i16
654}
655
656#[cfg(test)]
657mod tests {
658 use std::collections::HashSet;
659 use std::iter::FromIterator;
660
661 use super::*;
662 use crate::state::{
663 PokemonStatus, PokemonType, PokemonVolatileStatus, SideReference, State, Weather,
664 };
665
666 #[test]
667 fn test_basic_damaging_move() {
668 let state = State::default();
669 let mut choice = Choice {
670 ..Default::default()
671 };
672 choice.move_id = Choices::TACKLE;
673 choice.move_type = PokemonType::TYPELESS;
674 choice.base_power = 40.0;
675 choice.category = MoveCategory::Physical;
676
677 let dmg = calculate_damage(
678 &state,
679 &SideReference::SideOne,
680 &choice,
681 DamageRolls::Average,
682 );
683
684 assert_eq!(32, dmg.unwrap().0);
686 }
687
688 #[test]
689 fn test_basic_non_damaging_move() {
690 let state = State::default();
691 let mut choice = Choice {
692 ..Default::default()
693 };
694 choice.move_id = Choices::PROTECT;
695 choice.category = MoveCategory::Status;
696
697 let dmg = calculate_damage(
698 &state,
699 &SideReference::SideOne,
700 &choice,
701 DamageRolls::Average,
702 );
703
704 assert_eq!(None, dmg);
705 }
706
707 #[test]
708 fn test_move_with_zero_base_power() {
709 let state = State::default();
710 let mut choice = Choice {
711 ..Default::default()
712 };
713 choice.move_id = Choices::TACKLE;
714 choice.move_type = PokemonType::TYPELESS;
715 choice.base_power = 0.0;
716 choice.category = MoveCategory::Physical;
717
718 let dmg = calculate_damage(
719 &state,
720 &SideReference::SideOne,
721 &choice,
722 DamageRolls::Average,
723 );
724
725 assert_eq!(0, dmg.unwrap().0);
726 }
727
728 #[test]
729 fn test_boosted_damaging_move() {
730 let mut state = State::default();
731 let mut choice = Choice {
732 ..Default::default()
733 };
734 state.side_one.attack_boost = 1;
735 choice.move_id = Choices::TACKLE;
736 choice.move_type = PokemonType::TYPELESS;
737 choice.base_power = 40.0;
738 choice.category = MoveCategory::Physical;
739
740 let dmg = calculate_damage(
741 &state,
742 &SideReference::SideOne,
743 &choice,
744 DamageRolls::Average,
745 );
746
747 assert_eq!(48, dmg.unwrap().0);
748 }
749
750 #[test]
751 fn test_unaware_does_not_get_damaged_by_boosted_stats() {
752 let mut state = State::default();
753 let mut choice = Choice {
754 ..Default::default()
755 };
756 state.side_one.attack_boost = 1;
757 state.side_two.get_active().ability = Abilities::UNAWARE;
758 choice.move_id = Choices::TACKLE;
759 choice.move_type = PokemonType::TYPELESS;
760 choice.base_power = 40.0;
761 choice.category = MoveCategory::Physical;
762
763 let dmg = calculate_damage(
764 &state,
765 &SideReference::SideOne,
766 &choice,
767 DamageRolls::Average,
768 );
769
770 assert_eq!(32, dmg.unwrap().0);
771 }
772
773 #[test]
774 fn test_basic_super_effective_move() {
775 let mut state = State::default();
776 let mut choice = Choice {
777 ..Default::default()
778 };
779
780 state.side_two.get_active().types = (PokemonType::FIRE, PokemonType::TYPELESS);
781 choice.move_id = Choices::WATERGUN;
782 choice.move_type = PokemonType::WATER;
783 choice.base_power = 40.0;
784 choice.category = MoveCategory::Special;
785 let dmg = calculate_damage(
786 &state,
787 &SideReference::SideOne,
788 &choice,
789 DamageRolls::Average,
790 );
791
792 assert_eq!(64, dmg.unwrap().0);
793 }
794
795 #[test]
796 fn test_basic_not_very_effective_move() {
797 let mut state = State::default();
798 let mut choice = Choice {
799 ..Default::default()
800 };
801
802 state.side_two.get_active().types = (PokemonType::WATER, PokemonType::TYPELESS);
803 choice.move_id = Choices::WATERGUN;
804 choice.move_type = PokemonType::WATER;
805 choice.base_power = 40.0;
806 choice.category = MoveCategory::Special;
807 let dmg = calculate_damage(
808 &state,
809 &SideReference::SideOne,
810 &choice,
811 DamageRolls::Average,
812 );
813
814 assert_eq!(15, dmg.unwrap().0);
815 }
816
817 macro_rules! weather_tests {
818 ($($name:ident: $value:expr,)*) => {
819 $(
820 #[test]
821 fn $name() {
822 let (weather_type, move_type, expected_damage_amount) = $value;
823 let mut state = State::default();
824 let mut choice = Choice {
825 ..Default::default()
826 };
827 state.weather.weather_type = weather_type;
828
829 choice.move_type = move_type;
830 choice.base_power = 40.0;
831 choice.category = MoveCategory::Special;
832 let dmg = calculate_damage(&state, &SideReference::SideOne, &choice, DamageRolls::Average);
833
834 assert_eq!(expected_damage_amount, dmg.unwrap().0);
835 }
836 )*
837 }
838 }
839 weather_tests! {
840 test_rain_boosting_water: (Weather::RAIN, PokemonType::WATER, 48),
841 test_rain_not_boosting_normal: (Weather::RAIN, PokemonType::NORMAL, 48),
842 test_sun_boosting_fire: (Weather::SUN, PokemonType::FIRE, 48),
843 test_sun_reducing_water: (Weather::SUN, PokemonType::WATER, 15),
844 test_sun_not_boosting_normal: (Weather::SUN, PokemonType::NORMAL, 48),
845 test_heavy_rain_makes_fire_do_zero: (Weather::HEAVYRAIN, PokemonType::FIRE, 0),
846 test_heavy_rain_boost_water: (Weather::HEAVYRAIN, PokemonType::WATER, 48),
847 test_harsh_sun_makes_water_do_zero: (Weather::HARSHSUN, PokemonType::WATER, 0),
848 test_harsh_sun_boosting_fire: (Weather::HARSHSUN, PokemonType::FIRE, 48),
849 }
850
851 macro_rules! stab_tests {
852 ($($name:ident: $value:expr,)*) => {
853 $(
854 #[test]
855 fn $name() {
856 let (attacker_types, attacking_move_type, expected_damage_amount) = $value;
857 let mut state = State::default();
858 let mut choice = Choice {
859 ..Default::default()
860 };
861 state.side_one.get_active().types = attacker_types;
862
863 choice.move_type = attacking_move_type;
864 choice.base_power = 40.0;
865 choice.category = MoveCategory::Special;
866 let dmg = calculate_damage(&state, &SideReference::SideOne, &choice, DamageRolls::Average);
867
868 assert_eq!(expected_damage_amount, dmg.unwrap().0);
869 }
870 )*
871 }
872 }
873 stab_tests! {
874 test_basic_stab: ((PokemonType::WATER, PokemonType::FIRE), PokemonType::WATER, 48),
875 test_basic_without_stab: ((PokemonType::WATER, PokemonType::FIRE), PokemonType::NORMAL, 32),
876 }
877
878 macro_rules! burn_tests {
879 ($($name:ident: $value:expr,)*) => {
880 $(
881 #[test]
882 fn $name() {
883 let (attacking_move_category, expected_damage_amount) = $value;
884 let mut state = State::default();
885 let mut choice = Choice {
886 ..Default::default()
887 };
888 state.side_one.get_active().status = PokemonStatus::BURN;
889
890 choice.category = attacking_move_category;
891 choice.move_type = PokemonType::TYPELESS;
892 choice.base_power = 40.0;
893 let dmg = calculate_damage(&state, &SideReference::SideOne, &choice, DamageRolls::Average);
894
895 assert_eq!(expected_damage_amount, dmg.unwrap().0);
896 }
897 )*
898 }
899 }
900 burn_tests! {
901 test_physical_move_when_burned_reduces: (MoveCategory::Physical, 15),
902 test_special_move_when_burned_does_not_reduce: (MoveCategory::Special, 32),
903 }
904
905 macro_rules! screens_tests {
906 ($($name:ident: $value:expr,)*) => {
907 $(
908 #[test]
909 fn $name() {
910 let (reflect_count, lightscreen_count, auroraveil_count, move_category, expected_damage_amount) = $value;
911 let mut state = State::default();
912 let mut choice = Choice {
913 ..Default::default()
914 };
915 state.side_two.side_conditions.reflect = reflect_count;
916 state.side_two.side_conditions.light_screen = lightscreen_count;
917 state.side_two.side_conditions.aurora_veil = auroraveil_count;
918
919 choice.category = move_category;
920 choice.base_power = 40.0;
921 choice.move_type = PokemonType::TYPELESS;
922 let dmg = calculate_damage(&state, &SideReference::SideOne, &choice, DamageRolls::Average);
923
924 assert_eq!(expected_damage_amount, dmg.unwrap().0);
925 }
926 )*
927 }
928 }
929 screens_tests! {
930 test_reflect_reduces_physical_damage_by_half: (1, 0, 0, MoveCategory::Physical, 15),
931 test_lightscreen_reduces_special_damage_by_half: (0, 1, 0, MoveCategory::Special, 15),
932 test_auroraveil_reduces_physical_damage_by_half: (0, 0, 1, MoveCategory::Physical, 15),
933 test_auroraveil_reduces_special_damage_by_half: (0, 0, 1, MoveCategory::Special, 15),
934 test_reflect_does_not_reduce_special_damage: (1, 0, 0, MoveCategory::Special, 32),
935 test_light_screen_does_not_reduce_physical_damage: (0, 1, 0, MoveCategory::Physical, 32),
936 test_auroraveil_does_not_stack_with_reflect: (1, 1, 1, MoveCategory::Physical, 15),
937 test_auroraveil_does_not_stack_with_lightscreen: (1, 1, 1, MoveCategory::Special, 15),
938 }
939
940 macro_rules! volatile_status_tests{
941 ($($name:ident: $value:expr,)*) => {
942 $(
943 #[test]
944 fn $name() {
945 let (attacking_volatile_status, defending_volatile_status, move_type, move_name, expected_damage_amount) = $value;
946 let mut state = State::default();
947 let mut choice = Choice {
948 ..Default::default()
949 };
950 state.side_one.volatile_statuses = HashSet::from_iter(attacking_volatile_status);
951 state.side_two.volatile_statuses = HashSet::from_iter(defending_volatile_status);
952
953 choice.move_id = move_name;
954 choice.category = MoveCategory::Physical;
955 choice.move_type = move_type;
956 choice.base_power = 40.0;
957 let dmg = calculate_damage(&state, &SideReference::SideOne, &choice, DamageRolls::Average);
958
959 assert_eq!(expected_damage_amount, dmg.unwrap().0);
960 }
961 )*
962 }
963 }
964 volatile_status_tests! {
965 test_flashfire_boosts_fire_move: (
966 vec![PokemonVolatileStatus::FLASHFIRE],
967 vec![],
968 PokemonType::FIRE,
969 Choices::NONE,
970 48
971 ),
972 test_flashfire_does_not_boost_normal_move: (
973 vec![PokemonVolatileStatus::FLASHFIRE],
974 vec![],
975 PokemonType::TYPELESS,
976 Choices::NONE,
977 32
978 ),
979 test_magnetrise_makes_pkmn_immune_to_ground_move: (
980 vec![],
981 vec![PokemonVolatileStatus::MAGNETRISE],
982 PokemonType::GROUND,
983 Choices::NONE,
984 0
985 ),
986 test_thousandarrows_can_hit_magnetrise_pokemon: (
987 vec![],
988 vec![PokemonVolatileStatus::MAGNETRISE],
989 PokemonType::GROUND,
990 Choices::THOUSANDARROWS,
991 32
992 ),
993 test_tarshot_boosts_fire_move: (
994 vec![],
995 vec![PokemonVolatileStatus::TARSHOT],
996 PokemonType::FIRE,
997 Choices::NONE,
998 64
999 ),
1000 test_slowstart_halves_move: (
1001 vec![PokemonVolatileStatus::SLOWSTART],
1002 vec![],
1003 PokemonType::NORMAL,
1004 Choices::NONE,
1005 24
1006 ),
1007 test_tarshot_and_flashfire_together: (
1008 vec![PokemonVolatileStatus::FLASHFIRE],
1009 vec![PokemonVolatileStatus::TARSHOT],
1010 PokemonType::FIRE,
1011 Choices::NONE,
1012 97
1013 ),
1014 test_glaiverush_doubles_damage_against: (
1015 vec![],
1016 vec![PokemonVolatileStatus::GLAIVERUSH],
1017 PokemonType::NORMAL,
1018 Choices::NONE,
1019 97
1020 ),
1021 test_phantomforce_on_defender_causes_0_damage: (
1022 vec![],
1023 vec![PokemonVolatileStatus::PHANTOMFORCE],
1024 PokemonType::NORMAL,
1025 Choices::NONE,
1026 0
1027 ),
1028 }
1029}