From e7bf0b9a2cde2af57b36c288ab763b1fa6e0f76d Mon Sep 17 00:00:00 2001 From: Kurt Date: Wed, 4 Jan 2017 22:25:45 -0800 Subject: [PATCH] Minor refactoring Switch EncounterGeneric to IEncounterable. A way to tie together all encounters. Added a few setters for mystery gifts if the code is ever reused in other projects for that purpose. No functional change. --- PKHeX/Legality/Core.cs | 16 +++++++------- PKHeX/Legality/Structures/EncounterGeneric.cs | 8 ------- PKHeX/Legality/Structures/EncounterLink.cs | 5 +++-- PKHeX/Legality/Structures/EncounterSlot.cs | 6 ++--- PKHeX/Legality/Structures/EncounterStatic.cs | 5 +++-- PKHeX/Legality/Structures/EncounterTrade.cs | 5 +++-- PKHeX/Legality/Structures/IEncounterable.cs | 8 +++++++ PKHeX/MysteryGifts/MysteryGift.cs | 7 +++--- PKHeX/MysteryGifts/PGF.cs | 8 ++----- PKHeX/MysteryGifts/PGT.cs | 22 +++++++++---------- PKHeX/MysteryGifts/PL6.cs | 3 ++- PKHeX/MysteryGifts/WC6.cs | 11 ++++++++-- PKHeX/MysteryGifts/WC7.cs | 11 ++++++++-- PKHeX/PKHeX.csproj | 2 +- 14 files changed, 66 insertions(+), 51 deletions(-) delete mode 100644 PKHeX/Legality/Structures/EncounterGeneric.cs create mode 100644 PKHeX/Legality/Structures/IEncounterable.cs diff --git a/PKHeX/Legality/Core.cs b/PKHeX/Legality/Core.cs index e38288597..fcf5b4028 100644 --- a/PKHeX/Legality/Core.cs +++ b/PKHeX/Legality/Core.cs @@ -654,10 +654,10 @@ namespace PKHeX if (Encounter is int) minspec = (int)Encounter; - else if (Encounter is EncounterGeneric[]) - minspec = vs.Reverse().First(s => ((EncounterGeneric[]) Encounter).Any(slot => slot.Species == s.Species)).Species; - else if (Encounter is EncounterGeneric) - minspec = vs.Reverse().First(s => ((EncounterGeneric) Encounter).Species == s.Species).Species; + else if (Encounter is IEncounterable[]) + minspec = vs.Reverse().First(s => ((IEncounterable[]) Encounter).Any(slot => slot.Species == s.Species)).Species; + else if (Encounter is IEncounterable) + minspec = vs.Reverse().First(s => ((IEncounterable) Encounter).Species == s.Species).Species; else minspec = vs.Last().Species; @@ -670,11 +670,11 @@ namespace PKHeX var t = Encounter; if (pkm.WasEgg) return "Egg"; - if (t is EncounterGeneric) - return ((EncounterGeneric)t).Name; - if (t is EncounterGeneric[]) + if (t is IEncounterable) + return ((IEncounterable)t).Name; + if (t is IEncounterable[]) { - var arr = (EncounterGeneric[])t; + var arr = (IEncounterable[])t; if (arr.Any()) return arr.First().Name; } diff --git a/PKHeX/Legality/Structures/EncounterGeneric.cs b/PKHeX/Legality/Structures/EncounterGeneric.cs deleted file mode 100644 index 40ac312ce..000000000 --- a/PKHeX/Legality/Structures/EncounterGeneric.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace PKHeX -{ - public abstract class EncounterGeneric - { - public virtual int Species { get; set; } - public abstract string Name { get; } - } -} diff --git a/PKHeX/Legality/Structures/EncounterLink.cs b/PKHeX/Legality/Structures/EncounterLink.cs index 65ed31712..0b5c3292e 100644 --- a/PKHeX/Legality/Structures/EncounterLink.cs +++ b/PKHeX/Legality/Structures/EncounterLink.cs @@ -1,7 +1,8 @@ namespace PKHeX { - public class EncounterLink : EncounterGeneric + public class EncounterLink : IEncounterable { + public int Species { get; set; } public int Level; public int Location = 30011; public int Ability = 1; @@ -19,6 +20,6 @@ public bool ORAS = false; public bool SM = false; - public override string Name => "Pokémon Link Gift"; + public string Name => "Pokémon Link Gift"; } } diff --git a/PKHeX/Legality/Structures/EncounterSlot.cs b/PKHeX/Legality/Structures/EncounterSlot.cs index 3e9495b1f..4be49da59 100644 --- a/PKHeX/Legality/Structures/EncounterSlot.cs +++ b/PKHeX/Legality/Structures/EncounterSlot.cs @@ -1,7 +1,8 @@ namespace PKHeX { - public class EncounterSlot : EncounterGeneric + public class EncounterSlot : IEncounterable { + public int Species { get; set; } public int Form; public int LevelMin; public int LevelMax; @@ -24,7 +25,6 @@ Pressure = template.Pressure; } - public override string Name => "Wild Encounter"; + public string Name => "Wild Encounter"; } - } diff --git a/PKHeX/Legality/Structures/EncounterStatic.cs b/PKHeX/Legality/Structures/EncounterStatic.cs index a5a57106b..b14554cec 100644 --- a/PKHeX/Legality/Structures/EncounterStatic.cs +++ b/PKHeX/Legality/Structures/EncounterStatic.cs @@ -1,7 +1,8 @@ namespace PKHeX { - public class EncounterStatic : EncounterGeneric + public class EncounterStatic : IEncounterable { + public int Species { get; set; } public int Level; public int Location = 0; @@ -24,6 +25,6 @@ public bool RibbonWishing = false; public bool SkipFormCheck = false; - public override string Name => "Static Encounter"; + public string Name => "Static Encounter"; } } diff --git a/PKHeX/Legality/Structures/EncounterTrade.cs b/PKHeX/Legality/Structures/EncounterTrade.cs index c8d4a21b8..7f35a1fc1 100644 --- a/PKHeX/Legality/Structures/EncounterTrade.cs +++ b/PKHeX/Legality/Structures/EncounterTrade.cs @@ -1,7 +1,8 @@ namespace PKHeX { - public class EncounterTrade : EncounterGeneric + public class EncounterTrade : IEncounterable { + public int Species { get; set; } public int Level; public int Location = 30001; @@ -16,6 +17,6 @@ public int Gender = -1; public int OTGender = -1; - public override string Name => "In-game Trade"; + public string Name => "In-game Trade"; } } diff --git a/PKHeX/Legality/Structures/IEncounterable.cs b/PKHeX/Legality/Structures/IEncounterable.cs new file mode 100644 index 000000000..9696ae8ae --- /dev/null +++ b/PKHeX/Legality/Structures/IEncounterable.cs @@ -0,0 +1,8 @@ +namespace PKHeX +{ + public interface IEncounterable + { + int Species { get; } + string Name { get; } + } +} diff --git a/PKHeX/MysteryGifts/MysteryGift.cs b/PKHeX/MysteryGifts/MysteryGift.cs index 1353ebec2..0f8b48052 100644 --- a/PKHeX/MysteryGifts/MysteryGift.cs +++ b/PKHeX/MysteryGifts/MysteryGift.cs @@ -3,7 +3,7 @@ using System.Linq; namespace PKHeX { - public abstract class MysteryGift : EncounterGeneric + public abstract class MysteryGift : IEncounterable { /// @@ -92,9 +92,10 @@ namespace PKHeX return getMysteryGift(data); } public string Type => GetType().Name; - public override string Name => $"Event Gift ({Type})"; + public string Name => $"Event Gift ({Type})"; // Properties + public virtual int Species { get { return -1; } set { } } public abstract bool GiftUsed { get; set; } public abstract string CardTitle { get; set; } public abstract int CardID { get; set; } @@ -109,7 +110,7 @@ namespace PKHeX public string getCardHeader() => (CardID > 0 ? $"Card #: {CardID:0000}" : "N/A") + $" - {CardTitle.Replace('\u3000',' ').Trim()}"; // Search Properties - public virtual int[] Moves => new int[4]; + public virtual int[] Moves { get { return new int[4]; } set { } } public virtual int[] RelearnMoves { get { return new int[4]; } set { } } public virtual bool IsShiny => false; public virtual bool IsEgg { get { return false; } set { } } diff --git a/PKHeX/MysteryGifts/PGF.cs b/PKHeX/MysteryGifts/PGF.cs index 2b2ed8e72..cb16fe9fa 100644 --- a/PKHeX/MysteryGifts/PGF.cs +++ b/PKHeX/MysteryGifts/PGF.cs @@ -46,8 +46,7 @@ namespace PKHeX public int Move2 { get { return BitConverter.ToUInt16(Data, 0x14); } set { BitConverter.GetBytes((ushort)value).CopyTo(Data, 0x14); } } public int Move3 { get { return BitConverter.ToUInt16(Data, 0x16); } set { BitConverter.GetBytes((ushort)value).CopyTo(Data, 0x16); } } public int Move4 { get { return BitConverter.ToUInt16(Data, 0x18); } set { BitConverter.GetBytes((ushort)value).CopyTo(Data, 0x18); } } - public override int Species { get { return BitConverter.ToUInt16(Data, 0x1A); } - } + public override int Species { get { return BitConverter.ToUInt16(Data, 0x1A); } set { BitConverter.GetBytes((ushort) value).CopyTo(Data, 0x1A); } } public int Form { get { return Data[0x1C]; } set { Data[0x1C] = (byte)value; } } public int Language { get { return Data[0x1D]; } set { Data[0x1D] = (byte)value; } } public string Nickname @@ -143,10 +142,7 @@ namespace PKHeX public int[] IVs => new[] { IV_HP, IV_ATK, IV_DEF, IV_SPE, IV_SPA, IV_SPD }; public bool IsNicknamed => Nickname.Length > 0; - public override int[] Moves - { - get { return new[] { Move1, Move2, Move3, Move4 }; } - } + public override int[] Moves => new[] { Move1, Move2, Move3, Move4 }; public override bool IsPokémon { get { return CardType == 1; } set { if (value) CardType = 1; } } public override bool IsItem { get { return CardType == 2; } set { if (value) CardType = 2; } } public bool IsPower { get { return CardType == 3; } set { if (value) CardType = 3; } } diff --git a/PKHeX/MysteryGifts/PGT.cs b/PKHeX/MysteryGifts/PGT.cs index bda39cf54..62b2cff06 100644 --- a/PKHeX/MysteryGifts/PGT.cs +++ b/PKHeX/MysteryGifts/PGT.cs @@ -61,11 +61,11 @@ namespace PKHeX } } - public override int Species => Gift.PK.Species; - public override int[] Moves => Gift.PK.Moves; - public override bool IsShiny => Gift.PK.IsShiny; - public override bool IsEgg => Gift.PK.IsEgg; - public override int HeldItem => Gift.PK.HeldItem; + public override int Species { get { return Gift.Species; } set { Gift.Species = value; } } + public override int[] Moves { get { return Gift.Moves; } set { Gift.Moves = value; } } + public override int HeldItem { get { return Gift.HeldItem; } set { Gift.HeldItem = value; } } + public override bool IsShiny => Gift.IsShiny; + public override bool IsEgg { get { return Gift.IsEgg; } set { Gift.IsEgg = value; } } public override PKM convertToPKM(SaveFile SAV) { @@ -146,15 +146,15 @@ namespace PKHeX private GiftType PGTGiftType { get { return (GiftType)Data[0]; } set {Data[0] = (byte)value; } } public bool IsHatched => PGTGiftType == GiftType.Pokémon; - public override bool IsEgg => PGTGiftType == GiftType.PokémonEgg; - public bool IsManaphyEgg => PGTGiftType == GiftType.ManaphyEgg; - public override bool IsItem { get { return PGTGiftType == GiftType.Item; } set { if (value) CardType = (int)GiftType.Item; } } + public override bool IsEgg { get { return PGTGiftType == GiftType.PokémonEgg; } set { if (value) { PGTGiftType = GiftType.PokémonEgg; PK.IsEgg = true; } } } + public bool IsManaphyEgg { get { return PGTGiftType == GiftType.ManaphyEgg; } set { if (value) PGTGiftType = GiftType.ManaphyEgg; } } + public override bool IsItem { get { return PGTGiftType == GiftType.Item; } set { if (value) PGTGiftType = GiftType.Item; } } public override bool IsPokémon { get { return PGTGiftType == GiftType.Pokémon || PGTGiftType == GiftType.PokémonEgg || PGTGiftType == GiftType.ManaphyEgg; } set { } } - public override int Species => PK.Species; - public override int[] Moves => PK.Moves; + public override int Species { get { return PK.Species; } set { PK.Species = value; } } + public override int[] Moves { get { return PK.Moves; } set { PK.Moves = value; } } + public override int HeldItem { get { return PK.HeldItem; } set { PK.HeldItem = value; } } public override bool IsShiny => PK.IsShiny; - public override int HeldItem => PK.HeldItem; public override PKM convertToPKM(SaveFile SAV) { diff --git a/PKHeX/MysteryGifts/PL6.cs b/PKHeX/MysteryGifts/PL6.cs index f5d9fa700..9cc4c0a24 100644 --- a/PKHeX/MysteryGifts/PL6.cs +++ b/PKHeX/MysteryGifts/PL6.cs @@ -171,7 +171,7 @@ namespace PKHeX set { BitConverter.GetBytes((ushort)value).CopyTo(Data, 0x4A3); } } } - public class PL6_PKM //: PokemonLink + public class PL6_PKM : IEncounterable { internal const int Size = 0xA0; @@ -328,5 +328,6 @@ namespace PKHeX } } + public string Name => "Pokémon Link"; } } \ No newline at end of file diff --git a/PKHeX/MysteryGifts/WC6.cs b/PKHeX/MysteryGifts/WC6.cs index 78d47d46e..433a25bdf 100644 --- a/PKHeX/MysteryGifts/WC6.cs +++ b/PKHeX/MysteryGifts/WC6.cs @@ -98,7 +98,7 @@ namespace PKHeX // Pokémon Properties public override bool IsPokémon { get { return CardType == 0; } set { if (value) CardType = 0; } } - public override bool IsShiny { get { return PIDType == 2; } } + public override bool IsShiny => PIDType == 2; public int TID { get { return BitConverter.ToUInt16(Data, 0x68); } set { BitConverter.GetBytes((ushort)value).CopyTo(Data, 0x68); } } @@ -225,7 +225,14 @@ namespace PKHeX public override int[] Moves { - get { return new[] {Move1, Move2, Move3, Move4}; } + get { return new[] { Move1, Move2, Move3, Move4 }; } + set + { + if (value.Length > 0) Move1 = value[0]; + if (value.Length > 1) Move2 = value[1]; + if (value.Length > 2) Move3 = value[2]; + if (value.Length > 3) Move4 = value[3]; + } } public override int[] RelearnMoves { diff --git a/PKHeX/MysteryGifts/WC7.cs b/PKHeX/MysteryGifts/WC7.cs index d399a6bae..75ab8b462 100644 --- a/PKHeX/MysteryGifts/WC7.cs +++ b/PKHeX/MysteryGifts/WC7.cs @@ -93,7 +93,7 @@ namespace PKHeX // Pokémon Properties public override bool IsPokémon { get { return CardType == 0; } set { if (value) CardType = 0; } } - public override bool IsShiny { get { return PIDType == 2; } } + public override bool IsShiny => PIDType == 2; public int TID { get { return BitConverter.ToUInt16(Data, 0x68); } set { BitConverter.GetBytes((ushort)value).CopyTo(Data, 0x68); } } @@ -222,7 +222,14 @@ namespace PKHeX public override int[] Moves { - get { return new[] {Move1, Move2, Move3, Move4}; } + get { return new[] { Move1, Move2, Move3, Move4 }; } + set + { + if (value.Length > 0) Move1 = value[0]; + if (value.Length > 1) Move2 = value[1]; + if (value.Length > 2) Move3 = value[2]; + if (value.Length > 3) Move4 = value[3]; + } } public override int[] RelearnMoves { diff --git a/PKHeX/PKHeX.csproj b/PKHeX/PKHeX.csproj index f3e85fa70..ebdae83ad 100644 --- a/PKHeX/PKHeX.csproj +++ b/PKHeX/PKHeX.csproj @@ -115,7 +115,7 @@ - +