2015-12-27 14:23:04 +00:00
|
|
|
#include <iostream>
|
2015-12-27 15:34:17 +00:00
|
|
|
#include <random>
|
|
|
|
#include <time.h>
|
|
|
|
|
2015-12-27 14:23:04 +00:00
|
|
|
#define X_MAX 80
|
|
|
|
#define Y_MAX 23
|
2015-12-27 15:34:17 +00:00
|
|
|
#define ROOMS 8
|
|
|
|
#define ROOM_SIZE 7
|
2015-12-27 14:23:04 +00:00
|
|
|
|
2015-12-27 15:46:41 +00:00
|
|
|
#define WALL 0xB2
|
|
|
|
#define EMPTY 0xFF
|
|
|
|
|
2015-12-27 14:23:04 +00:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
//Aggiorna la console con la situazione corrente del gioco.
|
|
|
|
void draw(char map[X_MAX][Y_MAX])
|
|
|
|
{
|
|
|
|
for(int y=0; y<Y_MAX; y++)
|
|
|
|
{
|
|
|
|
for(int x=0; x<X_MAX; x++)
|
|
|
|
{
|
|
|
|
cout << map[x][y];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-27 14:31:40 +00:00
|
|
|
//Funzioni per la generazione della mappa
|
2015-12-27 14:23:04 +00:00
|
|
|
//Inizializza la mappa con spazi vuoti
|
|
|
|
void init(char map[X_MAX][Y_MAX])
|
|
|
|
{
|
|
|
|
for(int y=0; y<Y_MAX; y++)
|
|
|
|
{
|
|
|
|
for(int x=0; x<X_MAX; x++)
|
|
|
|
{
|
2015-12-27 15:46:41 +00:00
|
|
|
map[x][y] = WALL;
|
2015-12-27 14:23:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-27 14:31:40 +00:00
|
|
|
//Crea una stanza quadrata
|
|
|
|
void room(char map[X_MAX][Y_MAX], int start_x, int start_y, int end_x, int end_y)
|
|
|
|
{
|
|
|
|
for(int y=start_y; y<=end_y; y++)
|
|
|
|
{
|
|
|
|
for(int x=start_x; x<=end_x; x++)
|
|
|
|
{
|
2015-12-27 15:46:41 +00:00
|
|
|
map[x][y] = EMPTY;
|
2015-12-27 14:31:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-27 14:52:42 +00:00
|
|
|
//Crea un corridoio che connetta due punti
|
|
|
|
void corridor(char map[X_MAX][Y_MAX], int start_x, int start_y, int end_x, int end_y, bool verticale)
|
|
|
|
{
|
|
|
|
if(verticale)
|
|
|
|
{
|
|
|
|
if(start_y > end_y)
|
|
|
|
{
|
|
|
|
for(int y=end_y; y<=start_y; y++)
|
|
|
|
{
|
2015-12-27 15:46:41 +00:00
|
|
|
map[start_x][y] = EMPTY;
|
2015-12-27 14:52:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(int y=start_y; y<=end_y; y++)
|
|
|
|
{
|
2015-12-27 15:46:41 +00:00
|
|
|
map[start_x][y] = EMPTY;
|
2015-12-27 14:52:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(start_x > end_x)
|
|
|
|
{
|
|
|
|
for(int x=end_x; x<=start_x; x++)
|
|
|
|
{
|
2015-12-27 15:46:41 +00:00
|
|
|
map[x][end_y] = EMPTY;
|
2015-12-27 14:52:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(int x=start_x; x<=end_x; x++)
|
|
|
|
{
|
2015-12-27 15:46:41 +00:00
|
|
|
map[x][end_y] = EMPTY;
|
2015-12-27 14:52:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(start_x > end_x)
|
|
|
|
{
|
|
|
|
for(int x=end_x; x<=start_x; x++)
|
|
|
|
{
|
2015-12-27 15:46:41 +00:00
|
|
|
map[x][start_y] = EMPTY;
|
2015-12-27 14:52:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(int x=start_x; x<=end_x; x++)
|
|
|
|
{
|
2015-12-27 15:46:41 +00:00
|
|
|
map[x][start_y] = EMPTY;
|
2015-12-27 14:52:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(start_y > end_y)
|
|
|
|
{
|
|
|
|
for(int y=end_y; y<=start_y; y++)
|
|
|
|
{
|
2015-12-27 15:46:41 +00:00
|
|
|
map[end_x][y] = EMPTY;
|
2015-12-27 14:52:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(int y=start_y; y<=end_y; y++)
|
|
|
|
{
|
2015-12-27 15:46:41 +00:00
|
|
|
map[end_x][y] = EMPTY;
|
2015-12-27 14:52:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-27 14:31:40 +00:00
|
|
|
|
2015-12-27 15:34:17 +00:00
|
|
|
void generate(char map[X_MAX][Y_MAX])
|
|
|
|
{
|
|
|
|
int corridor_x;
|
|
|
|
int corridor_y;
|
|
|
|
for(int r=0; r<ROOMS; r++)
|
|
|
|
{
|
|
|
|
int size_x = rand() % ROOM_SIZE + 1;
|
|
|
|
int size_y = rand() % ROOM_SIZE + 1;
|
2015-12-27 20:40:59 +00:00
|
|
|
int start_x = rand() % (X_MAX - size_x - 2) + 1;
|
|
|
|
int start_y = rand() % (Y_MAX - size_y - 2) + 1;
|
2015-12-27 15:34:17 +00:00
|
|
|
room(map, start_x, start_y, start_x + size_x, start_y + size_y);
|
|
|
|
if(r > 0)
|
|
|
|
{
|
|
|
|
int link_x = rand() % size_x + 1 + start_x;
|
|
|
|
int link_y = rand() % size_y + 1 + start_y;
|
|
|
|
corridor(map, link_x, link_y, corridor_x, corridor_y, rand() % 2);
|
|
|
|
}
|
|
|
|
corridor_x = rand() % size_x + start_x;
|
|
|
|
corridor_y = rand() % size_y + start_y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-27 14:23:04 +00:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
char map[X_MAX][Y_MAX]; //Mappa del gioco
|
2015-12-27 15:34:17 +00:00
|
|
|
srand(time(NULL)); //TODO: Rendere il seed modificabile
|
2015-12-27 14:23:04 +00:00
|
|
|
init(map);
|
2015-12-27 15:34:17 +00:00
|
|
|
generate(map);
|
2015-12-27 14:23:04 +00:00
|
|
|
draw(map);
|
|
|
|
return 0;
|
|
|
|
}
|