-
Notifications
You must be signed in to change notification settings - Fork 9
/
LaunchSiteManager.cs
377 lines (331 loc) · 12.9 KB
/
LaunchSiteManager.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
using KerbalKonstructs.StaticObjects;
using KerbalKonstructs.API;
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
// R and T LOG
// 14112014 ASH
namespace KerbalKonstructs.LaunchSites
{
public class LaunchSiteManager
{
private static List<LaunchSite> launchSites = new List<LaunchSite>();
public static Texture defaultLaunchSiteLogo = GameDatabase.Instance.GetTexture("medsouz/KerbalKonstructs/Assets/DefaultSiteLogo", false);
public static LaunchSite runway = new LaunchSite("Runway", "Squad", SiteType.SPH, GameDatabase.Instance.GetTexture("medsouz/KerbalKonstructs/Assets/KSCRunway", false), null, "The KSC runway is a concrete runway measuring about 2.5km long and 70m wide, on a magnetic heading of 90/270. It is not uncommon to see burning chunks of metal sliding across the surface.", "Runway", 0, 0, "Open", null, new PSystemSetup.SpaceCenterFacility());
public static LaunchSite launchpad = new LaunchSite("LaunchPad", "Squad", SiteType.VAB, GameDatabase.Instance.GetTexture("medsouz/KerbalKonstructs/Assets/KSCLaunchpad", false), null, "The KSC launchpad is a platform used to fire screaming Kerbals into the kosmos. There was a tower here at one point but for some reason nobody seems to know where it went...", "RocketPad", 0, 0, "Open", null, new PSystemSetup.SpaceCenterFacility());
static LaunchSiteManager()
{
// TODO Expose KSC runway and launchpad descriptions to KerbalKonstructs.cfg so players can change them
// ASH 28102014 - Added category
// ASH 12112014 - Added career open close costs and state
launchSites.Add(runway);
launchSites.Add(launchpad);
}
// KerbTown legacy code - TODO Review and update?
public static void createLaunchSite(StaticObject obj)
{
if (obj.settings.ContainsKey("LaunchSiteName") && obj.gameObject.transform.Find((string) obj.getSetting("LaunchPadTransform")) != null)
{
// Debug.Log("KK: Creating launch site " + obj.getSetting("LaunchSiteName"));
obj.gameObject.transform.name = (string) obj.getSetting("LaunchSiteName");
obj.gameObject.name = (string) obj.getSetting("LaunchSiteName");
foreach (FieldInfo fi in PSystemSetup.Instance.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance))
{
if (fi.FieldType.Name == "SpaceCenterFacility[]")
{
PSystemSetup.SpaceCenterFacility[] facilities = (PSystemSetup.SpaceCenterFacility[])fi.GetValue(PSystemSetup.Instance);
if (PSystemSetup.Instance.GetSpaceCenterFacility((string) obj.getSetting("LaunchSiteName")) == null)
{
PSystemSetup.SpaceCenterFacility newFacility = new PSystemSetup.SpaceCenterFacility();
newFacility.name = "FacilityName";
newFacility.facilityName = (string) obj.getSetting("LaunchSiteName");
newFacility.facilityPQS = ((CelestialBody) obj.getSetting("CelestialBody")).pqsController;
newFacility.facilityTransformName = obj.gameObject.name;
newFacility.pqsName = ((CelestialBody) obj.getSetting("CelestialBody")).pqsController.name;
PSystemSetup.SpaceCenterFacility.SpawnPoint spawnPoint = new PSystemSetup.SpaceCenterFacility.SpawnPoint();
spawnPoint.name = (string) obj.getSetting("LaunchSiteName");
spawnPoint.spawnTransformURL = (string) obj.getSetting("LaunchPadTransform");
newFacility.spawnPoints = new PSystemSetup.SpaceCenterFacility.SpawnPoint[1];
newFacility.spawnPoints[0] = spawnPoint;
PSystemSetup.SpaceCenterFacility[] newFacilities = new PSystemSetup.SpaceCenterFacility[facilities.Length + 1];
for (int i = 0; i < facilities.Length; ++i)
{
newFacilities[i] = facilities[i];
}
newFacilities[newFacilities.Length - 1] = newFacility;
fi.SetValue(PSystemSetup.Instance, newFacilities);
facilities = newFacilities;
Texture logo = null;
Texture icon = null;
if (obj.settings.ContainsKey("LaunchSiteLogo"))
logo = GameDatabase.Instance.GetTexture(obj.model.path + "/" + obj.getSetting("LaunchSiteLogo"), false);
if (logo == null)
logo = defaultLaunchSiteLogo;
if(obj.settings.ContainsKey("LaunchSiteIcon"))
icon = GameDatabase.Instance.GetTexture(obj.model.path + "/" + obj.getSetting("LaunchSiteIcon"), false);
// TODO This is still hard-code and needs to use the API properly
// ASH 12112014 - Added career open close costs
launchSites.Add(new LaunchSite((string)obj.getSetting("LaunchSiteName"), (obj.settings.ContainsKey("LaunchSiteAuthor")) ? (string)obj.getSetting("LaunchSiteAuthor") : (string)obj.model.getSetting("author"), (SiteType)obj.getSetting("LaunchSiteType"), logo, icon, (string)obj.getSetting("LaunchSiteDescription"), (string)obj.getSetting("Category"), (float)obj.getSetting("OpenCost"), (float)obj.getSetting("CloseValue"), "Closed", /*(Vector3)obj.getSetting("RadialPosition"), */obj.gameObject, newFacility));
// Debug.Log("KK: Created launch site \"" + newFacility.name + "\" with transform " + obj.getSetting("LaunchSiteName") + "/" + obj.getSetting("LaunchPadTransform"));
}
else
{
Debug.Log("KK: Launch site " + obj.getSetting("LaunchSiteName") + " already exists.");
}
}
}
MethodInfo updateSitesMI = PSystemSetup.Instance.GetType().GetMethod("SetupFacilities", BindingFlags.NonPublic | BindingFlags.Instance);
if (updateSitesMI == null)
Debug.Log("KK: Failed to find SetupFacilities().");
else
updateSitesMI.Invoke(PSystemSetup.Instance, null);
if (obj.gameObject != null)
CustomSpaceCenter.CreateFromLaunchsite((string)obj.getSetting("LaunchSiteName"), obj.gameObject);
}
else
{
Debug.Log("KK: Launch pad transform \"" + obj.getSetting("LaunchPadTransform") + "\" missing for " + obj.getSetting("LaunchSiteName"));
}
}
// ASH 28102014 Added handling for Category filter
public static List<LaunchSite> getLaunchSites(String usedFilter = "ALL")
{
List<LaunchSite> sites = new List<LaunchSite>();
foreach (LaunchSite site in launchSites)
{
if (usedFilter.Equals("ALL"))
{
sites.Add(site);
}
else
{
if (site.category.Equals(usedFilter))
{
sites.Add(site);
}
}
}
return sites;
}
// ASH 28102014 Added handling for Category filter
public static List<LaunchSite> getLaunchSites(SiteType type, Boolean allowAny = true, String appliedFilter = "ALL")
{
List<LaunchSite> sites = new List<LaunchSite>();
foreach (LaunchSite site in launchSites)
{
if (site.type.Equals(type) || (site.type.Equals(SiteType.Any) && allowAny))
{
if (appliedFilter.Equals("ALL"))
{
sites.Add(site);
}
else
{
if (site.category.Equals(appliedFilter))
{
sites.Add(site);
}
}
}
}
return sites;
}
// Open and close a launchsite
public static void setSiteOpenCloseState(string sSiteName, string sState)
{
List<LaunchSite> sites = LaunchSiteManager.getLaunchSites();
foreach (LaunchSite site in sites)
{
if (site.name == sSiteName)
{
site.openclosestate = sState;
PersistenceFile<LaunchSite>.SaveList(sites, "LAUNCHSITES", "KK");
return;
}
}
}
// Find out if a launchsite is open or closed
public static void getSiteOpenCloseState(string sSiteName, out string sOpenCloseState, out float fOpenCost)
{
List<LaunchSite> sites = LaunchSiteManager.getLaunchSites();
foreach (LaunchSite site in sites)
{
if (site.name == sSiteName)
{
sOpenCloseState = site.openclosestate;
fOpenCost = site.opencost;
return;
}
}
sOpenCloseState = "Open";
fOpenCost = 0;
}
public static void setAllLaunchsitesClosed()
{
List<LaunchSite> sites = LaunchSiteManager.getLaunchSites();
foreach (LaunchSite site in sites)
{
site.openclosestate = "Closed";
}
}
public static float RangeNearestOpenBase = 0f;
public static string NearestOpenBase = "";
public static Boolean isCareerGame()
{
if (HighLogic.CurrentGame.Mode == Game.Modes.CAREER)
{
// disableCareerStrategyLayer is configurable in KerbalKonstructs.cfg
if (!KerbalKonstructs.instance.disableCareerStrategyLayer)
{
return true;
}
else
return false;
}
else
return false;
}
public static float getDistanceToBase(Vector3 position, LaunchSite lTarget)
{
float flRange = 0f;
List<LaunchSite> sites = LaunchSiteManager.getLaunchSites();
foreach (LaunchSite site in sites)
{
if (site == lTarget)
{
var radialposition = site.GameObject.transform.position;
var dist = Vector3.Distance(position, radialposition);
flRange = dist;
}
}
return flRange;
}
// Get the nearest open base and range to it
public static void getNearestOpenBase(Vector3 position, out string sBase, out float flRange, out LaunchSite lNearest)
{
SpaceCenter KSC = SpaceCenter.Instance;
var smallestDist = Vector3.Distance(KSC.gameObject.transform.position, position);
string sNearestBase = "";
string sOpenCloseState = "";
LaunchSite lNearestBase = null;
List<LaunchSite> basesites = LaunchSiteManager.getLaunchSites();
foreach (LaunchSite site in basesites)
{
sOpenCloseState = site.openclosestate;
if (!isCareerGame())
sOpenCloseState = "Open";
if (sOpenCloseState == "Open")
{
if (site.GameObject == null) continue;
var radialposition = site.GameObject.transform.position;
var dist = Vector3.Distance(position, radialposition);
if (site.name == "Runway" || site.name == "LaunchPad")
{ }
else
{
if ((float)dist < (float)smallestDist)
{
{
sNearestBase = site.name;
lNearestBase = site;
smallestDist = dist;
}
}
}
}
}
if (sNearestBase == "")
{
sNearestBase = "KSC";
}
RangeNearestOpenBase = (float)smallestDist;
// Air traffic control messaging
if (KerbalKonstructs.instance.enableATC)
{
if (sNearestBase != NearestOpenBase)
{
if (RangeNearestOpenBase < 25000)
{
NearestOpenBase = sNearestBase;
MessageSystemButton.MessageButtonColor color = MessageSystemButton.MessageButtonColor.BLUE;
MessageSystem.Message m = new MessageSystem.Message("KK ATC", "You have entered the airspace of " + sNearestBase + " ATC. Please keep this channel open and obey all signal lights. Thank you. " + sNearestBase + " Air Traffic Control out.", color, MessageSystemButton.ButtonIcons.MESSAGE);
MessageSystem.Instance.AddMessage(m);
}
else
if (NearestOpenBase != "")
{
// you have left ...
MessageSystemButton.MessageButtonColor color = MessageSystemButton.MessageButtonColor.GREEN;
MessageSystem.Message m = new MessageSystem.Message("KK ATC", "You are now leaving the airspace of " + sNearestBase + ". Safe journey. " + sNearestBase + " Air Traffic Control out.", color, MessageSystemButton.ButtonIcons.MESSAGE);
MessageSystem.Instance.AddMessage(m);
NearestOpenBase = "";
}
}
}
sBase = sNearestBase;
flRange = RangeNearestOpenBase;
lNearest = lNearestBase;
}
public static float RangeNearestBase = 0f;
public static string NearestBase = "";
// Get nearest base, either open or closed, and the range to it
public static void getNearestBase(Vector3 position, out string sBase, out float flRange, out LaunchSite lSite)
{
SpaceCenter KSC = SpaceCenter.Instance;
var smallestDist = Vector3.Distance(KSC.gameObject.transform.position, position);
string sNearestBase = "";
LaunchSite lTargetSite = null;
List<LaunchSite> basesites = LaunchSiteManager.getLaunchSites();
foreach (LaunchSite site in basesites)
{
if (site.GameObject == null) continue;
var radialposition = site.GameObject.transform.position;
var dist = Vector3.Distance(position, radialposition);
if (site.name == "Runway" || site.name == "LaunchPad")
{ }
else
{
if ((float)dist < (float)smallestDist)
{
{
sNearestBase = site.name;
smallestDist = dist;
lTargetSite = site;
}
}
}
}
if (sNearestBase == "")
{
sNearestBase = "KSC";
}
RangeNearestBase = (float)smallestDist;
sBase = sNearestBase;
flRange = RangeNearestBase;
lSite = lTargetSite;
}
public static void setLaunchSite(LaunchSite site)
{
Debug.Log("KK: EditorLogic.fetch.launchSiteName set to " + site.name);
//Trick KSP to think that you launched from Runway or LaunchPad
//I'm sure Squad will break this in the future
//This only works because they use multiple variables to store the same value, basically its black magic.
//--medsouz
if (EditorDriver.editorFacility.Equals(EditorFacility.SPH))
{
site.facility.name = "Runway";
}
else
{
site.facility.name = "LaunchPad";
}
EditorLogic.fetch.launchSiteName = site.name;
}
public static string getCurrentLaunchSite()
{
return EditorLogic.fetch.launchSiteName;
}
public static List<LaunchSite> AllLaunchSites { get { return launchSites; } }
}
}