diff --git a/SEWorldGenPlugin/Generator/MyStarSystemGenerator.cs b/SEWorldGenPlugin/Generator/MyStarSystemGenerator.cs index 8338eff..b50fe8f 100644 --- a/SEWorldGenPlugin/Generator/MyStarSystemGenerator.cs +++ b/SEWorldGenPlugin/Generator/MyStarSystemGenerator.cs @@ -57,16 +57,46 @@ public partial class MyStarSystemGenerator : MySessionComponentBase /// public MyObjectBuilder_SystemData StarSystem; + /// + /// Planets loaded with the world, that are not blacklisted, suns or moons + /// private List m_planets; + /// + /// List of planets used for definition finding in unique mode. + /// + private List m_uniquePlanets; + + /// + /// All planets defined as suns, loaded with the world + /// private List m_suns; + /// + /// All planets defined as Gas giants loaded with the world + /// private List m_gasGiants; + /// + /// All planets set as moons loaded with the world + /// private List m_moons; + /// + /// List of moons used for definition finding in unique mode. + /// + private List m_uniqueMoons; + + /// + /// All planets specified as mandatory + /// private List m_mandatoryPlanets; + /// + /// All moons specified as mandatory + /// + private List m_mandatoryMoons; + /// /// Initializes the system generator and generates a new system if enabled and no system was /// generated yet. @@ -133,6 +163,7 @@ public override void LoadData() m_suns = new List(); m_gasGiants = new List(); m_mandatoryPlanets = new List(); + m_mandatoryMoons = new List(); MyPluginLog.Log("Load Star system generator component completed"); } @@ -164,6 +195,9 @@ protected override void UnloadData() m_gasGiants?.Clear(); m_moons?.Clear(); m_mandatoryPlanets?.Clear(); + m_mandatoryMoons?.Clear(); + m_uniqueMoons?.Clear(); + m_uniquePlanets?.Clear(); UnloadNetworking(); @@ -399,7 +433,7 @@ private MySystemPlanetMoon[] GeneratePlanetMoons(MySystemPlanet parentPlanet) MyPluginLog.Debug("Generating moon " + i); double distance = parentPlanet.Diameter * (i + 1) + parentPlanet.Diameter * MyRandom.Instance.GetRandomFloat(1f, 1.5f); - var definition = FindMoonDefinitinon(parentPlanet.Diameter); + var definition = FindPlanetDefinitionForSize(parentPlanet.Diameter * 0.75f, true); if (definition == null) return moons; double diameter = CalculatePlanetDiameter(definition); @@ -484,62 +518,44 @@ private bool IsMoonPositionObstructed(Vector3D position, double moonDiameter, My return false; } - /// - /// Tries to find a planet defintion for a moon, that orbits a - /// planet with diameter parentPlanetDiameter. - /// - /// Diameter of the parent planet - /// The definition found for the moon - private MyPlanetGeneratorDefinition FindMoonDefinitinon(double parentPlanetDiameter) - { - if (m_moons.Count <= 0) return null; - - var settings = MySettingsSession.Static.Settings.GeneratorSettings; - - int tries = 0; - double diameter; - - MyPlanetGeneratorDefinition def; - - do - { - def = m_moons[MyRandom.Instance.Next(0, m_moons.Count)]; - diameter = CalculatePlanetDiameter(def); - - } while (diameter >= parentPlanetDiameter * 0.75f && ++tries <= MAX_DEF_FIND_ROUNDS); - - if(settings.SystemGenerator == SystemGenerationMethod.UNIQUE) - { - m_moons.Remove(def); - } - - return def; - } - /// /// Finds a fit planet definition for a planet with a diameter less than maxDiameter. /// If none can be found, a random one will be returned. /// /// Max diameter of the planet in meters /// A definition of a planet that tries to be smaller than maxDiameter - private MyPlanetGeneratorDefinition FindPlanetDefinitionForSize(double maxDiameter) + private MyPlanetGeneratorDefinition FindPlanetDefinitionForSize(double maxDiameter, bool isMoon = false) { var settings = MySettingsSession.Static.Settings.GeneratorSettings; - var planets = m_planets; + var planets = isMoon ? m_uniqueMoons : m_uniquePlanets; + var mandatory = isMoon ? m_mandatoryMoons : m_mandatoryPlanets; MyPlanetGeneratorDefinition def; double diameter = 0; int tries = 0; - if(m_planets.Count <= 0) + if(planets == null || planets.Count <= 0) { - MyPluginLog.Log("No planet definitions found. Trying again", LogLevel.WARNING); - LoadPlanetDefinitions(); - if (m_planets.Count <= 0) return null; + if (isMoon) + { + m_uniqueMoons = new List(m_moons); + planets = m_uniqueMoons; + } + else + { + m_uniquePlanets = new List(m_planets); + planets = m_uniquePlanets; + } } - if(settings.SystemGenerator >= SystemGenerationMethod.MANDATORY_FIRST && m_mandatoryPlanets.Count > 0) + if(planets.Count <= 0) { - planets = m_mandatoryPlanets; + MyPluginLog.Log("Cant find planet definition", LogLevel.WARNING); + return null; + } + + if(settings.SystemGenerator >= SystemGenerationMethod.MANDATORY_FIRST && mandatory.Count > 0) + { + planets = mandatory; } do @@ -551,12 +567,12 @@ private MyPlanetGeneratorDefinition FindPlanetDefinitionForSize(double maxDiamet if(settings.SystemGenerator == SystemGenerationMethod.MANDATORY_FIRST) { - m_mandatoryPlanets.Remove(def); + mandatory.Remove(def); } if(settings.SystemGenerator == SystemGenerationMethod.UNIQUE) { - m_planets.Remove(def); + planets.Remove(def); } return def; } @@ -604,6 +620,12 @@ private void LoadPlanetDefinitions() MyPluginLog.Log("Adding moon " + subtypeId); m_moons.Add(planet); + + if (settings.MandatoryPlanetDefinitions.Contains(subtypeId)) + { + m_mandatoryMoons.Add(planet); + } + continue; } else if (settings.SunDefinitions.Contains(subtypeId))