So I'm doing a procedural game and each room is a 5x5 grid of walkable and non-walkable areas. I'm using a 2D array like so.
1, 1, 1, 1, 1
1, 1, 1, 1, 1
1, 1, 1, 1, 1
1, 1, 1, 1, 1
1, 1, 1, 1, 1
1 is walkable 0 is non-walkable. Okay so the problem is that when I punch obstacles into my map by randomly making coords equal 0 I end up with "floating" tiles, or non-connected tiles. One way I've found to fix this is with a flood fill check before adding an obstacle. And that works, I found
this and am generally just doing what he does to solve it. But in most cases I need multiple tiles to always link to each other.
I'm doing a dungeon game and every room can have a north, east, south, and west entrance/exit. So I try to flood fill from each point. The problem is that it's treating each point as it's own separate area to fill from, so I get disconnected areas. The areas are 0,2 2,4 2,0 4,2 respective to n, e, s, w. I can't figure out how to get it to treat it as one area so I'm hoping someone can help or provide a different solution.
bool MapIsFullyAccessible(bool[,] obstacleArray, int currentObstacleCount, int[] currentRoom) {
bool[,] mapFlags = new bool[obstacleArray.GetLength(0), obstacleArray.GetLength(1)];
Queue<int[]> queue = new Queue<int[]>();
int accessibleTileCount = 0;
//These are north, east, south, and west toggles for exits
if (currentRoom[0] == 1) {
queue.Enqueue(new int[2] { 0, 2 } );
mapFlags[0, 2] = true;
accessibleTileCount++;
}
if (currentRoom[1] == 1) {
queue.Enqueue(new int[2] { 2, 4 } );
mapFlags[2, 4] = true;
accessibleTileCount++;
}
if (currentRoom[3] == 1) {
queue.Enqueue(new int[2] { 4, 2 } );
mapFlags[4, 2] = true;
accessibleTileCount++;
}
if (currentRoom[4] == 1) {
queue.Enqueue(new int[2] { 2, 0 } );
mapFlags[2, 0] = true;
accessibleTileCount++;
}
while (queue.Count > 0) {
int[] tile = queue.Dequeue();
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
int neighbourX = tile[0] + x;
int neighbourY = tile[1] + y;
if (x == 0 || y == 0) {
if (neighbourX >= 0 && neighbourX < obstacleArray.GetLength(0) && neighbourY >= 0 && neighbourY < obstacleArray.GetLength(1)) {
if (!mapFlags[neighbourX, neighbourY] && !obstacleArray[neighbourX, neighbourY]) {
mapFlags[neighbourX, neighbourY] = true;
queue.Enqueue(new int[2] { neighbourX, neighbourY } );
accessibleTileCount++;
}
}
}
}
}
}
int targetAccessibleTileCount = obstacleArray.GetLength(0) * obstacleArray.GetLength(1) - currentObstacleCount;
return targetAccessibleTileCount == accessibleTileCount;
}
Thanks!