WaveFunctionCollapse_Commented.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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.
  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.
  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 14
  22. #define MAP_HEIGHT 12
  23. #define MAX_TILES 15
  24. static void newmap(void);
  25. static void resetmap(void);
  26. // If the selected map position has a connection the l/r/u/d side than return true/false
  27. static bool domainhasleftsidedconnections(int x,int y);
  28. static bool domainhasrightsidedconnections(int x,int y);
  29. static bool domainhastopsidedconnections(int x,int y);
  30. static bool domainhasbottomsidedconnections(int x,int y);
  31. // Remove every tile flag from l/r/u/d.
  32. static void removeleftsidedconnections(int x,int y);
  33. static void removerightsidedconnections(int x,int y);
  34. static void removetopsidedconnections(int x,int y);
  35. static void removebottomsidedconnections(int x,int y);
  36. // Select a random tile from the list of available tiles.
  37. static void setrandomtile(int x,int y);
  38. // Update the current map position checking l/r/u/d for connections and setting
  39. // the flag for can use or can not use with setrandomtile.
  40. static void updatedomain(int x,int y);
  41. // create our final tilemap.
  42. static void finishmap(void);
  43. // Our tile array. 3by3 grid of x amount of tiles
  44. static int tile[3][3][MAX_TILES] = {0};
  45. // Our work map. We have every tile set up there. -1 if not usable or 1 if usable.
  46. static int map[MAP_WIDTH][MAP_HEIGHT][MAX_TILES] = {0};
  47. // This is a array we use to draw the map on the screen
  48. static int tilemap[MAP_WIDTH][MAP_HEIGHT] = {0};
  49. int main(void)
  50. { // Initialization
  51. //--------------------------------------------------------------------------------------
  52. const int screenWidth = 800;
  53. const int screenHeight = 450;
  54. InitWindow(screenWidth, screenHeight, "raylib example.");
  55. //
  56. // These are the tiles with which the map will be generated. You can set the amount you want to use in the
  57. // define MAX_TILES x
  58. // The code checks the connections between these tiles by reading if a 1 is on the center top/left/right/bottom.
  59. // Also the draw routine draws the 1's as black.
  60. //
  61. // You could change(experiment!) and for instance use maybe 4 tiles and see the output. You can change the depth in the
  62. // newmap() function for how many times a tile is going to be selected and fit into the map.
  63. int tile1[3][3] = { {0,0,0},
  64. {1,1,1},
  65. {0,0,0}};
  66. int tile2[3][3] = { {0,1,0},
  67. {0,1,0},
  68. {0,1,0}};
  69. int tile3[3][3] = { {1,1,1},
  70. {1,1,1},
  71. {1,1,1}};
  72. int tile4[3][3] = { {0,1,0},
  73. {1,1,1},
  74. {0,1,0}};
  75. int tile5[3][3] = { {0,1,0},
  76. {1,1,0},
  77. {0,0,0}};
  78. int tile6[3][3] = { {0,1,0},
  79. {0,1,1},
  80. {0,0,0}};
  81. int tile7[3][3] = { {0,0,0},
  82. {1,1,0},
  83. {0,1,0}};
  84. int tile8[3][3] = { {0,0,0},
  85. {0,1,1},
  86. {0,1,0}};
  87. int tile9[3][3] = { {0,1,0},
  88. {1,1,1},
  89. {0,0,0}};
  90. int tile10[3][3] = { {0,1,0},
  91. {0,1,1},
  92. {0,1,0}};
  93. int tile11[3][3] = { {0,0,0},
  94. {1,1,1},
  95. {0,1,0}};
  96. int tile12[3][3] = { {0,1,0},
  97. {1,1,0},
  98. {0,1,0}};
  99. int tile13[3][3] = { {1,1,0},
  100. {1,1,0},
  101. {1,1,0}};
  102. int tile14[3][3] = { {0,1,1},
  103. {0,1,1},
  104. {0,1,1}};
  105. int tile15[3][3] = { {1,1,1},
  106. {1,1,1},
  107. {1,1,1}};
  108. //
  109. // Here we copy each individual tile into a single array that we will
  110. // use in the code.
  111. for(int i=0;i<MAX_TILES;i++){
  112. for(int y=0;y<3;y++){
  113. for(int x=0;x<3;x++){
  114. switch (i){
  115. case 0:
  116. tile[x][y][i] = tile1[x][y];
  117. break;
  118. case 1:
  119. tile[x][y][i] = tile2[x][y];
  120. break;
  121. case 2:
  122. tile[x][y][i] = tile3[x][y];
  123. break;
  124. case 3:
  125. tile[x][y][i] = tile4[x][y];
  126. break;
  127. case 4:
  128. tile[x][y][i] = tile5[x][y];
  129. break;
  130. case 5:
  131. tile[x][y][i] = tile6[x][y];
  132. break;
  133. case 6:
  134. tile[x][y][i] = tile7[x][y];
  135. break;
  136. case 7:
  137. tile[x][y][i] = tile8[x][y];
  138. break;
  139. case 8:
  140. tile[x][y][i] = tile9[x][y];
  141. break;
  142. case 9:
  143. tile[x][y][i] = tile10[x][y];
  144. break;
  145. case 10:
  146. tile[x][y][i] = tile11[x][y];
  147. break;
  148. case 11:
  149. tile[x][y][i] = tile12[x][y];
  150. break;
  151. case 12:
  152. tile[x][y][i] = tile13[x][y];
  153. break;
  154. case 13:
  155. tile[x][y][i] = tile14[x][y];
  156. break;
  157. case 14:
  158. tile[x][y][i] = tile15[x][y];
  159. break;
  160. }
  161. }
  162. }
  163. }
  164. // Here we generate the first map
  165. newmap();
  166. SetTargetFPS(10); // Set our game to run at 60 frames-per-second
  167. //--------------------------------------------------------------------------------------
  168. int cnt=0;
  169. // Main game loop
  170. while (!WindowShouldClose()) // Detect window close button or ESC key
  171. {
  172. // Update
  173. //----------------------------------------------------------------------------------
  174. //---------------------------------------------------------------------------------
  175. cnt++;
  176. if(IsKeyReleased(KEY_SPACE) || cnt>4){
  177. newmap();
  178. cnt=0;
  179. }
  180. // Draw
  181. //----------------------------------------------------------------------------------
  182. BeginDrawing();
  183. ClearBackground(RAYWHITE);
  184. for (int y = 0; y< MAP_HEIGHT ; y++)
  185. {
  186. for (int x = 0; x< MAP_WIDTH ; x++)
  187. {
  188. int x3 = x*48;
  189. int y3 = y*32;
  190. for(int y2=0;y2<3;y2++){
  191. for(int x2=0;x2<3;x2++){
  192. if( tile[x2][y2][tilemap[x][y]]==1 )DrawRectangle(x3+x2*16,y3+y2*12,16,12,BLACK);
  193. }
  194. }
  195. }
  196. }
  197. DrawText("Press space for new map.", 100, screenHeight-40, 40, LIGHTGRAY);
  198. EndDrawing();
  199. //----------------------------------------------------------------------------------
  200. }
  201. // De-Initialization
  202. //--------------------------------------------------------------------------------------
  203. CloseWindow(); // Close window and OpenGL context
  204. //--------------------------------------------------------------------------------------
  205. return 0;
  206. }
  207. void newmap(){
  208. // Here we reset the map. Meaning nothing will be in the memory.
  209. resetmap();
  210. // here we generate the new map. I have set the depth to a number where it generates
  211. // output that I thought was ok!
  212. int depth=GetRandomValue(100,400);
  213. for(int i=0;i<depth;i++){
  214. setrandomtile(GetRandomValue(0,MAP_WIDTH-1),GetRandomValue(0,MAP_HEIGHT-1));
  215. }
  216. // Here we copy the remaining tile into the tilemap that we use to draw it onto the screen.
  217. finishmap();
  218. };
  219. void finishmap(){
  220. for(int y=0;y<MAP_HEIGHT;y++){
  221. for(int x=0;x<MAP_WIDTH;x++){
  222. for(int i=0;i<MAX_TILES;i++){
  223. if(map[x][y][i]>-1){
  224. tilemap[x][y] = i;
  225. }
  226. }
  227. }}
  228. };
  229. void resetmap(){
  230. for(int y=0;y<MAP_HEIGHT;y++){
  231. for(int x=0;x<MAP_WIDTH;x++){
  232. for(int i=0;i<MAX_TILES;i++){
  233. map[x][y][i] = 1; // set every tile flag to usable(1)
  234. }
  235. }
  236. }
  237. };
  238. bool domainhasleftsidedconnections(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[0][1][i]==1){
  242. return true;//the tile flag has a left side
  243. }
  244. }
  245. return false;//the map array has no tiles connecting on the left side
  246. };
  247. bool domainhasrightsidedconnections(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[2][1][i]==1){
  251. return true;
  252. }
  253. }
  254. return false;
  255. };
  256. bool domainhastopsidedconnections(int x,int y){
  257. if(x<0 || y<0 || x>=MAP_WIDTH || y>=MAP_HEIGHT)return false;
  258. for(int i=0;i<MAX_TILES;i++){
  259. if(map[x][y][i]==1 && tile[1][0][i]==1){
  260. return true;
  261. }
  262. }
  263. return false;
  264. };
  265. bool domainhasbottomsidedconnections(int x,int y){
  266. if(x<0 || y<0 || x>=MAP_WIDTH || y>=MAP_HEIGHT)return false;
  267. for(int i=0;i<MAX_TILES;i++){
  268. if(map[x][y][i]==1 && tile[1][2][i]==1){
  269. return true;
  270. }
  271. }
  272. return false;
  273. };
  274. void removeleftsidedconnections(int x,int y){
  275. if(x<0 || y<0 || x>=MAP_WIDTH || y>=MAP_HEIGHT)return;
  276. for(int i=0;i<MAX_TILES;i++){
  277. if(map[x][y][i]==1 && tile[0][1][i]==1){ //if the flag of the tile is enabled(1) and the tile has a connection on the left(x)and center(y) spot
  278. map[x][y][i]=-1; // disable the flag
  279. }
  280. }
  281. };
  282. void removerightsidedconnections(int x,int y){
  283. if(x<0 || y<0 || x>=MAP_WIDTH || y>=MAP_HEIGHT)return;
  284. for(int i=0;i<MAX_TILES;i++){
  285. if(map[x][y][i]==1 && tile[2][1][i]==1){
  286. map[x][y][i]=-1;
  287. }
  288. }
  289. };
  290. void removetopsidedconnections(int x,int y){
  291. if(x<0 || y<0 || x>=MAP_WIDTH || y>=MAP_HEIGHT)return;
  292. for(int i=0;i<MAX_TILES;i++){
  293. if(map[x][y][i]==1 && tile[1][0][i]==1){
  294. map[x][y][i]=-1;
  295. }
  296. }
  297. };
  298. void removebottomsidedconnections(int x,int y){
  299. if(x<0 || y<0 || x>=MAP_WIDTH || y>=MAP_HEIGHT)return;
  300. for(int i=0;i<MAX_TILES;i++){
  301. if(map[x][y][i]==1 && tile[1][2][i]==1){
  302. map[x][y][i]=-1;
  303. }
  304. }
  305. };
  306. void setrandomtile(int x,int y){
  307. bool exitloop=false;
  308. int count=0;
  309. while(exitloop==false){
  310. count++;
  311. int val=GetRandomValue(0,MAX_TILES-1);//get a random number
  312. if(map[x][y][val]>-1){ //if the random number inside the map flag position is enabled(on the list!)
  313. for(int i=0;i<MAX_TILES;i++){ //disable every flag
  314. map[x][y][i]=-1;
  315. }
  316. map[x][y][val]=1;//enable the one we randomly created
  317. exitloop = true;
  318. break;
  319. }
  320. if(count>500)exitloop=true;//if anything goes wrong than exit
  321. }
  322. // Update around the new tile so that the connections make sense
  323. updatedomain(x-1,y);
  324. updatedomain(x+1,y);
  325. updatedomain(x,y-1);
  326. updatedomain(x,y+1);
  327. };
  328. void updatedomain(int x,int y){
  329. if(x<0 || y<0 || x>=MAP_WIDTH || y>=MAP_HEIGHT)return;
  330. // Enable every tile flag
  331. for(int i=0;i<MAX_TILES;i++){
  332. map[x][y][i]=1;
  333. }
  334. // check sides and disable tiles pieces with connections that are not present.
  335. if(domainhasbottomsidedconnections(x,y-1)==false)removetopsidedconnections(x,y);
  336. if(domainhastopsidedconnections(x,y+1)==false)removebottomsidedconnections(x,y);
  337. if(domainhasleftsidedconnections(x+1,y)==false)removerightsidedconnections(x,y);
  338. if(domainhasrightsidedconnections(x-1,y)==false)removeleftsidedconnections(x,y);
  339. };