Exemple #1
0
 protected void saveState()
 {
     if (HighLogic.LoadedSceneIsFlight)
     {
         ConfigNode config   = PluginHelper.getPluginSaveFile();
         string     vesselID = vessel.id.ToString();
         if (config.HasNode("VESSEL_SEISMIC_PROBE_" + vesselID))
         {
             ConfigNode probe_node = config.GetNode("VESSEL_SEISMIC_PROBE_" + vesselID);
             if (probe_node.HasValue("is_active"))
             {
                 probe_node.SetValue("is_active", probeIsEnabled.ToString());
             }
             else
             {
                 probe_node.AddValue("is_active", probeIsEnabled.ToString());
             }
             if (probe_node.HasValue("celestial_body"))
             {
                 probe_node.SetValue("celestial_body", vessel.mainBody.flightGlobalsIndex.ToString());
             }
             else
             {
                 probe_node.AddValue("celestial_body", vessel.mainBody.flightGlobalsIndex.ToString());
             }
         }
         else
         {
             ConfigNode probe_node = config.AddNode("VESSEL_SEISMIC_PROBE_" + vesselID);
             probe_node.AddValue("is_active", probeIsEnabled.ToString());
             probe_node.AddValue("celestial_body", vessel.mainBody.flightGlobalsIndex.ToString());
         }
         config.Save(PluginHelper.getPluginSaveFilePath());
     }
 }
Exemple #2
0
        public static ConfigNode getPluginSaveFile()
        {
            ConfigNode config = ConfigNode.Load(PluginHelper.getPluginSaveFilePath());

            if (config == null)
            {
                config = new ConfigNode();
                config.AddValue("writtenat", DateTime.Now.ToString());
                config.Save(PluginHelper.getPluginSaveFilePath());
            }
            return(config);
        }
Exemple #3
0
 protected override void cleanUpScienceData()
 {
     if (science_vess_ref != null)
     {
         ConfigNode config = PluginHelper.getPluginSaveFile();
         if (config.HasNode("SEISMIC_SCIENCE_" + vessel.mainBody.name.ToUpper()))
         {
             ConfigNode planet_data = config.GetNode("SEISMIC_SCIENCE_" + vessel.mainBody.name.ToUpper());
             if (planet_data.HasNode(science_vess_ref))
             {
                 ConfigNode impact_node = planet_data.GetNode(science_vess_ref);
                 if (impact_node.HasValue("transmitted"))
                 {
                     impact_node.SetValue("transmitted", "True");
                 }
                 config.Save(PluginHelper.getPluginSaveFilePath());
             }
         }
     }
 }
Exemple #4
0
        public override void OnFixedUpdate()
        {
            activeCount++;


            if (IsEnabled)
            {
                List <Part> vesselparts = vessel.parts;
                float       electrical_current_available = 0;
                for (int i = 0; i < vesselparts.Count; ++i)
                {
                    Part           cPart = vesselparts.ElementAt(i);
                    PartModuleList pml   = cPart.Modules;
                    for (int j = 0; j < pml.Count; ++j)
                    {
                        var curFNGen    = pml.GetModule(j) as FNGenerator;
                        var curMwRec    = pml.GetModule(j) as MicrowavePowerReceiver;
                        var curSolarPan = pml.GetModule(j) as ModuleDeployableSolarPanel;
                        if (curFNGen != null)
                        {
                            float consumeMJ = curFNGen.getMaxPowerOutput() * TimeWarp.fixedDeltaTime;
                            float cvalue    = consumeFNResource(consumeMJ, FNResourceManager.FNRESOURCE_MEGAJOULES);
                            electrical_current_available = cvalue * 1000 / TimeWarp.fixedDeltaTime;
                            nuclear = true;
                        }
                        else if (curMwRec != null && nuclear == false)
                        {
                            //electrical_current_available = curMwRec.powerInput;
                            part.RequestResource("ElectricCharge", electrical_current_available * TimeWarp.fixedDeltaTime);
                            microwave = true;
                        }
                        else if (curSolarPan != null && nuclear == false && microwave == false)
                        {
                            electrical_current_available += curSolarPan.flowRate;
                            part.RequestResource("ElectricCharge", electrical_current_available * TimeWarp.fixedDeltaTime);
                            solar = true;
                        }
                    }
                }
                inputPower = electrical_current_available;
            }
            else
            {
                inputPower = 0;
            }

            if (activeCount % 1000 == 9)
            {
                ConfigNode config  = PluginHelper.getPluginSaveFile();
                string     genType = "undefined";

                //float inputPowerFixedAlt = (float) ((double)inputPower * (Math.Pow(FlightGlobals.Bodies[0].GetAltitude(vessel.transform.position), 2)) / PluginHelper.FIXED_SAT_ALTITUDE / PluginHelper.FIXED_SAT_ALTITUDE);
                float inputPowerFixedAlt = 0;
                if (nuclear == true)
                {
                    inputPowerFixedAlt = inputPower;
                    //print ("warp: nuclear inputPower " + inputPowerFixedAlt);
                    genType = "nuclear";
                }
                else if (microwave == true)
                {
                    inputPowerFixedAlt = inputPower;
                    //print ("warp: relay inputPower " + inputPowerFixedAlt);
                    genType = "relay";
                }
                else if (solar == true)
                {
                    //inputPowerFixedAlt = inputPower / PluginHelper.getSatFloatCurve ().Evaluate ((float)FlightGlobals.Bodies [0].GetAltitude (vessel.transform.position));
                    //print ("warp: solar inputPower " + inputPowerFixedAlt);
                    genType = "solar";
                }

                if (genType != "undefined")
                {
                    string vesselIDSolar = vessel.id.ToString();
                    string outputPower   = inputPowerFixedAlt.ToString("0.000");
                    if (!config.HasValue(vesselIDSolar))
                    {
                        config.AddValue(vesselIDSolar, outputPower);
                    }
                    else
                    {
                        config.SetValue(vesselIDSolar, outputPower);
                    }

                    if (!config.HasValue(vesselIDSolar + "type"))
                    {
                        config.AddValue(vesselIDSolar + "type", genType);
                    }
                    else
                    {
                        config.SetValue(vesselIDSolar + "type", genType);
                    }

                    config.Save(PluginHelper.getPluginSaveFilePath());
                }
            }
        }
        public void onVesselAboutToBeDestroyed()
        {
            print("[KSP Interstellar] Handling Impactor");
            int body = vessel.mainBody.flightGlobalsIndex;
            //print(vessel.srf_velocity.magnitude);
            ConfigNode config          = PluginHelper.getPluginSaveFile();
            Vector3d   net_vector      = Vector3d.zero;
            bool       first           = true;
            double     net_science     = 0;
            double     initial_science = 0;

            foreach (Vessel conf_vess in FlightGlobals.Vessels)
            {
                String conf_vess_ID = conf_vess.id.ToString();
                if (config.HasNode("VESSEL_SEISMIC_PROBE_" + conf_vess_ID))
                {
                    ConfigNode probe_node = config.GetNode("VESSEL_SEISMIC_PROBE_" + conf_vess_ID);
                    bool       is_active  = false;
                    int        planet     = 0;
                    if (probe_node.HasValue("is_active"))
                    {
                        is_active = bool.Parse(probe_node.GetValue("is_active"));
                    }
                    if (probe_node.HasValue("celestial_body"))
                    {
                        planet = int.Parse(probe_node.GetValue("celestial_body"));
                    }
                    double   theta          = vessel.longitude;
                    double   phi            = vessel.latitude;
                    Vector3d up             = vessel.mainBody.GetSurfaceNVector(phi, theta).normalized;
                    double   surface_height = vessel.mainBody.pqsController.GetSurfaceHeight(QuaternionD.AngleAxis(theta, Vector3d.down) * QuaternionD.AngleAxis(phi, Vector3d.forward) * Vector3d.right) - vessel.mainBody.Radius;
                    double   height_diff    = vessel.pqsAltitude - surface_height;
                    // record science if we have crashed into the surface at velocity > 40m/s
                    if (is_active && planet == body && vessel.heightFromSurface <= 0.75 && vessel.srf_velocity.magnitude > 40 && height_diff <= 1)
                    {
                        // do sciency stuff
                        Vector3d surface_vector = (conf_vess.transform.position - FlightGlobals.Bodies[body].transform.position);
                        surface_vector = surface_vector.normalized;
                        if (first)
                        {
                            first           = false;
                            net_vector      = surface_vector;
                            net_science     = 50 * PluginHelper.getImpactorScienceMultiplier(body);
                            initial_science = net_science;
                        }
                        else
                        {
                            net_science += (1.0 - Vector3d.Dot(surface_vector, net_vector.normalized)) * 50 * PluginHelper.getImpactorScienceMultiplier(body);
                            net_vector   = net_vector + surface_vector;
                        }
                    }
                    else
                    {
                        if (vessel.heightFromSurface > 0.5)
                        {
                            print("[KSP Interstellar] Impactor: Ignored due to vessel being destroyed at too high altitude.");
                        }
                        if (vessel.srf_velocity.magnitude <= 40)
                        {
                            print("[KSP Interstellar] Impactor: Ignored due to vessel being at too low velocity.");
                        }
                    }
                }
            }
            net_science = Math.Min(net_science, initial_science * 3.5); // no more than 3.5x boost to science by using multiple detectors
            if (net_science > 0 && !double.IsInfinity(net_science) && !double.IsNaN(net_science))
            {
                ConfigNode science_node;
                int        science_experiment_number = 0;
                if (config.HasNode("SEISMIC_SCIENCE_" + vessel.mainBody.name.ToUpper()))
                {
                    science_node = config.GetNode("SEISMIC_SCIENCE_" + vessel.mainBody.name.ToUpper());
                    science_experiment_number = science_node.nodes.Count;
                }
                else
                {
                    science_node = config.AddNode("SEISMIC_SCIENCE_" + vessel.mainBody.name.ToUpper());
                    science_node.AddValue("name", "interstellarseismicarchive");
                }


                double science_coeff = -science_experiment_number / 2.0;
                net_science = net_science * Math.Exp(science_coeff);
                ScreenMessages.PostScreenMessage("Impact Recorded, science report can now be accessed from one of your accelerometers deployed on this body.", 5f, ScreenMessageStyle.UPPER_CENTER);
                //science_node.AddValue(vessel.id.ToString(), net_science);
                if (!science_node.HasNode("IMPACT_" + vessel.id.ToString()))
                {
                    ConfigNode impact_node = new ConfigNode("IMPACT_" + vessel.id.ToString());
                    impact_node.AddValue("transmitted", "False");
                    impact_node.AddValue("vesselname", vessel.vesselName);
                    impact_node.AddValue("science", net_science);
                    impact_node.AddValue("number", (science_experiment_number + 1).ToString("0"));
                    science_node.AddNode(impact_node);
                }
                //if (ResearchAndDevelopment.Instance != null) {
                //    ResearchAndDevelopment.Instance.Science = ResearchAndDevelopment.Instance.Science + (float)net_science;
                //}

                config.Save(PluginHelper.getPluginSaveFilePath());
            }
        }
        public override void OnFixedUpdate()
        {
            activeCount++;
            nuclear_power         = 0;
            solar_power           = 0;
            displayed_solar_power = 0;
            if (IsEnabled && !relay)
            {
                foreach (FNGenerator generator in generators)
                {
                    if (generator.isActive())
                    {
                        IThermalSource thermal_source = generator.getThermalSource();
                        if (thermal_source != null && !thermal_source.IsVolatileSource)
                        {
                            double output = generator.getMaxPowerOutput();
                            if (thermal_source is InterstellarFusionReactor)
                            {
                                output = output * 0.95;
                            }
                            output = output * transmitPower / 100.0;
                            double gpower = consumeFNResource(output * TimeWarp.fixedDeltaTime, FNResourceManager.FNRESOURCE_MEGAJOULES);
                            nuclear_power += gpower * 1000 / TimeWarp.fixedDeltaTime;
                        }
                    }
                }

                foreach (ModuleDeployableSolarPanel panel in panels)
                {
                    double output          = panel.flowRate;
                    double spower          = part.RequestResource("ElectricCharge", output * TimeWarp.fixedDeltaTime);
                    double inv_square_mult = Math.Pow(Vector3d.Distance(FlightGlobals.Bodies[PluginHelper.REF_BODY_KERBIN].transform.position, FlightGlobals.Bodies[PluginHelper.REF_BODY_KERBOL].transform.position), 2) / Math.Pow(Vector3d.Distance(vessel.transform.position, FlightGlobals.Bodies[PluginHelper.REF_BODY_KERBOL].transform.position), 2);
                    displayed_solar_power += spower / TimeWarp.fixedDeltaTime;
                    //scale solar power to what it would be in Kerbin orbit for file storage
                    solar_power += spower / TimeWarp.fixedDeltaTime / inv_square_mult;
                }
            }

            if (double.IsInfinity(nuclear_power) || double.IsNaN(nuclear_power))
            {
                nuclear_power = 0;
            }

            if (double.IsInfinity(solar_power) || double.IsNaN(solar_power))
            {
                solar_power = 0;
            }

            if (activeCount % 1000 == 9)
            {
                ConfigNode config   = PluginHelper.getPluginSaveFile();
                string     vesselID = vessel.id.ToString();
                if (config.HasNode("VESSEL_MICROWAVE_POWER_" + vesselID))
                {
                    ConfigNode power_node = config.GetNode("VESSEL_MICROWAVE_POWER_" + vesselID);
                    if (power_node.HasValue("nuclear_power"))
                    {
                        power_node.SetValue("nuclear_power", MicrowavePowerTransmitter.getEnumeratedNuclearPowerForVessel(vessel).ToString("E"));
                    }
                    else
                    {
                        power_node.AddValue("nuclear_power", MicrowavePowerTransmitter.getEnumeratedNuclearPowerForVessel(vessel).ToString("E"));
                    }
                    if (power_node.HasValue("solar_power"))
                    {
                        power_node.SetValue("solar_power", MicrowavePowerTransmitter.getEnumeratedSolarPowerForVessel(vessel).ToString("E"));
                    }
                    else
                    {
                        power_node.AddValue("solar_power", MicrowavePowerTransmitter.getEnumeratedSolarPowerForVessel(vessel).ToString("E"));
                    }
                }
                else
                {
                    ConfigNode power_node = config.AddNode("VESSEL_MICROWAVE_POWER_" + vesselID);
                    power_node.AddValue("nuclear_power", MicrowavePowerTransmitter.getEnumeratedNuclearPowerForVessel(vessel).ToString("E"));
                    power_node.AddValue("solar_power", MicrowavePowerTransmitter.getEnumeratedSolarPowerForVessel(vessel).ToString("E"));
                }

                if (config.HasNode("VESSEL_MICROWAVE_RELAY_" + vesselID))
                {
                    ConfigNode relay_node = config.GetNode("VESSEL_MICROWAVE_RELAY_" + vesselID);
                    if (relay_node.HasValue("relay"))
                    {
                        relay_node.SetValue("relay", MicrowavePowerTransmitter.vesselIsRelay(vessel).ToString());
                    }
                    else
                    {
                        relay_node.AddValue("relay", MicrowavePowerTransmitter.vesselIsRelay(vessel).ToString());
                    }
                }
                else
                {
                    ConfigNode relay_node = config.AddNode("VESSEL_MICROWAVE_RELAY_" + vesselID);
                    relay_node.AddValue("relay", MicrowavePowerTransmitter.vesselIsRelay(vessel).ToString());
                }

                config.Save(PluginHelper.getPluginSaveFilePath());
            }
            activeCount++;
        }
        public void onVesselAboutToBeDestroyed(EventReport report)
        {
            Debug.Log("[KSP Interstellar] Handling Impactor");

            ConfigNode config;
            ConfigNode science_node;

            Vessel vessel = report.origin.vessel;
            float  vesselMass;
            int    science_experiment_number = 0;

            string vessel_impact_node_string  = string.Concat("IMPACT_", vessel.id.ToString());
            string vessel_seismic_node_string = string.Concat("SEISMIC_SCIENCE_", vessel.mainBody.name.ToUpper());

            // Do nothing if we don't have a vessel.  This seems improbable, but who knows.
            if (vessel == null)
            {
                Debug.Log("[KSP Interstellar] Impactor: Ignored because the vessel is undefined.");
                return;
            }

            // Do nothing if we have recorded an impact less than 10 physics updates ago.  This probably means this call
            // is a duplicate of a previous call.
            if (Planetarium.GetUniversalTime() - this.lastImpactTime < TimeWarp.fixedDeltaTime * 10f)
            {
                Debug.Log("[KSP Interstellar] Impactor: Ignored because we've just recorded an impact.");
                return;
            }

            // Do nothing if we are a debris item less than ten physics-updates old.  That probably means we were
            // generated by a recently-recorded impact.
            if (vessel.vesselType == VesselType.Debris && vessel.missionTime < Time.fixedDeltaTime * 10f)
            {
                Debug.Log("[KSP Interstellar] Impactor: Ignored due to vessel being brand-new debris.");
                return;
            }

            vesselMass = vessel.GetTotalMass();

            // Do nothing if we aren't very near the terrain.  Note that using heightFromTerrain probably allows
            // impactors against the ocean floor... good luck.
            float vesselDimension = vessel.MOI.magnitude / vesselMass;

            if (vessel.heightFromSurface > Mathf.Max(vesselDimension, 0.75f))
            {
                Debug.Log("[KSP Interstellar] Impactor: Ignored due to vessel altitude being too high.");
                return;
            }

            // Do nothing if we aren't impacting the surface.
            if (!(
                    report.other.ToLower().Contains(string.Intern("surface")) ||
                    report.other.ToLower().Contains(string.Intern("terrain")) ||
                    report.other.ToLower().Contains(vessel.mainBody.name.ToLower())
                    ))
            {
                Debug.Log("[KSP Interstellar] Impactor: Ignored due to not impacting the surface.");
                return;
            }

            /*
             * NOTE: This is a deviation from current KSPI behavior.  KSPI currently registers an impact over 40 m/s
             * regardless of its mass; this means that trivially light impactors (single instruments, even) could
             * trigger the experiment.
             *
             * The guard below requires that the impactor have at least as much vertical impact energy as a 1 Mg
             * object traveling at 40 m/s.  This means that nearly-tangential impacts or very light impactors will need
             * to be much faster, but that heavier impactors may be slower.
             *
             * */
            if (
                (Math.Pow(vessel.verticalSpeed, 2d) * vesselMass / 2d < 800d) &&
                (vessel.verticalSpeed > 20d)
                )
            {
                Debug.Log("[KSP Interstellar] Impactor: Ignored due to vessel imparting too little impact energy.");
                return;
            }

            config = PluginHelper.getPluginSaveFile();
            if (config.HasNode(vessel_seismic_node_string))
            {
                science_node = config.GetNode(vessel_seismic_node_string);
                science_experiment_number = science_node.nodes.Count;

                if (science_node.HasNode(vessel_impact_node_string))
                {
                    Debug.Log("[KSP Interstellar] Impactor: Ignored because this vessel's impact has already been recorded.");
                    return;
                }
            }
            else
            {
                science_node = config.AddNode(vessel_seismic_node_string);
                science_node.AddValue("name", "interstellarseismicarchive");
            }

            int      body            = vessel.mainBody.flightGlobalsIndex;
            Vector3d net_vector      = Vector3d.zero;
            bool     first           = true;
            double   net_science     = 0;
            double   initial_science = 0;

            foreach (Vessel conf_vess in FlightGlobals.Vessels)
            {
                String vessel_probe_node_string = string.Concat("VESSEL_SEISMIC_PROBE_", conf_vess.id.ToString());

                if (config.HasNode(vessel_probe_node_string))
                {
                    ConfigNode probe_node = config.GetNode(vessel_probe_node_string);

                    // If the seismometer is inactive, skip it.
                    bool is_active = false;
                    if (probe_node.HasValue("is_active"))
                    {
                        bool.TryParse(probe_node.GetValue("is_active"), out is_active);
                        if (!is_active)
                        {
                            continue;
                        }
                    }

                    // If the seismometer is on another planet, skip it.
                    int planet = -1;
                    if (probe_node.HasValue("celestial_body"))
                    {
                        int.TryParse(probe_node.GetValue("celestial_body"), out planet);
                        if (planet != body)
                        {
                            continue;
                        }
                    }

                    // do sciency stuff
                    Vector3d surface_vector = (conf_vess.transform.position - FlightGlobals.Bodies[body].transform.position);
                    surface_vector = surface_vector.normalized;
                    if (first)
                    {
                        first           = false;
                        net_vector      = surface_vector;
                        net_science     = 50 * PluginHelper.getImpactorScienceMultiplier(body);
                        initial_science = net_science;
                    }
                    else
                    {
                        net_science += (1.0 - Vector3d.Dot(surface_vector, net_vector.normalized)) * 50 * PluginHelper.getImpactorScienceMultiplier(body);
                        net_vector   = net_vector + surface_vector;
                    }
                }
            }

            net_science = Math.Min(net_science, initial_science * 3.5); // no more than 3.5x boost to science by using multiple detectors
            if (net_science > 0 && !double.IsInfinity(net_science) && !double.IsNaN(net_science))
            {
                double science_coeff = -science_experiment_number / 2.0;
                net_science = net_science * Math.Exp(science_coeff);
                ScreenMessages.PostScreenMessage("Impact Recorded, science report can now be accessed from one of your accelerometers deployed on this body.", 5f, ScreenMessageStyle.UPPER_CENTER);
                this.lastImpactTime = Planetarium.GetUniversalTime();
                Debug.Log("[KSP Interstellar] Impactor: Impact registered!");

                ConfigNode impact_node = new ConfigNode(vessel_impact_node_string);
                impact_node.AddValue(string.Intern("transmitted"), bool.FalseString);
                impact_node.AddValue(string.Intern("vesselname"), vessel.vesselName);
                impact_node.AddValue(string.Intern("science"), net_science);
                impact_node.AddValue(string.Intern("number"), (science_experiment_number + 1).ToString("0"));
                science_node.AddNode(impact_node);

                config.Save(PluginHelper.getPluginSaveFilePath());
            }
        }