Ozin

Orbital race

Mar 1st, 2021 (edited)
1,387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 7.14 KB | None | 0 0
  1. // !runscript xSjerGzW
  2. // racetrack setup script: !runscript FWe0mbFH
  3.  
  4. set station to vessel("Space Race").
  5. set racecourse to list("PStart","w1","w2","w3","w4","w5","w6","w7","w8", "w9", "w10","PEnd").
  6. set gates to list().
  7. set col0 to rgba(0,0,0,0).
  8. for i in range(10) {
  9.     set i2 to i + 1.
  10.     set str to racecourse[i2].
  11.     set g to station:partstagged(str)[0].
  12.     gates:add(list(g, highlight(g, col0) )).
  13. }
  14. set hl to gates[0][1].
  15. set i to 0.
  16. set gate to ship:rootpart.
  17. set gateT to 0.
  18. set gateTMax to 1.5.
  19. set rel_v to velocity:orbit - station:velocity:orbit.
  20. set gateFA to v(1,0,0).
  21. set gateVel to v(0,0,0).
  22. set baseSpeed to 12.
  23. set thrustMult to 1.5.
  24.  
  25. wait until not(brakes).
  26. set lastIntersect to time:seconds.
  27.  
  28.  
  29. set port to ship:rootpart.
  30. set docking to false.
  31. set t to time:seconds.
  32. set th to 0. set st to facing:vector.
  33.  
  34. function findPort {
  35.     parameter p is core:part.
  36.     set pp to p:parent.
  37.     if pp:typename = "dockingport" {
  38.         set port to pp.
  39.     } else {
  40.         findPort(pp).
  41.     }
  42. }
  43. findPort().
  44.  
  45. core:part:controlfrom().
  46. port:undock().
  47. wait 0.
  48. set station to vessel("Space Race").
  49. kuniverse:forceactive(ship).
  50. port:getmodule("moduledockingnode"):setfield("docking acquire force", 0).
  51. sas off.
  52. set t to time:seconds.
  53. set undockTime to time:seconds.
  54. set ship:control:fore to -0.5.
  55.  
  56.  
  57. list engines in engs.
  58. for en in engs {
  59.     en:activate().
  60. }
  61. for eng in ship:partsdubbedpattern("vernier") {
  62.     set eng:deadband to 0.000.
  63.     set eng:thrustlimit to 50.
  64. }
  65. for m in ship:modulesnamed("ModuleLight") {
  66.     if m:hasevent("Lights on") m:doevent("Lights on").
  67. }
  68.  
  69. set th to 0. set st to facing.
  70. lock throttle to th.
  71. lock steering to st.
  72.  
  73. function next {
  74.     if i = 1 {
  75.         if defined tOld set tNew to time:seconds - tOld.
  76.         set tOld to time:seconds.
  77.         if defined tNew print "<color>Round time: " + round(tNew,2) + "s</color>".
  78.     }
  79.     set gate to gates[i][0].
  80.     set hl:color to col0.
  81.     set hl to gates[i][1].
  82.  
  83.     set i to i + 1.
  84.     if i = gates:length set i to 0.
  85.     when not(docking) and abs(vdot(gateFA, gate:position)) < rel_v:mag/20 and vdot(rel_v:normalized, gate:position) > 0 and vxcl(gateFA,gate:position):mag < 2.6 then {
  86.         print "passed gate " + gate:tag.
  87.         set gateT to gateTMax.
  88.         next().
  89.     }
  90.     set gateVel to rel_v.
  91.     set lastIntersect to time:seconds.
  92.  
  93.     //set l to gate:children[0]:getmodule("modulelighteva").
  94.     //set hl:color to rgba(l:getfield("light r"),l:getfield("light g"),l:getfield("light b"),0.3).
  95. }
  96. next().
  97.  
  98.  
  99.  
  100. function go {
  101.     parameter spd is 12.
  102.     set basespeed to spd.
  103.     list targets in tars.  for t in tars if t:distance < 500 and t <> station t:connection:sendmessage(list("go", spd)).
  104. }
  105. function exit2 {
  106.     set ship:control:neutralize to true. rcs off. unlock throttle. unlock steering.
  107. }
  108. function dock {
  109.     set minAng to 180.
  110.     for d in station:dockingports {
  111.         if d:nodetype = port:nodetype and d:state = "ready" and vang(-d:portfacing:vector,d:position) < minAng and station:elements[0]:parts:contains(d) {
  112.             set gate to d.
  113.             set minAng to vang(-d:portfacing:vector,d:position).
  114.             set docking to true.
  115.            
  116.         }
  117.     }
  118.     if docking {
  119.         print "Docking".
  120.         port:getmodule("moduledockingnode"):setfield("docking acquire force", 150).
  121.         when (gate:nodeposition-port:nodeposition):mag < 0.5 then { exit2(). }
  122.         on ship:parts:length {
  123.             set TRANSFERALL("OXIDIZER", ship:elements[0], core:element):active to true.
  124.             set TRANSFERALL("LIQUIDFUEL", ship:elements[0], core:element):active to true.
  125.             set TRANSFERALL("ELECTRICCHARGE", ship:elements[0], core:element):active to true.
  126.             global dockTime is time:seconds. when time:seconds > dockTime + 3 then { undockandgo(). }
  127.         }
  128.        
  129.        
  130.     }
  131.    
  132. }
  133. when not(docking) and ship:resources[0]:amount / ship:resources[0]:capacity < 0.1 and time:seconds > 2 + lastIntersect and gate:position:mag > 10 then {
  134.     dock(). return true.
  135. }
  136.  
  137.  
  138. function vesselSetup {
  139.     rcs on.
  140.     set steeringmanager:maxstoppingtime to 8.
  141.     set steeringmanager:pitchpid:kp to 4.
  142.     set steeringmanager:yawpid:kp to 4.
  143.     set steeringmanager:pitchpid:kd to 0.3.
  144.     set steeringmanager:yawpid:kd to 0.3.
  145. }
  146. vesselSetup().
  147. set tpid to pidloop(0.8,0,0.0,-1,1).
  148. set spid to pidloop(tpid:kp,tpid:ki,tpid:kd,-1,1).
  149. set fpid to pidloop(tpid:kp,tpid:ki,tpid:kd,-1,1).
  150.  
  151. set vd_ve to vecdraw(v(0,0,0), v(0,0,0), yellow, "", 1, true, 0.4).
  152.  
  153. set turn to true.
  154. when true then {
  155.     if (not (port:state:contains("docked")) and undockTime + 5 < time:seconds) {
  156.         set rel_v to velocity:orbit - station:velocity:orbit.
  157.         set gateFA to gate:facing:vector.
  158.  
  159.         set side to vxcl(gateFA, gate:position):mag.
  160.         if docking {
  161.             set fwdDist to vdot(gateFA, port:nodeposition - gate:nodeposition).
  162.             set wantPos to gate:position + gateFA *  (min(3, max(fwdDist - 1, -0.1 + 8*side)) + min(15, side / 2)).
  163.             set wanted_v to gateVel * gateT + (gateTMax-gateT)/gateTMax * (wantPos:normalized * (wantPos:mag^0.75)).
  164.             set ve to wanted_v - rel_v.
  165.  
  166.             set st to choose -gateFA if side < 2 and gate:position:mag < 10 else (ve + (rel_v:normalized * (-5/20))).
  167.         }
  168.         else {
  169.             if vdot(gate:position, gate:facing:vector) < 0 set gateFA to -gateFA.
  170.             set sl to baseSpeed + (choose (gate:position:mag/10 - 1) if baseSpeed <= 0 else (max(0.001,side-3)^0.3)).
  171.             set wanted_v to (gateVel * (gateT/gateTMax)) + ( (gateTMax-gateT)/gateTMax * ((gate:position + gateFA * max(-14, 2 - side^1.2)):normalized * sl) ).
  172.             set ve to wanted_v - rel_v.
  173.             set st to ve + (rel_v:normalized * (-baseSpeed/30)).
  174.  
  175.             set vd_ve:vec to ve.
  176.         }
  177.  
  178.         set gateT to max(0,gateT- (basespeed / 300) ).
  179.         if vang(facing:vector, ve) < 60
  180.             set th to vdot(facing:vector,ve) * thrustMult.
  181.         else set th to 0.
  182.        
  183.         set ship:control:translation to v(-spid:update(time:seconds, vdot(facing:starvector,ve)),-tpid:update(time:seconds,vdot(facing:topvector,ve)),-fpid:update(time:seconds,vdot(facing:vector,ve))).
  184.     } else {
  185.         set ship:control:translation to v(0,0,0).
  186.         set th to 0.
  187.     }
  188.     return turn.
  189. }
  190.  
  191. set camDist to 5.
  192. set trackcam to true.
  193. set cam to addons:camera:flightcamera.
  194. set gPos to -facing:vector * 80.
  195. set camMode to "gate".
  196. if random() < 0.5 set camMode to "station".
  197. when trackcam then {
  198.     set gPos to gPos * 0.97 + (rel_v:normalized * 5 + gate:position):normalized * 0.03.
  199.  
  200.     if camMode = "gate" set cam:position to gPos:normalized * -camDist.
  201.     else if camMode = "puke" set cam:position to facing:vector * -camDist.
  202.     else if camMode = "chase" set cam:position to rel_v:normalized * -camDist.
  203.     else set cam:position to station:position:normalized * -camDist + vxcl(station:position,gPos:normalized * -camDist/2).
  204.     return true.
  205. }
  206.  
  207. function undockAndGo {
  208.     set minAng to 180.
  209.     for j in range(gates:length) {
  210.         if vang(-port:portfacing:vector, gates[j][0]:position) < minAng {
  211.             set i to j.
  212.             set minAng to vang(-port:portfacing:vector, gates[j][0]:position).
  213.         }
  214.     }
  215.     set docking to false.
  216.     core:part:controlfrom().
  217.     port:undock().
  218.     set undockTime to time:seconds.
  219.    
  220.     wait 0.
  221.     set station to vessel("Space Race").
  222.     port:getmodule("moduledockingnode"):setfield("docking acquire force", 0).
  223.     sas off.
  224.     set ship:control:fore to -0.5.
  225.    
  226.     set t to time:seconds.
  227.     lock throttle to th.
  228.     lock steering to st.
  229.    
  230.     vesselSetup().
  231.     set gate to gates[i][0].
  232. }
  233.  
Advertisement
Add Comment
Please, Sign In to add comment