Advertisement
Krythic

Voidwalker Random

Oct 10th, 2022
1,110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 17.78 KB | None | 0 0
  1. using SharpDX;
  2. using System;
  3. using System.Collections.Generic;
  4. using VoidwalkerEngine.Framework.DataTypes;
  5. using VoidwalkerEngine.Framework.Logic;
  6. using VoidwalkerEngine.Framework.Maths;
  7.  
  8. namespace VoidwalkerEngine.Framework.Utilities
  9. {
  10.     public sealed class VoidwalkerRandom
  11.     {
  12.         private int _seed;
  13.         private int _inext;
  14.         private int _inextp;
  15.         private int[] _seedArray = new int[56];
  16.  
  17.         /// <summary>
  18.         /// The current seed of this instance.
  19.         /// </summary>
  20.         public int Seed
  21.         {
  22.             get
  23.             {
  24.                 return _seed;
  25.             }
  26.             set
  27.             {
  28.                 _seed = value;
  29.                 int subtraction = (_seed == Int32.MinValue) ? Int32.MaxValue : Math.Abs(_seed);
  30.                 int mj = 0x9a4ec86 - subtraction;
  31.                 _seedArray[0x37] = mj;
  32.                 int mk = 1;
  33.                 for (int i = 1; i < 0x37; i++)
  34.                 {
  35.                     int ii = (0x15 * i) % 0x37;
  36.                     _seedArray[ii] = mk;
  37.                     mk = mj - mk;
  38.                     if (mk < 0x0)
  39.                     {
  40.                         mk += Int32.MaxValue;
  41.                     }
  42.                     mj = _seedArray[ii];
  43.                 }
  44.                 for (int k = 1; k < 0x5; k++)
  45.                 {
  46.                     for (int i = 1; i < 0x38; i++)
  47.                     {
  48.                         _seedArray[i] -= _seedArray[1 + (i + 0x1e) % 0x37];
  49.                         if (_seedArray[i] < 0)
  50.                         {
  51.                             _seedArray[i] += Int32.MaxValue;
  52.                         }
  53.                     }
  54.                 }
  55.                 _inext = 0;
  56.                 _inextp = 21;
  57.             }
  58.         }
  59.  
  60.         public VoidwalkerRandom()
  61.             // Used to generate a varying seed, regardless of close-frequency allocation
  62.             : this(Guid.NewGuid().GetHashCode())
  63.         { }
  64.  
  65.         public VoidwalkerRandom(string seed)
  66.             : this(seed.GetHashCode())
  67.         { }
  68.  
  69.         public VoidwalkerRandom(int[] saveState)
  70.         {
  71.             LoadState(saveState);
  72.         }
  73.  
  74.         public VoidwalkerRandom(int seed)
  75.         {
  76.             Seed = seed;
  77.         }
  78.  
  79.         /// <summary>
  80.         /// Resets this instance using it's current seed.
  81.         /// This means that the RNG will start over again,
  82.         /// repeating the same values that it originally had.
  83.         /// </summary>
  84.         public void Reset()
  85.         {
  86.             Reseed(this.Seed);
  87.         }
  88.  
  89.         /// <summary>
  90.         /// Reseeds this instance using a new GUID Hashcode
  91.         /// </summary>
  92.         public void Reseed()
  93.         {
  94.             Reseed(Guid.NewGuid().GetHashCode());
  95.         }
  96.  
  97.         /// <summary>
  98.         /// Reseeds this instance using the hashcode of a given string.
  99.         /// </summary>
  100.         /// <param name="seed"></param>
  101.         public void Reseed(string seed)
  102.         {
  103.             Reseed(seed.GetHashCode());
  104.         }
  105.  
  106.         /// <summary>
  107.         /// Reseeds this instance using a given integer seed.
  108.         /// </summary>
  109.         /// <param name="seed"></param>
  110.         public void Reseed(int seed)
  111.         {
  112.             this.Seed = seed;
  113.         }
  114.  
  115.         public int NextInteger()
  116.         {
  117.             return NextSample();
  118.         }
  119.  
  120.         public void NextIntegers(int[] buffer)
  121.         {
  122.             for (int i = 0; i < buffer.Length; i++)
  123.             {
  124.                 buffer[i] = NextInteger();
  125.             }
  126.         }
  127.  
  128.         public int[] NextIntegers(int quantity)
  129.         {
  130.             int[] buffer = new int[quantity];
  131.             NextIntegers(buffer);
  132.             return buffer;
  133.         }
  134.  
  135.         public int NextInteger(int minValue, int maxValue)
  136.         {
  137.             return (int)(this.NextSample() * (1.0D / Int32.MaxValue) * (maxValue - minValue)) + minValue;
  138.         }
  139.  
  140.         public void NextIntegers(int minValue, int maxValue, int[] buffer)
  141.         {
  142.             for (int i = 0; i < buffer.Length; i++)
  143.             {
  144.                 buffer[i] = NextInteger(minValue, maxValue);
  145.             }
  146.         }
  147.  
  148.         public int[] NextIntegers(int minValue, int maxValue, int quantity)
  149.         {
  150.             int[] buffer = new int[quantity];
  151.             NextIntegers(minValue, maxValue, buffer);
  152.             return buffer;
  153.         }
  154.  
  155.         public int NextInteger(int maxValue)
  156.         {
  157.             return NextInteger(0, maxValue);
  158.         }
  159.  
  160.         public void NextIntegers(int maxValue, int[] buffer)
  161.         {
  162.             for (int i = 0; i < buffer.Length; i++)
  163.             {
  164.                 buffer[i] = NextInteger(maxValue);
  165.             }
  166.         }
  167.  
  168.         public int[] NextIntegers(int maxValue, int quantity)
  169.         {
  170.             int[] buffer = new int[quantity];
  171.             NextIntegers(maxValue, buffer);
  172.             return buffer;
  173.         }
  174.  
  175.         public double NextDouble()
  176.         {
  177.             return NextSample() * (1.0D / Int32.MaxValue);
  178.         }
  179.  
  180.         public void NextDoubles(double[] buffer)
  181.         {
  182.             for (int i = 0; i < buffer.Length; i++)
  183.             {
  184.                 buffer[i] = NextDouble();
  185.             }
  186.         }
  187.  
  188.         public double[] NextDoubles(int quantity)
  189.         {
  190.             double[] buffer = new double[quantity];
  191.             NextDoubles(buffer);
  192.             return buffer;
  193.         }
  194.  
  195.         public double NextDouble(double minValue, double maxValue)
  196.         {
  197.             return NextDouble() * (maxValue - minValue) + minValue;
  198.         }
  199.  
  200.         public void NextDoubles(double minValue, double maxValue, double[] buffer)
  201.         {
  202.             for (int i = 0; i < buffer.Length; i++)
  203.             {
  204.                 buffer[i] = NextDouble(minValue, maxValue);
  205.             }
  206.         }
  207.  
  208.         public double[] NextDoubles(double minValue, double maxValue, int quantity)
  209.         {
  210.             double[] buffer = new double[quantity];
  211.             NextDoubles(minValue, maxValue, buffer);
  212.             return buffer;
  213.         }
  214.  
  215.         public float NextFloat()
  216.         {
  217.             return (float)(NextSample() * (1.0D / Int32.MaxValue));
  218.         }
  219.  
  220.         public void NextFloats(float[] buffer)
  221.         {
  222.             for (int i = 0; i < buffer.Length; i++)
  223.             {
  224.                 buffer[i] = NextFloat();
  225.             }
  226.         }
  227.  
  228.         public float[] NextFloats(int quantity)
  229.         {
  230.             float[] buffer = new float[quantity];
  231.             NextFloats(buffer);
  232.             return buffer;
  233.         }
  234.  
  235.         public float NextFloat(float minValue, float maxValue)
  236.         {
  237.             return NextFloat() * (maxValue - minValue) + minValue;
  238.         }
  239.  
  240.         public void NextFloats(float minValue, float maxValue, float[] buffer)
  241.         {
  242.             for (int i = 0; i < buffer.Length; i++)
  243.             {
  244.                 buffer[i] = NextFloat(minValue, maxValue);
  245.             }
  246.         }
  247.  
  248.         public float[] NextFloats(float minValue, float maxValue, int quantity)
  249.         {
  250.             float[] buffer = new float[quantity];
  251.             NextFloats(minValue, maxValue, buffer);
  252.             return buffer;
  253.         }
  254.  
  255.         public int NextRange(Range range)
  256.         {
  257.             return NextInteger(range.Minimum, range.Maximum + 1);
  258.         }
  259.  
  260.         public void NextRanges(Range range, int[] buffer)
  261.         {
  262.             for (int i = 0; i < buffer.Length; i++)
  263.             {
  264.                 buffer[i] = NextRange(range);
  265.             }
  266.         }
  267.  
  268.         public int[] NextRanges(Range range, int quantity)
  269.         {
  270.             int[] buffer = new int[quantity];
  271.             NextRanges(range, buffer);
  272.             return buffer;
  273.         }
  274.  
  275.         public int NextRange(int minValue, int maxValue)
  276.         {
  277.             return NextInteger(minValue, maxValue + 1);
  278.         }
  279.  
  280.         public void NextRanges(int minValue, int maxValue, int[] buffer)
  281.         {
  282.             for (int i = 0; i < buffer.Length; i++)
  283.             {
  284.                 buffer[i] = NextRange(minValue, maxValue);
  285.             }
  286.         }
  287.  
  288.         public int[] NextRanges(int minValue, int maxValue, int quantity)
  289.         {
  290.             int[] buffer = new int[quantity];
  291.             NextRanges(minValue, maxValue, buffer);
  292.             return buffer;
  293.         }
  294.  
  295.         public byte NextByte()
  296.         {
  297.             return (byte)NextInteger(0, 256);
  298.         }
  299.  
  300.         public void NextBytes(byte[] buffer)
  301.         {
  302.             for (int i = 0; i < buffer.Length; i++)
  303.             {
  304.                 buffer[i] = NextByte();
  305.             }
  306.         }
  307.  
  308.         public byte[] NextBytes(int quantity)
  309.         {
  310.             byte[] buffer = new byte[quantity];
  311.             NextBytes(buffer);
  312.             return buffer;
  313.         }
  314.  
  315.         public byte NextByte(byte minValue, byte maxValue)
  316.         {
  317.             return (byte)NextInteger(minValue, maxValue);
  318.         }
  319.  
  320.         public void NextBytes(byte minValue, byte maxValue, byte[] buffer)
  321.         {
  322.             for (int i = 0; i < buffer.Length; i++)
  323.             {
  324.                 buffer[i] = NextByte(minValue, maxValue);
  325.             }
  326.         }
  327.  
  328.         public byte[] NextBytes(byte minValue, byte maxValue, int quantity)
  329.         {
  330.             byte[] buffer = new byte[quantity];
  331.             NextBytes(minValue, maxValue, buffer);
  332.             return buffer;
  333.         }
  334.  
  335.         public byte NextByte(byte maxValue)
  336.         {
  337.             return (byte)NextInteger(0, maxValue);
  338.         }
  339.  
  340.         public void NextBytes(byte maxValue, byte[] buffer)
  341.         {
  342.             for (int i = 0; i < buffer.Length; i++)
  343.             {
  344.                 buffer[i] = NextByte(maxValue);
  345.             }
  346.         }
  347.  
  348.         public byte[] NextBytes(byte maxValue, int quantity)
  349.         {
  350.             byte[] buffer = new byte[quantity];
  351.             NextBytes(maxValue, buffer);
  352.             return buffer;
  353.         }
  354.  
  355.         public bool NextBoolean()
  356.         {
  357.             return NextInteger(0, 2) == 1;
  358.         }
  359.  
  360.         public void NextBools(bool[] buffer)
  361.         {
  362.             for (int i = 0; i < buffer.Length; i++)
  363.             {
  364.                 buffer[i] = NextBoolean();
  365.             }
  366.         }
  367.  
  368.         public bool[] NextBools(int quantity)
  369.         {
  370.             bool[] buffer = new bool[quantity];
  371.             NextBools(buffer);
  372.             return buffer;
  373.         }
  374.  
  375.         public string NextString(char[] possibleCharacters, int length)
  376.         {
  377.             char[] buffer = new char[length];
  378.             for (int i = 0; i < buffer.Length; i++)
  379.             {
  380.                 buffer[i] = possibleCharacters[NextInteger(0, possibleCharacters.Length)];
  381.             }
  382.             return new string(buffer);
  383.         }
  384.  
  385.         public void NextStrings(char[] possibleCharacters, int length, string[] buffer)
  386.         {
  387.             for (int i = 0; i < buffer.Length; i++)
  388.             {
  389.                 buffer[i] = NextString(possibleCharacters, length);
  390.             }
  391.         }
  392.  
  393.         public string[] NextStrings(char[] possibleCharacters, int length, int quantity)
  394.         {
  395.             string[] buffer = new string[quantity];
  396.             NextStrings(possibleCharacters, length, buffer);
  397.             return buffer;
  398.         }
  399.  
  400.         public void Shuffle<T>(List<T> list)
  401.         {
  402.             int n = list.Count;
  403.             while (n > 1)
  404.             {
  405.                 n--;
  406.                 int k = NextInteger(0, n + 1);
  407.                 T value = list[k];
  408.                 list[k] = list[n];
  409.                 list[n] = value;
  410.             }
  411.         }
  412.  
  413.         public void Shuffle<T>(T[] list)
  414.         {
  415.             int n = list.Length;
  416.             while (n > 1)
  417.             {
  418.                 n--;
  419.                 int k = NextInteger(0, n + 1);
  420.                 T value = list[k];
  421.                 list[k] = list[n];
  422.                 list[n] = value;
  423.             }
  424.         }
  425.  
  426.         public int NextIndex<T>(T[] array)
  427.         {
  428.             return NextInteger(array.Length);
  429.         }
  430.  
  431.         public int NextIndex<T>(List<T> list)
  432.         {
  433.             return NextInteger(list.Count);
  434.         }
  435.  
  436.         public T Choose<T>(T[] items)
  437.         {
  438.             return items[NextInteger(items.Length)];
  439.         }
  440.  
  441.         public void Choose<T>(T[] items, T[] resultBuffer)
  442.         {
  443.             for (int i = 0; i < resultBuffer.Length; i++)
  444.             {
  445.                 resultBuffer[i] = Choose(items);
  446.             }
  447.         }
  448.  
  449.         public T[] Choose<T>(T[] items, int quantity)
  450.         {
  451.             T[] buffer = new T[quantity];
  452.             Choose(items, buffer);
  453.             return buffer;
  454.         }
  455.  
  456.         public T Choose<T>(List<T> items)
  457.         {
  458.             return items[NextInteger(0, items.Count)];
  459.         }
  460.  
  461.         public List<T> Choose<T>(List<T> items, int quantity)
  462.         {
  463.             List<T> buffer = new List<T>(quantity);
  464.             for (int i = 0; i < quantity; i++)
  465.             {
  466.                 buffer.Add(Choose(items));
  467.             }
  468.             return buffer;
  469.         }
  470.  
  471.         public bool NextProbability(float percent)
  472.         {
  473.             if (percent >= 1.0f) return true;
  474.             if (percent <= 0.0f) return false;
  475.             return NextDouble() <= percent;
  476.         }
  477.  
  478.         public bool NextProbability(double percent)
  479.         {
  480.             if (percent >= 1.0D) return true;
  481.             if (percent <= 0.0D) return false;
  482.             return NextDouble() <= percent;
  483.         }
  484.  
  485.         public void NextProbability(float percent, bool[] buffer)
  486.         {
  487.             for (int i = 0; i < buffer.Length; i++)
  488.             {
  489.                 buffer[i] = NextProbability(percent);
  490.             }
  491.         }
  492.  
  493.         public bool[] NextProbabilities(float percent, int quantity)
  494.         {
  495.             bool[] buffer = new bool[quantity];
  496.             NextProbability(percent, buffer);
  497.             return buffer;
  498.         }
  499.  
  500.         public bool NextProbability(Probability probability)
  501.         {
  502.             if (probability.IsGuranteed)
  503.             {
  504.                 return true;
  505.             }
  506.             if (probability.IsImpossible)
  507.             {
  508.                 return false;
  509.             }
  510.             return probability.Numerator >= NextInteger(1, probability.Denominator + 1);
  511.         }
  512.  
  513.         public bool NextProbability(int numerator, int denominator)
  514.         {
  515.             return NextProbability(new Probability(numerator,denominator));
  516.         }
  517.  
  518.         public bool NextChance(int percent)
  519.         {
  520.             if (percent >= 100) return true;
  521.             if (percent <= 0) return false;
  522.             return NextInteger(101) <= percent;
  523.         }
  524.  
  525.         public void NextProbabilities(int percent, bool[] buffer)
  526.         {
  527.             for (int i = 0; i < buffer.Length; i++)
  528.             {
  529.                 buffer[i] = NextChance(percent);
  530.             }
  531.         }
  532.  
  533.         public bool[] NextProbabilities(int percent, int quantity)
  534.         {
  535.             bool[] buffer = new bool[quantity];
  536.             NextProbabilities(percent, buffer);
  537.             return buffer;
  538.         }
  539.  
  540.         public int NextDiceRoll(DiceType d)
  541.         {
  542.             return NextInteger(1, (int)d + 1);
  543.         }
  544.  
  545.         public void NextDiceRolls(DiceType d, int[] buffer)
  546.         {
  547.             for (int i = 0; i < buffer.Length; i++)
  548.             {
  549.                 buffer[i] = NextDiceRoll(d);
  550.             }
  551.         }
  552.  
  553.         public int[] NextDiceRolls(DiceType d, int quantity)
  554.         {
  555.             int[] buffer = new int[quantity];
  556.             NextDiceRolls(d, buffer);
  557.             return buffer;
  558.         }
  559.  
  560.         public Vector3 NextVector3(float xMin, float xMax, float yMin, float yMax, float zMin, float zMax)
  561.         {
  562.             return new Vector3(NextFloat(xMin, xMax), NextFloat(yMin, yMax), NextFloat(zMin, zMax));
  563.         }
  564.  
  565.         public Vector3 NextVector3(Vector3 min, Vector3 max)
  566.         {
  567.             return NextVector3(min.X, max.X, min.Y, max.Y, min.Z, max.Z);
  568.         }
  569.  
  570.         public Vector3 NextVector3(float min, float max)
  571.         {
  572.             return NextVector3(min, max, min, max, min, max);
  573.         }
  574.  
  575.         public int NextDamageSeed()
  576.         {
  577.             return NextRange(85, 100);
  578.         }
  579.  
  580.         private int NextSample()
  581.         {
  582.             int locINext = _inext;
  583.             int locINextp = _inextp;
  584.             if (++locINext >= 56)
  585.             {
  586.                 locINext = 1;
  587.             }
  588.             if (++locINextp >= 56)
  589.             {
  590.                 locINextp = 1;
  591.             }
  592.             int retVal = _seedArray[locINext] - _seedArray[locINextp];
  593.             if (retVal == Int32.MaxValue)
  594.             {
  595.                 retVal--;
  596.             }
  597.             if (retVal < 0)
  598.             {
  599.                 retVal += Int32.MaxValue;
  600.             }
  601.             _seedArray[locINext] = retVal;
  602.             _inext = locINext;
  603.             _inextp = locINextp;
  604.             return retVal;
  605.         }
  606.  
  607.         public int[] GetState()
  608.         {
  609.             int[] state = new int[59];
  610.             state[0] = _seed;
  611.             state[1] = _inext;
  612.             state[2] = _inextp;
  613.             for (int i = 3; i < this._seedArray.Length; i++)
  614.             {
  615.                 state[i] = _seedArray[i - 3];
  616.             }
  617.             return state;
  618.         }
  619.  
  620.         public void LoadState(int[] saveState)
  621.         {
  622.             if (saveState.Length != 59)
  623.             {
  624.                 throw new Exception("GrimoireRandom state was corrupted!");
  625.             }
  626.             _seed = saveState[0];
  627.             _inext = saveState[1];
  628.             _inextp = saveState[2];
  629.             _seedArray = new int[59];
  630.             for (int i = 3; i < this._seedArray.Length; i++)
  631.             {
  632.                 _seedArray[i - 3] = saveState[i];
  633.             }
  634.         }
  635.     }
  636. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement