OceanLands.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include "raylib.h"
  2. #include <math.h>
  3. int map[512][512];
  4. int mapWidth = 128;
  5. int mapHeight = 128;
  6. int screenWidth;
  7. int screenHeight;
  8. float tileWidth;
  9. float tileHeight;
  10. void makerandommap();
  11. int main(void)
  12. {
  13. // Initialization
  14. //--------------------------------------------------------------------------------------
  15. screenWidth = 800;
  16. screenHeight = 450;
  17. tileWidth = ceil((float)screenWidth/(float)mapWidth);
  18. tileHeight = ceil((float)screenHeight/(float)mapHeight);
  19. InitWindow(screenWidth, screenHeight, "raylib example.");
  20. int d=GetRandomValue(-1,1);
  21. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  22. //--------------------------------------------------------------------------------------
  23. int newmaptime=0;
  24. // Main game loop
  25. while (!WindowShouldClose()) // Detect window close button or ESC key
  26. {
  27. // Update
  28. //----------------------------------------------------------------------------------
  29. newmaptime--;
  30. if(newmaptime<=0){
  31. makerandommap();
  32. newmaptime=60;
  33. }
  34. //----------------------------------------------------------------------------------
  35. // Draw
  36. //----------------------------------------------------------------------------------
  37. BeginDrawing();
  38. ClearBackground(BLUE);
  39. for(int y=0;y<mapHeight;y++){
  40. for(int x=0;x<mapWidth;x++){
  41. if(map[y][x]>0 && map[y][x]<50){
  42. DrawRectangle(x*tileWidth,y*tileHeight,tileWidth,tileHeight,(Color){map[y][x]+150,150,0,255});
  43. }
  44. if(map[y][x]>=50 && map[y][x]<200){
  45. DrawRectangle(x*tileWidth,y*tileHeight,tileWidth,tileHeight,(Color){map[y][x],map[y][x],0,255});
  46. }
  47. if(map[y][x]>=200){
  48. DrawRectangle(x*tileWidth,y*tileHeight,tileWidth,tileHeight,(Color){map[y][x],map[y][x],map[y][x],255});
  49. }
  50. }}
  51. EndDrawing();
  52. //----------------------------------------------------------------------------------
  53. }
  54. // De-Initialization
  55. //--------------------------------------------------------------------------------------
  56. CloseWindow(); // Close window and OpenGL context
  57. //--------------------------------------------------------------------------------------
  58. return 0;
  59. }
  60. void makerandommap(){
  61. for(int y=0;y<mapHeight;y++){
  62. for(int x=0;x<mapWidth;x++){
  63. map[y][x]=0;
  64. }}
  65. // Create Drunken anty walk map
  66. int size=2000;
  67. Vector2 path[size+1];
  68. int pos=0;
  69. int px=50;
  70. int py=50;
  71. int npx=px;
  72. int npy=py;
  73. int lastx=0; // our last direction.
  74. int lasty=0;
  75. int currentx;
  76. int currenty;
  77. path[pos]=(Vector2){px,py};
  78. for(int len=0;len<size;len++){
  79. currentx = GetRandomValue(-1,1);
  80. currenty = GetRandomValue(-1,1);
  81. // Keep running into same diretion..
  82. if(GetRandomValue(0,10)<5){
  83. currentx = lastx;
  84. currenty = lasty;
  85. }
  86. npx=px+currentx;
  87. npy=py+currenty;
  88. if(npx<0)npx=0;
  89. if(npy<0)npy=0;
  90. if(npx>=mapWidth-1)npx=mapWidth-1;
  91. if(npy>=mapHeight-1)npy=mapHeight-1;
  92. lastx = currentx;
  93. lasty = currenty;
  94. for(int cnt=0;cnt<6;cnt++){
  95. for(int i=0;i<pos;i++){
  96. if((int)path[i].x == npx && (int)path[i].y==npy){
  97. npx=px+GetRandomValue(-1,1);
  98. npy=py+GetRandomValue(-1,1);
  99. if(npx<0)npx=0;
  100. if(npy<0)npy=0;
  101. if(npx>=mapWidth-1)npx=mapWidth-1;
  102. if(npy>=mapHeight-1)npy=mapHeight-1;
  103. break;
  104. }
  105. }
  106. }
  107. pos++;
  108. path[pos] = (Vector2){npx,npy};
  109. map[npy][npx]=1;
  110. px=npx;
  111. py=npy;
  112. }
  113. // Grow edges
  114. for(int i=0;i<mapWidth*30;i++){
  115. int x = GetRandomValue(2,mapWidth-3);
  116. int y = GetRandomValue(2,mapHeight-3);
  117. if(map[y][x]==1){
  118. for(int y1=y-1;y1<y+2;y1++){
  119. for(int x1=x-1;x1<x+2;x1++){
  120. map[y1][x1]=1;
  121. }}
  122. }
  123. }
  124. // raise land
  125. int modder=GetRandomValue(15,20);
  126. for(int i=0;i<(mapWidth*mapHeight)*modder;i++){
  127. int x = GetRandomValue(1,mapWidth-2);
  128. int y = GetRandomValue(1,mapHeight-2);
  129. if(map[y][x]>0){
  130. int h=0;
  131. for(int y1=y-1;y1<y+2;y1++){
  132. for(int x1=x-1;x1<x+2;x1++){
  133. if(map[y1][x1]>0)h++;
  134. }}
  135. if(h>5 && map[y][x]<240)map[y][x]+=h;
  136. }
  137. }
  138. }