kOS
kOS is a mod for Kerbal Space Program which adds the abiltiy to automate operations in the game by adding computers to the simulation which can be programmed.
The computers are represented as “processor” parts which run programs written in “KerboScript” which is a simple programming language.
The computers are run as part of the simulation including a file system that is persisted and time is based on game time for example it is affected by time warp.
KerboScript provides built-in functionality for operating your vessel, communicating with other vessels and the KSC, and also some operations for controlling the game itself.
KerboScript provides access to features from other mods for parts on your vessel which exposed via the part menu. Since KSP mods are able to attach “modules” to each part, KerboScript exposes the actions, and events for each module such as undocking ports or extending solar panels.
The language also provides relevant built-in math functionality such as vectors and rotations, and convenient access to key information such as prograde and time to apoapsis.
The Scripts folder in the game folder is represented as a drive in the simulation known as the “archive” located at the KSC can be accessed while your vessel can communicate with the KSC. Processers can both read and write data to the archive which allows for reading updated code and logging telemetry back.
Each processor can be running a script or in an interactive mode. kOS also provides an optional telnet server which can connect to any active processor.
Since processors are part of the simulation, they only run while the vessel is loaded. They reboot when they become loaded again.
A boot script can be configured for a processor which copies the script from the Scripts/boot folder into the processor’s /boot storage when the vessel is first launched. The processor will then run the script whenever the vessel is loaded.
If the script is changed in the Scripts folder, this change is not reflected in the processor because the script is only copied once as part of launching the vessel. In fact, reverting to launch will also not update the copy in the processor.
Implementing auto-updates can be done by checking if the script does not match the script stored in the processor. Drive 0 represents the script in the Scripts folder, and Drive 1 represents the script in the processor.
If the script does not match, a new version is copied and the processor is rebooted.
print "checking for updates".
if (
open("0:boot/octobattery.ks"):readall:string
<> open("1:boot/octobattery.ks"):readall:string
) {
switch to 0.
copypath("boot/octobattery.ks", "1:boot/octobattery.ks").
print "rebooting".
wait 1.
reboot.
}
The script that is run during boot can also be changed after the vessel is launched by changing core:bootfilename
Multiple processors can be attached to a vessel so messaging passing parallelism can be achieved. Introspection of the vessel can be done by accessing the parts.