467,166 Members | 1,043 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,166 developers. It's quick & easy.

Cave generation using cellular automata unexpected results

So I've been implementing a cave generating algorithm for a game that I'm working on. I'm using a cellular automaton for this algorithm but I've been facing some problems, it doesn't seem to be working properly. About the cellular automaton I'm using the following rule i found on a website:
Clouds 1 rule: 13-26/13-14,17-19/2/M
Alive cells with 13,14,15,16,17,18,19,20,21,22,23,24,25 or 26 neighbors survive. Empty cells with 13,14,17,18 or 19 neighbors have a new cell born at that location. 2 states. Cells are either dead or alive. No refractory period they fade from birth to death. M means a Moore neighborhood.

About Moore neighborhood : consider a 3x3x3 3D grid of little cubes. The interior cube is the current cell, so the remaining 26 cubes around it are the neighbors of the center cube.

Is there anything that I'm doing wrong? It's not giving me the expected results.

And by the way here is the code i used to generate the caves :

Expand|Select|Wrap|Line Numbers
  1. int cellular_iterations = 50;
  2.  
  3. srand((ax * CX) + (ay * CY) + (az * CZ));
  4. for (int x = 0; x < CX+2; x++)
  5.     for (int y = 0; y < CY+2; y++)
  6.         for (int z = 0; z < CZ+2; z++)
  7.         {
  8.             float rd = (float)rand() / (float)RAND_MAX;
  9.             if (rd > 0.5f)  cellules[x][y][z] = 1;
  10.             else    cellules[x][y][z] = 0;
  11.  
  12.             if (ay == (-SCY/2)) cellules[x][y][z] = 1;
  13.         }
  14.  
  15. std::vector<std::vector<std::vector<int>>> cellules_(CX + 2, std::vector<std::vector<int>>(CY + 2, std::vector<int>(CZ + 2, -1)));
  16. for (int i = 0; i < cellular_iterations; i++)
  17. {
  18.     for (int x = 1; x < CX+1; x++)
  19.         for (int y = 1; y < CY+1; y++)
  20.             for (int z = 1; z < CZ+1; z++)
  21.             {
  22.                 bool isalive = false;
  23.                 int alive_blocks = 0;
  24.                 for (int x_ = -1; x_ < 2; x_++)
  25.                     for (int y_ = -1; y_ < 2; y_++)
  26.                         for (int z_ = -1; z_ < 2; z_++)
  27.                         {
  28.                             if (!(x_ == 0 && y_ == 0 && z_ == 0))
  29.                             {
  30.                                 alive_blocks += (cellules[x+x_][y+y_][z+z_] == 1) ? 1 : 0;
  31.                             }
  32.                             else
  33.                             {
  34.                                 if(cellules[x + x_][y + y_][z + z_] == 1)   isalive = true;
  35.                             }
  36.                         }
  37.                 if (isalive && alive_blocks >= 13)
  38.                 {
  39.                     cellules_[x][y][z] = 1;
  40.                 }
  41.                 else if (!isalive && ((13 <= alive_blocks && alive_blocks <= 14) || (17 <= alive_blocks && alive_blocks <= 19)))
  42.                 {
  43.                     cellules_[x][y][z] = 1;
  44.                 }
  45.                 else
  46.                 {
  47.                     cellules_[x][y][z] = 0;
  48.                 }
  49.             }
  50.     for (int x = 0; x < CX + 2; x++)
  51.         for (int y = 0; y < CY + 2; y++)
  52.             for (int z = 0; z < CZ + 2; z++)
  53.             {
  54.                 cellules[x][y][z] = cellules_[x][y][z];
  55.                 if (ay == (-SCY/2)) cellules[x][y][z] = 1;
  56.             }
  57. }
  58.  
Dec 18 '20 #1
  • viewed: 1024
Share:
1 Reply
Rabbit
Expert Mod 8TB
You need to create the next generation into a new array. Right now, you're using the state of cellules to update itself, changing its state.
Dec 18 '20 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by defcon8@gmail.com | last post: by
4 posts views Thread by onkar | last post: by
2 posts views Thread by Martijn Mulder | last post: by
13 posts views Thread by bintom | last post: by
1 post views Thread by newphpcoder | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.