2021-04-05 01:30:01 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
using static PKHeX.Core.EggSource2;
|
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
|
|
public static class MoveBreed2
|
2021-04-05 01:30:01 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
private const int level = 5;
|
2021-04-05 01:30:01 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
public static EggSource2[] Validate(int species, GameVersion version, ReadOnlySpan<int> moves, out bool valid)
|
|
|
|
|
{
|
|
|
|
|
var count = moves.IndexOf(0);
|
|
|
|
|
if (count == 0)
|
2021-04-05 01:30:01 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
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 actual = new EggSource2[count];
|
|
|
|
|
Span<byte> possible = stackalloc byte[count];
|
|
|
|
|
var value = new BreedInfo<EggSource2>(actual, possible, learnset, moves, level);
|
|
|
|
|
{
|
|
|
|
|
bool inherit = Breeding.GetCanInheritMoves(species);
|
|
|
|
|
MarkMovesForOrigin(value, egg, count, inherit, pi, version);
|
|
|
|
|
valid = RecurseMovesForOrigin(value, count - 1);
|
2021-04-05 01:30:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
if (!valid)
|
|
|
|
|
CleanResult(actual, possible);
|
|
|
|
|
return actual;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void CleanResult(EggSource2[] valueActual, Span<byte> valuePossible)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < valueActual.Length; i++)
|
2021-04-08 22:58:09 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
if (valueActual[i] != 0)
|
|
|
|
|
continue;
|
|
|
|
|
var poss = valuePossible[i];
|
|
|
|
|
if (poss == 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
for (int j = 0; j < (int) Max; j++)
|
2021-04-08 22:58:09 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
if ((poss & (1 << j)) == 0)
|
2021-04-08 22:58:09 +00:00
|
|
|
|
continue;
|
2022-06-18 18:04:24 +00:00
|
|
|
|
valueActual[i] = (EggSource2)j;
|
|
|
|
|
break;
|
2021-04-08 22:58:09 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
}
|
2021-04-08 22:58:09 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
private static bool RecurseMovesForOrigin(in BreedInfo<EggSource2> info, int start, EggSource2 type = Max)
|
|
|
|
|
{
|
|
|
|
|
int i = start;
|
|
|
|
|
do
|
2021-04-05 01:30:01 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
if (type != Base)
|
2021-04-05 01:30:01 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
if (RecurseMovesForOrigin(info, i, Base))
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2021-04-05 01:30:01 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
var flag = 1 << (int)Base;
|
|
|
|
|
if (type != Base)
|
|
|
|
|
flag = ~flag;
|
2021-04-05 01:30:01 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
var permit = info.Possible[i];
|
|
|
|
|
if ((permit & flag) == 0)
|
|
|
|
|
return false;
|
2021-04-05 01:30:01 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
info.Actual[i] = type == Base ? Base : GetFirstType(permit);
|
|
|
|
|
} while (--i >= 0);
|
2021-04-05 01:30:01 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
return VerifyBaseMoves(info);
|
|
|
|
|
}
|
2021-04-05 01:30:01 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
private static EggSource2 GetFirstType(byte permit)
|
|
|
|
|
{
|
|
|
|
|
for (var type = FatherEgg; type < Max; type++)
|
2021-04-05 01:30:01 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
if ((permit & (1 << (int)type)) != 0)
|
|
|
|
|
return type;
|
2021-04-05 01:30:01 +00:00
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(permit), permit, null);
|
|
|
|
|
}
|
2021-04-05 01:30:01 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
private static bool VerifyBaseMoves(in BreedInfo<EggSource2> info)
|
|
|
|
|
{
|
|
|
|
|
var count = 0;
|
|
|
|
|
foreach (var x in info.Actual)
|
2021-04-05 01:30:01 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
if (x == Base)
|
|
|
|
|
count++;
|
|
|
|
|
else
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-04-05 01:30:01 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
var moves = info.Moves;
|
|
|
|
|
if (count == -1)
|
|
|
|
|
return moves[^1] != 0;
|
2021-04-05 01:30:01 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
var baseMoves = info.Learnset.GetBaseEggMoves(info.Level);
|
|
|
|
|
if (baseMoves.Length < count)
|
|
|
|
|
return false;
|
|
|
|
|
if (moves[^1] == 0 && count != baseMoves.Length)
|
|
|
|
|
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)
|
2021-04-05 01:30:01 +00:00
|
|
|
|
return false;
|
2022-06-18 18:04:24 +00:00
|
|
|
|
}
|
2021-04-05 01:30:01 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
// 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;
|
2021-04-05 01:30:01 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
for (int i = count; i < info.Actual.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
var isBase = (info.Possible[i] & (1 << (int)Base)) != 0;
|
|
|
|
|
if (!isBase)
|
|
|
|
|
continue;
|
2021-04-05 01:30:01 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
var baseIndex = baseMoves.IndexOf(moves[i]);
|
|
|
|
|
var min = moves.Length - baseMoves.Length + baseIndex;
|
|
|
|
|
if (i < min + count)
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-04-05 01:30:01 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2021-04-05 01:30:01 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
private static void MarkMovesForOrigin(in BreedInfo<EggSource2> value, ICollection<int> eggMoves, int count, bool inheritLevelUp, PersonalInfo info, GameVersion version)
|
|
|
|
|
{
|
|
|
|
|
var possible = value.Possible;
|
|
|
|
|
var learn = value.Learnset;
|
|
|
|
|
var baseEgg = value.Learnset.GetBaseEggMoves(value.Level);
|
|
|
|
|
var tm = info.TMHM;
|
2021-04-05 01:30:01 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
var moves = value.Moves;
|
|
|
|
|
for (int i = 0; i < count; i++)
|
2021-04-05 01:30:01 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
var move = moves[i];
|
2021-04-05 01:30:01 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
if (baseEgg.IndexOf(move) != -1)
|
|
|
|
|
possible[i] |= 1 << (int)Base;
|
2021-04-05 01:30:01 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
if (inheritLevelUp && learn.GetLevelLearnMove(move) != -1)
|
|
|
|
|
possible[i] |= 1 << (int)ParentLevelUp;
|
2021-04-05 01:30:01 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
if (eggMoves.Contains(move))
|
|
|
|
|
possible[i] |= 1 << (int)FatherEgg;
|
2021-04-05 01:30:01 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
var tmIndex = Array.IndexOf(Legal.TMHM_GSC, move, 0, 50);
|
|
|
|
|
if (tmIndex != -1 && tm[tmIndex])
|
|
|
|
|
possible[i] |= 1 << (int)FatherTM;
|
2021-04-05 01:30:01 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
var hmIndex = Array.IndexOf(Legal.TMHM_GSC, move, 50);
|
|
|
|
|
if (hmIndex != -1 && tm[hmIndex + 50])
|
|
|
|
|
possible[i] |= 1 << (int)FatherTM;
|
2021-08-28 20:54:58 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
if (version is GameVersion.C)
|
|
|
|
|
{
|
|
|
|
|
var tutorIndex = Array.IndexOf(Legal.Tutors_GSC, move);
|
|
|
|
|
if (tutorIndex != -1 && tm[57 + tutorIndex])
|
|
|
|
|
possible[i] |= 1 << (int)Tutor;
|
2021-04-05 01:30:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|