-
Notifications
You must be signed in to change notification settings - Fork 0
/
libguido.ks
589 lines (495 loc) · 13.2 KB
/
libguido.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
// Common Functions
@lazyglobal off.
// Constants
declare global ksplaunchpad to LATLNG(-0.0972077948308072, -74.557676885654786).
declare global ksprunwaystart to LATLNG(-0.048599996665065981,-74.724473624279071).
declare global ksprunwayend to LATLNG(-0.05016922733662988, -74.498071382014942).
declare global ksplandingspot to LATLNG(-0.0,-74.72).
// Quick function to check for 1%, 2% and 5% deviation
function prec_01 {
parameter a, b.
if abs(a-b)/max(abs(a),abs(b)) < 0.01 { return True. }
return False.
}
function prec_02 {
parameter a, b.
if abs(a-b)/max(abs(a),abs(b)) < 0.02 { return True. }
return False.
}
function prec_05 {
parameter a, b.
if abs(a-b)/max(abs(a),abs(b)) < 0.05 { return True. }
return False.
}
// Mix_x_Mac
// Make sure: Min < X < Max
function min_x_max {
parameter x_min.
parameter x.
parameter x_max.
return max( min(x_max,x) , x_min).
}
// Safe staging
function safe_stage {
print "staging.".
wait until stage:ready.
stage.
wait 0.1.
// Extend solar panels if we just activated ion engines
declare local elist to 0.
declare local e to 0.
list engines in elist.
for e in elist {
if e:IGNITION AND e:name = "ionEngine" {
panels ON.
print "switching to ion eng, extending solar.".
break.
}
}
}
function clear_all_nodes {
until NOT hasnode {
remove nextnode.
wait 1.
}
}
// Burn Maneuver Node
// Based on code from KOS documentation
function exec_n {
parameter n.
parameter p_warp is 0.
if ship:maxthrust = 0 {
print "fuel exhausted. staging.".
safe_stage().
wait 0.1.
}
declare local dt to n:deltav:mag/(ship:maxthrust/ship:mass).
print "executing node in "+round(n:eta,1)+" s with dv: "+round(n:deltav:mag,1).
p_status("Node dt:"+round(dt,1),7).
wait_turn_n(n).
if n:eta - dt/2 - 15 > 0 {
print "waiting for burn.".
rwait(n:eta - dt/2 -15).
lock steering to n:deltav.
pwait(15).
} else if n:eta > 0 {
print "too close: "+round(n:eta,1)+" s".
lock steering to n:deltav.
pwait(n:eta).
}
lock steering to n:deltav.
declare local dv0 to n:deltav.
if dv0:mag < 0.2 {
print "Node have dv ov "+n:deltav:mag +" m/s, ignoring.".
unlock steering.
REMOVE n.
wait(10).
return True.
}
local dv_min is abs(n:deltav:mag).
if p_warp > 0 { pstart(p_warp). }
until false {
if ship:maxthrust = 0 {
print "fuel exhausted. staging.".
wait until stage:ready.
safe_stage().
wait 0.1.
} else {
set_throttle(n:deltav:mag,5).
if vdot(dv0, n:deltav) < 0 { break. }
if abs(n:deltav:mag) > dv_min+1 {
// Emergency check. This can happen for very long burns.
print "ABORTING BURN! deltav is growing whne burning to target!".
break.
} else {
set dv_min to abs(n:deltav:mag).
}
if n:deltav:mag < 0.1
{
pstop().
wait until vdot(dv0, n:deltav) < 0.5.
break.
}
}
wait 0.1.
p_status("Node dA: "+round(vang(ship:facing:vector,n:deltav),2),6).
p_status("Node th: "+round(throttle,2),7).
}
if p_warp > 0 { pstop(). }
lock throttle to 0.
unlock steering.
print "done. dv: "+round(n:deltav:mag,1)+" m/s".
wait 0.1.
remove n.
p_status("",6).
p_status("Node: done.",7).
}
// Horizontal Vector
function v_hor {
parameter v.
declare local up_vec to 0.
set up_vec to heading(0,90):vector.
return v - (v*up_vec)*up_vec.
}
// Find closest approach of A to B for the next full orbit of A
function find_closest {
parameter orbl_a.
parameter orbl_b.
declare local t to time:seconds.
declare local dt_max to orbl_a:orbit:period.
declare local dt to 0.
declare local a_v to 0.
declare local b_v to 0.
declare local min_d to 999999999999.
declare local old_min_d to 999999999999.
declare local min_t to 0.
declare local step to 0.01.
// Check time in steps of 1/100.
until dt > dt_max {
set a_v to positionat(orbl_a,t+dt).
set b_v to positionat(orbl_b, t+dt).
if (a_v-b_v):mag < min_d {
set min_t to t+dt.
set min_d to (a_v-b_v):mag.
}
set dt to dt+0.01*dt_max.
}
// We have an initial closest approach, now hillclimb
set step to 0.01.
set dt to min_t-t.
// Check time in steps of 1/1000.
until step < 0.000001 {
set old_min_d to min_d.
// Try dt+step*max
set a_v to positionat(orbl_a, t+dt+step*dt_max).
set b_v to positionat(orbl_b, t+dt+step*dt_max).
if (a_v-b_v):mag < min_d {
set dt to dt+step*dt_max.
set min_d to (a_v-b_v):mag.
}
set a_v to positionat(orbl_a, t+dt-step*dt_max).
set b_v to positionat(orbl_b, t+dt-step*dt_max).
if (a_v-b_v):mag < min_d {
set dt to dt-step*dt_max.
set min_d to (a_v-b_v):mag.
}
//print " dt/min_d/step "+round(dt,1)+" / "+km(min_d)+" / "+step.
if min_d <= old_min_d {
// No improvement, so let's lower granularity
set step to step/4.
}
}
return min_d.
}
// Geo coordinates for landing
declare global lat_to_m to 1.
declare global lng_to_m to 1.
function calibrate_geo {
set lat_to_m to (latlng(0,0):position-latlng(1,0):position):mag.
set lng_to_m to (latlng(0,0):position-latlng(0,1):position):mag.
print "calibration of geo coordinates (equator).".
print "1 deg lat in m: "+lat_to_m.
print "1 deg lat in m: "+lng_to_m.
}
function geo_v {
parameter geo_pos.
parameter alt is 0.
return V(geo_pos:lat*lat_to_m, geo_pos:lng*lng_to_m, geo_pos:terrainheight+alt).
}
function rn_v {
parameter v.
return "("+round(v:x,1)+" "+round(v:y,1)+" "+round(v:z,1)+")".
}
// Burn at max thrust for dt
function burn {
parameter dt.
parameter thr.
lock throttle to thr.
declare local t0 is time:seconds.
until time:seconds-t0 > dt {
if ship:maxthrust = 0 {
print "fuel exhausted. staging.".
stage.
wait 0.1.
}
wait 0.01.
}
lock throttle to 0.0.
}
// Set throttle to a 10 second burn or to max
function set_throttle {
parameter delta_v.
parameter dt.
declare local max_thrust to ship:maxthrust.
//print max_thrust+" "+delta_v+" "+dt.
if max_thrust > 0 {
lock throttle to max(min(1.0,abs(delta_v/(max_thrust/ship:mass))/dt),0.01).
} else {
lock throttle to 0.01.
}
}
// timed_turn(direction, time)
// Turns the ship and returns after exactly [time] seconds
function timed_turn {
parameter v.
parameter t.
lock steering to v.
set warpmode to "physics".
set warp to 3.
wait t.
kuniverse:timewarp:cancelwarp().
}
function wait_turn_n {
parameter n.
lock steering to n:deltav.
set warpmode to "physics".
set warp to 3.
until vang(ship:facing:vector,n:deltav) < 2 {
//print ship:facing:vector +" "+ v:vector.
p_status("Node dA: "+round(vang(ship:facing:vector,n:deltav),2),6).
wait 0.1.
}
kuniverse:timewarp:cancelwarp().
p_status("Node dA: "+round(vang(ship:facing:vector,n:deltav),2)+" done.",6).
}
function wait_turn {
parameter v.
lock steering to v.
set warpmode to "physics".
set warp to 3.
until vang(ship:facing:vector,v:vector) < 2 {
p_status("Node dA: "+round(vang(ship:facing:vector,v:vector),2),6).
//print ship:facing:vector +" "+ v:vector.
wait 0.2.
}
kuniverse:timewarp:cancelwarp().
p_status("Node dA: "+round(vang(ship:facing:vector,v:vector),2)+" done.",6).
}
function wait_until_in_orbit_of {
parameter dst_body.
if ship:obt:body <> dst_body {
print "waiting for SOI of "+dst_body:name.
wait 1.
set warpmode to "rails".
set warp to 7.
wait until ship:obt:body = dst_body.
set warp to 0.
}
}
function set_warp_for_t {
parameter t.
if t > 110000 {
set warp to 7.
} else if t > 11000 {
set warp to 6.
} else if t > 1100 {
set warp to 5.
} else if t > 110 {
set warp to 4.
} else if t > 55 {
set warp to 3.
} else if t > 11 {
set warp to 2.
} else if t > 6 {
set warp to 1.
} else {
set warp to 0.
}
}
// rwait() - Wait in rails mode
function rwait {
parameter dt.
if dt < 0 {
print "warning: rwait time negative. dt = "+round(dt,3).
}
set warpmode to "rails".
declare local t to time:seconds+dt.
until time:seconds > t {
set_warp_for_t(t-time:seconds).
wait 0.2.
}
}
function pwait {
parameter t.
if t < 0 {
print "warning: pwait time negative. t = "+round(t,3).
}
set warpmode to "physics".
set warp to 3.
wait t.
set warp to 0.
}
function pfast {
set warpmode to "physics".
set warp to 3.
}
function pstart {
parameter w is 3.
set warpmode to "physics".
set warp to w.
}
function pstop {
kuniverse:timewarp:cancelwarp().
}
// Engines
declare function set_engine {
parameter name_str.
parameter on.
parameter s_str is "".
declare local c to 0.
declare local el to 0.
declare local e to 0.
declare local on_str to "".
list engines in el.
for e in el {
if e:Name:contains(name_str) {
if on { e:ACTIVATE(). }
else { e:SHUTDOWN(). }
set c to c+1.
}
}
if on {set on_str to "on". }
else {set on_str to "off". }
print s_str+"switched "+c+" engines of type "+name_str+" to "+on_str.
}
declare function rapier_air {
declare local c to 0.
declare local el to 0.
declare local e to 0.
list engines in el.
for e in el {
if e:Name:contains("RAPIER") {
if e:mode = "ClosedCycle" { e:TOGGLEMODE(). }
set c to c+1.
}
}
print "Switched "+c+" x RAPIERs to air-breathing.".
}
declare function rapier_space {
declare local c to 0.
declare local el to 0.
declare local e to 0.
list engines in el.
for e in el {
if e:Name:contains("RAPIER") {
if e:mode <> "ClosedCycle" { e:TOGGLEMODE(). }
set c to c+1.
}
}
print "Switched "+c+" x RAPIERs to closed cycle.".
}
// Quicksave
function myquicksave {
parameter name.
parameter maxtime is 10.
local t0 is time:seconds.
until KUniverse:CANQUICKSAVE OR time:seconds-t0 > maxtime {
wait 0.1.
}
if KUniverse:CANQUICKSAVE {
KUniverse:QUICKSAVETO(name).
print "saving progress: "+name.
} else {
print "save failed "+name.
}
}
// Printing/Logging
declare global last_print to 0.
// Print only if 1s has passed
function print1s {
parameter s.
if time:seconds-last_print > 1 {
print s.
set last_print to time:seconds.
}
}
// Print status on the right side of the terminal.
function p_status {
parameter s.
parameter l.
set s to " "+s+" ".
print s:substring(0,32) AT(terminal:width-32,l+1).
}
function clr_status {
p_status("----------- Status -----------",-1).
p_status("",0).
p_status("",1).
p_status("",2).
p_status("",3).
p_status("",4).
p_status("",5).
p_status("",6).
p_status("",7).
p_status("------------------------------",8).
}
// Print distance as 10,000km
function p3d {
parameter v.
set v to floor(v).
if v > 100 { return v. }
if v > 10 { return "0"+v. }
if v > 0 { return "00"+v. }
}
function km {
parameter d.
if d > 1000000000 {
return floor(d/1000000000)+","+p3d(floor(mod(d,1000000000)/1000000))+","+p3d(floor(mod(d,1000000)/1000))+" km".
} else if d > 1000000 {
return floor(d/1000000)+","+p3d(floor(mod(d,1000000)/1000))+" km".
} else if d > 1000 {
return round(d/1000,1)+" km".
} else if d > 10 {
return round(d,0)+" m".
} else if d > 1 {
return round(d,1)+" m".
} else {
return round(d,2)+" m".
}
}
// Print percentage of a of b
function percent {
parameter a.
parameter b.
if b = 0 { return "N/A". }
local p is abs(a/b)*100.
if p > 5 {
return round(p)+"%".
} else if p > 1 {
return round(p,1)+"%".
} else {
return round(p,3)+"%".
}
}
// Print percentage of a of b
function percent_err {
parameter a.
parameter b.
return percent(a-b,b).
}
declare global time_zero to 0.
function mytimer_s {
set time_zero to time:seconds.
}
function mytimer {
return "["+round(time:seconds-time_zero,1)+" s] ".
}
function myinit {
sas off.
lock throttle to 0.0.
kuniverse:timewarp:cancelwarp().
wait 1.
}
function myexit {
lock throttle to 0.0.
wait 0.1.
SteeringManager:RESETTODEFAULT().
SET SHIP:CONTROL:NEUTRALIZE TO TRUE.
wait 0.1.
kuniverse:timewarp:cancelwarp().
wait 0.1.
}
function panic {
parameter s.
print "PANIC: "+s.
KUniverse:PAUSE().
}