From 32f9f806dbefd358c9fb5ede95ae1cf2e97e1d4b Mon Sep 17 00:00:00 2001 From: Kurt Date: Sat, 31 Mar 2018 00:43:41 -0700 Subject: [PATCH] Add other iencounterable->pkm generators not tested --- .../Legality/Encounters/EncounterEgg.cs | 3 + .../Legality/Encounters/EncounterLink.cs | 1 - .../Legality/Encounters/EncounterSlot.cs | 70 ++++++++++++++- .../Legality/Encounters/EncounterStatic.cs | 87 ++++++++++++++++++- .../Legality/Encounters/EncounterTrade.cs | 67 +++++++++++++- PKHeX.Core/Legality/Enums/EncounterType.cs | 12 +++ PKHeX.Core/PKM/Editing/CommonEdits.cs | 8 ++ PKHeX.Core/PKM/PKM.cs | 16 ++++ PKHeX.Core/PKM/Shared/IContestStats.cs | 10 +++ 9 files changed, 267 insertions(+), 7 deletions(-) diff --git a/PKHeX.Core/Legality/Encounters/EncounterEgg.cs b/PKHeX.Core/Legality/Encounters/EncounterEgg.cs index 47f55586b..0f9a45802 100644 --- a/PKHeX.Core/Legality/Encounters/EncounterEgg.cs +++ b/PKHeX.Core/Legality/Encounters/EncounterEgg.cs @@ -28,6 +28,9 @@ namespace PKHeX.Core pk.CurrentLevel = Level; pk.Version = (int)Version; + int gender = Util.Rand.Next(2); + pk.Gender = pk.GetSaneGender(gender); + var moves = Legal.GetEggMoves(pk, Species, pk.AltForm, Version); pk.Moves = moves; pk.SetMaximumPPCurrent(moves); diff --git a/PKHeX.Core/Legality/Encounters/EncounterLink.cs b/PKHeX.Core/Legality/Encounters/EncounterLink.cs index 46752bace..43c2678ce 100644 --- a/PKHeX.Core/Legality/Encounters/EncounterLink.cs +++ b/PKHeX.Core/Legality/Encounters/EncounterLink.cs @@ -66,7 +66,6 @@ namespace PKHeX.Core pk.OT_Friendship = pk.PersonalInfo.BaseFriendship; pk.SetRandomIVs(flawless: 3); pk.RefreshAbility(Ability); - SAV.ApplyHandlingTrainerInfo(pk); if (RelearnMoves != null) pk.RelearnMoves = RelearnMoves; if (RibbonClassic) diff --git a/PKHeX.Core/Legality/Encounters/EncounterSlot.cs b/PKHeX.Core/Legality/Encounters/EncounterSlot.cs index ca7de8d18..93c2e17c3 100644 --- a/PKHeX.Core/Legality/Encounters/EncounterSlot.cs +++ b/PKHeX.Core/Legality/Encounters/EncounterSlot.cs @@ -1,4 +1,6 @@ -namespace PKHeX.Core +using System; + +namespace PKHeX.Core { public class EncounterSlotPermissions { @@ -62,6 +64,70 @@ } } - public PKM ConvertToPKM(ITrainerInfo SAV) => throw new System.NotImplementedException(); + public PKM ConvertToPKM(ITrainerInfo SAV) + { + var version = this.GetCompatibleVersion((GameVersion)SAV.Game); + int lang = (int)Legal.GetSafeLanguage(Generation, (LanguageID)SAV.Language); + int level = LevelMin; + var pk = PKMConverter.GetBlank(Generation); + int gender = Util.Rand.Next(2); + pk.Gender = pk.GetSaneGender(gender); + + int nature = Util.Rand.Next(25); + pk.Nature = nature; + pk.EncryptionConstant = Util.Rand32(); + pk.Species = Species; + pk.Language = lang; + pk.CurrentLevel = level; + pk.Version = (int) version; + pk.PID = Util.Rand32(); + pk.Nickname = PKX.GetSpeciesNameGeneration(Species, lang, Generation); + pk.Ball = 4; + pk.Met_Level = level; + pk.Met_Location = Location; + pk.MetDate = DateTime.Today; + + SAV.ApplyToPKM(pk); + pk.Language = lang; + + pk.SetRandomIVs(flawless: 3); + + if (Permissions.IsDexNav) + { + pk.RefreshAbility(2); + var eggMoves = Legal.GetEggMoves(pk, Species, pk.AltForm, Version); + if (eggMoves.Length > 0) + pk.RelearnMove1 = eggMoves[Util.Rand.Next(eggMoves.Length)]; + } + else + { + pk.RefreshAbility(Util.Rand.Next(2)); + } + + switch (pk.Format) + { + case 3: + case 4: + PIDGenerator.SetValuesFromSeed(pk, PIDType.Method_1, Util.Rand32()); + if (pk.Format == 4) + pk.EncounterType = TypeEncounter.GetIndex(); + break; + case 6: + pk.SetRandomMemory6(); + break; + } + + var moves = this is EncounterSlotMoves m ? m.Moves : Legal.GetEncounterMoves(pk, level, version); + pk.Moves = moves; + pk.SetMaximumPPCurrent(moves); + pk.OT_Friendship = pk.PersonalInfo.BaseFriendship; + if (pk.Format < 6) + return pk; + + SAV.ApplyHandlingTrainerInfo(pk); + pk.SetRandomEC(); + + return pk; + } } } diff --git a/PKHeX.Core/Legality/Encounters/EncounterStatic.cs b/PKHeX.Core/Legality/Encounters/EncounterStatic.cs index 1d31333c9..fc6efeb6c 100644 --- a/PKHeX.Core/Legality/Encounters/EncounterStatic.cs +++ b/PKHeX.Core/Legality/Encounters/EncounterStatic.cs @@ -1,4 +1,6 @@ -namespace PKHeX.Core +using System; + +namespace PKHeX.Core { /// /// Static Encounter Data @@ -64,6 +66,87 @@ private const string _name = "Static Encounter"; public string Name => Version == GameVersion.Any ? _name : $"{_name} ({Version})"; - public PKM ConvertToPKM(ITrainerInfo SAV) => throw new System.NotImplementedException(); + public PKM ConvertToPKM(ITrainerInfo SAV) + { + var version = this.GetCompatibleVersion((GameVersion)SAV.Game); + int lang = (int)Legal.GetSafeLanguage(Generation, (LanguageID)SAV.Language); + int level = LevelMin; + var pk = PKMConverter.GetBlank(Generation); + + pk.EncryptionConstant = Util.Rand32(); + pk.Species = Species; + pk.Language = lang; + pk.CurrentLevel = level; + pk.Version = (int)version; + pk.PID = Util.Rand32(); + pk.Nickname = PKX.GetSpeciesNameGeneration(Species, lang, Generation); + pk.Ball = Ball; + pk.Met_Level = level; + pk.Met_Location = Location; + var today = DateTime.Today; + pk.MetDate = today; + if (EggEncounter) + { + pk.Egg_Location = EggLocation; + pk.EggMetDate = today; + } + + int nature = Nature == Nature.Random ? Util.Rand.Next(25) : (int)Nature; + pk.Nature = nature; + int gender = Gender < 0 ? Util.Rand.Next(2) : Gender; + pk.Gender = pk.GetSaneGender(gender); + pk.AltForm = Form; + + SAV.ApplyToPKM(pk); + pk.Language = lang; + + pk.RefreshAbility(Ability >> 1); + + if (IVs != null) + pk.SetRandomIVs(IVs, FlawlessIVCount); + else + pk.SetRandomIVs(flawless: FlawlessIVCount); + + switch (pk.Format) + { + case 3: + case 4: + PIDGenerator.SetValuesFromSeed(pk, Roaming ? PIDType.Method_1_Roamer : PIDType.Method_1, Util.Rand32()); + if (this is EncounterStaticTyped t) + pk.EncounterType = t.TypeEncounter.GetIndex(); + break; + case 6: + pk.SetRandomMemory6(); + break; + } + + if (this is EncounterStaticPID pid) + { + pk.PID = pid.PID; + if (pk is PK5 pk5) + pk5.NPokémon = pid.NSparkle; + } + + this.CopyContestStatsTo(pk); + + var moves = Moves ?? Legal.GetEncounterMoves(pk, level, version); + pk.Moves = moves; + pk.SetMaximumPPCurrent(moves); + if (pk.Format >= 6 && Relearn != null) + pk.RelearnMoves = Relearn; + pk.OT_Friendship = pk.PersonalInfo.BaseFriendship; + if (Fateful) + pk.FatefulEncounter = true; + + if (pk.Format < 6) + return pk; + if (RibbonWishing && pk is IRibbonSetEvent4 e4) + e4.RibbonWishing = true; + + SAV.ApplyHandlingTrainerInfo(pk); + pk.SetRandomEC(); + + return pk; + } } } diff --git a/PKHeX.Core/Legality/Encounters/EncounterTrade.cs b/PKHeX.Core/Legality/Encounters/EncounterTrade.cs index 52364daae..aee0fcb96 100644 --- a/PKHeX.Core/Legality/Encounters/EncounterTrade.cs +++ b/PKHeX.Core/Legality/Encounters/EncounterTrade.cs @@ -1,4 +1,6 @@ -namespace PKHeX.Core +using System; + +namespace PKHeX.Core { /// /// Trade Encounter data @@ -54,6 +56,67 @@ 0, 126, 254, 2001, 30002, 30001, 30001, }; - public PKM ConvertToPKM(ITrainerInfo SAV) => throw new System.NotImplementedException(); + public PKM ConvertToPKM(ITrainerInfo SAV) + { + var version = this.GetCompatibleVersion((GameVersion)SAV.Game); + int lang = (int)Legal.GetSafeLanguage(Generation, (LanguageID)SAV.Language); + int level = CurrentLevel > 0 ? CurrentLevel : LevelMin; + var pk = PKMConverter.GetBlank(Generation); + + pk.EncryptionConstant = Util.Rand32(); + pk.Species = Species; + pk.Language = lang; + pk.CurrentLevel = level; + pk.Version = (int)version; + pk.PID = Util.Rand32(); + pk.Ball = Ball; + pk.Met_Level = LevelMin; + pk.Met_Location = Location; + pk.MetDate = DateTime.Today; + + int nature = Nature == Nature.Random ? Util.Rand.Next(25) : (int)Nature; + pk.Nature = nature; + int gender = Gender < 0 ? Util.Rand.Next(2) : Gender; + pk.Gender = pk.GetSaneGender(gender); + pk.AltForm = Form; + + SAV.ApplyToPKM(pk); + pk.TID = TID; + pk.SID = SID; + pk.OT_Name = GetOT(lang) ?? SAV.OT; + pk.OT_Gender = GetOT(lang) != null ? OTGender : SAV.Gender; + pk.SetNickname(GetNickname(lang)); + pk.Language = lang; + + pk.RefreshAbility(Ability >> 1); + + if (IVs != null) + pk.SetRandomIVs(IVs, 0); + else + pk.SetRandomIVs(flawless: 3); + + if (pk.Format == 6) + pk.SetRandomMemory6(); + + if (pk is PK1 pk1 && this is EncounterTradeCatchRate c) + pk1.Catch_Rate = (int)c.Catch_Rate; + + this.CopyContestStatsTo(pk); + + var moves = Moves ?? Legal.GetEncounterMoves(pk, level, version); + pk.Moves = moves; + pk.SetMaximumPPCurrent(moves); + pk.OT_Friendship = pk.PersonalInfo.BaseFriendship; + if (Fateful) + pk.FatefulEncounter = true; + + if (pk.Format < 6) + return pk; + + SAV.ApplyHandlingTrainerInfo(pk); + pk.SetRandomEC(); + + return pk; + } } } diff --git a/PKHeX.Core/Legality/Enums/EncounterType.cs b/PKHeX.Core/Legality/Enums/EncounterType.cs index 1848cb97a..9c3423a8a 100644 --- a/PKHeX.Core/Legality/Enums/EncounterType.cs +++ b/PKHeX.Core/Legality/Enums/EncounterType.cs @@ -28,5 +28,17 @@ namespace PKHeX.Core public static class EncounterTypeExtension { public static bool Contains(this EncounterType g1, int g2) => g1.HasFlag((EncounterType)(1 << g2)); + + public static int GetIndex(this EncounterType g) + { + int val = (int) g; + for (int i = 0; i < 8 * sizeof(EncounterType); i++) + { + val >>= 1; + if (val == 0) + return i; + } + return 0; + } } } diff --git a/PKHeX.Core/PKM/Editing/CommonEdits.cs b/PKHeX.Core/PKM/Editing/CommonEdits.cs index b3b90bcac..908ce1550 100644 --- a/PKHeX.Core/PKM/Editing/CommonEdits.cs +++ b/PKHeX.Core/PKM/Editing/CommonEdits.cs @@ -492,5 +492,13 @@ namespace PKHeX.Core pk.OT_Intensity = 1; pk.OT_TextVar = pk.XY ? 43 : 27; // riverside road : battling spot } + + public static void SetRandomMemory6(this PKM pk) + { + // for lack of better randomization :) + pk.OT_Memory = 63; + pk.OT_Intensity = 6; + pk.OT_Feeling = 3; + } } } diff --git a/PKHeX.Core/PKM/PKM.cs b/PKHeX.Core/PKM/PKM.cs index 2f2a4899d..7b3e2e4c4 100644 --- a/PKHeX.Core/PKM/PKM.cs +++ b/PKHeX.Core/PKM/PKM.cs @@ -921,6 +921,22 @@ namespace PKHeX.Core return ivs; } + /// + /// Randomizes the IVs within game constraints. + /// + /// Randomized IVs if desired. + 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 ? (int) (Util.Rand32() & MaxIV) : template[i]; + } while (ivs.Count(z => z == MaxIV) < count); + return ivs; + } + /// /// Gets the amount of flawless IVs that the should have. /// diff --git a/PKHeX.Core/PKM/Shared/IContestStats.cs b/PKHeX.Core/PKM/Shared/IContestStats.cs index 79b269111..3fa0465b7 100644 --- a/PKHeX.Core/PKM/Shared/IContestStats.cs +++ b/PKHeX.Core/PKM/Shared/IContestStats.cs @@ -42,5 +42,15 @@ return false; return true; } + + public static void CopyContestStatsTo(this IContestStats source, IContestStats dest) + { + dest.CNT_Cool = source.CNT_Cool; + dest.CNT_Beauty = source.CNT_Beauty; + dest.CNT_Cute = source.CNT_Cute; + dest.CNT_Smart = source.CNT_Smart; + dest.CNT_Tough = source.CNT_Tough; + dest.CNT_Sheen = source.CNT_Sheen; + } } }