Guest User

trb2

a guest
Jul 9th, 2017
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.89 KB | None | 0 0
  1. --t.buttonList.NameOfButton.active
  2. os.loadAPI("touchpoint")
  3. tM = touchpoint.new("back")
  4. tS = touchpoint.new("back")
  5. tA = touchpoint.new("back")
  6. m = peripheral.wrap("back")
  7. --Pages
  8. local t
  9. turbine = 0 --First turbine to see
  10.  
  11. RF = {}
  12. RPM = {}
  13. trb = {}
  14. turbineUhOh = {}
  15. turbineOk = {}
  16.  
  17. trb[0] = peripheral.wrap("BigReactors-Turbine_0")
  18. trb[1] = peripheral.wrap("BigReactors-Turbine_1")
  19. trb[2] = peripheral.wrap("BigReactors-Turbine_2")
  20. trb[3] = peripheral.wrap("BigReactors-Turbine_3")
  21. trb[4] = peripheral.wrap("BigReactors-Turbine_4")
  22. trb[5] = peripheral.wrap("BigReactors-Turbine_5")
  23. reactor = peripheral.wrap("BigReactors-Reactor_2")
  24.  
  25. function mainPage()
  26.   t = tA
  27.   mode = Auto
  28. end
  29.  
  30. function states()
  31.   if turbine ~= all then
  32.     coilState = trb[turbine].getInductorEngaged()
  33.     turbineState = trb[turbine].getActive()
  34.     if coilState == true then
  35.       coilState = "ON"
  36.     else
  37.       coilState = "OFF"
  38.     end
  39.     if turbineState == true then
  40.       turbineState = "ON"
  41.     else
  42.       turbineState = "OFF"
  43.     end
  44.   end
  45.   reactorState = reactor.getActive()
  46.   if reactorState == true then
  47.     reactorState = "ON"
  48.   else
  49.     reactorState = "OFF"
  50.   end  
  51. end
  52.  
  53. function auto()
  54.   if turbine == all then
  55.     turbine = 0
  56.   end
  57.   while true do
  58.     if reactor.getFuelAmount < 158000 then --makes sure reactor is off if it doesnt have yellorite
  59.       reactor.setActive(false)
  60.       reactorOK = false
  61.          reactorFuel = "Not OK"
  62.        else
  63.          reactorOK = true
  64.          reactorFuel = "OK"
  65.        end
  66.        if mode == auto and status == on and reactorOK == true then--automatic and on
  67.          for u=0,5 do --turbines on
  68.            if RPM[u] < 1782 then --Actions below 1782 RPM
  69.              trb[u].setActive(true)
  70.              trb[u].setInductorEngaged(false)
  71.            end
  72.            if RPM[u] > 1783 and RPM[u] < 1784 then --Actions between 1783 RPM and 1784 RPM
  73.                 trb[u].setInductorEngaged(true)
  74.                 trb[u].setActive(true)
  75.               end
  76.               if RPM[u] > 1785 then --Actions above 1785 RPM
  77.                 trb[u].setInductorEngaged(false)
  78.                 trb[u].setActive = false
  79.               end
  80.          end
  81.          reactor.setActive(true) --reactor on
  82.     elseif mode == auto and status == off or reactorOK == false then --automatic and off
  83.          for i=0,5 do --turbines off
  84.            trb[i].setActive(false)
  85.          end
  86.          reactor.setActive(false) --reactor off
  87.        elseif mode == auto and status == steam and reactorOK == true then --automatic and get steam
  88.          for i=0,5 do --turbines off
  89.            trb[i].setActive(false)
  90.          end
  91.          reactor.setActive(true) --reactor on
  92.     elseif mode == manual then --manual
  93.          break--Breaks loop to enter manual
  94.        end
  95.   end
  96. end
  97.  
  98. function test() --Check button funcionality
  99.   print("test")
  100. end
  101.  
  102. function changePage(changeTo) -- Change Page
  103.   if t == tA or t == tM then
  104.     oldT = t --Makes sure to change back to correct page from overall
  105.        t = changeTo
  106.     if changeTo == tA then
  107.       mode = Auto
  108.     elseif changeTo == tM then
  109.          mode = Manual
  110.     end
  111.   else
  112.        t = oldT
  113.   end
  114. end
  115.  
  116. function getINFO() --Gets RF and RPM, rounds RPM
  117.   allRF = 0
  118.   for o=0, 5 do
  119.     RPM[o] = trb[o].getRotorSpeed() --Gets RPM
  120.        RF[o] = trb[o].getEnergyProducedLastTick() --Gets RF
  121.     if RF[o] == nil then --Prevents errors
  122.          RF[o] = "0"
  123.        end
  124.        if RPM[o] == nil then --Prevents errors
  125.          RPM[o] = "0"
  126.     end
  127.     RPM[o] = math.floor(RPM[o]*10+0.5)*0.1 --Rounds RPM to 1 number after comma
  128.     RF[o] = math.floor(RF[o]+0.5) --Rounds RF/t
  129.        allRF = allRF + RF[o]
  130.   end
  131. end
  132.  
  133. function turbineThings()
  134.   okCount = 0
  135.   uhohCount = 0
  136.   for i=0,5 do
  137.     turbineUhOh[i] = nil
  138.        turbineOk[i] = nil
  139.   end
  140.   for i=0,5 do
  141.     if trb[i].getRPM ~= 1782 then
  142.          turbineUhOh[uhohCount] = i
  143.          uhohCount = uhohCount + 1
  144.        else
  145.          turbineOk[okCount] = i
  146.          okCount = okCount + 1
  147.        end
  148.   end
  149. end
  150.  
  151. function information() --Prints things
  152.   getINFO() --Gets info
  153.   states() --Gets states
  154.   if t == tA or t == tM then --Main screen, auto or manual mode
  155.     m.setCursorPos(1,1)
  156.     m.setBackgroundColor(colors.gray) --Sets text background to gray
  157.     if turbine ~= all then
  158.          m.write("Turbine "..turbine+1) --Writes what turbine is selected
  159.          m.setCursorPos(1,2)
  160.       m.write("RPM: "..RPM[turbine]) --Writes turbine's RPM
  161.          m.setCursorPos(1,3)
  162.          m.write("Coils: "..coilState)
  163.          m.setCursorPos(1,4)
  164.          m.write("State: "..turbineState)
  165.          m.setCursorPos(1,7)
  166.          m.write("Reactor: "..reactorState)
  167.          m.setCursorPos(1,8)
  168.          m.write("Reactor Fuel: "..reactorFuel)
  169.     elseif turbine == all then
  170.          turbineThings()
  171.          m.write("OK:")
  172.          repeat
  173.            m.write(" #"..turbineOk[okCount])
  174.             okCount = okCount - 1
  175.          until okCount == -1
  176.          m.setCursorPos(1,2)
  177.          m.write("Problems:")
  178.          repeat
  179.            m.write(" #"..turbineUhOh[uhohCount])
  180.             uhohCount = uhohCount - 1
  181.          until uhohCount == -1
  182.          m.setCursorPos(1,4)
  183.          m.write("Reactor: "..reactorState)
  184.       m.setCursorPos(1,5)
  185.          m.write("Total: "..allRF.." RF/t")
  186.     end
  187.   elseif t == tS then --Overall screen
  188.     mY = 1
  189.        for p=0, 5 do
  190.       m.setCursorPos(1,mY) --Sets where to write
  191.          m.setBackgroundColor(colors.gray) --Sets text background to gray
  192.          m.write("Turbine "..p+1..": "..RPM[p].." RPM "..RF[p].." RF/t") --Turbine's stats
  193.          mY = mY + 1 --Makes the next write next line
  194.     end
  195.   end
  196. end
  197.  
  198. function turbinePower()
  199.   if turbine ~= all then
  200.     trb[turbine].setActive(not trb[turbine].getActive())
  201.   else
  202.     for i=0,5 do
  203.          trb[i].setActive(not trb[i].getActive())
  204.        end
  205.   end
  206. end
  207.  
  208. function coilsPower()
  209.   if turbine ~= all then
  210.     trb[turbine].setInductorEngaged(not trb[turbine].getInductorEngaged())
  211.   else
  212.     for i=0,5 do
  213.          trb[i].setInductorEngaged(not trb[i].getInductorEngaged())
  214.        end
  215.   end
  216. end
  217.  
  218. function reactorPower()
  219.   reactor.setActive(not reactor.getActive())
  220. end
  221.  
  222. function onoff()
  223.   t:toggleButton("On")
  224.   t:toggleButton("Off")
  225. end
  226.  
  227. function automanual()
  228.   t:toggleButton("Auto")
  229.   t:toggleButton("Manual")
  230. end
  231.  
  232. function buttonToggles()
  233.   t:toggleButton("Auto")
  234. end
  235.  
  236. function buttonToggles2()
  237.   if trb[0].getActive == true and t.buttonList."Turbine 1".inactive then
  238.     t:toggleButton("Turbine 1")
  239.   end
  240.   if trb[1].getActive == true and t.buttonList."Turbine 2".inactive then
  241.     t:toggleButton("Turbine 2")
  242.   end
  243.   if trb[2].getActive == true and t.buttonList."Turbine 3".inactive then
  244.     t:toggleButton("Turbine 3")
  245.   end
  246.   if trb[3].getActive == true and t.buttonList."Turbine 4".inactive then
  247.     t:toggleButton("Turbine 4")
  248.   end
  249.   if trb[4].getActive == true and t.buttonList."Turbine 5".inactive then
  250.     t:toggleButton("Turbine 5")
  251.   end
  252.   if trb[5].getActive == true and t.buttonList."Turbine 6".inactive then
  253.     t:toggleButton("Turbine 6")
  254.   end
  255.   if trb[0].getActive == false and t.buttonList."Turbine 1".active then
  256.     t:toggleButton("Turbine 1")
  257.   end
  258.   if trb[1].getActive == false and t.buttonList."Turbine 2".active then
  259.     t:toggleButton("Turbine 2")
  260.   end
  261.   if trb[2].getActive == false and t.buttonList."Turbine 3".active then
  262.     t:toggleButton("Turbine 3")
  263.   end
  264.   if trb[3].getActive == false and t.buttonList."Turbine 4".active then
  265.     t:toggleButton("Turbine 4")
  266.   end
  267.   if trb[4].getActive == false and t.buttonList."Turbine 5".active then
  268.     t:toggleButton("Turbine 5")
  269.   end
  270.   if trb[5].getActive == false and t.buttonList."Turbine 6".active then
  271.     t:toggleButton("Turbine 6")
  272.   end
  273. end
  274.  
  275. --Things happen from here
  276. do --Buttons
  277.   tA:add("All Turbines", function() turbine = all end, 52, 2, 65, 4, colors.yellow, colors.yellow)
  278.   tA:add("Turbine 1", function() turbine = 0 end, 47, 6, 57, 8)
  279.   tA:add("Turbine 2", function() turbine = 1 end, 59, 6, 69, 8)
  280.   tA:add("Turbine 3", function() turbine = 2 end, 47, 10, 57, 12)
  281.   tA:add("Turbine 4", function() turbine = 3 end, 59, 10, 69, 12)
  282.   tA:add("Turbine 5", function() turbine = 4 end, 47, 14, 57, 16)
  283.   tA:add("Turbine 6", function() turbine = 5 end, 59, 14, 69, 16)
  284.   tA:add("Overall", function() t:flash("Overall") changePage(tS) end, 59, 23, 69, 25)
  285.   tA:add("Manual", function() automanual() changePage(tA) end, 59, 18, 69, 20)
  286.   tA:add("Auto", function() automanual() changePage(tM) end, 47, 18, 57, 20)
  287.   --tA:add("On", function() onoff() status = on end,
  288.   --tA:add("Off", function() onoff() status = off end,
  289.   --tA:add("Steam", function() status = steam end,
  290.  
  291.   tM:add("All Turbines", function() turbine = all  end, 52, 2, 65, 4, colors.yellow, colors.yellow)
  292.   tM:add("Turbine 1", function() turbine = 0 () end, 47, 6, 57, 8)
  293.   tM:add("Turbine 2", function() turbine = 1 end, 59, 6, 69, 8)
  294.   tM:add("Turbine 3", function() turbine = 2 end, 47, 10, 57, 12)
  295.   tM:add("Turbine 4", function() turbine = 3 end, 59, 10, 69, 12)
  296.   tM:add("Turbine 5", function() turbine = 4 end, 47, 14, 57, 16)
  297.   tM:add("Turbine 6", function() turbine = 5 end, 59, 14, 69, 16)
  298.   tM:add("Overall", function() t:flash("Overall") changePage(tS) end, 59, 23, 69, 25)
  299.   tM:add("Manual", function() changePage(tM) end, 59, 18, 69, 20)
  300.   tM:add("Auto", function() changePage(tA) end, 47, 18, 57, 20)
  301.   --tM:add("Power", function() turbinePower() end,
  302.   --tM:add("Coils", function() coilsPower() end,
  303.   --tM:add("Reactor", function() reactorPower() end,
  304.  
  305.   tS:add("Main", function() t:flash("Main") changePage(oldT) end, 59, 23, 69, 25)
  306.  
  307. end
  308.  
  309. mainPage() --set mode to auto on start
  310. t:draw() --to be able to do stuff with buttons outside loop
  311. buttonToggles() --toggle needed buttons
  312. while true do
  313.   m.clear() --Gets rid of old stuff
  314.   t:draw()--draw buttons
  315.   information()--RPMs, RF/t, etc.
  316.   buttonToggles2()
  317.   refreshTimer = os.startTimer(3) --Restart loop
  318.   local event, p1 = t:handleEvents(os.pullEvent())
  319.   if event == "button_click" then
  320.     t:run(p1)
  321.   elseif event == "timer" and p1 == refreshTimer then
  322.     --restart loop
  323.   end
  324. end
Advertisement
Add Comment
Please, Sign In to add comment