Skip to content

Commit

Permalink
Added option to allow throttle to be overridden by joystick or not
Browse files Browse the repository at this point in the history
 Fixed loading of settings upon entering the flight scene
 Fixed saving of:
  m_MaxMoveSpeedAlways
  m_UseOnPreInsteadOfOnFlyByWire
  • Loading branch information
linuxgurugamer committed Sep 27, 2018
1 parent 5413fb0 commit cf9c0cc
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 44 deletions.
19 changes: 12 additions & 7 deletions AFBW/AdvancedFlyByWire.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public class AFBW_Settings
public bool m_IgnoreFlightCtrlState = true;
public bool m_UseOnPreInsteadOfOnFlyByWire = false;


public bool m_ThrottleOverride = false;

public bool m_MaxMoveSpeedAlways = false;

public void Serialize(string filename, AFBW_Settings config)
{
Expand All @@ -46,7 +48,7 @@ public void Serialize(string filename, AFBW_Settings config)

public AFBW_Settings Deserialize(string filename)
{
// Debug.Log("Deserialize, filename: " + filename);
//Debug.Log("Deserialize, filename: " + filename);
var serializer = new XmlSerializer(typeof(AFBW_Settings));

try
Expand All @@ -57,7 +59,10 @@ public AFBW_Settings Deserialize(string filename)
return config;
}
}
catch { }
catch (Exception ex)
{
Debug.Log("Exception deserializing " + filename + ", " + ex.Message);
}

return null;
}
Expand All @@ -81,9 +86,9 @@ public class AdvancedFlyByWire : MonoBehaviour

public AFBW_Settings settings = new AFBW_Settings();

public bool m_UseOnPreInsteadOfOnFlyByWire = false;
//public bool m_UseOnPreInsteadOfOnFlyByWire = false;

public bool m_MaxMoveSpeedAlways = false;
//public bool m_MaxMoveSpeedAlways = false;

// Configuration
private static readonly string addonFolder = Path.Combine(Path.Combine(KSPUtil.ApplicationRootPath, "GameData"), "ksp-advanced-flybywire");
Expand Down Expand Up @@ -161,7 +166,7 @@ public void OnDestroy()

private void LoadState(ConfigNode configNode)
{
var s = settings.Deserialize("");
var s = settings.Deserialize(GetAbsoluteSettingsPath());
if (s != null)
settings = s;
m_Configuration = Configuration.Deserialize(GetAbsoluteConfigurationPath());
Expand Down Expand Up @@ -232,7 +237,7 @@ private void UnregisterCallbacks()

void AddFlyByWireCallbackToActiveVessel()
{
if (m_UseOnPreInsteadOfOnFlyByWire)
if (settings.m_UseOnPreInsteadOfOnFlyByWire)
FlightGlobals.ActiveVessel.OnPreAutopilotUpdate += m_FlightManager.OnFlyByWire;
else
FlightGlobals.ActiveVessel.OnFlyByWire += m_FlightManager.OnFlyByWire;
Expand Down
2 changes: 1 addition & 1 deletion AFBW/AssemblyVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@

using System.Reflection;

[assembly: AssemblyVersion("1.8.0.4")]
[assembly: AssemblyVersion("1.8.1.0")]
7 changes: 5 additions & 2 deletions AFBW/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ public static void Serialize(string filename, Configuration config)

public static Configuration Deserialize(string filename)
{
// Debug.Log("Deserialize, filename: " + filename);
//Debug.Log("Deserialize, filename: " + filename);
var serializer = new XmlSerializer(typeof(Configuration));

try
Expand All @@ -288,7 +288,10 @@ public static Configuration Deserialize(string filename)
config.OnPostDeserialize();
return config;
}
} catch {}
} catch (Exception ex)
{
Debug.Log("Exception Deserializing: " + ex.Message);
}

return null;
}
Expand Down
2 changes: 1 addition & 1 deletion AFBW/EVAController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void UpdateEVAFlightProperties(ControllerConfiguration config, FlightCtrl
{
float moveSpeed = moveDirection.magnitude;
// Decrease max moveSpeed when not moving in a forwards direction.
if (!AdvancedFlyByWire.Instance.m_MaxMoveSpeedAlways && Vector3.Angle(moveDirection, eva.transform.forward) > 45)
if (!AdvancedFlyByWire.Instance.settings.m_MaxMoveSpeedAlways && Vector3.Angle(moveDirection, eva.transform.forward) > 45)
{
moveSpeed = Mathf.Clamp(moveSpeed, 0, 0.25f);
}
Expand Down
96 changes: 69 additions & 27 deletions AFBW/FlightManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void OnFlyByWire(FlightCtrlState state)
{
config.iface.Update(state);
UpdateAxes(config, state);

if (FlightGlobals.ActiveVessel.isEVA)
{
EVAController.Instance.UpdateEVAFlightProperties(config, state);
Expand Down Expand Up @@ -84,12 +84,12 @@ public void OnFlyByWire(FlightCtrlState state)
state.yaw = preState.yaw;
state.roll = preState.roll;
}
else
{
state.yaw = Utility.Clamp(state.yaw + state.yawTrim, -1.0f, 1.0f);
state.pitch = Utility.Clamp(state.pitch + state.pitchTrim, -1.0f, 1.0f);
state.roll = Utility.Clamp(state.roll + state.rollTrim, -1.0f, 1.0f);
}
else
{
state.yaw = Utility.Clamp(state.yaw + state.yawTrim, -1.0f, 1.0f);
state.pitch = Utility.Clamp(state.pitch + state.pitchTrim, -1.0f, 1.0f);
state.roll = Utility.Clamp(state.roll + state.rollTrim, -1.0f, 1.0f);
}
}
}

Expand All @@ -109,20 +109,52 @@ private void UpdateAxes(ControllerConfiguration config, FlightCtrlState state)

foreach (var action in actions)
{
var axisValue = axisState;
var axisValue = axisState;
if (config.GetCurrentPreset().IsContinuousBindingInverted(action))
{
axisValue *= -1.0f;
}

if (axisState != 0.0f || action == ContinuousAction.Throttle || action == ContinuousAction.WheelThrottle)

throttleActive = true;
if (!AdvancedFlyByWire.Instance.settings.m_ThrottleOverride && action == ContinuousAction.Throttle)
{
if (axisValue == last_m_throttle)
{
throttleActive = false;
}
else
{
EvaluateContinuousAction(config, action, axisValue, state);
last_m_throttle = axisValue;
}
}
wheelThrottleActive = true;
if (!AdvancedFlyByWire.Instance.settings.m_ThrottleOverride && action == ContinuousAction.Throttle)
{
if (axisValue == m_lastWheelThrottle)
{
wheelThrottleActive = false;
}
else
{
m_lastWheelThrottle = axisValue;
EvaluateContinuousAction(config, action, axisValue, state);
}
}
if ((axisState != 0.0f && action != ContinuousAction.Throttle && action != ContinuousAction.WheelThrottle) ||
(AdvancedFlyByWire.Instance.settings.m_ThrottleOverride && (action == ContinuousAction.Throttle || action == ContinuousAction.WheelThrottle)))
{
EvaluateContinuousAction(config, action, axisValue, state);
}
}
}
}

float last_m_throttle = 0;
float m_lastWheelThrottle = 0;
bool throttleActive, wheelThrottleActive;

private void UpdateFlightProperties(FlightCtrlState state)
{
// As of 0.90, setting these trim values seems to have no effect.
Expand All @@ -142,9 +174,16 @@ private void UpdateFlightProperties(FlightCtrlState state)
state.wheelSteerTrim = m_WheelSteer.GetTrim();
state.wheelThrottleTrim = m_WheelThrottle.GetTrim();

state.mainThrottle = Utility.Clamp(state.mainThrottle + m_Throttle.Update(), 0.0f, 1.0f);
if (throttleActive)
{
state.mainThrottle = Utility.Clamp(state.mainThrottle + m_Throttle.Update(), 0.0f, 1.0f);
}
if (wheelThrottleActive)
{
state.wheelThrottle = Utility.Clamp(state.wheelThrottle + m_WheelThrottle.Update(), -1.0f, 1.0f);
}

state.wheelSteer = Utility.Clamp(state.wheelSteer + m_WheelSteer.Update(), -1.0f, 1.0f);
state.wheelThrottle = Utility.Clamp(state.wheelThrottle + m_WheelThrottle.Update(), -1.0f, 1.0f);
}

private void ZeroOutFlightProperties()
Expand Down Expand Up @@ -231,10 +270,10 @@ public void EvaluateDiscreteAction(ControllerConfiguration controller, DiscreteA
case DiscreteAction.PitchMinus:
m_Pitch.SetIncrement(-1, controller.discreteActionStep);
return;
case DiscreteAction.PitchTrimPlus:
case DiscreteAction.PitchTrimPlus:
m_Pitch.SetTrim(Utility.Clamp(m_Pitch.GetTrim() + controller.discreteActionStep / 10f, -1.0f, 1.0f));
return;
case DiscreteAction.PitchTrimMinus:
case DiscreteAction.PitchTrimMinus:
m_Pitch.SetTrim(Utility.Clamp(m_Pitch.GetTrim() - controller.discreteActionStep / 10f, -1.0f, 1.0f));
return;
case DiscreteAction.RollPlus:
Expand Down Expand Up @@ -345,7 +384,7 @@ public void EvaluateDiscreteAction(ControllerConfiguration controller, DiscreteA
case DiscreteAction.NavballToggle:
if (MapView.MapIsEnabled && MapView.fetch != null)
{
// MapView.fetch.maneuverModeToggle.OnPress.Invoke();
// MapView.fetch.maneuverModeToggle.OnPress.Invoke();
}
return;
case DiscreteAction.IVAViewToggle:
Expand Down Expand Up @@ -380,11 +419,11 @@ public void EvaluateDiscreteAction(ControllerConfiguration controller, DiscreteA
return;
// The radial controls are reversed
case DiscreteAction.SASRadialOut:
//case DiscreteAction.SASRadialIn:
//case DiscreteAction.SASRadialIn:
setAutopilotMode(VesselAutopilot.AutopilotMode.RadialIn, 5);
return;
case DiscreteAction.SASRadialIn:
//case DiscreteAction.SASRadialOut:
//case DiscreteAction.SASRadialOut:
setAutopilotMode(VesselAutopilot.AutopilotMode.RadialOut, 6);
return;
case DiscreteAction.SASManeuver:
Expand All @@ -406,10 +445,10 @@ public void EvaluateDiscreteAction(ControllerConfiguration controller, DiscreteA
if (FlightInputHandler.fetch != null)
{
FlightInputHandler.fetch.precisionMode = !FlightInputHandler.fetch.precisionMode;
GameEvents.Input.OnPrecisionModeToggle.Fire(FlightInputHandler.fetch.precisionMode);
GameEvents.Input.OnPrecisionModeToggle.Fire(FlightInputHandler.fetch.precisionMode);
}
return;
case DiscreteAction.ResetTrim:
case DiscreteAction.ResetTrim:
m_Yaw.SetTrim(0.0f);
m_Pitch.SetTrim(0.0f);
m_Roll.SetTrim(0.0f);
Expand Down Expand Up @@ -691,15 +730,18 @@ private static void setSASUI(int mode)
private static bool setAutopilotMode(VesselAutopilot.AutopilotMode mode, int ui_button)
{
//Debug.Log("setAutopilotMode, mode: " + mode);
if (FlightGlobals.ActiveVessel.Autopilot.CanSetMode(mode)) {
FlightGlobals.ActiveVessel.ActionGroups.SetGroup(KSPActionGroup.SAS, true);
FlightGlobals.ActiveVessel.Autopilot.Update();
FlightGlobals.ActiveVessel.Autopilot.SetMode(mode);
setSASUI(ui_button);
return true;
} else {
return false;
}
if (FlightGlobals.ActiveVessel.Autopilot.CanSetMode(mode))
{
FlightGlobals.ActiveVessel.ActionGroups.SetGroup(KSPActionGroup.SAS, true);
FlightGlobals.ActiveVessel.Autopilot.Update();
FlightGlobals.ActiveVessel.Autopilot.SetMode(mode);
setSASUI(ui_button);
return true;
}
else
{
return false;
}
}
}
}
10 changes: 8 additions & 2 deletions AFBW/ModSettingsWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,22 @@ public void DoWindow(int window)
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();

GUILayout.BeginHorizontal();
GUILayout.Label("AFBW input overrides throttle");
AdvancedFlyByWire.Instance.settings.m_ThrottleOverride = GUILayout.Toggle(AdvancedFlyByWire.Instance.settings.m_ThrottleOverride, "");
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();

GUILayout.BeginHorizontal();
GUILayout.Label("AtmosphereAutopilot compatibility");
AdvancedFlyByWire.Instance.m_UseOnPreInsteadOfOnFlyByWire = GUILayout.Toggle(AdvancedFlyByWire.Instance.m_UseOnPreInsteadOfOnFlyByWire, "");
AdvancedFlyByWire.Instance.settings.m_UseOnPreInsteadOfOnFlyByWire = GUILayout.Toggle(AdvancedFlyByWire.Instance.settings.m_UseOnPreInsteadOfOnFlyByWire, "");
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();


GUILayout.BeginHorizontal();
GUILayout.Label("Run at max speed always (even when not going forward)");
AdvancedFlyByWire.Instance.m_MaxMoveSpeedAlways = GUILayout.Toggle(AdvancedFlyByWire.Instance.m_MaxMoveSpeedAlways, "");
AdvancedFlyByWire.Instance.settings.m_MaxMoveSpeedAlways = GUILayout.Toggle(AdvancedFlyByWire.Instance.settings.m_MaxMoveSpeedAlways, "");
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();

Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
1.8.1
Added option to allow throttle to be overridden by joystick or not
Fixed loading of settings upon entering the flight scene
Fixed saving of:
m_MaxMoveSpeedAlways
m_UseOnPreInsteadOfOnFlyByWire

1.8.0.5
Centered Mod Settings button
Fixed RadialIn/RadialOut buttons (are reversed)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"VERSION": {
"MAJOR": 1,
"MINOR": 8,
"PATCH": 0,
"BUILD": 4
"PATCH": 1,
"BUILD": 0
},
"KSP_VERSION": {
"MAJOR": 1,
Expand Down
4 changes: 2 additions & 2 deletions ksp-advanced-flybywire.version
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"VERSION": {
"MAJOR": 1,
"MINOR": 8,
"PATCH": 0,
"BUILD": 5
"PATCH": 1,
"BUILD": 0
},
"KSP_VERSION": {
"MAJOR": 1,
Expand Down

0 comments on commit cf9c0cc

Please sign in to comment.