c++ - Smoothing Land Around Lakes -


sorry if title isn't descriptive. anyway, working on dealing randomly generating landscapes. made lakes, due how make, cause straight edges / dropoffs, aren't desirable. trying smooth (right after making lake, if possible), defining max variation amount (so land heights cannot vary more it), , have fix land if varies much, , quit if fine.

the problem:

large dropoff

my attempted fix:

attempted fix

as can see... didn't work. occurs me, think broken if had move down, although case shouldn't occur, because lakes ever sink landscape. anyway, here source of attempt:

//smoothing land nearby              int maxvariation = 2; //similar max height variation when land generated              //going right                (int xpos = rightbound + 1, previousheight = 0; ; ++xpos)             {                 if (previousheight == 0)                     (; previousheight < size.y; ++previousheight)                         if (grid[0][rightbound][previousheight] != blockcolor::dirt && grid[0][rightbound][previousheight] != blockcolor::grass)                         {                             --previousheight;                              break;                         }                   (int y = 0; y < size.y; ++y)                     if (grid[0][xpos][y] == blockcolor::water)                         goto done_smoothing_right;                  int height;                  (height = 0; height < size.y; ++height)                     if (grid[0][xpos][height] != blockcolor::dirt && grid[0][xpos][height] != blockcolor::grass)                     {                         --height;                          break;                     }                      int difference = std::abs(height - previousheight);                      previousheight = height;                      if (difference > maxvariation)                     {                         (int j = 0; j < size.y; ++j)                         {                             int tomove = difference;                              while (j + tomove >= size.y)                                 --tomove;                              grid[0][xpos][j] = grid[0][xpos][j + tomove];                         }                     }                     else                         goto done_smoothing_right;               }  done_smoothing_right:              int tomakegotowork; 

note right side, left should same. how can correctly?

thanks if can help.

edit:

i never did solve problem. instead, made recursive function measure air, (from height), , if pocket of air (formed land) had enough, fill water. has advantaged of land looking smooth because not altered.

this written in java need convert c++ should give basic idea. work smoothing upwards , did right side of lake easy modify left side of lake. tried match think functionality of code is.

hope helps...

void smoothlakeright(lake lake){      int x = lake.rightbound+1;      if(getgrassheight(x)-lake.height>worldconstants.max_lakeside_variation){         //if right bank high start smoothing         int y =lake.height+worldconstants.max_lakeside_variation;          while(grid[0][x][y] == blockcolor.dirt){             fixgrass(x++, y++);         }     } }  private int getgrassheight(int xpos){      int y = worldconstants.lowest_grass;      while(grid[0][xpos][y++] != blockcolor.grass);      return y-1; }  private void fixgrass(int xpos, int ypos){      grid[0][xpos][ypos] = blockcolor.grass;      aboveair(xpos,ypos);     belowdirt(xpos, ypos);  }  private void aboveair(int xpos, int ypos) {      while(grid[0][xpos][++ypos]!=blockcolor.air){         if(grid[0][xpos][ypos]==blockcolor.tree){             uproottree(xpos, ypos);         }else{             grid[0][xpos][ypos]=blockcolor.air;         }     } }  private void uproottree(int xpos, int ypos) {      while(grid[0][xpos][ypos]==blockcolor.tree){//remove stump         grid[0][xpos][ypos++]=blockcolor.air;     }      //remove leaves     grid[0][xpos][ypos] = blockcolor.air;     grid[0][xpos+1][ypos] = blockcolor.air;     grid[0][xpos-1][ypos] = blockcolor.air;     grid[0][xpos+1][ypos-1] = blockcolor.air;     grid[0][xpos-1][ypos-1] = blockcolor.air; }  private void belowdirt(int xpos, int ypos) {      while(grid[0][xpos][--ypos]!=blockcolor.dirt){         grid[0][xpos][ypos] = blockcolor.dirt;     } } 

Comments