pattern_map_generator.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. //
  2. // Pattern map generator.
  3. //
  4. #define MAX_POINT 124
  5. #include "raylib.h"
  6. #include <math.h>
  7. enum flag{UP,DOWN,LEFT,RIGHT};
  8. static int screenWidth = 800;
  9. static int screenHeight = 450;
  10. static int mapWidth=100;
  11. static int mapHeight=100;
  12. static float tileWidth;
  13. static float tileHeight;
  14. static int map[512][512];
  15. typedef struct point{
  16. bool active;
  17. Vector2 position;
  18. }point;
  19. static point arr_point[MAX_POINT];
  20. static void generate(void); // generate the map.
  21. static void makenice(); // add walls.
  22. int main(void)
  23. {
  24. // Initialization
  25. //--------------------------------------------------------------------------------------
  26. tileWidth = abs((float)screenWidth/(float)mapWidth);
  27. tileHeight = abs((float)screenHeight/(float)mapHeight);
  28. InitWindow(screenWidth, screenHeight, "raylib example.");
  29. generate();
  30. makenice();
  31. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  32. //--------------------------------+------------------------------------------------------
  33. static int time=0;
  34. // Main game loop
  35. while (!WindowShouldClose()) // Detect window close button or ESC key
  36. {
  37. // Update
  38. //----------------------------------------------------------------------------------
  39. if(IsKeyReleased(KEY_SPACE)){
  40. generate();
  41. makenice();
  42. }
  43. time++;
  44. if(time>100){
  45. time=0;
  46. generate();
  47. makenice();
  48. }
  49. //----------------------------------------------------------------------------------
  50. // Draw
  51. //----------------------------------------------------------------------------------
  52. BeginDrawing();
  53. ClearBackground(RAYWHITE);
  54. // Draw the map
  55. for(int y=0;y<mapHeight;y++){
  56. for(int x=0;x<mapWidth;x++){
  57. if(map[x][y]==1){
  58. DrawRectangle(x*tileWidth,y*tileHeight,tileWidth,tileHeight,BLACK);
  59. }
  60. if(map[x][y]==2){
  61. DrawRectangle(x*tileWidth,y*tileHeight,tileWidth,tileHeight,RED);
  62. }
  63. if(map[x][y]==3){
  64. DrawRectangle(x*tileWidth,y*tileHeight,tileWidth,tileHeight,BLUE);
  65. }
  66. if(map[x][y]==4){
  67. DrawRectangle(x*tileWidth,y*tileHeight,tileWidth,tileHeight,YELLOW);
  68. }
  69. }}
  70. DrawRectangle(0,0,screenWidth,20,(Color){50,50,50,150});
  71. DrawText("Press Space to generate new map..(Or wait)",0,0,20,RAYWHITE);
  72. EndDrawing();
  73. //----------------------------------------------------------------------------------
  74. }
  75. // De-Initialization
  76. //--------------------------------------------------------------------------------------
  77. CloseWindow(); // Close window and OpenGL context
  78. //--------------------------------------------------------------------------------------
  79. return 0;
  80. }
  81. void generate(){
  82. mapWidth=100;
  83. mapHeight=100;
  84. static int script[100];//= {RIGHT,RIGHT,DOWN,RIGHT,DOWN,DOWN,LEFT,LEFT,LEFT,UP,UP,UP};
  85. for(int i=0;i<100;i++){
  86. script[i]=GetRandomValue(0,3);
  87. }
  88. int dunlen=GetRandomValue(11,40);
  89. for(int i=0;i<MAX_POINT;i++){
  90. arr_point[i].active=false;
  91. }
  92. for(int y=0;y<512;y++){
  93. for(int x=0;x<512;x++){
  94. map[x][y]=0;
  95. }}
  96. int num=0;
  97. int x=mapWidth/2;
  98. int y=mapHeight/2;
  99. arr_point[num].active = true;
  100. arr_point[num].position = (Vector2){x,y};
  101. map[x][y]=1;
  102. for(int i=0;i<dunlen-1;i++){
  103. switch (script[i]){
  104. case RIGHT:
  105. x+=7;
  106. if(x>mapWidth-6)x-=7;
  107. break;
  108. case LEFT:
  109. x-=7;
  110. if(x<6)x+=7;
  111. break;
  112. case UP:
  113. y-=7;
  114. if(y<6)y+=7;
  115. break;
  116. case DOWN:
  117. y+=7;
  118. if(y<mapHeight-6)y-=7;
  119. break;
  120. }
  121. num++;
  122. map[x][y] = 1;
  123. arr_point[num].active = true;
  124. arr_point[num].position.x = x;
  125. arr_point[num].position.y = y;
  126. }
  127. for(int i=0;i<dunlen;i++){
  128. if(arr_point[i].active==false)continue;
  129. int x=arr_point[i].position.x;
  130. int y=arr_point[i].position.y;
  131. int offx=-2;
  132. int offy=-2;
  133. int w=GetRandomValue(4,6);
  134. int h=GetRandomValue(4,6);
  135. for(int y2=y+offy;y2<y+offy+h;y2++){
  136. for(int x2=x+offx;x2<x+offx+w;x2++){
  137. if(x2<0 || y2<0 || x2>mapWidth-1 || y2>mapHeight-1)continue;
  138. map[x2][y2]=1;
  139. }}
  140. }
  141. for(int i=0;i<dunlen-1;i++){
  142. int x1 = arr_point[i].position.x;
  143. int y1 = arr_point[i].position.y;
  144. int x2 = arr_point[i+1].position.x;
  145. int y2 = arr_point[i+1].position.y;
  146. if(x2>x1){
  147. while(x1<x2){
  148. x1++;
  149. if(x1<0 || y1<0 || x1>mapWidth-1 || y1>mapHeight-1)continue;
  150. map[x1][y1]=2;
  151. }
  152. }
  153. if(x1>x2){
  154. while(x1>x2){
  155. x1--;
  156. if(x1<0 || y1<0 || x1>mapWidth-1 || y1>mapHeight-1)continue;
  157. map[x1][y1]=2;
  158. }
  159. }
  160. if(y2>y1){
  161. while(y1<y2){
  162. y1++;
  163. if(x1<0 || y1<0 || x1>mapWidth-1 || y1>mapHeight-1)continue;
  164. map[x1][y1]=2;
  165. }
  166. }
  167. if(y1>y2){
  168. while(y1>y2){
  169. y1--;
  170. if(x1<0 || y1<0 || x1>mapWidth-1 || y1>mapHeight-1)continue;
  171. map[x1][y1]=2;
  172. }
  173. }
  174. }
  175. }
  176. void makenice(){
  177. // add walls to rooms
  178. for(int y=1;y<mapHeight-1;y++){
  179. for(int x=1;x<mapWidth-1;x++){
  180. int cnt=0;
  181. if(map[x][y]==1 || map[x][y]==2)continue;
  182. for(int y2=y-1;y2<y+2;y2++){
  183. for(int x2=x-1;x2<x+2;x2++){
  184. if(map[x2][y2]==1)cnt++;
  185. }
  186. }
  187. if(cnt>0)map[x][y]=3;
  188. }
  189. }
  190. // add walls to halls.
  191. for(int y=1;y<mapHeight-1;y++){
  192. for(int x=1;x<mapWidth-1;x++){
  193. int cnt=0;
  194. if(map[x][y]==0){
  195. for(int y2=y-1;y2<y+2;y2++){
  196. for(int x2=x-1;x2<x+2;x2++){
  197. if(map[x2][y2]==2)cnt++;
  198. }
  199. }
  200. if(cnt>0)map[x][y]=3;
  201. }
  202. }
  203. }
  204. // turn door lines into floor
  205. for(int y=1;y<mapHeight-1;y++){
  206. for(int x=1;x<mapWidth-1;x++){
  207. if(map[x][y]==2)map[x][y]=1;
  208. }}
  209. //find top most
  210. int top=0;
  211. for(int y=0;y<mapHeight;y++){
  212. bool ex=false;
  213. for(int x=0;x<mapWidth;x++){
  214. if(map[x][y]>0){
  215. top=y;
  216. ex=true;
  217. break;
  218. }
  219. }
  220. if(ex)break;
  221. }
  222. //find left most
  223. int left=0;
  224. for(int x=0;x<mapWidth;x++){
  225. bool ex=false;
  226. for(int y=0;y<mapHeight;y++){
  227. if(map[x][y]>0){
  228. left=x;
  229. ex=true;
  230. break;
  231. }
  232. }
  233. if(ex)break;
  234. }
  235. // Copy map into buffer map from where it begins on the map top and left
  236. // and then copy this all back to the main map.
  237. int m2[200][200];
  238. for(int y=0;y<mapHeight;y++){
  239. for(int x=0;x<mapWidth;x++){
  240. m2[x][y]=map[x+left][y+top];
  241. }}
  242. for(int y=0;y<512;y++){//erase map
  243. for(int x=0;x<512;x++){
  244. map[x][y]=0;
  245. }}
  246. for(int y=0;y<mapHeight;y++){//copy temp back into main map at left top
  247. for(int x=0;x<mapWidth;x++){
  248. map[x][y] = m2[x][y];
  249. }}
  250. // find right part
  251. int right=0;
  252. for(int x=mapWidth-1;x>0;x--){
  253. bool ex=false;
  254. for(int y=0;y<mapHeight;y++){
  255. if(map[x][y]>0){
  256. right = x;
  257. ex=true;
  258. break;
  259. }
  260. }
  261. if(ex)break;
  262. }
  263. // find most bottom part
  264. int bottom=0;
  265. for(int y=mapHeight-1;y>0;y--){
  266. bool ex=false;
  267. for(int x=0;x<mapWidth;x++){
  268. if(map[x][y]>0){
  269. bottom = y;
  270. ex=true;
  271. break;
  272. }
  273. }
  274. if(ex)break;
  275. }
  276. mapWidth = right+1;
  277. mapHeight = bottom+1;
  278. tileWidth = abs((float)screenWidth/(float)mapWidth);
  279. tileHeight = abs((float)screenHeight/(float)mapHeight);
  280. }