From a158f05a9dfd50b86e8da46b524231abce7b7206 Mon Sep 17 00:00:00 2001 From: "Nick J. Connors" Date: Tue, 4 Jan 2022 15:29:10 -0500 Subject: [PATCH] Fixes Fixed comet tail, gravity radius, axial tilt, linear gravity eccentric orbit lines, detail rotation, changed menu screen planets, map satellite orbit, --- NewHorizons/Builder/Body/CometTailBuilder.cs | 2 +- NewHorizons/Builder/General/GravityBuilder.cs | 5 + .../Builder/Orbital/InitialMotionBuilder.cs | 2 +- .../Builder/Orbital/OrbitlineBuilder.cs | 12 +- NewHorizons/Builder/Props/PropBuilder.cs | 27 +---- .../Components/MapSatelliteOrbitFix.cs | 22 ++++ NewHorizons/External/BaseModule.cs | 1 + NewHorizons/Main.cs | 114 ++++++++++++------ NewHorizons/NewHorizons.csproj | 1 + NewHorizons/Utility/AstroObjectLocator.cs | 2 +- NewHorizons/Utility/RandomUtility.cs | 51 ++++++++ 11 files changed, 174 insertions(+), 65 deletions(-) create mode 100644 NewHorizons/Components/MapSatelliteOrbitFix.cs create mode 100644 NewHorizons/Utility/RandomUtility.cs diff --git a/NewHorizons/Builder/Body/CometTailBuilder.cs b/NewHorizons/Builder/Body/CometTailBuilder.cs index 85ab4eb1..f65a99d8 100644 --- a/NewHorizons/Builder/Body/CometTailBuilder.cs +++ b/NewHorizons/Builder/Body/CometTailBuilder.cs @@ -17,7 +17,7 @@ namespace NewHorizons.Builder.Body cometTail.transform.localPosition = Vector3.zero; cometTail.name = "CometTail"; cometTail.transform.localScale = Vector3.one * module.SurfaceSize / 110; - cometTail.transform.localRotation = Quaternion.Euler(0, 180, 90); + cometTail.transform.localRotation = Quaternion.Euler(0, 90, 90); /* var alignment = cometTail.AddComponent(); alignment.SetTargetBody(primary.GetAttachedOWRigidbody()); diff --git a/NewHorizons/Builder/General/GravityBuilder.cs b/NewHorizons/Builder/General/GravityBuilder.cs index 3a049f94..ae2b2b1c 100644 --- a/NewHorizons/Builder/General/GravityBuilder.cs +++ b/NewHorizons/Builder/General/GravityBuilder.cs @@ -19,6 +19,11 @@ namespace NewHorizons.Builder.General var gravityRadius = GM / 0.1f; if (exponent == 2f) gravityRadius = Mathf.Sqrt(gravityRadius); + // To let you actually orbit things the way you would expect we cap this at 4x the diameter if its not a star or black hole (this is what giants deep has) + if (config.Star != null || config.Singularity != null) gravityRadius = Mathf.Min(gravityRadius, 4 * config.Base.SurfaceSize); + else gravityRadius = Mathf.Min(gravityRadius, 15 * config.Base.SurfaceSize); + if (config.Base.SphereOfInfluence != 0f) gravityRadius = config.Base.SphereOfInfluence; + GameObject gravityGO = new GameObject("GravityWell"); gravityGO.transform.parent = body.transform; gravityGO.transform.localPosition = Vector3.zero; diff --git a/NewHorizons/Builder/Orbital/InitialMotionBuilder.cs b/NewHorizons/Builder/Orbital/InitialMotionBuilder.cs index c41a352e..039e0ddb 100644 --- a/NewHorizons/Builder/Orbital/InitialMotionBuilder.cs +++ b/NewHorizons/Builder/Orbital/InitialMotionBuilder.cs @@ -26,7 +26,7 @@ namespace NewHorizons.Builder.Orbital // Rotation initialMotion.SetValue("_initAngularSpeed", orbit.SiderealPeriod == 0 ? 0f : 1.0f / orbit.SiderealPeriod); - var rotationAxis = Quaternion.AngleAxis(orbit.AxialTilt + orbit.Inclination, Vector3.right) * Vector3.up; + var rotationAxis = Quaternion.AngleAxis(orbit.AxialTilt + orbit.Inclination + 90f, Vector3.right) * Vector3.up; body.transform.rotation = Quaternion.FromToRotation(Vector3.up, rotationAxis); initialMotion._orbitImpulseScalar = 0f; diff --git a/NewHorizons/Builder/Orbital/OrbitlineBuilder.cs b/NewHorizons/Builder/Orbital/OrbitlineBuilder.cs index b41e196f..aeaff924 100644 --- a/NewHorizons/Builder/Orbital/OrbitlineBuilder.cs +++ b/NewHorizons/Builder/Orbital/OrbitlineBuilder.cs @@ -23,10 +23,16 @@ namespace NewHorizons.Builder.Orbital var ecc = config.Orbit.Eccentricity; + var parentGravity = astroobject.GetPrimaryBody()?.GetGravityVolume(); + OrbitLine orbitLine; - if (ecc == 0) orbitLine = orbitGO.AddComponent(); - else if (ecc > 0 && ecc < 1) orbitLine = orbitGO.AddComponent(); - else orbitLine = orbitGO.AddComponent(); + if (ecc == 0) + orbitLine = orbitGO.AddComponent(); + // Doesn't work for linear eccentric falloff + else if (ecc > 0 && ecc < 1 && (parentGravity != null && parentGravity._falloffType == GravityVolume.FalloffType.inverseSquared)) + orbitLine = orbitGO.AddComponent(); + else + orbitLine = orbitGO.AddComponent(); var color = Color.white; if (config.Orbit.Tint != null) color = config.Orbit.Tint.ToColor32(); diff --git a/NewHorizons/Builder/Props/PropBuilder.cs b/NewHorizons/Builder/Props/PropBuilder.cs index c3707beb..e626aa6d 100644 --- a/NewHorizons/Builder/Props/PropBuilder.cs +++ b/NewHorizons/Builder/Props/PropBuilder.cs @@ -94,8 +94,8 @@ namespace NewHorizons.Builder.Props prop.transform.parent = go.transform; prop.transform.localPosition = position == null ? Vector3.zero : (Vector3)position; - Quaternion rot = rotation == null ? prefab.transform.rotation : Quaternion.Euler((Vector3)rotation); - prop.transform.rotation = rot; + Quaternion rot = rotation == null ? Quaternion.identity : Quaternion.Euler((Vector3)rotation); + prop.transform.localRotation = rot; if (alignWithNormal) { var up = prop.transform.localPosition.normalized; @@ -116,7 +116,7 @@ namespace NewHorizons.Builder.Props private static void MakeScatter(GameObject go, PropModule.ScatterInfo[] scatterInfo, float radius, Sector sector, IModAssets assets, string uniqueModName) { var area = 4f * Mathf.PI * radius * radius; - var points = FibonacciSphere((int)area); + var points = RandomUtility.FibonacciSphere((int)area); foreach (var propInfo in scatterInfo) { @@ -136,27 +136,6 @@ namespace NewHorizons.Builder.Props } } - private static List FibonacciSphere(int samples) - { - List points = new List(); - - var phi = Mathf.PI * (3f - Mathf.Sqrt(5f)); - - for(int i = 0; i < samples; i++) - { - var y = 1 - (i / (float)(samples - 1)) * 2f; - var radius = Mathf.Sqrt(1 - y * y); - - var theta = phi * i; - - var x = Mathf.Cos(theta) * radius; - var z = Mathf.Sin(theta) * radius; - - points.Add(new Vector3(x, y, z)); - } - return points; - } - private static GameObject LoadPrefab(string assetBundle, string path, string uniqueModName, IModAssets assets) { string key = uniqueModName + "." + assetBundle; diff --git a/NewHorizons/Components/MapSatelliteOrbitFix.cs b/NewHorizons/Components/MapSatelliteOrbitFix.cs new file mode 100644 index 00000000..2b4fca89 --- /dev/null +++ b/NewHorizons/Components/MapSatelliteOrbitFix.cs @@ -0,0 +1,22 @@ +using NewHorizons.Builder.General; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UnityEngine; + +namespace NewHorizons.Components +{ + public class MapSatelliteOrbitFix : MonoBehaviour + { + public void Awake() + { + var detector = base.transform.GetComponentInChildren(); + var ao = base.GetComponent(); + var newDetector = DetectorBuilder.Make(base.gameObject, ao.GetAttachedOWRigidbody(), ao.GetPrimaryBody(), ao); + newDetector.transform.parent = detector.transform.parent; + GameObject.Destroy(detector); + } + } +} diff --git a/NewHorizons/External/BaseModule.cs b/NewHorizons/External/BaseModule.cs index f382d9c6..8ca4747c 100644 --- a/NewHorizons/External/BaseModule.cs +++ b/NewHorizons/External/BaseModule.cs @@ -14,6 +14,7 @@ namespace NewHorizons.External public float SurfaceGravity { get; set; } public string GravityFallOff { get; set; } = "linear"; public float SurfaceSize { get; set; } + public float SphereOfInfluence { get; set; } public float WaterSize { get; set; } public float GroundSize { get; set; } public float LavaSize { get; set; } diff --git a/NewHorizons/Main.cs b/NewHorizons/Main.cs index 50194d87..51c50c01 100644 --- a/NewHorizons/Main.cs +++ b/NewHorizons/Main.cs @@ -4,6 +4,7 @@ using NewHorizons.Builder.Body; using NewHorizons.Builder.General; using NewHorizons.Builder.Orbital; using NewHorizons.Builder.Props; +using NewHorizons.Components; using NewHorizons.External; using NewHorizons.OrbitalPhysics; using NewHorizons.Utility; @@ -24,7 +25,6 @@ namespace NewHorizons { public class Main : ModBehaviour { - public static AssetBundle ShaderBundle; public static Main Instance { get; private set; } @@ -34,7 +34,8 @@ namespace NewHorizons public static float FurthestOrbit { get; set; } = 50000f; public StarLightController StarLightController { get; private set; } - private static string _currentStarSystem = "SolarSystem"; + private string _currentStarSystem = "SolarSystem"; + private bool _isChangingStarSystem = false; public override object GetApi() { @@ -45,6 +46,7 @@ namespace NewHorizons { SceneManager.sceneLoaded += OnSceneLoaded; Instance = this; + GlobalMessenger.AddListener("PlayerDeath", OnDeath); ShaderBundle = Main.Instance.ModHelper.Assets.LoadBundle("AssetBundle/shader"); Utility.Patches.Apply(); @@ -60,7 +62,7 @@ namespace NewHorizons Logger.LogWarning("Couldn't find planets folder"); } - //UnityEngine.Random.InitState(); + UnityEngine.Random.InitState((int)DateTime.Now.Ticks); Instance.ModHelper.Events.Unity.FireOnNextUpdate(() => OnSceneLoaded(SceneManager.GetActiveScene(), LoadSceneMode.Single)); } @@ -68,21 +70,36 @@ namespace NewHorizons { Logger.Log($"Destroying NewHorizons"); SceneManager.sceneLoaded -= OnSceneLoaded; + GlobalMessenger.RemoveListener("PlayerDeath", OnDeath); + } + + void OnDeath(DeathType _) + { + // We reset the solar system on death (unless we just killed the player) + if (!_isChangingStarSystem) _currentStarSystem = "SolarSystem"; } void OnSceneLoaded(Scene scene, LoadSceneMode mode) { Logger.Log($"Scene Loaded: {scene.name} {mode}"); + _isChangingStarSystem = false; + HeavenlyBodyBuilder.Reset(); if (scene.name.Equals("TitleScreen")) DisplayBodyOnTitleScreen(); - if (scene.name != "SolarSystem") return; + if (scene.name != "SolarSystem") + { + // Reset back to original solar system after going to main menu. + _currentStarSystem = "SolarSystem"; + return; + } NewHorizonsData.Load(); Instance.ModHelper.Events.Unity.FireOnNextUpdate(() => ShipLogBuilder.Init()); + Instance.ModHelper.Events.Unity.FireOnNextUpdate(() => AstroObjectLocator.GetAstroObject("MapSatellite").gameObject.AddComponent()); // Need to manage this when there are multiple stars var sun = GameObject.Find("Sun_Body"); @@ -188,60 +205,85 @@ namespace NewHorizons public void DisplayBodyOnTitleScreen() { //Try loading one planet why not - GameObject titleScreenGO = new GameObject("TitleScreenPlanet"); - var eligible = BodyList.Where(b => b.Config.Ring != null && (b.Config.HeightMap != null || (b.Config.Atmosphere?.Cloud != null))).ToArray(); - var body = eligible[UnityEngine.Random.Range(0, eligible.Count())]; + var eligible = BodyList.Where(b => b.Config.HeightMap != null || b.Config.Atmosphere?.Cloud != null).ToArray(); + var eligibleCount = eligible.Count(); + if (eligibleCount == 0) return; + + var selectionCount = Mathf.Max(eligibleCount, UnityEngine.Random.Range(1, 3)); + var indices = RandomUtility.GetUniqueRandomArray(0, eligible.Count(), selectionCount); + + var body = eligible[UnityEngine.Random.Range(0, eligibleCount)]; Logger.Log($"Displaying {body.Config.Name} on the title screen"); - var flag = false; + GameObject body1, body2, body3; + + body1 = LoadTitleScreenBody(eligible[indices[0]]); + if (selectionCount > 2) + { + body1.transform.localScale = Vector3.one * (body1.transform.localScale.x) * 0.3f; + body1.transform.localPosition = new Vector3(0, -10, 0); + body2 = LoadTitleScreenBody(eligible[indices[1]]); + body2.transform.localScale = Vector3.one * (body2.transform.localScale.x) * 0.3f; + body2.transform.localPosition = new Vector3(7, 40, 0); + } + if (selectionCount > 3) + { + body3 = LoadTitleScreenBody(eligible[indices[2]]); + body3.transform.localScale = Vector3.one * (body3.transform.localScale.x) * 0.3f; + body3.transform.localPosition = new Vector3(-5, 15, 0); + } + + GameObject.Find("Scene/Background/PlanetPivot/Prefab_HEA_Campfire").SetActive(false); + GameObject.Find("Scene/Background/PlanetPivot/PlanetRoot").SetActive(false); + + var lightGO = new GameObject("Light"); + lightGO.transform.parent = GameObject.Find("Scene/Background").transform; + lightGO.transform.localPosition = new Vector3(-47.9203f, 145.7596f, 43.1802f); + var light = lightGO.AddComponent(); + light.color = new Color(1f, 1f, 1f, 1f); + light.range = 100; + light.intensity = 0.8f; + } + + private GameObject LoadTitleScreenBody(NewHorizonsBody body) + { + Logger.Log($"Displaying {body.Config.Name} on the title screen"); + GameObject titleScreenGO = new GameObject(body.Config.Name + "_TitleScreen"); HeightMapModule heightMap = new HeightMapModule(); var minSize = 20; var maxSize = 35; float size = minSize; if (body.Config.HeightMap != null) { - size = Mathf.Clamp(body.Config.HeightMap.MaxHeight / 10, minSize, maxSize); + size = Mathf.Clamp(body.Config.HeightMap.MaxHeight / 10, minSize, maxSize); heightMap.TextureMap = body.Config.HeightMap.TextureMap; heightMap.HeightMap = body.Config.HeightMap.HeightMap; heightMap.MaxHeight = size; heightMap.MinHeight = body.Config.HeightMap.MinHeight * size / body.Config.HeightMap.MaxHeight; - flag = true; } if (body.Config.Atmosphere != null && body.Config.Atmosphere.Cloud != null) { // Hacky but whatever I just want a sphere size = Mathf.Clamp(body.Config.Atmosphere.Size / 10, minSize, maxSize); - heightMap.MaxHeight = heightMap.MinHeight = size+1; + heightMap.MaxHeight = heightMap.MinHeight = size + 1; heightMap.TextureMap = body.Config.Atmosphere.Cloud; - flag = true; } - if (flag) + HeightMapBuilder.Make(titleScreenGO, heightMap, body.Assets); + if (body.Config.Ring != null) { - HeightMapBuilder.Make(titleScreenGO, heightMap, body.Assets); - if (body.Config.Ring != null) - { - RingModule newRing = new RingModule(); - newRing.InnerRadius = size * 1.2f; - newRing.OuterRadius = size * 2f; - newRing.Texture = body.Config.Ring.Texture; - RingBuilder.Make(titleScreenGO, newRing, body.Assets); - titleScreenGO.transform.localScale = Vector3.one * 0.8f; - } - GameObject.Find("Scene/Background/PlanetPivot/Prefab_HEA_Campfire").SetActive(false); - GameObject.Find("Scene/Background/PlanetPivot/PlanetRoot").SetActive(false); - titleScreenGO.transform.parent = GameObject.Find("Scene/Background/PlanetPivot/").transform; - titleScreenGO.transform.localPosition = Vector3.zero; - - var lightGO = new GameObject("Light"); - lightGO.transform.parent = titleScreenGO.transform.parent.parent; - lightGO.transform.localPosition = new Vector3(-47.9203f, 145.7596f, 43.1802f); - var light = lightGO.AddComponent(); - light.color = new Color(1f, 1f, 1f, 1f); - light.range = 100; - light.intensity = 0.8f; + RingModule newRing = new RingModule(); + newRing.InnerRadius = size * 1.2f; + newRing.OuterRadius = size * 2f; + newRing.Texture = body.Config.Ring.Texture; + RingBuilder.Make(titleScreenGO, newRing, body.Assets); + titleScreenGO.transform.localScale = Vector3.one * 0.8f; } + titleScreenGO.transform.parent = GameObject.Find("Scene/Background/PlanetPivot/").transform; + titleScreenGO.transform.localPosition = Vector3.zero; + + return titleScreenGO; } private bool LoadBody(NewHorizonsBody body, bool defaultPrimaryToSun = false) @@ -480,6 +522,8 @@ namespace NewHorizons public void ChangeCurrentStarSystem(string newStarSystem) { _currentStarSystem = newStarSystem; + _isChangingStarSystem = true; + Locator.GetDeathManager().KillPlayer(DeathType.Meditation); LoadManager.LoadSceneAsync(OWScene.SolarSystem, true, LoadManager.FadeType.ToBlack, 0.1f, true); } } diff --git a/NewHorizons/NewHorizons.csproj b/NewHorizons/NewHorizons.csproj index ac738e27..2ad71669 100644 --- a/NewHorizons/NewHorizons.csproj +++ b/NewHorizons/NewHorizons.csproj @@ -33,6 +33,7 @@ $(OuterWildsModsDirectory)\PacificEngine.OW_CommonResources\PacificEngine.OW_CommonResources.dll + False diff --git a/NewHorizons/Utility/AstroObjectLocator.cs b/NewHorizons/Utility/AstroObjectLocator.cs index 349abe25..f86edd36 100644 --- a/NewHorizons/Utility/AstroObjectLocator.cs +++ b/NewHorizons/Utility/AstroObjectLocator.cs @@ -19,7 +19,7 @@ namespace NewHorizons.Utility public static AstroObject GetAstroObject(string name) { - if (name.Equals("MAP_SATELLITE")) + if (name.ToUpper().Replace("_", "").Equals("MAPSATELLITE")) return GetAstroObject(AstroObject.Name.MapSatellite); var aoName = AstroObject.StringIDToAstroObjectName(name); if(aoName == AstroObject.Name.None) aoName = AstroObject.StringIDToAstroObjectName(name.ToUpper().Replace(" ", "_")); diff --git a/NewHorizons/Utility/RandomUtility.cs b/NewHorizons/Utility/RandomUtility.cs new file mode 100644 index 00000000..820bf3be --- /dev/null +++ b/NewHorizons/Utility/RandomUtility.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UnityEngine; + +namespace NewHorizons.Utility +{ + public static class RandomUtility + { + public static int[] GetUniqueRandomArray(int min, int max, int count) + { + int[] result = new int[count]; + List numbersInOrder = new List(); + for (var x = min; x < max; x++) + { + numbersInOrder.Add(x); + } + for (var x = 0; x < count; x++) + { + var randomIndex = UnityEngine.Random.Range(0, numbersInOrder.Count); + result[x] = numbersInOrder[randomIndex]; + numbersInOrder.RemoveAt(randomIndex); + } + + return result; + } + + public static List FibonacciSphere(int samples) + { + List points = new List(); + + var phi = Mathf.PI * (3f - Mathf.Sqrt(5f)); + + for (int i = 0; i < samples; i++) + { + var y = 1 - (i / (float)(samples - 1)) * 2f; + var radius = Mathf.Sqrt(1 - y * y); + + var theta = phi * i; + + var x = Mathf.Cos(theta) * radius; + var z = Mathf.Sin(theta) * radius; + + points.Add(new Vector3(x, y, z)); + } + return points; + } + } +}