So the code I’m dealing with is this:
bool[,] obstacleMap = new bool [mapSize.x, mapSize.y];
showmap(obstacleMap);
int obstacleCount = (int)(mapSize.x * mapSize.y * obstaclePercent);
int currentObstacleCount = 0;
Debug.Log("starting generation");
for ( int i = 0; i < obstacleCount; i++ ) {
Coord randomCoord = GetRandomCoord();
if ( randomCoord != mapCenter && FloodFill(obstacleMap, currentObstacleCount ) ) {
Debug.Log("coords at which obst will be placed:" + randomCoord.x + "," + randomCoord.y);
showmap(obstacleMap);
obstacleMap[randomCoord.x, randomCoord.y] = true;
currentObstacleCount++;
Vector3 obstaclePos = CoordToPosition(randomCoord.x, randomCoord.y);
Transform newObstacle = Instantiate(obstaclePrefab, obstaclePos + Vector3.up * .5f, Quaternion.identity ) as Transform;
newObstacle.parent = mapHolder;
newObstacle.name = "Obstacle" + i;
}
}
}
GetRandomCoord() just gets a random coord that is guaranteed to be on the map, and never repeats itself.
showmap() is just a quick debug function that prints obstacleMap in the log.
FloodFill works fine as far as i can tell, here it is anyways:
public bool FloodFill(bool[,] obstacleMap, int inaccessibleTiles ) {
Coord[] nextDir = {
new Coord(-1,0),
new Coord(0,1),
new Coord(1,0),
new Coord(0,-1)
}; //LEFT TOP RIGHT BOTTOM
Queue<Coord> q = new Queue<Coord> ();
bool[,] map = obstacleMap;
int targetTiles = mapSize.x * mapSize.y - inaccessibleTiles;
int currentTiles = 1;
q.Enqueue(mapCenter);
map[mapCenter.x, mapCenter.y] = true;
while ( q.Count > 0 ) {
Coord tile = q.Dequeue();
Debug.Log("visited " + tile.x + tile.y + ", now looking at neighbours");
foreach ( Coord nextDelta in nextDir ) {
Coord next = tile + nextDelta;
//Debug.Log("next is " + next.x + " " + next.y);
if ( next.x >= 0 && next.x < mapSize.x && next.y >= 0 && next.y < mapSize.y ) {
Debug.Log("next (at "+next.x+","+next.y+") is inside the map. is occupied: " + map[next.x,next.y] );
if ( !map[next.x, next.y] ) {
q.Enqueue(next);
map[next.x, next.y] = true;
currentTiles++;
}
}
}
}
Debug.Log("currentTiles: " + currentTiles);
Debug.Log("targetTiles: " + targetTiles);
return currentTiles == targetTiles;
}
edit: I’m an idiot and forgot to actually mention my issue:
after running obstacleMap[randomCoord.x, randomCoord.y] = true; the map is fully true in every position.
edit 2: its completely full even after running FloodFill() in the if.
please help as I’m losing my mind