public void Start() { OnStart(); origRadius = Target.Radius; SetupBody(Target); Target.CBUpdate(); if (Target.pqsController != null) { Target.pqsController.radius = Target.Radius; foreach (var child in Target.pqsController.ChildSpheres) { child.radius = Target.Radius; } SetupPQS(Target.pqsController); } foreach (var t in ScaledSpace.Instance.scaledSpaceTransforms) { if (t.name == Target.name) { origScale = t.localScale.x; newScale = (float)(Target.Radius / origRadius); SetupScaled(t.gameObject); } } }
static void WarpPlanet(CelestialBody body, Orbit newOrbit) { var oldBody = body.referenceBody; HardsetOrbit (body.orbit, newOrbit); if (oldBody != newOrbit.referenceBody) { oldBody.orbitingBodies.Remove (body); newOrbit.referenceBody.orbitingBodies.Add (body); } body.CBUpdate (); }
static void WarpPlanet(CelestialBody body, Orbit newOrbit) { var oldBody = body.referenceBody; HardSet(body.orbit, newOrbit); if (oldBody != newOrbit.referenceBody) { oldBody.orbitingBodies.Remove(body); newOrbit.referenceBody.orbitingBodies.Add(body); } body.CBUpdate(); }
// Finalize an Orbit public static void FinalizeOrbit(CelestialBody body) { if (body.orbitDriver != null) { if (body.referenceBody != null) { // Only recalculate the SOI, if it's not forced if (!body.Has("hillSphere")) { body.hillSphere = body.orbit.semiMajorAxis * (1.0 - body.orbit.eccentricity) * Math.Pow(body.Mass / body.orbit.referenceBody.Mass, 1.0 / 3.0); } if (!body.Has("sphereOfInfluence")) { body.sphereOfInfluence = Math.Max( body.orbit.semiMajorAxis * Math.Pow(body.Mass / body.orbit.referenceBody.Mass, 0.4), Math.Max(body.Radius * Templates.SOIMinRadiusMult, body.Radius + Templates.SOIMinAltitude)); } // this is unlike stock KSP, where only the reference body's mass is used. body.orbit.period = 2 * Math.PI * Math.Sqrt(Math.Pow(body.orbit.semiMajorAxis, 2) / 6.674E-11 * body.orbit.semiMajorAxis / (body.referenceBody.Mass + body.Mass)); body.orbit.meanMotion = 2 * Math.PI / body.orbit.period; // in theory this should work but I haven't tested it if (body.orbit.eccentricity <= 1.0) { body.orbit.meanAnomaly = body.orbit.meanAnomalyAtEpoch; body.orbit.orbitPercent = body.orbit.meanAnomalyAtEpoch / (Math.PI * 2); body.orbit.ObTAtEpoch = body.orbit.orbitPercent * body.orbit.period; } else { // ignores this body's own mass for this one... body.orbit.meanAnomaly = body.orbit.meanAnomalyAtEpoch; body.orbit.ObT = Math.Pow(Math.Pow(Math.Abs(body.orbit.semiMajorAxis), 3.0) / body.orbit.referenceBody.gravParameter, 0.5) * body.orbit.meanAnomaly; body.orbit.ObTAtEpoch = body.orbit.ObT; } } else { body.sphereOfInfluence = Double.PositiveInfinity; body.hillSphere = Double.PositiveInfinity; } } try { body.CBUpdate(); } catch (Exception e) { UnityEngine.Debug.Log("CBUpdate for " + body.name + " failed: " + e.Message); } }
public void OnPSystemReady(RootDefinition Root, CelestialBody OriginalSun, Transform ScaledSun) { Debug.Log("Altering sun..."); //Set Original Sun Parameters double SolarMasses; SolarMasses = Root.SolarMasses; OriginalSun.Mass = SolarMasses * OriginalSun.Mass; OriginalSun.Radius = (2 * (6.74E-11) * OriginalSun.Mass) / (Math.Pow(299792458, 2.0)); OriginalSun.GeeASL = OriginalSun.Mass * (6.674E-11 / 9.81) / Math.Pow(OriginalSun.Radius, 2.0); OriginalSun.gMagnitudeAtCenter = OriginalSun.GeeASL * 9.81 * Math.Pow(OriginalSun.Radius, 2.0); OriginalSun.gravParameter = OriginalSun.gMagnitudeAtCenter; OriginalSun.scienceValues.InSpaceLowDataValue = OriginalSun.scienceValues.InSpaceLowDataValue * 10f; OriginalSun.scienceValues.RecoveryValue = OriginalSun.scienceValues.RecoveryValue * 5f; OriginalSun.bodyName = "Blacky Karman"; OriginalSun.bodyDescription = "This recently discovered black hole, named after its discoverer Billy-Hadrick Kerman, is the central point where multiple star systems revolve around."; OriginalSun.CBUpdate(); //Make Sun Black ScaledSun.renderer.material.SetColor("_EmitColor0", new Color(0.0f, 0.0f, 0.0f, 1)); ScaledSun.renderer.material.SetColor("_EmitColor1", new Color(0.0f, 0.0f, 0.0f, 1)); ScaledSun.renderer.material.SetColor("_SunspotColor", new Color(0.0f, 0.0f, 0.0f, 1)); ScaledSun.renderer.material.SetColor("_RimColor", new Color(0.0f, 0.0f, 0.0f, 1.0f)); //Update Sun Scale var ScaledSunMeshFilter = (MeshFilter)ScaledSun.GetComponent(typeof(MeshFilter)); var SunRatio = (float)OriginalSun.Radius / 261600000f; MeshScaler.ScaleMesh(ScaledSunMeshFilter.mesh, SunRatio); //Change Sun Corona foreach (var SunCorona in ScaledSun.GetComponentsInChildren <SunCoronas>()) { SunCorona.renderer.material.mainTexture = GameDatabase.Instance.GetTexture("StarSystems/Resources/BlackHoleCorona", false); var SunCoronaMeshFilter = (MeshFilter)SunCorona.GetComponent(typeof(MeshFilter)); MeshScaler.ScaleMesh(SunCoronaMeshFilter.mesh, SunRatio); } Debug.Log("Sun altered"); }
public static void LoadCB(CelestialBody body) { var root = ConfigNode.Load(DataPath + body.bodyName + ".cfg"); if (root != null) { var cbConfig = root.nodes.GetNode("CelestialBody"); if (cbConfig != null) { print("loading CB config:" + body.bodyName); LoadConfiguration(body, cbConfig); body.CBUpdate(); } } }
public void OnPSystemReady(RootDefinition Root, CelestialBody OriginalSun, Transform ScaledSun) { Debug.Log("Altering sun..."); //Set Original Sun Parameters double SolarMasses; SolarMasses = Root.SolarMasses; OriginalSun.Mass = SolarMasses * OriginalSun.Mass; OriginalSun.Radius = (2 * (6.74E-11) * OriginalSun.Mass) / (Math.Pow(299792458, 2.0)); OriginalSun.GeeASL = OriginalSun.Mass * (6.674E-11 / 9.81) / Math.Pow(OriginalSun.Radius, 2.0); OriginalSun.gMagnitudeAtCenter = OriginalSun.GeeASL * 9.81 * Math.Pow(OriginalSun.Radius, 2.0); OriginalSun.gravParameter = OriginalSun.gMagnitudeAtCenter; OriginalSun.scienceValues.InSpaceLowDataValue = OriginalSun.scienceValues.InSpaceLowDataValue * 10f; OriginalSun.scienceValues.RecoveryValue = OriginalSun.scienceValues.RecoveryValue * 5f; OriginalSun.bodyName = "Blacky Karman"; OriginalSun.bodyDescription = "This recently discovered black hole, named after its discoverer Billy-Hadrick Kerman, is the central point where multiple star systems revolve around."; OriginalSun.CBUpdate(); //Make Sun Black ScaledSun.renderer.material.SetColor("_EmitColor0", new Color(0.0f, 0.0f, 0.0f, 1)); ScaledSun.renderer.material.SetColor("_EmitColor1", new Color(0.0f, 0.0f, 0.0f, 1)); ScaledSun.renderer.material.SetColor("_SunspotColor", new Color(0.0f, 0.0f, 0.0f, 1)); ScaledSun.renderer.material.SetColor("_RimColor", new Color(0.0f, 0.0f, 0.0f, 1.0f)); //Update Sun Scale var ScaledSunMeshFilter = (MeshFilter)ScaledSun.GetComponent(typeof(MeshFilter)); var SunRatio = (float)OriginalSun.Radius / 261600000f; MeshScaler.ScaleMesh(ScaledSunMeshFilter.mesh, SunRatio); //Change Sun Corona foreach (var SunCorona in ScaledSun.GetComponentsInChildren<SunCoronas>()) { SunCorona.renderer.material.mainTexture = GameDatabase.Instance.GetTexture("StarSystems/Resources/BlackHoleCorona", false); var SunCoronaMeshFilter = (MeshFilter)SunCorona.GetComponent(typeof(MeshFilter)); MeshScaler.ScaleMesh(SunCoronaMeshFilter.mesh, SunRatio); } Debug.Log("Sun altered"); }
/// <summary> /// Changes the parameters of the body /// </summary> public override Boolean Tweak(CelestialBody body) { // Does the body have an orbit? if (body.orbit == null) { return(false); } // Prefab PSystemBody pSystemBody = Resources.FindObjectsOfTypeAll <PSystemBody>().FirstOrDefault(b => b.celestialBody.bodyName == body.transform.name); if (pSystemBody == null) { return(false); } // Inclination if (GetRandom(HighLogic.CurrentGame.Seed, 0, 100) < 20) { body.orbitDriver.orbit.inclination = GetRandomDouble(HighLogic.CurrentGame.Seed, -3, 3); } // SMA body.orbitDriver.orbit.semiMajorAxis = pSystemBody.orbitDriver.orbit.semiMajorAxis * GetRandomDouble(HighLogic.CurrentGame.Seed, 0.8, 1.2); // LAN body.orbitDriver.orbit.LAN = GetRandom(HighLogic.CurrentGame.Seed, 0, 361); // MeanAnomalyAtEpoch body.orbitDriver.orbit.meanAnomalyAtEpoch = GetRandom(HighLogic.CurrentGame.Seed, 0, 361) * Math.PI / 180d; // argumentOfPeriapsis body.orbitDriver.orbit.argumentOfPeriapsis = GetRandom(HighLogic.CurrentGame.Seed, 0, 361); // Eccentricy body.orbitDriver.orbit.eccentricity = pSystemBody.orbitDriver.orbit.eccentricity * GetRandomDouble(HighLogic.CurrentGame.Seed, 0.8, 1.2); // Update body.orbitDriver.UpdateOrbit(); body.CBUpdate(); // Done return(true); }
public void OnPSystemReady(RootDefinition Root, CelestialBody OriginalSun, Transform ScaledSun) { Debug.Log("Altering sun..."); //Set Original Sun Parameters OriginalSun.Mass = Root.SolarMasses * OriginalSun.Mass; OriginalSun.Radius = (2 * (6.674E-11) * OriginalSun.Mass) / (Math.Pow(299792458, 2.0)); OriginalSun.GeeASL = OriginalSun.Mass * (6.674E-11 / 9.81) / Math.Pow(OriginalSun.Radius, 2.0); OriginalSun.gMagnitudeAtCenter = OriginalSun.GeeASL * 9.81 * Math.Pow(OriginalSun.Radius, 2.0); OriginalSun.gravParameter = OriginalSun.gMagnitudeAtCenter; OriginalSun.scienceValues.InSpaceLowDataValue = OriginalSun.scienceValues.InSpaceLowDataValue * 10f; OriginalSun.scienceValues.RecoveryValue = OriginalSun.scienceValues.RecoveryValue * 5f; OriginalSun.bodyName = Root.name; OriginalSun.bodyDescription = Root.description; OriginalSun.CBUpdate(); //Make Sun Black ScaledSun.renderer.material.SetColor("_EmitColor0", Root.color.emitColor0); ScaledSun.renderer.material.SetColor("_EmitColor1", Root.color.emitColor1); ScaledSun.renderer.material.SetColor("_SunspotColor", Root.color.sunSpotColor); ScaledSun.renderer.material.SetColor("_RimColor", Root.color.rimColor); //Update Sun Scale var ScaledSunMeshFilter = (MeshFilter)ScaledSun.GetComponent(typeof(MeshFilter)); var SunRatio = (float)OriginalSun.Radius / 261600000f; MeshScaler.ScaleMesh(ScaledSunMeshFilter.mesh, SunRatio); //Change Sun Corona foreach (var SunCorona in ScaledSun.GetComponentsInChildren <SunCoronas>()) { SunCorona.renderer.material.mainTexture = Root.color.coronaTexture; var SunCoronaMeshFilter = (MeshFilter)SunCorona.GetComponent(typeof(MeshFilter)); MeshScaler.ScaleMesh(SunCoronaMeshFilter.mesh, SunRatio); } Debug.Log("Sun altered"); }
public static void RealCbUpdate(this CelestialBody body) { body.CBUpdate(); try { body.resetTimeWarpLimits(); } catch (NullReferenceException) { Log("resetTimeWarpLimits threw NRE " + (TimeWarp.fetch == null ? "as expected" : "unexpectedly")); } // CBUpdate doesn't update hillSphere // http://en.wikipedia.org/wiki/Hill_sphere var orbit = body.orbit; var cubedRoot = Math.Pow(body.Mass / orbit.referenceBody.Mass, 1.0 / 3.0); body.hillSphere = orbit.semiMajorAxis * (1.0 - orbit.eccentricity) * cubedRoot; // Nor sphereOfInfluence // http://en.wikipedia.org/wiki/Sphere_of_influence_(astrodynamics) body.sphereOfInfluence = orbit.semiMajorAxis * Math.Pow(body.Mass / orbit.referenceBody.Mass, 2.0 / 5.0); }
private IEnumerator<YieldInstruction> LoadCB(ConfigNode node, CelestialBody body) { bool updateMass = false; //OnGui(); #region CBChanges print("Fixing CB " + node.name + " of radius " + body.Radius); guiMinor = "CelestialBody"; //OnGui(); double origRadius = body.Radius; double origAtmo = body.maxAtmosphereAltitude; #region CBMassRadius node.TryGetValue("bodyName", ref body.bodyName); node.TryGetValue("bodyDescription", ref body.bodyDescription); if (node.TryGetValue("Radius", ref body.Radius)) updateMass = true; print("Radius ratio: " + body.Radius / origRadius); if (node.TryGetValue("Mass", ref body.Mass)) { MassToOthers(body); } if (node.TryGetValue("GeeASL", ref body.GeeASL)) { GeeASLToOthers(body); updateMass = false; } if (node.TryGetValue("gravParameter", ref body.gravParameter)) { GravParamToOthers(body); updateMass = false; } #endregion #region CBAtmosphereTemperature node.TryGetValue("atmosphericAmbientColor", ref body.atmosphericAmbientColor); node.TryGetValue("atmosphere", ref body.atmosphere); node.TryGetValue("atmosphereScaleHeight", ref body.atmosphereScaleHeight); node.TryGetValue("atmosphereMultiplier", ref body.atmosphereMultiplier); node.TryGetValue("maxAtmosphereAltitude", ref body.maxAtmosphereAltitude); node.TryGetValue("staticPressureASL", ref body.staticPressureASL); node.TryGetValue("useLegacyAtmosphere", ref body.useLegacyAtmosphere); if (!body.useLegacyAtmosphere) { ConfigNode PCnode = node.GetNode("pressureCurve"); if (PCnode != null) { body.altitudeMultiplier = 1f; body.pressureMultiplier = 1f; AnimationCurve pressureCurve = Utils.LoadAnimationCurve(PCnode); if (pressureCurve != null) body.pressureCurve = pressureCurve; else { body.useLegacyAtmosphere = true; Debug.LogWarning("Unable to load pressureCurve data for " + body.name + ": Using legacy atmosphere"); } print("*RSS* finished with " + body.GetName() + ".pressureCurve (" + body.pressureCurve.keys.Length.ToString() + " keys)"); } else { print("*RSS* useLegacyAtmosphere = False but pressureCurve not found!"); } } if (node.HasNode("temperatureCurve")) { ConfigNode TCnode = node.GetNode("temperatureCurve"); if (TCnode != null) { AnimationCurve temperatureCurve = Utils.LoadAnimationCurve(TCnode); if (temperatureCurve != null) { body.temperatureCurve = temperatureCurve; // Following two lines corrects situations where planets without atmosphere's have these two fields zeroed out // Maybe think about making these configurable in the planet's config node? yes? no? meh? body.atmoshpereTemperatureMultiplier = 1f; body.altitudeMultiplier = 1f; print("*RSS* found and loaded temperatureCurve data for " + body.name); } } } #endregion #region CBRotation node.TryGetValue("rotationPeriod", ref body.rotationPeriod); node.TryGetValue("tidallyLocked", ref body.tidallyLocked); node.TryGetValue("initialRotation", ref body.initialRotation); node.TryGetValue("inverseRotation", ref body.inverseRotation); #endregion if (updateMass) GeeASLToOthers(body); /*if (node.HasValue("axialTilt")) { if (!body.inverseRotation && double.TryParse(node.GetValue("axialTilt"), out dtmp)) { CBRotationFixer.CBRotations.Add(body.name, new CBRotation(body.name, dtmp, body.rotationPeriod, body.initialRotation)); body.rotationPeriod = 0; } }*/ yield return null; #region CBOrbit ConfigNode onode = node.GetNode("Orbit"); if (body.orbitDriver != null && body.orbit != null && onode != null) { if (loadInfo.useEpoch) body.orbit.epoch = loadInfo.epoch; onode.TryGetValue("semiMajorAxis", ref body.orbit.semiMajorAxis); onode.TryGetValue("eccentricity", ref body.orbit.eccentricity); onode.TryGetValue("meanAnomalyAtEpoch", ref body.orbit.meanAnomalyAtEpoch); if (onode.TryGetValue("meanAnomalyAtEpochD", ref body.orbit.meanAnomalyAtEpoch)) body.orbit.meanAnomalyAtEpoch *= DEG2RAD; onode.TryGetValue("inclination", ref body.orbit.inclination); onode.TryGetValue("period", ref body.orbit.period); onode.TryGetValue("LAN", ref body.orbit.LAN); onode.TryGetValue("argumentOfPeriapsis", ref body.orbit.argumentOfPeriapsis); if (onode.HasValue("orbitColor")) { try { Vector4 col = KSPUtil.ParseVector4(onode.GetValue("orbitColor")); Color c = new Color(col.x, col.y, col.z, col.w); body.GetOrbitDriver().orbitColor = c; } catch (Exception e) { print("*RSS* Error parsing as color4: original text: " + onode.GetValue("orbitColor") + " --- exception " + e.Message); } } string bodyname = ""; if (onode.TryGetValue("referenceBody", ref bodyname)) { if (body.orbit.referenceBody == null || !body.orbit.referenceBody.Equals(bodyname)) { foreach (CelestialBody b in FlightGlobals.Bodies) { if (b.name.Equals(bodyname)) { if (body.orbit.referenceBody) { body.orbit.referenceBody.orbitingBodies.Remove(body); } b.orbitingBodies.Add(body); body.orbit.referenceBody = b; break; } } } } } yield return null; // SOI and HillSphere done at end body.CBUpdate(); #endregion #endregion #region SSPQSFade // Scaled space fader float SSFMult = 1.0f; float SSFStart = -1, SSFEnd = -1; node.TryGetValue("SSFStart", ref SSFStart); node.TryGetValue("SSFEnd", ref SSFEnd); node.TryGetValue("SSFMult", ref SSFMult); foreach (ScaledSpaceFader ssf in Resources.FindObjectsOfTypeAll(typeof(ScaledSpaceFader))) { if (ssf.celestialBody != null) { if (ssf.celestialBody.name.Equals(node.name)) { if (SSFStart >= 0) ssf.fadeStart = SSFStart; else ssf.fadeStart *= SSFMult; if (SSFEnd >= 0) ssf.fadeEnd = SSFEnd; else ssf.fadeEnd *= SSFMult; } } } // The CBT that fades out the PQS // Should probably do this as just another PQSMod, actually. foreach (PQSMod_CelestialBodyTransform c in Resources.FindObjectsOfTypeAll(typeof(PQSMod_CelestialBodyTransform))) { try { if (c.body != null) { if (c.body.name.Equals(node.name)) { print("Found CBT for " + node.name); node.TryGetValue("PQSdeactivateAltitude", ref c.deactivateAltitude); if (c.planetFade != null) { node.TryGetValue("PQSfadeStart", ref c.planetFade.fadeStart); node.TryGetValue("PQSfadeEnd", ref c.planetFade.fadeEnd); if (c.secondaryFades != null) { foreach (PQSMod_CelestialBodyTransform.AltitudeFade af in c.secondaryFades) { node.TryGetValue("PQSSecfadeStart", ref af.fadeStart); node.TryGetValue("PQSSecfadeEnd", ref af.fadeEnd); } } } } } } catch (Exception e) { print("CBT fix for " + node.name + " failed: " + e.Message); } } print("Did CBT for " + node.name); yield return null; #endregion #region Science // Science if (node.HasNode("CelestialBodyScienceParams")) { guiMinor = "Science"; //OnGui(); ConfigNode spNode = node.GetNode("CelestialBodyScienceParams"); if (body.scienceValues != null) { foreach (ConfigNode.Value val in spNode.values) { // meh, for now hard-code it. Saves worry of GIGO. /*if(body.scienceValues.GetType().GetField(val.name) != null) if(float.TryParse(val.value, out ftmp)) body.scienceValues.GetType().GetField(val.name).SetValue(*/ spNode.TryGetValue("LandedDataValue", ref body.scienceValues.LandedDataValue); spNode.TryGetValue("SplashedDataValue", ref body.scienceValues.SplashedDataValue); spNode.TryGetValue("FlyingLowDataValue", ref body.scienceValues.FlyingLowDataValue); spNode.TryGetValue("FlyingHighDataValue", ref body.scienceValues.FlyingHighDataValue); spNode.TryGetValue("InSpaceLowDataValue", ref body.scienceValues.InSpaceLowDataValue); spNode.TryGetValue("InSpaceHighDataValue", ref body.scienceValues.InSpaceHighDataValue); spNode.TryGetValue("RecoveryValue", ref body.scienceValues.RecoveryValue); spNode.TryGetValue("flyingAltitudeThreshold", ref body.scienceValues.flyingAltitudeThreshold); spNode.TryGetValue("spaceAltitudeThreshold", ref body.scienceValues.spaceAltitudeThreshold); } } guiMinor = ""; //OnGui(); } yield return null; #endregion }
public bool PatchBody(CelestialBody body, ConfigNode node) { if ((object)body == null) { return(false); } double dtmp; bool btmp; bool updateMass = false; double origRadius = body.Radius; if (node != null) { print("Patching body " + body.bodyName + " from node."); if (node.HasValue("rotationPeriod")) { if (double.TryParse(node.GetValue("rotationPeriod"), out dtmp)) { body.rotationPeriod = dtmp; } } if (node.HasValue("atmosphereDepth")) { if (double.TryParse(node.GetValue("atmosphereDepth"), out dtmp)) { body.atmosphereDepth = dtmp; } } if (node.HasValue("atmosphereAdiabaticIndex")) { if (double.TryParse(node.GetValue("atmosphereAdiabaticIndex"), out dtmp)) { body.atmosphereAdiabaticIndex = dtmp; } } if (node.HasValue("atmosphereMolarMass")) { if (double.TryParse(node.GetValue("atmosphereMolarMass"), out dtmp)) { body.atmosphereMolarMass = dtmp; } } if (node.HasValue("atmospherePressureSeaLevel")) { if (double.TryParse(node.GetValue("atmospherePressureSeaLevel"), out dtmp)) { body.atmospherePressureSeaLevel = dtmp; } } if (node.HasValue("atmosphereTemperatureSeaLevel")) { if (double.TryParse(node.GetValue("atmosphereTemperatureSeaLevel"), out dtmp)) { body.atmosphereTemperatureSeaLevel = dtmp; } } if (node.HasNode("atmospherePressureCurve")) { body.atmosphereUsePressureCurve = true; body.atmospherePressureCurve.Load(node.GetNode("atmospherePressureCurve")); } else if (node.HasValue("atmosphereUsePressureCurve")) { if (bool.TryParse(node.GetValue("atmosphereUsePressureCurve"), out btmp)) { body.atmosphereUsePressureCurve = btmp; } } if (node.HasNode("atmosphereTemperatureCurve")) { body.atmosphereUseTemperatureCurve = true; body.atmosphereTemperatureCurve.Load(node.GetNode("atmosphereTemperatureCurve")); } else if (node.HasValue("atmosphereUseTemperatureCurve")) { if (bool.TryParse(node.GetValue("atmosphereUseTemperatureCurve"), out btmp)) { body.atmosphereUseTemperatureCurve = btmp; } } if (node.HasNode("latitudeTemperatureBiasCurve")) { body.latitudeTemperatureBiasCurve.Load(node.GetNode("latitudeTemperatureBiasCurve")); } if (node.HasNode("latitudeTemperatureSunMultCurve")) { body.latitudeTemperatureSunMultCurve.Load(node.GetNode("latitudeTemperatureSunMultCurve")); } if (node.HasNode("axialTemperatureSunMultCurve")) { body.axialTemperatureSunMultCurve.Load(node.GetNode("axialTemperatureSunMultCurve")); } if (node.HasNode("atmosphereTemperatureSunMultCurve")) { body.atmosphereTemperatureSunMultCurve.Load(node.GetNode("atmosphereTemperatureSunMultCurve")); } if (node.HasNode("axialTemperatureSunBiasCurve")) { body.axialTemperatureSunBiasCurve.Load(node.GetNode("axialTemperatureSunBiasCurve")); } if (node.HasNode("axialTemperatureSunMultCurve")) { body.axialTemperatureSunMultCurve.Load(node.GetNode("axialTemperatureSunMultCurve")); } if (node.HasNode("eccentricityTemperatureBiasCurve")) { body.eccentricityTemperatureBiasCurve.Load(node.GetNode("eccentricityTemperatureBiasCurve")); } if (node.HasValue("Radius")) { if (double.TryParse(node.GetValue("Radius"), out dtmp)) { body.Radius = dtmp; updateMass = true; } } // Orbit ConfigNode onode = node.GetNode("Orbit"); if (body.orbitDriver != null && body.orbit != null && onode != null) { patchOrbits = true; if (node.HasValue("semiMajorAxis")) { if (double.TryParse(node.GetValue("semiMajorAxis"), out dtmp)) { body.orbit.semiMajorAxis = dtmp; } } if (node.HasValue("eccentricity")) { if (double.TryParse(node.GetValue("eccentricity"), out dtmp)) { body.orbit.eccentricity = dtmp; } } if (node.HasValue("meanAnomalyAtEpoch")) { if (double.TryParse(node.GetValue("meanAnomalyAtEpoch"), out dtmp)) { body.orbit.meanAnomalyAtEpoch = dtmp; } } if (node.HasValue("meanAnomalyAtEpochD")) { if (double.TryParse(node.GetValue("meanAnomalyAtEpochD"), out dtmp)) { body.orbit.meanAnomalyAtEpoch = dtmp; body.orbit.meanAnomalyAtEpoch *= DEG2RAD; } } if (node.HasValue("inclination")) { if (double.TryParse(node.GetValue("inclination"), out dtmp)) { body.orbit.inclination = dtmp; } } if (node.HasValue("LAN")) { if (double.TryParse(node.GetValue("LAN"), out dtmp)) { body.orbit.LAN = dtmp; } } if (node.HasValue("argumentOfPeriapsis")) { if (double.TryParse(node.GetValue("argumentOfPeriapsis"), out dtmp)) { body.orbit.argumentOfPeriapsis = dtmp; } } } } else if (globalRotationRescale != 1d || globalRescale != 1d) { print("Patching body " + body.bodyName + " from globals."); } body.rotationPeriod *= globalRotationRescale; if (globalRescale != 1d) { body.Radius *= globalRescale; updateMass = true; } if (updateMass) { patchOrbits = true; GeeASLToOthers(body); } if (globalRescaleAtmo != 1d && body.atmosphere) { body.atmospherePressureCurve = RescaleCurve(body.atmospherePressureCurve, globalRescaleAtmo); body.atmosphereTemperatureCurve = RescaleCurve(body.atmosphereTemperatureCurve, globalRescaleAtmo); body.atmosphereTemperatureSunMultCurve = RescaleCurve(body.atmosphereTemperatureSunMultCurve, globalRescaleAtmo); body.atmosphereDepth *= globalRescaleAtmo; } body.SetupConstants(); // Fix up PQS if (PatchPQS(body, node, origRadius) || origRadius != body.Radius) { StartCoroutine(PatchScaledSpace(body, node, origRadius)); } body.CBUpdate(); return(true); }
private void Spawn() { CelestialBody.CBUpdate(); isSpawned = true; mesh = ModelVariant.SpawnVariant(this); { if (_mesh == null) { Log.UserError("Cannot spawn 3dModel of Instance: " + model.name); Destroy(); return; } } if (model.isSquad) { InstanceUtil.MangleSquadStatic(this); } InstanceUtil.SetLayerRecursively(this, 15); mesh.SetActive(true); //Scaling origScale = gameObject.transform.localScale; // save the original scale for later use gameObject.transform.localScale *= ModelScale; foreach (StaticModule module in model.modules) { moduleKey = (module.moduleNamespace + "_" + module.moduleClassname); Type moduleType; if (staticModules.ContainsKey(moduleKey)) { moduleType = staticModules[moduleKey]; } else { moduleType = AssemblyLoader.loadedAssemblies.SelectMany(asm => asm.assembly.GetTypes()).FirstOrDefault(t => t.Namespace == module.moduleNamespace && t.Name == module.moduleClassname); staticModules.Add(moduleKey, moduleType); } StaticModule mod = mesh.AddComponent(moduleType) as StaticModule; if (mod != null) { mod.enabled = false; mod.staticInstance = this; foreach (string fieldName in module.moduleFields.Keys) { FieldInfo field = mod.GetType().GetField(fieldName); if (field != null) { field.SetValue(mod, Convert.ChangeType(module.moduleFields[fieldName], field.FieldType)); } else { Log.UserWarning("Field " + fieldName + " does not exist in " + module.moduleClassname); } } //myStaticModules.Add(mod); } else { Log.UserError("Module " + module.moduleClassname + " could not be loaded in " + gameObject.name); } } foreach (Renderer renderer in gameObject.GetComponentsInChildren <Renderer>(true)) { renderer.enabled = true; AdvancedTextures.CheckForExistingMaterial(renderer); } ModelVariant.ApplyVariant(this); //Make LaunchSites more sturdy if (!model.isSquad) { Destructable.MakeDestructable(this); if (hasLauchSites) { destructible.impactMomentumThreshold = Math.Max(destructible.impactMomentumThreshold, 3000f); launchSite.AttachSelector(); } } foreach (var facility in myFacilities) { facility.AttachSelector(); } }
internal void Spawn() { if (StaticDatabase.HasGroupCenter(dbKey)) { string oldName = Group; int index = 0; while (StaticDatabase.HasGroupCenter(dbKey)) { Group = oldName + "_" + index.ToString(); index++; } } gameObject = new GameObject(); GameObject.DontDestroyOnLoad(gameObject); CelestialBody.CBUpdate(); gameObject.name = Group; //gameObject.name = "SpaceCenter"; pqsCity = gameObject.AddComponent <PQSCity>(); PQSCity.LODRange range = new PQSCity.LODRange { renderers = new GameObject[0], objects = new GameObject[0], visibleRange = 25000 }; pqsCity.lod = new[] { range }; pqsCity.frameDelta = 10000; //update interval for its own visiblility range checking. unused by KK, so set this to a high value if (RadialPosition == Vector3.zero) { if ((RefLatitude != 361d) && (RefLongitude != 361d)) { RadialPosition = KKMath.GetRadiadFromLatLng(CelestialBody, RefLatitude, RefLongitude); } else { Log.UserError("No Valid Position found for Group: " + Group); } } else if ((RefLatitude == 361d) || (RefLongitude == 361d)) { { RefLatitude = KKMath.GetLatitudeInDeg(RadialPosition); RefLongitude = KKMath.GetLongitudeInDeg(RadialPosition); } } pqsCity.reorientFinalAngle = 0; pqsCity.repositionRadial = RadialPosition; //position pqsCity.repositionRadiusOffset = RadiusOffset; //height pqsCity.reorientInitialUp = Orientation; //orientation pqsCity.reorientToSphere = true; //adjust rotations to match the direction of gravity pqsCity.sphere = CelestialBody.pqsController; pqsCity.order = 100; pqsCity.modEnabled = true; pqsCity.transform.parent = CelestialBody.pqsController.transform; pqsCity.repositionToSphereSurfaceAddHeight = false; pqsCity.repositionToSphereSurface = false; SetReference(); pqsCity.OnSetup(); pqsCity.Orientate(); UpdateRotation2Heading(); StaticDatabase.AddGroupCenter(this); }
// Finalize an Orbit public static void FinalizeOrbit(CelestialBody body) { if (body.orbitDriver != null) { if (body.referenceBody != null) { // Only recalculate the SOI, if it's not forced if (!Templates.hillSphere.ContainsKey(body.transform.name)) body.hillSphere = body.orbit.semiMajorAxis * (1.0 - body.orbit.eccentricity) * Math.Pow(body.Mass / body.orbit.referenceBody.Mass, 1.0 / 3.0); if (!Templates.sphereOfInfluence.ContainsKey(body.transform.name)) body.sphereOfInfluence = Math.Max( body.orbit.semiMajorAxis * Math.Pow(body.Mass / body.orbit.referenceBody.Mass, 0.4), Math.Max(body.Radius * Templates.SOIMinRadiusMult, body.Radius + Templates.SOIMinAltitude)); // this is unlike stock KSP, where only the reference body's mass is used. body.orbit.period = 2 * Math.PI * Math.Sqrt(Math.Pow(body.orbit.semiMajorAxis, 2) / 6.674E-11 * body.orbit.semiMajorAxis / (body.referenceBody.Mass + body.Mass)); body.orbit.meanMotion = 2 * Math.PI / body.orbit.period; // in theory this should work but I haven't tested it if (body.orbit.eccentricity <= 1.0) { body.orbit.meanAnomaly = body.orbit.meanAnomalyAtEpoch; body.orbit.orbitPercent = body.orbit.meanAnomalyAtEpoch / (Math.PI * 2); body.orbit.ObTAtEpoch = body.orbit.orbitPercent * body.orbit.period; } else { // ignores this body's own mass for this one... body.orbit.meanAnomaly = body.orbit.meanAnomalyAtEpoch; body.orbit.ObT = Math.Pow(Math.Pow(Math.Abs(body.orbit.semiMajorAxis), 3.0) / body.orbit.referenceBody.gravParameter, 0.5) * body.orbit.meanAnomaly; body.orbit.ObTAtEpoch = body.orbit.ObT; } } else { body.sphereOfInfluence = Double.PositiveInfinity; body.hillSphere = Double.PositiveInfinity; } } try { body.CBUpdate(); } catch (Exception e) { UnityEngine.Debug.Log("CBUpdate for " + body.name + " failed: " + e.Message); } }
private void UpdateBody(CelestialBody body, double universal_time) { plugin_.UpdateCelestialHierarchy( body.flightGlobalsIndex, body.orbit.referenceBody.flightGlobalsIndex); QP from_parent = plugin_.CelestialFromParent(body.flightGlobalsIndex); // TODO(egg): Some of this might be be superfluous and redundant. Orbit original = body.orbit; Orbit copy = new Orbit(original.inclination, original.eccentricity, original.semiMajorAxis, original.LAN, original.argumentOfPeriapsis, original.meanAnomalyAtEpoch, original.epoch, original.referenceBody); copy.UpdateFromStateVectors((Vector3d)from_parent.q, (Vector3d)from_parent.p, copy.referenceBody, universal_time); body.orbit.inclination = copy.inclination; body.orbit.eccentricity = copy.eccentricity; body.orbit.semiMajorAxis = copy.semiMajorAxis; body.orbit.LAN = copy.LAN; body.orbit.argumentOfPeriapsis = copy.argumentOfPeriapsis; body.orbit.meanAnomalyAtEpoch = copy.meanAnomalyAtEpoch; body.orbit.epoch = copy.epoch; body.orbit.referenceBody = copy.referenceBody; body.orbit.Init(); body.orbit.UpdateFromUT(universal_time); body.CBUpdate(); body.orbit.UpdateFromStateVectors((Vector3d)from_parent.q, (Vector3d)from_parent.p, copy.referenceBody, universal_time); }
private void RebuildSystemDef() { print("Rebuilding System Default"); CelestialBody sun = GetSun(); sun.orbitingBodies.Clear(); foreach (ChangedPlanet planet in PlanetDefault.Instance.Planets) { CelestialBody target = (from c in FlightGlobals.Bodies where c.gameObject.name == planet.Name select c).FirstOrDefault(); if (target != null) { print("Changing " + planet.ToString()); target.orbitingBodies.Clear(); double origRadius = target.Radius; target.Radius = planet.Radius; if (target.pqsController != null) { target.pqsController.radius = planet.Radius; } foreach (Transform t in ScaledSpace.Instance.scaledSpaceTransforms) { if (t.gameObject.name == target.gameObject.name) { float origLocalScale = t.localScale.x; float scaleFactor = (float)((double)origLocalScale * planet.Radius / origRadius); t.localScale = new Vector3(scaleFactor, scaleFactor, scaleFactor); } } target.Mass = planet.Mass; target.GeeASL = target.Mass * (6.674E-11 / 9.81) / (target.Radius * target.Radius); target.gMagnitudeAtCenter = target.Mass * 6.674E-11; target.gravParameter = target.gMagnitudeAtCenter; target.tidallyLocked = false; target.rotationPeriod = planet.RotationPeriod; target.orbit.semiMajorAxis = planet.SemiMajorAxis; target.orbit.eccentricity = planet.Eccentricity; CelestialBody targetref = (from cRef in FlightGlobals.Bodies where cRef.gameObject.name == planet.ReferenceBody select cRef).FirstOrDefault(); targetref.orbitingBodies.Add(target); target.orbit.referenceBody = targetref; target.orbit.inclination = planet.Inclination; target.orbit.meanAnomalyAtEpoch = planet.MeanAnomalyAtEpoch; target.orbit.LAN = planet.LAN; target.orbit.argumentOfPeriapsis = planet.ArgumentOfPeriapsis; target.orbitDriver.QueuedUpdate = true; target.CBUpdate(); target.sphereOfInfluence = GetSOI(target); target.hillSphere = GetHillSphere(target); target.orbit.period = GetPeriod(target); } } foreach (CelestialBody body in FlightGlobals.fetch.bodies) { foreach (AtmosphereFromGround ag in Resources.FindObjectsOfTypeAll(typeof(AtmosphereFromGround))) { if (ag != null && ag.planet != null) { // generalized version of Starwaster's code. Thanks Starwaster! if (body.name == ag.planet.name) { print("Found atmo for " + body.name + ": " + ag.name + ", has localScale " + ag.transform.localScale.x); UpdateAFG(body, ag); print("Atmo updated"); } } } } print("Done changing planets"); }
public bool PatchBody(CelestialBody body, ConfigNode node) { if((object)body == null) return false; double dtmp; bool btmp; bool updateMass = false; double origRadius = body.Radius; if (node != null) { print("Patching body " + body.bodyName + " from node."); if (node.HasValue("rotationPeriod")) if (double.TryParse(node.GetValue("rotationPeriod"), out dtmp)) body.rotationPeriod = dtmp; if (node.HasValue("atmosphereDepth")) if (double.TryParse(node.GetValue("atmosphereDepth"), out dtmp)) body.atmosphereDepth = dtmp; if (node.HasValue("atmosphereAdiabaticIndex")) if (double.TryParse(node.GetValue("atmosphereAdiabaticIndex"), out dtmp)) body.atmosphereAdiabaticIndex = dtmp; if (node.HasValue("atmosphereMolarMass")) if (double.TryParse(node.GetValue("atmosphereMolarMass"), out dtmp)) body.atmosphereMolarMass = dtmp; if (node.HasValue("atmospherePressureSeaLevel")) if (double.TryParse(node.GetValue("atmospherePressureSeaLevel"), out dtmp)) body.atmospherePressureSeaLevel = dtmp; if (node.HasValue("atmosphereTemperatureSeaLevel")) if (double.TryParse(node.GetValue("atmosphereTemperatureSeaLevel"), out dtmp)) body.atmosphereTemperatureSeaLevel = dtmp; if (node.HasNode("atmospherePressureCurve")) { body.atmosphereUsePressureCurve = true; body.atmospherePressureCurve.Load(node.GetNode("atmospherePressureCurve")); } else if (node.HasValue("atmosphereUsePressureCurve")) { if (bool.TryParse(node.GetValue("atmosphereUsePressureCurve"), out btmp)) body.atmosphereUsePressureCurve = btmp; } if (node.HasNode("atmosphereTemperatureCurve")) { body.atmosphereUseTemperatureCurve = true; body.atmosphereTemperatureCurve.Load(node.GetNode("atmosphereTemperatureCurve")); } else if (node.HasValue("atmosphereUseTemperatureCurve")) { if (bool.TryParse(node.GetValue("atmosphereUseTemperatureCurve"), out btmp)) body.atmosphereUseTemperatureCurve = btmp; } if (node.HasNode("latitudeTemperatureBiasCurve")) { body.latitudeTemperatureBiasCurve.Load(node.GetNode("latitudeTemperatureBiasCurve")); } if (node.HasNode("latitudeTemperatureSunMultCurve")) { body.latitudeTemperatureSunMultCurve.Load(node.GetNode("latitudeTemperatureSunMultCurve")); } if (node.HasNode("axialTemperatureSunMultCurve")) { body.axialTemperatureSunMultCurve.Load(node.GetNode("axialTemperatureSunMultCurve")); } if (node.HasNode("atmosphereTemperatureSunMultCurve")) { body.atmosphereTemperatureSunMultCurve.Load(node.GetNode("atmosphereTemperatureSunMultCurve")); } if (node.HasNode("axialTemperatureSunBiasCurve")) { body.axialTemperatureSunBiasCurve.Load(node.GetNode("axialTemperatureSunBiasCurve")); } if (node.HasNode("axialTemperatureSunMultCurve")) { body.axialTemperatureSunMultCurve.Load(node.GetNode("axialTemperatureSunMultCurve")); } if (node.HasNode("eccentricityTemperatureBiasCurve")) { body.eccentricityTemperatureBiasCurve.Load(node.GetNode("eccentricityTemperatureBiasCurve")); } if (node.HasValue("Radius")) { if (double.TryParse(node.GetValue("Radius"), out dtmp)) { body.Radius = dtmp; updateMass = true; } } // Orbit ConfigNode onode = node.GetNode("Orbit"); if (body.orbitDriver != null && body.orbit != null && onode != null) { patchOrbits = true; if (node.HasValue("semiMajorAxis")) if (double.TryParse(node.GetValue("semiMajorAxis"), out dtmp)) body.orbit.semiMajorAxis = dtmp; if (node.HasValue("eccentricity")) if (double.TryParse(node.GetValue("eccentricity"), out dtmp)) body.orbit.eccentricity = dtmp; if (node.HasValue("meanAnomalyAtEpoch")) if (double.TryParse(node.GetValue("meanAnomalyAtEpoch"), out dtmp)) body.orbit.meanAnomalyAtEpoch = dtmp; if (node.HasValue("meanAnomalyAtEpochD")) { if (double.TryParse(node.GetValue("meanAnomalyAtEpochD"), out dtmp)) { body.orbit.meanAnomalyAtEpoch = dtmp; body.orbit.meanAnomalyAtEpoch *= DEG2RAD; } } if (node.HasValue("inclination")) if (double.TryParse(node.GetValue("inclination"), out dtmp)) body.orbit.inclination = dtmp; if (node.HasValue("LAN")) if (double.TryParse(node.GetValue("LAN"), out dtmp)) body.orbit.LAN = dtmp; if (node.HasValue("argumentOfPeriapsis")) if (double.TryParse(node.GetValue("argumentOfPeriapsis"), out dtmp)) body.orbit.argumentOfPeriapsis = dtmp; } } else if (globalRotationRescale != 1d || globalRescale != 1d) print("Patching body " + body.bodyName + " from globals."); body.rotationPeriod *= globalRotationRescale; if(globalRescale != 1d) { body.Radius *= globalRescale; updateMass = true; } if (updateMass) { patchOrbits = true; GeeASLToOthers(body); } if (globalRescaleAtmo != 1d && body.atmosphere) { body.atmospherePressureCurve = RescaleCurve(body.atmospherePressureCurve, globalRescaleAtmo); body.atmosphereTemperatureCurve = RescaleCurve(body.atmosphereTemperatureCurve, globalRescaleAtmo); body.atmosphereTemperatureSunMultCurve = RescaleCurve(body.atmosphereTemperatureSunMultCurve, globalRescaleAtmo); body.atmosphereDepth *= globalRescaleAtmo; } body.SetupConstants(); // Fix up PQS if(PatchPQS(body, node, origRadius) || origRadius != body.Radius) StartCoroutine(PatchScaledSpace(body, node, origRadius)); body.CBUpdate(); return true; }