Advertisement
Guest User

reactorControl

a guest
Aug 22nd, 2014
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.68 KB | None | 0 0
  1. -- RD CC Reactor Control
  2. -- v 0.0.2
  3.  
  4. -- Dependancies:
  5. --   rdCore
  6.  
  7. -- Thank you for choosing this Big Reactors reactor
  8. -- control program for automating it with CC.
  9.  
  10. -- User Settings
  11.  
  12. -- Stored Power Threshold
  13. -- Set a custom stored power threshold to lower the
  14. -- reactor cores to in order to save power.
  15.  
  16. local reacStTh = 75 -- 1/100 Default: 100
  17.  
  18. -- Reactor Shutdown Threshold
  19. -- Should the reactor shut down after a particular
  20. -- point to conserve fuel. Must be set equal or
  21. -- higher then Stored Power Threshold.
  22.  
  23. local reacOf = true -- true/false Default: false
  24.  
  25. local reacOfTh = 80 -- 5/100 Default: 100
  26.  
  27. -- Reactor Name
  28. -- If a monitor is found, a name may be displayed
  29. -- for the reactor.
  30.  
  31. local reacName = ""
  32.  
  33. -- Debug Feedback
  34. -- For double-checking numbers through the monitor.
  35. -- Should not be needed unless issues or discrepancies
  36. -- arrise.
  37.  
  38. local debugF = true -- true/false Default: false
  39.  
  40. --====== DO NOT EDIT BELOW THIS POINT ======--
  41.  
  42.  
  43.  
  44. -- Reactor Variables
  45.  
  46. local function reUpdate()
  47.  
  48.     reEn = tonumber(reactor.getEnergyStored())
  49.     dBR("Reactor Stored Energy: "..reEn.." rf")
  50.     reOn = reactor.getActive()
  51.   if reOn == true then
  52.     dBR("Reactor State: Online ")
  53.   else
  54.     dBR("Reactor State: Offline ")
  55.   end
  56.     reFuel = tonumber(reactor.getFuelAmount())
  57.     dBR("Reactor Fuel: "..reFuel.." mB")
  58.     reEnPr = tonumber(reactor.getEnergyProducedLastTick())
  59.     dBR("Reactor Energy Produced: "..reEnPr.." rf/t")
  60.     reWa = tonumber(reactor.getWasteAmount())
  61.     dBR("Reactor Waste: "..reWa.." mB")
  62.     reFuWa = tonumber(reactor.getFuelAmountMax())
  63.     dBR("Reactor Fuel/Waste Contained: "..reFuWa.." mB")
  64.     reEnPerc = math.floor((reEn/10000000)*100)
  65.     dBR("Reactor Energy Filled Percentage: "..reEnPerc.."%")
  66.     reFTemp = tonumber(reactor.getFuelTemperature())
  67.     dBR("Reactor Fuel Temp: "..reFTemp.." C")
  68.     reCTemp = tonumber(reactor.getCasingTemperature())
  69.     dBR("Reactor Casing Temp: "..reFTemp.." ¥B0 C")
  70.     reTemp = ((reCTemp+reFTemp)/2)
  71.     dBR("Reactor Avg Temp: "..reTemp.." ¥B0 C")
  72.  
  73.  
  74. end
  75.  
  76. -- Debug readout function
  77.  
  78. function dBR(var)
  79.     if debugF == true then
  80.         print(var)
  81.     end
  82. end
  83.  
  84. -- Control Rod Controls
  85.  
  86. function reCtrlRod()
  87.     --reUpdate()
  88.     print(reEnPerc.."%.")
  89.     if reEnPerc <= reacStTh then
  90.         dBR("Changing Ctrl Rod Levels to "..reEnPerc.."%.")
  91.         reactor.setAllControlRodLevels(reEnPerc)
  92.     end
  93. end
  94.  
  95. -- Power Threshold
  96.  
  97. function rePowThr()
  98.   dBR("Staus Power Threshold Neared")
  99.   local rtest = reactor.getActive()
  100.   --reUpdate()
  101.   dBR(reEnPerc.." %")
  102.   dBR(reacOfTh.." %")
  103.   if reEnPerc >= reacOfTh then
  104.     dBR("Reactor Shutdown In Progress")
  105.     reactor.setActive(false)
  106.     rtest = reactor.getActive()
  107.     if rtest == true then
  108.       dBR("Reactor Shutdown Failed")
  109.     end
  110.   elseif reEnPerc < (reacOfTh-5) then
  111.     dBR("Reactor Activation in Progress")
  112.     reactor.setActive(true)
  113.     rtest = reactor.getActive()
  114.     if rTest == false then
  115.       dBR("Reactor Startup Failed")
  116.     end
  117.    
  118.   end
  119. end
  120.  
  121. -- Adv Monitor Functionality
  122.  
  123. -- monitor variables
  124.  
  125. maxX, maxY = term.getSize()
  126.  
  127. -- monitor functions
  128.  
  129. function updateScreen()
  130.     if monTest ~= nil then
  131.         reUpdate()
  132.         mon.clear()
  133.         reacTitle()
  134.         reacEnergy()
  135.         reacReadout()
  136.     end
  137. end
  138.  
  139. function reacTitle()
  140.     local progTitle = "RD Reactor Control ver "..progVer
  141.     local reacTitle = reacName
  142.     mon.setCursorPos(maxX/2-string.len(progTitle)/2,2)
  143.     mon.setTextColor(colors.purple)
  144.     mon.write(progTitle)
  145.     if reacName ~= nil then
  146.         mon.setCursorPos(maxX/2-string.len(reacTitle)/2,3)
  147.         mon.setTextColor(colors.purple)
  148.         mon.write(reacTitle)
  149.     end
  150. end
  151.  
  152. function put(text,x,y,color)
  153.     term.setCursorPos(x,y)
  154.     term.setTextColor(color or 1)
  155.     term.write(text)
  156. end
  157.  
  158. function reacEnergy()
  159.     local enTitle = "Reactor RF Stored"
  160.     local enAmt = reEn.." RF"
  161.     local enPerc = reEnPerc.." %"
  162.    
  163.     put(enTitle,maxX/2-string.len(enTitle), 5, colors.blue)
  164.     put(enTitle,maxX/2-string.len(enAmt.." / "..enPerc), 7, colors.blue)
  165.    
  166.     paintutils.drawLine(8,9,28,9,colors.white)
  167.     paintutils.drawLine(8,9,(20*(reEnPerc/100))+8,9,colors.red)
  168.     mon.setBackgroundColor(colors.black)
  169.  
  170. end
  171.  
  172. function reacReadout()
  173.  
  174. end
  175.  
  176.  
  177. -- Find Reactor & Monitor Peripherals
  178.  
  179. local function getPeripherals()
  180.  
  181.     reactor = peripheral.wrap(pSearch("BigReactors-Reactor"))
  182.     monTest = peripheral.find("monitor")
  183.  
  184.     if reactor.getConnected() == true then
  185.         dBR("Reactor Connected")
  186.     else
  187.         print("Reactor Not Found")
  188.         term.setTextColor(colors.red)
  189.         print("Please relocate this computer next to the reactor's computer port!")
  190.         term.setTextColor(colors.white)
  191.         --break
  192.     end
  193.     if monTest ~= nil then
  194.         mon = peripheral.wrap(pSearch("monitor"))
  195.         dBR("Monitor Found")
  196.     else
  197.         dBR("Monitor Not Found")
  198.     end
  199.  
  200. end
  201.    
  202. -- Main Program
  203.  
  204. local rdCTest = fs.exists("rdCore")
  205. local rdCPB = "vKDJH93Y"
  206. progVer = "0.0.2"
  207. local req = 0
  208. local runs = 0
  209. local reactor
  210. local mon
  211.  
  212. if rdCTest == false then
  213.     print("rdCore Api Not Found. Installing embedded version.")
  214.     shell.run("pastebin", "get", rdCPB, "rdCore")
  215.     shell.run("rdCore")
  216.     rdCTest = fs.exists("rdCore")
  217.     if rdCTest == true then
  218.         add2start("reactorControl")
  219.         print("rdCore Api ver "..rdCV.." Installed Successfully")
  220.         req = 1
  221.     else
  222.         print("rdCore Api Unavailable. RD Reactor Control Unable to Start")
  223.         --break
  224.     end
  225. else
  226.     print("rdCore Api Found")
  227.     req = 1
  228. end
  229.  
  230. getPeripherals()
  231.  
  232. local temp = ("RD Reactor Control ver: "..progVer.." Initialized Successfully")
  233.  
  234. while true do
  235.     if runs == 0 then
  236.         if req == 0 then
  237.             break
  238.         else
  239.             print(temp)
  240.         end
  241.   runs = runs + 1
  242.  elseif runs == 5 then
  243.         updateScreen()
  244.         runs = 1
  245.     else
  246.         runs = runs + 1
  247.     end
  248.    
  249.   reUpdate()
  250.     reCtrlRod()
  251.     if reacOf == true then
  252.         rePowThr()
  253.     end
  254.    
  255.     sleep(2)
  256.   end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement