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@5:9bd276652111, 2015-04-17 (annotated)
- Committer:
- ThomasBGill
- Date:
- Fri Apr 17 01:20:18 2015 +0000
- Revision:
- 5:9bd276652111
- Parent:
- 4:6482ceb08dc8
- Child:
- 6:ca5db4353c95
Dead end value revised
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 | 5:9bd276652111 | 145 | void ExitRoom() |
ThomasBGill | 5:9bd276652111 | 146 | { |
ThomasBGill | 5:9bd276652111 | 147 | //Create initial room |
ThomasBGill | 5:9bd276652111 | 148 | int si = rand()%50 + 30; |
ThomasBGill | 5:9bd276652111 | 149 | int sj = rand()%25 + 20; |
ThomasBGill | 5:9bd276652111 | 150 | |
ThomasBGill | 5:9bd276652111 | 151 | int sw = rand()%5+5; |
ThomasBGill | 5:9bd276652111 | 152 | int sh = rand()%5+5; |
ThomasBGill | 5:9bd276652111 | 153 | |
ThomasBGill | 5:9bd276652111 | 154 | for(int i = si, w = si + sw; i <w; i++) { |
ThomasBGill | 5:9bd276652111 | 155 | for (int j = sj, h = sj + sh; j <h; j++) { |
ThomasBGill | 5:9bd276652111 | 156 | map[i][j] = FLOOR; |
ThomasBGill | 5:9bd276652111 | 157 | } |
ThomasBGill | 5:9bd276652111 | 158 | } |
ThomasBGill | 5:9bd276652111 | 159 | //Create enterance in room |
ThomasBGill | 5:9bd276652111 | 160 | enx = rand()%sw + si; |
ThomasBGill | 5:9bd276652111 | 161 | eny = rand()%sh + sj; |
ThomasBGill | 5:9bd276652111 | 162 | map[enx][eny] = EXIT; |
ThomasBGill | 5:9bd276652111 | 163 | } |
ThomasBGill | 5:9bd276652111 | 164 | |
ThomasBGill | 2:0b0311609edc | 165 | void SetExit() |
ThomasBGill | 2:0b0311609edc | 166 | { |
ThomasBGill | 2:0b0311609edc | 167 | do { |
ThomasBGill | 2:0b0311609edc | 168 | exx = enx + rand()%30 +15; |
ThomasBGill | 2:0b0311609edc | 169 | exy = eny + rand()%20 +15; |
ThomasBGill | 2:0b0311609edc | 170 | } while(map[exx][exy] == WALL && exx > 84 && exy > 48); |
ThomasBGill | 2:0b0311609edc | 171 | |
ThomasBGill | 2:0b0311609edc | 172 | map[exx][exy] = EXIT; |
ThomasBGill | 2:0b0311609edc | 173 | |
ThomasBGill | 0:9200b3c387ed | 174 | } |
ThomasBGill | 0:9200b3c387ed | 175 | |
ThomasBGill | 1:0f774d41584c | 176 | void MinePoint() |
ThomasBGill | 0:9200b3c387ed | 177 | { |
ThomasBGill | 0:9200b3c387ed | 178 | //Find start location |
ThomasBGill | 0:9200b3c387ed | 179 | for(int i=0; i<84; i++) { |
ThomasBGill | 0:9200b3c387ed | 180 | for (int j=0; j<48; j++) { |
ThomasBGill | 1:0f774d41584c | 181 | |
ThomasBGill | 0:9200b3c387ed | 182 | int r = rand()%6; |
ThomasBGill | 1:0f774d41584c | 183 | |
ThomasBGill | 0:9200b3c387ed | 184 | if(map[i][j] == FLOOR && map[i+1][j] == WALL && r == 0) { |
ThomasBGill | 0:9200b3c387ed | 185 | dir = RIGHT; |
ThomasBGill | 0:9200b3c387ed | 186 | sx = i; |
ThomasBGill | 0:9200b3c387ed | 187 | sy = j; |
ThomasBGill | 0:9200b3c387ed | 188 | break; |
ThomasBGill | 0:9200b3c387ed | 189 | } else if(map[i][j] == FLOOR && map[i-1][j] == WALL && r == 0) { |
ThomasBGill | 0:9200b3c387ed | 190 | dir = LEFT; |
ThomasBGill | 0:9200b3c387ed | 191 | sx = i; |
ThomasBGill | 0:9200b3c387ed | 192 | sy = j; |
ThomasBGill | 0:9200b3c387ed | 193 | break; |
ThomasBGill | 0:9200b3c387ed | 194 | } else if(map[i][j] == FLOOR && map[i][j+1] == WALL && r == 0) { |
ThomasBGill | 0:9200b3c387ed | 195 | dir = UP; |
ThomasBGill | 0:9200b3c387ed | 196 | sx = i; |
ThomasBGill | 0:9200b3c387ed | 197 | sy = j; |
ThomasBGill | 0:9200b3c387ed | 198 | break; |
ThomasBGill | 0:9200b3c387ed | 199 | } else if(map[i][j] == FLOOR && map[i][j-1] == WALL && r == 0) { |
ThomasBGill | 0:9200b3c387ed | 200 | dir = DOWN; |
ThomasBGill | 0:9200b3c387ed | 201 | sx = i; |
ThomasBGill | 0:9200b3c387ed | 202 | sy = j; |
ThomasBGill | 0:9200b3c387ed | 203 | break; |
ThomasBGill | 0:9200b3c387ed | 204 | } |
ThomasBGill | 0:9200b3c387ed | 205 | } |
ThomasBGill | 0:9200b3c387ed | 206 | } |
ThomasBGill | 1:0f774d41584c | 207 | } |
ThomasBGill | 1:0f774d41584c | 208 | |
ThomasBGill | 1:0f774d41584c | 209 | void MineCorridorBuilder() |
ThomasBGill | 1:0f774d41584c | 210 | { |
ThomasBGill | 1:0f774d41584c | 211 | MinePoint(); |
ThomasBGill | 1:0f774d41584c | 212 | |
ThomasBGill | 0:9200b3c387ed | 213 | //Get length |
ThomasBGill | 0:9200b3c387ed | 214 | int l = rand()%5 + 5; |
ThomasBGill | 0:9200b3c387ed | 215 | |
ThomasBGill | 0:9200b3c387ed | 216 | //Check direction of corridor |
ThomasBGill | 0:9200b3c387ed | 217 | if(dir == RIGHT) { |
ThomasBGill | 0:9200b3c387ed | 218 | for(int i = l; i>0; i--) { |
ThomasBGill | 1:0f774d41584c | 219 | if(map[sx+1][sy] == WALL && map[sx+1][sy+1] == WALL && map[sx+1][sy-1] == WALL && sx < 83) { |
ThomasBGill | 0:9200b3c387ed | 220 | sx++; |
ThomasBGill | 0:9200b3c387ed | 221 | map[sx][sy] = FLOOR; |
ThomasBGill | 0:9200b3c387ed | 222 | } else |
ThomasBGill | 0:9200b3c387ed | 223 | break; |
ThomasBGill | 0:9200b3c387ed | 224 | } |
ThomasBGill | 0:9200b3c387ed | 225 | } else if(dir == LEFT) { |
ThomasBGill | 0:9200b3c387ed | 226 | for(int i = l; i>0; i--) { |
ThomasBGill | 1:0f774d41584c | 227 | if(map[sx-1][sy] == WALL && map[sx-1][sy+1] == WALL && map[sx-1][sy-1] == WALL && sx > 1) { |
ThomasBGill | 0:9200b3c387ed | 228 | sx--; |
ThomasBGill | 0:9200b3c387ed | 229 | map[sx][sy] = FLOOR; |
ThomasBGill | 1:0f774d41584c | 230 | } else |
ThomasBGill | 0:9200b3c387ed | 231 | break; |
ThomasBGill | 0:9200b3c387ed | 232 | } |
ThomasBGill | 0:9200b3c387ed | 233 | } else if(dir == UP) { |
ThomasBGill | 1:0f774d41584c | 234 | for(int i = l; i>0; i--) { |
ThomasBGill | 1:0f774d41584c | 235 | if(map[sx][sy+1] == WALL && map[sx-1][sy+1] == WALL && map[sx+1][sy+1] == WALL && sy < 47) { |
ThomasBGill | 1:0f774d41584c | 236 | sy++; |
ThomasBGill | 1:0f774d41584c | 237 | map[sx][sy] = FLOOR; |
ThomasBGill | 1:0f774d41584c | 238 | } else |
ThomasBGill | 0:9200b3c387ed | 239 | break; |
ThomasBGill | 0:9200b3c387ed | 240 | } |
ThomasBGill | 0:9200b3c387ed | 241 | } else if(dir == DOWN) { |
ThomasBGill | 1:0f774d41584c | 242 | for(int i = l; i>0; i--) { |
ThomasBGill | 1:0f774d41584c | 243 | if(map[sx][sy-1] == WALL && map[sx-1][sy-1] == WALL && map[sx+1][sy-1] == WALL && sy > 1) { |
ThomasBGill | 1:0f774d41584c | 244 | sy--; |
ThomasBGill | 1:0f774d41584c | 245 | map[sx][sy] = FLOOR; |
ThomasBGill | 1:0f774d41584c | 246 | } else |
ThomasBGill | 0:9200b3c387ed | 247 | break; |
ThomasBGill | 0:9200b3c387ed | 248 | } |
ThomasBGill | 0:9200b3c387ed | 249 | } |
ThomasBGill | 0:9200b3c387ed | 250 | } |
ThomasBGill | 0:9200b3c387ed | 251 | |
ThomasBGill | 1:0f774d41584c | 252 | void MineRoomBuilder() |
ThomasBGill | 1:0f774d41584c | 253 | { |
ThomasBGill | 1:0f774d41584c | 254 | MinePoint(); |
ThomasBGill | 2:0b0311609edc | 255 | |
ThomasBGill | 1:0f774d41584c | 256 | //Get length |
ThomasBGill | 1:0f774d41584c | 257 | int sw = rand()%5 + 5; |
ThomasBGill | 1:0f774d41584c | 258 | int sh = rand()%5 + 5; |
ThomasBGill | 0:9200b3c387ed | 259 | |
ThomasBGill | 1:0f774d41584c | 260 | int b = 0; |
ThomasBGill | 1:0f774d41584c | 261 | |
ThomasBGill | 1:0f774d41584c | 262 | if(dir == RIGHT) { |
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 == LEFT) { |
ThomasBGill | 1:0f774d41584c | 283 | sx--; |
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 == UP) { |
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 | 1:0f774d41584c | 318 | } |
ThomasBGill | 1:0f774d41584c | 319 | } |
ThomasBGill | 1:0f774d41584c | 320 | } |
ThomasBGill | 1:0f774d41584c | 321 | } |
ThomasBGill | 1:0f774d41584c | 322 | if(dir == DOWN) { |
ThomasBGill | 1:0f774d41584c | 323 | sy--; |
ThomasBGill | 1:0f774d41584c | 324 | //Check each space. +1 to variable if wall. If total = w*h then build room |
ThomasBGill | 1:0f774d41584c | 325 | for(int i = sx; i < sx + sw; i++) { |
ThomasBGill | 1:0f774d41584c | 326 | for(int j = sy; j > sy - sh; j--) { |
ThomasBGill | 1:0f774d41584c | 327 | if(map[i][j] == WALL) { |
ThomasBGill | 1:0f774d41584c | 328 | b++; |
ThomasBGill | 1:0f774d41584c | 329 | } |
ThomasBGill | 1:0f774d41584c | 330 | } |
ThomasBGill | 1:0f774d41584c | 331 | } |
ThomasBGill | 1:0f774d41584c | 332 | if(b == sw*sh) { |
ThomasBGill | 1:0f774d41584c | 333 | for(int i = sx; i < sx + sw; i++) { |
ThomasBGill | 1:0f774d41584c | 334 | for(int j = sy; j > sy - sh; j--) { |
ThomasBGill | 2:0b0311609edc | 335 | if(i < 84 && j < 48) { |
ThomasBGill | 2:0b0311609edc | 336 | map[i][j] = FLOOR; |
ThomasBGill | 2:0b0311609edc | 337 | } |
ThomasBGill | 2:0b0311609edc | 338 | } |
ThomasBGill | 2:0b0311609edc | 339 | } |
ThomasBGill | 2:0b0311609edc | 340 | } |
ThomasBGill | 2:0b0311609edc | 341 | } |
ThomasBGill | 2:0b0311609edc | 342 | } |
ThomasBGill | 2:0b0311609edc | 343 | |
ThomasBGill | 2:0b0311609edc | 344 | void DungeonRoomBuilder() |
ThomasBGill | 2:0b0311609edc | 345 | { |
ThomasBGill | 2:0b0311609edc | 346 | sx = rand()%84; |
ThomasBGill | 2:0b0311609edc | 347 | sy = rand()%48; |
ThomasBGill | 2:0b0311609edc | 348 | |
ThomasBGill | 2:0b0311609edc | 349 | //Get length |
ThomasBGill | 2:0b0311609edc | 350 | int sw = rand()%5 + 5; |
ThomasBGill | 2:0b0311609edc | 351 | int sh = rand()%5 + 5; |
ThomasBGill | 2:0b0311609edc | 352 | |
ThomasBGill | 2:0b0311609edc | 353 | int b = 0; |
ThomasBGill | 2:0b0311609edc | 354 | |
ThomasBGill | 2:0b0311609edc | 355 | //Check each space. +1 to variable if wall. If total = w*h then build room |
ThomasBGill | 2:0b0311609edc | 356 | for(int i = sx; i < sx + sw; i++) { |
ThomasBGill | 2:0b0311609edc | 357 | for(int j = sy; j < sy + sh; j++) { |
ThomasBGill | 2:0b0311609edc | 358 | if(map[i][j] == WALL) { |
ThomasBGill | 2:0b0311609edc | 359 | b++; |
ThomasBGill | 2:0b0311609edc | 360 | } |
ThomasBGill | 2:0b0311609edc | 361 | } |
ThomasBGill | 2:0b0311609edc | 362 | } |
ThomasBGill | 2:0b0311609edc | 363 | if(b == sw*sh) { |
ThomasBGill | 2:0b0311609edc | 364 | for(int i = sx; i < sx + sw; i++) { |
ThomasBGill | 2:0b0311609edc | 365 | for(int j = sy; j < sy + sh; j++) { |
ThomasBGill | 2:0b0311609edc | 366 | if(i < 84 && j < 48) { |
ThomasBGill | 1:0f774d41584c | 367 | map[i][j] = FLOOR; |
ThomasBGill | 1:0f774d41584c | 368 | } |
ThomasBGill | 1:0f774d41584c | 369 | } |
ThomasBGill | 1:0f774d41584c | 370 | } |
ThomasBGill | 1:0f774d41584c | 371 | } |
ThomasBGill | 1:0f774d41584c | 372 | } |
ThomasBGill | 1:0f774d41584c | 373 | |
ThomasBGill | 4:6482ceb08dc8 | 374 | void Neighbours() |
ThomasBGill | 4:6482ceb08dc8 | 375 | { |
ThomasBGill | 4:6482ceb08dc8 | 376 | |
ThomasBGill | 4:6482ceb08dc8 | 377 | //Check neighbours |
ThomasBGill | 4:6482ceb08dc8 | 378 | n = 0; |
ThomasBGill | 4:6482ceb08dc8 | 379 | |
ThomasBGill | 4:6482ceb08dc8 | 380 | if(map[sx+1][sy] == FLOOR) { |
ThomasBGill | 4:6482ceb08dc8 | 381 | n++; |
ThomasBGill | 4:6482ceb08dc8 | 382 | } |
ThomasBGill | 4:6482ceb08dc8 | 383 | if(map[sx-1][sy] == FLOOR) { |
ThomasBGill | 4:6482ceb08dc8 | 384 | n++; |
ThomasBGill | 4:6482ceb08dc8 | 385 | } |
ThomasBGill | 4:6482ceb08dc8 | 386 | if(map[sx][sy+1] == FLOOR) { |
ThomasBGill | 4:6482ceb08dc8 | 387 | n++; |
ThomasBGill | 4:6482ceb08dc8 | 388 | } |
ThomasBGill | 4:6482ceb08dc8 | 389 | if(map[sx][sy-1] == FLOOR) { |
ThomasBGill | 4:6482ceb08dc8 | 390 | n++; |
ThomasBGill | 4:6482ceb08dc8 | 391 | } |
ThomasBGill | 4:6482ceb08dc8 | 392 | } |
ThomasBGill | 4:6482ceb08dc8 | 393 | |
ThomasBGill | 4:6482ceb08dc8 | 394 | void DeadEnds() |
ThomasBGill | 4:6482ceb08dc8 | 395 | { |
ThomasBGill | 5:9bd276652111 | 396 | for (int del = 20; del > 0; del--) { |
ThomasBGill | 4:6482ceb08dc8 | 397 | for(int i = 0; i < 84; i++) { |
ThomasBGill | 4:6482ceb08dc8 | 398 | for(int j = 0; j < 48; j++) { |
ThomasBGill | 4:6482ceb08dc8 | 399 | |
ThomasBGill | 4:6482ceb08dc8 | 400 | sx = i; |
ThomasBGill | 4:6482ceb08dc8 | 401 | sy = j; |
ThomasBGill | 4:6482ceb08dc8 | 402 | |
ThomasBGill | 4:6482ceb08dc8 | 403 | Neighbours(); |
ThomasBGill | 4:6482ceb08dc8 | 404 | |
ThomasBGill | 4:6482ceb08dc8 | 405 | if(n == 1) { |
ThomasBGill | 4:6482ceb08dc8 | 406 | map[i][j] = WALL; |
ThomasBGill | 4:6482ceb08dc8 | 407 | } |
ThomasBGill | 4:6482ceb08dc8 | 408 | } |
ThomasBGill | 4:6482ceb08dc8 | 409 | } |
ThomasBGill | 4:6482ceb08dc8 | 410 | } |
ThomasBGill | 4:6482ceb08dc8 | 411 | } |
ThomasBGill | 4:6482ceb08dc8 | 412 | |
ThomasBGill | 5:9bd276652111 | 413 | void Border() |
ThomasBGill | 5:9bd276652111 | 414 | { |
ThomasBGill | 5:9bd276652111 | 415 | |
ThomasBGill | 5:9bd276652111 | 416 | for(int i = 0; i < 84; i++) { |
ThomasBGill | 5:9bd276652111 | 417 | for(int j = 0; j < 48; j++) { |
ThomasBGill | 5:9bd276652111 | 418 | |
ThomasBGill | 5:9bd276652111 | 419 | if(i == 0 || i == 83 || j == 0 || j == 47) { |
ThomasBGill | 5:9bd276652111 | 420 | |
ThomasBGill | 5:9bd276652111 | 421 | map[i][j] = WALL; |
ThomasBGill | 5:9bd276652111 | 422 | |
ThomasBGill | 5:9bd276652111 | 423 | } |
ThomasBGill | 5:9bd276652111 | 424 | } |
ThomasBGill | 5:9bd276652111 | 425 | } |
ThomasBGill | 5:9bd276652111 | 426 | } |
ThomasBGill | 4:6482ceb08dc8 | 427 | |
ThomasBGill | 2:0b0311609edc | 428 | void Maze() |
ThomasBGill | 2:0b0311609edc | 429 | { |
ThomasBGill | 3:1a25939df22a | 430 | for(int i = 0; i < 84; i+=2) { |
ThomasBGill | 3:1a25939df22a | 431 | for(int j = 0; j < 48; j+=2) { |
ThomasBGill | 2:0b0311609edc | 432 | |
ThomasBGill | 5:9bd276652111 | 433 | if(map[i][j] == WALL) { |
ThomasBGill | 5:9bd276652111 | 434 | map[i][j] = FLOOR; |
ThomasBGill | 5:9bd276652111 | 435 | } |
ThomasBGill | 2:0b0311609edc | 436 | |
ThomasBGill | 3:1a25939df22a | 437 | dir = rand()%2; //South or east |
ThomasBGill | 2:0b0311609edc | 438 | |
ThomasBGill | 4:6482ceb08dc8 | 439 | |
ThomasBGill | 5:9bd276652111 | 440 | if(dir == SOUTH && j < 47 && map[i][j+1] == WALL) { |
ThomasBGill | 3:1a25939df22a | 441 | map[i][j+1] = FLOOR; |
ThomasBGill | 2:0b0311609edc | 442 | } |
ThomasBGill | 5:9bd276652111 | 443 | if(dir == EAST && i < 84 && map[i+1][j] == WALL) { |
ThomasBGill | 3:1a25939df22a | 444 | map[i+1][j] = FLOOR; |
ThomasBGill | 2:0b0311609edc | 445 | } |
ThomasBGill | 2:0b0311609edc | 446 | } |
ThomasBGill | 2:0b0311609edc | 447 | } |
ThomasBGill | 3:1a25939df22a | 448 | |
ThomasBGill | 4:6482ceb08dc8 | 449 | for(int space = rand()%50 + 51; space > 0; space--) { |
ThomasBGill | 3:1a25939df22a | 450 | |
ThomasBGill | 3:1a25939df22a | 451 | int i = rand()% 84; |
ThomasBGill | 3:1a25939df22a | 452 | int j = rand()% 48; |
ThomasBGill | 3:1a25939df22a | 453 | |
ThomasBGill | 5:9bd276652111 | 454 | if(rand()%2 == 0 && map[i][j] == WALL) { |
ThomasBGill | 3:1a25939df22a | 455 | map[i][j] = FLOOR; |
ThomasBGill | 3:1a25939df22a | 456 | } |
ThomasBGill | 3:1a25939df22a | 457 | } |
ThomasBGill | 4:6482ceb08dc8 | 458 | |
ThomasBGill | 5:9bd276652111 | 459 | Border(); |
ThomasBGill | 5:9bd276652111 | 460 | |
ThomasBGill | 4:6482ceb08dc8 | 461 | DeadEnds(); |
ThomasBGill | 3:1a25939df22a | 462 | |
ThomasBGill | 2:0b0311609edc | 463 | } |
ThomasBGill | 2:0b0311609edc | 464 | |
ThomasBGill | 1:0f774d41584c | 465 | void MineBuilder() |
ThomasBGill | 0:9200b3c387ed | 466 | { |
ThomasBGill | 2:0b0311609edc | 467 | Walls(); |
ThomasBGill | 2:0b0311609edc | 468 | |
ThomasBGill | 0:9200b3c387ed | 469 | FirstRoom(); |
ThomasBGill | 0:9200b3c387ed | 470 | |
ThomasBGill | 1:0f774d41584c | 471 | int fn = rand()%20 + 20; |
ThomasBGill | 0:9200b3c387ed | 472 | |
ThomasBGill | 1:0f774d41584c | 473 | for(int i = fn; i>0; i--) { |
ThomasBGill | 1:0f774d41584c | 474 | int f = rand()% 5; |
ThomasBGill | 1:0f774d41584c | 475 | if(f == 0) { |
ThomasBGill | 1:0f774d41584c | 476 | MineRoomBuilder(); |
ThomasBGill | 1:0f774d41584c | 477 | } else { |
ThomasBGill | 1:0f774d41584c | 478 | MineCorridorBuilder(); |
ThomasBGill | 0:9200b3c387ed | 479 | } |
ThomasBGill | 0:9200b3c387ed | 480 | } |
ThomasBGill | 2:0b0311609edc | 481 | |
ThomasBGill | 2:0b0311609edc | 482 | SetExit(); |
ThomasBGill | 0:9200b3c387ed | 483 | } |
ThomasBGill | 0:9200b3c387ed | 484 | |
ThomasBGill | 2:0b0311609edc | 485 | void DungeonBuilder() |
ThomasBGill | 0:9200b3c387ed | 486 | { |
ThomasBGill | 1:0f774d41584c | 487 | |
ThomasBGill | 4:6482ceb08dc8 | 488 | FirstRoom(); |
ThomasBGill | 5:9bd276652111 | 489 | ExitRoom(); |
ThomasBGill | 4:6482ceb08dc8 | 490 | |
ThomasBGill | 2:0b0311609edc | 491 | int rn = rand()%20 + 20; |
ThomasBGill | 2:0b0311609edc | 492 | |
ThomasBGill | 2:0b0311609edc | 493 | for(int i = rn; i>0; i--) { |
ThomasBGill | 2:0b0311609edc | 494 | DungeonRoomBuilder(); |
ThomasBGill | 2:0b0311609edc | 495 | } |
ThomasBGill | 2:0b0311609edc | 496 | |
ThomasBGill | 5:9bd276652111 | 497 | |
ThomasBGill | 4:6482ceb08dc8 | 498 | |
ThomasBGill | 3:1a25939df22a | 499 | DrawMap(); |
ThomasBGill | 4:6482ceb08dc8 | 500 | wait(2.0); |
ThomasBGill | 2:0b0311609edc | 501 | |
ThomasBGill | 3:1a25939df22a | 502 | Maze(); |
ThomasBGill | 3:1a25939df22a | 503 | |
ThomasBGill | 2:0b0311609edc | 504 | } |
ThomasBGill | 2:0b0311609edc | 505 | |
ThomasBGill | 2:0b0311609edc | 506 | void World() |
ThomasBGill | 2:0b0311609edc | 507 | { |
ThomasBGill | 2:0b0311609edc | 508 | Walls(); |
ThomasBGill | 3:1a25939df22a | 509 | //MineBuilder(); |
ThomasBGill | 3:1a25939df22a | 510 | DungeonBuilder(); |
ThomasBGill | 0:9200b3c387ed | 511 | DrawMap(); |
ThomasBGill | 1:0f774d41584c | 512 | |
ThomasBGill | 0:9200b3c387ed | 513 | } |
ThomasBGill | 0:9200b3c387ed | 514 | |
ThomasBGill | 0:9200b3c387ed | 515 | int main() |
ThomasBGill | 0:9200b3c387ed | 516 | { |
ThomasBGill | 1:0f774d41584c | 517 | //Power Saving |
ThomasBGill | 1:0f774d41584c | 518 | PHY_PowerDown (); |
ThomasBGill | 1:0f774d41584c | 519 | int result = semihost_powerdown(); |
ThomasBGill | 1:0f774d41584c | 520 | |
ThomasBGill | 1:0f774d41584c | 521 | //Generate random seed |
ThomasBGill | 0:9200b3c387ed | 522 | srand(Noise*1000000); |
ThomasBGill | 1:0f774d41584c | 523 | |
ThomasBGill | 1:0f774d41584c | 524 | //Initilize screen |
ThomasBGill | 0:9200b3c387ed | 525 | lcd.init(); |
ThomasBGill | 0:9200b3c387ed | 526 | |
ThomasBGill | 1:0f774d41584c | 527 | //Game loop |
ThomasBGill | 0:9200b3c387ed | 528 | while(1) { |
ThomasBGill | 3:1a25939df22a | 529 | World(); |
ThomasBGill | 4:6482ceb08dc8 | 530 | wait(4.0); |
ThomasBGill | 0:9200b3c387ed | 531 | } |
ThomasBGill | 0:9200b3c387ed | 532 | } |