예제 #1
0
            public void UpdateBody()
            {
                print("UpdateLocal:" + name);
                localUpdate(this);

                print("UpdateScaled:" + name);
                var smallPlanet = PFUtil.FindScaled(name);

                if (templateName == "Sun")
                {
                    var sunBody = PFUtil.FindCB("Sun");
                    var cb      = PFUtil.FindCB(name);

                    var orbitDriver = smallPlanet.AddComponent <OrbitDriver>();
                    orbitDriver.updateMode = OrbitDriver.UpdateMode.UPDATE;
                    orbitDriver.name       = cb.name;
                    orbitDriver.orbit      = new Orbit(orbit.inclination, orbit.eccentricity, orbit.semiMajorAxis, orbit.LAN,
                                                       orbit.argumentOfPeriapsis, orbit.meanAnomalyAtEpoch, orbit.epoch, sunBody);

                    cb.orbitDriver = orbitDriver;

                    orbitDriver.referenceBody = sunBody;
                    orbitDriver.celestialBody = cb;

                    cb.sphereOfInfluence        = cb.orbit.semiMajorAxis * Math.Pow(cb.Mass / cb.orbit.referenceBody.Mass, 0.4);
                    cb.hillSphere               = cb.orbit.semiMajorAxis * (1 - cb.orbit.eccentricity) * Math.Pow(cb.Mass / cb.orbit.referenceBody.Mass, 0.333333333333333);
                    cb.orbitDriver.QueuedUpdate = true;
                    cb.CBUpdate();

                    orbitDriver.UpdateOrbit();
                }

                LoadScaledPlanet(smallPlanet, name);
            }
예제 #2
0
            public static void LoadScaledPlanet(GameObject smallPlanet, string name, bool bLoadTemp = false)
            {
                var root = ConfigNode.Load(DataPath + name + ".cfg");

                if (root != null)
                {
                    var sConfig = root.nodes.GetNode("ScaledTransform");
                    //print(cbConfig);
                    if (sConfig != null)
                    {
                        var scaledBody = PFUtil.FindScaled(name);

                        var ratio    = float.Parse(sConfig.GetValue("ratio"));
                        var newScale = (float)PFUtil.FindCB(name).Radius *ratio;
                        scaledBody.transform.localScale = new Vector3(newScale, newScale, newScale);
                    }
                }

                var binName = name + ".bin";

                if (!bLoadTemp)
                {
                    var colorTexture = PFUtil.LoadTexture(DataPath + name + "_map.png");
                    var bumpTexture  = PFUtil.LoadTexture(DataPath + name + "_normal.png");

                    LoadScaledPlanetTextures(name, colorTexture, bumpTexture);
                }
                else
                {
                    var colorTexture = PFUtil.LoadTexture(DataPath + name + "_map_.png");
                    var bumpTexture  = PFUtil.LoadTexture(DataPath + name + "_normal_.png");
                    binName = name + "_.bin";
                    LoadScaledPlanetTextures(name, colorTexture, bumpTexture);
                }

                if (KSP.IO.File.Exists <PlanetFactory>(binName))
                {
                    //print("Loading mesh");
                    var smallPlanetMeshFilter = (MeshFilter)smallPlanet.GetComponentInChildren((typeof(MeshFilter)));
                    var newVerts = new Vector3[smallPlanetMeshFilter.mesh.vertices.Count()];
                    var reader   = KSP.IO.BinaryReader.CreateForType <PlanetFactory>(binName);
                    for (var i = 0; i < newVerts.Count(); i++)
                    {
                        newVerts[i].x = reader.ReadSingle();
                        newVerts[i].y = reader.ReadSingle();
                        newVerts[i].z = reader.ReadSingle();
                    }
                    smallPlanetMeshFilter.mesh.vertices = newVerts;
                    smallPlanetMeshFilter.mesh.RecalculateNormals();

                    //smallPlanetMeshFilter.mesh.tangents = null;
                    PFUtil.RecalculateTangents(smallPlanetMeshFilter.mesh);
                }
            }
예제 #3
0
 void LogoWindow(int windowID)
 {
     if (logoTexture == null)
     {
         logoTexture = PFUtil.LoadTexture(DataPath + "logo_small.png");
     }
     if (logoTexture != null)
     {
         GUI.DrawTexture(new Rect(4, 20, logoTexture.width, logoTexture.height), logoTexture);
     }
     isFactoryEnabled = GUI.Toggle(new Rect(10, 20, 320, 20), isFactoryEnabled, "Load Sentar Expansion");
 }
예제 #4
0
        public static Orbit ParseOrbit(ConfigNode node)
        {
            var inc     = double.Parse(node.GetValue("inclination"));
            var ecc     = double.Parse(node.GetValue("eccentricity"));
            var sma     = double.Parse(node.GetValue("semiMajorAxis"));
            var lan     = double.Parse(node.GetValue("LAN"));
            var aop     = double.Parse(node.GetValue("argumentOfPeriapsis"));
            var mae     = double.Parse(node.GetValue("meanAnomalyAtEpoch"));
            var epo     = double.Parse(node.GetValue("epoch"));
            var refBody = node.GetValue("referenceBody");

            return(new Orbit(inc, ecc, sma, lan, aop, mae, epo, PFUtil.FindCB(refBody)));
        }
예제 #5
0
        private void SetLocalCollision(string planetName, bool enabled = true)
        {
            var localPlanet = PFUtil.FindLocal(planetName);
            var cols        = localPlanet.GetComponentsInChildren <Collider>();

            foreach (var c in cols)
            {
                if (c.enabled != enabled)
                {
                    print("Updating collision " + c.gameObject.name + "=" + enabled);
                    c.enabled = enabled;
                }
            }
        }
예제 #6
0
        public static void LoadOrbit(CelestialBody body)
        {
            var orbit = new PFOrbit();

            if (!orbit.Load(body.name))
            {
                return;
            }

            var parentBody = PFUtil.FindCB(body.name);

            if (body.orbitDriver != null)
            {
                body.orbitDriver.orbit = new Orbit(orbit.inclination, orbit.eccentricity,
                                                   orbit.semiMajorAxis, orbit.LAN,
                                                   orbit.argumentOfPeriapsis, orbit.meanAnomalyAtEpoch, orbit.epoch, body.orbit.referenceBody);// parentBody);

                body.orbitDriver.UpdateOrbit();
            }
        }
예제 #7
0
        private void OnGUI()
        {
            if (!isTooLateToLoad)
            {
                windowRect = GUI.Window(0, windowRect, LogoWindow, "Krags Planet Factory");

                autoLoadSave = GUI.Toggle(new Rect(20, 280, 160, 20), autoLoadSave, "Auto Load Savegame->");

                int selectedItemIndex = comboBoxControl.SelectedItemIndex;
                selectedItemIndex = comboBoxControl.List(
                    new Rect(170, 280, 160, 20), comboBoxList[selectedItemIndex].text, comboBoxList, listStyle);

                autoLoadSaveName = comboBoxList[selectedItemIndex].text;

                if (GUI.changed)
                {
                    SaveConfig();
                }
            }
#if DEBUG
            if (guiHidden)
            {
                return;
            }

            if (FlightGlobals.currentMainBody == null)
            {
                return;
            }

            var curBodyName = FlightGlobals.currentMainBody.bodyName;
            if (GUI.Button(new Rect(20, 60, 100, 20), "Reload " + curBodyName))
            {
                LoadPQS(curBodyName);

                var cb = PFUtil.FindCB(curBodyName);
                if (cb)
                {
                    LoadCB(cb);
                    LoadOrbit(cb);
                }
            }
            if (GUI.Button(new Rect(20, 80, 100, 20), "Reload scaled " + curBodyName))
            {
                PFBody.LoadScaledPlanet(PFUtil.FindScaled(curBodyName), curBodyName, true);
            }

            if (GUI.Button(new Rect(20, 120, 100, 20), "Export " + curBodyName))
            {
                var width = 2048;
                if (FlightGlobals.currentMainBody.Radius < 50000)
                {
                    width = 1024;
                }
                PFExport.CreateScaledSpacePlanet(curBodyName, "Laythe", null, width, 20000);
            }
            //if (GUI.Button(new Rect(20, 140, 100, 20), "Effect " + curBodyName))
            //{
            //    PFEffects.TestEffects(PFUtil.FindScaled(curBodyName));
            //}
#endif
        }
예제 #8
0
        public static void CreateScaledSpacePlanet(string name, string templateName, GameObject body = null, int mapWidth = 2048, int maxHeight = 20000)
        {
            //print("Creating new scaledPlanet " + name);

            var hasOcean    = false;
            var oceanHeight = 0.0;
            var exportBin   = true;
            var removeAlpha = true;

            var configRoot = ConfigNode.Load(PlanetFactory.DataPath + name + ".cfg");

            if (configRoot != null && configRoot.HasNode("ScaledExport"))
            {
                PlanetFactory.print("Loading ScaledExport");
                var node = configRoot.GetNode("ScaledExport");
                if (node.HasValue("templateName"))
                {
                    templateName = node.GetValue("templateName");
                }
                if (node.HasValue("mapWidth"))
                {
                    mapWidth = int.Parse(node.GetValue("mapWidth"));
                }
                if (node.HasValue("maxHeight"))
                {
                    maxHeight = int.Parse(node.GetValue("maxHeight"));
                }

                if (node.HasValue("hasOcean"))
                {
                    hasOcean = bool.Parse(node.GetValue("hasOcean"));
                }
                if (node.HasValue("oceanHeight"))
                {
                    oceanHeight = double.Parse(node.GetValue("oceanHeight"));
                }

                if (node.HasValue("exportBin"))
                {
                    exportBin = bool.Parse(node.GetValue("exportBin"));
                }

                if (node.HasValue("removeAlpha"))
                {
                    removeAlpha = bool.Parse(node.GetValue("removeAlpha"));
                }

                PlanetFactory.print("Loaded ScaledExport");
            }



            var template = GameObject.Find(templateName);

            if (body == null)
            {
                body = GameObject.Find(name);
            }

            var bodyPQS = body.GetComponentInChildren <PQS>();

            var templatePQS = template.GetComponentInChildren <PQS>();

            if (exportBin)
            {
                var scaledSpace = GameObject.Find("scaledSpace");
                var smallMinmus = scaledSpace.transform.Find(templateName);

                var smallTemplateMeshFilter = (MeshFilter)smallMinmus.GetComponentInChildren((typeof(MeshFilter)));
                var originalVert            = smallTemplateMeshFilter.mesh.vertices[0];
                var originalHeight          = (float)templatePQS.GetSurfaceHeight(originalVert);
                var scale = originalHeight / originalVert.magnitude;
                //MonoBehaviour.print("Hei:" + originalHeight + " Mag:" + originalVert.magnitude + " Scale:" + scale);
                bodyPQS.isBuildingMaps = true;

                var newVerts = new Vector3[smallTemplateMeshFilter.mesh.vertices.Count()];
                for (int i = 0; i < smallTemplateMeshFilter.mesh.vertices.Count(); i++)
                {
                    var vertex  = smallTemplateMeshFilter.mesh.vertices[i];
                    var rootrad = (float)Math.Sqrt(vertex.x * vertex.x +
                                                   vertex.y * vertex.y +
                                                   vertex.z * vertex.z);
                    var radius = (float)bodyPQS.GetSurfaceHeight(vertex) / scale;
                    //radius = 1000;
                    newVerts[i] = vertex * (radius / rootrad);
                }
                bodyPQS.isBuildingMaps = false;

                //print("Writing scaledPlanet");
                var writer = KSP.IO.BinaryWriter.CreateForType <PlanetFactory>(name + "_.bin");
                foreach (var v in newVerts)
                {
                    writer.Write(v.x);
                    writer.Write(v.y);
                    writer.Write(v.z);
                }
                writer.Close();
            }

            var textures = bodyPQS.CreateMaps(mapWidth, maxHeight, hasOcean, oceanHeight, bodyPQS.mapOceanColor);//new Color(0.2f,0.2f,0.6f,0.5f));

            //print("Writing textures");
            if (removeAlpha)
            {
                for (int i = 0; i < textures[0].width * textures[0].height; i++)
                {
                    var c = textures[0].GetPixel(i % textures[0].width, i / textures[0].width);
                    c.a = 1.0f;
                    textures[0].SetPixel(i % textures[0].width, i / textures[0].width, c);
                }
            }
            var mapBytes = textures[0].EncodeToPNG();

            File.WriteAllBytes(PlanetFactory.DataPath + name + "_map_.png", mapBytes);

            mapBytes = textures[1].EncodeToPNG();
            File.WriteAllBytes(PlanetFactory.DataPath + name + "_bump_.png", mapBytes);

            var normalMap = PFUtil.BumpToNormalMap(textures[1], 9);

            mapBytes = normalMap.EncodeToPNG();
            File.WriteAllBytes(PlanetFactory.DataPath + name + "_normal_.png", mapBytes);
        }
예제 #9
0
        public static void LoadPQS(string bodyName)
        {
            var localGameObject = PFUtil.FindLocal(bodyName);// localSpace.transform.FindChild(body.name).gameObject;

            print("load config");
            print(typeof(PQSMod).AssemblyQualifiedName);
            var root = ConfigNode.Load(DataPath + bodyName + ".cfg");

            var pqs = localGameObject.GetComponentInChildren <PQS>();

            //Remove PQS cities. TODO:Refactor.
            var mods = localGameObject.GetComponentsInChildren <PQSMod>(true);

            foreach (var mod in mods)
            {
                if (mod.GetType().ToString().Contains("PQSCity"))
                {
                    mod.modEnabled = false;
                    mod.gameObject.SetActive(false);
                    Destroy(mod.gameObject);
                    print("Removed PQSCity:");
                }
            }


            for (int ni = 0; ni < root.nodes.Count; ni++)
            {
                var node = root.nodes[ni];

                if (!node.name.ToLower().StartsWith("pqs"))
                {
                    continue;
                }

                var componentTypeStr = node.name;
                var componentType    = Type.GetType(componentTypeStr + ", Assembly-CSharp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
                if (componentType == null)
                {
                    componentType = Type.GetType(componentTypeStr + ", PlanetFactory, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
                }
                if (componentType == null)
                {
                    print("Cant find PQSMod type:" + componentTypeStr);
                    continue;
                }
                var component = localGameObject.GetComponentInChildren(componentType);
                if (component == null)
                {
                    //print("pqs not found");
                    if (node.HasValue("modEnabled"))
                    {
                        //print("is pqs mod " + node.GetValue("modEnabled"));
                        if (node.GetValue("modEnabled").ToLower() == "true")
                        {
                            //print("Adding PQSMOD:" + componentTypeStr);
                            var newgob       = new GameObject();
                            var newComponent = (PQSMod)newgob.AddComponent(componentType);

                            newgob.transform.parent = pqs.gameObject.transform;
                            newComponent.sphere     = pqs;
                            component = newComponent;
                        }
                    }
                }

                if (component != null)
                {
                    print("Loading Config PQS:");// + Dump.GetGobPath(component.gameObject));
                    LoadConfiguration(component, node);

                    try
                    {
                        //print("Rebuilding");
                        var mod = (PQSMod)component;
                        mod.RebuildSphere();
                    }
                    catch
                    {
                        //print("failed");
                    }
                }
            }
            if (pqs != null)
            {
                pqs.RebuildSphere();
            }
        }
예제 #10
0
        //Overlay a .CFG on a given object.
        public static void LoadConfiguration(System.Object obj, ConfigNode node)
        {
            var t = obj.GetType();

            var config = ConfigToDict(node);

            foreach (var key in config.Keys)
            {
                var field = t.GetField(key);
                //print(field);
                if (field != null)
                {
                    //print(field.FieldType.ToString().ToLower() +" Was "+field.GetValue(obj));
                    switch (field.FieldType.ToString().ToLower())
                    {
                    case "system.string":
                        field.SetValue(obj, config[key]);
                        break;

                    case "system.single":
                        field.SetValue(obj, float.Parse(config[key]));
                        break;

                    case "system.double":
                        field.SetValue(obj, double.Parse(config[key]));
                        break;

                    case "system.int32":
                        field.SetValue(obj, int.Parse(config[key]));
                        break;

                    case "system.uint32":
                        field.SetValue(obj, uint.Parse(config[key]));
                        break;

                    case "system.boolean":
                        field.SetValue(obj, bool.Parse(config[key]));
                        break;

                    case "unityengine.color":
                        field.SetValue(obj, ConfigNode.ParseColor(config[key]));
                        break;

                    case "unityengine.vector3":
                        field.SetValue(obj, ConfigNode.ParseVector3(config[key]));
                        break;

                    case "unityengine.animationcurve":
                        field.SetValue(obj, ParseCurve(node.GetNode(key)));
                        break;

                    case "unityengine.gradient":
                        field.SetValue(obj, new Gradient());
                        break;

                    case "orbit":
                        field.SetValue(obj, ParseOrbit(node.GetNode(key)));
                        break;

                    case "pqsmod_pfheightcolor+landclass[]":
                        int i   = 0;
                        var lcs = new List <PQSMod_PFHeightColor.LandClass>();
                        //print("parse landclass");
                        var pn = node.GetNode(key);
                        while (true)
                        {
                            var n = pn.GetNode("LandClass", i);
                            if (n != null)
                            {
                                var nlc = new PQSMod_PFHeightColor.LandClass();
                                LoadConfiguration(nlc, n);
                                lcs.Add(nlc);
                            }
                            else
                            {
                                break;
                            }
                            i++;
                        }
                        field.SetValue(obj, lcs.ToArray());
                        break;

                    case "mapso":
                        //print("Loading map:"+config[key]);
                        if (config[key].ToLower() == "null")
                        {
                            field.SetValue(obj, null);
                        }
                        else
                        {
                            var colorTexture = PFUtil.LoadTexture(DataPath + config[key]);
                            var mapso        = (MapSO)ScriptableObject.CreateInstance(typeof(MapSO));
                            mapso.CreateMap(MapSO.MapDepth.RGBA, colorTexture);
                            field.SetValue(obj, mapso);
                        }
                        break;
                    }
                }
            }
        }