src/MapRoutines.cpp
//Generate the map using Diamond-Square Algorithm
Generate the map using Diamond-Square Algorithm
Generate the map using Diamond - Square Algorithm
void Map_GenerateMap()
{
    printf("Generating Map\n");
    int sideLength;
    int x;
    int y;
    double h = 100;
    numberoftriangles = 0;
    Map[POINT(0,0)].h = Map[POINT(0,MAP_SIZE-1)].h = Map[POINT(MAP_SIZE-1,0)].h = Map[POINT(MAP_SIZE-1,MAP_SIZE-1)].h = SEED;
    for(sideLength = MAP_SIZE-1; sideLength >= 2; sideLength /=2, h/= 2.0)
    {
        int halfSide = sideLength/2;
        for(x=0;x<MAP_SIZE-1;x+=sideLength)
        {
            for(y=0;y<MAP_SIZE-1;y+=sideLength)
            {
                double avg =    Map[POINT(x,y)].h +                       
                                Map[POINT(x+sideLength,y)].h +            
                                Map[POINT(x,y+sideLength)].h +            
                                Map[POINT(x+sideLength,y+sideLength)].h;  
                avg /= 4.0;
                Map[POINT(x+halfSide,y+halfSide)].h =
                avg + ((double)rand()/(double)RAND_MAX * 2 * h) - h;
            }
        }
        for(x=0;x<MAP_SIZE-1;x+=halfSide)
        {
            for(y=(x+halfSide)%sideLength;y<MAP_SIZE-1;y+=sideLength)
            {
                double  avg =   Map[POINT((x - halfSide  + MAP_SIZE) % (MAP_SIZE), y)].h +         
                                Map[POINT((x + halfSide) % MAP_SIZE - 1, y)].h +                   
                                Map[POINT(x, (y + halfSide) % MAP_SIZE - 1)].h +                   
                                Map[POINT(x, (y - halfSide  + MAP_SIZE - 1) % (MAP_SIZE - 1))].h;  
                avg /= 4.0;
                avg += ((double)rand() / (double)RAND_MAX * 2 * h) - h;
                Map[POINT(x,y)].h = avg;
                if(x == 0)  Map[POINT(MAP_SIZE-1,y)].h = avg;
                if(y == 0)  Map[POINT(x,MAP_SIZE-1)].h = avg;
            }
        }
    }
    Map_SmoothTerrain(0.75, 3);
    Map_NormalizeValues();
    Map_SetTiles();
    Map_GenerateMapData(MAP_HD, MAP_Normals, MAP_Outlines, MAP_Colors, Map, MAP_SIZE);
    printf("Map Generation Process Done\n");
}
void Map_GenerateMap ( ) { printf ( " \n " ) ; int sideLength ; int x ; int y ; double h = 100 ; numberoftriangles = 0 ; Map [ POINT ( 0 , 0 ) ] . h = Map [ POINT ( 0 , MAP_SIZE - 1 ) ] . h = Map [ POINT ( MAP_SIZE - 1 , 0 ) ] . h = Map [ POINT ( MAP_SIZE - 1 , MAP_SIZE - 1 ) ] . h = SEED ; for ( sideLength = MAP_SIZE - 1 ; sideLength >= 2 ; sideLength /= 2 , h /= 2.0 ) { int halfSide = sideLength / 2 ; for ( x = 0 ; x < MAP_SIZE - 1 ; x += sideLength ) { for ( y = 0 ; y < MAP_SIZE - 1 ; y += sideLength ) { double avg = Map [ POINT ( x , y ) ] . h + Map [ POINT ( x + sideLength , y ) ] . h + Map [ POINT ( x , y + sideLength ) ] . h + Map [ POINT ( x + sideLength , y + sideLength ) ] . h ; avg /= 4.0 ; Map [ POINT ( x + halfSide , y + halfSide ) ] . h = avg + ( ( double ) rand ( ) / ( double ) RAND_MAX * 2 * h ) - h ; } } for ( x = 0 ; x < MAP_SIZE - 1 ; x += halfSide ) { for ( y = ( x + halfSide ) % sideLength ; y < MAP_SIZE - 1 ; y += sideLength ) { double avg = Map [ POINT ( ( x - halfSide + MAP_SIZE ) % ( MAP_SIZE ) , y ) ] . h + Map [ POINT ( ( x + halfSide ) % MAP_SIZE - 1 , y ) ] . h + Map [ POINT ( x , ( y + halfSide ) % MAP_SIZE - 1 ) ] . h + Map [ POINT ( x , ( y - halfSide + MAP_SIZE - 1 ) % ( MAP_SIZE - 1 ) ) ] . h ; avg /= 4.0 ; avg += ( ( double ) rand ( ) / ( double ) RAND_MAX * 2 * h ) - h ; Map [ POINT ( x , y ) ] . h = avg ; if ( x == 0 ) Map [ POINT ( MAP_SIZE - 1 , y ) ] . h = avg ; if ( y == 0 ) Map [ POINT ( x , MAP_SIZE - 1 ) ] . h = avg ; } } } Map_SmoothTerrain ( 0.75 , 3 ) ; Map_NormalizeValues ( ) ; Map_SetTiles ( ) ; Map_GenerateMapData ( MAP_HD , MAP_Normals , MAP_Outlines , MAP_Colors , Map , MAP_SIZE ) ; printf ( " \n " ) ; }
Generate the map using Diamond-Square Algorithm
Generate the map using Diamond - Square Algorithm
//Seed the data //top left //top right //lower left //lower right //center is average plus random offset //left //right //below //above center