1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
#include "Map.h"
#define max std::max
#define min std::min
/*Constructor*/
Map::Map(int width, int height) :
m_width(width),
m_height(height),
m_map(new std::vector< std::vector<Building*> >)
{
/*Resize the 2D std::vector to the specified width and height*/
m_map->resize(m_height); //We need m_height sub-vectors
for (int i = 0; i < m_height; ++i)
(*m_map)[i].resize(m_width); //each sub-vector has a size of m_width
MIN_NUMBER_OF_BUILDINGS = m_width * m_height / 2 + max(m_width, m_height);
/*Map generation algorithm
*The algorithm works as follows:
*1.Initialize whole map with empty buildings (=Grass)
*2.Create all roads
*3.Create buildings next to roads
*
*Road creation:
* 1.Take two random coordinates that are on one straight line
* 2.Connect those using a road
* 3.Somewhere on that road, take a new starting coordinate
* 4.Take a new point
* 5.Repeat this from step 2 until minimum possible buildings is reached
* 6.Min buildings = (width*height) / 2 - max(width, heigth)
*/
/*Initialization*/
#ifndef NDEBUG
std::cout << "Init map\n";
#endif
for (int i = 0; i < m_width; ++i)
for (int j = 0; j < m_height; ++j)
((*m_map)[i][j]) = new Grass(j,i);
do
{
static bool firstIteration = true;
int ROAD_X_COORD_1, ROAD_Y_COORD_1, ROAD_X_COORD_2, ROAD_Y_COORD_2;
static int PREV_X,PREV_Y;
/*The first iteration is different from the others, as our starting point is not based on the previous road*/
if (firstIteration)
{
firstIteration = false;
/*Step 1*/
ROAD_X_COORD_1 = getRandomNumber(0,m_width - 1);
ROAD_Y_COORD_1 = getRandomNumber(0,m_height - 1);
}
else
{
ROAD_X_COORD_1 = PREV_X;
ROAD_Y_COORD_1 = PREV_Y;
}
/*Generate a random number to see if the road goes horizontal or vertical*/
if (getRandomNumber(0,1) == 0) /*Horizontal*/
{
ROAD_Y_COORD_2 = ROAD_Y_COORD_1;
ROAD_X_COORD_2 = getRandomNumber(0,m_width - 1);
for (int i = min(ROAD_X_COORD_1, ROAD_X_COORD_2); i <= max(ROAD_X_COORD_1, ROAD_X_COORD_2); ++i)
{
(*m_map)[ROAD_Y_COORD_1][i]= new Road(i, ROAD_Y_COORD_1);
}
PREV_Y = ROAD_Y_COORD_1;
PREV_X = getRandomNumber(min(ROAD_X_COORD_1, ROAD_X_COORD_2), max(ROAD_X_COORD_1, ROAD_X_COORD_1));
}
else /*Vertical*/
{
ROAD_X_COORD_2 = ROAD_X_COORD_1;
ROAD_Y_COORD_2 = getRandomNumber(0,m_height - 1);
for (int i = min(ROAD_Y_COORD_1, ROAD_Y_COORD_2); i < max(ROAD_Y_COORD_1, ROAD_Y_COORD_2); ++i)
{
(*m_map)[i][ROAD_X_COORD_1] = new Road(ROAD_X_COORD_1, i);
}
PREV_X = ROAD_X_COORD_1;
PREV_Y = getRandomNumber(min(ROAD_Y_COORD_1, ROAD_Y_COORD_2), max(ROAD_Y_COORD_1, ROAD_Y_COORD_1));
}
}
while (getPossibleBuildings() < MIN_NUMBER_OF_BUILDINGS);
#ifndef NDEBUG
Print();
#endif
/*Generate random buildings next to each road tile*/
for (int i = 0; i < m_height; ++i)
{
for (int j = 0; j < m_width; ++j)
{
if ((*m_map)[i][j]->getType() != GRASS)
continue;
if (i > 0)
{
if ((*m_map)[i-1][j]->getType() == ROAD)
{
(*m_map)[i][j] = new House(j,i);
}
}
if (i < m_height - 1)
{
if ((*m_map)[i+1][j]->getType() == ROAD)
{
(*m_map)[i][j] = new House(j,i);
}
}
if (j > 0)
{
if ((*m_map)[j-1][j]->getType() == ROAD)
{
(*m_map)[i][j] = new House(j,i);
}
}
if (j < m_width - 1)
{
if ((*m_map)[i][j+1]->getType() == ROAD)
{
(*m_map)[i][j] = new House(j,i);
}
}
}
}
}
int Map::getPossibleBuildings()
{
/*Possible house counting mechanism
*This algorithm will use a flag array to check if a spot has already been flagged or not
*It will loop through the whole map, checking for roads
*When the algorithm finds a road, it will flag all empty spots next to it and increase the counter
*/
bool flag[m_height][m_width];
int buildingCounter = 0;
/*Loop through map*/
for (int i = 0; i < m_height; ++i)
{
for (int j = 0; j < m_width; ++j)
{
/*Check if this point is a road*/
if ((*m_map)[i][j]->getType() == ROAD)
{
/*Now we search for empty, non-flagged spots around this road*/
/*Check all points above this road, so we need to make sure i > 0*/
if (i > 0)
{
/*Check if spots are grass (empty) and make sure they aren't already flagged, so we don't count spots twice*/
if ((*m_map)[i-1][j]->getType() == GRASS && flag[i-1][j] == false);
{
/*Finally, flag this spot and increment the counter*/
flag[i-1][j] = true;
++buildingCounter;
}
}
/*Find all spots underneath this road*/
if (i < m_height - 1)
{
if ((*m_map)[i+1][j]->getType() == GRASS && flag[i+1][j] == false);
{
flag[i+1][j] = true;
++buildingCounter;
}
}
/*Find all spots next to this road*/
if (j > 0)
{
if ((*m_map)[i][j-1]->getType() == GRASS && flag[i][j-1] == false)
{
flag[i][j-1] = true;
++buildingCounter;
}
}
if (j < m_width - 1)
{
if ((*m_map)[i][j+1]->getType() == GRASS && flag[i][j+1] == false)
{
flag[i][j+1] = true;
++buildingCounter;
}
}
}
}
}
return buildingCounter;
}
/*Destructor*/
Map::~Map()
{
//delete dynamically allocated map
delete m_map;
}
void Map::Print()
{
for (int i = 0; i < m_height; ++i)
{
for (int j = 0; j < m_width; ++j)
(*m_map)[i][j]->Print(true);
std::cout << "\n";
}
std::cout << "\n\n";
}
|