Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: N5110 PowerControl mbed
main.cpp@4:6482ceb08dc8, 2015-04-17 (annotated)
- Committer:
- ThomasBGill
- Date:
- Fri Apr 17 00:56:02 2015 +0000
- Revision:
- 4:6482ceb08dc8
- Parent:
- 3:1a25939df22a
- Child:
- 5:9bd276652111
Dungeon creator finished
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ThomasBGill | 0:9200b3c387ed | 1 | #include "mbed.h" |
ThomasBGill | 0:9200b3c387ed | 2 | #include "N5110.h" |
ThomasBGill | 1:0f774d41584c | 3 | #include "PowerControl/PowerControl.h" |
ThomasBGill | 1:0f774d41584c | 4 | #include "PowerControl/EthernetPowerControl.h" |
ThomasBGill | 0:9200b3c387ed | 5 | |
ThomasBGill | 0:9200b3c387ed | 6 | // vcc sce rst dc mosi clk led |
ThomasBGill | 0:9200b3c387ed | 7 | N5110 lcd (p5, p6, p7, p8, p11, p13, p21); |
ThomasBGill | 0:9200b3c387ed | 8 | InterruptIn Act (p22); |
ThomasBGill | 0:9200b3c387ed | 9 | InterruptIn Start (p23); |
ThomasBGill | 0:9200b3c387ed | 10 | InterruptIn Up (p24); |
ThomasBGill | 0:9200b3c387ed | 11 | InterruptIn Down (p25); |
ThomasBGill | 0:9200b3c387ed | 12 | InterruptIn Left (p26); |
ThomasBGill | 0:9200b3c387ed | 13 | InterruptIn Right (p27); |
ThomasBGill | 0:9200b3c387ed | 14 | AnalogIn Noise (p19); |
ThomasBGill | 0:9200b3c387ed | 15 | |
ThomasBGill | 1:0f774d41584c | 16 | #define USR_POWERDOWN (0x104) |
ThomasBGill | 1:0f774d41584c | 17 | |
ThomasBGill | 0:9200b3c387ed | 18 | #define WALL 0 |
ThomasBGill | 0:9200b3c387ed | 19 | #define FLOOR 1 |
ThomasBGill | 0:9200b3c387ed | 20 | #define ENTER 2 |
ThomasBGill | 0:9200b3c387ed | 21 | #define EXIT 3 |
ThomasBGill | 0:9200b3c387ed | 22 | |
ThomasBGill | 0:9200b3c387ed | 23 | #define RIGHT 0 |
ThomasBGill | 0:9200b3c387ed | 24 | #define LEFT 1 |
ThomasBGill | 0:9200b3c387ed | 25 | #define UP 2 |
ThomasBGill | 0:9200b3c387ed | 26 | #define DOWN 3 |
ThomasBGill | 0:9200b3c387ed | 27 | |
ThomasBGill | 3:1a25939df22a | 28 | #define SOUTH 0 |
ThomasBGill | 3:1a25939df22a | 29 | #define EAST 1 |
ThomasBGill | 3:1a25939df22a | 30 | |
ThomasBGill | 0:9200b3c387ed | 31 | //Variables |
ThomasBGill | 0:9200b3c387ed | 32 | int buttonFlagA = 0; |
ThomasBGill | 0:9200b3c387ed | 33 | int buttonFlagS = 0; |
ThomasBGill | 0:9200b3c387ed | 34 | int buttonFlagU = 0; |
ThomasBGill | 0:9200b3c387ed | 35 | int buttonFlagD = 0; |
ThomasBGill | 0:9200b3c387ed | 36 | int buttonFlagL = 0; |
ThomasBGill | 0:9200b3c387ed | 37 | int buttonFlagR = 0; |
ThomasBGill | 0:9200b3c387ed | 38 | |
ThomasBGill | 0:9200b3c387ed | 39 | int map[84][48]; |
ThomasBGill | 0:9200b3c387ed | 40 | |
ThomasBGill | 2:0b0311609edc | 41 | //Enterance coordinates |
ThomasBGill | 2:0b0311609edc | 42 | int enx; |
ThomasBGill | 2:0b0311609edc | 43 | int eny; |
ThomasBGill | 2:0b0311609edc | 44 | |
ThomasBGill | 2:0b0311609edc | 45 | //Exit coordinates |
ThomasBGill | 2:0b0311609edc | 46 | int exx; |
ThomasBGill | 2:0b0311609edc | 47 | int exy; |
ThomasBGill | 2:0b0311609edc | 48 | |
ThomasBGill | 1:0f774d41584c | 49 | int sx; |
ThomasBGill | 1:0f774d41584c | 50 | int sy; |
ThomasBGill | 1:0f774d41584c | 51 | int dir; |
ThomasBGill | 1:0f774d41584c | 52 | |
ThomasBGill | 2:0b0311609edc | 53 | int n; |
ThomasBGill | 2:0b0311609edc | 54 | |
ThomasBGill | 2:0b0311609edc | 55 | |
ThomasBGill | 1:0f774d41584c | 56 | //Power Saving |
ThomasBGill | 1:0f774d41584c | 57 | int semihost_powerdown() |
ThomasBGill | 1:0f774d41584c | 58 | { |
ThomasBGill | 1:0f774d41584c | 59 | uint32_t arg; |
ThomasBGill | 1:0f774d41584c | 60 | return __semihost(USR_POWERDOWN, &arg); |
ThomasBGill | 1:0f774d41584c | 61 | } |
ThomasBGill | 1:0f774d41584c | 62 | |
ThomasBGill | 0:9200b3c387ed | 63 | //ISR |
ThomasBGill | 0:9200b3c387ed | 64 | void ActPressed() |
ThomasBGill | 0:9200b3c387ed | 65 | { |
ThomasBGill | 0:9200b3c387ed | 66 | buttonFlagA = 0; |
ThomasBGill | 0:9200b3c387ed | 67 | } |
ThomasBGill | 0:9200b3c387ed | 68 | void StartPressed() |
ThomasBGill | 0:9200b3c387ed | 69 | { |
ThomasBGill | 0:9200b3c387ed | 70 | buttonFlagS = 0; |
ThomasBGill | 0:9200b3c387ed | 71 | } |
ThomasBGill | 0:9200b3c387ed | 72 | void UpPressed() |
ThomasBGill | 0:9200b3c387ed | 73 | { |
ThomasBGill | 0:9200b3c387ed | 74 | buttonFlagU = 0; |
ThomasBGill | 0:9200b3c387ed | 75 | } |
ThomasBGill | 0:9200b3c387ed | 76 | void DownPressed() |
ThomasBGill | 0:9200b3c387ed | 77 | { |
ThomasBGill | 0:9200b3c387ed | 78 | buttonFlagD = 0; |
ThomasBGill | 0:9200b3c387ed | 79 | } |
ThomasBGill | 0:9200b3c387ed | 80 | void LeftPressed() |
ThomasBGill | 0:9200b3c387ed | 81 | { |
ThomasBGill | 0:9200b3c387ed | 82 | buttonFlagL = 0; |
ThomasBGill | 0:9200b3c387ed | 83 | } |
ThomasBGill | 0:9200b3c387ed | 84 | void RightPressed() |
ThomasBGill | 0:9200b3c387ed | 85 | { |
ThomasBGill | 0:9200b3c387ed | 86 | buttonFlagR = 0; |
ThomasBGill | 0:9200b3c387ed | 87 | } |
ThomasBGill | 0:9200b3c387ed | 88 | |
ThomasBGill | 0:9200b3c387ed | 89 | void Walls() |
ThomasBGill | 0:9200b3c387ed | 90 | { |
ThomasBGill | 0:9200b3c387ed | 91 | //Fill map with walls |
ThomasBGill | 0:9200b3c387ed | 92 | for(int i=0; i<84; i++) { |
ThomasBGill | 0:9200b3c387ed | 93 | for (int j=0; j<48; j++) { |
ThomasBGill | 3:1a25939df22a | 94 | |
ThomasBGill | 0:9200b3c387ed | 95 | map[i][j] = WALL; |
ThomasBGill | 0:9200b3c387ed | 96 | } |
ThomasBGill | 0:9200b3c387ed | 97 | } |
ThomasBGill | 0:9200b3c387ed | 98 | } |
ThomasBGill | 0:9200b3c387ed | 99 | |
ThomasBGill | 3:1a25939df22a | 100 | void Floors() |
ThomasBGill | 3:1a25939df22a | 101 | { |
ThomasBGill | 3:1a25939df22a | 102 | //Fill map with floors |
ThomasBGill | 3:1a25939df22a | 103 | for(int i=0; i<84; i++) { |
ThomasBGill | 3:1a25939df22a | 104 | for (int j=0; j<48; j++) { |
ThomasBGill | 3:1a25939df22a | 105 | map[i][j] = FLOOR; |
ThomasBGill | 3:1a25939df22a | 106 | } |
ThomasBGill | 3:1a25939df22a | 107 | } |
ThomasBGill | 3:1a25939df22a | 108 | } |
ThomasBGill | 3:1a25939df22a | 109 | |
ThomasBGill | 1:0f774d41584c | 110 | void DrawMap() |
ThomasBGill | 1:0f774d41584c | 111 | { |
ThomasBGill | 1:0f774d41584c | 112 | //Draw map on screen |
ThomasBGill | 1:0f774d41584c | 113 | for(int i=0; i<84; i++) { |
ThomasBGill | 1:0f774d41584c | 114 | for (int j=0; j<48; j++) { |
ThomasBGill | 1:0f774d41584c | 115 | if(map[i][j] == FLOOR) { |
ThomasBGill | 1:0f774d41584c | 116 | lcd.clearPixel(i, j); |
ThomasBGill | 1:0f774d41584c | 117 | } else { |
ThomasBGill | 1:0f774d41584c | 118 | lcd.setPixel(i, j); |
ThomasBGill | 1:0f774d41584c | 119 | } |
ThomasBGill | 1:0f774d41584c | 120 | } |
ThomasBGill | 1:0f774d41584c | 121 | } |
ThomasBGill | 1:0f774d41584c | 122 | lcd.refresh(); |
ThomasBGill | 1:0f774d41584c | 123 | } |
ThomasBGill | 1:0f774d41584c | 124 | |
ThomasBGill | 0:9200b3c387ed | 125 | void FirstRoom() |
ThomasBGill | 0:9200b3c387ed | 126 | { |
ThomasBGill | 0:9200b3c387ed | 127 | //Create initial room |
ThomasBGill | 1:0f774d41584c | 128 | int si = rand()%25 + 1; |
ThomasBGill | 1:0f774d41584c | 129 | int sj = rand()%15 + 1; |
ThomasBGill | 0:9200b3c387ed | 130 | |
ThomasBGill | 0:9200b3c387ed | 131 | int sw = rand()%5+5; |
ThomasBGill | 0:9200b3c387ed | 132 | int sh = rand()%5+5; |
ThomasBGill | 0:9200b3c387ed | 133 | |
ThomasBGill | 0:9200b3c387ed | 134 | for(int i = si, w = si + sw; i <w; i++) { |
ThomasBGill | 0:9200b3c387ed | 135 | for (int j = sj, h = sj + sh; j <h; j++) { |
ThomasBGill | 0:9200b3c387ed | 136 | map[i][j] = FLOOR; |
ThomasBGill | 0:9200b3c387ed | 137 | } |
ThomasBGill | 0:9200b3c387ed | 138 | } |
ThomasBGill | 0:9200b3c387ed | 139 | //Create enterance in room |
ThomasBGill | 2:0b0311609edc | 140 | enx = rand()%sw + si; |
ThomasBGill | 2:0b0311609edc | 141 | eny = rand()%sh + sj; |
ThomasBGill | 2:0b0311609edc | 142 | map[enx][eny] = ENTER; |
ThomasBGill | 2:0b0311609edc | 143 | } |
ThomasBGill | 2:0b0311609edc | 144 | |
ThomasBGill | 2:0b0311609edc | 145 | void SetExit() |
ThomasBGill | 2:0b0311609edc | 146 | { |
ThomasBGill | 2:0b0311609edc | 147 | do { |
ThomasBGill | 2:0b0311609edc | 148 | exx = enx + rand()%30 +15; |
ThomasBGill | 2:0b0311609edc | 149 | exy = eny + rand()%20 +15; |
ThomasBGill | 2:0b0311609edc | 150 | } while(map[exx][exy] == WALL && exx > 84 && exy > 48); |
ThomasBGill | 2:0b0311609edc | 151 | |
ThomasBGill | 2:0b0311609edc | 152 | map[exx][exy] = EXIT; |
ThomasBGill | 2:0b0311609edc | 153 | |
ThomasBGill | 0:9200b3c387ed | 154 | } |
ThomasBGill | 0:9200b3c387ed | 155 | |
ThomasBGill | 1:0f774d41584c | 156 | void MinePoint() |
ThomasBGill | 0:9200b3c387ed | 157 | { |
ThomasBGill | 0:9200b3c387ed | 158 | //Find start location |
ThomasBGill | 0:9200b3c387ed | 159 | for(int i=0; i<84; i++) { |
ThomasBGill | 0:9200b3c387ed | 160 | for (int j=0; j<48; j++) { |
ThomasBGill | 1:0f774d41584c | 161 | |
ThomasBGill | 0:9200b3c387ed | 162 | int r = rand()%6; |
ThomasBGill | 1:0f774d41584c | 163 | |
ThomasBGill | 0:9200b3c387ed | 164 | if(map[i][j] == FLOOR && map[i+1][j] == WALL && r == 0) { |
ThomasBGill | 0:9200b3c387ed | 165 | dir = RIGHT; |
ThomasBGill | 0:9200b3c387ed | 166 | sx = i; |
ThomasBGill | 0:9200b3c387ed | 167 | sy = j; |
ThomasBGill | 0:9200b3c387ed | 168 | break; |
ThomasBGill | 0:9200b3c387ed | 169 | } else if(map[i][j] == FLOOR && map[i-1][j] == WALL && r == 0) { |
ThomasBGill | 0:9200b3c387ed | 170 | dir = LEFT; |
ThomasBGill | 0:9200b3c387ed | 171 | sx = i; |
ThomasBGill | 0:9200b3c387ed | 172 | sy = j; |
ThomasBGill | 0:9200b3c387ed | 173 | break; |
ThomasBGill | 0:9200b3c387ed | 174 | } else if(map[i][j] == FLOOR && map[i][j+1] == WALL && r == 0) { |
ThomasBGill | 0:9200b3c387ed | 175 | dir = UP; |
ThomasBGill | 0:9200b3c387ed | 176 | sx = i; |
ThomasBGill | 0:9200b3c387ed | 177 | sy = j; |
ThomasBGill | 0:9200b3c387ed | 178 | break; |
ThomasBGill | 0:9200b3c387ed | 179 | } else if(map[i][j] == FLOOR && map[i][j-1] == WALL && r == 0) { |
ThomasBGill | 0:9200b3c387ed | 180 | dir = DOWN; |
ThomasBGill | 0:9200b3c387ed | 181 | sx = i; |
ThomasBGill | 0:9200b3c387ed | 182 | sy = j; |
ThomasBGill | 0:9200b3c387ed | 183 | break; |
ThomasBGill | 0:9200b3c387ed | 184 | } |
ThomasBGill | 0:9200b3c387ed | 185 | } |
ThomasBGill | 0:9200b3c387ed | 186 | } |
ThomasBGill | 1:0f774d41584c | 187 | } |
ThomasBGill | 1:0f774d41584c | 188 | |
ThomasBGill | 1:0f774d41584c | 189 | void MineCorridorBuilder() |
ThomasBGill | 1:0f774d41584c | 190 | { |
ThomasBGill | 1:0f774d41584c | 191 | MinePoint(); |
ThomasBGill | 1:0f774d41584c | 192 | |
ThomasBGill | 0:9200b3c387ed | 193 | //Get length |
ThomasBGill | 0:9200b3c387ed | 194 | int l = rand()%5 + 5; |
ThomasBGill | 0:9200b3c387ed | 195 | |
ThomasBGill | 0:9200b3c387ed | 196 | //Check direction of corridor |
ThomasBGill | 0:9200b3c387ed | 197 | if(dir == RIGHT) { |
ThomasBGill | 0:9200b3c387ed | 198 | for(int i = l; i>0; i--) { |
ThomasBGill | 1:0f774d41584c | 199 | if(map[sx+1][sy] == WALL && map[sx+1][sy+1] == WALL && map[sx+1][sy-1] == WALL && sx < 83) { |
ThomasBGill | 0:9200b3c387ed | 200 | sx++; |
ThomasBGill | 0:9200b3c387ed | 201 | map[sx][sy] = FLOOR; |
ThomasBGill | 0:9200b3c387ed | 202 | } else |
ThomasBGill | 0:9200b3c387ed | 203 | break; |
ThomasBGill | 0:9200b3c387ed | 204 | } |
ThomasBGill | 0:9200b3c387ed | 205 | } else if(dir == LEFT) { |
ThomasBGill | 0:9200b3c387ed | 206 | for(int i = l; i>0; i--) { |
ThomasBGill | 1:0f774d41584c | 207 | if(map[sx-1][sy] == WALL && map[sx-1][sy+1] == WALL && map[sx-1][sy-1] == WALL && sx > 1) { |
ThomasBGill | 0:9200b3c387ed | 208 | sx--; |
ThomasBGill | 0:9200b3c387ed | 209 | map[sx][sy] = FLOOR; |
ThomasBGill | 1:0f774d41584c | 210 | } else |
ThomasBGill | 0:9200b3c387ed | 211 | break; |
ThomasBGill | 0:9200b3c387ed | 212 | } |
ThomasBGill | 0:9200b3c387ed | 213 | } else if(dir == UP) { |
ThomasBGill | 1:0f774d41584c | 214 | for(int i = l; i>0; i--) { |
ThomasBGill | 1:0f774d41584c | 215 | if(map[sx][sy+1] == WALL && map[sx-1][sy+1] == WALL && map[sx+1][sy+1] == WALL && sy < 47) { |
ThomasBGill | 1:0f774d41584c | 216 | sy++; |
ThomasBGill | 1:0f774d41584c | 217 | map[sx][sy] = FLOOR; |
ThomasBGill | 1:0f774d41584c | 218 | } else |
ThomasBGill | 0:9200b3c387ed | 219 | break; |
ThomasBGill | 0:9200b3c387ed | 220 | } |
ThomasBGill | 0:9200b3c387ed | 221 | } else if(dir == DOWN) { |
ThomasBGill | 1:0f774d41584c | 222 | for(int i = l; i>0; i--) { |
ThomasBGill | 1:0f774d41584c | 223 | if(map[sx][sy-1] == WALL && map[sx-1][sy-1] == WALL && map[sx+1][sy-1] == WALL && sy > 1) { |
ThomasBGill | 1:0f774d41584c | 224 | sy--; |
ThomasBGill | 1:0f774d41584c | 225 | map[sx][sy] = FLOOR; |
ThomasBGill | 1:0f774d41584c | 226 | } else |
ThomasBGill | 0:9200b3c387ed | 227 | break; |
ThomasBGill | 0:9200b3c387ed | 228 | } |
ThomasBGill | 0:9200b3c387ed | 229 | } |
ThomasBGill | 0:9200b3c387ed | 230 | } |
ThomasBGill | 0:9200b3c387ed | 231 | |
ThomasBGill | 1:0f774d41584c | 232 | void MineRoomBuilder() |
ThomasBGill | 1:0f774d41584c | 233 | { |
ThomasBGill | 1:0f774d41584c | 234 | MinePoint(); |
ThomasBGill | 2:0b0311609edc | 235 | |
ThomasBGill | 1:0f774d41584c | 236 | //Get length |
ThomasBGill | 1:0f774d41584c | 237 | int sw = rand()%5 + 5; |
ThomasBGill | 1:0f774d41584c | 238 | int sh = rand()%5 + 5; |
ThomasBGill | 0:9200b3c387ed | 239 | |
ThomasBGill | 1:0f774d41584c | 240 | int b = 0; |
ThomasBGill | 1:0f774d41584c | 241 | |
ThomasBGill | 1:0f774d41584c | 242 | if(dir == RIGHT) { |
ThomasBGill | 1:0f774d41584c | 243 | sx++; |
ThomasBGill | 1:0f774d41584c | 244 | //Check each space. +1 to variable if wall. If total = w*h then build room |
ThomasBGill | 1:0f774d41584c | 245 | for(int i = sx; i < sx + sw; i++) { |
ThomasBGill | 1:0f774d41584c | 246 | for(int j = sy; j < sy + sh; j++) { |
ThomasBGill | 1:0f774d41584c | 247 | if(map[i][j] == WALL) { |
ThomasBGill | 1:0f774d41584c | 248 | b++; |
ThomasBGill | 1:0f774d41584c | 249 | } |
ThomasBGill | 1:0f774d41584c | 250 | } |
ThomasBGill | 1:0f774d41584c | 251 | } |
ThomasBGill | 1:0f774d41584c | 252 | if(b == sw*sh) { |
ThomasBGill | 1:0f774d41584c | 253 | for(int i = sx; i < sx + sw; i++) { |
ThomasBGill | 1:0f774d41584c | 254 | for(int j = sy; j < sy + sh; j++) { |
ThomasBGill | 2:0b0311609edc | 255 | if(i < 84 && j < 48) { |
ThomasBGill | 2:0b0311609edc | 256 | map[i][j] = FLOOR; |
ThomasBGill | 2:0b0311609edc | 257 | } |
ThomasBGill | 1:0f774d41584c | 258 | } |
ThomasBGill | 1:0f774d41584c | 259 | } |
ThomasBGill | 1:0f774d41584c | 260 | } |
ThomasBGill | 1:0f774d41584c | 261 | } |
ThomasBGill | 1:0f774d41584c | 262 | if(dir == LEFT) { |
ThomasBGill | 1:0f774d41584c | 263 | sx--; |
ThomasBGill | 1:0f774d41584c | 264 | //Check each space. +1 to variable if wall. If total = w*h then build room |
ThomasBGill | 1:0f774d41584c | 265 | for(int i = sx; i > sx - sw; i--) { |
ThomasBGill | 1:0f774d41584c | 266 | for(int j = sy; j < sy + sh; j++) { |
ThomasBGill | 1:0f774d41584c | 267 | if(map[i][j] == WALL) { |
ThomasBGill | 1:0f774d41584c | 268 | b++; |
ThomasBGill | 1:0f774d41584c | 269 | } |
ThomasBGill | 1:0f774d41584c | 270 | } |
ThomasBGill | 1:0f774d41584c | 271 | } |
ThomasBGill | 1:0f774d41584c | 272 | if(b == sw*sh) { |
ThomasBGill | 1:0f774d41584c | 273 | for(int i = sx; i > sx - sw; i--) { |
ThomasBGill | 1:0f774d41584c | 274 | for(int j = sy; j < sy + sh; j++) { |
ThomasBGill | 2:0b0311609edc | 275 | if(i < 84 && j < 48) { |
ThomasBGill | 2:0b0311609edc | 276 | map[i][j] = FLOOR; |
ThomasBGill | 2:0b0311609edc | 277 | } |
ThomasBGill | 1:0f774d41584c | 278 | } |
ThomasBGill | 1:0f774d41584c | 279 | } |
ThomasBGill | 1:0f774d41584c | 280 | } |
ThomasBGill | 1:0f774d41584c | 281 | } |
ThomasBGill | 1:0f774d41584c | 282 | if(dir == UP) { |
ThomasBGill | 1:0f774d41584c | 283 | sy++; |
ThomasBGill | 1:0f774d41584c | 284 | //Check each space. +1 to variable if wall. If total = w*h then build room |
ThomasBGill | 1:0f774d41584c | 285 | for(int i = sx; i < sx + sw; i++) { |
ThomasBGill | 1:0f774d41584c | 286 | for(int j = sy; j < sy + sh; j++) { |
ThomasBGill | 1:0f774d41584c | 287 | if(map[i][j] == WALL) { |
ThomasBGill | 1:0f774d41584c | 288 | b++; |
ThomasBGill | 1:0f774d41584c | 289 | } |
ThomasBGill | 1:0f774d41584c | 290 | } |
ThomasBGill | 1:0f774d41584c | 291 | } |
ThomasBGill | 1:0f774d41584c | 292 | if(b == sw*sh) { |
ThomasBGill | 1:0f774d41584c | 293 | for(int i = sx; i < sx + sw; i++) { |
ThomasBGill | 1:0f774d41584c | 294 | for(int j = sy; j < sy + sh; j++) { |
ThomasBGill | 2:0b0311609edc | 295 | if(i < 84 && j < 48) { |
ThomasBGill | 2:0b0311609edc | 296 | map[i][j] = FLOOR; |
ThomasBGill | 2:0b0311609edc | 297 | } |
ThomasBGill | 1:0f774d41584c | 298 | } |
ThomasBGill | 1:0f774d41584c | 299 | } |
ThomasBGill | 1:0f774d41584c | 300 | } |
ThomasBGill | 1:0f774d41584c | 301 | } |
ThomasBGill | 1:0f774d41584c | 302 | if(dir == DOWN) { |
ThomasBGill | 1:0f774d41584c | 303 | sy--; |
ThomasBGill | 1:0f774d41584c | 304 | //Check each space. +1 to variable if wall. If total = w*h then build room |
ThomasBGill | 1:0f774d41584c | 305 | for(int i = sx; i < sx + sw; i++) { |
ThomasBGill | 1:0f774d41584c | 306 | for(int j = sy; j > sy - sh; j--) { |
ThomasBGill | 1:0f774d41584c | 307 | if(map[i][j] == WALL) { |
ThomasBGill | 1:0f774d41584c | 308 | b++; |
ThomasBGill | 1:0f774d41584c | 309 | } |
ThomasBGill | 1:0f774d41584c | 310 | } |
ThomasBGill | 1:0f774d41584c | 311 | } |
ThomasBGill | 1:0f774d41584c | 312 | if(b == sw*sh) { |
ThomasBGill | 1:0f774d41584c | 313 | for(int i = sx; i < sx + sw; i++) { |
ThomasBGill | 1:0f774d41584c | 314 | for(int j = sy; j > sy - sh; j--) { |
ThomasBGill | 2:0b0311609edc | 315 | if(i < 84 && j < 48) { |
ThomasBGill | 2:0b0311609edc | 316 | map[i][j] = FLOOR; |
ThomasBGill | 2:0b0311609edc | 317 | } |
ThomasBGill | 2:0b0311609edc | 318 | } |
ThomasBGill | 2:0b0311609edc | 319 | } |
ThomasBGill | 2:0b0311609edc | 320 | } |
ThomasBGill | 2:0b0311609edc | 321 | } |
ThomasBGill | 2:0b0311609edc | 322 | } |
ThomasBGill | 2:0b0311609edc | 323 | |
ThomasBGill | 2:0b0311609edc | 324 | void DungeonRoomBuilder() |
ThomasBGill | 2:0b0311609edc | 325 | { |
ThomasBGill | 2:0b0311609edc | 326 | sx = rand()%84; |
ThomasBGill | 2:0b0311609edc | 327 | sy = rand()%48; |
ThomasBGill | 2:0b0311609edc | 328 | |
ThomasBGill | 2:0b0311609edc | 329 | //Get length |
ThomasBGill | 2:0b0311609edc | 330 | int sw = rand()%5 + 5; |
ThomasBGill | 2:0b0311609edc | 331 | int sh = rand()%5 + 5; |
ThomasBGill | 2:0b0311609edc | 332 | |
ThomasBGill | 2:0b0311609edc | 333 | int b = 0; |
ThomasBGill | 2:0b0311609edc | 334 | |
ThomasBGill | 2:0b0311609edc | 335 | //Check each space. +1 to variable if wall. If total = w*h then build room |
ThomasBGill | 2:0b0311609edc | 336 | for(int i = sx; i < sx + sw; i++) { |
ThomasBGill | 2:0b0311609edc | 337 | for(int j = sy; j < sy + sh; j++) { |
ThomasBGill | 2:0b0311609edc | 338 | if(map[i][j] == WALL) { |
ThomasBGill | 2:0b0311609edc | 339 | b++; |
ThomasBGill | 2:0b0311609edc | 340 | } |
ThomasBGill | 2:0b0311609edc | 341 | } |
ThomasBGill | 2:0b0311609edc | 342 | } |
ThomasBGill | 2:0b0311609edc | 343 | if(b == sw*sh) { |
ThomasBGill | 2:0b0311609edc | 344 | for(int i = sx; i < sx + sw; i++) { |
ThomasBGill | 2:0b0311609edc | 345 | for(int j = sy; j < sy + sh; j++) { |
ThomasBGill | 2:0b0311609edc | 346 | if(i < 84 && j < 48) { |
ThomasBGill | 1:0f774d41584c | 347 | map[i][j] = FLOOR; |
ThomasBGill | 1:0f774d41584c | 348 | } |
ThomasBGill | 1:0f774d41584c | 349 | } |
ThomasBGill | 1:0f774d41584c | 350 | } |
ThomasBGill | 1:0f774d41584c | 351 | } |
ThomasBGill | 1:0f774d41584c | 352 | } |
ThomasBGill | 1:0f774d41584c | 353 | |
ThomasBGill | 4:6482ceb08dc8 | 354 | void Neighbours() |
ThomasBGill | 4:6482ceb08dc8 | 355 | { |
ThomasBGill | 4:6482ceb08dc8 | 356 | |
ThomasBGill | 4:6482ceb08dc8 | 357 | //Check neighbours |
ThomasBGill | 4:6482ceb08dc8 | 358 | n = 0; |
ThomasBGill | 4:6482ceb08dc8 | 359 | |
ThomasBGill | 4:6482ceb08dc8 | 360 | if(map[sx+1][sy] == FLOOR) { |
ThomasBGill | 4:6482ceb08dc8 | 361 | n++; |
ThomasBGill | 4:6482ceb08dc8 | 362 | } |
ThomasBGill | 4:6482ceb08dc8 | 363 | if(map[sx-1][sy] == FLOOR) { |
ThomasBGill | 4:6482ceb08dc8 | 364 | n++; |
ThomasBGill | 4:6482ceb08dc8 | 365 | } |
ThomasBGill | 4:6482ceb08dc8 | 366 | if(map[sx][sy+1] == FLOOR) { |
ThomasBGill | 4:6482ceb08dc8 | 367 | n++; |
ThomasBGill | 4:6482ceb08dc8 | 368 | } |
ThomasBGill | 4:6482ceb08dc8 | 369 | if(map[sx][sy-1] == FLOOR) { |
ThomasBGill | 4:6482ceb08dc8 | 370 | n++; |
ThomasBGill | 4:6482ceb08dc8 | 371 | } |
ThomasBGill | 4:6482ceb08dc8 | 372 | } |
ThomasBGill | 4:6482ceb08dc8 | 373 | |
ThomasBGill | 4:6482ceb08dc8 | 374 | void DeadEnds() |
ThomasBGill | 4:6482ceb08dc8 | 375 | { |
ThomasBGill | 4:6482ceb08dc8 | 376 | for (int del = rand()%5 + 1; del > 0; del--) { |
ThomasBGill | 4:6482ceb08dc8 | 377 | for(int i = 0; i < 84; i++) { |
ThomasBGill | 4:6482ceb08dc8 | 378 | for(int j = 0; j < 48; j++) { |
ThomasBGill | 4:6482ceb08dc8 | 379 | |
ThomasBGill | 4:6482ceb08dc8 | 380 | sx = i; |
ThomasBGill | 4:6482ceb08dc8 | 381 | sy = j; |
ThomasBGill | 4:6482ceb08dc8 | 382 | |
ThomasBGill | 4:6482ceb08dc8 | 383 | Neighbours(); |
ThomasBGill | 4:6482ceb08dc8 | 384 | |
ThomasBGill | 4:6482ceb08dc8 | 385 | if(n == 1) { |
ThomasBGill | 4:6482ceb08dc8 | 386 | map[i][j] = WALL; |
ThomasBGill | 4:6482ceb08dc8 | 387 | } |
ThomasBGill | 4:6482ceb08dc8 | 388 | } |
ThomasBGill | 4:6482ceb08dc8 | 389 | } |
ThomasBGill | 4:6482ceb08dc8 | 390 | } |
ThomasBGill | 4:6482ceb08dc8 | 391 | } |
ThomasBGill | 4:6482ceb08dc8 | 392 | |
ThomasBGill | 4:6482ceb08dc8 | 393 | |
ThomasBGill | 2:0b0311609edc | 394 | void Maze() |
ThomasBGill | 2:0b0311609edc | 395 | { |
ThomasBGill | 3:1a25939df22a | 396 | for(int i = 0; i < 84; i+=2) { |
ThomasBGill | 3:1a25939df22a | 397 | for(int j = 0; j < 48; j+=2) { |
ThomasBGill | 2:0b0311609edc | 398 | |
ThomasBGill | 3:1a25939df22a | 399 | map[i][j] = FLOOR; |
ThomasBGill | 2:0b0311609edc | 400 | |
ThomasBGill | 3:1a25939df22a | 401 | dir = rand()%2; //South or east |
ThomasBGill | 2:0b0311609edc | 402 | |
ThomasBGill | 4:6482ceb08dc8 | 403 | |
ThomasBGill | 3:1a25939df22a | 404 | if(dir == SOUTH && j < 47) { |
ThomasBGill | 3:1a25939df22a | 405 | map[i][j+1] = FLOOR; |
ThomasBGill | 2:0b0311609edc | 406 | } |
ThomasBGill | 3:1a25939df22a | 407 | if(dir == EAST && i < 84) { |
ThomasBGill | 3:1a25939df22a | 408 | map[i+1][j] = FLOOR; |
ThomasBGill | 2:0b0311609edc | 409 | } |
ThomasBGill | 2:0b0311609edc | 410 | } |
ThomasBGill | 2:0b0311609edc | 411 | } |
ThomasBGill | 3:1a25939df22a | 412 | |
ThomasBGill | 4:6482ceb08dc8 | 413 | for(int space = rand()%50 + 51; space > 0; space--) { |
ThomasBGill | 3:1a25939df22a | 414 | |
ThomasBGill | 3:1a25939df22a | 415 | int i = rand()% 84; |
ThomasBGill | 3:1a25939df22a | 416 | int j = rand()% 48; |
ThomasBGill | 3:1a25939df22a | 417 | |
ThomasBGill | 3:1a25939df22a | 418 | if(rand()%2 == 0) { |
ThomasBGill | 3:1a25939df22a | 419 | map[i][j] = FLOOR; |
ThomasBGill | 3:1a25939df22a | 420 | } |
ThomasBGill | 3:1a25939df22a | 421 | } |
ThomasBGill | 4:6482ceb08dc8 | 422 | |
ThomasBGill | 4:6482ceb08dc8 | 423 | DeadEnds(); |
ThomasBGill | 3:1a25939df22a | 424 | |
ThomasBGill | 2:0b0311609edc | 425 | } |
ThomasBGill | 2:0b0311609edc | 426 | |
ThomasBGill | 1:0f774d41584c | 427 | void MineBuilder() |
ThomasBGill | 0:9200b3c387ed | 428 | { |
ThomasBGill | 2:0b0311609edc | 429 | Walls(); |
ThomasBGill | 2:0b0311609edc | 430 | |
ThomasBGill | 0:9200b3c387ed | 431 | FirstRoom(); |
ThomasBGill | 0:9200b3c387ed | 432 | |
ThomasBGill | 1:0f774d41584c | 433 | int fn = rand()%20 + 20; |
ThomasBGill | 0:9200b3c387ed | 434 | |
ThomasBGill | 1:0f774d41584c | 435 | for(int i = fn; i>0; i--) { |
ThomasBGill | 1:0f774d41584c | 436 | int f = rand()% 5; |
ThomasBGill | 1:0f774d41584c | 437 | if(f == 0) { |
ThomasBGill | 1:0f774d41584c | 438 | MineRoomBuilder(); |
ThomasBGill | 1:0f774d41584c | 439 | } else { |
ThomasBGill | 1:0f774d41584c | 440 | MineCorridorBuilder(); |
ThomasBGill | 0:9200b3c387ed | 441 | } |
ThomasBGill | 0:9200b3c387ed | 442 | } |
ThomasBGill | 2:0b0311609edc | 443 | |
ThomasBGill | 2:0b0311609edc | 444 | SetExit(); |
ThomasBGill | 0:9200b3c387ed | 445 | } |
ThomasBGill | 0:9200b3c387ed | 446 | |
ThomasBGill | 2:0b0311609edc | 447 | void DungeonBuilder() |
ThomasBGill | 0:9200b3c387ed | 448 | { |
ThomasBGill | 1:0f774d41584c | 449 | |
ThomasBGill | 4:6482ceb08dc8 | 450 | FirstRoom(); |
ThomasBGill | 4:6482ceb08dc8 | 451 | |
ThomasBGill | 2:0b0311609edc | 452 | int rn = rand()%20 + 20; |
ThomasBGill | 2:0b0311609edc | 453 | |
ThomasBGill | 2:0b0311609edc | 454 | for(int i = rn; i>0; i--) { |
ThomasBGill | 2:0b0311609edc | 455 | DungeonRoomBuilder(); |
ThomasBGill | 2:0b0311609edc | 456 | } |
ThomasBGill | 2:0b0311609edc | 457 | |
ThomasBGill | 4:6482ceb08dc8 | 458 | SetExit(); |
ThomasBGill | 4:6482ceb08dc8 | 459 | |
ThomasBGill | 3:1a25939df22a | 460 | DrawMap(); |
ThomasBGill | 4:6482ceb08dc8 | 461 | wait(2.0); |
ThomasBGill | 2:0b0311609edc | 462 | |
ThomasBGill | 3:1a25939df22a | 463 | Maze(); |
ThomasBGill | 3:1a25939df22a | 464 | |
ThomasBGill | 2:0b0311609edc | 465 | } |
ThomasBGill | 2:0b0311609edc | 466 | |
ThomasBGill | 2:0b0311609edc | 467 | void World() |
ThomasBGill | 2:0b0311609edc | 468 | { |
ThomasBGill | 2:0b0311609edc | 469 | Walls(); |
ThomasBGill | 3:1a25939df22a | 470 | //MineBuilder(); |
ThomasBGill | 3:1a25939df22a | 471 | DungeonBuilder(); |
ThomasBGill | 0:9200b3c387ed | 472 | DrawMap(); |
ThomasBGill | 1:0f774d41584c | 473 | |
ThomasBGill | 0:9200b3c387ed | 474 | } |
ThomasBGill | 0:9200b3c387ed | 475 | |
ThomasBGill | 0:9200b3c387ed | 476 | int main() |
ThomasBGill | 0:9200b3c387ed | 477 | { |
ThomasBGill | 1:0f774d41584c | 478 | //Power Saving |
ThomasBGill | 1:0f774d41584c | 479 | PHY_PowerDown (); |
ThomasBGill | 1:0f774d41584c | 480 | int result = semihost_powerdown(); |
ThomasBGill | 1:0f774d41584c | 481 | |
ThomasBGill | 1:0f774d41584c | 482 | //Generate random seed |
ThomasBGill | 0:9200b3c387ed | 483 | srand(Noise*1000000); |
ThomasBGill | 1:0f774d41584c | 484 | |
ThomasBGill | 1:0f774d41584c | 485 | //Initilize screen |
ThomasBGill | 0:9200b3c387ed | 486 | lcd.init(); |
ThomasBGill | 0:9200b3c387ed | 487 | |
ThomasBGill | 1:0f774d41584c | 488 | //Game loop |
ThomasBGill | 0:9200b3c387ed | 489 | while(1) { |
ThomasBGill | 3:1a25939df22a | 490 | World(); |
ThomasBGill | 4:6482ceb08dc8 | 491 | wait(4.0); |
ThomasBGill | 0:9200b3c387ed | 492 | } |
ThomasBGill | 0:9200b3c387ed | 493 | } |