Skip to content

Instantly share code, notes, and snippets.

@SofieBrink
Last active August 18, 2024 19:37
Show Gist options
  • Save SofieBrink/f8ea9bbc5afe2561c7b41e96fbbd0766 to your computer and use it in GitHub Desktop.
Save SofieBrink/f8ea9bbc5afe2561c7b41e96fbbd0766 to your computer and use it in GitHub Desktop.
// !runscript f8ea9bbc5afe2561c7b41e96fbbd0766
// Sofie's Fake Docklanding Functions
//-------------------------------------------------------------------------------------------------
// Usage Description. Parameters with {OPT} infront can be left out.
// FakeDockland({opt} localPort, {opt} tgt, {opt} tgtPort) - Function to attempt to dockland on the target as soon as its loaded.
//-------------------------------------------------------------------------------------------------
// Variables
set MinSL to -2.
set MaxSL to -0.2.
set MaxSideSL to 5.
// Functions
function FakeDockland {
// Function to go through a gate, teleport to the mun and dock to the mining vehicle there.
parameter tgt is "default". // Default, string or vessel
parameter tgtPort is "default". // Default, (partial)tag or index.
parameter localPort is "default". // Default, (partial)tag or dockingport
local ctrlPart is ship:controlpart.
if (not(tgtPort:istype("string") OR tgtPort:istype("scalar"))) {
print "TgtPort is invalid: Type - " + tgtPort:typename.
return false.
}
if (tgt:istype("string")) {
if (tgt = "default") {
set tgt to vessel("The Stinky Octopus"). // Default docklanding target in TPKSP S8
}
else {
for t in buildList("Targets") { if t:name = tgt set tgt to t. break. }
}
}
if (not(tgt:istype("Vessel"))) {
print "Tgt is invalid: " + (choose tgt if tgt:istype("string") else "Type - " + tgt:typename).
return false.
}
if (localPort:istype("string")) {
if (localPort = "default") {
local bestPort is list().
for d in ship:dockingports {
if (d:nodetype = "size2" // Senior Dockingport, Default for Octopus in TPKSP S8
AND vang(ship:facing:vector, d:portfacing:vector) > 90 // atleast 90 degrees on.
AND (bestPort:length = 0
OR vang(ship:facing:vector, d:portfacing:vector) > bestPort[1]))
{ bestPort:insert(0, d). bestPort:insert(1, vang(ship:facing:vector, d:portfacing:vector)).}
}
if (bestPort:length <> 0) set localPort to bestPort[0].
}
else {
if (ship:partstagged(localPort):length > 0) set localPort to ship:partstagged(localPort)[0].
else if (ship:partstaggedpattern(localPort):length > 0) set localPort to ship:partstaggedpattern(localPort)[0].
}
}
if (not(localPort:istype("DockingPort"))) {
print "LocalPort is invalid: " + (choose "Tag - " + localPort if localPort:istype("string") else "Type - " + localPort:typename).
return false.
}
local controlparts is list().
for m in ship:modulesnamed("modulecommand") controlparts:add(m:part).
for d in ship:dockingports controlparts:add(d).
local bestPart is list().
for p in controlparts {
local facingvec is (choose p:portfacing if p:hassuffix("portfacing") else p:facing):vector.
if (vang(localPort:portfacing:vector, facingvec) > 90) {// atleast 90 degrees on.
bestPart:insert(0, p). bestPart:insert(1, vang(localPort:portfacing:vector, facingvec)).
}
}
if (not(bestPart:contains(ctrlPart))) { if (bestPart:length <> 0) set ctrlPart to bestPart[0]. } // needs tweaking to choose the best one, right now it chooses the last added acceptable one
print "Docklanding at: " + tgt:name.
print "On Target Port: " + (choose "Index - " if tgtPort:istype("scalar") else "(Partial)Tag - ") + tgtPort.
print "Using LocalPort: " + localPort:title + (choose " - " + localPort:tag if localPort:tag <> "" else "").
print "With Controlpart: " + ctrlPart:title + (choose " - " + ctrlPart:tag if ctrlPart:tag <> "" else "").
local state is "Ascending".
when tgt:loaded then {
lock sl to 2.
local isDocklanding is true.
ctrlPart:controlfrom().
lock steering to up.
rcs on.
// check for tgtPort.
if (tgtPort:istype("string")) {
if ((tgtPort = "default")) {
local bestPort is list().
for d in tgt:dockingports {
if (d:nodetype = localPort:nodetype
AND vang(up:vector, d:portfacing:vector) < 90 // less than 90 degrees on.
AND (bestPort:length = 0
OR vang(up:vector, d:portfacing:vector) < bestPort[1]))
{ bestPort:insert(0, d). bestPort:insert(1, vang(up:vector, d:portfacing:vector)).}
}
if (bestPort:length <> 0) set tgtPort to bestPort[0].
//lock tgtVector to bestPort[0]:position - localPort:position.
}
else {
if (tgt:partstagged(tgtPort):length > 0) {
//lock tgtVector to tgt:partstagged(tgtPort)[0]:position - localPort:position.
set tgtPort to tgt:partstagged(tgtPort)[0].
}
else if (tgt:partstaggedpattern(tgtPort):length > 0) {
//lock tgtVector to tgt:partstaggedpattern(tgtPort)[0]:position - localPort:position.
set tgtPort to tgt:partstaggedpattern(tgtPort)[0].
}
else {
print "tgtPort is invalid: Tag - " + tgtPort.
return false.
}
}
}
else {
if (tgtPort >= 0 AND tgtPort < tgt:dockingports[0] AND tgt:dockingports[tgtPort]:nodetype = localPort:nodetype AND vang(up:vector, tgt:dockingports[tgtPort]:portvacing:vector) < 90) {
//lock tgtVector to tgt:dockingports[tgtPort]:position - localPort:position.
set tgtPort to tgt:dockingports[tgtPort].
}
else {
print "tgtPort is invalid: Index - " + tgtPort.
return false.
}
}
lock tgtVector to tgtPort:position - localport:position.
lock throttle to choose 0 if vang(steeringmanager:target:vector, ship:facing:vector) > 15 else (-ship:verticalspeed + sl) / 2.5 * (body:mu / body:position:sqrmagnitude).
on isDocklanding { wait 0. set ship:control:neutralize to true. unlock steering. lock throttle to 0. when kuniverse:canquicksave then { kuniverse:quicksaveto("DocklandAutosave"). print "Saved to " + char(34) + "DocklandAutosave" + char(34).}}
on time:seconds { if (isDocklanding) { set ship:control:translation to (vxcl(up:vector, tgtVector:normalized * min(tgtVector:mag, MaxSideSL * 5)) -vxcl(up:vector, (ship:velocity:surface - tgtPort:ship:velocity:surface)*5)) * ship:facing:inverse. print ("H-Dist: " + round(vxcl(up:vector, tgtVector):mag, 2) + " m"):padright(terminal:width) at (0,0). } return isDocklanding. }
when (-vdot(up:vector, tgtVector) > 12) then {
lock sl to 0.
set state to "Hover".
}
when (vxcl(up:vector, tgtVector):mag < 0.3) then {
lock sl to max(MinSL, min(MaxSL, vdot(up:vector, tgtVector) / 2)) * max(0, min(1, 1 - vxcl(up:vector, tgtVector):mag)).
set state to "Descending".
}
on ship:parts:length {
if (localPort:state <> "ready") {
HUDTEXT("Succesful Dockland"
, 10 // Show for 10 seconds
, 2 // Under altimeter
, 24 // Font Size
, WHITE // Print in white
, false). // Not to the terminal
set state to "Docked".
isDocklanding off.
}
else {
HUDTEXT("Something Exploded!"
, 10 // Show for 10 seconds
, 2 // Under altimeter
, 24 // Font Size
, WHITE // Print in white
, false). // Not to the terminal
set state to "Ruh Roh!".
}
return state <> "Docked".
}
on state {
print state.
return isDocklanding.
}
}
}
function GoThroughClosestGate {
// Fake Update 6
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment