2021-04-05 01:30:01 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
using static PKHeX.Core.EggSource34;
|
|
|
|
|
using static PKHeX.Core.GameVersion;
|
|
|
|
|
|
|
|
|
|
namespace PKHeX.Core
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Inheritance logic for Generation 4.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>Refer to <see cref="EggSource34"/> for inheritance ordering.</remarks>
|
|
|
|
|
public static class MoveBreed4
|
|
|
|
|
{
|
|
|
|
|
private const int level = 1;
|
|
|
|
|
|
2022-03-06 21:04:56 +00:00
|
|
|
|
public static EggSource34[] Validate(int species, GameVersion version, ReadOnlySpan<int> moves, out bool valid)
|
2021-04-05 01:30:01 +00:00
|
|
|
|
{
|
2022-03-06 21:04:56 +00:00
|
|
|
|
var count = moves.IndexOf(0);
|
2021-04-05 01:30:01 +00:00
|
|
|
|
if (count == 0)
|
|
|
|
|
{
|
|
|
|
|
valid = false; // empty moveset
|
|
|
|
|
return Array.Empty<EggSource34>();
|
|
|
|
|
}
|
|
|
|
|
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 is HG or SS ? Legal.EggMovesHGSS : Legal.EggMovesDPPt)[species].Moves;
|
|
|
|
|
|
2022-03-06 21:04:56 +00:00
|
|
|
|
var actual = new EggSource34[count];
|
|
|
|
|
Span<byte> possible = stackalloc byte[count];
|
|
|
|
|
var value = new BreedInfo<EggSource34>(actual, possible, learnset, moves, level);
|
2021-09-16 01:43:18 +00:00
|
|
|
|
if (species is (int)Species.Pichu && moves[count - 1] is (int)Move.VoltTackle)
|
2022-03-06 21:04:56 +00:00
|
|
|
|
actual[--count] = VoltTackle;
|
2021-04-08 22:58:09 +00:00
|
|
|
|
|
|
|
|
|
if (count == 0)
|
2021-04-05 01:30:01 +00:00
|
|
|
|
{
|
2021-04-08 22:58:09 +00:00
|
|
|
|
valid = VerifyBaseMoves(value);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
bool inherit = Breeding.GetCanInheritMoves(species);
|
2021-08-28 20:54:58 +00:00
|
|
|
|
MarkMovesForOrigin(value, egg, count, inherit, pi, version);
|
2021-04-08 22:58:09 +00:00
|
|
|
|
valid = RecurseMovesForOrigin(value, count - 1);
|
2021-04-05 01:30:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-08 22:58:09 +00:00
|
|
|
|
if (!valid)
|
2022-03-06 21:04:56 +00:00
|
|
|
|
CleanResult(actual, possible);
|
2021-04-05 01:30:01 +00:00
|
|
|
|
return value.Actual;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-06 21:04:56 +00:00
|
|
|
|
private static void CleanResult(EggSource34[] valueActual, Span<byte> valuePossible)
|
2021-04-08 22:58:09 +00:00
|
|
|
|
{
|
|
|
|
|
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] = (EggSource34)j;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static bool RecurseMovesForOrigin(in BreedInfo<EggSource34> info, int start, EggSource34 type = Max - 1)
|
2021-04-05 01:30:01 +00:00
|
|
|
|
{
|
|
|
|
|
int i = start;
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
var unpeel = type - 1;
|
|
|
|
|
if (unpeel != 0 && RecurseMovesForOrigin(info, i, unpeel))
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
var permit = info.Possible[i];
|
|
|
|
|
if ((permit & (1 << (int)type)) == 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
info.Actual[i] = type;
|
|
|
|
|
} while (--i >= 0);
|
|
|
|
|
|
|
|
|
|
return VerifyBaseMoves(info);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2021-04-08 22:58:09 +00:00
|
|
|
|
private static bool VerifyBaseMoves(in BreedInfo<EggSource34> info)
|
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)
|
2021-05-14 22:30:55 +00:00
|
|
|
|
return moves[^1] != 0;
|
2021-04-05 01:30:01 +00:00
|
|
|
|
|
|
|
|
|
var baseMoves = info.Learnset.GetBaseEggMoves(info.Level);
|
|
|
|
|
if (baseMoves.Length < count)
|
|
|
|
|
return false;
|
2021-05-14 22:30:55 +00:00
|
|
|
|
if (moves[^1] == 0 && count != baseMoves.Length)
|
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;
|
2022-03-13 01:08:49 +00:00
|
|
|
|
if (i < min + count)
|
2021-04-05 01:30:01 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-28 20:54:58 +00:00
|
|
|
|
private static void MarkMovesForOrigin(in BreedInfo<EggSource34> value, ICollection<int> eggMoves, int count, bool inheritLevelUp, PersonalInfo info, GameVersion gameVersion)
|
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 tmlist = Legal.TM_4.AsSpan(0, 92);
|
2021-08-28 20:54:58 +00:00
|
|
|
|
var hmlist = (gameVersion is HG or SS ? Legal.HM_HGSS : Legal.HM_DPPt).AsSpan();
|
2021-04-05 01:30:01 +00:00
|
|
|
|
|
|
|
|
|
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 = tmlist.IndexOf(move);
|
|
|
|
|
if (tmIndex != -1 && tm[tmIndex])
|
|
|
|
|
possible[i] |= 1 << (int)FatherTM;
|
2021-08-28 20:54:58 +00:00
|
|
|
|
|
|
|
|
|
var hmIndex = hmlist.IndexOf(move);
|
|
|
|
|
if (hmIndex != -1 && tm[hmIndex + 92])
|
|
|
|
|
possible[i] |= 1 << (int)FatherTM;
|
2021-04-05 01:30:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|