Skip to content
This repository has been archived by the owner on Sep 7, 2021. It is now read-only.

Commit

Permalink
fix click close, add speed unit selection
Browse files Browse the repository at this point in the history
  • Loading branch information
Crzyrndm committed Nov 24, 2015
1 parent 4cd6ef5 commit b3e45b4
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 81 deletions.
Binary file modified GameData/Pilot Assistant/PilotAssistant.dll
Binary file not shown.
70 changes: 48 additions & 22 deletions PilotAssistant/FlightModules/PilotAssistant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,19 @@ void StartCoroutine(IEnumerator routine) // quick access to coroutine now it doe
public static readonly double[] defaultAccelGains = { 0.2, 0.08, 0.0, -1, 0, -1, 1, 1, 1 };

// speed mode change
SpeedRef speedRef = SpeedRef.True;
bool speedSelectWindowVisible;
Rect speedSelectWindow;
GUIContent[] speedReferences = new GUIContent[3] {new GUIContent("TAS"),

SpeedRef speedRef = SpeedRef.True;
GUIContent[] speedRefLabels = new GUIContent[3] {new GUIContent("TAS"),
new GUIContent("IAS"),
new GUIContent("EAS")};
SpeedUnits units = SpeedUnits.mSec;
GUIContent[] speedUnitLabels = new GUIContent[5] {new GUIContent("m/s"),
new GUIContent("km/h"),
new GUIContent("mph"),
new GUIContent("kts"),
new GUIContent("mach")};
/** Speed and acceleration accounting for TAS/IAS/EAS since calculating acceleration for modes other than TAS is not just a simple multiplier **/
double adjustedAcceleration, adjustedSpeed;

Expand Down Expand Up @@ -276,6 +283,14 @@ public void Update()
targetHeading = AsstList.HdgBank.GetAsst(this).SetPoint.ToString("0.00");
}
}

if (speedSelectWindowVisible && Input.GetMouseButtonDown(0))
{
Vector2 mouse = Input.mousePosition;
mouse.y = Screen.height - mouse.y;
if (!speedSelectWindow.Contains(mouse))
speedSelectWindowVisible = false;
}
}

public void InputResponse()
Expand Down Expand Up @@ -392,11 +407,11 @@ public void InputResponse()
break;
case ThrottleMode.Acceleration:
AsstList.Acceleration.GetAsst(this).SetPoint += speed / 10;
targetSpeed = AsstList.Acceleration.GetAsst(this).SetPoint.ToString("0.00");
targetSpeed = (AsstList.Acceleration.GetAsst(this).SetPoint * Utils.speedUnitTransform(units, vesModule.vesselRef.speedOfSound)).ToString("0.00");
break;
case ThrottleMode.Speed:
AsstList.Speed.GetAsst(this).SetPoint = Math.Max(AsstList.Speed.GetAsst(this).SetPoint + speed, 0);
targetSpeed = AsstList.Speed.GetAsst(this).SetPoint.ToString("0.00");
targetSpeed = (AsstList.Speed.GetAsst(this).SetPoint * Utils.speedUnitTransform(units, vesModule.vesselRef.speedOfSound)).ToString("0.00");
break;
}
}
Expand Down Expand Up @@ -596,12 +611,12 @@ public void SetVert(bool active, bool setTarget, VertMode mode, double target)
{
case VertMode.Altitude:
if (!VertActive)
AsstList.Altitude.GetAsst(this).SetPoint = vesModule.vesselRef.altitude;
AsstList.Altitude.GetAsst(this).SetPoint = vesModule.vesselRef.altitude + vesModule.vesselData.vertSpeed / AsstList.Altitude.GetAsst(this).PGain;
AsstList.Altitude.GetAsst(this).BumplessSetPoint = target;
break;
case VertMode.RadarAltitude:
if (!VertActive)
AsstList.Altitude.GetAsst(this).SetPoint = vesModule.vesselData.radarAlt;
AsstList.Altitude.GetAsst(this).SetPoint = vesModule.vesselData.radarAlt + vesModule.vesselData.vertSpeed / AsstList.Altitude.GetAsst(this).PGain;
AsstList.Altitude.GetAsst(this).BumplessSetPoint = target;
break;
case VertMode.VSpeed:
Expand Down Expand Up @@ -634,12 +649,12 @@ private void throttleModeChanged(ThrottleMode newMode, bool active, bool setTarg
case ThrottleMode.Speed:
if (setTarget)
AsstList.Speed.GetAsst(this).SetPoint = vesModule.vesselRef.srfSpeed;
targetSpeed = AsstList.Speed.GetAsst(this).SetPoint.ToString("0.00");
targetSpeed = (AsstList.Speed.GetAsst(this).SetPoint * Utils.speedUnitTransform(units, vesModule.vesselRef.speedOfSound)).ToString("0.00");
break;
case ThrottleMode.Acceleration:
if (setTarget)
AsstList.Acceleration.GetAsst(this).SetPoint = vesModule.vesselData.acceleration;
targetSpeed = AsstList.Acceleration.GetAsst(this).SetPoint.ToString("0.00");
targetSpeed = (AsstList.Acceleration.GetAsst(this).SetPoint * Utils.speedUnitTransform(units, vesModule.vesselRef.speedOfSound)).ToString("0.00");
break;
case ThrottleMode.Direct:
if (setTarget)
Expand Down Expand Up @@ -750,7 +765,7 @@ public void vesselController(FlightCtrlState state)
{
if (CurrentVertMode != VertMode.Pitch)
{
switch(CurrentVertMode)
switch (CurrentVertMode)
{
case VertMode.RadarAltitude:
AsstList.VertSpeed.GetAsst(this).SetPoint = Utils.Clamp(getClimbRateForConstAltitude() + AsstList.Altitude.GetAsst(this).ResponseD(vesModule.vesselData.radarAlt * Vector3.Dot(vesModule.vesselData.surfVelForward, vesModule.vesselRef.srf_velocity.normalized), useIntegral), -vesModule.vesselRef.srfSpeed * 0.9, vesModule.vesselRef.srfSpeed * 0.9);
Expand All @@ -760,23 +775,20 @@ public void vesselController(FlightCtrlState state)
break;
}
AsstList.Elevator.GetAsst(this).SetPoint = AsstList.VertSpeed.GetAsst(this).ResponseD(vesModule.vesselData.vertSpeed, useIntegral);
if (Math.Abs(vesModule.vesselData.bank) > 90)
AsstList.Elevator.GetAsst(this).SetPoint *= -1; // flying upside down
AsstList.Elevator.GetAsst(this).SetPoint *= (float)Utils.Clamp(Math.Cos(vesModule.vesselData.bank * Math.PI / 180) * 2.0, -1, 1); // only reduce control when bank angle exceeds ~60 degrees
state.pitch = AsstList.Elevator.GetAsst(this).ResponseF(vesModule.vesselData.AoA, useIntegral).Clamp(-1, 1);
}
else
{
state.pitch = AsstList.Elevator.GetAsst(this).ResponseF(vesModule.vesselData.pitch, useIntegral).Clamp(-1, 1);
if (Math.Abs(vesModule.vesselData.bank) > 90)
state.pitch *= -1; // flying upside down
state.pitch *= (float)Utils.Clamp(Math.Cos(vesModule.vesselData.bank * Math.PI / 180) * 2.0, -1, 1); // only reduce control when bank angle exceeds ~60 degrees
}
}
else
state.pitch = Mathf.Clamp(state.pitch + pitchHold, -1, 1);

if (ThrtActive && CurrentThrottleMode != ThrottleMode.Direct)
{

if (vesModule.vesselRef.ActionGroups[KSPActionGroup.Brakes] || (AsstList.Speed.GetAsst(this).SetPoint == 0 && vesModule.vesselRef.srfSpeed < -AsstList.Acceleration.GetAsst(this).OutMin))
state.mainThrottle = 0;
else
Expand Down Expand Up @@ -1321,6 +1333,7 @@ private void displayWindow(int id)

double newVal;
double.TryParse(targetSpeed, out newVal);
newVal /= Utils.speedUnitTransform(units, vesModule.vesselRef.speedOfSound);
switch (CurrentThrottleMode)
{
case ThrottleMode.Direct:
Expand Down Expand Up @@ -1358,9 +1371,9 @@ private void displayWindow(int id)
{
ThrtScrollbar = GUILayout.BeginScrollView(ThrtScrollbar, GUIStyle.none, GeneralUI.UISkin.verticalScrollbar, GUILayout.Height(Math.Min(thrtScrollHeight, maxThrtScrollbarHeight)));
if (CurrentThrottleMode == ThrottleMode.Speed)
drawPIDvalues(AsstList.Speed, "Speed", " m/s", adjustedSpeed, 2, "Accel ", " m/s", true);
drawPIDvalues(AsstList.Speed, "Speed", Utils.unitString(units), adjustedSpeed * Utils.speedUnitTransform(units, vesModule.vesselRef.speedOfSound), 2, "Accel ", Utils.unitString(units) + " / s", true);
if (CurrentThrottleMode != ThrottleMode.Direct)
drawPIDvalues(AsstList.Acceleration, "Acceleration", " m/s", adjustedAcceleration, 1, "Throttle ", "%", true);
drawPIDvalues(AsstList.Acceleration, "Acceleration", Utils.unitString(units) + " / s", adjustedAcceleration * Utils.speedUnitTransform(units, vesModule.vesselRef.speedOfSound), 1, "Throttle ", " %", true);
// can't have people bugging things out now can we...
AsstList.Acceleration.GetAsst(this).OutMax = AsstList.Speed.GetAsst(this).OutMax.Clamp(-1, 0);
AsstList.Acceleration.GetAsst(this).OutMin = AsstList.Speed.GetAsst(this).OutMin.Clamp(-1, 0);
Expand Down Expand Up @@ -1416,7 +1429,18 @@ private void drawPIDvalues(AsstList controllerid, string inputName, string input
if (controller.bShow)
{
if (showTarget)
GUILayout.Label("Target: " + controller.SetPoint.ToString("N" + displayPrecision.ToString()) + inputUnits, GUILayout.Width(200));
{
switch (controllerid)
{
case AsstList.Speed:
case AsstList.Acceleration:
GUILayout.Label("Target: " + (controller.SetPoint * Utils.speedUnitTransform(units, vesModule.vesselRef.speedOfSound)).ToString("N" + displayPrecision.ToString()) + inputUnits, GUILayout.Width(200));
break;
default:
GUILayout.Label("Target: " + controller.SetPoint.ToString("N" + displayPrecision.ToString()) + inputUnits, GUILayout.Width(200));
break;
}
}

GUILayout.BeginHorizontal();
GUILayout.BeginVertical();
Expand Down Expand Up @@ -1516,13 +1540,15 @@ private void displayPresetWindow(int id)

private void drawSpeedSelectWindow(int id)
{
SpeedRef tempRef = (SpeedRef)GUILayout.SelectionGrid((int)speedRef, speedReferences, 3);
SpeedRef tempRef = (SpeedRef)GUILayout.SelectionGrid((int)speedRef, speedRefLabels, 3);
if (tempRef != speedRef)
ChangeSpeedRef(tempRef);

Debug.Log(GUIUtility.ScreenToGUIPoint(Input.mousePosition));
if (Input.GetMouseButtonDown(0) && !speedSelectWindow.Contains(GUIUtility.ScreenToGUIPoint(Input.mousePosition)))
speedSelectWindowVisible = false;
SpeedUnits tempUnits = (SpeedUnits)GUILayout.SelectionGrid((int)units, speedUnitLabels, 5);
if (tempUnits != units)
{
units = tempUnits;
throttleModeChanged(CurrentThrottleMode, ThrtActive, false);
}
}

#endregion
Expand Down
15 changes: 3 additions & 12 deletions PilotAssistant/PilotAssistantFlightCore.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using UnityEngine;
using System.IO;
using UnityEngine;

namespace PilotAssistant
{
/* Flight core calls Unity functions of all flight scene classes. This improves control over execution order
* which has previously been a slight annoyance.
*
* It also simplifies management of event subscriptions and the like and serves as a location for settings
* and other common variables
*/

using Utility;
using Toolbar;
using FlightModules;
using Toolbar;
using Utility;

[KSPAddon(KSPAddon.Startup.Flight, false)]
class PilotAssistantFlightCore : MonoBehaviour
Expand Down
52 changes: 5 additions & 47 deletions PilotAssistant/Utility/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,48 +200,6 @@ public static double speedUnitTransform(SpeedUnits units, double soundSpeed)
return 1;
}

public static double mSecToSpeedUnit(this double mSec, SpeedRef mode, SpeedUnits units, AsstVesselModule avm)
{
if (mode == SpeedRef.Mach)
return mSec / avm.vesselRef.speedOfSound;
else
{
double speed = mSec * speedUnitTransform(units, avm.vesselRef.speedOfSound);
switch (mode)
{
case SpeedRef.True:
return speed;
case SpeedRef.Indicated:
double stagnationPres = Math.Pow(((avm.vesselRef.mainBody.atmosphereAdiabaticIndex - 1) * avm.vesselRef.mach * avm.vesselRef.mach * 0.5) + 1, avm.vesselRef.mainBody.atmosphereAdiabaticIndex / (avm.vesselRef.mainBody.atmosphereAdiabaticIndex - 1));
return speed * Math.Sqrt(avm.vesselRef.atmDensity / 1.225) * stagnationPres;
case SpeedRef.Equivalent:
return speed * Math.Sqrt(avm.vesselRef.atmDensity / 1.225);
}
return 0;
}
}

public static double SpeedUnitToMSec(this double speedUnit, SpeedRef mode, SpeedUnits units, AsstVesselModule avm)
{
if (mode == SpeedRef.Mach)
return speedUnit * avm.vesselRef.speedOfSound;
else
{
double speed = speedUnit / speedUnitTransform(units, avm.vesselRef.speedOfSound);
switch (mode)
{
case SpeedRef.True:
return speed;
case SpeedRef.Indicated:
double stagnationPres = Math.Pow(((avm.vesselRef.mainBody.atmosphereAdiabaticIndex - 1) * avm.vesselRef.mach * avm.vesselRef.mach * 0.5) + 1, avm.vesselRef.mainBody.atmosphereAdiabaticIndex / (avm.vesselRef.mainBody.atmosphereAdiabaticIndex - 1));
return speed / (Math.Sqrt(avm.vesselRef.atmDensity / 1.225) * stagnationPres);
case SpeedRef.Equivalent:
return speed / Math.Sqrt(avm.vesselRef.atmDensity / 1.225);
}
return 0;
}
}

public static double SpeedTransform(SpeedRef refMode, AsstVesselModule avm)
{
switch (refMode)
Expand All @@ -262,15 +220,15 @@ public static string unitString(SpeedUnits unit)
switch(unit)
{
case SpeedUnits.mSec:
return "m/s";
return " m/s";
case SpeedUnits.mach:
return "mach";
return " mach";
case SpeedUnits.knots:
return "knots";
return " knots";
case SpeedUnits.kmph:
return "km/h";
return " km/h";
case SpeedUnits.mph:
return "mph";
return " mph";
}
return "";
}
Expand Down

0 comments on commit b3e45b4

Please sign in to comment.