-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathRandomizer.cs
1468 lines (1285 loc) · 67.8 KB
/
Randomizer.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
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static ImpostersOrdeal.Distributions;
using static ImpostersOrdeal.ExternalJsonStructs;
using static ImpostersOrdeal.GameDataTypes;
using static ImpostersOrdeal.GlobalData;
namespace ImpostersOrdeal
{
/// <summary>
/// Responsible for all randomization related logic and execution.
/// </summary>
public class Randomizer
{
private readonly MainForm m;
private readonly Random rng;
public Randomizer(MainForm m)
{
this.m = m;
rng = new();
}
/// <summary>
/// Randomizes everything in accordance with current configuration.
/// </summary>
public void Randomize()
{
if (m.checkBox57.Checked)
ScaleEvolutionLevels((double)m.numericUpDown8.Value);
if (m.checkBox58.Checked)
ScaleLevelUpMoves((double)m.numericUpDown8.Value);
if (m.checkBox59.Checked)
ScaleWildEncounters((double)m.numericUpDown8.Value);
if (m.checkBox60.Checked)
ScaleTrainerPokemon((double)m.numericUpDown8.Value);
if (m.checkBox21.Checked)
RandomizeMoveTyping(m.itemDistributionControl8.Get());
if (m.checkBox22.Checked)
RandomizeDamageCategory(m.itemDistributionControl9.Get());
if (m.checkBox50.Checked)
RandomizeTMMoves(m.itemDistributionControl18.Get());
if (m.checkBox25.Checked)
RandomizePower(m.numericDistributionControl8.Get());
if (m.checkBox26.Checked)
RandomizeAccuracy(m.numericDistributionControl10.Get());
if (m.checkBox27.Checked)
RandomizePP(m.numericDistributionControl11.Get());
if (m.checkBox46.Checked)
RandomizePrices(m.numericDistributionControl18.Get());
if (m.checkBox47.Checked)
RandomizePickupItems(m.itemDistributionControl16.Get());
if (m.checkBox51.Checked)
RandomizeShopItems(m.itemDistributionControl19.Get(), m.checkBox52.Checked);
if (m.checkBox1.Checked)
RandomizeEvolutionDestinations(m.button2.Get(), m.checkBox3.Checked);
if (m.checkBox2.Checked)
RandomizeEvolutionLevels(m.groupBox1.Get());
if (m.checkBox5.Checked || m.checkBox7.Checked)
RandomizeStats(m.numericDistributionControl1.Get(), m.checkBox5.Checked, m.checkBox7.Checked, m.checkBox6.Checked);
if (m.checkBox8.Checked)
RandomizePokemonTyping(m.itemDistributionControl1.Get(), m.checkBox61.Checked, (double)m.numericUpDown1.Value, m.rsc.evolutionLogicTypingCorrelationDistribution);
if (m.checkBox15.Checked)
RandomizeTMCompatibility((double)m.numericUpDown2.Value, (double)m.numericUpDown3.Value, m.checkBox16.Checked);
if (m.checkBox10.Checked)
RandomizeWildHeldItems(m.itemDistributionControl2.Get());
if (m.checkBox12.Checked)
RandomizeGrowthRates(m.itemDistributionControl3.Get());
if (m.checkBox13.Checked)
RandomizePersonalAbilites(m.itemDistributionControl4.Get());
if (m.checkBox4.Checked)
RandomizeCatchRates(m.numericDistributionControl2.Get());
if (m.checkBox11.Checked)
RandomizeInitialFriendship(m.numericDistributionControl4.Get());
if (m.checkBox9.Checked)
RandomizeEvYields(m.numericDistributionControl3.Get());
if (m.checkBox14.Checked)
RandomizeExpYields(m.numericDistributionControl5.Get());
if (m.checkBox24.Checked)
RandomizeEggMoves(m.itemDistributionControl7.Get(), (double)m.numericUpDown5.Value, m.checkBox23.Checked, m.numericDistributionControl9.Get());
if (m.checkBox17.Checked || m.checkBox20.Checked)
RandomizeLevelUpMoves(m.checkBox17.Checked, m.itemDistributionControl6.Get(), m.checkBox20.Checked, m.numericDistributionControl7.Get(), (double)m.numericUpDown4.Value, m.checkBox19.Checked, m.numericDistributionControl6.Get(), m.checkBox18.Checked, m.rsc.evolutionMoveCount);
if (m.checkBox29.Checked || m.checkBox28.Checked)
RandomizeWildEncounters(m.checkBox29.Checked, m.itemDistributionControl10.Get(), m.checkBox28.Checked, m.numericDistributionControl12.Get(), m.checkBox31.Checked, m.checkBox30.Checked);
if (m.checkBox32.Checked)
RandomizeTrainerItems(m.itemDistributionControl11.Get(), m.checkBox33.Checked, m.numericDistributionControl13.Get());
if (m.checkBox36.Checked)
RandomizeTrainerPokemonCount(m.numericDistributionControl14.Get());
if (m.checkBox40.Checked)
RandomizeTrainerPokemonLevels(m.numericDistributionControl15.Get());
if (m.checkBox37.Checked)
RandomizeTrainerPokemonSpecies(m.itemDistributionControl12.Get(), m.checkBox34.Checked, m.checkBox38.Checked, m.checkBox35.Checked);
if (m.checkBox42.Checked)
RandomizeTrainerPokemonHeldItems(m.itemDistributionControl15.Get(), m.checkBox43.Checked);
if (m.checkBox41.Checked)
RandomizeTrainerPokemonNatures(m.itemDistributionControl13.Get());
if (m.comboBox1.SelectedIndex != 0)
RandomizeTrainerPokemonMoves(m.comboBox1.SelectedIndex == 1, m.itemDistributionControl14.Get(), (double)m.numericUpDown7.Value);
if (m.checkBox39.Checked)
RandomizeTrainerPokemonShininess((double)m.numericUpDown6.Value);
if (m.checkBox48.Checked)
RandomizeTrainerPokemonAbilities(m.checkBox49.Checked, m.itemDistributionControl17.Get());
if (m.checkBox44.Checked)
RandomizeTrainerPokemonIVs(m.numericDistributionControl16.Get());
if (m.checkBox44.Checked)
RandomizeTrainerPokemonEVs(m.numericDistributionControl17.Get());
if (m.checkBox63.Checked)
RandomizeTypeMatchups(m.itemDistributionControl5.Get());
if (m.checkBox53.Checked)
RandomizeScriptedPokemon(m.itemDistributionControl20.Get());
if (m.checkBox54.Checked)
RandomizeScriptedItems(m.itemDistributionControl21.Get());
if (m.checkBox55.Checked)
RandomizeText(m.checkBox56.Checked);
if (m.checkBox62.Checked)
RandomizeMusic();
}
private void RandomizeTypeMatchups(IDistribution distribution)
{
int typeCount = 18;
for (int o = 0; o < typeCount; o++)
for (int d = 0; d < typeCount; d++)
gameData.globalMetadata.SetTypeMatchup(o, d, ToAffinity(distribution.Next(ToAffinityEnum(gameData.globalMetadata.GetTypeMatchup(o, d)))));
gameData.SetModified(GameDataSet.DataField.GlobalMetadata);
}
private static int ToAffinityEnum(byte affinity)
{
return affinity switch
{
0 => 0,
2 => 1,
4 => 2,
8 => 3,
_ => throw new ArgumentOutOfRangeException(nameof(affinity)),
};
}
private static byte ToAffinity(int affinityEnum)
{
return affinityEnum switch
{
0 => 0,
1 => 2,
2 => 4,
3 => 8,
_ => throw new ArgumentOutOfRangeException(nameof(affinityEnum)),
};
}
private void RandomizeMusic()
{
uint[] groupIDs = {
2944413750, //BGM_BATTLE
3369806648, //BGM_CONTEST
1799075776, //BGM_EVENT
3346466364 //BGM_FIELD
};
uint[] ignoreIDs = {
0,
748895195, //NONE
1454758594, //BA_SILENCE
595984104, //B_CON_SILENCE
4176813980, //EV_SILENCE_100MS
3652652993, //EV_SILENCE_2000MS
789285764, //EV_SILENCE_VS_TRAINER
191715830, //FI_SILENCE
2460189219, //FI_SILENCE_ARC
1302076938, //SILENCE_BA013
};
foreach (Wwise.WwiseObject wo in gameData.audioData.objectsByID.Values)
if (wo is Wwise.MusicSwitchCntr msc)
{
(Wwise.GameSync gs, int i) argumentIdx = msc.arguments.Select((gs, i) => (gs, i)).FirstOrDefault(p => groupIDs.Contains(p.gs.group));
if (argumentIdx.gs == null)
continue;
List<Wwise.Node> nodes = new() { msc.decisionTree };
for (int i = 0; i <= argumentIdx.i; i++)
nodes = nodes.SelectMany(n => n.nodes).ToList();
nodes = nodes.Where(n => !ignoreIDs.Contains(n.key)).ToList();
while (nodes.Any(n => n.childrenCount > 0))
nodes = nodes.SelectMany(n => n.nodes).ToList();
List<uint> anis = nodes.Select(n => n.audioNodeId).Distinct().ToList();
foreach (Wwise.Node n in nodes)
n.audioNodeId = GetRandom(anis);
}
gameData.SetModified(GameDataSet.DataField.AudioData);
}
private void ScaleTrainerPokemon(double coefficient)
{
foreach (Trainer trainer in gameData.trainers)
foreach (TrainerPokemon trainerPokemon in trainer.trainerPokemon)
if (IsWithin(AbsoluteBoundary.Level, trainerPokemon.level))
trainerPokemon.level = (byte)Conform(AbsoluteBoundary.Level, (int)(trainerPokemon.level * coefficient));
gameData.SetModified(GameDataSet.DataField.Trainers);
}
private void ScaleWildEncounters(double coefficient)
{
foreach (EncounterTableFile encounterTableFile in gameData.encounterTableFiles)
foreach (EncounterTable encounterTable in encounterTableFile.encounterTables)
{
List<Encounter> encounters = new();
encounters.AddRange(encounterTable.day);
encounters.AddRange(encounterTable.goodRodMons);
encounters.AddRange(encounterTable.groundMons);
encounters.AddRange(encounterTable.night);
encounters.AddRange(encounterTable.oldRodMons);
encounters.AddRange(encounterTable.superRodMons);
encounters.AddRange(encounterTable.swayGrass);
encounters.AddRange(encounterTable.tairyo);
encounters.AddRange(encounterTable.waterMons);
foreach (Encounter encounter in encounters)
if (IsWithin(AbsoluteBoundary.Level, (int)encounter.GetAvgLevel()))
{
encounter.minLv = Conform(AbsoluteBoundary.Level, (int)(encounter.minLv * coefficient));
encounter.maxLv = Conform(AbsoluteBoundary.Level, (int)(encounter.maxLv * coefficient));
}
}
foreach (UgEncounterLevelSet ugEncounterLevelSet in gameData.ugEncounterLevelSets)
if (IsWithin(AbsoluteBoundary.Level, (int)ugEncounterLevelSet.GetAvgLevel()))
{
ugEncounterLevelSet.minLv = Conform(AbsoluteBoundary.Level, (int)(ugEncounterLevelSet.minLv * coefficient));
ugEncounterLevelSet.maxLv = Conform(AbsoluteBoundary.Level, (int)(ugEncounterLevelSet.maxLv * coefficient));
}
gameData.SetModified(GameDataSet.DataField.EncounterTableFiles);
gameData.SetModified(GameDataSet.DataField.UgEncounterLevelSets);
}
private void ScaleLevelUpMoves(double coefficient)
{
foreach (Pokemon pokemon in gameData.personalEntries)
foreach (LevelUpMove levelUpMove in pokemon.levelUpMoves)
if (IsWithin(AbsoluteBoundary.Level, levelUpMove.level))
levelUpMove.level = (ushort)Conform(AbsoluteBoundary.Level, (int)(levelUpMove.level * coefficient));
gameData.SetModified(GameDataSet.DataField.PersonalEntries);
}
private void ScaleEvolutionLevels(double coefficient)
{
foreach (Pokemon pokemon in gameData.personalEntries)
{
foreach (EvolutionPath evolutionPath in pokemon.evolutionPaths)
if (IsWithin(AbsoluteBoundary.Level, evolutionPath.level))
evolutionPath.level = (ushort)Conform(AbsoluteBoundary.Level, (int)(evolutionPath.level * coefficient));
pokemon.pastEvoLvs = (0, 0);
pokemon.nextEvoLvs = (ushort.MaxValue, ushort.MaxValue);
pokemon.pastPokemon = new();
pokemon.nextPokemon = new();
pokemon.inferiorForms = new();
pokemon.superiorForms = new();
}
DataParser.SetFamilies();
gameData.SetModified(GameDataSet.DataField.PersonalEntries);
}
private void RandomizeText(bool preserveStringLength)
{
if (gameData.messageFileSets == null)
Task.WaitAll(DataParser.ParseAllMessageFiles());
foreach (MessageFileSet messageFileSet in gameData.messageFileSets)
{
List<LabelData> labelDatas = messageFileSet.GetStrings();
labelDatas.ForEach(l => l.wordDatas.Last().eventID = 7);
if (!preserveStringLength)
{
Shuffle(labelDatas);
messageFileSet.SetStrings(labelDatas);
continue;
}
List<int>[] indexes = new List<int>[10];
for (int i = 0; i < indexes.Length; i++)
indexes[i] = new();
int[] targetLists = new int[labelDatas.Count];
for (int i = 0; i < labelDatas.Count; i++)
{
int currentList;
if (labelDatas[i].wordDatas.Count == 1)
{
if (labelDatas[i].GetString().Length < 3)
currentList = 0;
else if (labelDatas[i].GetString().Length < 8)
currentList = 1;
else if (labelDatas[i].GetString().Length < 21)
currentList = 2;
else if (labelDatas[i].GetString().Length < 55)
currentList = 3;
else
currentList = 4;
}
else if (labelDatas[i].wordDatas.Count < 3)
currentList = 5;
else if (labelDatas[i].wordDatas.Count < 8)
currentList = 6;
else if (labelDatas[i].wordDatas.Count < 21)
currentList = 7;
else if (labelDatas[i].wordDatas.Count < 55)
currentList = 8;
else
currentList = 9;
targetLists[i] = currentList;
indexes[currentList].Add(i);
}
List<LabelData> oldLabelDatas = new();
oldLabelDatas.AddRange(labelDatas);
List<int>[] newIndexes = new List<int>[10];
for (int i = 0; i < newIndexes.Length; i++)
{
newIndexes[i] = new();
newIndexes[i].AddRange(indexes[i]);
Shuffle(newIndexes[i]);
}
for (int i = 0; i < labelDatas.Count; i++)
labelDatas[newIndexes[targetLists[i]][indexes[targetLists[i]].IndexOf(i)]] = oldLabelDatas[i];
messageFileSet.SetStrings(labelDatas);
}
gameData.SetModified(GameDataSet.DataField.MessageFileSets);
}
private void RandomizeScriptedItems(IDistribution distribution)
{
foreach (EvScript evScript in gameData.evScripts)
foreach (Script script in evScript.scripts)
foreach (Command command in script.commands)
if (command.cmdType == 187 && gameData.items[(int)command.args[0].data].IsPurchasable())
{
command.args[0].argType = 1;
command.args[0].data = distribution.Next((int)command.args[0].data);
}
gameData.SetModified(GameDataSet.DataField.EvScripts);
}
private void RandomizeScriptedPokemon(IDistribution distribution)
{
foreach (EvScript evScript in gameData.evScripts)
foreach (Script script in evScript.scripts)
foreach(Command command in script.commands)
if (command.cmdType == 322)
{
command.args[1].argType = 1;
command.args[1].data = distribution.Next((int)command.args[1].data);
}
gameData.SetModified(GameDataSet.DataField.EvScripts);
}
private void RandomizeTrainerPokemonEVs(IDistribution distribution)
{
foreach (Trainer trainer in gameData.trainers)
foreach (TrainerPokemon trainerPokemon in trainer.trainerPokemon)
{
int[] evs = new int[6];
int evTotal = trainerPokemon.GetEVs().Sum();
if (IsWithin(AbsoluteBoundary.EvTotal, evTotal))
evTotal = Conform(AbsoluteBoundary.EvTotal, distribution.Next(evTotal));
while (evTotal > 0)
{
int index = rng.Next(evs.Length);
int room = (int)GetBoundaries(AbsoluteBoundary.Ev)[2] - evs[index];
int add = rng.Next(Math.Min(room, 1), Math.Min(room, evTotal));
evs[index] += add;
evTotal -= add;
}
trainerPokemon.SetEVs(evs);
}
gameData.SetModified(GameDataSet.DataField.Trainers);
}
private void RandomizeTrainerPokemonIVs(IDistribution distribution)
{
foreach (Trainer trainer in gameData.trainers)
foreach (TrainerPokemon trainerPokemon in trainer.trainerPokemon)
if (IsWithin(AbsoluteBoundary.Iv, (int)trainerPokemon.GetIVs().Average()))
{
trainerPokemon.hpIV = (byte)Conform(AbsoluteBoundary.Iv, distribution.Next(trainerPokemon.hpIV));
trainerPokemon.atkIV = (byte)Conform(AbsoluteBoundary.Iv, distribution.Next(trainerPokemon.atkIV));
trainerPokemon.defIV = (byte)Conform(AbsoluteBoundary.Iv, distribution.Next(trainerPokemon.defIV));
trainerPokemon.spAtkIV = (byte)Conform(AbsoluteBoundary.Iv, distribution.Next(trainerPokemon.spAtkIV));
trainerPokemon.spDefIV = (byte)Conform(AbsoluteBoundary.Iv, distribution.Next(trainerPokemon.spDefIV));
trainerPokemon.spdIV = (byte)Conform(AbsoluteBoundary.Iv, distribution.Next(trainerPokemon.spdIV));
}
gameData.SetModified(GameDataSet.DataField.Trainers);
}
private void RandomizeTrainerPokemonLevels(IDistribution distribution)
{
foreach (Trainer trainer in gameData.trainers)
foreach (TrainerPokemon trainerPokemon in trainer.trainerPokemon)
if (IsWithin(AbsoluteBoundary.Level, trainerPokemon.level))
trainerPokemon.level = (byte)Conform(AbsoluteBoundary.Level, distribution.Next(trainerPokemon.level));
gameData.SetModified(GameDataSet.DataField.Trainers);
}
private void RandomizeTrainerPokemonCount(IDistribution distribution)
{
foreach (Trainer trainer in gameData.trainers)
{
List<TrainerPokemon> trainerPokemon = trainer.trainerPokemon;
int trainerPokemonCount = trainerPokemon.Count;
if (IsWithin(AbsoluteBoundary.TrainerPokemonCount, trainerPokemonCount))
trainerPokemonCount = Conform(AbsoluteBoundary.TrainerPokemonCount, distribution.Next(trainerPokemon.Count));
while (trainerPokemon.Count < trainerPokemonCount)
{
if (trainerPokemon.Count == 0)
{
trainerPokemon.Add(new());
continue;
}
trainerPokemon.Add(Copy(GetRandom(trainerPokemon)));
}
while (trainerPokemon.Count > trainerPokemonCount)
trainerPokemon.RemoveAt(rng.Next(trainerPokemon.Count));
}
gameData.SetModified(GameDataSet.DataField.Trainers);
}
/// <summary>
/// Returns a separate identical instance of a TrainerPokemon object.
/// </summary>
private static TrainerPokemon Copy(TrainerPokemon o)
{
TrainerPokemon t = new()
{
abilityID = o.abilityID,
atkEV = o.atkEV,
atkIV = o.atkIV,
ballID = o.ballID,
defEV = o.defEV,
defIV = o.defIV,
dexID = o.dexID,
formID = o.formID,
hpEV = o.hpEV,
hpIV = o.hpIV,
isRare = o.isRare,
itemID = o.itemID,
level = o.level,
moveID1 = o.moveID1,
moveID2 = o.moveID2,
moveID3 = o.moveID3,
moveID4 = o.moveID4,
natureID = o.natureID,
seal = o.seal,
sex = o.sex,
spAtkEV = o.spAtkEV,
spAtkIV = o.spAtkIV,
spDefEV = o.spDefEV,
spDefIV = o.spDefIV,
spdEV = o.spdEV,
spdIV = o.spdIV
};
return t;
}
private void RandomizeTrainerPokemonAbilities(bool includeUnobtainable, IDistribution distribution)
{
foreach (Trainer trainer in gameData.trainers)
foreach (TrainerPokemon trainerPokemon in trainer.trainerPokemon)
{
if (includeUnobtainable)
{
trainerPokemon.abilityID = (ushort)distribution.Next(trainerPokemon.abilityID);
continue;
}
trainerPokemon.abilityID = (ushort)GetRandom(gameData.GetPokemon(trainerPokemon.dexID, trainerPokemon.formID).GetAbilities());
}
gameData.SetModified(GameDataSet.DataField.Trainers);
}
private void RandomizeTrainerPokemonShininess(double shinyP)
{
foreach (Trainer trainer in gameData.trainers)
foreach (TrainerPokemon trainerPokemon in trainer.trainerPokemon)
trainerPokemon.isRare = (byte)(P(shinyP) ? 1 : 0);
gameData.SetModified(GameDataSet.DataField.Trainers);
}
private void RandomizeTrainerPokemonNatures(IDistribution distribution)
{
foreach (Trainer trainer in gameData.trainers)
foreach (TrainerPokemon trainerPokemon in trainer.trainerPokemon)
trainerPokemon.natureID = (byte)distribution.Next(trainerPokemon.natureID);
gameData.SetModified(GameDataSet.DataField.Trainers);
}
private void RandomizeTrainerPokemonHeldItems(IDistribution distribution, bool levelLogic)
{
foreach (Trainer trainer in gameData.trainers)
foreach (TrainerPokemon trainerPokemon in trainer.trainerPokemon)
{
if (levelLogic && !P(trainerPokemon.level))
continue;
trainerPokemon.itemID = (ushort)distribution.Next(trainerPokemon.itemID);
}
gameData.SetModified(GameDataSet.DataField.Trainers);
}
private void RandomizeTrainerPokemonMoves(bool setToLevelUpMoves, IDistribution distribution, double typeBiasP)
{
foreach (Trainer trainer in gameData.trainers)
foreach (TrainerPokemon trainerPokemon in trainer.trainerPokemon)
{
List<ushort> moves = trainerPokemon.GetMoves();
Pokemon pokemon = gameData.GetPokemon(trainerPokemon.dexID, trainerPokemon.formID);
if (setToLevelUpMoves)
{
moves = pokemon.levelUpMoves.Where(l => l.level <= trainerPokemon.level).TakeLast(4).Select(l => l.moveID).ToList();
trainerPokemon.SetMoves(moves);
continue;
}
for (int i = 0; i < moves.Count; i++)
{
moves[i] = (ushort)distribution.Next(moves[i]);
if (P(typeBiasP))
while (!pokemon.GetTyping().Contains(gameData.moves[moves[i]].typingID))
moves[i] = (ushort)distribution.Next(moves[i]);
}
trainerPokemon.SetMoves(moves);
}
gameData.SetModified(GameDataSet.DataField.Trainers);
}
private void RandomizeTrainerPokemonSpecies(IDistribution distribution, bool legendLogic, bool typeThemes, bool evolveLogic)
{
HashSet<int> legendaryDexIDs = gameData.dexEntries.Where(d => d.forms[0].legendary).Select(d => d.dexID).ToHashSet();
foreach (Trainer trainer in gameData.trainers)
{
int typing = trainer.GetTypeTheme();
foreach (TrainerPokemon trainerPokemon in trainer.trainerPokemon)
{
bool acceptLegendary = !legendLogic || P(trainerPokemon.level);
Pokemon pokemon = gameData.GetPokemon(trainerPokemon.dexID, trainerPokemon.formID);
do
{
pokemon = GetRandom(gameData.dexEntries[distribution.Next(pokemon.dexID)].forms);
if (evolveLogic)
pokemon = FindStage(pokemon, trainerPokemon.level, false);
} while (!pokemon.IsValid() ||
typeThemes && typing != -1 && !pokemon.GetTyping().Contains(typing) ||
!acceptLegendary && legendaryDexIDs.Contains(pokemon.dexID));
trainerPokemon.dexID = pokemon.dexID;
trainerPokemon.formID = (ushort)pokemon.formID;
trainerPokemon.sex = 3;
}
}
gameData.SetModified(GameDataSet.DataField.Trainers);
}
private void RandomizeTrainerItems(IDistribution itemDistribution, bool randomizeItemCount, IDistribution itemCountdistribution)
{
foreach (Trainer trainer in gameData.trainers)
{
List<int> items = trainer.GetItems();
if (randomizeItemCount)
{
int itemCount = itemCountdistribution.Next(items.Count, 0, 4);
while (items.Count < itemCount)
{
if (items.Count == 0)
{
items.Add(itemDistribution.Next(1));
continue;
}
items.Add(GetRandom(items));
}
while (items.Count > itemCount)
items.RemoveAt(rng.Next(items.Count));
}
for (int i = 0; i < items.Count; i++)
items[i] = itemDistribution.Next(items[i]);
trainer.SetItems(items);
trainer.SetItemFlag();
}
gameData.SetModified(GameDataSet.DataField.Trainers);
}
private void RandomizeWildEncounters(bool randomizeSpecies, IDistribution speciesDistribution, bool randomizeLevels, IDistribution levelDistribution, bool legendLogic, bool evolveLogic)
{
bool randomizeEncounterTableFormIDs = gameData.Uint16EncounterTables();
bool ugVersionsUnbounded = gameData.UgVersionsUnbounded();
bool uint16UgTables = gameData.Uint16UgTables();
bool randomizeUgEncounterTableFormIDs = ugVersionsUnbounded || uint16UgTables;
List<int> legendaryDexIDs = gameData.dexEntries.Where(d => d.forms[0].legendary).Select(d => d.dexID).ToList();
foreach (EncounterTableFile encounterTableFile in gameData.encounterTableFiles)
{
foreach (EncounterTable encounterTable in encounterTableFile.encounterTables)
{
RandomizeEncounterList(encounterTable.day, randomizeSpecies, speciesDistribution, randomizeLevels, levelDistribution, legendLogic, evolveLogic, randomizeEncounterTableFormIDs);
RandomizeEncounterList(encounterTable.goodRodMons, randomizeSpecies, speciesDistribution, randomizeLevels, levelDistribution, legendLogic, evolveLogic, randomizeEncounterTableFormIDs);
RandomizeEncounterList(encounterTable.groundMons, randomizeSpecies, speciesDistribution, randomizeLevels, levelDistribution, legendLogic, evolveLogic, randomizeEncounterTableFormIDs);
RandomizeEncounterList(encounterTable.night, randomizeSpecies, speciesDistribution, randomizeLevels, levelDistribution, legendLogic, evolveLogic, randomizeEncounterTableFormIDs);
RandomizeEncounterList(encounterTable.oldRodMons, randomizeSpecies, speciesDistribution, randomizeLevels, levelDistribution, legendLogic, evolveLogic, randomizeEncounterTableFormIDs);
RandomizeEncounterList(encounterTable.superRodMons, randomizeSpecies, speciesDistribution, randomizeLevels, levelDistribution, legendLogic, evolveLogic, randomizeEncounterTableFormIDs);
RandomizeEncounterList(encounterTable.swayGrass, randomizeSpecies, speciesDistribution, randomizeLevels, levelDistribution, legendLogic, evolveLogic, randomizeEncounterTableFormIDs);
RandomizeEncounterList(encounterTable.tairyo, randomizeSpecies, speciesDistribution, randomizeLevels, levelDistribution, legendLogic, evolveLogic, randomizeEncounterTableFormIDs);
RandomizeEncounterList(encounterTable.waterMons, randomizeSpecies, speciesDistribution, randomizeLevels, levelDistribution, legendLogic, evolveLogic, randomizeEncounterTableFormIDs);
// Unused tables too, why not?
RandomizeEncounterList(encounterTable.gbaRuby, randomizeSpecies, speciesDistribution, randomizeLevels, levelDistribution, legendLogic, evolveLogic, randomizeEncounterTableFormIDs);
RandomizeEncounterList(encounterTable.gbaSapphire, randomizeSpecies, speciesDistribution, randomizeLevels, levelDistribution, legendLogic, evolveLogic, randomizeEncounterTableFormIDs);
RandomizeEncounterList(encounterTable.gbaEmerald, randomizeSpecies, speciesDistribution, randomizeLevels, levelDistribution, legendLogic, evolveLogic, randomizeEncounterTableFormIDs);
RandomizeEncounterList(encounterTable.gbaFire, randomizeSpecies, speciesDistribution, randomizeLevels, levelDistribution, legendLogic, evolveLogic, randomizeEncounterTableFormIDs);
RandomizeEncounterList(encounterTable.gbaLeaf, randomizeSpecies, speciesDistribution, randomizeLevels, levelDistribution, legendLogic, evolveLogic, randomizeEncounterTableFormIDs);
}
if (randomizeSpecies)
{
foreach (HoneyTreeEncounter honeyTreeEncounter in encounterTableFile.honeyTreeEnconters)
{
honeyTreeEncounter.normalDexID = speciesDistribution.Next(honeyTreeEncounter.normalDexID);
honeyTreeEncounter.rareDexID = speciesDistribution.Next(honeyTreeEncounter.rareDexID);
honeyTreeEncounter.superRareDexID = speciesDistribution.Next(honeyTreeEncounter.superRareDexID);
}
for (int i = 0; i < encounterTableFile.safariMons.Count; i++)
encounterTableFile.safariMons[i] = speciesDistribution.Next(encounterTableFile.safariMons[i]);
for (int i = 0; i < encounterTableFile.trophyGardenMons.Count; i++)
encounterTableFile.trophyGardenMons[i] = speciesDistribution.Next(encounterTableFile.trophyGardenMons[i]);
}
}
if (randomizeSpecies)
foreach (UgEncounterFile ugEncounterFile in gameData.ugEncounterFiles)
for (int i = 0; i < ugEncounterFile.ugEncounters.Count; i++)
{
ugEncounterFile.ugEncounters[i].dexID = speciesDistribution.Next((ushort)ugEncounterFile.ugEncounters[i].dexID);
if (randomizeUgEncounterTableFormIDs)
{
ushort formID = (ushort)rng.Next(gameData.dexEntries[(ushort)ugEncounterFile.ugEncounters[i].dexID].forms.Count);
if (gameData.dexEntries[(ushort)ugEncounterFile.ugEncounters[i].dexID].forms.Any(u => u.IsValid()))
formID = (ushort)GetRandom(gameData.dexEntries[(ushort)ugEncounterFile.ugEncounters[i].dexID].forms.Where(p => p.IsValid()).ToList()).formID;
if (ugVersionsUnbounded)
ugEncounterFile.ugEncounters[i].version = formID;
if (uint16UgTables)
ugEncounterFile.ugEncounters[i].dexID += formID << 16;
}
}
if (randomizeSpecies)
foreach (UgSpecialEncounter ugSpecialEncounter in gameData.ugSpecialEncounters)
ugSpecialEncounter.dexID = speciesDistribution.Next(ugSpecialEncounter.dexID);
if (randomizeLevels)
foreach (UgEncounterLevelSet ugEncounterLevelSet in gameData.ugEncounterLevelSets)
if (IsWithin(AbsoluteBoundary.Level, (int)ugEncounterLevelSet.GetAvgLevel()))
{
ugEncounterLevelSet.minLv = Conform(AbsoluteBoundary.Level, levelDistribution.Next(ugEncounterLevelSet.minLv));
ugEncounterLevelSet.maxLv = Conform(AbsoluteBoundary.Level, levelDistribution.Next(ugEncounterLevelSet.maxLv));
}
gameData.SetModified(GameDataSet.DataField.EncounterTableFiles);
gameData.SetModified(GameDataSet.DataField.UgEncounterFiles);
gameData.SetModified(GameDataSet.DataField.UgEncounterLevelSets);
gameData.SetModified(GameDataSet.DataField.UgSpecialEncounters);
if (gameData.externalStarters != null)
{
foreach ((string _, Starter starter) in gameData.externalStarters)
{
if (randomizeLevels && IsWithin(AbsoluteBoundary.Level, starter.level))
{
starter.level = Conform(AbsoluteBoundary.Level, levelDistribution.Next(starter.level));
if (evolveLogic)
{
Pokemon p = FindStage(gameData.GetPokemon(starter.monsNo, starter.formNo), starter.level, true);
starter.monsNo = p.dexID;
starter.formNo = p.formID;
}
}
if (randomizeSpecies)
{
bool acceptLegendary = !legendLogic || P(starter.level);
Func<Pokemon, Pokemon> resolveStage = evolveLogic ? p => FindStage(p, starter.level, true) : p => p;
do
{
starter.monsNo = speciesDistribution.Next(starter.monsNo);
starter.formNo = rng.Next(gameData.dexEntries[starter.monsNo].forms.Count);
Pokemon p = resolveStage(gameData.GetPokemon(starter.monsNo, starter.formNo));
starter.monsNo = p.dexID;
starter.formNo = p.formID;
} while (!gameData.GetPokemon(starter.monsNo, starter.formNo).IsValid() ||
!acceptLegendary && legendaryDexIDs.Contains(starter.monsNo));
}
}
gameData.SetModified(GameDataSet.DataField.ExternalStarters);
}
if (gameData.externalHoneyTrees != null)
{
foreach ((string _, HoneyTreeZone honeyTree) in gameData.externalHoneyTrees)
foreach (HoneyTreeSlot slot in honeyTree.slots)
{
if (randomizeLevels && IsWithin(AbsoluteBoundary.Level, (int)slot.GetAvgLevel()))
{
slot.minlv = Conform(AbsoluteBoundary.Level, levelDistribution.Next(slot.minlv));
slot.maxlv = Conform(AbsoluteBoundary.Level, levelDistribution.Next(slot.maxlv));
if (slot.minlv > slot.maxlv)
(slot.maxlv, slot.minlv) = (slot.minlv, slot.maxlv);
if (evolveLogic)
{
Pokemon p = FindStage(gameData.GetPokemon(slot.monsNo, slot.formNo), (int)slot.GetAvgLevel(), true);
slot.monsNo = p.dexID;
slot.formNo = p.formID;
}
}
if (randomizeSpecies)
{
bool acceptLegendary = !legendLogic || P(slot.GetAvgLevel());
Func<Pokemon, Pokemon> resolveStage = evolveLogic ? p => FindStage(p, (int)slot.GetAvgLevel(), true) : p => p;
do
{
slot.monsNo = speciesDistribution.Next(slot.monsNo);
slot.formNo = rng.Next(gameData.dexEntries[slot.monsNo].forms.Count);
Pokemon p = resolveStage(gameData.GetPokemon(slot.monsNo, slot.formNo));
slot.monsNo = p.dexID;
slot.formNo = p.formID;
} while (!gameData.GetPokemon(slot.monsNo, slot.formNo).IsValid() ||
!acceptLegendary && legendaryDexIDs.Contains(slot.monsNo));
}
}
gameData.SetModified(GameDataSet.DataField.ExternalHoneyTrees);
}
}
/// <summary>
/// Randomizes a list of Encounter objects.
/// </summary>
private void RandomizeEncounterList(List<Encounter> encounters, bool randomizeSpecies, IDistribution speciesDistribution, bool randomizeLevels, IDistribution levelDistribution, bool legendLogic, bool evolveLogic, bool randomizeFormIDs)
{
List<int> legendaryDexIDs = gameData.dexEntries.Where(d => d.forms[0].legendary).Select(d => d.dexID).ToList();
foreach (Encounter encounter in encounters)
{
if (randomizeLevels && IsWithin(AbsoluteBoundary.Level, (int)encounter.GetAvgLevel()))
{
encounter.minLv = Conform(AbsoluteBoundary.Level, levelDistribution.Next(encounter.minLv));
encounter.maxLv = Conform(AbsoluteBoundary.Level, levelDistribution.Next(encounter.maxLv));
if (encounter.minLv > encounter.maxLv)
(encounter.maxLv, encounter.minLv) = (encounter.minLv, encounter.maxLv);
if (evolveLogic)
{
if (randomizeFormIDs)
{
Pokemon p = FindStage(gameData.GetPokemon((ushort)encounter.dexID, encounter.dexID >> 16), (int)encounter.GetAvgLevel(), true);
encounter.dexID = p.dexID + (p.formID << 16);
}
else
encounter.dexID = FindStage(gameData.personalEntries[(ushort)encounter.dexID], (int)encounter.GetAvgLevel(), true).dexID;
}
}
if (randomizeSpecies)
{
bool acceptLegendary = !legendLogic || P(encounter.GetAvgLevel());
Func<Pokemon, Pokemon> resolveStage = evolveLogic ? p => FindStage(p, (int)encounter.GetAvgLevel(), true) : p => p;
do
{
encounter.dexID = speciesDistribution.Next((ushort)encounter.dexID);
if (randomizeFormIDs)
{
encounter.dexID += rng.Next(gameData.dexEntries[(ushort)encounter.dexID].forms.Count) << 16;
Pokemon p = resolveStage(gameData.GetPokemon((ushort)encounter.dexID, encounter.dexID >> 16));
encounter.dexID = p.dexID + (p.formID << 16);
}
else
encounter.dexID = resolveStage(gameData.personalEntries[(ushort)encounter.dexID]).dexID;
} while (!gameData.GetPokemon((ushort)encounter.dexID, encounter.dexID >> 16).IsValid() ||
!acceptLegendary && legendaryDexIDs.Contains((ushort)encounter.dexID));
}
}
}
/// <summary>
/// Finds the evolution stage a certain pokemon is likely to be at for the specified level.
/// </summary>
private Pokemon FindStage(Pokemon pokemon, int level, bool wild)
{
//(wildLevel, trainerLevel)
int pastEvoLevel = wild ? pokemon.pastEvoLvs.Item1 : pokemon.pastEvoLvs.Item2;
if (pastEvoLevel > level)
return FindStage(GetRandom(pokemon.pastPokemon), level, wild);
int nextEvoLevel = wild ? pokemon.nextEvoLvs.Item1 : pokemon.nextEvoLvs.Item2;
if (nextEvoLevel <= level)
return FindStage(GetRandom(pokemon.nextPokemon), level, wild);
return pokemon;
}
private void RandomizeShopItems(IDistribution distribution, bool preserveRegularMarts)
{
if (!preserveRegularMarts)
foreach (MartItem item in gameData.shopTables.martItems)
item.itemID = (ushort)distribution.Next(item.itemID);
foreach (FixedShopItem item in gameData.shopTables.fixedShopItems)
item.itemID = (ushort)distribution.Next(item.itemID);
foreach (BpShopItem item in gameData.shopTables.bpShopItems)
item.itemID = (ushort)distribution.Next(item.itemID);
gameData.SetModified(GameDataSet.DataField.ShopTables);
}
private void RandomizePickupItems(IDistribution distribution)
{
foreach (PickupItem item in gameData.pickupItems)
item.itemID = (ushort)distribution.Next(item.itemID);
gameData.SetModified(GameDataSet.DataField.PickupItems);
}
private void RandomizePrices(IDistribution distribution)
{
foreach (Item item in gameData.items)
if (IsWithin(AbsoluteBoundary.Price, item.price))
item.price = Conform(AbsoluteBoundary.Price, distribution.Next(item.price));
gameData.SetModified(GameDataSet.DataField.Items);
}
private void RandomizePP(IDistribution distribution)
{
foreach (Move move in gameData.moves)
if (IsWithin(AbsoluteBoundary.Pp, move.basePP))
move.basePP = (byte)Conform(AbsoluteBoundary.Pp, distribution.Next(move.basePP));
gameData.SetModified(GameDataSet.DataField.Moves);
}
private void RandomizeAccuracy(IDistribution distribution)
{
foreach (Move move in gameData.moves)
if (IsWithin(AbsoluteBoundary.Accuracy, move.hitPer))
move.hitPer = (byte)Conform(AbsoluteBoundary.Accuracy, distribution.Next(move.hitPer));
gameData.SetModified(GameDataSet.DataField.Moves);
}
private void RandomizePower(IDistribution distribution)
{
foreach (Move move in gameData.moves)
if (IsWithin(AbsoluteBoundary.Power, move.power))
move.power = (byte)Conform(AbsoluteBoundary.Power, distribution.Next(move.power));
gameData.SetModified(GameDataSet.DataField.Moves);
}
private void RandomizeTMMoves(IDistribution distribution)
{
foreach (TM tm in gameData.tms)
tm.moveID = distribution.Next(tm.moveID);
gameData.SetModified(GameDataSet.DataField.TMs);
}
private void RandomizeDamageCategory(IDistribution distribution)
{
foreach (Move move in gameData.moves)
if (move.damageCategoryID != 0)
move.damageCategoryID = (byte)distribution.Next(move.damageCategoryID);
gameData.SetModified(GameDataSet.DataField.Moves);
}
private void RandomizeMoveTyping(IDistribution distribution)
{
foreach (Move move in gameData.moves)
move.typingID = (byte)distribution.Next(move.typingID);
gameData.SetModified(GameDataSet.DataField.Moves);
}
private void RandomizeLevelUpMoves(bool randomizeMoves, IDistribution moveDistribution, bool randomizeLevels, IDistribution levelDistribution, double typeBiasP, bool randomizeMoveCount, IDistribution moveCountDistribution, bool sortByPower, IDistribution evolutionMoveCountDistribution)
{
foreach (Pokemon pokemon in gameData.personalEntries)
{
List<LevelUpMove> levelUpMoves = pokemon.levelUpMoves;
if (!IsWithin(AbsoluteBoundary.LevelUpMoveCount, levelUpMoves.Count))
continue;
if (randomizeMoveCount)
{
int moveCount = levelUpMoves.Count;
moveCount = Conform(AbsoluteBoundary.LevelUpMoveCount, moveCountDistribution.Next(levelUpMoves.Count));
while (levelUpMoves.Count < moveCount)
{
if (levelUpMoves.Count == 0)
{
LevelUpMove l = new()
{
level = (ushort)Conform(AbsoluteBoundary.Level, levelDistribution.Next(1)),
moveID = (ushort)moveDistribution.Next(1)
};
levelUpMoves.Add(l);
continue;
}
levelUpMoves.Add(Copy(GetRandom(levelUpMoves)));
}
while (levelUpMoves.Count > moveCount)
levelUpMoves.RemoveAt(rng.Next(levelUpMoves.Count));
}
if (randomizeMoves)
foreach (LevelUpMove move in levelUpMoves)
{
move.moveID = (ushort)moveDistribution.Next(move.moveID);
if (P(typeBiasP))
while (!pokemon.GetTyping().Contains(gameData.moves[move.moveID].typingID))
move.moveID = (ushort)moveDistribution.Next(move.moveID);
}
if (randomizeLevels)
{
foreach (LevelUpMove move in levelUpMoves)
if (IsWithin(AbsoluteBoundary.Level, move.level))
move.level = (ushort)Conform(AbsoluteBoundary.Level, levelDistribution.Next(move.level));
if (pokemon.pastPokemon.Count > 0)
{
int evolutionMoveCount = evolutionMoveCountDistribution.Next(0);
LevelUpMove[] evolutionMoves = new LevelUpMove[evolutionMoveCount];
for (int i = 0; i < evolutionMoveCount; i++)
evolutionMoves[i] = GetRandom(levelUpMoves);
foreach (LevelUpMove move in evolutionMoves)
move.level = 0;
}
}
LevelUpMove firstMove = GetRandom(levelUpMoves);
firstMove.level = 1;
List<LevelUpMove> attacks = levelUpMoves.Where(l => IsWithin(AbsoluteBoundary.Power, gameData.moves[l.moveID].power)).ToList();
if (attacks.Count > 0)
{
LevelUpMove target = GetRandom(attacks);
(target.moveID, firstMove.moveID) = (firstMove.moveID, target.moveID);
}
else
while (!IsWithin(AbsoluteBoundary.Power, gameData.moves[firstMove.moveID].power))
firstMove.moveID = (ushort)GetRandom(gameData.moves).moveID;
levelUpMoves.Sort((m1, m2) => m1.level - m2.level);