eytixis

New Plant Project

Apr 29th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.03 KB | None | 0 0
  1. reactor = "BigReactors-Reactor_244"
  2. mon = "monitor_434"
  3. turbineBase = "BigReactors-Turbine_"
  4. turbine = {}
  5. turbineCountLevels = {}
  6. sleepDelay = 1 -- in seconds / floats allowed --
  7. coil_rpm = 1600
  8. turbineEnergyStoredMax = 950000
  9.  
  10. function readFromConfig()
  11.     s = fs.open("//config", "r")
  12.     data = s.readAll()
  13.     if data == nil then
  14.         return true
  15.     else
  16.         data = textutils.unserialise(data)
  17.         sleepDelay = data.sleepDelay
  18.         reactor.auto = data.reactorAuto
  19.         for i = 1, #turbine do
  20.             turbine[i].auto = data.turbineAuto[i]
  21.         end
  22.         reactor.setActive(data.reactorActive)
  23.         for i = 1, #turbine do
  24.             turbine[i].setActive(data.turbineActive[i])
  25.         end
  26.         for i = 1, #turbine do
  27.             turbine[i].coil_rpm = data.turbineCoilRpm[i]
  28.         end
  29.         turbineEnergyStoredMax = data.turbineEnergyStoredMax
  30.         reactor.setAllControlRodLevels(data.reactorControlRodLevel)
  31.     end
  32.     data = nil
  33. end
  34.  
  35. function saveToConfig()
  36.  
  37.     s = fs.open("//config", "w")
  38.     data = {}
  39.     data.sleepDelay = sleepDelay
  40.     data.turbineCoilRpm = {}
  41.     for i = 1, #turbine do
  42.         data.turbineCoilRpm[i] = turbine[i].coil_rpm
  43.     end
  44.     data.reactorActive = reactor.getActive()
  45.     data.turbineActive = {}
  46.     for i = 1, #turbine do
  47.         data.turbineActive[i] = turbine[i].getActive()
  48.     end
  49.     data.reactorAuto = reactor.auto
  50.     data.turbineAuto = {}
  51.     for i = 1,#turbine do
  52.         data.turbineAuto[i] = turbine[i].auto
  53.     end
  54.     data.turbineEnergyStoredMax = turbineEnergyStoredMax
  55.     data.reactorControlRodLevel = reactor.getControlRodLevel(math.random(reactor.getNumberOfControlRods()))
  56.    
  57.     data = textutils.serialise(data)
  58.     s.write(data)
  59.     s.close()
  60. end
  61.  
  62. function utilize()
  63.     for i = 1, 24 do
  64.         turbine[i] = i + 105
  65.     end
  66.     -- changes on names --
  67.     for i = 1 , #turbine do
  68.         turbine[i] = (turbineBase..tostring(turbine[i]))
  69.         turbine[i] = peripheral.wrap(turbine[i])
  70.     end
  71.     mon = peripheral.wrap(mon)
  72.     reactor = peripheral.wrap(reactor)
  73.     turbineCountLevels = {
  74.     [1]=96,[2]=93,[3]=89,[4]=86,[5]=83,
  75.     [6]=79,[7]=76,[8]=73,[9]=69,[10]=66,
  76.     [11]=63,[12]=59,[13]=56,[14]=52,[15]=49,
  77.     [16]=45,[17]=42,[18]=38,[19]=35,[20]=31,
  78.     [21]=27,[22]=23,[23]=20,[24]=16,[25]=11 }
  79.    
  80.     for i = 1, #turbine do
  81.         turbine[i].coil_rpm = coil_rpm
  82.     end
  83.    
  84.     for i = 1 , #turbine do
  85.         turbine[i].auto = false
  86.     end
  87.     reactor.auto = false
  88.    
  89.     if fs.exists("//config") then
  90.         readFromConfig()
  91.     end
  92. end
  93.  
  94. function monClear()
  95.     mon.setBackgroundColor(colors.black)
  96.     mon.clear()
  97.     mon.setCursorPos(1,1)
  98. end
  99.  
  100.  
  101.  
  102. function termClear()
  103.     term.clear()
  104.     term.setCursorPos(1,1)
  105. end
  106.  
  107. function getActiveTurbineCount()
  108.     turbineCount = 0
  109.     for i = 1, #turbine do
  110.         if( turbine[i].getActive() ) then
  111.             turbineCount = turbineCount + 1
  112.         end
  113.     end
  114.     return turbineCount
  115. end
  116.  
  117. function getAutoTurbineCount()
  118.     turbineCount = 0
  119.     for i = 1, #turbine do
  120.         if( turbine[i].auto ) then
  121.             turbineCount = turbineCount + 1
  122.         end
  123.     end
  124.     return turbineCount
  125. end
  126.  
  127. function getTurbineInductorEngagedCount()
  128.     turbineCount = 0
  129.     for i = 1, #turbine do
  130.         if( turbine[i].getInductorEngaged() ) then
  131.             turbineCount = turbineCount + 1
  132.         end
  133.     end
  134.     return turbineCount
  135. end
  136.  
  137. function getPlantProductionRate()
  138.     x = 0
  139.     for i = 1, #turbine do
  140.         x = x + turbine[i].getEnergyProducedLastTick()
  141.     end
  142.     return x
  143. end
  144.  
  145. function calcRods(turbineCount)
  146.     if turbineCount == 0 then
  147.         return 100
  148.     else
  149.         return turbineCountLevels[turbineCount]
  150.     end
  151. end
  152.  
  153. function controlTurbine(l)
  154.     if(l.auto) then
  155.         l.setInductorEngaged(l.getRotorSpeed() > l.coil_rpm)
  156.         l.setActive(l.getEnergyStored() < tonumber(turbineEnergyStoredMax))
  157.     end
  158. end
  159.    
  160. function controlTurbines()
  161.     for i = 1,#turbine do
  162.         controlTurbine(turbine[i])
  163.     end
  164.  
  165. end
  166.  
  167. function controlReactor()
  168.     if reactor.auto then
  169.         x = getActiveTurbineCount()
  170.         x = calcRods(x)
  171.         if x == 100 then
  172.             reactor.setActive(false)
  173.         else
  174.             reactor.setActive(true)
  175.             reactor.setAllControlRodLevels(x)
  176.         end
  177.     end
  178. end
  179.  -- here and down its not ready yet --
  180.  
  181. function createDisplay()
  182.     --monClear()
  183.     --for il = 1, #turbine do
  184.     --  displayTurbine(il)
  185.     --end
  186. end
  187.  
  188. function reactorControlInterface()
  189.  
  190. end
  191.  
  192. function turbineControlInterface(turb)
  193.     termClear()
  194.    
  195. end
  196.  
  197. function interface()
  198.    
  199.     termClear()
  200.     term.write("Enter Your Command:")
  201.     term.setCursorPos(1,2)
  202.     input = read()
  203.     if input == "reactor" then
  204.         termClear()
  205.         term.write("Active: ".. tostring(reactor.getActive()))
  206.         term.setCursorPos(1,2)
  207.         term.write("Auto: ".. tostring(reactor.auto))
  208.         term.setCursorPos(1,3)
  209.         term.write("ControlRods: ".. tostring(reactor.getControlRodLevel(math.random(reactor.getNumberOfControlRods()))))
  210.         term.setCursorPos(1,4)
  211.         term.write("What You Want To Change? ")
  212.         term.setCursorPos(1,5)
  213.         input = read()
  214.         if input == "active" then
  215.             reactor.setActive(not reactor.getActive())
  216.             return true
  217.         elseif input == "auto" then
  218.             reactor.auto = (not reactor.auto)
  219.             return true
  220.         elseif input == "control rods" then
  221.             termClear()
  222.             term.write("Current reactor control rods level is ".. tostring(reactor.getControlRodLevel(math.random(reactor.getNumberOfControlRods()))))
  223.             term.setCursorPos(1,2)
  224.             term.write("Set to : ")
  225.             input = read()
  226.             input = tonumber(input)
  227.             reactor.setAllControlRodLevels(input)
  228.             return true
  229.         else
  230.             termClear()
  231.             term.write("There was an error in your imports")
  232.             sleep(0.2)
  233.             os.pullEvent()
  234.             return true
  235.         end
  236.         -- end of input "reactor"
  237.     elseif input == "turbine" then
  238.         termClear()
  239.         term.write("turbine number: ")
  240.         input = read()
  241.         sd = tonumber(input)
  242.         termClear()
  243.         term.write("Turbine number ".. tostring(sd))
  244.         term.setCursorPos(1,2)
  245.         term.write("Active: ".. tostring(turbine[sd].getActive()))
  246.         term.setCursorPos(1,3)
  247.         term.write("Auto: ".. tostring(turbine[sd].auto))
  248.         term.setCursorPos(1,4)
  249.         term.write("Coils engaged: ".. tostring(turbine[sd].getInductorEngaged()))
  250.         term.setCursorPos(1,5)
  251.         term.write("What you want to change? ")
  252.         term.setCursorPos(1,6)
  253.         input = read()
  254.         if input == "active" then
  255.             turbine[sd].setActive(not turbine[sd].getActive())
  256.             return true
  257.         elseif input == "auto" then
  258.             turbine[sd].auto = not turbine[sd].auto
  259.             return true
  260.         elseif input == "coils engaged" then
  261.             turbine[sd].setInductorEngaged(not turbine[sd].getInductorEngaged())
  262.             return true
  263.         else
  264.             termClear()
  265.             term.write("There was an error to your imports")
  266.             sleep(0.2)
  267.             os.pullEvent()
  268.             return true
  269.         end
  270.     elseif input == "turbines" then
  271.         termClear()
  272.         term.write("Turbines")
  273.         term.setCursorPos(1,2)
  274.         term.write("Active: ".. tostring(getActiveTurbineCount()), "/24 Turbines")
  275.         term.setCursorPos(1,3)
  276.         term.write("Auto: ".. getAutoTurbineCount(), "/24 Turbines")
  277.         term.setCursorPos(1,4)
  278.         term.write("Indictors engaged: ".. tostring(getTurbineInductorEngagedCount()), "/24 Turbines")
  279.         term.setCursorPos(1,5)
  280.         term.write("What you want to change?")
  281.         term.setCursorPos(1,6)
  282.        
  283.         input = read()
  284.        
  285.         if input == "active" then
  286.             if getActiveTurbineCount() == 0 then
  287.                 for i = 1, #turbine do
  288.                     turbine[i].setActive(true)
  289.                 end
  290.                 return true
  291.             elseif getActiveTurbineCount() == 24 then
  292.                 for i = 1, #turbine do
  293.                     turbine[i].setActive(false)
  294.                 end
  295.                 return true
  296.             else
  297.                 termClear()
  298.                 term.write("enable or disable all turbines?")
  299.                 term.setCursorPos(1,2)
  300.                 input = read()
  301.                 if input == "enable" then
  302.                     for i = 1, #turbine do
  303.                         turbine[i].setActive(true)
  304.                     end
  305.                 elseif input == "disable" then
  306.                     for i = 1, #turbine do
  307.                         turbine[i].setActive(false)
  308.                     end
  309.                 else
  310.                     termClear()
  311.                     term.write("There was an error with your imports")
  312.                     sleep(0.2)
  313.                     os.pullEvent()
  314.                     return true
  315.                 end
  316.                 return true
  317.             end
  318.        
  319.    
  320.         elseif input == "auto" then
  321.             if getAutoTurbineCount() == 0 then
  322.                 for i = 1, #turbine do
  323.                     turbine[i].auto = true
  324.                 end
  325.                 return true
  326.             elseif getAutoTurbineCount() == 24 then
  327.                 for i = 1, #turbine do
  328.                     turbine[i].auto = false
  329.                 end
  330.                 return true
  331.             else
  332.                 termClear()
  333.                 term.write("enable or disable auto at all turbines?")
  334.                 term.setCursorPos(1,2)
  335.                 input = read()
  336.                 if input == "enable" then
  337.                     for i = 1, #turbine do
  338.                         turbine[i].auto = true
  339.                     end
  340.                 elseif input == "disable" then
  341.                     for i = 1, #turbine do
  342.                         turbine[i].auto = false
  343.                     end
  344.                 else
  345.                     termClear()
  346.                     term.write("There was an error with your imports")
  347.                     sleep(0.2)
  348.                     os.pullEvent()
  349.                     return true
  350.                 end
  351.                 return true
  352.             end
  353.            
  354.         elseif input == "inductors engaged" then
  355.             if getTurbineInductorEngagedCount() == 0 then
  356.                 for i = 1, #turbine do
  357.                     turbine[i].setInductorEngaged(true)
  358.                 end
  359.                 return true
  360.             elseif getTurbineInductorEngagedCount() == 24 then
  361.                 for i = 1, #turbine do
  362.                     turbine[i].setInductorEngaged(false)
  363.                 end
  364.                 return true
  365.             else
  366.                 termClear()
  367.                 term.write("engage or disengage inductors at all turbines?")
  368.                 term.setCursorPos(1,2)
  369.                 input = read()
  370.                 if input == "engage" then
  371.                     for i = 1, #turbine do
  372.                         turbine[i].setInductorEngaged(true)
  373.                     end
  374.                 elseif input == "disenage" then
  375.                     for i = 1, #turbine do
  376.                         turbine[i].setInductorEngaged(false)
  377.                     end
  378.                 else
  379.                     termClear()
  380.                     term.write("There was an error with your imports")
  381.                     os.pullEvent()
  382.                     return true
  383.                 end
  384.                 return true
  385.             end
  386.         end
  387.    
  388.     elseif input == "power down" then
  389.         saveToConfig()
  390.         for i = 1,#turbine do
  391.             turbine[i].auto = false
  392.             turbine[i].setInductorEngaged(false)
  393.             turbine[i].setActive(false)
  394.         end
  395.         reactor.setActive(false)
  396.         reactor.setAllControlRodLevels(100)
  397.         monClear()
  398.         termClear()
  399.         os.shutdown()
  400.    
  401.     elseif input == "auto on" then
  402.         for i = 1,#turbine do
  403.             turbine[i].auto = true
  404.         end
  405.         reactor.auto = true
  406.         return true
  407.        
  408.     elseif input == "auto off" then
  409.         for i = 1,#turbine do
  410.             turbine[i].auto = false
  411.         end
  412.         reactor.auto = false
  413.         return true
  414.    
  415.     end
  416. end
  417.  
  418. function listener()
  419.     while true do
  420.         interface()
  421.     end
  422. end
  423.  
  424. function loop()
  425.     while true do
  426.         controlReactor()
  427.         controlTurbines()
  428.         createDisplay()
  429.         sleep(SleepDelay)
  430.     end
  431. end
  432.  
  433. function displayTurbine(tNumber)
  434.     sy = 2
  435.     z = tNumber
  436.     if tNumber > 4 then
  437.         sy = 5
  438.         z = tNumber - 4
  439.         if tNumber > 8 then
  440.             sy = 8
  441.             z = tNumber - 8
  442.             if tNumber > 12 then
  443.                 sy = 11
  444.                 z = tNumber - 12
  445.                 if tNumber > 16 then
  446.                     sy = 14
  447.                     z = tNumber - 16
  448.                     if tNumber > 20 then
  449.                         sy = 17
  450.                         z = tNumber - 20
  451.                         if tNumber > 24 then
  452.                             sy = 20
  453.                             z = tNumber - 24
  454.                         end
  455.                     end
  456.                 end
  457.             end
  458.         end
  459.     end
  460.     z = z - 1
  461.     sx = 2 + z * 3
  462.     mon.setCursorPos(sx,sy)
  463.     mon.setBackgroundColor(colors.red)
  464.     if turbine[tNumber].getActive() then
  465.         mon.setBackgroundColor(colors.lime)
  466.     end
  467.     mon.write(" ")
  468.     mon.setBackgroundColor(colors.brown)
  469.     if turbine[tNumber].getInductorEngaged() then
  470.         mon.setBackgroundColor(colors.pink)
  471.     end
  472.     mon.write(" ")
  473.     mon.setCursorPos(sx,sy + 1)
  474.     mon.setBackgroundColor(colors.red)
  475.     if turbine[tNumber].auto then
  476.         mon.setBackgroundColor(colors.lightBlue)
  477.     end
  478.     mon.write("  ")
  479.    
  480.    
  481.  
  482. end
  483.  
  484.  
  485. utilize()
  486. parallel.waitForAll(loop, listener)
Add Comment
Please, Sign In to add comment