WaveFunctionCollapse.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. //
  2. // Wave Function Collapse - sort of...
  3. //
  4. // I barely understand how this works. It has taken me a number of tries and a whack load of hours
  5. // top get to this current point. The tutorials and explanations on the internet are not that clear and easy
  6. // to understand. (defines?!)
  7. //
  8. // In the example here I have a number of tiles. I pick a random tile from the valid ones and place it on the grid.
  9. // I then fix the neighbour tiles to fit this current position. Connected/unconnected. I keep placing random tiles
  10. // for a x amount of times.
  11. // There is a array that contains a list of all possible tiles. There are functions that check if a position on the grid
  12. // has connections to another position on the grid. This is used to unflag the tiles that are on the list to get
  13. // selected(one random) as a tile to be placed. So we have a list on each grid position with valid connections to the neigbours.
  14. //
  15. // The MAX_TILES is the amount of tiles that I have created to create the map with. I check the middle top/left/right/bottom
  16. // tile value to see if there is a connection(1). Experiment with making different tiles.
  17. //
  18. // I might(should) add comments to the code later.
  19. //
  20. #include "raylib.h"
  21. #define MAP_WIDTH 10
  22. #define MAP_HEIGHT 10
  23. #define MAX_TILES 15
  24. static void newmap(void);
  25. static void resetmap(void);
  26. static bool domainhasleftsidedconnections(int x,int y);
  27. static bool domainhasrightsidedconnections(int x,int y);
  28. static bool domainhastopsidedconnections(int x,int y);
  29. static bool domainhasbottomsidedconnections(int x,int y);
  30. static void removeleftsidedconnections(int x,int y);
  31. static void removerightsidedconnections(int x,int y);
  32. static void removetopsidedconnections(int x,int y);
  33. static void removebottomsidedconnections(int x,int y);
  34. static void setrandomtile(int x,int y);
  35. static void updatedomain(int x,int y);
  36. static void finishmap(void);
  37. static int tile[3][3][MAX_TILES] = {0};
  38. static int map[MAP_WIDTH][MAP_HEIGHT][MAX_TILES] = {0};
  39. static int tilemap[MAP_WIDTH][MAP_HEIGHT] = {0};
  40. int main(void)
  41. { // Initialization
  42. //--------------------------------------------------------------------------------------
  43. const int screenWidth = 800;
  44. const int screenHeight = 450;
  45. InitWindow(screenWidth, screenHeight, "raylib example.");
  46. int tile3[3][3] = { {0,0,0},
  47. {1,1,1},
  48. {0,0,0}};
  49. int tile4[3][3] = { {0,1,0},
  50. {0,1,0},
  51. {0,1,0}};
  52. int tile1[3][3] = { {0,0,0},
  53. {0,0,0},
  54. {0,0,0}};
  55. int tile2[3][3] = { {0,1,0},
  56. {1,1,1},
  57. {0,1,0}};
  58. int tile5[3][3] = { {0,1,0},
  59. {1,1,0},
  60. {0,0,0}};
  61. int tile6[3][3] = { {0,1,0},
  62. {0,1,1},
  63. {0,0,0}};
  64. int tile7[3][3] = { {0,0,0},
  65. {1,1,0},
  66. {0,1,0}};
  67. int tile8[3][3] = { {0,0,0},
  68. {0,1,1},
  69. {0,1,0}};
  70. int tile9[3][3] = { {0,1,0},
  71. {1,1,1},
  72. {0,0,0}};
  73. int tile10[3][3] = { {0,1,0},
  74. {0,1,1},
  75. {0,1,0}};
  76. int tile11[3][3] = { {0,0,0},
  77. {1,1,1},
  78. {0,1,0}};
  79. int tile12[3][3] = { {0,1,0},
  80. {1,1,0},
  81. {0,1,0}};
  82. int tile13[3][3] = { {1,1,0},
  83. {1,1,0},
  84. {1,1,0}};
  85. int tile14[3][3] = { {0,1,1},
  86. {0,1,1},
  87. {0,1,1}};
  88. int tile15[3][3] = { {0,0,0},
  89. {1,1,1},
  90. {1,1,1}};
  91. // int tile[3][3][4] = {0};
  92. for(int i=0;i<MAX_TILES;i++){
  93. for(int y=0;y<3;y++){
  94. for(int x=0;x<3;x++){
  95. switch (i){
  96. case 0:
  97. tile[x][y][i] = tile1[x][y];
  98. break;
  99. case 1:
  100. tile[x][y][i] = tile2[x][y];
  101. break;
  102. case 2:
  103. tile[x][y][i] = tile3[x][y];
  104. break;
  105. case 3:
  106. tile[x][y][i] = tile4[x][y];
  107. break;
  108. case 4:
  109. tile[x][y][i] = tile5[x][y];
  110. break;
  111. case 5:
  112. tile[x][y][i] = tile6[x][y];
  113. break;
  114. case 6:
  115. tile[x][y][i] = tile7[x][y];
  116. break;
  117. case 7:
  118. tile[x][y][i] = tile8[x][y];
  119. break;
  120. case 8:
  121. tile[x][y][i] = tile9[x][y];
  122. break;
  123. case 9:
  124. tile[x][y][i] = tile10[x][y];
  125. break;
  126. case 10:
  127. tile[x][y][i] = tile11[x][y];
  128. break;
  129. case 11:
  130. tile[x][y][i] = tile12[x][y];
  131. break;
  132. case 12:
  133. tile[x][y][i] = tile13[x][y];
  134. break;
  135. case 13:
  136. tile[x][y][i] = tile14[x][y];
  137. break;
  138. case 14:
  139. tile[x][y][i] = tile15[x][y];
  140. break;
  141. }
  142. }
  143. }
  144. }
  145. newmap();
  146. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  147. //--------------------------------------------------------------------------------------
  148. // Main game loop
  149. while (!WindowShouldClose()) // Detect window close button or ESC key
  150. {
  151. // Update
  152. //----------------------------------------------------------------------------------
  153. //----------------------------------------------------------------------------------
  154. if(IsKeyReleased(KEY_SPACE)){
  155. newmap();
  156. }
  157. // Draw
  158. //----------------------------------------------------------------------------------
  159. BeginDrawing();
  160. ClearBackground(RAYWHITE);
  161. for (int y = 0; y< MAP_HEIGHT ; y++)
  162. {
  163. for (int x = 0; x< MAP_WIDTH ; x++)
  164. {
  165. int x3 = x*48;
  166. int y3 = y*32;
  167. for(int y2=0;y2<3;y2++){
  168. for(int x2=0;x2<3;x2++){
  169. if( tile[x2][y2][tilemap[x][y]]==1 )DrawRectangle(x3+x2*16,y3+y2*12,16,12,BLACK);
  170. }
  171. }
  172. }
  173. }
  174. DrawText("Press space for new map.", 100, screenHeight-40, 40, LIGHTGRAY);
  175. EndDrawing();
  176. //----------------------------------------------------------------------------------
  177. }
  178. // De-Initialization
  179. //--------------------------------------------------------------------------------------
  180. CloseWindow(); // Close window and OpenGL context
  181. //--------------------------------------------------------------------------------------
  182. return 0;
  183. }
  184. void newmap(){
  185. resetmap();
  186. int depth=GetRandomValue(40,100);
  187. for(int i=0;i<depth;i++){
  188. setrandomtile(GetRandomValue(0,MAP_WIDTH-1),GetRandomValue(0,MAP_HEIGHT-1));
  189. }
  190. finishmap();
  191. };
  192. void finishmap(){
  193. for(int y=0;y<MAP_HEIGHT;y++){
  194. for(int x=0;x<MAP_WIDTH;x++){
  195. for(int i=0;i<MAX_TILES;i++){
  196. if(map[x][y][i]>-1){
  197. tilemap[x][y] = i;
  198. }
  199. }
  200. }}
  201. };
  202. void resetmap(){
  203. for(int y=0;y<MAP_HEIGHT;y++){
  204. for(int x=0;x<MAP_WIDTH;x++){
  205. for(int i=0;i<MAX_TILES;i++){
  206. map[x][y][i] = 1;
  207. }
  208. }
  209. }
  210. };
  211. bool domainhasleftsidedconnections(int x,int y){
  212. if(x<0 || y<0 || x>=MAP_WIDTH || y>=MAP_HEIGHT)return false;
  213. for(int i=0;i<MAX_TILES;i++){
  214. if(map[x][y][i]==1 && tile[0][1][i]==1){
  215. return true;
  216. }
  217. }
  218. return false;
  219. };
  220. bool domainhasrightsidedconnections(int x,int y){
  221. if(x<0 || y<0 || x>=MAP_WIDTH || y>=MAP_HEIGHT)return false;
  222. for(int i=0;i<MAX_TILES;i++){
  223. if(map[x][y][i]==1 && tile[2][1][i]==1){
  224. return true;
  225. }
  226. }
  227. return false;
  228. };
  229. bool domainhastopsidedconnections(int x,int y){
  230. if(x<0 || y<0 || x>=MAP_WIDTH || y>=MAP_HEIGHT)return false;
  231. for(int i=0;i<MAX_TILES;i++){
  232. if(map[x][y][i]==1 && tile[1][0][i]==1){
  233. return true;
  234. }
  235. }
  236. return false;
  237. };
  238. bool domainhasbottomsidedconnections(int x,int y){
  239. if(x<0 || y<0 || x>=MAP_WIDTH || y>=MAP_HEIGHT)return false;
  240. for(int i=0;i<MAX_TILES;i++){
  241. if(map[x][y][i]==1 && tile[1][2][i]==1){
  242. return true;
  243. }
  244. }
  245. return false;
  246. };
  247. void removeleftsidedconnections(int x,int y){
  248. if(x<0 || y<0 || x>=MAP_WIDTH || y>=MAP_HEIGHT)return false;
  249. for(int i=0;i<MAX_TILES;i++){
  250. if(map[x][y][i]==1 && tile[0][1][i]==1){
  251. map[x][y][i]=-1;
  252. }
  253. }
  254. };
  255. void removerightsidedconnections(int x,int y){
  256. if(x<0 || y<0 || x>=MAP_WIDTH || y>=MAP_HEIGHT)return false;
  257. for(int i=0;i<MAX_TILES;i++){
  258. if(map[x][y][i]==1 && tile[2][1][i]==1){
  259. map[x][y][i]=-1;
  260. }
  261. }
  262. };
  263. void removetopsidedconnections(int x,int y){
  264. if(x<0 || y<0 || x>=MAP_WIDTH || y>=MAP_HEIGHT)return false;
  265. for(int i=0;i<MAX_TILES;i++){
  266. if(map[x][y][i]==1 && tile[1][0][i]==1){
  267. map[x][y][i]=-1;
  268. }
  269. }
  270. };
  271. void removebottomsidedconnections(int x,int y){
  272. if(x<0 || y<0 || x>=MAP_WIDTH || y>=MAP_HEIGHT)return false;
  273. for(int i=0;i<MAX_TILES;i++){
  274. if(map[x][y][i]==1 && tile[1][2][i]==1){
  275. map[x][y][i]=-1;
  276. }
  277. }
  278. };
  279. void setrandomtile(int x,int y){
  280. bool exitloop=false;
  281. int count=0;
  282. while(exitloop==false){
  283. count++;
  284. int val=GetRandomValue(0,MAX_TILES-1);
  285. if(map[x][y][val]>-1){
  286. for(int i=0;i<MAX_TILES;i++){
  287. map[x][y][i]=-1;
  288. }
  289. map[x][y][val]=1;
  290. exitloop = true;
  291. }
  292. if(count>500)exitloop=true;
  293. }
  294. updatedomain(x-1,y);
  295. updatedomain(x+1,y);
  296. updatedomain(x,y-1);
  297. updatedomain(x,y+1);
  298. };
  299. void updatedomain(int x,int y){
  300. if(x<0 || y<0 || x>=MAP_WIDTH || y>=MAP_HEIGHT)return;
  301. for(int i=0;i<MAX_TILES;i++){
  302. map[x][y][i]=1;
  303. }
  304. if(domainhasbottomsidedconnections(x,y-1)==false)removetopsidedconnections(x,y);
  305. if(domainhastopsidedconnections(x,y+1)==false)removebottomsidedconnections(x,y);
  306. if(domainhasleftsidedconnections(x+1,y)==false)removerightsidedconnections(x,y);
  307. if(domainhasrightsidedconnections(x-1,y)==false)removeleftsidedconnections(x,y);
  308. };