PKHeX/PKHeX.Core/Legality/Moves/Breeding/MoveBreed2.cs

180 lines
5.9 KiB
C#
Raw Normal View History

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-05 01:30:01 +00:00
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using static PKHeX.Core.EggSource2;
namespace PKHeX.Core
{
public static class MoveBreed2
{
private const int level = 5;
public static EggSource2[] Validate(int species, GameVersion version, int[] moves, out bool valid)
{
var count = Array.IndexOf(moves, 0);
if (count == 0)
{
valid = false; // empty moveset
return Array.Empty<EggSource2>();
}
if (count == -1)
count = moves.Length;
var learn = GameData.GetLearnsets(version);
var table = GameData.GetPersonal(version);
var learnset = learn[species];
var pi = table[species];
var egg = (version == GameVersion.C ? Legal.EggMovesC : Legal.EggMovesGS)[species].Moves;
var value = new BreedInfo<EggSource2>(count, learnset, moves, level);
{
bool inherit = Breeding.GetCanInheritMoves(species);
MarkMovesForOrigin(value, egg, count, inherit, pi, version);
valid = RecurseMovesForOrigin(value, count - 1);
}
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-05 01:30:01 +00:00
if (!valid)
CleanResult(value.Actual, value.Possible);
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-05 01:30:01 +00:00
return value.Actual;
}
private static void CleanResult(EggSource2[] valueActual, byte[] valuePossible)
{
for (int i = 0; i < valueActual.Length; i++)
{
if (valueActual[i] != 0)
continue;
var poss = valuePossible[i];
if (poss == 0)
continue;
for (int j = 0; j < (int) Max; j++)
{
if ((poss & (1 << j)) == 0)
continue;
valueActual[i] = (EggSource2)j;
break;
}
}
}
private static bool RecurseMovesForOrigin(in BreedInfo<EggSource2> info, int start, EggSource2 type = Max)
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-05 01:30:01 +00:00
{
int i = start;
do
{
if (type != Base)
{
if (RecurseMovesForOrigin(info, i, Base))
return true;
}
var flag = 1 << (int)Base;
if (type != Base)
flag = ~flag;
var permit = info.Possible[i];
if ((permit & flag) == 0)
return false;
info.Actual[i] = type == Base ? Base : GetFirstType(permit);
} while (--i >= 0);
return VerifyBaseMoves(info);
}
private static EggSource2 GetFirstType(byte permit)
{
for (var type = FatherEgg; type < Max; type++)
{
if ((permit & (1 << (int)type)) != 0)
return type;
}
throw new ArgumentOutOfRangeException(nameof(permit), permit, null);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool VerifyBaseMoves(in BreedInfo<EggSource2> info)
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-05 01:30:01 +00:00
{
var count = 0;
foreach (var x in info.Actual)
{
if (x == Base)
count++;
else
break;
}
var moves = info.Moves;
if (count == -1)
return moves[^1] != 0;
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-05 01:30:01 +00:00
var baseMoves = info.Learnset.GetBaseEggMoves(info.Level);
if (baseMoves.Length < count)
return false;
if (moves[^1] == 0 && count != baseMoves.Length)
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-05 01:30:01 +00:00
return false;
for (int i = count - 1, b = baseMoves.Length - 1; i >= 0; i--, b--)
{
var move = moves[i];
var expect = baseMoves[b];
if (expect != move)
return false;
}
// A low-index base egg move may be nudged out, but can only reappear if sufficient non-base moves are before it.
if (baseMoves.Length == count)
return true;
for (int i = count; i < info.Actual.Length; i++)
{
var isBase = (info.Possible[i] & (1 << (int)Base)) != 0;
if (!isBase)
continue;
var baseIndex = baseMoves.IndexOf(moves[i]);
var min = moves.Length - baseMoves.Length + baseIndex;
if (i <= min + count)
return false;
}
return true;
}
private static void MarkMovesForOrigin(in BreedInfo<EggSource2> value, ICollection<int> eggMoves, int count, bool inheritLevelUp, PersonalInfo info, GameVersion version)
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-05 01:30:01 +00:00
{
var possible = value.Possible;
var learn = value.Learnset;
var baseEgg = value.Learnset.GetBaseEggMoves(value.Level);
var tm = info.TMHM;
var moves = value.Moves;
for (int i = 0; i < count; i++)
{
var move = moves[i];
if (baseEgg.IndexOf(move) != -1)
possible[i] |= 1 << (int)Base;
if (inheritLevelUp && learn.GetLevelLearnMove(move) != -1)
possible[i] |= 1 << (int)ParentLevelUp;
if (eggMoves.Contains(move))
possible[i] |= 1 << (int)FatherEgg;
var tmIndex = Array.IndexOf(Legal.TMHM_GSC, move, 0, 50);
if (tmIndex != -1 && tm[tmIndex])
possible[i] |= 1 << (int)FatherTM;
if (version is GameVersion.C)
{
var tutorIndex = Array.IndexOf(Legal.Tutors_GSC, move);
if (tutorIndex != -1 && tm[57 + tutorIndex])
possible[i] |= 1 << (int)Tutor;
}
}
}
}
}