/// <summary>
        /// Consumes electric charge and return whether boiloff happens
        /// </summary>
        /// <returns></returns>
        public bool ConsumeCharge()
        {
            bool boiloff = false;

            if (CoolingEnabled && IsCoolable())
            {
                double chargeRequest = finalCoolingCost * TimeWarp.fixedDeltaTime;

                vessel.GetConnectedResourceTotals(PartResourceLibrary.Instance.GetDefinition("ElectricCharge").id, out double currentEC, out double maxEC);

                // only use EC if there is more then minResToLeave left
                double consumedEC = 0;
                if (currentEC > (chargeRequest + minResToLeave))
                {
                    consumedEC = part.RequestResource("ElectricCharge", chargeRequest);
                }

                double tolerance = 0.0001;
                if (consumedEC >= (chargeRequest - tolerance))
                {
                    boiloff       = false;
                    BoiloffStatus = Localizer.Format("#LOC_CryoTanks_ModuleCryoTank_Field_BoiloffStatus_Insulated");
                    CoolingStatus = Localizer.Format("#LOC_CryoTanks_ModuleCryoTank_Field_CoolingStatus_Cooling", finalCoolingCost.ToString("F2"));
                }
                else
                {
                    boiloff       = true;
                    BoiloffStatus = BoiloffUtils.FormatRate(GetTotalBoiloffRate() * fuelAmount * fluxScale);
                    CoolingStatus = Localizer.Format("#LOC_CryoTanks_ModuleCryoTank_Field_CoolingStatus_Uncooled");
                }
            }
            return(boiloff);
        }
        public void Start()
        {
            if (HighLogic.LoadedSceneIsFlight || HighLogic.LoadedSceneIsEditor)
            {
                ReloadDatabaseNodes();
                SetDebugMode(DebugMode);
            }

            if (HighLogic.LoadedSceneIsFlight)
            {
                /// Check to see if there's any boiloff resources on this part
                HasAnyBoiloffResource = false;
                foreach (BoiloffFuel fuel in fuels)
                {
                    if (BoiloffUtils.IsPartResourcePresent(fuel.fuelName, part))
                    {
                        HasAnyBoiloffResource = true;
                        fuel.Initialize();
                    }
                    else
                    {
                        fuel.fuelPresent = false;
                    }
                }
                /// if no resources turn off the UI
                if (!HasAnyBoiloffResource)
                {
                    Events["Disable"].guiActive       = false;
                    Events["Enable"].guiActive        = false;
                    Fields["BoiloffStatus"].guiActive = false;
                    return;
                }

                maxFuelAmount = GetTotalMaxResouceAmount();

                planetFlux = LongwaveFluxBaseline;
                solarFlux  = ShortwaveFluxBaseline;

                if (GetTotalCoolingCost() > 0.0)
                {
                    finalCoolingCost                  = maxFuelAmount / 1000.0 * GetTotalCoolingCost();
                    Events["Disable"].guiActive       = true;
                    Events["Enable"].guiActive        = true;
                    Events["Enable"].guiActiveEditor  = true;
                    Events["Disable"].guiActiveEditor = true;
                }
                // Catchup
                DoCatchup();
            }
        }
 /// <summary>
 /// Sets the boiloff state
 /// </summary>
 /// <param name="state">Whether boiloff is occuring or not</param>
 /// <return>The total cost of the boiloff</return>
 public double SetBoiloffState(bool isBoiling)
 {
     if (CoolingEnabled && IsCoolable())
     {
         if (isBoiling)
         {
             BoiloffOccuring = true;
             BoiloffStatus   = BoiloffUtils.FormatRate(GetTotalBoiloffRate() * fuelAmount * fluxScale);
             CoolingStatus   = Localizer.Format("#LOC_CryoTanks_ModuleCryoTank_Field_CoolingStatus_Uncooled");
         }
         else
         {
             BoiloffOccuring = false;
             BoiloffStatus   = String.Format("Insulated");
             CoolingStatus   = Localizer.Format("#LOC_CryoTanks_ModuleCryoTank_Field_CoolingStatus_Cooling", finalCoolingCost.ToString("F2"));
         }
         return((double)finalCoolingCost);
     }
     return(0d);
 }
        protected void FixedUpdate()
        {
            if (HighLogic.LoadedSceneIsFlight && HasAnyBoiloffResource)
            {
                fluxScale  = CalculateRadiativeEffects();
                fuelAmount = GetTotalResouceAmount();

                // If we have no fuel, no need to do any calculations
                if (fuelAmount == 0.0)
                {
                    BoiloffStatus      = Localizer.Format("#LOC_CryoTanks_ModuleCryoTank_Field_BoiloffStatus_NoFuel");
                    CoolingStatus      = Localizer.Format("#LOC_CryoTanks_ModuleCryoTank_Field_CoolingStatus_NoFuel");
                    currentCoolingCost = 0.0;
                    return;
                }

                // If the tank is not coolable we must boil off
                if (!IsCoolable())
                {
                    BoiloffOccuring    = true;
                    BoiloffStatus      = BoiloffUtils.FormatRate(GetTotalBoiloffRate() * fuelAmount * fluxScale);
                    currentCoolingCost = 0.0;
                }
                // else check for available power
                else
                {
                    if (!CoolingEnabled)
                    {
                        BoiloffOccuring    = true;
                        BoiloffStatus      = BoiloffUtils.FormatRate(GetTotalBoiloffRate() * fuelAmount * fluxScale);
                        CoolingStatus      = Localizer.Format("#LOC_CryoTanks_ModuleCryoTank_Field_CoolingStatus_Disabled");
                        currentCoolingCost = 0.0;
                    }
                    else
                    {
                        BoiloffOccuring    = ConsumeCharge();
                        currentCoolingCost = finalCoolingCost;
                    }
                }

                if (BoiloffOccuring)
                {
                    DoBoiloff();
                }
                if (part.vessel.missionTime > 0.0)
                {
                }
            }
            if (HighLogic.LoadedSceneIsFlight && DebugMode)
            {
                D_Albedo   = String.Format("{0:F4}", Albedo);
                D_Emiss    = String.Format("{0:F4}", part.emissiveConstant);
                D_InPlanet = String.Format("{0:F4}", planetFlux);
                D_InSolar  = String.Format("{0:F4}", solarFlux * Albedo);
                D_NetRad   = String.Format("{0:F4}", fluxScale);
            }
            if (HighLogic.LoadedSceneIsEditor && HasAnyBoiloffResource)
            {
                currentCoolingCost = GetTotalCoolingCost() * GetTotalMaxResouceAmount() / 1000d;
            }
        }
        public void Update()
        {
            if (HighLogic.LoadedSceneIsFlight && HasAnyBoiloffResource)
            {
                /// Show the insulation status field if there is a cooling cost
                if (IsCoolable())
                {
                    Fields["CoolingStatus"].guiActive = true;
                    if (Events["Enable"].active == CoolingEnabled || Events["Disable"].active != CoolingEnabled)
                    {
                        Events["Disable"].active = CoolingEnabled;
                        Events["Enable"].active  = !CoolingEnabled;
                    }
                }
                else
                {
                    Fields["CoolingStatus"].guiActive = false;
                    Events["Disable"].active          = false;
                    Events["Enable"].active           = false;
                }

                /// if there is no more fuel, hide the boiloff status
                if (fuelAmount == 0.0)
                {
                    Fields["BoiloffStatus"].guiActive = false;
                }
            }
            else if (HighLogic.LoadedSceneIsEditor)
            {
                /// Check for the presence of any resource
                HasAnyBoiloffResource = false;
                foreach (BoiloffFuel fuel in fuels)
                {
                    if (BoiloffUtils.IsPartResourcePresent(fuel.fuelName, part))
                    {
                        HasAnyBoiloffResource = true;
                        fuel.Initialize();
                    }
                    else
                    {
                        fuel.fuelPresent = false;
                    }
                }

                if (IsCoolable() && HasAnyBoiloffResource)
                {
                    Fields["CoolingStatus"].guiActiveEditor = true;
                    Fields["CoolingStatus"].guiActiveEditor = true;

                    double max = GetTotalMaxResouceAmount();

                    CoolingStatus = Localizer.Format("#LOC_CryoTanks_ModuleCryoTank_Field_CoolingStatus_Editor", (GetTotalCoolingCost() * (float)(max / 1000.0)).ToString("F2"));

                    Events["Disable"].guiActiveEditor = true;
                    Events["Enable"].guiActiveEditor  = true;

                    if (Events["Enable"].active == CoolingEnabled || Events["Disable"].active != CoolingEnabled)
                    {
                        Events["Disable"].active = CoolingEnabled;
                        Events["Enable"].active  = !CoolingEnabled;
                    }
                }
                else
                {
                    Fields["CoolingStatus"].guiActiveEditor = false;
                    Events["Disable"].guiActiveEditor       = false;
                    Events["Enable"].guiActiveEditor        = false;
                }
            }
        }