Cave generation using cellular automata unexpected results

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • johntelman
    New Member
    • Dec 2020
    • 1

    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 :

    Code:
    int cellular_iterations = 50;
    
    srand((ax * CX) + (ay * CY) + (az * CZ));
    for (int x = 0; x < CX+2; x++)
        for (int y = 0; y < CY+2; y++)
            for (int z = 0; z < CZ+2; z++)
            {
                float rd = (float)rand() / (float)RAND_MAX;
                if (rd > 0.5f)  cellules[x][y][z] = 1;
                else    cellules[x][y][z] = 0;
    
                if (ay == (-SCY/2)) cellules[x][y][z] = 1;
            }
    
    std::vector<std::vector<std::vector<int>>> cellules_(CX + 2, std::vector<std::vector<int>>(CY + 2, std::vector<int>(CZ + 2, -1)));
    for (int i = 0; i < cellular_iterations; i++)
    {
        for (int x = 1; x < CX+1; x++)
            for (int y = 1; y < CY+1; y++)
                for (int z = 1; z < CZ+1; z++)
                {
                    bool isalive = false;
                    int alive_blocks = 0;
                    for (int x_ = -1; x_ < 2; x_++)
                        for (int y_ = -1; y_ < 2; y_++)
                            for (int z_ = -1; z_ < 2; z_++)
                            {
                                if (!(x_ == 0 && y_ == 0 && z_ == 0))
                                {
                                    alive_blocks += (cellules[x+x_][y+y_][z+z_] == 1) ? 1 : 0;
                                }
                                else
                                {
                                    if(cellules[x + x_][y + y_][z + z_] == 1)   isalive = true;
                                }
                            }
                    if (isalive && alive_blocks >= 13)
                    {
                        cellules_[x][y][z] = 1;
                    }
                    else if (!isalive && ((13 <= alive_blocks && alive_blocks <= 14) || (17 <= alive_blocks && alive_blocks <= 19)))
                    {
                        cellules_[x][y][z] = 1;
                    }
                    else
                    {
                        cellules_[x][y][z] = 0;
                    }
                }
        for (int x = 0; x < CX + 2; x++)
            for (int y = 0; y < CY + 2; y++)
                for (int z = 0; z < CZ + 2; z++)
                {
                    cellules[x][y][z] = cellules_[x][y][z];
                    if (ay == (-SCY/2)) cellules[x][y][z] = 1;
                }
    }
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    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.

    Comment

    Working...