From face8d64a965b493e593c8af5eacafa260697456 Mon Sep 17 00:00:00 2001 From: Kurt Date: Thu, 11 Jul 2019 00:13:52 -0700 Subject: [PATCH] Fix gen5 dexflag set Add unit tests to check Closes #2356 --- .../Saves/Substructures/PokeDex/Zukan.cs | 5 ++ .../Saves/Substructures/PokeDex/Zukan5.cs | 18 +++++ Tests/PKHeX.Core.Tests/Saves/PokeDex.cs | 68 +++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 Tests/PKHeX.Core.Tests/Saves/PokeDex.cs diff --git a/PKHeX.Core/Saves/Substructures/PokeDex/Zukan.cs b/PKHeX.Core/Saves/Substructures/PokeDex/Zukan.cs index e7c8ee5b3..5db344a0c 100644 --- a/PKHeX.Core/Saves/Substructures/PokeDex/Zukan.cs +++ b/PKHeX.Core/Saves/Substructures/PokeDex/Zukan.cs @@ -124,6 +124,11 @@ protected void SetDexFlags(int baseBit, int formBit, int gender, int shiny, bool SetFlag(OFS_SEEN + (shift * BitSeenSize), baseBit, value); // Set the Display flag if none are set + SetDisplayedFlag(baseBit, formBit, value, shift); + } + + protected virtual void SetDisplayedFlag(int baseBit, int formBit, bool value, int shift) + { bool displayed = GetIsSpeciesFormAnyDisplayed(baseBit, formBit); if (!displayed || !value) SetFlag(OFS_SEEN + ((4 + shift) * BitSeenSize), formBit, value); diff --git a/PKHeX.Core/Saves/Substructures/PokeDex/Zukan5.cs b/PKHeX.Core/Saves/Substructures/PokeDex/Zukan5.cs index dfa83d6c3..e749dd883 100644 --- a/PKHeX.Core/Saves/Substructures/PokeDex/Zukan5.cs +++ b/PKHeX.Core/Saves/Substructures/PokeDex/Zukan5.cs @@ -74,6 +74,24 @@ public override void SetDex(PKM pkm) private void SetCaughtFlag(int bit) => SetFlag(OFS_CAUGHT, bit); + protected override void SetDisplayedFlag(int baseBit, int formBit, bool value, int shift) + { + bool displayed = GetIsSpeciesAnyDisplayed(baseBit); + if (!displayed || !value) + SetFlag(OFS_SEEN + ((4 + shift) * BitSeenSize), baseBit, value); + } + + private bool GetIsSpeciesAnyDisplayed(int baseBit) + { + // Check Displayed Status for base form + for (int i = 0; i < 4; i++) + { + if (GetDisplayed(baseBit, i)) + return true; + } + return false; + } + private int FormLen => SAV.B2W2 ? 0xB : 0x9; private int FormDex => 0x8 + (BitSeenSize * 9); diff --git a/Tests/PKHeX.Core.Tests/Saves/PokeDex.cs b/Tests/PKHeX.Core.Tests/Saves/PokeDex.cs new file mode 100644 index 000000000..eaf4b1f63 --- /dev/null +++ b/Tests/PKHeX.Core.Tests/Saves/PokeDex.cs @@ -0,0 +1,68 @@ +using Xunit; +using FluentAssertions; + +namespace PKHeX.Core.Tests.Saves +{ + public static class PokeDex + { + [Theory] + [InlineData(Species.Bulbasaur)] + [InlineData(Species.Voltorb)] + [InlineData(Species.Genesect)] + public static void Gen5(Species species) + { + var bw = new SAV5B2W2(); + SetDexSpecies(bw, (int)species, 0x54); + } + + [Theory] + [InlineData(Species.Landorus)] + public static void Gen5Form(Species species) + { + var bw = new SAV5B2W2(); + SetDexSpecies(bw, (int)species, 0x54); + CheckDexFlags5(bw, (int)species, 0, 0x54, 0xB); + } + + private static void SetDexSpecies(SaveFile sav, int species, int regionSize) + { + var pk5 = new PK5 {Species = species, TID = 1337}; // non-shiny + + int boxofs = sav.GetBoxSlotOffset(0, 0); + sav.SetStoredSlot(pk5, boxofs); + + CheckFlags(sav, species, regionSize); + } + + private static void CheckFlags(SaveFile sav, int species, int regionSize) + { + var dex = sav.PokeDex; + var data = sav.Data; + + var bit = species - 1; + var val = (byte) (1 << (bit & 7)); + var ofs = bit >> 3; + data[dex + 0x08 + ofs].Should().Be(val, "caught flag"); + data[dex + 0x08 + regionSize + ofs].Should().Be(val, "seen flag"); + data[dex + 0x08 + regionSize + (regionSize * 4) + ofs].Should().Be(val, "displayed flag"); + } + + private static void CheckDexFlags5(SaveFile sav, int species, int form, int regionSize, int formRegionSize) + { + var dex = sav.PokeDex; + var data = sav.Data; + + var formDex = dex + 8 + regionSize * 9; + + int fc = sav.Personal[species].FormeCount; + var bit = ((SAV5)sav).Zukan.DexFormIndexFetcher(species, fc, 0); + if (bit < 0) + return; + bit += form; + var val = (byte)(1 << (bit & 7)); + var ofs = bit >> 3; + data[formDex + ofs].Should().Be(val, "seen flag"); + data[formDex + ofs + (formRegionSize * 2)].Should().Be(val, "displayed flag"); + } + } +}