Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Quick Question about perlin noise

Discussion in 'Scripting' started by RiokuTheSlayer, Mar 4, 2015.

  1. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    I just had a question about perlin noise. I was wondering how, if you had infinite terrain in a game (Yes, like minecraft), you would use Unity's perlin noise to generate everything? There's a few problems I can see.

    Problems.
    (1)Mathf.PerlinNoise isn't pseduo-random, so to speak. Every point will give you the exact same value at any time for any person, no matter what. Your (0.2,0.2) is the same as everyone else's.(2) Alright, so you can scale it to not use as big of an area at once, cool. But that makes the terrain differences seem much "smoother", as less space is being sampled in the image for the same amount of world space, and (3) after moving away from that point far enough, the terrain would become flat forever. (4) But, if we loop back to the other side, we get the problem of repetitive terrain.


    I'm just wondering if there's a way to solve all four of these problems by having something that allows you to generate a perlin noise field of infinite size, all you need to do is give it a seed, then the position, which will be based off of that seed.

    Is there any simple file or something from the assets store to grab for this, or is there a way to do this with the built-in system that I don't know about?
     
  2. bloomingdedalus

    bloomingdedalus

    Joined:
    Aug 13, 2012
    Posts:
    139
    No matter what your perlin noise will repeat at some point unless you make a custom coordinate system with literally no limit to the size of the number - but that would be unnecessary for 99.99999% of applications. In almost all situations that you're going to move very far from Unity's origin it's better to design your entire game to move around the player rather than the other way around due to the fact that Unity uses floats for coordinates and at far distances you start losing a lot of precision.

    Here's one I wrote and it's all yours if you want it for commercial or personal applications (not giving out my 3d one free though as it's a far more refined version of this 2d one)... It tiles Perlin noise from long.MinValue to long.MaxValue - though you might have to take a look at the gradient function to ensure it's not actually repeating at some point. However, you can only sample integers with this with a desired initial zoom. (64 is 1/64, 32 is 1/32 - eg integer 32 is perlin coordinate .5 at 64 zoom)

    Instructions for use:

    1. Create a DeterministicRNG object with (long)seed.
    2. Create a GradientManager object with the DeterministicRNG and a Zoom Factor
    3. Create a Perlin2dHash object with the GradientManager
    4. Create PerlinVector object each time you want to sample a coordinate with the same zoom factor as the Gradient manager (very important)
    5. Use Perlin2dHash.Sample(PerlinVector) as many times as you want octaves. The vector will automatically change its coordinates by multiplying them by two for the next octave sample each time it is sampled.
    6. Under PerlinVector.vals will be the results of your sampling... vals[0] is octave 1, vals[1] is octave 2, vals[2] is octave 3 etc.. You can then add these together with the desired persistance.


    Code (csharp):
    1.  
    2.  
    3. namespace Perlin2d
    4. {
    5.   using System.Collections.Generic;
    6.   using System;
    7.   using System.Text;
    8.   using System.IO;
    9.   using UnityEngine;
    10.  
    11.   public delegate fVec2 AssignGradDel(ulong x, ulong y);
    12.   public delegate float LerpDel(float a, float b, float amount);
    13.   public delegate float FadeDel(float t);
    14.  
    15.   public class DeterministicRNG
    16.   {
    17.   private long sd = 0;
    18.   public byte[] Array;
    19.  
    20.   public DeterministicRNG(long Seed)
    21.   {
    22.   sd = Seed;
    23.   string seed = String.Format("{0}", Seed);
    24.   RNG2_0 tempRNG = new RNG2_0(seed);
    25.   Array = tempRNG.GetArray();
    26.   }
    27.  
    28.   public DeterministicRNG(ulong Seed)
    29.   {
    30.   sd = BitConverter.ToInt64(BitConverter.GetBytes(Seed), 0);
    31.   string seed = String.Format("{0}", Seed);
    32.   RNG2_0 tempRNG = new RNG2_0(seed);
    33.   Array = tempRNG.GetArray();
    34.   }
    35.   }
    36.  
    37.   public class GradientManager : PerlinCoord
    38.   {
    39.   public AssignGradDel MyDel;
    40.   public LerpDel LerpDel;
    41.   public FadeDel FadeDel;
    42.   private ulong Modulus;
    43.  
    44.   private DeterministicRNG MyRNG;
    45.  
    46.   public GradientManager(ulong X, ulong Y, DeterministicRNG rng, int ZoomFactor)
    47.   {
    48.   MyRNG = rng;
    49.   //Coord[0] = X;
    50.   //Coord[1] = Y;
    51.   MyDel = new AssignGradDel(ReturnGradient);
    52.   LerpDel = new LerpDel(Slerp);
    53.   FadeDel = new FadeDel(Fade);
    54.   Modulus = ulong.MaxValue /(ulong)(ZoomFactor*2);
    55.   }
    56.  
    57.   #region Gradient Functions
    58.  
    59.   public fVec2 ReturnGradient(ulong X, ulong Y)
    60.   {
    61.   ulong x1 = (ulong)X & Modulus ;
    62.   ulong y1 = (ulong)Y & Modulus ;
    63.   x1 = x1 * x1  + 1;
    64.   y1 = y1 * y1 * y1 +3 * x1 ;
    65.   int val = ((byte)(((y1 ^ x1) * 961747207 ) % 256));
    66.   fVec2 ret = fVec2.CircleGrad[MyRNG.Array[val]*2];
    67.  
    68.   //AppConsole.Instance.WriteLine(X + "  " + Y + "  " + ret.ToString());  961747207
    69.   return ret;
    70.  
    71.   }
    72.  
    73.   #endregion
    74.  
    75.   #region LerpFunctions
    76.  
    77.   public static float StandardLerp(float a, float b, float amount)
    78.   {
    79.   return a * (1 - amount) + b * (amount);
    80.   }
    81.  
    82.   public static float Slerp(float a, float b, float amount)
    83.   {
    84.   amount = (amount * amount) * (3 - 2 * amount);
    85.   return StandardLerp(a, b, amount);
    86.   }
    87.  
    88.   public static float Fade(float t)
    89.   {
    90.   return t*t*t*(t*(t*6-15)+10);
    91.   }
    92.  
    93.   #endregion
    94.   }
    95.  
    96.   public class PerlinCoord
    97.   {
    98.   protected ulong[] Coord = new ulong[2];
    99.   protected fVec2 FP = new fVec2(0,0);
    100.   protected bool flipneg = false;
    101.  
    102.   public ulong x
    103.   {
    104.   get
    105.   {
    106.   return Coord[0];
    107.   }
    108.   set
    109.   {
    110.   Coord[0] = value;
    111.   }
    112.   }
    113.   public ulong y
    114.   {
    115.   get
    116.   {
    117.   return Coord[1];
    118.   }
    119.   set
    120.   {
    121.   Coord[1] = value;
    122.   }
    123.   }
    124.  
    125.   public PerlinCoord()
    126.   {
    127.  
    128.   }
    129.  
    130.   public PerlinCoord(long X, long Y)
    131.   {
    132.   Coord[0] =  ConvertLong(X);
    133.   Coord[1] = ConvertLong(Y);
    134.   }
    135.  
    136.   public PerlinCoord(PerlinCoord pc)
    137.   {
    138.   x = pc.x;
    139.   y = pc.y;
    140.   }
    141.  
    142.   public ulong ConvertLong(long inc)
    143.   {
    144.   if(inc < 0)
    145.   {
    146.   flipneg = true;
    147.   }
    148.   if (flipneg)
    149.   {
    150.   return BitConverter.ToUInt64(BitConverter.GetBytes(inc), 0) + 1;
    151.   }
    152.   else
    153.   {
    154.   return BitConverter.ToUInt64(BitConverter.GetBytes(inc), 0);
    155.   }
    156.   }
    157.  
    158.   public override int GetHashCode()
    159.   {
    160.   return (int)(x ^ y);
    161.   }
    162.  
    163.   public override bool Equals(object obj)
    164.   {
    165.   if (obj is PerlinCoord)
    166.   {
    167.   PerlinCoord pc = (PerlinCoord)obj;
    168.   if (pc.x == this.x && pc.y == this.y)
    169.   {
    170.   return true;
    171.   }
    172.   else
    173.   {
    174.   return false;
    175.   }
    176.   }
    177.   else
    178.   {
    179.   return false;
    180.   }
    181.   }
    182.  
    183.   public string DebugString()
    184.   {
    185.   return "< " + x + ", " + y + " >  < " + FP.x + ", " + FP.y + " >";
    186.   }
    187.   }
    188.  
    189.   public class PerlinVector : PerlinCoord
    190.   {
    191.   private float ZoomFactor = 64;
    192.   private int sampnum = 0;
    193.   public List<float> Vals = new List<float>();
    194.  
    195.   public ulong x
    196.   {
    197.   get
    198.   {
    199.   return Coord[0];
    200.   }
    201.   }
    202.   public ulong y
    203.   {
    204.   get
    205.   {
    206.   return Coord[1];
    207.   }
    208.   }
    209.   public int CurrentSample
    210.   {
    211.   get
    212.   {
    213.   return sampnum;
    214.   }
    215.   }
    216.   public fVec2 Float
    217.   {
    218.   get
    219.   {
    220.   return FP;
    221.   }
    222.   }
    223.   public float ZoomVal
    224.   {
    225.   get
    226.   {
    227.   return ZoomFactor;
    228.   }
    229.   set
    230.   {
    231.   ZoomFactor = value;
    232.   }
    233.   }
    234.  
    235.   public PerlinVector()
    236.   {
    237.  
    238.   }
    239.  
    240.   public PerlinVector(long X, long Y)
    241.   {
    242.   Coord[0] = ConvertLong( X);
    243.   Coord[1] = ConvertLong(Y);
    244.   Initilize();
    245.   //AppConsole.Instance.WriteLine(DebugString());
    246.   }
    247.  
    248.   public PerlinVector(long X, long Y, int Zoom)
    249.   {
    250.   Coord[0] = ConvertLong(X);
    251.   Coord[1] = ConvertLong(Y);
    252.   ZoomFactor = Zoom;
    253.   Initilize();
    254.   //AppConsole.Instance.WriteLine(DebugString());
    255.   }
    256.  
    257.   public void AddVal(float val)
    258.   {
    259.   Vals.Add(val);
    260.   StepFreq();
    261.   }
    262.  
    263.   public void StepFreq()
    264.   {
    265.   FP.x = FP.x * 2;
    266.   FP.y = FP.y * 2;
    267.   Coord[0] = (x << 1) + (ulong)sampnum + (ulong)FP.x;
    268.   Coord[1] = (y << 1)  + (ulong)sampnum + (ulong)FP.y;
    269.   FP.x = FP.x % 1;
    270.   FP.y = FP.y % 1;
    271.   sampnum++;
    272.   }
    273.  
    274.   public void Initilize()
    275.   {
    276.   if (!flipneg)
    277.   {
    278.   FP.x = (float)(x % (ulong)ZoomFactor) / ZoomFactor;
    279.   FP.y = (float)(y % (ulong)ZoomFactor) / ZoomFactor;
    280.   }
    281.   else
    282.   {
    283.   FP.x = (float)(x % (ulong)ZoomFactor) / ZoomFactor;
    284.   FP.y = (float)(y % (ulong)ZoomFactor) / ZoomFactor;
    285.   }
    286.   Coord[0] = Coord[0] / (ulong)ZoomFactor;
    287.   Coord[1] = Coord[1] / (ulong)ZoomFactor;
    288.   }
    289.  
    290.   public override int GetHashCode()
    291.   {
    292.   return (int)(Coord[0] ^ Coord[1]);
    293.   }
    294.  
    295.   public override bool Equals(object obj)
    296.   {
    297.   if (obj is PerlinVector)
    298.   {
    299.   PerlinVector pv = (PerlinVector)obj;
    300.   if (this.x == pv.x && this.y == pv.y)
    301.   {
    302.   return true;
    303.   }
    304.   else
    305.   {
    306.   return false;
    307.   }
    308.   }
    309.   else
    310.   {
    311.   return false;
    312.   }
    313.   }
    314.   }
    315.  
    316.   public partial class Perlin2dSquare
    317.   {
    318.   private fVec2[,] Grads = new fVec2[2, 2];
    319.   GradientManager gm;
    320.  
    321.   public Perlin2dSquare(GradientManager Gradient, PerlinCoord Coord)
    322.   {
    323.   gm = Gradient;
    324.   for (int i = 0; i < 2; i++)
    325.   {
    326.   for (int j = 0; j < 2; j++)
    327.   {
    328.   Grads[i, j] = Gradient.ReturnGradient((ulong)i+Coord.x,  (ulong)j +Coord.y);
    329.   }
    330.   }
    331.   }
    332.  
    333.   public float Sample(fVec2 per)
    334.   {
    335.   float x1 = per.x - 1;
    336.   float y1 = per.y - 1;
    337.  
    338.   float x11 = PrivateDot(x1, y1, Grads[1, 1]);
    339.   float x01 = PrivateDot(per.x, y1, Grads[0,  1]);
    340.   float x10 = PrivateDot(x1, per.y, Grads[1, 0]);
    341.   float x00 = PrivateDot(per.x, per.y, Grads[0, 0]);
    342.  
    343.   float xx0 = gm.LerpDel(x00, x01, per.y);
    344.   float xx1 = gm.LerpDel(x10, x11, per.y);
    345.  
    346.  
    347.  
    348.   return gm.LerpDel(xx0, xx1, per.x);
    349.   }
    350.  
    351.   private float PrivateDot(float x, float y, fVec2 vec)
    352.   {
    353.   return x * vec.x + y * vec.y;
    354.   }
    355.   }
    356.  
    357.   public class Perlin2dHash
    358.   {
    359.   public int MaxBuffer = 1000;
    360.   private object AddRemoveLock = new object();
    361.   private Queue<PerlinCoord> AddOrder = new Queue<PerlinCoord>();
    362.   private Dictionary<PerlinCoord, Perlin2dSquare> Squares = new Dictionary<PerlinCoord, Perlin2dSquare>();
    363.   private GradientManager GM;
    364.  
    365.   public Perlin2dHash(GradientManager gm)
    366.   {
    367.   GM = gm;
    368.   }
    369.  
    370.   public void Sample(PerlinVector vec)
    371.   {
    372.   if(!Squares.ContainsKey(vec))
    373.   {
    374.   CreateSquare((PerlinCoord)vec);
    375.   }
    376.   vec.AddVal(Squares[(PerlinCoord)vec].Sample(vec.Float));
    377.   }
    378.  
    379.   private void CreateSquare(PerlinCoord pc)
    380.   {
    381.   lock(AddRemoveLock)
    382.   {
    383.   GM.x = pc.x;
    384.   GM.y = pc.y;
    385.   PerlinCoord copy = new PerlinCoord(pc);
    386.  
    387.   Perlin2dSquare p2d = new Perlin2dSquare(GM, copy);
    388.   AddOrder.Enqueue(copy);
    389.   Squares.Add(copy, p2d);
    390.  
    391.   while(AddOrder.Count > MaxBuffer)
    392.   {
    393.   Squares.Remove(AddOrder.Dequeue());
    394.   }
    395.   }
    396.   }
    397.  
    398.   }
    399.  
    400.   public struct fVec2
    401.   {
    402.   #region CircleGrad
    403.  
    404.   public static fVec2[] CircleGrad = { new float[]{ 0f, 1f }, new float[]{ -0.01227154f, 0.9999247f }, new float[]{ -0.02454123f, 0.9996988f }, new float[]{ -0.03680722f, 0.9993224f }, new float[]{ -0.04906768f, 0.9987954f }, new float[]{ -0.06132074f, 0.9981181f }, new float[]{ -0.07356457f, 0.9972904f }, new float[]{ -0.08579732f, 0.9963126f }, new float[]{ -0.09801714f, 0.9951847f }, new float[]{ -0.1102222f, 0.993907f }, new float[]{ -0.1224107f, 0.9924796f }, new float[]{ -0.1345807f, 0.9909027f }, new float[]{ -0.1467305f, 0.9891765f }, new float[]{ -0.1588582f, 0.9873014f }, new float[]{ -0.1709619f, 0.9852777f }, new float[]{ -0.1830399f, 0.9831055f }, new float[]{ -0.1950903f, 0.9807853f }, new float[]{ -0.2071114f, 0.9783174f }, new float[]{ -0.2191012f, 0.9757021f }, new float[]{ -0.2310581f, 0.97294f }, new float[]{ -0.2429802f, 0.9700313f }, new float[]{ -0.2548657f, 0.9669765f }, new float[]{ -0.2667128f, 0.9637761f }, new float[]{ -0.2785197f, 0.9604305f }, new float[]{ -0.2902847f, 0.9569404f }, new float[]{ -0.3020059f, 0.953306f }, new float[]{ -0.3136818f, 0.9495282f }, new float[]{ -0.3253103f, 0.9456073f }, new float[]{ -0.3368899f, 0.9415441f }, new float[]{ -0.3484187f, 0.937339f }, new float[]{ -0.3598951f, 0.9329928f }, new float[]{ -0.3713172f, 0.9285061f }, new float[]{ -0.3826835f, 0.9238795f }, new float[]{ -0.3939921f, 0.9191139f }, new float[]{ -0.4052413f, 0.9142097f }, new float[]{ -0.4164296f, 0.9091679f }, new float[]{ -0.4275551f, 0.9039893f }, new float[]{ -0.4386162f, 0.8986745f }, new float[]{ -0.4496113f, 0.8932243f }, new float[]{ -0.4605387f, 0.8876396f }, new float[]{ -0.4713967f, 0.8819212f }, new float[]{ -0.4821838f, 0.8760701f }, new float[]{ -0.4928982f, 0.870087f }, new float[]{ -0.5035384f, 0.8639728f }, new float[]{ -0.5141028f, 0.8577286f }, new float[]{ -0.5245897f, 0.8513552f }, new float[]{ -0.5349976f, 0.8448536f }, new float[]{ -0.545325f, 0.8382247f }, new float[]{ -0.5555702f, 0.8314696f }, new float[]{ -0.5657318f, 0.8245893f },
    405.                      new float[]{ -0.5758082f, 0.8175848f }, new float[]{ -0.5857979f, 0.8104572f }, new float[]{ -0.5956993f, 0.8032075f }, new float[]{ -0.605511f, 0.7958369f }, new float[]{ -0.6152316f, 0.7883464f }, new float[]{ -0.6248595f, 0.7807372f }, new float[]{ -0.6343933f, 0.7730104f }, new float[]{ -0.6438316f, 0.7651672f }, new float[]{ -0.6531729f, 0.7572088f }, new float[]{ -0.6624158f, 0.7491364f }, new float[]{ -0.671559f, 0.7409511f }, new float[]{ -0.680601f, 0.7326543f }, new float[]{ -0.6895406f, 0.7242471f }, new float[]{ -0.6983763f, 0.7157308f }, new float[]{ -0.7071068f, 0.7071068f }, new float[]{ -0.7157308f, 0.6983762f }, new float[]{ -0.7242471f, 0.6895405f }, new float[]{ -0.7326543f, 0.680601f }, new float[]{ -0.7409512f, 0.6715589f }, new float[]{ -0.7491364f, 0.6624157f }, new float[]{ -0.7572089f, 0.6531728f }, new float[]{ -0.7651673f, 0.6438316f }, new float[]{ -0.7730104f, 0.6343933f }, new float[]{ -0.7807373f, 0.6248595f }, new float[]{ -0.7883464f, 0.6152316f }, new float[]{ -0.7958369f, 0.605511f }, new float[]{ -0.8032075f, 0.5956993f }, new float[]{ -0.8104572f, 0.5857978f }, new float[]{ -0.8175848f, 0.5758082f }, new float[]{ -0.8245893f, 0.5657318f }, new float[]{ -0.8314697f, 0.5555702f }, new float[]{ -0.8382247f, 0.545325f }, new float[]{ -0.8448536f, 0.5349976f }, new float[]{ -0.8513552f, 0.5245897f }, new float[]{ -0.8577287f, 0.5141027f }, new float[]{ -0.8639728f, 0.5035384f }, new float[]{ -0.870087f, 0.4928982f }, new float[]{ -0.8760701f, 0.4821837f }, new float[]{ -0.8819213f, 0.4713967f }, new float[]{ -0.8876396f, 0.4605387f }, new float[]{ -0.8932243f, 0.4496113f }, new float[]{ -0.8986745f, 0.4386162f }, new float[]{ -0.9039893f, 0.4275551f }, new float[]{ -0.909168f, 0.4164295f }, new float[]{ -0.9142098f, 0.4052413f }, new float[]{ -0.9191139f, 0.3939919f }, new float[]{ -0.9238795f, 0.3826834f }, new float[]{ -0.9285061f, 0.3713171f }, new float[]{ -0.9329928f, 0.359895f }, new float[]{ -0.937339f, 0.3484187f },
    406.                      new float[]{ -0.9415441f, 0.3368898f }, new float[]{ -0.9456074f, 0.3253102f }, new float[]{ -0.9495282f, 0.3136817f }, new float[]{ -0.953306f, 0.3020059f }, new float[]{ -0.9569404f, 0.2902846f }, new float[]{ -0.9604306f, 0.2785196f }, new float[]{ -0.9637761f, 0.2667128f }, new float[]{ -0.9669765f, 0.2548656f }, new float[]{ -0.9700313f, 0.2429801f }, new float[]{ -0.97294f, 0.2310581f }, new float[]{ -0.9757021f, 0.2191012f }, new float[]{ -0.9783174f, 0.2071113f }, new float[]{ -0.9807853f, 0.1950902f }, new float[]{ -0.9831055f, 0.1830399f }, new float[]{ -0.9852777f, 0.1709619f }, new float[]{ -0.9873014f, 0.1588581f }, new float[]{ -0.9891765f, 0.1467305f }, new float[]{ -0.9909027f, 0.1345807f }, new float[]{ -0.9924796f, 0.1224106f }, new float[]{ -0.993907f, 0.1102221f }, new float[]{ -0.9951847f, 0.09801713f }, new float[]{ -0.9963126f, 0.08579727f }, new float[]{ -0.9972904f, 0.07356449f }, new float[]{ -0.9981181f, 0.06132075f }, new float[]{ -0.9987954f, 0.04906765f }, new float[]{ -0.9993224f, 0.03680716f }, new float[]{ -0.9996988f, 0.02454114f }, new float[]{ -0.9999247f, 0.01227153f }, new float[]{ -1f, -4.371139E-08f }, new float[]{ -0.9999247f, -0.01227162f }, new float[]{ -0.9996988f, -0.02454122f }, new float[]{ -0.9993224f, -0.03680725f }, new float[]{ -0.9987954f, -0.04906774f }, new float[]{ -0.9981181f, -0.06132083f }, new float[]{ -0.9972904f, -0.07356457f }, new float[]{ -0.9963126f, -0.08579736f }, new float[]{ -0.9951847f, -0.09801722f }, new float[]{ -0.993907f, -0.1102222f }, new float[]{ -0.9924795f, -0.1224107f }, new float[]{ -0.9909026f, -0.1345808f }, new float[]{ -0.9891765f, -0.1467306f }, new float[]{ -0.9873014f, -0.1588582f }, new float[]{ -0.9852777f, -0.1709619f }, new float[]{ -0.9831055f, -0.18304f }, new float[]{ -0.9807853f, -0.1950903f }, new float[]{ -0.9783174f, -0.2071114f }, new float[]{ -0.9757021f, -0.2191013f }, new float[]{ -0.9729399f, -0.2310582f }, new float[]{ -0.9700313f, -0.2429802f }, new float[]{ -0.9669765f, -0.2548657f },
    407.                      new float[]{ -0.9637761f, -0.2667128f }, new float[]{ -0.9604305f, -0.2785197f }, new float[]{ -0.9569403f, -0.2902847f }, new float[]{ -0.953306f, -0.302006f }, new float[]{ -0.9495282f, -0.3136817f }, new float[]{ -0.9456073f, -0.3253103f }, new float[]{ -0.9415441f, -0.3368899f }, new float[]{ -0.9373389f, -0.3484188f }, new float[]{ -0.9329928f, -0.3598951f }, new float[]{ -0.9285061f, -0.3713172f }, new float[]{ -0.9238795f, -0.3826835f }, new float[]{ -0.9191139f, -0.393992f }, new float[]{ -0.9142097f, -0.4052413f }, new float[]{ -0.909168f, -0.4164295f }, new float[]{ -0.9039893f, -0.4275551f }, new float[]{ -0.8986744f, -0.4386162f }, new float[]{ -0.8932243f, -0.4496114f }, new float[]{ -0.8876396f, -0.4605388f }, new float[]{ -0.8819212f, -0.4713968f }, new float[]{ -0.87607f, -0.4821839f }, new float[]{ -0.870087f, -0.4928982f }, new float[]{ -0.8639728f, -0.5035384f }, new float[]{ -0.8577286f, -0.5141028f }, new float[]{ -0.8513551f, -0.5245897f }, new float[]{ -0.8448535f, -0.5349977f }, new float[]{ -0.8382246f, -0.5453251f }, new float[]{ -0.8314695f, -0.5555704f }, new float[]{ -0.8245893f, -0.5657318f }, new float[]{ -0.8175848f, -0.5758082f }, new float[]{ -0.8104572f, -0.5857979f }, new float[]{ -0.8032075f, -0.5956994f }, new float[]{ -0.7958369f, -0.6055111f }, new float[]{ -0.7883464f, -0.6152317f }, new float[]{ -0.7807371f, -0.6248596f }, new float[]{ -0.7730105f, -0.6343933f }, new float[]{ -0.7651672f, -0.6438316f }, new float[]{ -0.7572088f, -0.6531729f }, new float[]{ -0.7491363f, -0.6624158f }, new float[]{ -0.7409511f, -0.671559f }, new float[]{ -0.7326542f, -0.6806011f }, new float[]{ -0.724247f, -0.6895407f }, new float[]{ -0.7157308f, -0.6983762f }, new float[]{ -0.7071068f, -0.7071068f }, new float[]{ -0.6983762f, -0.7157308f }, new float[]{ -0.6895405f, -0.7242472f }, new float[]{ -0.6806009f, -0.7326543f }, new float[]{ -0.6715589f, -0.7409512f }, new float[]{ -0.6624156f, -0.7491365f }, new float[]{ -0.6531729f, -0.7572088f }, new float[]{ -0.6438316f, -0.7651673f },
    408.                      new float[]{ -0.6343933f, -0.7730105f }, new float[]{ -0.6248595f, -0.7807373f }, new float[]{ -0.6152315f, -0.7883465f }, new float[]{ -0.605511f, -0.795837f }, new float[]{ -0.5956991f, -0.8032076f }, new float[]{ -0.5857978f, -0.8104572f }, new float[]{ -0.5758082f, -0.8175848f }, new float[]{ -0.5657318f, -0.8245893f }, new float[]{ -0.5555702f, -0.8314697f }, new float[]{ -0.5453249f, -0.8382248f }, new float[]{ -0.5349975f, -0.8448536f }, new float[]{ -0.5245895f, -0.8513553f }, new float[]{ -0.5141028f, -0.8577286f }, new float[]{ -0.5035384f, -0.8639728f }, new float[]{ -0.4928981f, -0.870087f }, new float[]{ -0.4821837f, -0.8760701f }, new float[]{ -0.4713966f, -0.8819214f }, new float[]{ -0.4605386f, -0.8876397f }, new float[]{ -0.4496114f, -0.8932243f }, new float[]{ -0.4386162f, -0.8986745f }, new float[]{ -0.4275551f, -0.9039893f }, new float[]{ -0.4164295f, -0.909168f }, new float[]{ -0.4052412f, -0.9142098f }, new float[]{ -0.3939919f, -0.9191139f }, new float[]{ -0.3826833f, -0.9238796f }, new float[]{ -0.3713172f, -0.9285061f }, new float[]{ -0.3598951f, -0.9329928f }, new float[]{ -0.3484187f, -0.937339f }, new float[]{ -0.3368898f, -0.9415441f }, new float[]{ -0.3253102f, -0.9456074f }, new float[]{ -0.3136816f, -0.9495282f }, new float[]{ -0.3020058f, -0.9533061f }, new float[]{ -0.2902847f, -0.9569404f }, new float[]{ -0.2785197f, -0.9604305f }, new float[]{ -0.2667127f, -0.9637761f }, new float[]{ -0.2548656f, -0.9669765f }, new float[]{ -0.2429801f, -0.9700313f }, new float[]{ -0.231058f, -0.97294f }, new float[]{ -0.2191011f, -0.9757022f }, new float[]{ -0.2071114f, -0.9783174f }, new float[]{ -0.1950903f, -0.9807853f }, new float[]{ -0.1830398f, -0.9831055f }, new float[]{ -0.1709618f, -0.9852777f }, new float[]{ -0.158858f, -0.9873014f }, new float[]{ -0.1467303f, -0.9891765f }, new float[]{ -0.1345805f, -0.9909027f }, new float[]{ -0.1224107f, -0.9924795f }, new float[]{ -0.1102222f, -0.993907f }, new float[]{ -0.0980171f, -0.9951847f }, new float[]{ -0.08579723f, -0.9963126f },
    409.                      new float[]{ -0.07356445f, -0.9972905f }, new float[]{ -0.06132058f, -0.9981181f }, new float[]{ -0.04906749f, -0.9987954f }, new float[]{ -0.03680724f, -0.9993224f }, new float[]{ -0.02454121f, -0.9996988f }, new float[]{ -0.01227149f, -0.9999247f }, new float[]{ 8.742278E-08f, -1f }, new float[]{ 0.01227166f, -0.9999247f }, new float[]{ 0.02454139f, -0.9996988f }, new float[]{ 0.03680741f, -0.9993224f }, new float[]{ 0.04906766f, -0.9987954f }, new float[]{ 0.06132076f, -0.9981181f }, new float[]{ 0.07356462f, -0.9972904f }, new float[]{ 0.08579741f, -0.9963126f }, new float[]{ 0.09801727f, -0.9951847f }, new float[]{ 0.1102224f, -0.993907f }, new float[]{ 0.1224109f, -0.9924795f }, new float[]{ 0.1345807f, -0.9909027f }, new float[]{ 0.1467305f, -0.9891765f }, new float[]{ 0.1588582f, -0.9873014f }, new float[]{ 0.170962f, -0.9852777f }, new float[]{ 0.18304f, -0.9831055f }, new float[]{ 0.1950905f, -0.9807853f }, new float[]{ 0.2071116f, -0.9783173f }, new float[]{ 0.2191012f, -0.9757021f }, new float[]{ 0.2310581f, -0.97294f }, new float[]{ 0.2429802f, -0.9700313f }, new float[]{ 0.2548658f, -0.9669765f }, new float[]{ 0.2667129f, -0.9637761f }, new float[]{ 0.2785199f, -0.9604304f }, new float[]{ 0.2902849f, -0.9569403f }, new float[]{ 0.3020059f, -0.953306f }, new float[]{ 0.3136818f, -0.9495282f }, new float[]{ 0.3253103f, -0.9456073f }, new float[]{ 0.33689f, -0.9415441f }, new float[]{ 0.3484188f, -0.9373389f }, new float[]{ 0.3598952f, -0.9329928f }, new float[]{ 0.3713174f, -0.928506f }, new float[]{ 0.3826834f, -0.9238795f }, new float[]{ 0.3939921f, -0.9191138f }, new float[]{ 0.4052414f, -0.9142097f }, new float[]{ 0.4164297f, -0.9091679f }, new float[]{ 0.4275552f, -0.9039893f }, new float[]{ 0.4386164f, -0.8986744f }, new float[]{ 0.4496115f, -0.8932242f }, new float[]{ 0.4605387f, -0.8876396f }, new float[]{ 0.4713968f, -0.8819212f }, new float[]{ 0.4821838f, -0.8760701f }, new float[]{ 0.4928983f, -0.8700869f }, new float[]{ 0.5035385f, -0.8639728f },
    410.                      new float[]{ 0.5141029f, -0.8577285f }, new float[]{ 0.5245897f, -0.8513552f }, new float[]{ 0.5349976f, -0.8448536f }, new float[]{ 0.545325f, -0.8382246f }, new float[]{ 0.5555703f, -0.8314695f }, new float[]{ 0.5657319f, -0.8245893f }, new float[]{ 0.5758083f, -0.8175847f }, new float[]{ 0.585798f, -0.8104571f }, new float[]{ 0.5956993f, -0.8032075f }, new float[]{ 0.6055111f, -0.7958369f }, new float[]{ 0.6152316f, -0.7883464f }, new float[]{ 0.6248596f, -0.7807372f }, new float[]{ 0.6343934f, -0.7730104f }, new float[]{ 0.6438317f, -0.7651672f }, new float[]{ 0.653173f, -0.7572087f }, new float[]{ 0.6624158f, -0.7491364f }, new float[]{ 0.671559f, -0.7409511f }, new float[]{ 0.6806011f, -0.7326542f }, new float[]{ 0.6895406f, -0.724247f }, new float[]{ 0.6983764f, -0.7157307f }, new float[]{ 0.7071069f, -0.7071066f }, new float[]{ 0.715731f, -0.6983761f }, new float[]{ 0.7242471f, -0.6895406f }, new float[]{ 0.7326543f, -0.680601f }, new float[]{ 0.7409512f, -0.6715589f }, new float[]{ 0.7491364f, -0.6624157f }, new float[]{ 0.7572088f, -0.6531729f }, new float[]{ 0.7651672f, -0.6438316f }, new float[]{ 0.7730104f, -0.6343933f }, new float[]{ 0.7807372f, -0.6248595f }, new float[]{ 0.7883465f, -0.6152316f }, new float[]{ 0.7958369f, -0.605511f }, new float[]{ 0.8032076f, -0.5956992f }, new float[]{ 0.8104573f, -0.5857977f }, new float[]{ 0.8175849f, -0.575808f }, new float[]{ 0.8245894f, -0.5657316f }, new float[]{ 0.8314698f, -0.55557f }, new float[]{ 0.8382249f, -0.5453247f }, new float[]{ 0.8448538f, -0.5349973f }, new float[]{ 0.8513554f, -0.5245894f }, new float[]{ 0.8577285f, -0.5141028f }, new float[]{ 0.8639728f, -0.5035384f }, new float[]{ 0.870087f, -0.4928982f }, new float[]{ 0.8760701f, -0.4821838f }, new float[]{ 0.8819213f, -0.4713967f }, new float[]{ 0.8876396f, -0.4605386f }, new float[]{ 0.8932244f, -0.4496112f }, new float[]{ 0.8986745f, -0.4386161f }, new float[]{ 0.9039894f, -0.4275549f }, new float[]{ 0.9091681f, -0.4164294f },
    411.                      new float[]{ 0.9142098f, -0.4052411f }, new float[]{ 0.919114f, -0.3939918f }, new float[]{ 0.9238797f, -0.3826831f }, new float[]{ 0.928506f, -0.3713173f }, new float[]{ 0.9329928f, -0.3598951f }, new float[]{ 0.937339f, -0.3484187f }, new float[]{ 0.9415441f, -0.3368899f }, new float[]{ 0.9456073f, -0.3253103f }, new float[]{ 0.9495282f, -0.3136817f }, new float[]{ 0.9533061f, -0.3020059f }, new float[]{ 0.9569404f, -0.2902845f }, new float[]{ 0.9604306f, -0.2785195f }, new float[]{ 0.9637761f, -0.2667126f }, new float[]{ 0.9669765f, -0.2548654f }, new float[]{ 0.9700313f, -0.2429799f }, new float[]{ 0.97294f, -0.2310578f }, new float[]{ 0.9757022f, -0.2191009f }, new float[]{ 0.9783173f, -0.2071115f }, new float[]{ 0.9807853f, -0.1950904f }, new float[]{ 0.9831055f, -0.1830399f }, new float[]{ 0.9852777f, -0.1709619f }, new float[]{ 0.9873014f, -0.1588581f }, new float[]{ 0.9891765f, -0.1467304f }, new float[]{ 0.9909027f, -0.1345806f }, new float[]{ 0.9924796f, -0.1224105f }, new float[]{ 0.993907f, -0.110222f }, new float[]{ 0.9951847f, -0.09801693f }, new float[]{ 0.9963126f, -0.08579707f }, new float[]{ 0.9972905f, -0.07356428f }, new float[]{ 0.9981181f, -0.06132042f }, new float[]{ 0.9987954f, -0.04906733f }, new float[]{ 0.9993224f, -0.03680731f }, new float[]{ 0.9996988f, -0.02454129f }, new float[]{ 0.9999247f, -0.01227156f }, new float[]{ 1f, 1.192488E-08f }, new float[]{ 0.9999247f, 0.01227158f }, new float[]{ 0.9996988f, 0.02454131f }, new float[]{ 0.9993224f, 0.03680734f }, new float[]{ 0.9987954f, 0.04906783f }, new float[]{ 0.9981181f, 0.06132092f }, new float[]{ 0.9972904f, 0.07356478f }, new float[]{ 0.9963126f, 0.08579757f }, new float[]{ 0.9951847f, 0.09801743f }, new float[]{ 0.9939069f, 0.1102225f }, new float[]{ 0.9924795f, 0.122411f }, new float[]{ 0.9909027f, 0.1345806f }, new float[]{ 0.9891765f, 0.1467304f }, new float[]{ 0.9873014f, 0.1588581f }, new float[]{ 0.9852777f, 0.1709619f }, new float[]{ 0.9831055f, 0.1830399f },
    412.                      new float[]{ 0.9807853f, 0.1950904f }, new float[]{ 0.9783173f, 0.2071115f }, new float[]{ 0.9757021f, 0.2191014f }, new float[]{ 0.9729399f, 0.2310583f }, new float[]{ 0.9700312f, 0.2429804f }, new float[]{ 0.9669764f, 0.2548659f }, new float[]{ 0.963776f, 0.2667131f }, new float[]{ 0.9604304f, 0.27852f }, new float[]{ 0.9569402f, 0.290285f }, new float[]{ 0.9533061f, 0.3020059f }, new float[]{ 0.9495282f, 0.3136817f }, new float[]{ 0.9456073f, 0.3253103f }, new float[]{ 0.9415441f, 0.3368899f }, new float[]{ 0.937339f, 0.3484187f }, new float[]{ 0.9329928f, 0.3598951f }, new float[]{ 0.928506f, 0.3713173f }, new float[]{ 0.9238794f, 0.3826836f }, new float[]{ 0.9191138f, 0.3939922f }, new float[]{ 0.9142097f, 0.4052415f }, new float[]{ 0.9091679f, 0.4164298f }, new float[]{ 0.9039891f, 0.4275554f }, new float[]{ 0.8986743f, 0.4386165f }, new float[]{ 0.8932241f, 0.4496117f }, new float[]{ 0.8876396f, 0.4605387f }, new float[]{ 0.8819213f, 0.4713967f }, new float[]{ 0.8760701f, 0.4821838f }, new float[]{ 0.870087f, 0.4928982f }, new float[]{ 0.8639728f, 0.5035384f }, new float[]{ 0.8577285f, 0.5141028f }, new float[]{ 0.8513551f, 0.5245898f }, new float[]{ 0.8448535f, 0.5349978f }, new float[]{ 0.8382246f, 0.5453252f }, new float[]{ 0.8314695f, 0.5555704f }, new float[]{ 0.8245891f, 0.5657321f }, new float[]{ 0.8175846f, 0.5758085f }, new float[]{ 0.810457f, 0.5857981f }, new float[]{ 0.8032076f, 0.5956993f }, new float[]{ 0.7958369f, 0.605511f }, new float[]{ 0.7883464f, 0.6152316f }, new float[]{ 0.7807372f, 0.6248595f }, new float[]{ 0.7730104f, 0.6343933f }, new float[]{ 0.7651672f, 0.6438316f }, new float[]{ 0.7572088f, 0.6531729f }, new float[]{ 0.7491363f, 0.6624159f }, new float[]{ 0.740951f, 0.6715591f }, new float[]{ 0.7326541f, 0.6806012f }, new float[]{ 0.7242469f, 0.6895407f }, new float[]{ 0.7157306f, 0.6983765f }, new float[]{ 0.7071065f, 0.707107f }, new float[]{ 0.698376f, 0.7157311f },
    413.                      new float[]{ 0.6895406f, 0.724247f }, new float[]{ 0.680601f, 0.7326543f }, new float[]{ 0.671559f, 0.7409511f }, new float[]{ 0.6624157f, 0.7491364f }, new float[]{ 0.6531728f, 0.7572089f }, new float[]{ 0.6438315f, 0.7651674f }, new float[]{ 0.6343932f, 0.7730106f }, new float[]{ 0.6248593f, 0.7807373f }, new float[]{ 0.6152315f, 0.7883465f }, new float[]{ 0.6055108f, 0.795837f }, new float[]{ 0.5956991f, 0.8032077f }, new float[]{ 0.5857976f, 0.8104573f }, new float[]{ 0.5758079f, 0.817585f }, new float[]{ 0.5657315f, 0.8245895f }, new float[]{ 0.5555703f, 0.8314696f }, new float[]{ 0.545325f, 0.8382247f }, new float[]{ 0.5349976f, 0.8448536f }, new float[]{ 0.5245897f, 0.8513552f }, new float[]{ 0.5141027f, 0.8577287f }, new float[]{ 0.5035383f, 0.8639729f }, new float[]{ 0.4928981f, 0.8700871f }, new float[]{ 0.4821836f, 0.8760702f }, new float[]{ 0.4713965f, 0.8819214f }, new float[]{ 0.4605385f, 0.8876398f }, new float[]{ 0.4496111f, 0.8932244f }, new float[]{ 0.4386159f, 0.8986746f }, new float[]{ 0.4275548f, 0.9039894f }, new float[]{ 0.4164292f, 0.9091681f }, new float[]{ 0.4052414f, 0.9142097f }, new float[]{ 0.3939921f, 0.9191138f }, new float[]{ 0.3826834f, 0.9238796f }, new float[]{ 0.3713171f, 0.9285061f }, new float[]{ 0.359895f, 0.9329928f }, new float[]{ 0.3484186f, 0.9373391f }, new float[]{ 0.3368897f, 0.9415441f }, new float[]{ 0.3253101f, 0.9456074f }, new float[]{ 0.3136815f, 0.9495283f }, new float[]{ 0.3020057f, 0.9533061f }, new float[]{ 0.2902844f, 0.9569404f }, new float[]{ 0.2785194f, 0.9604306f }, new float[]{ 0.2667124f, 0.9637762f }, new float[]{ 0.2548653f, 0.9669766f }, new float[]{ 0.2429802f, 0.9700313f }, new float[]{ 0.2310581f, 0.97294f }, new float[]{ 0.2191012f, 0.9757021f }, new float[]{ 0.2071113f, 0.9783174f }, new float[]{ 0.1950902f, 0.9807853f }, new float[]{ 0.1830398f, 0.9831055f }, new float[]{ 0.1709617f, 0.9852777f }, new float[]{ 0.1588579f, 0.9873015f },
    414.                      new float[]{ 0.1467302f, 0.9891766f }, new float[]{ 0.1345804f, 0.9909027f }, new float[]{ 0.1224104f, 0.9924796f }, new float[]{ 0.1102219f, 0.993907f }, new float[]{ 0.09801677f, 0.9951848f }, new float[]{ 0.08579691f, 0.9963126f }, new float[]{ 0.0735646f, 0.9972904f }, new float[]{ 0.06132074f, 0.9981181f }, new float[]{ 0.04906764f, 0.9987954f }, new float[]{ 0.03680715f, 0.9993224f }, new float[]{ 0.02454112f, 0.9996988f }, new float[]{ 0.0122714f, 0.9999247f }  };
    415.  
    416.   #endregion
    417.  
    418.   private float[] Floats;
    419.  
    420.   public float x
    421.   {
    422.   get
    423.   {
    424.   return Floats[0];
    425.   }
    426.   set
    427.   {
    428.   Floats[0] = value;
    429.   }
    430.   }
    431.   public float y
    432.   {
    433.   get
    434.   {
    435.   return Floats[1];
    436.   }
    437.   set
    438.   {
    439.   Floats[1] = value;
    440.   }
    441.   }
    442.   public float this[int a]
    443.   {
    444.   get
    445.   {
    446.   switch (a)
    447.   {
    448.   case 0:
    449.   return Floats[0];
    450.   case 1:
    451.   return Floats[1];
    452.   default:
    453.   throw new IndexOutOfRangeException("Attempted to access a nonexistent component of an fVec2");
    454.   }
    455.   }
    456.   set
    457.   {
    458.   switch (a)
    459.   {
    460.   case 0:
    461.   Floats[0] = value;
    462.   break;
    463.   case 1:
    464.   Floats[1] = value;
    465.   break;
    466.   default:
    467.   throw new IndexOutOfRangeException("Attempted to access a nonexistent component of an fVec2");
    468.   }
    469.   }
    470.   }
    471.   public fVec2 U
    472.   {
    473.   get
    474.   {
    475.   return GetUnit();
    476.   }
    477.   }
    478.   public float L
    479.   {
    480.   get
    481.   {
    482.   return (float)Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2));
    483.   }
    484.   set
    485.   {
    486.   this = value * U;
    487.   }
    488.   }
    489.  
    490.   #region Constructors
    491.  
    492.  
    493.  
    494.   public fVec2(float X, float Y)
    495.   {
    496.   Floats = new float[2];
    497.   x = X;
    498.   y = Y;
    499.   }
    500.  
    501.   #endregion
    502.  
    503.   public fVec2 RotateVec(float rads)
    504.   {
    505.   float COS = (float)Math.Cos(rads);
    506.   float SIN = (float)Math.Sin(rads);
    507.  
    508.   return new fVec2(x * COS - y * SIN, x * SIN + y * COS);
    509.   }
    510.  
    511.   public override string ToString()
    512.   {
    513.   return String.Format("{0}, {1} ", x, y);
    514.   }
    515.  
    516.   private fVec2 GetUnit()
    517.   {
    518.   float len = L;
    519.   return new fVec2(x / L, y / L);
    520.   }
    521.  
    522.   #region Overloads
    523.  
    524.   public static fVec2 operator +(float a, fVec2 b)
    525.   {
    526.   return new fVec2(a + b.x, a + b.y);
    527.   }
    528.  
    529.   public static fVec2 operator +(fVec2 a, float b)
    530.   {
    531.   return b + a;
    532.   }
    533.  
    534.   public static fVec2 operator +(fVec2 a, fVec2 b)
    535.   {
    536.   return new fVec2(a.x + b.x, a.y + b.y);
    537.   }
    538.  
    539.   public static fVec2 operator -(fVec2 a, float b)
    540.   {
    541.   return new fVec2(a.x - b, a.y - b);
    542.   }
    543.  
    544.   public static fVec2 operator -(fVec2 a, fVec2 b)
    545.   {
    546.   return new fVec2(a.x - b.x, a.y - b.y);
    547.   }
    548.  
    549.   public static fVec2 operator -(fVec2 a)
    550.   {
    551.   return new fVec2(-a.x, -a.y);
    552.   }
    553.  
    554.   public static fVec2 operator *(float a, fVec2 b)
    555.   {
    556.   return new fVec2(a * b.x, a * b.y);
    557.   }
    558.  
    559.   public static float operator *(fVec2 a, fVec2 b)
    560.   {
    561.   return a.x * b.x + a.y * b.y;
    562.   }
    563.  
    564.   public static implicit operator fVec2(float[] Vals)
    565.   {
    566.   return new fVec2(Vals[0], Vals[1]);
    567.   }
    568.  
    569.   public static implicit operator fVec2(Vector2 vec)
    570.   {
    571.   return new fVec2(vec.x, vec.y);
    572.   }
    573.  
    574.   public static implicit operator Vector2(fVec2 vec)
    575.   {
    576.   return new Vector2(vec.x, vec.y);
    577.   }
    578.  
    579.   public static implicit operator string(fVec2 vec)
    580.   {
    581.   StringBuilder sb = new StringBuilder();
    582.   sb.Append("< ");
    583.   sb.Append(vec.ToString());
    584.   sb.Append(" >");
    585.   return sb.ToString();
    586.   }
    587.  
    588.   #endregion
    589.   }
    590.  
    591.   public class RNG2_0
    592.   {
    593.   public static byte[] AllByte = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F, 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF, 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF, 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF, 0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF, 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF };
    594.  
    595.   private byte[] NewArray = new byte[256];
    596.   private byte[] SecondArray = new byte[256];
    597.   private Pointer[] Array = new Pointer[8];
    598.   private MemoryStream ms;
    599.   private BinaryReader br;
    600.  
    601.   private byte[] CurKey = new byte[8];
    602.  
    603.   public class Pointer
    604.   {
    605.   public byte Loc = 0;
    606.  
    607.   private byte[] arr;
    608.   private int Sel = 0;
    609.  
    610.   public Pointer(byte[] Array, byte Seed, int Behavior)
    611.   {
    612.   Loc = Seed;
    613.   Sel = Behavior;
    614.   arr = Array;
    615.  
    616.  
    617.   }
    618.  
    619.   public byte Next(byte val)
    620.   {
    621.   switch (Sel)
    622.   {
    623.   case 0:
    624.   Loc++;
    625.   break;
    626.   case 1:
    627.   Loc--;
    628.   break;
    629.   case 2:
    630.   Loc += val;
    631.   break;
    632.   case 3:
    633.   Loc -= val;
    634.   break;
    635.   case 4:
    636.   Loc = (byte)(Loc ^ val);
    637.   break;
    638.   case 5:
    639.   Loc = (byte)((Loc + val) ^ val);
    640.   break;
    641.   case 6:
    642.   Loc = (byte)((Loc >> 1) ^ val);
    643.   break;
    644.   case 7:
    645.   Loc += (byte)(Loc ^ (val >> 1));
    646.   break;
    647.   default:
    648.   break;
    649.   }
    650.  
    651.   //stats[Loc]++;
    652.  
    653.   return (byte)(arr[Loc] ^ val);
    654.   }
    655.   }
    656.  
    657.   public RNG2_0()
    658.   {
    659.   long val = DateTime.UtcNow.Ticks;
    660.   byte[] Seed = new byte[] { (byte)(val >> 56), (byte)(val >> 48), (byte)(val >> 40), (byte)(val >> 32), (byte)(val >> 24), (byte)(val >> 16), (byte)(val >> 8), (byte)val };
    661.  
    662.   for (int i = 0; i < Seed.Length; i++)
    663.   {
    664.   if (i == 0)
    665.   {
    666.   for (int j = 0; j < 256; j++)
    667.   {
    668.   NewArray[j] = AllByte[(Seed[0] ^ j)];
    669.   }
    670.   }
    671.   else if (i % 2 == 1)
    672.   {
    673.   //AppConsole.AppConsole.Instance.WriteLine("Second Array!");
    674.   for (int j = 0; j < 256; j++)
    675.   {
    676.   SecondArray[j] = (byte)(NewArray[(Seed[i] ^ j)] ^ Seed[i]);
    677.   }
    678.   }
    679.   else
    680.   {
    681.   for (int j = 0; j < 256; j++)
    682.   {
    683.   NewArray[j] = (byte)((SecondArray[(Seed[i] ^ j)] + Seed[i]));
    684.   }
    685.   //AppConsole.AppConsole.Instance.WriteLine("First Array!");
    686.   }
    687.   }
    688.   if (Seed.Length % 2 == 0)
    689.   {
    690.   //AppConsole.AppConsole.Instance.WriteLine("Switching!");
    691.   NewArray = SecondArray;
    692.   }
    693.  
    694.   FillPointerArr(Seed);
    695.   InitalizeMS();
    696.   InitializeKey(Seed);
    697.  
    698.   }
    699.  
    700.   public RNG2_0(string StrSeed)
    701.   {
    702.   byte[] Seed = StringtoByteArrayU8(StrSeed);
    703.  
    704.   for (int i = 0; i < Seed.Length; i++)
    705.   {
    706.   if (i == 0)
    707.   {
    708.   for (int j = 0; j < 256; j++)
    709.   {
    710.   NewArray[j] = AllByte[(Seed[0] ^ j)];
    711.   }
    712.   }
    713.   else if (i % 2 == 1)
    714.   {
    715.   //AppConsole.AppConsole.Instance.WriteLine("Second Array!");
    716.   for (int j = 0; j < 256; j++)
    717.   {
    718.   SecondArray[j] = (byte)(NewArray[(Seed[i] ^ j)] ^ Seed[i]);
    719.   }
    720.   }
    721.   else
    722.   {
    723.   for (int j = 0; j < 256; j++)
    724.   {
    725.   NewArray[j] = (byte)((SecondArray[(Seed[i] ^ j)] + Seed[i]));
    726.   }
    727.   //AppConsole.AppConsole.Instance.WriteLine("First Array!");
    728.   }
    729.   }
    730.   if (Seed.Length % 2 == 0)
    731.   {
    732.   //AppConsole.AppConsole.Instance.WriteLine("Switching!");
    733.   NewArray = SecondArray;
    734.   }
    735.  
    736.   FillPointerArr(Seed);
    737.   InitalizeMS();
    738.   InitializeKey(Seed);
    739.   }
    740.  
    741.   public RNG2_0(byte[] Seed)
    742.   {
    743.   for (int i = 0; i < Seed.Length; i++)
    744.   {
    745.   if (i == 0)
    746.   {
    747.   for (int j = 0; j < 256; j++)
    748.   {
    749.   NewArray[j] = AllByte[(Seed[0] ^ j)];
    750.   }
    751.   }
    752.   else if (i % 2 == 1)
    753.   {
    754.   //AppConsole.AppConsole.Instance.WriteLine("Second Array!");
    755.   for (int j = 0; j < 256; j++)
    756.   {
    757.   SecondArray[j] = (byte)(NewArray[(Seed[i] ^ j)] ^ Seed[i]);
    758.   }
    759.   }
    760.   else
    761.   {
    762.   for (int j = 0; j < 256; j++)
    763.   {
    764.   NewArray[j] = (byte)((SecondArray[(Seed[i] ^ j)] + Seed[i]));
    765.   }
    766.   //AppConsole.AppConsole.Instance.WriteLine("First Array!");
    767.   }
    768.   }
    769.   if (Seed.Length % 2 == 0)
    770.   {
    771.   //AppConsole.AppConsole.Instance.WriteLine("Switching!");
    772.   NewArray = SecondArray;
    773.   }
    774.  
    775.   FillPointerArr(Seed);
    776.   InitalizeMS();
    777.   InitializeKey(Seed);
    778.   }
    779.  
    780.   private static byte[] StringtoByteArrayU8(string tempstr)
    781.   {
    782.   int length = tempstr.Length;
    783.   byte[] returnArray = new byte[length];
    784.   for (int i = 0; i < length; i++)
    785.   {
    786.   byte a = (byte)((uint)tempstr[i]);
    787.   returnArray[i] = a;
    788.   }
    789.  
    790.   return returnArray;
    791.   }
    792.  
    793.   private void FillPointerArr(byte[] Seed)
    794.   {
    795.   for (int i = 0; i < 8; i++)
    796.   {
    797.   Array[i] = new Pointer(NewArray, Seed[i % Seed.Length], i);
    798.   }
    799.   }
    800.  
    801.   private void InitializeKey(byte[] Seed)
    802.   {
    803.   for (int i = 0; i < 8; i++)
    804.   {
    805.   CurKey[i] = Seed[i % Seed.Length];
    806.   }
    807.   NextKey();
    808.   }
    809.  
    810.   private void InitalizeMS()
    811.   {
    812.   ms = new MemoryStream(CurKey);
    813.   br = new BinaryReader(ms);
    814.   }
    815.  
    816.   private void NextKey()
    817.   {
    818.   for (int i = 0; i < 8; i++)
    819.   {
    820.   for (int j = 0; j < 8; j++)
    821.   {
    822.   CurKey[i] = (byte)(Array[j].Next(CurKey[i]));
    823.   }
    824.   }
    825.   ms.Seek(0, SeekOrigin.Begin);
    826.   //AppConsole.AppConsole.Instance.WriteLine(DPMethods.ReadByteArray(CurKey));
    827.   }
    828.  
    829.   public float NextFloat(float min, float max)
    830.   {
    831.   if (min == 0)
    832.   {
    833.   return (float)(NextRatio() * max);
    834.   }
    835.   else
    836.   {
    837.   float range = max - min;
    838.   return (float)(min + range * NextRatio());
    839.   }
    840.   }
    841.  
    842.   public int NextInt(int min, int max)
    843.   {
    844.   if (min == 0)
    845.   {
    846.   return (int)(NextRatio() * (double)max);
    847.   }
    848.   else
    849.   {
    850.   int range = max - min;
    851.   return min + (int)(NextRatio() * (double)range);
    852.   }
    853.   }
    854.  
    855.   public ulong GetNextULong(ulong start, ulong stop)
    856.   {
    857.   if (start == 0)
    858.   {
    859.   return (ulong)(NextRatio() * (ulong)stop);
    860.   }
    861.   else
    862.   {
    863.   ulong range = stop - start;
    864.   return start + (ulong)(NextRatio() * (ulong)range);
    865.   }
    866.   }
    867.  
    868.   public byte[] GetArray()
    869.   {
    870.   return NewArray;
    871.   }
    872.  
    873.   public double NextRatio()
    874.   {
    875.   double val = (double)((double)NextUlong() / (double)ulong.MaxValue);
    876.   if (val == 1)
    877.   {
    878.   return NextRatio();
    879.   }
    880.   else
    881.   {
    882.   return val;
    883.   }
    884.   }
    885.  
    886.   private ulong NextUlong()
    887.   {
    888.   NextKey();
    889.   return br.ReadUInt64();
    890.   }
    891.  
    892.  
    893.   }
    894.  
    895. }
    In reality though, Perlin isn't that hard of an algorithm at all - so you can always just make your own to your specific liking.
     
    Last edited: Mar 4, 2015
  3. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    I think that's more code than I've ever written into a single file in my entire time of programming. I'll try it out in the morning, thanks.

    If it doesn't work, I may have found a solution elsewhere, so yep.
     
  4. bloomingdedalus

    bloomingdedalus

    Joined:
    Aug 13, 2012
    Posts:
    139
    It was all in different files - I just threw them into one. However, when I did start programming I had a 25,000k line file of class objects. I'm still in the process of cutting it all up into small files.