Misc qol updates

Check max case for level first, then iterate upwards (eliminates inner
if)
change wc* nature to sbyte (update comparisons, 0xFF too magic-y)
use Rand.Next(x) instead of (0, x)
This commit is contained in:
Kurt 2018-10-27 21:59:31 -07:00
parent ed3699fbb4
commit 39f98a4a94
15 changed files with 32 additions and 33 deletions

View File

@ -145,7 +145,7 @@ private void ParseLegality()
if (!pkm.IsOriginValid)
AddLine(Severity.Invalid, LEncConditionBadSpecies, CheckIdentifier.GameOrigin);
if (pkm.Format == 1 || pkm.Format == 2) // prior to storing GameVersion
if (pkm.Format <= 2) // prior to storing GameVersion
{
ParsePK1();
return;

View File

@ -727,7 +727,7 @@ private static IEnumerable<int> GetValidMoves(PKM pkm, GameVersion Version, IRea
{
int formcount = pkm.PersonalInfo.FormeCount;
// In gen 3 deoxys has different forms depending on the current game, in personal info there is no alter form info
// In gen 3 deoxys has different forms depending on the current game, in the PersonalInfo there is no alternate form info
if (species == 386 && pkm.Format == 3)
formcount = 4;

View File

@ -95,7 +95,7 @@ public PKM ConvertToPKM(ITrainerInfo SAV)
}
pk.Language = lang;
var ability = Util.Rand.Next(0, 2);
var ability = Util.Rand.Next(2);
var pidtype = GetPIDType();
if (pidtype == PIDType.PokeSpot)
PIDGenerator.SetRandomPokeSpotPID(pk, nature, gender, ability, SlotNumber);

View File

@ -84,8 +84,8 @@ public static IEnumerable<EncounterSlot> GetValidWildEncounters(PKM pkm, IReadOn
return Enumerable.Empty<EncounterSlot>();
var s = GetRawEncounterSlots(pkm, lvl, vs, gameSource);
bool IsSafariBall = pkm.Ball == 5;
bool IsSportBall = pkm.Ball == 0x18;
bool IsSafariBall = pkm.Ball == (int)Ball.Safari;
bool IsSportBall = pkm.Ball == (int)Ball.Sport;
bool IsHidden = pkm.AbilityNumber == 4; // hidden Ability
int species = pkm.Species;
@ -102,8 +102,8 @@ public static IEnumerable<EncounterSlot> GetValidWildEncounters(PKM pkm, GameVer
return Enumerable.Empty<EncounterSlot>();
var s = GetRawEncounterSlots(pkm, lvl, gameSource);
bool IsSafariBall = pkm.Ball == 5;
bool IsSportBall = pkm.Ball == 0x18;
bool IsSafariBall = pkm.Ball == (int)Ball.Safari;
bool IsSportBall = pkm.Ball == (int)Ball.Sport;
bool IsHidden = pkm.AbilityNumber == 4; // hidden Ability
int species = pkm.Species;
@ -218,8 +218,8 @@ private static IEnumerable<EncounterSlot> GetFilteredSlots67(PKM pkm, IEnumerabl
// Edge Case Handling
switch (species)
{
case 744 when form == 1:
case 745 when form == 2:
case 744 when form == 1: // Rockruff Event
case 745 when form == 2: // Lycanroc Event
yield break;
}
@ -432,7 +432,7 @@ internal static bool IsDexNavValid(PKM pkm)
return false;
var vs = EvolutionChain.GetValidPreEvolutions(pkm);
var table = pkm.Version == (int) GameVersion.AS ? Encounters6.SlotsA : Encounters6.SlotsO;
var table = pkm.Version == (int) GameVersion.AS ? SlotsA : SlotsO;
int loc = pkm.Met_Location;
var areas = table.Where(l => l.Location == loc);
var d_areas = areas.Select(area => GetValidEncounterSlots(pkm, area, vs, DexNav: true));

View File

@ -305,7 +305,7 @@ private static bool GetIsMatchPGF(PKM pkm, PGF wc, IEnumerable<DexLevel> vs)
if (wc.Level != pkm.Met_Level) return false;
if (wc.Ball != pkm.Ball) return false;
if (wc.Nature != 0xFF && wc.Nature != pkm.Nature) return false;
if (wc.Nature != -1 && wc.Nature != pkm.Nature) return false;
if (wc.Gender != 2 && wc.Gender != pkm.Gender) return false;
if (pkm is IContestStats s && s.IsContestBelow(wc))
@ -354,7 +354,7 @@ private static bool GetIsMatchWC6(PKM pkm, WC6 wc, IEnumerable<DexLevel> vs)
if (wc.Level != pkm.Met_Level) return false;
if (wc.Ball != pkm.Ball) return false;
if (wc.OTGender < 3 && wc.OTGender != pkm.OT_Gender) return false;
if (wc.Nature != 0xFF && wc.Nature != pkm.Nature) return false;
if (wc.Nature != -1 && wc.Nature != pkm.Nature) return false;
if (wc.Gender != 3 && wc.Gender != pkm.Gender) return false;
if (pkm is IContestStats s && s.IsContestBelow(wc))
@ -415,7 +415,7 @@ private static bool GetIsMatchWC7(PKM pkm, WC7 wc, IEnumerable<DexLevel> vs)
if (wc.MetLevel != pkm.Met_Level) return false;
if (wc.Ball != pkm.Ball) return false;
if (wc.OTGender < 3 && wc.OTGender != pkm.OT_Gender) return false;
if (wc.Nature != 0xFF && wc.Nature != pkm.Nature) return false;
if (wc.Nature != -1 && wc.Nature != pkm.Nature) return false;
if (wc.Gender != 3 && wc.Gender != pkm.Gender) return false;
if (pkm is IContestStats s && s.IsContestBelow(wc))

View File

@ -180,7 +180,7 @@ public static int GetRandomFeeling(int memory, int max = 24)
var bits = MemoryFeelings[memory];
while (true)
{
int feel = Util.Rand.Next(0, max);
int feel = Util.Rand.Next(max);
if ((bits & (1 << feel)) != 0)
return feel;
}

View File

@ -340,7 +340,7 @@ private static PIDType GetPIDType(PKM pk, PIDType specific)
if (pk.Version == 15)
return PIDType.CXD;
if (pk.GenNumber == 3 && pk.Species == 201)
return PIDType.Method_1_Unown + Util.Rand.Next(0, 3);
return PIDType.Method_1_Unown + Util.Rand.Next(3);
return PIDType.Method_1;
}

View File

@ -16,7 +16,6 @@ public static partial class Legal
public static readonly int[] Gen4EncounterTypes = { 1, 2, 4, 5, 7, 9, 10, 12, 23, 24 };
public static readonly HashSet<int> LightBall = new HashSet<int> { 25, 26, 172 };
public static readonly int[] Fossils = { 138, 140, 142, 345, 347, 408, 410, 564, 566, 696, 698 };
public static readonly int[] RotomMoves = { 0, 315, 056, 059, 403, 437 };
public static readonly HashSet<int> WildForms = new HashSet<int>

View File

@ -153,7 +153,7 @@ public static partial class Legal
}).ToArray();
public static readonly Dictionary<int, int> ZCrystalDictionary = Pouch_ZCrystal_USUM
.Zip(Pouch_ZCrystalHeld_USUM, (k, v) => new { Key = (int)k, Value = (int)v })
.Zip(Pouch_ZCrystalHeld_USUM, (k, v) => new KeyValuePair<int, int>(k, v))
.ToDictionary(x => x.Key, x => x.Value);
internal static readonly ushort[] HeldItems_SM = new ushort[1].Concat(Pouch_Items_SM).Concat(Pouch_Berries_SM).Concat(Pouch_Medicine_SM).Concat(Pouch_ZCrystalHeld_SM).ToArray();

View File

@ -134,7 +134,7 @@ IEnumerable<RibbonResult> getMissingContestRibbons(IReadOnlyList<bool> bits, IRe
if (pkm is IRibbonSetCommon4 s4)
{
bool inhabited4 = 3 <= gen && gen <= 4;
IEnumerable<RibbonResult> iterate = GetInvalidRibbons4Any(pkm, s4, gen);
var iterate = GetInvalidRibbons4Any(pkm, s4, gen);
if (!inhabited4)
iterate = iterate.Concat(GetInvalidRibbonsNone(s4.RibbonBitsOnly(), s4.RibbonNamesOnly()));
foreach (var z in iterate)
@ -382,12 +382,13 @@ private static IEnumerable<RibbonResult> GetInvalidRibbonsNone(IReadOnlyList<boo
private static bool IsAllowedInContest4(int species) => species != 201 && species != 132; // Disallow Unown and Ditto
private static bool IsAllowedBattleFrontier(int species, int form = 0, int gen = 0)
private static bool IsAllowedBattleFrontier(int species) => !Legal.BattleFrontierBanlist.Contains(species);
private static bool IsAllowedBattleFrontier(int species, int form, int gen)
{
if (gen == 4 && species == 172 && form == 1) // spiky
return false;
return !Legal.BattleFrontierBanlist.Contains(species);
return IsAllowedBattleFrontier(species);
}
}
}

View File

@ -57,7 +57,7 @@ public string Nickname
set => Encoding.Unicode.GetBytes(value.PadRight(0xB, (char)0xFFFF)).CopyTo(Data, 0x1E);
}
public int Nature { get => Data[0x34]; set => Data[0x34] = (byte)value; }
public int Nature { get => (sbyte)Data[0x34]; set => Data[0x34] = (byte)value; }
public override int Gender { get => Data[0x35]; set => Data[0x35] = (byte)value; }
public override int AbilityType { get => Data[0x36]; set => Data[0x36] = (byte)value; }
public int PIDType { get => Data[0x37]; set => Data[0x37] = (byte)value; }
@ -181,7 +181,7 @@ public override PKM ConvertToPKM(ITrainerInfo SAV)
Species = Species,
HeldItem = HeldItem,
Met_Level = currentLevel,
Nature = Nature != 0xFF ? Nature : Util.Rand.Next(25),
Nature = Nature != -1 ? Nature : Util.Rand.Next(25),
Gender = pi.Gender == 255 ? 2 : (Gender != 2 ? Gender : pi.RandomGender),
AltForm = Form,
Version = OriginGame == 0 ? SAV.Game : OriginGame,

View File

@ -164,7 +164,7 @@ public string Nickname
set => Encoding.Unicode.GetBytes(value.PadRight(12 + 1, '\0')).CopyTo(Data, 0x86);
}
public int Nature { get => Data[0xA0]; set => Data[0xA0] = (byte)value; }
public int Nature { get => (sbyte)Data[0xA0]; set => Data[0xA0] = (byte)value; }
public override int Gender { get => Data[0xA1]; set => Data[0xA1] = (byte)value; }
public override int AbilityType { get => Data[0xA2]; set => Data[0xA2] = (byte)value; }
public Shiny PIDType { get => (Shiny)Data[0xA3]; set => Data[0xA3] = (byte)value; }
@ -297,7 +297,7 @@ public override PKM ConvertToPKM(ITrainerInfo SAV)
TID = TID,
SID = SID,
Met_Level = currentLevel,
Nature = Nature != 0xFF ? Nature : Util.Rand.Next(25),
Nature = Nature != -1 ? Nature : Util.Rand.Next(25),
Gender = Gender != 3 ? Gender : pi.RandomGender,
AltForm = Form,
EncryptionConstant = EncryptionConstant != 0 ? EncryptionConstant : Util.Rand32(),

View File

@ -201,7 +201,7 @@ public string Nickname
set => Encoding.Unicode.GetBytes(value.PadRight(12 + 1, '\0')).CopyTo(Data, 0x86);
}
public int Nature { get => Data[0xA0]; set => Data[0xA0] = (byte)value; }
public int Nature { get => (sbyte)Data[0xA0]; set => Data[0xA0] = (byte)value; }
public override int Gender { get => Data[0xA1]; set => Data[0xA1] = (byte)value; }
public override int AbilityType { get => Data[0xA2]; set => Data[0xA2] = (byte)value; }
public Shiny PIDType { get => (Shiny)Data[0xA3]; set => Data[0xA3] = (byte)value; }
@ -337,7 +337,7 @@ public override PKM ConvertToPKM(ITrainerInfo SAV)
TID = TID,
SID = SID,
Met_Level = metLevel,
Nature = Nature != 0xFF ? Nature : Util.Rand.Next(25),
Nature = Nature != -1 ? Nature : Util.Rand.Next(25),
Gender = Gender != 3 ? Gender : pi.RandomGender,
AltForm = Form,
EncryptionConstant = EncryptionConstant != 0 ? EncryptionConstant : Util.Rand32(),

View File

@ -14,12 +14,11 @@ public static class Experience
public static int GetLevel(int species, uint exp)
{
int growth = PKX.Personal[species].EXPGrowth;
if (exp >= ExpTable[99, growth])
return 100;
int tl = 1; // Initial Level. Iterate upwards to find the level
while (ExpTable[tl, growth] <= exp)
{
if (++tl == 100)
break;
}
while (exp >= ExpTable[tl, growth])
++tl;
return tl;
}

View File

@ -1137,7 +1137,7 @@ public static ushort[] AdvanceGCKeys(ushort[] oldKeys)
uint s7 = 4294;
if (val > 967295)
s7--;
s7 = (uint)Util.Rand.Next(0, (int)s7);
s7 = (uint)Util.Rand.Next((int)s7);
val += s7 * 1000000;
}
var TID = (ushort)(val & 0xFFFF);