Labyrinth of the Minotaur A simple roguelike/RPG using a nokia 5110 screen

Dependencies:   N5110 PowerControl mbed

Committer:
ThomasBGill
Date:
Mon May 04 17:16:31 2015 +0000
Revision:
21:aa4feee6aa39
Parent:
20:e54792b89571
Child:
22:dae750e4d749
main.cpp cleanup

Who changed what in which revision?

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