How can I make a better cave generator?

edited December 2015 in Questions about Code

Can someone please help me make a better cave generator? It would be much appreciated thank you.

void setup() {
  size(800, 500);
}

void draw() {
}

void mouseClicked() {
  gen();
}

void gen() {
  background(0);
  translate(width/2, height/2);
  rotate(PI);
  int w = 10, h = 10;

  for (int i = -width/2; i < width; i += w) {  //GROUND
    for (int j = -height/2; j < int(random(0, h*2)); j += h) {
      fill(127);  //Rock
      rect(i, j, w, h);
    }
  }

  for (int i = -width/2; i < width; i += w) {  //CAVE
    for (int j = -height/2; j < int(random(0, h*2)); j += h) {
      fill(0);  //Cave
      int caveChance = 1000;
      int cave = int(random(caveChance + 1));
      if (cave == caveChance) {
        println(cave);
        for (int k = 0; k < int(random(10, 25))*h; k += h) {
          for (int l = 0; l < int(random(10, 25))*w; l += w) {
            rect(i + l + int(random(10))*w, j + k + int(random(10))*h, w, h);
          }
        }
      }
    }
  }
}
Tagged:

Answers

  • that is a birds eye view of a map, mine is a side view and it doesn't really help me figure out how to generate caves.

  • I guess the real question is... What makes a "better" cave? Like, how do you define a cave as "good" or "better" than some other cave?

  • Answer ✓

    Here cave generation algorithm using Celluar Automata (if you familliar with game of life that should be easy, or else, read Shiffman).

  • edited December 2015

    What I would like to happen is when the code generates the black tiles there are no gray spots in there with them. So more of a smooth and easily definable cave side if possible?

  • Thank you Ater I have figured out how to create the caves thanks to the links you sent :)

    int w = 10, h = 10;
    
    void setup() {
      size(800, 500);
    }
    
    void draw() {
      //gen();
      //cave();
    }
    
    void mouseClicked() {
      noStroke();
      gen();
      cave();
    }
    
    void gen() {
      for (int i = 0; i < width; i += w) {
        for (int j = height/2; j < height; j += h) {
          fill(0, 127, 0);
          rect(i, j, w, h);
          //cave();
        }
      }
    }
    
    void cave() {
      fill(255);
      for (int a = 0; a < 5; a++) {
        int totalSize = int(random(100, 1000));
        int x = int(random(1, 80))*w, y = int(random(1, 50))*h;
        for (int i = 0; i < totalSize; i++) {
          int dir = int(random(1, 5));
          int k = int(random(1, 3))*w, l = int(random(1, 3))*h;
          if (dir == 1) x += w;
          if (dir == 2) x -= w;
          if (dir == 3) y += h;
          if (dir == 4) y -= h;
          x = constrain(x, k, width-k);
          y = constrain(y, l, height-1);
          rect(x, y + height/2, k, l);
        }
      }
    }
    
  • Amazing, makes me want to try to generate something too! :)

  • edited December 2015

    It's fun, but can get annoying if you don't think in multiples in the width and height of a single cell.

    I'll be posting the final code on my Open Processing Port Folio later on this week hopefully.

    http://www.openprocessing.org/user/50389

Sign In or Register to comment.