Advertisement
bob558

Energy, Machines, fluid ME control

Sep 22nd, 2020
1,779
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.17 KB | None | 0 0
  1. comp = require("component")
  2. screen = require("term")
  3. computer = require("computer")
  4. event = require("event")
  5. GPU1 = comp.gpu
  6. sides = require("sides")
  7. rs = comp.redstone
  8. me_interface = comp.me_interface
  9. gtBattBuf = comp.gt_batterybuffer
  10. gtMachine = comp.gt_machine
  11. --Конфиг
  12. screenWidth = 160
  13. screenHeight = 50
  14. borderColor = 0x1A1A1A
  15. backgroundColor = 0x0D0D0D
  16. progressBarColor = 0x00CC00
  17. errorColor = 0xCC0000
  18. workingColor = 0x00FF00
  19. idleColor = 0xFFFF00
  20. textColor = 0xFFFFFF
  21. maxEUColor = 0xFFE800
  22. currentEUColor = 0x2592E7
  23. positiveEUColor = 0x00CC00
  24. negativeEUColor = 0xCC0000
  25. euUpdateInterval = 3
  26.  
  27. GPU1.setResolution(screenWidth, screenHeight)
  28. firstRead, lastRead, counter, currentIO = 0, 0, 1, 1
  29. readings = {}
  30. machines = {}
  31. fluidNames = {}
  32. fluids = {}
  33.  
  34. --=========== Утилиты
  35.  
  36. -- Формат: Час Минута Секунда
  37. function time(number)
  38.     local formattedTime = {}
  39.     formattedTime[1] = math.floor(number / 3600); formattedTime[2] = "h "
  40.     formattedTime[3] = math.floor((number - formattedTime[1] * 3600) / 60); formattedTime[4] = "min "
  41.     formattedTime[5] = number % 60; formattedTime[6] = "s"
  42.     return table.concat(formattedTime, "")
  43. end
  44.  
  45. -- Формат чисел XXX,XXX,XXX
  46. function splitNumber(number)
  47.     local formattedNumber = {}
  48.     local string = tostring(math.abs(number))
  49.     local sign = number / math.abs(number)
  50.     for i = 1, #string do
  51.         n = string:sub(i, i)
  52.         formattedNumber[i] = n
  53.         if ((#string - i) % 3 == 0) and (#string - i > 0) then
  54.             formattedNumber[i] = formattedNumber[i] .. ","
  55.         end
  56.     end
  57.     if (sign < 0) then table.insert(formattedNumber, 1, "-")
  58.     end
  59.     return table.concat(formattedNumber, "")
  60. end
  61.  
  62. -- Формат: процентраж (%)
  63. function getPercent(number)
  64.     percent = {}
  65.     percent[1] = math.floor(number * 1000) / 10
  66.     percent[2] = "%"
  67.     return table.concat(percent, " ")
  68. end
  69.  
  70. -- Прогресс текст
  71. function progressText(current, max)
  72.     formattedProgress = {}
  73.     formattedProgress[1] = current
  74.     formattedProgress[2] = max
  75.     return table.concat(formattedProgress, "/")
  76. end
  77.  
  78. -- Вывод боксов
  79. function box(x, y, w, h, color)
  80.     local oldColor = GPU1.setBackground(color)
  81.     GPU1.fill(x, y, w, h, " ")
  82.     GPU1.setBackground(oldColor)
  83. end
  84.  
  85. -- Вывод текста
  86. function write(x, y, text, color)
  87.     color = color or textColor
  88.     screen.setCursor(x, y)
  89.     oldColor = GPU1.setForeground(color)
  90.     screen.write(text)
  91.     GPU1.setForeground(oldColor)
  92. end
  93.  
  94. function updateEU(pss)
  95.     if counter == 1 then firstRead = computer.uptime()
  96.     end
  97.     if counter < euUpdateInterval then
  98.         readings[counter] = pss.getEUStored(); counter = counter + 1
  99.     end
  100.     if counter == euUpdateInterval then
  101.         lastRead = computer.uptime()
  102.         ticks = math.ceil((lastRead - firstRead) * 20)
  103.         currentIO = string.gsub(pss.getSensorInformation()[6], "([^0-9]+)", "") - string.gsub(pss.getSensorInformation()[8], "([^0-9]+)", "")
  104.         counter = 1
  105.     end
  106. end
  107.  
  108. -- Регистрация Мультиблоков
  109. function gtMachine(x, y, address, label)
  110.     machine = comp.proxy(comp.get(address))
  111.  
  112.     maintenanceIndex = 0
  113.     if machines[label] == nil then
  114.         machines[label] = machine
  115.         box(x, y, 4, 3, borderColor)
  116.         box(x + 4, y, 19, 3, backgroundColor)
  117.         write(x + 5, y, label, textColor)
  118.     end
  119.  
  120.     box(x + 4, y + 1, 19, 2, backgroundColor)
  121.     box(x + 5 + #label, y, 23 - #label - 5, 1, backgroundColor)
  122.     for i = 1, #machine.getSensorInformation() do
  123.         if string.match(machine.getSensorInformation()[i], "Problems") then
  124.             maintenanceIndex = i
  125.         end
  126.     end
  127.     if (machine.hasWork()) then
  128.         GPU1.setBackground(backgroundColor)
  129.         box(x + 1, y + 1, 2, 1, workingColor)
  130.         box(x + 5, y + 1, math.floor(18 * (machine.getWorkProgress() / machine.getWorkMaxProgress())), 1, progressBarColor)
  131.         local progressString = progressText(math.floor(machine.getWorkProgress() / 20), math.floor(machine.getWorkMaxProgress() / 20)) .. "s"
  132.         write(x + 23 - #progressString, y, progressString)
  133.     else
  134.         box(x + 1, y + 1, 2, 1, idleColor)
  135.     end
  136.     GPU1.setBackground(backgroundColor)
  137.     if (maintenanceIndex == 0 or string.match(machine.getSensorInformation()[maintenanceIndex], "c0")) and machine.isWorkAllowed() then
  138.         --Do nothing
  139.     else
  140.         if (machine.isWorkAllowed()) then
  141.             box(x + 1, y + 1, 2, 1, idleColor)
  142.             write(x + 5, y + 2, "Needs maintenance!", errorColor)
  143.         else
  144.             box(x + 1, y + 1, 2, 1, errorColor)
  145.             write(x + 5, y + 2, "Machine disabled!", errorColor)
  146.         end
  147.     end
  148. end
  149.  
  150. --Регистрация БатБуферов
  151. function substation(x, y, width, heigth)
  152.     local pss = gtBattBuf
  153.     updateEU(pss)
  154.     width = width or screenWidth - 2 * x; heigth = heigth or 4
  155.     GPU1.setBackground(0x000000)
  156.     local currentEU = string.gsub(pss.getSensorInformation()[3], "([^0-9]+)", "")
  157.     local maxEU = string.gsub(pss.getSensorInformation()[4], "([^0-9]+)", "")
  158.     local progress = (width - 2) * (currentEU / maxEU)
  159.     box(x, y, width, heigth, borderColor)
  160.     box(x + 1 + progress, y + 1, width - 1 - progress, heigth - 2, backgroundColor)
  161.     box(x + 1, y + 1, progress, heigth - 2, progressBarColor)
  162.     write(x, y - 1, splitNumber(currentEU), currentEUColor); screen.write(" EU")
  163.     write(x + width - #splitNumber(maxEU) - 3, y - 1, splitNumber(maxEU), maxEUColor); screen.write(" EU")
  164.     box(x + width / 2 - 3, y - 1, 8, 1, 0x000000)
  165.     local percentString = getPercent(progress / (width - 2))
  166.     write(x + width / 2 - #percentString / 2, y - 1, percentString)
  167.     box(x, y + heigth, width, 1, 0x000000)
  168.     if currentIO >= 0 then
  169.         local EUString = "+" .. splitNumber(currentIO)
  170.         write(x + width / 2 - #EUString / 2 - 2, y + heigth, EUString, positiveEUColor); screen.write(" EU/t")
  171.     else
  172.         local EUString = splitNumber(currentIO)
  173.         write(x + width / 2 - #EUString / 2 - 2, y + heigth, EUString, negativeEUColor); screen.write(" EU/t")
  174.     end
  175.     write(x + 18, y + heigth, string.gsub(pss.getSensorInformation()[6], "([^0-9]+)", ""), positiveEUColor); screen.write(" EU/t")
  176.     write(x, y + heigth, "Total Generation:");
  177.     write(x, y + heigth + 1, "Total Cost:");
  178.     write(x + 12, y + heigth + 1, string.gsub(pss.getSensorInformation()[8], "([^0-9]+)", ""), negativeEUColor); screen.write(" EU/t")
  179.     if currentIO > 0 then
  180.         fillTime = math.floor((maxEU - currentEU) / (currentIO * 20))
  181.         fillTimeString = time(math.abs(fillTime))
  182.         write(x + width - #fillTimeString - 6, y + heigth, "Full: " .. fillTimeString)
  183.     elseif currentIO == 0 then
  184.         if currentEU == maxEU then
  185.             write(x + width - 13, y + heigth, "Stable charge")
  186.         else
  187.             write(x + width - 11, y + heigth, "Need charge")
  188.         end
  189.     else
  190.         fillTime = math.floor((currentEU) / (currentIO * 20))
  191.         fillTimeString = time(math.abs(fillTime))
  192.         write(x + width - #fillTimeString - 7, y + heigth, "Empty: " .. fillTimeString)
  193.     end
  194.     if currentEU / maxEU <= 0.08 then
  195.         rs.setOutput(0, 15)
  196.         rs.setOutput(1, 15)
  197.         rs.setOutput(2, 15)
  198.         rs.setOutput(3, 15)
  199.         rs.setOutput(4, 15)
  200.         rs.setOutput(5, 15)
  201.     else if currentEU / maxEU >= 0.98 then
  202.         rs.setOutput(0, 0)
  203.         rs.setOutput(1, 0)
  204.         rs.setOutput(2, 0)
  205.         rs.setOutput(3, 0)
  206.         rs.setOutput(4, 0)
  207.         rs.setOutput(5, 0)
  208.     end
  209.     end
  210.     if rs.getOutput(0) == 0 then
  211.         box(x, y + heigth - 10, 2, 1, 0xff0000)
  212.         GPU1.set(x + 4, y + heigth - 10, "Energy Generation Disable")
  213.     else
  214.         box(x, y + heigth - 10, 2, 1, 0x00ff00)
  215.         GPU1.set(x + 4, y + heigth - 10, "Energy Generation Enable ")
  216.     end
  217. end
  218.  
  219. local fluidList = me_interface.getFluidsInNetwork()
  220. function fluidslistUpdate()
  221.     fluidList = me_interface.getFluidsInNetwork()
  222. end
  223.  
  224. -- Регистрация жидкостей
  225. function fluid(name, color, max, x, y)
  226.     local amountFLuid
  227.     local amount
  228.     local abort = true
  229.     for number, fluid in ipairs(fluidList) do
  230.         if fluid.label == name then
  231.             label = fluid.label
  232.             amountFLuid = tonumber(fluid.amount)
  233.             abort = false
  234.             break
  235.         end
  236.     end
  237.     if abort then return end -- Пропустить если нету жижи
  238.  
  239.     amount = math.floor(amountFLuid / 1000)
  240.     local width = 21
  241.     old = GPU1.setBackground(borderColor)
  242.     if (fluidNames[name] == nil) then
  243.         fluidNames[name] = 1
  244.         box(x, y, width + 2, 3, borderColor)
  245.         write(x + 1, y, label, color)
  246.         write(x + 16, y + 2, "L/1000", color)
  247.     else
  248.     end
  249.     local progress = 0
  250.     if (amount / max > 0.5) then
  251.         progress = math.min(math.ceil((amount / max) * width), width)
  252.     else
  253.         progress = math.min(math.floor((amount / max) * width), width)
  254.     end
  255.     local fillString = tostring(amount) .. "/" .. tostring(max)
  256.     GPU1.setBackground(color)
  257.     if (#fillString <= progress) then
  258.         write(x + 1, y + 1, fillString, backgroundColor)
  259.         box(x + 1 + #fillString, y + 1, progress - #fillString, 1, color)
  260.         box(x + 1 + progress, y + 1, width - progress, 1, backgroundColor)
  261.     else
  262.         write(x + 1, y + 1, fillString:sub(1, progress), backgroundColor)
  263.         GPU1.setBackground(backgroundColor)
  264.         write(x + 1 + progress, y + 1, fillString:sub(progress + 1, #fillString), color)
  265.         box(x + 1 + #fillString, y + 1, width - #fillString, 1, backgroundColor)
  266.     end
  267.     GPU1.setBackground(old)
  268. end
  269.  
  270. screen.clear()
  271.  
  272. --[[=================================================================================================================
  273.  
  274.     ДОБАВЛЕНИЕ СВОИХ КОМПОНЕНТОВ
  275.  
  276.     Критерии для работы:
  277.     1) Комп тир 3
  278.     2) Мониторы тир 3
  279.     3) GPU тир 3
  280.     4) CPU тир 3
  281.  
  282. ]]
  283.  
  284. while (true) do
  285.  
  286.     --[[=================================================================================================================
  287.  
  288.     Многоблоки
  289.     Как работает отображение машин? Вы подключаетесь адаптером (или адаптером с MFU, если к контролеру нельзя напрямую подключится)
  290.     к контролеру, задаете адресс адаптера в регистрацию и комп выводит инфу
  291.  
  292.  
  293.     Как правильно зарегать машины:
  294.     gtMachine(Координата X, Координата Y, Адресс Адаптера (или Адаптера с MFU), Название для вывода)
  295.  
  296.     Важно! Расположение бокса по координатам между машин должно быть минимум 4 по координате Y
  297.  
  298.     ]]
  299.  
  300.     gtMachine(1, 4, "4823a6f2", "Пример")
  301.     --gtMachine(1, 8, "9645a", "Пример2")
  302.  
  303.     --[[=================================================================================================================
  304.     Батбуфер
  305.     Как работает отображение батбуфера? Вы подключаетесь адаптером к любому батбуферу, комп ищет в сети батбуфер и выдает всю информацию
  306.     Обязательно нужно для подключения Redstone Port (RS Port) из OC, сигнал силой 15 выдается во все стороны
  307.  
  308.     Как правильно зарегать буфер:
  309.     substation(Координата X, Координата Y, Ширина(x = растянуть по всей ширине), Высота бара)
  310.  
  311.     ]]
  312.  
  313.     substation(31, 40, 100, 6)
  314.  
  315.     --[[=================================================================================================================
  316.     Жижи
  317.  
  318.     Как работает отображение жидкостей? Вы подключаетесь адаптером к обычному интерфейсу (не жидкостному!), комп сканирует
  319.     жидкости в МЕ, далее жидкость которую вы указали, он выведет на экран, если такой жидоксти нет, то ничего не выведет
  320.  
  321.     Как правильно зарегать жидкость:
  322.     fluid(Название для вывода, Цвет по HEX, Максимальный размер(не ограничен, но я указываю обычно 256 ведер, чтобы были для вида), Координата X, Координата Y)
  323.  
  324.     Размеры бокса 21x3
  325.  
  326.     ]]
  327.     fluidslistUpdate()
  328.     fluid("Nitrogen Gas", 0xe517b3, 256, screenWidth - 21, 4)
  329.     fluid("Oxygen Gas", 0x688790, 256, screenWidth - 21, 8)
  330.  
  331.     -- Выключить программу ctrl+c
  332.     os.sleep(0.1)
  333.     if event.pull(.5, "interrupted") then
  334.         screen.clear()
  335.         break
  336.     end
  337. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement