Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local event = require("event")
- local colors = require("colors")
- local text = require("text")
- local gpu = component.gpu
- local w, h = gpu.getResolution()
- --список ячеек
- list_all_cell = {
- {
- "d84e5c05-5fba-4fea-b523-df103932002e",
- "aea80191-5961-486a-a175-5b3b1e70895f",
- "38c29be3-9d12-48d2-9147-09eac04bd3ad"
- },
- {
- "e8938c89-fd36-4d4c-b13a-414043937d2d",
- "cdc87287-8ebc-4a5b-85ed-fb6630cd540c",
- "c591cd76-0558-48cb-b7ea-2aeab05f3498"
- },
- {
- "b6ff451b-5eb8-4fb8-bf60-e1ff8d2f2b6d",
- "85e3e65f-0a14-4e34-983f-facb613dc823",
- "6fe772a2-d5b2-45af-ab70-34a68786135d"
- },
- {
- "c8f421d6-a31f-4c08-9bdd-286f6505080d",
- "a43d2e3d-ba3a-4eb9-a588-5f02e7d96fe3",
- "ac6969c0-9cd1-4356-ab2e-6ca6cf3c1c21"
- }
- }
- --Контроль генераторов
- list_all_disel_generator = {
- "984e503d-983d-45d2-bc18-1b33ff169166",
- "d0fd57a5-dae2-4b8d-ad17-8a2b84218806",
- "d447e55e-763c-4696-b5f9-a04bbe2eb851"
- }
- --Список всех входов
- list_all_tanks = {
- "aa1429dc-8a92-4c00-8dc0-499f69c1abeb",
- "4c513a7b-369a-4b02-88a3-3347769f3897",
- "09e11c94-5e1e-4d8b-ac58-807d565e46d2",
- "5f6ea351-d2f0-4634-a55c-317bda930345"
- }
- --Case для танкеров
- case_tank = {
- [0] = "0-6%",
- [1] = "7%-13%",
- [2] = "14%-18%",
- [3] = "19%-24%",
- [4] = "25%-31%",
- [5] = "32%-37%",
- [6] = "38%-44%",
- [7] = "45%-51%",
- [8] = "52%-55%",
- [9] = "56%-64%",
- [10] = "65%-71%",
- [11] = "72%-77%",
- [12] = "78%-83%",
- [13] = "84%-91%",
- [14] = "92%-99%",
- [15] = "100%"
- }
- --макс энергии в ячейке
- max_energy_in_cell = 50000000
- --значение для запуска движка
- min_average = 20000000
- --Автоматический режим?
- AutoMode = false
- --Сторона от редстоун контроллера
- local side = 3
- --Pass это pass
- function pass()
- end
- --Копипаст.... Ну кто не без греха
- function format_int(number)
- local i, j, minus, int, fraction = tostring(number):find("([-]?)(%d+)([.]?%d*)")
- -- reverse the int-string and append a comma to all blocks of 3 digits
- int = int:reverse():gsub("(%d%d%d)", "%1,")
- -- reverse the int-string back remove an optional comma and put the
- -- optional minus and fractional part back
- return minus .. int:reverse():gsub("^,", "") .. fraction
- end
- --Округление
- function floor_to_step(what, step)
- return math.floor(what / step) * step
- end
- --Состояние резервуара
- function str_tank(in_count)
- return case_tank[math.floor(in_count)]
- end
- --Сумма
- function sum(list)
- result = 0
- for item in ipairs(list) do
- result = result + list[item].getEnergyStored()
- end
- return result
- end
- --Вырубаем все!
- function stop_all_gen()
- for id in ipairs(list_all_disel_generator) do
- list_all_disel_generator[id].setEnabled(false)
- end
- end
- --Стартуем
- function start_all_gen()
- for id in ipairs(list_all_disel_generator) do
- list_all_disel_generator[id].setEnabled(true)
- end
- end
- --Контроль генераторов
- function on_off_generator()
- local all_energy = 0
- for id in ipairs(list_all_cell) do -- Обход всех ячеек
- local temp_sum = sum(list_all_cell[id])
- all_energy = all_energy + temp_sum
- local average_cell = temp_sum / #list_all_cell[id] -- Странно что ее мало кто упоминает....
- local generator = list_all_disel_generator[id]
- if generator ~= nil then
- local generator_status = generator.isActive()
- if average_cell < min_average and AutoMode then
- if generator_status then
- pass()
- else
- generator.setEnabled(true)
- end
- elseif average_cell >= max_energy_in_cell - 120 and AutoMode then
- if generator_status == false then
- pass()
- else
- generator.setEnabled(false)
- end
- else
- pass()
- end
- gpu.set(42, id + 3, " Статус ген." .. tostring(generator.isActive()) .. " ")
- end
- gpu.set(3, id + 3, "Ячейка номер " .. id .. " RF:" .. format_int(floor_to_step(average_cell, 0.01)))
- gpu.set(63, id + 3, "Танкер " .. id .. ":" .. str_tank(list_all_tanks[id].getInput(side)) .. " ")
- end
- gpu.set(3, 1, "Всего RF:" .. format_int(floor_to_step(all_energy, 0.01)))
- end
- function clearScreen() -- очистка экранна
- gpu.setBackground(0x2D2D2D)
- gpu.fill(1, 1, w, h, " ")
- end
- function drawbuttons() -- отрисовка кнопок
- gpu.setBackground(0x0066cc)
- gpu.set(70, 24, " Exit ")
- gpu.set(62, 24, "StopAll")
- if AutoMode then
- gpu.setBackground(0x006600)
- gpu.set(53, 24, "AutoMode")
- gpu.setBackground(0x0066cc)
- else
- gpu.set(53, 24, "AutoMode")
- end
- gpu.set(44, 24, "StartAll")
- gpu.setBackground(0x2D2D2D)
- end
- function button_handler(x, y) --Обработка кнопок
- gpu.fill(1, 24, 17, 25, " ")
- if y == 24 and 43 < x and x < 52 then
- gpu.set(2, 25, "Start All")
- start_all_gen()
- AutoMode = true
- elseif y == 24 and 52 < x and x < 61 then
- gpu.set(2, 25, "AutoMode")
- if AutoMode ~= true then
- AutoMode = true
- gpu.set(2, 25, "AutoMode on")
- else
- AutoMode = false
- gpu.set(2, 25, "AutoMode off")
- end
- elseif y == 24 and 61 < x and x < 69 then
- AutoMode = false
- stop_all_gen()
- gpu.set(2, 25, "StopAll")
- elseif y == 24 and 69 < x and x < 78 then
- gpu.setBackground(0x000000)
- gpu.fill(1, 1, w, h, " ")
- os.exit()
- gpu.set(2, 25, "Exit")
- end
- drawbuttons()
- end
- --Готовим экран
- clearScreen()
- drawbuttons()
- --Прогон всех компонентов
- gpu.set(2, 25, "config cells")
- for col_three in ipairs(list_all_cell) do
- for row_item in ipairs(list_all_cell[col_three]) do
- temp_str_id = list_all_cell[col_three][row_item]
- list_all_cell[col_three][row_item] = component.proxy(temp_str_id)
- end
- end
- gpu.set(2, 25, "config generator")
- for col_three in ipairs(list_all_disel_generator) do
- temp_str_id = list_all_disel_generator[col_three]
- list_all_disel_generator[col_three] = component.proxy(temp_str_id)
- list_all_disel_generator[col_three].enableComputerControl(true)
- list_all_disel_generator[col_three].setEnabled(false)
- end
- gpu.set(2, 25, "config tanks")
- for id in ipairs(list_all_tanks) do
- temp_str_id = list_all_tanks[id]
- list_all_tanks[id] = component.proxy(component.get(temp_str_id))
- end
- -- Первый запуск для отрисовки
- on_off_generator()
- while true do
- local id, _, x, y = event.pull(5, "touch") --это не очень решение но мне хватило...
- gpu.fill(1, 24, 17, 25, " ")
- if id == "touch" then
- --gpu.set(2,25,x.." "..y)
- button_handler(x, y)
- else
- if AutoMode then
- gpu.set(2, 25, "update")
- on_off_generator()
- else
- on_off_generator()
- gpu.set(2, 25, "All stop!")
- end
- end
- end
Add Comment
Please, Sign In to add comment