Skip to content

Commit

Permalink
Defer singularity linking (#548)
Browse files Browse the repository at this point in the history
<!-- Some improvement that requires no action on the part of add-on
creators i.e., improved star graphics -->
## Improvements
- Deferred singularity linking until after all planets are built, so
build order no longer affects pairs where only one has
`pairedSingularity` set.
  • Loading branch information
xen-42 committed Mar 23, 2023
2 parents 410f77a + c7632aa commit cbac7c2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 45 deletions.
88 changes: 43 additions & 45 deletions NewHorizons/Builder/Body/SingularityBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public static class SingularityBuilder
public static readonly int Color1 = Shader.PropertyToID("_Color");

private static Dictionary<string, GameObject> _singularitiesByID;
private static List<(string, string)> _pairsToLink;

private static Mesh _blackHoleMesh;
private static GameObject _blackHoleAmbience;
Expand All @@ -45,14 +46,8 @@ public static class SingularityBuilder
private static GameObject _whiteHoleRulesetVolume;
private static GameObject _whiteHoleVolume;

private static bool _isInit;

internal static void InitPrefabs()
{
if (_isInit) return;

_isInit = true;

if (_blackHoleProxyPrefab == null) _blackHoleProxyPrefab = SearchUtilities.Find(_blackHoleProxyPath).InstantiateInactive().Rename("BlackHoleSingularity").DontDestroyOnLoad();
if (_whiteHoleProxyPrefab == null) _whiteHoleProxyPrefab = SearchUtilities.Find(_whiteHoleProxyPath).InstantiateInactive().Rename("WhiteHoleSingularity").DontDestroyOnLoad();

Expand All @@ -72,13 +67,14 @@ internal static void InitPrefabs()
if (_whiteHoleVolume == null) _whiteHoleVolume = SearchUtilities.Find("WhiteHole_Body/WhiteHoleVolume").InstantiateInactive().Rename("WhiteHoleVolume").DontDestroyOnLoad();
}

public static void Make(GameObject go, Sector sector, OWRigidbody OWRB, PlanetConfig config, SingularityModule singularity)
public static void Init()
{
InitPrefabs();

// If we've reloaded the first one will now be null so we have to refresh the list
if (_singularitiesByID?.Values?.FirstOrDefault() == null) _singularitiesByID = new Dictionary<string, GameObject>();
_singularitiesByID = new Dictionary<string, GameObject>();
_pairsToLink = new List<(string, string)>();
}

public static void Make(GameObject go, Sector sector, OWRigidbody OWRB, PlanetConfig config, SingularityModule singularity)
{
var horizonRadius = singularity.horizonRadius;
var distortRadius = singularity.distortRadius != 0f ? singularity.distortRadius : horizonRadius * 2.5f;
var pairedSingularity = singularity.pairedSingularity;
Expand All @@ -95,51 +91,57 @@ public static void Make(GameObject go, Sector sector, OWRigidbody OWRB, PlanetCo
hasHazardVolume, singularity.targetStarSystem, singularity.curve, singularity.hasWarpEffects, singularity.renderQueueOverride, singularity.rename, singularity.parentPath, singularity.isRelativeToParent);

var uniqueID = string.IsNullOrEmpty(singularity.uniqueID) ? config.name : singularity.uniqueID;

_singularitiesByID.Add(uniqueID, newSingularity);

// Try to pair them
if (!string.IsNullOrEmpty(pairedSingularity) && newSingularity != null)
if (!string.IsNullOrEmpty(pairedSingularity))
{
if (_singularitiesByID.TryGetValue(pairedSingularity, out var pairedSingularityGO))
if (polarity)
{
switch (polarity)
{
case true:
PairSingularities(uniqueID, pairedSingularity, newSingularity, pairedSingularityGO);
break;
case false:
PairSingularities(pairedSingularity, uniqueID, pairedSingularityGO, newSingularity);
break;
}
_pairsToLink.Add((uniqueID, pairedSingularity));
}
else
{
_pairsToLink.Add((pairedSingularity, uniqueID));
}
}

}

public static void PairSingularities(string blackHoleID, string whiteHoleID, GameObject blackHole, GameObject whiteHole)
public static void PairAllSingularities()
{
InitPrefabs();

if (blackHole == null || whiteHole == null) return;

Logger.LogVerbose($"Pairing singularities [{blackHoleID}], [{whiteHoleID}]");

var whiteHoleVolume = whiteHole.GetComponentInChildren<WhiteHoleVolume>();
var blackHoleVolume = blackHole.GetComponentInChildren<BlackHoleVolume>();

if (whiteHoleVolume == null || blackHoleVolume == null)
foreach (var pair in _pairsToLink)
{
Logger.LogWarning($"[{blackHoleID}] and [{whiteHoleID}] do not have compatible polarities");
return;
var (blackHoleID, whiteHoleID) = pair;
if (!_singularitiesByID.TryGetValue(blackHoleID, out GameObject blackHole))
{
Logger.LogWarning($"Black hole [{blackHoleID}] is missing.");
break;
}
if (!_singularitiesByID.TryGetValue(whiteHoleID, out GameObject whiteHole))
{
Logger.LogWarning($"White hole [{whiteHoleID}] is missing.");
break;
}
var whiteHoleVolume = whiteHole.GetComponentInChildren<WhiteHoleVolume>();
var blackHoleVolume = blackHole.GetComponentInChildren<BlackHoleVolume>();
if (whiteHoleVolume == null || blackHoleVolume == null)
{
Logger.LogWarning($"Singularities [{blackHoleID}] and [{whiteHoleID}] do not have compatible polarities.");
break;
}
if (blackHoleVolume._whiteHole != null && blackHoleVolume._whiteHole != whiteHoleVolume)
{
Logger.LogWarning($"Black hole [{blackHoleID}] has already been linked!");
break;
}
Logger.LogVerbose($"Pairing singularities [{blackHoleID}], [{whiteHoleID}]");
blackHoleVolume._whiteHole = whiteHoleVolume;
}

blackHoleVolume._whiteHole = whiteHoleVolume;
}

public static GameObject MakeSingularity(GameObject planetGO, Sector sector, Vector3 position, Vector3 rotation, bool polarity, float horizon, float distort,
bool hasDestructionVolume, string targetStarSystem = null, TimeValuePair[] curve = null, bool warpEffects = true, int renderQueue = 2985, string rename = null, string parentPath = null, bool isRelativeToParent = false)
{
InitPrefabs();

// polarity true = black, false = white

var info = new SingularityModule
Expand Down Expand Up @@ -272,8 +274,6 @@ public static void PairSingularities(string blackHoleID, string whiteHoleID, Gam

public static MeshRenderer MakeSingularityGraphics(GameObject singularity, bool polarity, float horizon, float distort, int queue = 2985)
{
InitPrefabs();

var singularityRenderer = new GameObject(polarity ? "BlackHoleRenderer" : "WhiteHoleRenderer");
singularityRenderer.transform.parent = singularity.transform;
singularityRenderer.transform.localPosition = Vector3.zero;
Expand All @@ -296,8 +296,6 @@ public static MeshRenderer MakeSingularityGraphics(GameObject singularity, bool

public static GameObject MakeSingularityProxy(GameObject rootObject, MVector3 position, bool polarity, float horizon, float distort, TimeValuePair[] curve = null, int queue = 2985)
{
InitPrefabs();

var singularityRenderer = MakeSingularityGraphics(rootObject, polarity, horizon, distort, queue);
if (position != null) singularityRenderer.transform.localPosition = position;

Expand Down
2 changes: 2 additions & 0 deletions NewHorizons/Handlers/PlanetCreationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ public static void Init(List<NewHorizonsBody> bodies)

Logger.Log("Done loading bodies");

SingularityBuilder.PairAllSingularities();

// Events.FireOnNextUpdate(PlanetDestroyer.RemoveAllProxies);

if (Main.SystemDict[Main.Instance.CurrentStarSystem].Config.destroyStockPlanets) PlanetDestructionHandler.RemoveStockPlanets();
Expand Down
1 change: 1 addition & 0 deletions NewHorizons/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
AudioTypeHandler.Init();
InterferenceHandler.Init();
RemoteHandler.Init();
SingularityBuilder.Init();
AtmosphereBuilder.Init();
BrambleNodeBuilder.Init(BodyDict[CurrentStarSystem].Select(x => x.Config).Where(x => x.Bramble?.dimension != null).ToArray());

Expand Down

0 comments on commit cbac7c2

Please sign in to comment.