Пример #1
0
        private WBISpaceAnomaly createRandomAnomaly()
        {
            WBISpaceAnomaly anomaly    = new WBISpaceAnomaly(this);
            ConfigNode      vesselNode = createAnomalyVessel(anomaly);

            return(anomaly);
        }
Пример #2
0
        public override void OnLoad(ConfigNode node)
        {
            base.OnLoad(node);

            // Load anomalies
            spaceAnomalies = new List <WBISpaceAnomaly>();
            if (node.HasNode(WBISpaceAnomaly.kNodeName))
            {
                ConfigNode[] nodes = node.GetNodes(WBISpaceAnomaly.kNodeName);
                for (int index = 0; index < nodes.Length; index++)
                {
                    spaceAnomalies.Add(WBISpaceAnomaly.CreateFromNode(nodes[index]));
                }
            }

            // Load anomaly templates
            anomalyTemplates = new List <WBISpaceAnomaly>();
            ConfigNode[]    templateNodes = GameDatabase.Instance.GetConfigNodes(WBISpaceAnomaly.kNodeName);
            WBISpaceAnomaly anomaly;

            if (templateNodes != null)
            {
                for (int index = 0; index < templateNodes.Length; index++)
                {
                    anomaly = WBISpaceAnomaly.CreateFromNode(templateNodes[index]);

                    anomalyTemplates.Add(anomaly);
                }
            }
        }
Пример #3
0
        private ConfigNode createAnomalyVessel(WBISpaceAnomaly anomaly)
        {
            // Generate vessel name
            string vesselName = DiscoverableObjectsUtil.GenerateAsteroidName();
            string prefix     = Localizer.Format("#autoLOC_6001923");

            prefix     = prefix.Replace(" <<1>>", "");
            vesselName = vesselName.Replace(prefix, "UNK-");
            vesselName = vesselName.Replace("- ", "-");

            // Generate orbit
            Orbit orbit = generateOrbit(anomaly);

            // Create part nodes
            ConfigNode[] partNodes = new ConfigNode[] { ProtoVessel.CreatePartNode(anomaly.partName, 0) };

            // Determine lifetime
            double minLifetime = anomaly.minLifetime;
            double maxLifetime = anomaly.maxLifetime;

            if (minLifetime < 0)
            {
                minLifetime = double.MaxValue;
                maxLifetime = double.MaxValue;
            }

            // Setup object class
            UntrackedObjectClass objectClass = UntrackedObjectClass.A;

            Enum.TryParse(anomaly.sizeClass, out objectClass);

            // Create discovery and additional nodes.
            ConfigNode discoveryNode = ProtoVessel.CreateDiscoveryNode(DiscoveryLevels.Presence, objectClass, minLifetime, maxLifetime);

            ConfigNode[] additionalNodes = new ConfigNode[] { new ConfigNode("ACTIONGROUPS"), discoveryNode };

            // Create vessel node
            ConfigNode vesselNode = ProtoVessel.CreateVesselNode(vesselName, VesselType.SpaceObject, orbit, 0, partNodes, additionalNodes);

            // Add vessel node to the game.
            HighLogic.CurrentGame.AddVessel(vesselNode);

            // Get vessel ID
            if (vesselNode.HasValue("persistentId"))
            {
                anomaly.vesselId = vesselNode.GetValue("persistentId");
            }

            return(vesselNode);
        }
Пример #4
0
        public WBISpaceAnomaly(WBISpaceAnomaly copyFrom)
        {
            name      = copyFrom.name;
            partName  = copyFrom.partName;
            sizeClass = copyFrom.sizeClass;
            spawnMode = copyFrom.spawnMode;
            orbitType = copyFrom.orbitType;
            maxDaysToClosestApproach = copyFrom.maxDaysToClosestApproach;
            flyByOrbitChance         = copyFrom.flyByOrbitChance;
            fixedBody         = copyFrom.fixedBody;
            fixedSMA          = copyFrom.fixedSMA;
            fixedEccentricity = copyFrom.fixedEccentricity;
            fixedInclination  = copyFrom.fixedInclination;
            minLifetime       = copyFrom.minLifetime;
            maxLifetime       = copyFrom.maxLifetime;
            spawnTargetNumber = copyFrom.spawnTargetNumber;
            maxInstances      = copyFrom.maxInstances;
            vesselId          = copyFrom.vesselId;

            lastSeed = UnityEngine.Random.Range(0, int.MaxValue);
            UnityEngine.Random.InitState(lastSeed);
        }
Пример #5
0
        private List <WBISpaceAnomaly> createLastPlanetAnomalies(List <WBISpaceAnomaly> existingAnomalies)
        {
            // Filter the existing anomalies based on our parameters.
            List <WBISpaceAnomaly> filteredAnomalies = existingAnomalies.FindAll(p => p.spawnMode == spawnMode && p.partName == partName);

            List <WBISpaceAnomaly> anomalies = new List <WBISpaceAnomaly>();
            WBISpaceAnomaly        anomaly;
            ConfigNode             vesselNode;

            // Get the list of last planets
            List <CelestialBody> lastPlanets = BlueshiftScenario.shared.GetEveryLastPlanet();

            // Build the string containing the names of the bodies with anomalies.
            StringBuilder stringBuilder = new StringBuilder();
            int           count         = filteredAnomalies.Count;

            for (int index = 0; index < count; index++)
            {
                stringBuilder.Append(filteredAnomalies[index].fixedBody);
            }
            string bodiesWithAnomalies = stringBuilder.ToString();

            // Now check the list of last planets and see if the name of the body is in the string.
            count = lastPlanets.Count;
            for (int index = 0; index < count; index++)
            {
                if (!bodiesWithAnomalies.Contains(lastPlanets[index].name))
                {
                    anomaly           = new WBISpaceAnomaly(this);
                    anomaly.fixedBody = lastPlanets[index].name;
                    vesselNode        = createAnomalyVessel(anomaly);
                    anomalies.Add(anomaly);
                }
            }

            return(anomalies);
        }
Пример #6
0
        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);
        }
Пример #7
0
        public static WBISpaceAnomaly CreateFromNode(ConfigNode node)
        {
            WBISpaceAnomaly anomaly = new WBISpaceAnomaly();

            if (node.HasValue(kName))
            {
                anomaly.name = node.GetValue(kName);
            }

            if (node.HasValue(kPartName))
            {
                anomaly.partName = node.GetValue(kPartName);
            }

            if (node.HasValue(kSizeClass))
            {
                anomaly.sizeClass = node.GetValue(kSizeClass);
            }

            if (node.HasValue(kSpawnMode))
            {
                anomaly.spawnMode = (WBIAnomalySpawnModes)Enum.Parse(typeof(WBIAnomalySpawnModes), node.GetValue(kSpawnMode));
            }

            if (node.HasValue(kFixedBody))
            {
                anomaly.fixedBody = node.GetValue(kFixedBody);
            }

            if (node.HasValue(kFixedSMA))
            {
                double.TryParse(node.GetValue(kFixedSMA), out anomaly.fixedSMA);
            }

            if (node.HasValue(kFixedEccentricity))
            {
                double.TryParse(node.GetValue(kFixedEccentricity), out anomaly.fixedEccentricity);
            }

            if (node.HasValue(kFixedInclination))
            {
                double.TryParse(node.GetValue(kFixedInclination), out anomaly.fixedInclination);
            }

            if (node.HasValue(kMinLifetime))
            {
                double.TryParse(node.GetValue(kMinLifetime), out anomaly.minLifetime);
            }

            if (node.HasValue(kMaxLifetime))
            {
                double.TryParse(node.GetValue(kMaxLifetime), out anomaly.maxLifetime);
            }

            if (node.HasValue(kSpawnTargetNumber))
            {
                int.TryParse(node.GetValue(kSpawnTargetNumber), out anomaly.spawnTargetNumber);
            }

            if (node.HasValue(kMaxInstances))
            {
                int.TryParse(node.GetValue(kMaxInstances), out anomaly.maxInstances);
            }

            if (node.HasValue(kVesselId))
            {
                anomaly.vesselId = node.GetValue(kVesselId);
            }

            if (node.HasValue(kOrbitType))
            {
                Enum.TryParse(node.GetValue(kOrbitType), out anomaly.orbitType);
            }

            if (node.HasValue(kFlyByOrbitChance))
            {
                int.TryParse(node.GetValue(kFlyByOrbitChance), out anomaly.flyByOrbitChance);
            }

            if (node.HasValue(kMaxDaysToClosestApproach))
            {
                float.TryParse(node.GetValue(kMaxDaysToClosestApproach), out anomaly.maxDaysToClosestApproach);
            }

            return(anomaly);
        }