public static bool CheckForCollisions(PSystem system, Planet pp, Planet moon, int planetDistanceApart, int moonDistanceFromSun, int moonDistanceApart, GalaxyManager galaxyManager) { // Generate locations of: Other planet, basePlanetPos+MoonPos, OtherMoonParentPos+MoonPos if (system.PlanetCount >= 1) { float moon1PosX = 0, moon1PosY = 0; // Outside because used twice float Distance; foreach (Planet p in system.GetPlanets()) { if (moon.IDToOrbit == p.Id) { continue; } // Planet 1 Calculation float planet1PosX, planet1PosY; GetPosition(p, out planet1PosX, out planet1PosY); // Planet 2 Calculation float planet2PosX, planet2PosY; GetPosition(pp, out planet2PosX, out planet2PosY); // Moon 1 Calculation GetPosition(moon, planet2PosX, planet2PosY, out moon1PosX, out moon1PosY); // Distance check between points Distance = Distance2D(planet1PosX, planet1PosY, moon1PosX, moon1PosY); if (Distance < planetDistanceApart) // We don't want moons too close to other planets! { return(true); } // Distance check between points Distance = Distance2D(0, 0, moon1PosX, moon1PosY); // Assuming sun is 0,0 if (Distance < moonDistanceFromSun) // We don't want moons too close to the sun! { return(true); } } // This is in a second forloop because otherwise we do waaaay extra calculations foreach (Planet m in system.GetMoons()) { // We need to get the parent planet to get the location // Planet 1 Calculation float planetParentPosX, planetParentPosY; GetPosition((Planet)galaxyManager.GetArea(m.IDToOrbit), out planetParentPosX, out planetParentPosY); // Moon 2 Calculation float moon2PosX, moon2PosY; GetPosition(m, planetParentPosX, planetParentPosY, out moon2PosX, out moon2PosY); // Distance check between points Distance = Distance2D(moon1PosX, moon1PosY, moon2PosX, moon2PosY); if (Distance < moonDistanceApart) // We don't want moons too close to other moons! { return(true); } } } return(false); // Return false if no collisions }
public static PSystem GenerateStarsystem(int numberOfPlanets, int ID, GalaxyManager galaxyManager, LocalIDManager idManager, GalaxyRegistrationManager rm, LocatorService ls) { bool hasPort = false; // Add sun float size = r.Next(100, 255) / 100f; float mass = r.Next(50, 255) / 10f; float innerGravity = mass / 1875f; float outerGravity = innerGravity / 6f; var s = (SunTypes[])Enum.GetValues(typeof(SunTypes)); int type = r.Next(0, s.Length); var star = new Star(size, mass, innerGravity, outerGravity, s[type]); var system = new PSystem(star, ID, ls); system.AreaSize = baseBorder + r.Next(0, borderVariation); // Add Name system.AreaName = NameProvider.GetRandomName(); rm.RegisterObject(system); for (int i = 0; i < numberOfPlanets; i++) { Planet planet; bool SatisfiedWithResults = false; int numAttempts = MaxAttempts; system.IsCluster = false; int clusterType = 0; planet = new Planet(system, 0, ls); planet.AreaName = system.AreaName + " " + ToRoman(i + 1); if (r.Next(0, 101) > (100 - chanceOfCluster)) // Chance for planets to cluster { system.IsCluster = true; clusterType = r.Next(0, clusterRadii.Count()); // Get which type of cluster } planet.Init(i, moonChance, minDistance, incrementOfOrbit, variationOfOrbit, 0, r.Next()); SatisfiedWithResults = !CheckForCollisions(system, planet); // True if we find any collisions. while (!SatisfiedWithResults)// While we're not content with the generation { planet.Init(i, moonChance, minDistance, incrementOfOrbit, variationOfOrbit, 0, r.Next()); SatisfiedWithResults = !CheckForCollisions(system, planet); // True if we find any collisions. if (SatisfiedWithResults && system.IsCluster && i > 0) { SatisfiedWithResults = CheckForCollisions(system, planet, clusterRadii[clusterType]); // Cluster planets } numAttempts++; if (numAttempts >= MaxAttempts && !system.IsCluster) { break; // Breaks out of infinite pass if it ever occurs } else if (numAttempts >= MaxAttempts && system.IsCluster) { i = 0; // Reset the whole operation because there's a bad system being generated. foreach (Planet pll in system.GetPlanets()) { system.RemovePlanet(pll); rm.DeRegisterObject(pll); } break; } } if (SatisfiedWithResults == false) // Don't add a colliding planet! { //Logger.log(Log_Type.WARNING, "Skipped adding a planet due to MaxAttempts being too much"); continue; } planet.Id = idManager.PopFreeID(); system.AddPlanet(planet); planet.ParentAreaID = system.Id; rm.RegisterObject(planet); if (system.AreaSize < planet.Distance - baseBorder) // If we're skinny, throw in a little extra space { system.AreaSize = planet.Distance + baseBorder + r.Next(0, borderVariation); } } foreach (Planet p in system.GetPlanets()) { _generateMoons(system, p, galaxyManager, idManager, rm, ls); } foreach (Planet m in system.GetMoons()) { if (system.AreaSize < ls.AreaLocator.GetArea(m.IDToOrbit).Distance + m.Distance + baseBorder) { // If we're skinny, throw in a little extra space system.AreaSize = ls.AreaLocator.GetArea(m.IDToOrbit).Distance + m.Distance + baseBorder + r.Next(0, borderVariation); } } // Port Generation if (r.Next(0, 100) > 100 - portChance && !hasPort) { if (system.MoonCount > 0) { for (int i = 0; i < system.MoonCount; i++) { } int cr = r.Next(0, system.MoonCount + 1); // Finds moon to turn into port. int currentMoon = 0; // Used to get the moon foreach (Planet m in system.GetMoons()) { if (currentMoon == cr) { Planet moonToPort = m; moonToPort.ParentAreaID = system.Id; system.RemoveMoon(m); rm.DeRegisterObject(m); Port por = new Port(idManager.PopFreeID(), moonToPort, system.AreaName, ShipStatManager.StatShipList.ToList <ShipStats>(), ls); // Converts a moon into a port. system.AddPort(por); rm.RegisterObject(por); hasPort = true; break; } currentMoon++; } } } return(system); }
public static bool CheckForCollisions(PSystem system, Planet pp, int PlanetApartDistanceToCheck) { if (system.PlanetCount >= 1) { float planet1PosX = 0, planet1PosY = 0; float planet2PosX = 0, planet2PosY = 0; float moon2PosX, moon2PosY; foreach (Planet p in system.GetPlanets()) { // Planet 1 Calculation GetPosition(p, out planet1PosX, out planet1PosY); // Planet 2 Calculation GetPosition(pp, out planet2PosX, out planet2PosY); // Distance check between points float Distance = Distance2D(planet1PosX, planet1PosY, planet2PosX, planet2PosY); if (Distance < PlanetApartDistanceToCheck) // Collision between planets { return(true); } } if (system.MoonCount <= 0) // Only continue if we've got moons, otherwise we're done. { return(false); } foreach (Planet m in system.GetMoons()) { if (m.IDToOrbit == pp.Id) // If we're checking parent, skip { continue; } foreach (Planet p in system.GetPlanets()) { if (m.IDToOrbit != p.Id) // If we're checking parent, skip { continue; } // Planet 1 Calculation GetPosition(p, out planet1PosX, out planet1PosY); // Moon calculation GetPosition(m, planet1PosX, planet1PosY, out moon2PosX, out moon2PosY); // Distance check between points float Distance; Distance = Distance2D(planet2PosX, planet2PosY, moon2PosX, moon2PosY); if (Distance < PlanetApartDistanceToCheck) // Collision between planets and moons { return(true); } } } } return(false); // Return false if no collisions }