Advertisement
timeshifter

Untitled

Jan 23rd, 2015
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.50 KB | None | 0 0
  1.             byte[,] map = new byte[width, height];
  2.  
  3.             Random R = new Random(seed);
  4.  
  5.             int typeSpaceLeft = 0;
  6.             double typeRange = 0.0,
  7.                 lastX = R.Next(surfaceLevel - 5, surfaceLevel + 5);
  8.  
  9.             //first pass, generates the surface
  10.             for (int x = 0; x < width; x++) {
  11.                 if (typeSpaceLeft == 0) {
  12.                     typeSpaceLeft = R.Next(50, 300);
  13.                     typeRange = R.NextDouble() * 3.0;
  14.                 }
  15.  
  16.                 lastX += (R.NextDouble() * typeRange * 2.0) - typeRange;
  17.  
  18.  
  19.                 for (int y = 0; y < height; y++) {
  20.                     if (y < lastX)
  21.                         map[x, y] = 0;
  22.                     else
  23.                         map[x, y] = 1;
  24.                 }
  25.                 typeSpaceLeft--;
  26.             }
  27.  
  28.             //number of caves
  29.             int mapCaveRatio = (width * height) / 1000;
  30.             int caveCount = mapCaveRatio;
  31.             //which caves will have water
  32.             List<Point> lakePts = new List<Point>();
  33.             double lakePct = 0.05; //percent chance of each generated cave having a lake
  34.             //second pass generates rough caves
  35.             while (caveCount > 0 && false) {
  36.                 int cx = R.Next(width),
  37.                     cy = R.Next(height),
  38.                     maxTurns = 500;
  39.                 double caveHorizPct = R.NextDouble() * 0.8,
  40.                     caveVertPct = R.NextDouble() * 0.5,
  41.                     depthFactor = 1.0 - ((double)cy / (double)height);
  42.                 int caveTurns = (int)((double)maxTurns * depthFactor);
  43.                 List<Point> pts = new List<Point>();
  44.                 if (map[cx, cy] == 1) {
  45.                     pts.Add(new Point(cx, cy));
  46.                     if (R.NextDouble() <= lakePct)
  47.                         lakePts.Add(new Point(cx, cy));
  48.                 }
  49.  
  50.                 while (caveTurns > 0 && pts.Count > 0) {
  51.  
  52.                     int bx = pts[0].X, by = pts[0].Y;
  53.  
  54.                     map[bx, by] = 0;
  55.  
  56.                     if (R.NextDouble() <= caveHorizPct && bx > 0 && map[bx - 1, by] == 1) {
  57.                         pts.Add(new Point(bx - 1, by));
  58.                         map[bx - 1, by] = 0;
  59.                     }
  60.  
  61.                     if (R.NextDouble() <= caveHorizPct && bx < (width - 1) && map[bx + 1, by] == 1) {
  62.                         pts.Add(new Point(bx + 1, by));
  63.                         map[bx + 1, by] = 0;
  64.                     }
  65.  
  66.                     if (R.NextDouble() <= caveVertPct && by > 0 && map[bx, by - 1] == 1) {
  67.                         pts.Add(new Point(bx, by - 1));
  68.                         map[bx, by - 1] = 0;
  69.                     }
  70.  
  71.                     if (R.NextDouble() <= caveVertPct && by < (height - 1) && map[bx, by + 1] == 1) {
  72.                         pts.Add(new Point(bx, by + 1));
  73.                         map[bx, by + 1] = 0;
  74.                     }
  75.  
  76.                     caveTurns--;
  77.  
  78.                     if (caveTurns == 0 || pts.Count == 1) {
  79.                         if (R.NextDouble() < 0.5) { //50% chance to start a new cave system from the first point still available
  80.                             pts.RemoveRange(1, pts.Count - 1);
  81.                             maxTurns /= 2;
  82.                             caveTurns = (int)(maxTurns * depthFactor);
  83.                         }
  84.                     }
  85.                     else
  86.                         pts.RemoveAt(0);
  87.  
  88.                 }
  89.  
  90.  
  91.                 caveCount--;
  92.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement