Simplified null or empty check for readability.
[VesselSimulator.git] / KerbalEngineer / Flight / Readouts / Surface / AtmosphericProcessor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// 
//     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 System;
using System.Linq;
using System.Reflection;
 
using KerbalEngineer.Extensions;
 
#endregion
 
namespace KerbalEngineer.Flight.Readouts.Surface
{
    public class AtmosphericProcessor : IUpdatable, IUpdateRequest
    {
        #region Instance
 
        #region Fields
 
        private static readonly AtmosphericProcessor instance = new AtmosphericProcessor();
 
        #endregion
 
        #region Properties
 
        /// <summary>
        ///     Gets the current instance of the atmospheric processor.
        /// </summary>
        public static AtmosphericProcessor Instance
        {
            get { return instance; }
        }
 
        #endregion
 
        #endregion
 
        #region Fields
 
        private MethodInfo farTerminalVelocity;
        private bool hasCheckedAeroMods;
 
        #endregion
 
        #region Properties
 
        /// <summary>
        ///     Gets the deceleration caused by drag.
        /// </summary>
        public static double Deceleration { get; private set; }
 
        /// <summary>
        ///     Gets the difference between current velocity and terminal velocity.
        /// </summary>
        public static double Efficiency { get; private set; }
 
        /// <summary>
        ///     Gets whether FAR is installed.
        /// </summary>
        public static bool FarInstalled { get; private set; }
 
        /// <summary>
        ///     Gets whether NEAR is installed.
        /// </summary>
        public static bool NearInstalled { get; private set; }
 
        /// <summary>
        ///     Gets whether the details are ready to be shown.
        /// </summary>
        public static bool ShowDetails { get; private set; }
 
        /// <summary>
        ///     Gets the terminal velocity of the active vessel.
        /// </summary>
        public static double TerminalVelocity { get; private set; }
 
        #endregion
 
        #region IUpdatable Members
 
        /// <summary>
        ///     Updates the details by recalculating if requested.
        /// </summary>
        public void Update()
        {
            try
            {
                if (!this.hasCheckedAeroMods)
                {
                    this.CheckAeroMods();
                }
 
                if (FlightGlobals.ActiveVessel.atmDensity < double.Epsilon || NearInstalled)
                {
                    ShowDetails = false;
                    return;
                }
 
                ShowDetails = true;
 
                if (FarInstalled)
                {
                    TerminalVelocity = (double)this.farTerminalVelocity.Invoke(null, null);
                }
                else
                {
                    var mass = FlightGlobals.ActiveVessel.parts.Sum(p => p.GetWetMass());
                    var drag = FlightGlobals.ActiveVessel.parts.Sum(p => p.GetWetMass() * p.maximum_drag);
                    var grav = FlightGlobals.getGeeForceAtPosition(FlightGlobals.ship_position).magnitude;
                    var atmo = FlightGlobals.ActiveVessel.atmDensity;
                    var coef = FlightGlobals.DragMultiplier;
 
                    TerminalVelocity = Math.Sqrt((2 * mass * grav) / (atmo * drag * coef));
                }
 
                Efficiency = FlightGlobals.ship_srfSpeed / TerminalVelocity;
            }
            catch (Exception ex)
            {
                Logger.Exception(ex, "AtmosphericProcessor->Update");
            }
        }
 
        #endregion
 
        #region IUpdateRequest Members
 
        /// <summary>
        ///     Gets and sets whether the updatable object should be updated.
        /// </summary>
        public bool UpdateRequested { get; set; }
 
        #endregion
 
        #region Methods: public
 
        /// <summary>
        ///     Request an update to calculate the details.
        /// </summary>
        public static void RequestUpdate()
        {
            instance.UpdateRequested = true;
        }
 
        #endregion
 
        #region Private Methods
 
        private void CheckAeroMods()
        {
            try
            {
                this.hasCheckedAeroMods = true;
 
                foreach (var loadedAssembly in AssemblyLoader.loadedAssemblies)
                {
                    switch (loadedAssembly.name)
                    {
                        case "FerramAerospaceResearch":
                            this.farTerminalVelocity = loadedAssembly.assembly.GetType("ferram4.FARAPI").GetMethod("GetActiveControlSys_TermVel");
                            FarInstalled = true;
                            Logger.Log("FAR detected!");
                            break;
 
                        case "NEAR":
                            NearInstalled = true;
                            Logger.Log("NEAR detected! Turning off atmospheric details!");
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Exception(ex, "AtmosphericProcessor->CheckAeroMods");
            }
        }
 
        #endregion
    }
}