VOID_VesselInfo: Minor tweak to the VesselSimulator failsafes.
[VOID.git] / VOID_VesselInfo.cs
blob:a/VOID_VesselInfo.cs -> blob:b/VOID_VesselInfo.cs
--- a/VOID_VesselInfo.cs
+++ b/VOID_VesselInfo.cs
@@ -22,14 +22,13 @@
 using System;
 using System.Collections.Generic;
 using UnityEngine;
+using Engineer.VesselSimulator;
+using Engineer.Extensions;
 
 namespace VOID
 {
 	public class VOID_VesselInfo : VOID_WindowModule
 	{
-		[AVOID_SaveValue("toggleExtended")]
-		protected VOID_SaveValue<bool> toggleExtended = false;
-
 		protected VOID_DoubleValue geeForce = new VOID_DoubleValue(
 			"G-force",
 			new Func<double>(() => VOID_Core.Instance.vessel.geeForce),
@@ -44,7 +43,7 @@
 
 		protected VOID_DoubleValue totalMass = new VOID_DoubleValue(
 			"Total Mass",
-			new Func<double>(() => VOID_Core.Instance.vessel.GetTotalMass()),
+			new Func<double> (() => SimManager.Instance.TryGetLastMass()),
 			"tons"
 		);
 
@@ -66,9 +65,11 @@
 			"DeltaV (Current Stage)",
 			delegate()
 			{
-				if (Engineer.VesselSimulator.SimManager.Instance.Stages == null)
+				if (SimManager.Instance.Stages == null ||
+			    	SimManager.Instance.Stages.Length <= Staging.lastStage
+			    )
 					return double.NaN;
-				return Engineer.VesselSimulator.SimManager.Instance.Stages[Staging.lastStage].deltaV;
+				return SimManager.Instance.Stages[Staging.lastStage].deltaV;
 			},
 			"m/s"
 		);
@@ -77,9 +78,9 @@
 			"DeltaV (Total)",
 			delegate()
 			{
-				if (Engineer.VesselSimulator.SimManager.Instance.Stages == null)
+				if (SimManager.Instance.Stages == null)
 					return double.NaN;
-				return Engineer.VesselSimulator.SimManager.Instance.LastStage.totalDeltaV;
+				return SimManager.Instance.LastStage.totalDeltaV;
 			},
 			"m/s"
 		);
@@ -94,11 +95,11 @@
 			"Thrust (curr/max)",
 			delegate()
 			{
-				if (Engineer.VesselSimulator.SimManager.Instance.Stages == null)
+				if (SimManager.Instance.Stages == null)
 					return "N/A";
 
-				double currThrust = Engineer.VesselSimulator.SimManager.Instance.LastStage.actualThrust;
-				double maxThrust = Engineer.VesselSimulator.SimManager.Instance.LastStage.thrust;
+				double currThrust = SimManager.Instance.LastStage.actualThrust;
+				double maxThrust = SimManager.Instance.LastStage.thrust;
 
 				return string.Format(
 					"{0} / {1}",
@@ -112,12 +113,12 @@
 			"T:W (curr/max)",
 			delegate()
 			{
-				if (Engineer.VesselSimulator.SimManager.Instance.Stages == null)
+				if (SimManager.Instance.Stages == null)
 					return "N/A";
 
-				double currThrust = Engineer.VesselSimulator.SimManager.Instance.LastStage.actualThrust;
-				double maxThrust = Engineer.VesselSimulator.SimManager.Instance.LastStage.thrust;
-				double mass = VOID_Core.Instance.vessel.GetTotalMass();
+				double currThrust = SimManager.Instance.LastStage.actualThrust;
+				double maxThrust = SimManager.Instance.LastStage.thrust;
+				double mass = SimManager.Instance.TryGetLastMass();
 				double gravity = VOID_Core.Instance.vessel.mainBody.gravParameter /
 				                 Math.Pow(
 					                 VOID_Core.Instance.vessel.mainBody.Radius + VOID_Core.Instance.vessel.altitude,
@@ -137,11 +138,11 @@
 			"Max T:W @ surface",
 			delegate()
 			{
-				if (Engineer.VesselSimulator.SimManager.Instance.Stages == null)
+				if (SimManager.Instance.Stages == null)
 					return double.NaN;
 
-				double maxThrust = Engineer.VesselSimulator.SimManager.Instance.LastStage.thrust;
-				double mass = VOID_Core.Instance.vessel.GetTotalMass();
+				double maxThrust = SimManager.Instance.LastStage.thrust;
+				double mass = SimManager.Instance.TryGetLastMass();
 				double gravity = (VOID_Core.Constant_G * VOID_Core.Instance.vessel.mainBody.Mass) /
 				                 Math.Pow(VOID_Core.Instance.vessel.mainBody.Radius, 2);
 				double weight = mass * gravity;
@@ -151,6 +152,51 @@
 			""
 		);
 
+		protected VOID_StrValue intakeAirStatus = new VOID_StrValue(
+			"Intake Air (Curr / Req)",
+			delegate()
+			{
+				double currentAmount;
+				double currentRequirement;
+
+				currentAmount = 0d;
+				currentRequirement = 0d;
+
+				foreach (Part part in VOID_Core.Instance.vessel.Parts)
+				{
+					if (part.HasModule<ModuleEngines>() && part.enabled)
+					{
+						foreach (Propellant propellant in part.GetModule<ModuleEngines>().propellants)
+						{
+							if (propellant.name == "IntakeAir")
+							{
+								// currentAmount += propellant.currentAmount;
+								currentRequirement += propellant.currentRequirement / TimeWarp.fixedDeltaTime;
+								break;
+							}
+						}
+					}
+
+					if (part.HasModule<ModuleResourceIntake>() && part.enabled)
+					{
+						ModuleResourceIntake intakeModule = part.GetModule<ModuleResourceIntake>();
+
+						if (intakeModule.resourceName == "IntakeAir")
+						{
+							currentAmount += intakeModule.airFlow;
+						}
+					}
+				}
+
+				if (currentAmount == 0 && currentRequirement == 0)
+				{
+					return "N/A";
+				}
+
+				return string.Format("{0:F3} / {1:F3}", currentAmount, currentRequirement);
+			}
+		);
+
 		public VOID_VesselInfo() : base()
 		{
 			this._Name = "Vessel Information";
@@ -165,10 +211,8 @@
 
 			if ((TimeWarp.WarpMode == TimeWarp.Modes.LOW) || (TimeWarp.CurrentRate <= TimeWarp.MaxPhysicsRate))
 			{
-				Engineer.VesselSimulator.SimManager.Instance.RequestSimulation();
-			}
-
-			Engineer.VesselSimulator.Stage[] stages = Engineer.VesselSimulator.SimManager.Instance.Stages;
+				SimManager.Instance.RequestSimulation();
+			}
 
 			GUILayout.BeginVertical();
 
@@ -196,6 +240,8 @@
 			this.currmaxThrustWeight.DoGUIHorizontal ();
 
 			this.surfaceThrustWeight.DoGUIHorizontal ("F2");
+
+			this.intakeAirStatus.DoGUIHorizontal();
 
 			GUILayout.EndVertical();
 			GUI.DragWindow();