Hiranus

Reactor + Turbine Manager

May 3rd, 2014
493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.20 KB | None | 0 0
  1.  
  2.  
  3. --this program can be used to manage Big Reactors
  4. --Reactors & Turbines
  5.  
  6. --important note
  7. --your reactor HAS to be set to manual waste ejection
  8.  
  9.  
  10.  
  11. local _interval = 1 -- in sec
  12. local reactor, turbine, monitor
  13.  
  14. local isReactor
  15. local isMonitor
  16. local isTurbine
  17. local _maxEnergy = 10000000
  18. local _maxEnergyTurbine = 1000000
  19. local _cyaniteMode = false
  20. local _cyaniteToCreate = 0
  21. local _loop = true
  22.     local turbinePowerToEnableReactor = 50 -- in % , at 0 reactor will run only if turbine has no power
  23.  
  24. function search(perToFind)
  25.     local printMode = false
  26.     if perToFind == "print" then
  27.         printMode=true
  28.     end
  29.     if printMode == true then
  30.         term.clear()
  31.         term.setCursorPos(1,1)
  32.     end
  33.     local sides = { "top", "bottom", "left", "right", "front", "back" }
  34.     for i = 1, #sides do
  35.         if peripheral.isPresent(sides[i]) then
  36.             if peripheral.getType(sides[i]) == "modem" then
  37.                 local modem = peripheral.wrap(sides[i])
  38.                 local methods = peripheral.getMethods(sides[i])
  39.                 local wired = false
  40.                 for j = 1, #methods do
  41.                     if methods[j] == "getNamesRemote" then
  42.                         wired=true
  43.                     end
  44.                 end
  45.                 if wired == true then
  46.                     if printMode == true then
  47.                         print("There is " ..peripheral.getType(sides[i]).." on the "..sides[i] )
  48.                     else
  49.                         if perToFind==peripheral.getType(sides[i]) then
  50.                             return sides[i]
  51.                         end
  52.                     ----------------------------------------------------------------
  53.                     end
  54.                     local net = modem.getNamesRemote()
  55.                     for j = 1, #net do
  56.                         if printMode == true then      
  57.                             print("   ".. peripheral.getType(net[j]))
  58.                         else
  59.                             if perToFind==peripheral.getType(net[j]) then
  60.                                 return net[j]
  61.                             end
  62.                         ---------------------------------
  63.                         end
  64.                     end
  65.                 else
  66.                     if printMode == true then
  67.                         print("There is wireless modem on the "..sides[i] )
  68.                     else
  69.                         if perToFind==peripheral.getType(sides[i]) then
  70.                             return sides[i]
  71.                         end
  72.                    
  73.                     ---------------------------------
  74.                     end
  75.                 end
  76.             else
  77.                 if printMode == true then
  78.                     print("There is " ..peripheral.getType(sides[i]).." on the "..sides[i] )
  79.                 else
  80.                     if perToFind==peripheral.getType(sides[i]) then
  81.                             return sides[i]
  82.                         end
  83.                 -----------------------
  84.                 end
  85.             end
  86.         else
  87.         if printMode == true then
  88.             print("There is nothing on the "..sides[i] )
  89.         end
  90.         end
  91.     end
  92.     return nil
  93. end
  94.  
  95.  
  96. function wrapper ()
  97.     local r1 = search("BigReactors-Reactor")
  98.    
  99.     if r1 ~= nil then
  100.         reactor = peripheral.wrap(r1)
  101.         if reactor.getConnected() == true then
  102.             isReactor = true
  103.         else
  104.             isReactor = false
  105.         end
  106.         isReactor = true
  107.     else
  108.         isReactor = false
  109.     end
  110.    
  111.     local t1 = search("BigReactors-Turbine")
  112.    
  113.     if t1 ~= nil then
  114.         turbine = peripheral.wrap(t1)
  115.         if turbine.getConnected() == true then 
  116.             isTurbine = true
  117.         else
  118.             isTurbine = false
  119.         end
  120.     else
  121.         isTurbine = false
  122.     end
  123.    
  124.     local m1 = search("monitor")
  125.    
  126.     if m1 ~= nil then
  127.         monitor = peripheral.wrap(m1)
  128.         isMonitor = true
  129.     else
  130.         isMonitor = false
  131.     end
  132.    
  133. end
  134.  
  135. function newLine(obj)
  136.   local _,cY= obj.getCursorPos()
  137.   obj.setCursorPos(1,cY+1)
  138. end
  139.  
  140. function clear(obj)
  141.     obj.clear()
  142.     obj.setCursorPos(1,1)
  143. end
  144.  
  145. function clearMarks(obj,number)
  146.     local x,y = obj.getCursorPos()
  147.     local obj_x,obj_y = obj.getSize()
  148.     while x+number > obj_x do
  149.         number=number-1
  150.         if number>= 0 then
  151.             return
  152.         end
  153.     end
  154.     for i=1,number do
  155.         obj.write(" ")
  156.     end
  157.     obj.setCursorPos(x,y)
  158. end
  159. local arr_x={}
  160. local arr_y={}
  161.  
  162. function prepare (obj)
  163.     clear(obj)
  164.     if isReactor then
  165.         obj.write("Reactor Status:")
  166.         newLine(obj)       
  167.         obj.write("Energy stored: ")
  168.         arr_x[1],arr_y[1] = obj.getCursorPos()
  169.         newLine(obj)
  170.         obj.write("Energy produced: ")
  171.         arr_x[2],arr_y[2] = obj.getCursorPos()
  172.         newLine(obj)
  173.         obj.write("Fuel temperature: ")
  174.         arr_x[3],arr_y[3] = obj.getCursorPos()
  175.         newLine(obj)
  176.         obj.write("Waste amount: ")
  177.         arr_x[4],arr_y[4] = obj.getCursorPos()
  178.         newLine(obj)
  179.         obj.write("Reactivity: ")
  180.         arr_x[5],arr_y[5] = obj.getCursorPos()
  181.         newLine(obj)
  182.         obj.write("Fuel burnup: ")
  183.         arr_x[6],arr_y[6] = obj.getCursorPos()
  184.     end
  185.     if isTurbine then
  186.         newLine(obj)
  187.         newLine(obj)
  188.         obj.write("Turbine Status:")
  189.         newLine(obj)
  190.         obj.write("Energy stored: ")
  191.         arr_x[7],arr_y[7] = obj.getCursorPos()
  192.         newLine(obj)
  193.         obj.write("Energy produced: ")
  194.         arr_x[8],arr_y[8] = obj.getCursorPos()
  195.         newLine(obj)
  196.         obj.write("Rotor speed: ")
  197.         arr_x[9],arr_y[9] = obj.getCursorPos()
  198.     end
  199.     newLine(obj)
  200.     newLine(obj)
  201. end
  202.  
  203. function show (obj)
  204.     local x,y = obj.getCursorPos()
  205.     if isReactor then      
  206.         obj.setCursorPos(arr_x[1],arr_y[1])
  207.         obj.write((math.floor(reactor.getEnergyStored()/_maxEnergy*1000)/10).." %")
  208.         clearMarks(obj,5)
  209.         obj.setCursorPos(arr_x[2],arr_y[2])
  210.         obj.write((math.floor(reactor.getEnergyProducedLastTick())).. " RF")
  211.         clearMarks(obj,5)
  212.         obj.setCursorPos(arr_x[3],arr_y[3])
  213.         obj.write((math.floor(reactor.getFuelTemperature())).. " C")
  214.         clearMarks(obj,5)
  215.         obj.setCursorPos(arr_x[4],arr_y[4])
  216.         obj.write(reactor.getWasteAmount().. " mB")
  217.         clearMarks(obj,5)
  218.         obj.setCursorPos(arr_x[5],arr_y[5])
  219.         obj.write((math.floor(reactor.getFuelReactivity())).. " %")
  220.         clearMarks(obj,5)
  221.         obj.setCursorPos(arr_x[6],arr_y[6])
  222.         obj.write((math.floor(reactor.getFuelConsumedLastTick()*1000)/1000).. " mB")
  223.         clearMarks(obj,5)
  224.     end
  225.     if isTurbine then
  226.         obj.setCursorPos(arr_x[7],arr_y[7])
  227.         obj.write((math.floor(turbine.getEnergyStored()/_maxEnergyTurbine*1000)/10).." %")
  228.         clearMarks(obj,5)
  229.         obj.setCursorPos(arr_x[8],arr_y[8])
  230.         obj.write((math.floor(turbine.getEnergyProducedLastTick())).. " RF")
  231.         clearMarks(obj,5)
  232.         obj.setCursorPos(arr_x[9],arr_y[9])
  233.         obj.write((math.ceil(turbine.getRotorSpeed()*10)/10).. " RPM")
  234.         clearMarks(obj,5)
  235.     end
  236.     obj.setCursorPos(x,y)
  237. end
  238.  
  239. function eject ()
  240.     local wasteAmount = reactor.getWasteAmount()
  241.     if wasteAmount > 1000 then
  242.         reactor.doEjectWaste()
  243.         return true
  244.     else
  245.         return false
  246.     end
  247. end
  248.  
  249. function cyanite ()
  250.     local ejected = eject()
  251.     if ejected == true then
  252.         _cyaniteToCreate = _cyaniteToCreate - 1
  253.         if _cyaniteToCreate <= 0 then
  254.             _cyaniteMode = false
  255.         end
  256.     end
  257. end
  258.  
  259. function updateReactor()
  260.  eject()
  261.   local rodControl = math.floor((reactor.getEnergyStored()/_maxEnergy)*100)
  262.   if isTurbine then
  263.     local turbineEnergy = math.floor(turbine.getEnergyStored()/_maxEnergy*100)
  264.     local turbineMultipler = 100 / turbinePowerToEnableReactor
  265.     local turbineControl = turbineEnergy * turbineMultipler
  266.     if rodControl < turbineControl then
  267.         if turbineControl > 100 then
  268.             rodControl = 100
  269.         else
  270.             rodControl = turbineControl
  271.         end
  272.     end
  273.   end
  274.   reactor.setAllControlRodLevels(rodControl)
  275. end
  276.  
  277. function psleep(n)
  278.     local tTimer = os.startTimer(n)
  279.     for sEvent, t in os.pullEventRaw do
  280.         if sEvent == "timer" and t == tTimer then
  281.             break
  282.         end
  283.     end
  284. end
  285.  
  286. function reactorManagement()
  287.     if _cyaniteMode == true then
  288.         cyanite()
  289.     else
  290.         updateReactor()
  291.     end
  292. end
  293.  
  294. function menu()
  295.     if isMonitor == true then
  296.         clear(term)
  297.     end
  298.     print("1. Cyanite production")
  299.     print("2. Normal reactor work")
  300.     print("3. Shutdown")
  301.     local x,y = term.getCursorPos()
  302.     while _loop do
  303.         local keyPressed = read()
  304.         if keyPressed == "1" then
  305.             term.write("How many: ")
  306.             _cyaniteToCreate = tonumber(read())
  307.             _cyaniteMode = true
  308.             reactor.setAllControlRodLevels(0)
  309.         elseif keyPressed == "2" then
  310.             _cyaniteToCreate = 0
  311.             _cyaniteMode = false
  312.         elseif keyPressed == "3" then
  313.             _loop = false
  314.             break
  315.         end
  316.         local x2,y2 = term.getCursorPos()
  317.         for i=y,y2 do
  318.             term.setCursorPos(1,i)
  319.             term.clearLine()
  320.         end
  321.         term.setCursorPos(x,y)
  322.     end
  323. end
  324.  
  325. function mainLoop()
  326.   if isReactor == true then
  327.    if reactor.getActive() == false then
  328.         reactor.setActive(true)
  329.    end
  330.   end
  331.     if isTurbine then
  332.         if turbine.getActive() == false then
  333.             turbine.setActive(true)
  334.         end
  335.     end
  336.     while _loop do
  337.         psleep(_interval)
  338.         if isReactor == true then
  339.             reactorManagement()
  340.         end
  341.         if isMonitor==true then
  342.             show(monitor)
  343.         else
  344.             show(term)
  345.         end
  346.     end
  347.     if isReactor == true then
  348.         reactor.setActive(false)
  349.     end
  350. end
  351.  
  352. wrapper()
  353. if isMonitor == true then
  354.     prepare(monitor)
  355. else
  356.     prepare(term)
  357. end
  358. parallel.waitForAll(mainLoop, menu)
Advertisement
Add Comment
Please, Sign In to add comment