Skip to content

Instantly share code, notes, and snippets.

@quill18
Last active February 22, 2017 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save quill18/c4b71d81f9202db11455 to your computer and use it in GitHub Desktop.
Save quill18/c4b71d81f9202db11455 to your computer and use it in GitHub Desktop.
// A Reactor Management Script by quill18 (http://quill18.com)
// Latest Version: https://gist.github.com/quill18/c4b71d81f9202db11455
//
//
// Developed for:
// Kerbal Space Program 1.04
// kOS 0.17.3
// Near Future Electrical 0.5.3
//
//
// This kOS script manages parts with "FissionReactor" modules, which is
// mostly stuff from the Near Future Electrical mod. These reactors
// burn uranium while operating, even if batteries are full and solar
// panels would be sufficient to supply power to the vessel. To conserve
// fuel, it is desirable to disable your reactors when you have suffient
// power.
//
// This script automatically enables reactors when your total power storage
// is below 50% and automatically disables them when your storage is above
// 95%. Additionally, it will disable reactors if their temperature gets
// too high.
//
// A flag to track if reactors are enabled or not. Slightly faster than
// looping through them and checking the status flag.
GLOBAL ReactorStatus TO false.
// Find every part with a "FissionReactor" module.
GLOBAL ReactorModuleList to SHIP:MODULESNAMED("FissionReactor").
FOR MOD IN ReactorModuleList {
//print "Reactor Name: " + MOD:NAME.
//print MOD:ALLFIELDS.
//print MOD:ALLEVENTS.
//print MOD:ALLACTIONS.
//print MOD:GETFIELD("status").
}.
PRINT "--- STARTING ALL REACTORS TO SET INITIAL STATE ---".
PRINT "--- Script will then check electric charge every 10 secs. ---".
PRINT "--- Use CTRL-C if you need to cancel this script. ---".
StartAllReactors().
WAIT 1.
UNTIL FALSE {
LIST RESOURCES IN RESLIST.
FOR RES IN RESLIST {
if(RES:NAME = "ELECTRICCHARGE") {
LOCAL battery_percentage TO ROUND(100*RES:AMOUNT/RES:CAPACITY).
PRINT "Electric Charge is at: " + battery_percentage + "%".
if( IsReactorOverheated() ) {
if(ReactorStatus = true) {
PRINT "Temperature is at: " + temp + " -- INITIATING SAFETY SHUTDOWN".
StopAllReactors().
}
}
else if(battery_percentage < 50 AND ReactorStatus = false) {
print "STARTING REACTOR!".
StartAllReactors().
}
else if (battery_percentage >= 95 AND ReactorStatus = true) {
print "STOPPING REACTOR!".
StopAllReactors().
}
}
}.
WAIT 10.
}
function StartAllReactors {
FOR MOD IN ReactorModuleList {
MOD:DOACTION("start reactor", true).
}
SET ReactorStatus TO true.
}
function StopAllReactors {
FOR MOD IN ReactorModuleList {
MOD:DOACTION("deactivate reactor", true).
}
SET ReactorStatus TO false.
}
function IsReactorOverheated {
// NOTE: At time of writting, kOS has no proper string functions, nor
// any way to convert a string to an int. String comparisons
// are based purely on string length. Luckily, this works out
// okay for us because once temp reaches 1000 degress, the
// string reads "1000 K", which is longer than "999 K" and,
// 1000 K is roughly where we want to shut down the reactors.
FOR MOD IN ReactorModuleList {
if(MOD:GETFIELD("reactor temperature") > "999 K") {
return true.
}
}
return false.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment