Guest User

trb

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