2018-08-17 03:06:40 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace PKHeX.Core.Searching
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2018-10-31 21:08:58 +00:00
|
|
|
|
/// <see cref="PKM"/> search settings & searcher
|
2018-08-17 03:06:40 +00:00
|
|
|
|
/// </summary>
|
2019-10-04 02:09:02 +00:00
|
|
|
|
public sealed class SearchSettings
|
2018-08-17 03:06:40 +00:00
|
|
|
|
{
|
|
|
|
|
public int Format { private get; set; }
|
|
|
|
|
public int Generation { private get; set; }
|
2018-11-11 05:04:24 +00:00
|
|
|
|
public int Species { get; set; } = -1;
|
2018-08-17 03:06:40 +00:00
|
|
|
|
public int Ability { private get; set; } = -1;
|
|
|
|
|
public int Nature { private get; set; } = -1;
|
|
|
|
|
public int Item { private get; set; } = -1;
|
|
|
|
|
public int Version { private get; set; } = -1;
|
|
|
|
|
public int HiddenPowerType { private get; set; } = -1;
|
|
|
|
|
|
|
|
|
|
public SearchComparison SearchFormat { private get; set; }
|
|
|
|
|
public SearchComparison SearchLevel { private get; set; }
|
|
|
|
|
|
|
|
|
|
public bool? SearchShiny { private get; set; }
|
|
|
|
|
public bool? SearchLegal { private get; set; }
|
2018-11-11 05:04:24 +00:00
|
|
|
|
public bool? SearchEgg { get; set; }
|
2018-08-17 03:06:40 +00:00
|
|
|
|
public int? ESV { private get; set; }
|
|
|
|
|
public int? Level { private get; set; }
|
|
|
|
|
|
|
|
|
|
public int IVType { private get; set; }
|
|
|
|
|
public int EVType { private get; set; }
|
|
|
|
|
|
|
|
|
|
public CloneDetectionMethod SearchClones { private get; set; }
|
PKHeX.Core Nullable cleanup (#2401)
* Handle some nullable cases
Refactor MysteryGift into a second abstract class (backed by a byte array, or fake data)
Make some classes have explicit constructors instead of { } initialization
* Handle bits more obviously without null
* Make SaveFile.BAK explicitly readonly again
* merge constructor methods to have readonly fields
* Inline some properties
* More nullable handling
* Rearrange box actions
define straightforward classes to not have any null properties
* Make extrabyte reference array immutable
* Move tooltip creation to designer
* Rearrange some logic to reduce nesting
* Cache generated fonts
* Split mystery gift album purpose
* Handle more tooltips
* Disallow null setters
* Don't capture RNG object, only type enum
* Unify learnset objects
Now have readonly properties which are never null
don't new() empty learnsets (>800 Learnset objects no longer created,
total of 2400 objects since we also new() a move & level array)
optimize g1/2 reader for early abort case
* Access rewrite
Initialize blocks in a separate object, and get via that object
removes a couple hundred "might be null" warnings since blocks are now readonly getters
some block references have been relocated, but interfaces should expose all that's needed
put HoF6 controls in a groupbox, and disable
* Readonly personal data
* IVs non nullable for mystery gift
* Explicitly initialize forced encounter moves
* Make shadow objects readonly & non-null
Put murkrow fix in binary data resource, instead of on startup
* Assign dex form fetch on constructor
Fixes legality parsing edge cases
also handle cxd parse for valid; exit before exception is thrown in FrameGenerator
* Remove unnecessary null checks
* Keep empty value until init
SetPouch sets the value to an actual one during load, but whatever
* Readonly team lock data
* Readonly locks
Put locked encounters at bottom (favor unlocked)
* Mail readonly data / offset
Rearrange some call flow and pass defaults
Add fake classes for SaveDataEditor mocking
Always party size, no need to check twice in stat editor
use a fake save file as initial data for savedata editor, and for
gamedata (wow i found a usage)
constrain eventwork editor to struct variable types (uint, int, etc),
thus preventing null assignment errors
2019-10-17 01:47:31 +00:00
|
|
|
|
public IList<string> BatchInstructions { private get; set; } = Array.Empty<string>();
|
2018-08-17 03:06:40 +00:00
|
|
|
|
|
2018-11-11 05:04:24 +00:00
|
|
|
|
public readonly List<int> Moves = new List<int>();
|
2018-08-17 03:06:40 +00:00
|
|
|
|
|
|
|
|
|
// 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 List<Func<PKM, bool>>();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds a move to the required move list.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="move"></param>
|
|
|
|
|
public void AddMove(int move)
|
|
|
|
|
{
|
|
|
|
|
if (move > 0 && !Moves.Contains(move))
|
|
|
|
|
Moves.Add(move);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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)
|
|
|
|
|
{
|
|
|
|
|
var result = SearchSimple(list);
|
|
|
|
|
result = SearchIntermediate(result);
|
|
|
|
|
result = SearchComplex(result);
|
|
|
|
|
|
|
|
|
|
foreach (var filter in ExtraFilters)
|
|
|
|
|
result = result.Where(filter);
|
|
|
|
|
|
|
|
|
|
if (SearchClones != CloneDetectionMethod.None)
|
|
|
|
|
result = SearchUtil.GetClones(result, SearchClones);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerable<PKM> SearchSimple(IEnumerable<PKM> res)
|
|
|
|
|
{
|
|
|
|
|
if (Format > 0)
|
|
|
|
|
res = SearchUtil.FilterByFormat(res, Format, SearchFormat);
|
|
|
|
|
if (Species > -1)
|
|
|
|
|
res = res.Where(pk => pk.Species == Species);
|
|
|
|
|
if (Ability > -1)
|
|
|
|
|
res = res.Where(pk => pk.Ability == Ability);
|
|
|
|
|
if (Nature > -1)
|
|
|
|
|
res = res.Where(pk => pk.Nature == Nature);
|
|
|
|
|
if (Item > -1)
|
|
|
|
|
res = res.Where(pk => pk.HeldItem == Item);
|
|
|
|
|
if (Version > -1)
|
|
|
|
|
res = res.Where(pk => pk.Version == Version);
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerable<PKM> SearchIntermediate(IEnumerable<PKM> res)
|
|
|
|
|
{
|
|
|
|
|
if (Generation > 0)
|
|
|
|
|
res = SearchUtil.FilterByGeneration(res, Generation);
|
|
|
|
|
if (Moves.Count > 0)
|
|
|
|
|
res = SearchUtil.FilterByMoves(res, Moves);
|
|
|
|
|
if (HiddenPowerType > -1)
|
|
|
|
|
res = res.Where(pk => pk.HPType == HiddenPowerType);
|
|
|
|
|
if (SearchShiny != null)
|
|
|
|
|
res = res.Where(pk => pk.IsShiny == SearchShiny);
|
|
|
|
|
|
|
|
|
|
if (IVType > 0)
|
|
|
|
|
res = SearchUtil.FilterByIVs(res, IVType);
|
|
|
|
|
if (EVType > 0)
|
|
|
|
|
res = SearchUtil.FilterByEVs(res, EVType);
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerable<PKM> SearchComplex(IEnumerable<PKM> res)
|
|
|
|
|
{
|
|
|
|
|
if (SearchEgg != null)
|
|
|
|
|
res = FilterResultEgg(res);
|
|
|
|
|
|
|
|
|
|
if (Level != null)
|
2020-06-17 02:46:22 +00:00
|
|
|
|
res = SearchUtil.FilterByLevel(res, SearchLevel, (int)Level);
|
2018-08-17 03:06:40 +00:00
|
|
|
|
|
|
|
|
|
if (SearchLegal != null)
|
|
|
|
|
res = res.Where(pk => new LegalityAnalysis(pk).Valid == SearchLegal);
|
|
|
|
|
|
|
|
|
|
if (BatchInstructions != null)
|
|
|
|
|
res = SearchUtil.FilterByBatchInstruction(res, BatchInstructions);
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerable<PKM> FilterResultEgg(IEnumerable<PKM> res)
|
|
|
|
|
{
|
|
|
|
|
if (SearchEgg == false)
|
|
|
|
|
return res.Where(pk => !pk.IsEgg);
|
|
|
|
|
if (ESV != null)
|
|
|
|
|
return res.Where(pk => pk.IsEgg && pk.PSV == ESV);
|
|
|
|
|
return res.Where(pk => pk.IsEgg);
|
|
|
|
|
}
|
2018-11-11 05:04:24 +00:00
|
|
|
|
|
2020-06-17 02:46:22 +00:00
|
|
|
|
public IReadOnlyList<GameVersion> GetVersions(SaveFile sav) => GetVersions(sav, GetFallbackVersion(sav));
|
2018-12-31 01:30:57 +00:00
|
|
|
|
|
2020-06-17 02:46:22 +00:00
|
|
|
|
public IReadOnlyList<GameVersion> GetVersions(SaveFile sav, GameVersion fallback)
|
2018-11-11 05:04:24 +00:00
|
|
|
|
{
|
|
|
|
|
if (Version > 0)
|
|
|
|
|
return new[] {(GameVersion) Version};
|
|
|
|
|
if (Generation != 0)
|
|
|
|
|
{
|
|
|
|
|
return fallback.GetGeneration() == Generation
|
2020-06-17 02:46:22 +00:00
|
|
|
|
? GameUtil.GetVersionsWithinRange(sav, Generation).ToArray()
|
2018-11-11 05:04:24 +00:00
|
|
|
|
: GameUtil.GameVersions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return GameUtil.GameVersions;
|
|
|
|
|
}
|
2018-12-31 01:30:57 +00:00
|
|
|
|
|
2020-06-17 02:46:22 +00:00
|
|
|
|
private static GameVersion GetFallbackVersion(ITrainerInfo sav)
|
2018-12-31 01:30:57 +00:00
|
|
|
|
{
|
2020-06-17 02:46:22 +00:00
|
|
|
|
var parent = GameUtil.GetMetLocationVersionGroup((GameVersion)sav.Game);
|
2018-12-31 01:30:57 +00:00
|
|
|
|
if (parent == GameVersion.Invalid)
|
2020-06-17 02:46:22 +00:00
|
|
|
|
parent = GameUtil.GetMetLocationVersionGroup(GameUtil.GetVersion(sav.Generation));
|
2018-12-31 01:30:57 +00:00
|
|
|
|
return parent;
|
|
|
|
|
}
|
2018-08-17 03:06:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|