Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- byte[,] map = new byte[width, height];
- Random R = new Random(seed);
- int typeSpaceLeft = 0;
- double typeRange = 0.0,
- lastX = R.Next(surfaceLevel - 5, surfaceLevel + 5);
- //first pass, generates the surface
- for (int x = 0; x < width; x++) {
- if (typeSpaceLeft == 0) {
- typeSpaceLeft = R.Next(50, 300);
- typeRange = R.NextDouble() * 3.0;
- }
- lastX += (R.NextDouble() * typeRange * 2.0) - typeRange;
- for (int y = 0; y < height; y++) {
- if (y < lastX)
- map[x, y] = 0;
- else
- map[x, y] = 1;
- }
- typeSpaceLeft--;
- }
- //number of caves
- int mapCaveRatio = (width * height) / 1000;
- int caveCount = mapCaveRatio;
- //which caves will have water
- List<Point> lakePts = new List<Point>();
- double lakePct = 0.05; //percent chance of each generated cave having a lake
- //second pass generates rough caves
- while (caveCount > 0 && false) {
- int cx = R.Next(width),
- cy = R.Next(height),
- maxTurns = 500;
- double caveHorizPct = R.NextDouble() * 0.8,
- caveVertPct = R.NextDouble() * 0.5,
- depthFactor = 1.0 - ((double)cy / (double)height);
- int caveTurns = (int)((double)maxTurns * depthFactor);
- List<Point> pts = new List<Point>();
- if (map[cx, cy] == 1) {
- pts.Add(new Point(cx, cy));
- if (R.NextDouble() <= lakePct)
- lakePts.Add(new Point(cx, cy));
- }
- while (caveTurns > 0 && pts.Count > 0) {
- int bx = pts[0].X, by = pts[0].Y;
- map[bx, by] = 0;
- if (R.NextDouble() <= caveHorizPct && bx > 0 && map[bx - 1, by] == 1) {
- pts.Add(new Point(bx - 1, by));
- map[bx - 1, by] = 0;
- }
- if (R.NextDouble() <= caveHorizPct && bx < (width - 1) && map[bx + 1, by] == 1) {
- pts.Add(new Point(bx + 1, by));
- map[bx + 1, by] = 0;
- }
- if (R.NextDouble() <= caveVertPct && by > 0 && map[bx, by - 1] == 1) {
- pts.Add(new Point(bx, by - 1));
- map[bx, by - 1] = 0;
- }
- if (R.NextDouble() <= caveVertPct && by < (height - 1) && map[bx, by + 1] == 1) {
- pts.Add(new Point(bx, by + 1));
- map[bx, by + 1] = 0;
- }
- caveTurns--;
- if (caveTurns == 0 || pts.Count == 1) {
- if (R.NextDouble() < 0.5) { //50% chance to start a new cave system from the first point still available
- pts.RemoveRange(1, pts.Count - 1);
- maxTurns /= 2;
- caveTurns = (int)(maxTurns * depthFactor);
- }
- }
- else
- pts.RemoveAt(0);
- }
- caveCount--;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement