From c7f6c2a648dab27ec7d2f00af28e6d33c2c3d772 Mon Sep 17 00:00:00 2001 From: Kurt Date: Sat, 10 Dec 2016 14:12:07 -0800 Subject: [PATCH] Add pkm randomization Somewhat functional but pushing what exists so far trainer aspects and smart EVs not yet implemented --- pk3DS/Legality/Randomizer.cs | 74 ++++++++- pk3DS/Subforms/Gen7/SMTE.Designer.cs | 60 +++---- pk3DS/Subforms/Gen7/SMTE.cs | 100 ++++++++++-- pk3DS/Subforms/Gen7/SMTE.resx | 228 +++++++++++++++++++++++++++ 4 files changed, 406 insertions(+), 56 deletions(-) diff --git a/pk3DS/Legality/Randomizer.cs b/pk3DS/Legality/Randomizer.cs index 0ca2b88..80d2c44 100644 --- a/pk3DS/Legality/Randomizer.cs +++ b/pk3DS/Legality/Randomizer.cs @@ -2,8 +2,21 @@ namespace pk3DS { - public static class Randomizer + public class Randomizer { + public PersonalInfo[] Stats { private get; set; } + public bool BST; + private int[] SpeciesList; + private int ctr; + public Randomizer(bool G1, bool G2, bool G3, bool G4, bool G5, bool G6, bool G7, bool L, bool E, bool Shedinja = true) + { + SpeciesList = getSpeciesList(G1, G2, G3, G4, G5, G6, G7, L, E, Shedinja); + } + public int getRandomSpecies(int oldSpecies, int Type = -1) + { + return getRandomSpecies(ref SpeciesList, ref ctr, oldSpecies, BST, Stats, Type); + } + internal static int getRandomSpecies(ref int[] list, ref int ctr) { if (ctr == 0) { Util.Shuffle(list); } @@ -12,7 +25,7 @@ internal static int getRandomSpecies(ref int[] list, ref int ctr) } internal static int MaxSpeciesID = 721; internal static int[] RandomSpeciesList => Enumerable.Range(1, MaxSpeciesID).ToArray(); - internal static int getRandomSpecies(ref int[] sL, ref int ctr, int oldSpecies, bool BST, PersonalInfo[] stats = null) + internal static int getRandomSpecies(ref int[] sL, ref int ctr, int oldSpecies, bool BST, PersonalInfo[] stats = null, int Type = -1) { int species = getRandomSpecies(ref sL, ref ctr); if (!BST || stats == null) @@ -25,13 +38,20 @@ internal static int getRandomSpecies(ref int[] sL, ref int ctr, int oldSpecies, int a = 11; const int c = 10; - int iter = 0; - while (!(pkm.BST * c/a < oldpkm.BST && pkm.BST * a/c > oldpkm.BST)) + int iter = 1; + bool valid = !(pkm.BST*c/a < oldpkm.BST && pkm.BST*a/c > oldpkm.BST); + if (Type > -1) valid &= pkm.Types.Any(type => type == Type); + + while (!valid) { species = getRandomSpecies(ref sL, ref ctr); pkm = Main.SpeciesStat[species]; if (++iter % sL.Length == 0) a++; + + // Check Satisfaction + valid = !(pkm.BST*c/a < oldpkm.BST && pkm.BST*a/c > oldpkm.BST); + if (Type > -1) valid &= pkm.Types.Any(type => type == Type); } return species; } @@ -195,5 +215,51 @@ internal static int[] getRandomItemList() internal static readonly ushort[] HeldItems_SM = new ushort[1].Concat(Pouch_Items_SM).Concat(Pouch_Berries_SM).Concat(Pouch_Medicine_SM).Concat(Pouch_ZCrystalHeld_SM).ToArray(); internal static readonly ushort[] HeldItemsBuy_SM = new ushort[1].Concat(Pouch_Items_SM).Concat(Pouch_Medicine_SM).ToArray(); #endregion + + internal static int[] getRandomMoves(int[] Types, Move[] moveData, bool rDMG, int rDMGCount, bool rSTAB, int rSTABCount) + { + int maxmove = Main.Config.XY ? 617 + : Main.Config.ORAS ? 620 + : 718; // SM + maxmove += 1; + int[] moves = new int[4]; + int loopctr = 0; + const int maxLoop = 666; + + getMoves: + switch (Main.Config.Generation) + { + case 6: + for (int i = 0; i < 4; i++) + moves[i] = (int)(Util.rnd32() % maxmove); + break; + + case 7: + int m = 0; + while (m != 4) + { + moves[m] = (int)(Util.rnd32() % maxmove); + if (!Legal.Z_Moves.Contains(moves[m])) + m++; // Valid + } + break; + + default: + return moves; + } + if (loopctr++ < maxLoop || ScreenMoves(moves, Types, moveData, rDMG, rDMGCount, rSTAB, rSTABCount)) + return moves; + goto getMoves; + } + + private static bool ScreenMoves(int[] moves, int[] Types, Move[] moveData, bool rDMG, int rDMGCount, bool rSTAB, int rSTABCount) + { + if (rDMG && rDMGCount > moves.Count(move => moveData[move].Category != 0)) + return false; + if (rSTAB && rSTABCount > moves.Count(move => Types.Contains(moveData[move].Type))) + return false; + + return true; + } } } diff --git a/pk3DS/Subforms/Gen7/SMTE.Designer.cs b/pk3DS/Subforms/Gen7/SMTE.Designer.cs index 79bcf91..10a7687 100644 --- a/pk3DS/Subforms/Gen7/SMTE.Designer.cs +++ b/pk3DS/Subforms/Gen7/SMTE.Designer.cs @@ -191,9 +191,7 @@ private void InitializeComponent() this.L_MaxPKM = new System.Windows.Forms.Label(); this.NUD_RMin = new System.Windows.Forms.NumericUpDown(); this.NUD_RMax = new System.Windows.Forms.NumericUpDown(); - this.CHK_GymE4Only = new System.Windows.Forms.CheckBox(); this.CHK_RandomMegaForm = new System.Windows.Forms.CheckBox(); - this.CHK_GymTrainers = new System.Windows.Forms.CheckBox(); this.CHK_TypeTheme = new System.Windows.Forms.CheckBox(); this.CHK_OnlySingles = new System.Windows.Forms.CheckBox(); this.CHK_IgnoreSpecialClass = new System.Windows.Forms.CheckBox(); @@ -466,10 +464,11 @@ private void InitializeComponent() // // PB_Team1 // + this.PB_Team1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.PB_Team1.ContextMenuStrip = this.mnuVSD; this.PB_Team1.Location = new System.Drawing.Point(53, 244); this.PB_Team1.Name = "PB_Team1"; - this.PB_Team1.Size = new System.Drawing.Size(80, 60); + this.PB_Team1.Size = new System.Drawing.Size(82, 62); this.PB_Team1.TabIndex = 444; this.PB_Team1.TabStop = false; // @@ -502,52 +501,56 @@ private void InitializeComponent() // // PB_Team2 // + this.PB_Team2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.PB_Team2.ContextMenuStrip = this.mnuVSD; this.PB_Team2.Location = new System.Drawing.Point(139, 244); this.PB_Team2.Name = "PB_Team2"; - this.PB_Team2.Size = new System.Drawing.Size(80, 60); + this.PB_Team2.Size = new System.Drawing.Size(82, 62); this.PB_Team2.TabIndex = 443; this.PB_Team2.TabStop = false; // // PB_Team3 // + this.PB_Team3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.PB_Team3.ContextMenuStrip = this.mnuVSD; this.PB_Team3.Location = new System.Drawing.Point(225, 244); this.PB_Team3.Name = "PB_Team3"; - this.PB_Team3.Size = new System.Drawing.Size(80, 60); + this.PB_Team3.Size = new System.Drawing.Size(82, 62); this.PB_Team3.TabIndex = 442; this.PB_Team3.TabStop = false; // // PB_Team4 // + this.PB_Team4.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.PB_Team4.ContextMenuStrip = this.mnuVSD; this.PB_Team4.Location = new System.Drawing.Point(311, 244); this.PB_Team4.Name = "PB_Team4"; - this.PB_Team4.Size = new System.Drawing.Size(80, 60); + this.PB_Team4.Size = new System.Drawing.Size(82, 62); this.PB_Team4.TabIndex = 441; this.PB_Team4.TabStop = false; // // PB_Team5 // + this.PB_Team5.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.PB_Team5.ContextMenuStrip = this.mnuVSD; this.PB_Team5.Location = new System.Drawing.Point(397, 244); this.PB_Team5.Name = "PB_Team5"; - this.PB_Team5.Size = new System.Drawing.Size(80, 60); + this.PB_Team5.Size = new System.Drawing.Size(82, 62); this.PB_Team5.TabIndex = 440; this.PB_Team5.TabStop = false; // // PB_Team6 // + this.PB_Team6.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.PB_Team6.ContextMenuStrip = this.mnuVSD; this.PB_Team6.Location = new System.Drawing.Point(483, 244); this.PB_Team6.Name = "PB_Team6"; - this.PB_Team6.Size = new System.Drawing.Size(80, 60); + this.PB_Team6.Size = new System.Drawing.Size(82, 62); this.PB_Team6.TabIndex = 439; this.PB_Team6.TabStop = false; // // B_Randomize // - this.B_Randomize.Enabled = false; this.B_Randomize.Location = new System.Drawing.Point(61, 3); this.B_Randomize.Name = "B_Randomize"; this.B_Randomize.Size = new System.Drawing.Size(93, 23); @@ -1907,7 +1910,8 @@ private void InitializeComponent() this.CB_Moves.Items.AddRange(new object[] { "Don\'t Modify", "Randomize All", - "Use Levelup Only"}); + "Use Levelup Only", + "High Powered Attacks"}); this.CB_Moves.Location = new System.Drawing.Point(54, 6); this.CB_Moves.Name = "CB_Moves"; this.CB_Moves.Size = new System.Drawing.Size(121, 21); @@ -2024,9 +2028,7 @@ private void InitializeComponent() this.Tab_Trainer1.Controls.Add(this.L_MaxPKM); this.Tab_Trainer1.Controls.Add(this.NUD_RMin); this.Tab_Trainer1.Controls.Add(this.NUD_RMax); - this.Tab_Trainer1.Controls.Add(this.CHK_GymE4Only); this.Tab_Trainer1.Controls.Add(this.CHK_RandomMegaForm); - this.Tab_Trainer1.Controls.Add(this.CHK_GymTrainers); this.Tab_Trainer1.Controls.Add(this.CHK_TypeTheme); this.Tab_Trainer1.Controls.Add(this.CHK_OnlySingles); this.Tab_Trainer1.Controls.Add(this.CHK_IgnoreSpecialClass); @@ -2144,23 +2146,11 @@ private void InitializeComponent() this.NUD_RMax.Size = new System.Drawing.Size(40, 20); this.NUD_RMax.TabIndex = 335; this.NUD_RMax.Value = new decimal(new int[] { - 1, + 6, 0, 0, 0}); // - // CHK_GymE4Only - // - this.CHK_GymE4Only.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); - this.CHK_GymE4Only.AutoSize = true; - this.CHK_GymE4Only.Enabled = false; - this.CHK_GymE4Only.Location = new System.Drawing.Point(6, 95); - this.CHK_GymE4Only.Name = "CHK_GymE4Only"; - this.CHK_GymE4Only.Size = new System.Drawing.Size(124, 17); - this.CHK_GymE4Only.TabIndex = 334; - this.CHK_GymE4Only.Text = "Theme Trial/E4 Only"; - this.CHK_GymE4Only.UseVisualStyleBackColor = true; - // // CHK_RandomMegaForm // this.CHK_RandomMegaForm.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); @@ -2172,23 +2162,11 @@ private void InitializeComponent() this.CHK_RandomMegaForm.Text = "Allow RandForm\r\nfor Mega PKM"; this.CHK_RandomMegaForm.UseVisualStyleBackColor = true; // - // CHK_GymTrainers - // - this.CHK_GymTrainers.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); - this.CHK_GymTrainers.AutoSize = true; - this.CHK_GymTrainers.Enabled = false; - this.CHK_GymTrainers.Location = new System.Drawing.Point(6, 81); - this.CHK_GymTrainers.Name = "CHK_GymTrainers"; - this.CHK_GymTrainers.Size = new System.Drawing.Size(124, 17); - this.CHK_GymTrainers.TabIndex = 331; - this.CHK_GymTrainers.Text = "Theme Gym Trainers"; - this.CHK_GymTrainers.UseVisualStyleBackColor = true; - // // CHK_TypeTheme // this.CHK_TypeTheme.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.CHK_TypeTheme.AutoSize = true; - this.CHK_TypeTheme.Location = new System.Drawing.Point(6, 67); + this.CHK_TypeTheme.Location = new System.Drawing.Point(6, 80); this.CHK_TypeTheme.Name = "CHK_TypeTheme"; this.CHK_TypeTheme.Size = new System.Drawing.Size(127, 17); this.CHK_TypeTheme.TabIndex = 329; @@ -2446,9 +2424,7 @@ private void InitializeComponent() private System.Windows.Forms.CheckBox CHK_IgnoreSpecialClass; private System.Windows.Forms.CheckBox CHK_OnlySingles; private System.Windows.Forms.CheckBox CHK_TypeTheme; - private System.Windows.Forms.CheckBox CHK_GymTrainers; private System.Windows.Forms.CheckBox CHK_RandomMegaForm; - private System.Windows.Forms.CheckBox CHK_GymE4Only; private System.Windows.Forms.NumericUpDown NUD_RMax; private System.Windows.Forms.NumericUpDown NUD_RMin; private System.Windows.Forms.Label L_MaxPKM; @@ -2460,10 +2436,8 @@ private void InitializeComponent() private System.Windows.Forms.TabPage Tab_Trainer1; private System.Windows.Forms.CheckBox CHK_STAB; private System.Windows.Forms.CheckBox CHK_RandomItems; - private System.Windows.Forms.CheckBox CHK_Damage; private System.Windows.Forms.NumericUpDown NUD_STAB; private System.Windows.Forms.CheckBox CHK_RandomAbilities; - private System.Windows.Forms.NumericUpDown NUD_Damage; private System.Windows.Forms.CheckBox CHK_MaxDiffPKM; private System.Windows.Forms.ComboBox CB_Moves; private System.Windows.Forms.Label L_Moves; @@ -2488,6 +2462,8 @@ private void InitializeComponent() private System.Windows.Forms.Label L_ShinyPCT; private System.Windows.Forms.TabPage Tab_PKM1; private System.Windows.Forms.TabControl TC_rand; + private System.Windows.Forms.CheckBox CHK_Damage; + private System.Windows.Forms.NumericUpDown NUD_Damage; } } diff --git a/pk3DS/Subforms/Gen7/SMTE.cs b/pk3DS/Subforms/Gen7/SMTE.cs index 6239055..45a6a91 100644 --- a/pk3DS/Subforms/Gen7/SMTE.cs +++ b/pk3DS/Subforms/Gen7/SMTE.cs @@ -12,11 +12,6 @@ public partial class SMTE : Form { private readonly trdata7[] Trainers; private string[][] AltForms; - private static readonly Random rand = new Random(); - internal static uint rnd32() - { - return (uint)rand.Next(1 << 30) << 2 | (uint)rand.Next(1 << 2); - } private int index = -1; private PictureBox[] pba; @@ -48,6 +43,7 @@ public SMTE(byte[][] trc, byte[][] trd, byte[][] trp) pb.Click += clickSlot; CB_TrainerID.SelectedIndex = 0; + CB_Moves.SelectedIndex = 0; } private int getSlot(object sender) @@ -380,13 +376,17 @@ private static int[] getHighAttacks(trpoke7 pk) { int i = Main.Config.Personal.getFormeIndex(pk.Species, pk.Form); var learnset = Main.Config.Learnsets[i]; - return learnset.Moves.OrderByDescending(move => Main.Config.Moves[move].Power).Take(4).ToArray(); + var moves = learnset.Moves.OrderByDescending(move => Main.Config.Moves[move].Power).Distinct().Take(4).ToArray(); + Array.Resize(ref moves, 4); + return moves; } private static int[] getCurrentAttacks(trpoke7 pk) { int i = Main.Config.Personal.getFormeIndex(pk.Species, pk.Form); var learnset = Main.Config.Learnsets[i]; - return learnset.getMoves(pk.Level); + var moves = learnset.getMoves(pk.Level).Distinct().Take(4).ToArray(); + Array.Resize(ref moves, 4); + return moves; } protected override void OnFormClosing(FormClosingEventArgs e) @@ -561,7 +561,88 @@ public static int[] setHPIVs(int type, int[] ivs) private void B_Randomize_Click(object sender, EventArgs e) { - Util.Alert("Not yet implemented."); + Randomizer rnd = new Randomizer(CHK_G1.Checked, CHK_G2.Checked, CHK_G3.Checked, CHK_G4.Checked, CHK_G5.Checked, + CHK_G6.Checked, CHK_G7.Checked, CHK_L.Checked, CHK_E.Checked, Shedinja: true) + { + BST = CHK_BST.Checked, + Stats = Main.SpeciesStat + }; + + var items = Randomizer.getRandomItemList(); + foreach (var tr in Trainers) + { + if (tr.Pokemon.Count == 0) + continue; + // Trainer Properties + if (CHK_OnlyDoubles.Checked) + { } + else if (CHK_OnlySingles.Checked) + { } + + if (CHK_RandomGift.Checked && Util.rnd32() < NUD_GiftPercent.Value) + { } + + if (CHK_RandomClass.Checked) + { + if (CHK_IgnoreSpecialClass.Checked) + { } + else + { } + } + + if (tr.NumPokemon < NUD_RMin.Value) + { + var avgBST = (int)tr.Pokemon.Average(pk => Main.SpeciesStat[pk.Species].BST); + var pinfo = Main.SpeciesStat.OrderBy(pk => Math.Abs(avgBST - pk.BST)).First(); + int avgSpec = Array.IndexOf(Main.SpeciesStat, pinfo); + for (int p = tr.NumPokemon; p < NUD_RMin.Value; p++) + tr.Pokemon.Add(new trpoke7 {Species = rnd.getRandomSpecies(avgSpec)}); + tr.NumPokemon = (int)NUD_RMin.Value; + } + if (tr.NumPokemon > NUD_RMax.Value) + { + tr.Pokemon.RemoveRange((int)NUD_RMax.Value, (int)(tr.NumPokemon - NUD_RMax.Value)); + tr.NumPokemon = (int)NUD_RMax.Value; + } + + // PKM Properties + foreach (var pk in tr.Pokemon) + { + if (CHK_RandomPKM.Checked) + { + int Type = CHK_TypeTheme.Checked ? (int)Util.rnd32()%17 : -1; + pk.Species = rnd.getRandomSpecies(pk.Species, Type); + pk.Form = Randomizer.GetRandomForme(pk.Species, CHK_RandomMegaForm.Checked, true, Main.SpeciesStat); + } + if (CHK_Level.Checked) + pk.Level = (int)(pk.Level*(100 + NUD_LevelBoost.Value))/100; + if (CHK_RandomShiny.Checked) + pk.Shiny = Util.rand.Next(0, 100 + 1) < NUD_Shiny.Value; + if (CHK_RandomItems.Checked) + pk.Item = items[Util.rnd32()%items.Length]; + if (CHK_RandomAbilities.Checked) + pk.Ability = (int)Util.rnd32()%4; + if (CHK_MaxDiffPKM.Checked) + pk.IVs = new[] {31, 31, 31, 31, 31, 31}; + + switch (CB_Moves.SelectedIndex) + { + case 1: // Random + pk.Moves = Randomizer.getRandomMoves( + Main.Config.Personal.getFormeEntry(pk.Species, pk.Form).Types, + Main.Config.Moves, + CHK_Damage.Checked, (int)NUD_Damage.Value, + CHK_STAB.Checked, (int)NUD_STAB.Value); + break; + case 2: // Current LevelUp + pk.Moves = getCurrentAttacks(pk); + break; + case 3: // High Attacks + pk.Moves = getHighAttacks(pk); + break; + } + } + } } private void B_HighAttack_Click(object sender, EventArgs e) { @@ -586,9 +667,8 @@ private void B_CurrentAttack_Click(object sender, EventArgs e) } private void setMoves(int[] moves) { - Array.Resize(ref moves, 4); var mcb = new[] { CB_Move1, CB_Move2, CB_Move3, CB_Move4 }; - for (int i = 0; i < moves.Length; i++) + for (int i = 0; i < mcb.Length; i++) mcb[i].SelectedIndex = moves[i]; } } diff --git a/pk3DS/Subforms/Gen7/SMTE.resx b/pk3DS/Subforms/Gen7/SMTE.resx index 0161958..05589a3 100644 --- a/pk3DS/Subforms/Gen7/SMTE.resx +++ b/pk3DS/Subforms/Gen7/SMTE.resx @@ -150,12 +150,54 @@ True + + True + + + True + + + True + + + True + True True + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + True @@ -171,6 +213,12 @@ True + + True + + + True + True @@ -180,6 +228,12 @@ True + + True + + + True + True @@ -189,6 +243,12 @@ True + + True + + + True + True @@ -198,6 +258,12 @@ True + + True + + + True + True @@ -207,6 +273,12 @@ True + + True + + + True + True @@ -216,6 +288,12 @@ True + + True + + + True + True @@ -225,15 +303,54 @@ True + + True + + + True + True True + + True + True + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + True @@ -246,6 +363,15 @@ True + + True + + + True + + + True + True @@ -261,6 +387,18 @@ True + + True + + + True + + + True + + + True + True @@ -276,6 +414,18 @@ True + + True + + + True + + + True + + + True + True @@ -291,6 +441,18 @@ True + + True + + + True + + + True + + + True + True @@ -306,6 +468,18 @@ True + + True + + + True + + + True + + + True + True @@ -321,6 +495,18 @@ True + + True + + + True + + + True + + + True + True @@ -336,6 +522,18 @@ True + + True + + + True + + + True + + + True + True @@ -348,6 +546,15 @@ True + + True + + + True + + + True + True @@ -357,12 +564,33 @@ True + + True + + + True + True True + + True + + + True + + + True + + + True + + + True + True