
Labyrinth of the Minotaur A simple roguelike/RPG using a nokia 5110 screen
Dependencies: N5110 PowerControl mbed
WorldBuilder/WorldBuilder.cpp@20:e54792b89571, 2015-05-04 (annotated)
- Committer:
- ThomasBGill
- Date:
- Mon May 04 16:26:43 2015 +0000
- Revision:
- 20:e54792b89571
- Child:
- 21:aa4feee6aa39
Worldbuilder functions put into separate header and source
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ThomasBGill | 20:e54792b89571 | 1 | #include "WorldBuilder.h" |
ThomasBGill | 20:e54792b89571 | 2 | |
ThomasBGill | 20:e54792b89571 | 3 | int map[84][48]; |
ThomasBGill | 20:e54792b89571 | 4 | |
ThomasBGill | 20:e54792b89571 | 5 | //Enterance coordinates |
ThomasBGill | 20:e54792b89571 | 6 | int enx; |
ThomasBGill | 20:e54792b89571 | 7 | int eny; |
ThomasBGill | 20:e54792b89571 | 8 | |
ThomasBGill | 20:e54792b89571 | 9 | //Exit coordinates |
ThomasBGill | 20:e54792b89571 | 10 | int exx; |
ThomasBGill | 20:e54792b89571 | 11 | int exy; |
ThomasBGill | 20:e54792b89571 | 12 | |
ThomasBGill | 20:e54792b89571 | 13 | int sx; |
ThomasBGill | 20:e54792b89571 | 14 | int sy; |
ThomasBGill | 20:e54792b89571 | 15 | int dir; |
ThomasBGill | 20:e54792b89571 | 16 | |
ThomasBGill | 20:e54792b89571 | 17 | int level; |
ThomasBGill | 20:e54792b89571 | 18 | |
ThomasBGill | 20:e54792b89571 | 19 | void Walls() |
ThomasBGill | 20:e54792b89571 | 20 | { |
ThomasBGill | 20:e54792b89571 | 21 | //Fill map with walls |
ThomasBGill | 20:e54792b89571 | 22 | for (int i = 0; i<84; i++) { |
ThomasBGill | 20:e54792b89571 | 23 | for (int j = 0; j<48; j++) { |
ThomasBGill | 20:e54792b89571 | 24 | |
ThomasBGill | 20:e54792b89571 | 25 | map[i][j] = WALL; |
ThomasBGill | 20:e54792b89571 | 26 | } |
ThomasBGill | 20:e54792b89571 | 27 | } |
ThomasBGill | 20:e54792b89571 | 28 | } |
ThomasBGill | 20:e54792b89571 | 29 | |
ThomasBGill | 20:e54792b89571 | 30 | void FirstRoom() |
ThomasBGill | 20:e54792b89571 | 31 | { |
ThomasBGill | 20:e54792b89571 | 32 | //Create initial room |
ThomasBGill | 20:e54792b89571 | 33 | int si = rand() % 25 + 1; |
ThomasBGill | 20:e54792b89571 | 34 | int sj = rand() % 15 + 1; |
ThomasBGill | 20:e54792b89571 | 35 | |
ThomasBGill | 20:e54792b89571 | 36 | int sw = rand() % 5 + 5; |
ThomasBGill | 20:e54792b89571 | 37 | int sh = rand() % 5 + 5; |
ThomasBGill | 20:e54792b89571 | 38 | |
ThomasBGill | 20:e54792b89571 | 39 | for (int i = si, w = si + sw; i <w; i++) { |
ThomasBGill | 20:e54792b89571 | 40 | for (int j = sj, h = sj + sh; j <h; j++) { |
ThomasBGill | 20:e54792b89571 | 41 | map[i][j] = FLOOR; |
ThomasBGill | 20:e54792b89571 | 42 | } |
ThomasBGill | 20:e54792b89571 | 43 | } |
ThomasBGill | 20:e54792b89571 | 44 | //Create enterance in room |
ThomasBGill | 20:e54792b89571 | 45 | enx = rand() % sw + si; |
ThomasBGill | 20:e54792b89571 | 46 | eny = rand() % sh + sj; |
ThomasBGill | 20:e54792b89571 | 47 | map[enx][eny] = ENTER; |
ThomasBGill | 20:e54792b89571 | 48 | } |
ThomasBGill | 20:e54792b89571 | 49 | |
ThomasBGill | 20:e54792b89571 | 50 | void ExitRoom() |
ThomasBGill | 20:e54792b89571 | 51 | { |
ThomasBGill | 20:e54792b89571 | 52 | //Create exit room |
ThomasBGill | 20:e54792b89571 | 53 | int si = rand() % 50 + 30; |
ThomasBGill | 20:e54792b89571 | 54 | int sj = rand() % 25 + 20; |
ThomasBGill | 20:e54792b89571 | 55 | |
ThomasBGill | 20:e54792b89571 | 56 | int sw = rand() % 5 + 5; |
ThomasBGill | 20:e54792b89571 | 57 | int sh = rand() % 5 + 5; |
ThomasBGill | 20:e54792b89571 | 58 | |
ThomasBGill | 20:e54792b89571 | 59 | for (int i = si, w = si + sw; i <w; i++) { |
ThomasBGill | 20:e54792b89571 | 60 | for (int j = sj, h = sj + sh; j <h; j++) { |
ThomasBGill | 20:e54792b89571 | 61 | map[i][j] = FLOOR; |
ThomasBGill | 20:e54792b89571 | 62 | } |
ThomasBGill | 20:e54792b89571 | 63 | } |
ThomasBGill | 20:e54792b89571 | 64 | //Create exit in room |
ThomasBGill | 20:e54792b89571 | 65 | exx = rand() % sw + si; |
ThomasBGill | 20:e54792b89571 | 66 | exy = rand() % sh + sj; |
ThomasBGill | 20:e54792b89571 | 67 | map[exx][exy] = EXIT; |
ThomasBGill | 20:e54792b89571 | 68 | |
ThomasBGill | 20:e54792b89571 | 69 | } |
ThomasBGill | 20:e54792b89571 | 70 | |
ThomasBGill | 20:e54792b89571 | 71 | void DungeonRoomBuilder() |
ThomasBGill | 20:e54792b89571 | 72 | { |
ThomasBGill | 20:e54792b89571 | 73 | sx = rand() % 84; |
ThomasBGill | 20:e54792b89571 | 74 | sy = rand() % 48; |
ThomasBGill | 20:e54792b89571 | 75 | |
ThomasBGill | 20:e54792b89571 | 76 | //Get length |
ThomasBGill | 20:e54792b89571 | 77 | int sw = rand() % 5 + 5; |
ThomasBGill | 20:e54792b89571 | 78 | int sh = rand() % 5 + 5; |
ThomasBGill | 20:e54792b89571 | 79 | |
ThomasBGill | 20:e54792b89571 | 80 | for (int i = sx; i < sx + sw; i++) { |
ThomasBGill | 20:e54792b89571 | 81 | for (int j = sy; j < sy + sh; j++) { |
ThomasBGill | 20:e54792b89571 | 82 | if (i < 83 && j < 47) { |
ThomasBGill | 20:e54792b89571 | 83 | if (map[i][j] == WALL) { |
ThomasBGill | 20:e54792b89571 | 84 | map[i][j] = FLOOR; |
ThomasBGill | 20:e54792b89571 | 85 | } |
ThomasBGill | 20:e54792b89571 | 86 | } |
ThomasBGill | 20:e54792b89571 | 87 | } |
ThomasBGill | 20:e54792b89571 | 88 | } |
ThomasBGill | 20:e54792b89571 | 89 | |
ThomasBGill | 20:e54792b89571 | 90 | if (rand() % 3 == 0) { |
ThomasBGill | 20:e54792b89571 | 91 | int i = rand() % sw + sx; |
ThomasBGill | 20:e54792b89571 | 92 | int j = rand() % sh + sy; |
ThomasBGill | 20:e54792b89571 | 93 | map[i][j] = CHEST; |
ThomasBGill | 20:e54792b89571 | 94 | } |
ThomasBGill | 20:e54792b89571 | 95 | } |
ThomasBGill | 20:e54792b89571 | 96 | |
ThomasBGill | 20:e54792b89571 | 97 | int Neighbours(int i, int j) |
ThomasBGill | 20:e54792b89571 | 98 | { |
ThomasBGill | 20:e54792b89571 | 99 | //Check neighbours |
ThomasBGill | 20:e54792b89571 | 100 | int n = 0; |
ThomasBGill | 20:e54792b89571 | 101 | |
ThomasBGill | 20:e54792b89571 | 102 | if (map[i + 1][j] == FLOOR) { |
ThomasBGill | 20:e54792b89571 | 103 | n++; |
ThomasBGill | 20:e54792b89571 | 104 | } |
ThomasBGill | 20:e54792b89571 | 105 | if (map[i - 1][j] == FLOOR) { |
ThomasBGill | 20:e54792b89571 | 106 | n++; |
ThomasBGill | 20:e54792b89571 | 107 | } |
ThomasBGill | 20:e54792b89571 | 108 | if (map[i][j + 1] == FLOOR) { |
ThomasBGill | 20:e54792b89571 | 109 | n++; |
ThomasBGill | 20:e54792b89571 | 110 | } |
ThomasBGill | 20:e54792b89571 | 111 | if (map[i][j - 1] == FLOOR) { |
ThomasBGill | 20:e54792b89571 | 112 | n++; |
ThomasBGill | 20:e54792b89571 | 113 | } |
ThomasBGill | 20:e54792b89571 | 114 | |
ThomasBGill | 20:e54792b89571 | 115 | return n; |
ThomasBGill | 20:e54792b89571 | 116 | } |
ThomasBGill | 20:e54792b89571 | 117 | |
ThomasBGill | 20:e54792b89571 | 118 | void DeadEnds(int d) |
ThomasBGill | 20:e54792b89571 | 119 | { |
ThomasBGill | 20:e54792b89571 | 120 | for (int del = d; del > 0; del--) { |
ThomasBGill | 20:e54792b89571 | 121 | for (int i = 0; i < 84; i++) { |
ThomasBGill | 20:e54792b89571 | 122 | for (int j = 0; j < 48; j++) { |
ThomasBGill | 20:e54792b89571 | 123 | |
ThomasBGill | 20:e54792b89571 | 124 | if (Neighbours(i, j) < 2) { |
ThomasBGill | 20:e54792b89571 | 125 | map[i][j] = WALL; |
ThomasBGill | 20:e54792b89571 | 126 | } |
ThomasBGill | 20:e54792b89571 | 127 | } |
ThomasBGill | 20:e54792b89571 | 128 | } |
ThomasBGill | 20:e54792b89571 | 129 | } |
ThomasBGill | 20:e54792b89571 | 130 | } |
ThomasBGill | 20:e54792b89571 | 131 | |
ThomasBGill | 20:e54792b89571 | 132 | void Border() |
ThomasBGill | 20:e54792b89571 | 133 | { |
ThomasBGill | 20:e54792b89571 | 134 | |
ThomasBGill | 20:e54792b89571 | 135 | for (int i = 0; i < 84; i++) { |
ThomasBGill | 20:e54792b89571 | 136 | for (int j = 0; j < 48; j++) { |
ThomasBGill | 20:e54792b89571 | 137 | |
ThomasBGill | 20:e54792b89571 | 138 | if (i == 0 || i == 83 || j == 0 || j == 47) { |
ThomasBGill | 20:e54792b89571 | 139 | |
ThomasBGill | 20:e54792b89571 | 140 | map[i][j] = WALL; |
ThomasBGill | 20:e54792b89571 | 141 | |
ThomasBGill | 20:e54792b89571 | 142 | } |
ThomasBGill | 20:e54792b89571 | 143 | } |
ThomasBGill | 20:e54792b89571 | 144 | } |
ThomasBGill | 20:e54792b89571 | 145 | } |
ThomasBGill | 20:e54792b89571 | 146 | |
ThomasBGill | 20:e54792b89571 | 147 | void RandFloor(int r) |
ThomasBGill | 20:e54792b89571 | 148 | { |
ThomasBGill | 20:e54792b89571 | 149 | |
ThomasBGill | 20:e54792b89571 | 150 | for (int space = rand() % 50 + r; space > 0; space--) { |
ThomasBGill | 20:e54792b89571 | 151 | |
ThomasBGill | 20:e54792b89571 | 152 | int i = rand() % 84; |
ThomasBGill | 20:e54792b89571 | 153 | int j = rand() % 48; |
ThomasBGill | 20:e54792b89571 | 154 | |
ThomasBGill | 20:e54792b89571 | 155 | if (rand() % 2 == 0 && map[i][j] == WALL) { |
ThomasBGill | 20:e54792b89571 | 156 | map[i][j] = FLOOR; |
ThomasBGill | 20:e54792b89571 | 157 | } |
ThomasBGill | 20:e54792b89571 | 158 | } |
ThomasBGill | 20:e54792b89571 | 159 | |
ThomasBGill | 20:e54792b89571 | 160 | } |
ThomasBGill | 20:e54792b89571 | 161 | |
ThomasBGill | 20:e54792b89571 | 162 | void MazeKill() |
ThomasBGill | 20:e54792b89571 | 163 | { |
ThomasBGill | 20:e54792b89571 | 164 | |
ThomasBGill | 20:e54792b89571 | 165 | int move[4] = { UP, DOWN, LEFT, RIGHT }; |
ThomasBGill | 20:e54792b89571 | 166 | |
ThomasBGill | 20:e54792b89571 | 167 | bool moved = true; |
ThomasBGill | 20:e54792b89571 | 168 | |
ThomasBGill | 20:e54792b89571 | 169 | while (moved == true) { |
ThomasBGill | 20:e54792b89571 | 170 | |
ThomasBGill | 20:e54792b89571 | 171 | moved = false; |
ThomasBGill | 20:e54792b89571 | 172 | |
ThomasBGill | 20:e54792b89571 | 173 | for (int s = 0; s < 3; s++) { //Shuffle array |
ThomasBGill | 20:e54792b89571 | 174 | |
ThomasBGill | 20:e54792b89571 | 175 | int r = rand() % 4; |
ThomasBGill | 20:e54792b89571 | 176 | |
ThomasBGill | 20:e54792b89571 | 177 | int temp = move[s]; |
ThomasBGill | 20:e54792b89571 | 178 | |
ThomasBGill | 20:e54792b89571 | 179 | move[s] = move[r]; |
ThomasBGill | 20:e54792b89571 | 180 | |
ThomasBGill | 20:e54792b89571 | 181 | move[r] = temp; |
ThomasBGill | 20:e54792b89571 | 182 | } |
ThomasBGill | 20:e54792b89571 | 183 | |
ThomasBGill | 20:e54792b89571 | 184 | |
ThomasBGill | 20:e54792b89571 | 185 | |
ThomasBGill | 20:e54792b89571 | 186 | for (int i = 0; i < 3; i++) { |
ThomasBGill | 20:e54792b89571 | 187 | if (move[i] == UP) { |
ThomasBGill | 20:e54792b89571 | 188 | if (map[sx][sy - 1] == WALL && Neighbours(sx, sy - 1) == 1 && map[sx][sy - 2] == WALL && Neighbours(sx, sy - 2) == 0 && sy > 3) { |
ThomasBGill | 20:e54792b89571 | 189 | map[sx][sy - 1] = FLOOR; |
ThomasBGill | 20:e54792b89571 | 190 | map[sx][sy - 2] = FLOOR; |
ThomasBGill | 20:e54792b89571 | 191 | sy = sy - 2; |
ThomasBGill | 20:e54792b89571 | 192 | moved = true; |
ThomasBGill | 20:e54792b89571 | 193 | break; |
ThomasBGill | 20:e54792b89571 | 194 | } |
ThomasBGill | 20:e54792b89571 | 195 | } |
ThomasBGill | 20:e54792b89571 | 196 | else if (move[i] == DOWN) { |
ThomasBGill | 20:e54792b89571 | 197 | if (map[sx][sy + 1] == WALL && Neighbours(sx, sy + 1) == 1 && map[sx][sy + 2] == WALL && Neighbours(sx, sy + 2) == 0 && sy < 45) { |
ThomasBGill | 20:e54792b89571 | 198 | map[sx][sy + 1] = FLOOR; |
ThomasBGill | 20:e54792b89571 | 199 | map[sx][sy + 2] = FLOOR; |
ThomasBGill | 20:e54792b89571 | 200 | sy = sy + 2; |
ThomasBGill | 20:e54792b89571 | 201 | moved = true; |
ThomasBGill | 20:e54792b89571 | 202 | break; |
ThomasBGill | 20:e54792b89571 | 203 | } |
ThomasBGill | 20:e54792b89571 | 204 | } |
ThomasBGill | 20:e54792b89571 | 205 | else if (move[i] == LEFT) { |
ThomasBGill | 20:e54792b89571 | 206 | if (map[sx - 1][sy] == WALL && Neighbours(sx - 1, sy) == 1 && map[sx - 2][sy] == WALL && Neighbours(sx - 2, sy) == 0 && sx > 3) { |
ThomasBGill | 20:e54792b89571 | 207 | map[sx - 1][sy] = FLOOR; |
ThomasBGill | 20:e54792b89571 | 208 | map[sx - 2][sy] = FLOOR; |
ThomasBGill | 20:e54792b89571 | 209 | sx = sx - 2; |
ThomasBGill | 20:e54792b89571 | 210 | moved = true; |
ThomasBGill | 20:e54792b89571 | 211 | break; |
ThomasBGill | 20:e54792b89571 | 212 | } |
ThomasBGill | 20:e54792b89571 | 213 | } |
ThomasBGill | 20:e54792b89571 | 214 | else if (move[i] == RIGHT) { |
ThomasBGill | 20:e54792b89571 | 215 | if (map[sx + 1][sy] == WALL && Neighbours(sx + 1, sy) == 1 && map[sx + 2][sy] == WALL && Neighbours(sx + 2, sy) == 0 && sx < 81) { |
ThomasBGill | 20:e54792b89571 | 216 | map[sx + 1][sy] = FLOOR; |
ThomasBGill | 20:e54792b89571 | 217 | map[sx + 2][sy] = FLOOR; |
ThomasBGill | 20:e54792b89571 | 218 | sx = sx + 2; |
ThomasBGill | 20:e54792b89571 | 219 | moved = true; |
ThomasBGill | 20:e54792b89571 | 220 | break; |
ThomasBGill | 20:e54792b89571 | 221 | } |
ThomasBGill | 20:e54792b89571 | 222 | } |
ThomasBGill | 20:e54792b89571 | 223 | } |
ThomasBGill | 20:e54792b89571 | 224 | } |
ThomasBGill | 20:e54792b89571 | 225 | |
ThomasBGill | 20:e54792b89571 | 226 | } |
ThomasBGill | 20:e54792b89571 | 227 | |
ThomasBGill | 20:e54792b89571 | 228 | void Maze() |
ThomasBGill | 20:e54792b89571 | 229 | { |
ThomasBGill | 20:e54792b89571 | 230 | sx = 1; |
ThomasBGill | 20:e54792b89571 | 231 | sy = 1; |
ThomasBGill | 20:e54792b89571 | 232 | |
ThomasBGill | 20:e54792b89571 | 233 | //Choose random direction |
ThomasBGill | 20:e54792b89571 | 234 | //Check if 2 cells in direction have no neighbours (excluding current position) |
ThomasBGill | 20:e54792b89571 | 235 | //If true then build and set new current position |
ThomasBGill | 20:e54792b89571 | 236 | //If false chose next direction |
ThomasBGill | 20:e54792b89571 | 237 | |
ThomasBGill | 20:e54792b89571 | 238 | //If cannot move in any direction scan through each cell until there is one which can be built on |
ThomasBGill | 20:e54792b89571 | 239 | //If scan completes END |
ThomasBGill | 20:e54792b89571 | 240 | |
ThomasBGill | 20:e54792b89571 | 241 | int end = false; |
ThomasBGill | 20:e54792b89571 | 242 | |
ThomasBGill | 20:e54792b89571 | 243 | while (end == false) { |
ThomasBGill | 20:e54792b89571 | 244 | |
ThomasBGill | 20:e54792b89571 | 245 | end = true; |
ThomasBGill | 20:e54792b89571 | 246 | |
ThomasBGill | 20:e54792b89571 | 247 | map[sx][sy] = FLOOR; |
ThomasBGill | 20:e54792b89571 | 248 | |
ThomasBGill | 20:e54792b89571 | 249 | MazeKill(); |
ThomasBGill | 20:e54792b89571 | 250 | |
ThomasBGill | 20:e54792b89571 | 251 | //DrawMap(); |
ThomasBGill | 20:e54792b89571 | 252 | |
ThomasBGill | 20:e54792b89571 | 253 | for (int i = 1; i < 83; i++) { |
ThomasBGill | 20:e54792b89571 | 254 | for (int j = 1; j < 47; j++) { |
ThomasBGill | 20:e54792b89571 | 255 | |
ThomasBGill | 20:e54792b89571 | 256 | if (map[i][j] == WALL && Neighbours(i, j) == 1) { |
ThomasBGill | 20:e54792b89571 | 257 | sx = i; |
ThomasBGill | 20:e54792b89571 | 258 | sy = j; |
ThomasBGill | 20:e54792b89571 | 259 | |
ThomasBGill | 20:e54792b89571 | 260 | end = false; |
ThomasBGill | 20:e54792b89571 | 261 | } |
ThomasBGill | 20:e54792b89571 | 262 | |
ThomasBGill | 20:e54792b89571 | 263 | } |
ThomasBGill | 20:e54792b89571 | 264 | } |
ThomasBGill | 20:e54792b89571 | 265 | |
ThomasBGill | 20:e54792b89571 | 266 | } |
ThomasBGill | 20:e54792b89571 | 267 | } |
ThomasBGill | 20:e54792b89571 | 268 | |
ThomasBGill | 20:e54792b89571 | 269 | void DungeonBuilder() |
ThomasBGill | 20:e54792b89571 | 270 | { |
ThomasBGill | 20:e54792b89571 | 271 | |
ThomasBGill | 20:e54792b89571 | 272 | Maze(); |
ThomasBGill | 20:e54792b89571 | 273 | |
ThomasBGill | 20:e54792b89571 | 274 | FirstRoom(); |
ThomasBGill | 20:e54792b89571 | 275 | ExitRoom(); |
ThomasBGill | 20:e54792b89571 | 276 | |
ThomasBGill | 20:e54792b89571 | 277 | int rn = rand() % 10 + 6; |
ThomasBGill | 20:e54792b89571 | 278 | |
ThomasBGill | 20:e54792b89571 | 279 | for (int i = rn; i>0; i--) { |
ThomasBGill | 20:e54792b89571 | 280 | DungeonRoomBuilder(); |
ThomasBGill | 20:e54792b89571 | 281 | } |
ThomasBGill | 20:e54792b89571 | 282 | |
ThomasBGill | 20:e54792b89571 | 283 | |
ThomasBGill | 20:e54792b89571 | 284 | |
ThomasBGill | 20:e54792b89571 | 285 | RandFloor(51); |
ThomasBGill | 20:e54792b89571 | 286 | |
ThomasBGill | 20:e54792b89571 | 287 | Border(); |
ThomasBGill | 20:e54792b89571 | 288 | |
ThomasBGill | 20:e54792b89571 | 289 | DeadEnds(20); |
ThomasBGill | 20:e54792b89571 | 290 | |
ThomasBGill | 20:e54792b89571 | 291 | } |
ThomasBGill | 20:e54792b89571 | 292 | |
ThomasBGill | 20:e54792b89571 | 293 | void LabyrinthBuilder() |
ThomasBGill | 20:e54792b89571 | 294 | { |
ThomasBGill | 20:e54792b89571 | 295 | |
ThomasBGill | 20:e54792b89571 | 296 | Maze(); |
ThomasBGill | 20:e54792b89571 | 297 | |
ThomasBGill | 20:e54792b89571 | 298 | FirstRoom(); |
ThomasBGill | 20:e54792b89571 | 299 | ExitRoom(); |
ThomasBGill | 20:e54792b89571 | 300 | |
ThomasBGill | 20:e54792b89571 | 301 | |
ThomasBGill | 20:e54792b89571 | 302 | |
ThomasBGill | 20:e54792b89571 | 303 | RandFloor(151); |
ThomasBGill | 20:e54792b89571 | 304 | |
ThomasBGill | 20:e54792b89571 | 305 | DeadEnds(1); |
ThomasBGill | 20:e54792b89571 | 306 | |
ThomasBGill | 20:e54792b89571 | 307 | Border(); |
ThomasBGill | 20:e54792b89571 | 308 | |
ThomasBGill | 20:e54792b89571 | 309 | } |