-
Notifications
You must be signed in to change notification settings - Fork 1
/
state.rs
2848 lines (2663 loc) · 101 KB
/
state.rs
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use crate::abilities::Abilities;
use crate::choices::{Choice, Choices, MoveCategory, MOVES};
use crate::define_enum_with_from_str;
use crate::instruction::{
BoostInstruction, ChangeSideConditionInstruction, EnableMoveInstruction, Instruction,
RemoveVolatileStatusInstruction,
};
use crate::items::Items;
use crate::pokemon::PokemonName;
use core::panic;
use std::collections::HashSet;
use std::ops::{Index, IndexMut};
use std::str::FromStr;
fn multiply_boost(boost_num: i8, stat_value: i16) -> i16 {
match boost_num {
-6 => stat_value * 2 / 8,
-5 => stat_value * 2 / 7,
-4 => stat_value * 2 / 6,
-3 => stat_value * 2 / 5,
-2 => stat_value * 2 / 4,
-1 => stat_value * 2 / 3,
0 => stat_value,
1 => stat_value * 3 / 2,
2 => stat_value * 4 / 2,
3 => stat_value * 5 / 2,
4 => stat_value * 6 / 2,
5 => stat_value * 7 / 2,
6 => stat_value * 8 / 2,
_ => panic!("Invalid boost number"),
}
}
#[derive(Debug, Clone)]
pub struct DamageDealt {
pub damage: i16,
pub move_category: MoveCategory,
pub hit_substitute: bool,
}
impl Default for DamageDealt {
fn default() -> DamageDealt {
DamageDealt {
damage: 0,
move_category: MoveCategory::Physical,
hit_substitute: false,
}
}
}
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum LastUsedMove {
Move(PokemonMoveIndex),
Switch(PokemonIndex),
None,
}
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
pub enum MoveChoice {
MoveTera(PokemonMoveIndex),
Move(PokemonMoveIndex),
Switch(PokemonIndex),
None,
}
impl MoveChoice {
pub fn to_string(&self, side: &Side) -> String {
match self {
MoveChoice::MoveTera(index) => {
format!("{}-tera", side.get_active_immutable().moves[index].id).to_lowercase()
}
MoveChoice::Move(index) => {
format!("{}", side.get_active_immutable().moves[index].id).to_lowercase()
}
MoveChoice::Switch(index) => {
format!("switch {}", side.pokemon[*index].id).to_lowercase()
}
MoveChoice::None => "No Move".to_string(),
}
}
}
define_enum_with_from_str! {
#[repr(u8)]
#[derive(Debug, PartialEq, Copy, Clone, Hash)]
PokemonStatus {
NONE,
BURN,
SLEEP,
FREEZE,
PARALYZE,
POISON,
TOXIC,
}
}
define_enum_with_from_str! {
#[repr(u8)]
#[derive(PartialEq, Eq, Hash, Debug, Copy, Clone)]
PokemonVolatileStatus {
NONE,
AQUARING,
ATTRACT,
AUTOTOMIZE,
BANEFULBUNKER,
BIDE,
BOUNCE,
BURNINGBULWARK,
CHARGE,
CONFUSION,
CURSE,
DEFENSECURL,
DESTINYBOND,
DIG,
DISABLE,
DIVE,
ELECTRIFY,
ELECTROSHOT,
EMBARGO,
ENCORE,
ENDURE,
FLASHFIRE,
FLINCH,
FLY,
FOCUSENERGY,
FOLLOWME,
FORESIGHT,
FREEZESHOCK,
GASTROACID,
GEOMANCY,
GLAIVERUSH,
GRUDGE,
HEALBLOCK,
HELPINGHAND,
ICEBURN,
IMPRISON,
INGRAIN,
KINGSSHIELD,
LASERFOCUS,
LEECHSEED,
LIGHTSCREEN,
LOCKEDMOVE,
MAGICCOAT,
MAGNETRISE,
MAXGUARD,
METEORBEAM,
MINIMIZE,
MIRACLEEYE,
MUSTRECHARGE,
NIGHTMARE,
NORETREAT,
OCTOLOCK,
PARTIALLYTRAPPED,
PERISH4,
PERISH3,
PERISH2,
PERISH1,
PHANTOMFORCE,
POWDER,
POWERSHIFT,
POWERTRICK,
PROTECT,
PROTOSYNTHESISATK,
PROTOSYNTHESISDEF,
PROTOSYNTHESISSPA,
PROTOSYNTHESISSPD,
PROTOSYNTHESISSPE,
QUARKDRIVEATK,
QUARKDRIVEDEF,
QUARKDRIVESPA,
QUARKDRIVESPD,
QUARKDRIVESPE,
RAGE,
RAGEPOWDER,
RAZORWIND,
REFLECT,
ROOST,
SALTCURE,
SHADOWFORCE,
SKULLBASH,
SKYATTACK,
SKYDROP,
SILKTRAP,
SLOWSTART,
SMACKDOWN,
SNATCH,
SOLARBEAM,
SOLARBLADE,
SPARKLINGARIA,
SPIKYSHIELD,
SPOTLIGHT,
STOCKPILE,
SUBSTITUTE,
SYRUPBOMB,
TARSHOT,
TAUNT,
TELEKINESIS,
THROATCHOP,
TRUANT,
TORMENT,
UNBURDEN,
UPROAR,
YAWN,
YAWNSLEEPTHISTURN,
},
default = NONE
}
#[derive(Debug, Eq, PartialEq, Hash, Copy, Clone)]
pub enum PokemonSideCondition {
AuroraVeil,
CraftyShield,
HealingWish,
LightScreen,
LuckyChant,
LunarDance,
MatBlock,
Mist,
Protect,
QuickGuard,
Reflect,
Safeguard,
Spikes,
Stealthrock,
StickyWeb,
Tailwind,
ToxicCount,
ToxicSpikes,
WideGuard,
}
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum SideReference {
SideOne,
SideTwo,
}
impl SideReference {
pub fn get_other_side(&self) -> SideReference {
match self {
SideReference::SideOne => SideReference::SideTwo,
SideReference::SideTwo => SideReference::SideOne,
}
}
}
define_enum_with_from_str! {
#[repr(u8)]
#[derive(Debug, PartialEq, Copy, Clone)]
Weather {
NONE,
SUN,
RAIN,
SAND,
HAIL,
SNOW,
HARSHSUN,
HEAVYRAIN,
}
}
#[derive(Debug, PartialEq, Clone)]
pub struct StateWeather {
pub weather_type: Weather,
pub turns_remaining: i8,
}
define_enum_with_from_str! {
#[repr(u8)]
#[derive(Debug, PartialEq, Copy, Clone)]
Terrain {
NONE,
ELECTRICTERRAIN,
PSYCHICTERRAIN,
MISTYTERRAIN,
GRASSYTERRAIN,
}
}
#[derive(Debug, PartialEq, Clone)]
pub struct StateTerrain {
pub terrain_type: Terrain,
pub turns_remaining: i8,
}
#[derive(Debug, PartialEq, Clone)]
pub struct StateTrickRoom {
pub active: bool,
pub turns_remaining: i8,
}
define_enum_with_from_str! {
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq)]
PokemonType {
NORMAL,
FIRE,
WATER,
ELECTRIC,
GRASS,
ICE,
FIGHTING,
POISON,
GROUND,
FLYING,
PSYCHIC,
BUG,
ROCK,
GHOST,
DRAGON,
DARK,
STEEL,
FAIRY,
STELLAR,
TYPELESS,
},
default = TYPELESS
}
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum PokemonBoostableStat {
Attack,
Defense,
SpecialAttack,
SpecialDefense,
Speed,
Evasion,
Accuracy,
}
#[derive(Debug, PartialEq, Clone)]
pub struct SideConditions {
pub aurora_veil: i8,
pub crafty_shield: i8,
pub healing_wish: i8,
pub light_screen: i8,
pub lucky_chant: i8,
pub lunar_dance: i8,
pub mat_block: i8,
pub mist: i8,
pub protect: i8,
pub quick_guard: i8,
pub reflect: i8,
pub safeguard: i8,
pub spikes: i8,
pub stealth_rock: i8,
pub sticky_web: i8,
pub tailwind: i8,
pub toxic_count: i8,
pub toxic_spikes: i8,
pub wide_guard: i8,
}
impl Default for SideConditions {
fn default() -> SideConditions {
SideConditions {
aurora_veil: 0,
crafty_shield: 0,
healing_wish: 0,
light_screen: 0,
lucky_chant: 0,
lunar_dance: 0,
mat_block: 0,
mist: 0,
protect: 0,
quick_guard: 0,
reflect: 0,
safeguard: 0,
spikes: 0,
stealth_rock: 0,
sticky_web: 0,
tailwind: 0,
toxic_count: 0,
toxic_spikes: 0,
wide_guard: 0,
}
}
}
#[derive(Debug, Copy, PartialEq, Clone, Eq, Hash)]
pub enum PokemonMoveIndex {
M0,
M1,
M2,
M3,
M4,
M5,
}
#[derive(Debug, Clone)]
pub struct PokemonMoves {
pub m0: Move,
pub m1: Move,
pub m2: Move,
pub m3: Move,
pub m4: Move,
pub m5: Move,
}
impl Index<&PokemonMoveIndex> for PokemonMoves {
type Output = Move;
fn index(&self, index: &PokemonMoveIndex) -> &Self::Output {
match index {
PokemonMoveIndex::M0 => &self.m0,
PokemonMoveIndex::M1 => &self.m1,
PokemonMoveIndex::M2 => &self.m2,
PokemonMoveIndex::M3 => &self.m3,
PokemonMoveIndex::M4 => &self.m4,
PokemonMoveIndex::M5 => &self.m5,
}
}
}
impl IndexMut<&PokemonMoveIndex> for PokemonMoves {
fn index_mut(&mut self, index: &PokemonMoveIndex) -> &mut Self::Output {
match index {
PokemonMoveIndex::M0 => &mut self.m0,
PokemonMoveIndex::M1 => &mut self.m1,
PokemonMoveIndex::M2 => &mut self.m2,
PokemonMoveIndex::M3 => &mut self.m3,
PokemonMoveIndex::M4 => &mut self.m4,
PokemonMoveIndex::M5 => &mut self.m5,
}
}
}
pub struct PokemonMoveIterator<'a> {
pub pokemon_move: &'a PokemonMoves,
pub pokemon_move_index: PokemonMoveIndex,
pub index: usize,
}
impl<'a> Iterator for PokemonMoveIterator<'a> {
type Item = &'a Move;
fn next(&mut self) -> Option<Self::Item> {
match self.index {
0 => {
self.index += 1;
self.pokemon_move_index = PokemonMoveIndex::M0;
Some(&self.pokemon_move.m0)
}
1 => {
self.index += 1;
self.pokemon_move_index = PokemonMoveIndex::M1;
Some(&self.pokemon_move.m1)
}
2 => {
self.index += 1;
self.pokemon_move_index = PokemonMoveIndex::M2;
Some(&self.pokemon_move.m2)
}
3 => {
self.index += 1;
self.pokemon_move_index = PokemonMoveIndex::M3;
Some(&self.pokemon_move.m3)
}
4 => {
self.index += 1;
self.pokemon_move_index = PokemonMoveIndex::M4;
Some(&self.pokemon_move.m4)
}
5 => {
self.index += 1;
self.pokemon_move_index = PokemonMoveIndex::M5;
Some(&self.pokemon_move.m5)
}
_ => None,
}
}
}
impl<'a> IntoIterator for &'a PokemonMoves {
type Item = &'a Move;
type IntoIter = PokemonMoveIterator<'a>;
fn into_iter(self) -> Self::IntoIter {
PokemonMoveIterator {
pokemon_move: &self,
pokemon_move_index: PokemonMoveIndex::M0,
index: 0,
}
}
}
#[derive(Debug, Clone)]
pub struct Move {
pub id: Choices,
pub disabled: bool,
pub pp: i8,
pub choice: Choice,
}
impl Default for Move {
fn default() -> Move {
Move {
id: Choices::NONE,
disabled: false,
pp: 32,
choice: Choice::default(),
}
}
}
#[derive(Debug, Clone)]
pub struct Pokemon {
pub id: PokemonName,
pub level: i8,
pub types: (PokemonType, PokemonType),
pub hp: i16,
pub maxhp: i16,
pub ability: Abilities,
pub item: Items,
pub attack: i16,
pub defense: i16,
pub special_attack: i16,
pub special_defense: i16,
pub speed: i16,
pub status: PokemonStatus,
pub rest_turns: i8,
pub sleep_turns: i8,
pub weight_kg: f32,
pub terastallized: bool,
pub tera_type: PokemonType,
pub moves: PokemonMoves,
}
impl Pokemon {
pub fn get_stat_from_boostable_stat(&self, stat: PokemonBoostableStat) -> i16 {
match stat {
PokemonBoostableStat::Attack => self.attack,
PokemonBoostableStat::Defense => self.defense,
PokemonBoostableStat::SpecialAttack => self.special_attack,
PokemonBoostableStat::SpecialDefense => self.special_defense,
PokemonBoostableStat::Speed => self.speed,
_ => panic!("Not implemented"),
}
}
pub fn get_sleep_talk_choices(&self) -> Vec<Choice> {
let mut vec = Vec::with_capacity(4);
for p in self.moves.into_iter() {
if p.id != Choices::SLEEPTALK && p.id != Choices::NONE {
vec.push(p.choice.clone());
}
}
vec
}
pub fn replace_move(&mut self, move_index: PokemonMoveIndex, new_move_name: Choices) {
self.moves[&move_index].choice = MOVES.get(&new_move_name).unwrap().to_owned();
self.moves[&move_index].id = new_move_name;
}
pub fn add_available_moves(
&self,
vec: &mut Vec<MoveChoice>,
last_used_move: &LastUsedMove,
encored: bool,
can_tera: bool,
) {
let mut iter = self.moves.into_iter();
while let Some(p) = iter.next() {
if !p.disabled && p.pp > 0 {
if (iter.pokemon_move_index == PokemonMoveIndex::M4
|| iter.pokemon_move_index == PokemonMoveIndex::M5)
&& p.id == Choices::NONE
{
break;
}
match last_used_move {
LastUsedMove::Move(last_used_move) => {
if encored && last_used_move != &iter.pokemon_move_index {
continue;
} else if (self.moves[last_used_move].id == Choices::BLOODMOON
|| self.moves[last_used_move].id == Choices::GIGATONHAMMER)
&& &iter.pokemon_move_index == last_used_move
{
continue;
}
}
_ => {
// there are some situations where you switched out and got encored into
// a move from a different pokemon because you also have that move.
// just assume nothing is locked in this case
}
}
vec.push(MoveChoice::Move(iter.pokemon_move_index));
if can_tera {
vec.push(MoveChoice::MoveTera(iter.pokemon_move_index));
}
}
}
}
pub fn add_move_from_choice(&self, vec: &mut Vec<MoveChoice>, choice: Choices) {
let mut iter = self.moves.into_iter();
while let Some(p) = iter.next() {
if p.id == choice {
vec.push(MoveChoice::Move(iter.pokemon_move_index));
}
}
}
#[cfg(feature = "terastallization")]
pub fn has_type(&self, pkmn_type: &PokemonType) -> bool {
if self.terastallized {
pkmn_type == &self.tera_type
} else {
pkmn_type == &self.types.0 || pkmn_type == &self.types.1
}
}
#[cfg(not(feature = "terastallization"))]
pub fn has_type(&self, pkmn_type: &PokemonType) -> bool {
pkmn_type == &self.types.0 || pkmn_type == &self.types.1
}
pub fn item_is_permanent(&self) -> bool {
match self.item {
Items::SPLASHPLATE => self.id == PokemonName::ARCEUSWATER,
Items::TOXICPLATE => self.id == PokemonName::ARCEUSPOISON,
Items::EARTHPLATE => self.id == PokemonName::ARCEUSGROUND,
Items::STONEPLATE => self.id == PokemonName::ARCEUSROCK,
Items::INSECTPLATE => self.id == PokemonName::ARCEUSBUG,
Items::SPOOKYPLATE => self.id == PokemonName::ARCEUSGHOST,
Items::IRONPLATE => self.id == PokemonName::ARCEUSSTEEL,
Items::FLAMEPLATE => self.id == PokemonName::ARCEUSFIRE,
Items::MEADOWPLATE => self.id == PokemonName::ARCEUSGRASS,
Items::ZAPPLATE => self.id == PokemonName::ARCEUSELECTRIC,
Items::MINDPLATE => self.id == PokemonName::ARCEUSPSYCHIC,
Items::ICICLEPLATE => self.id == PokemonName::ARCEUSICE,
Items::DRACOPLATE => self.id == PokemonName::ARCEUSDRAGON,
Items::DREADPLATE => self.id == PokemonName::ARCEUSDARK,
Items::FISTPLATE => self.id == PokemonName::ARCEUSFIGHTING,
Items::BLANKPLATE => self.id == PokemonName::ARCEUS,
Items::SKYPLATE => self.id == PokemonName::ARCEUSFLYING,
Items::PIXIEPLATE => self.id == PokemonName::ARCEUSFAIRY,
Items::BUGMEMORY => self.id == PokemonName::SILVALLYBUG,
Items::FIGHTINGMEMORY => self.id == PokemonName::SILVALLYFIGHTING,
Items::GHOSTMEMORY => self.id == PokemonName::SILVALLYGHOST,
Items::PSYCHICMEMORY => self.id == PokemonName::SILVALLYPSYCHIC,
Items::FLYINGMEMORY => self.id == PokemonName::SILVALLYFLYING,
Items::STEELMEMORY => self.id == PokemonName::SILVALLYSTEEL,
Items::ICEMEMORY => self.id == PokemonName::SILVALLYICE,
Items::POISONMEMORY => self.id == PokemonName::SILVALLYPOISON,
Items::FIREMEMORY => self.id == PokemonName::SILVALLYFIRE,
Items::DRAGONMEMORY => self.id == PokemonName::SILVALLYDRAGON,
Items::GROUNDMEMORY => self.id == PokemonName::SILVALLYGROUND,
Items::WATERMEMORY => self.id == PokemonName::SILVALLYWATER,
Items::DARKMEMORY => self.id == PokemonName::SILVALLYDARK,
Items::ROCKMEMORY => self.id == PokemonName::SILVALLYROCK,
Items::GRASSMEMORY => self.id == PokemonName::SILVALLYGRASS,
Items::FAIRYMEMORY => self.id == PokemonName::SILVALLYFAIRY,
Items::ELECTRICMEMORY => self.id == PokemonName::SILVALLYELECTRIC,
Items::CORNERSTONEMASK => {
self.id == PokemonName::OGERPONCORNERSTONE
|| self.id == PokemonName::OGERPONCORNERSTONETERA
}
Items::HEARTHFLAMEMASK => {
self.id == PokemonName::OGERPONHEARTHFLAME
|| self.id == PokemonName::OGERPONHEARTHFLAMETERA
}
Items::WELLSPRINGMASK => {
self.id == PokemonName::OGERPONWELLSPRING
|| self.id == PokemonName::OGERPONWELLSPRINGTERA
}
_ => false,
}
}
pub fn item_can_be_removed(&self) -> bool {
if self.ability == Abilities::STICKYHOLD {
return false;
}
!self.item_is_permanent()
}
pub fn is_grounded(&self) -> bool {
if self.item == Items::IRONBALL {
return true;
}
if self.has_type(&PokemonType::FLYING)
|| self.ability == Abilities::LEVITATE
|| self.item == Items::AIRBALLOON
{
return false;
}
true
}
pub fn volatile_status_can_be_applied(
&self,
volatile_status: &PokemonVolatileStatus,
active_volatiles: &HashSet<PokemonVolatileStatus>,
first_move: bool,
) -> bool {
if active_volatiles.contains(volatile_status) || self.hp == 0 {
return false;
}
match volatile_status {
// grass immunity to leechseed covered by `powder`
PokemonVolatileStatus::LEECHSEED | PokemonVolatileStatus::CONFUSION => {
if active_volatiles.contains(&PokemonVolatileStatus::SUBSTITUTE) {
return false;
}
true
}
PokemonVolatileStatus::SUBSTITUTE => self.hp > self.maxhp / 4,
PokemonVolatileStatus::FLINCH => {
if !first_move || [Abilities::INNERFOCUS].contains(&self.ability) {
return false;
}
true
}
PokemonVolatileStatus::PROTECT => first_move,
PokemonVolatileStatus::TAUNT
| PokemonVolatileStatus::TORMENT
| PokemonVolatileStatus::ENCORE
| PokemonVolatileStatus::DISABLE
| PokemonVolatileStatus::HEALBLOCK
| PokemonVolatileStatus::ATTRACT => self.ability != Abilities::AROMAVEIL,
PokemonVolatileStatus::YAWN => {
// immunity to yawn via sleep immunity is handled in `get_instructions_from_volatile_statuses`
!active_volatiles.contains(&PokemonVolatileStatus::YAWNSLEEPTHISTURN)
}
_ => true,
}
}
pub fn immune_to_stats_lowered_by_opponent(
&self,
stat: &PokemonBoostableStat,
volatiles: &HashSet<PokemonVolatileStatus>,
) -> bool {
if [
Abilities::CLEARBODY,
Abilities::WHITESMOKE,
Abilities::FULLMETALBODY,
]
.contains(&self.ability)
|| ([Items::CLEARAMULET].contains(&self.item))
{
return true;
}
if volatiles.contains(&PokemonVolatileStatus::SUBSTITUTE) {
return true;
}
if stat == &PokemonBoostableStat::Attack && self.ability == Abilities::HYPERCUTTER {
return true;
} else if stat == &PokemonBoostableStat::Accuracy && self.ability == Abilities::KEENEYE {
return true;
}
false
}
}
impl Default for Pokemon {
fn default() -> Pokemon {
Pokemon {
id: PokemonName::NONE,
level: 100,
types: (PokemonType::NORMAL, PokemonType::TYPELESS),
hp: 100,
maxhp: 100,
ability: Abilities::NONE,
item: Items::NONE,
attack: 100,
defense: 100,
special_attack: 100,
special_defense: 100,
speed: 100,
status: PokemonStatus::NONE,
rest_turns: 0,
sleep_turns: 0,
weight_kg: 1.0,
terastallized: false,
tera_type: PokemonType::NORMAL,
moves: PokemonMoves {
m0: Default::default(),
m1: Default::default(),
m2: Default::default(),
m3: Default::default(),
m4: Default::default(),
m5: Default::default(),
},
}
}
}
#[derive(Debug, Copy, PartialEq, Clone, Eq, Hash)]
pub enum PokemonIndex {
P0,
P1,
P2,
P3,
P4,
P5,
}
pub fn pokemon_index_iter() -> PokemonIndexIterator {
PokemonIndexIterator { index: 0 }
}
pub struct PokemonIndexIterator {
index: usize,
}
impl Iterator for PokemonIndexIterator {
type Item = PokemonIndex;
fn next(&mut self) -> Option<Self::Item> {
match self.index {
0 => {
self.index += 1;
Some(PokemonIndex::P0)
}
1 => {
self.index += 1;
Some(PokemonIndex::P1)
}
2 => {
self.index += 1;
Some(PokemonIndex::P2)
}
3 => {
self.index += 1;
Some(PokemonIndex::P3)
}
4 => {
self.index += 1;
Some(PokemonIndex::P4)
}
5 => {
self.index += 1;
Some(PokemonIndex::P5)
}
_ => None,
}
}
}
#[derive(Debug, Clone)]
pub struct SidePokemon {
pub p0: Pokemon,
pub p1: Pokemon,
pub p2: Pokemon,
pub p3: Pokemon,
pub p4: Pokemon,
pub p5: Pokemon,
}
impl<'a> IntoIterator for &'a SidePokemon {
type Item = &'a Pokemon;
type IntoIter = SidePokemonIterator<'a>;
fn into_iter(self) -> Self::IntoIter {
SidePokemonIterator {
side_pokemon: &self,
pokemon_index: PokemonIndex::P0,
index: 0,
}
}
}
pub struct SidePokemonIterator<'a> {
pub side_pokemon: &'a SidePokemon,
pub pokemon_index: PokemonIndex,
pub index: usize,
}
impl<'a> Iterator for SidePokemonIterator<'a> {
type Item = &'a Pokemon;
fn next(&mut self) -> Option<Self::Item> {
match self.index {
0 => {
self.index += 1;
self.pokemon_index = PokemonIndex::P0;
Some(&self.side_pokemon.p0)
}
1 => {
self.index += 1;
self.pokemon_index = PokemonIndex::P1;
Some(&self.side_pokemon.p1)
}
2 => {
self.index += 1;
self.pokemon_index = PokemonIndex::P2;
Some(&self.side_pokemon.p2)
}
3 => {
self.index += 1;
self.pokemon_index = PokemonIndex::P3;
Some(&self.side_pokemon.p3)
}
4 => {
self.index += 1;
self.pokemon_index = PokemonIndex::P4;
Some(&self.side_pokemon.p4)
}
5 => {
self.index += 1;
self.pokemon_index = PokemonIndex::P5;
Some(&self.side_pokemon.p5)
}
_ => None,
}
}
}
impl Index<PokemonIndex> for SidePokemon {
type Output = Pokemon;
fn index(&self, index: PokemonIndex) -> &Self::Output {
match index {
PokemonIndex::P0 => &self.p0,
PokemonIndex::P1 => &self.p1,
PokemonIndex::P2 => &self.p2,
PokemonIndex::P3 => &self.p3,
PokemonIndex::P4 => &self.p4,
PokemonIndex::P5 => &self.p5,
}
}
}
impl Index<&PokemonIndex> for SidePokemon {
type Output = Pokemon;
fn index(&self, index: &PokemonIndex) -> &Self::Output {
match index {
PokemonIndex::P0 => &self.p0,
PokemonIndex::P1 => &self.p1,
PokemonIndex::P2 => &self.p2,
PokemonIndex::P3 => &self.p3,
PokemonIndex::P4 => &self.p4,
PokemonIndex::P5 => &self.p5,
}
}
}
impl IndexMut<PokemonIndex> for SidePokemon {
fn index_mut(&mut self, index: PokemonIndex) -> &mut Self::Output {
match index {
PokemonIndex::P0 => &mut self.p0,
PokemonIndex::P1 => &mut self.p1,
PokemonIndex::P2 => &mut self.p2,
PokemonIndex::P3 => &mut self.p3,
PokemonIndex::P4 => &mut self.p4,
PokemonIndex::P5 => &mut self.p5,
}
}
}
#[derive(Debug, Clone)]
pub struct Side {
pub active_index: PokemonIndex,
pub baton_passing: bool,
pub pokemon: SidePokemon,
pub side_conditions: SideConditions,
pub wish: (i8, i16),
pub future_sight: (i8, PokemonIndex),
pub force_switch: bool,
pub force_trapped: bool,
pub slow_uturn_move: bool,
pub volatile_statuses: HashSet<PokemonVolatileStatus>,
pub substitute_health: i16,
pub attack_boost: i8,
pub defense_boost: i8,
pub special_attack_boost: i8,
pub special_defense_boost: i8,
pub speed_boost: i8,
pub accuracy_boost: i8,
pub evasion_boost: i8,
pub last_used_move: LastUsedMove,
pub damage_dealt: DamageDealt,
pub switch_out_move_second_saved_move: Choices,
}
impl Side {
pub fn calculate_highest_stat(&self) -> PokemonBoostableStat {
let mut highest_stat = PokemonBoostableStat::Attack;
let mut highest_stat_value = self.calculate_boosted_stat(PokemonBoostableStat::Attack);
for stat in [
PokemonBoostableStat::Defense,
PokemonBoostableStat::SpecialAttack,
PokemonBoostableStat::SpecialDefense,
PokemonBoostableStat::Speed,
] {
let stat_value = self.calculate_boosted_stat(stat);
if stat_value > highest_stat_value {
highest_stat = stat;
highest_stat_value = stat_value;
}
}
highest_stat
}