scatmanjohn

Minecraft reactor monitor prog

Jan 8th, 2014
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2.     Program name: EZ-NUKE reactor control system
  3.     Version: v0001 Beta
  4.     Programmer: ScatmanJohn
  5.     Last update: 10.1.2014
  6.     Pastebin: http://pastebin.com/A7iGzTCH
  7.     Description:
  8.     This program controls a Big Reactors nuclear reactor
  9.     in Minecraft with a Computercraft computer, using Computercraft's
  10.     own wired modem connected to the reactors computer control port.
  11.     Resources:
  12.     Reactor control:
  13.         http://pastebin.com/HjUVNDau
  14.     FC API:
  15.         http://pastebin.com/A9hcbZWe
  16.     Stargate control startup:
  17.         http://pastebin.com/L4yhWweE
  18.     My gate:
  19.         FUEHMVD
  20.     Enderchests:
  21.         Red-Blue-Red, for leaf blocks and herba jars
  22.         Black-Blue-Black, feeds directly into AE
  23.        
  24.     Monitor size is X: 29, Y: 12 with a 3x2 size
  25. ]]--
  26.  
  27. print("Initializing program...");
  28.  
  29. if not os.loadAPI("FC_API") then
  30.     error("Missing FC_API")
  31. end
  32. --Done loading API
  33.  
  34. function wrapThis(thing)
  35.         local wrapped, f = nil, 0
  36.         while wrapped == nil and f <= 100 do
  37.                 wrapped = peripheral.wrap(thing.."_"..f)
  38.                 f = f + 1
  39.         end
  40.  
  41.         if wrapped == nil then
  42.                 side = getDeviceSide(thing)
  43.                 if side ~= nil then
  44.                         return peripheral.wrap(side)
  45.                 else
  46.                         return nil
  47.                 end
  48.         else
  49.                 return wrapped
  50.         end
  51. end
  52.  
  53. local function print(str, x, y)
  54.     term.setCursorPos(x, y)
  55.     term.write(str)
  56. end
  57. -- Done helper functions
  58.  
  59. -- Then initialize the monitor
  60. local monitor = wrapThis("monitor")
  61. local monitorx, monitory = monitor.getSize()
  62. if monitorx ~= 29 and monitory ~= 12 then
  63.     error("Monitor is the wrong size! Needs to be 3x2.")
  64. end
  65.  
  66. if  monitor then
  67.     --error("No Monitor Attached")
  68.     term.clear()
  69.     term.setCursorPos(1,1)
  70.     term.write("Display redirected to Monitor")
  71.     term.redirect(monitor)
  72. end
  73.  
  74. -- Let's connect to the reactor peripheral
  75. local reactor = wrapThis("BigReactors-Reactor")
  76. if reactor == nil then
  77.     error("Can't find reactor.")
  78. end
  79.  
  80. local xClick, yClick = 0,0
  81. local controlRodLevel = 0
  82. local loopTime = 0.5
  83. local adjustamount = 5
  84. local basecontrolrodlevel = 40
  85. local dataLogging = false
  86.  
  87. reactor.setAllControlRodLevels(basecontrolrodlevel)
  88.  
  89. FC_API.clearMonitor("EZ-NUKE Control")
  90.  
  91. --Done initializing
  92.  
  93. local function displayBars()
  94.    
  95.     -- Draw some cool lines
  96.     term.setBackgroundColor(colors.black)
  97.     local width, height = term.getSize()
  98.  
  99.     for i=3, 5 do
  100.         term.setCursorPos(22, i)
  101.         term.write("|")
  102.     end
  103.    
  104.     for i=1, width do
  105.         term.setCursorPos(i, 2)
  106.         term.write("-")
  107.     end
  108.    
  109.     for i=1, width do
  110.         term.setCursorPos(i, 6)
  111.         term.write("-")
  112.     end
  113.    
  114.     -- Draw some text
  115.    
  116.     fuelstring = "Fuel: "
  117.     tempstring = "Temp: "
  118.     energystring = "Producing: "
  119.    
  120.     local padding = math.max(string.len(fuelstring), string.len(tempstring),string.len(energystring))
  121.    
  122.     fuelpercentage = math.ceil(reactor.getFuelAmount()/reactor.getFuelAmountMax()*100)
  123.     print(fuelstring,2,3)
  124.     print(fuelpercentage.." %",padding+2,3)
  125.    
  126.     energy = reactor.getEnergyProducedLastTick()
  127.     print(energystring,2,4)
  128.     print(math.ceil(energy).."RF/t",padding+2,4)
  129.    
  130.     reactortemp = reactor.getTemperature()
  131.     print(tempstring,2,5)
  132.     print(reactortemp.." C",padding+2,5)
  133.    
  134.     -- Decrease rod button: 22X, 4Y
  135.     -- Increase rod button: 28X, 4Y
  136.    
  137.     local rodamount = reactor.getNumberOfControlRods() - 1
  138.     local rodtotal = 0
  139.     for i=0, rodamount do
  140.         rodtotal = rodtotal + reactor.getControlRodLevel(i)
  141.     end
  142.     rodpercentage = math.ceil(rodtotal/(rodamount+1))
  143.    
  144.     print("Control",23,3)
  145.     print("<     >",23,4)
  146.     print(rodpercentage,25,4)
  147.     print("percent",23,5)
  148.    
  149.     if (xClick == 22  and yClick == 4) then
  150.         --Decrease rod level by amount
  151.         newrodpercentage = rodpercentage - adjustamount
  152.         if newrodpercentage < 0 then
  153.             newrodpercentage = 0
  154.         end
  155.         xClick, yClick = 0,0
  156.         reactor.setAllControlRodLevels(newrodpercentage)
  157.     end
  158.    
  159.     if (xClick == 28  and yClick == 4) then
  160.         --Increase rod level by amount
  161.         newrodpercentage = rodpercentage + adjustamount
  162.         if newrodpercentage > 100 then
  163.             newrodpercentage = 100
  164.         end
  165.         xClick, yClick = 0,0
  166.         reactor.setAllControlRodLevels(newrodpercentage)
  167.     end
  168.    
  169.     local energystorage = reactor.getEnergyStored()
  170.     storagepercent = math.floor(energystorage/10000000*100)
  171.     paintutils.drawLine(2, 8, 28, 8, colors.gray)
  172.     if storagepercent > 0 then
  173.         paintutils.drawLine(2, 8, math.floor(27*storagepercent/100)+2, 8, colors.yellow)
  174.     end
  175.     term.setBackgroundColor(colors.black)
  176.     print("Energy",2,7)
  177.     print(storagepercent, width-(string.len(storagepercent)+3),7)
  178.     print("%",28,7)
  179.     term.setBackgroundColor(colors.black)
  180.    
  181. end
  182.  
  183.  
  184. function reactorStatus()
  185.     local width, height = term.getSize()
  186.     local str = ""
  187.     if reactor.getConnected() then
  188.         if reactor.getActive() then
  189.             str = "ONLINE"
  190.             term.setTextColor(colors.green)
  191.         else
  192.             if autoStart then
  193.                 reactor.setActive(true)
  194.             end
  195.             str = "OFFLINE"
  196.             term.setTextColor(colors.red)
  197.         end
  198.        
  199.         if(xClick >= (width - string.len(str) - 1) and xClick <= (width-1)) then
  200.             if yClick == 1 and not autoStart then
  201.                 reactor.setActive(not reactor.getActive())
  202.                 xClick, yClick = 0,0
  203.             end
  204.         end
  205.        
  206.     else
  207.         str = "DISCONNECTED"
  208.         term.setTextColor(colors.red)
  209.     end
  210.    
  211.     print(str, width - string.len(str) - 1, 1)
  212.     term.setTextColor(colors.white)
  213. end
  214.  
  215.  
  216. function main()
  217.     while not finished do
  218.         FC_API.clearMonitor("EZ-NUKE control")
  219.         reactorStatus()
  220.         if reactor.getConnected() then
  221.             displayBars()
  222.             sleep(loopTime)
  223.         end
  224.     end
  225. end
  226.  
  227. function eventHandler()
  228.     while not finished do
  229.         event, arg1, arg2, arg3 = os.pullEvent()
  230.        
  231.         if event == "monitor_touch" then
  232.             xClick, yClick = math.floor(arg2), math.floor(arg3)
  233.             -- Draw debug stuff
  234.             --print("Monitor touch X: "..xClick.." Y: "..yClick, 1, 10)
  235.         elseif event == "mouse_click" and not monitor then
  236.             xClick, yClick = math.floor(arg2), math.floor(arg3)
  237.             --print("Mouse click X: "..xClick.." Y: "..yClick, 1, 11)
  238.         elseif event == "char" and not inManualMode then
  239.             local ch = string.lower(arg1)
  240.             if ch == "q" then
  241.                 finished = true
  242.             elseif ch == "r" then
  243.                 finished = true
  244.                 os.reboot()
  245.             end
  246.         end
  247.     end
  248. end
  249.  
  250. while not finished do
  251.     parallel.waitForAny(eventHandler, main)
  252.     sleep(loopTime)
  253. end
  254.  
  255. term.clear()
  256. term.setCursorPos(1,1)
  257. FC_API.restoreNativeTerminal()
  258. term.clear()
  259. term.setCursorPos(1,1)
  260.  
  261. --[[ OLD CODE USED FOR TESTING
  262.  
  263. print("Program loaded. Scanning for reactor...");
  264. local reactor
  265. reactor = peripheral.wrap("back")
  266. isactive = reactor.getActive();
  267. if isactive == true
  268.     print("Reactor active.");
  269.     monitor.print("All's well!");
  270. else
  271.     print("Reactor inactive.");
  272. end
  273.  
  274. ]]--
Add Comment
Please, Sign In to add comment