Krythic

Truncation Selection

Sep 24th, 2021 (edited)
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.24 KB | None | 0 0
  1.  
  2.  
  3.  
  4. public List<MagicAffix> GenerateAffixes(GameItemType itemConstraint, int level, AffixType affixType, int count, bool allowDuplicates = false)
  5.         {
  6.             List<MagicAffix> results = new List<MagicAffix>();
  7.             List<MagicAffix> candidates = new List<MagicAffix>();
  8.             double totalFitness = 0.0;
  9.             /**
  10.              * Filter MagicAffix Candidates by our provided constraints
  11.              */
  12.             foreach (MagicAffix affix in MagicAffix.AllAffixes)
  13.             {
  14.                 if (affix.LevelBracket.Contains(level))
  15.                 {
  16.                     if (affix.Constraints.Contains(itemConstraint) && affix.AffixType == affixType)
  17.                     {
  18.                         candidates.Add(affix);
  19.                         totalFitness += affix.SpawnChance.ToPercentage();
  20.                     }
  21.                 }
  22.             }
  23.             /**
  24.              * If the total candidates is zero, it means there were not matches
  25.              * in the database for the provided constraints. This shouldn't ever
  26.              * happen.
  27.              */
  28.             if (candidates.Count == 0)
  29.             {
  30.                 return null;
  31.             }
  32.             /**
  33.              * If the total candidates is less than the desired count, we will
  34.              * tranform the desired count to the total available candidates. This
  35.              * can be bypassed if allowDuplicates is set to true.
  36.              */
  37.             if (candidates.Count < count && allowDuplicates == false)
  38.             {
  39.                 count = candidates.Count;
  40.             }
  41.             /**
  42.              * Normalize the spawn chances to a 0.0-1.0 range, then sort them based upon it.
  43.              */
  44.             candidates.OrderBy(o => o.SpawnChance.ToPercentage() / totalFitness).Reverse().ToList();
  45.             Console.WriteLine("Desired Count: " + count);
  46.             for (int i = 0; i < count; i++)
  47.             {
  48.                 Console.WriteLine("New Iteration");
  49.                 double sample = _random.NextDouble();
  50.                 foreach (MagicAffix candidate in candidates)
  51.                 {
  52.                     double candidateSpawnChance = candidate.SpawnChance.ToPercentage();
  53.                     double fitness = candidateSpawnChance / totalFitness;
  54.                     if (sample < fitness)
  55.                     {
  56.                         results.Add(candidate);
  57.                         if (!allowDuplicates)
  58.                         {
  59.                             /**
  60.                              * Remove the selected candidate and recalculate normalized fitness
  61.                              */
  62.                             candidates.Remove(candidate);
  63.                             totalFitness = 0;
  64.                             foreach (MagicAffix affix in candidates)
  65.                             {
  66.                                 totalFitness += affix.SpawnChance.ToPercentage();
  67.                             }
  68.                             candidates.OrderBy(obj => obj.SpawnChance.ToPercentage() / totalFitness).Reverse().ToList();
  69.                         }
  70.                         break;
  71.                     }
  72.                     sample -= fitness;
  73.                 }
  74.             }
  75.             return results;
  76.         }
Add Comment
Please, Sign In to add comment