Skip to content

Commit 9032b3f

Browse files
author
Sander Hoksbergen
committed
FlightComputer rewrite from scratch; Color options in config; Time warp now drops out 1 second before the event triggers.
1 parent e0b36fd commit 9032b3f

24 files changed

+862
-760
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace RemoteTech
7+
{
8+
public abstract class AbstractCommand : ICommand
9+
{
10+
public double TimeStamp { get; set; }
11+
public virtual double ExtraDelay { get; set; }
12+
public virtual String Description {
13+
get
14+
{
15+
double delay = Math.Max(TimeStamp - RTUtil.GameTime, 0);
16+
if (delay > 0 || ExtraDelay > 0)
17+
{
18+
var extra = ExtraDelay > 0 ? String.Format("{0} + {1}", RTUtil.FormatDuration(delay), RTUtil.FormatDuration(ExtraDelay))
19+
: RTUtil.FormatDuration(delay);
20+
return "Signal delay: " + extra;
21+
}
22+
return "";
23+
}
24+
}
25+
public virtual int Priority { get { return 255; } }
26+
27+
// true: move to active.
28+
public virtual bool Pop(FlightComputer f) { return false; }
29+
30+
// true: delete afterwards.
31+
public virtual bool Execute(FlightComputer f, FlightCtrlState fcs) { return true; }
32+
33+
public int CompareTo(ICommand dc)
34+
{
35+
return TimeStamp.CompareTo(dc.TimeStamp);
36+
}
37+
}
38+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace RemoteTech
7+
{
8+
public class ActionGroupCommand : AbstractCommand
9+
{
10+
public KSPActionGroup ActionGroup { get; private set; }
11+
12+
public override string Description
13+
{
14+
get { return "Toggle " + ActionGroup + Environment.NewLine + base.Description; }
15+
}
16+
17+
public override bool Pop(FlightComputer f)
18+
{
19+
f.Vessel.ActionGroups.ToggleGroup(ActionGroup);
20+
if (ActionGroup == KSPActionGroup.Stage && !(f.Vessel == FlightGlobals.ActiveVessel && FlightInputHandler.fetch.stageLock))
21+
{
22+
Staging.ActivateNextStage();
23+
ResourceDisplay.Instance.Refresh();
24+
}
25+
if (ActionGroup == KSPActionGroup.RCS && f.Vessel == FlightGlobals.ActiveVessel)
26+
{
27+
FlightInputHandler.fetch.rcslock = !FlightInputHandler.RCSLock;
28+
}
29+
30+
return false;
31+
}
32+
33+
public static ActionGroupCommand WithGroup(KSPActionGroup group)
34+
{
35+
return new ActionGroupCommand()
36+
{
37+
ActionGroup = group,
38+
TimeStamp = RTUtil.GameTime,
39+
};
40+
}
41+
}
42+
}
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using UnityEngine;
6+
7+
namespace RemoteTech
8+
{
9+
public enum FlightMode
10+
{
11+
Off,
12+
KillRot,
13+
AttitudeHold,
14+
AltitudeHold,
15+
Rover,
16+
}
17+
18+
public enum FlightAttitude
19+
{
20+
Prograde,
21+
Retrograde,
22+
NormalPlus,
23+
NormalMinus,
24+
RadialPlus,
25+
RadialMinus,
26+
Surface,
27+
}
28+
29+
public enum ReferenceFrame
30+
{
31+
Orbit,
32+
Surface,
33+
TargetVelocity,
34+
TargetParallel,
35+
North,
36+
Maneuver,
37+
World,
38+
}
39+
40+
public class AttitudeCommand : AbstractCommand
41+
{
42+
public static readonly Dictionary<FlightMode, String> FormatMode = new Dictionary<FlightMode, String>()
43+
{
44+
{ FlightMode.Off, "Mode: Off," },
45+
{ FlightMode.KillRot, "Mode: Kill rotation" },
46+
{ FlightMode.AttitudeHold, "Mode: Hold {0} {1}" },
47+
{ FlightMode.AltitudeHold, "Mode: Hold {0}" },
48+
{ FlightMode.Rover, "" },
49+
};
50+
51+
public static readonly Dictionary<FlightAttitude, String> FormatAttitude = new Dictionary<FlightAttitude, String>()
52+
{
53+
{ FlightAttitude.Prograde, "Prograde" },
54+
{ FlightAttitude.Retrograde, "Retrograde" },
55+
{ FlightAttitude.RadialMinus, "Radial -" },
56+
{ FlightAttitude.RadialPlus, "Radial +" },
57+
{ FlightAttitude.NormalMinus, "Normal -" },
58+
{ FlightAttitude.NormalPlus, "Normal +" },
59+
{ FlightAttitude.Surface, "Direction" },
60+
};
61+
62+
public static readonly Dictionary<ReferenceFrame, String> FormatReference = new Dictionary<ReferenceFrame, String>()
63+
{
64+
{ ReferenceFrame.Orbit, "OBT" },
65+
{ ReferenceFrame.Surface, "SRF" },
66+
{ ReferenceFrame.TargetVelocity, "RVEL" },
67+
{ ReferenceFrame.TargetParallel, "TGT" },
68+
{ ReferenceFrame.North, "North" },
69+
{ ReferenceFrame.Maneuver, "Maneuver" },
70+
{ ReferenceFrame.World, "World" },
71+
};
72+
73+
public FlightMode Mode { get; set; }
74+
public FlightAttitude Attitude { get; set; }
75+
public ReferenceFrame Frame { get; set; }
76+
public Quaternion Orientation { get; set; }
77+
public float Altitude { get; set; }
78+
public override int Priority { get { return 0; } }
79+
80+
public override string Description
81+
{
82+
get
83+
{
84+
String res = "";
85+
switch (Mode)
86+
{
87+
default: res = FormatMode[Mode]; break;
88+
case FlightMode.AltitudeHold: res = String.Format(FormatMode[Mode], RTUtil.FormatSI(Altitude, "m")); break;
89+
case FlightMode.AttitudeHold:
90+
if (Attitude == FlightAttitude.Surface)
91+
{
92+
res = String.Format(FormatMode[Mode], Orientation.eulerAngles.x.ToString("F1") + "°, " +
93+
(360 - Orientation.eulerAngles.y).ToString("F1") + "°, " +
94+
RTUtil.Format360To180(180 - Orientation.eulerAngles.z).ToString("F1") + "°", "");
95+
break;
96+
}
97+
res = String.Format(FormatMode[Mode], FormatReference[Frame], FormatAttitude[Attitude]);
98+
break;
99+
}
100+
return res + Environment.NewLine + base.Description;
101+
}
102+
}
103+
104+
public override bool Pop(FlightComputer f)
105+
{
106+
if (Mode == FlightMode.KillRot)
107+
{
108+
Orientation = f.Vessel.transform.rotation;
109+
}
110+
return true;
111+
}
112+
113+
public override bool Execute(FlightComputer f, FlightCtrlState fcs)
114+
{
115+
switch (Mode)
116+
{
117+
case FlightMode.Off:
118+
break;
119+
case FlightMode.KillRot:
120+
FlightCore.HoldOrientation(fcs, f, Orientation * Quaternion.AngleAxis(90, Vector3.left));
121+
break;
122+
case FlightMode.AttitudeHold:
123+
FlightCore.HoldAttitude(fcs, f, Frame, Attitude, Orientation);
124+
break;
125+
case FlightMode.AltitudeHold:
126+
break;
127+
}
128+
129+
return false;
130+
}
131+
132+
public static AttitudeCommand Off()
133+
{
134+
return new AttitudeCommand()
135+
{
136+
Mode = FlightMode.Off,
137+
Attitude = FlightAttitude.Prograde,
138+
Frame = ReferenceFrame.World,
139+
Orientation = Quaternion.identity,
140+
Altitude = Single.NaN,
141+
TimeStamp = RTUtil.GameTime,
142+
};
143+
}
144+
145+
public static AttitudeCommand KillRot()
146+
{
147+
return new AttitudeCommand()
148+
{
149+
Mode = FlightMode.KillRot,
150+
Attitude = FlightAttitude.Prograde,
151+
Frame = ReferenceFrame.World,
152+
Orientation = Quaternion.identity,
153+
Altitude = Single.NaN,
154+
TimeStamp = RTUtil.GameTime,
155+
};
156+
}
157+
158+
public static AttitudeCommand ManeuverNode()
159+
{
160+
return new AttitudeCommand()
161+
{
162+
Mode = FlightMode.AttitudeHold,
163+
Attitude = FlightAttitude.Prograde,
164+
Frame = ReferenceFrame.Maneuver,
165+
Orientation = Quaternion.identity,
166+
Altitude = Single.NaN,
167+
TimeStamp = RTUtil.GameTime,
168+
};
169+
}
170+
171+
public static AttitudeCommand WithAttitude(FlightAttitude att, ReferenceFrame frame)
172+
{
173+
return new AttitudeCommand()
174+
{
175+
Mode = FlightMode.AttitudeHold,
176+
Attitude = att,
177+
Frame = frame,
178+
Orientation = Quaternion.identity,
179+
Altitude = Single.NaN,
180+
TimeStamp = RTUtil.GameTime,
181+
};
182+
}
183+
184+
public static AttitudeCommand WithAltitude(float height)
185+
{
186+
return new AttitudeCommand()
187+
{
188+
Mode = FlightMode.AltitudeHold,
189+
Attitude = FlightAttitude.Prograde,
190+
Frame = ReferenceFrame.North,
191+
Orientation = Quaternion.identity,
192+
Altitude = height,
193+
TimeStamp = RTUtil.GameTime,
194+
};
195+
}
196+
197+
public static AttitudeCommand WithSurface(double pitch, double yaw, double roll)
198+
{
199+
Quaternion rotation = Quaternion.Euler(new Vector3d(Double.IsNaN(pitch) ? 0 : pitch,
200+
Double.IsNaN(yaw) ? 0 : -yaw,
201+
Double.IsNaN(roll) ? 0 : 180 - roll));
202+
return new AttitudeCommand()
203+
{
204+
Mode = FlightMode.AttitudeHold,
205+
Attitude = FlightAttitude.Surface,
206+
Frame = ReferenceFrame.North,
207+
Orientation = rotation,
208+
Altitude = Single.NaN,
209+
TimeStamp = RTUtil.GameTime,
210+
};
211+
}
212+
}
213+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace RemoteTech
7+
{
8+
public class BurnCommand : AbstractCommand
9+
{
10+
public float Throttle { get; set; }
11+
public double Duration { get; set; }
12+
public double DeltaV { get; set; }
13+
public override int Priority { get { return 2; } }
14+
15+
public override String Description
16+
{
17+
get
18+
{
19+
return String.Format("Burn {0}, {1}", Throttle.ToString("P2"), Duration > 0 ? RTUtil.FormatDuration(Duration) : (DeltaV.ToString("F2") + "m/s")) + Environment.NewLine + base.Description;
20+
}
21+
}
22+
23+
public override bool Pop(FlightComputer f)
24+
{
25+
return true;
26+
}
27+
28+
public override bool Execute(FlightComputer f, FlightCtrlState fcs)
29+
{
30+
if (Duration > 0)
31+
{
32+
fcs.mainThrottle = Throttle;
33+
Duration -= TimeWarp.deltaTime;
34+
}
35+
else if (DeltaV > 0)
36+
{
37+
fcs.mainThrottle = Throttle;
38+
DeltaV -= (Throttle * FlightCore.GetTotalThrust(f.Vessel) / f.Vessel.GetTotalMass()) * TimeWarp.deltaTime;
39+
}
40+
else
41+
{
42+
fcs.mainThrottle = 0.0f;
43+
return true;
44+
}
45+
return false;
46+
}
47+
48+
public static BurnCommand Off()
49+
{
50+
return new BurnCommand()
51+
{
52+
Throttle = Single.NaN,
53+
Duration = 0,
54+
DeltaV = 0,
55+
TimeStamp = RTUtil.GameTime,
56+
};
57+
}
58+
59+
public static BurnCommand WithDuration(float throttle, double duration)
60+
{
61+
return new BurnCommand()
62+
{
63+
Throttle = throttle,
64+
Duration = duration,
65+
DeltaV = 0,
66+
TimeStamp = RTUtil.GameTime,
67+
};
68+
}
69+
70+
public static BurnCommand WithDeltaV(float throttle, double delta)
71+
{
72+
return new BurnCommand()
73+
{
74+
Throttle = throttle,
75+
Duration = 0,
76+
DeltaV = delta,
77+
TimeStamp = RTUtil.GameTime,
78+
};
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)