// Post spawn fixups (ewwwww........) public void PostSpawnFixups() { Debug.Log("[Kopernicus]: Post-Spawn"); // Fix the flight globals index of each body and patch it's SOI int counter = 0; foreach (CelestialBody body in FlightGlobals.Bodies) { // Patch the flightGlobalsIndex body.flightGlobalsIndex = counter++; // Finalize the Orbit if (Templates.finalizeBodies.Contains(body.transform.name)) { OrbitLoader.FinalizeOrbit(body); } // Patch the SOI if (Templates.sphereOfInfluence.ContainsKey(body.transform.name)) { body.sphereOfInfluence = Templates.sphereOfInfluence[body.transform.name]; } // Patch the Hill Sphere if (Templates.hillSphere.ContainsKey(body.transform.name)) { body.hillSphere = Templates.hillSphere[body.transform.name]; } // Make the Body a barycenter if (Templates.barycenters.Contains(body.transform.name)) { body.scaledBody.SetActive(false); } Logger.Default.Log("Found Body: " + body.bodyName + ":" + body.flightGlobalsIndex + " -> SOI = " + body.sphereOfInfluence + ", Hill Sphere = " + body.hillSphere); } // Fix the maximum viewing distance of the map view camera (get the farthest away something can be from the root object) PSystemBody rootBody = PSystemManager.Instance.systemPrefab.rootBody; double maximumDistance = 1000d; // rootBody.children.Max(b => (b.orbitDriver != null) ? b.orbitDriver.orbit.semiMajorAxis * (1 + b.orbitDriver.orbit.eccentricity) : 0); if (rootBody != null) { maximumDistance = rootBody.celestialBody.Radius * 100d; if (rootBody.children != null && rootBody.children.Count > 0) { foreach (PSystemBody body in rootBody.children) { if (body.orbitDriver != null) { maximumDistance = Math.Max(maximumDistance, body.orbitDriver.orbit.semiMajorAxis * (1d + body.orbitDriver.orbit.eccentricity)); } else { Debug.Log("[Kopernicus]: Body " + body.name + " has no orbitdriver!"); } } } else { Debug.Log("[Kopernicus]: Root body children null or 0"); } } else { Debug.Log("[Kopernicus]: Root body null!"); } if (Templates.maxViewDistance >= 0) { maximumDistance = Templates.maxViewDistance; Debug.Log("Found max distance override " + maximumDistance); } else { Debug.Log("Found max distance " + maximumDistance); } PlanetariumCamera.fetch.maxDistance = ((float)maximumDistance * 3.0f) / ScaledSpace.Instance.scaleFactor; // Select the closest star to home StarLightSwitcher.HomeStar().SetAsActive(); // Flush the logger Logger.Default.Flush(); // Fixups complete, time to surrender to fate Destroy(this); }
// Post spawn fixups (ewwwww........) public void PostSpawnFixups() { // Wrap this in a try - catch block so we can display a warning if Kopernicus fails to load for some reason try { // Log Debug.Log("[Kopernicus]: Post-Spawn"); // Fire Event Events.OnPreFixing.Fire(); // Fix the SpaceCenter SpaceCenter.Instance = PSystemManager.Instance.localBodies.First(cb => cb.isHomeWorld).GetComponentsInChildren <SpaceCenter>(true).FirstOrDefault(); SpaceCenter.Instance.Start(); // Fix the flight globals index of each body and patch it's SOI Int32 counter = 0; foreach (CelestialBody body in FlightGlobals.Bodies) { // Event Events.OnPreBodyFixing.Fire(body); // Patch the flightGlobalsIndex body.flightGlobalsIndex = counter++; // Finalize the Orbit if (body.Get("finalizeBody", false)) { OrbitLoader.FinalizeOrbit(body); } // Set Custom OrbitalPeriod if (body.Has("customOrbitalPeriod")) { OrbitLoader.OrbitalPeriod(body); } // Patch the SOI if (body.Has("sphereOfInfluence")) { body.sphereOfInfluence = body.Get <Double>("sphereOfInfluence"); } // Patch the Hill Sphere if (body.Has("hillSphere")) { body.hillSphere = body.Get <Double>("hillSphere"); } // Make the Body a barycenter if (body.Get("barycenter", false)) { body.scaledBody.SetActive(false); } // Make the bodies scaled space invisible if (body.Get("invisibleScaledSpace", false)) { foreach (Renderer renderer in body.scaledBody.GetComponentsInChildren <Renderer>(true)) { renderer.enabled = false; } } // Event Events.OnPostBodyFixing.Fire(body); // Log Logger.Default.Log("Found Body: " + body.bodyName + ":" + body.flightGlobalsIndex + " -> SOI = " + body.sphereOfInfluence + ", Hill Sphere = " + body.hillSphere); } // Fix the maximum viewing distance of the map view camera (get the farthest away something can be from the root object) PSystemBody rootBody = PSystemManager.Instance.systemPrefab.rootBody; Double maximumDistance = 1000d; // rootBody.children.Max(b => (b.orbitDriver != null) ? b.orbitDriver.orbit.semiMajorAxis * (1 + b.orbitDriver.orbit.eccentricity) : 0); if (rootBody != null) { maximumDistance = rootBody.celestialBody.Radius * 100d; if (rootBody.children != null && rootBody.children.Count > 0) { foreach (PSystemBody body in rootBody.children) { if (body.orbitDriver != null) { maximumDistance = Math.Max(maximumDistance, body.orbitDriver.orbit.semiMajorAxis * (1d + body.orbitDriver.orbit.eccentricity)); } else { Debug.Log("[Kopernicus]: Body " + body.name + " has no orbitdriver!"); } } } else { Debug.Log("[Kopernicus]: Root body children null or 0"); } } else { Debug.Log("[Kopernicus]: Root body null!"); } if (Templates.maxViewDistance >= 0) { maximumDistance = Templates.maxViewDistance; Debug.Log("Found max distance override " + maximumDistance); } else { Debug.Log("Found max distance " + maximumDistance); } PlanetariumCamera.fetch.maxDistance = ((Single)maximumDistance * 3.0f) / ScaledSpace.Instance.scaleFactor; // Call the event Events.OnPostFixing.Fire(); // Flush the logger Logger.Default.Flush(); // Fixups complete, time to surrender to fate Destroy(this); } catch (Exception e) { // Log the exception Debug.LogException(e); // Open the Warning popup DisplayWarning(); } }
// Execute MainMenu functions void Start() { previous = PlanetariumCamera.fetch.initialTarget; PlanetariumCamera.fetch.targets .Where(m => m.celestialBody != null && (m.celestialBody.Has("barycenter") || m.celestialBody.Has("notSelectable"))) .ToList() .ForEach(map => PlanetariumCamera.fetch.targets.Remove(map)); // Stars GameObject gob = Sun.Instance.gameObject; KopernicusStar star = gob.AddComponent <KopernicusStar>(); Utility.CopyObjectFields(Sun.Instance, star, false); DestroyImmediate(Sun.Instance); Sun.Instance = star; // Bodies Dictionary <String, KeyValuePair <CelestialBody, CelestialBody> > fixes = new Dictionary <String, KeyValuePair <CelestialBody, CelestialBody> >(); foreach (CelestialBody body in PSystemManager.Instance.localBodies) { // More stars if (body.flightGlobalsIndex != 0 && body.scaledBody.GetComponentsInChildren <SunShaderController>(true).Length > 0) { GameObject starObj = Instantiate(Sun.Instance.gameObject); KopernicusStar star_ = starObj.GetComponent <KopernicusStar>(); star_.sun = body; starObj.transform.parent = Sun.Instance.transform.parent; starObj.name = body.name; starObj.transform.localPosition = Vector3.zero; starObj.transform.localRotation = Quaternion.identity; starObj.transform.localScale = Vector3.one; starObj.transform.position = body.position; starObj.transform.rotation = body.rotation; } // Post spawn patcher if (body.Has("orbitPatches")) { ConfigNode orbitNode = body.Get <ConfigNode>("orbitPatches"); OrbitLoader loader = new OrbitLoader(body); Parser.LoadObjectFromConfigurationNode(loader, orbitNode, "Kopernicus"); body.orbitDriver.orbit = loader.orbit; CelestialBody oldRef = body.referenceBody; body.referenceBody.orbitingBodies.Remove(body); body.orbit.referenceBody = body.orbitDriver.referenceBody = PSystemManager.Instance.localBodies.Find(b => b.transform.name == loader.referenceBody); fixes.Add(body.transform.name, new KeyValuePair <CelestialBody, CelestialBody>(oldRef, body.referenceBody)); body.referenceBody.orbitingBodies.Add(body); body.referenceBody.orbitingBodies = body.referenceBody.orbitingBodies.OrderBy(cb => cb.orbit.semiMajorAxis).ToList(); body.orbit.Init(); body.orbitDriver.UpdateOrbit(); // Calculations if (!body.Has("sphereOfInfluence")) { body.sphereOfInfluence = body.orbit.semiMajorAxis * Math.Pow(body.Mass / body.orbit.referenceBody.Mass, 0.4); } if (!body.Has("hillSphere")) { body.hillSphere = body.orbit.semiMajorAxis * (1 - body.orbit.eccentricity) * Math.Pow(body.Mass / body.orbit.referenceBody.Mass, 0.333333333333333); } if (body.solarRotationPeriod) { double rotPeriod = Utility.FindBody(PSystemManager.Instance.systemPrefab.rootBody, body.transform.name).celestialBody.rotationPeriod; double num1 = Math.PI * 2 * Math.Sqrt(Math.Pow(Math.Abs(body.orbit.semiMajorAxis), 3) / body.orbit.referenceBody.gravParameter); body.rotationPeriod = rotPeriod * num1 / (num1 + rotPeriod);; } } } // Update the order in the tracking station List <MapObject> trackingstation = new List <MapObject>(); Utility.DoRecursive(PSystemManager.Instance.localBodies[0], cb => cb.orbitingBodies, cb => { trackingstation.Add(PlanetariumCamera.fetch.targets.Find(t => t.celestialBody == cb)); }); PlanetariumCamera.fetch.targets.Clear(); PlanetariumCamera.fetch.targets.AddRange(trackingstation); // Undo stuff foreach (CelestialBody b in PSystemManager.Instance.localBodies.Where(b_ => b_.Has("orbitPatches"))) { fixes[b.transform.name].Value.orbitingBodies.Remove(b); fixes[b.transform.name].Key.orbitingBodies.Add(b); fixes[b.transform.name].Key.orbitingBodies = fixes[b.transform.name].Key.orbitingBodies.OrderBy(cb => cb.orbit.semiMajorAxis).ToList(); } UpdateMenu(); }
// Apply PostSpawnOrbit patches private void ApplyOrbitPatches() { // Bodies Dictionary <String, KeyValuePair <CelestialBody, CelestialBody> > fixes = new Dictionary <String, KeyValuePair <CelestialBody, CelestialBody> >(); for (Int32 i = 0; i < PSystemManager.Instance.localBodies.Count; i++) { CelestialBody body = PSystemManager.Instance.localBodies[i]; // Post spawn patcher if (!body.Has("orbitPatches")) { continue; } ConfigNode orbitNode = body.Get <ConfigNode>("orbitPatches"); OrbitLoader loader = new OrbitLoader(body); CelestialBody oldRef = body.referenceBody; Parser.LoadObjectFromConfigurationNode(loader, orbitNode, "Kopernicus"); oldRef.orbitingBodies.Remove(body); if (body.referenceBody == null) { // Log the exception Debug.Log("Exception: PostSpawnOrbit reference body for \"" + body.name + "\" could not be found. Missing body name is \"" + loader.ReferenceBody + "\"."); // Open the Warning popup Injector.DisplayWarning(); Destroy(this); return; } fixes.Add(body.transform.name, new KeyValuePair <CelestialBody, CelestialBody>(oldRef, body.referenceBody)); body.referenceBody.orbitingBodies.Add(body); body.referenceBody.orbitingBodies = body.referenceBody.orbitingBodies.OrderBy(cb => cb.orbit.semiMajorAxis).ToList(); body.orbit.Init(); //body.orbitDriver.UpdateOrbit(); // Calculations if (!body.Has("sphereOfInfluence")) { body.sphereOfInfluence = body.orbit.semiMajorAxis * Math.Pow(body.Mass / body.orbit.referenceBody.Mass, 0.4); } if (!body.Has("hillSphere")) { body.hillSphere = body.orbit.semiMajorAxis * (1 - body.orbit.eccentricity) * Math.Pow(body.Mass / body.orbit.referenceBody.Mass, 0.333333333333333); } if (!body.solarRotationPeriod) { continue; } Double rotationPeriod = body.rotationPeriod; Double orbitalPeriod = body.orbit.period; body.rotationPeriod = rotationPeriod * orbitalPeriod / (orbitalPeriod + rotationPeriod); } // Update the order in the tracking station List <MapObject> trackingstation = new List <MapObject>(); Utility.DoRecursive(PSystemManager.Instance.localBodies[0], cb => cb.orbitingBodies, cb => { trackingstation.Add(PlanetariumCamera.fetch.targets.Find(t => t.celestialBody == cb)); }); PlanetariumCamera.fetch.targets = trackingstation.Where(m => m != null).ToList(); // Undo stuff foreach (CelestialBody b in PSystemManager.Instance.localBodies.Where(b => b.Has("orbitPatches"))) { String transformName = b.transform.name; fixes[transformName].Value.orbitingBodies.Remove(b); fixes[transformName].Key.orbitingBodies.Add(b); fixes[transformName].Key.orbitingBodies = fixes[transformName].Key.orbitingBodies .OrderBy(cb => cb.orbit.semiMajorAxis).ToList(); } }
// Execute MainMenu functions void Start() { previous = PlanetariumCamera.fetch.initialTarget; PlanetariumCamera.fetch.targets .Where(m => m.celestialBody != null && (m.celestialBody.Has("barycenter") || !m.celestialBody.Get("selectable", true))) .ToList() .ForEach(map => PlanetariumCamera.fetch.targets.Remove(map)); // Stars GameObject gob = Sun.Instance.gameObject; KopernicusStar star = gob.AddComponent <KopernicusStar>(); Utility.CopyObjectFields(Sun.Instance, star, false); DestroyImmediate(Sun.Instance); Sun.Instance = star; // LensFlares gob = SunFlare.Instance.gameObject; KopernicusSunFlare flare = gob.AddComponent <KopernicusSunFlare>(); gob.name = star.sun.name; Utility.CopyObjectFields(SunFlare.Instance, flare, false); DestroyImmediate(SunFlare.Instance); SunFlare.Instance = star.lensFlare = flare; // Bodies Dictionary <String, KeyValuePair <CelestialBody, CelestialBody> > fixes = new Dictionary <String, KeyValuePair <CelestialBody, CelestialBody> >(); foreach (CelestialBody body in PSystemManager.Instance.localBodies) { // More stars if (body.flightGlobalsIndex != 0 && body.scaledBody.GetComponentsInChildren <SunShaderController>(true).Length > 0) { GameObject starObj = Instantiate(Sun.Instance.gameObject); star = starObj.GetComponent <KopernicusStar>(); star.sun = body; starObj.transform.parent = Sun.Instance.transform.parent; starObj.name = body.name; starObj.transform.localPosition = Vector3.zero; starObj.transform.localRotation = Quaternion.identity; starObj.transform.localScale = Vector3.one; starObj.transform.position = body.position; starObj.transform.rotation = body.rotation; GameObject flareObj = Instantiate(SunFlare.Instance.gameObject); flare = flareObj.GetComponent <KopernicusSunFlare>(); star.lensFlare = flare; flareObj.transform.parent = SunFlare.Instance.transform.parent; flareObj.name = body.name; flareObj.transform.localPosition = Vector3.zero; flareObj.transform.localRotation = Quaternion.identity; flareObj.transform.localScale = Vector3.one; flareObj.transform.position = body.position; flareObj.transform.rotation = body.rotation; } // Post spawn patcher if (body.Has("orbitPatches")) { ConfigNode orbitNode = body.Get <ConfigNode>("orbitPatches"); OrbitLoader loader = new OrbitLoader(body); Parser.LoadObjectFromConfigurationNode(loader, orbitNode, "Kopernicus"); CelestialBody oldRef = body.referenceBody; body.referenceBody.orbitingBodies.Remove(body); CelestialBody newRef = UBI.GetBody(loader.referenceBody); if (newRef != null) { body.orbit.referenceBody = body.orbitDriver.referenceBody = newRef; } else { // Log the exception Debug.Log("Exception: PostSpawnOrbit reference body for \"" + body.name + "\" could not be found. Missing body name is \"" + loader.referenceBody + "\"."); // Open the Warning popup Injector.DisplayWarning(); } fixes.Add(body.transform.name, new KeyValuePair <CelestialBody, CelestialBody>(oldRef, body.referenceBody)); body.referenceBody.orbitingBodies.Add(body); body.referenceBody.orbitingBodies = body.referenceBody.orbitingBodies.OrderBy(cb => cb.orbit.semiMajorAxis).ToList(); body.orbit.Init(); body.orbitDriver.UpdateOrbit(); // Calculations if (!body.Has("sphereOfInfluence")) { body.sphereOfInfluence = body.orbit.semiMajorAxis * Math.Pow(body.Mass / body.orbit.referenceBody.Mass, 0.4); } if (!body.Has("hillSphere")) { body.hillSphere = body.orbit.semiMajorAxis * (1 - body.orbit.eccentricity) * Math.Pow(body.Mass / body.orbit.referenceBody.Mass, 0.333333333333333); } if (body.solarRotationPeriod) { Double rotPeriod = Utility.FindBody(PSystemManager.Instance.systemPrefab.rootBody, body.transform.name).celestialBody.rotationPeriod; Double num1 = Math.PI * 2 * Math.Sqrt(Math.Pow(Math.Abs(body.orbit.semiMajorAxis), 3) / body.orbit.referenceBody.gravParameter); body.rotationPeriod = rotPeriod * num1 / (num1 + rotPeriod);; } } } // Update the order in the tracking station List <MapObject> trackingstation = new List <MapObject>(); Utility.DoRecursive(PSystemManager.Instance.localBodies[0], cb => cb.orbitingBodies, cb => { trackingstation.Add(PlanetariumCamera.fetch.targets.Find(t => t.celestialBody == cb)); }); PlanetariumCamera.fetch.targets.Clear(); PlanetariumCamera.fetch.targets.AddRange(trackingstation); // Update the initialTarget of the tracking station Resources.FindObjectsOfTypeAll <PlanetariumCamera>().FirstOrDefault().initialTarget = Resources.FindObjectsOfTypeAll <ScaledMovement>().FirstOrDefault(o => o.celestialBody.isHomeWorld); // Undo stuff foreach (CelestialBody b in PSystemManager.Instance.localBodies.Where(b_ => b_.Has("orbitPatches"))) { fixes[b.transform.name].Value.orbitingBodies.Remove(b); fixes[b.transform.name].Key.orbitingBodies.Add(b); fixes[b.transform.name].Key.orbitingBodies = fixes[b.transform.name].Key.orbitingBodies.OrderBy(cb => cb.orbit.semiMajorAxis).ToList(); } #if !KSP131 if (ExpansionsLoader.IsExpansionInstalled("MakingHistory")) { PQSCity2[] cities = FindObjectsOfType <PQSCity2>(); foreach (String site in Templates.RemoveLaunchSites) { // Remove the launch site from the list if it exists if (PSystemSetup.Instance.LaunchSites.Any(s => s.name == site)) { PSystemSetup.Instance.RemoveLaunchSite(site); } PQSCity2 city = cities.FirstOrDefault(c => c.gameObject.name == site || c.gameObject.name == site + "(Clone)"); // Kill the PQSCity if it exists if (city != null) { Destroy(city.gameObject); } } // PSystemSetup.RemoveLaunchSite does not remove launch sites from PSystemSetup.StockLaunchSites // Currently an ArgumentOutOfRangeException can result when they are different lists because of a bug in stock code try { FieldInfo stockLaunchSitesField = typeof(PSystemSetup).GetField("stocklaunchsites", BindingFlags.NonPublic | BindingFlags.Instance); LaunchSite[] stockLaunchSites = (LaunchSite[])stockLaunchSitesField.GetValue(PSystemSetup.Instance); LaunchSite[] updateStockLaunchSites = stockLaunchSites.Where(site => !Templates.RemoveLaunchSites.Contains(site.name)).ToArray(); if (stockLaunchSites.Length != updateStockLaunchSites.Length) { stockLaunchSitesField.SetValue(PSystemSetup.Instance, updateStockLaunchSites); } } catch (Exception ex) { Logger.Default.Log("Failed to remove launch sites from 'stockLaunchSites' field. Exception information follows."); Logger.Default.LogException(ex); } } #endif #if FALSE // AFG-Ception foreach (CelestialBody body in PSystemManager.Instance.localBodies) { if (body.afg == null) { continue; } foreach (KopernicusStar s in KopernicusStar.Stars) { AtmosphereFromGround afg; if (s != Sun.Instance) { afg = Instantiate(body.afg.gameObject) .GetComponent <AtmosphereFromGround>(); Utility.CopyObjectFields(body.afg, afg, false); afg.transform.parent = body.afg.transform.parent; afg.transform.localPosition = body.afg.transform.localPosition; afg.transform.localScale = body.afg.transform.localScale; afg.transform.localRotation = body.afg.transform.localRotation; afg.gameObject.layer = body.afg.gameObject.layer; } else { afg = body.afg; } afg.gameObject.AddComponent <KopernicusStarAFG>().Star = s; } } #endif }
// Parser Apply Event public void Apply (ConfigNode node) { // If we have a template, generatedBody *is* the template body if (template != null) { generatedBody = template.body; // Patch the game object names in the template generatedBody.name = name; generatedBody.celestialBody.bodyName = name; generatedBody.celestialBody.transform.name = name; generatedBody.celestialBody.bodyTransform.name = name; generatedBody.scaledVersion.name = name; if (generatedBody.pqsVersion != null) { generatedBody.pqsVersion.name = name; generatedBody.pqsVersion.gameObject.name = name; generatedBody.pqsVersion.transform.name = name; foreach (PQS p in generatedBody.pqsVersion.GetComponentsInChildren(typeof (PQS), true)) p.name = p.name.Replace (template.body.celestialBody.bodyName, name); } // If we've changed the name, reset use_The_InName if (generatedBody.name != template.body.name) { generatedBody.celestialBody.use_The_InName = false; } // If this body has an orbit, create editor/loader if (generatedBody.orbitDriver != null) { orbit = new OrbitLoader(generatedBody); } // If this body has a PQS, create editor/loader if (generatedBody.pqsVersion != null) { pqs = new PQSLoader(generatedBody.pqsVersion); // If this body has an ocean PQS, create editor/loader if (generatedBody.celestialBody.ocean == true) { foreach (PQS PQSocean in generatedBody.pqsVersion.GetComponentsInChildren<PQS>(true)) { if (PQSocean.name == name + "Ocean") { ocean = new OceanPQS(PQSocean); break; } } } } // Create the scaled version editor/loader scaledVersion = new ScaledVersion(generatedBody.scaledVersion, generatedBody.celestialBody, template.type); } // Otherwise we have to generate all the things for this body else { // Create the PSystemBody object GameObject generatedBodyGameObject = new GameObject (name); generatedBodyGameObject.transform.parent = Utility.Deactivator; generatedBody = generatedBodyGameObject.AddComponent<PSystemBody> (); generatedBody.flightGlobalsIndex = 0; // Create the celestial body GameObject generatedBodyProperties = new GameObject (name); generatedBodyProperties.transform.parent = generatedBodyGameObject.transform; generatedBody.celestialBody = generatedBodyProperties.AddComponent<CelestialBody> (); generatedBody.resources = generatedBodyProperties.AddComponent<PResource> (); generatedBody.celestialBody.progressTree = null; // Sensible defaults generatedBody.celestialBody.bodyName = name; generatedBody.celestialBody.atmosphere = false; generatedBody.celestialBody.ocean = false; // Create the scaled version generatedBody.scaledVersion = new GameObject(name); generatedBody.scaledVersion.layer = Constants.GameLayers.ScaledSpace; generatedBody.scaledVersion.transform.parent = Utility.Deactivator; // Create the scaled version editor/loader scaledVersion = new ScaledVersion(generatedBody.scaledVersion, generatedBody.celestialBody, BodyType.Atmospheric); } // Create property editor/loader objects properties = new Properties (generatedBody.celestialBody); // Atmospheric settings atmosphere = new Atmosphere(generatedBody.celestialBody, generatedBody.scaledVersion); // Particles particle = new ParticleLoader(generatedBody.scaledVersion.gameObject); }
public string OrbitPatcher(Body body) { // This if is here to make sure stars don't give us trouble if (body.generatedBody.orbitDriver.orbit.referenceBody == null) body.generatedBody.orbitDriver.orbit.referenceBody = ListOfBodies.Find(rb => rb.name == body.orbit.referenceBody).generatedBody.celestialBody; if (!body.generatedBody.celestialBody.Has("sbPatched") && body.generatedBody.celestialBody.Has("orbitPatches")) { ConfigNode patch = new ConfigNode(); if (body.generatedBody.celestialBody.orbit != null) { OrbitLoader loader = new OrbitLoader(); patch.AddData(body.generatedBody.celestialBody.Get<ConfigNode>("orbitPatches")); Parser.LoadObjectFromConfigurationNode(loader, patch); body.generatedBody.orbitDriver.orbit = new Orbit(loader.orbit); } // This "else" is here to make sure stars don't give us trouble else { OrbitLoader loader = new OrbitLoader(); loader.orbit = new Orbit(); loader.orbit.referenceBody = body.generatedBody.orbitDriver.orbit.referenceBody; patch.AddData(body.generatedBody.celestialBody.Get<ConfigNode>("orbitPatches")); Parser.LoadObjectFromConfigurationNode(loader, patch); if (!patch.HasValue("inclination")) loader.orbit.inclination = 0; if (!patch.HasValue("eccentricity")) loader.orbit.eccentricity = 0; if (!patch.HasValue("semiMajorAxis")) loader.orbit.semiMajorAxis = 0; if (!patch.HasValue("longitudeOfAscendingNode")) loader.orbit.LAN = 0; if (!patch.HasValue("argumentOfPeriapsis")) loader.orbit.argumentOfPeriapsis = 0; if (!patch.HasValue("meanAnomalyAtEpoch") && !patch.HasValue("meanAnomalyAtEpochD")) loader.orbit.meanAnomalyAtEpoch = 0; if (!patch.HasValue("epoch")) loader.orbit.epoch = 0; body.generatedBody.orbitDriver.orbit = new Orbit(loader.orbit); } body.generatedBody.celestialBody.Set("orbitPatches", new ConfigNode()); if (patch.GetValue("referenceBody") != null) body.orbit.referenceBody = patch.GetValue("referenceBody"); if (patch.GetValue("referenceBody") != null && body.name == "Kerbin") { // Keep the ConfigNode for Kerbin's referenceBody in case Kerbin is the sbSecondary ConfigNode temp = body.generatedBody.celestialBody.Get<ConfigNode>("orbitPatches"); temp.AddValue("referenceBody", patch.GetValue("referenceBody")); body.generatedBody.celestialBody.Set("orbitPatches", temp); body.generatedBody.celestialBody.Set("sbPatched", true); } else { body.generatedBody.celestialBody.Set("orbitPatches", new ConfigNode()); } // Fix sphereOfInfluence if (!body.generatedBody.celestialBody.Has("sphereOfInfluence")) body.generatedBody.celestialBody.sphereOfInfluence = body.generatedBody.orbitDriver.orbit.semiMajorAxis * Math.Pow(body.generatedBody.celestialBody.Mass / ListOfBodies.Find(rb => rb.name == body.orbit.referenceBody).generatedBody.celestialBody.Mass, 0.4); } return body.orbit.referenceBody; }
// Execute MainMenu functions void Start() { previous = PlanetariumCamera.fetch.initialTarget; PlanetariumCamera.fetch.targets .Where(m => m.celestialBody != null && (m.celestialBody.Has("barycenter") || !m.celestialBody.Get("selectable", true))) .ToList() .ForEach(map => PlanetariumCamera.fetch.targets.Remove(map)); // Stars GameObject gob = Sun.Instance.gameObject; KopernicusStar star = gob.AddComponent <KopernicusStar>(); Utility.CopyObjectFields(Sun.Instance, star, false); DestroyImmediate(Sun.Instance); Sun.Instance = star; // LensFlares gob = SunFlare.Instance.gameObject; KopernicusSunFlare flare = gob.AddComponent <KopernicusSunFlare>(); gob.name = star.sun.name; Utility.CopyObjectFields(SunFlare.Instance, flare, false); DestroyImmediate(SunFlare.Instance); SunFlare.Instance = star.lensFlare = flare; // Bodies Dictionary <String, KeyValuePair <CelestialBody, CelestialBody> > fixes = new Dictionary <String, KeyValuePair <CelestialBody, CelestialBody> >(); foreach (CelestialBody body in PSystemManager.Instance.localBodies) { // More stars if (body.flightGlobalsIndex != 0 && body.scaledBody.GetComponentsInChildren <SunShaderController>(true).Length > 0) { GameObject starObj = Instantiate(Sun.Instance.gameObject); star = starObj.GetComponent <KopernicusStar>(); star.sun = body; starObj.transform.parent = Sun.Instance.transform.parent; starObj.name = body.name; starObj.transform.localPosition = Vector3.zero; starObj.transform.localRotation = Quaternion.identity; starObj.transform.localScale = Vector3.one; starObj.transform.position = body.position; starObj.transform.rotation = body.rotation; GameObject flareObj = Instantiate(SunFlare.Instance.gameObject); flare = flareObj.GetComponent <KopernicusSunFlare>(); star.lensFlare = flare; flareObj.transform.parent = SunFlare.Instance.transform.parent; flareObj.name = body.name; flareObj.transform.localPosition = Vector3.zero; flareObj.transform.localRotation = Quaternion.identity; flareObj.transform.localScale = Vector3.one; flareObj.transform.position = body.position; flareObj.transform.rotation = body.rotation; } // Post spawn patcher if (body.Has("orbitPatches")) { ConfigNode orbitNode = body.Get <ConfigNode>("orbitPatches"); OrbitLoader loader = new OrbitLoader(body); Parser.LoadObjectFromConfigurationNode(loader, orbitNode, "Kopernicus"); CelestialBody oldRef = body.referenceBody; body.referenceBody.orbitingBodies.Remove(body); CelestialBody newRef = PSystemManager.Instance.localBodies.FirstOrDefault(b => b.transform.name == loader.referenceBody); if (newRef != null) { body.orbit.referenceBody = body.orbitDriver.referenceBody = newRef; } else { // Log the exception Debug.Log("Exception: PostSpawnOrbit reference body for \"" + body.name + "\" could not be found. Missing body name is \"" + loader.referenceBody + "\"."); // Open the Warning popup Injector.DisplayWarning(); } fixes.Add(body.transform.name, new KeyValuePair <CelestialBody, CelestialBody>(oldRef, body.referenceBody)); body.referenceBody.orbitingBodies.Add(body); body.referenceBody.orbitingBodies = body.referenceBody.orbitingBodies.OrderBy(cb => cb.orbit.semiMajorAxis).ToList(); body.orbit.Init(); body.orbitDriver.UpdateOrbit(); // Calculations if (!body.Has("sphereOfInfluence")) { body.sphereOfInfluence = body.orbit.semiMajorAxis * Math.Pow(body.Mass / body.orbit.referenceBody.Mass, 0.4); } if (!body.Has("hillSphere")) { body.hillSphere = body.orbit.semiMajorAxis * (1 - body.orbit.eccentricity) * Math.Pow(body.Mass / body.orbit.referenceBody.Mass, 0.333333333333333); } if (body.solarRotationPeriod) { Double rotPeriod = Utility.FindBody(PSystemManager.Instance.systemPrefab.rootBody, body.transform.name).celestialBody.rotationPeriod; Double num1 = Math.PI * 2 * Math.Sqrt(Math.Pow(Math.Abs(body.orbit.semiMajorAxis), 3) / body.orbit.referenceBody.gravParameter); body.rotationPeriod = rotPeriod * num1 / (num1 + rotPeriod);; } } } // Update the order in the tracking station List <MapObject> trackingstation = new List <MapObject>(); Utility.DoRecursive(PSystemManager.Instance.localBodies[0], cb => cb.orbitingBodies, cb => { trackingstation.Add(PlanetariumCamera.fetch.targets.Find(t => t.celestialBody == cb)); }); PlanetariumCamera.fetch.targets.Clear(); PlanetariumCamera.fetch.targets.AddRange(trackingstation); // Update the initialTarget of the tracking station Resources.FindObjectsOfTypeAll <PlanetariumCamera>().FirstOrDefault().initialTarget = Resources.FindObjectsOfTypeAll <ScaledMovement>().FirstOrDefault(o => o.celestialBody.isHomeWorld); // Undo stuff foreach (CelestialBody b in PSystemManager.Instance.localBodies.Where(b_ => b_.Has("orbitPatches"))) { fixes[b.transform.name].Value.orbitingBodies.Remove(b); fixes[b.transform.name].Key.orbitingBodies.Add(b); fixes[b.transform.name].Key.orbitingBodies = fixes[b.transform.name].Key.orbitingBodies.OrderBy(cb => cb.orbit.semiMajorAxis).ToList(); } #if FALSE // AFG-Ception foreach (CelestialBody body in PSystemManager.Instance.localBodies) { if (body.afg == null) { continue; } foreach (KopernicusStar s in KopernicusStar.Stars) { AtmosphereFromGround afg; if (s != Sun.Instance) { afg = Instantiate(body.afg.gameObject) .GetComponent <AtmosphereFromGround>(); Utility.CopyObjectFields(body.afg, afg, false); afg.transform.parent = body.afg.transform.parent; afg.transform.localPosition = body.afg.transform.localPosition; afg.transform.localScale = body.afg.transform.localScale; afg.transform.localRotation = body.afg.transform.localRotation; afg.gameObject.layer = body.afg.gameObject.layer; } else { afg = body.afg; } afg.gameObject.AddComponent <KopernicusStarAFG>().Star = s; } } #endif }