M4thG33k

Reactor Display

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