2
0

Doors_Based_Dungeon_Map.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #include "raylib.h"
  2. #include <math.h>
  3. enum mapflag{mleft,mright,mup,mdown};
  4. static int mapWidth = 50;
  5. static int mapHeight = 50;
  6. static float tileWidth;
  7. static float tileHeight;
  8. static int map[200][200];
  9. static void generatemap(void);
  10. static bool roomfits(int x,int y,int w,int h);
  11. static void insertroom(int x,int y,int w,int h);
  12. static void makeroom(int x,int y);
  13. int main(void)
  14. {
  15. // Initialization
  16. //--------------------------------------------------------------------------------------
  17. const int screenWidth = 800;
  18. const int screenHeight = 450;
  19. InitWindow(screenWidth, screenHeight, "raylib example.");
  20. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  21. //--------------------------------------------------------------------------------------
  22. generatemap();
  23. tileWidth = abs((float)screenWidth/(float)mapWidth);
  24. tileHeight = abs((float)screenHeight/(float)mapHeight);
  25. // Main game loop
  26. while (!WindowShouldClose()) // Detect window close button or ESC key
  27. {
  28. // Update
  29. //----------------------------------------------------------------------------------
  30. if(IsKeyReleased(KEY_SPACE) || (IsMouseButtonReleased(0))){
  31. int z=GetRandomValue(30,100);
  32. mapWidth=z;
  33. mapHeight=z;
  34. tileWidth = abs((float)screenWidth/(float)mapWidth);
  35. tileHeight = abs((float)screenHeight/(float)mapHeight);
  36. generatemap();
  37. }
  38. //----------------------------------------------------------------------------------
  39. // Draw
  40. //----------------------------------------------------------------------------------
  41. BeginDrawing();
  42. ClearBackground(RAYWHITE);
  43. // Draw the map..
  44. for(int y=0;y<mapHeight;y++){
  45. for(int x=0;x<mapWidth;x++){
  46. if(map[x][y]==1){ // Wall
  47. DrawRectangle(x*tileWidth,y*tileHeight,tileWidth,tileHeight,BLACK);
  48. }
  49. if(map[x][y]==2){ // Floor
  50. DrawRectangle(x*tileWidth,y*tileHeight,tileWidth,tileHeight,DARKGRAY);
  51. }
  52. if(map[x][y]==3){ // Door
  53. DrawRectangle(x*tileWidth,y*tileHeight,tileWidth,tileHeight,RED);
  54. }
  55. }
  56. }
  57. DrawText("Press Space or Left Mouse Button for new map.",0,0,30,RED);
  58. EndDrawing();
  59. //----------------------------------------------------------------------------------
  60. }
  61. // De-Initialization
  62. //--------------------------------------------------------------------------------------
  63. CloseWindow(); // Close window and OpenGL context
  64. //--------------------------------------------------------------------------------------
  65. return 0;
  66. }
  67. void generatemap(){
  68. // erase old map;
  69. for(int y=0;y<mapHeight;y++){
  70. for(int x=0;x<mapWidth;x++){
  71. map[x][y] = 0;
  72. }}
  73. map[mapWidth/2][mapHeight/2]=3;
  74. //
  75. int timeout=0;
  76. while (timeout<(mapWidth*mapHeight)*20){
  77. timeout+=1;
  78. int x=GetRandomValue(11,mapWidth-11);
  79. int y=GetRandomValue(11,mapHeight-11);
  80. if(map[x][y] == 3){
  81. makeroom(x,y);
  82. }
  83. if(timeout>12345612)return;
  84. }
  85. //'here we turn doors into walls
  86. //'if they should be walls
  87. for(int y1=1;y1<mapHeight-1;y1++){
  88. for(int x1=1;x1<mapWidth-1;x1++){
  89. if(map[x1][y1] == 3){
  90. int cnt=0;
  91. for(int y2=y1-1;y2<y1+1;y2++){
  92. for(int x2=x1-1;x2<x1+1;x2++){
  93. if(map[x2][y2] == 2)cnt+=1;
  94. }}
  95. if(cnt>3)map[x1][y1] = 2;
  96. }
  97. }}
  98. //here we turn doors into walls if it is blocked
  99. for(int y1=1;y1<=mapHeight-1;y1++){
  100. for(int x1=1;x1<=mapWidth-1;x1++){
  101. if(map[x1][y1] == 3){
  102. int cnt=0;
  103. for(int y2=y1-1;y2<=y1+1;y2++){
  104. for(int x2=x1-1;x2<=x1+1;x2++){
  105. if(map[x2][y2]==2)cnt+=2;
  106. }}
  107. if(cnt>6)map[x1][y1]=2;
  108. }
  109. }}
  110. //'here we turn doors into walls if they
  111. //' touch tiles that are nothing (0)
  112. for(int y1=1;y1<=mapHeight-1;y1++){
  113. for(int x1=1;x1<=mapWidth-1;x1++){
  114. if(map[x1][y1] == 3){
  115. int cnt=0;
  116. for(int y2=y1-1;y2<=y1+1;y2++){
  117. for(int x2=x1-1;x2<=x1+1;x2++){
  118. if(map[x2][y2] == 0)cnt+=1;
  119. }}
  120. if(cnt>0)map[x1][y1] = 2;
  121. }
  122. }}
  123. //'here we turn the doors into floors
  124. for(int y1=0;y1<=mapHeight;y1++){
  125. for(int x1=0;x1<=mapWidth;x1++){
  126. if(map[x1][y1] == 3)map[x1][y1] = 1;
  127. }}
  128. }
  129. bool roomfits(int x,int y,int w,int h){
  130. for(int y1=y;y1<=y+h;y1++){
  131. for(int x1=x;x1<=x+w;x1++){
  132. if(x1<0 || x1>=mapWidth || y1<0 || y1>=mapHeight)return false;
  133. if(map[x1][y1] == 1)return false;
  134. }
  135. }
  136. return true;
  137. }
  138. void insertroom(int x,int y,int w,int h){
  139. for(int y2=y;y2<y+h;y2++){
  140. for(int x2=x;x2<x+w;x2++){
  141. if(x2<0 || x2>=mapWidth || y2<0 || y2>=mapHeight)return;
  142. if( map[x2][y2] != 3 )map[x2][y2] = 2;
  143. }}
  144. for(int y2=y+1;y2<y+h-1;y2++){
  145. for(int x2=x+1;x2<x+w-1;x2++){
  146. if(x2<0 || x2>=mapWidth || y2<0 || y2>=mapHeight)return;
  147. map[x2][y2] = 1;
  148. }}
  149. }
  150. void makeroom(int x,int y){
  151. int side=0;
  152. if(map[x][y-1] == 0){
  153. side=mup;
  154. }else if(map[x+1][y] == 0){
  155. side=mright;
  156. }else if(map[x][y+1] == 0){
  157. side=mdown;
  158. }else if(map[x-1][y] == 0){
  159. side=mleft;
  160. }
  161. int w=GetRandomValue(5,10);
  162. int h=GetRandomValue(5,10);
  163. // Sometimes create a big room
  164. if(GetRandomValue(0,20)<2){
  165. w*=2;
  166. h*=2;
  167. // sometimes it is wider or higher
  168. if(GetRandomValue(0,5)==1){
  169. if(GetRandomValue(0,1)==1){
  170. w/=2;
  171. }else{
  172. h/=2;
  173. }
  174. }
  175. }
  176. // Here we create the room based on where the door is.
  177. if(side==mup){
  178. int x1=x-w/2;
  179. int y1=y-h;
  180. if(roomfits(x1,y1,w,h)){
  181. insertroom(x1,y1,w,h+1);
  182. //'door up
  183. map[x1+GetRandomValue(2,w-3)][y1] = 3;
  184. //' door right
  185. map[x1+w-1][y1+GetRandomValue(2,h-3)] = 3;
  186. //'door left
  187. map[x1][y1+GetRandomValue(2,h-3)] = 3;
  188. }
  189. }
  190. if(side==mright){
  191. int x1=x+1;
  192. int y1=y-h/2;
  193. if(roomfits(x1,y1,w,h)){
  194. insertroom(x1-1,y1,w,h);
  195. //'door up
  196. map[x1+GetRandomValue(2,w-3)][y1] = 3;
  197. //'door down
  198. map[x1+GetRandomValue(2,w-3)][y1+h-1] = 3;
  199. //' door right
  200. map[x1+w-2][y1+GetRandomValue(2,h-3)] = 3;
  201. }
  202. }
  203. if(side==mleft){
  204. int x1=x-w;
  205. int y1=y-h/2;
  206. if(roomfits(x1,y1,w,h)){
  207. insertroom(x1,y1,w+1,h);
  208. //'door up
  209. map[x1+GetRandomValue(2,w-3)][y1] = 3;
  210. //'door down
  211. map[x1+GetRandomValue(2,w-3)][y1+h-1] = 3;
  212. //'door left
  213. map[x1][y1+GetRandomValue(2,h-3)] = 3;
  214. }
  215. }
  216. if(side==mdown){
  217. int x1=x-w/2;
  218. int y1=y+1;
  219. if(roomfits(x1,y1,w,h)){
  220. insertroom(x1,y1-1,w,h);
  221. //'door down
  222. map[x1+GetRandomValue(2,w-3)][y1+h-2] = 3;
  223. //'door left
  224. map[x1][y1+GetRandomValue(2,h-3)] = 3;
  225. //' door right
  226. map[x1+w-1][y1+GetRandomValue(2,h-3)] = 3;
  227. }
  228. }
  229. }