Advertisement
Guest User

Missile

a guest
Jul 26th, 2014
2,478
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. //Missile by Peter Sharpe
  2. //For KSP with the kOS mod
  3.  
  4. //YOU MUST MANUALLY TARGET YOUR TARGET BEFORE EXECUTING THIS PROGRAM.
  5.  
  6. //Editable Parameters
  7. set explosionradius to 8.
  8. set iterations to 4.
  9. set initialguess to 15.
  10.  
  11. Clearscreen.
  12.  
  13. //Do Targeting
  14. print "Enemy heat signature acquired. Launching...".
  15.  
  16. //Launch Sequence
  17. stage.
  18. lock throttle to 1.
  19. lock steering to ship:srfprograde.
  20. wait until alt:radar>100 or velocity:surface:mag>100.
  21. print "Launch sequence completed. Now tracking target.".
  22.  
  23. //Staging
  24. when stage:solidfuel<0.001 then {stage. wait 0.01. If ship:maxthrust>0 {preserve.}.}.
  25.  
  26. //Set up outputs
  27. print "Time to interception:" at (0,10).
  28. print "Distance to Target:" at (0,12).
  29. print "Steering Error:" at (0,14).
  30.  
  31. //Take steering control, lock it to "steeringangle"
  32. set steeringangle to target:direction.
  33. sas off.
  34. lock steering to steeringangle.
  35.  
  36. //Arm the warhead to trigger with radius of "explosionradius" + 1 frame to decouple.
  37. when t<explosionradius/50+0.05 or target:distance<50 then {stage. set vectordraw:show to false.}
  38. print "Warhead armed. Initiating homing algorithm.".
  39.  
  40. //Homing Algorithm, infinite loop
  41. set t to 10.
  42. until 0
  43. {
  44. set rpos to (0-1)*(target:Position).
  45. set rvel to (ship:velocity:surface-target:velocity:orbit).
  46. if target:loaded {set rvel to ship:velocity:surface-target:velocity:surface.}.
  47. if altitude>35000 or target:altitude>35000 {set rvel to ship:velocity:orbit-target:velocity:orbit.}.
  48. set amag to ship:maxthrust/(ship:mass*9.81).
  49.  
  50. //Solve for t, where 0=at^4+bt^2+ct+d. First get coefficients
  51. set a to 0-((amag)^2)/4.
  52. set b to (rvel:sqrmagnitude).
  53. set c to 2*(rvel*rpos).
  54. set d to (rpos:sqrmagnitude).
  55.  
  56. //Do a few Newton-Raphson iterations:
  57. set timeguesses to list().
  58. set timeguesses:add to initialguess.
  59. set position to 0.
  60. until position>=iterations
  61. {
  62. set timeguesses:add to timeguesses[position]-(a*timeguesses[position]^4+b*timeguesses[position]^2+c*timeguesses[position]+d)/(4*a*timeguesses[position]^3+2*b*timeguesses[position]+c).
  63. set position to position+1.
  64. }.
  65. set initialguess to abs(timeguesses[iterations]).
  66.  
  67. //Then calculate your desired direction
  68. set t to abs(timeguesses[iterations])/1.15.
  69. if altitude>35000 {set t to abs(timeguesses[iterations]).}.
  70. print t at (22,10).
  71. print target:distance at (20,12).
  72. set steeringvector to (v((0-2)*(rpos:x+(rvel:x)*t)/(t^2),(0-2)*(rpos:y+(rvel:y)*t)/(t^2),(0-2)*(rpos:z+(rvel:z)*t)/(t^2))+6*(up:vector)).
  73. set steeringangle to steeringvector:direction.
  74. print vectorangle(steeringvector,ship:facing:vector) at (16,14).
  75. set Vectordraw TO VECDRAWARGS(v(0,0,0), steeringvector:normalized, rgb(1,0.5,0.5),"", 12.0, true ).
  76. wait 0.01.
  77. }.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement