-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStandard_Lib.ks
405 lines (355 loc) · 16.4 KB
/
Standard_Lib.ks
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
@lazyglobal off.
function GetYorN {
//Function to get the users input of 'y' or 'n' lower or upper case
Declare Parameter DisplayText is "(Y/N)". //Option parameter to display a different message
Declare Local Answer to "n/a".
Declare Local Done to False.
Print DisplayText.
until Done {
if terminal:input:haschar {
set Answer to terminal:input:getchar().
if Answer = "y" or Answer = "n" {
set Done to True.
} else {
Print (" Type 'Y' or 'N'").
Print DisplayText.
}
}
}
Return Answer.
}.
function GetUserInput{
//Function to get the users input
Declare Local Answer to "n/a".
Declare Local Done to False.
until Done {
if terminal:input:haschar {
set Answer to terminal:input:getchar().
set Done to True.
}
}
Return Answer.
}.
function CheckCharging {
//Checks to see if the ship is gaining electrical charge
Declare Local C1 to 0.0.
Declare Local C2 to 0.0.
set C1 to ship:electriccharge.
wait 0.25.
set C2 to ship:electriccharge.
if c1 < c2 {
return true.
}
Else {
return false.
}.
}.
function CalcChargeTime {
//Returns the time needed for a ship to increase it's charge by a specific charge amount
Declare Parameter ChargeNeeded. //Required parameter - the amount of charge needed to increase
Declare Local Charge1 to 0.0.
Declare Local Charge1Time to 0.0.
Declare Local Charge2 to 0.0.
Declare Local Charge2Time to 0.0.
set Charge1 to ship:electriccharge.
set Charge1Time to Time:Seconds.
wait 0.25.
set Charge2 to ship:electriccharge.
set Charge2Time to Time:Seconds.
if Charge1 = Charge2 {
return 0.
} else {
return ChargeNeeded / ((Charge2 - Charge1) / (Charge2Time - Charge1Time)).
}
}.
function ShipMaxCharge {
//Returns the total charge of the ship at full charge
Declare Local ResList to ship:resources.
Declare Local amount to 0.0.
for item in ResList {
if item:name = "ElectricCharge" {
set amount to item:capacity.
}
}
return amount.
}.
function GetESUlist {
//Returns a list of 1 ESU part (first it finds) or a blank list if none on ship.
Declare Local plist to List().
Declare Local NumInList to 0.
Set plist to ship:PARTSNAMED("ScienceBox").
if plist:length = 0 {
print("No ESU on this ship").
Return plist.
}.
//Remove any extra ESUs and only return the 1st if found
If plist:length > 1 {
set NumInList to plist:length.
From {Local I is NumInList.} UNTIL I = 1 STEP {set I to I - 1.} do {
plist:remove(I-1).
}
}.
Return plist.
}.
function GetDeployableAntList {
//Returns a list of Deployable Antennas
Declare Local AntList to list().
Declare Local plist to ship:parts.
for item in plist {
if item:hasmodule("ModuleDeployableAntenna") {
AntList:add(item).
}
}
return AntList.
}.
function GetNonDeployableAntList {
//Returns a list of Non-Deployable Antennas
Declare Local AntList to list().
Declare Local plist to ship:parts.
for item in plist {
if NOT item:hasmodule("ModuleDeployableAntenna") AND NOT item:hasmodule("ModuleCommand") AND item:hasmodule("ModuleDataTransmitter"){
AntList:add(item).
}
}.
return AntList.
}.
function ExtendAllAnts {
//Extend all Deployable Antennas or deploy a list of antennas
Declare Parameter AntList is list(). //Optional paramenter - list of antennas to deploy
if AntList:length = 0 {
set AntList to GetDeployableAntList().
}.
For Ant in AntList {
if Ant:getmodule("ModuleDeployableAntenna"):hasaction("extend antenna"){
Ant:getmodule("ModuleDeployableAntenna"):doaction("extend antenna", true).
}
}
}.
function RetractAllAnts {
//Retract all Deployable Antennas or deploy a list of antennas
Declare Parameter AntList is list(). //Optional paramenter - list of antennas to retract
if AntList:length = 0 {
set AntList to GetDeployableAntList().
}.
For Ant in AntList {
if Ant:getmodule("ModuleDeployableAntenna"):hasaction("retract antenna"){
Ant:getmodule("ModuleDeployableAntenna"):doaction("retract antenna", true).
}
}
}.
function GetSensorList {
//Returns a list of all science parts
Declare Local SensorList to list().
Declare Local plist to ship:parts.
for item in plist {
if item:hasmodule("ModuleScienceExperiment") {
SensorList:add(item).
}
}
return SensorList.
}.
Function ClearAllScience {
Declare Parameter SensorList is list(). //Optional parameter - list of science parts
//If no list of science parts sent to the function, get a list of all science parts on the ship.
Declare Local counter to 0.
if SensorList:length = 0 {
set SensorList to GetSensorList().
}.
For Sensor in SensorList {
//only proceed is the experiment doesn't already have data
if Sensor:getmodule("ModuleScienceExperiment"):hasdata {
if Sensor:NAME = ("GooExperiment") {
//If it the part is a goo canister, we need to reset it.
//Deleting the data will not allow the experiement to be run again
if Sensor:getmodule("ModuleScienceExperiment"):hasevent("reset goo canister") {
Sensor:getmodule("ModuleScienceExperiment"):doevent("reset goo canister").
set counter to counter + 1.
}
} else if Sensor:NAME = ("science.module") {
//If it the part is a science bay, we need to reset it.
//Deleting the data will not allow the experiement to be run again
if Sensor:getmodule("ModuleScienceExperiment"):hasevent("reset materials bay") {
Sensor:getmodule("ModuleScienceExperiment"):doevent("reset materials bay").
set counter to counter + 1.
}
} else if Sensor:getmodule("ModuleScienceExperiment"):hasaction("delete data"){
Sensor:getmodule("ModuleScienceExperiment"):doaction("delete data", true).
set counter to counter + 1.
} else if Sensor:getmodule("ModuleScienceExperiment"):hasaction("discard crew report"){
Sensor:getmodule("ModuleScienceExperiment"):doaction("discard crew report", true).
set counter to counter + 1.
} else if Sensor:getmodule("ModuleScienceExperiment"):hasaction("discard data"){
//Discard data is needed for the Atmospheric Fluid Spectro-Variometer - others?
Sensor:getmodule("ModuleScienceExperiment"):doaction("discard data", true).
set counter to counter + 1.
}
}
}.
Return counter.
}.
Function GetAllScience {
//Runs all science experiments on the ship or from a list of parts.
//Will only run 1 Goo and 1 Science Bay experiment if more than 1 in the list
Declare Parameter SensorList is list(). //Optional parameter - list of science parts
Declare Parameter Verbose is True. //Optional parameter - if true prints messages
//If no list of science parts sent to the function, get a list of all science parts on the ship.
if SensorList:length = 0 {
set SensorList to GetSensorList().
}.
Declare local DidGoo is false.
Declare local DidBay is false.
Declare local IsGoo is false.
Declare local IsBay is false.
Declare local Counter to 0.
if Verbose {Print ("Running science experiments...").}
For Sensor in SensorList {
//only proceed is the experiment doesn't already have data
if not Sensor:getmodule("ModuleScienceExperiment"):hasdata {
//Find out if the current sensor is a Goo Container or Science Bay
if Sensor:NAME = ("GooExperiment") set IsGoo to true.
if Sensor:NAME = ("science.module") set IsBay to true.
//If not goo or bay go ahead and do experiment
if NOT IsGoo AND NOT IsBay {
if Verbose {print " Running experiment on " + Sensor:NAME.}
Sensor:getmodule("ModuleScienceExperiment"):Deploy.
set Counter to Counter +1.
} else if IsGoo {
//only do 1 goo and check if the goo hasn't already been done.
if NOT DidGoo AND Sensor:getmodule("ModuleScienceExperiment"):HasEvent("observe mystery goo") {
if Verbose {print " Running experiment on " + Sensor:NAME.}
Sensor:getmodule("ModuleScienceExperiment"):Deploy.
set Counter to Counter +1.
set DidGoo to true.
}
} else if IsBay {
//only do 1 materials bay and check if it's already been done.
if NOT DidBay AND Sensor:getmodule("ModuleScienceExperiment"):HasEvent("observe materials bay") {
if Verbose {print " Running experiment on " + Sensor:NAME.}
Sensor:getmodule("ModuleScienceExperiment"):Deploy.
set Counter to Counter +1.
set DidBay to true.
}
}
//set back to false inside HASDATA IF statement so it's inside for senson loop.
set IsGoo to false.
set IsBay to false.
}
}
if Verbose {print ("Completed " + Counter + " available science experiments").}
return SensorList.
}.
function TransmitAllScience {
//Transmits all science experiments that have data on the ship or from a list of sensors sent to the function
//Returns True only if all science is successfuly transmitted, else returns False.
Declare Parameter SensorList is list(). //Optional parameter - list of science parts to xmit data
Declare Parameter WarpTime is True. //Optional parameter - lets the user decide if warp while waiting
Declare Parameter Verbose is True. //Optional parameter - if true prints messages
Declare Parameter ChargePerMit is 24. //Optional parameter to know the charge per mit for antenna being used
//Defaulted to 24 (RA-2 Relay) - the highest of all the antennas
Declare Parameter TransSpeed is 2.86. //Optional Parameter to know the antenna transmission speed
//Defaulted to 2.86, (RA-2 Relay) - the slowest antenna
Declare Local Datalist to list().
Declare Local Charge to 0.0.
Declare Local ChargeTime to 0.0.
Declare Local DataSize to 0.0.
Declare Local IsCharging to true.
Declare Local WaitTime to 1.0.
Declare Local WarpDelay to 0.0.
Declare Local ChargeCapacity to 0.0.
Declare Local Success to True.
Declare Local DataName to " ".
Declare Local ReserveCharge to 0.05. //Minimum % no to let ship charge drop below
Declare Local AntList to GetDeployableAntList().
//Must have antennas to transmit data
if AntList:length = 0 {
set Antlist to GetNonDeployableAntList().
if AntList:length = 0 {
if Verbose {print ("****NO ANTENNAS ON SHIP - ABORTING TRANSMISSION ****").}
return false.
}
//Must reset Antlit to a deployable list, otherwise ExtendAllAnts() will crash.
Set AntList to GetDeployableAntList().
}.
if Verbose {print (" ").}
//Check if comms to KSC, else stop here and return false
if HomeConnection:IsConnected = false {
if Verbose {print ("****NO CONNECTION TO KSC - ABORTING TRANSMISSION ****").}
return false.
}.
//Deploy all antennas
ExtendAllAnts(AntList).
if Verbose {print ("Deploying antennas...").}
wait 3.5. //wait for antenna animation
//If no list of science parts sent to the function, get a list of all science parts on the ship.
if SensorList:length = 0 {
set SensorList to GetSensorList().
}.
for Sensor in SensorList {
//Check each sensor to see if has data.
if Sensor:getmodule("ModuleScienceExperiment"):hasdata {
set DataList to Sensor:getmodule("ModuleScienceExperiment"):data.
//Get size and name of data
//":data" returns a list, must step through list to get info
for item in Datalist {
set DataSize to item:dataamount.
set DataName to item:title.
}
if Verbose {print "Transmitting " + DataName.}
//Check if electric charge capacity of ship is enough to transmit this science
set ChargeCapacity to ShipMaxCharge().
if Verbose {print " Current Charge: " + round(ship:electriccharge, 1).}
if Verbose {print " Needed Charge: " + (ChargePerMit * DataSize + Round(ReserveCharge * ChargeCapacity, 1)).}
if ChargeCapacity < (ChargePerMit * DataSize) + (ChargeCapacity * ReserveCharge) {
if Verbose {print (" Vessel Electric Charge capacity not sufficient to transmit").}
if Verbose {print (" ****Aborting Transmission****").}
set Success to false.
} else {
//Check if current charge is enough while maintaining reserve charge
set Charge to ship:electriccharge.
if Charge > (ChargePerMit * DataSize) + (ChargeCapacity * ReserveCharge) {
set WaitTime to (DataSize / TransSpeed) + 2.
Sensor:getmodule("ModuleScienceExperiment"):transmit().
if Verbose {print " Transmission time: " + round(WaitTime, 1) + " seconds.".}
wait until Sensor:getmodule("ModuleScienceExperiment"):hasdata = false.
wait WaitTime. //wait while antenna xmits
if Verbose {print (" Transmission complete.").}
if Verbose {print (" ").}
} else {
if Verbose {print (" Not enough charge.").}
set IsCharging to CheckCharging().
//add 0.005 or to make sure the Charge time gets the ship charged slightly above the ReserveCharge
set ChargeTime to CalcChargeTime((DataSize * ChargePerMit) + (ChargeCapacity * ReserveCharge) - Charge).
if Verbose {print " Charging time is " + round(ChargeTime,1) + " seconds".}
// //wait until there is enough charge, exit function if not charging
until Charge > ((ChargePerMit * DataSize) + ChargeCapacity * ReserveCharge) or not (IsCharging) {
WarpTo(time:seconds + ChargeTime).
wait ChargeTime.
set ChargeTime to CalcChargeTime((DataSize * ChargePerMit) + (ChargeCapacity * ReserveCharge) - Charge + 1).
set IsCharging to CheckCharging().
set Charge to ship:electriccharge.
}
//exit function if ship not charging.
if not (IsCharging) {
if Verbose {print("****Not charging. Aborting ALL Transmissions****").}
return false.
//else the ship has the charge and we can xmit
} else {
set WaitTime to (DataSize / TransSpeed) + 2.
Sensor:getmodule("ModuleScienceExperiment"):transmit().
if Verbose {print " Transmission time: " + round(WaitTime, 1) + " seconds.".}
wait until Sensor:getmodule("ModuleScienceExperiment"):hasdata = false.
wait WaitTime. //wait while antenna xmits
if Verbose {print (" Transmission complete.").}
if Verbose {print (" ").}
}
}
}
}
//Wait until transmitting next sensor.
//There is a delay when an antenna is done before it can xmit again.
Wait .5.
}.
if Verbose {Print ("All Science Data Transmitted").}
Return Success.
}.
print("Loaded Library Standard_Lib.ks").