Thor_s_Crafter

reactorControl

Mar 11th, 2016
11,812
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.30 KB | None | 0 0
  1. -- Reaktor- und Turbinenprogramm von Thor_s_Crafter --
  2. -- Version 2.3 --
  3. -- Reaktorkontrolle --
  4.  
  5. shell.run("cp /reactor-turbine-program/config/touchpoint.lua /touchpoint")
  6. os.loadAPI("touchpoint")
  7. shell.run("rm touchpoint")
  8.  
  9. shell.run("cp /reactor-turbine-program/config/input.lua /input")
  10. os.loadAPI("input")
  11. shell.run("rm input")
  12.  
  13. local page = touchpoint.new(touchpointLocation)
  14. local rodLevel
  15. local enPer
  16. local fuel
  17. local fuelPer
  18. local rfGen
  19. local fuelCons
  20. local isOn
  21. local enPerR
  22. local rOn
  23. local rOff
  24. local modeA
  25. local modeM
  26.  
  27. function createButtons()
  28.   if lang == "de" then
  29.     modeA = {" Automatisch ",label = "modeSwitch"}
  30.     modeM = {"  Manuell   ",label = "modeSwitch"}
  31.   elseif lang == "en" then
  32.     modeA = {" Automatic ",label = "modeSwitch"}
  33.     modeM = {"  Manual   ",label = "modeSwitch"}
  34.   end
  35.  
  36.   page:add("modeSwitch",switchMode,19,22,33,22)
  37.   if overallMode == "auto" then
  38.     page:rename("modeSwitch",modeA,true)
  39.   elseif overallMode == "manual" then
  40.     page:rename("modeSwitch",modeM,true)
  41.   end
  42.   --In Deutsch
  43.   if lang == "de" then
  44.     page:add("Neu starten",restart,2,18,17,18)
  45.     page:add("Optionen",function() run("/reactor-turbine-program/program/editOptions.lua") end,2,20,17,20)
  46.     page:add("Hauptmenue",function() run("/reactor-turbine-program/start/menu.lua") end,2,22,17,22)
  47.     --In Englisch
  48.   elseif lang == "en" then
  49.     page:add("Reboot",restart,2,18,17,18)
  50.     page:add("Options",function() run("/reactor-turbine-program/program/editOptions.lua") end,2,20,17,20)
  51.     page:add("Main Menu",function() run("/reactor-turbine-program/start/menu.lua") end,2,22,17,22)
  52.   end
  53.   page:draw()
  54. end
  55.  
  56. function createButtonsMan()
  57.   createButtons()
  58.   if lang == "de" then
  59.     rOn = {" Ein ",label = "reactorOn"}
  60.     rOff = {" Aus ",label = "reactorOn"}
  61.   elseif lang == "en" then
  62.     rOn = {" On ",label = "reactorOn"}
  63.     rOff = {" Off ",label = "reactorOn"}
  64.   end
  65.   page:add("reactorOn",toggleReactor,11,10,15,10)
  66.   if r.getActive() then
  67.     page:rename("reactorOn",rOn,true)
  68.     page:toggleButton("reactorOn")
  69.   else
  70.     page:rename("reactorOn",rOff,true)
  71.   end
  72.   page:draw()
  73. end
  74.  
  75. function checkPeripherals()
  76.   mon.setBackgroundColor(colors.black)
  77.   mon.clear()
  78.   mon.setCursorPos(1,1)
  79.   mon.setTextColor(colors.red)
  80.   term.clear()
  81.   term.setCursorPos(1,1)
  82.   term.setTextColor(colors.red)
  83.   if r == "" then
  84.     if lang == "de" then
  85.       mon.write("Reaktor nicht gefunden! Bitte pruefen und den Computer neu starten (Strg+R gedrueckt halten)")
  86.       error("Reaktor nicht gefunden! Bitte pruefen und den Computer neu starten (Strg+R gedrueckt halten)")
  87.     elseif lang == "en" then
  88.       mon.write("Reactor not found! Please check and reboot the computer (Press and hold Ctrl+R)")
  89.       error("Reactor not found! Please check and reboot the computer (Press and hold Ctrl+R)")
  90.     end
  91.   end
  92.   if v == "" then
  93.     if lang == "de" then
  94.       mon.write("Energiezelle nicht gefunden! Bitte pruefen und den Computer neu starten (Strg+R gedrueckt halten)")
  95.       error("Energiezelle nicht gefunden! Bitte pruefen und den Computer neu starten (Strg+R gedrueckt halten)")
  96.     elseif lang == "en" then
  97.       mon.write("Energy Cell not found! Please check and reboot the computer (Press and hold Ctrl+R)")
  98.       error("Energy Cell not found! Please check and reboot the computer (Press and hold Ctrl+R)")
  99.     end
  100.   end
  101. end
  102.  
  103. --Schaltet den Reaktor um
  104. function toggleReactor()
  105.   r.setActive(not r.getActive())
  106.   page:toggleButton("reactorOn")
  107.   if r.getActive() then
  108.     page:rename("reactorOn",rOn,true)
  109.   else
  110.     page:rename("reactorOn",rOff,true)
  111.   end
  112. end
  113.  
  114. function getEnergy()
  115.   local en = v.getEnergyStored()
  116.   local enMax = v.getMaxEnergyStored()
  117.   return math.floor(en/enMax*100)
  118. end
  119.  
  120. function getEnergyR()
  121.   local en = r.getEnergyStored()
  122.   local enMax = 10000000
  123.   return math.floor(en/enMax*100)
  124. end
  125.  
  126. function getReactorData()
  127.   rodLevel = r.getControlRodLevel(1)
  128.   enPer = getEnergy()
  129.   enPerR = getEnergyR()
  130.   fuel = r.getFuelAmount()
  131.   local fuelMax = r.getFuelAmountMax()
  132.   fuelPer = math.floor(fuel/fuelMax*100)
  133.   rfGen = r.getEnergyProducedLastTick()
  134.   fuelCons = r.getFuelConsumedLastTick()
  135.   isOn = r.getActive()
  136. end
  137.  
  138. function getClick()
  139.   getReactorData()
  140.  
  141.   if overallMode == "auto" then
  142.     displayDataAuto()
  143.   elseif overallMode == "manual" then
  144.     displayDataMan()
  145.   end
  146.  
  147.   local time = os.startTimer(0.5)
  148.  
  149.   local event, but = page:handleEvents(os.pullEvent())
  150.   print(event)
  151.  
  152.   if event == "button_click" then
  153.     page:flash(but)
  154.     page.buttonList[but].func()
  155.   elseif event == "terminate" then
  156.     sleep(2)
  157.     mon.clear()
  158.     mon.setCursorPos(1,1)
  159.     mon.setTextColor(colors.red)
  160.     mon.write("Programm abgebrochen!")
  161.     error("Manuell abgebrochen")
  162.   end
  163. end
  164.  
  165. function switchMode()
  166.   if overallMode == "auto" then
  167.     overallMode = "manual"
  168.     saveOptionFile()
  169.   elseif overallMode == "manual" then
  170.     overallMode = "auto"
  171.     saveOptionFile()
  172.   end
  173.   page = ""
  174.   mon.clear()
  175.   run("/reactor-turbine-program/program/reactorControl.lua")
  176. end
  177.  
  178. function displayDataAuto()
  179.   if enPer <= reactorOnAt then
  180.     r.setActive(true)
  181.   elseif enPer > reactorOffAt then
  182.     r.setActive(false)
  183.   end
  184.  
  185.   page:draw()
  186.  
  187.   mon.setBackgroundColor(tonumber(backgroundColor))
  188.   mon.setTextColor(tonumber(textColor))
  189.  
  190.   mon.setCursorPos(2,2)
  191.   if lang == "de" then
  192.     mon.write("Energie: "..enPer.."%  ")
  193.   elseif lang == "en" then
  194.     mon.write("Energy: "..enPer.."%  ")
  195.   end
  196.   mon.setCursorPos(2,3)
  197.   mon.setBackgroundColor(colors.green)
  198.   for i=0 ,enPer,5 do
  199.     mon.write(" ")
  200.   end
  201.   mon.setBackgroundColor(colors.lightGray)
  202.   local tmpEn = enPer/5
  203.   local pos = 22-(19-tmpEn)
  204.   mon.setCursorPos(pos,3)
  205.   for i=0,(19-tmpEn),1 do
  206.     mon.write(" ")
  207.   end
  208.  
  209.   mon.setBackgroundColor(tonumber(backgroundColor))
  210.  
  211.   mon.setCursorPos(2,5)
  212.   if lang == "de" then
  213.     mon.write("Energie (Reaktor): "..enPerR.."%  ")
  214.   elseif lang == "en" then
  215.     mon.write("Energy (Reactor): "..enPerR.."%  ")
  216.   end
  217.   mon.setCursorPos(2,6)
  218.   mon.setBackgroundColor(colors.green)
  219.   if enPerR > 5 then
  220.     for i=0 ,enPerR,5 do
  221.       mon.write(" ")
  222.     end
  223.   end
  224.   mon.setBackgroundColor(colors.lightGray)
  225.   if enPerR < 5 then
  226.     for i=1,20 do
  227.       mon.write(" ")
  228.     end
  229.   else
  230.     local tmpEnR = enPerR/5
  231.     local posR = 22-(19-tmpEnR)
  232.     mon.setCursorPos(posR,6)
  233.     for i=0,(19-tmpEnR),1 do
  234.       mon.write(" ")
  235.     end
  236.   end
  237.  
  238.   mon.setBackgroundColor(tonumber(backgroundColor))
  239.  
  240.   mon.setCursorPos(30,2)
  241.   mon.write("RodLevel: "..rodLevel.."  ")
  242.   mon.setCursorPos(30,3)
  243.   mon.setBackgroundColor(colors.green)
  244.   for i=0 ,rodLevel,5 do
  245.     mon.write(" ")
  246.   end
  247.   mon.setBackgroundColor(colors.lightGray)
  248.   local tmpRod = rodLevel/5
  249.   local posRL = 50-(19-tmpRod)
  250.   mon.setCursorPos(posRL,3)
  251.   for i=0,(19-tmpRod),1 do
  252.     mon.write(" ")
  253.   end
  254.  
  255.   mon.setBackgroundColor(tonumber(backgroundColor))
  256.  
  257.   mon.setCursorPos(2,8)
  258.   if lang == "de" then
  259.     mon.write("RF-Produktion: "..math.floor(rfGen).." RF/t      ")
  260.   elseif lang == "en" then
  261.     mon.write("RF-Production: "..math.floor(rfGen).." RF/t      ")
  262.   end
  263.  
  264.   mon.setCursorPos(2,10)
  265.   if lang == "de" then
  266.     mon.write("Reaktor: ")
  267.     if r.getActive() then
  268.       mon.setTextColor(colors.green)
  269.       mon.write("an ")
  270.     end
  271.     if not r.getActive() then
  272.       mon.setTextColor(colors.red)
  273.       mon.write("aus")
  274.     end
  275.   elseif lang == "en" then
  276.     mon.write("Reactor: ")
  277.     if r.getActive() then
  278.       mon.setTextColor(colors.green)
  279.       mon.write("on ")
  280.     end
  281.     if not r.getActive() then
  282.       mon.setTextColor(colors.red)
  283.       mon.write("off")
  284.     end
  285.   end
  286.  
  287.   mon.setTextColor(tonumber(textColor))
  288.  
  289.   mon.setCursorPos(2,12)
  290.   local fuelCons = tostring(r.getFuelConsumedLastTick())
  291.   local fuelCons2 = string.sub(fuelCons, 0,4)
  292.  
  293.   if lang == "de" then
  294.     mon.write("Reaktor-Verbrauch: "..fuelCons2.."mb/t     ")
  295.   elseif lang == "en" then
  296.     mon.write("Fuel Consumption: "..fuelCons2.."mb/t     ")
  297.   end
  298.  
  299.   local caT = tostring(r.getCasingTemperature())
  300.   local caseTemp = string.sub(caT,0,6)
  301.   local coT = tostring(r.getFuelTemperature())
  302.   local coreTemp = string.sub(coT,0,6)
  303.  
  304.   mon.setCursorPos(2,14)
  305.   if lang == "de" then
  306.     mon.write("Huellentemperatur: "..caseTemp.."C    ")
  307.     mon.setCursorPos(2,15)
  308.     mon.write("Kerntemperatur: "..coreTemp.."C    ")
  309.   elseif lang == "en" then
  310.     mon.write("Casing Temperature: "..caseTemp.."C    ")
  311.     mon.setCursorPos(2,15)
  312.     mon.write("Core Temperature: "..coreTemp.."C    ")
  313.   end
  314.  
  315.  
  316.   mon.setCursorPos(2,25)
  317.   mon.write("Version "..version)
  318. end
  319.  
  320. function displayDataMan()
  321.  
  322.   if r.getActive() then
  323.     if not page.buttonList["reactorOn"].active then
  324.       page:toggleButton("reactorOn")
  325.       page:rename("reactorOn",rOn,true)
  326.     end
  327.   else
  328.     if page.buttonList["reactorOn"].active then
  329.       page:toggleButton("reactorOn")
  330.       page:rename("reactorOn",rOff,true)
  331.     end
  332.   end
  333.  
  334.   page:draw()
  335.  
  336.   mon.setBackgroundColor(tonumber(backgroundColor))
  337.   mon.setTextColor(tonumber(textColor))
  338.  
  339.   mon.setCursorPos(2,2)
  340.   if lang == "de" then
  341.     mon.write("Energie: "..enPer.."%  ")
  342.   elseif lang == "en" then
  343.     mon.write("Energy: "..enPer.."%  ")
  344.   end
  345.   mon.setCursorPos(2,3)
  346.   mon.setBackgroundColor(colors.green)
  347.   for i=0 ,enPer,5 do
  348.     mon.write(" ")
  349.   end
  350.   mon.setBackgroundColor(colors.lightGray)
  351.   local tmpEn = enPer/5
  352.   local pos = 22-(19-tmpEn)
  353.   mon.setCursorPos(pos,3)
  354.   for i=0,(19-tmpEn),1 do
  355.     mon.write(" ")
  356.   end
  357.  
  358.   mon.setBackgroundColor(tonumber(backgroundColor))
  359.  
  360.   mon.setCursorPos(2,5)
  361.   if lang == "de" then
  362.     mon.write("Energie (Reaktor): "..enPerR.."%  ")
  363.   elseif lang == "en" then
  364.     mon.write("Energy (Reactor): "..enPerR.."%  ")
  365.   end
  366.   mon.setCursorPos(2,6)
  367.   mon.setBackgroundColor(colors.green)
  368.   if enPerR > 5 then
  369.     for i=0 ,enPerR,5 do
  370.       mon.write(" ")
  371.     end
  372.   end
  373.   mon.setBackgroundColor(colors.lightGray)
  374.   if enPerR < 5 then
  375.     for i=1,20 do
  376.       mon.write(" ")
  377.     end
  378.   else
  379.     local tmpEnR = enPerR/5
  380.     local posR = 22-(19-tmpEnR)
  381.     mon.setCursorPos(posR,6)
  382.     for i=0,(19-tmpEnR),1 do
  383.       mon.write(" ")
  384.     end
  385.   end
  386.  
  387.   mon.setBackgroundColor(tonumber(backgroundColor))
  388.  
  389.   mon.setCursorPos(30,2)
  390.   mon.write("RodLevel: "..rodLevel.."  ")
  391.   mon.setCursorPos(30,3)
  392.   mon.setBackgroundColor(colors.green)
  393.   for i=0 ,rodLevel,5 do
  394.     mon.write(" ")
  395.   end
  396.   mon.setBackgroundColor(colors.lightGray)
  397.   local tmpRod = rodLevel/5
  398.   local posRL = 50-(19-tmpRod)
  399.   mon.setCursorPos(posRL,3)
  400.   for i=0,(19-tmpRod),1 do
  401.     mon.write(" ")
  402.   end
  403.  
  404.   mon.setBackgroundColor(tonumber(backgroundColor))
  405.  
  406.   mon.setCursorPos(2,8)
  407.   if lang == "de" then
  408.     mon.write("RF-Produktion: "..math.floor(rfGen).." RF/t      ")
  409.   elseif lang == "en" then
  410.     mon.write("RF-Production: "..math.floor(rfGen).." RF/t      ")
  411.   end
  412.  
  413.   mon.setCursorPos(2,10)
  414.   if lang == "de" then
  415.     mon.write("Reaktor: ")
  416.   elseif lang == "en" then
  417.     mon.write("Reactor: ")
  418.   end
  419.  
  420.   mon.setTextColor(tonumber(textColor))
  421.  
  422.   mon.setCursorPos(2,12)
  423.   local fuelCons = tostring(r.getFuelConsumedLastTick())
  424.   local fuelCons2 = string.sub(fuelCons, 0,4)
  425.  
  426.   if lang == "de" then
  427.     mon.write("Reaktor-Verbrauch: "..fuelCons2.."mb/t     ")
  428.   elseif lang == "en" then
  429.     mon.write("Fuel Consumption: "..fuelCons2.."mb/t     ")
  430.   end
  431.  
  432.   local caT = tostring(r.getCasingTemperature())
  433.   local caseTemp = string.sub(caT,0,6)
  434.   local coT = tostring(r.getFuelTemperature())
  435.   local coreTemp = string.sub(coT,0,6)
  436.  
  437.   mon.setCursorPos(2,14)
  438.   if lang == "de" then
  439.     mon.write("Huellentemperatur: "..caseTemp.."C    ")
  440.     mon.setCursorPos(2,15)
  441.     mon.write("Kerntemperatur: "..coreTemp.."C    ")
  442.   elseif lang == "en" then
  443.     mon.write("Casing Temperature: "..caseTemp.."C    ")
  444.     mon.setCursorPos(2,15)
  445.     mon.write("Core Temperature: "..coreTemp.."C    ")
  446.   end
  447.  
  448.  
  449.   mon.setCursorPos(2,25)
  450.   mon.write("Version "..version)
  451. end
  452.  
  453. function run(program)
  454.   shell.run(program)
  455.   error("end reactorControl")
  456. end
  457.  
  458. checkPeripherals()
  459. if overallMode == "auto" then
  460.   createButtons()
  461. elseif overallMode == "manual" then
  462.   createButtonsMan()
  463. end
  464. mon.setBackgroundColor(tonumber(backgroundColor))
  465. mon.setTextColor(tonumber(textColor))
  466. mon.clear()
  467. while true do
  468.   getClick()
  469. end
Advertisement
Add Comment
Please, Sign In to add comment