Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
const int max_x = 500;
const int max_y = 500;
const char stone = '#';
const char dirt = '_';
const int wall_width = 1;
const int wall_height = 1;
const char staricase_up = 'U';
const char staricase_down = 'D';
const int MAXROOMS = 200;
struct ROOM{
int x, y;
int width, height;
int room_id;
bool placed;
};
ROOM gen_room()
{
int x, y, width, height;
width = ((int)(rand()%25)+5);
height = ((int)(rand()%25)+5);
x = y = 0;
ROOM room;
room.x = x;
room.y = y;
room.width = width;
room.height = height;
room.placed = false;
return room;
}
bool valueInRange(int value, int min, int max);
bool valueInRangeEx(int value, int min, int max);
bool rectOverlap(ROOM A, ROOM B);
bool rectContains(ROOM A, ROOM B);
void generate_map2();
void update_map_file(FILE * pFile, char _map[][max_y]);
void place_all_rooms(int map_width, int map_height, char _map[][max_y], int number_of_rooms, ROOM list[], FILE *pFile);
int main()
{
generate_map2();
return 0;
}
void generate_map2()
{
int number_of_rooms = 0;
int m_width, m_height;
int m_centre_x;
int m_centre_y;
int placed_rooms = 0;
bool increase_placed_rooms_flag = false;
char map[max_x][max_y];
int room_id_count = 0;
FILE *pFile = NULL;
ROOM map_room;
srand((unsigned int)time(NULL)); // sead for random room gen
ROOM room_list[MAXROOMS];
number_of_rooms = MAXROOMS;
//randomly choose a map size if the user hasent chosen one
m_width = (rand() % 499)+50;
m_height = (rand() % 499)+50;
int total_map_area = m_width*m_height;
map_room.x = 0;
map_room.y = 0;
map_room.width = m_width;
map_room.height = m_height;
//make map, then fill the map with stone
for(int x = 0; x<m_width; x++)
{
for(int y = 0; y<m_height; y++)
{
map[x][y] = stone;
}
}
//clean up map
for(int x = 0; x<max_x; x++)
{
for(int y = 0; y<max_y; y++)
{
if(map[x][y] != '#')
{
map[x][y] = ' ';
}
}
}
//find center of the map
m_centre_x = m_width/2;
m_centre_y = m_height/2;
for(int i = 1; i<number_of_rooms; i++)
{
room_list[i] = gen_room();
}
room_list[0].height = 10;
room_list[0].width = 10;
room_list[0].x = m_centre_x - (int)room_list[0].width/2;
room_list[0].y = m_centre_y - (int)room_list[0].width/2;
room_list[0].placed = true;
for(int i = 1; i<number_of_rooms;)
{
room_list[i].x = (int)(rand() % m_width) + 1;
room_list[i].y = (int)(rand() % m_height) + 1;
if(placed_rooms > 0)
{
for(int j = 1; j <= placed_rooms; j++)
{
if(rectOverlap(room_list[i], room_list[i-j]) == false)
{
if((rectContains(map_room, room_list[i]) == true))
{
increase_placed_rooms_flag = true;
}
}
}
if(increase_placed_rooms_flag == true)
{
placed_rooms++;
increase_placed_rooms_flag = false;
room_list[i].placed = true;
i++;
}
}
else
{
if(rectOverlap(room_list[0], room_list[i]) == false)
{
if(rectContains(map_room, room_list[i]) == true)
{
placed_rooms++;
room_list[i].placed = true;
i++;
}
}
}
}
place_all_rooms(m_width, m_height, map, number_of_rooms, room_list, pFile);
update_map_file(pFile, map);
}
bool valueInRange(int value, int min, int max)
{
return (value >= min) && (value <= max);
}
bool valueInRangeEx(int value, int min, int max)
{
return (value > min) && (value < max);
}
bool rectOverlap(ROOM A, ROOM B)
{
bool xOverlap = valueInRangeEx(A.x, B.x, B.x + B.width+2) ||
valueInRangeEx(B.x, A.x, A.x + A.width+2);
bool yOverlap = valueInRangeEx(A.y, B.y, B.y + B.height+2) ||
valueInRangeEx(B.y, A.y, A.y + B.height+2);
return xOverlap && yOverlap;
}
bool rectContains(ROOM A, ROOM B)
{
bool containsX = valueInRange(B.x, A.x, A.x + A.width) && valueInRange(B.x + B.width, A.x, A.x + A.width);
bool containsY = valueInRange(B.y, A.y, A.y + A.height) && valueInRange(B.y + B.height, A.y, A.y + A.height);
return containsX && containsY;
}
void place_all_rooms(int map_width, int map_height, char _map[][max_y], int number_of_rooms, ROOM list[], FILE *pFile)
{
for(int z = 0; z < number_of_rooms; z++)
{
if((list[z].placed == true))
{
for(int room_x = 1; room_x<=list[z].width; room_x++)
{
for(int room_y = 1; room_y<=list[z].height; room_y++)
{
_map[list[z].x + room_x][list[z].y + room_y] = dirt;
//update_map_file(pFile, _map);
}
}
}
}
}
void update_map_file(FILE * pFile, char _map[][max_y])
{
pFile = fopen ("mymap.txt","w");
for(int y = 0; y < max_y; y++)
{
for(int x = 0; x < max_x; x++)
{
fputc(_map[x][y], pFile);
}
fputs("\n", pFile);
}
fclose(pFile);
}
this code doesn't do exactly what I want, however it is close. If you compile it it will generate a map but the rooms still intersect and sometimes are not contained in the map. I know there are some memory leaks in here so if someone could help me with that too I would be very grateful.