-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickLauncher.cs
262 lines (228 loc) · 8.13 KB
/
QuickLauncher.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
using System;
using System.Collections.Generic;
using UnityEngine;
namespace BeenThereDoneThat
{
[KSPScenario(ScenarioCreationOptions.AddToAllGames, new GameScenes[]
{
GameScenes.FLIGHT,
GameScenes.TRACKSTATION,
GameScenes.SPACECENTER,
GameScenes.EDITOR
})]
public class QuickLauncher : ScenarioModule
{
public static QuickLauncher Instance
{
get;
private set;
}
public override void OnAwake()
{
Debug.Log("[BeenThereDoneThat]: AWAKING ORBITCONTROLLER");
Instance = this;
}
public override void OnSave(ConfigNode node)
{
Debug.Log("[BeenThereDoneThat]: SAVING ORBITCONTROLLER");
}
public override void OnLoad(ConfigNode node)
{
Debug.Log("[BeenThereDoneThat]: LOADING ORBITCONTROLLER");
}
public bool Split(ProtoVessel protoVessel, out ProtoLaunchVehicle launchVehicle, out ProtoPayload payload)
{
ProtoPartSnapshot payloadSeparator = FindPayloadSeparatorPart(protoVessel.protoPartSnapshots);
if (payloadSeparator == null)
{
Debug.Log("[BeenThereDoneThat]: No payloadseparator found");
launchVehicle = null;
payload = null;
return false;
}
ProtoPartSnapshotTree tree = new ProtoPartSnapshotTree(protoVessel.protoPartSnapshots);
List<ProtoPartSnapshot> payloadParts;
List<ProtoPartSnapshot> launchVehicleParts;
tree.Split(payloadSeparator, out launchVehicleParts, out payloadParts);
payload = new ProtoPayload(payloadParts);
launchVehicle = new ProtoLaunchVehicle(payloadSeparator, launchVehicleParts);
return true;
}
public bool Split(List<Part> parts, out LaunchVehicle launchVehicle, out Payload payload)
{
Part payloadSeparator = FindPayloadSeparatorPart(parts);
if (payloadSeparator == null)
{
Debug.Log("[BeenThereDoneThat]: No payloadseparator found");
launchVehicle = null;
payload = null;
return false;
}
Part root = parts[0].localRoot;
List<Part> payloadParts = new List<Part>();
List<Part> launchVehicleParts = new List<Part>();
PartTreeHelper.AddBreadthFirst(root, payloadParts, payloadSeparator);
PartTreeHelper.AddBreadthFirst(payloadSeparator, launchVehicleParts);
launchVehicle = new LaunchVehicle(payloadSeparator, launchVehicleParts);
payload = new Payload(payloadParts);
return true;
}
public ProtoPartSnapshot FindPayloadSeparatorPart(List<ProtoPartSnapshot> parts)
{
foreach (ProtoPartSnapshot part in parts)
{
ProtoPartModuleSnapshot module = part.FindModule("PayloadSeparatorPart");
if (module == null)
{
continue;
}
if (bool.Parse(module.moduleValues.GetValue("isPayloadSeparator")))
{
return part;
}
}
return null;
}
public Part FindPayloadSeparatorPart(List<Part> parts)
{
foreach (Part part in parts)
{
foreach (PayloadSeparatorPart module in part.FindModulesImplementing<PayloadSeparatorPart>())
{
if (module.isPayloadSeparator)
{
return part;
}
}
}
return null;
}
}
internal class PartTreeHelper
{
class PartNameComparer : IComparer<Part>
{
public int Compare(Part x, Part y)
{
return x.name.CompareTo(y.name);
}
}
internal static void AddBreadthFirst(Part start, List<Part> parts)
{
AddBreadthFirst(start, parts, null);
}
internal static void AddBreadthFirst(Part start, List<Part> parts, Part stopAt)
{
parts.Add(start);
List<Part> children = new List<Part>(start.children);
children.Sort(new PartNameComparer());
foreach (Part child in children)
{
if (child != stopAt)
{
AddBreadthFirst(child, parts, stopAt);
}
}
}
}
internal class ProtoPartSnapshotTree
{
Dictionary<uint, ProtoPartSnapshotNode> tree;
ProtoPartSnapshotNode root;
List<ProtoPartSnapshot> protoPartSnapshots;
internal ProtoPartSnapshotTree(List<ProtoPartSnapshot> snapshots)
{
tree = new Dictionary<uint, ProtoPartSnapshotNode>();
protoPartSnapshots = snapshots;
root = null;
foreach (ProtoPartSnapshot part in protoPartSnapshots)
{
CreateNode(part);
}
}
internal ProtoPartSnapshotNode CreateNode(ProtoPartSnapshot part)
{
ProtoPartSnapshotNode node = null;
ProtoPartSnapshotNode parentNode = null;
ProtoPartSnapshot parent = protoPartSnapshots[part.parentIdx];
if (parent != part)
{
parentNode = CreateNode(parent);
}
if (!tree.TryGetValue(part.persistentId, out node))
{
node = new ProtoPartSnapshotNode(part, parentNode);
tree.Add(part.persistentId, node);
}
if (parentNode == null)
{
root = node;
}
return node;
}
internal void Split(ProtoPartSnapshot separatorPart, out List<ProtoPartSnapshot> launchVehicleParts, out List<ProtoPartSnapshot> payloadParts)
{
ProtoPartSnapshotNode splitAt = null;
if (!tree.TryGetValue(separatorPart.persistentId, out splitAt))
{
launchVehicleParts = null;
payloadParts = null;
Debug.Log(string.Format("Could not split tree, part not found {0}", separatorPart.partName));
return;
}
launchVehicleParts = new List<ProtoPartSnapshot>();
payloadParts = new List<ProtoPartSnapshot>();
root.AddBreadthFirst(payloadParts, splitAt);
splitAt.AddBreadthFirst(launchVehicleParts);
}
}
internal class ProtoPartSnapshotNode : IComparable
{
ProtoPartSnapshot part;
ProtoPartSnapshotNode parent;
List<ProtoPartSnapshotNode> children;
int IComparable.CompareTo(object obj)
{
if (obj == null)
{
return 1;
}
ProtoPartSnapshotNode otherNode = obj as ProtoPartSnapshotNode;
if (otherNode == null)
{
throw new ArgumentException("Object is not a ProtoPartSnapshotNode");
}
return part.partName.CompareTo(otherNode.part.partName);
}
internal ProtoPartSnapshotNode(ProtoPartSnapshot protoPart, ProtoPartSnapshotNode parentNode)
{
part = protoPart;
parent = parentNode;
children = new List<ProtoPartSnapshotNode>();
if (parent != null)
{
parent.AddChild(this);
}
}
internal void AddChild(ProtoPartSnapshotNode child)
{
children.Add(child);
}
internal void AddBreadthFirst(List<ProtoPartSnapshot> parts)
{
AddBreadthFirst(parts, null);
}
internal void AddBreadthFirst(List<ProtoPartSnapshot> parts, ProtoPartSnapshotNode stopAt)
{
parts.Add(part);
children.Sort();
foreach (ProtoPartSnapshotNode child in children)
{
if (child != stopAt)
{
child.AddBreadthFirst(parts, stopAt);
}
}
}
}
}