Thomas Gill / Mbed 2 deprecated LabyrinthOfTheMinotaur

Dependencies:   N5110 PowerControl mbed

main.cpp

Committer:
ThomasBGill
Date:
2015-04-17
Revision:
6:ca5db4353c95
Parent:
5:9bd276652111
Child:
7:cd799c701997

File content as of revision 6:ca5db4353c95:

#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);
InterruptIn Act (p22);
InterruptIn Start (p23);
InterruptIn Up (p24);
InterruptIn Down (p25);
InterruptIn Left (p26);
InterruptIn Right (p27);
AnalogIn Noise (p19);

#define USR_POWERDOWN (0x104)

#define WALL 0
#define FLOOR 1
#define ENTER 2
#define EXIT 3

#define RIGHT 0
#define LEFT 1
#define UP 2
#define DOWN 3

#define SOUTH 0
#define EAST 1

//Variables
int buttonFlagA = 0;
int buttonFlagS = 0;
int buttonFlagU = 0;
int buttonFlagD = 0;
int buttonFlagL = 0;
int buttonFlagR = 0;

int map[84][48];

//Enterance coordinates
int enx;
int eny;

//Exit coordinates
int exx;
int exy;

int sx;
int sy;
int dir;

int n;


//Power Saving
int semihost_powerdown()
{
    uint32_t arg;
    return __semihost(USR_POWERDOWN, &arg);
}

//ISR
void ActPressed()
{
    buttonFlagA = 0;
}
void StartPressed()
{
    buttonFlagS = 0;
}
void UpPressed()
{
    buttonFlagU = 0;
}
void DownPressed()
{
    buttonFlagD = 0;
}
void LeftPressed()
{
    buttonFlagL = 0;
}
void RightPressed()
{
    buttonFlagR = 0;
}

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 Floors()
{
    //Fill map with floors
    for(int i=0; i<84; i++) {
        for (int j=0; j<48; j++) {
            map[i][j] = FLOOR;
        }
    }
}

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 + 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
    enx = rand()%sw + si;
    eny = rand()%sh + sj;
    map[enx][eny] = EXIT;
}

void DungeonRoomBuilder()
{
    sx = rand()%84;
    sy = rand()%48;

    //Get length
    int sw = rand()%5 + 5;
    int sh = rand()%5 + 5;

    int b = 0;

    //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++) {
                if(i < 84 && j < 48) {
                    map[i][j] = FLOOR;
                }
            }
        }
    }
}

void Neighbours()
{

    //Check neighbours
    n = 0;

    if(map[sx+1][sy] == FLOOR) {
        n++;
    }
    if(map[sx-1][sy] == FLOOR) {
        n++;
    }
    if(map[sx][sy+1] == FLOOR) {
        n++;
    }
    if(map[sx][sy-1] == FLOOR) {
        n++;
    }
}

void DeadEnds()
{
    for (int del = 20; del > 0; del--) {
        for(int i = 0; i < 84; i++) {
            for(int j = 0; j < 48; j++) {

                sx = i;
                sy = j;

                Neighbours();

                if(n == 1) {
                    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 Maze()
{
    for(int i = 0; i < 84; i+=2) {
        for(int j = 0; j < 48; j+=2) {

            if(map[i][j] == WALL) {
                map[i][j] = FLOOR;
            }

            dir = rand()%2; //South or east


            if(dir == SOUTH && j < 47 && map[i][j+1] == WALL) {
                map[i][j+1] = FLOOR;
            }
            if(dir == EAST && i < 84 && map[i+1][j] == WALL) {
                map[i+1][j] = FLOOR;
            }
        }
    }

    for(int space = rand()%50 + 51; space > 0; space--) {

        int i = rand()% 84;
        int j = rand()% 48;

        if(rand()%2 == 0 && map[i][j] == WALL) {
            map[i][j] = FLOOR;
        }
    }

    Border();

    DeadEnds();

}

void DungeonBuilder()
{

    FirstRoom();
    ExitRoom();

    int rn = rand()%20 + 20;

    for(int i = rn; i>0; i--) {
        DungeonRoomBuilder();
    }



    DrawMap();
    wait(2.0);

    Maze();

}

void World()
{
    Walls();

    DungeonBuilder();

    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) {
        World();
        wait(4.0);
    }
}