From fc0697076fc19b205e07ad44be4d373d9d9347be Mon Sep 17 00:00:00 2001 From: Petruchio96 <44350446+Petruchio96@users.noreply.github.com> Date: Tue, 8 Jan 2019 15:49:31 -0600 Subject: [PATCH] Added error checking, refactored and optimized code. - breaks previous version Signed-off-by: Petruchio96 <44350446+Petruchio96@users.noreply.github.com> --- README.md | 32 ++++++++++----------- RunScience.ks | 25 ++++++++++------- Standard_Lib.ks | 75 ++++++++++++++++++++++++++++++++----------------- 3 files changed, 80 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index 2b47db6..48b9a0f 100644 --- a/README.md +++ b/README.md @@ -44,18 +44,17 @@ _**Optional Parameters:** none_ * Function CheckCharging() * Function CalcChargeTime(DataSize, _Optional ChargePerMit_) * Function ShipMaxCharge() -* Function GetESU() +* Function GetESUlist() * Function ClearAllScience(_Optional SensorList_) * Function GetDeployableAntList() +* Function GetNonDeployableAntList() * Function ExtendAllAnts(_Optional AntList_) * Function RetractAllAnts(_Optional AntList_) * Function GetSensorList() ### Function GetAllScience(_Optional SensorList, Optional Verbose_) -**Description:** Runs all science experiments available on the ship, or only on a list of science parts that is passed -to the function as a parameter. If the ship (or the list) has more than 1 Goo or Science Bay, only 1 of each will be -used. +**Description:** Runs all science experiments available on the ship, or only on a list of science parts that is passed to the function as a parameter. If the ship (or the list) has more than 1 Goo or Science Bay, only 1 of each will be used. **Required functions called:** GetSensorList() @@ -67,12 +66,7 @@ used. ### Function TransmitAllScience(_Optional SensorList, Optional WarpTime, Optional Verbose, Optional ChargePerMit_) -**Description:** Transmits all science data available on the ship, or only a list of science parts that is passed to -the function as a parameter. For each science experiment it will check to see if there is enough charge. If not, -it will check to see if the ship is charging and calculate the time required to charge. The function will warp by -default unless the optional parameter WarpTime is passed in as "false." The fuction aborts if the ship's total -charge capacity isn't enough to transmit the data or if the ship is not charging and there's not enough charge to -transmit. If all science is transmitted the function returns "Success." +**Description:** Transmits all science data available on the ship, or only a list of science parts that is passed to the function as a parameter. The ship must have an external antenna part with a connection to the KSC to transmit; otherwise, the function will abort. This will not use the data transmitter in a command module. For each science experiment it will check to see if there is enough charge. If not, it will check to see if the ship is charging and calculate the time required to charge. The function will warp by default unless the optional parameter WarpTime is passed in as "false." The fuction aborts if the ship's total charge capacity isn't enough to transmit the data, or if the ship is not charging and there's not enough charge to transmit. **Required functions called:** * GetDeployableAntList() @@ -91,9 +85,7 @@ _**Optional Parameters:**_ * _ChargePerMit - if you know the charge permit for the antenna, pass this for more accurate_ _time calculations._ -_**Known Issues:**_ Do not use this while the ship is at high velocity and in an atmosphere. It will deploy the -antennas and break them. I do not know how to make KSP select a specific antenna to transmit, therefore I wrote the -function to extend all the antennas and use which ever one KSP picks. _**If you know how to specify a particular antenna for KSP to use for each transmission let me know!**_ +_**Known Issues:**_ Do not use this while the ship is at high velocity and in an atmosphere. It will deploy the antennas and break them. I do not know how to make KSP select a specific antenna to transmit, therefore I wrote thefunction to extend all the antennas and use which ever one KSP picks. _**If you know how to specify a particular antenna for KSP to use for each transmission let me know!**_ ### Function GetYorN(_Optional DisplayText_) @@ -130,9 +122,9 @@ _**Optional Parameters:** ChargePerMit - this is the charge required to send one _**Optional Parameters:** None_ -### Function GetEsu() +### Function GetEsulist() -**Description:** Searches the ship for an Experiment Storage Unit or "Science Box". Finds the first one and returns the part. Returns "False" if there is no ESU on the ship. +**Description:** Searches the ship for an Experiment Storage Unit or "Science Box". Finds the first one and returns a list with just 1 ESU - the first one it finds. Returns a blank list if there is no ESU on the ship. **Required Parameters:** None @@ -154,6 +146,14 @@ _**Optional Parameters:** Sensor list - a list of science parts the user can sen _**Optional Parameters:** None_ +### Function GetNonDeployableAntList() + +**Description:** Returns a list of fixed, non deployable antenna parts. The function checks all the parts on the ship and returns a list of parts of just fixed antennas. This does not include the transmitter in a command module + +**Required Parameters:** None + +_**Optional Parameters:** None_ + ### Function ExtendAllAnts(_Optional AntList_) **Description:** Extends all deployable antennas on the ship or just the ones in a list passed to the function @@ -172,7 +172,7 @@ _**Optional Parameters:** AntList - a list of parts, must only be deployable ant ### Function GetSensorList() -**Description:** Returns a list of parts that can perform science and save data. The function checks all the parts on the ship and returns a list of just science parts. +**Description:** Returns a list of parts that can perform science and save data. The function checks all the parts on the ship and returns a list of those that can perform science. **Required Parameters:** None diff --git a/RunScience.ks b/RunScience.ks index c6eaf27..2cdcc21 100644 --- a/RunScience.ks +++ b/RunScience.ks @@ -9,41 +9,46 @@ //click on each part or check to see if I've already got the data before running a goo or science bay. //Requirements - must have "Standard_Lib.ks" file in the same directory -// - Must have a Experiment Storage Unit part (ESU or "Science Box") on the ship +// - For option #2 to function, an Experiment Storage Unit part (ESU or "Science Box") on +// the ship. The program will inform you of this. Options 1 and 3 will still function run Standard_Lib.ks. declare sensorlist to GetSensorList(). declare UserChoice to "none". -declare ESU to GetESU(). +declare ESUList to GetESUlist(). declare Done is False. function ResetScreen{ + Clearscreen. print ("Select an option: ") at(0,1). print ("1: Run all science experiments") at(5,2). print ("2: Collect all science") at(5,3). print ("3: Clear all science experiments") at(5,4). print ("4: End program") at(5,5). + print (" ") at(0,7). }. -Clearscreen. +ResetScreen(). until Done { - ResetScreen(). set UserChoice to GetUserInput(). if UserChoice = 1 { GetAllScience(sensorlist, False). - Clearscreen. ResetScreen(). print("Ran all available science experiments") at(0,7). } else if UserChoice = 2 { - ESU:GETMODULE("ModuleScienceContainer"):doaction("collect all", true). - Clearscreen. - ResetScreen(). - print("Collected all available science") at(0,7). + if ESUList:length = 0 { + ResetScreen(). + print("No ESU on this ship, no data stored.") at(0,7). + } else { + ESUList[0]:GETMODULE("ModuleScienceContainer"):doaction("collect all", true). + ResetScreen(). + print("Collected all available science") at(0,7). + } } else if UserChoice = 3 { - Clearscreen. ResetScreen(). print ("Reset " + ClearAllScience(sensorlist) + " experiments") at(0,7). + } else if UserChoice = 4 { set Done to True. } diff --git a/Standard_Lib.ks b/Standard_Lib.ks index 3ec2de1..27c5df6 100644 --- a/Standard_Lib.ks +++ b/Standard_Lib.ks @@ -80,37 +80,53 @@ function ShipMaxCharge { return amount. }. -function GetESU { -//Returns an Experiment Storage Unit Part - - Declare Local plist to ship:parts. +function GetESUlist { +//Returns a list of 1 ESU part (first it finds) or a blank list if none on ship. + Declare Local plist to List(). + Declare Local NumInList to 0. + + Set plist to ship:PARTSNAMED("ScienceBox"). + if plist:length = 0 { + print("No ESU on this ship"). + Return plist. + }. - for item in plist { - if item:name = "ScienceBox" { - return item. - } + //Remove any extra ESUs and only return the 1st if found + If plist:length > 1 { + set NumInList to plist:length. + From {Local I is NumInList.} UNTIL I = 1 STEP {set I to I - 1.} do { + plist:remove(I-1). } - print("No ESU on this ship"). - Return false. + }. + Return plist. }. function GetDeployableAntList { //Returns a list of Deployable Antennas Declare Local AntList to list(). - Declare Local ModuleList to list(). Declare Local plist to ship:parts. for item in plist { - set ModuleList to item:modules. - for mod in modulelist { - if mod = "ModuleDeployableAntenna" { - AntList:add(item). - } + if item:hasmodule("ModuleDeployableAntenna") { + AntList:add(item). } } return AntList. }. +function GetNonDeployableAntList { +//Returns a list of Non-Deployable Antennas + Declare Local AntList to list(). + Declare Local plist to ship:parts. + + for item in plist { + if NOT item:hasmodule("ModuleDeployableAntenna") AND NOT item:hasmodule("ModuleCommand") AND item:hasmodule("ModuleDataTransmitter"){ + AntList:add(item). + } + }. + return AntList. +}. + function ExtendAllAnts { //Extend all Deployable Antennas or deploy a list of antennas Declare Parameter AntList is list(). //Optional paramenter - list of antennas to deploy @@ -142,15 +158,11 @@ function RetractAllAnts { function GetSensorList { //Returns a list of all science parts Declare Local SensorList to list(). - Declare Local ModuleList to list(). Declare Local plist to ship:parts. for item in plist { - set ModuleList to item:modules. - for mod in modulelist { - if mod = "ModuleScienceExperiment" { - SensorList:add(item). - } + if item:hasmodule("ModuleScienceExperiment") { + SensorList:add(item). } } return SensorList. @@ -256,6 +268,7 @@ Function GetAllScience { if Verbose {print ("Completed " + Counter + " available science experiments").} return SensorList. }. + function TransmitAllScience { //Transmits all science experiments that have data on the ship or from a list of sensors sent to the function @@ -278,12 +291,23 @@ function TransmitAllScience { Declare Local DataName to " ". Declare Local AntList to GetDeployableAntList(). - print (" "). + //Must have antennas to transmit data + if AntList:length = 0 { + set Antlist to GetNonDeployableAntList(). + if AntList:length = 0 { + if Verbose {print ("****NO ANTENNAS ON SHIP - ABORTING TRANSMISSION ****").} + return false. + } + //Must reset Antlit to a deployable list, otherwise ExtendAllAnts() will crash. + Set AntList to GetDeployableAntList(). + }. + + if Verbose {print (" ").} //Check if comms to KSC, else stop here and return false if HomeConnection:IsConnected = false { if Verbose {print ("****NO CONNECTION TO KSC - ABORTING TRANSMISSION ****").} return false. - } + }. //Deploy all antennas ExtendAllAnts(AntList). @@ -391,8 +415,7 @@ function TransmitAllScience { //There is a delay when an antenna is done before it can xmit again00 Wait .5. }. - Print ("All Science Data Transmitted"). - return Success. + if Verbose {Print ("All Science Data Transmitted").} }. print("Loaded Library Standard_Lib.ks"). \ No newline at end of file