Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public List<MagicAffix> GenerateAffixes(GameItemType itemConstraint, int level, AffixType affixType, int count, bool allowDuplicates = false)
- {
- List<MagicAffix> results = new List<MagicAffix>();
- List<MagicAffix> candidates = new List<MagicAffix>();
- double totalFitness = 0.0;
- /**
- * Filter MagicAffix Candidates by our provided constraints
- */
- foreach (MagicAffix affix in MagicAffix.AllAffixes)
- {
- if (affix.LevelBracket.Contains(level))
- {
- if (affix.Constraints.Contains(itemConstraint) && affix.AffixType == affixType)
- {
- candidates.Add(affix);
- totalFitness += affix.SpawnChance.ToPercentage();
- }
- }
- }
- /**
- * If the total candidates is zero, it means there were not matches
- * in the database for the provided constraints. This shouldn't ever
- * happen.
- */
- if (candidates.Count == 0)
- {
- return null;
- }
- /**
- * If the total candidates is less than the desired count, we will
- * tranform the desired count to the total available candidates. This
- * can be bypassed if allowDuplicates is set to true.
- */
- if (candidates.Count < count && allowDuplicates == false)
- {
- count = candidates.Count;
- }
- /**
- * Normalize the spawn chances to a 0.0-1.0 range, then sort them based upon it.
- */
- candidates.OrderBy(o => o.SpawnChance.ToPercentage() / totalFitness).Reverse().ToList();
- Console.WriteLine("Desired Count: " + count);
- for (int i = 0; i < count; i++)
- {
- Console.WriteLine("New Iteration");
- double sample = _random.NextDouble();
- foreach (MagicAffix candidate in candidates)
- {
- double candidateSpawnChance = candidate.SpawnChance.ToPercentage();
- double fitness = candidateSpawnChance / totalFitness;
- if (sample < fitness)
- {
- results.Add(candidate);
- if (!allowDuplicates)
- {
- /**
- * Remove the selected candidate and recalculate normalized fitness
- */
- candidates.Remove(candidate);
- totalFitness = 0;
- foreach (MagicAffix affix in candidates)
- {
- totalFitness += affix.SpawnChance.ToPercentage();
- }
- candidates.OrderBy(obj => obj.SpawnChance.ToPercentage() / totalFitness).Reverse().ToList();
- }
- break;
- }
- sample -= fitness;
- }
- }
- return results;
- }
Add Comment
Please, Sign In to add comment