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++;
        }
Exemple #2
0
        public void calculateTransmitters()
        {
            unloaded_counter++;
            foreach (var vessel in FlightGlobals.Vessels)
            {
                // if vessek is offloaded to rails, parse file system
                if (vessel.state == Vessel.State.INACTIVE)
                {
                    if (unloaded_counter % 100 != 1)                // sometimes rebuild unloaded vessels as transmitters and relays
                    {
                        continue;
                    }
                    // parse transmitter
                    var trans_pers = new VesselMicrowavePersistence(vessel);
                    trans_pers.setNuclearPower(MicrowavePowerTransmitter.getEnumeratedNuclearPowerForVessel(vessel.protoVessel));
                    trans_pers.setSolarPower(MicrowavePowerTransmitter.getEnumeratedSolarPowerForVessel(vessel.protoVessel));

                    if (trans_pers.getAvailablePower() > 1.0)
                    {
                        transmitters[vessel] = trans_pers;
                    }
                    else
                    {
                        transmitters.Remove(vessel);
                    }
                    // parse relay
                    var persistence = new VesselRelayPersistence(vessel);
                    persistence.setActive(MicrowavePowerTransmitter.vesselIsRelay(vessel.protoVessel));
                    if (persistence.isActive())
                    {
                        relays[vessel] = persistence;
                    }
                    else
                    {
                        relays.Remove(vessel);
                    }
                    continue;
                }

                // if vessel is dead
                if (vessel.state == Vessel.State.DEAD)
                {
                    transmitters.Remove(vessel);
                    relays.Remove(vessel);
                    continue;
                }

                // if vessel is loaded
                var transes = vessel.FindPartModulesImplementing <MicrowavePowerTransmitter>();
                if (transes.Count > 0)
                {
                    var persistence = new VesselMicrowavePersistence(vessel);
                    persistence.setNuclearPower(MicrowavePowerTransmitter.getEnumeratedNuclearPowerForVessel(vessel));
                    persistence.setSolarPower(MicrowavePowerTransmitter.getEnumeratedSolarPowerForVessel(vessel));

                    if (persistence.getAvailablePower() > 1.0)
                    {
                        transmitters[vessel] = persistence;
                    }
                    else
                    {
                        transmitters.Remove(vessel);
                    }
                }

                if (MicrowavePowerTransmitter.vesselIsRelay(vessel))
                {
                    var persistence = new VesselRelayPersistence(vessel);
                    persistence.setActive(true);
                    if (persistence.isActive())
                    {
                        relays[vessel] = persistence;
                    }
                    else
                    {
                        relays.Remove(vessel);
                    }
                }
            }
        }