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

Dependencies:   N5110 PowerControl mbed

Committer:
ThomasBGill
Date:
Fri May 08 22:45:25 2015 +0000
Revision:
29:89bc8c8aa8ac
Parent:
24:4c4467971c91
Child:
30:4a03611a3d99
Max player health changed to 20

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ThomasBGill 20:e54792b89571 1 #include "WorldBuilder.h"
ThomasBGill 20:e54792b89571 2
ThomasBGill 22:dae750e4d749 3 int map[MAP_WIDTH][MAP_HEIGHT];
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 22:dae750e4d749 26 for (int i = 0; i<MAP_WIDTH; i++) {
ThomasBGill 22:dae750e4d749 27 for (int j = 0; j<MAP_HEIGHT; 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 22:dae750e4d749 37 int si = rand()%25 + 1;
ThomasBGill 22:dae750e4d749 38 int sj = rand()%15 + 1;
ThomasBGill 20:e54792b89571 39
ThomasBGill 22:dae750e4d749 40 int sw = rand()%5 + 5;
ThomasBGill 22:dae750e4d749 41 int sh = rand()%5 + 5;
ThomasBGill 20:e54792b89571 42
ThomasBGill 22:dae750e4d749 43 for (int i = si; i < si + sw; i++) {
ThomasBGill 22:dae750e4d749 44 for (int j = sj; j < sj + sh; j++) {
ThomasBGill 20:e54792b89571 45 map[i][j] = FLOOR;
ThomasBGill 20:e54792b89571 46 }
ThomasBGill 20:e54792b89571 47 }
ThomasBGill 22:dae750e4d749 48
ThomasBGill 20:e54792b89571 49 //Create enterance in room
ThomasBGill 22:dae750e4d749 50 enx = rand()% sw + si;
ThomasBGill 22:dae750e4d749 51 eny = rand()% sh + sj;
ThomasBGill 20:e54792b89571 52 map[enx][eny] = ENTER;
ThomasBGill 22:dae750e4d749 53
ThomasBGill 20:e54792b89571 54 }
ThomasBGill 20:e54792b89571 55
ThomasBGill 20:e54792b89571 56 void ExitRoom()
ThomasBGill 20:e54792b89571 57 {
ThomasBGill 22:dae750e4d749 58
ThomasBGill 20:e54792b89571 59 //Create exit room
ThomasBGill 22:dae750e4d749 60 int si = rand()%50 + 30;
ThomasBGill 22:dae750e4d749 61 int sj = rand()%25 + 20;
ThomasBGill 20:e54792b89571 62
ThomasBGill 22:dae750e4d749 63 int sw = rand()%5 + 5;
ThomasBGill 22:dae750e4d749 64 int sh = rand()%5 + 5;
ThomasBGill 20:e54792b89571 65
ThomasBGill 22:dae750e4d749 66 for (int i = si; i < si + sw; i++) {
ThomasBGill 22:dae750e4d749 67 for (int j = sj; j < sj + sh; j++) {
ThomasBGill 20:e54792b89571 68 map[i][j] = FLOOR;
ThomasBGill 20:e54792b89571 69 }
ThomasBGill 20:e54792b89571 70 }
ThomasBGill 22:dae750e4d749 71
ThomasBGill 20:e54792b89571 72 //Create exit in room
ThomasBGill 22:dae750e4d749 73 exx = rand()% sw + si;
ThomasBGill 22:dae750e4d749 74 exy = rand()% sh + sj;
ThomasBGill 20:e54792b89571 75 map[exx][exy] = EXIT;
ThomasBGill 20:e54792b89571 76 }
ThomasBGill 20:e54792b89571 77
ThomasBGill 20:e54792b89571 78 void DungeonRoomBuilder()
ThomasBGill 20:e54792b89571 79 {
ThomasBGill 22:dae750e4d749 80 sx = rand() % (MAP_WIDTH - 1) + 1;
ThomasBGill 22:dae750e4d749 81 sy = rand() % (MAP_HEIGHT - 1) + 1;
ThomasBGill 20:e54792b89571 82
ThomasBGill 20:e54792b89571 83 //Get length
ThomasBGill 20:e54792b89571 84 int sw = rand() % 5 + 5;
ThomasBGill 20:e54792b89571 85 int sh = rand() % 5 + 5;
ThomasBGill 20:e54792b89571 86
ThomasBGill 20:e54792b89571 87 for (int i = sx; i < sx + sw; i++) {
ThomasBGill 20:e54792b89571 88 for (int j = sy; j < sy + sh; j++) {
ThomasBGill 22:dae750e4d749 89 if (i < MAP_WIDTH - 1 && j < MAP_HEIGHT - 1) {
ThomasBGill 20:e54792b89571 90 if (map[i][j] == WALL) {
ThomasBGill 20:e54792b89571 91 map[i][j] = FLOOR;
ThomasBGill 20:e54792b89571 92 }
ThomasBGill 20:e54792b89571 93 }
ThomasBGill 20:e54792b89571 94 }
ThomasBGill 20:e54792b89571 95 }
ThomasBGill 20:e54792b89571 96
ThomasBGill 29:89bc8c8aa8ac 97 if (rand() % 2 == 0) {
ThomasBGill 20:e54792b89571 98 int i = rand() % sw + sx;
ThomasBGill 20:e54792b89571 99 int j = rand() % sh + sy;
ThomasBGill 20:e54792b89571 100 map[i][j] = CHEST;
ThomasBGill 20:e54792b89571 101 }
ThomasBGill 20:e54792b89571 102 }
ThomasBGill 20:e54792b89571 103
ThomasBGill 20:e54792b89571 104 int Neighbours(int i, int j)
ThomasBGill 20:e54792b89571 105 {
ThomasBGill 20:e54792b89571 106 //Check neighbours
ThomasBGill 20:e54792b89571 107 int n = 0;
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 - 1][j] == 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 if (map[i][j - 1] == FLOOR) {
ThomasBGill 20:e54792b89571 119 n++;
ThomasBGill 20:e54792b89571 120 }
ThomasBGill 20:e54792b89571 121
ThomasBGill 20:e54792b89571 122 return n;
ThomasBGill 20:e54792b89571 123 }
ThomasBGill 20:e54792b89571 124
ThomasBGill 20:e54792b89571 125 void DeadEnds(int d)
ThomasBGill 20:e54792b89571 126 {
ThomasBGill 20:e54792b89571 127 for (int del = d; del > 0; del--) {
ThomasBGill 20:e54792b89571 128 for (int i = 0; i < 84; i++) {
ThomasBGill 20:e54792b89571 129 for (int j = 0; j < 48; j++) {
ThomasBGill 20:e54792b89571 130
ThomasBGill 20:e54792b89571 131 if (Neighbours(i, j) < 2) {
ThomasBGill 20:e54792b89571 132 map[i][j] = WALL;
ThomasBGill 20:e54792b89571 133 }
ThomasBGill 20:e54792b89571 134 }
ThomasBGill 20:e54792b89571 135 }
ThomasBGill 20:e54792b89571 136 }
ThomasBGill 20:e54792b89571 137 }
ThomasBGill 20:e54792b89571 138
ThomasBGill 20:e54792b89571 139 void Border()
ThomasBGill 20:e54792b89571 140 {
ThomasBGill 20:e54792b89571 141
ThomasBGill 20:e54792b89571 142 for (int i = 0; i < 84; i++) {
ThomasBGill 20:e54792b89571 143 for (int j = 0; j < 48; j++) {
ThomasBGill 20:e54792b89571 144
ThomasBGill 20:e54792b89571 145 if (i == 0 || i == 83 || j == 0 || j == 47) {
ThomasBGill 20:e54792b89571 146
ThomasBGill 20:e54792b89571 147 map[i][j] = WALL;
ThomasBGill 20:e54792b89571 148
ThomasBGill 20:e54792b89571 149 }
ThomasBGill 20:e54792b89571 150 }
ThomasBGill 20:e54792b89571 151 }
ThomasBGill 20:e54792b89571 152 }
ThomasBGill 20:e54792b89571 153
ThomasBGill 20:e54792b89571 154 void RandFloor(int r)
ThomasBGill 20:e54792b89571 155 {
ThomasBGill 20:e54792b89571 156
ThomasBGill 20:e54792b89571 157 for (int space = rand() % 50 + r; space > 0; space--) {
ThomasBGill 20:e54792b89571 158
ThomasBGill 20:e54792b89571 159 int i = rand() % 84;
ThomasBGill 20:e54792b89571 160 int j = rand() % 48;
ThomasBGill 20:e54792b89571 161
ThomasBGill 20:e54792b89571 162 if (rand() % 2 == 0 && map[i][j] == WALL) {
ThomasBGill 20:e54792b89571 163 map[i][j] = FLOOR;
ThomasBGill 20:e54792b89571 164 }
ThomasBGill 20:e54792b89571 165 }
ThomasBGill 20:e54792b89571 166
ThomasBGill 20:e54792b89571 167 }
ThomasBGill 20:e54792b89571 168
ThomasBGill 20:e54792b89571 169 void MazeKill()
ThomasBGill 20:e54792b89571 170 {
ThomasBGill 20:e54792b89571 171
ThomasBGill 20:e54792b89571 172 int move[4] = { UP, DOWN, LEFT, RIGHT };
ThomasBGill 20:e54792b89571 173
ThomasBGill 20:e54792b89571 174 bool moved = true;
ThomasBGill 20:e54792b89571 175
ThomasBGill 20:e54792b89571 176 while (moved == true) {
ThomasBGill 20:e54792b89571 177
ThomasBGill 20:e54792b89571 178 moved = false;
ThomasBGill 20:e54792b89571 179
ThomasBGill 20:e54792b89571 180 for (int s = 0; s < 3; s++) { //Shuffle array
ThomasBGill 20:e54792b89571 181
ThomasBGill 20:e54792b89571 182 int r = rand() % 4;
ThomasBGill 20:e54792b89571 183
ThomasBGill 20:e54792b89571 184 int temp = move[s];
ThomasBGill 20:e54792b89571 185
ThomasBGill 20:e54792b89571 186 move[s] = move[r];
ThomasBGill 20:e54792b89571 187
ThomasBGill 20:e54792b89571 188 move[r] = temp;
ThomasBGill 20:e54792b89571 189 }
ThomasBGill 20:e54792b89571 190
ThomasBGill 20:e54792b89571 191
ThomasBGill 20:e54792b89571 192
ThomasBGill 20:e54792b89571 193 for (int i = 0; i < 3; i++) {
ThomasBGill 20:e54792b89571 194 if (move[i] == UP) {
ThomasBGill 20:e54792b89571 195 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 196 map[sx][sy - 1] = FLOOR;
ThomasBGill 20:e54792b89571 197 map[sx][sy - 2] = FLOOR;
ThomasBGill 20:e54792b89571 198 sy = sy - 2;
ThomasBGill 20:e54792b89571 199 moved = true;
ThomasBGill 20:e54792b89571 200 break;
ThomasBGill 20:e54792b89571 201 }
ThomasBGill 22:dae750e4d749 202 } else if (move[i] == DOWN) {
ThomasBGill 20:e54792b89571 203 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 204 map[sx][sy + 1] = FLOOR;
ThomasBGill 20:e54792b89571 205 map[sx][sy + 2] = FLOOR;
ThomasBGill 20:e54792b89571 206 sy = sy + 2;
ThomasBGill 20:e54792b89571 207 moved = true;
ThomasBGill 20:e54792b89571 208 break;
ThomasBGill 20:e54792b89571 209 }
ThomasBGill 22:dae750e4d749 210 } else if (move[i] == LEFT) {
ThomasBGill 20:e54792b89571 211 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 212 map[sx - 1][sy] = FLOOR;
ThomasBGill 20:e54792b89571 213 map[sx - 2][sy] = FLOOR;
ThomasBGill 20:e54792b89571 214 sx = sx - 2;
ThomasBGill 20:e54792b89571 215 moved = true;
ThomasBGill 20:e54792b89571 216 break;
ThomasBGill 20:e54792b89571 217 }
ThomasBGill 22:dae750e4d749 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 22:dae750e4d749 257 for (int i = 1; i < MAP_WIDTH - 1; i++) {
ThomasBGill 22:dae750e4d749 258 for (int j = 1; j < MAP_HEIGHT-1; 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 22:dae750e4d749 281 int rn = rand() % 15 + 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 22:dae750e4d749 289 RandFloor(31);
ThomasBGill 20:e54792b89571 290
ThomasBGill 20:e54792b89571 291 Border();
ThomasBGill 20:e54792b89571 292
ThomasBGill 22:dae750e4d749 293 DeadEnds(50);
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 24:4c4467971c91 318 if(level%5 == 0) {
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 }