/// <summary> /// Generates a random orbit in as similar a manner to stock as possible. /// </summary> /// <returns>The orbit of a randomly selected member of the population.</returns> /// /// <exception cref="InvalidOperationException">Thrown if cannot produce stockalike /// orbits. The program will be in a consistent state in the event of an exception.</exception> public Orbit drawOrbit() { CelestialBody kerbin = FlightGlobals.Bodies.Find(body => body.isHomeWorld); CelestialBody dres = FlightGlobals.Bodies.Find(body => body.name.Equals("Dres")); if (dres != null && reachedBody(dres) && UnityEngine.Random.Range(0, 4) == 0) { // Drestroids double a = RandomDist.drawLogUniform(0.55, 0.65) * dres.sphereOfInfluence; double e = RandomDist.drawRayleigh(0.005); double i = RandomDist.drawRayleigh(0.005); // lAn takes care of negative inclinations double lAn = RandomDist.drawAngle(); double aPe = RandomDist.drawAngle(); double mEp = Math.PI / 180.0 * RandomDist.drawAngle(); double epoch = Planetarium.GetUniversalTime(); Debug.Log("[CustomAsteroids]: " + Localizer.Format("#autoLOC_CustomAsteroids_LogOrbit", a, e, i, aPe, lAn, mEp, epoch)); return(new Orbit(i, e, a, lAn, aPe, mEp, epoch, dres)); } if (kerbin != null) { // Kerbin interceptors double delay = RandomDist.drawUniform(12.5, 55.0); Debug.Log("[CustomAsteroids]: " + Localizer.Format("#autoLOC_CustomAsteroids_LogDefault", delay)); return(Orbit.CreateRandomOrbitFlyBy(kerbin, delay)); } throw new InvalidOperationException( Localizer.Format("#autoLOC_CustomAsteroids_ErrorDefaultNoKerbin")); }
public ProtoVessel SpawnAsteroid() { double baseDays = 21 + random.NextDouble() * 360; Orbit orbit = Orbit.CreateRandomOrbitFlyBy(Planetarium.fetch.Home, baseDays); ProtoVessel pv = DiscoverableObjectsUtil.SpawnAsteroid( DiscoverableObjectsUtil.GenerateAsteroidName(), orbit, (uint)SentinelUtilities.RandomRange(random), SentinelUtilities.WeightedAsteroidClass(random), double.PositiveInfinity, double.PositiveInfinity); return(pv); }
public static void LowTechWarp(CelestialBody TargetBody, float TimeFactor) { Debug.Log(Utils.Log("BMSHyperdrive.WarpDriver.LowTechWarp is Triggered. Beginning jump drive action.")); System.Random days = new System.Random(); int orbdays = days.Next(1, 10); Orbit orbit = Orbit.CreateRandomOrbitFlyBy(TargetBody, (orbdays / 10)); //Create random orbit parameters ///These warp drives blow raspberries at reality in a way that has some issues- ///one of them being going forward in time 20000 seconds- and even more for Kerbin warping ///Remember kids, DON'T BLOW RASPBERRIES AT REALITY!!! //Time warp - Set new UT var OldUT = Planetarium.GetUniversalTime(); var UTChange = TimeFactor * 1.667; var NewUT = (OldUT + UTChange); Planetarium.SetUniversalTime(NewUT); print(Utils.Log("UT updated. New UT: " + NewUT + ". Jumping.")); if (FlightGlobals.ActiveVessel != null) //If you have a vessel { print(Utils.Log("Activating WarpDrive, property FlightGlobals.ActiveVessel has a value.")); OrbitPhysicsManager.HoldVesselUnpack(60); FlightGlobals.ActiveVessel.GoOnRails(); //Make vessel controllable by code //Set vessel orbit params to those generated earlier 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; //Activate OPS-201 PRO - Inotialize orbit FlightGlobals.ActiveVessel.orbitDriver.orbit.Init(); FlightGlobals.ActiveVessel.orbitDriver.orbit.UpdateFromUT(Planetarium.GetUniversalTime()); } else { print(Utils.Log("Error. Could not initiate Jump Drive. Property FlightGlobals.ActiveVessel is null.")); print(Utils.Log(String.Format("How on ", FlightGlobals.GetHomeBodyDisplayName(), "did you even call this if there is no active vessel?"))); return; } }
public void SpawnAsteroid(Asteroid asteroid, UInt32 seed) { // Create Default Orbit Orbit orbit = null; CelestialBody body = null; // Select Orbit Type Int32 type = Random.Range(0, 3); if (type == 0 && asteroid.Location.Around.Count != 0) { // Around Location.AroundLoader[] around = GetProbabilityList(asteroid.Location.Around, asteroid.Location.Around.Select(a => a.Probability.Value).ToList()).ToArray(); Location.AroundLoader loader = around[Random.Range(0, around.Length)]; body = UBI.GetBody(loader.Body); if (!body) { return; } if (loader.Reached && !ReachedBody(body)) { return; } orbit = new Orbit { referenceBody = body, eccentricity = loader.Eccentricity, semiMajorAxis = loader.SemiMajorAxis, inclination = loader.Inclination, LAN = loader.LongitudeOfAscendingNode, argumentOfPeriapsis = loader.ArgumentOfPeriapsis, meanAnomalyAtEpoch = loader.MeanAnomalyAtEpoch, epoch = loader.Epoch }; orbit.Init(); } else if (type == 1 && asteroid.Location.Nearby.Count != 0) { // Nearby Location.NearbyLoader[] nearby = GetProbabilityList(asteroid.Location.Nearby, asteroid.Location.Nearby.Select(a => a.Probability.Value).ToList()).ToArray(); Location.NearbyLoader loader = nearby[Random.Range(0, nearby.Length)]; body = UBI.GetBody(loader.Body); if (!body) { return; } if (loader.Reached && !ReachedBody(body)) { return; } orbit = new Orbit { eccentricity = body.orbit.eccentricity + loader.Eccentricity, semiMajorAxis = body.orbit.semiMajorAxis * loader.SemiMajorAxis, inclination = body.orbit.inclination + loader.Inclination, LAN = body.orbit.LAN * loader.LongitudeOfAscendingNode, argumentOfPeriapsis = body.orbit.argumentOfPeriapsis * loader.ArgumentOfPeriapsis, meanAnomalyAtEpoch = body.orbit.meanAnomalyAtEpoch * loader.MeanAnomalyAtEpoch, epoch = body.orbit.epoch, referenceBody = body.orbit.referenceBody }; orbit.Init(); } else if (type == 2 && asteroid.Location.Flyby.Count != 0) { // Flyby Location.FlybyLoader[] flyby = GetProbabilityList(asteroid.Location.Flyby, asteroid.Location.Flyby.Select(a => a.Probability.Value).ToList()).ToArray(); Location.FlybyLoader loader = flyby[Random.Range(0, flyby.Length)]; body = UBI.GetBody(loader.Body); if (!body) { return; } if (loader.Reached && !ReachedBody(body)) { return; } orbit = Orbit.CreateRandomOrbitFlyBy(body, Random.Range(loader.MinDuration, loader.MaxDuration)); } // Check if (orbit == null) { Debug.Log("[Kopernicus] No new objects this time. (Probability is " + asteroid.Probability.Value + "%)"); return; } // Name String asteroidName = DiscoverableObjectsUtil.GenerateAsteroidName(); // Lifetime Double lifetime = Random.Range(asteroid.MinUntrackedLifetime, asteroid.MaxUntrackedLifetime) * 24d * 60d * 60d; Double maxLifetime = asteroid.MaxUntrackedLifetime * 24d * 60d * 60d; // Size UntrackedObjectClass size = (UntrackedObjectClass)(Int32)(asteroid.Size.Evaluate(Random.Range(0f, 1f)) * Enum.GetNames(typeof(UntrackedObjectClass)).Length); // Spawn ConfigNode vessel = null; vessel = ProtoVessel.CreateVesselNode( asteroidName, VesselType.SpaceObject, orbit, 0, new[] { ProtoVessel.CreatePartNode( "PotatoRoid", seed ) }, new ConfigNode("ACTIONGROUPS"), ProtoVessel.CreateDiscoveryNode( DiscoveryLevels.Presence, size, lifetime, maxLifetime ) ); OverrideNode(ref vessel, asteroid.Vessel); ProtoVessel protoVessel = new ProtoVessel(vessel, HighLogic.CurrentGame); if (asteroid.UniqueName && FlightGlobals.Vessels.Count(v => v.vesselName == protoVessel.vesselName) != 0) { return; } Kopernicus.Events.OnRuntimeUtilitySpawnAsteroid.Fire(asteroid, protoVessel); protoVessel.Load(HighLogic.CurrentGame.flightState); GameEvents.onNewVesselCreated.Fire(protoVessel.vesselRef); GameEvents.onAsteroidSpawned.Fire(protoVessel.vesselRef); Debug.Log("[Kopernicus] New object found near " + body.name + ": " + protoVessel.vesselName + "!"); }
public void SpawnAsteroid(Asteroid asteroid, UInt32 seed) { // Create Default Orbit Orbit orbit = null; CelestialBody body = null; // Select Orbit Type Int32 type = Random.Range(0, 3); if (type == 0 && asteroid.Location.Around.Count != 0) { // Around Location.AroundLoader[] around = GetProbabilityList(asteroid.Location.Around, asteroid.Location.Around.Select(a => a.Probability.Value).ToList()).ToArray(); Location.AroundLoader loader = around[Random.Range(0, around.Length)]; body = UBI.GetBody(loader.Body); if (!body) { return; } if (loader.Reached && !ReachedBody(body)) { return; } orbit = new Orbit { referenceBody = body, eccentricity = loader.Eccentricity, semiMajorAxis = loader.SemiMajorAxis, inclination = loader.Inclination, LAN = loader.LongitudeOfAscendingNode, argumentOfPeriapsis = loader.ArgumentOfPeriapsis, meanAnomalyAtEpoch = loader.MeanAnomalyAtEpoch, epoch = loader.Epoch }; orbit.Init(); } else if (type == 1 && asteroid.Location.Nearby.Count != 0) { // Nearby Location.NearbyLoader[] nearby = GetProbabilityList(asteroid.Location.Nearby, asteroid.Location.Nearby.Select(a => a.Probability.Value).ToList()).ToArray(); Location.NearbyLoader loader = nearby[Random.Range(0, nearby.Length)]; body = UBI.GetBody(loader.Body); if (!body) { return; } if (loader.Reached && !ReachedBody(body)) { return; } orbit = new Orbit { eccentricity = body.orbit.eccentricity + loader.Eccentricity, semiMajorAxis = body.orbit.semiMajorAxis * loader.SemiMajorAxis, inclination = body.orbit.inclination + loader.Inclination, LAN = body.orbit.LAN * loader.LongitudeOfAscendingNode, argumentOfPeriapsis = body.orbit.argumentOfPeriapsis * loader.ArgumentOfPeriapsis, meanAnomalyAtEpoch = body.orbit.meanAnomalyAtEpoch * loader.MeanAnomalyAtEpoch, epoch = body.orbit.epoch, referenceBody = body.orbit.referenceBody }; orbit.Init(); } else if (type == 2 && asteroid.Location.Flyby.Count != 0) { // Flyby Location.FlybyLoader[] flyby = GetProbabilityList(asteroid.Location.Flyby, asteroid.Location.Flyby.Select(a => a.Probability.Value).ToList()).ToArray(); Location.FlybyLoader loader = flyby[Random.Range(0, flyby.Length)]; body = UBI.GetBody(loader.Body); if (!body) { return; } if (loader.Reached && !ReachedBody(body)) { return; } orbit = Orbit.CreateRandomOrbitFlyBy(body, Random.Range(loader.MinDuration, loader.MaxDuration)); } // Check if (orbit == null) { Debug.Log("[Kopernicus] No new objects this time. (Probability is " + asteroid.Probability.Value + "%)"); return; } // Name String asteroidName = DiscoverableObjectsUtil.GenerateAsteroidName(); // Lifetime Double lifetime = Random.Range(asteroid.MinUntrackedLifetime, asteroid.MaxUntrackedLifetime) * 24d * 60d * 60d; Double maxLifetime = asteroid.MaxUntrackedLifetime * 24d * 60d * 60d; // Size UntrackedObjectClass size = (UntrackedObjectClass)(Int32)(asteroid.Size.Evaluate(Random.Range(0f, 1f)) * Enum.GetNames(typeof(UntrackedObjectClass)).Length); // Spawn ConfigNode vessel = null; #if (KSP_VERSION_1_10_1 || KSP_VERSION_1_11_1) if (Random.Range(0, 100) > RuntimeUtility.KopernicusConfig.CometPercentage) { #endif vessel = ProtoVessel.CreateVesselNode( asteroidName, VesselType.SpaceObject, orbit, 0, new[] { ProtoVessel.CreatePartNode( "PotatoRoid", seed ) }, new ConfigNode("ACTIONGROUPS"), ProtoVessel.CreateDiscoveryNode( DiscoveryLevels.Presence, size, lifetime, maxLifetime ) ); OverrideNode(ref vessel, asteroid.Vessel); ProtoVessel protoVessel = new ProtoVessel(vessel, HighLogic.CurrentGame); if (asteroid.UniqueName && FlightGlobals.Vessels.Count(v => v.vesselName == protoVessel.vesselName) != 0) { return; } Kopernicus.Events.OnRuntimeUtilitySpawnAsteroid.Fire(asteroid, protoVessel); protoVessel.Load(HighLogic.CurrentGame.flightState); GameEvents.onNewVesselCreated.Fire(protoVessel.vesselRef); GameEvents.onAsteroidSpawned.Fire(protoVessel.vesselRef); Debug.Log("[Kopernicus] New object found near " + body.name + ": " + protoVessel.vesselName + "!"); #if (KSP_VERSION_1_10_1 || KSP_VERSION_1_11_1) } else { float fragmentDynamicPressureModifier = 0f; bool optimizedCollider = false; CometOrbitType cometType = CometManager.GenerateWeightedCometType(); Orbit cometOrbit = cometType.CalculateHomeOrbit(); UntrackedObjectClass randomObjClass = cometType.GetRandomObjClass(); CometDefinition cometDef = CometManager.GenerateDefinition(cometType, randomObjClass, (int)seed); ConfigNode configNode = cometDef.CreateVesselNode(optimizedCollider, fragmentDynamicPressureModifier, hasName: false); ConfigNode configNode2 = ProtoVessel.CreatePartNode("PotatoComet", seed); uint value = 0u; configNode2.TryGetValue("persistentId", ref value); configNode.AddValue("cometPartId", value); ConfigNode configNode3 = new ConfigNode("VESSELMODULES"); configNode3.AddNode(configNode); vessel = ProtoVessel.CreateVesselNode(DiscoverableObjectsUtil.GenerateCometName(), VesselType.SpaceObject, cometOrbit, 0, new ConfigNode[1] { configNode2 }, new ConfigNode("ACTIONGROUPS"), ProtoVessel.CreateDiscoveryNode(DiscoveryLevels.Presence, randomObjClass, lifetime, maxLifetime), configNode3); OverrideNode(ref vessel, asteroid.Vessel); ProtoVessel protoVessel = new ProtoVessel(vessel, HighLogic.CurrentGame); Kopernicus.Events.OnRuntimeUtilitySpawnAsteroid.Fire(asteroid, protoVessel); protoVessel.Load(HighLogic.CurrentGame.flightState); GameEvents.onNewVesselCreated.Fire(protoVessel.vesselRef); GameEvents.onAsteroidSpawned.Fire(protoVessel.vesselRef); Debug.Log("[Kopernicus] New object found near " + body.name + ": " + protoVessel.vesselName + "!"); } #endif }
// Spawn the actual asteroid public void SpawnAsteroid(Asteroid asteroid, uint seed) { // Create Default Orbit Orbit orbit = null; CelestialBody body = null; // Select Orbit Type int type = Random.Range(0, 3); if (type == 0 && asteroid.location.around.Count != 0) { // Around IEnumerable <Location.AroundLoader> arounds = GetProbabilityList(asteroid.location.around, asteroid.location.around.Select(a => a.probability.value)); Location.AroundLoader around = arounds.ElementAt(Random.Range(0, arounds.Count())); body = PSystemManager.Instance.localBodies.Find(b => b.name == around.body); if (!body) { return; } if (around.reached && !ReachedBody(body)) { return; } orbit = new Orbit(); orbit.referenceBody = body; orbit.eccentricity = around.eccentricity; orbit.semiMajorAxis = around.semiMajorAxis; orbit.inclination = around.inclination; orbit.LAN = around.longitudeOfAscendingNode; orbit.argumentOfPeriapsis = around.argumentOfPeriapsis; orbit.meanAnomalyAtEpoch = around.meanAnomalyAtEpoch; orbit.epoch = around.epoch; orbit.Init(); } else if (type == 1 && asteroid.location.nearby.Count != 0) { // Nearby IEnumerable <Location.NearbyLoader> nearbys = GetProbabilityList(asteroid.location.nearby, asteroid.location.nearby.Select(a => a.probability.value)); Location.NearbyLoader nearby = nearbys.ElementAt(Random.Range(0, nearbys.Count())); body = PSystemManager.Instance.localBodies.Find(b => b.name == nearby.body); if (!body) { return; } if (nearby.reached && !ReachedBody(body)) { return; } orbit = new Orbit(); orbit.eccentricity = body.orbit.eccentricity + nearby.eccentricity; orbit.semiMajorAxis = body.orbit.semiMajorAxis * nearby.semiMajorAxis; orbit.inclination = body.orbit.inclination + nearby.inclination; orbit.LAN = body.orbit.LAN * nearby.longitudeOfAscendingNode; orbit.argumentOfPeriapsis = body.orbit.argumentOfPeriapsis * nearby.argumentOfPeriapsis; orbit.meanAnomalyAtEpoch = body.orbit.meanAnomalyAtEpoch * nearby.meanAnomalyAtEpoch; orbit.epoch = body.orbit.epoch; orbit.referenceBody = body.orbit.referenceBody; orbit.Init(); } else if (type == 2 && asteroid.location.flyby.Count != 0) { // Flyby IEnumerable <Location.FlybyLoader> flybys = GetProbabilityList(asteroid.location.flyby, asteroid.location.flyby.Select(a => a.probability.value)); Location.FlybyLoader flyby = flybys.ElementAt(Random.Range(0, flybys.Count())); body = PSystemManager.Instance.localBodies.Find(b => b.name == flyby.body); if (!body) { return; } if (flyby.reached && !ReachedBody(body)) { return; } orbit = Orbit.CreateRandomOrbitFlyBy(body, Random.Range(flyby.minDuration, flyby.maxDuration)); } // Check if (orbit == null) { Debug.Log("[Kopernicus]: No new objects this time. (Probablility is " + asteroid.probability.value + "%)"); return; } // Name string name = DiscoverableObjectsUtil.GenerateAsteroidName(); // Lifetime double lifetime = Random.Range(asteroid.minUntrackedLifetime, asteroid.maxUntrackedLifetime) * 24d * 60d * 60d; double maxLifetime = asteroid.maxUntrackedLifetime * 24d * 60d * 60d; // Size UntrackedObjectClass size = (UntrackedObjectClass)((int)(asteroid.size.curve.Evaluate(Random.Range(0f, 1f)) * Enum.GetNames(typeof(UntrackedObjectClass)).Length)); // Spawn ConfigNode vessel = ProtoVessel.CreateVesselNode( name, VesselType.SpaceObject, orbit, 0, new[] { ProtoVessel.CreatePartNode( "PotatoRoid", seed, new ProtoCrewMember[0] ) }, new ConfigNode("ACTIONGROUPS"), ProtoVessel.CreateDiscoveryNode( DiscoveryLevels.Presence, size, lifetime, maxLifetime ) ); OverrideNode(ref vessel, asteroid.vessel); ProtoVessel protoVessel = new ProtoVessel(vessel, HighLogic.CurrentGame); if (asteroid.uniqueName && FlightGlobals.Vessels.Count(v => v.vesselName == protoVessel.vesselName) != 0) { return; } protoVessel.Load(HighLogic.CurrentGame.flightState); GameEvents.onNewVesselCreated.Fire(protoVessel.vesselRef); Debug.Log("[Kopernicus]: New object found near " + body.name + ": " + protoVessel.vesselName + "!"); }
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); }