Example #1
0
        /// <summary>
        ///     Generates the parameters for a planet
        /// </summary>
        private static ConfigNode GenerateBody(Planet planet, String folder, String referenceBody = "Sun",
                                               String systematicName = null)
        {
            String     name = systematicName ?? Utility.GenerateName();
            ConfigNode node = new ConfigNode("Body");

            // Name
            if (planet != Kerbin)
            {
                node.AddValue("name", name);
            }
            else
            {
                node.AddValue("name", "Kerbin");
                node.AddValue("cbNameLater", name);
            }

            // Random Main Menu Body
            node.AddValue("randomMainMenuBody", "" + (referenceBody == "Sun"));

            // Template
            ConfigNode template = new ConfigNode("Template");

            node.AddConfigNode(template);
            if (planet != Kerbin)
            {
                if (planet.gas_giant)
                {
                    template.AddValue("name", "Jool");
                }
                else
                {
                    template.AddValue("name", Utility.GetTemplate(planet.surface_pressure > 0.00001, false));
                    template.AddValue("removeAllPQSMods", "True");
                    if (planet.surface_pressure <= 0.00001)
                    {
                        template.AddValue("removeAtmosphere", "True");
                    }
                    template.AddValue("removeOcean", "True");
                    // TODO: Handle atmospheres and ocean
                }
            }
            else
            {
                template.AddValue("name", "Kerbin");
                template.AddValue("removePQSMods",
                                  "PQSLandControl, QuadEnhanceCoast, VertexHeightMap, VertexHeightNoiseVertHeightCurve2, VertexRidgedAltitudeCurve, VertexSimplexHeightAbsolute");
                template.AddValue("removeAtmosphere", "True");
                template.AddValue("removeOcean", "True");
            }

            // Properties
            ConfigNode properties = new ConfigNode("Properties");

            node.AddConfigNode(properties);
            properties.AddValue("radius", "" + (planet.radius * 100));
            properties.AddValue("geeASL", "" + planet.surface_grav);
            properties.AddValue("rotationPeriod", "" + (planet.day * 60 * 60));
            properties.AddValue("tidallyLocked", "" + planet.resonant_period);
            properties.AddValue("initialRotation", "" + Random.Next(0, 361));
            properties.AddValue("albedo", "" + planet.albedo);
            properties.AddValue("timewarpAltitudeLimits",
                                String.Join(" ",
                                            Templates.KerbinTimewarplimits.Select(i =>
                                                                                  (Int32)(i * ((planet.radius *
                                                                                                100) / 600000)))));
            //properties.AddValue("useTheInName", "False");

            // Log
            Console.WriteLine($"Generated a planet named {name}. GasGiant: {planet.gas_giant}. Template: {template.GetValue("name")}");

            // Color
            Color planetColor = Utility.GenerateColor();

            // Orbit
            ConfigNode orbit = new ConfigNode("Orbit");

            node.AddConfigNode(orbit);
            orbit.AddValue("referenceBody", referenceBody);
            orbit.AddValue("eccentricity", "" + planet.e);
            orbit.AddValue("semiMajorAxis", "" + (planet.a * 100 * Constants.KM_PER_AU));
            orbit.AddValue("longitudeOfAscendingNode", "" + Random.Range(0, 181));
            orbit.AddValue("meanAnomalyAtEpochD", "" + Random.Range(0, 181));
            if (planet.gas_giant)
            {
                orbit.AddValue("color", Parser.WriteColor(Utility.AlterColor(planetColor)));
            }
            // Inclination
            orbit.AddValue("inclination", "" + Random.Range(-3, 5));

            // Log
            Console.WriteLine($"Generated orbit around {referenceBody} for {name}");

            // Scaled Space
            ConfigNode scaled = new ConfigNode("ScaledVersion");

            node.AddConfigNode(scaled);

            // Material
            ConfigNode mat = new ConfigNode("Material");

            scaled.AddConfigNode(mat);
            if (!planet.gas_giant)
            {
                mat.AddValue("texture", folder + "/PluginData/" + name + "_Texture.png");
                mat.AddValue("normals", folder + "/PluginData/" + name + "_Normals.png");
            }
            if (planet.gas_giant)
            {
                // Texture
                String[] files       = Directory.GetFiles(Directory.GetCurrentDirectory() + "/data/textures/");
                String   texture     = Random.Choose(files);
                String   textureName = Path.GetFileNameWithoutExtension(texture) + ".png";
                Directory.CreateDirectory(Directory.GetCurrentDirectory() + "/systems/" + folder + "/PluginData/");
                File.Copy(texture,
                          Directory.GetCurrentDirectory() + "/systems/" + folder + "/PluginData/" + textureName, true);
                mat.AddValue("texture", folder + "/PluginData/" + textureName);

                // Load the Image
                Color average;
                using (Bitmap image = new Bitmap(Directory.GetCurrentDirectory() + "/systems/" + folder +
                                                 "/PluginData/" +
                                                 textureName))
                {
                    average = Utility.GetAverageColor(image);
                }

                // Scale
                mat.AddValue("mainTexScale",
                             (Random.Next(0, 2) == 1 ? 1 : -1) + "," + (Random.Next(0, 2) == 1 ? 1 : -1));

                mat.AddValue("color", Parser.WriteColor(Utility.ReColor(Utility.AlterColor(planetColor), average)));
                ConfigNode gradient = new ConfigNode("Gradient");
                mat.AddConfigNode(gradient);
                gradient.AddValue("0.0", Parser.WriteColor(Utility.AlterColor(planetColor)));
                gradient.AddValue("0.6", "0.0549,0.0784,0.141,1");
                gradient.AddValue("1.0", "0.0196,0.0196,0.0196,1");
            }

            // Log
            Console.WriteLine($"Generated scaled space for {name}");

            // Atmosphere
            if (planet.surface_pressure > 0.00001)
            {
                ConfigNode atmosphere = new ConfigNode("Atmosphere");
                node.AddConfigNode(atmosphere);
                atmosphere.AddValue("enabled", "True");
                atmosphere.AddValue("oxygen", "" + Random.Boolean(10));
                atmosphere.AddValue("atmosphereDepth", "" + (planet.radius * 100 * Random.Range(0.1, 0.16)));
                atmosphere.AddValue("atmosphereMolarMass", "" + (planet.molecule_weight / 1000));
                atmosphere.AddValue("staticPressureASL", "" + ((planet.surface_pressure / 1000) * 101.324996948242));
                atmosphere.AddValue("temperatureSeaLevel", "" + planet.surface_temp);
                if (planet.gas_giant)
                {
                    atmosphere.AddValue("ambientColor", Parser.WriteColor(Utility.AlterColor(planetColor)));
                    atmosphere.AddValue("lightColor",
                                        Parser.WriteColor(Utility.AlterColor(Utility.LightColor(planetColor))));
                }
                GenerateAtmosphereCurves(ref atmosphere, planet.gas_giant ? "Jool" : Utility.GetTemplate(true, true));

                // Log
                Console.WriteLine($"Generated atmosphere for {name}");

                // Ocean
                if (!planet.gas_giant)
                {
                    if (planet.surface_temp < planet.boil_point && (Random.Next(0, 100) < 35))
                    {
                        ConfigNode ocean = new ConfigNode("Ocean");
                        node.AddConfigNode(ocean);
                        ocean.AddValue("density", "" + planet.hydrosphere * (Random.NextDouble() * 2));
                        ocean.AddValue("minLevel", "1");
                        ocean.AddValue("maxLevel", "6");
                        ocean.AddValue("minDetailDistance", "8");
                        ocean.AddValue("maxQuadLengthsPerFrame", "0.03");

                        Color waterColor = Utility.GenerateColor();
                        ocean.AddValue("oceanColor", Parser.WriteColor(waterColor));

                        ConfigNode oceanmat = new ConfigNode("Material");
                        ocean.AddConfigNode(oceanmat);

                        oceanmat.AddValue("colorFromSpace", Parser.WriteColor(waterColor));
                        oceanmat.AddValue("color", Parser.WriteColor(waterColor));

                        ConfigNode oceanfallbackmat = new ConfigNode("FallbackMaterial");
                        ocean.AddConfigNode(oceanfallbackmat);

                        oceanfallbackmat.AddValue("colorFromSpace", Parser.WriteColor(waterColor));
                        oceanfallbackmat.AddValue("color", Parser.WriteColor(waterColor));

                        Console.WriteLine($"Generated ocean for {name}");
                    }
                }
            }

            // Rings :D
            if (planet.gas_giant && (Random.Next(0, 100) < 5))
            {
                ConfigNode rings = new ConfigNode("Rings");
                node.AddConfigNode(rings);
                ConfigNode ringDatatbase = Utility.Load("rings");
                ConfigNode data          = Random.Choose(ringDatatbase.nodes);
                RingPrefab def           = Parser.CreateObjectFromConfigNode <RingPrefab>(data);
                foreach (RingPrefab.Ring r in def.Rings)
                {
                    ConfigNode ring = new ConfigNode("Ring");
                    rings.AddConfigNode(ring);
                    ring.AddValue("innerRadius", "" + (planet.radius * 0.1 * r.InnerRadius));
                    ring.AddValue("outerRadius", "" + (planet.radius * 0.1 * r.OuterRadius));
                    ring.AddValue("angle", "" + r.Angle);
                    ring.AddValue("color", Parser.WriteColor(Utility.AlterColor(planetColor)));
                    ring.AddValue("lockRotation", "" + r.LockRotation);
                    ring.AddValue("unlit", "False");
                }

                // Log
                Console.WriteLine($"Generated rings around {name}");
            }

            // PQS
            if (!planet.gas_giant)
            {
                Color        average;
                List <Color> biomes;
                GeneratePQS(ref node, name, folder, planet, planetColor, out average, out biomes);

                // Biomes
                ConfigNode bio = new ConfigNode("Biomes");
                properties.AddConfigNode(bio);
                for (int i = 0; i < biomes.Count; i++)
                {
                    ConfigNode biome = new ConfigNode("Biome");
                    bio.AddConfigNode(biome);

                    biome.AddValue("name", "Biome " + (i + 1));
                    biome.AddValue("value", "1");
                    biome.AddValue("color", Parser.WriteColor(biomes[i]));
                }
                Console.WriteLine($"Generated biomes for {name}");

                // Apply colors
                orbit.AddValue("color", Parser.WriteColor(Utility.AlterColor(average)));
                if (node.HasNode("Atmosphere"))
                {
                    ConfigNode atmosphere = node.GetNode("Atmosphere");
                    atmosphere.AddValue("ambientColor", Parser.WriteColor(Utility.AlterColor(average)));
                    atmosphere.AddValue("lightColor",
                                        Parser.WriteColor(Utility.AlterColor(Utility.LightColor(average))));
                }
                ConfigNode gradient = new ConfigNode("Gradient");
                mat.AddConfigNode(gradient);
                gradient.AddValue("0.0", Parser.WriteColor(Utility.AlterColor(average)));
                gradient.AddValue("0.6", "0.0549,0.0784,0.141,1");
                gradient.AddValue("1.0", "0.0196,0.0196,0.0196,1");
            }

            // Log
            Console.WriteLine($"Generation of body {name} finished!");
            Console.WriteLine();

            // Return
            return(node);
        }