scatmanjohn

Minecraft reactor monitor prog

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