Refactoring

misc cleanup/breaking up large methods from recent PR
This commit is contained in:
Kurt
2017-07-06 17:01:29 -07:00
parent 461fb70f90
commit ca04abfca6
5 changed files with 87 additions and 70 deletions

View File

@@ -327,7 +327,7 @@ private static void MarkEncountersGeneration(ref EncounterStatic[] Encounters, i
private static void MarkEncountersVersion(ref EncounterArea[] Areas, GameVersion Version)
{
foreach (EncounterArea Area in Areas)
foreach (EncounterSlot1 Slot in Area.Slots)
foreach (var Slot in Area.Slots.OfType<EncounterSlot1>())
Slot.Version = Version;
}
private static void MarkEncountersGeneration(ref EncounterArea[] Areas, int Generation)
@@ -1467,14 +1467,14 @@ internal static IEnumerable<int> GetExclusivePreEvolutionMoves(PKM pkm, int Spec
}
// Encounter
internal static GameVersion[] GetGen2GameEncounter(PKM pkm, LegalInfo Info)
internal static GameVersion[] GetGen2Versions(LegalInfo Info)
{
if (AllowGen2Crystal && Info.Game == GameVersion.C)
return new[] { GameVersion.C };
// Any encounter marked with version GSC is for pokemon with the same moves in g/s and crystal, it is enought to check only g/s moves
return new[] { GameVersion.GS };
}
internal static GameVersion[] GetGen1GameEncounter(PKM pkm, LegalInfo Info)
internal static GameVersion[] GetGen1Versions(LegalInfo Info)
{
if (Info.EncounterMatch.Species == 133 && Info.Game == GameVersion.Stadium)
// Staidum eevee, check for red/blue and yellow initial moves
@@ -2719,7 +2719,7 @@ private static IEnumerable<int> GetMoves(PKM pkm, int species, int minlvlG1, int
return r;
}
internal static int[] GetEggMoves(PKM pkm, int species, int formnum, GameVersion Version = GameVersion.Any)
internal static int[] GetEggMoves(PKM pkm, int species, int formnum)
{
if (!pkm.InhabitedGeneration(pkm.GenNumber, species) || pkm.PersonalInfo.Gender == 255)
return new int[0];
@@ -2986,7 +2986,7 @@ internal static bool IsOutsider(PKM pkm)
return Outsider;
}
internal static TreesArea GetCrystalTreeArea(PKM pkm,EncounterSlot Slot)
internal static TreesArea GetCrystalTreeArea(EncounterSlot Slot)
{
return HeadbuttTreesC.FirstOrDefault(a => a.Location == Slot.Location);
}

View File

@@ -81,7 +81,7 @@ private static LegalInfo VerifyWithoutEncounter(PKM pkm, LegalInfo info)
info.Moves = VerifyCurrentMoves.VerifyMoves(pkm, info);
return info;
}
private static Func<PKM, LegalInfo,IEncounterable, CheckResult> GetEncounterVerifierMethod(PKM pkm)
private static Func<PKM, LegalInfo, IEncounterable, CheckResult> GetEncounterVerifierMethod(PKM pkm)
{
switch (pkm.GenNumber)
{

View File

@@ -416,7 +416,7 @@ private static IEnumerable<EncounterStatic> GetMatchingStaticEncounters(PKM pkm,
if (e.Species == 054 && !japanese && catch_rate != 168)
continue;
}
// Encounter with the different catch rate in yellow and redblue are duplicated with different gameverion
// Encounters with different catch rates in yellow and redblue are duplicated with different gameverion
else if (e.Version == GameVersion.YW && catch_rate != PersonalTable.Y[e.Species].CatchRate)
continue;
else if (e.Version != GameVersion.YW && catch_rate != PersonalTable.RB[e.Species].CatchRate)
@@ -542,44 +542,8 @@ private static IEnumerable<EncounterSlot> GetValidEncounterSlots(PKM pkm, Encoun
// Get Valid levels
IEnumerable<DexLevel> vs = GetValidPreEvolutions(pkm, maxspeciesorigin: maxspeciesorigin, lvl: ignoreLevel ? 100 : -1, skipChecks: ignoreLevel);
bool RBDragonair = false;
GameVersion Gen1Version = GameVersion.RBY;
if (pkm is PK1 pk1 && pkm.Gen1_NotTradeback)
{
// Pure gen 1, slots can be filter by catch rate
if ((pkm.Species == 25 || pkm.Species == 26) && pk1.Catch_Rate == 163)
// Yellow Pikachu, is not a wild encounter
yield break;
if ((pkm.Species == 64 || pkm.Species == 65) && pk1.Catch_Rate == 96)
{
// Yellow Kadabra, ignore Abra encounters
vs = vs.Where(s => s.Species == 64);
Gen1Version = GameVersion.YW;
}
if (pkm.Species == 148 || pkm.Species == 149)
{
if (pk1.Catch_Rate == 27)
{
// Yellow Dragonair, ignore Dratini encounters
vs = vs.Where(s => s.Species == 148);
Gen1Version = GameVersion.YW;
}
else
// Red blue dragonair have the same catch rate as dratini, it could also be a dratini from any game
RBDragonair = true;
}
else
{
if((pkm.Species == 64 || pkm.Species == 65) && pk1.Catch_Rate == 100)
{
// Red Blue Kadabra
vs = vs.Where(s => s.Species == 64);
Gen1Version = GameVersion.RB;
}
vs = vs.Where(s => pk1.Catch_Rate == PersonalTable.RB[s.Species].CatchRate);
}
}
if (!FilterGBSlotsCatchRate(pkm, ref vs, out GameVersion Gen1Version, out bool RBDragonair))
yield break;
// Get slots where pokemon can exist with respect to the evolution chain
IEnumerable<EncounterSlot> slots = loc.Slots.Where(slot => vs.Any(evo => evo.Species == slot.Species && (ignoreLevel || evo.Level >= slot.LevelMin - df)));
@@ -594,23 +558,8 @@ private static IEnumerable<EncounterSlot> GetValidEncounterSlots(PKM pkm, Encoun
if (gen <= 2)
{
if (gen == 1 && Gen1Version != GameVersion.RBY)
{
encounterSlots = encounterSlots.Where(slot => Gen1Version.Contains(((EncounterSlot1)slot).Version)).ToList();
}
if (gen == 1 && RBDragonair)
{
// Red Blue dragonair or dratini from any gen 1 games
encounterSlots = encounterSlots.Where(slot => GameVersion.RB.Contains(((EncounterSlot1)slot).Version) || slot.Species == 147).ToList();
}
if (gen == 2 && pkm is PK2 pk2 && pk2.Met_Day != 0)
{
encounterSlots = encounterSlots.Where(slot => ((EncounterSlot1)slot).Time.Contains(pk2.Met_Day)).ToList();
}
foreach (var s in encounterSlots.OrderBy(slot => slot.LevelMin))
var gbslots = FilterGBSlots(pkm, gen, Gen1Version, encounterSlots, RBDragonair);
foreach (var s in gbslots.OrderBy(slot => slot.LevelMin))
yield return s;
yield break;
}
@@ -666,7 +615,78 @@ EncounterSlot getPressureSlot(EncounterSlot s)
return max;
}
}
private static bool FilterGBSlotsCatchRate(PKM pkm, ref IEnumerable<DexLevel> vs, out GameVersion Gen1Version, out bool RBDragonair)
{
RBDragonair = false;
Gen1Version = GameVersion.RBY;
if (!(pkm is PK1 pk1) || !pkm.Gen1_NotTradeback)
return true;
// Pure gen 1, slots can be filter by catch rate
switch (pkm.Species)
{
// Pikachu
case 25 when pk1.Catch_Rate == 163:
case 26 when pk1.Catch_Rate == 163:
return false; // Yellow Pikachu is not a wild encounter
// Kadabra (YW)
case 64 when pk1.Catch_Rate == 96:
case 65 when pk1.Catch_Rate == 96:
vs = vs.Where(s => s.Species == 64);
Gen1Version = GameVersion.YW;
return true;
// Kadabra (RB)
case 64 when pk1.Catch_Rate == 100:
case 65 when pk1.Catch_Rate == 100:
vs = vs.Where(s => s.Species == 64);
Gen1Version = GameVersion.RB;
return true;
// Dragonair (YW)
case 148 when pk1.Catch_Rate == 27:
case 149 when pk1.Catch_Rate == 27:
vs = vs.Where(s => s.Species == 148); // Yellow Dragonair, ignore Dratini encounters
Gen1Version = GameVersion.YW;
return true;
// Dragonair (RB)
case 148:
case 149:
// Red blue dragonair have the same catch rate as dratini, it could also be a dratini from any game
vs = vs.Where(s => pk1.Catch_Rate == PersonalTable.RB[s.Species].CatchRate);
RBDragonair = true;
return true;
default:
vs = vs.Where(s => pk1.Catch_Rate == PersonalTable.RB[s.Species].CatchRate);
return true;
}
}
private static IEnumerable<EncounterSlot> FilterGBSlots(PKM pkm, int gen, GameVersion Gen1Version, IEnumerable<EncounterSlot> slots, bool RBDragonair)
{
switch (gen)
{
case 1:
if (Gen1Version != GameVersion.RBY)
slots = slots.Where(slot => Gen1Version.Contains(((EncounterSlot1)slot).Version));
// Red Blue dragonair or dratini from any gen 1 games
if (RBDragonair)
return slots.Where(slot => GameVersion.RB.Contains(((EncounterSlot1)slot).Version) || slot.Species == 147);
return slots;
case 2:
if (pkm is PK2 pk2 && pk2.Met_Day != 0)
slots = slots.Where(slot => ((EncounterSlot1)slot).Time.Contains(pk2.Met_Day));
return slots;
default:
return slots;
}
}
private static IEnumerable<EncounterArea> GetEncounterSlots(PKM pkm, int lvl = -1, GameVersion gameSource = GameVersion.Any)
{
if (gameSource == GameVersion.Any)

View File

@@ -182,7 +182,7 @@ private static CheckMoveResult[] ParseMovesPre3DS(PKM pkm, int[] Moves, LegalInf
}
private static CheckMoveResult[] ParseMovesGenGB(PKM pkm, int[] Moves, LegalInfo info)
{
GameVersion[] games = (info.EncounterMatch as IGeneration)?.Generation == 1 ? Legal.GetGen1GameEncounter(pkm, info) : Legal.GetGen2GameEncounter(pkm, info);
GameVersion[] games = (info.EncounterMatch as IGeneration)?.Generation == 1 ? Legal.GetGen1Versions(info) : Legal.GetGen2Versions(info);
CheckMoveResult[] res = new CheckMoveResult[4];
var G1Encounter = info.EncounterMatch;
if (G1Encounter == null)
@@ -214,7 +214,7 @@ private static CheckMoveResult[] ParseMovesRelearn(PKM pkm, int[] Moves, LegalIn
var emptyegg = new int[0];
var e = info.EncounterMatch as EncounterEgg;
var EggMoves = e != null ? Legal.GetEggMoves(pkm, e.Species, pkm.AltForm, e.Game) : emptyegg;
var EggMoves = e != null ? Legal.GetEggMoves(pkm, e.Species, pkm.AltForm) : emptyegg;
var TradebackPreevo = pkm.Format == 2 && info.EncounterMatch.Species > 151 && pkm.InhabitedGeneration(1);
var NonTradebackLvlMoves = TradebackPreevo ? Legal.GetExclusivePreEvolutionMoves(pkm, info.EncounterMatch.Species, info.EvoChainsAllGens[2], 2, e.Game).Where(m => m > Legal.MaxMoveID_1).ToArray() : new int[0];

View File

@@ -11,20 +11,17 @@ public enum EncounterTime
public static class EncounterTimeExtension
{
public static bool Contains(this EncounterTime t1, int t2)
{
return t1.Contains((EncounterTime)t2);
}
public static bool Contains(this EncounterTime t1, int t2) => t1.Contains((EncounterTime)t2);
private static bool Contains(this EncounterTime t1, EncounterTime t2)
{
if (t1 == t2 || t1 == EncounterTime.Any || t2 == EncounterTime.Any)
return true;
if (t1 == EncounterTime.MorningDay)
return (t2 == EncounterTime.Morning || t2 == EncounterTime.Day);
return t2 == EncounterTime.Morning || t2 == EncounterTime.Day;
if (t2 == EncounterTime.MorningDay)
return (t1 == EncounterTime.Morning || t1 == EncounterTime.Day);
return t1 == EncounterTime.Morning || t1 == EncounterTime.Day;
return false;
}