ExpandingGridFloodFill.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // Quickly programmed/coded and not tested much.
  3. // Floodfill/seed fill -
  4. //
  5. // Around the start position use a for loop(s) with x and y and grow the edges that touch with
  6. // walkable area. Each loop itteration we use a larger area to loop from(optimalisation!)
  7. //
  8. // We start at the start position and stop when we reach a end position - not a real flood fil here but more usefull for checking walkability.
  9. #include "raylib.h"
  10. #include <math.h>
  11. int main(void)
  12. {
  13. // Initialization
  14. //--------------------------------------------------------------------------------------
  15. const int screenWidth = 800;
  16. const int screenHeight = 450;
  17. InitWindow(screenWidth, screenHeight, "raylib example.");
  18. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  19. //--------------------------------------------------------------------------------------
  20. int mapWidth = 10;
  21. int mapHeight = 5;
  22. float tileWidth = ceil((float)screenWidth/(float)mapWidth);
  23. float tileHeight = ceil((float)screenHeight/(float)mapHeight);
  24. int map[5][10] = { {1,1,1,1,1,1,1,1,1,1},
  25. {1,0,0,0,1,1,0,0,3,1},
  26. {1,0,0,0,1,1,0,1,1,1},
  27. {1,2,0,0,0,0,0,0,0,1},
  28. {1,1,1,1,1,1,1,1,1,1}};
  29. bool visitedmap[5][10] = {false};
  30. bool pathfound = false;
  31. // Main game loop
  32. while (!WindowShouldClose()) // Detect window close button or ESC key
  33. {
  34. // Update
  35. //----------------------------------------------------------------------------------
  36. if(IsKeyPressed(KEY_SPACE)){
  37. // Erase the visited map
  38. // find start position (2)
  39. // find end position (3)
  40. Vector2 posstart;
  41. Vector2 posend;
  42. for(int y=0;y<mapHeight;y++){
  43. for(int x=0;x<mapWidth;x++){
  44. visitedmap[y][x] = false;
  45. if(map[y][x]==2)posstart = (Vector2){x,y};
  46. if(map[y][x]==3)posend = (Vector2){x,y};
  47. }}
  48. // Flood the map until we find the destination cell/position
  49. int mx[] = {0,1,0,-1};//These are the values we use to find the above left and right cells.
  50. int my[] = {-1,0,1,0};
  51. visitedmap[(int)posstart.x][(int)posstart.y]=true; // we start by setting the vistedmap position where the flood starts true
  52. for(int size=1;size<10;size++){ // we check up to a maximum of -10 and 10 size area for the flood.
  53. for(int y=posstart.y-size;y<posstart.y+size;y++){
  54. for(int x=posstart.x-size;x<posstart.x+size;x++){
  55. if(map[y][x]==0 && visitedmap[y][x]==true){ // if we are on a walkable terrain and we have visited this area before
  56. for(int j=0;j<4;j++){ // loop through the neighbouring cells
  57. int x2=x+mx[j]; // get cell position next to current visited cell
  58. int y2=y+my[j];
  59. if(x2<0 || y2<0 || x2>mapWidth || y2>mapHeight)continue; // if we are outside of bounds then exit and continue with next loop(j)
  60. if(x2==posend.x && y2==posend.y){ // if we find the end position than set a flag
  61. pathfound=true;
  62. }
  63. if(map[y2][x2]!=0)continue; // if this cell is not walkable then exit and continue with next loop itteration(j)
  64. visitedmap[y2][x2]=true; // if we are here it means this current cell position is walkable so set the visited map to true
  65. }
  66. }
  67. }}
  68. }
  69. }
  70. //----------------------------------------------------------------------------------
  71. // Draw
  72. //----------------------------------------------------------------------------------
  73. BeginDrawing();
  74. ClearBackground(GREEN);
  75. //drawmap
  76. for(int y=0;y<5;y++){
  77. for(int x=0;x<10;x++){
  78. if(map[y][x]==1){ // wall
  79. DrawRectangle(x*tileWidth,y*tileHeight,tileWidth,tileHeight,BLACK);
  80. }
  81. if(map[y][x]==2){ // start position
  82. DrawRectangle(x*tileWidth,y*tileHeight,tileWidth,tileHeight,RED);
  83. DrawText(FormatText("S"),x*tileWidth+tileWidth/8,y*tileHeight+tileHeight/8,tileWidth,WHITE);
  84. }
  85. if(map[y][x]==3){ // end position
  86. DrawRectangle(x*tileWidth,y*tileHeight,tileWidth,tileHeight,YELLOW);
  87. DrawText(FormatText("X"),x*tileWidth+tileWidth/8,y*tileHeight+tileHeight/8,tileWidth,WHITE);
  88. }
  89. if(visitedmap[y][x]==true){
  90. DrawText(FormatText("."),x*tileWidth,y*tileHeight,tileWidth/2,WHITE);
  91. }
  92. }}
  93. if(pathfound)DrawText(FormatText("PAth was found/is reachable.."),0,0,20,WHITE);
  94. EndDrawing();
  95. //----------------------------------------------------------------------------------
  96. }
  97. // De-Initialization
  98. //--------------------------------------------------------------------------------------
  99. CloseWindow(); // Close window and OpenGL context
  100. //--------------------------------------------------------------------------------------
  101. return 0;
  102. }