Advertisement
sumguytwitches

0zin race clone multistart

Mar 5th, 2021 (edited)
1,138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function findPort {
  2.     parameter p is core:part.
  3.     set pp to p:parent.
  4.     if pp:typename = "dockingport" set port to pp.
  5.     else findPort(pp).
  6. }
  7. findPort().
  8.  
  9. core:part:controlfrom().
  10. port:undock().
  11. wait 0.2.
  12. kuniverse:forceactive(ship).
  13. wait 0.2.
  14. list engines in engs. for e in engs e:activate.
  15. sas off.
  16. set t to time:seconds.
  17. wait 1.
  18. set target to "Space Race".
  19. lock rel_v to choose (velocity:orbit - target:velocity:orbit) if hastarget else v(0,0,0).
  20. set gateFA to v(1,0,0).
  21. set gateVel to v(0,0,0).
  22. set baseSpeed to 0.
  23. when vessel("space race"):partsdubbedpattern("station")[0]:tag = "stationGo" then set basespeed to 8 + random()*1.
  24. set docking to false.
  25. for eng in ship:partsdubbedpattern("vernier") {
  26.     set eng:deadband to 0.005.
  27. }
  28.  
  29. set racecourse to list("PStart","w1","w2","w3","w4","w5","w6","w7","w8","PEnd").
  30. set gates to list().
  31. set col0 to rgba(0,0,0,0).
  32. for i in range(8) {
  33.     set i2 to i + 1.
  34.     set str to racecourse[i2].
  35.     set g to target:partstagged(str)[0].
  36.     gates:add(list(g, highlight(g, col0) )).
  37. }
  38. set hl to gates[0][1].
  39. set i to 0.
  40. set gateT to 0.
  41.  
  42. function dock {
  43.     set minAng to 180.
  44.     for d in target:dockingports {
  45.         if d:nodetype = port:nodetype and d:state = "ready" and vang(-d:portfacing:vector,d:position) < minAng {
  46.             set gate to d.
  47.             set minAng to vang(-d:portfacing:vector,d:position).
  48.             set docking to true.
  49.         }
  50.     }
  51.     if docking when (gate:nodeposition-port:nodeposition):mag < 0.5 then { exit2(). }
  52.    
  53. }
  54. function exit2 {
  55.     lock throttle to 0. rcs off. unlock throttle. set ship:control:neutralize to true. unlock steering.
  56. }
  57.  
  58. function next {
  59.     if i = 1 {
  60.         if defined tOld set tNew to time:seconds - tOld.
  61.         set tOld to time:seconds.
  62.  
  63.         if defined tNew print "<color>Round time: " + round(tNew,2) + "s</color>".
  64.     }
  65.    
  66.     set gate to gates[i][0].
  67.     set hl:color to col0.
  68.     set hl to gates[i][1].
  69.  
  70.     set i to i + 1.
  71.     if i = 8 set i to 0.
  72.     when 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 {
  73.         print "passed gate " + gate:tag.
  74.         next().
  75.     }
  76.     set gateVel to rel_v.
  77.     set gateT to 1.
  78.  
  79.     set l to gate:children[0]:getmodule("moduleColoredLensLight").
  80.    
  81.     set hl:color to rgba(l:getfield("light r"),l:getfield("light g"),l:getfield("light b"),0.3).
  82. }
  83. next().
  84.  
  85. rcs on.
  86. set steeringmanager:maxstoppingtime to 2.
  87. set steeringmanager:pitchpid:kp to 2.
  88. set steeringmanager:yawpid:kp to 2.
  89. set steeringmanager:pitchpid:kd to 0.3.
  90. set steeringmanager:yawpid:kd to 0.3.
  91. set tpid to pidloop(0.5,0,0.0,-1,1).
  92. set spid to pidloop(tpid:kp,tpid:ki,tpid:kd,-1,1).
  93. set fpid to pidloop(tpid:kp,tpid:ki,tpid:kd,-1,1).
  94.  
  95. set th to 0. set st to facing.
  96. lock throttle to th.
  97. lock steering to st.
  98.  
  99.  
  100.  
  101. when true then {
  102.     set gateFA to gate:facing:vector.
  103.     set side to vxcl(gateFA, gate:position):mag.
  104.     if docking {
  105.         set fwdDist to vdot(gateFA, port:nodeposition - gate:nodeposition).
  106.         set wantPos to gate:position + gateFA *  (min(3, max(fwdDist - 1, -0.1 + 8*side)) + min(15, side / 2)).
  107.         set wanted_v to gateVel * gateT + (1-gateT) * (wantPos:normalized * (wantPos:mag^0.75)).
  108.         set ve to wanted_v - rel_v.
  109.  
  110.         set st to choose -gateFA if side < 1 and gate:position:mag < 10 else (ve + (rel_v:normalized * (-5/20))).
  111.     }
  112.     else {
  113.         if vdot(gate:position, gate:facing:vector) < 0 set gateFA to -gateFA.
  114.         set sl to baseSpeed + max(0.001,side-5)^0.1.
  115.         set wanted_v to gateVel * gateT + (1-gateT) * ((gate:position + gateFA * max(-12, 5 - side^1.25)):normalized * sl).
  116.         set ve to wanted_v - rel_v.
  117.         set st to ve + (rel_v:normalized * (-baseSpeed/20)).
  118.     }
  119.  
  120.     set gateT to max(0,gateT-0.01).
  121.     if vang(facing:vector, ve) < 60
  122.         set th to vdot(facing:vector,ve) * 3.
  123.     else set th to 0.
  124.    
  125.     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))).
  126.     return rcs.
  127. }
  128.  
  129.  
  130.  
  131. //set camDist to 5.
  132. set trackcam to false.
  133. //set cam to addons:camera:flightcamera.
  134. //set gPos to -facing:vector * 80.
  135. //set camMode to "station".
  136. when trackcam then {
  137.     set gPos to gPos * 0.97 + (rel_v:normalized * 5 + gate:position):normalized * 0.03.
  138.  
  139.     if camMode = "gate" set cam:position to gPos:normalized * -camDist.
  140.     else if camMode = "puke" set cam:position to facing:vector * -camDist.
  141.     else if camMode = "chase" set cam:position to rel_v:normalized * -camDist.
  142.     else if hastarget set cam:position to target:position:normalized * -camDist + vxcl(target:position,gPos:normalized * -camDist/2).
  143.     return true.
  144. }
  145.  
  146.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement