Krythic

New Generator Function

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