• API
• FAQ
• Tools
• Archive
daily pastebin goal
33%
SHARE
TWEET

# Untitled

a guest Oct 21st, 2018 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1.     bool CA_wallOrFloor(int x, int y, int sizex, int sizey) {   //true = wall, false = floor
2.         int countW = 0;
3.         //this counts how many walls are surrounding it
4.         for (int i = -1; i <= 1; ++i) {
5.             for (int j = -1; j < 1; ++j) {
6.                 bool outOfBound = false;
7.                 //these watch for out of bounds tiles
8.                 //
9.                 //note: out of bounds tiles are treated as walls for counting purposes
10.                 //
11.                 if ((y+i < 0) || (y+i >= sizey)) {  //if y out of range
12.                     outOfBound = true;
13.                     countW++;
14.                 }
15.                 if ((x+j < 0) || (x+j >= sizex)) {  //if x out of range
16.                     outOfBound = true;
17.                     countW++;
18.                 }
19.                 if (i == 0 && j == 0) { //not strictly necessary, but ignores the actual tile itself
20.                     outOfBound = true;
21.                 }
22.                 //if hasn't failed yet, counts the tile
23.                 if (not outOfBound) {
24.                     if (map[y+i][x+j].tile == '#') {
25.                         countW++;
26.                     }
27.                 }
28.             }
29.         }
30.         //if more than 5 of its surroundings are walls,
31.         //or it is already a wall and 4 of its surroundings are walls, it will be a wall
32.         //else, it will be a floor
33.         if (countW >= 5) {
34.             return true;
35.         } else if ((map[y][x].tile == '#') && (countW >= 4)) {
36.             return true;
37.         } else {
38.             return false;
39.         }
40.     }
41.     void initAsCave_CA(int sizex, int sizey, int iterations, int postprocIterations) {
42.         //generate the initial map
43.         for (int i = 0; i < sizey; ++i) {
44.             map.push_back({});
45.             for (int j = 0; j < sizex; ++j) {
46.                 if (((i == 0) || (i == sizey - 1)) || ((j == 0) || (j == sizex - 1))) { //if edge, make it wall
47.                     map[i].push_back(Tile('#'));
48.                 } else {
50.                     map[i].push_back(Tile('.'));
51.                     //45% chance of actually making it a wall
52.                     if (rand() % 100 <= 45) {
53.                         map[i][j] = (Tile('#'));
54.                     }
55.                 }
56.             }
57.         }
58.
59.         //smooth the map
60.         for (int t = 0; t < iterations; ++t) {
61.             cout << "\n\ngeneration " << t;
62.             _printMap();
63.             //this creates a new map and bases it off the old
64.             //then, it sets that to be the old map
65.             vector<vector<Tile>> newMap;
66.             for (int i = 0; i < sizey; ++i) {
67.                 newMap.push_back({});
68.                 for (int j = 0; j < sizex; ++j) {
70.                     newMap[i].push_back(Tile('.'));
71.                     //if it's a wall
72.                     if (CA_wallOrFloor(j, i, sizex, sizey)) {
73.                         newMap[i][j] = (Tile('#'));
74.                     }
75.                 }
76.             }
77.             //set the old map to the new map in preparation for next iteration
78.             map = newMap;
79.         }
80.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top