Taruu

SmartDiselGen

Jun 12th, 2020
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.95 KB | None | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3. local colors = require("colors")
  4. local text = require("text")
  5. local gpu = component.gpu
  6. local w, h = gpu.getResolution()
  7.  
  8. --список ячеек
  9. list_all_cell = {
  10.     {
  11.         "d84e5c05-5fba-4fea-b523-df103932002e",
  12.         "aea80191-5961-486a-a175-5b3b1e70895f",
  13.         "38c29be3-9d12-48d2-9147-09eac04bd3ad"
  14.     },
  15.     {
  16.         "e8938c89-fd36-4d4c-b13a-414043937d2d",
  17.         "cdc87287-8ebc-4a5b-85ed-fb6630cd540c",
  18.         "c591cd76-0558-48cb-b7ea-2aeab05f3498"
  19.     },
  20.     {
  21.         "b6ff451b-5eb8-4fb8-bf60-e1ff8d2f2b6d",
  22.         "85e3e65f-0a14-4e34-983f-facb613dc823",
  23.         "6fe772a2-d5b2-45af-ab70-34a68786135d"
  24.     },
  25.     {
  26.         "c8f421d6-a31f-4c08-9bdd-286f6505080d",
  27.         "a43d2e3d-ba3a-4eb9-a588-5f02e7d96fe3",
  28.         "ac6969c0-9cd1-4356-ab2e-6ca6cf3c1c21"
  29.     }
  30. }
  31. --Контроль генераторов
  32. list_all_disel_generator = {
  33.     "984e503d-983d-45d2-bc18-1b33ff169166",
  34.     "d0fd57a5-dae2-4b8d-ad17-8a2b84218806",
  35.     "d447e55e-763c-4696-b5f9-a04bbe2eb851"
  36. }
  37.  
  38. --Список всех входов
  39. list_all_tanks = {
  40.     "aa1429dc-8a92-4c00-8dc0-499f69c1abeb",
  41.     "4c513a7b-369a-4b02-88a3-3347769f3897",
  42.     "09e11c94-5e1e-4d8b-ac58-807d565e46d2",
  43.     "5f6ea351-d2f0-4634-a55c-317bda930345"
  44. }
  45.  
  46. --Case для танкеров
  47. case_tank = {
  48.     [0] = "0-6%",
  49.     [1] = "7%-13%",
  50.     [2] = "14%-18%",
  51.     [3] = "19%-24%",
  52.     [4] = "25%-31%",
  53.     [5] = "32%-37%",
  54.     [6] = "38%-44%",
  55.     [7] = "45%-51%",
  56.     [8] = "52%-55%",
  57.     [9] = "56%-64%",
  58.     [10] = "65%-71%",
  59.     [11] = "72%-77%",
  60.     [12] = "78%-83%",
  61.     [13] = "84%-91%",
  62.     [14] = "92%-99%",
  63.     [15] = "100%"
  64. }
  65.  
  66. --макс энергии в ячейке
  67. max_energy_in_cell = 50000000
  68. --значение для запуска движка
  69. min_average = 20000000
  70. --Автоматический режим?
  71. AutoMode = false
  72. --Сторона от редстоун контроллера
  73. local side = 3
  74.  
  75. --Pass это pass
  76. function pass()
  77. end
  78.  
  79. --Копипаст.... Ну кто не без греха
  80. function format_int(number)
  81.     local i, j, minus, int, fraction = tostring(number):find("([-]?)(%d+)([.]?%d*)")
  82.  
  83.     -- reverse the int-string and append a comma to all blocks of 3 digits
  84.     int = int:reverse():gsub("(%d%d%d)", "%1,")
  85.  
  86.     -- reverse the int-string back remove an optional comma and put the
  87.     -- optional minus and fractional part back
  88.     return minus .. int:reverse():gsub("^,", "") .. fraction
  89. end
  90.  
  91. --Округление
  92. function floor_to_step(what, step)
  93.     return math.floor(what / step) * step
  94. end
  95.  
  96. --Состояние резервуара
  97. function str_tank(in_count)
  98.     return case_tank[math.floor(in_count)]
  99. end
  100.  
  101. --Сумма
  102. function sum(list)
  103.     result = 0
  104.     for item in ipairs(list) do
  105.         result = result + list[item].getEnergyStored()
  106.     end
  107.     return result
  108. end
  109.  
  110. --Вырубаем все!
  111. function stop_all_gen()
  112.     for id in ipairs(list_all_disel_generator) do
  113.         list_all_disel_generator[id].setEnabled(false)
  114.     end
  115. end
  116. --Стартуем
  117. function start_all_gen()
  118.     for id in ipairs(list_all_disel_generator) do
  119.         list_all_disel_generator[id].setEnabled(true)
  120.     end
  121. end
  122.  
  123. --Контроль генераторов
  124. function on_off_generator()
  125.     local all_energy = 0
  126.     for id in ipairs(list_all_cell) do -- Обход всех ячеек
  127.         local temp_sum = sum(list_all_cell[id])
  128.         all_energy = all_energy + temp_sum
  129.         local average_cell = temp_sum / #list_all_cell[id] -- Странно что ее мало кто упоминает....
  130.         local generator = list_all_disel_generator[id]
  131.         if generator ~= nil then
  132.             local generator_status = generator.isActive()
  133.             if average_cell < min_average and AutoMode then
  134.                 if generator_status then
  135.                     pass()
  136.                 else
  137.                     generator.setEnabled(true)
  138.                 end
  139.             elseif average_cell >= max_energy_in_cell - 120 and AutoMode then
  140.                 if generator_status == false then
  141.                     pass()
  142.                 else
  143.                     generator.setEnabled(false)
  144.                 end
  145.             else
  146.                 pass()
  147.             end
  148.             gpu.set(42, id + 3, " Статус ген." .. tostring(generator.isActive()) .. "   ")
  149.         end
  150.         gpu.set(3, id + 3, "Ячейка номер " .. id .. " RF:" .. format_int(floor_to_step(average_cell, 0.01)))
  151.         gpu.set(63, id + 3, "Танкер " .. id .. ":" .. str_tank(list_all_tanks[id].getInput(side)) .. "      ")
  152.     end
  153.     gpu.set(3, 1, "Всего RF:" .. format_int(floor_to_step(all_energy, 0.01)))
  154. end
  155.  
  156. function clearScreen() -- очистка экранна
  157.     gpu.setBackground(0x2D2D2D)
  158.     gpu.fill(1, 1, w, h, " ")
  159. end
  160.  
  161. function drawbuttons() -- отрисовка кнопок
  162.     gpu.setBackground(0x0066cc)
  163.     gpu.set(70, 24, "  Exit  ")
  164.     gpu.set(62, 24, "StopAll")
  165.     if AutoMode then
  166.         gpu.setBackground(0x006600)
  167.         gpu.set(53, 24, "AutoMode")
  168.         gpu.setBackground(0x0066cc)
  169.     else
  170.         gpu.set(53, 24, "AutoMode")
  171.     end
  172.     gpu.set(44, 24, "StartAll")
  173.  
  174.     gpu.setBackground(0x2D2D2D)
  175. end
  176.  
  177. function button_handler(x, y) --Обработка кнопок
  178.     gpu.fill(1, 24, 17, 25, " ")
  179.     if y == 24 and 43 < x and x < 52 then
  180.         gpu.set(2, 25, "Start All")
  181.         start_all_gen()
  182.         AutoMode = true
  183.     elseif y == 24 and 52 < x and x < 61 then
  184.         gpu.set(2, 25, "AutoMode")
  185.         if AutoMode ~= true then
  186.             AutoMode = true
  187.             gpu.set(2, 25, "AutoMode on")
  188.         else
  189.             AutoMode = false
  190.             gpu.set(2, 25, "AutoMode off")
  191.         end
  192.     elseif y == 24 and 61 < x and x < 69 then
  193.         AutoMode = false
  194.         stop_all_gen()
  195.         gpu.set(2, 25, "StopAll")
  196.     elseif y == 24 and 69 < x and x < 78 then
  197.         gpu.setBackground(0x000000)
  198.         gpu.fill(1, 1, w, h, " ")
  199.         os.exit()
  200.         gpu.set(2, 25, "Exit")
  201.     end
  202.     drawbuttons()
  203. end
  204. --Готовим экран
  205. clearScreen()
  206. drawbuttons()
  207.  
  208. --Прогон всех компонентов
  209. gpu.set(2, 25, "config cells")
  210.  
  211. for col_three in ipairs(list_all_cell) do
  212.     for row_item in ipairs(list_all_cell[col_three]) do
  213.         temp_str_id = list_all_cell[col_three][row_item]
  214.         list_all_cell[col_three][row_item] = component.proxy(temp_str_id)
  215.     end
  216. end
  217. gpu.set(2, 25, "config generator")
  218.  
  219. for col_three in ipairs(list_all_disel_generator) do
  220.     temp_str_id = list_all_disel_generator[col_three]
  221.     list_all_disel_generator[col_three] = component.proxy(temp_str_id)
  222.     list_all_disel_generator[col_three].enableComputerControl(true)
  223.     list_all_disel_generator[col_three].setEnabled(false)
  224. end
  225.  
  226. gpu.set(2, 25, "config tanks")
  227.  
  228. for id in ipairs(list_all_tanks) do
  229.     temp_str_id = list_all_tanks[id]
  230.     list_all_tanks[id] = component.proxy(component.get(temp_str_id))
  231. end
  232.  
  233. -- Первый запуск для отрисовки
  234. on_off_generator()
  235.  
  236. while true do
  237.     local id, _, x, y = event.pull(5, "touch") --это не очень решение но мне хватило...
  238.     gpu.fill(1, 24, 17, 25, " ")
  239.     if id == "touch" then
  240.         --gpu.set(2,25,x.." "..y)
  241.         button_handler(x, y)
  242.     else
  243.         if AutoMode then
  244.             gpu.set(2, 25, "update")
  245.             on_off_generator()
  246.         else
  247.             on_off_generator()
  248.             gpu.set(2, 25, "All stop!")
  249.         end
  250.     end
  251. end
Add Comment
Please, Sign In to add comment