Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #2652 and comes with raw control NEUTRALIZE suffix refactor. #2770

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 51 additions & 5 deletions doc/source/commands/flight/raw.rst
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ These "Raw" controls allow you the direct control of flight parameters while the

* - :ref:`NEUTRAL <SHIP CONTROL NEUTRAL>`
- :ref:`Boolean <boolean>`
- Is **kOS** Controlling?
- True if ship:control is doing nothing.

* - :ref:`NEUTRALIZE <SHIP CONTROL NEUTRALIZE>`
- :ref:`Boolean <boolean>`
- Releases Control
Expand Down Expand Up @@ -178,6 +179,11 @@ These "Raw" controls allow you the direct control of flight parameters while the
trim instead is because KSP only looks at the trim when its part of
the *pilot's* own control structure, not an autpilot's control structure.

*Warning*:
Setting this value can cause :ref:`:NEUTRAL <SHIP CONTROL NEUTRAL>` to
return false negatives by confusing the system about where the "at
rest" point of the controls are.

.. _SHIP CONTROL PITCHTRIM:
.. object:: SHIP:CONTROL:PITCHTRIM

Expand All @@ -190,6 +196,11 @@ These "Raw" controls allow you the direct control of flight parameters while the
trim instead is because KSP only looks at the trim when its part of
the *pilot's* own control structure, not an autpilot's control structure.

*Warning*:
Setting this value can cause :ref:`NEUTRAL <SHIP CONTROL NEUTRAL>` to
return false negatives by confusing the system about where the "at
rest" point of the controls are.

.. _SHIP CONTROL ROLLTRIM:
.. object:: SHIP:CONTROL:ROLLTRIM

Expand All @@ -202,6 +213,11 @@ These "Raw" controls allow you the direct control of flight parameters while the
trim when its part of the *pilot's* own control structure, not an
autpilot's control structure.

*Warning*:
Setting this value can cause :ref:`NEUTRAL <SHIP CONTROL NEUTRAL>` to
return false negatives by confusing the system about where the "at
rest" point of the controls are.

.. _SHIP CONTROL FORE:
.. object:: SHIP:CONTROL:FORE

Expand Down Expand Up @@ -253,6 +269,11 @@ These "Raw" controls allow you the direct control of flight parameters while the
trim when its part of the *pilot's* own control structure, not an
autpilot's control structure.

*Warning*:
Setting this value can cause :ref:`NEUTRAL <SHIP CONTROL NEUTRAL>` to
return false negatives by confusing the system about where the "at
rest" point of the controls are.

.. _SHIP CONTROL WHEELTHROTTLETRIM:
.. object:: SHIP:CONTROL:WHEELTHROTTLETRIM

Expand All @@ -265,15 +286,40 @@ These "Raw" controls allow you the direct control of flight parameters while the
trim when its part of the *pilot's* own control structure, not an
autpilot's control structure.

*Warning*:
Setting this value can cause :ref:`NEUTRAL <SHIP CONTROL NEUTRAL>` to
return false negatives by confusing the system about where the "at
rest" point of the controls are.

.. _SHIP CONTROL NEUTRAL:
.. _SHIP CONTROL NEUTRALIZE:
.. object:: SHIP:CONTROL:NEUTRAL
.. object:: SHIP:CONTROL:NEUTRALIZE

Returns true or false depending if **kOS** has any set controls. *This is not settable.*
These used to be two suffixes but they are now synonyms who's meaning
changes depending on if you set or get them.

.. _SHIP CONTROL NEUTRALIZE:
.. object:: SHIP:CONTROL:NEUTRALIZE
*Getting*:

``if (SHIP:CONTROL:NEUTRAL)`` is true when the raw controls are at rest.

*Setting*:

``set SHIP:CONTROL:NEUTRALIZE TO TRUE.`` causes the raw controls to let go.
Setting it to false has no effect.

*Warnings*:

Although it has no effect, setting a raw control TRIM value CAN cause
``NEUTRAL`` to return false when the control is at rest. For example,
if you do ``SET SHIP:CONTROL:YAWTRIM to 0.1.` then when the controls
are at rest, ``SHIP:CONTROL:NEUTRAL`` will return false because the yaw
position of 0 is differing from its trim position of 0.1.

This causes manual control to let go. When set to true, **kOS** lets go of the controls and allows the player to manually control them again. *This is not gettable.*
The two terms ``NEUTRAL`` and ``NEUTRALIZE`` are synonyms. (They used to
be two separate suffixes, one for getting and one for setting, but
that made no sense so they were combined but both spellings were
retained for backward compantiblity with old scripts.)


Unlocking controls
Expand Down
89 changes: 24 additions & 65 deletions src/kOS/Suffixed/FlightControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using kOS.AddOns.RemoteTech;
using kOS.Safe.Encapsulation;
using kOS.Safe.Encapsulation.Suffixes;
using kOS.Safe.Exceptions;
using kOS.Safe.Utilities;
using kOS.Utilities;
using Math = System.Math;
Expand Down Expand Up @@ -31,18 +32,14 @@ public class FlightControl : Structure , IDisposable, IFlightControlParameter
private float wheelThrottle;
private float wheelThrottleTrim;
private float mainThrottle;
private readonly Flushable<bool> neutral;
private readonly Flushable<bool> killRotation;
private readonly Flushable<bool> resetTrim;
private bool bound;
private Flushable<bool> neutral;
private readonly List<string> floatSuffixes;
private readonly List<string> vectorSuffixes;

public FlightControl(Vessel vessel)
{
neutral = new Flushable<bool>();
killRotation = new Flushable<bool>();
resetTrim = new Flushable<bool>();
bound = false;
Vessel = vessel;

Expand All @@ -59,21 +56,6 @@ public override bool SetSuffix(string suffixName, object value, bool failOkay =
float floatValue = 0;
Vector vectorValue = null;

if (CheckNeutral(suffixName, value))
{
return true;
}

if (CheckKillRotation(suffixName, value))
{
return true;
}

if (CheckResetTrim(suffixName, value))
{
return true;
}

if (floatSuffixes.Contains(suffixName))
{
if (!ValueToFloat(value, ref floatValue)) return true;
Expand Down Expand Up @@ -174,8 +156,7 @@ private void InitializeSuffixes()

//OTHER
AddSuffix(new[] { "BOUND" }, new SetSuffix<BooleanValue>(() => bound, value => bound = value));
AddSuffix(new[] { "NEUTRAL" }, new Suffix<BooleanValue>(() => neutral.Value));

AddSuffix(new[] { "NEUTRAL", "NEUTRALIZE" }, new SetSuffix<BooleanValue>(IsNeutral, v => {if (v) Neutralize();}));
}

private Vector GetPilotTranslation()
Expand Down Expand Up @@ -270,49 +251,36 @@ private static bool ValueToFloat(object value, ref float doubleValue)
return true;
}

private bool CheckKillRotation(string suffixName, object value)
private void Neutralize()
{
if (suffixName.Equals("KILLROTATION", StringComparison.OrdinalIgnoreCase))
{
killRotation.Value = bool.Parse(value.ToString());
return true;
}
killRotation.Value = false;
return false;
}
private bool CheckResetTrim(string suffixName, object value)
{
if (suffixName.Equals("RESETTRIM", StringComparison.OrdinalIgnoreCase))
{
resetTrim.Value = bool.Parse(value.ToString());
return true;
}
resetTrim.Value = false;
return false;
}

private bool CheckNeutral(string suffixName, object value)
{
if (suffixName.Equals("NEUTRALIZE", StringComparison.OrdinalIgnoreCase))
{
ResetControls();
neutral.Value = bool.Parse(value.ToString());
return true;
}
neutral.Value = false;
return false;
ResetControls();
}

private void ResetControls()
{
yaw = default(float);
yawTrim = default(float);
pitch = default(float);
pitchTrim = default(float);
roll = default(float);
rollTrim = default(float);
fore = default(float);
starboard = default(float);
top = default(float);
wheelSteer = default(float);
wheelSteerTrim = default(float);
wheelThrottle = default(float);
wheelThrottleTrim = default(float);
neutral.Value = true;
neutral.SetStale();
}

private BooleanValue IsNeutral()
{
return (yaw == yawTrim && pitch == pitchTrim && roll == rollTrim &&
fore == 0 && starboard == 0 && top == 0 &&
wheelSteer == wheelSteerTrim && wheelThrottle == wheelSteerTrim);

}

private void OnFlyByWire(FlightCtrlState st)
Expand All @@ -322,14 +290,7 @@ private void OnFlyByWire(FlightCtrlState st)
if (neutral.FlushValue)
{
st.Neutralize();
}
}

if (resetTrim.IsStale)
{
if (resetTrim.FlushValue)
{
st.ResetTrim();
neutral.Value = false;
}
}

Expand Down Expand Up @@ -363,7 +324,7 @@ bool IFlightControlParameter.Enabled
{
get
{
return !neutral.Value;
return !IsNeutral();
}
}

Expand Down Expand Up @@ -417,14 +378,12 @@ void IFlightControlParameter.EnableControl(SharedObjects shared)

void IFlightControlParameter.DisableControl(SharedObjects shared)
{
// We'll just neutralize here
neutral.Value = true;
Neutralize();
}

void IFlightControlParameter.DisableControl()
{
// We'll just neutralize here
neutral.Value = true;
Neutralize();
}

void IFlightControlParameter.CopyFrom(IFlightControlParameter origin)
Expand Down