Advertisement
NotUrGenre

Peter Sharpe's Missile homing script- syntax updated

Apr 9th, 2022
1,504
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // Final homing
  3. //set Target to "nameoftarget".
  4. //YOU MUST MANUALLY TARGET YOUR TARGET BEFORE EXECUTING THIS PROGRAM.
  5. //Editable Parameters
  6. set explosionradius to 8.
  7. set iterations to 8.
  8. set initialguess to 15.
  9. set pitchangle to 15.
  10. Clearscreen.
  11. //Do Targeting
  12. print "Enemy heat signature acquired. Homing!!!".
  13. print"Missile by Peter Sharpe".
  14. print"Syntax updated to Kos 1.3.0.0 from 1.1.3.0".
  15. print"By NotUrGenre".
  16. //Launch Sequence
  17. lock throttle to 1.
  18. //Set up outputs
  19. print "Time to interception:" at (6,0).
  20. print "Distance to Target:" at (7,0).
  21. print "Steering Error:" at (8,0).
  22. //Take steering control, lock it to "steeringangle"
  23. set steeringangle to up+target:direction.
  24. sas off.
  25. lock steering to steeringangle.
  26. //Arm the warhead to trigger with radius of "explosionradius" + 1 frame to decouple.
  27. when t<explosionradius/50+0.05 or target:distance<50 then {stage. set vectordraw:show to false.}
  28. print "Warhead armed. Initiating homing algorithm." at (9,0).
  29. //Homing Algorithm, infinite loop
  30. set t to 10.
  31. until 0
  32. {
  33.     set steeringangle to up+target:direction.
  34.     set rpos to (0-1)*(target:Position).
  35.     set rvel to (ship:velocity:surface-target:velocity:orbit).
  36. if target:loaded {set rvel to ship:velocity:surface-target:velocity:surface.}.
  37. if altitude>35000 or target:altitude>35000 {set rvel to ship:velocity:orbit-target :velocity:orbit.}.
  38.     set amag to eng2:maxthrust/(ship:mass*9.81).
  39. //Solve for t, where 0=at^4+bt^2+ct+d. First get coefficients
  40.     set a to 0-((amag)^2)/4.
  41.     set b to (rvel:sqrmagnitude).
  42.     set c to 2*(rvel*rpos).
  43.     set d to (rpos:sqrmagnitude).
  44. //Do a few Newton-Raphson iterations:
  45.     set timeguesses to list().
  46.     timeguesses:add(initialguess).
  47.     set position to 0.
  48. until position>=iterations
  49.     {
  50.     timeguesses:add(position-(a*timeguesses[position]^4+b*timeguesses[position]^2+c*timeguesses[position]+d)/(4*a*timeguesses[position]^3+2*b*timeguesses[position]+c)).
  51.     set position to position+1.
  52.     }.
  53.     set initialguess to abs(timeguesses[iterations]).
  54. //Then calculate your desired direction
  55.     set t to abs(timeguesses[iterations])/1.15.
  56. if altitude>35000 {set t to abs(timeguesses[iterations]).}.
  57. print t at (22,10).
  58. print target:distance at (20,12).
  59.     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)).
  60.     set steeringangle to steeringvector.//:direction.
  61. print vectorangle(steeringvector,ship:facing:vector) at (16,14).
  62.     set Vectordraw TO VECDRAWARGS(v(0,0,0), steeringvector:normalized, rgb(1,0.5,0.5),"", 12.0, true ).
  63. }.
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement