/// <summary>
 /// Adds flight data for the given body.  Creates new entry if one doesn't yet exist, or updates one if it exists
 /// </summary>
 /// <param name="name">Name of the body as given by Vessel.mainBody.name</param>
 /// <param name="atmosphere">Flight data value for Atmosphere</param>
 /// <param name="space">Flight data value for Space</param>
 public void AddBodyData(string name, float dataAtmosphere, float dataSpace, int timeAtmosphere, int timeSpace)
 {
     if (dataBodies == null)
     {
         dataBodies = new List<FlightDataBody>();
         FlightDataBody body = new FlightDataBody();
         body.bodyName = name;
         body.dataAtmosphere = dataAtmosphere;
         body.dataSpace = dataSpace;
         body.flightTimeAtmosphere = timeAtmosphere;
         body.flightTimeSpace = timeSpace;
         dataBodies.Add(body);
     }
     else
     {
         FlightDataBody body = dataBodies.Find(s => s.bodyName == name);
         if (body != null)
         {
             body.dataAtmosphere = dataAtmosphere;
             body.dataSpace = dataSpace;
             body.flightTimeAtmosphere = timeAtmosphere;
             body.flightTimeSpace = timeSpace;
         }
         else
         {
             body = new FlightDataBody();
             body.bodyName = name;
             body.dataAtmosphere = dataAtmosphere;
             body.dataSpace = dataSpace;
             body.flightTimeAtmosphere = timeAtmosphere;
             body.flightTimeSpace = timeSpace;
             dataBodies.Add(body);
         }
     }
 }
 /// <summary>
 /// Adds flight data for the given body.  Creates new entry if one doesn't yet exist, or updates one if it exists
 /// </summary>
 /// <param name="name">Name of the body as given by Vessel.mainBody.name</param>
 /// <param name="atmosphere">Flight data value for Atmosphere</param>
 /// <param name="space">Flight data value for Space</param>
 public void AddBodyData(string name, float dataAtmosphere, float dataSpace, int timeAtmosphere, int timeSpace)
 {
     if (dataBodies == null)
     {
         dataBodies = new List <FlightDataBody>();
         FlightDataBody body = new FlightDataBody();
         body.bodyName             = name;
         body.dataAtmosphere       = dataAtmosphere;
         body.dataSpace            = dataSpace;
         body.flightTimeAtmosphere = timeAtmosphere;
         body.flightTimeSpace      = timeSpace;
         dataBodies.Add(body);
     }
     else
     {
         FlightDataBody body = dataBodies.Find(s => s.bodyName == name);
         if (body != null)
         {
             body.dataAtmosphere       = dataAtmosphere;
             body.dataSpace            = dataSpace;
             body.flightTimeAtmosphere = timeAtmosphere;
             body.flightTimeSpace      = timeSpace;
         }
         else
         {
             body                      = new FlightDataBody();
             body.bodyName             = name;
             body.dataAtmosphere       = dataAtmosphere;
             body.dataSpace            = dataSpace;
             body.flightTimeAtmosphere = timeAtmosphere;
             body.flightTimeSpace      = timeSpace;
             dataBodies.Add(body);
         }
     }
 }
        public double GetFlightTimeForScope(String scope)
        {
            FlightDataBody dataBody = flightData.GetFlightData(scope);

            if (dataBody == null)
            {
                return(0);
            }
            else
            {
                return(dataBody.flightTime);
            }
        }
        public double GetBaseFailureRateForScope(String scope)
        {
            // Since the Base Failure Rate does not change during the lifetime of a part
            // we cache that data internally so as to not have to call the Reliability module
            // constantly.  Therefore here we are returning what we have if we have it,
            // or else getting it from the Reliability module and caching that for next time.
            scope = scope.ToLower().Trim();
//            LogFormatted_DebugOnly(String.Format("TestFlightCore: GetBaseFailureRateForScope({0})",scope));
            if (baseFailureRate == null)
            {
//                LogFormatted_DebugOnly("BaseFailureRate data is invalid");
                return(TestFlightUtil.MIN_FAILURE_RATE);
            }
            if (baseFlightData == null)
            {
//                LogFormatted_DebugOnly("baseFlightData is invalid");
                return(TestFlightUtil.MIN_FAILURE_RATE);
            }
            if (baseFailureRate.ContainsKey(scope))
            {
//                LogFormatted_DebugOnly("TestFlightCore: Returning cached Base Failure Rate");
                return(baseFailureRate[scope]);
            }
            else
            {
//                LogFormatted_DebugOnly("TestFlightCore: Calculating Base Failure Rate from Reliability modules");
                double         totalBFR = 0;
                double         data     = 0;
                FlightDataBody body     = baseFlightData.GetFlightData(scope);
                if (body != null)
                {
                    data = body.flightData;
                }

                List <ITestFlightReliability> reliabilityModules = TestFlightUtil.GetReliabilityModules(this.part);
                if (reliabilityModules == null)
                {
                    return(TestFlightUtil.MIN_FAILURE_RATE);
                }

                foreach (ITestFlightReliability rm in reliabilityModules)
                {
                    totalBFR += rm.GetBaseFailureRateForScope(data, scope);
                }
                totalBFR = Mathf.Max((float)totalBFR, (float)TestFlightUtil.MIN_FAILURE_RATE);
                baseFailureRate.Add(scope, totalBFR);
                return(totalBFR);
            }
        }
        public double GetInitialFlightDataforScope(String scope)
        {
            if (baseFlightData == null)
            {
                return(0);
            }

            FlightDataBody dataBody = baseFlightData.GetFlightData(scope);

            if (dataBody == null)
            {
                return(0);
            }
            else
            {
                return(dataBody.flightData);
            }
        }
        public double GetFlightDataForScope(String scope)
        {
            if (flightData == null)
            {
                Log("FlightData is invalid");
                return(0);
            }
            FlightDataBody dataBody = flightData.GetFlightData(scope);

            if (dataBody == null)
            {
                return(0);
            }
            else
            {
                return(dataBody.flightData);
            }
        }
        public double ModifyFlightDataForScope(double modifier, String scope, bool additive)
        {
            if (flightData == null)
            {
                return(0);
            }

            FlightDataBody bodyData = flightData.GetFlightData(scope);

            if (bodyData == null)
            {
                if (!additive)
                {
                    return(0);
                }
                modifier = ApplyFlightDataMultiplier(modifier);

                if (modifier >= dataCap)
                {
                    modifier = dataCap;
                }

                flightData.AddFlightData(scope, modifier, 0);
                return(modifier);
            }

            if (additive)
            {
                modifier             = ApplyFlightDataMultiplier(modifier);
                bodyData.flightData += modifier;
            }
            else
            {
                bodyData.flightData *= modifier;
            }

            if (bodyData.flightData >= dataCap)
            {
                bodyData.flightData = dataCap;
            }

            flightData.AddFlightData(scope, bodyData.flightData, bodyData.flightTime);
            return(bodyData.flightData);
        }
        public static FlightDataBody FromString(string s)
        {
            FlightDataBody bodyData = null;

            string[] sections = s.Split(new char[1] {
                ','
            });
            if (sections.Length == 5)
            {
                bodyData                      = new FlightDataBody();
                bodyData.bodyName             = sections[0];
                bodyData.dataSpace            = float.Parse(sections[1]);
                bodyData.dataAtmosphere       = float.Parse(sections[2]);
                bodyData.flightTimeAtmosphere = int.Parse(sections[3]);
                bodyData.flightTimeSpace      = int.Parse(sections[4]);
            }

            return(bodyData);
        }
        public void Load(ConfigNode node)
        {
            Debug.Log("FlightData Load");
            Debug.Log(node.ToString());
            if (node.HasValue("dataDeepSpace"))
            {
                dataDeepSpace = float.Parse(node.GetValue("dataDeepSpace"));
            }
            else
            {
                dataDeepSpace = 0.0f;
            }

            if (node.HasValue("flightTimeDeepSpace"))
            {
                flightTimeDeepSpace = int.Parse(node.GetValue("flightTimeDeepSpace"));
            }
            else
            {
                flightTimeDeepSpace = 0;
            }

            if (node.HasNode("bodyData"))
            {
                if (dataBodies == null)
                {
                    dataBodies = new List <FlightDataBody>();
                }
                else
                {
                    dataBodies.Clear();
                }
                foreach (ConfigNode bodyNode in node.GetNodes("bodyData"))
                {
                    FlightDataBody body = new FlightDataBody();
                    body.Load(bodyNode);
                    dataBodies.Add(body);
                }
            }
        }
        public void Load(ConfigNode node)
        {
            Debug.Log("FlightData Load");
            Debug.Log(node.ToString());
            if (node.HasValue("dataDeepSpace"))
                dataDeepSpace = float.Parse(node.GetValue("dataDeepSpace"));
            else
                dataDeepSpace = 0.0f;

            if (node.HasValue("flightTimeDeepSpace"))
                flightTimeDeepSpace = int.Parse(node.GetValue("flightTimeDeepSpace"));
            else
                flightTimeDeepSpace = 0;

            if (node.HasNode("bodyData"))
            {
                if (dataBodies == null)
                    dataBodies = new List<FlightDataBody>();
                else
                    dataBodies.Clear();
                foreach (ConfigNode bodyNode in node.GetNodes("bodyData"))
                {
                    FlightDataBody body = new FlightDataBody();
                    body.Load(bodyNode);
                    dataBodies.Add(body);
                }
            }
        }
        public static FlightDataBody FromString(string s)
        {
            FlightDataBody bodyData = null;

            string[] sections = s.Split(new char[1] { ',' });
            if (sections.Length == 5)
            {
                bodyData = new FlightDataBody();
                bodyData.bodyName = sections[0];
                bodyData.dataSpace = float.Parse(sections[1]);
                bodyData.dataAtmosphere = float.Parse(sections[2]);
                bodyData.flightTimeAtmosphere = int.Parse(sections[3]);
                bodyData.flightTimeSpace = int.Parse(sections[4]);
            }

            return bodyData;
        }