Merge pull request #59 from itwtx/master
Merge pull request #59 from itwtx/master

Optimized time formatting

--- a/Documents/CHANGES.txt
+++ b/Documents/CHANGES.txt
@@ -1,9 +1,15 @@
+1.0.18.1
+    Changed: The 'Torque' value in the editor is now precise to two decimal places.
+    
 1.0.18.0
     Added: Orbital readouts - "Speed at Periapsis" and "Speed at Apoapsis". (Padishar)
     Added: Manoeuvre readouts - "Post-burn Apoapsis" and "Post-burn Periapsis". (Padishar)
+    Added: Orbital readout - "Time to Atmosphere".
     Fixed: Synched the minimum simulation time sliders and stopped them from snapping back after 999ms. (saybur)
     Fixed: Added workaround for the bug in Vessel.horizontalSrfSpeed (Padishar)
     Fixed: Physically insignificant part mass was not being correctly cascaded down through multiple parent parts.
+    Fixed: Intake air demand calculation not working.
+    Fixed: Some build engineer settings labels do not scale with UI size.
 
 1.0.17.0
     Added: 'Mach Number' readout under the 'Surface' category and included it on the default surface HUD.

--- a/KerbalEngineer/EngineerGlobals.cs
+++ b/KerbalEngineer/EngineerGlobals.cs
@@ -25,7 +25,7 @@
         /// <summary>
         ///     Current version of the Kerbal Engineer assembly.
         /// </summary>
-        public const string ASSEMBLY_VERSION = "1.0.18.0";
+        public const string ASSEMBLY_VERSION = "1.0.18.1";
 
         private static string assemblyFile;
         private static string assemblyName;

--- a/KerbalEngineer/Flight/Readouts/Orbital/TimeToAtmosphere.cs
+++ b/KerbalEngineer/Flight/Readouts/Orbital/TimeToAtmosphere.cs
@@ -50,7 +50,7 @@
             String str;
             Orbit orbit = FlightGlobals.ship_orbit;
 
-            if (orbit.referenceBody.atmosphere && orbit.PeA < orbit.referenceBody.atmosphereDepth)
+			if (orbit.referenceBody.atmosphere && orbit.PeA < orbit.referenceBody.atmosphereDepth && orbit.ApA > orbit.referenceBody.atmosphereDepth)
             {
                 double tA = orbit.TrueAnomalyAtRadius(orbit.referenceBody.atmosphereDepth + orbit.referenceBody.Radius);
                 //log.buf.AppendFormat("tA = {0}\n", tA);
@@ -87,7 +87,7 @@
             else
             {
                 str = "---s";
-                //log.buf.AppendLine("no atmosphere or pe > atmosphere");
+                //log.buf.AppendLine("no atmosphere, pe > atmosphere, or ap < atmosphere");
             }
 
             //log.Flush();

--- a/KerbalEngineer/Flight/Readouts/ReadoutLibrary.cs
+++ b/KerbalEngineer/Flight/Readouts/ReadoutLibrary.cs
@@ -120,6 +120,7 @@
                 readouts.Add(new ImpactBiome());
 
                 // Vessel
+                readouts.Add(new Name());
                 readouts.Add(new DeltaVStaged());
                 readouts.Add(new DeltaVCurrent());
                 readouts.Add(new DeltaVTotal());
@@ -167,6 +168,8 @@
                 readouts.Add(new Rendezvous.OrbitalPeriod());
                 readouts.Add(new Rendezvous.SemiMajorAxis());
                 readouts.Add(new Rendezvous.SemiMinorAxis());
+                readouts.Add(new Rendezvous.RelativeRadialVelocity());
+                readouts.Add(new Rendezvous.TimeToRendezvous());
 
                 // Thermal
                 readouts.Add(new InternalFlux());

--- /dev/null
+++ b/KerbalEngineer/Flight/Readouts/Rendezvous/RelativeRadialVelocity.cs
@@ -1,1 +1,66 @@
+// 
+//     Kerbal Engineer Redux
+// 
+//     Copyright (C) 2014 CYBUTEK
+// 
+//     This program is free software: you can redistribute it and/or modify
+//     it under the terms of the GNU General Public License as published by
+//     the Free Software Foundation, either version 3 of the License, or
+//     (at your option) any later version.
+// 
+//     This program is distributed in the hope that it will be useful,
+//     but WITHOUT ANY WARRANTY; without even the implied warranty of
+//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//     GNU General Public License for more details.
+// 
+//     You should have received a copy of the GNU General Public License
+//     along with this program.  If not, see <http://www.gnu.org/licenses/>.
+// 
 
+#region Using Directives
+
+using KerbalEngineer.Extensions;
+using KerbalEngineer.Flight.Sections;
+
+#endregion
+
+namespace KerbalEngineer.Flight.Readouts.Rendezvous
+{
+    public class RelativeRadialVelocity : ReadoutModule
+    {
+        #region Constructors
+
+        public RelativeRadialVelocity()
+        {
+            this.Name = "Relative Radial Velocity";
+            this.Category = ReadoutCategory.GetCategory("Rendezvous");
+            this.HelpString = "Relative radial velocity between your vessel and the target object";
+            this.IsDefault = false;
+        }
+
+        #endregion
+
+        #region Methods: public
+
+        public override void Draw(SectionModule section)
+        {
+            if (RendezvousProcessor.ShowDetails)
+            {
+               this.DrawLine(RendezvousProcessor.RelativeRadialVelocity.ToSpeed(), section.IsHud);
+            }
+        }
+
+        public override void Reset()
+        {
+            FlightEngineerCore.Instance.AddUpdatable(RendezvousProcessor.Instance);
+        }
+
+        public override void Update()
+        {
+            RendezvousProcessor.RequestUpdate();
+        }
+
+        #endregion
+    }
+}
+

--- a/KerbalEngineer/Flight/Readouts/Rendezvous/RendezvousProcessor.cs
+++ b/KerbalEngineer/Flight/Readouts/Rendezvous/RendezvousProcessor.cs
@@ -135,6 +135,16 @@
         ///     Gets the target's time to periapsis.
         /// </summary>
         public static double TimeToPeriapsis { get; private set; }
+
+        /// <summary>
+        ///     Gets the relative radial velocity.
+        /// </summary>
+        public static double RelativeRadialVelocity { get; private set; }
+
+        /// <summary>
+        ///     Gets approximate (linearly) time to the minimum distance between objects.
+        /// </summary>
+        public static double TimeToRendezvous { get; private set; }
 
         /// <summary>
         ///     Gets and sets whether the updatable object should be updated.
@@ -193,6 +203,14 @@
 
             Distance = Vector3d.Distance(targetOrbit.pos, originOrbit.pos);
             OrbitalPeriod = targetOrbit.period;
+
+            // beware that the order/sign of coordinates is inconsistent across different exposed variables
+            // in particular, v below does not equal to FlightGlobals.ship_tgtVelocity
+            Vector3d x = targetOrbit.pos - originOrbit.pos;
+            Vector3d v = targetOrbit.vel - originOrbit.vel;
+            double xv = Vector3d.Dot(x, v);
+            TimeToRendezvous = - xv / Vector3d.SqrMagnitude(v);
+            RelativeRadialVelocity = xv / Vector3d.Magnitude(x);
         }
 
         private double CalcInterceptAngle()

--- /dev/null
+++ b/KerbalEngineer/Flight/Readouts/Rendezvous/TimeToRendezvous.cs
@@ -1,1 +1,65 @@
+// 
+//     Kerbal Engineer Redux
+// 
+//     Copyright (C) 2014 CYBUTEK
+// 
+//     This program is free software: you can redistribute it and/or modify
+//     it under the terms of the GNU General Public License as published by
+//     the Free Software Foundation, either version 3 of the License, or
+//     (at your option) any later version.
+// 
+//     This program is distributed in the hope that it will be useful,
+//     but WITHOUT ANY WARRANTY; without even the implied warranty of
+//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//     GNU General Public License for more details.
+// 
+//     You should have received a copy of the GNU General Public License
+//     along with this program.  If not, see <http://www.gnu.org/licenses/>.
+// 
 
+#region Using Directives
+
+using KerbalEngineer.Flight.Sections;
+using KerbalEngineer.Helpers;
+
+#endregion
+
+namespace KerbalEngineer.Flight.Readouts.Rendezvous
+{
+    public class TimeToRendezvous : ReadoutModule
+    {
+        #region Constructors
+
+        public TimeToRendezvous()
+        {
+            this.Name = "Time to Rendezvous";
+            this.Category = ReadoutCategory.GetCategory("Rendezvous");
+            this.HelpString = "Approximate (linearly) time to the minimum distance between objects";
+            this.IsDefault = false;
+        }
+
+        #endregion
+
+        #region Methods: public
+
+        public override void Draw(SectionModule section)
+        {
+            if (RendezvousProcessor.ShowDetails)
+            {
+               this.DrawLine(TimeFormatter.ConvertToString(RendezvousProcessor.TimeToRendezvous), section.IsHud);
+            }
+        }
+
+        public override void Reset()
+        {
+            FlightEngineerCore.Instance.AddUpdatable(RendezvousProcessor.Instance);
+        }
+
+        public override void Update()
+        {
+            RendezvousProcessor.RequestUpdate();
+        }
+
+        #endregion
+    }
+}

--- /dev/null
+++ b/KerbalEngineer/Flight/Readouts/Vessel/Name.cs
@@ -1,1 +1,55 @@
+// 
+//     Kerbal Engineer Redux
+// 
+//     Copyright (C) 2014 CYBUTEK
+// 
+//     This program is free software: you can redistribute it and/or modify
+//     it under the terms of the GNU General Public License as published by
+//     the Free Software Foundation, either version 3 of the License, or
+//     (at your option) any later version.
+// 
+//     This program is distributed in the hope that it will be useful,
+//     but WITHOUT ANY WARRANTY; without even the implied warranty of
+//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//     GNU General Public License for more details.
+// 
+//     You should have received a copy of the GNU General Public License
+//     along with this program.  If not, see <http://www.gnu.org/licenses/>.
+// 
 
+#region Using Directives
+
+using KerbalEngineer.Flight.Sections;
+using KerbalEngineer.Helpers;
+
+#endregion
+
+namespace KerbalEngineer.Flight.Readouts.Vessel
+{
+    public class Name : ReadoutModule
+    {
+        #region Constructors
+
+        public Name()
+        {
+            Name = "Name";
+            Category = ReadoutCategory.GetCategory("Vessel");
+            HelpString = "Displays the name of the current vessel.";
+            IsDefault = true;
+        }
+
+        #endregion
+
+        #region Methods: public
+
+        public override void Draw(SectionModule section)
+        {
+            if (SimulationProcessor.ShowDetails)
+            {
+                DrawLine(FlightGlobals.ActiveVessel.vesselName);
+            }
+        }
+
+        #endregion
+    }
+}

--- a/KerbalEngineer/Helpers/TimeFormatter.cs
+++ b/KerbalEngineer/Helpers/TimeFormatter.cs
@@ -58,31 +58,23 @@
         public static string ConvertToString(double seconds, string format = "F1")
         {
             var years = 0;
-            while (seconds >= SecondsPerYear)
+            var days = 0;
+            var hours = 0;
+            var minutes = 0;
+
+            if (seconds > 0)
             {
-                years++;
-                seconds -= SecondsPerYear;
-            }
+                years = (int)(seconds / SecondsPerYear);
+                seconds -= years * SecondsPerYear;
 
-            var days = 0;
-            while (seconds >= SecondsPerDay)
-            {
-                days++;
-                seconds -= SecondsPerDay;
-            }
+                days = (int)(seconds / SecondsPerDay);
+                seconds -= days * SecondsPerDay;
 
-            var hours = 0;
-            while (seconds >= SecondsPerHour)
-            {
-                hours++;
-                seconds -= SecondsPerHour;
-            }
+                hours = (int)(seconds / SecondsPerHour);
+                seconds -= hours * SecondsPerHour;
 
-            var minutes = 0;
-            while (seconds >= SecondsPerMinute)
-            {
-                minutes++;
-                seconds -= SecondsPerMinute;
+                minutes = (int)(seconds / SecondsPerMinute);
+                seconds -= minutes * SecondsPerMinute;
             }
 
             if (years > 0)

--- a/KerbalEngineer/Helpers/Units.cs
+++ b/KerbalEngineer/Helpers/Units.cs
@@ -205,7 +205,7 @@
 
         public static string ToTorque(double value)
         {
-            return value.ToString((value < 100.0) ? (Math.Abs(value) < Double.Epsilon) ? "N0" : "N1" : "N0") + "kNm";
+            return value.ToString((value < 100.0) ? (Math.Abs(value) < Double.Epsilon) ? "N0" : "N2" : "N0") + "kNm";
         }
     }
 }

--- a/KerbalEngineer/KerbalEngineer.csproj
+++ b/KerbalEngineer/KerbalEngineer.csproj
@@ -115,6 +115,7 @@
     <Compile Include="Flight\Readouts\Thermal\HottestTemperature.cs" />
     <Compile Include="Flight\Readouts\Thermal\HottestPart.cs" />
     <Compile Include="Flight\Readouts\Thermal\ThermalProcessor.cs" />
+    <Compile Include="Flight\Readouts\Vessel\Name.cs" />
     <Compile Include="Flight\Readouts\Vessel\AttitudeProcessor.cs" />
     <Compile Include="Flight\Readouts\Vessel\DeltaVCurrentTotal.cs" />
     <Compile Include="Flight\Readouts\Vessel\PitchRate.cs" />
@@ -230,6 +231,8 @@
     <Compile Include="VesselSimulator\SimManager.cs" />
     <Compile Include="VesselSimulator\Simulation.cs" />
     <Compile Include="VesselSimulator\Stage.cs" />
+    <Compile Include="Flight\Readouts\Rendezvous\RelativeRadialVelocity.cs" />
+    <Compile Include="Flight\Readouts\Rendezvous\TimeToRendezvous.cs" />
   </ItemGroup>
   <ItemGroup>
     <Reference Include="Assembly-CSharp">

--- a/KerbalEngineer/VesselSimulator/EngineSim.cs
+++ b/KerbalEngineer/VesselSimulator/EngineSim.cs
@@ -273,14 +273,36 @@
 
         HashSet<PartSim> visited = new HashSet<PartSim>();
 
+        public void DumpSourcePartSets(String msg)
+        {
+            MonoBehaviour.print("DumpSourcePartSets " + msg);
+            foreach (int type in sourcePartSets.Keys)
+            {
+                MonoBehaviour.print("SourcePartSet for " + ResourceContainer.GetResourceName(type));
+                HashSet<PartSim> sourcePartSet = sourcePartSets[type];
+                if (sourcePartSet.Count > 0)
+                {
+                    foreach (PartSim partSim in sourcePartSet)
+                    {
+                        MonoBehaviour.print("Part " + partSim.name + ":" + partSim.partId);
+                    }
+                }
+                else
+                {
+                    MonoBehaviour.print("No parts");
+                }
+            }
+        }
+
         public bool SetResourceDrains(List<PartSim> allParts, List<PartSim> allFuelLines, HashSet<PartSim> drainingParts)
         {
             LogMsg log = null;
-            
+            //DumpSourcePartSets("before clear");
             foreach (HashSet<PartSim> sourcePartSet in sourcePartSets.Values)
             {
                 sourcePartSet.Clear();
             }
+            //DumpSourcePartSets("after clear");
 
             for (int index = 0; index < this.resourceConsumptions.Types.Count; index++)
             {
@@ -338,20 +360,27 @@
                                 maxStage = stage;
                             }
 
-                            if (!stagePartSets.TryGetValue(stage, out sourcePartSet))
+                            HashSet<PartSim> tempPartSet;
+                            if (!stagePartSets.TryGetValue(stage, out tempPartSet))
                             {
-                                sourcePartSet = new HashSet<PartSim>();
-                                stagePartSets.Add(stage, sourcePartSet);
+                                tempPartSet = new HashSet<PartSim>();
+                                stagePartSets.Add(stage, tempPartSet);
                             }
-                            sourcePartSet.Add(aPartSim);
-                        }
-
-                        for (int j = 0; j <= maxStage; j++)
+                            tempPartSet.Add(aPartSim);
+                        }
+
+                        for (int j = maxStage; j >= 0; j--)
                         {
                             HashSet<PartSim> stagePartSet;
                             if (stagePartSets.TryGetValue(j, out stagePartSet) && stagePartSet.Count > 0)
                             {
-                                sourcePartSet = stagePartSet;
+                                // We have to copy the contents of the set here rather than copying the set reference or 
+                                // bad things (tm) happen
+                                foreach (PartSim aPartSim in stagePartSet)
+                                {
+                                    sourcePartSet.Add(aPartSim);
+                                }
+                                break;
                             }
                         }
                         break;
@@ -376,11 +405,9 @@
                         break;
                 }
 
-
-                if (sourcePartSet.Count > 0)
-                {
-                    sourcePartSets[type] = sourcePartSet;
-                    if (SimManager.logOutput)
+                if (SimManager.logOutput)
+                {
+                    if (sourcePartSet.Count > 0)
                     {
                         log = new LogMsg();
                         log.buf.AppendLine("Source parts for " + ResourceContainer.GetResourceName(type) + ":");
@@ -391,6 +418,8 @@
                         MonoBehaviour.print(log.buf);
                     }
                 }
+
+                //DumpSourcePartSets("after " + ResourceContainer.GetResourceName(type));
             }
             
             // If we don't have sources for all the needed resources then return false without setting up any drains
@@ -409,6 +438,7 @@
                     return false;
                 }
             }
+
             // Now we set the drains on the members of the sets and update the draining parts set
             for (int i = 0; i < this.resourceConsumptions.Types.Count; i++)
             {

 Binary files a/Output/KerbalEngineer/KerbalEngineer.dll and b/Output/KerbalEngineer/KerbalEngineer.dll differ