Advertisement
Ozin

race track

Oct 7th, 2020 (edited)
1,460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 4.92 KB | None | 0 0
  1. // vtol code: !runscript https://pastebin.com/xeNy4xmv
  2. // self:      !runscript https://pastebin.com/3gF9PnXd
  3. clearvecdraws().
  4. local tracks is lexicon("temp", 1).
  5. global gates is list().
  6. set tracks to readjson("0:/json/tracks.json").
  7.  
  8. set defaultGateWidth to 25.
  9. set defaultSL to 150.
  10.  
  11. //cache
  12. set gateI to 0.
  13. set gate to lexicon().
  14. set nextGate to lexicon().
  15. set gateGeo to latlng(0,0).
  16. set gatePos to v(0,0,0).
  17. set gateFacing to facing.
  18. set gateRadius to 0.
  19. set vdList to list().
  20. set vdScale to 50.
  21. set terSlope to 0.
  22. for i in range(4) {
  23.     vdList:add(vecdraw(v(0,0,0), v(0,0,0), rgb(0.8,0.8,0.8), "", 1, true, 1, false)).
  24.     set vdList[i]:wiping to false.
  25. }
  26. set vdList[0]:color to rgb(1,0.25,0).
  27. set vdList[2]:color to rgb(0.1,0.35,1).
  28.  
  29. function createGate {
  30.     parameter geo, hdg, p, alt2, gatesl is defaultSL, width is defaultGateWidth.
  31.     return lexicon("geo", geo, "hdg", hdg, "p", p, "alt", alt2, "sl", gatesl, "width", width).
  32. }
  33. function gateHere {
  34.     parameter hdg is 0, ptch is 0, gatesl is defaultSL.
  35.     return createGate(ship:geoposition, hdg, ptch, round(altitude,1), gatesl).
  36. }
  37. function addGateHereFacing {
  38.     parameter pitch is 0, gatesl is defaultSL.
  39.     gates:add(gateHere(body:geopositionof(facing:topvector * -1000):heading,pitch, gatesl)).
  40.     next().
  41. }
  42. function updateGate {
  43.     set gates[(gateI - 1)] to gate.
  44. }
  45. function clearTrack {
  46.     set gateI to 0. set gates to list().
  47. }
  48. function randomGate {
  49.     set gpos to body:geopositionof(heading(random() * 360,0):vector * 1000 * random()).
  50.     return createGate(gpos, random() * 360, 0, gpos:terrainheight + 20).
  51. }
  52.  
  53. function randomTrack {
  54.     parameter cnt is 10.
  55.     set gates to list().
  56.     //createGate(geo, hdg, pitch,  alt, (sl), (width) )
  57.     for i in range(cnt) gates:add(randomGate()).
  58.    
  59. }
  60.  
  61. function loadTrack {
  62.     parameter str is "default".
  63.     set tracks to readjson("0:/json/tracks.json").
  64.     set gates to tracks[str].
  65.     set gateI to 0. next().
  66. }
  67. function saveTrack {
  68.     parameter str.
  69.     local tracks is readjson("0:/json/tracks.json").
  70.     if tracks:haskey(str) set tracks[str] to gates.
  71.     else tracks:add(str, gates).
  72.    
  73.     writejson(tracks, "0:/json/tracks.json").
  74. }
  75.  
  76. function next {
  77.    
  78.     set gate to gates[gateI].
  79.     print "next gate: " + gateI + ", SL: " + gate:sl + "m/s".
  80.    
  81.    
  82.     set gateI to gateI + 1.
  83.     if gateI >= gates:length set gateI to 0.
  84.  
  85.     set nextGate to gates[gateI].
  86. }
  87.  
  88. //loadtrack("ozin").
  89.  
  90. set cam to addons:camera:flightcamera.
  91. set race to false.
  92. when race then {
  93.     set gateFacing to heading(gate["hdg"], gate["p"]).
  94.     set gateRadius to gate["width"] / 2.
  95.     set gatePos to gate["geo"]:altitudeposition(gate:alt).
  96.    
  97.     //draw gate
  98.     set vdList[0]:vec to gateFacing:topvector * (gateRadius * 2).
  99.     set vdList[0]:start to gatePos + gateFacing:starvector * -gateRadius + gateFacing:topvector * -gateRadius.
  100.     set vdList[0]:width to vdScale / (gatePos - cam:position):mag.
  101.     for i in range(1, 4) {
  102.         set rot to angleaxis(90 * i, gateFacing:vector).
  103.         set vdList[i]:vec to rot * vdList[0]:vec.
  104.         set vdList[i]:start to (rot * (vdList[0]:start-gatePos)) + gatePos.
  105.         set vdList[i]:width to vdList[0]:width.
  106.     }
  107.    
  108.     //detect
  109.     set frontDist to vdot(gateFacing:vector, gatePos).
  110.     if abs(vdot(gatePos, gateFacing:topvector)) < gateRadius and abs(vdot(gatePos, gateFacing:starvector)) < gateRadius and frontDist >= 0 and frontDist-vdot(gateFacing:vector, velocity:surface / 25) < 0 {
  111.         //ship is in front of gate, within the star and top distance bounds, and is a about to pass through it within next two physics ticks based on current velocity, so consider completed
  112.         next().
  113.     }
  114.    
  115.    
  116.     return race.
  117. }
  118.  
  119.  
  120. function go {
  121.     race on.
  122.     lock taltoverride to gate:alt.
  123.     lock sl to min(100, gate:sl).
  124.     set tersteps to 6.
  125.     set alignterrainheight to -100.
  126.     set vsMin to -3.
  127.     set tltpid to pidloop(20, 2, 2, -50, 50).
  128.     set tltStarpid to pidloop(40, 2, 2, -40, 40).
  129.     when true then {
  130.         set sideDist to abs(vdot(gateFacing:starvector, gatePos)).
  131.         set frontDist to vdot(gateFacing:vector, gatePos).
  132.        
  133.         set aimPoint to vxcl(up:vector, gatePos + gateFacing:vector * max(-300, -1 * (sideDist^1.1) / 3 + (choose 5 if frontDist >= 0 else -10))):normalized.
  134.         set ang to min(90, vang(vxcl(up:vector,gatePos + gateFacing:vector * 10), vxcl(gateFacing:vector, gatePos))).
  135.         set tv to aimPoint * min(sl, sqrt(2 * 3 * max(10, sideDist - max(0,vdot(aimpoint,velocity:surface)) * 0.25)) / cos(ang)).
  136.  
  137.         set terSlope to min(90 - vang(up:vector, gatePos), terSlope).
  138.         return race.
  139.     }
  140. }
  141.  
  142. //needs to be run when spawned on the launchpad without moving!!
  143. function convertTrackFromKerbinToGael {
  144.     set kerbinRW to LATLNG(-0.0485129700103269,-74.7243272069546).
  145.     set gp to latlng(8.6870184757, -168.3459248301).
  146.    
  147.     set heightDiff to ship:geoposition:terrainheight - 69.1217.
  148.     set latDiff to gp:lat - kerbinRW:lat.
  149.     set lngDiff to gp:lng - kerbinRW:lng.
  150.     for g in gates {
  151.         set g:geo to latlng(g:geo:lat + latDiff, g:geo:lng + lngDiff).
  152.         set g:alt to g:alt + heightDiff.
  153.     }
  154.     set gateI to 0. next().
  155. }
  156.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement