using System; using System.Collections.Generic; using System.Linq; namespace PKHeX.Core { /// /// Represents an Area where can be encountered, which contains a Location ID and data. /// public abstract class EncounterArea { public int Location; public EncounterSlot[] Slots = Array.Empty(); /// /// Gets the encounter areas for species with same level range and same slot type at same location /// /// List of species that exist in the Area. /// Paired min and max levels of the encounter slots. /// Location index of the encounter area. /// Encounter slot type of the encounter area. /// Encounter area with slots public static T[] GetSimpleEncounterArea(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 } }; } /// /// Gets the slots contained in the area that match the provided data. /// /// Pokémon Data /// Evolution lineage /// Minimum level of the encounter /// Enumerable list of encounters public virtual IEnumerable GetMatchingSlots(PKM pkm, IReadOnlyList 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 GetMatchFromEvoLevel(PKM pkm, IEnumerable 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 GetFilteredSlots(PKM pkm, IEnumerable slots, int minLevel) { return Legal.WildForms.Contains(pkm.Species) ? slots.Where(slot => slot.Form == pkm.AltForm) : slots; } /// /// Checks if the provided met location ID matches the parameters for the area. /// /// Met Location ID /// True if possibly originated from this area, false otherwise. public virtual bool IsMatchLocation(int location) => Location == location; } }