Allow changing stock planet orbit (except for primary body)

This commit is contained in:
Nick J. Connors 2022-02-09 13:38:06 -05:00
parent 90cff5bfc2
commit 3699c3a9b7
6 changed files with 185 additions and 48 deletions

View File

@ -21,10 +21,15 @@ namespace NewHorizons.Builder.Orbital
return Update(initialMotion, body, primaryBody, OWRB, orbit);
}
public static float SiderealPeriodToAngularSpeed(float siderealPeriod)
{
return siderealPeriod == 0 ? 0f : 2f * Mathf.PI / (siderealPeriod * 60f);
}
public static InitialMotion Update(InitialMotion initialMotion, GameObject body, AstroObject primaryBody, OWRigidbody OWRB, OrbitModule orbit)
{
// Rotation
initialMotion.SetValue("_initAngularSpeed", orbit.SiderealPeriod == 0 ? 0f : 2f * Mathf.PI / (orbit.SiderealPeriod * 60f));
initialMotion.SetValue("_initAngularSpeed", SiderealPeriodToAngularSpeed(orbit.SiderealPeriod));
var rotationAxis = Quaternion.AngleAxis(orbit.AxialTilt + 90f, Vector3.right) * Vector3.up;
body.transform.rotation = Quaternion.FromToRotation(Vector3.up, rotationAxis);

View File

@ -9,7 +9,7 @@ namespace NewHorizons.External
{
public class OrbitModule : Module
{
public int SemiMajorAxis { get; set; } = 5000;
public int SemiMajorAxis { get; set; }
public float Inclination { get; set; }
public string PrimaryBody { get; set; }
public bool IsMoon { get; set; }

View File

@ -33,7 +33,7 @@ namespace NewHorizons.External
public PlanetConfig(Dictionary<string, object> dict)
{
// Always have to have a base module and orbit module
// Always have to have a base module
Base = new BaseModule();
Orbit = new OrbitModule();

View File

@ -14,6 +14,8 @@ using OWML.Common;
using OWML.ModHelper;
using OWML.Utils;
using PacificEngine.OW_CommonResources.Game.Player;
using PacificEngine.OW_CommonResources.Game.Resource;
using PacificEngine.OW_CommonResources.Game.State;
using System;
using System.Collections.Generic;
using System.IO;
@ -421,7 +423,64 @@ namespace NewHorizons
}
// Do stuff that's shared between generating new planets and updating old ones
return SharedGenerateBody(body, go, sector, rb);
go = SharedGenerateBody(body, go, sector, rb);
// Update a position using CommonResources
// Since orbits are always there just check if they set a semi major axis
if (body.Config.Orbit != null && body.Config.Orbit.SemiMajorAxis != 0f)
{
var mapping = Planet.defaultMapping;
var heavenlyBody = CommonResourcesUtilities.HeavenlyBodyFromAstroObject(AstroObjectLocator.GetAstroObject(body.Config.Name));
Logger.Log($"Updating position of {body.Config.Name} -> {heavenlyBody}");
if (heavenlyBody != PacificEngine.OW_CommonResources.Game.Resource.HeavenlyBody.None)
{
var original = mapping[heavenlyBody];
var targetScale = original.state.orbit.scale;
var coords = OrbitalHelper.KeplerCoordinatesFromOrbitModule(body.Config.Orbit);
var orientation = original.state.orbit.orientation;
var parent = original.state.parent;
if (body.Config.Orbit.PrimaryBody != null)
{
var parentAO = AstroObjectLocator.GetAstroObject(body.Config.Orbit.PrimaryBody);
var newParent = CommonResourcesUtilities.HeavenlyBodyFromAstroObject(parentAO);
if (newParent != HeavenlyBody.None)
{
parent = newParent;
// Have to change the gravity stuff
go.GetComponentInChildren<ConstantForceDetector>()._detectableFields = new ForceVolume[] { parentAO.GetGravityVolume() };
go.GetComponent<AstroObject>()._primaryBody = parentAO;
Logger.Log($"WHO???? {body.Config.Name}, {parentAO.name}");
//Instance.ModHelper.Events.Unity.FireOnNextUpdate(() => UpdatePosition(go, body, parentAO));
}
else Logger.LogError($"Couldn't find new parent {body.Config.Orbit.PrimaryBody}");
}
Logger.Log($"{body.Config.Name} has parent {parent}");
var planetoid = new Planet.Plantoid(
original.size,
original.gravity,
go.transform.rotation,
InitialMotionBuilder.SiderealPeriodToAngularSpeed(body.Config.Orbit.SiderealPeriod),
parent,
coords
);
mapping[heavenlyBody] = planetoid;
Planet.defaultMapping = mapping;
Planet.mapping = mapping;
}
else
{
Logger.LogError($"Couldn't find heavenlyBody for {body.Config.Name}");
}
}
return go;
}
#endregion Load
@ -504,13 +563,7 @@ namespace NewHorizons
body.Object = go;
// Now that we're done move the planet into place
go.transform.parent = Locator.GetRootTransform();
go.transform.position = OrbitalHelper.GetCartesian(new OrbitalHelper.Gravity(1, 100), body.Config.Orbit).Item1 + (primaryBody == null ? Vector3.zero : primaryBody.transform.position);
if (go.transform.position.magnitude > FurthestOrbit)
{
FurthestOrbit = go.transform.position.magnitude + 30000f;
}
UpdatePosition(go, body, primaryBody);
// Have to do this after setting position
var initialMotion = InitialMotionBuilder.Make(go, primaryBody, owRigidBody, body.Config.Orbit);
@ -603,6 +656,17 @@ namespace NewHorizons
return go;
}
private void UpdatePosition(GameObject go, NewHorizonsBody body, AstroObject primaryBody)
{
go.transform.parent = Locator.GetRootTransform();
go.transform.position = OrbitalHelper.GetCartesian(new OrbitalHelper.Gravity(1, 100), body.Config.Orbit).Item1 + (primaryBody == null ? Vector3.zero : primaryBody.transform.position);
if (go.transform.position.magnitude > FurthestOrbit)
{
FurthestOrbit = go.transform.position.magnitude + 30000f;
}
}
#endregion Body generation
#region Change star system

View File

@ -0,0 +1,61 @@
using PacificEngine.OW_CommonResources.Game.Resource;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NewHorizons.Utility
{
public static class CommonResourcesUtilities
{
public static HeavenlyBody HeavenlyBodyFromAstroObject(AstroObject obj)
{
switch (obj.GetAstroObjectName())
{
case AstroObject.Name.CustomString:
return HeavenlyBody.FromString(obj.GetCustomName());
case AstroObject.Name.BrittleHollow:
return HeavenlyBodies.BrittleHollow;
case AstroObject.Name.CaveTwin:
return HeavenlyBodies.EmberTwin;
case AstroObject.Name.Comet:
return HeavenlyBodies.Interloper;
case AstroObject.Name.DarkBramble:
return HeavenlyBodies.DarkBramble;
case AstroObject.Name.DreamWorld:
return HeavenlyBodies.DreamWorld;
case AstroObject.Name.GiantsDeep:
return HeavenlyBodies.GiantsDeep;
case AstroObject.Name.HourglassTwins:
return HeavenlyBodies.HourglassTwins;
case AstroObject.Name.MapSatellite:
return HeavenlyBodies.SatiliteMapping;
case AstroObject.Name.ProbeCannon:
return HeavenlyBodies.ProbeCannon;
case AstroObject.Name.QuantumMoon:
return HeavenlyBodies.QuantumMoon;
case AstroObject.Name.RingWorld:
return HeavenlyBodies.Stranger;
case AstroObject.Name.Sun:
return HeavenlyBodies.Sun;
case AstroObject.Name.SunStation:
return HeavenlyBodies.SunStation;
case AstroObject.Name.TimberHearth:
return HeavenlyBodies.TimberHearth;
case AstroObject.Name.TimberMoon:
return HeavenlyBodies.Attlerock;
case AstroObject.Name.TowerTwin:
return HeavenlyBodies.AshTwin;
case AstroObject.Name.VolcanicMoon:
return HeavenlyBodies.HollowLantern;
case AstroObject.Name.WhiteHole:
return HeavenlyBodies.WhiteHole;
case AstroObject.Name.WhiteHoleTarget:
return HeavenlyBodies.WhiteHoleStation;
default:
return HeavenlyBodies.None;
}
}
}
}

View File

@ -99,60 +99,67 @@ namespace NewHorizons.Utility
public static GameObject Find(string path)
{
var go = GameObject.Find(path);
var names = path.Split(new char[] { '\\', '/' });
if (go == null)
try
{
var go = GameObject.Find(path);
// Get the root object and hope its the right one
var root = GameObject.Find(names[0]);
if (root == null) root = FindObjectOfTypeAndName<GameObject>(names[0]);
var t = root?.transform;
if (t == null)
var names = path.Split(new char[] { '\\', '/' });
if (go == null)
{
Logger.LogWarning($"Couldn't find root object in path ({names[0]})");
return null;
}
for (int i = 1; i < names.Length; i++)
{
var child = t.transform.Find(names[i]);
// Get the root object and hope its the right one
var root = GameObject.Find(names[0]);
if (root == null) root = FindObjectOfTypeAndName<GameObject>(names[0]);
if(child == null)
var t = root?.transform;
if (t == null)
{
foreach(Transform c in t.GetComponentsInChildren<Transform>(true))
Logger.LogWarning($"Couldn't find root object in path ({names[0]})");
return null;
}
for (int i = 1; i < names.Length; i++)
{
var child = t.transform.Find(names[i]);
if (child == null)
{
if(t.name.Equals(names[i]))
foreach (Transform c in t.GetComponentsInChildren<Transform>(true))
{
child = c;
break;
if (t.name.Equals(names[i]))
{
child = c;
break;
}
}
}
if (child == null)
{
Logger.LogWarning($"Couldn't find object in path ({names[i]})");
t = null;
break;
}
t = child;
}
if (child == null)
{
Logger.LogWarning($"Couldn't find object in path ({names[i]})");
t = null;
break;
}
t = child;
go = t?.gameObject;
}
go = t?.gameObject;
}
if (go == null)
{
var name = names.Last();
Logger.LogWarning($"Couldn't find object {path}, will look for potential matches for name {name}");
go = FindObjectOfTypeAndName<GameObject>(name);
}
if(go == null)
return go;
}
catch(Exception)
{
var name = names.Last();
Logger.LogWarning($"Couldn't find object {path}, will look for potential matches for name {name}");
go = FindObjectOfTypeAndName<GameObject>(name);
return null;
}
return go;
}
}
}