Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --#Библиотеки
- local fs = require("filesystem")
- local term = require("term")
- local serialization = require("serialization")
- local component = require("component")
- local event = require("event")
- local colors = require("colors")
- --#глобальные перменные
- local gpu = component.gpu
- local config = {}
- local reactor = nil
- local running = true
- local screen = "main"
- local max_speed = "N.A"
- local reason = "неопознаная ошибка"
- local reactor_error = "Реактор не найден!"
- --#переменне системы
- local version = "[1.2]"
- --#Процедура установки
- function install()
- screen = "install"
- term.clear()
- print("Вас привествует мастер установки rControl "..version)
- print("")
- print("Требывания:")
- print("Дисплей - не менее 2уровня")
- print("Видеокарта - не менее 2 уровня")
- print("Ваш реактор должен быть подключен к компьютеру через специальный порт")
- print("Клавиатура [Опционально]")
- print("")
- print("Начать установку rControl? [да - y, нет - n]")
- --21,49
- local result = false
- while not result do
- local name, adress, char, code, player = event.pull("key_down")
- if code == 21 then
- result = true
- elseif code == 49 then
- os.exit()
- else
- print("Некорректный ввод. Возможно включен CapsLock")
- end
- end
- --set resolution and continue
- gpu.setResolution(80,25)
- gpu.setForeground(0x000000)
- term.clear()
- gpu.setBackground(0xF8F8FF) -- белый слегка серый цвет фона (общий)
- term.clear()
- gpu.setBackground(0x808080) -- серый цвет
- gpu.fill(20,9,40,6," ")
- term.setCursor(25,9)
- print("Спасибо что выбрали")
- term.setCursor(25,10)
- print("rControl")
- term.setCursor(25,11)
- print("Нажми ok чтоб продолжить")
- term.setCursor(25,12)
- print("Нажми отмена чтоб выйти")
- gpu.setBackground(0x008000)
- gpu.fill(20,14,20,1," ")
- term.setCursor(29,14)
- print("ok")
- gpu.setBackground(0x800000)
- gpu.fill(40,14,20,1," ")
- term.setCursor(48,14)
- print("отмена")
- local event_running = true
- while event_running do
- local name, address, x, y, button, player = event.pull("touch")
- if x >= 20 and x <= 39 and y == 14 then
- print("ok")
- event_running = false
- elseif x>=40 and x <= 59 and y == 14 then
- os.exit()
- end
- end
- install_pick_reactor()
- set_color_scheme()
- save_config()
- main()
- end
- --#главная функция
- function main()
- screen = "main"
- running = true
- gpu.setResolution(82,26)
- read_config()
- reactor_error = "Реактор ["..config.reactor.."] не найден!"
- reactor = component.proxy(config.reactor)
- event.listen("touch",listen)
- while running do
- gpu.setBackground(0xB0C4DE) -- главная прорисовка фона (общего)
- -- попытка прорисовать футер.. ловим ошибки
- status_futter, err = pcall(draw_futter)
- if not status_futter then
- --draw_error(reactor_error)
- running = false
- reactor_search()
- break
- end
- ---------------------------------------------
- -- попытка нарисовать меню-бар.. ловим ошибки
- status_menu_bar, err = pcall(draw_menubar)
- if not status_menu_bar then
- --draw_error(reactor_error)
- running = false
- reactor_search()
- break
- end
- if screen == "main" then
- status_main, err = pcall(draw_main)
- if not status_main then
- draw_error(reactor_error)
- running = false
- reactor_search()
- break
- end
- elseif screen == "config" then
- draw_config()
- elseif screen == "error" then
- draw_error(reason)
- end
- os.sleep(.05)
- end
- end
- function reactor_search()
- gpu.setBackground(0xB0C4DE)
- term.clear()
- search_timer = true
- while search_timer do
- reactor = component.proxy(config.reactor)
- if reactor == nil then
- draw_error(reactor_error)
- else
- gpu.setBackground(0xB0C4DE)
- term.clear()
- screen = "main"
- main()
- search_timer = false
- end
- end
- end
- function draw_error(str)
- head = 0x8A0808 -- темно красный (шапка)
- body = 0xF6CECE -- светло красный (тело)
- head_text = 0xFFFFFF -- белый (цвет текста)
- body_text = 0x000000 -- черный (цвет причины)
- gpu.setBackground(head)
- gpu.fill(12,8,60,3," ")
- gpu.setForeground(head_text)
- term.setCursor(36,9)
- term.write("Внимание!")
- gpu.setBackground(body)
- gpu.fill(12,11,60,6," ")
- gpu.setForeground(body_text)
- term.setCursor(13,14)
- term.write(str)
- os.sleep(0,2)
- end
- --#прорисовка верхнего меню
- function draw_menubar()
- gpu.setResolution(82,26)
- term.setCursor(1,1)
- gpu.setBackground(config.color_scheme.menubar.background)
- gpu.setForeground(config.color_scheme.menubar.foreground)
- term.clearLine()
- term.setCursor(1,1)
- term.write("Напряжение: ")
- gpu.setForeground(config.color_scheme.info)
- if config.max_speed == nil then
- config.max_speed = 0
- max_speed = 0
- save_config()
- end
- if round(reactor.getEnergyProducedLastTick()) > config.max_speed then
- config.max_speed = round(reactor.getEnergyProducedLastTick())
- save_config()
- end
- max_speed = config.max_speed
- term.write(round(reactor.getEnergyProducedLastTick()) .. "/" .. max_speed .. "RF/t")
- gpu.setForeground(config.color_scheme.menubar.foreground)
- term.write(" Темп. стержней: ")
- gpu.setForeground(config.color_scheme.info)
- term.write(round(reactor.getFuelTemperature()).."C ")
- gpu.setForeground(config.color_scheme.menubar.foreground)
- term.write(" Темп. корпуса: ")
- gpu.setForeground(config.color_scheme.info)
- term.write(round(reactor.getCasingTemperature()).."C ")
- term.setCursor(76,1)
- gpu.setForeground(config.color_scheme.menubar.foreground)
- term.write("[")
- gpu.setForeground(config.color_scheme.error)
- term.write("Выход")
- gpu.setForeground(config.color_scheme.menubar.foreground)
- term.write("]")
- end
- --#прорисовка футера
- function draw_futter()
- gpu.setBackground(config.color_scheme.menubar.background)
- gpu.setForeground(config.color_scheme.menubar.foreground)
- term.setCursor(1,26)
- term.clearLine()
- term.write("Статус: ")
- if reactor.getActive() then
- gpu.setForeground(config.color_scheme.success)
- term.write("В работе ")
- else
- gpu.setForeground(config.color_scheme.error)
- term.write("Отключен ")
- end
- if config.auto_power.enabled then
- gpu.setForeground(config.color_scheme.menubar.foreground)
- term.write("(")
- gpu.setForeground(config.color_scheme.info)
- term.write("Автомат")
- gpu.setForeground(config.color_scheme.menubar.foreground)
- term.write(") ")
- end
- term.setCursor(58,26)
- gpu.setForeground(config.color_scheme.info)
- term.write("rControl" .. version)
- gpu.setForeground(config.color_scheme.error)
- term.write(" (с)4epB9Ik")
- end
- --#сохранение конфига
- function save_config()
- local file = io.open("/home/rControl.cfg","w")
- file:write(serialization.serialize(config,false))
- file:close()
- end
- --#считывание конфига
- function read_config()
- local file = io.open("/home/rControl.cfg","r")
- local c = serialization.unserialize(file:read(fs.size("/home/rControl.cfg")))
- file:close()
- for k,v in pairs(c) do
- config[k] = v
- end
- end
- --#установка цветовой схемы
- function set_color_scheme()
- config.color_scheme = {}
- config.color_scheme.background = 0xF8F8FF
- config.color_scheme.button = 0x606060
- config.color_scheme.button_disabled = 0xC0C0C0
- config.color_scheme.foreground = 0x000000
- config.color_scheme.progressBar = {}
- config.color_scheme.progressBar.background = 0x008080
- config.color_scheme.progressBar.foreground = 0xB22222
- config.color_scheme.menubar={}
- config.color_scheme.menubar.background = 0x000000
- config.color_scheme.menubar.foreground = 0xFFFFFF
- config.color_scheme.success = 0x008000
- config.color_scheme.error = 0x800000
- config.color_scheme.info = 0x808000
- config.auto_power = {}
- config.auto_power.enabled = false
- config.auto_power.start_percent = 15
- config.auto_power.stop_percent = 80
- end
- --#выбор реактора при установке
- function install_pick_reactor()
- gpu.setBackground(0xF8F8FF)-- общий фон
- term.clear()
- gpu.setBackground(0x808080)
- local reactors = component.list("br_reactor")
- local len = 3
- for k,v in pairs(reactors) do
- if len<#k then
- len = #k
- end
- end
- local s_x = 40-len/2
- local s_y = 13-round(countTable(reactors)/2)
- gpu.fill(s_x-1,s_y-2,len+2,countTable(reactors)+3," ")
- term.setCursor(s_x+7,s_y-2)
- print("Выбери нужный реактор")
- local i = s_y
- for k,v in pairs(reactors) do
- term.setCursor(s_x,i)
- print(k)
- i=i+1
- end
- local event_running = true
- while event_running do
- local name, address, x, y, button, player = event.pull("touch")
- print(y-s_y)
- if x>=s_x and x <= s_x+len and y>=s_y and y<= s_y+countTable(reactors) then
- event_running = false
- local i = y-s_y
- for k,v in pairs(reactors) do
- if i == 0 then
- config.reactor = k
- end
- i=i-1
- end
- end
- end
- end
- --#прорисовка главного экрана
- function draw_main()
- if config.auto_power.enabled == true then
- if reactor.getEnergyStored()/10^5<config.auto_power.start_percent then
- reactor.setActive(true)
- elseif reactor.getEnergyStored()/10^5>config.auto_power.stop_percent then
- reactor.setActive(false)
- end
- end
- if config.auto_power.enabled then
- gpu.setBackground(config.color_scheme.button)
- gpu.setForeground(0xFFFFFF)
- gpu.fill(1,2,71,3," ")
- term.setCursor(25,3)
- term.write("Выкл. автоматический режим")
- gpu.setBackground(0x153F3F)
- gpu.fill(72,2,11,3," ")
- term.setCursor(73,3)
- term.write("Настроить")
- else
- gpu.setBackground(config.color_scheme.button)
- gpu.setForeground(0xFFFFFF)
- gpu.fill(1,2,71,3," ")
- term.setCursor(25,3)
- term.write("Вкл. автоматический режим")
- gpu.setBackground(0x153F3F)
- gpu.fill(72,2,11,3," ")
- term.setCursor(74,3)
- term.write("Питание")
- end
- gpu.setForeground(0xFFFFFF)
- gpu.setBackground(config.color_scheme.button)
- gpu.fill(1,6,13,3," ")
- gpu.fill(1,10,13,3," ")
- gpu.fill(1,14,13,3," ")
- gpu.fill(1,18,13,3," ")
- -- отрисовка пункта "энергоблок"
- term.setCursor(2,6)
- term.write("Энерго-блок")
- term.setCursor(4,7)
- term.write("[ mRF ]")
- term.setCursor(2,8)
- reactor_storage = calculate(reactor.getEnergyStored()/1000000,2)
- rsalt_1, rsalt_2 = math.modf(reactor_storage)
- if rsalt_2 == 0 then
- term.write(reactor_storage ..",00/10,00")
- else
- term.write(calculate(reactor_storage,2) .. "/10,00")
- end
- -- отрисовка пункта "топливо"
- term.setCursor(2,10)
- term.write("Топлив-блок")
- term.setCursor(3,11)
- term.write("[ buck ]")
- reactor_fuel_max = reactor.getFuelAmountMax() / 1000
- reactor_fuel_curent = calculate(reactor.getFuelAmount() / 1000, 2)
- term.setCursor(2,12)
- term.write(reactor_fuel_curent.."/"..reactor_fuel_max..".00")
- -- отрисовка пункта "радиоктивность топлива"
- term.setCursor(2,15)
- term.write("Рад.топлива")
- -- отрисовка пункта "мощность"
- term.setCursor(3,19)
- term.write("Мощность")
- drawProgressBar(14,6,67,3,reactor.getEnergyStored()/10^7)
- drawProgressBar(14,10,67,3,reactor.getFuelAmount()/reactor.getFuelAmountMax())
- react = 50
- if reactor.getFuelReactivity() > 100 then
- react = 100
- else
- react = reactor.getFuelReactivity()
- end
- drawProgressBar(14,14,67,3,react/10^2)
- if config.auto_power.enabled then
- gpu.setBackground(config.color_scheme.success)
- gpu.fill(14+67*config.auto_power.start_percent/10^2,6,1,3," ")
- gpu.setBackground(config.color_scheme.error)
- gpu.fill(14+67*config.auto_power.stop_percent/10^2,6,1,3," ")
- end
- -- power_percent = 100-reactor.getControlRodLevel()
- power_percent = 0
- for i = 0, reactor.getNumberOfControlRods()-1 do
- power_percent = power_percent+(100/reactor.getNumberOfControlRods()/100*(100-reactor.getControlRodLevel(i)))
- end
- drawProgressBar(14,18,67,3,power_percent/10^2)
- draw_buttons()
- end
- --#Функция округления чисел
- function calculate(num,rows)
- local mul = math.pow(10,rows)
- return (math.floor((num*mul)+0.5)/mul)
- end
- --#Прорисовка кнопок с низу
- function draw_buttons()
- gpu.setForeground(0xB22222)
- gpu.setBackground(0xB0C4De)
- term.setCursor(24,21)
- term.write("(..функции в процессе разработки..)")
- term.setCursor(4,23)
- gpu.setForeground(0xFFFFFF)
- gpu.setBackground(config.color_scheme.button)
- term.write("[Регулировка мощности]")
- term.setCursor(56,23)
- term.write("[Прием/выгрузка топлива]")
- end
- --#прорисовка экрана конфигурации
- function draw_config()
- gpu.setBackground(config.color_scheme.button)
- gpu.fill(5,9,71,9," ")
- gpu.setForeground(0xFFFFFF)
- term.setCursor(36,9)
- term.write("Конфигурация")
- term.setCursor(33,10)
- term.write("Старт: "..config.auto_power.start_percent.."%")
- term.setCursor(34,11)
- term.write("Стоп: "..config.auto_power.stop_percent.."%")
- drawProgressBar(8,12,65,3,reactor.getEnergyStored()/10^7)
- gpu.setBackground(config.color_scheme.success)
- gpu.fill(8+65*config.auto_power.start_percent/100,12,1,3," ")
- gpu.setBackground(config.color_scheme.error)
- gpu.fill(8+65*config.auto_power.stop_percent/100,12,1,3," ")
- gpu.setBackground(config.color_scheme.button)
- gpu.setForeground(0xFFFFFF)
- term.setCursor(32+#("Старт: "..config.auto_power.start_percent.."%"),10)
- term.write("[")
- gpu.setForeground(config.color_scheme.error)
- term.write("-")
- gpu.setForeground(0xFFFFFF)
- term.write("] [")
- gpu.setForeground(config.color_scheme.success)
- term.write("+")
- gpu.setForeground(0xFFFFFF)
- term.write("]")
- term.setCursor(38+#("Stop: "..config.auto_power.stop_percent.."#"),11)
- term.write("[")
- gpu.setForeground(config.color_scheme.error)
- term.write("-")
- gpu.setForeground(0xFFFFFF)
- term.write("] [")
- gpu.setForeground(config.color_scheme.success)
- term.write("+")
- gpu.setForeground(0xFFFFFF)
- term.write("]")
- term.setCursor(5,9)
- term.write("[")
- gpu.setForeground(config.color_scheme.info)
- term.write("Назад")
- gpu.setForeground(0xFFFFFF)
- term.write("]")
- end
- --#прорисовка прогресс баров
- function drawProgressBar(x,y,w,h,percent)
- red_back = 0xFA8072
- blue_back = 0x1E90FF
- percent_new = calculate(percent*w,0)
- gpu.setBackground(red_back) -- (красный - нижний слой прогрес бара)
- gpu.fill(x,y,w,h," ")
- gpu.setBackground(blue_back) -- (зеленый - верхний слой прогрес бара)
- gpu.fill(x,y,percent_new,h," ")
- percent_new = calculate(percent*100,0)
- salt = string.len(percent_new .. "%")
- per_pos_x = (w-x)/2+x+6
- -- term.setCursor(per_pos_x,y+1)
- if percent_new <=9 then
- per_pos_x = per_pos_x - 1
- elseif percent_new <= 99 then
- per_pos_x = per_pos_x - 1
- elseif percent_new == 100 then
- per_pos_x = per_pos_x - 2
- end
- term.setCursor(per_pos_x + 1, y+1)
- if percent_new < 49 then
- gpu.setBackground(red_back)
- term.write(percent_new .. "%")
- elseif percent_new == 49 then
- gpu.setBackground(blue_back)
- term.write("4")
- gpu.setBackground(red_back)
- term.write("9%")
- elseif percent_new == 50 then
- gpu.setBackground(blue_back)
- term.write("50")
- gpu.setBackground(red_back)
- term.write("%")
- elseif percent_new == 51 then
- gpu.setBackground(blue_back)
- term.write("51%")
- elseif percent_new <= 100 then
- gpu.setBackground(blue_back)
- term.write(percent_new .. "%")
- end
- end
- --#слушатель кликов
- function listen(name,address,x,y,button,player)
- if x >= 76 and x <= 80 and y == 1 then
- running = false
- end
- if screen == "main" then
- if x >= 72 and y >=2 and x <= 80 and y <= 4 and config.auto_power.enabled ~= true then
- reactor.setActive(not reactor.getActive())
- elseif x >= 1 and y >=2 and x <= 71 and y <= 4 then
- config.auto_power.enabled = not config.auto_power.enabled
- save_config()
- elseif x >= 72 and y >= 2 and x <= 80 and y <= 4 and config.auto_power.enabled then
- gpu.setBackground(0xB0C4DE)
- term.clear()
- screen = "config"
- end
- elseif screen=="config" then
- if x>= 5 and x <= 10 and y == 9 then
- gpu.setBackground(0xB0C4DE)
- term.clear()
- screen="main"
- elseif x >= 34 + #("Пуск: "..config.auto_power.start_percent.."%") and x <= 35+#("Пуск: "..config.auto_power.start_percent.."%") and y == 10 and config.auto_power.start_percent ~= 0 then
- config.auto_power.start_percent = config.auto_power.start_percent-1
- save_config()
- elseif x >= 39 + #("Пуск: "..config.auto_power.start_percent.."%") and x <= 40+#("Пуск: "..config.auto_power.start_percent.."%") and y == 10 and config.auto_power.start_percent+1 ~= config.auto_power.stop_percent then
- config.auto_power.start_percent = config.auto_power.start_percent+1
- save_config()
- elseif x >= 34 + #("Стоп: "..config.auto_power.stop_percent.."%") and x <= 35 + #("Стоп: "..config.auto_power.stop_percent.."%") and y == 11 and config.auto_power.stop_percent - 1 ~= config.auto_power.start_percent then
- config.auto_power.stop_percent = config.auto_power.stop_percent - 1
- save_config()
- elseif x >= 39 + #("Стоп: "..config.auto_power.stop_percent.."%") and x <= 40 + #("Стоп: "..config.auto_power.stop_percent.."%") and y == 11 and config.auto_power.stop_percent ~= 100 then
- config.auto_power.stop_percent = config.auto_power.stop_percent + 1
- save_config()
- end
- end
- end
- --#просчет таблиц
- function countTable(table)
- local result = 0
- for k,v in pairs(table) do
- result = result+1
- end
- return result
- end
- --#round
- function round(num,idp)
- local mult = 10^(idp or 0)
- return math.floor(num*mult+0.5)/mult
- end
- --#Начало работы софта
- gpu.setBackground(0xB0C4DE)
- term.clear()
- if not fs.exists("/home/rControl.cfg") then
- install()
- else
- main()
- end
- function event.shouldInterrupt()
- return false
- end
- function soft_exit()
- gpu.setBackground(0x000000)
- gpu.setForeground(0xFFFFFF)
- gpu.setResolution(160,50)
- term.clear()
- end
- event.ignore("touch",listen)
- soft_exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement