-
Notifications
You must be signed in to change notification settings - Fork 8
/
GaleDarknessRNGSystem.cpp
executable file
·558 lines (499 loc) · 15.7 KB
/
GaleDarknessRNGSystem.cpp
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
#include "GaleDarknessRNGSystem.h"
#include <array>
#include <string>
// HP stat of the quick battle team pokemon without IV and EV at level 100
static const std::array<std::array<int, 2>, 10> s_quickBattleTeamMaxBaseHPStat = {{{{322, 340}},
{{310, 290}},
{{210, 620}},
{{320, 230}},
{{310, 310}},
{{290, 310}},
{{290, 270}},
{{290, 250}},
{{320, 270}},
{{270, 230}}}};
static const int eeveeGenderRatio = 0x1f;
static const int eeveeBaseHpStat = 55;
static const int eeveeStartingLevel = 10;
static const int s_nbrPossiblePlayerTeams = 5;
static const int s_nbrPossibleEnemyTeams = 5;
// The minimum possible amount of naming screen animation render calls obtained by TASing the input
// of the naming screen when using a custom name.
static const int minNamingScreenAnimRenderCalls = 725;
// The minimum possible amount of frames spending between confirming a new game and confirming
// a custom name on the naming screen, obtained by TASing.
static const int minNamingScreenFrames = 116;
static bool s_palEnabled = false;
bool GaleDarknessRNGSystem::getPalEnabled()
{
return s_palEnabled;
}
void GaleDarknessRNGSystem::setPalEnabled(const bool palEnabled)
{
s_palEnabled = palEnabled;
}
std::vector<BaseRNGSystem::StartersPrediction>
GaleDarknessRNGSystem::predictStartersForNbrSeconds(u32 seed, const int nbrSeconds)
{
if (s_palEnabled)
{
std::vector<StartersPrediction> predictionsResult;
seed = rollRNGNamingScreenInit(seed);
BaseRNGSystem::StartersPrediction prediction = generateStarterPokemons(seed);
prediction.frameNumber = -1;
predictionsResult.push_back(prediction);
return predictionsResult;
}
return BaseRNGSystem::predictStartersForNbrSeconds(seed, nbrSeconds);
}
std::string GaleDarknessRNGSystem::getPrecalcFilename()
{
return "xd.precalc";
}
u32 inline GaleDarknessRNGSystem::generatePokemonPID(u32& seed, const u32 hTrainerId,
const u32 lTrainerId, const u32 dummyId,
u16* counter, const WantedShininess shininess,
const s8 wantedGender, const u32 genderRatio,
const s8 wantedNature)
{
u32 pid = 0;
bool goodId = false;
int nbrPidAttemptLeft = 3145728;
while (nbrPidAttemptLeft > 0)
{
// A personality ID is generated as candidate, high then low 16 bits
u32 hId = LCG(seed, counter) >> 16;
u32 lId = LCG(seed, counter) >> 16;
pid = (hId << 16) | (lId);
nbrPidAttemptLeft--;
// If we want a gender AND the gender of the pokemon is uncertain
if (wantedGender != -1 && !(genderRatio == 0xff || genderRatio == 0xfe || genderRatio == 0x00))
{
if (wantedGender == 2)
{
u8 pokemonIdGender = genderRatio > (dummyId & 0xff) ? 1 : 0;
u8 idGender = genderRatio > (pid & 0xff) ? 1 : 0;
if (pokemonIdGender != idGender)
continue;
}
else
{
u8 idGender = genderRatio > (pid & 0xff) ? 1 : 0;
if (idGender != wantedGender)
continue;
}
}
// Reroll until we have the correct nature in the perosnality ID
if (wantedNature != -1 && pid % 25 != wantedNature)
continue;
if (shininess != WantedShininess::any)
{
if (shininess == WantedShininess::notShiny)
{
if (!isPidShiny(lTrainerId, hTrainerId, pid))
return pid;
}
else if (shininess == WantedShininess::shiny)
{
if (isPidShiny(lTrainerId, hTrainerId, pid))
return pid;
}
}
else
{
return pid;
}
}
return pid;
}
std::array<u8, 6> inline GaleDarknessRNGSystem::generateEVs(u32& seed, const bool allowUnfilledEV,
const bool endPrematurely, u16* counter)
{
std::array<u8, 6> EVs = {0};
int sumEV = 0;
for (int j = 0; j < 101; j++)
{
sumEV = 0;
for (int k = 0; k < 6; k++)
{
EVs[k] += (LCG(seed, counter) >> 16) & 0xFF;
sumEV += EVs[k];
}
if (allowUnfilledEV)
{
if (sumEV <= 510)
{
return EVs;
}
else
{
if (j >= 100)
continue;
EVs.fill(0);
continue;
}
}
else if (sumEV == 510)
{
return EVs;
}
else if (sumEV <= 490)
{
continue;
}
else if (sumEV >= 530)
{
if (j >= 100)
continue;
EVs.fill(0);
continue;
}
else
{
break;
}
}
bool goodSum = false;
while (!goodSum && !endPrematurely)
{
for (int k = 0; k < 6; k++)
{
if (sumEV < 510)
{
if (EVs[k] < 255)
{
EVs[k]++;
sumEV++;
}
}
else if (sumEV > 510)
{
if (EVs[k] != 0)
{
EVs[k]--;
sumEV--;
}
}
if (sumEV == 510)
goodSum = true;
}
}
return EVs;
}
u32 GaleDarknessRNGSystem::rollRNGToBattleMenu(u32 seed, u16* counter)
{
// Before getting to the quick battle menu, the reason this is deterministic is because the only
// thing that is consequential is a dummy pokemon generation, but there's no criteria on the pid
// meaning the first pid generated will always be the one used
seed = LCGn(seed, 1139, counter);
// Some dummy team gen going on...
for (int i = 0; i < 4; i++)
{
// The player trainer ID is generated, low then high 16 bits
u32 lTrainerId = LCG(seed, counter) >> 16;
u32 hTrainerId = LCG(seed, counter) >> 16;
for (int j = 0; j < 2; j++)
{
// Dummy personality ID (doesn't matter)
LCG(seed, counter);
LCG(seed, counter);
// HP, ATK, DEF IV
LCG(seed, counter);
// SPEED, SPATK, SPDEF IV
LCG(seed, counter);
// Ability
LCG(seed, counter);
generatePokemonPID(seed, hTrainerId, lTrainerId, 0, counter, WantedShininess::notShiny);
generateEVs(seed, true, true, counter);
}
}
return seed;
}
bool GaleDarknessRNGSystem::generateBattleTeam(u32& seed, const std::vector<int> criteria)
{
// Player trainer name generation
LCG(seed);
// Player team index
int playerTeamIndex = (LCG(seed) >> 16) % 5;
if (playerTeamIndex != criteria[0] && criteria[0] != -1)
return false;
// Enemy team index
int enemyTeamIndex = (LCG(seed) >> 16) % 5;
if (enemyTeamIndex != criteria[1] && criteria[1] != -1)
return false;
// modulo 6 ???
LCG(seed);
// Enemy gen
std::vector<int> obtainedEnemyHP;
// The enemy trainer ID is generated, low then high 16 bits
u32 lTrainerId = LCG(seed) >> 16;
u32 hTrainerId = LCG(seed) >> 16;
// Pokemon gen
for (int i = 0; i < 2; i++)
{
// Dummy personality ID (doesn't matter)
LCG(seed);
LCG(seed);
// HP, ATK, DEF IV
LCG(seed);
u32 hpIV = (seed >> 16) & 31;
// SPEED, SPATK, SPDEF IV
LCG(seed);
// Ability
LCG(seed);
generatePokemonPID(seed, hTrainerId, lTrainerId, 0, nullptr, WantedShininess::notShiny);
std::array<u8, 6> EVs = generateEVs(seed, false, false, nullptr);
int hp = EVs[0] / 4 + hpIV + s_quickBattleTeamMaxBaseHPStat[enemyTeamIndex + 5][i];
if (hp != criteria[4 + i] && criteria[4 + i] != -1)
return false;
obtainedEnemyHP.push_back(hp);
}
// modulo 6 ???
LCG(seed);
// Player gen
std::vector<int> obtainedPlayerHP;
// The player trainer ID is generated, low then high 16 bits
lTrainerId = LCG(seed) >> 16;
hTrainerId = LCG(seed) >> 16;
// Pokemon gen
for (int i = 0; i < 2; i++)
{
// Dummy personality ID (doesn't matter)
LCG(seed);
LCG(seed);
// HP, ATK, DEF IV
LCG(seed);
u32 hpIV = (seed >> 16) & 31;
// SPEED, SPATK, SPDEF IV
LCG(seed);
// Ability
LCG(seed);
generatePokemonPID(seed, hTrainerId, lTrainerId, 0, nullptr, WantedShininess::notShiny);
std::array<u8, 6> EVs = generateEVs(seed, false, false, nullptr);
int hp = EVs[0] / 4 + hpIV + s_quickBattleTeamMaxBaseHPStat[playerTeamIndex][i];
if (hp != criteria[2 + i] && criteria[2 + i] != -1)
return false;
obtainedPlayerHP.push_back(hp);
}
#pragma omp critical
{
m_lastObtainedCriterias.clear();
m_lastObtainedCriterias.push_back(playerTeamIndex);
m_lastObtainedCriterias.push_back(enemyTeamIndex);
m_lastObtainedCriterias.insert(m_lastObtainedCriterias.end(), obtainedPlayerHP.begin(),
obtainedPlayerHP.end());
m_lastObtainedCriterias.insert(m_lastObtainedCriterias.end(), obtainedEnemyHP.begin(),
obtainedEnemyHP.end());
}
return true;
}
std::string GaleDarknessRNGSystem::getLastObtainedCriteriasString()
{
std::string criteriasString = "Player team leader: ";
switch (m_lastObtainedCriterias[0])
{
case Mewtwo:
criteriasString += "MEWTWO";
break;
case Mew:
criteriasString += "MEW";
break;
case Deoxys:
criteriasString += "DEOXYS";
break;
case Rayquaza:
criteriasString += "RAYQUAZA";
break;
case Jirachi:
criteriasString += "JIRACHI";
break;
}
criteriasString += "\nComputer team leader: ";
switch (m_lastObtainedCriterias[1])
{
case Articuno:
criteriasString += "ARTICUNO";
break;
case Zapdos:
criteriasString += "ZAPDOS";
break;
case Moltres:
criteriasString += "MOLTRES";
break;
case Kangaskhan:
criteriasString += "KANGASKHAN";
break;
case Latias:
criteriasString += "LATIAS";
break;
}
criteriasString += "\nPlayer Pokémon's HP: " + std::to_string(m_lastObtainedCriterias[2]) + " " +
std::to_string(m_lastObtainedCriterias[3]) + "\n";
criteriasString += "Computer Pokémon's HP: " + std::to_string(m_lastObtainedCriterias[4]) + " " +
std::to_string(m_lastObtainedCriterias[5]);
return criteriasString;
}
std::vector<int> GaleDarknessRNGSystem::obtainTeamGenerationCritera(u32& seed)
{
std::vector<int> criteria;
// Player trainer name generation
LCG(seed);
// Player team index
criteria.push_back((LCG(seed) >> 16) % 5);
// Enemy team index
criteria.push_back((LCG(seed) >> 16) % 5);
// modulo 6 ???
LCG(seed);
// Enemy gen
// The player trainer ID is generated, low then high 16 bits
u32 lTrainerId = LCG(seed) >> 16;
u32 hTrainerId = LCG(seed) >> 16;
// Pokemon gen
for (int i = 0; i < 2; i++)
{
// Dummy personality ID (doesn't matter)
LCG(seed);
LCG(seed);
// HP, ATK, DEF IV
LCG(seed);
u32 hpIV = (seed >> 16) & 31;
// SPEED, SPATK, SPDEF IV
LCG(seed);
// Ability
LCG(seed);
generatePokemonPID(seed, hTrainerId, lTrainerId, 0, nullptr, WantedShininess::notShiny);
generateEVs(seed, false, false, nullptr);
}
// modulo 6 ???
LCG(seed);
// Player gen
// The player trainer ID is generated, low then high 16 bits
lTrainerId = LCG(seed) >> 16;
hTrainerId = LCG(seed) >> 16;
// Pokemon gen
for (int i = 0; i < 2; i++)
{
// Dummy personality ID (doesn't matter)
LCG(seed);
LCG(seed);
// HP, ATK, DEF IV
LCG(seed);
u32 hpIV = (seed >> 16) & 31;
// SPEED, SPATK, SPDEF IV
LCG(seed);
// Ability
LCG(seed);
generatePokemonPID(seed, hTrainerId, lTrainerId, 0, nullptr, WantedShininess::notShiny);
generateEVs(seed, false, false, nullptr);
}
return criteria;
}
int GaleDarknessRNGSystem::getNbrCombinationsFirstTwoCriteria()
{
return s_nbrPossibleEnemyTeams * s_nbrPossiblePlayerTeams;
}
int GaleDarknessRNGSystem::firstTwoCriteriaToIndex(const std::vector<int> criteria)
{
return criteria[0] * s_nbrPossiblePlayerTeams + criteria[1];
}
int GaleDarknessRNGSystem::getMinFramesAmountNamingScreen()
{
return minNamingScreenFrames;
}
int GaleDarknessRNGSystem::getNbrStartersPrediction()
{
return 1;
}
std::vector<std::string> GaleDarknessRNGSystem::getStartersName()
{
std::vector<std::string> name;
name.push_back("Eevee");
return name;
}
std::vector<std::string> GaleDarknessRNGSystem::getSecondariesName()
{
std::vector<std::string> names;
names.push_back("Teddiursa");
return names;
}
u32 GaleDarknessRNGSystem::rollRNGNamingScreenInit(u32 seed)
{
LCG(seed);
LCG(seed);
// The PAL version of XD has the particularity of not call the RNG at all when the naming screen
// is shown unlike any other versions of both Colosseum and XD. Everything after the naming screen
// is done as usual, but it locks the predictions to have only one possible prediction
if (s_palEnabled)
return seed;
// Exhaust all guaranteed calls before considering input and wasted frames.
for (int i = 0; i < minNamingScreenAnimRenderCalls - 1; i++)
seed = rollRNGNamingScreenNext(seed);
return seed;
}
u32 GaleDarknessRNGSystem::rollRNGNamingScreenNext(u32 seed)
{
// Since PAL has no calls during the naming screen, just do nothing!
if (s_palEnabled)
return seed;
seed = LCG(seed);
u16 hiSeed = seed >> 16;
if (static_cast<double>(hiSeed) / 65536.0 < 0.1)
seed = LCGn(seed, 4);
return seed;
}
BaseRNGSystem::StartersPrediction GaleDarknessRNGSystem::generateStarterPokemons(u32 seed)
{
StartersPrediction result;
std::vector<PokemonProperties> startersProperties;
PokemonProperties starter;
// 500 numbers of 32 bits are generated, but they don't seem to influence anything.
seed = LCGn(seed, 1000);
// Every RNG call from now on influence the starters.
result.startingSeed = seed;
// The trainer ID is generated, low then high 16 bits
u32 lTrainerId = LCG(seed) >> 16;
u32 hTrainerId = LCG(seed) >> 16;
result.trainerId = lTrainerId;
// Dummy personality ID (doesn't matter)
LCG(seed);
LCG(seed);
// HP, ATK, DEF IV
LCG(seed);
starter.hpIV = (seed >> 16) & 31;
starter.hpStartingStat =
(2 * eeveeBaseHpStat + starter.hpIV) * eeveeStartingLevel / 100 + eeveeStartingLevel + 10;
starter.atkIV = (seed >> 21) & 31;
starter.defIV = (seed >> 26) & 31;
// SPEED, SPATK, SPDEF IV
LCG(seed);
starter.speedIV = (seed >> 16) & 31;
starter.spAtkIV = (seed >> 21) & 31;
starter.spDefIV = (seed >> 26) & 31;
// Ability, doesn't matter
LCG(seed);
fillStarterGenHiddenPowerInfo(starter);
// Generates the true perosnality ID with no criteria
u32 personalityID = generatePokemonPID(seed, hTrainerId, lTrainerId, 0, nullptr);
starter.isShiny = isPidShiny(lTrainerId, hTrainerId, personalityID);
starter.genderIndex = getPidGender(eeveeGenderRatio, personalityID);
starter.natureIndex = personalityID % 25;
startersProperties.push_back(starter);
result.starters = startersProperties;
return result;
}
void GaleDarknessRNGSystem::generateAllSecondariesInSearchRange(const u32 postStarterSeed,
const int secondaryIndex)
{
if (secondaryIndex != teddiursaSecondaryIndex)
return;
BaseRNGSystem::generateAllSecondariesInSearchRange(
postStarterSeed, teddiursaBaseStats, teddiursaLevel, teddiursaGenderRatio,
secondaryRngAdvanceSearchStart, secondarySearchSeedsAmount);
}
std::array<BaseRNGSystem::StatsRange, 6>
GaleDarknessRNGSystem::getSecondaryStatsRange(const int secondaryIndex)
{
if (secondaryIndex != teddiursaSecondaryIndex)
return {};
return teddiursaStatsRange;
}