-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathForScience.cs
More file actions
272 lines (225 loc) · 12.4 KB
/
ForScience.cs
File metadata and controls
272 lines (225 loc) · 12.4 KB
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
using KSP.UI.Screens;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace ForScience
{
[KSPAddon(KSPAddon.Startup.Flight, false)]
class ForScience : MonoBehaviour
{
//GUI
ApplicationLauncherButton FSAppButton;
//states
Vessel stateVessel;
CelestialBody stateBody;
string stateBiome;
ExperimentSituations stateSituation = 0;
//thread control
bool autoTransfer = true;
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
// to do list
//
// integrate science lab
// allow a user specified container to hold data
// transmit data from probes automaticly
void Awake()
{
GameEvents.onGUIApplicationLauncherReady.Add(SetupAppButton);
}
void OnDestroy()
{
GameEvents.onGUIApplicationLauncherReady.Remove(SetupAppButton);
if (FSAppButton != null) ApplicationLauncher.Instance.RemoveModApplication(FSAppButton);
}
void SetupAppButton()
{
if (FSAppButton == null)
{
FSAppButton = ApplicationLauncher.Instance.AddModApplication(
onTrue: ToggleCollection,
onFalse: ToggleCollection,
onHover: null,
onHoverOut: null,
onEnable: null,
onDisable: null,
visibleInScenes: ApplicationLauncher.AppScenes.FLIGHT,
texture: GetIconTexture(autoTransfer)
);
}
}
void FixedUpdate() // running in physics update so that the vessel is always in a valid state to check for science.
{
// this is the primary logic that controls when to do what, so we aren't contstantly eating cpu
if (FlightGlobals.ActiveVessel.FindPartModulesImplementing<ModuleScienceContainer>().Any() == false)
{
// Check if any science containers are on the vessel, if not, remove the app button
if (FSAppButton != null) ApplicationLauncher.Instance.RemoveModApplication(FSAppButton);
}
else if (HighLogic.CurrentGame.Mode == Game.Modes.CAREER | HighLogic.CurrentGame.Mode == Game.Modes.SCIENCE_SANDBOX) // only modes with science mechanics will run
{
if (FSAppButton == null) SetupAppButton();
if (autoTransfer) // if we've enabled the app to run, on by default, the toolbar button toggles this.
{
TransferScience();// always move experiment data to science container, mostly for manual experiments
if (StatesHaveChanged()) // if we are in a new state, we will check and run experiments
{
RunScience();
}
}
}
}
void TransferScience() // automaticlly find, transer and consolidate science data on the vessel
{
if (ActiveContainer().GetActiveVesselDataCount() != ActiveContainer().GetScienceCount()) // only actually transfer if there is data to move
{
Debug.Log("[ForScience!] Transfering science to container.");
ActiveContainer().StoreData(GetExperimentList().Cast<IScienceDataContainer>().ToList(), true); // this is what actually moves the data to the active container
var containerstotransfer = GetContainerList(); // a temporary list of our containers
containerstotransfer.Remove(ActiveContainer()); // we need to remove the container we storing the data in because that would be wierd and buggy
ActiveContainer().StoreData(containerstotransfer.Cast<IScienceDataContainer>().ToList(), true); // now we store all data from other containers
}
}
void RunScience() // this is primary business logic for finding and running valid experiments
{
if (GetExperimentList() == null) // hey, it can happen!
{
Debug.Log("[ForScience!] There are no experiments.");
}
else
{
foreach (ModuleScienceExperiment currentExperiment in GetExperimentList()) // loop through all the experiments onboard
{
Debug.Log("[ForScience!] Checking experiment: " + CurrentScienceSubject(currentExperiment.experiment).id);
if (ActiveContainer().HasData(NewScienceData(currentExperiment))) // skip data we already have onboard
{
Debug.Log("[ForScience!] Skipping: We already have that data onboard.");
}
else if (!SurfaceSamplesUnlocked() && currentExperiment.experiment.id == "surfaceSample") // check to see is surface samples are unlocked
{
Debug.Log("[ForScience!] Skipping: Surface Samples are not unlocked.");
}
else if (!currentExperiment.rerunnable && !IsScientistOnBoard()) // no cheating goo and materials here
{
Debug.Log("[ForScience!] Skipping: Experiment is not repeatable.");
}
else if (!currentExperiment.experiment.IsAvailableWhile(CurrentSituation(), CurrentBody())) // this experiement isn't available here so we skip it
{
Debug.Log("[ForScience!] Skipping: Experiment is not available for this situation/atmosphere.");
}
// TODO - Science Labs can use zero value science , so do not skip it if there is a lab on board
// as a temporary workaround, if there is a scientist on board it will still gather the data.
else if (CurrentScienceValue(currentExperiment) >= 0.1 || IsScientistOnBoard())
{
Debug.Log("[ForScience!] Running experiment: " + CurrentScienceSubject(currentExperiment.experiment).id);
//manually add data to avoid deployexperiment state issues
ActiveContainer().AddData(NewScienceData(currentExperiment));
}
else // this experiment has no more value so we skip it
{
Debug.Log("[ForScience!] Skipping: No more science is available: ");
}
}
}
}
private bool SurfaceSamplesUnlocked() // checking that the appropriate career unlocks are flagged
{
return GameVariables.Instance.UnlockedEVA(ScenarioUpgradeableFacilities.GetFacilityLevel(SpaceCenterFacility.AstronautComplex))
&& GameVariables.Instance.UnlockedFuelTransfer(ScenarioUpgradeableFacilities.GetFacilityLevel(SpaceCenterFacility.ResearchAndDevelopment));
}
float CurrentScienceValue(ModuleScienceExperiment currentExperiment) // the ammount of science an experiment should return
{
return ResearchAndDevelopment.GetScienceValue(
currentExperiment.experiment.baseValue * currentExperiment.experiment.dataScale,
CurrentScienceSubject(currentExperiment.experiment));
}
ScienceData NewScienceData(ModuleScienceExperiment currentExperiment) // construct our own science data for an experiment
{
return new ScienceData(
amount: currentExperiment.experiment.baseValue * CurrentScienceSubject(currentExperiment.experiment).dataScale,
xmitValue: currentExperiment.xmitDataScalar,
xmitBonus: 0f,
id: CurrentScienceSubject(currentExperiment.experiment).id,
dataName: CurrentScienceSubject(currentExperiment.experiment).title
);
}
Vessel CurrentVessel() // dur :P
{
return FlightGlobals.ActiveVessel;
}
CelestialBody CurrentBody()
{
return FlightGlobals.ActiveVessel.mainBody;
}
ExperimentSituations CurrentSituation()
{
return ScienceUtil.GetExperimentSituation(FlightGlobals.ActiveVessel);
}
string CurrentBiome() // some crazy nonsense to get the actual biome string
{
if (FlightGlobals.ActiveVessel != null)
if (FlightGlobals.ActiveVessel.mainBody.BiomeMap != null)
return !string.IsNullOrEmpty(FlightGlobals.ActiveVessel.landedAt)
? Vessel.GetLandedAtString(FlightGlobals.ActiveVessel.landedAt)
: ScienceUtil.GetExperimentBiome(FlightGlobals.ActiveVessel.mainBody,
FlightGlobals.ActiveVessel.latitude, FlightGlobals.ActiveVessel.longitude);
return string.Empty;
}
ScienceSubject CurrentScienceSubject(ScienceExperiment experiment)
{
string fixBiome = string.Empty; // some biomes don't have 4th string, so we just put an empty in to compare strings later
if (experiment.BiomeIsRelevantWhile(CurrentSituation())) fixBiome = CurrentBiome();// for those that do, we add it to the string
return ResearchAndDevelopment.GetExperimentSubject(experiment, CurrentSituation(), CurrentBody(), fixBiome, null);//ikr!, we pretty much did all the work already, jeez
}
ModuleScienceContainer ActiveContainer() // set the container to gather all science data inside, usualy this is the root command pod of the oldest vessel
{
return FlightGlobals.ActiveVessel.FindPartModulesImplementing<ModuleScienceContainer>().FirstOrDefault();
}
List<ModuleScienceExperiment> GetExperimentList() // a list of all experiments
{
return FlightGlobals.ActiveVessel.FindPartModulesImplementing<ModuleScienceExperiment>();
}
List<ModuleScienceContainer> GetContainerList() // a list of all science containers
{
return FlightGlobals.ActiveVessel.FindPartModulesImplementing<ModuleScienceContainer>(); // list of all experiments onboard
}
bool StatesHaveChanged() // Track our vessel state, it is used for thread control to know when to fire off new experiments since there is no event for this
{
if (CurrentVessel() != stateVessel | CurrentSituation() != stateSituation | CurrentBody() != stateBody | CurrentBiome() != stateBiome)
{
stateVessel = CurrentVessel();
stateBody = CurrentBody();
stateSituation = CurrentSituation();
stateBiome = CurrentBiome();
stopwatch.Reset();
stopwatch.Start();
return true;
}
else return false;
//if (stopwatch.ElapsedMilliseconds > 100) // throttling detection to kill transient states.
//{
// stopwatch.Reset();
// Debug.Log("[ForScience!] Vessel in new experiment state.");
// return true;
//}
//else return false;
}
void ToggleCollection() // This is our main toggle for the logic and changes the icon between green and red versions on the bar when it does so.
{
autoTransfer = !autoTransfer;
FSAppButton.SetTexture(GetIconTexture(autoTransfer));
}
bool IsScientistOnBoard() // check if there is a scientist onboard so we can rerun things like goo or scijrs
{
foreach (ProtoCrewMember kerbal in CurrentVessel().GetVesselCrew())
{
if (kerbal.experienceTrait.Title == "Scientist") return true;
}
return false;
}
Texture2D GetIconTexture(bool b) // just returns the correct icon for the given state
{
if (b) return GameDatabase.Instance.GetTexture("ForScience/Icons/FS_active", false);
else return GameDatabase.Instance.GetTexture("ForScience/Icons/FS_inactive", false);
}
}
}