From 8d5525363d40d43febacf3df823f295e5486dc6b Mon Sep 17 00:00:00 2001 From: Greg Edwards Date: Wed, 27 Oct 2021 01:00:40 -0400 Subject: [PATCH] Added support for calculating gender from PV. --- library/Pokedex/Species.cs | 3 ++ library/Structures/PokemonBase.cs | 56 +++++++++++++++++-------------- 2 files changed, 34 insertions(+), 25 deletions(-) diff --git a/library/Pokedex/Species.cs b/library/Pokedex/Species.cs index b6f86649..aad4d4eb 100644 --- a/library/Pokedex/Species.cs +++ b/library/Pokedex/Species.cs @@ -59,6 +59,9 @@ namespace PkmnFoundations.Pokedex public int NationalDex { get; private set; } public LocalizedString Name { get; private set; } public GrowthRates GrowthRate { get; private set; } + /// + /// Represents the odds, out of 8, of the Pokémon being female. 255 is reserved for genderless Pokémon. + /// public byte GenderRatio { get; private set; } public EggGroups EggGroup1 { get; private set; } public EggGroups EggGroup2 { get; private set; } diff --git a/library/Structures/PokemonBase.cs b/library/Structures/PokemonBase.cs index 82659a59..5054482b 100644 --- a/library/Structures/PokemonBase.cs +++ b/library/Structures/PokemonBase.cs @@ -18,30 +18,6 @@ namespace PkmnFoundations.Structures Initialize(); } - public PokemonBase(Pokedex.Pokedex pokedex, BinaryReader data) - : base() - { - m_pokedex = pokedex; - Initialize(); - Load(data); - } - - public PokemonBase(Pokedex.Pokedex pokedex, byte[] data) - : base() - { - m_pokedex = pokedex; - Initialize(); - Load(data); - } - - public PokemonBase(Pokedex.Pokedex pokedex, byte[] data, int offset) - : base() - { - m_pokedex = pokedex; - Initialize(); - Load(data, offset); - } - private void Initialize() { m_species_pair = Species.CreatePair(m_pokedex); @@ -130,7 +106,37 @@ namespace PkmnFoundations.Structures public ByteStatValues EVs { get; set; } public abstract byte Level { get; set; } - public abstract Genders Gender { get; set; } + public virtual Genders Gender + { + get + { + // https://bulbapedia.bulbagarden.net/wiki/Gender#In_Generations_III_to_V + var ratio = Species.GenderRatio; + switch (ratio) + { + case 0: + return Genders.Male; + case 8: + return Genders.Female; + case 255: + return Genders.None; + default: + { + // massage gender value into the corresponding value in bulbapedia table. + // This is off-by-one in true Game Freak fashion, causing a slight bias towards male Pokémon. + uint mangledRatio = ratio * 32u - 1; + uint pGender = Personality & 0xffu; + return pGender >= mangledRatio ? Genders.Male : Genders.Female; + } + } + } + set + { + // xxx: we don't really want this setter at all but we need to be able to override and provide a setter + // https://github.com/dotnet/csharplang/issues/1568 + throw new NotSupportedException(); + } + } public abstract String Nickname { get; set; } public virtual bool IsShiny