Advertisement
MuChT007

CoalSteamBoilerControl 0.2

May 12th, 2020 (edited)
574
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.64 KB | None | 0 0
  1. --Variables--
  2. local printer = peripheral.wrap("bottom")
  3. local modem = peripheral.wrap("right")
  4. local txChannel = 1400
  5. local rxChannel = 1410
  6. local rsOutSide = "left"
  7. local rsInfoSide = "top"
  8. local boilerFeeder = {colors.red,colors.orange,colors.gray}
  9. local boilerStock = {colors.blue,colors.lightBlue,colors.black}
  10. local coal = {false,false,false}
  11. local coalInfo = {"","",""}
  12. local boilerInfo ={"","",""}
  13. local alarm = colors.orange
  14. local signal = colors.red
  15. local modeSwitch = colors.white
  16. local mode = "local" -- local,offline
  17. local switch = ""
  18. local tankMax = {colors.purple,colors.green}
  19. local tankMin = {colors.cyan,colors.lime}
  20. local boilerOn = {false,false,false}
  21. local tankLevel = {111,222}
  22. local rsOut = 0
  23. local rsInfo = 0
  24. local devMode = false
  25. local dataEvent ={}
  26. x, y = term.getSize()
  27. --Network variables
  28. local production = "coal"
  29. local typeOf = "production"
  30. local boilerWork ={"","",""}
  31. local coalLevel ={"","",""}
  32.  
  33.  
  34. --Functions--
  35. function drawAcross(line, char)
  36.     term.setCursorPos(1, line)
  37.     for i = 1,  x do
  38.         term.write(char)
  39.     end
  40. end
  41.  
  42. function drawCentered(line, text)
  43.     term.setCursorPos((x - string.len(text))/ 2, line)
  44.     term.write(text)
  45. end
  46.  
  47. function printReport ()
  48.     printer.newPage()
  49.          local time = os.time()
  50.          local day = os.day()
  51.          local formattedTime = textutils.formatTime(time, false)
  52.          printer.setCursorPos(1, 1)
  53.          printer.write("Report:")
  54.          printer.setCursorPos(1, 2)
  55.          printer.write("Day :"..day)
  56.          printer.setCursorPos(1, 3)
  57.          printer.write("Time :".. formattedTime)
  58.          printer.setCursorPos(1, 4)
  59.          printer.write(""..printerLine1)
  60.          printer.setCursorPos(1, 5)
  61.          printer.write(""..printerLine2)
  62.          printer.setCursorPos(1, 6)
  63.          printer.write(""..printerLine3)
  64.          printer.setCursorPos(1, 7)
  65.          printer.write(""..printerLine4)
  66.          printer.setCursorPos(1, 8)
  67.          printer.write(""..printerLine5)
  68.          printer.setCursorPos(1, 9)
  69.          printer.write(""..printerLine6)
  70.          printer.setCursorPos(1, 10)
  71.          printer.write(""..printerLine7)
  72.          printer.setCursorPos(1, 11)
  73.          printer.write(""..printerLine8)
  74.          printer.setCursorPos(1, 12)
  75.          printer.write(""..printerLine9)
  76.          printer.setCursorPos(1, 13)
  77.          printer.write(""..printerLine10)
  78.     printer.endPage()
  79.    
  80. end
  81.  
  82. function cleanPrintReport ()
  83.     printerLine1 = ""
  84.     printerLine2 = ""
  85.     printerLine3 = ""
  86.     printerLine4 = ""
  87.     printerLine5 = ""
  88.     printerLine6 = ""
  89.     printerLine7 = ""
  90.     printerLine8 = ""
  91.     printerLine9 = ""
  92.     printerLine10 = ""
  93. end
  94.  
  95. function checkTanks ()
  96.     check = redstone.testBundledInput (rsOutSide, tankMax[1] )
  97.     if check == true then
  98.         tankLevel[1] = 100
  99.     else
  100.         check = redstone.testBundledInput (rsOutSide, tankMin[1] )
  101.         if check == true then
  102.             tankLevel[1] = 51
  103.         else
  104.             tankLevel[1] = 0
  105.         end
  106.     end
  107.    
  108.     check = redstone.testBundledInput (rsOutSide, tankMax[2] )
  109.     if check == true then
  110.         tankLevel[2] = 100
  111.     else
  112.         check = redstone.testBundledInput (rsOutSide, tankMin[2] )
  113.         if check == true then
  114.             tankLevel[2] = 51
  115.         else
  116.             tankLevel[2] = 0
  117.         end
  118.     end
  119.    
  120. end
  121.  
  122. function checkCoal ()
  123.     for i = 1, 3 do
  124.         check = redstone.testBundledInput (rsOutSide, boilerStock[i] )
  125.         if check == true then
  126.             coal[i] = true
  127.             coalInfo[i] = "Yes"
  128.             coalLevel[i] = 100
  129.         else
  130.             coal[i] = false
  131.             coalInfo[i] = "No"
  132.             coalLevel[i] = 0
  133.         end
  134.     end
  135.    
  136. end
  137.  
  138. function checkBoiler ()
  139.     for i = 1, 3 do
  140.         check = redstone.testBundledInput (rsOutSide, boilerFeeder[i] )
  141.         if check == true then
  142.             boilerOn[i] = false
  143.             boilerInfo[i] = "Off"
  144.             boilerWork[i] = "no"
  145.         else
  146.             boilerOn[i] = true
  147.             boilerInfo[i] = "On"
  148.             boilerWork[i] = "yes"
  149.         end
  150.     end
  151. end
  152.  
  153. function termClearLines ()
  154.     term.setCursorPos(1, 5)
  155.     term.clearLine()
  156.     term.setCursorPos(1, 6)
  157.     term.clearLine()
  158.     term.setCursorPos(1, 7)
  159.     term.clearLine()
  160.     term.setCursorPos(1, 8)
  161.     term.clearLine()
  162.     term.setCursorPos(1, 9)
  163.     term.clearLine()
  164.     term.setCursorPos(1, 10)
  165.     term.clearLine()
  166.     term.setCursorPos(1, 11)
  167.     term.clearLine()
  168.     term.setCursorPos(1, 12)
  169.     term.clearLine()
  170.     term.setCursorPos(1, 13)
  171.     term.clearLine()
  172. end
  173.  
  174. function termShowInfo ()
  175.     --steam tanks levels--
  176.     term.setCursorPos(1, 6)
  177.     term.write("Steam tank 1 %:"..tankLevel[1])
  178.     term.setCursorPos(1, 7)
  179.     term.write("Steam tank 2 %:"..tankLevel[2])
  180.     --coal chests level--
  181.     term.setCursorPos(1, 9)
  182.     term.write("Fuel boiler 1:"..coalInfo[1])
  183.     term.setCursorPos(1, 10)
  184.     term.write("Fuel boiler 2:"..coalInfo[2])
  185.     term.setCursorPos(1, 11)
  186.     term.write("Fuel boiler 3:"..coalInfo[3])
  187.     --sytem mode--
  188.     term.setCursorPos(1, 13)
  189.     term.write("Mode:"..mode)
  190.     --Boilers--
  191.     term.setCursorPos(23, 6)
  192.     term.write("Boiler 1:"..boilerInfo[1])
  193.     term.setCursorPos(23, 7)
  194.     term.write("Boiler 2:"..boilerInfo[2])
  195.     term.setCursorPos(23, 8)
  196.     term.write("Boiler 3:"..boilerInfo[3])
  197. end
  198.  
  199. function OnOff ()
  200.     check = redstone.testBundledInput (rsOutSide, modeSwitch )
  201.         if check == true then
  202.             switch = "On"
  203.         else
  204.             switch = "Off"
  205.     end
  206. end
  207.  
  208. function siren()
  209.     rsInfo = colors.combine(rsInfo, alarm)
  210.     redstone.setBundledOutput(rsInfoSide, rsInfo)
  211.     sleep (4)
  212.     rsInfo = colors.subtract(colors.combine(rsInfo), alarm)
  213.     redstone.setBundledOutput(rsInfoSide, rsInfo)
  214. end
  215.  
  216. function prepareDatas()
  217.  
  218.     data = {production,typeOf,0,0,switch,boilerWork,coalLevel,tankLevel}
  219. end
  220.  
  221. --Init--
  222. rsOut = colors.combine(boilerFeeder[1], boilerFeeder[2], boilerFeeder[3] )
  223. redstone.setBundledOutput(rsOutSide, rsOut)
  224. sleep (3)
  225. OnOff ()
  226. if (devMode == true) then
  227.     term.clear()
  228.     drawAcross(1, "-")
  229.     drawCentered(2, "DEV mode")
  230.     drawCentered(4,"---")
  231.     while (devMode == true) do
  232.         term.setCursorPos(1, 17)
  233.         term.write("press T to refresh, E to exit, C to continue")
  234.         local event, key = os.pullEvent( "key" )
  235.         if key == keys.t then -- if the key pressed was 't'
  236.             checkTanks ()
  237.             checkCoal ()
  238.             checkBoiler ()
  239.             termClearLines ()
  240.             termShowInfo ()
  241.         end
  242.         if key == keys.e then -- if the key pressed was 'e'
  243.             devMode = false
  244.             os.reboot()
  245.         end
  246.         if key == keys.c then -- if the key pressed was 'c'
  247.             devMode = false
  248.         end
  249.     end
  250. end
  251.  
  252. term.clear()
  253. drawAcross(1, "-")
  254. drawCentered(2, "Init.")
  255. drawCentered(4,"---")
  256. checkTanks ()
  257. checkCoal ()
  258. checkBoiler ()
  259. termClearLines ()
  260. termShowInfo ()
  261.  
  262.    
  263. while true do
  264. OnOff ()
  265.     if switch == "Off" then
  266.         rsOut = colors.combine(boilerFeeder[1], boilerFeeder[2], boilerFeeder[3] )
  267.         redstone.setBundledOutput(rsOutSide, rsOut )
  268.         term.clear()
  269.         drawAcross(1, "-")
  270.         drawCentered(2, "Standby mode")
  271.         drawCentered(4,"---")
  272.         checkTanks ()
  273.         checkCoal ()
  274.         checkBoiler ()
  275.         termClearLines ()
  276.         termShowInfo ()
  277.         event = os.pullEvent("redstone")
  278.         OnOff ()
  279.         if switch == "On" then
  280.             siren ()
  281.         end
  282.     end
  283.    
  284.     if switch == "On" then
  285.         term.clear()
  286.         drawAcross(1, "-")
  287.         drawCentered(2, "Operation mode")
  288.         drawCentered(4,"---")
  289.         checkTanks ()
  290.         checkCoal ()
  291.         checkBoiler ()
  292.         termClearLines ()
  293.         termShowInfo ()
  294.         event, dataEvent[1], dataEvent[2],dataEvent[3],dataEvent[4],dataEvent[5]= os.pullEvent()
  295.         if (event == "char") and  (dataEvent[1] == "m") then
  296.             if mode == "local" then
  297.                 mode = "offline"
  298.             else
  299.                 mode = "local"
  300.             end
  301.            
  302.         end
  303.        
  304.         if (event == "redstone") or ((event == "char") and  (dataEvent[1] == "r")) then
  305.             checkTanks ()
  306.             checkCoal ()
  307.             checkBoiler ()
  308.             termClearLines ()
  309.             termShowInfo ()
  310.             --Boiler 1
  311.             if tankLevel[1] < 99 then
  312.                 boilerOn[1] = true
  313.             else
  314.                 boilerOn[1] = false
  315.             end
  316.             --Boiler 2
  317.             if (tankLevel[1] < 50) or (tankLevel[2] < 99) then
  318.                 boilerOn[2] = true
  319.             else
  320.                 boilerOn[2] = false
  321.             end
  322.             --Boiler 3
  323.             if tankLevel[2] < 50 then
  324.                 boilerOn[3] = true
  325.             else
  326.                 boilerOn[3] = false
  327.             end
  328.             --If fuel
  329.             for i = 1,3 do
  330.                 if (coal[i] == false) and (boilerOn[i] == true) then
  331.                     boilerOn[i] = false
  332.                 end
  333.             end
  334.             --Turn boilers on needed boilers
  335.             for i = 1,3 do
  336.                 if boilerOn[i] == true then
  337.                     check = redstone.testBundledInput (rsOutSide, boilerFeeder[i] )
  338.                     if check == true then
  339.                         rsOut = colors.subtract(colors.combine(rsOut), boilerFeeder[i])
  340.                         redstone.setBundledOutput(rsOutSide, rsOut)
  341.                     end
  342.                 end
  343.             end
  344.             --Turn off the others
  345.             for i = 1,3 do
  346.                 if boilerOn[i] == false then
  347.                     check = redstone.testBundledInput (rsOutSide, boilerFeeder[i] )
  348.                     if check == false then
  349.                         rsOut = colors.combine(rsOut, boilerFeeder[i] )
  350.                         redstone.setBundledOutput(rsOutSide, rsOut)
  351.                     end
  352.                 end
  353.             end
  354.             checkTanks ()
  355.             checkCoal ()
  356.             checkBoiler ()
  357.             termClearLines ()
  358.             termShowInfo ()
  359.            
  360.         end
  361.     end
  362.    
  363.     --Sending to control room
  364.     if mode == "local" then
  365.         prepareDatas()
  366.         modem.transmit(txChannel,rxChannel,textutils.serialize(data))
  367.     end    
  368. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement