Skip to content

Commit

Permalink
When there is no CommNet connection or local kerbal control, disable …
Browse files Browse the repository at this point in the history
…monitors.

Add some CommNet variables.
  • Loading branch information
Virindi-AC committed Apr 22, 2017
1 parent 15339c4 commit 95bd9b1
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 10 deletions.
30 changes: 30 additions & 0 deletions RasterPropMonitor/Core/RPMCEvaluators.cs
Expand Up @@ -2332,6 +2332,36 @@ internal VariableEvaluator GetEvaluator(string input, out bool cacheable)
case "FUNDS":
return (string variable, RPMVesselComputer comp) => { return Funding.Instance != null ? Funding.Instance.Funds : 0.0; };

// CommNet
case "COMMNETCONNECTED":
return (string variable, RPMVesselComputer comp) => {
return ((vessel.connection != null) && (vessel.connection.IsConnected)) ? 1.0 : 0.0;
};
case "COMMNETSIGNALSTRENGTH":
return (string variable, RPMVesselComputer comp) => {
return (vessel.connection != null) ? (float)vessel.connection.SignalStrength : 0.0;
};
case "COMMNETVESSELCONTROLSTATE":
return (string variable, RPMVesselComputer comp) => {
if (vessel.connection == null)
return 0.0;
switch (vessel.connection.ControlState)
{
case CommNet.VesselControlState.None: return 0.0;
case CommNet.VesselControlState.Probe: return 2.0;
//case CommNet.VesselControlState.ProbeNone: return 2.0;
case CommNet.VesselControlState.Kerbal: return 4.0;
//case CommNet.VesselControlState.KerbalNone: return 4.0;
case CommNet.VesselControlState.Partial: return 8.0;
case CommNet.VesselControlState.ProbePartial: return 10.0;
case CommNet.VesselControlState.KerbalPartial: return 12.0;
case CommNet.VesselControlState.Full: return 16.0;
case CommNet.VesselControlState.ProbeFull: return 18.0;
case CommNet.VesselControlState.KerbalFull: return 20.0;
default: return 0.0;
}
};

// Action group flags. To properly format those, use this format:
// {0:on;0;OFF}
Expand Down
48 changes: 38 additions & 10 deletions RasterPropMonitor/Core/RasterPropMonitor.cs
Expand Up @@ -68,7 +68,11 @@ public class RasterPropMonitor : InternalModule
[KSPField]
public string resourceName = "SYSR_ELECTRICCHARGE";
private bool resourceDepleted = false; // Managed by rpmComp callback
private Action<bool> del;
private Action<bool> delResourceCallback;
[KSPField]
public bool needsCommConnection = true;
private bool noCommConnection = false; // Managed by rpmComp callback
private Action<float> delCommConnectionCallback;
[KSPField]
public string defaultFontTint = string.Empty;
public Color defaultFontTintValue = Color.white;
Expand Down Expand Up @@ -254,8 +258,14 @@ public void Start()

if (needsElectricCharge)
{
del = (Action<bool>)Delegate.CreateDelegate(typeof(Action<bool>), this, "ResourceDepletedCallback");
rpmComp.RegisterResourceCallback(resourceName, del);
delResourceCallback = (Action<bool>)Delegate.CreateDelegate(typeof(Action<bool>), this, "ResourceDepletedCallback");
rpmComp.RegisterResourceCallback(resourceName, delResourceCallback);
}

if (needsCommConnection)
{
delCommConnectionCallback = (Action<float>)Delegate.CreateDelegate(typeof(Action<float>), this, "CommConnectionCallback");
rpmComp.RegisterVariableCallback("COMMNETVESSELCONTROLSTATE", delCommConnectionCallback);
}

// And if the try block never completed, startupComplete will never be true.
Expand Down Expand Up @@ -289,9 +299,13 @@ public void OnDestroy()
{
Destroy(screenMat);
}
if (del != null)
if (delResourceCallback != null)
{
rpmComp.UnregisterResourceCallback(resourceName, del);
rpmComp.UnregisterResourceCallback(resourceName, delResourceCallback);
}
if (delCommConnectionCallback != null)
{
rpmComp.UnregisterVariableCallback ("COMMNETVESSELCONTROLSTATE", delCommConnectionCallback);
}
}

Expand All @@ -305,7 +319,7 @@ private static void PlayClickSound(FXGroup audioOutput)

public void GlobalButtonClick(int buttonID)
{
if (resourceDepleted)
if (resourceDepleted || noCommConnection)
{
return;
}
Expand Down Expand Up @@ -341,7 +355,7 @@ private MonitorPage FindPageByName(string pageName)

public void PageButtonClick(MonitorPage triggeredPage)
{
if (resourceDepleted)
if (resourceDepleted || noCommConnection)
{
return;
}
Expand Down Expand Up @@ -397,7 +411,7 @@ private void RenderScreen()
screenTexture.DiscardContents();
RenderTexture.active = screenTexture;

if (resourceDepleted)
if (resourceDepleted || noCommConnection)
{
// If we're out of electric charge, we're drawing a blank screen.
GL.Clear(true, true, emptyColorValue);
Expand Down Expand Up @@ -485,7 +499,7 @@ public override void OnUpdate()
}
else
{
if (!resourceDepleted)
if (!resourceDepleted && !noCommConnection)
{
RenderScreen();
}
Expand All @@ -498,7 +512,7 @@ public override void OnUpdate()
FillScreenBuffer();
textRefreshRequired = false;
}
if (!resourceDepleted)
if (!resourceDepleted && !noCommConnection)
{
RenderScreen();
}
Expand Down Expand Up @@ -554,6 +568,20 @@ void ResourceDepletedCallback(bool newValue)
{
resourceDepleted = newValue;
}

/// <summary>
/// Similar to ResourceDepletedCallback, allows computer to inform monitor
/// of commnet connection status.
/// </summary>
/// <param name="newValue"></param>
void CommConnectionCallback(float newValue)
{
//None, ProbeNone, Partial, ProbePartial
if ((newValue == 0.0) || (newValue == 2.0) || (newValue == 8.0) || (newValue == 10.0))
noCommConnection = true;
else
noCommConnection = false;
}
}
}

0 comments on commit 95bd9b1

Please sign in to comment.