2018-11-01 22:38:09 +00:00
|
|
|
using System;
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
namespace PKHeX.Core
|
|
|
|
{
|
|
|
|
public abstract class Zukan
|
|
|
|
{
|
|
|
|
protected SaveFile SAV { get; set; }
|
|
|
|
protected int PokeDex { get; set; }
|
|
|
|
protected int PokeDexLanguageFlags { get; set; }
|
|
|
|
|
2019-06-22 23:53:25 +00:00
|
|
|
protected Zukan(SaveFile sav, int dex, int langflag)
|
|
|
|
{
|
|
|
|
SAV = sav;
|
|
|
|
PokeDex = dex;
|
|
|
|
PokeDexLanguageFlags = langflag;
|
|
|
|
if (langflag > dex)
|
|
|
|
throw new ArgumentException(nameof(langflag));
|
|
|
|
}
|
|
|
|
|
2018-11-01 22:38:09 +00:00
|
|
|
protected abstract int OFS_SEEN { get; }
|
|
|
|
protected abstract int OFS_CAUGHT { get; }
|
|
|
|
protected abstract int BitSeenSize { get; }
|
|
|
|
protected abstract int DexLangFlagByteCount { get; }
|
|
|
|
protected abstract int DexLangIDCount { get; }
|
|
|
|
protected abstract int GetDexLangFlag(int lang);
|
|
|
|
|
2018-11-14 03:14:23 +00:00
|
|
|
public Func<int, int, int, int> DexFormIndexFetcher { get; protected set; }
|
2018-11-01 22:38:09 +00:00
|
|
|
|
|
|
|
protected abstract bool GetSaneFormsToIterate(int species, out int formStart, out int formEnd, int formIn);
|
2018-11-14 03:14:23 +00:00
|
|
|
protected virtual void SetSpindaDexData(PKM pkm, bool alreadySeen) { }
|
2018-11-01 22:38:09 +00:00
|
|
|
protected abstract void SetAllDexFlagsLanguage(int bit, int lang, bool value = true);
|
|
|
|
protected abstract void SetAllDexSeenFlags(int baseBit, int altform, int gender, bool isShiny, bool value = true);
|
|
|
|
|
2018-11-14 03:14:23 +00:00
|
|
|
protected bool GetFlag(int ofs, int bitIndex) => SAV.GetFlag(PokeDex + ofs + (bitIndex >> 3), bitIndex);
|
|
|
|
protected void SetFlag(int ofs, int bitIndex, bool value = true) => SAV.SetFlag(PokeDex + ofs + (bitIndex >> 3), bitIndex, value);
|
2018-11-01 22:38:09 +00:00
|
|
|
|
2018-11-14 03:14:23 +00:00
|
|
|
public virtual bool GetCaught(int species) => GetFlag(OFS_CAUGHT, species - 1);
|
|
|
|
public virtual void SetCaught(int species, bool value = true) => SetFlag(OFS_CAUGHT, species - 1, value);
|
2018-11-01 22:38:09 +00:00
|
|
|
|
|
|
|
public int SeenCount => Enumerable.Range(1, SAV.MaxSpeciesID).Count(GetSeen);
|
|
|
|
public int CaughtCount => Enumerable.Range(1, SAV.MaxSpeciesID).Count(GetCaught);
|
|
|
|
public decimal PercentSeen => (decimal)SeenCount / SAV.MaxSpeciesID;
|
|
|
|
public decimal PercentCaught => (decimal)CaughtCount / SAV.MaxSpeciesID;
|
|
|
|
|
2018-11-14 03:14:23 +00:00
|
|
|
public virtual bool GetSeen(int species)
|
2018-11-01 22:38:09 +00:00
|
|
|
{
|
|
|
|
// check all 4 seen flags (gender/shiny)
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
2018-11-14 03:14:23 +00:00
|
|
|
if (GetSeen(species, i))
|
2018-11-01 22:38:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-14 03:14:23 +00:00
|
|
|
public bool GetSeen(int species, int i) => GetFlag(OFS_SEEN + (i * BitSeenSize), species - 1);
|
|
|
|
public void SetSeen(int species, int i, bool value) => SetFlag(OFS_SEEN + (i * BitSeenSize), species - 1, value);
|
|
|
|
|
|
|
|
public bool GetDisplayed(int bit, int i) => GetFlag(OFS_SEEN + ((i + 4) * BitSeenSize), bit);
|
|
|
|
public void SetDisplayed(int bit, int i, bool value) => SetFlag(OFS_SEEN + ((i + 4) * BitSeenSize), bit, value);
|
|
|
|
|
|
|
|
public bool GetLanguageFlag(int bit, int lang) => GetFlag(PokeDexLanguageFlags, (bit * DexLangIDCount) + lang);
|
|
|
|
public void SetLanguageFlag(int bit, int lang, bool value) => SetFlag(PokeDexLanguageFlags, (bit * DexLangIDCount) + lang, value);
|
|
|
|
|
|
|
|
public virtual void SetSeen(int species, bool value = true)
|
2018-11-01 22:38:09 +00:00
|
|
|
{
|
|
|
|
if (!value)
|
|
|
|
{
|
|
|
|
ClearSeen(species);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check all 4 seen flags (gender/shiny)
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
if (GetFlag(OFS_SEEN + (i * BitSeenSize), species - 1))
|
|
|
|
return;
|
|
|
|
}
|
2019-02-18 06:13:40 +00:00
|
|
|
var gender = SAV.Personal[species].RandomGender() & 1;
|
2018-11-01 22:38:09 +00:00
|
|
|
SetAllDexSeenFlags(species - 1, 0, gender, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ClearSeen(int species)
|
|
|
|
{
|
|
|
|
SetCaught(species, false);
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
SetFlag(OFS_SEEN + (i * BitSeenSize), species - 1, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void SetDex(PKM pkm)
|
|
|
|
{
|
|
|
|
if (PokeDex < 0 || SAV.Version == GameVersion.Invalid) // sanity
|
|
|
|
return;
|
|
|
|
if (pkm.Species == 0 || pkm.Species > SAV.MaxSpeciesID) // out of range
|
|
|
|
return;
|
|
|
|
if (pkm.IsEgg) // do not add
|
|
|
|
return;
|
|
|
|
|
2018-11-14 03:14:23 +00:00
|
|
|
int species = pkm.Species;
|
|
|
|
if (species == 327) // Spinda
|
|
|
|
SetSpindaDexData(pkm, GetSeen(species));
|
|
|
|
|
2018-11-01 22:38:09 +00:00
|
|
|
int bit = pkm.Species - 1;
|
2018-11-14 03:14:23 +00:00
|
|
|
int form = pkm.AltForm;
|
|
|
|
int gender = pkm.Gender & 1;
|
|
|
|
bool shiny = pkm.IsShiny;
|
|
|
|
int lang = pkm.Language;
|
|
|
|
SetDex(species, bit, form, gender, shiny, lang);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void SetDex(int species, int bit, int form, int gender, bool shiny, int lang)
|
|
|
|
{
|
|
|
|
SetCaught(species); // Set the Owned Flag
|
|
|
|
SetAllDexSeenFlags(bit, form, gender, shiny); // genderless -> male
|
|
|
|
SetAllDexFlagsLanguage(bit, lang);
|
2018-11-01 22:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected void SetDexFlags(int baseBit, int formBit, int gender, int shiny, bool value = true)
|
|
|
|
{
|
2019-07-13 06:43:12 +00:00
|
|
|
int shift = (gender & 1) | (shiny << 1);
|
2018-11-01 22:38:09 +00:00
|
|
|
|
|
|
|
// Set the [Species/Gender/Shiny] Seen Flag
|
|
|
|
SetFlag(OFS_SEEN + (shift * BitSeenSize), baseBit, value);
|
|
|
|
|
|
|
|
// Set the Display flag if none are set
|
2019-07-11 07:13:52 +00:00
|
|
|
SetDisplayedFlag(baseBit, formBit, value, shift);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void SetDisplayedFlag(int baseBit, int formBit, bool value, int shift)
|
|
|
|
{
|
2018-11-01 22:38:09 +00:00
|
|
|
bool displayed = GetIsSpeciesFormAnyDisplayed(baseBit, formBit);
|
|
|
|
if (!displayed || !value)
|
|
|
|
SetFlag(OFS_SEEN + ((4 + shift) * BitSeenSize), formBit, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool GetIsSpeciesFormAnyDisplayed(int baseBit, int formBit)
|
|
|
|
{
|
|
|
|
// Check Displayed Status for base form
|
2018-11-14 03:14:23 +00:00
|
|
|
for (int i = 0; i < 4; i++)
|
2018-11-01 22:38:09 +00:00
|
|
|
{
|
2018-11-14 03:14:23 +00:00
|
|
|
if (GetDisplayed(baseBit, i))
|
2018-11-01 22:38:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (baseBit == formBit)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If form is not base form, check form too
|
2018-11-14 03:14:23 +00:00
|
|
|
for (int i = 0; i < 4; i++)
|
2018-11-01 22:38:09 +00:00
|
|
|
{
|
2018-11-14 03:14:23 +00:00
|
|
|
if (GetDisplayed(formBit, i))
|
2018-11-01 22:38:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bulk Manipulation
|
2019-07-06 01:49:43 +00:00
|
|
|
public void SeenNone() => SetDexEntriesAll(false, shinyToo: true);
|
|
|
|
public void CaughtNone() => SetAllCaught(false, true);
|
|
|
|
public void SeenAll(bool shinyToo = false) => SetAllSeen(shinyToo);
|
|
|
|
public void CompleteDex(bool shinyToo = false) => SetDexEntriesAll(shinyToo: shinyToo);
|
2018-11-01 22:38:09 +00:00
|
|
|
|
2019-07-06 01:49:43 +00:00
|
|
|
public void CaughtAll(bool shinyToo = false)
|
|
|
|
{
|
|
|
|
SetAllSeen(true, shinyToo);
|
|
|
|
SetAllCaught(true, shinyToo);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetAllCaught(bool value = true, bool shinyToo = false)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < SAV.MaxSpeciesID; i++)
|
|
|
|
{
|
|
|
|
int species = i + 1;
|
|
|
|
SetCaught(species, value); // Set the Owned Flag
|
|
|
|
SetSeenSingle(i + 1, value, shinyToo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetAllSeen(bool value = true, bool shinyToo = false)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < SAV.MaxSpeciesID; i++)
|
|
|
|
SetSeenSingle(i + 1, value, shinyToo);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetDexEntriesAll(bool value = true, int max = -1, bool shinyToo = false)
|
2018-11-01 22:38:09 +00:00
|
|
|
{
|
|
|
|
if (max <= 0)
|
|
|
|
max = SAV.MaxSpeciesID;
|
2019-07-06 01:49:43 +00:00
|
|
|
|
2018-11-01 22:38:09 +00:00
|
|
|
for (int i = 1; i <= max; i++)
|
2019-07-06 01:49:43 +00:00
|
|
|
{
|
|
|
|
SetSeenSingle(i, value, shinyToo);
|
|
|
|
SetCaughtSingle(i, value);
|
|
|
|
}
|
2018-11-01 22:38:09 +00:00
|
|
|
}
|
|
|
|
|
2019-07-06 01:49:43 +00:00
|
|
|
public void SetCaughtSingle(int species, bool value = true)
|
2018-11-01 22:38:09 +00:00
|
|
|
{
|
|
|
|
SetCaught(species, value);
|
2019-07-06 01:49:43 +00:00
|
|
|
int baseBit = species - 1;
|
|
|
|
SetAllDexFlagsLanguage(baseBit, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetSeenSingle(int species, bool seen = true, bool shinyToo = false)
|
|
|
|
{
|
|
|
|
SetSeen(species, seen);
|
2018-11-01 22:38:09 +00:00
|
|
|
|
|
|
|
var entry = SAV.Personal[species];
|
|
|
|
int baseBit = species - 1;
|
|
|
|
int fc = entry.FormeCount;
|
|
|
|
for (int f = 0; f < fc; f++)
|
|
|
|
{
|
|
|
|
if (!entry.OnlyFemale)
|
|
|
|
{
|
2019-07-06 01:49:43 +00:00
|
|
|
SetAllDexSeenFlags(baseBit, f, 0, false, seen);
|
|
|
|
if (shinyToo)
|
|
|
|
SetAllDexSeenFlags(baseBit, f, 0, true, seen);
|
2018-11-01 22:38:09 +00:00
|
|
|
}
|
|
|
|
if (!entry.OnlyMale && !entry.Genderless)
|
|
|
|
{
|
2019-07-06 01:49:43 +00:00
|
|
|
SetAllDexSeenFlags(baseBit, f, 1, false, seen);
|
|
|
|
if (shinyToo)
|
|
|
|
SetAllDexSeenFlags(baseBit, f, 1, true, seen);
|
2018-11-01 22:38:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void SetAllDexFlagsLanguage(int bit, bool value = true)
|
|
|
|
{
|
|
|
|
for (int i = 1; i <= DexLangIDCount + 1; i++)
|
|
|
|
SetAllDexFlagsLanguage(bit, i, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|