# include # include # include # include # include /* Génère un monde automatiquement. */ using namespace std ; // Place un bloc en évitant la sortie de tableau void install( char** world, int w, int h, int y, int x, char blc ) { if ( x < 0 || x >= w || y < 0 || y >= h ) return ; world[y][x] = blc; } // Dessine une ligne verticale void drawVLine( char** world, int w, int h, int y1, int y2, int x, char blc ) { for ( int i(y1); i <= y2; i ++ ) { install( world, w, h, i, x, blc ) ; } } // Dessine un cube void drawCube( char** world, int w, int h, int y, int x, int lx, int ly, char blc ) { for ( int i(y); i < y + ly; i ++ ) for ( int j(x); j < x + lx; j ++ ) install( world, w, h, i, j, blc ); } // Créer le terrain void create( char** &world, int w = 15, int h = 10 ) { // Création world = new char*[h]; for ( int i(0); i < h; i ++ ) { world[i] = new char[w]; } } // Réinitialise le terrain avec des espaces void empty( char** world, int w, int h ) { for ( int i(0); i < h; i ++ ) for ( int j(0); j < w; j++ ) { world[i][j] = ' '; } } // Réalise une bordure d'étoiles void putStar( char** world, int y, int x ) { if ( world[y][x] == ' ' ) world[y][x] = '*'; } void border( char** world, int w, int h ) { for ( int i(0); i < h; i ++ ) { putStar(world, i, 0); putStar(world, i, w - 1); } for ( int j(1); j < w - 1 ; j ++ ) { putStar(world, 0, j); putStar(world, h - 1, j); } } // Affiche le terrain void display( char** world, int w, int h ) { for ( int i(0); i < h; i ++ ) { for ( int j(0); j < w; j++ ) { cout << world[i][j]; } cout << endl ; } } // Plante des arbres void trees( char** world, int w, int h, int space = 10, int dec = 5, int maxray = 3, int maxtall = 7 ) { int j(0); do { // Où poser la graine j += space ; j += rand() % ( 2 * dec ) + 1 ; j -= dec ; int i(h - 2); while ( i > 0 && world[i][j] != ' ' ) i -- ; // Créer un arbre int ray( rand() % maxray + 1 ); int tall( rand() % maxtall + 1 ); if ( world[i + 1][j] == 'M' ) { drawCube( world, w, h, i - 2*ray - tall - 1, j - ray, 2*ray + 1, 2*ray + 1, 'o' ); drawVLine( world, w, h, i - tall - 1, i, j, 'H' ); } } while ( j < w ) ; } // Créer une grotte carrée void caveSquare( char** world, int w, int h, int y, int x, int scale = 5, char blc = ' ' ) { drawCube( world, w, h, y - scale / 2, x - scale / 2, scale, scale, blc ); } // Créer une grotte bombom void caveBomBom( char** world, int w, int h, int y, int x, int scale = 4, int burst = 4, char blc = ' ' ) { for ( int k(0); k < burst ; k ++ ) { int s( scale ); int i(y), j(x); while( s > 0 ) { i += rand() % ( s + 1 ) ; i -= s / 2 ; j += rand() % ( s + 1 ) ; j -= s / 2 ; caveSquare( world, w, h, i, j, s ); s -- ; } } } // Créer les grottes void caves( char** world, int w, int h, char blc = ' ', int space = 20, int dec = 15 ) { int j(0); do { // Intervalle pour faire une cave j += space ; j += rand() % ( 2 * dec ) + 1 ; j -= dec ; int i(0); while ( i < h - 2 && world[i][j] == ' ' ) i ++ ; if ( i == h ) return ; // Randomiser la hauteur de la cave int y(0); y = rand() % ( h - i ) + i; // Randomiser la taille int scale( rand() % 6 + 1 ); // Randomiser le type switch( rand() % 2 ) { case 0 : caveBomBom( world, w, h, y, j, scale ); //2nd type break; case 1 : caveBomBom( world, w, h, y, j, scale ); break; default: break; } } while ( j < w ) ; } // Créer le sol avec une marche aléatoire void linear( char** world, int w, int h, int dec = 1, int grs = 2 ) { int l = h * 3 / 4 ; for ( int j(0); j < w; j++ ) { // Variation de la hauteur l += rand() % ( 2 * dec + 1 ) ; l -= dec ; // Sécurité limite if ( l >= h ) l = h - 1 ; if ( l < 0 ) l = 0 ; // Herbe for ( int i(l); i < h && i < l + grs ; i ++ ) { world[i][j] = 'M'; } for ( int i(l + grs); i < h; i ++ ) { world[i][j] = '#'; } } } // Fait apparaître une structure void popStruct( char** world, int w, int h, int nb ) { // Localisation int j( rand() % w ); int i( rand() % h ); while( i < h - 1 && world[i][j] == ' ' ) i ++ ; // Ouvrir le flux string filename("Structs/"); filename += char( nb + 48 ); filename += ".stc"; ifstream flux( filename.c_str() ); // Faire pop la structure string line; while( getline( flux, line) ) { for ( unsigned int k(0); k < line.size(); k ++ ) { install( world, w, h, i, j + k, line[k] ); } i++; } } // Procédure int main() { srand( time(0) ); // Création standard int w(105), h(32); char** world(0x0); create( world, w, h ); empty( world, w, h ); // Sol linear( world, w, h ); // Caves caves( world, w, h ); // Structures popStruct( world, w, h, 0 ); popStruct( world, w, h, 1 ); // Arbres trees( world, w, h ); // Bordure border( world, w, h ); // Affichage display( world, w, h ); return 0; }