Skip to content

Instantly share code, notes, and snippets.

@numberlesstim
Created August 12, 2024 14:02
Show Gist options
  • Save numberlesstim/b6459854660e43e0138b8bb1d2e8c629 to your computer and use it in GitHub Desktop.
Save numberlesstim/b6459854660e43e0138b8bb1d2e8c629 to your computer and use it in GitHub Desktop.
//gist b6459854660e43e0138b8bb1d2e8c629
approach on.
decelflag off.
if not (defined fliptime) set fliptime to 35. //how long does it take the ship to flip 180 degrees?
if not (defined sleepdist) set sleepdist to 50. //how far away from the tarpos should it simply keep stopped?
if not (defined sleepspd) set sleepspd to .001. //how slow is slow enough to qualify as 'stopped'?
if not (defined estSize) set estSize to 50. //estimated size of target if not loaded
if not (defined sl) set sl to -1. //how fast should it approach the target at most? -1 -> dv limited
if not (defined dvmargin) set dvmargin to 10. //how much remaining dv should this script aim for?
if not (defined autocam) autocam on. //automatically aim the camera towards the target?
if not (defined autotarget) autotarget on. //automatically use whatever target is selected as target?
if not (defined fancycam) fancycam off.
set tardist to 0.
lock relvel to v(0,0,0).
lock tarpos to v(0,0,0).
set cam to addons:camera:flightcamera.
set oldtarget to "none".
set oldtarget_loaded to false.
set cx to -10.
set cy to -35.
set cz to 0.
set cd to cam:distance.
function updateTarget {
parameter _target.
if _target:typename = "vessel" {
set tardist to ship:bounds:size:mag + (choose _target:bounds:size:mag if _target:loaded else estSize).
lock relvel to velocity:orbit - _target:velocity:orbit.
lock tarpos to _target:position - _target:position:normalized * tardist.
}
else if _target:hassuffix("ship") {
set tardist to ship:bounds:size:mag + (choose _target:ship:bounds:size:mag if _target:ship:loaded else estSize).
lock relvel to velocity:orbit - _target:ship:velocity:orbit.
lock tarpos to _target:ship:position - _target:ship:position:normalized * tardist.
}
}
lock nrelvel to vxcl(tarpos, relvel).
lock drelvel to relvel - nrelvel.
lock acc to max(0.0001, ship:availablethrust/ship:mass).
lock sdist to relvel:mag^2/(2*acc).
lock asteer to tarpos:normalized * sqrt(tarpos:mag*2*acc) - nrelvel*acc.
lock dsteer to -relvel.
lock steering to choose (choose ship:facing if relvel:mag < sleepspd else lookdirup(-relvel, ship:facing:topvector)) if tarpos:mag <= sleepdist else choose lookdirup(dsteer, ship:facing:topvector) if decelflag else lookdirup(asteer, ship:facing:topvector).
lock throttle to choose min(3, (relvel:mag - sleepSpd)/acc) - vang(steering:vector, ship:facing:vector) if tarpos:mag <= sleepdist else choose (min(1, relvel:mag/20) + sdist - tarpos:mag) if decelflag else (min(60, (min(max(1, nrelvel:mag), tarpos:mag))) - vang(ship:facing:vector, steering:vector)) * max(0, min(1, (choose ((stage:deltav:current-dvmargin) - vdot(relvel, tarpos:normalized)) if sl < 0 else (sl - vdot(relvel, tarpos:normalized))))).
when true then {
if not approach return false.
if autotarget {
if hastarget {
if target <> oldtarget OR oldtarget_loaded <> (choose target:ship:loaded if target:hassuffix("ship") else target:loaded){
updateTarget(target).
set oldtarget to target.
set oldtarget_loaded to (choose target:ship:loaded if target:hassuffix("ship") else target:loaded).
}
}
else {
set tardist to 0.
lock relvel to v(0,0,0).
lock tarpos to v(0,0,0).
set oldtarget to "none".
set oldtarget_loaded to false.
}
}
if autocam {
if cam:mode = "auto" set cam:mode to "orbital".
if hastarget {
if not (choose target:ship:loaded if target:hassuffix("ship") else target:loaded) OR not fancycam set cam:position to (-target:position:normalized*cd)*r(cx,cy,cz).
else set cam:position to target:position + (lookdirup(target:position, target:facing:topvector)*r(45,0,0)):vector:normalized * cd.
}
else if tarpos:mag > 0{
if fancycam set cam:position to tarpos + (lookdirup(tarpos, up:vector)*r(45,0,0)):vector:normalized * cd.
else set cam:position to (-tarpos:normalized*cd)*r(cx,cy,cz).
}
}
set decelflag to (sdist >= tarpos:mag - relvel:mag * fliptime AND vdot(relvel, tarpos) >= 0) OR ((stage:deltav:current-dvmargin) <= vdot(relvel, tarpos:normalized)) OR (sl >= 0 AND sl <= vdot(relvel, tarpos:normalized)).
return approach.
}
when not approach then {
lock steering to "kill".
lock throttle to 0.
}
clearscreen.
print "Adjustable variables (not updated):".
print " flipTime: " + fliptime.
print " sleepDist: " + sleepdist.
print " sleepSpd: " + sleepspd.
print " speed limit: " + sl.
print " dvMargin: " + dvmargin.
print " autocam: " + (choose "on" if autocam else "off").
print " autotarget: " + (choose "on" if autotarget else "off").
print "'approach off' to cancle".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment