Ozin

Orbital race

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

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×