2017-07-29 18:54:52 +00:00
|
|
|
|
using System.Linq;
|
2017-05-28 04:17:53 +00:00
|
|
|
|
using static PKHeX.Core.LegalityCheckStrings;
|
|
|
|
|
|
|
|
|
|
namespace PKHeX.Core
|
|
|
|
|
{
|
2017-07-30 19:31:17 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Finds matching <see cref="IEncounterable"/> data and relevant <see cref="LegalInfo"/> for a <see cref="PKM"/>.
|
|
|
|
|
/// </summary>
|
2017-05-28 04:17:53 +00:00
|
|
|
|
public static class EncounterFinder
|
|
|
|
|
{
|
2017-07-30 19:31:17 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Iterates through all possible encounters until a sufficient match is found
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// The iterator lazily finds matching encounters, then verifies secondary checks to weed out any nonexact matches.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
/// <param name="pkm">Source data to find a match for</param>
|
|
|
|
|
/// <returns>
|
|
|
|
|
/// Information containing the matched encounter and any parsed checks.
|
|
|
|
|
/// If no clean match is found, the last checked match is returned.
|
|
|
|
|
/// If no match is found, an invalid encounter object is returned.
|
|
|
|
|
/// </returns>
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public static LegalInfo FindVerifiedEncounter(PKM pkm)
|
2017-05-28 04:17:53 +00:00
|
|
|
|
{
|
|
|
|
|
LegalInfo info = new LegalInfo(pkm);
|
2017-06-18 01:37:19 +00:00
|
|
|
|
var encounters = EncounterGenerator.GetEncounters(pkm, info);
|
2017-05-28 04:17:53 +00:00
|
|
|
|
|
|
|
|
|
using (var encounter = new PeekEnumerator<IEncounterable>(encounters.GetEnumerator()))
|
|
|
|
|
{
|
|
|
|
|
if (!encounter.PeekIsNext())
|
2017-07-12 16:02:03 +00:00
|
|
|
|
return VerifyWithoutEncounter(pkm, info);
|
2017-05-28 04:17:53 +00:00
|
|
|
|
|
2017-07-29 18:54:52 +00:00
|
|
|
|
var EncounterValidator = EncounterVerifier.GetEncounterVerifierMethod(pkm);
|
2017-05-28 04:17:53 +00:00
|
|
|
|
while (encounter.MoveNext())
|
|
|
|
|
{
|
2017-07-12 16:02:03 +00:00
|
|
|
|
info.EncounterMatch = encounter.Current;
|
|
|
|
|
var e = EncounterValidator(pkm, info);
|
2017-05-28 04:17:53 +00:00
|
|
|
|
if (!e.Valid && encounter.PeekIsNext())
|
2017-07-12 16:02:03 +00:00
|
|
|
|
{
|
|
|
|
|
info.Reject(e);
|
2017-05-28 04:17:53 +00:00
|
|
|
|
continue;
|
2017-07-12 16:02:03 +00:00
|
|
|
|
}
|
2017-05-28 04:17:53 +00:00
|
|
|
|
info.Parse.Add(e);
|
|
|
|
|
|
2017-07-29 18:54:52 +00:00
|
|
|
|
if (VerifySecondaryChecks(pkm, info, encounter))
|
2017-06-18 01:37:19 +00:00
|
|
|
|
break; // passes
|
|
|
|
|
}
|
2017-09-30 20:42:57 +00:00
|
|
|
|
|
|
|
|
|
if (!info.PIDIVMatches) // if false, all valid PIDIV matches have already been consumed
|
|
|
|
|
info.Parse.Add(new CheckResult(Severity.Invalid, V411, CheckIdentifier.PID));
|
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
return info;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-28 04:17:53 +00:00
|
|
|
|
|
2017-07-30 19:31:17 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Checks supplementary info to see if the encounter is still valid.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// When an encounter is initially validated, only encounter-related checks are performed.
|
|
|
|
|
/// By checking Moves, Evolution, and <see cref="PIDIV"/> data, a best match encounter can be found.
|
|
|
|
|
/// If the encounter is not valid, the method will not reject it unless another encounter is available to check.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
/// <param name="pkm">Source data to check the match for</param>
|
|
|
|
|
/// <param name="info">Information containing the matched encounter</param>
|
|
|
|
|
/// <param name="iterator">Peekable iterator </param>
|
|
|
|
|
/// <returns>Indication whether or not the encounter passes secondary checks</returns>
|
2017-07-29 18:54:52 +00:00
|
|
|
|
private static bool VerifySecondaryChecks(PKM pkm, LegalInfo info, PeekEnumerator<IEncounterable> iterator)
|
2017-06-18 01:37:19 +00:00
|
|
|
|
{
|
|
|
|
|
if (pkm.Format >= 6)
|
|
|
|
|
{
|
|
|
|
|
info.Relearn = VerifyRelearnMoves.VerifyRelearn(pkm, info);
|
|
|
|
|
if (info.Relearn.Any(z => !z.Valid) && iterator.PeekIsNext())
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
|
info.Relearn[i] = new CheckResult(CheckIdentifier.RelearnMove);
|
2017-05-28 04:17:53 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
info.Moves = VerifyCurrentMoves.VerifyMoves(pkm, info);
|
|
|
|
|
if (info.Moves.Any(z => !z.Valid) && iterator.PeekIsNext())
|
|
|
|
|
return false;
|
2017-05-28 04:17:53 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
var evo = EvolutionVerifier.VerifyEvolution(pkm, info);
|
|
|
|
|
if (!evo.Valid && iterator.PeekIsNext())
|
|
|
|
|
return false;
|
|
|
|
|
info.Parse.Add(evo);
|
2017-06-03 07:12:05 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
return true;
|
2017-05-28 04:17:53 +00:00
|
|
|
|
}
|
2017-07-30 19:31:17 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns legality info for an unmatched encounter scenario, including a hint as to what the actual match could be.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pkm">Source data to check the match for</param>
|
|
|
|
|
/// <param name="info">Information containing the unmatched encounter</param>
|
|
|
|
|
/// <returns>Updated information pertaining to the unmatched encounter</returns>
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private static LegalInfo VerifyWithoutEncounter(PKM pkm, LegalInfo info)
|
2017-05-28 04:17:53 +00:00
|
|
|
|
{
|
|
|
|
|
info.EncounterMatch = new EncounterInvalid(pkm);
|
|
|
|
|
|
|
|
|
|
string hint; // hint why an encounter was not found
|
|
|
|
|
if (pkm.WasGiftEgg)
|
|
|
|
|
hint = V359;
|
|
|
|
|
else if (pkm.WasEventEgg)
|
|
|
|
|
hint = V360;
|
|
|
|
|
else if (pkm.WasEvent)
|
|
|
|
|
hint = V78;
|
|
|
|
|
else
|
|
|
|
|
hint = V80;
|
|
|
|
|
|
|
|
|
|
info.Parse.Add(new CheckResult(Severity.Invalid, hint, CheckIdentifier.Encounter));
|
2017-06-18 01:37:19 +00:00
|
|
|
|
info.Relearn = VerifyRelearnMoves.VerifyRelearn(pkm, info);
|
|
|
|
|
info.Moves = VerifyCurrentMoves.VerifyMoves(pkm, info);
|
2017-05-28 04:17:53 +00:00
|
|
|
|
return info;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|