PKHeX/PKHeX.Core/PKM/Searching/SearchSettings.cs

219 lines
7 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using static PKHeX.Core.GameVersion;
namespace PKHeX.Core.Searching;
/// <summary>
/// <see cref="PKM"/> search settings &amp; searcher
/// </summary>
public sealed class SearchSettings
{
public int Format { get; init; }
public int Generation { get; init; }
public ushort Species { get; init; }
public int Ability { get; init; } = -1;
public int Nature { get; init; } = -1;
public int Item { get; init; } = -1;
public int Version { get; init; } = -1;
public int HiddenPowerType { get; init; } = -1;
public SearchComparison SearchFormat { get; init; }
public SearchComparison SearchLevel { get; init; }
public bool? SearchShiny { get; set; }
public bool? SearchLegal { get; set; }
public bool? SearchEgg { get; set; }
public int? ESV { get; set; }
public int? Level { get; init; }
public int IVType { get; init; }
public int EVType { get; init; }
public CloneDetectionMethod SearchClones { get; set; }
public IList<string> BatchInstructions { get; init; } = Array.Empty<string>();
private StringInstruction[] BatchFilters { get; set; } = Array.Empty<StringInstruction>();
public readonly List<ushort> Moves = new();
// ReSharper disable once CollectionNeverUpdated.Global
/// <summary>
/// Extra Filters to be checked after all other filters have been checked.
/// </summary>
/// <remarks>Collection is iterated right before clones are checked.</remarks>
public List<Func<PKM, bool>> ExtraFilters { get; } = new();
/// <summary>
/// Adds a move to the required move list.
/// </summary>
/// <param name="move"></param>
public void AddMove(ushort move)
{
if (move != 0 && !Moves.Contains(move))
Moves.Add(move);
}
Track a PKM's Box,Slot,StorageFlags,Identifier metadata separately (#3222) * Track a PKM's Box,Slot,StorageFlags,Identifier metadata separately Don't store within the object, track the slot origin data separately. Batch editing now pre-filters if using Box/Slot/Identifier logic; split up mods/filters as they're starting to get pretty hefty. - Requesting a Box Data report now shows all slots in the save file (party, misc) - Can now exclude backup saves from database search via toggle (separate from settings preventing load entirely) - Replace some linq usages with direct code * Remove WasLink virtual in PKM Inline any logic, since we now have encounter objects to indicate matching, rather than the proto-legality logic checking properties of a PKM. * Use Fateful to directly check gen5 mysterygift origins No other encounter types in gen5 apply Fateful * Simplify double ball comparison Used to be separate for deferral cases, now no longer needed to be separate. * Grab move/relearn reference and update locally Fix relearn move identifier * Inline defog HM transfer preference check HasMove is faster than getting moves & checking contains. Skips allocation by setting values directly. * Extract more met location metadata checks: WasBredEgg * Replace Console.Write* with Debug.Write* There's no console output UI, so don't include them in release builds. * Inline WasGiftEgg, WasEvent, and WasEventEgg logic Adios legality tags that aren't entirely correct for the specific format. Just put the computations in EncounterFinder.
2021-06-23 03:23:48 +00:00
/// <summary>
/// Searches the input list, filtering out entries as specified by the settings.
/// </summary>
/// <param name="list">List of entries to search</param>
/// <returns>Search results that match all criteria</returns>
public IEnumerable<PKM> Search(IEnumerable<PKM> list)
{
BatchFilters = StringInstruction.GetFilters(BatchInstructions).ToArray();
var result = SearchInner(list);
Track a PKM's Box,Slot,StorageFlags,Identifier metadata separately (#3222) * Track a PKM's Box,Slot,StorageFlags,Identifier metadata separately Don't store within the object, track the slot origin data separately. Batch editing now pre-filters if using Box/Slot/Identifier logic; split up mods/filters as they're starting to get pretty hefty. - Requesting a Box Data report now shows all slots in the save file (party, misc) - Can now exclude backup saves from database search via toggle (separate from settings preventing load entirely) - Replace some linq usages with direct code * Remove WasLink virtual in PKM Inline any logic, since we now have encounter objects to indicate matching, rather than the proto-legality logic checking properties of a PKM. * Use Fateful to directly check gen5 mysterygift origins No other encounter types in gen5 apply Fateful * Simplify double ball comparison Used to be separate for deferral cases, now no longer needed to be separate. * Grab move/relearn reference and update locally Fix relearn move identifier * Inline defog HM transfer preference check HasMove is faster than getting moves & checking contains. Skips allocation by setting values directly. * Extract more met location metadata checks: WasBredEgg * Replace Console.Write* with Debug.Write* There's no console output UI, so don't include them in release builds. * Inline WasGiftEgg, WasEvent, and WasEventEgg logic Adios legality tags that aren't entirely correct for the specific format. Just put the computations in EncounterFinder.
2021-06-23 03:23:48 +00:00
if (SearchClones != CloneDetectionMethod.None)
Track a PKM's Box,Slot,StorageFlags,Identifier metadata separately (#3222) * Track a PKM's Box,Slot,StorageFlags,Identifier metadata separately Don't store within the object, track the slot origin data separately. Batch editing now pre-filters if using Box/Slot/Identifier logic; split up mods/filters as they're starting to get pretty hefty. - Requesting a Box Data report now shows all slots in the save file (party, misc) - Can now exclude backup saves from database search via toggle (separate from settings preventing load entirely) - Replace some linq usages with direct code * Remove WasLink virtual in PKM Inline any logic, since we now have encounter objects to indicate matching, rather than the proto-legality logic checking properties of a PKM. * Use Fateful to directly check gen5 mysterygift origins No other encounter types in gen5 apply Fateful * Simplify double ball comparison Used to be separate for deferral cases, now no longer needed to be separate. * Grab move/relearn reference and update locally Fix relearn move identifier * Inline defog HM transfer preference check HasMove is faster than getting moves & checking contains. Skips allocation by setting values directly. * Extract more met location metadata checks: WasBredEgg * Replace Console.Write* with Debug.Write* There's no console output UI, so don't include them in release builds. * Inline WasGiftEgg, WasEvent, and WasEventEgg logic Adios legality tags that aren't entirely correct for the specific format. Just put the computations in EncounterFinder.
2021-06-23 03:23:48 +00:00
{
var method = SearchUtil.GetCloneDetectMethod(SearchClones);
result = SearchUtil.GetExtraClones(result, method);
}
return result;
}
/// <summary>
/// Searches the input list, filtering out entries as specified by the settings.
/// </summary>
/// <param name="list">List of entries to search</param>
/// <returns>Search results that match all criteria</returns>
public IEnumerable<SlotCache> Search(IEnumerable<SlotCache> list)
{
BatchFilters = StringInstruction.GetFilters(BatchInstructions).ToArray();
var result = SearchInner(list);
if (SearchClones != CloneDetectionMethod.None)
{
var method = SearchUtil.GetCloneDetectMethod(SearchClones);
string GetHash(SlotCache z) => method(z.Entity);
result = SearchUtil.GetExtraClones(result, GetHash);
}
return result;
}
private IEnumerable<PKM> SearchInner(IEnumerable<PKM> list)
{
foreach (var pk in list)
{
if (!IsSearchMatch(pk))
continue;
yield return pk;
}
}
private IEnumerable<SlotCache> SearchInner(IEnumerable<SlotCache> list)
{
foreach (var entry in list)
{
var pk = entry.Entity;
if (!IsSearchMatch(pk))
continue;
yield return entry;
Track a PKM's Box,Slot,StorageFlags,Identifier metadata separately (#3222) * Track a PKM's Box,Slot,StorageFlags,Identifier metadata separately Don't store within the object, track the slot origin data separately. Batch editing now pre-filters if using Box/Slot/Identifier logic; split up mods/filters as they're starting to get pretty hefty. - Requesting a Box Data report now shows all slots in the save file (party, misc) - Can now exclude backup saves from database search via toggle (separate from settings preventing load entirely) - Replace some linq usages with direct code * Remove WasLink virtual in PKM Inline any logic, since we now have encounter objects to indicate matching, rather than the proto-legality logic checking properties of a PKM. * Use Fateful to directly check gen5 mysterygift origins No other encounter types in gen5 apply Fateful * Simplify double ball comparison Used to be separate for deferral cases, now no longer needed to be separate. * Grab move/relearn reference and update locally Fix relearn move identifier * Inline defog HM transfer preference check HasMove is faster than getting moves & checking contains. Skips allocation by setting values directly. * Extract more met location metadata checks: WasBredEgg * Replace Console.Write* with Debug.Write* There's no console output UI, so don't include them in release builds. * Inline WasGiftEgg, WasEvent, and WasEventEgg logic Adios legality tags that aren't entirely correct for the specific format. Just put the computations in EncounterFinder.
2021-06-23 03:23:48 +00:00
}
}
private bool IsSearchMatch(PKM pk)
{
if (!SearchSimple(pk))
return false;
if (!SearchIntermediate(pk))
return false;
if (!SearchComplex(pk))
return false;
foreach (var filter in ExtraFilters)
Track a PKM's Box,Slot,StorageFlags,Identifier metadata separately (#3222) * Track a PKM's Box,Slot,StorageFlags,Identifier metadata separately Don't store within the object, track the slot origin data separately. Batch editing now pre-filters if using Box/Slot/Identifier logic; split up mods/filters as they're starting to get pretty hefty. - Requesting a Box Data report now shows all slots in the save file (party, misc) - Can now exclude backup saves from database search via toggle (separate from settings preventing load entirely) - Replace some linq usages with direct code * Remove WasLink virtual in PKM Inline any logic, since we now have encounter objects to indicate matching, rather than the proto-legality logic checking properties of a PKM. * Use Fateful to directly check gen5 mysterygift origins No other encounter types in gen5 apply Fateful * Simplify double ball comparison Used to be separate for deferral cases, now no longer needed to be separate. * Grab move/relearn reference and update locally Fix relearn move identifier * Inline defog HM transfer preference check HasMove is faster than getting moves & checking contains. Skips allocation by setting values directly. * Extract more met location metadata checks: WasBredEgg * Replace Console.Write* with Debug.Write* There's no console output UI, so don't include them in release builds. * Inline WasGiftEgg, WasEvent, and WasEventEgg logic Adios legality tags that aren't entirely correct for the specific format. Just put the computations in EncounterFinder.
2021-06-23 03:23:48 +00:00
{
if (!filter(pk))
Track a PKM's Box,Slot,StorageFlags,Identifier metadata separately (#3222) * Track a PKM's Box,Slot,StorageFlags,Identifier metadata separately Don't store within the object, track the slot origin data separately. Batch editing now pre-filters if using Box/Slot/Identifier logic; split up mods/filters as they're starting to get pretty hefty. - Requesting a Box Data report now shows all slots in the save file (party, misc) - Can now exclude backup saves from database search via toggle (separate from settings preventing load entirely) - Replace some linq usages with direct code * Remove WasLink virtual in PKM Inline any logic, since we now have encounter objects to indicate matching, rather than the proto-legality logic checking properties of a PKM. * Use Fateful to directly check gen5 mysterygift origins No other encounter types in gen5 apply Fateful * Simplify double ball comparison Used to be separate for deferral cases, now no longer needed to be separate. * Grab move/relearn reference and update locally Fix relearn move identifier * Inline defog HM transfer preference check HasMove is faster than getting moves & checking contains. Skips allocation by setting values directly. * Extract more met location metadata checks: WasBredEgg * Replace Console.Write* with Debug.Write* There's no console output UI, so don't include them in release builds. * Inline WasGiftEgg, WasEvent, and WasEventEgg logic Adios legality tags that aren't entirely correct for the specific format. Just put the computations in EncounterFinder.
2021-06-23 03:23:48 +00:00
return false;
}
return true;
}
private bool SearchSimple(PKM pk)
{
if (Format > 0 && !SearchUtil.SatisfiesFilterFormat(pk, Format, SearchFormat))
return false;
if (Species != 0 && pk.Species != Species)
return false;
if (Ability > -1 && pk.Ability != Ability)
return false;
if (Nature > -1 && pk.StatNature != Nature)
return false;
if (Item > -1 && pk.HeldItem != Item)
return false;
if (Version > -1 && pk.Version != Version)
return false;
return true;
}
Track a PKM's Box,Slot,StorageFlags,Identifier metadata separately (#3222) * Track a PKM's Box,Slot,StorageFlags,Identifier metadata separately Don't store within the object, track the slot origin data separately. Batch editing now pre-filters if using Box/Slot/Identifier logic; split up mods/filters as they're starting to get pretty hefty. - Requesting a Box Data report now shows all slots in the save file (party, misc) - Can now exclude backup saves from database search via toggle (separate from settings preventing load entirely) - Replace some linq usages with direct code * Remove WasLink virtual in PKM Inline any logic, since we now have encounter objects to indicate matching, rather than the proto-legality logic checking properties of a PKM. * Use Fateful to directly check gen5 mysterygift origins No other encounter types in gen5 apply Fateful * Simplify double ball comparison Used to be separate for deferral cases, now no longer needed to be separate. * Grab move/relearn reference and update locally Fix relearn move identifier * Inline defog HM transfer preference check HasMove is faster than getting moves & checking contains. Skips allocation by setting values directly. * Extract more met location metadata checks: WasBredEgg * Replace Console.Write* with Debug.Write* There's no console output UI, so don't include them in release builds. * Inline WasGiftEgg, WasEvent, and WasEventEgg logic Adios legality tags that aren't entirely correct for the specific format. Just put the computations in EncounterFinder.
2021-06-23 03:23:48 +00:00
private bool SearchIntermediate(PKM pk)
{
if (Generation > 0 && !SearchUtil.SatisfiesFilterGeneration(pk, Generation))
return false;
if (Moves.Count > 0 && !SearchUtil.SatisfiesFilterMoves(pk, Moves))
return false;
if (HiddenPowerType > -1 && pk.HPType != HiddenPowerType)
return false;
if (SearchShiny != null && pk.IsShiny != SearchShiny)
return false;
Track a PKM's Box,Slot,StorageFlags,Identifier metadata separately (#3222) * Track a PKM's Box,Slot,StorageFlags,Identifier metadata separately Don't store within the object, track the slot origin data separately. Batch editing now pre-filters if using Box/Slot/Identifier logic; split up mods/filters as they're starting to get pretty hefty. - Requesting a Box Data report now shows all slots in the save file (party, misc) - Can now exclude backup saves from database search via toggle (separate from settings preventing load entirely) - Replace some linq usages with direct code * Remove WasLink virtual in PKM Inline any logic, since we now have encounter objects to indicate matching, rather than the proto-legality logic checking properties of a PKM. * Use Fateful to directly check gen5 mysterygift origins No other encounter types in gen5 apply Fateful * Simplify double ball comparison Used to be separate for deferral cases, now no longer needed to be separate. * Grab move/relearn reference and update locally Fix relearn move identifier * Inline defog HM transfer preference check HasMove is faster than getting moves & checking contains. Skips allocation by setting values directly. * Extract more met location metadata checks: WasBredEgg * Replace Console.Write* with Debug.Write* There's no console output UI, so don't include them in release builds. * Inline WasGiftEgg, WasEvent, and WasEventEgg logic Adios legality tags that aren't entirely correct for the specific format. Just put the computations in EncounterFinder.
2021-06-23 03:23:48 +00:00
if (IVType > 0 && !SearchUtil.SatisfiesFilterIVs(pk, IVType))
return false;
if (EVType > 0 && !SearchUtil.SatisfiesFilterEVs(pk, EVType))
return false;
Track a PKM's Box,Slot,StorageFlags,Identifier metadata separately (#3222) * Track a PKM's Box,Slot,StorageFlags,Identifier metadata separately Don't store within the object, track the slot origin data separately. Batch editing now pre-filters if using Box/Slot/Identifier logic; split up mods/filters as they're starting to get pretty hefty. - Requesting a Box Data report now shows all slots in the save file (party, misc) - Can now exclude backup saves from database search via toggle (separate from settings preventing load entirely) - Replace some linq usages with direct code * Remove WasLink virtual in PKM Inline any logic, since we now have encounter objects to indicate matching, rather than the proto-legality logic checking properties of a PKM. * Use Fateful to directly check gen5 mysterygift origins No other encounter types in gen5 apply Fateful * Simplify double ball comparison Used to be separate for deferral cases, now no longer needed to be separate. * Grab move/relearn reference and update locally Fix relearn move identifier * Inline defog HM transfer preference check HasMove is faster than getting moves & checking contains. Skips allocation by setting values directly. * Extract more met location metadata checks: WasBredEgg * Replace Console.Write* with Debug.Write* There's no console output UI, so don't include them in release builds. * Inline WasGiftEgg, WasEvent, and WasEventEgg logic Adios legality tags that aren't entirely correct for the specific format. Just put the computations in EncounterFinder.
2021-06-23 03:23:48 +00:00
return true;
}
private bool SearchComplex(PKM pk)
{
if (SearchEgg != null && !FilterResultEgg(pk))
return false;
if (Level is { } x and not 0 && !SearchUtil.SatisfiesFilterLevel(pk, SearchLevel, x))
return false;
if (SearchLegal != null && new LegalityAnalysis(pk).Valid != SearchLegal)
return false;
if (BatchFilters.Length != 0 && !SearchUtil.SatisfiesFilterBatchInstruction(pk, BatchFilters))
return false;
return true;
}
private bool FilterResultEgg(PKM pk)
{
if (SearchEgg == false)
return !pk.IsEgg;
if (ESV != null)
return pk.IsEgg && pk.PSV == ESV;
return pk.IsEgg;
}
public IReadOnlyList<GameVersion> GetVersions(SaveFile sav) => GetVersions(sav, GetFallbackVersion(sav));
public IReadOnlyList<GameVersion> GetVersions(SaveFile sav, GameVersion fallback)
{
if (Version > 0)
return new[] {(GameVersion) Version};
return Generation switch
{
1 when !ParseSettings.AllowGen1Tradeback => new[] {RD, BU, GN, YW},
2 when sav is SAV2 {Korean: true} => new[] {GD, SI},
1 or 2 => new[] {RD, BU, GN, YW, /* */ GD, SI, C},
_ when fallback.GetGeneration() == Generation => GameUtil.GetVersionsWithinRange(sav, Generation).ToArray(),
_ => GameUtil.GameVersions,
};
}
private static GameVersion GetFallbackVersion(ITrainerInfo sav)
{
var parent = GameUtil.GetMetLocationVersionGroup((GameVersion)sav.Game);
if (parent == Invalid)
parent = GameUtil.GetMetLocationVersionGroup(GameUtil.GetVersion(sav.Generation));
return parent;
}
}