private void GenerateStrandedKerbal(int bodyID, string kerbalName) { //Add kerbal to crew roster. DarkLog.Debug("Spawning missing kerbal, name: " + kerbalName); ProtoCrewMember pcm = HighLogic.CurrentGame.CrewRoster.GetNewKerbal(ProtoCrewMember.KerbalType.Unowned); pcm.name = kerbalName; pcm.rosterStatus = ProtoCrewMember.RosterStatus.Assigned; //Create protovessel uint newPartID = ShipConstruction.GetUniqueFlightID(HighLogic.CurrentGame.flightState); CelestialBody contractBody = FlightGlobals.Bodies[bodyID]; //Atmo: 10km above atmo, to half the planets radius out. //Non-atmo: 30km above ground, to half the planets radius out. double minAltitude = FinePrint.Utilities.CelestialUtilities.GetMinimumOrbitalAltitude(contractBody, 1.1f); double maxAltitude = minAltitude + contractBody.Radius * 0.5; Orbit strandedOrbit = Orbit.CreateRandomOrbitAround(FlightGlobals.Bodies[bodyID], minAltitude, maxAltitude); ConfigNode[] kerbalPartNode = new ConfigNode[1]; ProtoCrewMember[] partCrew = new ProtoCrewMember[1]; partCrew[0] = pcm; kerbalPartNode[0] = ProtoVessel.CreatePartNode("kerbalEVA", newPartID, partCrew); ConfigNode protoVesselNode = ProtoVessel.CreateVesselNode(kerbalName, VesselType.EVA, strandedOrbit, 0, kerbalPartNode); ConfigNode discoveryNode = ProtoVessel.CreateDiscoveryNode(DiscoveryLevels.Unowned, UntrackedObjectClass.A, double.PositiveInfinity, double.PositiveInfinity); ProtoVessel protoVessel = new ProtoVessel(protoVesselNode, HighLogic.CurrentGame); protoVessel.discoveryInfo = discoveryNode; //It's not supposed to be infinite, but you're crazy if you think I'm going to decipher the values field of the rescue node. HighLogic.CurrentGame.flightState.protoVessels.Add(protoVessel); }
protected void setupOrbit() { Orbit orbit = Orbit.CreateRandomOrbitAround(Planetarium.fetch.Sun, kometMinAltitude, kometMaxAltitude); //Komets have eccentric orbits, let's randomize the eccentricity. orbit.eccentricity = UnityEngine.Random.Range(eccentricityMin, eccentricityMax); //Set the new orbit. FlightGlobals.fetch.SetShipOrbit(orbit.referenceBody.flightGlobalsIndex, orbit.eccentricity, orbit.semiMajorAxis, orbit.inclination, orbit.LAN, orbit.meanAnomaly, orbit.argumentOfPeriapsis, orbit.ObT); }
public void ConvertToKomet(Vessel asteroid) { //Set name asteroid.vesselName = "Kmt. " + ModuleKomet.CreateKometName(Planetarium.GetUniversalTime()); if (asteroid.rootPart != null) { asteroid.rootPart.initialVesselName = asteroid.vesselName; } Log("New komet " + asteroid.vesselName + " discovered!"); //Generate a random orbit for the komet. Orbit orbit = Orbit.CreateRandomOrbitAround(Planetarium.fetch.Sun, kometMinAltitude, kometMaxAltitude); //Komets have eccentric orbits, let's randomize the eccentricity and such. orbit.eccentricity = UnityEngine.Random.Range(eccentricityMin, eccentricityMax); //Initial komets are easier to reach.. if (!KerbalKometScenario.Instance.GetStartingKometsFlag()) { orbit.inclination = UnityEngine.Random.Range(0, 90f); orbit.LAN = UnityEngine.Random.Range(0, 360f); orbit.argumentOfPeriapsis = UnityEngine.Random.Range(0, 360f); } double orbitTime = UnityEngine.Random.Range(0, (float)orbit.GetTimeToPeriapsis()); //Set the orbit asteroid.orbit.SetOrbit(orbit.inclination, orbit.eccentricity, orbit.semiMajorAxis, orbit.LAN, orbit.argumentOfPeriapsis, orbit.meanAnomalyAtEpoch, orbitTime, Planetarium.fetch.Sun); //Hack: Astroids appear to spawn unloaded, and they don't appear to have any part modules when they first show up. To get around this, register the komet in the komet registry. //When the asteroid comes into physics range, we'll flip it to a komet. if (KerbalKometScenario.Instance.IsKometRegistered(asteroid.vesselName) == false) { KerbalKometScenario.Instance.RegisterKomet(asteroid.vesselName); } //Send out a press release if (KerbalKometSettings.SendPressRelease) { sendPressRelease(asteroid); } //Automatically track the new komet. if (KerbalKometSettings.AutoTrackKomets) { SpaceTracking.StartTrackingObject(asteroid); } }
private static void GenerateStrandedKerbal(int bodyId, string kerbalName) { //Add kerbal to crew roster. LunaLog.Log($"[LMP]: Spawning missing kerbal, Name: {kerbalName}"); var pcm = HighLogic.CurrentGame.CrewRoster.GetNewKerbal(ProtoCrewMember.KerbalType.Unowned); pcm.ChangeName(kerbalName); pcm.rosterStatus = ProtoCrewMember.RosterStatus.Assigned; //Create protovessel var newPartId = ShipConstruction.GetUniqueFlightID(HighLogic.CurrentGame.flightState); var contractBody = FlightGlobals.Bodies[bodyId]; //Atmo: 10km above atmo, to half the planets radius out. //Non-atmo: 30km above ground, to half the planets radius out. var minAltitude = CelestialUtilities.GetMinimumOrbitalDistance(contractBody, 1.1f); var maxAltitude = minAltitude + contractBody.Radius * 0.5; var strandedOrbit = Orbit.CreateRandomOrbitAround(FlightGlobals.Bodies[bodyId], minAltitude, maxAltitude); var kerbalPartNode = new[] { ProtoVessel.CreatePartNode("kerbalEVA", newPartId, pcm) }; var protoVesselNode = ProtoVessel.CreateVesselNode(kerbalName, VesselType.EVA, strandedOrbit, 0, kerbalPartNode); //It's not supposed to be infinite, but you're crazy if you think I'm going to decipher the values field of the rescue node. var discoveryNode = ProtoVessel.CreateDiscoveryNode(DiscoveryLevels.Unowned, UntrackedObjectClass.A, double.PositiveInfinity, double.PositiveInfinity); var protoVessel = new ProtoVessel(protoVesselNode, HighLogic.CurrentGame) { discoveryInfo = discoveryNode }; HighLogic.CurrentGame.flightState.protoVessels.Add(protoVessel); }
public static void MedTechWarp(CelestialBody TargetBody, float UTChange) { print("MrcarrotBMSHyperdrive.WarpDriver.MedTechWarp is Triggered. Beginning jump drive action."); //Ccreate orbital params and change UT var orbit = Orbit.CreateRandomOrbitAround(TargetBody, TargetBody.Radius + (TargetBody.sphereOfInfluence - 120000), TargetBody.Radius + (TargetBody.sphereOfInfluence - 100000)); var OldUT = Planetarium.GetUniversalTime(); var NewUT = (OldUT + UTChange); Planetarium.SetUniversalTime(NewUT); print(Utils.Log("UT updated. Jumping.")); if (FlightGlobals.ActiveVessel != null) { print(Utils.Log("Activating WarpDrive, property FlightGlobals.ActiveVessel has a value.")); OrbitPhysicsManager.HoldVesselUnpack(60); FlightGlobals.ActiveVessel.GoOnRails(); FlightGlobals.ActiveVessel.orbit.referenceBody = orbit.referenceBody; FlightGlobals.ActiveVessel.orbit.LAN = orbit.LAN; FlightGlobals.ActiveVessel.orbit.semiMajorAxis = orbit.semiMajorAxis; FlightGlobals.ActiveVessel.orbit.eccentricity = orbit.eccentricity; FlightGlobals.ActiveVessel.orbit.inclination = orbit.inclination; FlightGlobals.ActiveVessel.orbit.argumentOfPeriapsis = orbit.argumentOfPeriapsis; FlightGlobals.ActiveVessel.orbit.meanAnomalyAtEpoch = orbit.meanAnomalyAtEpoch; FlightGlobals.ActiveVessel.orbit.epoch = orbit.epoch; FlightGlobals.ActiveVessel.orbitDriver.orbit = orbit; FlightGlobals.ActiveVessel.orbitDriver.orbit.Init(); FlightGlobals.ActiveVessel.orbitDriver.orbit.UpdateFromUT(Planetarium.GetUniversalTime()); } else { Debug.LogError(Utils.Log("BMSHyperdrive.WarpDriver.MedTechWarp has encountered an 'anomaly'. Jump canceled.")); print("How on Kerbin did you even call this if there is no active vessel?"); return; } }
/// <summary> /// [HACK] Spawn a new body from the PSystem-Prefab /// </summary> public static void Instantiate(PSystemBody template, string name) { // Fix Templates if (template == null) { ScreenMessages.PostScreenMessage("You need a valid Template!", 3f, ScreenMessageStyle.UPPER_CENTER); return; } // Spawn Message ScreenMessages.PostScreenMessage("Created new Planet " + name + ", based on " + template.name + "!", 5f, ScreenMessageStyle.UPPER_CENTER); ScreenMessages.PostScreenMessage("This tool is meant to be used by modders, it can break mods!", 5f, ScreenMessageStyle.UPPER_CENTER); // Clone the Template GameObject bodyObject = UnityEngine.Object.Instantiate(template.gameObject); PSystemBody body = bodyObject.GetComponent <PSystemBody>(); // Alter it's name and flight-Number body.name = name; body.celestialBody.bodyName = name; body.celestialBody.transform.name = name; body.celestialBody.bodyTransform.name = name; body.scaledVersion.name = name; if (body.pqsVersion != null) { body.pqsVersion.name = name; body.pqsVersion.gameObject.name = name; body.pqsVersion.transform.name = name; foreach (PQS p in body.pqsVersion.GetComponentsInChildren(typeof(PQS), true)) { p.name = p.name.Replace(template.celestialBody.bodyName, name); } } body.flightGlobalsIndex = PSystemManager.Instance.localBodies.Last().flightGlobalsIndex + 1; // Change it's Orbit body.orbitDriver.orbit = Orbit.CreateRandomOrbitAround(PSystemManager.Instance.localBodies.First(), 4000000000, 60000000000); body.orbitDriver.referenceBody = PSystemManager.Instance.localBodies.First(); body.orbitDriver.orbit.referenceBody = body.orbitDriver.referenceBody; body.orbitRenderer.lowerCamVsSmaRatio = template.orbitRenderer.lowerCamVsSmaRatio; body.orbitRenderer.upperCamVsSmaRatio = template.orbitRenderer.upperCamVsSmaRatio; // Clear it's childs body.children = new List <PSystemBody>(); // Add it to the System-Prefab body.transform.parent = PSystemManager.Instance.systemPrefab.transform; PSystemManager.Instance.systemPrefab.rootBody.children.Add(body); // Hack^6 - Hack the PSystemManager to spawn this thing MethodInfo spawnBody = typeof(PSystemManager).GetMethod("SpawnBody", BindingFlags.NonPublic | BindingFlags.Instance); spawnBody.Invoke(PSystemManager.Instance, new object[] { PSystemManager.Instance.localBodies.First(), body }); CelestialBody cBody = PSystemManager.Instance.localBodies.Last(); // Add the body to FlightGlobals.Bodies FlightGlobals.fetch.bodies.Add(cBody); // Start the CelestialBody typeof(CelestialBody).GetMethod("Start", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(cBody, null); // Start the OrbitDriver if (cBody.orbitDriver != null) { typeof(OrbitDriver).GetMethod("Start", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(cBody.orbitDriver, null); } // Fix and start the OrbitRenderer if (Resources.FindObjectsOfTypeAll <OrbitRenderer>().Count(r => r.name == cBody.name) == 1) { OrbitRenderer renderer = Resources.FindObjectsOfTypeAll <OrbitRenderer>().First(r => r.name == cBody.name); renderer.driver = cBody.orbitDriver; renderer.celestialBody = cBody; typeof(OrbitRenderer).GetMethod("Start", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(renderer, null); } // Force the start of the PQS-Spheres foreach (PQS p in cBody.GetComponentsInChildren <PQS>(true)) { p.ForceStart(); } // Fix the ScaledVersion if (cBody.scaledBody.GetComponents <ScaledSpaceFader>().Length == 1) { cBody.scaledBody.GetComponent <ScaledSpaceFader>().celestialBody = cBody; } if (cBody.scaledBody.GetComponents <AtmosphereFromGround>().Length == 1) { cBody.scaledBody.GetComponent <AtmosphereFromGround>().planet = cBody; } }
private Orbit generateOrbit(WBISpaceAnomaly anomaly) { Orbit orbit = null; int bodyIndex = 0; CelestialBody body = null; switch (anomaly.spawnMode) { case WBIAnomalySpawnModes.fixedOrbit: body = FlightGlobals.GetBodyByName(anomaly.fixedBody); if (body != null) { double inclination = anomaly.fixedInclination; if (inclination < 0) { inclination = UnityEngine.Random.Range(0, 90); } return(new Orbit(inclination, anomaly.fixedEccentricity, anomaly.fixedSMA, 0, 0, 0, Planetarium.GetUniversalTime(), body)); } break; case WBIAnomalySpawnModes.randomOrbit: List <CelestialBody> bodies = FlightGlobals.fetch.bodies; bodyIndex = UnityEngine.Random.Range(0, bodies.Count - 1); body = bodies[bodyIndex]; break; case WBIAnomalySpawnModes.randomSolarOrbit: List <CelestialBody> stars = BlueshiftScenario.shared.GetStars(); bodyIndex = UnityEngine.Random.Range(0, stars.Count - 1); body = stars[bodyIndex]; break; case WBIAnomalySpawnModes.randomPlanetOrbit: List <CelestialBody> planets = BlueshiftScenario.shared.GetPlanets(); bodyIndex = UnityEngine.Random.Range(0, planets.Count - 1); body = planets[bodyIndex]; break; case WBIAnomalySpawnModes.everyLastPlanet: body = FlightGlobals.GetBodyByName(anomaly.fixedBody); break; } if (body != null) { switch (orbitType) { case WBIAnomalyOrbitTypes.elliptical: orbit = Orbit.CreateRandomOrbitAround(body); break; case WBIAnomalyOrbitTypes.flyBy: orbit = Orbit.CreateRandomOrbitFlyBy(body, UnityEngine.Random.Range(0, anomaly.maxDaysToClosestApproach)); break; case WBIAnomalyOrbitTypes.random: if (UnityEngine.Random.Range(1, 100) >= anomaly.flyByOrbitChance) { orbit = Orbit.CreateRandomOrbitFlyBy(body, UnityEngine.Random.Range(0, anomaly.maxDaysToClosestApproach)); } else { orbit = Orbit.CreateRandomOrbitAround(body); } break; } } return(orbit); }
private void GenerateStrandedKerbal(ConfigNode contractNode) { if (contractNode.GetValue("state") == "Active") { DarkLog.Debug("Generating stranded kerbal/compound contract"); int recoveryType = int.Parse(contractNode.GetValue("recoveryType")); int bodyID = int.Parse(contractNode.GetValue("targetBody")); int recoveryLocation = int.Parse(contractNode.GetValue("recoveryLocation")); int contractSeed = int.Parse(contractNode.GetValue("seed")); bool recoveringKerbal = recoveryType == 1 || recoveryType == 3; bool recoveringPart = recoveryType == 2 || recoveryType == 3; System.Random generator = new System.Random(contractSeed); // RECOVERY TYPES: // 0: None // 1: Kerbal // 2: Part // 3: Compound // Generate vessel part string partName = contractNode.GetValue("partName"); string[] contractValues = contractNode.GetValue("values").Split(','); double contractDeadline = double.Parse(contractValues[1]); uint newPartID = uint.Parse(contractNode.GetValue("partID")); CelestialBody contractBody = FlightGlobals.Bodies[bodyID]; List <string> vesselDescriptionList; if (recoveringKerbal) { vesselDescriptionList = new List <string> { "Shipwreck", "Wreckage", "Pod", "Capsule", "Derelict", "Heap", "Hulk", "Craft", "Debris", "Scrap" }; } else { vesselDescriptionList = new List <string> { "Prototype", "Device", "Part", "Module", "Unit", "Component" }; } string vesselDescription = vesselDescriptionList[generator.Next(0, vesselDescriptionList.Count)]; Orbit strandedOrbit; // Low orbit if (recoveryLocation != 1) { // High orbit if (recoveryLocation != 2) { strandedOrbit = new Orbit(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, contractBody); } else { strandedOrbit = FinePrint.Utilities.OrbitUtilities.GenerateOrbit(contractSeed, contractBody, FinePrint.Utilities.OrbitType.RANDOM, FinePrint.ContractDefs.Recovery.HighOrbitDifficulty, FinePrint.ContractDefs.Recovery.HighOrbitDifficulty, 0.0); } } else { double minAltitude = FinePrint.Utilities.CelestialUtilities.GetMinimumOrbitalDistance(contractBody, 1f) - contractBody.Radius; strandedOrbit = Orbit.CreateRandomOrbitAround(contractBody, contractBody.Radius + minAltitude * 1.1000000238418579, contractBody.Radius + minAltitude * 1.25); strandedOrbit.meanAnomalyAtEpoch = generator.NextDouble() * 2.0 * Math.PI; } ConfigNode configNode = null; if (recoveringKerbal) { DarkLog.Debug("We want to recover a kerbal, so let's do it"); string kerbalName = contractNode.GetValue("kerbalName"); int kerbalGender = int.Parse(contractNode.GetValue("gender")); string vesselName = FinePrint.Utilities.StringUtilities.PossessiveString(FinePrint.Utilities.StringUtilities.ShortKerbalName(kerbalName)) + " " + vesselDescription; // Recovery Locations: // 0: None, // 1: Low Orbit, // 2: High Orbit, // 3: Surface ProtoCrewMember pcm = null; if (!HighLogic.CurrentGame.CrewRoster.Exists(kerbalName)) { DarkLog.Debug("Spawning missing kerbal, name: " + kerbalName); pcm = HighLogic.CurrentGame.CrewRoster.GetNewKerbal(ProtoCrewMember.KerbalType.Unowned); pcm.ChangeName(kerbalName); pcm.gender = (ProtoCrewMember.Gender)kerbalGender; pcm.rosterStatus = ProtoCrewMember.RosterStatus.Assigned; pcm.seat = null; pcm.seatIdx = -1; //Add kerbal to crew roster. } else { pcm = HighLogic.CurrentGame.CrewRoster[kerbalName]; } // Spawn lone kerbal if (partName == "kerbalEVA") { configNode = ProtoVessel.CreateVesselNode(kerbalName, VesselType.EVA, strandedOrbit, 0, new ConfigNode[] { CreateProcessedPartNode(partName, newPartID, new ProtoCrewMember[] { pcm }) }, new ConfigNode[] { ProtoVessel.CreateDiscoveryNode(DiscoveryLevels.Unowned, UntrackedObjectClass.A, contractDeadline * 2, contractDeadline * 2) }); configNode.AddValue("prst", true); ProtoVessel pv = HighLogic.CurrentGame.AddVessel(configNode); VesselWorker.fetch.LoadVessel(configNode, pv.vesselID, false); NetworkWorker.fetch.SendVesselProtoMessage(pv, false, false); } // Spawn kerbal in capsule/pod else { configNode = ProtoVessel.CreateVesselNode(vesselName, (recoveryLocation != 3) ? VesselType.Ship : VesselType.Lander, strandedOrbit, 0, new ConfigNode[] { CreateProcessedPartNode(partName, newPartID, new ProtoCrewMember[] { pcm }) }, new ConfigNode[] { new ConfigNode("ACTIONGROUPS"), ProtoVessel.CreateDiscoveryNode(DiscoveryLevels.Unowned, UntrackedObjectClass.A, contractDeadline * 2, contractDeadline * 2) }); configNode.AddValue("prst", true); ProtoVessel pv = HighLogic.CurrentGame.AddVessel(configNode); VesselWorker.fetch.LoadVessel(configNode, pv.vesselID, false); NetworkWorker.fetch.SendVesselProtoMessage(pv, false, false); } } } }