M4thG33k

Reactor Program

Jun 10th, 2015
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.35 KB | None | 0 0
  1. os.loadAPI("button")
  2. os.loadAPI("bar")
  3. os.loadAPI("label")
  4.  
  5.  
  6. --first we find the monitor, reactor, and capacitor bank
  7.  
  8. M = peripheral.find("monitor")
  9. R = peripheral.find("BigReactors-Reactor")
  10. C = peripheral.find("tile_blockcapacitorbank_name")
  11.  
  12. --set configs here
  13. ----------------------------------------------------------------------------
  14. --the reactor won't turn on until we are below minPercentage and will run
  15. --until we are above the maxPercentage
  16. local minPercentage = .50
  17. local maxPercentage = .75
  18.  
  19. ----------------------------------------------------------------------------
  20. M.clear()
  21. local width,height = M.getSize()
  22. local maxPowerStorage = C.getMaxEnergyStored()
  23. local maxPowerDisplay
  24. if (maxPowerStorage<1000) then
  25.     maxPowerDisplay = tostring(maxPowerStorage).." RF"
  26. elseif (maxPowerStorage<1000000) then
  27.     maxPowerDisplay = tostring(maxPowerStorage/1000).." kRF"
  28. elseif (maxPowerStorage<1000000000) then
  29.     maxPowerDisplay = tostring((math.floor(maxPowerStorage/1000))/1000).." mRF"
  30. else
  31.     maxPowerDisplay = tostring((math.floor(maxPowerStorage/1000000))/1000).." bRF"
  32. end
  33. local currPower
  34. local currFuel
  35. local maxFuel = R.getFuelAmountMax()
  36. local rodInsertion
  37. local RfPerTick
  38. local throughput
  39.  
  40. local isAutomatic = true
  41.  
  42. --gets the current power stored in the capacitor. It then edits the loading bar and text information
  43. function updatePower()
  44.     currPower = C.getEnergyStored()
  45.     bar.changePercentage("power",currPower/maxPowerStorage)
  46.    
  47.     --get the RF/t change in the capacitor
  48.     local a = C.getEnergyStored()
  49.     sleep(0.05)
  50.     local b = C.getEnergyStored()
  51.     throughput = (b-a)/2
  52.    
  53. end
  54.  
  55.  
  56. --checks to see if the reactor should be turned on/off and get reactor information
  57. function updateReactor()
  58.     if (isAutomatic==true) then
  59.         if (R.getActive()) then
  60.             if (currPower/maxPowerStorage) >= maxPercentage then
  61.                 R.setActive(false)
  62.             end
  63.         else
  64.             if (currPower/maxPowerStorage) < minPercentage then
  65.                 R.setActive(true)
  66.             end
  67.         end
  68.     end
  69.    
  70.     label.changeGood("active",R.getActive())
  71.    
  72.     --check control rod levels
  73.     rodInsertion = R.getControlRodLevel(0)
  74.     --print("Rod insertion at"..rodInsertion)
  75.     bar.changePercentage("rods_foreground",rodInsertion/100)
  76.    
  77.     --check fuel levels
  78.     currFuel = R.getFuelAmount()
  79.     bar.changePercentage("fuel",currFuel/maxFuel)
  80.    
  81.     --get energy production
  82.     RfPerTick = R.getEnergyProducedLastTick()
  83. end
  84.  
  85. --initializes the loading bars
  86. function initBars()
  87.     bar.editBar("power",true,2,height,3,height-3,0,colors.gray,colors.orange)
  88.     bar.editBar("fuel",true,width-3,height,3,height-3,0,colors.gray,colors.cyan)
  89.     bar.editBar("rods_foreground",false,6,height,width-12,1,0,colors.yellow,colors.gray)
  90. end
  91.  
  92. --initializes the labels
  93. function initLabels()
  94.     label.editLabel("active","ACTIVE",1,1,colors.red,colors.black,colors.black,colors.green,false)
  95. end
  96.  
  97. --initializes the buttons
  98. function initButtons()
  99.     button.editButton("+100",rodIn,100,18,21,height-1,height-1)
  100.     button.editButton("+10",rodIn,10,23,25,height-1,height-1)
  101.     button.editButton("+1",rodIn,1,27,29,height-1,height-1)
  102.     button.editButton("-1",rodIn,-1,31,33,height-1,height-1)
  103.     button.editButton("-10",rodIn,-10,35,37,height-1,height-1)
  104.     button.editButton("-100",rodIn,-100,39,42,height-1,height-1)
  105.     button.editButton("AutoMode",toggleAuto,"",16,25,1,1)
  106.     button.editButton("active",toggleActive,"",1,6,1,1)
  107.     button.toggleButton("active") --defaults to on
  108. end
  109.  
  110. --changes the rod insertion by the input amount. (negative values decrease the insertion)
  111. function rodIn(amount)
  112.     if (rodInsertion+amount>100) then
  113.         R.setAllControlRodLevels(100)
  114.     elseif (rodInsertion+amount<0) then
  115.         R.setAllControlRodLevels(0)
  116.     else
  117.         R.setAllControlRodLevels(rodInsertion+amount)
  118.     end
  119.    
  120.     --flash the correct button
  121.     if (amount==100) then
  122.         button.flash("+100")
  123.     elseif (amount==10) then
  124.         button.flash("+10")
  125.     elseif (amount==1) then
  126.         button.flash("+1")
  127.     elseif (amount==-1) then
  128.         button.flash("-1")
  129.     elseif (amount==-10) then
  130.         button.flash("-10")
  131.     elseif (amount==-100) then
  132.         button.flash("-100")
  133.     end
  134. end
  135.  
  136. --toggles between manual and automatic modes
  137. function toggleAuto()
  138.     isAutomatic = not isAutomatic
  139.     button.toggleButton("AutoMode")
  140. end
  141.  
  142. --toggles whether the reactor is on or off
  143. function toggleActive()
  144.     R.setActive(not R.getActive())
  145.     button.toggleButton("active")
  146. end
  147.  
  148. --inits everything
  149. function initAll()
  150.     initBars()
  151.     initLabels()
  152.     initButtons()
  153. end
  154.  
  155.  
  156. --writes information
  157. function drawInfo()
  158.     --power percentage
  159.     M.setCursorPos(8,1)
  160.     val = math.floor((currPower/maxPowerStorage)*10000)/100
  161.     val = tostring(val).."%"
  162.     M.write(val)
  163.    
  164.     --fuel percentage
  165.     M.setCursorPos(width-3,2)
  166.     val = math.ceil((currFuel/maxFuel)*100)
  167.     val = tostring(val).."%"
  168.     M.write(val)
  169.    
  170.     --power values
  171.     M.setCursorPos(1,2)
  172.     if currPower<1000 then
  173.         val = tostring(currPower).." RF/"
  174.     elseif currPower < 1000000 then
  175.         val = tostring(currPower/1000).." kRF/"
  176.     elseif currPower < 1000000000 then
  177.         val = tostring((math.floor(currPower/1000))/1000).." mRF/"
  178.     else
  179.         val = tostring((math.floor(currPower/1000000))/1000).." bRF/"
  180.     end
  181.     val = val..maxPowerDisplay
  182.     M.write(val)
  183.    
  184.     --core rod insertion
  185.     M.setCursorPos(6,height-1)
  186.     val = "Rods at "..tostring(rodInsertion).."%"
  187.     M.write(val)
  188.    
  189.     --Rf/t production
  190.     M.setCursorPos(27,1)
  191.     val = "RF/t Prod: "..tostring(RfPerTick)
  192.     M.write(val)
  193.    
  194.     --Capacitor throughput
  195.     M.setCursorPos(1,3)
  196.     if (throughput>=0) then
  197.         M.setTextColor(colors.green)
  198.     else
  199.         M.setTextColor(colors.red)
  200.     end
  201.     M.write("Capacitor change: "..throughput)
  202.     M.setTextColor(colors.white)
  203. end
  204.  
  205.  
  206. --draws the screen
  207. function drawScreen()
  208.     M.clear()
  209.     button.screen()
  210.     bar.drawBars()
  211.     label.writeLabels()
  212.     drawInfo()
  213. end
  214.  
  215. --update all the information
  216. function updateAll()
  217.     updatePower()
  218.     updateReactor()
  219. end
  220.  
  221.  
  222. --does button things
  223. function doButton()
  224.    
  225.     timerCode = os.startTimer(1)
  226.     local event,side,x,y
  227.     repeat
  228.    
  229.         event,side,x,y = os.pullEvent()
  230.         --print(event)
  231.         --if event == "timer" then
  232.             --print(timerCode..":"..side)
  233.             --if timerCode ~= side then
  234.             --  print("Wrong code!")
  235.             --else
  236.             --  print("Right code!")
  237.             --end
  238.         --end
  239.     until even~="timer" or timerCode==side
  240.     if event=="monitor_touch" then
  241.         print(x..":"..y)
  242.         button.checkXY(x,y)
  243.     end
  244. end
  245.  
  246. --run everything
  247. initAll()
  248. while true do
  249.     doButton()
  250.     updateAll()
  251.     drawScreen()
  252.     sleep(0.1)
  253. end
Advertisement
Add Comment
Please, Sign In to add comment