Advertisement
snieh

BasicReactorCore

Apr 4th, 2020
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.55 KB | None | 0 0
  1. --##Link all chosen peripherals
  2. mon = peripheral.wrap("monitor_2")
  3. core = peripheral.wrap("draconic_rf_storage_2")
  4. reactor = peripheral.wrap("BigReactors-Reactor_1")
  5.  
  6. backColor = colors.black
  7. exitProgram = 0
  8.  
  9. --##increase screen resolution to max and ensure all terminal output goes to monitor.
  10. term.redirect(mon)
  11. mon.setTextScale(.5)
  12.  
  13. local function int(num1)
  14.    
  15.     num2 = math.ceil(num1)
  16.     if num2 - num1 > .6 then
  17.         num1 = math.floor(num1)
  18.     elseif num2 - num1 <= .5 then
  19.         num1 = math.ceil(num1) 
  20.     end
  21.  
  22.     return num1
  23. end
  24.  
  25. --##Add Chosen Text At Location
  26. local function label(xPos, yPos, labelText, textColor)
  27.  
  28.     mon.setBackgroundColor(backColor)
  29.     mon.setTextColor(textColor)
  30.     mon.setCursorPos(xPos, yPos)
  31.     mon.write(labelText)
  32.  
  33. end
  34.  
  35. --##Create A Button at the location with the chosen text
  36. local function button(xPos, yPos, bLen, bWid, labelText, textColor, buttonColor)
  37.  
  38.     paintutils.drawFilledBox(xPos, yPos, xPos + bLen - 1, yPos + bWid - 1, buttonColor)
  39.     mon.setTextColor(textColor)
  40.     mon.setBackgroundColor(buttonColor)
  41.     mon.setCursorPos(xPos + int(bLen / 2) - int(string.len(labelText) / 2), yPos + int(bWid / 2)-1)
  42.     mon.write(labelText)
  43.  
  44. end
  45.  
  46. --##Create A frame at the location with the chosen text
  47. local function drawFrame(xPos, yPos, xPos2, yPos2, labelText, textColor, frameColor)
  48.  
  49.     paintutils.drawBox(xPos, yPos, xPos2, yPos2, frameColor)
  50.     mon.setTextColor(textColor)
  51.     mon.setBackgroundColor(backColor)
  52.    
  53.     mon.setCursorPos(xPos + ((xPos2 - xPos) / 2) - int(string.len(labelText) / 2) + 1, yPos)
  54.     mon.write(labelText)
  55.  
  56. end
  57.  
  58.  
  59. --##Wipe background to chosen color
  60. local function clean(bColor)
  61.  
  62.     mon.setBackgroundColor(bColor)
  63.     mon.clear()
  64.  
  65. end
  66.  
  67. --##Convert number to engineering notation
  68. local function eng(rawNum)
  69.    
  70.     tempNum = tostring(rawNum)
  71.     numLen = string.len(tempNum)
  72.     if string.sub(tempNum,1,1) == "-" then
  73.         isNegative = true
  74.         tempNum2 = string.sub(tempNum, 2, numLen)
  75.         tempNum = tempNum2
  76.     else
  77.         isNegative = false
  78.     end
  79.  
  80.     if numLen == 4 then
  81.         tempNum = string.sub(tempNum, 1, 1) .. "," .. string.sub(tempNum, 2, 3) .. "K"
  82.     elseif numLen == 5 then
  83.         tempNum = string.sub(tempNum, 1, 2) .. "," .. string.sub(tempNum, 3, 4) .. "K"
  84.     elseif numLen == 6 then
  85.         tempNum = string.sub(tempNum, 1, 3) .. "," .. string.sub(tempNum, 4, 5) .. "K"
  86.        
  87.     elseif numLen == 7 then
  88.         tempNum = string.sub(tempNum, 1, 1) .. "," .. string.sub(tempNum, 2, 3) .. "M"
  89.     elseif numLen == 8 then
  90.         tempNum = string.sub(tempNum, 1, 2) .. "," .. string.sub(tempNum, 3, 4) .. "M"
  91.     elseif numLen == 9 then
  92.         tempNum = string.sub(tempNum, 1, 3) .. "," .. string.sub(tempNum, 4, 5) .. "M" 
  93.  
  94.     elseif numLen == 10 then
  95.         tempNum = string.sub(tempNum, 1, 1) .. "," .. string.sub(tempNum, 2, 3) .. "G"
  96.     elseif numLen == 11 then
  97.         tempNum = string.sub(tempNum, 1, 2) .. "," .. string.sub(tempNum, 3, 4) .. "G"
  98.     elseif numLen == 12 then
  99.         tempNum = string.sub(tempNum, 1, 3) .. "," .. string.sub(tempNum, 4, 5) .. "G" 
  100.  
  101.     elseif numLen == 13 then
  102.         tempNum = string.sub(tempNum, 1, 1) .. "," .. string.sub(tempNum, 2, 3) .. "T"
  103.     elseif numLen == 14 then
  104.         tempNum = string.sub(tempNum, 1, 2) .. "," .. string.sub(tempNum, 3, 4) .. "T"
  105.     elseif numLen == 15 then
  106.         tempNum = string.sub(tempNum, 1, 3) .. "," .. string.sub(tempNum, 4, 5) .. "T" 
  107.  
  108.     end
  109.    
  110.     if isNegative == true then
  111.         tempNum = "-" .. tempNum
  112.     end
  113.  
  114.     --tempNum = tonumber(tempNum)
  115.     return tempNum
  116.  
  117. end
  118.  
  119. --##Figures out the core tier
  120.  
  121. local function coreTier()
  122.    
  123.     if core.getMaxEnergyStored() == 45500000 then
  124.         return "1"
  125.     elseif core.getMaxEnergyStored() == 273000000 then
  126.         return "2"
  127.     elseif core.getMaxEnergyStored() == 1640000000 then
  128.         return "3"
  129.     elseif core.getMaxEnergyStored() == 9880000000 then
  130.         return "4"
  131.     elseif core.getMaxEnergyStored() == 59300000000 then
  132.         return "5"
  133.     elseif core.getMaxEnergyStored() == 356000000000 then
  134.         return "6"
  135.     elseif core.getMaxEnergyStored() == 2140000000000 then
  136.         return "7"
  137.     else
  138.         return "8"
  139.  
  140.     end
  141.  
  142. end
  143.  
  144.  
  145. --##Draws GUI
  146.  
  147. local function drawScreen()
  148.  
  149.     clean(backColor)
  150.  
  151.     drawFrame(2,2,36,11, "Energy Core", colors.white, colors.gray)
  152.     label(5,5,"Tier: ",colors.white)
  153.     label(5,7,"Stored: ",colors.white)
  154.     label(5,9,"Energy Flow: ",colors.white)
  155.     button(2,13,16,3, "Online", colors.white, colors.green)
  156.     button(21,13,16,3, "Offline", colors.white, colors.red)
  157.     button(2,21,16,3, "End Program", colors.white, colors.blue)
  158.  
  159.     label(11,5, coreTier(), colors.white)
  160.     label(13,7, eng(core.getEnergyStored()) .. "/" .. eng(core.getMaxEnergyStored()) .. " RF/t", colors.orange)
  161.  
  162.     if core.getTransferPerTick() ~= nil then
  163.  
  164.  
  165.     if core.getTransferPerTick() < 0 then
  166.         label(18,9, core.getTransferPerTick(), colors.red)
  167.     elseif core.getTransferPerTick() > 0 then
  168.         label(18,9, core.getTransferPerTick(), colors.green)
  169.     else
  170.         label(18,9, core.getTransferPerTick(), colors.white)
  171.     end
  172.     end
  173.  
  174.     drawFrame(38,2,78,15, "Reactor Status", colors.white, colors.gray)
  175.     label(41,5,"Status: ",colors.white)
  176.     label(41,7,"Generating: ",colors.white)
  177.     label(41,9,"Fuel Consumption: ",colors.white)
  178.     label(41,11,"Spent Fuel: ",colors.white)
  179.     label(41,13,"Control Rod Insertion: ",colors.white)
  180.  
  181.     if reactor.getActive() == true then
  182.         label(49,5,"Online",colors.green)
  183.     else
  184.         label(49,5,"Offline",colors.red)
  185.     end
  186.  
  187.     label(53,7,eng(int(reactor.getEnergyProducedLastTick())) .. " RF/t",colors.white)
  188.     label(59,9,math.floor(reactor.getFuelConsumedLastTick()*1000)/1000 .. " mB/t",colors.yellow)
  189.     label(53,11,math.floor(reactor.getWasteAmount()*1000) / 1000 .. " mB/t",colors.blue)
  190.     label(64,13,reactor.getControlRodLevel(1) .. "%",colors.white)
  191.  
  192.  
  193.     drawFrame(38,17,78,23, "Control Rods", colors.white, colors.gray)
  194.  
  195.     button(41,19,7,3, "+10", colors.white, colors.gray)
  196.     button(50,19,7,3, "+1", colors.white, colors.gray)
  197.     button(59,19,7,3, "-1", colors.white, colors.gray)
  198.     button(68,19,7,3, "-10", colors.white, colors.gray)
  199.  
  200.     sleep(1)
  201.  
  202. end
  203.  
  204. --##Exit Program
  205. local function shutDown()
  206.  
  207.     button(1,1,79,25, "Shutting Down.  Goodbye.", colors.white, colors.black)
  208.     sleep(3)
  209.     mon.clear()
  210. end
  211.  
  212.  
  213. --##Waits for touch screen input
  214.  
  215. local function touch()
  216.  
  217.     local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  218.  
  219.     if xPos >= 2 and xPos <= 18 and yPos >= 21 and yPos <= 23 then
  220.         exitProgram = 1
  221.     elseif xPos >= 2 and xPos <= 17 and yPos >= 13 and yPos <= 15 then
  222.         reactor.setActive(true)
  223.     elseif xPos >= 21 and xPos <= 36 and yPos >= 13 and yPos <= 15 then
  224.         reactor.setActive(false)
  225.     elseif xPos >= 41 and xPos <= 47 and yPos >= 19 and yPos <= 21 then
  226.        
  227.         if reactor.getControlRodLevel(1) + 10 <= 100 then
  228.             reactor.setAllControlRodLevels(reactor.getControlRodLevel(1) + 10)
  229.         else
  230.             reactor.setAllControlRodLevels(100)
  231.         end
  232.  
  233.     elseif xPos >= 68 and xPos <= 74 and yPos >= 19 and yPos <= 21 then
  234.        
  235.         if reactor.getControlRodLevel(1) - 10 >= 0 then
  236.             reactor.setAllControlRodLevels(reactor.getControlRodLevel(1) - 10)
  237.         else
  238.             reactor.setAllControlRodLevels(0)
  239.         end
  240.  
  241.     elseif xPos >= 50 and xPos <= 56 and yPos >= 19 and yPos <= 21 then
  242.        
  243.         if reactor.getControlRodLevel(1) + 1 <= 100 then
  244.             reactor.setAllControlRodLevels(reactor.getControlRodLevel(1) + 1)
  245.         else
  246.             reactor.setAllControlRodLevels(100)
  247.         end
  248.  
  249.     elseif xPos >= 59 and xPos <= 65 and yPos >= 19 and yPos <= 21 then
  250.        
  251.         if reactor.getControlRodLevel(1) - 1 >= 0 then
  252.             reactor.setAllControlRodLevels(reactor.getControlRodLevel(1) - 1)
  253.         else
  254.             reactor.setAllControlRodLevels(0)
  255.         end
  256.  
  257.     end
  258.     drawScreen()
  259. end
  260.  
  261. while true do
  262.    
  263.     parallel.waitForAny(touch, drawScreen)
  264.    
  265.     if exitProgram == 1 then
  266.         shutDown()
  267.         return
  268.     end
  269.  
  270. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement