
Labyrinth of the Minotaur A simple roguelike/RPG using a nokia 5110 screen
Dependencies: N5110 PowerControl mbed
Diff: WorldBuilder/WorldBuilder.cpp
- Revision:
- 20:e54792b89571
- Child:
- 21:aa4feee6aa39
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WorldBuilder/WorldBuilder.cpp Mon May 04 16:26:43 2015 +0000 @@ -0,0 +1,309 @@ +#include "WorldBuilder.h" + +int map[84][48]; + +//Enterance coordinates +int enx; +int eny; + +//Exit coordinates +int exx; +int exy; + +int sx; +int sy; +int dir; + +int level; + +void Walls() +{ + //Fill map with walls + for (int i = 0; i<84; i++) { + for (int j = 0; j<48; j++) { + + map[i][j] = WALL; + } + } +} + +void FirstRoom() +{ + //Create initial room + int si = rand() % 25 + 1; + int sj = rand() % 15 + 1; + + int sw = rand() % 5 + 5; + int sh = rand() % 5 + 5; + + for (int i = si, w = si + sw; i <w; i++) { + for (int j = sj, h = sj + sh; j <h; j++) { + map[i][j] = FLOOR; + } + } + //Create enterance in room + enx = rand() % sw + si; + eny = rand() % sh + sj; + map[enx][eny] = ENTER; +} + +void ExitRoom() +{ + //Create exit room + int si = rand() % 50 + 30; + int sj = rand() % 25 + 20; + + int sw = rand() % 5 + 5; + int sh = rand() % 5 + 5; + + for (int i = si, w = si + sw; i <w; i++) { + for (int j = sj, h = sj + sh; j <h; j++) { + map[i][j] = FLOOR; + } + } + //Create exit in room + exx = rand() % sw + si; + exy = rand() % sh + sj; + map[exx][exy] = EXIT; + +} + +void DungeonRoomBuilder() +{ + sx = rand() % 84; + sy = rand() % 48; + + //Get length + int sw = rand() % 5 + 5; + int sh = rand() % 5 + 5; + + for (int i = sx; i < sx + sw; i++) { + for (int j = sy; j < sy + sh; j++) { + if (i < 83 && j < 47) { + if (map[i][j] == WALL) { + map[i][j] = FLOOR; + } + } + } + } + + if (rand() % 3 == 0) { + int i = rand() % sw + sx; + int j = rand() % sh + sy; + map[i][j] = CHEST; + } +} + +int Neighbours(int i, int j) +{ + //Check neighbours + int n = 0; + + if (map[i + 1][j] == FLOOR) { + n++; + } + if (map[i - 1][j] == FLOOR) { + n++; + } + if (map[i][j + 1] == FLOOR) { + n++; + } + if (map[i][j - 1] == FLOOR) { + n++; + } + + return n; +} + +void DeadEnds(int d) +{ + for (int del = d; del > 0; del--) { + for (int i = 0; i < 84; i++) { + for (int j = 0; j < 48; j++) { + + if (Neighbours(i, j) < 2) { + map[i][j] = WALL; + } + } + } + } +} + +void Border() +{ + + for (int i = 0; i < 84; i++) { + for (int j = 0; j < 48; j++) { + + if (i == 0 || i == 83 || j == 0 || j == 47) { + + map[i][j] = WALL; + + } + } + } +} + +void RandFloor(int r) +{ + + for (int space = rand() % 50 + r; space > 0; space--) { + + int i = rand() % 84; + int j = rand() % 48; + + if (rand() % 2 == 0 && map[i][j] == WALL) { + map[i][j] = FLOOR; + } + } + +} + +void MazeKill() +{ + + int move[4] = { UP, DOWN, LEFT, RIGHT }; + + bool moved = true; + + while (moved == true) { + + moved = false; + + for (int s = 0; s < 3; s++) { //Shuffle array + + int r = rand() % 4; + + int temp = move[s]; + + move[s] = move[r]; + + move[r] = temp; + } + + + + for (int i = 0; i < 3; i++) { + if (move[i] == UP) { + if (map[sx][sy - 1] == WALL && Neighbours(sx, sy - 1) == 1 && map[sx][sy - 2] == WALL && Neighbours(sx, sy - 2) == 0 && sy > 3) { + map[sx][sy - 1] = FLOOR; + map[sx][sy - 2] = FLOOR; + sy = sy - 2; + moved = true; + break; + } + } + else if (move[i] == DOWN) { + if (map[sx][sy + 1] == WALL && Neighbours(sx, sy + 1) == 1 && map[sx][sy + 2] == WALL && Neighbours(sx, sy + 2) == 0 && sy < 45) { + map[sx][sy + 1] = FLOOR; + map[sx][sy + 2] = FLOOR; + sy = sy + 2; + moved = true; + break; + } + } + else if (move[i] == LEFT) { + if (map[sx - 1][sy] == WALL && Neighbours(sx - 1, sy) == 1 && map[sx - 2][sy] == WALL && Neighbours(sx - 2, sy) == 0 && sx > 3) { + map[sx - 1][sy] = FLOOR; + map[sx - 2][sy] = FLOOR; + sx = sx - 2; + moved = true; + break; + } + } + else if (move[i] == RIGHT) { + if (map[sx + 1][sy] == WALL && Neighbours(sx + 1, sy) == 1 && map[sx + 2][sy] == WALL && Neighbours(sx + 2, sy) == 0 && sx < 81) { + map[sx + 1][sy] = FLOOR; + map[sx + 2][sy] = FLOOR; + sx = sx + 2; + moved = true; + break; + } + } + } + } + +} + +void Maze() +{ + sx = 1; + sy = 1; + + //Choose random direction + //Check if 2 cells in direction have no neighbours (excluding current position) + //If true then build and set new current position + //If false chose next direction + + //If cannot move in any direction scan through each cell until there is one which can be built on + //If scan completes END + + int end = false; + + while (end == false) { + + end = true; + + map[sx][sy] = FLOOR; + + MazeKill(); + + //DrawMap(); + + for (int i = 1; i < 83; i++) { + for (int j = 1; j < 47; j++) { + + if (map[i][j] == WALL && Neighbours(i, j) == 1) { + sx = i; + sy = j; + + end = false; + } + + } + } + + } +} + +void DungeonBuilder() +{ + + Maze(); + + FirstRoom(); + ExitRoom(); + + int rn = rand() % 10 + 6; + + for (int i = rn; i>0; i--) { + DungeonRoomBuilder(); + } + + + + RandFloor(51); + + Border(); + + DeadEnds(20); + +} + +void LabyrinthBuilder() +{ + + Maze(); + + FirstRoom(); + ExitRoom(); + + + + RandFloor(151); + + DeadEnds(1); + + Border(); + +} \ No newline at end of file