PKHeX/PKHeX.Core/Legality/Encounters/Information/EncounterLearn.cs
Kurt 6ee7a8724b
Offload EncounterSlot loading logic to reduce complexity (#2980)
* Rework gen1 slot loading

Slot templates are precomputed from ROM data and just loaded straight in, with tight coupling to the encounter area (grouped by slot types).

* Revise fuzzy met check for underleveled wild evos

Example: Level 23 poliwhirl in RBY as a level 50 poliwhirl, will assume the chain is 25-50 for poliwhirl (as poliwag evolves at 25). Instead of revising the origin chain, just ignore the evo min level in the comparison.

Previous commit fixed it for gen1.

* Rework gen2-4 slot loading

Gen4 not finished, Type Encounter data and some edge encounters not recognizing yet...

* Add feebas slots for old/good encounters

* Begin moving properties

Great news! Gen5-7 need to be de-dumbed like Gen1-4.

Then I can remove the bang (!) on the Area accessor and ensure that it's never null!

* Split off XD pokespot slot encounter table type

* Set area in constructor

* Deduplicate g3 roaming encounters

* Deduplicate xd encounter locations (rebattle)

Only difference is met location; no need to create 500 extra encounter objects. A simple contains check is ok (rarely in gen3 format).

* Make all slots have a readonly reference to their parent area

* Minor clean

* Remove "Safari" slot type flag

Can be determined via other means (generation-location), allows us to reduce the size of SlotType member to a byte

Output of slot binaries didn't preserve the Safari flag anyway.

* Update SlotType.cs

* Handle type encounters correctly

* Merge safari area into regular xy area

* Merge dexnav accessor logic

* fix some logic so that tests pass again

rearrange g5 dw init to be done outside of static constructor (initializer instead)
PIDGenerator: friend safari slots now generate with required flawless IV count

* Add cianwood tentacool gift encounter

* Remove unnecessary abstractions

Fake area just returned a slot; since Slots have a non-null reference to the area, we can just return the slot and use the API to grab a list of possible slots for the chain.

Increase restrictiveness of location/type get-set operations

* Minor tweaks, pass parameters

DexNav observed state isn't necessary to use, only need to see if it's possible to dexnav. Now that we have metadata for slots, we can.

* Remove unused legality tables
2020-08-30 10:23:22 -07:00

160 lines
5.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
namespace PKHeX.Core
{
public static class EncounterLearn
{
static EncounterLearn()
{
if (!EncounterEvent.Initialized)
EncounterEvent.RefreshMGDB();
}
/// <summary>
/// Default response if there are no matches.
/// </summary>
public const string NoMatches = "None";
/// <summary>
/// Checks if a <see cref="species"/> can learn all input <see cref="moves"/>.
/// </summary>
public static bool CanLearn(string species, IEnumerable<string> moves, string lang = GameLanguage.DefaultLanguage)
{
var encounters = GetLearn(species, moves, lang);
return encounters.Any();
}
/// <summary>
/// Gets a summary of all encounters where a <see cref="species"/> can learn all input <see cref="moves"/>.
/// </summary>
public static IEnumerable<string> GetLearnSummary(string species, IEnumerable<string> moves, string lang = GameLanguage.DefaultLanguage)
{
var encounters = GetLearn(species, moves, lang);
var msg = Summarize(encounters).ToList();
if (msg.Count == 0)
msg.Add(NoMatches);
return msg;
}
/// <summary>
/// Gets all encounters where a <see cref="species"/> can learn all input <see cref="moves"/>.
/// </summary>
public static IEnumerable<IEncounterable> GetLearn(string species, IEnumerable<string> moves, string lang = GameLanguage.DefaultLanguage)
{
var str = GameInfo.GetStrings(lang);
var spec = StringUtil.FindIndexIgnoreCase(str.specieslist, species);
var moveIDs = StringUtil.GetIndexes(str.movelist, moves.ToList());
return GetLearn(spec, moveIDs);
}
/// <summary>
/// Gets all encounters where a <see cref="species"/> can learn all input <see cref="moves"/>.
/// </summary>
public static IEnumerable<IEncounterable> GetLearn(int species, int[] moves)
{
if (species <= 0)
return Array.Empty<IEncounterable>();
if (moves.Any(z => z < 0))
return Array.Empty<IEncounterable>();
var blank = PKMConverter.GetBlank(PKX.Generation);
blank.Species = species;
var vers = GameUtil.GameVersions;
return EncounterMovesetGenerator.GenerateEncounters(blank, moves, vers);
}
/// <summary>
/// Summarizes all encounters by grouping by <see cref="IEncounterable.Name"/>.
/// </summary>
public static IEnumerable<string> Summarize(IEnumerable<IEncounterable> encounters, bool advanced = false)
{
var types = encounters.GroupBy(z => z.Name);
return Summarize(types, advanced);
}
/// <summary>
/// Summarizes groups of encounters.
/// </summary>
public static IEnumerable<string> Summarize(IEnumerable<IGrouping<string, IEncounterable>> types, bool advanced = false)
{
return types.SelectMany(g => EncounterSummary.SummarizeGroup(g, g.Key, advanced));
}
}
public readonly struct EncounterSummary
{
private readonly GameVersion Version;
private readonly string LocationName;
private EncounterSummary(IEncounterable z, string type)
{
Version = z.Version;
LocationName = GetLocationName(z) + $"({type}) ";
}
private EncounterSummary(IEncounterable z)
{
Version = z.Version;
LocationName = GetLocationName(z);
}
private static string GetLocationName(IEncounterable z)
{
var gen = z.Generation;
var version = z.Version;
if (gen < 0 && version > 0)
gen = version.GetGeneration();
if (!(z is ILocation l))
return $"[Gen{gen}]\t";
var loc = l.GetEncounterLocation(gen, (int)version);
if (string.IsNullOrWhiteSpace(loc))
return $"[Gen{gen}]\t";
return $"[Gen{gen}]\t{loc}: ";
}
public static IEnumerable<string> SummarizeGroup(IEnumerable<IEncounterable> items, string header = "", bool advanced = false)
{
if (!string.IsNullOrWhiteSpace(header))
yield return $"=={header}==";
var summaries = advanced ? GetSummaries(items) : items.Select(z => new EncounterSummary(z));
var objs = summaries.GroupBy(z => z.LocationName);
foreach (var g in objs)
yield return $"\t{g.Key}{string.Join(", ", g.Select(z => z.Version).Distinct())}";
}
public static IEnumerable<EncounterSummary> GetSummaries(IEnumerable<IEncounterable> items)
{
return items.SelectMany(GetSummaries);
}
private static IEnumerable<EncounterSummary> GetSummaries(IEncounterable item)
{
switch (item)
{
case EncounterSlot s when s.Area.Type != 0:
{
yield return new EncounterSummary(item, s.Area.Type.ToString());
break;
}
default:
yield return new EncounterSummary(item);
break;
}
}
public bool Equals(EncounterSummary obj) => obj.Version == Version && obj.LocationName == LocationName;
public override bool Equals(object obj) => obj is EncounterSummary t && Equals(t);
public override int GetHashCode() => LocationName.GetHashCode();
public static bool operator ==(EncounterSummary left, EncounterSummary right) => left.Equals(right);
public static bool operator !=(EncounterSummary left, EncounterSummary right) => !(left == right);
}
}