diff --git a/PKHeX.Core/PKM/PK1.cs b/PKHeX.Core/PKM/PK1.cs index 191922886..7fcd2266e 100644 --- a/PKHeX.Core/PKM/PK1.cs +++ b/PKHeX.Core/PKM/PK1.cs @@ -367,7 +367,7 @@ namespace PKHeX.Core CurrentLevel = CurrentLevel, EXP = EXP, Met_Level = CurrentLevel, - Nature = (int) (EXP%25), + Nature = Experience.GetNatureVC(EXP), PID = Util.Rand32(), Ball = 4, MetDate = DateTime.Now, diff --git a/PKHeX.Core/PKM/PK2.cs b/PKHeX.Core/PKM/PK2.cs index 2c42f14d4..25e39d6d2 100644 --- a/PKHeX.Core/PKM/PK2.cs +++ b/PKHeX.Core/PKM/PK2.cs @@ -387,7 +387,7 @@ namespace PKHeX.Core CurrentLevel = CurrentLevel, EXP = EXP, Met_Level = CurrentLevel, - Nature = (int)(EXP % 25), + Nature = Experience.GetNatureVC(EXP), PID = Util.Rand32(), Ball = 4, MetDate = DateTime.Now, diff --git a/PKHeX.Core/PKM/Util/Experience.cs b/PKHeX.Core/PKM/Util/Experience.cs new file mode 100644 index 000000000..60cb76f69 --- /dev/null +++ b/PKHeX.Core/PKM/Util/Experience.cs @@ -0,0 +1,189 @@ +namespace PKHeX.Core +{ + public static class Experience + { + /// + /// Gets the current level of a species. + /// + /// National Dex number of the Pokémon. + /// Experience points + /// Current level of the species. + public static int GetLevel(int species, uint exp) + { + int growth = PKX.Personal[species].EXPGrowth; + int tl = 1; // Initial Level. Iterate upwards to find the level + while (ExpTable[++tl, growth] <= exp) + { + if (tl == 100) + return 100; + } + return --tl; + } + + /// + /// Gets the minimum Experience points for the specified level. + /// + /// Current level + /// National Dex number of the Pokémon. + /// Experience points needed to have specified level. + public static uint GetEXP(int level, int species) + { + if (level <= 1) + return 0; + if (level > 100) + level = 100; + return ExpTable[level, PKX.Personal[species].EXPGrowth]; + } + + /// + /// Gets the value for / entries based on the + /// + /// Current Experience + /// Nature ID () + public static int GetNatureVC(uint experience) => (int)(experience % 25); + + /// + /// Gets the amount of EXP to be earned until the next level-up occurs. + /// + /// Current Level + /// + /// Percentage [0,1.00) + public static uint GetEXPToLevelUp(int level, int species) + { + if (level >= 100) + return 0; + var growth = PKX.Personal[species].EXPGrowth; + var current = ExpTable[level, growth]; + var next = ExpTable[level + 1, growth]; + return next - current; + } + + /// + /// Gets a percentage for Experience Bar progress indication. + /// + /// Current Level + /// + /// Current Experience + /// Percentage [0,1.00) + public static double GetEXPToLevelUpPercentage(int level, int species, uint exp) + { + if (level >= 100) + return 0; + var growth = PKX.Personal[species].EXPGrowth; + var current = ExpTable[level, growth]; + var next = ExpTable[level + 1, growth]; + var amount = next - current; + double progress = exp - current; + return progress / amount; + } + + #region ExpTable + + private static readonly uint[,] ExpTable = + { + {0000000, 0000000, 0000000, 0000000, 0000000, 0000000}, + {0000000, 0000000, 0000000, 0000000, 0000000, 0000000}, + {0000008, 0000015, 0000004, 0000009, 0000006, 0000010}, + {0000027, 0000052, 0000013, 0000057, 0000021, 0000033}, + {0000064, 0000122, 0000032, 0000096, 0000051, 0000080}, + {0000125, 0000237, 0000065, 0000135, 0000100, 0000156}, + {0000216, 0000406, 0000112, 0000179, 0000172, 0000270}, + {0000343, 0000637, 0000178, 0000236, 0000274, 0000428}, + {0000512, 0000942, 0000276, 0000314, 0000409, 0000640}, + {0000729, 0001326, 0000393, 0000419, 0000583, 0000911}, + {0001000, 0001800, 0000540, 0000560, 0000800, 0001250}, + {0001331, 0002369, 0000745, 0000742, 0001064, 0001663}, + {0001728, 0003041, 0000967, 0000973, 0001382, 0002160}, + {0002197, 0003822, 0001230, 0001261, 0001757, 0002746}, + {0002744, 0004719, 0001591, 0001612, 0002195, 0003430}, + {0003375, 0005737, 0001957, 0002035, 0002700, 0004218}, + {0004096, 0006881, 0002457, 0002535, 0003276, 0005120}, + {0004913, 0008155, 0003046, 0003120, 0003930, 0006141}, + {0005832, 0009564, 0003732, 0003798, 0004665, 0007290}, + {0006859, 0011111, 0004526, 0004575, 0005487, 0008573}, + {0008000, 0012800, 0005440, 0005460, 0006400, 0010000}, + {0009261, 0014632, 0006482, 0006458, 0007408, 0011576}, + {0010648, 0016610, 0007666, 0007577, 0008518, 0013310}, + {0012167, 0018737, 0009003, 0008825, 0009733, 0015208}, + {0013824, 0021012, 0010506, 0010208, 0011059, 0017280}, + {0015625, 0023437, 0012187, 0011735, 0012500, 0019531}, + {0017576, 0026012, 0014060, 0013411, 0014060, 0021970}, + {0019683, 0028737, 0016140, 0015244, 0015746, 0024603}, + {0021952, 0031610, 0018439, 0017242, 0017561, 0027440}, + {0024389, 0034632, 0020974, 0019411, 0019511, 0030486}, + {0027000, 0037800, 0023760, 0021760, 0021600, 0033750}, + {0029791, 0041111, 0026811, 0024294, 0023832, 0037238}, + {0032768, 0044564, 0030146, 0027021, 0026214, 0040960}, + {0035937, 0048155, 0033780, 0029949, 0028749, 0044921}, + {0039304, 0051881, 0037731, 0033084, 0031443, 0049130}, + {0042875, 0055737, 0042017, 0036435, 0034300, 0053593}, + {0046656, 0059719, 0046656, 0040007, 0037324, 0058320}, + {0050653, 0063822, 0050653, 0043808, 0040522, 0063316}, + {0054872, 0068041, 0055969, 0047846, 0043897, 0068590}, + {0059319, 0072369, 0060505, 0052127, 0047455, 0074148}, + {0064000, 0076800, 0066560, 0056660, 0051200, 0080000}, + {0068921, 0081326, 0071677, 0061450, 0055136, 0086151}, + {0074088, 0085942, 0078533, 0066505, 0059270, 0092610}, + {0079507, 0090637, 0084277, 0071833, 0063605, 0099383}, + {0085184, 0095406, 0091998, 0077440, 0068147, 0106480}, + {0091125, 0100237, 0098415, 0083335, 0072900, 0113906}, + {0097336, 0105122, 0107069, 0089523, 0077868, 0121670}, + {0103823, 0110052, 0114205, 0096012, 0083058, 0129778}, + {0110592, 0115015, 0123863, 0102810, 0088473, 0138240}, + {0117649, 0120001, 0131766, 0109923, 0094119, 0147061}, + {0125000, 0125000, 0142500, 0117360, 0100000, 0156250}, + {0132651, 0131324, 0151222, 0125126, 0106120, 0165813}, + {0140608, 0137795, 0163105, 0133229, 0112486, 0175760}, + {0148877, 0144410, 0172697, 0141677, 0119101, 0186096}, + {0157464, 0151165, 0185807, 0150476, 0125971, 0196830}, + {0166375, 0158056, 0196322, 0159635, 0133100, 0207968}, + {0175616, 0165079, 0210739, 0169159, 0140492, 0219520}, + {0185193, 0172229, 0222231, 0179056, 0148154, 0231491}, + {0195112, 0179503, 0238036, 0189334, 0156089, 0243890}, + {0205379, 0186894, 0250562, 0199999, 0164303, 0256723}, + {0216000, 0194400, 0267840, 0211060, 0172800, 0270000}, + {0226981, 0202013, 0281456, 0222522, 0181584, 0283726}, + {0238328, 0209728, 0300293, 0234393, 0190662, 0297910}, + {0250047, 0217540, 0315059, 0246681, 0200037, 0312558}, + {0262144, 0225443, 0335544, 0259392, 0209715, 0327680}, + {0274625, 0233431, 0351520, 0272535, 0219700, 0343281}, + {0287496, 0241496, 0373744, 0286115, 0229996, 0359370}, + {0300763, 0249633, 0390991, 0300140, 0240610, 0375953}, + {0314432, 0257834, 0415050, 0314618, 0251545, 0393040}, + {0328509, 0267406, 0433631, 0329555, 0262807, 0410636}, + {0343000, 0276458, 0459620, 0344960, 0274400, 0428750}, + {0357911, 0286328, 0479600, 0360838, 0286328, 0447388}, + {0373248, 0296358, 0507617, 0377197, 0298598, 0466560}, + {0389017, 0305767, 0529063, 0394045, 0311213, 0486271}, + {0405224, 0316074, 0559209, 0411388, 0324179, 0506530}, + {0421875, 0326531, 0582187, 0429235, 0337500, 0527343}, + {0438976, 0336255, 0614566, 0447591, 0351180, 0548720}, + {0456533, 0346965, 0639146, 0466464, 0365226, 0570666}, + {0474552, 0357812, 0673863, 0485862, 0379641, 0593190}, + {0493039, 0367807, 0700115, 0505791, 0394431, 0616298}, + {0512000, 0378880, 0737280, 0526260, 0409600, 0640000}, + {0531441, 0390077, 0765275, 0547274, 0425152, 0664301}, + {0551368, 0400293, 0804997, 0568841, 0441094, 0689210}, + {0571787, 0411686, 0834809, 0590969, 0457429, 0714733}, + {0592704, 0423190, 0877201, 0613664, 0474163, 0740880}, + {0614125, 0433572, 0908905, 0636935, 0491300, 0767656}, + {0636056, 0445239, 0954084, 0660787, 0508844, 0795070}, + {0658503, 0457001, 0987754, 0685228, 0526802, 0823128}, + {0681472, 0467489, 1035837, 0710266, 0545177, 0851840}, + {0704969, 0479378, 1071552, 0735907, 0563975, 0881211}, + {0729000, 0491346, 1122660, 0762160, 0583200, 0911250}, + {0753571, 0501878, 1160499, 0789030, 0602856, 0941963}, + {0778688, 0513934, 1214753, 0816525, 0622950, 0973360}, + {0804357, 0526049, 1254796, 0844653, 0643485, 1005446}, + {0830584, 0536557, 1312322, 0873420, 0664467, 1038230}, + {0857375, 0548720, 1354652, 0902835, 0685900, 1071718}, + {0884736, 0560922, 1415577, 0932903, 0707788, 1105920}, + {0912673, 0571333, 1460276, 0963632, 0730138, 1140841}, + {0941192, 0583539, 1524731, 0995030, 0752953, 1176490}, + {0970299, 0591882, 1571884, 1027103, 0776239, 1212873}, + {1000000, 0600000, 1640000, 1059860, 0800000, 1250000}, + }; + + #endregion + } +} diff --git a/PKHeX.Core/PKM/Util/PKX.cs b/PKHeX.Core/PKM/Util/PKX.cs index 2fcb26b41..ff2acc387 100644 --- a/PKHeX.Core/PKM/Util/PKX.cs +++ b/PKHeX.Core/PKM/Util/PKX.cs @@ -61,112 +61,6 @@ namespace PKHeX.Core public static uint LCRNG(uint seed) => RNG.LCRNG.Next(seed); public static uint LCRNG(ref uint seed) => seed = RNG.LCRNG.Next(seed); - #region ExpTable - private static readonly uint[,] ExpTable = - { - {0, 0, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0}, - {8, 15, 4, 9, 6, 10}, - {27, 52, 13, 57, 21, 33}, - {64, 122, 32, 96, 51, 80}, - {125, 237, 65, 135, 100, 156}, - {216, 406, 112, 179, 172, 270}, - {343, 637, 178, 236, 274, 428}, - {512, 942, 276, 314, 409, 640}, - {729, 1326, 393, 419, 583, 911}, - {1000, 1800, 540, 560, 800, 1250}, - {1331, 2369, 745, 742, 1064, 1663}, - {1728, 3041, 967, 973, 1382, 2160}, - {2197, 3822, 1230, 1261, 1757, 2746}, - {2744, 4719, 1591, 1612, 2195, 3430}, - {3375, 5737, 1957, 2035, 2700, 4218}, - {4096, 6881, 2457, 2535, 3276, 5120}, - {4913, 8155, 3046, 3120, 3930, 6141}, - {5832, 9564, 3732, 3798, 4665, 7290}, - {6859, 11111, 4526, 4575, 5487, 8573}, - {8000, 12800, 5440, 5460, 6400, 10000}, - {9261, 14632, 6482, 6458, 7408, 11576}, - {10648, 16610, 7666, 7577, 8518, 13310}, - {12167, 18737, 9003, 8825, 9733, 15208}, - {13824, 21012, 10506, 10208, 11059, 17280}, - {15625, 23437, 12187, 11735, 12500, 19531}, - {17576, 26012, 14060, 13411, 14060, 21970}, - {19683, 28737, 16140, 15244, 15746, 24603}, - {21952, 31610, 18439, 17242, 17561, 27440}, - {24389, 34632, 20974, 19411, 19511, 30486}, - {27000, 37800, 23760, 21760, 21600, 33750}, - {29791, 41111, 26811, 24294, 23832, 37238}, - {32768, 44564, 30146, 27021, 26214, 40960}, - {35937, 48155, 33780, 29949, 28749, 44921}, - {39304, 51881, 37731, 33084, 31443, 49130}, - {42875, 55737, 42017, 36435, 34300, 53593}, - {46656, 59719, 46656, 40007, 37324, 58320}, - {50653, 63822, 50653, 43808, 40522, 63316}, - {54872, 68041, 55969, 47846, 43897, 68590}, - {59319, 72369, 60505, 52127, 47455, 74148}, - {64000, 76800, 66560, 56660, 51200, 80000}, - {68921, 81326, 71677, 61450, 55136, 86151}, - {74088, 85942, 78533, 66505, 59270, 92610}, - {79507, 90637, 84277, 71833, 63605, 99383}, - {85184, 95406, 91998, 77440, 68147, 106480}, - {91125, 100237, 98415, 83335, 72900, 113906}, - {97336, 105122, 107069, 89523, 77868, 121670}, - {103823, 110052, 114205, 96012, 83058, 129778}, - {110592, 115015, 123863, 102810, 88473, 138240}, - {117649, 120001, 131766, 109923, 94119, 147061}, - {125000, 125000, 142500, 117360, 100000, 156250}, - {132651, 131324, 151222, 125126, 106120, 165813}, - {140608, 137795, 163105, 133229, 112486, 175760}, - {148877, 144410, 172697, 141677, 119101, 186096}, - {157464, 151165, 185807, 150476, 125971, 196830}, - {166375, 158056, 196322, 159635, 133100, 207968}, - {175616, 165079, 210739, 169159, 140492, 219520}, - {185193, 172229, 222231, 179056, 148154, 231491}, - {195112, 179503, 238036, 189334, 156089, 243890}, - {205379, 186894, 250562, 199999, 164303, 256723}, - {216000, 194400, 267840, 211060, 172800, 270000}, - {226981, 202013, 281456, 222522, 181584, 283726}, - {238328, 209728, 300293, 234393, 190662, 297910}, - {250047, 217540, 315059, 246681, 200037, 312558}, - {262144, 225443, 335544, 259392, 209715, 327680}, - {274625, 233431, 351520, 272535, 219700, 343281}, - {287496, 241496, 373744, 286115, 229996, 359370}, - {300763, 249633, 390991, 300140, 240610, 375953}, - {314432, 257834, 415050, 314618, 251545, 393040}, - {328509, 267406, 433631, 329555, 262807, 410636}, - {343000, 276458, 459620, 344960, 274400, 428750}, - {357911, 286328, 479600, 360838, 286328, 447388}, - {373248, 296358, 507617, 377197, 298598, 466560}, - {389017, 305767, 529063, 394045, 311213, 486271}, - {405224, 316074, 559209, 411388, 324179, 506530}, - {421875, 326531, 582187, 429235, 337500, 527343}, - {438976, 336255, 614566, 447591, 351180, 548720}, - {456533, 346965, 639146, 466464, 365226, 570666}, - {474552, 357812, 673863, 485862, 379641, 593190}, - {493039, 367807, 700115, 505791, 394431, 616298}, - {512000, 378880, 737280, 526260, 409600, 640000}, - {531441, 390077, 765275, 547274, 425152, 664301}, - {551368, 400293, 804997, 568841, 441094, 689210}, - {571787, 411686, 834809, 590969, 457429, 714733}, - {592704, 423190, 877201, 613664, 474163, 740880}, - {614125, 433572, 908905, 636935, 491300, 767656}, - {636056, 445239, 954084, 660787, 508844, 795070}, - {658503, 457001, 987754, 685228, 526802, 823128}, - {681472, 467489, 1035837, 710266, 545177, 851840}, - {704969, 479378, 1071552, 735907, 563975, 881211}, - {729000, 491346, 1122660, 762160, 583200, 911250}, - {753571, 501878, 1160499, 789030, 602856, 941963}, - {778688, 513934, 1214753, 816525, 622950, 973360}, - {804357, 526049, 1254796, 844653, 643485, 1005446}, - {830584, 536557, 1312322, 873420, 664467, 1038230}, - {857375, 548720, 1354652, 902835, 685900, 1071718}, - {884736, 560922, 1415577, 932903, 707788, 1105920}, - {912673, 571333, 1460276, 963632, 730138, 1140841}, - {941192, 583539, 1524731, 995030, 752953, 1176490}, - {970299, 591882, 1571884, 1027103, 776239, 1212873}, - {1000000, 600000, 1640000, 1059860, 800000, 1250000}, - }; - #endregion /// /// Species name lists indexed by the value. @@ -322,11 +216,7 @@ namespace PKHeX.Core /// Current level of the species. public static int GetLevel(int species, uint exp) { - int growth = Personal[species].EXPGrowth; - int tl = 1; // Initial Level. Iterate upwards to find the level - while (ExpTable[++tl, growth] <= exp) - if (tl == 100) return 100; - return --tl; + return Experience.GetLevel(species, exp); } /// @@ -337,9 +227,7 @@ namespace PKHeX.Core /// Experience points needed to have specified level. public static uint GetEXP(int level, int species) { - if (level <= 1) return 0; - if (level > 100) level = 100; - return ExpTable[level, Personal[species].EXPGrowth]; + return Experience.GetEXP(level, species); } /// diff --git a/PKHeX.WinForms/Controls/SAV Editor/SlotChangeManager.cs b/PKHeX.WinForms/Controls/SAV Editor/SlotChangeManager.cs index fba6dfac5..20b4d74b9 100644 --- a/PKHeX.WinForms/Controls/SAV Editor/SlotChangeManager.cs +++ b/PKHeX.WinForms/Controls/SAV Editor/SlotChangeManager.cs @@ -219,7 +219,7 @@ namespace PKHeX.WinForms.Controls } var set = new ShowdownSet(pk); if (pk.Format <= 2) // Nature preview from IVs - set.Nature = (int)(pk.EXP % 25); + set.Nature = Experience.GetNatureVC(pk.EXP); ShowSet.SetToolTip(pb, set.LocalizedText(Settings.Default.Language)); } @@ -280,7 +280,7 @@ namespace PKHeX.WinForms.Controls Array.Resize(ref dragdata, SAV.SIZE_STORED); PKM pkx = SAV.GetPKM(dragdata); string fn = pkx.FileName; fn = fn.Substring(0, fn.LastIndexOf('.')); - string filename = $"{fn}{(encrypt ? $".ek{pkx.Format}" : $".{pkx.Extension}")}"; + string filename = fn + (encrypt ? $".ek{pkx.Format}" : $".{pkx.Extension}"); // Make File string newfile = Path.Combine(Path.GetTempPath(), Util.CleanFileName(filename));