public StarSystem SetHomeworld(Empire empire, out Planet homePlanet)
{
lock (_lockGalaxy) //Won't modify anything, but ensures that galaxy is created and won't throw exceptions due to contents being modified
{
Random r = new Random();
List<StarSystem> potentialSystems = new List<StarSystem>(starSystems);
while (true)
{
if (potentialSystems.Count == 0)
{
homePlanet = null;
return null;
}
var potentialSystem = potentialSystems[r.Next(potentialSystems.Count)];
if (potentialSystem.EmpiresWithPlanetsInThisSystem.Count > 0)
{
potentialSystems.Remove(potentialSystem);
continue;
}
//Validation checks
bool FourParsecsSystem = false;
bool SixParsecsSystem = false;
bool AtLeastSixParsecsAwayFromOthers = true;
foreach (StarSystem starSystem in starSystems)
{
if (potentialSystem != starSystem)
{
int x = potentialSystem.X - starSystem.X;
int y = potentialSystem.Y - starSystem.Y;
double distance = Math.Sqrt((x * x) + (y * y));
if (distance <= PARSEC_SIZE_IN_PIXELS * 4)
{
if (!FourParsecsSystem)
{
FourParsecsSystem = true;
}
else
{
//Already have another star system that's within 4 parsecs
SixParsecsSystem = true;
}
if (starSystem.EmpiresWithPlanetsInThisSystem.Count > 0)
{
AtLeastSixParsecsAwayFromOthers = false;
break;
}
}
else if (distance <= PARSEC_SIZE_IN_PIXELS * 6)
{
SixParsecsSystem = true;
if (starSystem.EmpiresWithPlanetsInThisSystem.Count > 0)
{
AtLeastSixParsecsAwayFromOthers = false;
break;
}
}
}
}
if (FourParsecsSystem && SixParsecsSystem && AtLeastSixParsecsAwayFromOthers)
{
potentialSystem.SetHomeworld(empire, out homePlanet, r);
return potentialSystem;
}
potentialSystems.Remove(potentialSystem);
}
}
}