PKHeX/PKHeX.Core/Legality/Encounters/EncounterMisc/EncounterEgg.cs
Kurt 0626b0c29b
Add Breeding move ordering logic, and use in legality analysis (#3183)
* Initial bred moveset validation logic

Unpeel the inheritance via recursion and permitted moves

* Volt tackle considerations

* Optimize out empty slot skips

* Add tests, fix off-by-one's

* Require all base moves if empty slot in moveset

* Add test to prove failure per Anubis' provided test

* Tweak enum labels for easier debugging

When two enums share the same underlying value, the ToString/name of the value may be either of the two (or the last defined one, in my debugging). Just give it a separate magic value.

* Fix recursion oopsie

Also check for scenario where no-base-moves but not enough moves to push base moves out

* Add Crystal tutor checks

* Add specialized gen2 verification method

Game loops through father's moves and pushes in one iteration, rather than checking by type.

* Add another case with returning base move

* Add push-out requirement for re-added base moves

* Minor tweaks

Condense tests, fix another off-by-one noticed when creating tests

* Disallow inherited parent levelup moves

Disallow volt tackle on Gen2/R/S

* Split MoveBreed into generation specific classes

Gen2 behaves slightly different from Gen3/4, which behaves slightly different from Gen5... and Gen6 behaves differently too.

Add some xmldoc as the api is starting to solidify

* Add method overload that returns the parse

Verify that the parse order is as expected

* Add reordering suggestion logic

Try sorting first, then go nuclear with rebuilding.

* Return base moves if complete fail

* Set base moves when generating eggs, only.

* Use breed logic to check for egg ordering legality

Don't bother helping for split-breed species
2021-04-04 18:30:01 -07:00

138 lines
4.3 KiB
C#

using System;
namespace PKHeX.Core
{
/// <summary>
/// Egg Encounter Data
/// </summary>
public sealed record EncounterEgg : IEncounterable
{
public int Species { get; }
public int Form { get; }
public string Name => "Egg";
public string LongName => "Egg";
public bool EggEncounter => true;
public int LevelMin => Level;
public int LevelMax => Level;
public readonly int Level;
public int Generation { get; }
public GameVersion Version { get; }
public EncounterEgg(int species, int form, int level, int gen, GameVersion game)
{
Species = species;
Form = form;
Level = level;
Generation = gen;
Version = game;
}
public PKM ConvertToPKM(ITrainerInfo sav) => ConvertToPKM(sav, EncounterCriteria.Unrestricted);
public PKM ConvertToPKM(ITrainerInfo sav, EncounterCriteria criteria)
{
int gen = Generation;
var version = Version;
var pk = PKMConverter.GetBlank(gen, version);
sav.ApplyTo(pk);
pk.Species = Species;
pk.Form = Form;
pk.Nickname = SpeciesName.GetSpeciesNameGeneration(Species, sav.Language, gen);
pk.CurrentLevel = Level;
pk.Version = (int)version;
pk.Ball = (int)Ball.Poke;
pk.OT_Friendship = pk.PersonalInfo.BaseFriendship;
SetEncounterMoves(pk, version);
pk.HealPP();
SetPINGA(pk, criteria);
if (gen <= 2 && version != GameVersion.C)
return pk;
SetMetData(pk);
if (gen < 3)
return pk;
if (gen >= 4)
pk.SetEggMetData(version, (GameVersion)sav.Game);
if (gen < 6)
return pk;
if (pk is PK6 pk6)
pk6.SetHatchMemory6();
SetForm(pk, sav);
pk.SetRandomEC();
pk.RelearnMove1 = pk.Move1;
pk.RelearnMove2 = pk.Move2;
pk.RelearnMove3 = pk.Move3;
pk.RelearnMove4 = pk.Move4;
return pk;
}
private void SetForm(PKM pk, ITrainerInfo sav)
{
switch (Species)
{
case (int)Core.Species.Minior:
pk.Form = Util.Rand.Next(7, 14);
break;
case (int)Core.Species.Scatterbug or (int)Core.Species.Spewpa or (int)Core.Species.Vivillon:
if (sav is IRegionOrigin o)
pk.Form = Vivillon3DS.GetPattern((byte)o.Country, (byte)o.Region);
// else 0
break;
}
}
private static void SetPINGA(PKM pk, EncounterCriteria criteria)
{
pk.SetRandomIVs(flawless: 3);
if (pk.Format <= 2)
return;
int gender = criteria.GetGender(-1, pk.PersonalInfo);
int nature = (int)criteria.GetNature(Nature.Random);
if (pk.Format <= 5)
{
pk.SetPIDGender(gender);
pk.Gender = gender;
pk.SetPIDNature(nature);
pk.Nature = nature;
pk.RefreshAbility(pk.PIDAbility);
}
else
{
pk.PID = Util.Rand32();
pk.Nature = nature;
pk.Gender = gender;
pk.RefreshAbility(Util.Rand.Next(2));
}
pk.StatNature = nature;
}
private static void SetMetData(PKM pk)
{
pk.Met_Level = EncounterSuggestion.GetSuggestedEncounterEggMetLevel(pk);
pk.Met_Location = Math.Max(0, EncounterSuggestion.GetSuggestedEggMetLocation(pk));
}
private void SetEncounterMoves(PKM pk, GameVersion version)
{
var learnset = GameData.GetLearnset(version, Species, Form);
var baseMoves = learnset.GetBaseEggMoves(Level);
if (baseMoves.Length == 0) return; pk.Move1 = baseMoves[0];
if (baseMoves.Length == 1) return; pk.Move2 = baseMoves[1];
if (baseMoves.Length == 2) return; pk.Move3 = baseMoves[2];
if (baseMoves.Length == 3) return; pk.Move4 = baseMoves[3];
}
}
}