Make generic randomizer more generic

int -> T
add getmany method
This commit is contained in:
Kurt
2018-12-01 15:16:37 -08:00
parent b085c1976b
commit 104a554e9a
4 changed files with 22 additions and 15 deletions

View File

@@ -5,14 +5,14 @@
/// The shuffled list is iterated over, and reshuffled when exhausted.
/// The list does not repeat values until the list is exhausted.
/// </remarks>
public class GenericRandomizer
public class GenericRandomizer<T>
{
public GenericRandomizer(int[] randomValues)
public GenericRandomizer(T[] randomValues)
{
RandomValues = randomValues;
}
private readonly int[] RandomValues;
private readonly T[] RandomValues;
private int ctr;
public int Count => RandomValues.Length;
@@ -22,14 +22,22 @@ public void Reset()
Util.Shuffle(RandomValues);
}
public int Next()
public T Next()
{
if (ctr == 0)
Util.Shuffle(RandomValues);
int value = RandomValues[ctr++];
T value = RandomValues[ctr++];
ctr %= RandomValues.Length;
return value;
}
public T[] GetMany(int count)
{
var arr = new T[count];
for (int i = 0; i < arr.Length; i++)
arr[i] = Next();
return arr;
}
}
}

View File

@@ -11,7 +11,7 @@ public class MoveRandomizer : Randomizer
private readonly PersonalTable SpeciesStat;
private readonly GameInfo Config;
private GenericRandomizer RandMove;
private GenericRandomizer<int> RandMove;
internal MovesetRandSettings Settings;
public MoveRandomizer(GameInfo config, Move[] moves, PersonalTable t)
@@ -20,7 +20,7 @@ public MoveRandomizer(GameInfo config, Move[] moves, PersonalTable t)
var maxMoveId = config.MaxMoveID;
MoveData = moves;
SpeciesStat = t;
RandMove = new GenericRandomizer(Enumerable.Range(1, maxMoveId - 1).ToArray());
RandMove = new GenericRandomizer<int>(Enumerable.Range(1, maxMoveId - 1).ToArray());
}
public override void Execute() => throw new Exception("Shouldn't be called.");
@@ -41,7 +41,7 @@ public void Initialize(MovesetRandSettings settings, int[] bannedMoves)
var all = Enumerable.Range(1, Config.MaxMoveID - 1);
var moves = all.Except(nanned);
RandMove = new GenericRandomizer(moves.ToArray());
RandMove = new GenericRandomizer<int>(moves.ToArray());
}
public int[] GetRandomLearnset(int index, int movecount) => GetRandomLearnset(SpeciesStat[index].Types, movecount);
@@ -128,7 +128,7 @@ public void ReorderMovesPower(IList<int> moves)
141, // Leech Life
};
private static readonly GenericRandomizer first = new GenericRandomizer(firstMoves);
private static readonly GenericRandomizer<int> first = new GenericRandomizer<int>(firstMoves);
public int GetRandomFirstMoveAny()
{

View File

@@ -1,5 +1,4 @@
using System;
using System.Linq;
using System.Linq;
using pkNX.Structures;
namespace pkNX.Randomization
@@ -28,11 +27,11 @@ public void Initialize(SpeciesSettings settings, params int[] banlist)
{
s = settings;
var list = s.GetSpecies(Game.MaxSpeciesID, Game.Generation).Except(banlist);
RandSpec = new GenericRandomizer(list.ToArray());
RandSpec = new GenericRandomizer<int>(list.ToArray());
}
#region Random Species Filtering Parameters
private GenericRandomizer RandSpec;
private GenericRandomizer<int> RandSpec;
private int loopctr;
private const int l = 10; // tweakable scalars
private const int h = 11;

View File

@@ -16,7 +16,7 @@ public class TrainerRandomizer : Randomizer
private readonly IList<int> SpecialClasses;
private readonly IList<int> CrashClasses;
public GenericRandomizer Class { get; set; }
public GenericRandomizer<int> Class { get; set; }
public LearnsetRandomizer Learn { get; set; }
public SpeciesRandomizer RandSpec { get; set; }
public MoveRandomizer RandMove { get; set; }
@@ -46,7 +46,7 @@ public void Initialize(TrainerRandSettings settings)
IEnumerable<int> classes = Enumerable.Range(0, ClassCount).Except(CrashClasses);
if (Settings.SkipSpecialClasses)
classes = classes.Except(SpecialClasses);
Class = new GenericRandomizer(classes.ToArray());
Class = new GenericRandomizer<int>(classes.ToArray());
}
public override void Execute()