CubicCrabGrid GetSwarms(int[] populationConcentration, int number, int emptySpaceAroundCenters) { var crabGrid = new CubicCrabGrid(); for (int i = 1; i <= number; i++) { Vector3Int newSwarmPlace = gridTracker.GetRandomSwarmPlacementInCubic(); bool acceptablePlacement = false; int attemptNumber = 0; while (!acceptablePlacement) { attemptNumber++; if (!crabGrid.IsAcceptableSwarmPlace(newSwarmPlace, emptySpaceAroundCenters) || !gridTracker.SwarmPlacementIsWithinPlayArea(newSwarmPlace)) { newSwarmPlace = gridTracker.GetRandomSwarmPlacementInCubic(); if (attemptNumber >= maximumAttempts) { Debug.LogWarningFormat("Attempted {0} times and couldn't find placement for swarm number {1}", attemptNumber, i); acceptablePlacement = true; } continue; } else { acceptablePlacement = true; } } crabGrid.AddSwarm(newSwarmPlace, populationConcentration); } return(crabGrid); }