Guest User

trb

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