Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool CA_wallOrFloor(int x, int y, int sizex, int sizey) { //true = wall, false = floor
- int countW = 0;
- //this counts how many walls are surrounding it
- for (int i = -1; i <= 1; ++i) {
- for (int j = -1; j < 1; ++j) {
- bool outOfBound = false;
- //these watch for out of bounds tiles
- //
- //note: out of bounds tiles are treated as walls for counting purposes
- //
- if ((y+i < 0) || (y+i >= sizey)) { //if y out of range
- outOfBound = true;
- countW++;
- }
- if ((x+j < 0) || (x+j >= sizex)) { //if x out of range
- outOfBound = true;
- countW++;
- }
- if (i == 0 && j == 0) { //not strictly necessary, but ignores the actual tile itself
- outOfBound = true;
- }
- //if hasn't failed yet, counts the tile
- if (not outOfBound) {
- if (map[y+i][x+j].tile == '#') {
- countW++;
- }
- }
- }
- }
- //if more than 5 of its surroundings are walls,
- //or it is already a wall and 4 of its surroundings are walls, it will be a wall
- //else, it will be a floor
- if (countW >= 5) {
- return true;
- } else if ((map[y][x].tile == '#') && (countW >= 4)) {
- return true;
- } else {
- return false;
- }
- }
- void initAsCave_CA(int sizex, int sizey, int iterations, int postprocIterations) {
- //generate the initial map
- for (int i = 0; i < sizey; ++i) {
- map.push_back({});
- for (int j = 0; j < sizex; ++j) {
- if (((i == 0) || (i == sizey - 1)) || ((j == 0) || (j == sizex - 1))) { //if edge, make it wall
- map[i].push_back(Tile('#'));
- } else {
- //add a floor
- map[i].push_back(Tile('.'));
- //45% chance of actually making it a wall
- if (rand() % 100 <= 45) {
- map[i][j] = (Tile('#'));
- }
- }
- }
- }
- //smooth the map
- for (int t = 0; t < iterations; ++t) {
- cout << "\n\ngeneration " << t;
- _printMap();
- //this creates a new map and bases it off the old
- //then, it sets that to be the old map
- vector<vector<Tile>> newMap;
- for (int i = 0; i < sizey; ++i) {
- newMap.push_back({});
- for (int j = 0; j < sizex; ++j) {
- //add a floor
- newMap[i].push_back(Tile('.'));
- //if it's a wall
- if (CA_wallOrFloor(j, i, sizex, sizey)) {
- newMap[i][j] = (Tile('#'));
- }
- }
- }
- //set the old map to the new map in preparation for next iteration
- map = newMap;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement