Skip to content

Commit

Permalink
Merge pull request #133 from Touhma/OrbitRework
Browse files Browse the repository at this point in the history
Orbit rework
  • Loading branch information
innominata authored Aug 12, 2021
2 parents f0b5fb3 + 1b809c0 commit 8e4c7a4
Show file tree
Hide file tree
Showing 25 changed files with 516 additions and 383 deletions.
4 changes: 2 additions & 2 deletions GalacticScale2/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.0.84.1")]
[assembly: AssemblyFileVersion("2.0.84.1")]
[assembly: AssemblyVersion("2.0.85.0")]
[assembly: AssemblyFileVersion("2.0.85.0")]
2 changes: 1 addition & 1 deletion GalacticScale2/Scripts/Bootstrap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace GalacticScale
{
[BepInPlugin("dsp.galactic-scale.2", "Galactic Scale 2 Plug-In", "2.0.84.1")]
[BepInPlugin("dsp.galactic-scale.2", "Galactic Scale 2 Plug-In", "2.0.85.0")]
[BepInDependency("space.customizing.console", BepInDependency.DependencyFlags.SoftDependency)]
public class Bootstrap : BaseUnityPlugin
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void Init()
//OnImportPositionsClick(null);
}

public void Generate(int starCount)
public void Generate(int starCount, StarData birthStar = null)
{
//starCount = 2048;
var random = new GS2.Random(GSSettings.Seed);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void Init()
//OnImportPositionsClick(null);
}

public void Generate(int starCount)
public void Generate(int starCount, StarData birthStar = null)
{
foreach (var a in stars) GS2.Log(a.Name);
var p = new GSPlanets();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public partial class GS2Generator : iConfigurableGenerator

public string GUID => "space.customizing.generators.gs2";

public void Generate(int starCount)
public void Generate(int starCount, StarData birthStar = null)
{
Log($"Start {GSSettings.Seed}");
GSSettings.Reset(GSSettings.Seed);
Expand Down Expand Up @@ -97,6 +97,7 @@ public void Generate(int starCount)
}

EnsureBirthSystemHasTi();
GSSettings.BirthPlanetName = birthPlanet.Name;
}


Expand Down
315 changes: 164 additions & 151 deletions GalacticScale2/Scripts/GalacticScale2.0/Generators/GS2Dev/GS2Generator.cs

Large diffs are not rendered by default.

83 changes: 65 additions & 18 deletions GalacticScale2/Scripts/GalacticScale2.0/Generators/GS2Dev/Orbits.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using static GalacticScale.GS2;

namespace GalacticScale.Generators
{
public partial class GS2Generator2 : iConfigurableGenerator
{
public void NamePlanets(GSStar star)
{
var i = 1;
foreach (var planet in star.Planets)
{
planet.Name = $"{star.Name} - {RomanNumbers.roman[i]}";
var j = 1;
foreach (var moon in planet.Moons) {
moon.Name = $"{planet.Name} - {RomanNumbers.roman[j]}";
var h = 1;
foreach (var moon2 in moon.Moons)
{
moon2.Name = $"{moon.Name} - {RomanNumbers.roman[h]}";
h++;
}
j++;
}
i++;
}
}
private void AssignPlanetOrbits(GSStar star)
{
// GS2.Warn("Start:"+star.Planets.Count);
var r = new GS2.Random(star.Seed);
var orbits = new List<Orbit>();
ref var planets = ref star.Planets;
GSPlanets brokenPlanets = new GSPlanets();
planets.Sort(PlanetSortBySystemRadius);
// GS2.WarnJson((from s in planets select s.details).ToList());
CalculateHabitableZone(star);
var minimumOrbit = CalculateMinimumOrbit(star);
var maximumOrbit = CalculateMaximumOrbit(star);
Expand Down Expand Up @@ -40,39 +64,40 @@ private void AssignPlanetOrbits(GSStar star)
freeOrbitRanges.Clear();
freeOrbitRanges.Add((minimumOrbit, maximumOrbit));
}

// GS2.Warn("Begin Loop:" + star.Planets.Count);
for (var i = 0; i < planets.Count; i++)
{
Orbit orbit;
var planet = planets[i];
// GS2.Log($"Finding Orbit for planet index {i} - {planet.Name}");
if (planet == birthPlanet)
{
// planet.Name += " BIRTH";
continue;
}

// Log(planet.SystemRadius.ToString());
planet.OrbitInclination = 0f;
//planet.OrbitInclination = 0f;


//Log($"Orbit Count > 1. Free orbit range count = {freeOrbitRanges.Count}");
// Log($"Orbit Count > 1. Free orbit range count = {freeOrbitRanges.Count}");
var availableOrbits = new List<(float inner, float outer)>();
foreach (var range in freeOrbitRanges)
{
//GS2.Log($"Free orbits:{range}. Checking SystemRadius:{planet.SystemRadius}. {(1 + 1 * (GetSystemDensityBiasForStar(star) / 50)) * 2*planet.SystemRadius}");
if (range.outer - range.inner >
(1 + 1 * (GetSystemDensityBiasForStar(star) / 50)) * 2*planet.SystemRadius)
// GS2.Log($"Free orbits:{range}. Checking SystemRadius:{planet.SystemRadius}. {(0.05f+ 2 * planet.SystemRadius)}");
if (range.outer - range.inner > (0.05f+ 2 * planet.SystemRadius))
//(1 + 1 * (GetSystemDensityBiasForStar(star) / 50)) * 2*planet.SystemRadius)
{
//Log($"Adding {range}");
// Log($"Adding {range}");
availableOrbits.Add(range);
}
}

if (availableOrbits.Count == 0)
{
//Warn("Free Orbit Ranges:");
// Warn("Free Orbit Ranges:");
// LogJson(freeOrbitRanges);
//Warn($"No Orbit Ranges found for planet {planet.Name} radius:{planet.SystemRadius}");
// Warn($"No Orbit Ranges found for planet {planet.Name} {planet.genData["hosttype"]} {planet.genData["hostname"]} radius:{planet.SystemRadius}");
var success = false;
foreach (var existingOrbit in orbits)
if (existingOrbit.hasRoom && existingOrbit.SystemRadius > planet.SystemRadius)
Expand All @@ -84,14 +109,22 @@ private void AssignPlanetOrbits(GSStar star)
success = true;
break;
}
//GS2.Log($"{planet.Name} orbit radius {planet.OrbitRadius}");
// GS2.Log($"{planet.Name} orbit radius {planet.OrbitRadius}");
if (success) continue;

Warn($"After all that, just couldn't find an orbit for {planet.Name}. Throwing planet into the sun.");
star.Planets.Remove(planet);
Warn($"After all that, just couldn't find an orbit for {planet.Name} {planet.genData["hosttype"]} {planet.genData["hostname"]} . Throwing planet into the sun.");

brokenPlanets.Add(planet);


continue;
}
if (availableOrbits.Count == 0)

{
// GS2.Log($"No Available Orbits Found for Planet {planet.Name}");
continue;
}
var selectedRange = r.Item(availableOrbits);
// GS2.Log($"radius = r.NextFloat({selectedRange.inner + planet.SystemRadius}, {selectedRange.outer - planet.SystemRadius})");
var radius = r.NextFloat(selectedRange.inner + planet.SystemRadius,
Expand All @@ -100,20 +133,25 @@ private void AssignPlanetOrbits(GSStar star)
orbit = new Orbit(radius);
orbit.planets.Add(planet);
planet.OrbitRadius = radius;
//GS2.Log($"-{planet.Name} orbit radius {planet.OrbitRadius}");
// GS2.Log($"-{planet.Name} orbit radius {planet.OrbitRadius}");

planet.OrbitalPeriod = Utils.CalculateOrbitPeriod(planet.OrbitRadius);
//Log(
//$"selected orbit({radius}) for {planet.Name}({planet.SystemRadius}) SelectedRange:{selectedRange.inner}, {selectedRange.outer} New Ranges: {selectedRange.inner},{radius - planet.SystemRadius}({radius - planet.SystemRadius - selectedRange.inner}) | {radius + planet.SystemRadius}, {selectedRange.outer}({selectedRange.outer - radius - planet.SystemRadius})");
orbits.Add(orbit);
// Log(
// $"selected orbit({radius}) for {planet.Name}({planet.SystemRadius}) SelectedRange:{selectedRange.inner}, {selectedRange.outer} New Ranges: {selectedRange.inner},{radius - planet.SystemRadius}({radius - planet.SystemRadius - selectedRange.inner}) | {radius + planet.SystemRadius}, {selectedRange.outer}({selectedRange.outer - radius - planet.SystemRadius})");
orbits.Add(orbit);
var minGap = 0.1f;

if (radius - planet.SystemRadius*2 - selectedRange.inner > minGap)
freeOrbitRanges.Add((selectedRange.inner, radius - planet.SystemRadius*2));
if (selectedRange.outer - radius - planet.SystemRadius*2 > minGap)
freeOrbitRanges.Add((radius + planet.SystemRadius*2, selectedRange.outer));
}

foreach (var brokenPlanet in brokenPlanets)
{
GS2.Warn($"Removing Planet {brokenPlanet}");
star.Planets.Remove(brokenPlanet);
}
brokenPlanets = null;
starOrbits[star] = orbits;
star.Planets.Sort(PlanetSortByOrbit);
orbits.Sort(OrbitSort);
Expand Down Expand Up @@ -189,7 +227,16 @@ public bool hasRoom
{
get
{
if (planets.Count < 6) return true;
float largestRadius = 0f;
foreach (var planet in planets)
{
if (planet.SystemRadius > largestRadius) largestRadius = planet.SystemRadius;
}
largestRadius += 0.05f;
var circumference = radius * 2 * Mathf.PI;
// GS2.Log($"HasRoom Circumference = {circumference} largestRadius = {largestRadius} Planet Count = {planets.Count}");
if (largestRadius*2 * planets.Count < circumference) return true;
//if (planets.Count < 10) return true;
return false;
}
}
Expand Down
Loading

0 comments on commit 8e4c7a4

Please sign in to comment.