Template_-_BreathFDist_TileMap.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //
  2. // Genetic Algorithm Example
  3. #include "raylib.h"
  4. #include <math.h>
  5. #define MAX_PATH 1024
  6. enum terrain{TREE=1,GROUND=0,AIPLAYER1=2,AIPLAYER2=3,GOAL=4,GOALREACHED=5};
  7. enum flag{UP,DOWN,LEFT,RIGHT,WAIT};
  8. //
  9. // You can set any number of 3 and 2 on the map. more 4's untested. keep the borders(1) intact(memory errors)
  10. //
  11. //1=border,2=ai unit,3=defensiveunit,4=objective
  12. int map1[10][10] = { {1,1,1,1,1,1,1,1,1,1},
  13. {1,0,0,0,0,1,0,0,3,1},
  14. {1,0,0,0,0,1,0,0,0,1},
  15. {1,3,0,0,0,1,0,0,0,1},
  16. {1,0,0,0,0,1,1,0,1,1},
  17. {1,0,0,0,0,1,0,0,0,1},
  18. {1,1,1,0,1,1,0,0,0,1},
  19. {1,0,0,0,0,0,2,0,0,1},
  20. {1,0,0,0,0,0,0,0,0,1},
  21. {1,1,1,1,1,1,1,1,1,1}
  22. };
  23. int map[10][10] = {0};
  24. int dijkstramap[10][10]={0};
  25. typedef struct pathnode{
  26. int x;
  27. int y;
  28. }pathnode;
  29. void makedijkstramap();
  30. static float distance(float x1,float y1,float x2,float y2);
  31. static float edistance(float x1,float y1,float x2,float y2);
  32. int topdijkstralen=0;
  33. int screenWidth;
  34. int screenHeight;
  35. int tileWidth,tileHeight,mapWidth,mapHeight;
  36. int main(void)
  37. {
  38. // Initialization
  39. //--------------------------------------------------------------------------------------
  40. screenWidth = 800;
  41. screenHeight = 600;
  42. mapWidth = 10;
  43. mapHeight = 10;
  44. tileWidth = ceil((float)screenWidth/(float)mapWidth);
  45. tileHeight = ceil((float)screenHeight/(float)mapHeight);
  46. InitWindow(screenWidth, screenHeight, "raylib example.");
  47. SetTargetFPS(200); // Set our game to run at 60 frames-per-second
  48. //--------------------------------------------------------------------------------------
  49. //makedijkstramap(); // The 4 = the goal that gets flooded from...
  50. for(int y=0;y<mapHeight;y++){
  51. for(int x=0;x<mapHeight;x++){
  52. map[y][x]= map1[y][x];
  53. }}
  54. int playposition=0;
  55. // Main game loop
  56. while (!WindowShouldClose()) // Detect window close button or ESC key
  57. {
  58. // Update
  59. //----------------------------------------------------------------------------------
  60. //----------------------------------------------------------------------------------
  61. // Draw
  62. //----------------------------------------------------------------------------------
  63. BeginDrawing();
  64. ClearBackground(RAYWHITE);
  65. //drawmap
  66. for(int y=0;y<10;y++){
  67. for(int x =0;x<10;x++){
  68. int m=map[y][x];
  69. if(m==TREE){
  70. DrawRectangle(x*tileWidth,y*tileHeight,tileWidth,tileHeight,(Color){40,90,20,255});
  71. DrawCircle(x*tileWidth+tileWidth/1.5,y*tileHeight+tileHeight/1.5,tileWidth/5,(Color){20,50,20,255});
  72. DrawRectangle(x*tileWidth+tileWidth/2.2,y*tileHeight+tileHeight/2,tileWidth/8,tileHeight/3,BROWN);
  73. DrawCircle(x*tileWidth+tileWidth/2,y*tileHeight+tileHeight/3,tileWidth/4,(Color){120,250,20,255});
  74. DrawCircle(x*tileWidth+tileWidth/2.2,y*tileHeight+tileHeight/4,tileWidth/9,(Color){220,255,220,155});
  75. }
  76. if(m==GROUND){
  77. DrawRectangle(x*tileWidth,y*tileHeight,tileWidth,tileHeight,DARKGREEN);
  78. DrawRectangle(x*tileWidth+5,y*tileHeight+10,2,1,GREEN);
  79. DrawRectangle(x*tileWidth+tileWidth/6,y*tileHeight+tileHeight/6,2,1,GREEN);
  80. DrawRectangle(x*tileWidth+tileWidth/1.5,y*tileHeight+tileHeight/1.5,2,1,GREEN);
  81. DrawRectangle(x*tileWidth+tileWidth/2,y*tileHeight+tileHeight/2,2,1,GREEN);
  82. }
  83. if(m==AIPLAYER1){
  84. DrawRectangle(x*tileWidth,y*tileHeight,tileWidth,tileHeight,BLUE);
  85. DrawText("Player",x*tileWidth,y*tileHeight+tileHeight/4,16,BLACK);
  86. }
  87. if(m==AIPLAYER2){
  88. DrawRectangle(x*tileWidth,y*tileHeight,tileWidth,tileHeight,RED);
  89. DrawText("Ai_Player",x*tileWidth,y*tileHeight+tileHeight/4,16,WHITE);
  90. }
  91. if(m==GOAL){
  92. DrawRectangle(x*tileWidth,y*tileHeight,tileWidth,tileHeight,WHITE);
  93. DrawText("City",x*tileWidth+tileWidth/4,y*tileHeight+tileHeight/4,26,BLACK);
  94. }
  95. if(m==GOALREACHED){
  96. DrawRectangle(x*tileWidth,y*tileHeight,tileWidth,tileHeight,YELLOW);
  97. DrawText("Captured",x*tileWidth+4,y*tileHeight+tileHeight/4,16,BLACK);
  98. }
  99. DrawText(FormatText("%i",dijkstramap[y][x]),x*tileWidth,y*tileHeight,20,BLACK);
  100. }
  101. }
  102. DrawText("Press space for new simulation.(Autorun=on)",10,10,26,BLACK);
  103. DrawText("Press space for new simulation.(Autorun=on)",9,9,26,WHITE);
  104. EndDrawing();
  105. //----------------------------------------------------------------------------------
  106. }
  107. // De-Initialization
  108. //--------------------------------------------------------------------------------------
  109. CloseWindow(); // Close window and OpenGL context
  110. //--------------------------------------------------------------------------------------
  111. return 0;
  112. }
  113. void makedijkstramap(){
  114. int startx;
  115. int starty;
  116. // First make sure every map value is 0.
  117. for(int y=0;y<mapHeight;y++){
  118. for(int x=0;x<mapWidth;x++){
  119. dijkstramap[y][x] = 0;
  120. if(map1[y][x]==1)dijkstramap[y][x]=-1;
  121. if(map1[y][x]==4){
  122. startx = x;
  123. starty = y;
  124. }
  125. }}
  126. //
  127. //
  128. // Flood the map with distances from the start.
  129. //
  130. // We store the distance on each map cell if there is no wall there.
  131. //
  132. struct pathnode list[MAX_PATH];
  133. dijkstramap[starty][startx]=1;
  134. int listlen=0;
  135. list[listlen].x=startx;
  136. list[listlen].y=starty;
  137. listlen+=1;
  138. int failed=0;
  139. // 4 way search! left/up/down/right
  140. int dx[8]={ -1,0,1,-1,1,-1,0,1};
  141. int dy[8]={-1,-1,-1,0,0,1,1,1};
  142. // While we have a list to work with
  143. while(listlen>0){
  144. // Take the first value from the array.
  145. int x1=list[0].x;
  146. int y1=list[0].y;
  147. // shift all up.
  148. for(int i=0;i<listlen;i++){
  149. list[i].x = list[i+1].x;
  150. list[i].y = list[i+1].y;
  151. }
  152. // Decrease list length
  153. listlen-=1;
  154. //
  155. // Here we check around our current position.
  156. for(int i=0;i<8;i++){
  157. int nx = x1+dx[i];
  158. int ny = y1+dy[i];
  159. if(nx<0 || ny<0 || nx>= mapWidth || ny>= mapHeight)continue;
  160. // If we can get there then put the new distance there and add this position
  161. // to the list.
  162. if(dijkstramap[ny][nx]==0 && map[ny][ny]!=1){
  163. dijkstramap[ny][nx]=dijkstramap[y1][x1]+1;
  164. if(dijkstramap[ny][nx]>topdijkstralen)topdijkstralen=dijkstramap[ny][nx];
  165. // add to last
  166. //
  167. list[listlen].x = nx;
  168. list[listlen].y = ny;
  169. listlen++;
  170. //
  171. }
  172. }
  173. // Error?
  174. failed+=1;
  175. if(failed>160000)return;
  176. }
  177. }
  178. // Manhattan Distance (less precise)
  179. float distance(float x1,float y1,float x2,float y2){
  180. return (float)abs(x2-x1)+abs(y2-y1);
  181. }
  182. // Euclidean distance (more precise)
  183. float edistance(float x1,float y1,float x2,float y2){
  184. return sqrt( (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2) );
  185. }