Esempio n. 1
0
        // 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 !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);
                    }
                }
            }
            #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
        }
Esempio n. 2
0
        // Apply PostSpawnOrbit patches
        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 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")))
            {
                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();
            }
        }