2020-03-22 19:05:26 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using MessagePack;
|
2020-12-01 00:16:11 +00:00
|
|
|
|
using UniRx;
|
2020-03-22 19:05:26 +00:00
|
|
|
|
using Unity.Mathematics;
|
|
|
|
|
|
using static Unity.Mathematics.math;
|
|
|
|
|
|
using Random = Unity.Mathematics.Random;
|
|
|
|
|
|
|
|
|
|
|
|
public class ZoneGenerator
|
|
|
|
|
|
{
|
2020-11-27 22:47:33 +00:00
|
|
|
|
public static ZonePack GenerateZone(
|
2020-12-01 00:16:11 +00:00
|
|
|
|
ZoneGenerationSettings settings,
|
|
|
|
|
|
Dictionary<string,GalaxyMapLayerData> mapLayers,
|
|
|
|
|
|
IEnumerable<SimpleCommodityData> resources,
|
2020-11-27 22:47:33 +00:00
|
|
|
|
string name,
|
2020-12-01 00:16:11 +00:00
|
|
|
|
float2 position)
|
|
|
|
|
|
{
|
|
|
|
|
|
var random = new Random(Convert.ToUInt32(abs(name.GetHashCode())));
|
|
|
|
|
|
var radius = settings.ZoneRadius.Evaluate(random.NextFloat() * mapLayers["Radius"].Evaluate(position, settings.ShapeSettings));
|
|
|
|
|
|
var mass = settings.ZoneMass.Evaluate(random.NextFloat() * mapLayers["Mass"].Evaluate(position, settings.ShapeSettings));
|
|
|
|
|
|
|
|
|
|
|
|
return GenerateZone(settings, mapLayers, resources, name, position, mass, radius, random);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static ZonePack GenerateZone(
|
|
|
|
|
|
ZoneGenerationSettings settings,
|
|
|
|
|
|
Dictionary<string,GalaxyMapLayerData> mapLayers,
|
|
|
|
|
|
IEnumerable<SimpleCommodityData> resources,
|
|
|
|
|
|
float mass,
|
|
|
|
|
|
float radius)
|
|
|
|
|
|
{
|
|
|
|
|
|
var name = Guid.NewGuid().ToString().Substring(0, 8);
|
|
|
|
|
|
var position = float2(0f);
|
|
|
|
|
|
var random = new Random(Convert.ToUInt32(abs(name.GetHashCode())));
|
|
|
|
|
|
|
|
|
|
|
|
return GenerateZone(settings, mapLayers, resources, name, position, mass, radius, random);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static ZonePack GenerateZone(
|
|
|
|
|
|
ZoneGenerationSettings settings,
|
|
|
|
|
|
Dictionary<string,GalaxyMapLayerData> mapLayers,
|
2020-04-09 07:48:45 +01:00
|
|
|
|
IEnumerable<SimpleCommodityData> resources,
|
2020-12-01 00:16:11 +00:00
|
|
|
|
string name,
|
|
|
|
|
|
float2 position,
|
|
|
|
|
|
float mass,
|
|
|
|
|
|
float radius,
|
|
|
|
|
|
Random random)
|
2020-03-30 03:11:57 +01:00
|
|
|
|
{
|
2020-11-27 22:47:33 +00:00
|
|
|
|
var zone = new ZoneData
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = name,
|
|
|
|
|
|
Position = position
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var pack = new ZonePack
|
|
|
|
|
|
{
|
|
|
|
|
|
Data = zone
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2020-12-01 00:16:11 +00:00
|
|
|
|
zone.Radius = radius;
|
|
|
|
|
|
zone.Mass = mass;
|
2020-03-30 03:11:57 +01:00
|
|
|
|
|
2020-03-30 17:54:54 +01:00
|
|
|
|
//Debug.Log($"Generating zone at position {zone.Position} with radius {zoneRadius} and mass {zoneMass}");
|
2020-03-30 03:11:57 +01:00
|
|
|
|
|
2020-12-01 00:16:11 +00:00
|
|
|
|
var planets = GenerateEntities(settings, ref random, zone);
|
2020-03-30 03:11:57 +01:00
|
|
|
|
|
|
|
|
|
|
// Create collections to map between zone generator output and database entries
|
2020-11-27 22:47:33 +00:00
|
|
|
|
var orbitMap = new Dictionary<GeneratorPlanet, OrbitData>();
|
|
|
|
|
|
var orbitInverseMap = new Dictionary<OrbitData, GeneratorPlanet>();
|
2020-03-30 03:11:57 +01:00
|
|
|
|
|
|
|
|
|
|
// Create orbit database entries
|
2020-11-27 22:47:33 +00:00
|
|
|
|
pack.Orbits = planets.Select(planet =>
|
2020-03-30 03:11:57 +01:00
|
|
|
|
{
|
2020-05-25 03:05:30 +01:00
|
|
|
|
var data = new OrbitData
|
2020-03-30 03:11:57 +01:00
|
|
|
|
{
|
2020-12-01 00:16:11 +00:00
|
|
|
|
Distance = new ReactiveProperty<float>(planet.Distance),
|
|
|
|
|
|
//Period = planet.Period,
|
2020-11-27 22:47:33 +00:00
|
|
|
|
Phase = planet.Phase
|
2020-03-30 03:11:57 +01:00
|
|
|
|
};
|
|
|
|
|
|
orbitMap[planet] = data;
|
|
|
|
|
|
orbitInverseMap[data] = planet;
|
|
|
|
|
|
return data;
|
2020-11-27 22:47:33 +00:00
|
|
|
|
}).ToList();
|
2020-03-30 03:11:57 +01:00
|
|
|
|
|
|
|
|
|
|
// Link OrbitData parents to database GUIDs
|
2020-11-27 22:47:33 +00:00
|
|
|
|
foreach (var data in pack.Orbits)
|
2020-03-30 03:11:57 +01:00
|
|
|
|
data.Parent = orbitInverseMap[data].Parent != null
|
|
|
|
|
|
? orbitMap[orbitInverseMap[data].Parent].ID
|
|
|
|
|
|
: Guid.Empty;
|
|
|
|
|
|
|
2020-04-09 07:48:45 +01:00
|
|
|
|
// Cache resource densities
|
2020-12-01 00:16:11 +00:00
|
|
|
|
var resourceMaps = mapLayers.Values
|
|
|
|
|
|
.ToDictionary(m => m.ID, m => m.Evaluate(zone.Position, settings.ShapeSettings));
|
2020-04-09 07:48:45 +01:00
|
|
|
|
|
2020-11-27 22:47:33 +00:00
|
|
|
|
pack.Planets = planets.Where(p=>!p.Empty).Select(planet =>
|
2020-03-30 03:11:57 +01:00
|
|
|
|
{
|
2020-11-27 22:47:33 +00:00
|
|
|
|
Dictionary<Guid, float> planetResources = new Dictionary<Guid, float>();
|
|
|
|
|
|
BodyType bodyType = planet.Belt ? BodyType.Asteroid :
|
2020-12-01 00:16:11 +00:00
|
|
|
|
planet.Mass > settings.SunMass ? BodyType.Sun :
|
|
|
|
|
|
planet.Mass > settings.GasGiantMass ? BodyType.GasGiant :
|
|
|
|
|
|
planet.Mass > settings.PlanetMass ? BodyType.Planet : BodyType.Planetoid;
|
2020-11-27 22:47:33 +00:00
|
|
|
|
|
2020-05-02 01:30:33 +01:00
|
|
|
|
foreach (var r in resources)
|
|
|
|
|
|
{
|
|
|
|
|
|
if ((bodyType & r.ResourceBodyType) != 0)
|
|
|
|
|
|
{
|
2020-12-01 00:16:11 +00:00
|
|
|
|
float quantity = ResourceValue(ref random, settings, r, r.ResourceDensity.Aggregate(1f, (m, rdm) => m * resourceMaps[rdm]));
|
2020-11-27 22:47:33 +00:00
|
|
|
|
if (r.Floor < quantity) planetResources.Add(r.ID, quantity);
|
2020-05-02 01:30:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-29 22:50:12 +00:00
|
|
|
|
BodyData planetData;
|
2020-11-27 22:47:33 +00:00
|
|
|
|
switch (bodyType)
|
|
|
|
|
|
{
|
|
|
|
|
|
case BodyType.Asteroid:
|
|
|
|
|
|
planetData = new AsteroidBeltData();
|
|
|
|
|
|
break;
|
|
|
|
|
|
case BodyType.Planetoid:
|
|
|
|
|
|
case BodyType.Planet:
|
|
|
|
|
|
planetData = new PlanetData();
|
|
|
|
|
|
break;
|
|
|
|
|
|
case BodyType.GasGiant:
|
|
|
|
|
|
planetData = new GasGiantData();
|
|
|
|
|
|
break;
|
|
|
|
|
|
case BodyType.Sun:
|
|
|
|
|
|
planetData = new SunData();
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
throw new ArgumentOutOfRangeException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-29 22:50:12 +00:00
|
|
|
|
planetData.Mass.Value = planet.Mass;
|
2020-11-27 22:47:33 +00:00
|
|
|
|
planetData.Orbit = orbitMap[planet].ID;
|
|
|
|
|
|
planetData.Resources = planetResources;
|
2020-11-29 22:50:12 +00:00
|
|
|
|
planetData.Name.Value = planetData.ID.ToString().Substring(0, 8);
|
2020-11-27 22:47:33 +00:00
|
|
|
|
if (planetData is AsteroidBeltData beltData)
|
2020-05-02 01:30:33 +01:00
|
|
|
|
{
|
2020-11-27 22:47:33 +00:00
|
|
|
|
beltData.Asteroids =
|
2020-12-01 00:16:11 +00:00
|
|
|
|
Enumerable.Range(0, (int) (settings.AsteroidCount.Evaluate(beltData.Mass.Value * orbitMap[planet].Distance.Value)))
|
2020-05-28 04:50:40 +01:00
|
|
|
|
.Select(_ => new Asteroid
|
|
|
|
|
|
{
|
2020-12-01 20:38:28 +00:00
|
|
|
|
Distance = random.NextFloat(orbitMap[planet].Distance.Value, orbitMap[planet].Distance.Value + settings.AsteroidBeltWidth.Evaluate(orbitMap[planet].Distance.Value)),
|
2020-12-01 00:16:11 +00:00
|
|
|
|
Phase = random.NextFloat(),
|
|
|
|
|
|
Size = random.NextFloat(),
|
|
|
|
|
|
RotationSpeed = settings.AsteroidRotationSpeed.Evaluate(random.NextFloat())
|
|
|
|
|
|
})
|
|
|
|
|
|
//.OrderByDescending(a=>a.Size)
|
|
|
|
|
|
.ToArray();
|
2020-05-02 01:30:33 +01:00
|
|
|
|
}
|
2020-11-27 22:47:33 +00:00
|
|
|
|
else if (planetData is GasGiantData gas)
|
|
|
|
|
|
{
|
2020-12-01 20:38:28 +00:00
|
|
|
|
if (gas is SunData sun)
|
|
|
|
|
|
{
|
|
|
|
|
|
float primary = random.NextFloat();
|
|
|
|
|
|
float secondary = frac(primary + 1 + settings.SunSecondaryColorDistance * (random.NextFloat() > .5 ? 1 : -1));
|
|
|
|
|
|
gas.Colors.Value = new []
|
|
|
|
|
|
{
|
|
|
|
|
|
float4(ColorMath.HsvToRgb(float3(primary, settings.SunColorSaturation, .5f)), 0),
|
|
|
|
|
|
float4(ColorMath.HsvToRgb(float3(secondary, settings.SunColorSaturation, 1)), 1)
|
|
|
|
|
|
};
|
|
|
|
|
|
sun.FogTintColor.Value = ColorMath.HsvToRgb(float3(primary, settings.SunFogTintSaturation, 1));
|
|
|
|
|
|
sun.LightColor.Value = ColorMath.HsvToRgb(float3(primary, settings.SunLightSaturation, 1));
|
|
|
|
|
|
gas.FirstOffsetDomainRotationSpeed.Value = 5;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// Define primary color and two adjacent colors
|
|
|
|
|
|
float primary = random.NextFloat();
|
|
|
|
|
|
float right = frac(primary + settings.GasGiantBandColorSeparation);
|
|
|
|
|
|
float left = frac(primary + 1 - settings.GasGiantBandColorSeparation);
|
|
|
|
|
|
|
|
|
|
|
|
// Create n time keys from 0 to 1
|
|
|
|
|
|
var bandCount = (int) (settings.GasGiantBandCount.Evaluate(random.NextFloat()) + .5f);
|
|
|
|
|
|
var times = Enumerable.Range(0, bandCount)
|
|
|
|
|
|
.Select(i => (float) i / (bandCount-1));
|
|
|
|
|
|
|
|
|
|
|
|
// Each band has a chance of being either the primary or one of the adjacent hues
|
|
|
|
|
|
// Saturation and Value are random with curves applied
|
|
|
|
|
|
gas.Colors.Value = times
|
|
|
|
|
|
.Select(time => float4(ColorMath.HsvToRgb(float3(
|
|
|
|
|
|
random.NextFloat() > settings.GasGiantBandAltColorChance ? primary : (random.NextFloat() > .5f ? right : left),
|
|
|
|
|
|
settings.GasGiantBandSaturation.Evaluate(random.NextFloat()),
|
|
|
|
|
|
settings.GasGiantBandSaturation.Evaluate(random.NextFloat()))),time))
|
|
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
|
|
|
|
gas.FirstOffsetDomainRotationSpeed.Value = 0;
|
|
|
|
|
|
}
|
2020-11-29 22:50:12 +00:00
|
|
|
|
gas.AlbedoRotationSpeed.Value = -3;
|
|
|
|
|
|
gas.FirstOffsetRotationSpeed.Value = 5;
|
|
|
|
|
|
gas.SecondOffsetRotationSpeed.Value = 10;
|
|
|
|
|
|
gas.SecondOffsetDomainRotationSpeed.Value = -25;
|
2020-11-27 22:47:33 +00:00
|
|
|
|
}
|
2020-04-09 07:48:45 +01:00
|
|
|
|
return planetData;
|
2020-11-27 22:47:33 +00:00
|
|
|
|
}).ToList();
|
2020-03-30 03:11:57 +01:00
|
|
|
|
|
2020-11-27 22:47:33 +00:00
|
|
|
|
return pack;
|
2020-03-30 03:11:57 +01:00
|
|
|
|
}
|
2020-05-14 23:20:19 +01:00
|
|
|
|
|
2020-12-01 00:16:11 +00:00
|
|
|
|
static float ResourceValue(ref Random random, ZoneGenerationSettings settings, SimpleCommodityData resource, float density)
|
2020-05-14 23:20:19 +01:00
|
|
|
|
{
|
2020-12-01 00:16:11 +00:00
|
|
|
|
return random.NextPowerDistribution(resource.Minimum, resource.Maximum, resource.Exponent,
|
|
|
|
|
|
1 / lerp(settings.ResourceDensityMinimum, settings.ResourceDensityMaximum, density));
|
2020-05-14 23:20:19 +01:00
|
|
|
|
}
|
2020-03-30 03:11:57 +01:00
|
|
|
|
|
2020-12-01 00:16:11 +00:00
|
|
|
|
public static GeneratorPlanet[] GenerateEntities(ZoneGenerationSettings settings, ref Random random, ZoneData zone)
|
2020-03-22 19:05:26 +00:00
|
|
|
|
{
|
2020-11-27 22:47:33 +00:00
|
|
|
|
var root = new GeneratorPlanet
|
2020-03-22 19:05:26 +00:00
|
|
|
|
{
|
2020-12-01 00:16:11 +00:00
|
|
|
|
Settings = settings,
|
|
|
|
|
|
Mass = zone.Mass,
|
|
|
|
|
|
ChildDistanceMaximum = zone.Radius * .75f,
|
|
|
|
|
|
ChildDistanceMinimum = settings.PlanetSafetyRadius.Evaluate(zone.Mass)
|
2020-03-22 19:05:26 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// There is some chance of generating a rosette or binary system
|
2020-03-28 18:36:12 +00:00
|
|
|
|
// Probabilities which are fixed for the entire galaxy are in GlobalData, contained in the GameContext
|
2020-12-01 00:16:11 +00:00
|
|
|
|
var rosette = random.NextFloat() < settings.RosetteProbability;
|
2020-03-22 19:05:26 +00:00
|
|
|
|
|
|
|
|
|
|
if (rosette)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Create a rosette with a number of vertices between 2 and 9 inclusive
|
2020-12-01 00:16:11 +00:00
|
|
|
|
root.ExpandRosette(ref random, (int)(random.NextFloat(1, 5) + random.NextFloat(1, 5)));
|
2020-03-22 19:05:26 +00:00
|
|
|
|
|
|
|
|
|
|
// Create a small number of less massive "captured" planets orbiting past the rosette
|
|
|
|
|
|
root.ExpandSolar(
|
2020-12-01 00:16:11 +00:00
|
|
|
|
ref random,
|
2020-04-09 07:48:45 +01:00
|
|
|
|
count: (int)(random.NextFloat(1, 3) * random.NextFloat(1, 2)),
|
2020-03-22 19:05:26 +00:00
|
|
|
|
massMulMin: .6f,
|
|
|
|
|
|
massMulMax: .8f,
|
|
|
|
|
|
distMulMin: 1.25f,
|
|
|
|
|
|
distMulMax: 1.75f,
|
|
|
|
|
|
jupiterJump: 1,
|
|
|
|
|
|
massFraction: .1f);
|
|
|
|
|
|
|
|
|
|
|
|
var averageChildMass = root.Children.Sum(p => p.Mass) / root.Children.Count;
|
2020-12-01 00:16:11 +00:00
|
|
|
|
foreach(var p in root.Children.Where(c=>c.Mass>settings.GasGiantMass))
|
2020-03-22 19:05:26 +00:00
|
|
|
|
{
|
|
|
|
|
|
var m = p.Mass / averageChildMass;
|
|
|
|
|
|
// Give each child in the rosette its own mini solar system
|
|
|
|
|
|
p.ExpandSolar(
|
2020-12-01 00:16:11 +00:00
|
|
|
|
ref random,
|
2020-05-03 00:36:47 +01:00
|
|
|
|
count: (int) (random.NextFloat(1, 3 * m) + random.NextFloat(1, 3 * m)),
|
2020-03-22 19:05:26 +00:00
|
|
|
|
massMulMin: 0.75f,
|
|
|
|
|
|
massMulMax: 2.5f,
|
2020-03-30 03:11:57 +01:00
|
|
|
|
distMulMin: 1 + m * .25f,
|
|
|
|
|
|
distMulMax: 1.05f + m * .5f,
|
2020-03-22 19:05:26 +00:00
|
|
|
|
jupiterJump: random.NextFloat() * random.NextFloat() * 10 + 1,
|
|
|
|
|
|
massFraction: .5f
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// Create a regular old boring solar system
|
|
|
|
|
|
root.ExpandSolar(
|
2020-12-01 00:16:11 +00:00
|
|
|
|
ref random,
|
2020-03-22 19:05:26 +00:00
|
|
|
|
count: random.NextInt(5, 15),
|
|
|
|
|
|
massMulMin: 0.5f,
|
|
|
|
|
|
massMulMax: 2.0f,
|
|
|
|
|
|
distMulMin: 1.1f,
|
2020-03-30 03:11:57 +01:00
|
|
|
|
distMulMax: 1.25f,
|
2020-03-22 19:05:26 +00:00
|
|
|
|
jupiterJump: random.NextFloat() * random.NextFloat() * 10 + 1,
|
|
|
|
|
|
massFraction: .25f
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-01 00:16:11 +00:00
|
|
|
|
var alreadyExpanded = new List<GeneratorPlanet>();
|
2020-11-27 22:47:33 +00:00
|
|
|
|
var binaries = new List<GeneratorPlanet>();
|
2020-12-01 00:16:11 +00:00
|
|
|
|
for(int i=0; i<settings.SatellitePasses; i++)
|
2020-03-22 19:05:26 +00:00
|
|
|
|
{
|
2020-12-01 00:16:11 +00:00
|
|
|
|
// Get all children that are above the satellite creation mass floor and not rosette members
|
|
|
|
|
|
var satelliteCandidates = rosette
|
|
|
|
|
|
? root.AllPlanets().Where(p =>
|
|
|
|
|
|
p != root &&
|
|
|
|
|
|
p.Parent != root &&
|
|
|
|
|
|
p.Mass > settings.SatelliteCreationMassFloor &&
|
|
|
|
|
|
!alreadyExpanded.Contains(p))
|
|
|
|
|
|
: root.AllPlanets().Where(p =>
|
|
|
|
|
|
p != root &&
|
|
|
|
|
|
p.Mass > settings.SatelliteCreationMassFloor &&
|
|
|
|
|
|
!alreadyExpanded.Contains(p));
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var planet in satelliteCandidates)
|
2020-03-22 19:05:26 +00:00
|
|
|
|
{
|
2020-12-01 00:16:11 +00:00
|
|
|
|
// There's a chance of generating satellites for each qualified planet
|
|
|
|
|
|
if (random.NextFloat() < settings.SatelliteCreationProbability)
|
2020-03-30 17:54:54 +01:00
|
|
|
|
{
|
2020-12-01 00:16:11 +00:00
|
|
|
|
// Sometimes the satellite is so massive that it forms a binary system (like Earth!)
|
|
|
|
|
|
if (random.NextFloat() < settings.BinaryCreationProbability)
|
|
|
|
|
|
{
|
|
|
|
|
|
planet.ExpandRosette(ref random, 2);
|
|
|
|
|
|
binaries.AddRange(planet.Children);
|
|
|
|
|
|
}
|
|
|
|
|
|
// Otherwise, terrestrial planets get a couple satellites while gas giants get many
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
planet.ExpandSolar(
|
|
|
|
|
|
ref random,
|
|
|
|
|
|
count: planet.Mass < settings.GasGiantMass ? random.NextInt(1, 3) : random.NextInt(2, 6),
|
|
|
|
|
|
massMulMin: .75f,
|
|
|
|
|
|
massMulMax: 1.5f,
|
|
|
|
|
|
distMulMin: 1.05f,
|
|
|
|
|
|
distMulMax: 1.25f,
|
|
|
|
|
|
jupiterJump: 1,
|
|
|
|
|
|
massFraction: .15f); // Planetary satellites are not nearly as massive as planets themselves
|
|
|
|
|
|
}
|
|
|
|
|
|
alreadyExpanded.Add(planet);
|
2020-03-30 17:54:54 +01:00
|
|
|
|
}
|
2020-03-22 19:05:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-30 17:54:54 +01:00
|
|
|
|
// Get all children that are below the belt creation mass floor and not rosette members, also exclude binaries
|
|
|
|
|
|
var beltCandidates = rosette
|
2020-12-01 00:16:11 +00:00
|
|
|
|
? root.AllPlanets().Where(p => p != root && p.Parent != root && p.Mass < settings.BeltMassCeiling && !binaries.Contains(p))
|
|
|
|
|
|
: root.AllPlanets().Where(p => p != root && p.Mass < settings.BeltMassCeiling && !binaries.Contains(p));
|
|
|
|
|
|
|
2020-03-31 13:44:18 +01:00
|
|
|
|
foreach(var planet in beltCandidates.Reverse())
|
2020-12-01 00:16:11 +00:00
|
|
|
|
if (random.NextFloat() < settings.BeltProbability && !planet.Parent.Children.Any(p=>p.Belt))
|
2020-03-30 17:54:54 +01:00
|
|
|
|
planet.Belt = true;
|
|
|
|
|
|
|
2020-12-01 00:16:11 +00:00
|
|
|
|
var totalMass = root.AllPlanets().Sum(p => p.Mass);
|
|
|
|
|
|
var rootMass = root.Mass;
|
|
|
|
|
|
foreach (var planet in root.AllPlanets())
|
|
|
|
|
|
planet.Mass = (planet.Mass / totalMass) * rootMass;
|
|
|
|
|
|
|
2020-03-22 19:05:26 +00:00
|
|
|
|
return root.AllPlanets().ToArray();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-28 18:36:12 +00:00
|
|
|
|
// All orbiting bodies are referred to here as "Planets"
|
|
|
|
|
|
// This goes back to the original etymology for the word planet, which is traveler,
|
|
|
|
|
|
// Because they were objects we observed to move in the sky instead of remaining fixed like the stars
|
2020-11-27 22:47:33 +00:00
|
|
|
|
public class GeneratorPlanet
|
2020-03-22 19:05:26 +00:00
|
|
|
|
{
|
2020-12-01 00:16:11 +00:00
|
|
|
|
public ZoneGenerationSettings Settings;
|
2020-03-22 19:05:26 +00:00
|
|
|
|
public float Distance;
|
|
|
|
|
|
public float Phase;
|
|
|
|
|
|
public float Mass;
|
2020-12-01 00:16:11 +00:00
|
|
|
|
//public float Period;
|
2020-03-22 19:05:26 +00:00
|
|
|
|
public float ChildDistanceMinimum;
|
|
|
|
|
|
public float ChildDistanceMaximum;
|
|
|
|
|
|
public bool Empty = false;
|
2020-03-30 17:54:54 +01:00
|
|
|
|
public bool Belt = false;
|
2020-11-27 22:47:33 +00:00
|
|
|
|
public List<GeneratorPlanet> Children = new List<GeneratorPlanet>(); // Planets orbiting this one are referred to as children
|
|
|
|
|
|
public GeneratorPlanet Parent;
|
2020-03-22 19:05:26 +00:00
|
|
|
|
|
2020-03-28 18:36:12 +00:00
|
|
|
|
// Recursively gather all planets in the hierarchy
|
2020-11-27 22:47:33 +00:00
|
|
|
|
public IEnumerable<GeneratorPlanet> AllPlanets()
|
2020-03-22 19:05:26 +00:00
|
|
|
|
{
|
|
|
|
|
|
return new[]{this}.Concat(Children.SelectMany(c=>c.AllPlanets()));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Create children that fill a single orbit, equally spaced
|
|
|
|
|
|
// https://en.wikipedia.org/wiki/Klemperer_rosette
|
2020-12-01 00:16:11 +00:00
|
|
|
|
public void ExpandRosette(ref Random random, int vertices)
|
2020-03-22 19:05:26 +00:00
|
|
|
|
{
|
2020-03-30 17:54:54 +01:00
|
|
|
|
//Debug.Log("Expanding Rosette");
|
2020-03-30 03:11:57 +01:00
|
|
|
|
|
2020-03-22 19:05:26 +00:00
|
|
|
|
// Rosette children replace the parent, parent orbital node is left empty
|
|
|
|
|
|
Empty = true;
|
|
|
|
|
|
|
|
|
|
|
|
// Masses in a rosette alternate, so every sequential pair has the same shared mass
|
|
|
|
|
|
var sharedMass = Mass / vertices * 2;
|
|
|
|
|
|
|
|
|
|
|
|
// Proportion of the masses of sequential pairs is random, but only for even vertex counts
|
2020-12-01 00:16:11 +00:00
|
|
|
|
var proportion = vertices % 2 == 0 ? random.NextFloat(.5f,.95f) : .5f;
|
2020-03-22 19:05:26 +00:00
|
|
|
|
|
|
|
|
|
|
// Place children at a fixed distance in the center of the range
|
|
|
|
|
|
var dist = (ChildDistanceMinimum + ChildDistanceMaximum) / 2;
|
|
|
|
|
|
|
|
|
|
|
|
// Position of first child
|
|
|
|
|
|
var p0 = new float2(0, dist);
|
|
|
|
|
|
|
|
|
|
|
|
// Position of second child
|
2020-03-29 04:19:24 +01:00
|
|
|
|
var p1 = OrbitData.Evaluate(1.0f / vertices) * dist;
|
2020-03-22 19:05:26 +00:00
|
|
|
|
|
|
|
|
|
|
// Maximum child distance is half the distance to the neighbor minus the neighbor's radius
|
2020-12-01 00:16:11 +00:00
|
|
|
|
var p0ChildDist = (distance(p0, p1) * proportion - Settings.PlanetSafetyRadius.Evaluate(sharedMass * (1 - proportion))) * .75f;
|
|
|
|
|
|
var p1ChildDist = (distance(p0, p1) * (1 - proportion) - Settings.PlanetSafetyRadius.Evaluate(sharedMass * proportion)) * .75f;
|
2020-03-22 19:05:26 +00:00
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < vertices; i++)
|
|
|
|
|
|
{
|
2020-11-27 22:47:33 +00:00
|
|
|
|
var child = new GeneratorPlanet
|
2020-03-22 19:05:26 +00:00
|
|
|
|
{
|
2020-12-01 00:16:11 +00:00
|
|
|
|
Settings = Settings,
|
2020-03-22 19:05:26 +00:00
|
|
|
|
Parent = this,
|
|
|
|
|
|
Mass = sharedMass * (i % 2 == 0 ? proportion : 1 - proportion), // Masses alternate
|
|
|
|
|
|
Distance = dist,
|
|
|
|
|
|
Phase = (float) i / vertices,
|
|
|
|
|
|
ChildDistanceMaximum = (i % 2 == 0 ? p0ChildDist : p1ChildDist)
|
|
|
|
|
|
};
|
2020-12-01 00:16:11 +00:00
|
|
|
|
child.ChildDistanceMinimum = Settings.PlanetSafetyRadius.Evaluate(child.Mass) * 2;
|
|
|
|
|
|
//child.Period = Settings..Evaluate(child.Distance);
|
2020-03-22 19:05:26 +00:00
|
|
|
|
Children.Add(child);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ChildDistanceMinimum = dist + p0ChildDist;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Create children that mimic the distribution of planetary masses in the solar system
|
2020-12-01 00:16:11 +00:00
|
|
|
|
public void ExpandSolar(ref Random random, int count, float massMulMin, float massMulMax, float distMulMin, float distMulMax, float jupiterJump, float massFraction)
|
2020-03-22 19:05:26 +00:00
|
|
|
|
{
|
2020-03-30 17:54:54 +01:00
|
|
|
|
//Debug.Log("Expanding Solar");
|
2020-03-22 19:05:26 +00:00
|
|
|
|
// Expansion is impossible when child space is full
|
|
|
|
|
|
if (count == 0 || ChildDistanceMaximum < ChildDistanceMinimum)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
var masses = new float[count];
|
|
|
|
|
|
var distances = new float[count];
|
|
|
|
|
|
|
|
|
|
|
|
// Accumulate total mass
|
|
|
|
|
|
// Initialize first mass and distance, actual number doesn't matter since total mass will be divided proportionally
|
|
|
|
|
|
float massTotal = distances[0] = masses[0] = 1;
|
|
|
|
|
|
|
|
|
|
|
|
// Masses and distances multiply from one planet to the next
|
|
|
|
|
|
// Mass typically increases exponentially as you go further out
|
|
|
|
|
|
// jupiterJump is an additional mass multiplier applied after half of the planets
|
|
|
|
|
|
for (var i = 1; i < count; i++)
|
|
|
|
|
|
{
|
2020-12-01 00:16:11 +00:00
|
|
|
|
massTotal += masses[i] = masses[i - 1] * random.NextFloat(massMulMin, massMulMax) * (count/2==i ? jupiterJump : 1);
|
|
|
|
|
|
distances[i] = distances[i - 1] * random.NextFloat(distMulMin, distMulMax);
|
2020-03-22 19:05:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Add some randomness to child masses
|
|
|
|
|
|
for (var i = 0; i < masses.Length; i++)
|
2020-12-01 00:16:11 +00:00
|
|
|
|
masses[i] *= random.NextFloat(.1f, 1f);
|
2020-03-22 19:05:26 +00:00
|
|
|
|
|
|
|
|
|
|
// Normalize the masses of the children
|
|
|
|
|
|
for (var i = 0; i < count; i++)
|
|
|
|
|
|
masses[i] = masses[i] / massTotal * Mass * massFraction;
|
|
|
|
|
|
|
|
|
|
|
|
// Map child distances to range between minimum and maximum
|
2020-03-29 04:19:24 +01:00
|
|
|
|
if (count > 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
var oldDistances = (float[]) distances.Clone();
|
|
|
|
|
|
for (var i = 0; i < count; i++)
|
|
|
|
|
|
distances[i] = lerp(ChildDistanceMinimum, ChildDistanceMaximum,
|
2020-12-01 00:16:11 +00:00
|
|
|
|
(oldDistances[i] - oldDistances[0]) / (oldDistances[count - 1] - oldDistances[0])) + Settings.PlanetSafetyRadius.Evaluate(masses[i]);
|
2020-03-29 04:19:24 +01:00
|
|
|
|
}
|
2020-03-22 19:05:26 +00:00
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < count; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Only instantiate children above the mass floor
|
2020-12-01 00:16:11 +00:00
|
|
|
|
if (masses[i] > Settings.MassFloor)
|
2020-03-22 19:05:26 +00:00
|
|
|
|
{
|
2020-11-27 22:47:33 +00:00
|
|
|
|
var child = new GeneratorPlanet
|
2020-03-22 19:05:26 +00:00
|
|
|
|
{
|
2020-12-01 00:16:11 +00:00
|
|
|
|
Settings = Settings,
|
2020-03-22 19:05:26 +00:00
|
|
|
|
Parent = this,
|
|
|
|
|
|
Mass = masses[i],
|
|
|
|
|
|
Distance = distances[i],
|
2020-12-01 00:16:11 +00:00
|
|
|
|
Phase = random.NextFloat()
|
2020-03-22 19:05:26 +00:00
|
|
|
|
};
|
2020-12-01 00:16:11 +00:00
|
|
|
|
//child.Period = Context.GlobalData.OrbitalPeriod(child.Distance);
|
|
|
|
|
|
child.ChildDistanceMinimum = Settings.PlanetSafetyRadius.Evaluate(child.Mass) * 2;
|
2020-03-22 19:05:26 +00:00
|
|
|
|
// Maximum child distance of child is the smallest distance to either of its neighbors
|
|
|
|
|
|
child.ChildDistanceMaximum = min(i == 0 ? child.Distance - ChildDistanceMinimum : child.Distance - distances[i - 1],
|
|
|
|
|
|
i < count - 1 ? distances[i + 1] - child.Distance : float.PositiveInfinity);
|
|
|
|
|
|
if (float.IsNaN(child.Distance))
|
|
|
|
|
|
throw new NotFiniteNumberException($"Planet created with NaN distance, something went very wrong!");
|
|
|
|
|
|
if (child.ChildDistanceMaximum > child.ChildDistanceMinimum)
|
|
|
|
|
|
Children.Add(child);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|