Hill_Algorithm_2dMap_Height.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // Simple way to generate a map. I learned that this method is called the Hill ALgorithm.'
  3. // You basically drop some sand on the map and enter a loop. Then pick random points
  4. // and if on that spot is sand than drop sand on that spot and around it. This will
  5. // grow that into a hill.
  6. //
  7. // I drop the sand in a pattern so the hills grow with that shape.
  8. //
  9. //
  10. //
  11. #include "raylib.h"
  12. #include <math.h>
  13. static int map[300][300];
  14. static int mapWidth = 100;
  15. static int mapHeight = 100;
  16. static float tileWidth;
  17. static float tileHeight;
  18. static const int screenWidth = 640;
  19. static const int screenHeight = 480;
  20. static void makemap();
  21. static void drawmap();
  22. int main(void)
  23. {
  24. // Initialization
  25. //--------------------------------------------------------------------------------------
  26. InitWindow(screenWidth, screenHeight, "raylib example.");
  27. // We want to have the tile width and height fill the entire screen.
  28. tileWidth = ceil((float)screenWidth / (float)mapWidth);
  29. tileHeight = ceil((float)screenHeight / (float)mapHeight);
  30. // Here we generate the map using the hill algorithm
  31. makemap();
  32. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  33. //--------------------------------------------------------------------------------------
  34. // Main game loop
  35. while (!WindowShouldClose()) // Detect window close button or ESC key
  36. {
  37. // Update
  38. //----------------------------------------------------------------------------------
  39. if(IsKeyReleased(KEY_SPACE)){
  40. makemap();
  41. }
  42. //----------------------------------------------------------------------------------
  43. // Draw
  44. //----------------------------------------------------------------------------------
  45. BeginDrawing();
  46. ClearBackground(RAYWHITE);
  47. drawmap();
  48. DrawRectangle(0,0,screenWidth,24,DARKGRAY);
  49. DrawText("Press Space to Generate new map",2,2,20,WHITE);
  50. EndDrawing();
  51. //----------------------------------------------------------------------------------
  52. }
  53. // De-Initialization
  54. //--------------------------------------------------------------------------------------
  55. CloseWindow(); // Close window and OpenGL context
  56. //--------------------------------------------------------------------------------------
  57. return 0;
  58. }
  59. static void makemap(){
  60. for(int y=0;y<mapHeight;y++){
  61. for(int x=0;x<mapWidth;x++){
  62. map[x][y]=0;
  63. }}
  64. // First we create a couple of hills in the map
  65. // We create a random spot and draw a line there somewhat randomly
  66. // We create a line of some sort to create more realistic landmasses. :Experiment:
  67. int rx,ry;
  68. for(int i=0;i<mapWidth/3;i++){
  69. rx = GetRandomValue(5,mapWidth-5);
  70. ry = GetRandomValue(5,mapHeight-5);
  71. int mx=GetRandomValue(-1,1);
  72. int my=GetRandomValue(-1,1);
  73. int len=GetRandomValue(1,mapWidth/4);
  74. for(int ii=0;ii<len;ii++){
  75. if(GetRandomValue(0,10)<2){
  76. mx=GetRandomValue(-1,1);
  77. my=GetRandomValue(-1,1);
  78. }
  79. int nx=rx+mx;
  80. int ny=ry+my;
  81. if(nx>0&&nx<mapWidth-1 && ny>0 && ny<mapHeight-1){
  82. map[nx][ny] = 1;
  83. }
  84. }
  85. }
  86. // Now we want to pile sand ontop and right next to the current hills.
  87. // This will make them into larger hills. We do this by picking a random
  88. // spot. If there is something below that we increase the height there.
  89. for(int i=0;i<(mapWidth*mapHeight)*2;i++){
  90. int nx,ny;
  91. nx = GetRandomValue(1,mapWidth-2);
  92. ny = GetRandomValue(1,mapHeight-2);
  93. if(map[nx][ny]>0){
  94. for(int y=-1;y<2;y++){
  95. for(int x=-1;x<2;x++){
  96. map[nx+x][ny+y]++;
  97. if(map[nx+x][ny+y]>100)map[nx+x][ny+y]=100;
  98. }
  99. }
  100. }
  101. }
  102. };
  103. static void drawmap(){
  104. for(int y=0;y<mapHeight;y++){
  105. for(int x=0;x<mapWidth;x++){
  106. if(map[x][y]==0){ // water
  107. DrawRectangle(x*tileWidth,y*tileHeight,tileWidth,tileHeight,BLUE);
  108. }
  109. if(map[x][y]>0){
  110. if(map[x][y]>0){//Draw the beach
  111. DrawRectangle(x*tileWidth,y*tileHeight,tileWidth,tileHeight,(Color){250,220,0,255});
  112. }
  113. if(map[x][y]>3){//If the height is larger than 3 than draw trees
  114. DrawRectangle(x*tileWidth,y*tileHeight,tileWidth,tileHeight,(Color){0,210,0,255});
  115. }
  116. if(map[x][y]>15){ // here we draw some more trees
  117. DrawRectangle(x*tileWidth,y*tileHeight,tileWidth,tileHeight,(Color){0,170,0,255});
  118. }
  119. if(map[x][y]>20){ // draw mountains or rocks
  120. DrawRectangle(x*tileWidth,y*tileHeight,tileWidth,tileHeight,(Color){110,110,110,255});
  121. }
  122. }
  123. }
  124. }
  125. };