PKHeX/PKHeX.Core/Legality/Areas/EncounterArea.cs
Kurt fa344208b2 Add crossover location handling
Closes #2461 by implementing slightly differently

Dictionary instead of new[] every area
Checks original met location first before attempting the dictionary
lookup (missing in pull request)
Easy path forward if LGPE crossover is needed

Potential issues: Since encounter types aren't marked, shaking trees and
fishing encounters are allowed to cross over. Maybe need to have some
marking for encounter slots or something...
2019-11-19 08:38:18 -08:00

79 lines
3.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
namespace PKHeX.Core
{
/// <summary>
/// Represents an Area where <see cref="PKM"/> can be encountered, which contains a Location ID and <see cref="EncounterSlot"/> data.
/// </summary>
public abstract class EncounterArea
{
public int Location;
public EncounterSlot[] Slots = Array.Empty<EncounterSlot>();
/// <summary>
/// Gets the encounter areas for species with same level range and same slot type at same location
/// </summary>
/// <param name="species">List of species that exist in the Area.</param>
/// <param name="lvls">Paired min and max levels of the encounter slots.</param>
/// <param name="location">Location index of the encounter area.</param>
/// <param name="t">Encounter slot type of the encounter area.</param>
/// <returns>Encounter area with slots</returns>
public static T[] GetSimpleEncounterArea<T>(int[] species, int[] lvls, int location, SlotType t) where T : EncounterArea, new()
{
if ((lvls.Length & 1) != 0) // levels data not paired; expect multiple of 2
throw new ArgumentException(nameof(lvls));
var count = species.Length * (lvls.Length / 2);
var slots = new EncounterSlot[count];
int ctr = 0;
foreach (var s in species)
{
for (int i = 0; i < lvls.Length;)
{
slots[ctr++] = new EncounterSlot
{
LevelMin = lvls[i++],
LevelMax = lvls[i++],
Species = s,
Type = t
};
}
}
return new[] { new T { Location = location, Slots = slots } };
}
/// <summary>
/// Gets the slots contained in the area that match the provided data.
/// </summary>
/// <param name="pkm">Pokémon Data</param>
/// <param name="vs">Evolution lineage</param>
/// <param name="minLevel">Minimum level of the encounter</param>
/// <returns>Enumerable list of encounters</returns>
public virtual IEnumerable<EncounterSlot> GetMatchingSlots(PKM pkm, IReadOnlyList<EvoCriteria> vs, int minLevel = 0)
{
if (minLevel == 0) // any
return Slots.Where(slot => vs.Any(evo => evo.Species == slot.Species));
var slots = GetMatchFromEvoLevel(pkm, vs, minLevel);
return GetFilteredSlots(pkm, slots, minLevel);
}
protected virtual IEnumerable<EncounterSlot> GetMatchFromEvoLevel(PKM pkm, IEnumerable<EvoCriteria> vs, int minLevel)
{
var slots = Slots.Where(slot => vs.Any(evo => evo.Species == slot.Species && evo.Level >= slot.LevelMin));
// Get slots where pokemon can exist with respect to level constraints
return slots.Where(slot => slot.IsLevelWithinRange(minLevel));
}
protected virtual IEnumerable<EncounterSlot> GetFilteredSlots(PKM pkm, IEnumerable<EncounterSlot> slots, int minLevel)
{
return Legal.WildForms.Contains(pkm.Species)
? slots.Where(slot => slot.Form == pkm.AltForm)
: slots;
}
public virtual bool IsMatchLocation(int location) => Location == location;
}
}