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
|
void Game_manager::generate_map(int height, int width, vector<vector<cell*> > &grid)
{
int num_immovables = (height*width)/100;
int num_movables = (height*width)/4;
int num_beasts = (height*width)/200;
grid.resize(height);
for(int i = 0; i<height; i++){
grid[i].resize(width);
}
for(int i = 0; i<height; i++){ //Sets the coordinates for each cell
for(int j = 0; j<width; j++){
grid[i][j] = new Empty;
point p(i,j);
grid[i][j]->setCoord(p);
}
}
for(int j = 0; j<grid[0].size(); j++){ //Changes the first row to immovable
point p = grid[0][j]->getCoord();
delete grid[0][j];
grid[0][j] = NULL;
grid[0][j] = new Immovable;
grid[0][j]->setCoord(p);
}
for(int i = 0; i<grid.size(); i++){ //Changes the first and last cell in the row to immovable
point p = grid[i][0]->getCoord();
delete grid[i][0];
grid[i][0] = NULL;
grid[i][0] = new Immovable;
grid[i][0]->setCoord(p);
p = grid[i][grid[i].size()-1]->getCoord();
delete grid[i][grid[i].size()-1];
grid[i][grid[i].size()-1] = NULL;
grid[i][grid[i].size()-1] = new Immovable;
grid[i][grid[i].size()-1]->setCoord(p);
}
for(int j = 0; j<grid[grid.size()-1].size(); j++){ //Changes the last row to immovable
point p = grid[grid.size()-1][j]->getCoord();
delete grid[grid.size()-1][j];
grid[grid.size()-1][j] = NULL;
grid[grid.size()-1][j] = new Immovable;
grid[grid.size()-1][j]->setCoord(p);
}
srand ( time(NULL) );
for(int i=0; i < num_immovables; i++){ //Generates random immovable blocks
int x, y;
get_random_coord(x, y, height, width);
if(grid[y][x]->getType() != empty)
get_random_coord(x, y, height, width);
delete grid[y][x];
grid[y][x] = NULL;
grid[y][x] = new Immovable;
grid[y][x]->setCoord(x, y);
}
// for(int i=0; i < num_movables; i++){ //Generates random movable blocks
// int x, y;
//
// get_random_coord(x, y, height, width);
// if(grid[y][x]->getType() != empty)
// get_random_coord(x, y, height, width);
// delete grid[y][x];
// GRID[y][x] = NULL;
// grid[y][x] = new Movable;
// grid[y][x]->setCoord(x, y);
// }
for(int i=0; i < num_beasts; i++){ //Generates random beasts
int x, y;
get_random_coord(x, y, height, width);
if(grid[y][x]->getType() != empty)
get_random_coord(x, y, height, width);
delete grid[y][x];
grid[y][x] = NULL;
grid[y][x] = new Beast;
grid[y][x]->setCoord(x, y);
}
{ //Generates a player
int x, y;
get_random_coord(x, y, height, width);
if(grid[y][x]->getType() != empty)
get_random_coord(x, y, height, width);
delete grid[y][x];
grid[y][x] = NULL;
grid[y][x] = new Player;
grid[y][x]->setCoord(x, y);
}
}
|