forked from Nivekk/KOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.cs
153 lines (124 loc) · 4.55 KB
/
Node.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace kOS
{
public class Node : SpecialValue
{
ManeuverNode nodeRef;
Vessel vesselRef;
public double UT;
public double Pro;
public double RadOut;
public double Norm;
public static Dictionary<ManeuverNode, Node> NodeLookup = new Dictionary<ManeuverNode, Node>();
public Node(double ut, double radialOut, double normal, double prograde)
{
this.UT = ut;
this.Pro = prograde;
this.RadOut = radialOut;
this.Norm = normal;
}
public Node(Vessel v, ManeuverNode existingNode)
{
nodeRef = existingNode;
vesselRef = v;
NodeLookup.Add(existingNode, this);
updateValues();
}
public static Node FromExisting(Vessel v, ManeuverNode existingNode)
{
if (NodeLookup.ContainsKey(existingNode)) return NodeLookup[existingNode];
return new Node(v, existingNode);
}
public void AddToVessel(Vessel v)
{
if (nodeRef != null) throw new kOSException("Node has already been added");
vesselRef = v;
nodeRef = v.patchedConicSolver.AddManeuverNode(UT);
UpdateNodeDeltaV();
v.patchedConicSolver.UpdateFlightPlan();
NodeLookup.Add(nodeRef, this);
}
public void UpdateAll()
{
UpdateNodeDeltaV();
if (vesselRef != null) vesselRef.patchedConicSolver.UpdateFlightPlan();
}
private void UpdateNodeDeltaV()
{
if (nodeRef != null)
{
Vector3d dv = new Vector3d(RadOut, Norm, Pro);
nodeRef.DeltaV = dv;
}
}
public void CheckNodeRef()
{
if (nodeRef == null)
{
throw new kOSException("Must attach node first");
}
}
public Vector GetBurnVector()
{
CheckNodeRef();
return new Vector(nodeRef.GetBurnVector(vesselRef.GetOrbit()));
}
private void updateValues()
{
// If this node is attached, and the values on the attached node have chaged, I need to reflect that
if (nodeRef != null)
{
UT = nodeRef.UT;
RadOut = nodeRef.DeltaV.x;
Norm = nodeRef.DeltaV.y;
Pro = nodeRef.DeltaV.z;
}
}
public override object GetSuffix(string suffixName)
{
updateValues();
if (suffixName == "BURNVECTOR") return GetBurnVector();
else if (suffixName == "ETA") return UT - Planetarium.GetUniversalTime();
else if (suffixName == "DELTAV") return GetBurnVector();
else if (suffixName == "PROGRADE") return Pro;
else if (suffixName == "RADIALOUT") return RadOut;
else if (suffixName == "NORMAL") return Norm;
else if (suffixName == "APOAPSIS")
{
if (nodeRef == null) throw new kOSException("Node must be added to flight plan first");
return nodeRef.nextPatch.ApA;
}
else if (suffixName == "PERIAPSIS")
{
if (nodeRef == null) throw new kOSException("Node must be added to flight plan first");
return nodeRef.nextPatch.PeA;
}
return base.GetSuffix(suffixName);
}
public override bool SetSuffix(string suffixName, object value)
{
if (suffixName == "BURNVECTOR" || suffixName == "ETA" || suffixName == "DELTAV") throw new kOSReadOnlyException(suffixName);
else if (suffixName == "PROGRADE") { Pro = (double)value; UpdateAll(); return true; }
else if (suffixName == "RADIALOUT") { RadOut = (double)value; UpdateAll(); return true; }
else if (suffixName == "NORMAL") { Norm = (double)value; UpdateAll(); return true; }
return false;
}
public void Remove()
{
if (nodeRef != null)
{
NodeLookup.Remove(nodeRef);
vesselRef.patchedConicSolver.RemoveManeuverNode(nodeRef);
nodeRef = null;
vesselRef = null;
}
}
public override string ToString()
{
return "NODE(" + UT + "," + RadOut + "," + Norm + "," + Pro + ")";
}
}
}