Thomas Gill / Mbed 2 deprecated LabyrinthOfTheMinotaur

Dependencies:   N5110 PowerControl mbed

main.cpp

Committer:
ThomasBGill
Date:
2015-04-17
Revision:
3:1a25939df22a
Parent:
2:0b0311609edc
Child:
4:6482ceb08dc8

File content as of revision 3:1a25939df22a:

#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 SetExit()
{
    do {
        exx = enx + rand()%30 +15;
        exy = eny + rand()%20 +15;
    } while(map[exx][exy] == WALL && exx > 84 && exy > 48);

    map[exx][exy] = EXIT;

}

void MinePoint()
{
    //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;
                sy = j;
                break;
            } else if(map[i][j] == FLOOR && map[i-1][j] == WALL && r == 0) {
                dir = LEFT;
                sx = i;
                sy = j;
                break;
            } else if(map[i][j] == FLOOR && map[i][j+1] == WALL && r == 0) {
                dir = UP;
                sx = i;
                sy = j;
                break;
            } else if(map[i][j] == FLOOR && map[i][j-1] == WALL && r == 0) {
                dir = DOWN;
                sx = i;
                sy = j;
                break;
            }
        }
    }
}

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 && sx < 83) {
                sx++;
                map[sx][sy] = FLOOR;
            } else
                break;
        }
    } 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 && sx > 1) {
                sx--;
                map[sx][sy] = FLOOR;
            } 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 < 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 > 1) {
                sy--;
                map[sx][sy] = FLOOR;
            } else
                break;
        }
    }
}

void MineRoomBuilder()
{
    MinePoint();

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

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

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 Maze()
{

    for(int i = 0; i < 84; i+=2) {
        for(int j = 0; j < 48; j+=2) {

            map[i][j] = FLOOR;

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

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

    for(int del = rand()%250 + 151; del > 0; del--) {

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

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

}

void MineBuilder()
{
    Walls();

    FirstRoom();

    int fn = rand()%20 + 20;

    for(int i = fn; i>0; i--) {
        int f = rand()% 5;
        if(f == 0) {
            MineRoomBuilder();
        } else {
            MineCorridorBuilder();
        }
    }

    SetExit();
}

void DungeonBuilder()
{

    int rn = rand()%20 + 20;

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

    DrawMap();
    wait(1.0);

    Maze();

}

void World()
{
    Walls();
    //MineBuilder();
    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(10.0);
    }
}