Guest User

trb

a guest
Jul 11th, 2017
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.29 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(false)
  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.     if trb[i].getRPM ~= 1782 then
  149.          uhohCount = uhohCount + 1
  150.       turbineUhOh[uhohCount] = i
  151.          --uhohCount = uhohCount + 1
  152.        else
  153.          okCount = okCount + 1
  154.       turbineOk[okCount] = i
  155.          --okCount = okCount + 1
  156.        end
  157.   end
  158.   --uhohCount = uhohCount-1
  159.   --okCount = okCount-1
  160. end
  161.  
  162. function information() --Prints things
  163.   getINFO() --Gets info
  164.   states() --Gets states
  165.   if t == tA or t == tM then --Main screen, auto or manual mode
  166.     m.setCursorPos(1,1)
  167.     m.setBackgroundColor(colors.gray) --Sets text background to gray
  168.     if turbine ~= all then
  169.          m.write("Turbine "..turbine+1) --Writes what turbine is selected
  170.          m.setCursorPos(1,2)
  171.       m.write("RPM: "..RPM[turbine]) --Writes turbine's RPM
  172.          m.setCursorPos(1,3)
  173.          m.write("Coils: "..coilState)
  174.          m.setCursorPos(1,4)
  175.          m.write("State: "..turbineState)
  176.          m.setCursorPos(1,7)
  177.          m.write("Reactor: "..reactorState)
  178.          m.setCursorPos(1,8)
  179.          m.write("Reactor Fuel: "..reactorFuel)
  180.     elseif turbine == all then
  181.          turbineThings()
  182.          m.write("OK:")
  183.          repeat
  184.            m.write(" #"..turbineOk[okCount])
  185.             okCount = okCount - 1
  186.          until okCount == 0
  187.          m.setCursorPos(1,2)
  188.          m.write("Problems:")
  189.          repeat
  190.            m.write(" #"..turbineUhOh[uhohCount])
  191.             uhohCount = uhohCount - 1
  192.          until uhohCount == 0
  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()
  234.  
  235.   t:toggleButton("On")
  236.   t:toggleButton("Off")
  237.   t:toggleButton("Steam")
  238. end
  239.  
  240. function buttonToggles()
  241.   t:toggleButton("Auto")
  242.   t:toggleButton("On")
  243. end
  244.  
  245. function buttonToggles2()
  246.   if trb[0].getActive == true and not t.buttonList["Turbine 1"].active then
  247.     t:toggleButton("Turbine 1")    
  248.   end
  249.   if trb[1].getActive == true and not t.buttonList["Turbine 2"].active then
  250.     t:toggleButton("Turbine 2")
  251.   end
  252.   if trb[2].getActive == true and not t.buttonList["Turbine 3"].active then
  253.     t:toggleButton("Turbine 3")
  254.   end
  255.   if trb[3].getActive == true and not t.buttonList["Turbine 4"].active then
  256.     t:toggleButton("Turbine 4")
  257.   end
  258.   if trb[4].getActive == true and not t.buttonList["Turbine 5"].active then
  259.     t:toggleButton("Turbine 5")
  260.   end
  261.   if trb[5].getActive == true and not t.buttonList["Turbine 6"].active then
  262.     t:toggleButton("Turbine 6")
  263.   end
  264.   if trb[0].getActive == false and t.buttonList["Turbine 1"].active then
  265.     t:toggleButton("Turbine 1")
  266.   end
  267.   if trb[1].getActive == false and t.buttonList["Turbine 2"].active then
  268.     t:toggleButton("Turbine 2")
  269.   end
  270.   if trb[2].getActive == false and t.buttonList["Turbine 3"].active then
  271.     t:toggleButton("Turbine 3")
  272.   end
  273.   if trb[3].getActive == false and t.buttonList["Turbine 4"].active then
  274.     t:toggleButton("Turbine 4")
  275.   end
  276.   if trb[4].getActive == false and t.buttonList["Turbine 5"].active then
  277.     t:toggleButton("Turbine 5")
  278.   end
  279.   if trb[5].getActive == false and t.buttonList["Turbine 6"].active then
  280.     t:toggleButton("Turbine 6")
  281.   end
  282. end
  283.  
  284. --Things happen from here
  285. m.setTextScale(1)
  286. do --Buttons
  287.   tA:add("All Turbines", function() turbine = all end, 52, 2, 65, 4, colors.yellow, colors.yellow)
  288.   tA:add("Turbine 1", function() turbine = 0 end, 47, 6, 57, 8)
  289.   tA:add("Turbine 2", function() turbine = 1 end, 59, 6, 69, 8)
  290.   tA:add("Turbine 3", function() turbine = 2 end, 47, 10, 57, 12)
  291.   tA:add("Turbine 4", function() turbine = 3 end, 59, 10, 69, 12)
  292.   tA:add("Turbine 5", function() turbine = 4 end, 47, 14, 57, 16)
  293.   tA:add("Turbine 6", function() turbine = 5 end, 59, 14, 69, 16)
  294.   tA:add("Overall", function() t:flash("Overall") changePage(tS) end, 59, 23, 69, 25)
  295.   tA:add("Manual", function() changePage(tM) end, 59, 18, 69, 20)
  296.   tA:add("Auto", function() end, 47, 18, 57, 20)
  297.   tA:add("On", function() onoff(on) status = on end, 2, 22, 12, 24)
  298.   tA:add("Off", function() onoff(off) status = off end, 14, 22, 24, 24)
  299.   tA:add("Steam", function() onoff(steam) status = steam end, 26, 22, 36, 24)
  300.  
  301.   tM:add("All Turbines", function() turbine = all  end, 52, 2, 65, 4, colors.yellow, colors.yellow)
  302.   tM:add("Turbine 1", function() turbine = 0 end, 47, 6, 57, 8)
  303.   tM:add("Turbine 2", function() turbine = 1 end, 59, 6, 69, 8)
  304.   tM:add("Turbine 3", function() turbine = 2 end, 47, 10, 57, 12)
  305.   tM:add("Turbine 4", function() turbine = 3 end, 59, 10, 69, 12)
  306.   tM:add("Turbine 5", function() turbine = 4 end, 47, 14, 57, 16)
  307.   tM:add("Turbine 6", function() turbine = 5 end, 59, 14, 69, 16)
  308.   tM:add("Overall", function() t:flash("Overall") changePage(tS) end, 59, 23, 69, 25)
  309.   tM:add("Manual", function() end, 59, 18, 69, 20)
  310.   tM:add("Auto", function() changePage(tA) end, 47, 18, 57, 20)
  311.   tM:add("Power", function() turbinePower() end, 2, 22, 12, 24)
  312.   tM:add("Coils", function() coilsPower() end, 14, 22, 24, 24)
  313.   tM:add("Reactor", function() reactorPower() end, 26, 22, 36, 24)
  314.  
  315.   tS:add("Main", function() t:flash("Main") changePage(oldT) end, 59, 23, 69, 25)
  316.  
  317. end
  318.  
  319. mainPage() --set mode to auto on start
  320. t:draw() --to be able to do stuff with buttons outside loop
  321. buttonToggles() --toggle needed buttons
  322. refreshTimer = os.startTimer(3)
  323. function mainLoop()
  324.   while true do
  325.     --m.clear() --Gets rid of old stuff
  326.     t:draw()--draw buttons
  327.     information()--RPMs, RF/t, etc.
  328.     buttonToggles2()
  329.     local event, p1 = t:handleEvents(os.pullEvent())
  330.     if event == "button_click" then
  331.       t.buttonList[p1].func()
  332.     elseif event == "timer" and p1 == "refreshTimer01" then
  333.       refreshTimer = os.startTimer(3)
  334.     end
  335.   end
  336. end
  337. parallel.waitForAny(mainLoop, auto)
Advertisement
Add Comment
Please, Sign In to add comment