Use less efficient IsLevelWithinRange, for clarity

See previous commit for discussion; it's easier to read and maintain
this rather than have the precomputed min/max comparisons.

LevelMin - minDecrease <= min && max <= LevelMax + maxIncrease;
See EncounterArea7b:
Since we know min and minDecrease (0) before doing the linq filter we
can precompute the lowbound, same for highbound; but this isn't
immediately obvious to the reader. The perf cost is pretty much
negligible in the big picture, so leave it in the more readable state.

Thanks @fattard & @ammako !

#2389
This commit is contained in:
Kurt 2019-09-19 22:54:53 -07:00
parent a4a0e3ac6e
commit 3c50983afd
4 changed files with 28 additions and 7 deletions

View file

@ -9,17 +9,20 @@ namespace PKHeX.Core
/// </summary>
public sealed class EncounterArea6AO : EncounterArea32
{
private const int FluteBoostMax = 3; // Black Flute increases levels, White Flute decreases levels.
private const int DexNavBoost = 30;
private const int FluteBoostMin = 3; // White Flute decreases levels.
private const int FluteBoostMax = 3; // Black Flute increases levels.
private const int DexNavBoost = 30; // Maximum DexNav chain
protected override IEnumerable<EncounterSlot> GetMatchFromEvoLevel(PKM pkm, IEnumerable<DexLevel> vs, int minLevel)
{
var slots = Slots.Where(slot => vs.Any(evo => evo.Species == slot.Species && evo.Level >= (slot.LevelMin - FluteBoostMax)));
int getMaxLevelBoost(EncounterSlot s) => s.Type == SlotType.Rock_Smash ? FluteBoostMax : DexNavBoost;
int fluteMinLevel = minLevel + FluteBoostMax; // highest possible min-level of slot before flute decrease
// note: it's probably possible to determine a reduced DexNav boost based on the flawless IV count (no flawless = not chained)
// if someone wants to implement that logic to have the below method return a calculated max DexNavBoost, send a pull request :)
int getMaxLevelBoost(EncounterSlot s) => s.Type != SlotType.Rock_Smash ? DexNavBoost : FluteBoostMax; // DexNav encounters most likely
// Get slots where pokemon can exist with respect to level constraints
return slots.Where(s => s.IsLevelWithinRange(fluteMinLevel, minLevel - getMaxLevelBoost(s)));
return slots.Where(s => s.IsLevelWithinRange(minLevel, minLevel, FluteBoostMin, getMaxLevelBoost(s)));
}
protected override IEnumerable<EncounterSlot> GetFilteredSlots(PKM pkm, IEnumerable<EncounterSlot> slots, int minLevel)

View file

@ -16,7 +16,7 @@ namespace PKHeX.Core
var slots = Slots.Where(slot => vs.Any(evo => evo.Species == slot.Species && evo.Level >= (slot.LevelMin - CatchComboBonus)));
// Get slots where pokemon can exist with respect to level constraints
return slots.Where(s => s.IsLevelWithinRange(minLevel, minLevel - CatchComboBonus));
return slots.Where(s => s.IsLevelWithinRange(minLevel, minLevel, 0, CatchComboBonus));
}
protected override IEnumerable<EncounterSlot> GetFilteredSlots(PKM pkm, IEnumerable<EncounterSlot> slots, int minLevel)

View file

@ -17,7 +17,6 @@ namespace PKHeX.Core
/// </summary>
/// <param name="lvl">Single level</param>
/// <returns>True if within slot's range, false if impossible.</returns>
/// <remarks>Use <see cref="IsLevelWithinRange(int, int)"/> if <see cref="lvl"/> can have a range of values</remarks>
public bool IsLevelWithinRange(int lvl) => LevelMin <= lvl && lvl <= LevelMax;
/// <summary>
@ -28,6 +27,25 @@ namespace PKHeX.Core
/// <returns>True if within slot's range, false if impossible.</returns>
public bool IsLevelWithinRange(int min, int max) => LevelMin <= min && max <= LevelMax;
/// <summary>
/// Gets if the specified level inputs are within range of the <see cref="LevelMin"/> and <see cref="LevelMax"/>
/// </summary>
/// <param name="lvl">Single level</param>
/// <param name="minDecrease">Highest value the low end of levels can be</param>
/// <param name="maxIncrease">Lowest value the high end of levels can be</param>
/// <returns>True if within slot's range, false if impossible.</returns>
public bool IsLevelWithinRange(int lvl, int minDecrease, int maxIncrease) => LevelMin - minDecrease <= lvl && lvl <= LevelMax + maxIncrease;
/// <summary>
/// Gets if the specified level inputs are within range of the <see cref="LevelMin"/> and <see cref="LevelMax"/>
/// </summary>
/// <param name="min">Lowest level allowed</param>
/// <param name="max">Highest level allowed</param>
/// <param name="minDecrease">Highest value the low end of levels can be</param>
/// <param name="maxIncrease">Lowest value the high end of levels can be</param>
/// <returns>True if within slot's range, false if impossible.</returns>
public bool IsLevelWithinRange(int min, int max, int minDecrease, int maxIncrease) => LevelMin - minDecrease <= min && max <= LevelMax + maxIncrease;
public SlotType Type { get; set; } = SlotType.Any;
public EncounterType TypeEncounter { get; set; } = EncounterType.None;
public int SlotNumber { get; set; }