Replace some linq usage with direct logic

This commit is contained in:
Kurt 2021-05-06 23:26:38 -07:00
parent fa5bc0d634
commit 31142ee297
19 changed files with 124 additions and 73 deletions

View file

@ -24,7 +24,7 @@ namespace PKHeX.Core
if (pk.Format <= 3)
return; // no markings (gen3 only has 4; can't mark stats intelligently
var markings = ivs.Select(MarkingMethod(pk)).ToArray();
var markings = ivs.Select(MarkingMethod(pk)).ToArray(); // future: stackalloc
pk.Markings = PKX.ReorderSpeedLast(markings);
}

View file

@ -107,16 +107,24 @@ namespace PKHeX.Core
private static int[]? GetSuggestedHiddenPowerIVs(int hpVal, int[] IVs)
{
var flawless = IVs.Select((v, i) => v == 31 ? i : -1).Where(v => v != -1).ToArray();
var permutations = GetPermutations(flawless, flawless.Length);
int flawedCount = 0;
int[]? best = null;
const int max = 31;
var flawless = new int[IVs.Length]; // future: stackalloc
int flawlessCount = 0;
for (int i = 0; i < IVs.Length; i++)
{
if (IVs[i] == max)
flawless[++flawlessCount] = i;
}
var permutations = GetPermutations(flawless, flawlessCount);
int flawedCount = 0; // result tracking
int[]? best = null; // result tracking
int[] ivs = (int[])IVs.Clone();
foreach (var permute in permutations)
{
foreach (var item in permute)
foreach (var index in permute)
{
ivs[item] ^= 1;
ivs[index] ^= 1;
if (hpVal != GetType(ivs))
continue;
@ -129,7 +137,7 @@ namespace PKHeX.Core
break; // any further flaws are always worse
}
// Restore IVs for another iteration
Array.Copy(IVs, 0, ivs, 0, ivs.Length);
Buffer.BlockCopy(IVs, 0, ivs, 0, ivs.Length);
}
return best;
}

View file

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
namespace PKHeX.Core
@ -25,7 +26,7 @@ namespace PKHeX.Core
public string Move2 => Get(Strings.movelist, pkm.Move2);
public string Move3 => Get(Strings.movelist, pkm.Move3);
public string Move4 => Get(Strings.movelist, pkm.Move4);
public string HeldItem => Get(Strings.GetItemStrings(pkm.Format), pkm.HeldItem);
public string HeldItem => GetSpan(Strings.GetItemStrings(pkm.Format), pkm.HeldItem);
public string HP => Stats[0].ToString();
public string ATK => Stats[1].ToString();
public string DEF => Stats[2].ToString();
@ -121,5 +122,6 @@ namespace PKHeX.Core
/// <param name="val">Index to fetch</param>
/// <returns>Null if array is null</returns>
private static string Get(IReadOnlyList<string> arr, int val) => (uint)val < arr.Count ? arr[val] : string.Empty;
private static string GetSpan(ReadOnlySpan<string> arr, int val) => (uint)val < arr.Length ? arr[val] : string.Empty;
}
}
}

View file

@ -123,19 +123,9 @@ namespace PKHeX.Core
return Array.Empty<string>();
var ew = ((SAV7b)S1).Blocks.EventWork;
var fOn = SetFlags.Select(z => new { Type = ew.GetFlagType(z, out var subIndex), Index = subIndex, Raw = z })
.Select(z => $"{z.Raw:0000}\t{true }\t{z.Index:0000}\t{z.Type}").ToArray();
var fOff = ClearedFlags.Select(z => new { Type = ew.GetFlagType(z, out var subIndex), Index = subIndex, Raw = z })
.Select(z => $"{z.Raw:0000}\t{false}\t{z.Index:0000}\t{z.Type}").ToArray();
var w = WorkChanged.Select((z, i) => new
{
Type = ew.GetWorkType(z, out var subIndex),
Index = subIndex,
Raw = z,
Text = WorkDiff[i]
}).ToArray();
var wt = w.Select(z => $"{z.Raw:000}\t{z.Text}\t{z.Index:000}\t{z.Type}").ToArray();
var fOn = SetFlags.Select(z => new FlagSummary(z, ew).ToString());
var fOff = ClearedFlags.Select(z => new FlagSummary(z, ew).ToString());
var wt = WorkChanged.Select((z, i) => new WorkSummary(z, ew, WorkDiff[i]).ToString());
var list = new List<string> { "Flags: ON", "=========" };
list.AddRange(fOn);
@ -158,5 +148,39 @@ namespace PKHeX.Core
return list;
}
private readonly struct FlagSummary
{
private EventVarType Type { get; }
private int Index { get; }
private int Raw { get; }
public override string ToString() => $"{Raw:0000}\t{false}\t{Index:0000}\t{Type}";
public FlagSummary(int rawIndex, EventWork7b ew)
{
Type = ew.GetFlagType(rawIndex, out var subIndex);
Index = subIndex;
Raw = rawIndex;
}
}
private readonly struct WorkSummary
{
private EventVarType Type { get; }
private int Index { get; }
private int Raw { get; }
private string Text { get; }
public override string ToString() => $"{Raw:000}\t{Text}\t{Index:000}\t{Type}";
public WorkSummary(int rawIndex, EventWork7b ew, string text)
{
Type = ew.GetFlagType(rawIndex, out var subIndex);
Index = subIndex;
Raw = rawIndex;
Text = text;
}
}
}
}

View file

@ -273,7 +273,7 @@ namespace PKHeX.Core
if (HeldItem > 0)
{
var items = Strings.GetItemStrings(Format);
if ((uint)HeldItem < items.Count)
if ((uint)HeldItem < items.Length)
result += $" @ {items[HeldItem]}";
}
return result;
@ -384,7 +384,7 @@ namespace PKHeX.Core
bool TrySetItem(int format)
{
var items = (string[])Strings.GetItemStrings(format); // IReadOnlyList<string>->string[] must be possible for the provided strings
var items = Strings.GetItemStrings(format);
int item = StringUtil.FindIndexIgnoreCase(items, itemName);
if (item < 0)
return false;

View file

@ -384,18 +384,15 @@ namespace PKHeX.Core
// metSWSH_30000[17] += " (-)"; // Pokémon HOME -- duplicate with 40000's entry
}
public IReadOnlyList<string> GetItemStrings(int generation, GameVersion game = GameVersion.Any)
public string[] GetItemStrings(int generation, GameVersion game = GameVersion.Any) => generation switch
{
return generation switch
{
0 => Array.Empty<string>(),
1 => g1items,
2 => g2items,
3 => GetItemStrings3(game),
4 => g4items, // mail names changed 4->5
_ => itemlist
};
}
0 => Array.Empty<string>(),
1 => g1items,
2 => g2items,
3 => GetItemStrings3(game),
4 => g4items, // mail names changed 4->5
_ => itemlist
};
private string[] GetItemStrings3(GameVersion game)
{

View file

@ -32,21 +32,23 @@ namespace PKHeX.Core
private static List<ComboItem> CreateGen2(GameStrings s)
{
var locations = Util.GetCBList(s.metGSC_00000, Enumerable.Range(0, 0x5F).ToArray());
Util.AddCBWithOffset(locations, s.metGSC_00000, 00000, 0x7E, 0x7F);
var locations = Util.GetCBList(s.metGSC_00000.AsSpan(0, 0x5F));
Util.AddCBWithOffset(locations, s.metGSC_00000.AsSpan(0x7E, 2), 0x7E);
return locations;
}
private static List<ComboItem> CreateGen3(GameStrings s)
{
var locations = Util.GetCBList(s.metRSEFRLG_00000, Enumerable.Range(0, 213).ToArray());
Util.AddCBWithOffset(locations, s.metRSEFRLG_00000, 00000, 253, 254, 255);
var locations = Util.GetCBList(s.metRSEFRLG_00000.AsSpan(0, 213));
Util.AddCBWithOffset(locations, s.metRSEFRLG_00000.AsSpan(253, 3), 253);
return locations;
}
private static List<ComboItem> CreateGen3CXD(GameStrings s)
{
return Util.GetCBList(s.metCXD_00000, Enumerable.Range(0, s.metCXD_00000.Length).ToArray()).Where(c => c.Text.Length > 0).ToList();
var list = Util.GetCBList(s.metCXD_00000);
list.RemoveAll(z => z.Text.Length == 0);
return list;
}
private static List<ComboItem> CreateGen4(GameStrings s)

View file

@ -1,4 +1,4 @@
using System.Linq;
using System;
using static PKHeX.Core.EncounterUtil;
using static PKHeX.Core.GameVersion;
using static PKHeX.Core.EncounterGBLanguage;
@ -103,9 +103,9 @@ namespace PKHeX.Core
new EncounterStatic2Roam(245, 40, GS), // Suicune
};
private static readonly EncounterStatic2[] Encounter_GS = Encounter_GSC_Common.Concat(Encounter_GS_Exclusive).Concat(Encounter_GSC_Roam).ToArray();
private static readonly EncounterStatic2[] Encounter_C = Encounter_GSC_Common.Concat(Encounter_C_Exclusive).Concat(Encounter_GSC_Roam.Slice(0, 2)).ToArray();
private static readonly EncounterStatic2[] Encounter_GSC = Encounter_GSC_Common.Concat(Encounter_GS_Exclusive).Concat(Encounter_C_Exclusive).Concat(Encounter_GSC_Roam).ToArray();
private static readonly EncounterStatic2[] Encounter_GS = ArrayUtil.ConcatAll(Encounter_GSC_Common, Encounter_GS_Exclusive, Encounter_GSC_Roam);
private static readonly EncounterStatic2[] Encounter_C = ArrayUtil.ConcatAll(Encounter_GSC_Common, Encounter_C_Exclusive, Encounter_GSC_Roam.AsSpan(0, 2));
private static readonly EncounterStatic2[] Encounter_GSC = ArrayUtil.ConcatAll(Encounter_GSC_Common, Encounter_GS_Exclusive, Encounter_C_Exclusive, Encounter_GSC_Roam);
internal static readonly EncounterTrade2[] TradeGift_GSC =
{

View file

@ -1,5 +1,4 @@
using System.Linq;
using static PKHeX.Core.EncounterUtil;
using static PKHeX.Core.EncounterUtil;
using static PKHeX.Core.GameVersion;
namespace PKHeX.Core
@ -396,13 +395,15 @@ namespace PKHeX.Core
new EncounterTrade4RanchSpecial(489, 01) { Moves = new[] {447,240,156,057}, TID = 1000, SID = 09248, OTGender = 1, Location = 3000, Ball = 0x10, Gender = 2, CurrentLevel = 50, EggLocation = 3000, }, // Phione
};
internal static readonly EncounterTrade4[] TradeGift_DPPt = new EncounterTrade4PID[]
private static readonly EncounterTrade4PID[] TradeGift_DPPtIngame =
{
new(DPPt, 0x0000008E, 063, 01) { Ability = 1, TID = 25643, SID = 00000, OTGender = 1, Gender = 0, IVs = new[] {15,15,15,20,25,25} }, // Machop -> Abra
new(DPPt, 0x00000867, 441, 01) { Ability = 2, TID = 44142, SID = 00000, OTGender = 0, Gender = 1, IVs = new[] {15,20,15,25,25,15}, Contest = 20 }, // Buizel -> Chatot
new(DPPt, 0x00000088, 093, 35) { Ability = 1, TID = 19248, SID = 00000, OTGender = 1, Gender = 0, IVs = new[] {20,25,15,25,15,15} }, // Medicham (35 from Route 217) -> Haunter
new(DPPt, 0x0000045C, 129, 01) { Ability = 1, TID = 53277, SID = 00000, OTGender = 0, Gender = 1, IVs = new[] {15,25,15,20,25,15} }, // Finneon -> Magikarp
}.Concat(RanchGifts).ToArray();
};
internal static readonly EncounterTrade4[] TradeGift_DPPt = ArrayUtil.ConcatAll(TradeGift_DPPtIngame, RanchGifts);
internal static readonly EncounterTrade4PID[] TradeGift_HGSS =
{

View file

@ -4,7 +4,6 @@ using static PKHeX.Core.GameVersion;
using static PKHeX.Core.AreaWeather8;
using static PKHeX.Core.Encounters8Nest;
using System.Linq;
namespace PKHeX.Core
{
@ -790,7 +789,7 @@ namespace PKHeX.Core
new(SWSH, 105,15,01,038,06,2) { TID7 = 101141, FlawlessIVCount = 3, DynamaxLevel = 5, OTGender = 1, Shiny = Shiny.Random, IsNicknamed = false, Relearn = new[] {174,000,000,000}, Form = 1 }, // Marowak-1
};
internal static readonly EncounterTrade8[] TradeGift_SWSH = TradeGift_Regular.Concat(TradeGift_R1).ToArray();
internal static readonly EncounterTrade8[] TradeGift_SWSH = ArrayUtil.ConcatAll(TradeGift_Regular, TradeGift_R1);
internal static readonly EncounterStatic[] StaticSW = ArrayUtil.ConcatAll<EncounterStatic>(Nest_Common, Nest_SW, Nest_SH, Dist_DLC2, Dist_DLC1, Dist_Base, GetEncounters(Crystal_SWSH, SW), DynAdv_SWSH, GetEncounters(Encounter_SWSH, SW));
internal static readonly EncounterStatic[] StaticSH = ArrayUtil.ConcatAll<EncounterStatic>(Nest_Common, Nest_SW, Nest_SH, Dist_DLC2, Dist_DLC1, Dist_Base, GetEncounters(Crystal_SWSH, SH), DynAdv_SWSH, GetEncounters(Encounter_SWSH, SH));

View file

@ -1,5 +1,4 @@
using System.Linq;
using static PKHeX.Core.Encounters8Nest;
using static PKHeX.Core.Encounters8Nest;
namespace PKHeX.Core
{

View file

@ -49,7 +49,7 @@ namespace PKHeX.Core
if (pkm.HeldItem > 0)
{
var items = s.GetItemStrings(pkm.Format);
if ((uint)pkm.HeldItem < items.Count)
if ((uint)pkm.HeldItem < items.Length)
yield return $" @ {items[pkm.HeldItem]}";
}

View file

@ -470,7 +470,7 @@ namespace PKHeX.Core
new InventoryPouchGB(InventoryType.Items, LegalItems, 99, Offsets.PouchItem, 20),
new InventoryPouchGB(InventoryType.KeyItems, LegalKeyItems, 99, Offsets.PouchKey, 26),
new InventoryPouchGB(InventoryType.Balls, LegalBalls, 99, Offsets.PouchBall, 12),
new InventoryPouchGB(InventoryType.PCItems, LegalItems.Concat(LegalKeyItems).Concat(LegalBalls).Concat(LegalTMHMs).ToArray(), 99, Offsets.PouchPC, 50)
new InventoryPouchGB(InventoryType.PCItems, ArrayUtil.ConcatAll(LegalItems, LegalKeyItems, LegalBalls, LegalTMHMs), 99, Offsets.PouchPC, 50)
};
return pouch.LoadAll(Data);
}

View file

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace PKHeX.Core
{
@ -135,7 +134,8 @@ namespace PKHeX.Core
public override string GetDaycareRNGSeed(int loc)
{
int ofs = loc == 0 ? DaycareOffset : Daycare2;
var data = Data.Skip(ofs + 0x1E8).Take(DaycareSeedSize / 2).Reverse().ToArray();
var data = Data.AsSpan(ofs + 0x1E8, DaycareSeedSize / 2).ToArray();
Array.Reverse(data);
return BitConverter.ToString(data).Replace("-", string.Empty);
}

View file

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace PKHeX.Core
{
@ -100,7 +99,8 @@ namespace PKHeX.Core
public override string GetDaycareRNGSeed(int loc)
{
int ofs = DaycareOffset;
var data = Data.Skip(ofs + 0x1E8).Take(DaycareSeedSize / 2).Reverse().ToArray();
var data = Data.AsSpan(ofs + 0x1E8, DaycareSeedSize / 2).ToArray();
Array.Reverse(data);
return BitConverter.ToString(data).Replace("-", string.Empty);
}

View file

@ -1,5 +1,4 @@
using System;
using System.Linq;
namespace PKHeX.Core
{
@ -37,7 +36,8 @@ namespace PKHeX.Core
{
if (value.Length == 0)
{
Enumerable.Repeat<byte>(0xFF, 8).ToArray().CopyTo(Data, 0x12);
for (int i = 0; i < 8; i++)
Data[0x12 + i] = 0xFF;
}
else
{

View file

@ -37,7 +37,7 @@ namespace PKHeX.Core
if (msg != null)
{
var itemstr = GameInfo.Strings.GetItemStrings(pkm.Format, (GameVersion)pkm.Version);
errata.Add($"{msg} {(held >= itemstr.Count ? held.ToString() : itemstr[held])}");
errata.Add($"{msg} {(held >= itemstr.Length ? held.ToString() : itemstr[held])}");
}
}

View file

@ -203,5 +203,15 @@ namespace PKHeX.Core
arr3.CopyTo(result, arr1.Length + arr2.Length);
return result;
}
internal static T[] ConcatAll<T>(T[] arr1, T[] arr2, Span<T> arr3)
{
int len = arr1.Length + arr2.Length + arr3.Length;
var result = new T[len];
arr1.CopyTo(result, 0);
arr2.CopyTo(result, arr1.Length);
arr3.CopyTo(result.AsSpan(arr1.Length + arr2.Length));
return result;
}
}
}
}

View file

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace PKHeX.Core
{
@ -43,10 +42,10 @@ namespace PKHeX.Core
return arr;
}
public static List<ComboItem> GetCBList(IReadOnlyList<string> inStrings)
public static List<ComboItem> GetCBList(ReadOnlySpan<string> inStrings)
{
var list = new List<ComboItem>(inStrings.Count);
for (int i = 0; i < inStrings.Count; i++)
var list = new List<ComboItem>(inStrings.Length);
for (int i = 0; i < inStrings.Length; i++)
list.Add(new ComboItem(inStrings[i], i));
list.Sort(Comparer);
return list;
@ -82,12 +81,10 @@ namespace PKHeX.Core
return list;
}
public static List<ComboItem> GetCBList(IReadOnlyList<string> inStrings, params int[][] allowed)
public static List<ComboItem> GetCBList(IReadOnlyList<string> inStrings, int[] allowed)
{
var count = allowed.Sum(z => z.Length);
var list = new List<ComboItem>(count);
foreach (var arr in allowed)
AddCB(list, inStrings, arr);
var list = new List<ComboItem>(allowed.Length);
AddCB(list, inStrings, allowed);
return list;
}
@ -97,7 +94,7 @@ namespace PKHeX.Core
list.Add(item);
}
public static void AddCBWithOffset(List<ComboItem> cbList, IReadOnlyList<string> inStrings, int offset, params int[] allowed)
public static void AddCBWithOffset(List<ComboItem> cbList, IReadOnlyList<string> inStrings, int offset, int[] allowed)
{
int beginCount = cbList.Count;
foreach (var index in allowed)
@ -108,6 +105,18 @@ namespace PKHeX.Core
cbList.Sort(beginCount, allowed.Length, Comparer);
}
public static void AddCBWithOffset(List<ComboItem> cbList, Span<string> inStrings, int offset)
{
int beginCount = cbList.Count;
for (int i = 0; i < inStrings.Length; i++)
{
var x = inStrings[i];
var item = new ComboItem(x, i + offset);
cbList.Add(item);
}
cbList.Sort(beginCount, inStrings.Length, Comparer);
}
public static void AddCB(List<ComboItem> cbList, IReadOnlyList<string> inStrings, int[] allowed)
{
int beginCount = cbList.Count;