Pokédex refactoring

Add ability to get SeenCount & CaughtCount
switch gen1/2/3 get/set to use int species (since that's all they end up
using), removes the need to create a pk6 template for setting the flag
via the dex editor.
This commit is contained in:
Kurt 2017-03-14 22:41:15 -07:00
parent e33b5323d3
commit 9da79379f8
9 changed files with 225 additions and 143 deletions

View file

@ -16,12 +16,11 @@ namespace PKHeX.WinForms
string[] spec = Util.getSpeciesList(Main.curlanguage);
for (int i = 0; i < seen.Length; i++)
{
PKM tempPkm = new PK6();
tempPkm.Species = i + 1;
seen[i] = SAV.getSeen(tempPkm);
caught[i] = SAV.getCaught(tempPkm);
CLB_Seen.Items.Add(spec[i + 1]);
CLB_Caught.Items.Add(spec[i + 1]);
int species = i + 1;
seen[i] = SAV.getSeen(species);
caught[i] = SAV.getCaught(species);
CLB_Seen.Items.Add(spec[species]);
CLB_Caught.Items.Add(spec[species]);
CLB_Seen.SetItemChecked(i, seen[i]);
CLB_Caught.SetItemChecked(i, caught[i]);
}
@ -37,10 +36,9 @@ namespace PKHeX.WinForms
{
for (int i = 0; i < seen.Length; i++)
{
PKM tempPkm = new PK6();
tempPkm.Species = i + 1;
SAV.setSeen(tempPkm, seen[i]);
SAV.setCaught(tempPkm, caught[i]);
int species = i + 1;
SAV.setSeen(species, seen[i]);
SAV.setCaught(species, caught[i]);
}
SAV.Data.CopyTo(Main.SAV.Data, 0);
Main.SAV.Edited = true;

View file

@ -436,69 +436,62 @@ namespace PKHeX.Core
}
// Pokédex
public override bool getSeen(PKM pkm)
private int PokedexSeenOffset => Japanese ? 0x25B1 : 0x25B6;
private int PokedexCaughtOffset => Japanese ? 0x259E : 0x25A3;
protected override void setDex(PKM pkm)
{
if (pkm.Species == 0)
return false;
if (pkm.Species > MaxSpeciesID)
return false;
if (Version == GameVersion.Unknown)
return false;
int species = pkm.Species;
if (!canSetDex(species))
return;
int bit = pkm.Species - 1;
int ofs = bit >> 3;
byte bitval = (byte)(1 << (bit & 7));
// Get the Seen Flag
return (Data[(Japanese ? 0x25B1 : 0x25B6) + ofs] & bitval) != 0;
setCaught(pkm.Species, true);
setSeen(pkm.Species, true);
}
public override bool getCaught(PKM pkm)
private bool canSetDex(int species)
{
if (pkm.Species == 0)
if (species <= 0)
return false;
if (pkm.Species > MaxSpeciesID)
if (species > MaxSpeciesID)
return false;
if (Version == GameVersion.Unknown)
return false;
int bit = pkm.Species - 1;
int ofs = bit >> 3;
byte bitval = (byte)(1 << (bit & 7));
// Get the Caught Flag
return (Data[(Japanese ? 0x259E : 0x25A3) + ofs] & bitval) != 0;
return true;
}
public override void setSeen(PKM pkm, bool seen = true)
public override void setSeen(int species, bool seen)
{
if (pkm.Species == 0)
return;
if (pkm.Species > MaxSpeciesID)
return;
if (Version == GameVersion.Unknown)
return;
int bit = pkm.Species - 1;
int bit = species - 1;
int ofs = bit >> 3;
byte bitval = (byte)(1 << (bit & 7));
// Set the Seen Flag
Data[(Japanese ? 0x25B1 : 0x25B6) + ofs] &= (byte)~bitval;
if (seen)
Data[(Japanese ? 0x25B1 : 0x25B6) + ofs] |= bitval;
Data[PokedexSeenOffset + ofs] |= bitval;
else
Data[PokedexSeenOffset + ofs] &= (byte)~bitval;
}
public override void setCaught(PKM pkm, bool caught = true)
public override void setCaught(int species, bool caught)
{
if (pkm.Species == 0)
return;
if (pkm.Species > MaxSpeciesID)
return;
if (Version == GameVersion.Unknown)
return;
int bit = pkm.Species - 1;
int bit = species - 1;
int ofs = bit >> 3;
byte bitval = (byte)(1 << (bit & 7));
// Set the Captured Flag
Data[(Japanese ? 0x259E : 0x25A3) + ofs] &= (byte)~bitval;
if (caught)
Data[(Japanese ? 0x259E : 0x25A3) + ofs] |= bitval;
Data[PokedexCaughtOffset + ofs] |= bitval;
else
Data[PokedexCaughtOffset + ofs] &= (byte)~bitval;
}
public override bool getSeen(int species)
{
int bit = species - 1;
int ofs = bit >> 3;
byte bitval = (byte)(1 << (bit & 7));
return (Data[PokedexSeenOffset + ofs] & bitval) != 0;
}
public override bool getCaught(int species)
{
int bit = species - 1;
int ofs = bit >> 3;
byte bitval = (byte)(1 << (bit & 7));
return (Data[PokedexCaughtOffset + ofs] & bitval) != 0;
}
}
}

View file

@ -532,69 +532,39 @@ namespace PKHeX.Core
}
// Pokédex
public override bool getSeen(PKM pkm)
protected override void setDex(PKM pkm)
{
if (pkm.Species == 0)
return false;
if (pkm.Species > MaxSpeciesID)
return false;
if (Version == GameVersion.Unknown)
return false;
int species = pkm.Species;
if (!canSetDex(species))
return;
int bit = pkm.Species - 1;
int ofs = bit >> 3;
byte bitval = (byte)(1 << (bit & 7));
// Get the Seen Flag
return (Data[PokedexSeenOffset + ofs] & bitval) != 0;
setCaught(pkm.Species, true);
setSeen(pkm.Species, true);
}
public override bool getCaught(PKM pkm)
private bool canSetDex(int species)
{
if (pkm.Species == 0)
if (species <= 0)
return false;
if (pkm.Species > MaxSpeciesID)
if (species > MaxSpeciesID)
return false;
if (Version == GameVersion.Unknown)
return false;
int bit = pkm.Species - 1;
int ofs = bit >> 3;
byte bitval = (byte)(1 << (bit & 7));
// Get the Caught Flag
return (Data[PokedexCaughtOffset + ofs] & bitval) != 0;
return true;
}
public override void setSeen(PKM pkm, bool seen = true)
public override void setSeen(int species, bool seen)
{
if (pkm.Species == 0)
return;
if (pkm.Species > MaxSpeciesID)
return;
if (Version == GameVersion.Unknown)
return;
int bit = pkm.Species - 1;
int bit = species - 1;
int ofs = bit >> 3;
byte bitval = (byte)(1 << (bit & 7));
if (!seen)
{
// Clear the Seen Flag
if (seen)
Data[PokedexSeenOffset + ofs] |= bitval;
else
Data[PokedexSeenOffset + ofs] &= (byte)~bitval;
return;
}
// Set the Seen Flag
Data[PokedexSeenOffset + ofs] |= bitval;
}
public override void setCaught(PKM pkm, bool caught = true)
public override void setCaught(int species, bool caught)
{
if (pkm.Species == 0)
return;
if (pkm.Species > MaxSpeciesID)
return;
if (Version == GameVersion.Unknown)
return;
int bit = pkm.Species - 1;
int bit = species - 1;
int ofs = bit >> 3;
byte bitval = (byte)(1 << (bit & 7));
@ -607,14 +577,28 @@ namespace PKHeX.Core
// Set the Captured Flag
Data[PokedexCaughtOffset + ofs] |= bitval;
if (pkm.Species == 201) // Unown
{
// Give all Unown caught to prevent a crash on pokedex view
for (int i = 1; i <= 26; i++)
{
Data[PokedexSeenOffset + 0x1F + i] = (byte)i;
}
}
if (species != 201)
return;
// Give all Unown caught to prevent a crash on pokedex view
for (int i = 1; i <= 26; i++)
Data[PokedexSeenOffset + 0x1F + i] = (byte) i;
}
public override bool getSeen(int species)
{
int bit = species - 1;
int ofs = bit >> 3;
byte bitval = (byte)(1 << (bit & 7));
// Get the Seen Flag
return (Data[PokedexSeenOffset + ofs] & bitval) != 0;
}
public override bool getCaught(int species)
{
int bit = species - 1;
int ofs = bit >> 3;
byte bitval = (byte)(1 << (bit & 7));
// Get the Caught Flag
return (Data[PokedexCaughtOffset + ofs] & bitval) != 0;
}
}
}

View file

@ -489,7 +489,7 @@ namespace PKHeX.Core
}
private bool canSetDex(int species)
{
if (species == 0)
if (species <= 0)
return false;
if (species > MaxSpeciesID)
return false;
@ -499,17 +499,18 @@ namespace PKHeX.Core
return false;
return true;
}
private bool getCaught(int species)
public override bool getCaught(int species)
{
int bit = species - 1;
int ofs = bit / 8;
int ofs = bit >> 3;
byte bitval = (byte) (1 << (bit&7));
int caughtOffset = BlockOfs[0] + 0x28 + ofs;
return (Data[caughtOffset] & bitval) != 0;
}
private void setCaught(int species, bool caught)
public override void setCaught(int species, bool caught)
{
int bit = species - 1;
int ofs = bit / 8;
@ -521,16 +522,17 @@ namespace PKHeX.Core
else
Data[caughtOffset] &= (byte)~bitval;
}
private bool getSeen(int species)
public override bool getSeen(int species)
{
int bit = species - 1;
int ofs = bit / 8;
int ofs = bit >> 3;
byte bitval = (byte)(1 << (bit&7));
int seenOffset = BlockOfs[0] + 0x5C + ofs;
return (Data[seenOffset] & bitval) != 0;
}
private void setSeen(int species, bool seen)
public override void setSeen(int species, bool seen)
{
int bit = species - 1;
int ofs = bit / 8;
@ -547,10 +549,6 @@ namespace PKHeX.Core
Data[SeenFlagOffsets[i] + ofs] &= (byte) ~bitval;
}
}
public override bool getCaught(PKM pkm) => getCaught(pkm.Species);
public override void setCaught(PKM pkm, bool caught = true) => setCaught(pkm.Species, caught);
public override bool getSeen(PKM pkm) => getSeen(pkm.Species);
public override void setSeen(PKM pkm, bool seen = true) => setSeen(pkm.Species, seen);
public bool NationalDex
{

View file

@ -841,6 +841,28 @@ namespace PKHeX.Core
Data[PokeDexLanguageFlags + pkm.Species] |= (byte) (1 << lang);
}
public override bool getCaught(int species)
{
int bit = species - 1;
int bd = bit >> 3; // div8
int bm = bit & 7; // mod8
int ofs = PokeDex // Raw Offset
+ 0x4; // Magic
return (1 << bm & Data[ofs + bd]) != 0;
}
public override bool getSeen(int species)
{
const int brSize = 0x40;
int bit = species - 1;
int bd = bit >> 3; // div8
int bm = bit & 7; // mod8
int ofs = PokeDex // Raw Offset
+ 0x4; // Magic
return (1 << bm & Data[ofs + bd + brSize*1]) != 0;
}
public int[] getForms(int species)
{
const int brSize = 0x40;

View file

@ -670,5 +670,30 @@ namespace PKHeX.Core
bit = f + pkm.AltForm;
Data[FormDex + FormLen * (2 + shiny) + (bit>>3)] |= (byte)(1 << (bit&7));
}
public override bool getCaught(int species)
{
int bit = species - 1;
int bd = bit >> 3; // div8
int bm = bit & 7; // mod8
int ofs = PokeDex // Raw Offset
+ 0x08; // Magic + Flags
return (1 << bm & Data[ofs + bd]) != 0;
}
public override bool getSeen(int species)
{
const int brSize = 0x54;
int bit = species - 1;
int bd = bit >> 3; // div8
int bm = bit & 7; // mod8
int ofs = PokeDex // Raw Offset
+ 0x08; // Magic + Flags
for (int i = 1; i <= 4; i++)
if ((1 << bm & Data[ofs + bd + i * brSize]) != 0)
return true;
return false;
}
}
}

View file

@ -777,25 +777,29 @@ namespace PKHeX.Core
int origin = pkm.Version;
int gender = pkm.Gender % 2; // genderless -> male
int shiny = pkm.IsShiny ? 1 : 0;
int shiftoff = shiny * brSize * 2 + gender * brSize + brSize;
// Set the [Species/Gender/Shiny] Owned Flag
Data[PokeDex + shiftoff + bit / 8 + 0x8] |= (byte)(1 << (bit % 8));
int shiftoff = brSize*(1 + gender + 2*shiny); // after the Owned region
int bd = bit >> 3; // div8
int bm = bit & 7; // mod8
byte mask = (byte)(1 << bm);
int ofs = PokeDex + 0x8 + bd;
// Owned quality flag
if (origin < 0x18 && bit < 649 && !ORAS) // Species: 1-649 for X/Y, and not for ORAS; Set the Foreign Owned Flag
Data[PokeDex + 0x64C + bit / 8] |= (byte)(1 << (bit % 8));
Data[ofs + 0x644] |= mask;
else if (origin >= 0x18 || ORAS) // Set Native Owned Flag (should always happen)
Data[PokeDex + bit / 8 + 0x8] |= (byte)(1 << (bit % 8));
Data[ofs + brSize * 0] |= mask;
// Set the [Species/Gender/Shiny] Seen Flag
Data[ofs + shiftoff] |= mask;
// Set the Display flag if none are set
bool Displayed = false;
Displayed |= (Data[PokeDex + brSize * 5 + bit / 8 + 0x8] & (byte)(1 << (bit % 8))) != 0;
Displayed |= (Data[PokeDex + brSize * 6 + bit / 8 + 0x8] & (byte)(1 << (bit % 8))) != 0;
Displayed |= (Data[PokeDex + brSize * 7 + bit / 8 + 0x8] & (byte)(1 << (bit % 8))) != 0;
Displayed |= (Data[PokeDex + brSize * 8 + bit / 8 + 0x8] & (byte)(1 << (bit % 8))) != 0;
Displayed |= (Data[ofs + brSize * 5] & mask) != 0;
Displayed |= (Data[ofs + brSize * 6] & mask) != 0;
Displayed |= (Data[ofs + brSize * 7] & mask) != 0;
Displayed |= (Data[ofs + brSize * 8] & mask) != 0;
if (!Displayed) // offset is already biased by brSize, reuse shiftoff but for the display flags.
Data[PokeDex + shiftoff + brSize * 4 + bit / 8 + 0x8] |= (byte)(1 << (bit % 8));
Data[ofs + brSize * 4 + shiftoff] |= mask;
// Set the Language
if (lang < 0) lang = 1;
@ -829,6 +833,39 @@ namespace PKHeX.Core
bit = f + pkm.AltForm;
Data[FormDex + FormLen * (2 + shiny) + bit / 8] |= (byte)(1 << (bit % 8));
}
public override bool getCaught(int species)
{
int bit = species - 1;
int bd = bit >> 3; // div8
int bm = bit & 7; // mod8
int ofs = PokeDex // Raw Offset
+ 0x08; // Magic + Flags
if ((1 << bm & Data[ofs + bd]) != 0)
return true; // Owned Native
if (ORAS || bit >= 649) // no Foreign flag
return false;
return (1 << bm & Data[ofs + bd + 0x644]) != 0;
}
public override bool getSeen(int species)
{
const int brSize = 0x60;
int bit = species - 1;
int bd = bit >> 3; // div8
int bm = bit & 7; // mod8
byte mask = (byte)(1 << bm);
int ofs = PokeDex // Raw Offset
+ 0x08; // Magic + Flags
for (int i = 1; i <= 4; i++) // check all 4 seen flags (gender/shiny)
if ((Data[ofs + bd + i * brSize] & mask) != 0)
return true;
return false;
}
public override byte[] decryptPKM(byte[] data)
{
return PKX.decryptArray(data);

View file

@ -1035,6 +1035,34 @@ namespace PKHeX.Core
// Set the Display flag if none are set
Data[ofs + (4 + shift) * brSize + bd] |= (byte)(1 << bm);
}
public override bool getCaught(int species)
{
int bit = species - 1;
int bd = bit >> 3; // div8
int bm = bit & 7; // mod8
int ofs = PokeDex // Raw Offset
+ 0x08 // Magic + Flags
+ 0x80; // Misc Data (1024 bits)
return (1 << bm & Data[ofs + bd]) != 0;
}
public override bool getSeen(int species)
{
const int brSize = 0x8C;
int bit = species - 1;
int bd = bit >> 3; // div8
int bm = bit & 7; // mod8
byte mask = (byte)(1 << bm);
int ofs = PokeDex // Raw Offset
+ 0x08 // Magic + Flags
+ 0x80; // Misc Data (1024 bits)
for (int i = 1; i <= 4; i++) // check all 4 seen flags (gender/shiny)
if ((Data[ofs + bd + i * brSize] & mask) != 0)
return true;
return false;
}
public override byte[] decryptPKM(byte[] data)
{
return PKX.decryptArray(data);

View file

@ -628,17 +628,14 @@ namespace PKHeX.Core
}
protected virtual void setPKM(PKM pkm) { }
protected virtual void setDex(PKM pkm)
{
setSeen(pkm);
setCaught(pkm);
}
protected virtual void setDex(PKM pkm) { }
public virtual bool getSeen(int species) => false;
public virtual void setSeen(int species, bool seen) { }
public virtual bool getCaught(int species) => false;
public virtual void setCaught(int species, bool caught) { }
public int SeenCount => PokeDex < 0 ? 0 : new bool[MaxSpeciesID].Where((b, i) => getSeen(i+1)).Count();
public int CaughtCount => PokeDex < 0 ? 0 : new bool[MaxSpeciesID].Where((b, i) => getCaught(i+1)).Count();
public virtual bool getSeen(PKM pkm) { throw new NotImplementedException(); }
public virtual bool getCaught(PKM pkm) { throw new NotImplementedException(); }
public virtual void setSeen(PKM pkm, bool seen = true) { }
public virtual void setCaught(PKM pkm, bool caught = true) { }
public byte[] getData(int Offset, int Length)
{
if (Offset + Length > Data.Length)