Refactoring

Relocate & simplify some logic
This commit is contained in:
Kurt
2018-06-21 20:13:41 -07:00
parent 1db41c8b21
commit c1f4e2a9ff
4 changed files with 85 additions and 108 deletions

View File

@@ -287,7 +287,7 @@ private void VerifyNickname()
else
{
// Can't have another language name if it hasn't evolved or wasn't a language-traded egg.
bool evolved = Legal.IsNotBaseSpecies(pkm);
bool evolved = EncounterMatch.Species != pkm.Species;
bool match = PKX.GetSpeciesNameGeneration(pkm.Species, pkm.Language, pkm.Format) == nickname;
if (pkm.WasTradedEgg || evolved)
match |= !PKX.IsNicknamedAnyLanguage(pkm.Species, nickname, pkm.Format);
@@ -1740,7 +1740,7 @@ private CheckResult VerifyHistory()
if (VerifyHistoryUntradedHandler(pkm, out CheckResult chk1))
return chk1;
if (EncounterMatch.Species != pkm.Species && VerifyHistoryUntradedEvolution(pkm, out CheckResult chk2))
if (EncounterMatch.Species != pkm.Species && VerifyHistoryUntradedEvolution(pkm, Info.EvoChainsAllGens, out CheckResult chk2))
return chk2;
}
else // Is Traded
@@ -1785,7 +1785,7 @@ private CheckResult VerifyHistory7(int[] geo)
{
if (VerifyHistoryUntradedHandler(pkm, out CheckResult chk1))
return chk1;
if (EncounterMatch.Species != pkm.Species && VerifyHistoryUntradedEvolution(pkm, out CheckResult chk2))
if (EncounterMatch.Species != pkm.Species && VerifyHistoryUntradedEvolution(pkm, Info.EvoChainsAllGens, out CheckResult chk2))
return chk2;
}
@@ -1805,7 +1805,7 @@ private static bool VerifyHistoryUntradedHandler(PKM pkm, out CheckResult result
return true;
}
private static bool VerifyHistoryUntradedEvolution(PKM pkm, out CheckResult result)
private static bool VerifyHistoryUntradedEvolution(PKM pkm, EvoCriteria[][] chain, out CheckResult result)
{
result = null;
// Handling Trainer string is empty implying it has not been traded.
@@ -1813,7 +1813,7 @@ private static bool VerifyHistoryUntradedEvolution(PKM pkm, out CheckResult resu
if (pkm.Species == 350) // Milotic
{
if (Legal.IsTradeEvolved(pkm))
if (Legal.IsTradeEvolved(chain, pkm.Format))
return false;
if (pkm is IContestStats s && s.CNT_Beauty < 170) // Beauty Contest Stat Requirement
result = new CheckResult(Severity.Invalid, V143, CheckIdentifier.History);
@@ -1823,59 +1823,38 @@ private static bool VerifyHistoryUntradedEvolution(PKM pkm, out CheckResult resu
return false;
return true;
}
if (!Legal.IsTradeEvolved(pkm))
if (!Legal.IsTradeEvolved(chain, pkm.Format))
return false;
result = new CheckResult(Severity.Invalid, V142, CheckIdentifier.History);
return true;
}
private CheckResult VerifyCommonMemory(int handler)
{
int m = 0;
int t = 0;
int i = 0;
int f = 0;
string resultPrefix = "";
switch (handler)
{
case 0:
m = pkm.OT_Memory;
t = pkm.OT_TextVar;
i = pkm.OT_Intensity;
f = pkm.OT_Feeling;
resultPrefix = V205;
break;
case 1:
m = pkm.HT_Memory;
t = pkm.HT_TextVar;
i = pkm.HT_Intensity;
f = pkm.HT_Feeling;
resultPrefix = V206;
break;
}
Memories.GetMemoryVariables(pkm, out int m, out int t, out int i, out int f, out string tr, handler);
int matchingMoveMemory = Array.IndexOf(Memories.MoveSpecificMemories[0], m);
if (matchingMoveMemory != -1 && pkm.Species != 235 && !Legal.GetCanLearnMachineMove(pkm, Memories.MoveSpecificMemories[1][matchingMoveMemory], 6))
return new CheckResult(Severity.Invalid, string.Format(V153, resultPrefix), CheckIdentifier.Memory);
return new CheckResult(Severity.Invalid, string.Format(V153, tr), CheckIdentifier.Memory);
if (m == 6 && !Memories.LocationsWithPKCenter[0].Contains(t))
return new CheckResult(Severity.Invalid, string.Format(V154, resultPrefix), CheckIdentifier.Memory);
return new CheckResult(Severity.Invalid, string.Format(V154, tr), CheckIdentifier.Memory);
if (m == 21) // {0} saw {2} carrying {1} on its back. {4} that {3}.
if (!Legal.GetCanLearnMachineMove(new PK6 {Species = t, EXP = PKX.GetEXP(100, t)}, 19, 6))
return new CheckResult(Severity.Invalid, string.Format(V153, resultPrefix), CheckIdentifier.Memory);
if (!Legal.GetCanLearnMachineMove(new PK6 { Species = t, EXP = PKX.GetEXP(100, t) }, 19, 6))
return new CheckResult(Severity.Invalid, string.Format(V153, tr), CheckIdentifier.Memory);
if ((m == 16 || m == 48) && (t == 0 || !Legal.GetCanKnowMove(pkm, t, 6)))
return new CheckResult(Severity.Invalid, string.Format(V153, resultPrefix), CheckIdentifier.Memory);
return new CheckResult(Severity.Invalid, string.Format(V153, tr), CheckIdentifier.Memory);
if (m == 49 && (t == 0 || !Legal.GetCanRelearnMove(pkm, t, 6))) // {0} was able to remember {2} at {1}'s instruction. {4} that {3}.
return new CheckResult(Severity.Invalid, string.Format(V153, resultPrefix), CheckIdentifier.Memory);
return new CheckResult(Severity.Invalid, string.Format(V153, tr), CheckIdentifier.Memory);
if (i < Memories.MemoryMinIntensity[m])
return new CheckResult(Severity.Invalid, string.Format(V254, resultPrefix, Memories.MemoryMinIntensity[m]), CheckIdentifier.Memory);
return new CheckResult(Severity.Invalid, string.Format(V254, tr, Memories.MemoryMinIntensity[m]), CheckIdentifier.Memory);
if (m != 4 && (Memories.MemoryFeelings[m] & (1 << f)) == 0)
return new CheckResult(Severity.Invalid, string.Format(V255, resultPrefix), CheckIdentifier.Memory);
return new CheckResult(Severity.Invalid, string.Format(V255, tr), CheckIdentifier.Memory);
return new CheckResult(Severity.Valid, string.Format(V155, resultPrefix), CheckIdentifier.Memory);
return new CheckResult(Severity.Valid, string.Format(V155, tr), CheckIdentifier.Memory);
}
private void VerifyOTMemoryIs(int[] values)
@@ -2033,30 +2012,10 @@ private static CheckResult VerifyConsoleRegion(PKM pkm)
int consoleRegion = pkm.ConsoleRegion;
if (consoleRegion >= 7)
return new CheckResult(Severity.Invalid, V301, CheckIdentifier.Geography);
return IsConsoleRegionCountryValid(consoleRegion, pkm.Country)
return Legal.IsConsoleRegionCountryValid(consoleRegion, pkm.Country)
? new CheckResult(Severity.Valid, V303, CheckIdentifier.Geography)
: new CheckResult(Severity.Invalid, V302, CheckIdentifier.Geography);
}
private static bool IsConsoleRegionCountryValid(int consoleRegion, int country)
{
switch (consoleRegion)
{
case 0: // Japan
return country == 1;
case 1: // Americas
return 8 <= country && country <= 52 || new[] {153, 156, 168, 174, 186}.Contains(country);
case 2: // Europe
return 64 <= country && country <= 127 || new[] {169, 184, 185}.Contains(country);
case 4: // China
return country == 144 || country == 160;
case 5: // Korea
return country == 136;
case 6: // Taiwan
return country == 144 || country == 128;
default:
return false;
}
}
private void VerifyForm()
{
if (!Encounter.Valid)
@@ -2354,7 +2313,7 @@ private void VerifyMiscG1CatchRate(PK1 pk1)
}
private void VerifyMisc()
{
if (pkm.Format == 7 && ((PK7)pkm).PelagoEventStatus != 0)
if (pkm is PK7 pk7 && pk7.PelagoEventStatus != 0)
{
// TODO: Figure out what PelagoEventStati are legal.
}
@@ -2477,9 +2436,9 @@ private void VerifyFatefulIngameActive()
private void VerifyNsPKM()
{
bool req = EncounterMatch is EncounterStaticPID s && s.NSparkle;
if (pkm.Format == 5)
if (pkm is PK5 pk5)
{
bool has = ((PK5)pkm).NPokémon;
bool has = pk5.NPokémon;
if (req && !has)
AddLine(Severity.Invalid, V326, CheckIdentifier.Fateful);
if (!req && has)
@@ -2499,45 +2458,27 @@ private bool VerifyNsPKMOTValid()
{
if (pkm.TID != 00002 || pkm.SID != 00000)
return false;
var OT = pkm.Language == (int)LanguageID.Japanese ? "" : "N";
return OT == pkm.OT_Name;
var ot = pkm.OT_Name;
if (ot.Length != 1)
return false;
var c = pkm.Language == (int)LanguageID.Japanese ? '' : 'N';
return c == ot[0];
}
private void VerifyVersionEvolution()
{
if (pkm.Format < 7)
if (pkm.Format < 7 || EncounterMatch.Species == pkm.Species)
return;
// No point using the evolution tree. Just handle certain species.
bool Sun() => pkm.Version == (int)GameVersion.SN || pkm.Version == (int)GameVersion.US;
bool Moon() => pkm.Version == (int)GameVersion.MN || pkm.Version == (int)GameVersion.UM;
switch (pkm.Species)
{
case 745: // Lycanroc
if (!pkm.WasEgg)
break;
if (pkm.AltForm == 0 && Moon()
|| pkm.AltForm == 1 && Sun())
if (pkm.IsUntraded)
AddLine(Severity.Invalid, V328, CheckIdentifier.Evolution);
break;
case 791: // Solgaleo
if (Moon() && pkm.IsUntraded)
{
if (EncounterMatch is MysteryGift g && g.Species == pkm.Species) // Gifted via Mystery Gift
break;
case 745 when (pkm.AltForm == 0 && Moon()) || (pkm.AltForm == 1 && Sun()): // Lycanroc
case 791 when Moon(): // Solgaleo
case 792 when Sun(): // Lunala
bool Sun() => pkm.Version == (int)GameVersion.SN || pkm.Version == (int)GameVersion.US;
bool Moon() => pkm.Version == (int)GameVersion.MN || pkm.Version == (int)GameVersion.UM;
if (pkm.IsUntraded)
AddLine(Severity.Invalid, V328, CheckIdentifier.Evolution);
}
break;
case 792: // Lunala
if (Sun() && pkm.IsUntraded)
{
if (EncounterMatch is MysteryGift g && g.Species == pkm.Species) // Gifted via Mystery Gift
break;
AddLine(Severity.Invalid, V328, CheckIdentifier.Evolution);
}
break;
}
}

View File

@@ -717,9 +717,9 @@ private static ICollection<int> GetSplitBreedGeneration(int generation)
case 1:
case 2: return Empty;
case 3: return SplitBreed_3;
case 4: return SplitBreed;
case 5: return SplitBreed;
case 6: return SplitBreed;
case 4:
case 5:
case 6:
case 7: return SplitBreed;
default: return Empty;
}
@@ -822,13 +822,6 @@ private static bool IsHeldItemAllowed(int item, int generation)
return items.Length > item && items[item];
}
internal static bool IsNotBaseSpecies(PKM pkm)
{
if (pkm.IsEgg)
return false;
return EvolutionChain.GetValidPreEvolutions(pkm).Count > 1;
}
private static bool IsEvolvedFormChange(PKM pkm)
{
if (pkm.IsEgg)
@@ -842,14 +835,9 @@ private static bool IsEvolvedFormChange(PKM pkm)
return true;
return false;
}
internal static bool IsTradeEvolved(PKM pkm)
internal static bool IsTradeEvolved(EvoCriteria[][] chain, int pkmFormat)
{
if (pkm.IsEgg)
return false;
var table = EvolutionTree.GetEvolutionTree(pkm.Format);
var lineage = table.GetValidPreEvolutions(pkm, maxLevel: 100, skipChecks:true);
return lineage.Any(evolution => EvolutionMethod.TradeMethods.Contains(evolution.Method)); // Trade Evolutions
return chain[pkmFormat].Any(z => EvolutionMethod.TradeMethods.Contains(z.Method));
}
internal static bool IsEvolutionValid(PKM pkm, int minSpecies = -1, int minLevel = -1)
{

View File

@@ -149,5 +149,26 @@ public static int GetRandomFeeling(int memory, int max = 24)
return feel;
}
}
public static void GetMemoryVariables(PKM pkm, out int m, out int t, out int i, out int f, out string resultPrefix, int handler = -1)
{
if (handler < 0)
handler = pkm.CurrentHandler;
switch (handler)
{
case 0:
m = pkm.OT_Memory; t = pkm.OT_TextVar; i = pkm.OT_Intensity; f = pkm.OT_Feeling;
resultPrefix = LegalityCheckStrings.V205;
break;
case 1:
m = pkm.HT_Memory; t = pkm.HT_TextVar; i = pkm.HT_Intensity; f = pkm.HT_Feeling;
resultPrefix = LegalityCheckStrings.V206;
break;
default:
m = t = i = f = 0;
resultPrefix = string.Empty;
break;
}
}
}
}

View File

@@ -281,5 +281,32 @@ public static int GetVivillonPattern(int country, int region)
return sub.form;
return ct.mainform;
}
/// <summary>
/// Compares the <see cref="PKM.ConsoleRegion"/> and <see cref="PKM.Country"/> to determine if the country is available within that region.
/// </summary>
/// <param name="consoleRegion">Console region.</param>
/// <param name="country">Country of nationality</param>
/// <returns>Country is within Console Region</returns>
public static bool IsConsoleRegionCountryValid(int consoleRegion, int country)
{
switch (consoleRegion)
{
case 0: // Japan
return country == 1;
case 1: // Americas
return 8 <= country && country <= 52 || new[] { 153, 156, 168, 174, 186 }.Contains(country);
case 2: // Europe
return 64 <= country && country <= 127 || new[] { 169, 184, 185 }.Contains(country);
case 4: // China
return country == 144 || country == 160;
case 5: // Korea
return country == 136;
case 6: // Taiwan
return country == 144 || country == 128;
default:
return false;
}
}
}
}