/
PKM.cs
995 lines (903 loc) · 40 KB
/
PKM.cs
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
using System;
using System.Linq;
namespace PKHeX.Core
{
/// <summary>
/// Object representing a <see cref="PKM"/>'s data and derived properties.
/// </summary>
public abstract class PKM : ITrainerID, ILangNick, IGameValueLimit
{
public static readonly string[] Extensions = PKX.GetPKMExtensions();
public abstract int SIZE_PARTY { get; }
public abstract int SIZE_STORED { get; }
public string Extension => GetType().Name.ToLower();
public abstract PersonalInfo PersonalInfo { get; }
// Internal Attributes set on creation
public byte[] Data; // Raw Storage
public string Identifier; // User or Form Custom Attribute
public int Box { get; set; } = -1; // Batch Editor
public int Slot { get; set; } = -1; // Batch Editor
public virtual byte[] EncryptedPartyData => Encrypt().Take(SIZE_PARTY).ToArray();
public virtual byte[] EncryptedBoxData => Encrypt().Take(SIZE_STORED).ToArray();
public virtual byte[] DecryptedPartyData => Write().Take(SIZE_PARTY).ToArray();
public virtual byte[] DecryptedBoxData => Write().Take(SIZE_STORED).ToArray();
public virtual bool Valid { get => ChecksumValid && Sanity == 0; set { if (!value) return; Sanity = 0; RefreshChecksum(); } }
// Trash Bytes
public abstract byte[] Nickname_Trash { get; set; }
public abstract byte[] OT_Trash { get; set; }
public virtual byte[] HT_Trash { get; set; }
protected byte[] GetData(int Offset, int Length)
{
if (Offset + Length > Data.Length)
return null;
byte[] data = new byte[Length];
Array.Copy(Data, Offset, data, 0, Length);
return data;
}
protected virtual ushort CalculateChecksum()
{
ushort chk = 0;
switch (Format)
{
case 3:
for (int i = 32; i < SIZE_STORED; i += 2)
chk += BitConverter.ToUInt16(Data, i);
return chk;
default: // 4+
for (int i = 8; i < SIZE_STORED; i += 2)
chk += BitConverter.ToUInt16(Data, i);
return chk;
}
}
protected abstract byte[] Encrypt();
public abstract int Format { get; }
private byte[] Write()
{
RefreshChecksum();
return Data;
}
// Surface Properties
public abstract int Species { get; set; }
public abstract string Nickname { get; set; }
public abstract int HeldItem { get; set; }
public abstract int Gender { get; set; }
public abstract int Nature { get; set; }
public abstract int Ability { get; set; }
public abstract int CurrentFriendship { get; set; }
public abstract int AltForm { get; set; }
public abstract bool IsEgg { get; set; }
public abstract bool IsNicknamed { get; set; }
public abstract uint EXP { get; set; }
public abstract int TID { get; set; }
public abstract string OT_Name { get; set; }
public abstract int OT_Gender { get; set; }
public abstract int Ball { get; set; }
public abstract int Met_Level { get; set; }
// Battle
public abstract int Move1 { get; set; }
public abstract int Move2 { get; set; }
public abstract int Move3 { get; set; }
public abstract int Move4 { get; set; }
public abstract int Move1_PP { get; set; }
public abstract int Move2_PP { get; set; }
public abstract int Move3_PP { get; set; }
public abstract int Move4_PP { get; set; }
public abstract int Move1_PPUps { get; set; }
public abstract int Move2_PPUps { get; set; }
public abstract int Move3_PPUps { get; set; }
public abstract int Move4_PPUps { get; set; }
public abstract int EV_HP { get; set; }
public abstract int EV_ATK { get; set; }
public abstract int EV_DEF { get; set; }
public abstract int EV_SPE { get; set; }
public abstract int EV_SPA { get; set; }
public abstract int EV_SPD { get; set; }
public abstract int IV_HP { get; set; }
public abstract int IV_ATK { get; set; }
public abstract int IV_DEF { get; set; }
public abstract int IV_SPE { get; set; }
public abstract int IV_SPA { get; set; }
public abstract int IV_SPD { get; set; }
public abstract int Stat_Level { get; set; }
public abstract int Stat_HPMax { get; set; }
public abstract int Stat_HPCurrent { get; set; }
public abstract int Stat_ATK { get; set; }
public abstract int Stat_DEF { get; set; }
public abstract int Stat_SPE { get; set; }
public abstract int Stat_SPA { get; set; }
public abstract int Stat_SPD { get; set; }
// Hidden Properties
public abstract int Version { get; set; }
public abstract int SID { get; set; }
public abstract int PKRS_Strain { get; set; }
public abstract int PKRS_Days { get; set; }
public abstract uint EncryptionConstant { get; set; }
public abstract uint PID { get; set; }
public abstract ushort Sanity { get; set; }
public abstract ushort Checksum { get; set; }
// Misc Properties
public abstract int Language { get; set; }
public abstract bool FatefulEncounter { get; set; }
public abstract int TSV { get; }
public abstract int PSV { get; }
public abstract int Characteristic { get; }
public abstract int MarkValue { get; protected set; }
public abstract int Met_Location { get; set; }
public abstract int Egg_Location { get; set; }
public abstract int OT_Friendship { get; set; }
public virtual bool Japanese => Language == (int)LanguageID.Japanese;
public virtual bool Korean => Language == (int)LanguageID.Korean;
public virtual bool Chinese => Language == (int)LanguageID.ChineseS || Language == (int)LanguageID.ChineseT;
// Future Properties
public virtual int Met_Year { get => 0; set { } }
public virtual int Met_Month { get => 0; set { } }
public virtual int Met_Day { get => 0; set { } }
public virtual string HT_Name { get; set; }
public virtual int HT_Gender { get; set; }
public virtual int HT_Affection { get; set; }
public virtual int HT_Friendship { get; set; }
public virtual int HT_Memory { get; set; }
public virtual int HT_TextVar { get; set; }
public virtual int HT_Feeling { get; set; }
public virtual int HT_Intensity { get; set; }
public virtual int OT_Memory { get; set; }
public virtual int OT_TextVar { get; set; }
public virtual int OT_Feeling { get; set; }
public virtual int OT_Intensity { get; set; }
public virtual byte Enjoyment { get; set; }
public virtual byte Fullness { get; set; }
public virtual int AbilityNumber { get; set; }
public virtual int Country { get; set; }
public virtual int Region { get; set; }
public virtual int ConsoleRegion { get; set; }
/// <summary>
/// The date the Pokémon was met.
/// </summary>
/// <returns>
/// A DateTime representing the date the Pokémon was met.
/// Returns null if either the <see cref="PKM"/> format does not support dates or the stored date is invalid.</returns>
/// <remarks>
/// Not all <see cref="PKM"/> types support the <see cref="MetDate"/> property. In these cases, this property will return null.
/// If null is assigned to this property, it will be cleared.
/// </remarks>
public DateTime? MetDate
{
get
{
// Check to see if date is valid
if (!Util.IsDateValid(2000 + Met_Year, Met_Month, Met_Day))
return null;
return new DateTime(2000 + Met_Year, Met_Month, Met_Day);
}
set
{
if (value.HasValue)
{
// Only update the properties if a value is provided.
Met_Year = value.Value.Year - 2000;
Met_Month = value.Value.Month;
Met_Day = value.Value.Day;
}
else
{
// Clear the Met Date.
// If code tries to access MetDate again, null will be returned.
Met_Year = 0;
Met_Month = 0;
Met_Day = 0;
}
}
}
public virtual int Egg_Year { get => 0; set { } }
public virtual int Egg_Month { get => 0; set { } }
public virtual int Egg_Day { get => 0; set { } }
/// <summary>
/// The date a Pokémon was met as an egg.
/// </summary>
/// <returns>
/// A DateTime representing the date the Pokémon was met as an egg.
/// Returns null if either the <see cref="PKM"/> format does not support dates or the stored date is invalid.</returns>
/// <remarks>
/// Not all <see cref="PKM"/> types support the <see cref="EggMetDate"/> property. In these cases, this property will return null.
/// If null is assigned to this property, it will be cleared.
/// </remarks>
public DateTime? EggMetDate
{
get
{
// Check to see if date is valid
if (!Util.IsDateValid(2000 + Egg_Year, Egg_Month, Egg_Day))
return null;
return new DateTime(2000 + Egg_Year, Egg_Month, Egg_Day);
}
set
{
if (value.HasValue)
{
// Only update the properties if a value is provided.
Egg_Year = value.Value.Year - 2000;
Egg_Month = value.Value.Month;
Egg_Day = value.Value.Day;
}
else
{
// Clear the Met Date.
// If code tries to access MetDate again, null will be returned.
Egg_Year = 0;
Egg_Month = 0;
Egg_Day = 0;
}
}
}
public virtual int OT_Affection { get => 0; set { } }
public virtual int RelearnMove1 { get => 0; set { } }
public virtual int RelearnMove2 { get => 0; set { } }
public virtual int RelearnMove3 { get => 0; set { } }
public virtual int RelearnMove4 { get => 0; set { } }
public virtual int EncounterType { get => 0; set { } }
// Exposed but not Present in all
public abstract int CurrentHandler { get; set; }
// Maximums
public abstract int MaxMoveID { get; }
public abstract int MaxSpeciesID { get; }
public abstract int MaxItemID { get; }
public abstract int MaxAbilityID { get; }
public abstract int MaxBallID { get; }
public abstract int MaxGameID { get; }
public virtual int MinGameID => 0;
public abstract int MaxIV { get; }
public abstract int MaxEV { get; }
public abstract int OTLength { get; }
public abstract int NickLength { get; }
// Derived
public int SpecForm { get => Species + (AltForm << 11); set { Species = value & 0x7FF; AltForm = value >> 11; } }
public virtual int SpriteItem => HeldItem;
public virtual bool IsShiny => TSV == PSV;
public virtual bool Locked { get => false; set { } }
public int TrainerID7 { get => (int)((uint)(TID | (SID << 16)) % 1000000); set => SetID7(TrainerSID7, value); }
public int TrainerSID7 { get => (int)((uint)(TID | (SID << 16)) / 1000000); set => SetID7(value, TrainerID7); }
private void SetID7(int sid7, int tid7)
{
var oid = (sid7 * 1_000_000) + (tid7 % 1_000_000);
TID = (ushort)oid;
SID = oid >> 16;
}
public bool VC2 => Version >= 39 && Version <= 41;
public bool VC1 => Version >= 35 && Version <= 38;
public bool Horohoro => Version == 34;
public bool E => Version == (int)GameVersion.E;
public bool FRLG => Version == (int)GameVersion.FR || Version == (int)GameVersion.LG;
public bool Pt => (int)GameVersion.Pt == Version;
public bool HGSS => Version == (int)GameVersion.HG || Version == (int)GameVersion.SS;
public bool BW => Version == (int)GameVersion.B || Version == (int)GameVersion.W;
public bool B2W2 => Version == (int)GameVersion.B2 || Version == (int)GameVersion.W2;
public bool XY => Version == (int)GameVersion.X || Version == (int)GameVersion.Y;
public bool AO => Version == (int)GameVersion.AS || Version == (int)GameVersion.OR;
public bool SM => Version == (int)GameVersion.SN || Version == (int)GameVersion.MN;
public bool USUM => Version == (int)GameVersion.US || Version == (int)GameVersion.UM;
public bool GG => Version == (int)GameVersion.GP || Version == (int)GameVersion.GE || Version == (int)GameVersion.GO;
protected bool PtHGSS => Pt || HGSS;
public bool VC => VC1 || VC2;
public bool Gen7 => (Version >= 30 && Version <= 33) || GG;
public bool Gen6 => Version >= 24 && Version <= 29;
public bool Gen5 => Version >= 20 && Version <= 23;
public bool Gen4 => Version >= 7 && Version <= 12 && Version != 9;
public bool Gen3 => (Version >= 1 && Version <= 5) || Version == 15;
public bool Gen2 => Version == (int)GameVersion.GSC;
public bool Gen1 => Version == (int)GameVersion.RBY;
public bool GenU => !(Gen7 || Gen6 || Gen5 || Gen4 || Gen3 || Gen2 || Gen1 || VC);
public int GenNumber
{
get
{
if (Gen7) return 7;
if (Gen6) return 6;
if (Gen5) return 5;
if (Gen4) return 4;
if (Gen3) return 3;
if (Gen2) return Format; // 2
if (Gen1) return Format; // 1
if (VC1) return 1;
if (VC2) return 2;
return -1;
}
}
public int DebutGeneration => Legal.GetDebutGeneration(Species);
public bool PKRS_Infected => PKRS_Strain > 0;
public bool PKRS_Cured => PKRS_Days == 0 && PKRS_Strain > 0;
public virtual bool ChecksumValid => Checksum == CalculateChecksum();
public int CurrentLevel { get => Experience.GetLevel(EXP, Species, AltForm); set => EXP = Experience.GetEXP(Stat_Level = value, Species, AltForm); }
public int MarkCircle { get => Markings[0]; set { var marks = Markings; marks[0] = value; Markings = marks; } }
public int MarkTriangle { get => Markings[1]; set { var marks = Markings; marks[1] = value; Markings = marks; } }
public int MarkSquare { get => Markings[2]; set { var marks = Markings; marks[2] = value; Markings = marks; } }
public int MarkHeart { get => Markings[3]; set { var marks = Markings; marks[3] = value; Markings = marks; } }
public int MarkStar { get => Markings[4]; set { var marks = Markings; marks[4] = value; Markings = marks; } }
public int MarkDiamond { get => Markings[5]; set { var marks = Markings; marks[5] = value; Markings = marks; } }
public int IVTotal => IV_HP + IV_ATK + IV_DEF + IV_SPA + IV_SPD + IV_SPE;
public int EVTotal => EV_HP + EV_ATK + EV_DEF + EV_SPA + EV_SPD + EV_SPE;
public string[] QRText => this.GetQRLines();
public string FileName => $"{FileNameWithoutExtension}.{Extension}";
public virtual string FileNameWithoutExtension
{
get
{
string form = AltForm > 0 ? $"-{AltForm:00}" : "";
string star = IsShiny ? " ★" : "";
return $"{Species:000}{form}{star} - {Nickname} - {Checksum:X4}{EncryptionConstant:X8}";
}
}
public int[] IVs
{
get => new[] { IV_HP, IV_ATK, IV_DEF, IV_SPE, IV_SPA, IV_SPD };
set
{
if (value?.Length != 6) return;
IV_HP = value[0]; IV_ATK = value[1]; IV_DEF = value[2];
IV_SPE = value[3]; IV_SPA = value[4]; IV_SPD = value[5];
}
}
public int[] EVs
{
get => new[] { EV_HP, EV_ATK, EV_DEF, EV_SPE, EV_SPA, EV_SPD };
set
{
if (value?.Length != 6) return;
EV_HP = value[0]; EV_ATK = value[1]; EV_DEF = value[2];
EV_SPE = value[3]; EV_SPA = value[4]; EV_SPD = value[5];
}
}
public int[] Stats
{
get => new[] { Stat_HPCurrent, Stat_ATK, Stat_DEF, Stat_SPE, Stat_SPA, Stat_SPD };
set
{
if (value?.Length != 6)
return;
Stat_HPCurrent = value[0]; Stat_ATK = value[1]; Stat_DEF = value[2];
Stat_SPE = value[3]; Stat_SPA = value[4]; Stat_SPD = value[5];
}
}
public int[] Moves
{
get => new[] { Move1, Move2, Move3, Move4 };
set
{
Move1 = value.Length > 0 ? value[0] : 0;
Move2 = value.Length > 1 ? value[1] : 0;
Move3 = value.Length > 2 ? value[2] : 0;
Move4 = value.Length > 3 ? value[3] : 0;
}
}
public int[] RelearnMoves
{
get => new[] { RelearnMove1, RelearnMove2, RelearnMove3, RelearnMove4 };
set
{
RelearnMove1 = value.Length > 0 ? value[0] : 0;
RelearnMove2 = value.Length > 1 ? value[1] : 0;
RelearnMove3 = value.Length > 2 ? value[2] : 0;
RelearnMove4 = value.Length > 3 ? value[3] : 0;
}
}
public int PIDAbility
{
get
{
if (GenNumber > 5 || Format > 5)
return -1;
if (Version == (int) GameVersion.CXD)
return Array.IndexOf(PersonalInfo.Abilities, Ability);
return (int)((Gen5 ? PID >> 16 : PID) & 1);
}
}
public virtual int[] Markings
{
get
{
int[] mark = new int[8];
for (int i = 0; i < 8; i++)
mark[i] = (MarkValue >> i) & 1;
return mark;
}
set
{
if (value.Length > 8)
return;
byte b = 0;
for (int i = 0; i < value.Length; i++)
b |= (byte)(Math.Min(value[i], 1) << i);
MarkValue = b;
}
}
protected static int GetHiddenPowerBitVal(int[] ivs)
{
int sum = 0;
for (int i = 0; i < ivs.Length; i++)
sum |= (ivs[i] & 1) << i;
return sum;
}
private int HPVal => GetHiddenPowerBitVal(IVs);
public virtual int HPPower => Format < 6 ? (40 *HPVal/63) + 30 : 60;
public virtual int HPType
{
get => 15 * HPVal / 63;
set
{
IV_HP = (IV_HP & ~1) + PKX.hpivs[value, 0];
IV_ATK = (IV_ATK & ~1) + PKX.hpivs[value, 1];
IV_DEF = (IV_DEF & ~1) + PKX.hpivs[value, 2];
IV_SPE = (IV_SPE & ~1) + PKX.hpivs[value, 3];
IV_SPA = (IV_SPA & ~1) + PKX.hpivs[value, 4];
IV_SPD = (IV_SPD & ~1) + PKX.hpivs[value, 5];
}
}
// Legality Extensions
public TradebackType TradebackStatus { get; set; } = TradebackType.Any;
public bool Gen1_NotTradeback => TradebackStatus == TradebackType.Gen1_NotTradeback;
public bool Gen2_NotTradeback => TradebackStatus == TradebackType.Gen2_NotTradeback;
public virtual bool WasLink => false;
private bool _WasEgg;
public bool WasEgg
{
get
{
switch (GenNumber)
{
case 4: return Legal.EggLocations4.Contains(Egg_Location) || (Species == 490 && Egg_Location == 3001) || (Egg_Location == 3002 && PtHGSS); // faraway
case 5: return Legal.EggLocations5.Contains(Egg_Location);
case 6: return Legal.EggLocations6.Contains(Egg_Location);
case 7: return Legal.EggLocations7.Contains(Egg_Location);
}
// Gen 1/2 and pal park Gen 3
return _WasEgg;
}
set => _WasEgg = value;
}
public bool WasBredEgg
{
get
{
int loc = Egg_Location;
switch (GenNumber)
{
case 4: return loc == 02000 || loc == 02002 || (loc == 03002 && PtHGSS); // faraway
case 5: return loc == 60002 || loc == 30003;
case 6:
case 7: return loc == 60002 || loc == 30002;
default: return _WasEgg; // Gen 1/2 and pal park Gen 3
}
}
set => _WasEgg = value;
}
public virtual bool WasGiftEgg
{
get
{
if (!WasEgg) return false;
switch (GenNumber)
{
case 4: return Legal.GiftEggLocation4.Contains(Egg_Location) || (Egg_Location == 3002 && HGSS); // faraway
case 5: return Egg_Location == 60003;
case 6:
case 7: return Egg_Location == 60004;
}
return false;
}
}
public virtual bool WasEvent => (Met_Location > 40000 && Met_Location < 50000) || FatefulEncounter;
public virtual bool WasEventEgg => Gen4 ? WasEgg && Species == 490 : ((Egg_Location > 40000 && Egg_Location < 50000) || (FatefulEncounter && Egg_Location > 0)) && Met_Level == 1;
public bool WasTradedEgg => Egg_Location == GetTradedEggLocation();
public bool IsTradedEgg => Met_Location == GetTradedEggLocation();
private int GetTradedEggLocation()
{
switch (GenNumber)
{
case 4: return 2002;
case 5: return 30003;
default: return 30002;
}
}
public virtual bool WasIngameTrade => Met_Location == 30001 || (Gen4 && Egg_Location == 2001);
public virtual bool IsUntraded => false;
public virtual bool IsNative => GenNumber == Format;
public virtual bool IsOriginValid => Species <= Legal.GetMaxSpeciesOrigin(Format);
public virtual bool SecretSuperTrainingUnlocked { get => false; set { } }
public virtual bool SecretSuperTrainingComplete { get => false; set { } }
public virtual int SuperTrainingMedalCount(int maxCount = 30) => 0;
/// <summary>
/// Checks if the <see cref="PKM"/> could inhabit a set of games.
/// </summary>
/// <param name="Generation">Set of games.</param>
/// <param name="species"></param>
/// <returns>True if could inhabit, False if not.</returns>
public bool InhabitedGeneration(int Generation, int species = -1)
{
if (species < 0)
species = Species;
if (Format == Generation)
return true;
if (!IsOriginValid)
return false;
// Sanity Check Species ID
if (Legal.GetMaxSpeciesOrigin(Generation) < species && !Legal.GetFutureGenEvolutions(Generation).Contains(species))
return false;
// Trade generation 1 -> 2
if (Format == 2 && Generation == 1 && !Gen2_NotTradeback)
return true;
// Trade generation 2 -> 1
if (Format == 1 && Generation == 2 && !Gen1_NotTradeback)
return true;
if (Format < Generation)
return false; // Future
int gen = GenNumber;
switch (Generation)
{
case 1: return Format == 1 || VC; // species compat checked via sanity above
case 2: return Format == 2 || VC;
case 3: return Gen3;
case 4: return 3 <= gen && gen <= 4;
case 5: return 3 <= gen && gen <= 5;
case 6: return 3 <= gen && gen <= 6;
case 7: return (3 <= gen && gen <= 7) || VC;
default:
return false;
}
}
/// <summary>
/// Checks if the PKM has its original met location.
/// </summary>
/// <returns>Returns false if the Met Location has been overwritten via generational transfer.</returns>
public virtual bool HasOriginalMetLocation => !(Format < 3 || VC || (GenNumber <= 4 && Format != GenNumber));
/// <summary>
/// Checks if the current <see cref="Gender"/> is valid.
/// </summary>
/// <returns>True if valid, False if invalid.</returns>
public virtual bool IsGenderValid()
{
int gender = Gender;
int gv = PersonalInfo.Gender;
if (gv == 255)
return gender == 2;
if (gv == 254)
return gender == 1;
if (gv == 0)
return gender == 0;
int gen = GenNumber;
if (2 >= gen || gen >= 6)
return gender == (gender & 1);
return gender == PKX.GetGenderFromPIDAndRatio(PID, gv);
}
/// <summary>
/// Updates the checksum of the <see cref="PKM"/>.
/// </summary>
public void RefreshChecksum() => Checksum = CalculateChecksum();
/// <summary>
/// Reorders moves and fixes PP if necessary.
/// </summary>
public void FixMoves()
{
ReorderMoves();
if (Move1 == 0) Move1_PP = Move1_PPUps = 0;
if (Move2 == 0) Move2_PP = Move2_PPUps = 0;
if (Move3 == 0) Move3_PP = Move3_PPUps = 0;
if (Move4 == 0) Move4_PP = Move4_PPUps = 0;
}
/// <summary>
/// Reorders moves to put Empty entries last.
/// </summary>
private void ReorderMoves()
{
if (Move4 != 0 && Move3 == 0)
{
Move3 = Move4;
Move3_PP = Move4_PP;
Move3_PPUps = Move4_PPUps;
Move4 = 0;
}
if (Move3 != 0 && Move2 == 0)
{
Move2 = Move3;
Move2_PP = Move3_PP;
Move2_PPUps = Move3_PPUps;
Move3 = 0;
ReorderMoves();
}
if (Move2 != 0 && Move1 == 0)
{
Move1 = Move2;
Move1_PP = Move2_PP;
Move1_PPUps = Move2_PPUps;
Move2 = 0;
ReorderMoves();
}
}
/// <summary>
/// Applies the desired Ability option.
/// </summary>
/// <param name="n">Ability Number (0/1/2)</param>
public void RefreshAbility(int n)
{
AbilityNumber = 1 << n;
int[] abilities = PersonalInfo.Abilities;
if (n < abilities.Length)
{
if (abilities[n] == abilities[0])
n = 0;
Ability = abilities[n];
}
if (this is PK5 pk5)
pk5.HiddenAbility = n == 2;
}
/// <summary>
/// Gets the IV Judge Rating value.
/// </summary>
/// <remarks>IV Judge scales his response 0 (worst) to 3 (best).</remarks>
public int PotentialRating
{
get
{
int ivTotal = IVTotal;
if (ivTotal <= 90)
return 0;
if (ivTotal <= 120)
return 1;
return ivTotal <= 150 ? 2 : 3;
}
}
/// <summary>
/// Gets the current Battle Stats.
/// </summary>
/// <param name="p"><see cref="PersonalInfo"/> entry containing Base Stat Info</param>
/// <returns>Battle Stats (H/A/B/S/C/D)</returns>
public virtual ushort[] GetStats(PersonalInfo p)
{
int level = CurrentLevel;
ushort[] Stats = this is IHyperTrain t ? GetStats(p, t, level) : GetStats(p, level);
// Account for nature
PKX.ModifyStatsForNature(Stats, Nature);
return Stats;
}
private ushort[] GetStats(PersonalInfo p, IHyperTrain t, int level)
{
ushort[] Stats = new ushort[6];
Stats[0] = (ushort)(p.HP == 1 ? 1 : (((t.HT_HP ? 31 : IV_HP) + (2 * p.HP) + (EV_HP / 4) + 100) * level / 100) + 10);
Stats[1] = (ushort)((((t.HT_ATK ? 31 : IV_ATK) + (2 * p.ATK) + (EV_ATK / 4)) * level / 100) + 5);
Stats[2] = (ushort)((((t.HT_DEF ? 31 : IV_DEF) + (2 * p.DEF) + (EV_DEF / 4)) * level / 100) + 5);
Stats[4] = (ushort)((((t.HT_SPA ? 31 : IV_SPA) + (2 * p.SPA) + (EV_SPA / 4)) * level / 100) + 5);
Stats[5] = (ushort)((((t.HT_SPD ? 31 : IV_SPD) + (2 * p.SPD) + (EV_SPD / 4)) * level / 100) + 5);
Stats[3] = (ushort)((((t.HT_SPE ? 31 : IV_SPE) + (2 * p.SPE) + (EV_SPE / 4)) * level / 100) + 5);
return Stats;
}
private ushort[] GetStats(PersonalInfo p, int level)
{
ushort[] Stats = new ushort[6];
Stats[0] = (ushort)(p.HP == 1 ? 1 : ((IV_HP + (2 * p.HP) + (EV_HP / 4) + 100) * level / 100) + 10);
Stats[1] = (ushort)(((IV_ATK + (2 * p.ATK) + (EV_ATK / 4)) * level / 100) + 5);
Stats[2] = (ushort)(((IV_DEF + (2 * p.DEF) + (EV_DEF / 4)) * level / 100) + 5);
Stats[4] = (ushort)(((IV_SPA + (2 * p.SPA) + (EV_SPA / 4)) * level / 100) + 5);
Stats[5] = (ushort)(((IV_SPD + (2 * p.SPD) + (EV_SPD / 4)) * level / 100) + 5);
Stats[3] = (ushort)(((IV_SPE + (2 * p.SPE) + (EV_SPE / 4)) * level / 100) + 5);
return Stats;
}
/// <summary>
/// Applies the specified stats to the <see cref="PKM"/>.
/// </summary>
/// <param name="Stats">Battle Stats (H/A/B/S/C/D)</param>
public void SetStats(ushort[] Stats)
{
Stat_HPMax = Stat_HPCurrent = Stats[0];
Stat_ATK = Stats[1];
Stat_DEF = Stats[2];
Stat_SPE = Stats[3];
Stat_SPA = Stats[4];
Stat_SPD = Stats[5];
}
/// <summary>
/// Checks if the <see cref="PKM"/> can hold its <see cref="HeldItem"/>.
/// </summary>
/// <param name="ValidArray">Items that the <see cref="PKM"/> can hold.</param>
/// <returns>True/False if the <see cref="PKM"/> can hold its <see cref="HeldItem"/>.</returns>
public virtual bool CanHoldItem(ushort[] ValidArray) => ValidArray.Contains((ushort)HeldItem);
/// <summary>
/// Deep clones the <see cref="PKM"/> object. The clone will not have any shared resources with the source.
/// </summary>
/// <returns>Cloned <see cref="PKM"/> object</returns>
public abstract PKM Clone();
/// <summary>
/// Sets Link Trade data for an <see cref="IsEgg"/>.
/// </summary>
/// <param name="day">Day the <see cref="PKM"/> was traded.</param>
/// <param name="month">Month the <see cref="PKM"/> was traded.</param>
/// <param name="y">Day the <see cref="PKM"/> was traded.</param>
/// <param name="location">Link Trade location value.</param>
protected void SetLinkTradeEgg(int day, int month, int y, int location = 30002)
{
Met_Day = day;
Met_Month = month;
Met_Year = y - 2000;
Met_Location = location;
}
/// <summary>
/// Gets the PP of a Move ID with consideration of the amount of PP Ups applied.
/// </summary>
/// <param name="move">Move ID</param>
/// <param name="ppup">PP Ups count</param>
/// <returns>Current PP for the move.</returns>
public virtual int GetMovePP(int move, int ppup) => GetBasePP(move) * (5 + ppup) / 5;
/// <summary>
/// Gets the base PP of a move ID depending on the <see cref="PKM"/>'s format.
/// </summary>
/// <param name="move">Move ID</param>
/// <returns>Amount of PP the move has by default (no PP Ups).</returns>
private int GetBasePP(int move)
{
var pptable = Legal.GetPPTable(this, Format);
if (move >= pptable.Count)
move = 0;
return pptable[move];
}
/// <summary>
/// Applies a shiny <see cref="PID"/> to the <see cref="PKM"/>.
/// </summary>
/// <remarks>
/// If a <see cref="PKM"/> originated in a generation prior to Generation 6, the <see cref="EncryptionConstant"/> is updated.
/// If a <see cref="PKM"/> is in the <see cref="_K12"/> format, it will update the <see cref="IVs"/> instead.
/// </remarks>
public virtual void SetShiny()
{
while (!IsShiny)
PID = PKX.GetRandomPID(Species, Gender, Version, Nature, AltForm, PID);
if (Format >= 6 && 3 <= GenNumber && GenNumber <= 5)
EncryptionConstant = PID;
}
/// <summary>
/// Applies a shiny <see cref="SID"/> to the <see cref="PKM"/>.
/// </summary>
public void SetShinySID()
{
if (IsShiny) return;
var xor = TID ^ (PID >> 16) ^ (PID & 0xFFFF);
SID = (int)(xor & 0xFFF8) | Util.Rand.Next(8);
}
/// <summary>
/// Applies a <see cref="PID"/> to the <see cref="PKM"/> according to the specified <see cref="Gender"/>.
/// </summary>
/// <param name="gender"><see cref="Gender"/> to apply</param>
/// <remarks>
/// If a <see cref="PKM"/> originated in a generation prior to Generation 6, the <see cref="EncryptionConstant"/> is updated.
/// </remarks>
public void SetPIDGender(int gender)
{
do PID = PKX.GetRandomPID(Species, gender, Version, Nature, AltForm, PID); while (IsShiny);
if (Format >= 6 && 3 <= GenNumber && GenNumber <= 5)
EncryptionConstant = PID;
}
/// <summary>
/// Applies a <see cref="PID"/> to the <see cref="PKM"/> according to the specified <see cref="Gender"/>.
/// </summary>
/// <param name="nature"><see cref="Nature"/> to apply</param>
/// <remarks>
/// If a <see cref="PKM"/> originated in a generation prior to Generation 6, the <see cref="EncryptionConstant"/> is updated.
/// </remarks>
public void SetPIDNature(int nature)
{
do PID = PKX.GetRandomPID(Species, Gender, Version, nature, AltForm, PID); while (IsShiny);
if (Format >= 6 && 3 <= GenNumber && GenNumber <= 5)
EncryptionConstant = PID;
}
/// <summary>
/// Applies a <see cref="PID"/> to the <see cref="PKM"/> according to the specified <see cref="AltForm"/>.
/// </summary>
/// <param name="form"><see cref="AltForm"/> to apply</param>
/// <remarks>
/// This method should only be used for Unown originating in Generation 3 games.
/// If a <see cref="PKM"/> originated in a generation prior to Generation 6, the <see cref="EncryptionConstant"/> is updated.
/// </remarks>
public void SetPIDUnown3(int form)
{
do PID = Util.Rand32(); while (PKX.GetUnownForm(PID) != form);
if (Format >= 6 && 3 <= GenNumber && GenNumber <= 5)
EncryptionConstant = PID;
}
/// <summary>
/// Randomizes the IVs within game constraints.
/// </summary>
/// <param name="flawless">Count of flawless IVs to set. If none provided, a count will be detected.</param>
/// <returns>Randomized IVs if desired.</returns>
public int[] SetRandomIVs(int? flawless = null)
{
int[] ivs = new int[6];
for (int i = 0; i < 6; i++)
ivs[i] = Util.Rand.Next(MaxIV + 1);
int count = flawless ?? GetFlawlessIVCount();
if (count != 0)
{
for (int i = 0; i < count; i++)
ivs[i] = MaxIV;
Util.Shuffle(ivs); // Randomize IV order
}
IVs = ivs;
return ivs;
}
/// <summary>
/// Randomizes the IVs within game constraints.
/// </summary>
/// <param name="template">IV template to generate from</param>
/// <param name="flawless">Count of flawless IVs to set. If none provided, a count will be detected.</param>
/// <returns>Randomized IVs if desired.</returns>
public int[] SetRandomIVs(int[] template, int? flawless = null)
{
int count = flawless ?? GetFlawlessIVCount();
int[] ivs = new int[6];
do
{
for (int i = 0; i < 6; i++)
ivs[i] = template[i] < 0 ? Util.Rand.Next(MaxIV + 1) : template[i];
} while (ivs.Count(z => z == MaxIV) < count);
IVs = ivs;
return ivs;
}
/// <summary>
/// Gets the amount of flawless IVs that the <see cref="PKM"/> should have.
/// </summary>
/// <returns>Count of IVs that should be max.</returns>
public int GetFlawlessIVCount()
{
if (GenNumber >= 6 && (Legal.Legends.Contains(Species) || Legal.SubLegends.Contains(Species)))
return 3;
if (XY)
{
if (Met_Location == 148 && Met_Level == 30) // Friend Safari
return 2;
if (PersonalInfo.EggGroup1 == 15) // Undiscovered
return 3;
}
if (VC)
return Species == 151 || Species == 251 ? 5 : 3;
return 0;
}
/// <summary>
/// Applies all shared properties from the current <see cref="PKM"/> to <see cref="Destination"/> <see cref="PKM"/>.
/// </summary>
/// <param name="Destination"><see cref="PKM"/> that receives property values.</param>
public void TransferPropertiesWithReflection(PKM Destination)
{
// Only transfer declared properties not defined in PKM.cs but in the actual type
var SourceProperties = ReflectUtil.GetPropertiesCanWritePublicDeclared(GetType());
var DestinationProperties = ReflectUtil.GetPropertiesCanWritePublicDeclared(Destination.GetType());
// Transfer properties in the order they are defined in the destination PKM format for best conversion
var shared = DestinationProperties.Intersect(SourceProperties);
foreach (string property in shared)
{
BatchEditing.TryGetHasProperty(Destination, property, out var src);
var prop = src.GetValue(this);
if (prop != null && !(prop is byte[]) && BatchEditing.TryGetHasProperty(Destination, property, out var pi))
ReflectUtil.SetValue(pi, Destination, prop);
}
}
/// <summary>
/// Clears moves that a <see cref="PKM"/> may have, possibly from a future generation.
/// </summary>
public void ClearInvalidMoves()
{
uint invalid = 0;
var moves = Moves;
for (var i = 0; i < moves.Length; i++)
{
if (moves[i] <= MaxMoveID)
continue;
invalid++;
moves[i] = 0;
}
if (invalid == 0)
return;
if (invalid == 4) // no moves remain
{
moves[0] = 1; // Pound
Move1_PP = GetMovePP(1, Move1_PPUps);
}
Moves = moves;
FixMoves();
}
}
}