Add syntax highlighting for PyCharm. Update syntax throughout project to accommodate.

This commit is contained in:
Casper V. Kristensen 2018-08-05 19:40:16 +02:00
parent d353debe5a
commit 32852274cc
Signed by: caspervk
GPG key ID: B1156723DB3BDDA8
10 changed files with 95 additions and 84 deletions

View file

@ -2,3 +2,6 @@
Scripts for Kerbal Operating System.
Clone to `Kerbal Space Program/Ships/Script`.
### Syntax highlighting for PyCharm
Copy `kOS.xml` to `~/.PyCharm/config/filetypes/kOS.xml`.

View file

@ -1,10 +1,18 @@
@LAZYGLOBAL OFF.
CLEARSCREEN.
print "=================== BOOTING ===================".
wait until SHIP:LOADED and SHIP:UNPACKED.
clearscreen.
clearguis().
clearvecdraws().
if HOMECONNECTION:ISCONNECTED {
set TERMINAL:width to 50.
set TERMINAL:height to 30.
CORE:doaction("Open Terminal", true).
print "=================== BOOTING ===================".
wait until SHIP:loaded and SHIP:unpacked.
wait 0.001.
if HOMECONNECTION:isconnected {
update_scripts().
} else {
print "No connection to KSC: not updating scripts.".
@ -17,26 +25,26 @@ function update_scripts {
// Copy all .ks-files from the archive to the CPU's internal storage.
// Get lexicon of existing items (files/directories) and sizes on the internal storage
local existing is lexicon().
for item in CORE:VOLUME:FILES:VALUES {
set existing[item:NAME] to item:SIZE.
local existing is LEXICON().
for item in CORE:volume:files:values {
set existing[item:name] to item:size.
}
// Check if items from the archive match those in the internal storage
for item in VOLUME("ARCHIVE"):FILES:VALUES {
for item in volume("ARCHIVE"):files:values {
local skip is false. // kOS doesn't support the 'continue' keyword...
if not skip and item:NAME[0] = "." set skip to true. // skip linux hidden dirs (e.g. .git)
if not skip and item:ISFILE
and item:EXTENSION <> "ks" set skip to true. // skip non-script items (e.g. README.md)
if not skip and existing:HASKEY(item:NAME)
and item:SIZE = existing[item:NAME] set skip to true. // skip if local item is same size
if not skip and item:name[0] = "." set skip to true. // skip linux hidden dirs (e.g. .git)
if not skip and item:isfile
and item:extension <> "ks" set skip to true. // skip non-script items (e.g. README.md)
if not skip and existing:haskey(item:name)
and item:size = existing[item:name] set skip to true. // skip if local item is same size
if not skip {
print "Copying " + item:NAME + "..".
COPYPATH(PATH(VOLUME("ARCHIVE")):COMBINE(item:NAME), CORE:VOLUME).
print "Copying " + item:name + "..".
copypath(path(volume("ARCHIVE")):combine(item:name), CORE:volume).
if item:NAME = "boot" reboot. // reboot CPU if boot/ directory was updated
if item:name = "boot" reboot. // reboot CPU if boot/ directory was updated
}
}
}

View file

@ -1,4 +1,4 @@
@LAZYGLOBAL OFF.
@LAZYGLOBAL off.
run once "lib/equations".
run once "lib/node".
@ -29,8 +29,8 @@ function launch {
// Since the "center" of an orbit must be at the center of gravity of the body, the latitude of the launch site establishes the minimum absolute orbital inclination.
// KSC is almost on the equator, so we're going to always round the latitude towards zero, and accept the inaccuracies it may introduce.
local launch_site_latitude is round_towards_zero(SHIP:LATITUDE).
local target_orbit_inclination is MAX(target_orbit_inclination, launch_site_latitude).
local launch_site_latitude is round_towards_zero(LATITUDE).
local target_orbit_inclination is max(target_orbit_inclination, launch_site_latitude).
// Calculate the launch azimuth; the compass heading we head for when launching to achieve orbit of desired inclination
local launch_azimuth is calculate_launch_azimuth(target_orbit_inclination, target_orbit_altitude, launch_site_latitude).
@ -41,17 +41,17 @@ function launch {
set launch_azimuth to 180 - launch_azimuth.
}
print "Launch site latitude: " + ROUND(SHIP:LATITUDE, 3) + "° (~" + launch_site_latitude + "°)".
print "Launch site latitude: " + round(LATITUDE, 3) + "° (~" + launch_site_latitude + "°)".
print "Available orbital inclination: " + target_orbit_inclination + "°".
print "Launch azimuth: " + ROUND(launch_azimuth, 3) + "°".
print "Launch azimuth: " + round(launch_azimuth, 3) + "°".
print "Launch southwards: " + southwards.
print "--- VERTICAL CLIMB ---".
SAS OFF.
RCS OFF.
SAS off.
RCS on.
set NAVMODE to "SURFACE".
lock STEERING to HEADING(launch_azimuth, 90). // roll to launch azimuth
lock STEERING to heading(launch_azimuth, 90). // roll to launch azimuth
lock THROTTLE to 1.0.
@ -64,19 +64,19 @@ function launch {
}
// Once a certain altitude (or velocity) is reached, a slight turn is made, called the pitchover maneuver
wait until SHIP:ALTITUDE > pitchover_altitude
or SHIP:VELOCITY:SURFACE:MAG > pitchover_velocity.
wait until ALTITUDE > pitchover_altitude
or VELOCITY:surface:mag > pitchover_velocity.
print "--- PITCHOVER ---".
lock STEERING to HEADING(launch_azimuth, 90-pitchover_tilt).
lock STEERING to heading(launch_azimuth, 90-pitchover_tilt).
wait until actual_prograde_pitch() > pitchover_tilt. // wait until the prograde "catches up" to our tilted heading
print "--- GRAVITY TURN ---".
// TODO: the angle of the launch azimuth will not account for the fact that our compass will change as we move north/south.
lock STEERING to HEADING(launch_azimuth, 90-actual_prograde_pitch()). // Follow prograde pitch to get 0 deg angle of attack, but force compass heading at launch azimuth.
lock STEERING to heading(launch_azimuth, 90-actual_prograde_pitch()). // Follow prograde pitch to get 0 deg angle of attack, but force compass heading at launch azimuth.
wait until APOAPSIS > target_orbit_altitude.
lock THROTTLE TO 0.
@ -84,18 +84,18 @@ function launch {
print "--- CIRCULARIZE ---".
print "Waiting for ship to leave the atmosphere..".
wait until SHIP:DYNAMICPRESSURE = 0. // don't create maneuver node until we are out of the atmosphere - otherwise the apoapsis altitude and eta will change due to drag
wait until SHIP:dynamicpressure = 0. // don't create maneuver node until we are out of the atmosphere - otherwise the apoapsis altitude and eta will change due to drag
print "Deploying solar panels".
PANELS ON.
PANELS on.
// Create maneuver node that will circularize the orbit.
// NOTE: Potential errors in the inclination are not fixed, since we are most likely going to change our orbit, which will make the inclination change cheaper later on.
local node is NODE(TIME:SECONDS + ETA:APOAPSIS, 0, 0, 0).
local node is node(TIME:SECONDS + ETA:APOAPSIS, 0, 0, 0).
// Burn magnitude in the prograde direction is the difference between the required velocity to acheive circular orbit at apoapsis altitude and our velocity at apoapsis
local required_velocity is orbital_velocity_from_ap_pe(APOAPSIS, APOAPSIS, target_orbit_altitude).
set node:PROGRADE to required_velocity - VELOCITYAT(SHIP, TIME:SECONDS + ETA:APOAPSIS):ORBIT:MAG.
set node:prograde to required_velocity - velocityat(SHIP, TIME:seconds + ETA:apoapsis):orbit:mag.
add node.
execute_node().
@ -114,14 +114,14 @@ function calculate_launch_azimuth {
parameter target_orbit_altitude. // to compensate for the rotation of the body, we need to know the velocity of the target orbit, which is calculated from its altitude
parameter launch_site_latitude.
local inertial_azimuth is ARCSIN(COS(target_orbit_inclination) / COS(launch_site_latitude)). // azimuth in inertial space, that is, disregarding the rotation of the body
local inertial_azimuth is arcsin(cos(target_orbit_inclination) / cos(launch_site_latitude)). // azimuth in inertial space, that is, disregarding the rotation of the body
local equatorial_rotational_velocity is (2 * CONSTANT:PI * BODY:RADIUS) / BODY:ROTATIONPERIOD.
local equatorial_rotational_velocity is (2 * CONSTANT:PI * BODY:radius) / BODY:rotationperiod.
local target_orbit_velocity is orbital_velocity_from_ap_pe(target_orbit_altitude, target_orbit_altitude, target_orbit_altitude).
local launch_vector_x_component is target_orbit_velocity * SIN(inertial_azimuth) - equatorial_rotational_velocity * COS(launch_site_latitude).
local launch_vector_y_component is target_orbit_velocity * COS(inertial_azimuth).
return ARCTAN2(launch_vector_x_component, launch_vector_y_component).
local launch_vector_x_component is target_orbit_velocity * sin(inertial_azimuth) - equatorial_rotational_velocity * cos(launch_site_latitude).
local launch_vector_y_component is target_orbit_velocity * cos(inertial_azimuth).
return arctan2(launch_vector_x_component, launch_vector_y_component).
}

View file

@ -8,10 +8,10 @@ function semi_major_axis {
// https://en.wikipedia.org/wiki/Semi-major_and_semi-minor_axes
// http://www.orbiterwiki.org/wiki/Front_Cover_Equations
//
parameter apoapsis is ORBIT:APOAPSIS.
parameter periapsis is ORBIT:PERIAPSIS.
parameter ap is APOAPSIS.
parameter pe is PERIAPSIS.
return (apoapsis + periapsis + 2*BODY:RADIUS) / 2. // Add 2x body radius because KSP measures apoapsis/periapsis from the surface
return (ap + pe + 2*BODY:radius) / 2. // Add 2x body radius because KSP measures apoapsis/periapsis from the surface
}
@ -23,14 +23,14 @@ function orbital_period {
// https://en.wikipedia.org/wiki/Semi-major_and_semi-minor_axes#Astronomy
// http://www.orbiterwiki.org/wiki/Front_Cover_Equations
//
parameter semi_major_axis is semi_major_axis().
parameter sme is semi_major_axis().
return 2 * CONSTANT:PI * SQRT(semi_major_axis^3 / BODY:MU).
return 2 * CONSTANT:PI * sqrt(sme^3 / BODY:mu).
}
function orbital_period_from_ap_pe {
parameter apoapsis, periapsis.
return 2 * CONSTANT:PI * SQRT(semi_major_axis(apoapsis, periapsis)^3 / BODY:MU).
parameter ap, pe.
return orbital_period(semi_major_axis(ap, pe)).
}
@ -44,12 +44,12 @@ function orbital_velocity {
parameter altitude.
parameter sma is semi_major_axis().
return SQRT(BODY:MU * ((2 / (altitude+BODY:RADIUS)) - (1 / sma))). // add body radius because KSP measures altitude from the surface
return sqrt(BODY:mu * ((2 / (altitude+BODY:radius)) - (1 / sma))). // add body radius because KSP measures altitude from the surface
}
function orbital_velocity_from_ap_pe {
parameter altitude, apoapsis, periapsis.
return orbital_velocity(altitude, semi_major_axis(apoapsis, periapsis)).
parameter altitude, ap, pe.
return orbital_velocity(altitude, semi_major_axis(ap, pe)).
}
@ -59,8 +59,8 @@ function orbital_eccentricity {
// Based on:
// https://en.wikipedia.org/wiki/Orbital_eccentricity#Calculation
// http://www.orbiterwiki.org/wiki/Front_Cover_Equations
parameter apoapsis is ORBIT:APOAPSIS.
parameter periapsis is ORBIT:PERIAPSIS.
parameter ap is APOAPSIS.
parameter pe is PERIAPSIS.
return (apoapsis - periapsis) / (apoapsis + periapsis).
return (ap - pe) / (ap + pe).
}

View file

@ -13,8 +13,8 @@ function estimated_burn_duration {
//
parameter node is NEXTNODE.
local exhaust_velocity is isp_sum() * (CONSTANT:G * KERBIN:MASS).
return ((SHIP:MASS * exhaust_velocity) / SHIP:MAXTHRUST) * (1 - CONSTANT:E^(-node:DELTAV:MAG/exhaust_velocity)).
local exhaust_velocity is isp_sum() * (CONSTANT:G * KERBIN:mass).
return ((SHIP:mass * exhaust_velocity) / SHIP:maxthrust) * (1 - CONSTANT:E^(-node:deltav:mag/exhaust_velocity)).
}
@ -30,15 +30,15 @@ function execute_node {
print "Estimated burn duration: " + ROUND(burn_duration, 1) + "s".
print "Aligning ship with burn vector..".
SAS OFF.
lock STEERING to node:DELTAV.
wait until VANG(SHIP:FACING:VECTOR, node:DELTAV) < 0.5.
SAS off.
lock STEERING to node:deltav.
wait until vang(SHIP:facing:vector, node:deltav) < 0.5.
print "Initializing warp".
warp_for(MAX(0, node:ETA - (burn_duration/2) - 5)). // warp until 5s before node
warp_for(MAX(0, node:eta - (burn_duration/2) - 5)). // warp until 5s before node
print "Approaching".
wait until node:ETA <= CEILING(burn_duration/2). // CEILING instead of ROUND, since we'd rather start the burn too soon to have time for perfecting the burn
wait until node:eta <= CEILING(burn_duration/2). // CEILING instead of ROUND, since we'd rather start the burn too soon to have time for perfecting the burn
print "Burn!".
lock THROTTLE to 1.0.
@ -48,15 +48,15 @@ function execute_node {
lock THROTTLE to MAX(0.01, estimated_burn_duration(node)). // ensure we always finish by burning with at least 1% power
// The burn vector will start to drift once we have very little left to burn. Therefore, take a "snapshot" of the burn vector as it is right now, and lock steering to it, instead of the dynamic vector
local dv0 is node:DELTAV.
local dv0 is node:deltav.
lock STEERING to dv0.
// Stop the burn once the "snapshot" vector dv0 and current burn vector start facing opposite directions
wait until VDOT(dv0, node:DELTAV) < 0.
wait until vdot(dv0, node:deltav) < 0.
set THROTTLE to 0.0.
print "=== MANEUVER NODE EXECUTED ===".
print ROUND(node:DELTAV:MAG, 3) + "m/s delta-v remaining".
print round(node:deltav:mag, 3) + "m/s delta-v remaining".
unlock_control().
wait 1.
remove node.

View file

@ -4,10 +4,10 @@ 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().
local engines_list is LIST().
list ENGINES in engines_list.
for engine in engines_list {
if engine:IGNITION and engine:FLAMEOUT {
if engine:ignition and engine:flameout {
return true.
}
}
@ -19,9 +19,9 @@ function should_stage {
//
// Return true if the rocket needs to stage.
//
return SHIP:STATUS = "PRELAUNCH" // e.g. still attached to launch tower
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)
or SHIP:maxthrustat(0) = 0. // no active engines (e.g. when we have an intermediate stage for decouplers before activating the next engine)
}
@ -29,7 +29,7 @@ function stage_when_ready {
//
// Stage when ready. Blocks until staging has been initiated.
//
wait until STAGE:READY.
wait until STAGE:ready.
print "STAGING".
STAGE.
}
@ -40,11 +40,11 @@ function isp_sum {
// Return the sum of vacuum ISP for enabled engines.
//
local sum is 0.
local engines_list is list().
local engines_list is LIST().
list ENGINES in engines_list.
for engine in engines_list {
if engine:IGNITION {
set sum to sum + engine:VACUUMISP.
if engine:ignition {
set sum to sum + engine:vacuumisp.
}
}
return sum.
@ -55,8 +55,8 @@ 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.
set SHIP:control:pilotmainthrottle to 0.
set SHIP:control:neutralize to true.
unlock STEERING.
unlock THROTTLE.
}

View file

@ -3,6 +3,6 @@
function round_towards_zero {
parameter n.
if n < 0 return CEILING(n).
return FLOOR(n).
if n < 0 return ceiling(n).
return floor(n).
}

View file

@ -4,10 +4,10 @@
local function set_actual_prograde {
if NAVMODE = "SURFACE" {
print "Switched actual_prograde to surface SRFPROGRADE".
lock actual_prograde to SHIP:SRFPROGRADE.
lock actual_prograde to SHIP:srfprograde.
} else {
print "Switched actual_prograde to orbit PROGRADE".
lock actual_prograde to SHIP:PROGRADE.
lock actual_prograde to SHIP:prograde.
}
return true. // preserve trigger
}
@ -16,7 +16,7 @@ set_actual_prograde().
// ANGLES
lock actual_prograde_pitch to VANG(actual_prograde:VECTOR, UP:VECTOR).
lock actual_prograde_pitch to vang(actual_prograde:vector, UP:vector).

View file

@ -3,11 +3,11 @@
function warp_to {
parameter timestamp.
KUNIVERSE:TIMEWARP:WARPTO(timestamp). // TODO: improve.
wait until TIME:SECONDS >= timestamp. // TODO
KUNIVERSE:timewarp:warpto(timestamp). // TODO: improve.
wait until TIME:seconds >= timestamp. // TODO
}
function warp_for {
parameter seconds.
return warp_to(TIME:SECONDS + seconds).
return warp_to(TIME:seconds + seconds).
}

View file

@ -8,16 +8,16 @@ function change_orbit {
//
// Change current orbit. Set parameter to -1 to maintain the current value
//
parameter inclination is ORBIT:INCLINATION. // vertical tilt of the orbit with respect to the equator
parameter eccentricity is ORBIT:ECCENTRICITY. // how circular the orbit is (e=0 circular)
parameter periapsis is ORBIT:PERIAPSIS. // lowest point in the orbit
parameter LAN is 0. // longitude of ascending node; the longitude where the orbit crosses the equator northwards
parameter argument_of_periapsis is ORBIT:ARGUMENTOFPERIAPSIS. // the angle from the ascending node to the periapsis, measured in the direction of motion
parameter inclination is ORBIT:inclination. // vertical tilt of the orbit with respect to the equator
parameter eccentricity is ORBIT:eccentricity. // how circular the orbit is (e=0 circular)
parameter periapsis is ORBIT:periapsis. // lowest point in the orbit
parameter lan is 0. // longitude of ascending node; the longitude where the orbit crosses the equator northwards
parameter argument_of_periapsis is ORBIT:argumentofperiapsis. // the angle from the ascending node to the periapsis, measured in the direction of motion
print inclination.
print eccentricity.
print periapsis.
print LAN.
print lan.
print argument_of_periapsis.
}