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

Dependencies:   N5110 PowerControl mbed

Revision:
1:0f774d41584c
Parent:
0:9200b3c387ed
Child:
2:0b0311609edc
--- a/main.cpp	Thu Apr 09 17:29:22 2015 +0000
+++ b/main.cpp	Fri Apr 10 11:47:04 2015 +0000
@@ -1,5 +1,7 @@
 #include "mbed.h"
 #include "N5110.h"
+#include "PowerControl/PowerControl.h"
+#include "PowerControl/EthernetPowerControl.h"
 
 //         vcc sce rst dc  mosi clk  led
 N5110 lcd (p5, p6, p7, p8, p11, p13, p21);
@@ -11,6 +13,8 @@
 InterruptIn Right (p27);
 AnalogIn Noise (p19);
 
+#define USR_POWERDOWN (0x104)
+
 #define WALL 0
 #define FLOOR 1
 #define ENTER 2
@@ -31,6 +35,17 @@
 
 int map[84][48];
 
+int sx;
+int sy;
+int dir;
+
+//Power Saving
+int semihost_powerdown()
+{
+    uint32_t arg;
+    return __semihost(USR_POWERDOWN, &arg);
+}
+
 //ISR
 void ActPressed()
 {
@@ -67,11 +82,26 @@
     }
 }
 
+void DrawMap()
+{
+    //Draw map on screen
+    for(int i=0; i<84; i++) {
+        for (int j=0; j<48; j++) {
+            if(map[i][j] == FLOOR) {
+                lcd.clearPixel(i, j);
+            } else {
+                lcd.setPixel(i, j);
+            }
+        }
+    }
+    lcd.refresh();
+}
+
 void FirstRoom()
 {
     //Create initial room
-    int si = rand()%25 + 25;
-    int sj = rand()%15 + 20;
+    int si = rand()%25 + 1;
+    int sj = rand()%15 + 1;
 
     int sw = rand()%5+5;
     int sh = rand()%5+5;
@@ -87,19 +117,14 @@
     map[ex][ey] = ENTER;
 }
 
-void CorridorBuilder()
+void MinePoint()
 {
-    //Get start point
-    int sx;
-    int sy;
-    int dir;
-
     //Find start location
     for(int i=0; i<84; i++) {
         for (int j=0; j<48; j++) {
-            
+
             int r = rand()%6;
-            
+
             if(map[i][j] == FLOOR && map[i+1][j] == WALL && r == 0) {
                 dir = RIGHT;
                 sx = i;
@@ -123,13 +148,19 @@
             }
         }
     }
+}
+
+void MineCorridorBuilder()
+{
+    MinePoint();
+
     //Get length
     int l = rand()%5 + 5;
 
     //Check direction of corridor
     if(dir == RIGHT) {
         for(int i = l; i>0; i--) {
-            if(map[sx+1][sy] == WALL && map[sx+1][sy+1] == WALL && map[sx+1][sy-1] == WALL) {
+            if(map[sx+1][sy] == WALL && map[sx+1][sy+1] == WALL && map[sx+1][sy-1] == WALL && sx < 83) {
                 sx++;
                 map[sx][sy] = FLOOR;
             } else
@@ -137,75 +168,154 @@
         }
     } else if(dir == LEFT) {
         for(int i = l; i>0; i--) {
-            if(map[sx-1][sy] == WALL && map[sx-1][sy+1] == WALL && map[sx-1][sy-1] == WALL){
+            if(map[sx-1][sy] == WALL && map[sx-1][sy+1] == WALL && map[sx-1][sy-1] == WALL && sx > 1) {
                 sx--;
                 map[sx][sy] = FLOOR;
-            }else
+            } else
                 break;
         }
     } else if(dir == UP) {
-        for(int i = l; i>0; i--){
-            if(map[sx][sy+1] == WALL && map[sx-1][sy+1] == WALL && map[sx+1][sy+1] == WALL){
-            sy++;
-            map[sx][sy] = FLOOR;
-            }else
+        for(int i = l; i>0; i--) {
+            if(map[sx][sy+1] == WALL && map[sx-1][sy+1] == WALL && map[sx+1][sy+1] == WALL && sy < 47) {
+                sy++;
+                map[sx][sy] = FLOOR;
+            } else
                 break;
         }
     } else if(dir == DOWN) {
-        for(int i = l; i>0; i--){
-            if(map[sx][sy-1] == WALL && map[sx-1][sy-1] == WALL && map[sx+1][sy-1] == WALL){
-            sy--;
-            map[sx][sy] = FLOOR;
-            }else
+        for(int i = l; i>0; i--) {
+            if(map[sx][sy-1] == WALL && map[sx-1][sy-1] == WALL && map[sx+1][sy-1] == WALL && sy > 1) {
+                sy--;
+                map[sx][sy] = FLOOR;
+            } else
                 break;
         }
     }
-
 }
 
+void MineRoomBuilder()
+{
+    MinePoint();
+    
+    //Get length
+    int sw = rand()%5 + 5;
+    int sh = rand()%5 + 5;
 
-void DungeonBuilder()
+    int b = 0;
+
+    if(dir == RIGHT) {
+        sx++;
+        //Check each space. +1 to variable if wall. If total = w*h then build room
+        for(int i = sx; i < sx + sw; i++) {
+            for(int j = sy; j < sy + sh; j++) {
+                if(map[i][j] == WALL) {
+                    b++;
+                }
+            }
+        }
+        if(b == sw*sh) {
+            for(int i = sx; i < sx + sw; i++) {
+                for(int j = sy; j < sy + sh; j++) {
+                    map[i][j] = FLOOR;
+                }
+            }
+        }
+    }
+    if(dir == LEFT) {
+        sx--;
+        //Check each space. +1 to variable if wall. If total = w*h then build room
+        for(int i = sx; i > sx - sw; i--) {
+            for(int j = sy; j < sy + sh; j++) {
+                if(map[i][j] == WALL) {
+                    b++;
+                }
+            }
+        }
+        if(b == sw*sh) {
+            for(int i = sx; i > sx - sw; i--) {
+                for(int j = sy; j < sy + sh; j++) {
+                    map[i][j] = FLOOR;
+                }
+            }
+        }
+    }
+    if(dir == UP) {
+        sy++;
+        //Check each space. +1 to variable if wall. If total = w*h then build room
+        for(int i = sx; i < sx + sw; i++) {
+            for(int j = sy; j < sy + sh; j++) {
+                if(map[i][j] == WALL) {
+                    b++;
+                }
+            }
+        }
+        if(b == sw*sh) {
+            for(int i = sx; i < sx + sw; i++) {
+                for(int j = sy; j < sy + sh; j++) {
+                    map[i][j] = FLOOR;
+                }
+            }
+        }
+    }
+    if(dir == DOWN) {
+        sy--;
+        //Check each space. +1 to variable if wall. If total = w*h then build room
+        for(int i = sx; i < sx + sw; i++) {
+            for(int j = sy; j > sy - sh; j--) {
+                if(map[i][j] == WALL) {
+                    b++;
+                }
+            }
+        }
+        if(b == sw*sh) {
+            for(int i = sx; i < sx + sw; i++) {
+                for(int j = sy; j > sy - sh; j--) {
+                    map[i][j] = FLOOR;
+                }
+            }
+        }
+    }
+}
+
+void MineBuilder()
 {
     FirstRoom();
 
-    int cn = rand()%10 + 15;
-
-    for(int i = cn; i>0; i--) {
-        CorridorBuilder();
-    }
-
-}
-
-
+    int fn = rand()%20 + 20;
 
-void DrawMap()
-{
-    //Draw map on screen
-    for(int i=0; i<84; i++) {
-        for (int j=0; j<48; j++) {
-            if(map[i][j] == FLOOR) {
-                lcd.clearPixel(i, j);
-            } else {
-                lcd.setPixel(i, j);
-            }
+    for(int i = fn; i>0; i--) {
+        int f = rand()% 5;
+        if(f == 0) {
+            MineRoomBuilder();
+        } else {
+            MineCorridorBuilder();
         }
     }
-    lcd.refresh();
 }
 
 void Dungeon()
 {
-    Walls();
-    DungeonBuilder();
+
+    MineBuilder();
     DrawMap();
+
 }
 
 int main()
 {
+    //Power Saving
+    PHY_PowerDown ();
+    int result = semihost_powerdown();
+
+    //Generate random seed
     srand(Noise*1000000);
+
+    //Initilize screen
     lcd.init();
 
+    //Game loop
     while(1) {
+        Walls();
         Dungeon();
         wait(2.0);
     }