Skip to content

nmmarzano/CellularCaves.py

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 

Repository files navigation

CellularCaves.py

Caves generated via Cellular Automata as per the algorithm explained in roguebasin, implemented in Python.

Running python cellularcaves.py will ask first for the width and height of the map, then how many regular and pillar-generating iterations of the cellular automata to pass it through, printing to the console the original random map and each version of the map after every iteration.

Regular iterations just run the cellular automata algorithm normally. Pillar-generating iterations add the rule that an empty space will turn into a wall if the count of walls in its 3x3 cell is zero, reducing the number of wide open spaces.

The chance for walls to spawn at the start is set at 40% (Seems to work better with this and at least 1 or 2 pillar-generating iterations, 0 of them will make the map look too sparse compared to when it was 45% even without pillar generation). Outputs seem to have little variance outside "looks good" and "flooded"/"completely empty" and letting the user define that percentage just made running the demo a bit more cumbersome if you don't know the base parameters, but I've left the code to change it to user input commented out inside. Same thing happens with the "rule" that decides how many walls have to exist in a 3x3 cell for the center tile to be a wall in the next iteration, which is currently set at 5.

After the last iteration, a random open point on the map is chosen from where a flood algorithm is executed which copies that open area onto a completely walled off version of the map, generating a map with only one connected area. On a big enough map it's very probable that this area is the biggest cave generated, but it still sometimes gets it wrong, so the algorithm will also count how many open spaces were found. I've been testing and it seems like a minimum of 40% seems like the best possible results for a generated map, so I'm planning to make the algorithm try again one or two times if the area it generates is less than that, and if it still keeps happening (out of bad luck or, for example, having two big separated caves occupying 25% of the map each) then it could just go for a completely new map.