Skip to content

Commit

Permalink
Unified planet and moon definition searching, so both can use mandato…
Browse files Browse the repository at this point in the history
…ry planets
  • Loading branch information
8vogt committed Feb 23, 2021
1 parent bd547c5 commit 2403263
Showing 1 changed file with 65 additions and 43 deletions.
108 changes: 65 additions & 43 deletions SEWorldGenPlugin/Generator/MyStarSystemGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,46 @@ public partial class MyStarSystemGenerator : MySessionComponentBase
/// </summary>
public MyObjectBuilder_SystemData StarSystem;

/// <summary>
/// Planets loaded with the world, that are not blacklisted, suns or moons
/// </summary>
private List<MyPlanetGeneratorDefinition> m_planets;

/// <summary>
/// List of planets used for definition finding in unique mode.
/// </summary>
private List<MyPlanetGeneratorDefinition> m_uniquePlanets;

/// <summary>
/// All planets defined as suns, loaded with the world
/// </summary>
private List<MyPlanetGeneratorDefinition> m_suns;

/// <summary>
/// All planets defined as Gas giants loaded with the world
/// </summary>
private List<MyPlanetGeneratorDefinition> m_gasGiants;

/// <summary>
/// All planets set as moons loaded with the world
/// </summary>
private List<MyPlanetGeneratorDefinition> m_moons;

/// <summary>
/// List of moons used for definition finding in unique mode.
/// </summary>
private List<MyPlanetGeneratorDefinition> m_uniqueMoons;

/// <summary>
/// All planets specified as mandatory
/// </summary>
private List<MyPlanetGeneratorDefinition> m_mandatoryPlanets;

/// <summary>
/// All moons specified as mandatory
/// </summary>
private List<MyPlanetGeneratorDefinition> m_mandatoryMoons;

/// <summary>
/// Initializes the system generator and generates a new system if enabled and no system was
/// generated yet.
Expand Down Expand Up @@ -133,6 +163,7 @@ public override void LoadData()
m_suns = new List<MyPlanetGeneratorDefinition>();
m_gasGiants = new List<MyPlanetGeneratorDefinition>();
m_mandatoryPlanets = new List<MyPlanetGeneratorDefinition>();
m_mandatoryMoons = new List<MyPlanetGeneratorDefinition>();

MyPluginLog.Log("Load Star system generator component completed");
}
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -484,62 +518,44 @@ private bool IsMoonPositionObstructed(Vector3D position, double moonDiameter, My
return false;
}

/// <summary>
/// Tries to find a planet defintion for a moon, that orbits a
/// planet with diameter parentPlanetDiameter.
/// </summary>
/// <param name="parentPlanetDiameter">Diameter of the parent planet</param>
/// <returns>The definition found for the moon</returns>
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;
}

/// <summary>
/// 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.
/// </summary>
/// <param name="maxDiameter">Max diameter of the planet in meters</param>
/// <returns>A definition of a planet that tries to be smaller than maxDiameter</returns>
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<MyPlanetGeneratorDefinition>(m_moons);
planets = m_uniqueMoons;
}
else
{
m_uniquePlanets = new List<MyPlanetGeneratorDefinition>(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
Expand All @@ -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;
}
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 2403263

Please sign in to comment.