kOS-scripts/lib/rocket.ks
Casper V. Kristensen 49b5236521
Implement docking.
2019-02-12 23:33:17 +01:00

114 lines
2.9 KiB
Plaintext

@LAZYGLOBAL OFF.
function any_flameout {
//
// Return true if any of our engines are starved for fuel. Primarily used to determine the need for staging.
//
local engines_list is LIST().
list ENGINES in engines_list.
for engine in engines_list {
if engine:ignition and engine:flameout {
return true.
}
}
return false.
}
function should_stage {
//
// Return true if the rocket needs to stage.
//
return STATUS = "PRELAUNCH" // e.g. still attached to launch tower
or any_flameout() // any engine starved for fuel
or SHIP:maxthrustat(0) = 0. // no active engines (e.g. when we have an intermediate stage for decouplers before activating the next engine)
}
function stage_when_ready {
//
// Stage when ready. Blocks until staging has been initiated.
//
wait until STAGE:ready.
print "STAGING".
STAGE.
}
function isp_sum {
//
// Return the sum of vacuum ISP for enabled engines.
//
local sum is 0.
local engines_list is LIST().
list ENGINES in engines_list.
for engine in engines_list {
if engine:ignition {
set sum to sum + engine:vacuumisp.
}
}
return sum.
}
function rcs_isp_sum {
//
// Return the sum of ISP for RCS thrusters.
//
local sum is 0.
for thruster in SHIP:modulesnamed("ModuleRCSFX") {
set sum to sum + thruster:getfield("rcs isp").
}
return sum.
}
function rcs_maxthrust {
// There's no way to get thrust of RCS dynamically in kOS; gonna have to hard-code it :/
// Source: https://wiki.kerbalspaceprogram.com/wiki/Reaction_Control_System#Thrusters
local rcs_thrusts is LEXICON( // in kN
"RCSBlock", 1.0, // RV-105 RCS Thruster Block
"linearRcs", 2.0, // Place-Anywhere 7 Linear RCS Port
"vernierEngine", 12.0 // Vernor Engine
).
local thrust_sum is 0.
for module in SHIP:modulesnamed("ModuleRCSFX") {
local part_name is module:part:name.
if rcs_thrusts:haskey(part_name) {
set thrust_sum to thrust_sum + rcs_thrusts[part_name].
} else { // if non-stock part
print "WARNING: Unknown RCS Thruster '" + part_name + "'; using default thrust of 1.0 kN".
set thrust_sum to thrust_sum + 1.0.
}
}
return thrust_sum.
}
function deploy_fairings {
print "Deploying fairings".
for module in SHIP:modulesnamed("ModuleProceduralFairing") {
module:doaction("deploy", true).
}
}
function extend_antennas {
print "Extending antennas".
for module in SHIP:modulesnamed("ModuleDeployableAntenna") {
module:doaction("extend antenna", true).
}
}
function unlock_control {
//
// Ensure that the throttle is 0 and that the player is not locked out of control.
//
set SHIP:control:pilotmainthrottle to 0.
set SHIP:control:neutralize to true.
unlock STEERING.
unlock THROTTLE.
}