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(enchantment => enchantment.SpawnChance.ToPercentage() / totalFitness).Reverse().ToList();
- bool isSearching = true;
- int iterations = 0;
- int maximumIterations = 256;
- while (isSearching)
- {
- MagicAffix candidate = null;
- double sample = _random.NextDouble();
- foreach (MagicAffix attempt in candidates)
- {
- double fitness = attempt.SpawnChance.ToPercentage() / totalFitness;
- if (sample < fitness)
- {
- candidate = attempt;
- break;
- }
- sample -= fitness;
- }
- if (!results.Exists(x => x.Property == candidate.Property) || allowDuplicates == true)
- {
- iterations = 0;
- results.Add(candidate);
- }
- iterations++;
- if (results.Count == count || iterations >= maximumIterations)
- {
- isSearching = false;
- }
- }
- //System.Console.WriteLine();
- //System.Console.WriteLine("Affix Type: " + affixType);
- //System.Console.WriteLine("Candidates Count: " + candidates.Count);
- //System.Console.WriteLine("Requested Affix Count: " + count);
- //System.Console.WriteLine("Iterations: " + iterations);
- //System.Console.WriteLine("Affix Total: " + affixTotal);
- //System.Console.WriteLine();
- return results;
- }
Add Comment
Please, Sign In to add comment