Advertisement
Guest User

ARK: Breeding and mutation probabilities pseudo code

a guest
Jun 13th, 2017
1,177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.10 KB | None | 0 0
  1. namespace ArkBreedingPseudoCode
  2. {
  3.     public enum Gender { Female, Male }
  4.  
  5.     public class PrimalDinoCharacter
  6.     {
  7.         /// <summary>
  8.         /// Stat levels (0: health, 1: stamina, 2: torpor, 3: oxygen, 4: food, 5: water, 6: temperature, 7: weight, 8: melee damage, 9: movement speed, 10: fortitude, 11: crafting speed)
  9.         /// </summary>
  10.         public int[] NumberOfLevelUpPointsApplied { get; set; }
  11.         public int[] ColorSetIndices { get; set; }
  12.         public int RandomMutationsMale { get; set; }
  13.         public int RandomMutationsFemale { get; set; }
  14.         public Gender Gender { get; set; }
  15.  
  16.         // Creature specific defines
  17.         public bool[] CanLevelUpValue { get; set; }
  18.         public bool[] DontUseValue { get; set; }
  19.         public int RandomMutationRolls { get; set; }
  20.         public float RandomMutationChance { get; set; }
  21.         public float RandomMutationGivePoints { get; set; }
  22.         public bool[] PreventColorizationRegions { get; set; }
  23.  
  24.         // Game specific defines
  25.         public int RandomMutationCountLimit { get; set; }
  26.  
  27.         public PrimalDinoCharacter()
  28.         {
  29.             // Setup default values
  30.             ColorSetIndices = new int[6];
  31.             NumberOfLevelUpPointsApplied = new int[12];
  32.  
  33.             // Species settings (these may vary with different species)
  34.             CanLevelUpValue = new[] { true, true, false, true, true, false, false, true, true, true, false, false };
  35.             DontUseValue = new[] { false, false, false, false, false, false, false, false, false, false, false, false };
  36.             PreventColorizationRegions = new[] { false, false, false, false, false, false };
  37.             RandomMutationRolls = 3; //currently 3 for all species
  38.             RandomMutationChance = 0.025f; //currently 0.025 for all species
  39.             RandomMutationGivePoints = 2.0f; //currently +2 for all species
  40.  
  41.             // Game settings
  42.             RandomMutationCountLimit = 20;
  43.         }
  44.  
  45.         private Random _random = new Random();
  46.  
  47.         /// <summary>
  48.         /// Generates a random floating-point number that is greater than or equal to 0.0, and less than 1.0.
  49.         /// </summary>
  50.         /// <remarks>ARK uses std::rand</remarks>
  51.         private double NewRandom()
  52.         {
  53.             return _random.NextDouble();
  54.         }
  55.  
  56.         /// <summary>
  57.         /// Generates a random bounded integer.
  58.         /// </summary>
  59.         /// <remarks>ARK uses PCG_Random</remarks>
  60.         private int NewBoundedRandom(int bound)
  61.         {
  62.             return _random.Next(0, bound);
  63.         }
  64.  
  65.         /// <summary>
  66.         /// Simulate when two ARK Creatures are mated
  67.         /// </summary>
  68.         public PrimalDinoCharacter DoMate(PrimalDinoCharacter withMate)
  69.         {
  70.             if (this.Gender == withMate.Gender) return null;
  71.  
  72.             var randomMutationCount = this.RandomMutationsMale + this.RandomMutationsFemale;
  73.             var randomMutationCountMate = withMate.RandomMutationsMale + withMate.RandomMutationsFemale;
  74.             var offspring = new PrimalDinoCharacter
  75.             {
  76.                 RandomMutationsFemale = this.Gender == Gender.Female ? randomMutationCount : randomMutationCountMate,
  77.                 RandomMutationsMale = this.Gender == Gender.Male ? randomMutationCount : randomMutationCountMate
  78.             };
  79.  
  80.             // Inherited colors (there is a 50/50 chance the color is inherited from each respective parent)
  81.             for (var i = 0; i < 6; i++)
  82.             {
  83.                 offspring.ColorSetIndices[i] = NewRandom() <= 0.5 ? withMate.ColorSetIndices[i] : this.ColorSetIndices[i];
  84.             }
  85.  
  86.             // Inherited stats (there is a 55/45 chance to inherit the best stat)
  87.             var mutatableStatIndices = new List<int>();
  88.             for (var i = 0; i < 12; i++)
  89.             {
  90.                 var chanceToInheritStat = this.NumberOfLevelUpPointsApplied[i] <= withMate.NumberOfLevelUpPointsApplied[i]
  91.                     ? 0.55000001f : 0.44999999f;
  92.  
  93.                 offspring.NumberOfLevelUpPointsApplied[i] = NewRandom() <= chanceToInheritStat
  94.                     ? withMate.NumberOfLevelUpPointsApplied[i] : this.NumberOfLevelUpPointsApplied[i];
  95.  
  96.                 if (this.CanLevelUpValue[i] && !this.DontUseValue[i]) mutatableStatIndices.Add(i);
  97.             }
  98.  
  99.             // Random mutations
  100.             if (mutatableStatIndices.Count > 0)
  101.             {
  102.                 for (var i = 0; i < this.RandomMutationRolls; i++)
  103.                 {
  104.                     // A random stat is selected
  105.                     var randomStatIndex = mutatableStatIndices[NewBoundedRandom(mutatableStatIndices.Count)];
  106.  
  107.                     // Mutation is tied to one parent (there is a 55/45 chance for it to be the parent with the best stat)
  108.                     var chanceForParent = this.NumberOfLevelUpPointsApplied[randomStatIndex] <= withMate.NumberOfLevelUpPointsApplied[randomStatIndex]
  109.                         ? 0.55000001f : 0.44999999f;
  110.                     var randomParentRoll = NewRandom();
  111.                     var randomMutationIsFromMate = randomParentRoll <= chanceForParent;
  112.  
  113.                     // If the selected parent have exceeded the random mutation limit skip this roll
  114.                     if ((randomMutationIsFromMate ? randomMutationCountMate : randomMutationCount) >= this.RandomMutationCountLimit)
  115.                         continue;
  116.  
  117.                     // Roll for a random mutation to this stat
  118.                     if (NewRandom() >= this.RandomMutationChance)
  119.                         continue;
  120.  
  121.                     var newStatValue = offspring.NumberOfLevelUpPointsApplied[randomStatIndex] + this.RandomMutationGivePoints;
  122.  
  123.                     // All stats are limited to 255
  124.                     if (newStatValue > 255.0)
  125.                         continue;
  126.  
  127.                     // Update offspring
  128.                     offspring.NumberOfLevelUpPointsApplied[randomStatIndex] = (int)Math.Floor(newStatValue);
  129.                     if ((randomMutationIsFromMate ? withMate : this).Gender == Gender.Male) offspring.RandomMutationsMale++;
  130.                     else offspring.RandomMutationsFemale++;
  131.  
  132.                     // Random color mutation code simplified (may not be perfectly accurate):
  133.                     // 1. Make sure the species have customizable colors
  134.                     // 2. Create an array with all color regions that are not disallowed (->PreventColorizationRegions)
  135.                     // 3. Roll a random color from the available ones
  136.                     // 4. Roll a random color index from the available ones
  137.  
  138.                     var colors = 56;
  139.                     var colorIndices = Enumerable.Range(0, 6).Select(x => this.PreventColorizationRegions[i] ? null : (int?)x).Where(x => x.HasValue).Select(x => x.Value).ToArray();
  140.                     var randomColor = NewBoundedRandom(colors) + 1;
  141.                     var randomColorIndex = colorIndices[NewBoundedRandom(colorIndices.Length)];
  142.  
  143.                     offspring.ColorSetIndices[randomColorIndex] = randomColor;
  144.                 }
  145.             }
  146.  
  147.             return offspring;
  148.         }
  149.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement