Skip to content

Commit

Permalink
Added caching to the star system data structure for faster access to …
Browse files Browse the repository at this point in the history
…the objects whithin the star system.

This will allow the plugin to skip a traversal of the whole star system tree and directly access objects by type, id, or just all at once in a set
  • Loading branch information
thorwin99 committed Feb 12, 2023
1 parent 95b9d90 commit a978438
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public override MySystemAsteroids GenerateInstance(int systemIndex, in MySystemO
asteroidObject.CenterPosition = Vector3D.Zero;
asteroidObject.AsteroidSize = new MySerializableMinMax(256, 1024);
asteroidObject.AsteroidTypeName = GetTypeName();
asteroidObject.ParentId = parent != null ? parent.Id : Guid.Empty;

MyAsteroidRingData belt = new MyAsteroidRingData();
belt.AngleDegrees = Vector3D.Zero;
Expand Down
18 changes: 9 additions & 9 deletions SEWorldGenPlugin/Generator/MyStarSystemGenerator.Networking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,17 @@ private static void SendAddSystemObjectServer(MySystemObject obj, Guid parentId,
var parent = Static.StarSystem.GetById(parentId);
if (parent != null)
{
parent.ChildObjects.Add(obj);
obj.ParentId = parentId;
Static.StarSystem.Add(obj);
PluginEventHandler.Static.RaiseStaticEvent(BroadcastObjectAdded, obj, callbackId, senderId);
callback?.Invoke(true);
}
else
{
if(Static.StarSystem.CenterObject != null)
{
Static.StarSystem.CenterObject.ChildObjects.Add(obj);
obj.ParentId = Static.StarSystem.CenterObject.Id;
Static.StarSystem.Add(obj);
PluginEventHandler.Static.RaiseStaticEvent(BroadcastObjectAdded, obj, callbackId, senderId);
callback?.Invoke(true);
}
Expand All @@ -121,18 +121,18 @@ private static void SendAddSystemObjectServer(MySystemObject obj, Guid parentId,
sun.Type = MySystemObjectType.EMPTY;
sun.ParentId = Guid.Empty;

Static.StarSystem.CenterObject = sun;
Static.StarSystem.Add(sun);

obj.ParentId = sun.Id;
Static.StarSystem.CenterObject.ChildObjects.Add(obj);
Static.StarSystem.Add(obj);

PluginEventHandler.Static.RaiseStaticEvent(BroadcastObjectAdded, obj, callbackId, senderId);
callback?.Invoke(true);
}
else
{
Static.StarSystem.CenterObject = obj;
obj.ParentId = Guid.Empty;
Static.StarSystem.Add(obj);
PluginEventHandler.Static.RaiseStaticEvent(BroadcastObjectAdded, obj, callbackId, senderId);
callback?.Invoke(true);
}
Expand Down Expand Up @@ -168,7 +168,7 @@ private static void BroadcastObjectAdded(MySystemObject obj, uint callbackId, ul

if(!Static.StarSystem.Contains(obj.Id) && parent != null)
{
parent.ChildObjects.Add(obj);
Static.StarSystem.Add(obj);
callback?.Invoke(true);
}
else if(obj.ParentId == Guid.Empty && Static.StarSystem.CenterObject == null)
Expand All @@ -180,16 +180,16 @@ private static void BroadcastObjectAdded(MySystemObject obj, uint callbackId, ul
sun.Type = MySystemObjectType.EMPTY;
sun.ParentId = Guid.Empty;

Static.StarSystem.CenterObject = sun;
Static.StarSystem.Add(sun);

obj.ParentId = sun.Id;
Static.StarSystem.CenterObject.ChildObjects.Add(obj);
Static.StarSystem.Add(obj);

callback?.Invoke(true);
}
else
{
Static.StarSystem.CenterObject = obj;
Static.StarSystem.Add(obj);
callback?.Invoke(true);
}
}
Expand Down
75 changes: 39 additions & 36 deletions SEWorldGenPlugin/Generator/MyStarSystemGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using SEWorldGenPlugin.Utilities;
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using VRage.Game;
using VRage.Game.Components;
using VRage.Game.Entity;
Expand Down Expand Up @@ -128,7 +129,7 @@ public override void Init(MyObjectBuilder_SessionComponent sessionComponent)

if (StarSystem.Count() <= 0 && MySettingsSession.Static.Settings.GeneratorSettings.SystemGenerator != SystemGenerationMethod.NONE)
{
StarSystem = GenerateNewStarSystem();
GenerateNewStarSystem();
}

MyPluginLog.Log("Initializing Star system generator completed");
Expand All @@ -155,19 +156,16 @@ public override void LoadData()

if(StarSystem != null && StarSystem.CenterObject != null)
{
foreach (var obj in StarSystem.GetAll())
foreach (var obj in StarSystem.GetAllByType(MySystemObjectType.ASTEROIDS))
{
if (obj.Type == MySystemObjectType.ASTEROIDS)
{
var asteroid = obj as MySystemAsteroids;
var asteroid = obj as MySystemAsteroids;

if (!MyAsteroidObjectsManager.Static.AsteroidObjectProviders.TryGetValue(asteroid.AsteroidTypeName, out var provider)) continue;
if (!MyAsteroidObjectsManager.Static.AsteroidObjectProviders.TryGetValue(asteroid.AsteroidTypeName, out var provider)) continue;

if (!provider.TryLoadObject(asteroid))
{
MyPluginLog.Log("No data found associated with asteroid object " + asteroid.DisplayName + " (" + asteroid.Id + "), Removing it.", LogLevel.WARNING);
StarSystem.Remove(asteroid.Id);
}
if (!provider.TryLoadObject(asteroid))
{
MyPluginLog.Log("No data found associated with asteroid object " + asteroid.DisplayName + " (" + asteroid.Id + "), Removing it.", LogLevel.WARNING);
StarSystem.Remove(asteroid.Id);
}
}
}
Expand Down Expand Up @@ -266,12 +264,12 @@ private void AddAllPersistentGps()
/// world settings.
/// </summary>
/// <returns></returns>
private MyObjectBuilder_SystemData GenerateNewStarSystem()
private void GenerateNewStarSystem()
{
MyPluginLog.Log("Generating a new Solar system ...");

int seed = MySession.Static.Settings.ProceduralSeed + Guid.NewGuid().GetHashCode();
MyObjectBuilder_SystemData system = new MyObjectBuilder_SystemData();
StarSystem = new MyObjectBuilder_SystemData();

var settings = MySettingsSession.Static.Settings.GeneratorSettings;

Expand Down Expand Up @@ -316,32 +314,33 @@ private MyObjectBuilder_SystemData GenerateNewStarSystem()
sun.SubtypeId = sunDef.Id.SubtypeId.String;
sun.DisplayName = sunDef.Id.SubtypeId.String;
sun.Diameter = CalculatePlanetDiameter(sunDef) * 2;
sun.ChildObjects = new HashSet<MySystemObject>();
sun.Generated = false;
sun.Type = MySystemObjectType.PLANET;

system.CenterObject = sun;
StarSystem.Add(sun);
currentOrbitDistance += (long)sun.Diameter * 2 + (long)sunDef.AtmosphereHeight;
}
else
{
system.CenterObject = new MySystemObject();
system.CenterObject.Type = MySystemObjectType.EMPTY;
system.CenterObject.DisplayName = "System center";
MySystemObject center;

center = new MySystemObject();
center.Type = MySystemObjectType.EMPTY;
center.DisplayName = "System center";

StarSystem.Add(center);
}

while(planetCount > 0 || asteroidObjectCount > 0)
{
currentOrbitDistance += MyRandom.Instance.Next(orbitDistances.Min, orbitDistances.Max);

//Maybe rework to override orbit distance, so all objects fit
if (worldSize >= 0 && currentOrbitDistance >= worldSize) return system;

MySystemObject obj = null;
if (worldSize >= 0 && currentOrbitDistance >= worldSize) return;

if (asteroidObjectCount <= 0 || (MyRandom.Instance.NextDouble() <= planetProb && planetCount > 0)) // Generate planet
{
obj = GeneratePlanet(currentPlanetIndex++, Math.Sin((system.Count() - 1) * Math.PI / systemSize), currentOrbitDistance);
GeneratePlanet(currentPlanetIndex++, Math.Sin((StarSystem.Count() - 1) * Math.PI / systemSize), currentOrbitDistance, StarSystem.CenterObject.Id);
planetCount--;
}
else if (asteroidObjectCount > 0) // Generate asteroid object
Expand All @@ -365,34 +364,31 @@ private MyObjectBuilder_SystemData GenerateNewStarSystem()
currentAsteroidIndices.Add(provider.GetTypeName(), 0);
}

obj = provider.GenerateInstance(currentAsteroidIndices[provider.GetTypeName()]++, null, currentOrbitDistance);
MySystemAsteroids roid = provider.GenerateInstance(currentAsteroidIndices[provider.GetTypeName()]++, StarSystem.CenterObject, currentOrbitDistance);
roid.ParentId = StarSystem.CenterObject.Id;

if (roid == null) continue;

if (obj == null) continue;
roid.AsteroidTypeName = provider.GetTypeName();

(obj as MySystemAsteroids).AsteroidTypeName = provider.GetTypeName();
StarSystem.Add(roid);

asteroidObjectCount--;
}
if (obj == null) continue;

obj.ParentId = system.CenterObject.Id;
system.CenterObject.ChildObjects.Add(obj);
}
}

MyPluginLog.Log("Solar system generated ...");

return system;
}

/// <summary>
/// Generates a planet for the star system.
/// Generates a planet for the star system and adds it to the system.
/// </summary>
/// <param name="planetIndex">Index of the planet in the system</param>
/// <param name="maxDiameter">The largest diameter the planet should have</param>
/// <param name="orbitDistance">The distance the planet is away from Vector3D.Zero</param>
/// <returns>A new MySystemPlanet</returns>
private MySystemPlanet GeneratePlanet(int planetIndex, double maxDiameter, long orbitDistance)
private MySystemPlanet GeneratePlanet(int planetIndex, double maxDiameter, long orbitDistance, Guid parent)
{
MyPluginLog.Log("Generating new planet");

Expand Down Expand Up @@ -421,19 +417,23 @@ private MySystemPlanet GeneratePlanet(int planetIndex, double maxDiameter, long
DisplayName = name,
Generated = false,
SubtypeId = def.Id.SubtypeId.String,
ParentId = parent
};

StarSystem.Add(planet);

if(MyRandom.Instance.NextFloat() < settings.BaseRingProbability * def.SurfaceGravity)
{
planet.ChildObjects.Add(GenrateRing(planet));
MySystemAsteroids ring = GenrateRing(planet);
StarSystem.Add(ring);
}

if (MyRandom.Instance.NextFloat() < settings.BaseMoonProbability * def.SurfaceGravity)
{
foreach(var moon in GeneratePlanetMoons(planet))
{
if (moon == null) continue;
planet.ChildObjects.Add(moon);
StarSystem.Add(moon);
}
}
MyPluginLog.Log("Planet generated");
Expand Down Expand Up @@ -511,6 +511,7 @@ private MySystemAsteroids GenrateRing(MySystemPlanet parentPlanet)

var provider = MyAsteroidObjectsManager.Static.AsteroidObjectProviders[MyAsteroidRingProvider.TYPE_NAME];
var ring = provider.GenerateInstance(0, parentPlanet, 0);
ring.ParentId = parentPlanet.Id;

return ring;
}
Expand Down Expand Up @@ -806,6 +807,8 @@ private MyObjectBuilder_SystemData LoadSystemData()
data = new MyObjectBuilder_SystemData();
}

data.RebuildCache();

return data;
}
return new MyObjectBuilder_SystemData();
Expand All @@ -825,7 +828,7 @@ public override void UpdateBeforeSimulation()
var e = MyEntities.GetEntityById(p.EntityId) as MyPlanet;
bool exists = false;

foreach(var obj in StarSystem.GetAll())
foreach(var obj in StarSystem.GetAllByType(MySystemObjectType.PLANET))
{
if(obj is MySystemPlanet)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,11 @@ public override void UpdateGpsForPlayer(MyEntityTracker entity)
var settings = MySettingsSession.Static.Settings.GeneratorSettings.GPSSettings;
if (settings.AsteroidGPSMode != MyGPSGenerationMode.DISCOVERY) return;

var objects = MyStarSystemGenerator.Static.StarSystem.GetAll();
var objects = MyStarSystemGenerator.Static.StarSystem.GetAllByType(MySystemObjectType.ASTEROIDS);
MyCharacter player = entity.Entity as MyCharacter;

foreach(var obj in objects)
{
if (obj.Type != MySystemObjectType.ASTEROIDS) continue;

MySystemAsteroids asteroid = obj as MySystemAsteroids;
Vector3D entityPosition = player.PositionComp.GetPosition();

Expand Down Expand Up @@ -347,11 +345,10 @@ private Vector3I GetAsteroidVoxelSize(double asteroidRadius)
/// <returns>The first ring found, that contains the given position.</returns>
private MySystemAsteroids GetAsteroidObjectAt(Vector3D position)
{
var systemObjects = MyStarSystemGenerator.Static.StarSystem.GetAll();
var systemObjects = MyStarSystemGenerator.Static.StarSystem.GetAllByType(MySystemObjectType.ASTEROIDS);

foreach(var obj in systemObjects)
{
if (obj.Type != MySystemObjectType.ASTEROIDS) continue;
var asteroids = obj as MySystemAsteroids;

if (!MyAsteroidObjectsManager.Static.AsteroidObjectProviders.TryGetValue(asteroids.AsteroidTypeName, out var prov)) continue;
Expand Down
Loading

0 comments on commit a978438

Please sign in to comment.