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 res_X = 82
- local res_Y = 26
- --#глобальные перменные
- 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 program_key = " Ii1lLl1lL1Ii711lL1lLl1lL1l1lL1lLl1lL1ВIiIi71lLl1lL1ы7IiIi 7IiI1lLl1lL1iн7аIiIi7р7уIi1lLl1lL1Ii7ш7и7л71lLl1lL1и7IiIi1lLl1lL1 IiIi7IiIiа7вIiIi7т7IiIi1lLl1lL1Ii711Iiо71lLl1l1lLl1lL1L1рIi1lLl1lL11lLl1lL1Ii7с7к7IiIi1lLl1lL1и7Ii1lLl1lL1Ii711е7 7пIiI1lLl1lL1i7р71lLl1lL1аIiIi7в7аIIi1lLl1lL1Ii711iIi7 7п7рIiIiIi1lLl1lL1Ii7117о7г1lLl1lL1IiIi7р7Ii1lLl1lL1Iiа7м1lLl1lL17мIiI1lLl1lL1i7ы7.IiIi 7П1lLl1lL17ИIiIi71lLl1lL1ДIiIi7О7IiIiР1lLl1lL17IiIiА7СIiIi1lLl1lL11lLl1lL17!7"
- --#переменне системы
- local version = "[1.3]"
- local red_back = 0xFA8072
- local blue_back = 0x1E90FF
- --#Функция рисования лого
- function draw_logo()
- gpu.setForeground(0x224363)
- gpu.set(23,2," ■■■ ■ ■")
- gpu.set(23,3,"■ ■ ■ ■ ■■ ■ ■ ■ ■ ■ ■■ ■")
- gpu.set(23,4,"■■ ■ ■ ■ ■ ■■ ■ ■■■ ■■ ■ ■ ■ ■")
- gpu.set(23,5,"■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■")
- gpu.set(23,6,"■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■")
- gpu.set(23,7,"■ ■■■ ■■ ■ ■ ■ ■ ■■ ■")
- end
- --#Процедура выхода из программы
- function soft_exit()
- gpu.setBackground(0x000000)
- gpu.setForeground(0xFFFFFF)
- gpu.setResolution(160,50)
- term.clear()
- print("[rControl] Программа завершена пользователем.")
- os.exit()
- end
- --#Процедура установки
- function install()
- -- Установка: ШАГ#1
- screen = "install"
- gpu.setResolution(res_X, res_Y)
- gpu.setBackground(0xB0C4DE) -- белый слегка серый цвет фона (общий)
- gpu.fill(0,0,res_X + 1, res_Y + 1, " ")
- --Установка: РСУЕМ лого
- draw_logo()
- --Установка: РИСУЕМ форму
- gpu.setBackground(0x808080) -- серый цвет
- gpu.fill(15,10,54,15," ")
- --Установка: ПИШЕМ ПРИВЕТСТВИЕ
- gpu.setForeground(0xFCCD64) -- слегка желтый цвет
- gpu.set(18,11,"Вас приветствует мастер установки rControl "..version)
- --Установка: ПИШЕМ ПРИДУПРЕЖДЕНИЕ
- gpu.setForeground(0xFF741F) -- оранжевый цвет
- gpu.set(20,13,"ВНИМАНИЕ! В данной программе возможна работа")
- gpu.set(24,14,"только с классическим RF реактором.")
- --Установка: РИСУЕМ кнопку "Продолжить"
- gpu.setForeground(0xFCCD64) -- слегка желтый цвет
- gpu.setBackground(blue_back)
- gpu.fill(17,21,24,3," ")
- gpu.set(24,22,"Продолжить")
- --Установка: РИСУЕМ кнопку "Выход"
- gpu.setBackground(red_back)
- gpu.fill(43,21,24,3," ")
- gpu.set(52,22, "Выход")
- --Установка: ЛОВИМ КЛИКИ по кнопкам
- local event_running = true
- while event_running do
- local name, address, x, y, button, player = event.pull("touch")
- if x >= 17 and x <= 41 and y >=21 and y <= 23then
- event_running = false
- elseif x>=43 and x <= 67 and y >=21 and y <= 23 then
- soft_exit()
- end
- end
- --Установка: ПЕРЕХОДИМ к выбору реактора
- install_pick_reactor()
- --Установка: СОХРАНЯЕМ цветовую схему
- set_color_scheme()
- --Установка: СОХРАНЯЕМ изменения в конфиг
- save_config()
- --Установка: ЧИСТИМ первично экран
- gpu.setBackground(0xB0C4DE)
- gpu.fill(0,0,res_X + 1, res_Y + 1, " ")
- --Установка: ЗАПУСКАЕМ главную функцию
- main()
- end
- function key_encode(str)
- local result = str
- os.sleep(0,1)
- result = result:gsub("7", "")
- os.sleep(0,1)
- result = result:gsub("i", "")
- os.sleep(0,1)
- result = result:gsub("I", "")
- os.sleep(0.1)
- result = result:gsub("l", "")
- os.sleep(0.1)
- result = result:gsub("L", "")
- os.sleep(0.1)
- result = result:gsub("1", "")
- os.sleep(0.1)
- return result
- 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
- local sys1, sys2, sys3 = gpu.get(75,26)
- if sys1 ~= "4" then
- gpu.setBackground(0xB0C4DE)
- gpu.fill(0,0,res_X + 1, res_Y + 1, " ")
- draw_error(key_encode(program_key))
- running = false
- 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(37,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(10,0)
- 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
- --#прорисовка футера
- local reactor_active_last = true
- local reactor_auto_power_last = false
- function draw_futter()
- local reactor_active = reactor.getActive()
- local reactor_auto_power = config.auto_power.enabled
- local pixel, pixel1, pixel2 = gpu.get(1,26)
- if reactor_active_last == reactor_active and reactor_auto_power_last == reactor_auto_power and pixel == "С" then
- else
- reactor_active_last = reactor_active
- reactor_auto_power_last = reactor_auto_power
- 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
- 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(0xB0C4DE)
- gpu.fill(0,0,res_X + 1, res_Y + 1, " ")
- --Выбор реактора: рисуем лого
- draw_logo()
- --Выбор реактора: рисуем форму
- 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 = 42-len/2
- local s_y = 15-round(countTable(reactors)/2)
- gpu.fill(s_x-1,s_y-2,len+2,countTable(reactors)+3," ")
- gpu.setForeground(0xFF741F)
- gpu.set(s_x+7, s_y-2, "Выбери нужный реактор")
- local i = s_y
- gpu.setForeground(0xFCCD64)
- for k,v in pairs(reactors) do
- gpu.set(s_x,i,k)
- i=i+1
- end
- local event_running = true
- while event_running do
- local name, address, x, y, button, player = event.pull("touch")
- 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)
- 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
- --#Начало работы софта
- term.clear()
- if not fs.exists("/home/rControl.cfg") then
- install()
- else
- gpu.setBackground(0xB0C4DE) -- белый слегка серый цвет фона (общий)
- gpu.fill(0,0,res_X + 1, res_Y + 1, " ")
- main()
- end
- function event.shouldInterrupt()
- return false
- end
- event.ignore("touch",listen)
- soft_exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement