tbfromny

kOS Automated Science for Rover

Jun 21st, 2016
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. declare function ListScienceModules {
  2.     declare local scienceModules to list().
  3.     declare local partList to ship:parts.
  4.  
  5.     for thePart in partList { // loop through all of the parts
  6.         declare local moduleList to thePart:modules.
  7.         for theModule in moduleList { // loop through all of the modules of this part
  8.             declare local actionList to thePart:getmodule(theModule):allactions.
  9.             for theAction in actionList { // loop through all of the actions of this module
  10.                 if theAction:contains("data") { // name of the action contains the word 'data', so it's a science module
  11.                     scienceModules:add(thePart:getmodule(theModule)). // add it to the list
  12.                     break. // no need to continue looking at this module's actions
  13.                 }
  14.             }
  15.         }
  16.     }
  17.     return scienceModules.
  18. }
  19.  
  20. // GetSpecifiedResource takes one parameter, a search term, and returns the resource with that search term
  21. declare function GetSpecifiedResource {
  22.     declare parameter searchTerm.
  23.    
  24.     declare local allResources to ship:resources.
  25.     declare local theResult to "".
  26.    
  27.     for theResource in allResources {
  28.         if theResource:name = searchTerm {
  29.             set theResult to theResource.
  30.             break.
  31.         }
  32.     }
  33.     return theResult.
  34. }
  35.  
  36. // Given some science data to transmit,
  37. // - verify that sufficient electrical capacity exists to attempt to transmit
  38. // - wait until sufficient charge before transmitting
  39. declare function WaitForCharge {
  40.     declare parameter scienceData.
  41.    
  42.     // This value are from http://wiki.kerbalspaceprogram.com/wiki/Antenna
  43.     // for the Communotron 16 antenna.
  44.     // It'd be better if I could search for the antenna and get these values,
  45.     // but they don't appear to be there
  46.     declare local electricalPerData to 6.
  47.  
  48.     declare local electricalResource to GetSpecifiedResource("ElectricCharge").
  49.     declare local chargeMargin to 1.05. // Want to have not just enough, but a 5% margin
  50.     declare local canTransmit to true.
  51.     declare local neededCharge to scienceData:dataamount * electricalPerData * chargeMargin.
  52.    
  53.     if electricalResource:capacity > neededCharge {
  54.         if (electricalResource:amount < neededCharge) {
  55.             // current electrical capacity is insufficient, so wait and display messages
  56.             until electricalResource:amount > neededCharge {
  57.                 print "Waiting for sufficient electrical charge" at (1,2).
  58.                 print "Need: " + round(neededCharge, 1) + "  Have: " + round(electricalResource:amount, 1) + "   " at (1,3).
  59.                 wait 1.
  60.             }
  61.         }  
  62.     } else {
  63.         print "Insufficient electrical capacity to attempt transmission" at (1,2).
  64.         set canTransmit to false.
  65.     }  
  66.     return canTransmit.
  67. }
  68.  
  69. declare function TransmitScience {
  70.     declare parameter scienceModule.
  71.    
  72.     // This value is from http://wiki.kerbalspaceprogram.com/wiki/Antenna
  73.     // for the Communotron 16 antenna.
  74.     // It'd be better if I could search for the antenna and get these values,
  75.     // but they don't appear to be there
  76.     declare local timePerData to 10/3.
  77.     declare local transmissionMargin to 1.05. // Let's also put a 5% margin on the transmission time
  78.  
  79.     // Figure out how long it'll take to transmit data.
  80.     // Add 1 second for margin
  81.    
  82.     declare local transmissionTime to scienceModule:data[0]:dataamount / timePerData * transmissionMargin.
  83.  
  84.     print "Transmitting data                                   " at (1,2).
  85.     scienceModule:transmit().
  86.  
  87.     // Display transmission countdown timer
  88.     declare local startTime to time:seconds.
  89.     until time:seconds > startTime + transmissionTime {
  90.         print round(startTime + transmissionTime - time:seconds,1) + " seconds to go                                " at (1,3).
  91.         wait 0.2.
  92.     }
  93. }
  94.  
  95. // Function to run all re-runnable science experiments and transmit the results
  96. declare function PerformScienceExperiments {
  97.     declare local scienceModules to ListScienceModules().
  98.    
  99.     clearscreen.
  100.     // start by looking for existing science from previous experiments; transmit if found
  101.     for theModule in scienceModules {
  102.         if theModule:hasdata {
  103.             print "Existing data found in " + theModule:part:title at (1,1).
  104.             if WaitForCharge(theModule:data[0]) {
  105.                 TransmitScience(theModule).
  106.             }
  107.         }
  108.     }
  109.    
  110.     // Now, loop through the operable, re-runnable experiments, running them and transmitting data back
  111.     for theModule in scienceModules {
  112.         clearscreen.
  113.         print "Working with: " + theModule:part:title at (1,1).
  114.         wait 1.
  115.         // Only perform operable, re-runnable experiments on modules that don't have data
  116.         if (not theModule:inoperable) and (theModule:rerunnable) and (not theModule:hasdata) {
  117.             print "Collecting data                                               " at (1,2).
  118.             theModule:deploy(). // collect science, waiting for results to be ready
  119.             wait until theModule:hasdata.
  120.             if WaitForCharge(theModule:data[0]) {
  121.                 TransmitScience(theModule).
  122.             }
  123.         }
  124.         wait 1.
  125.     }
  126.     clearscreen.
  127.     print "All data collection and transmission complete".
  128. }
  129.  
  130. PerformScienceExperiments().
Advertisement
Add Comment
Please, Sign In to add comment