-
Notifications
You must be signed in to change notification settings - Fork 9
/
GroundConstructionScenario.cs
273 lines (248 loc) · 9.65 KB
/
GroundConstructionScenario.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
// GroundConstructionScenario.cs
//
// Author:
// Allis Tauri <[email protected]>
//
// Copyright (c) 2016 Allis Tauri
using System;
using System.Linq;
using System.Collections.Generic;
using UnityEngine;
using AT_Utils;
namespace GroundConstruction
{
[KSPScenario(ScenarioCreationOptions.AddToAllGames, new []
{
GameScenes.SPACECENTER,
GameScenes.FLIGHT,
GameScenes.TRACKSTATION,
})]
public class GroundConstructionScenario : ScenarioModule
{
private static Globals GLB { get { return Globals.Instance; } }
private static SortedDictionary<Guid, WorkshopManager>
Workshops = new SortedDictionary<Guid, WorkshopManager>();
private static SortedDictionary<string, Guid> DisplayOrder = new SortedDictionary<string, Guid>();
private static List<string> CelestialBodies = new List<string>();
private static string CelestialBodyTab = "";
public static bool ShowDeployHint;
public static bool AutoSave = true;
private double now = -1;
public static void SaveGame(string name)
{
if(!AutoSave)
return;
Utils.SaveGame(name);
}
public static void CheckinVessel(WorkshopManager workshop_manager)
{
if(workshop_manager.Vessel == null || workshop_manager.Empty)
return;
Workshops[workshop_manager.VesselID] = workshop_manager;
remove_display_entry(workshop_manager.VesselID);
DisplayOrder[workshop_manager.DisplayID] = workshop_manager.VesselID;
}
public static void CheckoutVessel(WorkshopManager workshop_manager)
{
if(workshop_manager.Vessel == null)
return;
Workshops.Remove(workshop_manager.VesselID);
DisplayOrder.Remove(workshop_manager.DisplayID);
}
public static void CheckoutVessel(Guid vesselID)
{
Workshops.Remove(vesselID);
remove_display_entry(vesselID);
}
private static void remove_display_entry(Guid vesselID)
{
var del = DisplayOrder.FirstOrDefault(i => i.Value == vesselID);
if(!string.IsNullOrEmpty(del.Key))
DisplayOrder.Remove(del.Key);
}
private static bool recheck_workshops()
{
if(FlightGlobals.Vessels != null && FlightGlobals.Vessels.Count > 0)
{
var tab_valid = false;
var bodies = new HashSet<string>();
var del = new List<Guid>();
foreach(var workshop in Workshops)
{
if(workshop.Value != null
&& workshop.Value.Vessel != null
&& workshop.Value.VesselID == workshop.Key
&& !workshop.Value.Empty)
{
if(workshop.Value.isOperable)
{
var cb = workshop.Value.CB;
bodies.Add(cb);
tab_valid |= cb == CelestialBodyTab;
}
}
else
del.Add(workshop.Key);
}
del.ForEach(CheckoutVessel);
CelestialBodies.Clear();
CelestialBodies.AddRange(bodies);
CelestialBodies.Sort();
if(!tab_valid && CelestialBodies.Count > 0)
CelestialBodyTab = CelestialBodies[0];
return true;
}
return false;
}
// Analysis disable once FunctionNeverReturns
private IEnumerator<YieldInstruction> slow_update()
{
while(true)
{
recheck_workshops();
var finished = false;
now = Planetarium.GetUniversalTime();
foreach(var workshop in Workshops.Values)
finished = workshop.CheckETA(now) || finished;
if(finished)
Utils.StopTimeWarp(!HighLogic.LoadedSceneIsFlight);
yield return new WaitForSeconds(1);
}
}
public override void OnAwake()
{
base.OnAwake();
StartCoroutine(slow_update());
}
private void OnDestroy()
{
Utils.LockIfMouseOver(LockName, WindowPos, false);
}
public override void OnSave(ConfigNode node)
{
base.OnSave(node);
node.AddValue(nameof(ShowDeployHint), ShowDeployHint);
node.AddValue(nameof(AutoSave), AutoSave);
}
public override void OnLoad(ConfigNode node)
{
base.OnLoad(node);
var val = node.GetValue(nameof(ShowDeployHint));
if(!string.IsNullOrEmpty(val))
bool.TryParse(val, out ShowDeployHint);
val = node.GetValue(nameof(AutoSave));
if(!string.IsNullOrEmpty(val))
bool.TryParse(val, out AutoSave);
}
private void Update()
{
if(switchto != null)
{
if(!switchto.SwitchTo())
CheckoutVessel(switchto);
switchto = null;
}
}
#region GUI
private const float width = 500;
private const float height = 120;
private const float cb_width = 60;
private const float workshops_width = width - cb_width - 10;
private static bool show_window;
public static void ShowWindow(bool show)
{
show_window = show;
}
public static void ToggleWindow()
{
show_window = !show_window;
}
private WorkshopManager switchto = null;
private Vector2 workshops_scroll = Vector2.zero;
private Vector2 cb_scroll = Vector2.zero;
private Rect WindowPos = new Rect(Screen.width - width - 100, 0, Screen.width / 4, Screen.height / 4);
private void main_window(int WindowID)
{
GUILayout.BeginVertical();
GUILayout.BeginHorizontal();
if(CelestialBodies.Count > 0)
{
GUILayout.BeginVertical();
cb_scroll = GUILayout.BeginScrollView(cb_scroll,
GUILayout.Height(height),
GUILayout.Width(cb_width + 10));
foreach(var cb in CelestialBodies)
{
if(GUILayout.Button(new GUIContent(cb, "Show workshops on " + cb),
CelestialBodyTab == cb ? Styles.enabled : Styles.active,
GUILayout.Width(cb_width)))
CelestialBodyTab = cb;
}
GUILayout.EndScrollView();
GUILayout.EndVertical();
GUILayout.BeginVertical(Styles.white);
workshops_scroll = GUILayout.BeginScrollView(workshops_scroll,
GUILayout.Height(height),
GUILayout.Width(workshops_width));
foreach(var item in DisplayOrder.Values.ToList())
{
var info = Workshops[item];
if(info.CB != CelestialBodyTab)
continue;
GUILayout.BeginHorizontal();
info.Draw();
if(info.IsActive)
GUILayout.Label(new GUIContent("◉", "This is the active vessel"),
Styles.inactive,
GUILayout.ExpandWidth(false));
else if(GUILayout.Button(new GUIContent("◉", "Switch to this workshop"),
Styles.enabled_button,
GUILayout.ExpandWidth(false)))
switchto = info;
GUILayout.EndHorizontal();
}
GUILayout.EndScrollView();
GUILayout.EndVertical();
}
else
GUILayout.Label("No Operational Workshops", Styles.white, GUILayout.ExpandWidth(true));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
Utils.ButtonSwitch("Show Deploy Hints",
ref ShowDeployHint,
"Draw visual cues to help position a DIY Kit",
GUILayout.ExpandWidth(false));
Utils.ButtonSwitch("Auto Save",
ref AutoSave,
"Save the game before deploying containers and spawning new vessels",
GUILayout.ExpandWidth(false));
GUILayout.FlexibleSpace();
if(GUILayout.Button("Close", Styles.close_button, GUILayout.ExpandWidth(false)))
show_window = false;
GUILayout.EndHorizontal();
GUILayout.EndVertical();
GUIWindowBase.TooltipsAndDragWindow();
}
private const string LockName = "GroundConstructionScenario";
private void OnGUI()
{
if(Event.current.type != EventType.Layout && Event.current.type != EventType.Repaint)
return;
if(show_window && GUIWindowBase.HUD_enabled)
{
Styles.Init();
Utils.LockIfMouseOver(LockName, WindowPos);
WindowPos = GUILayout.Window(GetInstanceID(),
WindowPos,
main_window,
"Workshops",
GUILayout.Width(width),
GUILayout.Height(height))
.clampToScreen();
}
else
Utils.LockIfMouseOver(LockName, WindowPos, false);
}
#endregion
}
}