Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------------------------------------------------------
- -- Программа --
- -- Контроллер полужидкостных генераторов --
- -- для компьютеров из мода OpenComputers --
- -- проект http://computercraft.ru --
- -- Version 0.1 (Высокого класса) --
- -- 2019, © Asior --
- -------------------------------------------------------
- local c = require("component")
- local unicode = require('unicode')
- local com = require('computer')
- local tr = c.transposer
- local gpu = c.gpu
- tb = {3, 7,
- ['col'] = {'Название', 'Энергия/топливо', 'Батарея'},
- ['line'] = {{'Генератор 1', 'load', 'load'},
- {'Генератор 2', 'load', 'load'},
- {'Генератор 3', 'load', 'load'},
- {'Генератор 4', 'load', 'load'},
- {'Энергобуфер', 'load', 'load'},
- {'Контроллер', 'load', '-'}}}
- tbSetting = {}
- local function gpuTableSetting()
- tbSetting.handLine = false --равнение по краю 1 строки
- tbSetting.handCol = false --равнение по краю 1 стобца
- tbSetting.cent = true --общее равнение по центру
- tbSetting.acrosScreen = true --растяжение по ширине экрана
- if not tbSetting.acrosScreen then
- local maxLen, vr = 0
- for i = 1, tb[1] do
- if tostring(tb.col[i]) ~= 'nil' then
- vr = unicode.len(tostring(tb.col[i]))
- if vr > maxLen then
- maxLen = vr
- end
- end
- end
- for i = 1, tb[2]-1 do
- if tostring(tb.line[i][1]) ~= 'nil' then
- vr = unicode.len(tostring(tb.line[i][1]))
- if vr > maxLen then
- maxLen = vr
- end
- end
- end
- tbSetting.x = maxLen*tb[1]+tb[1]
- else
- tbSetting.x, tbSetting.y = gpu.getResolution()
- end
- tbSetting.pos = math.ceil(tbSetting.x/tb[1])
- settingCmd = {2,tb[2]+3,tbSetting.x-2,tbSetting.y-(tb[2]+3)}
- end
- local function gpuTable(tp) --1:отрисовка границы; пустота: отрисовка самой таблицы
- local vr, cent = ''
- if tp then
- gpu.set(1, 1, '╔')
- gpu.set(2, 1, string.rep('═', tbSetting.x-2))
- gpu.set(tbSetting.x, 1, '╗')
- gpu.set(2, tb[2]+2, string.rep('═', tbSetting.x-2))
- vr = 1
- for i=1, tb[1] do
- gpu.set(vr, 1, '╦')
- gpu.set(vr, tb[2]+2, '╩')
- vr = vr+tbSetting.pos
- end
- gpu.set(1, tb[2]+2, '╠')
- gpu.set(tbSetting.x, tb[2]+2, '╣')
- local x,y = gpu.getResolution()
- for i=tb[2]+3, y-1 do
- gpu.set(1, i, '║')
- gpu.set(tbSetting.x, i, '║')
- end
- gpu.set(2, y, string.rep('═', tbSetting.x-2))
- gpu.set(1, y, '╚')
- gpu.set(tbSetting.x, y, '╝')
- else
- for y = 1, tb[2] do
- vr = 1
- gpu.set(2, y+1, string.rep(' ', tbSetting.x-2)..'║')
- for i=1, tb[1] do
- if y == 1 then
- text = tostring(tb.col[i])
- else
- text = tostring(tb.line[y-1][i])
- end
- if unicode.len(text) > tbSetting.pos then
- text = unicode.sub(text, 1, tbSetting.pos-3)
- end
- cent = math.ceil(tbSetting.pos/2)-math.ceil(unicode.len(text)/2)
- if (i == 1 and tbSetting.handCol) or (y == 1 and tbSetting.handLine) then
- gpu.set(vr+1, y+1, text)
- else
- if tbSetting.cent then
- gpu.set(vr+cent+1, y+1, text)
- else
- gpu.set(vr+1, y+1, text)
- end
- end
- gpu.set(vr, y+1, '║')
- vr = vr+tbSetting.pos
- end
- end
- end
- end
- local function unFind(s,ch) --аналог strind.find() поиск в строке только 1 символа
- for i=1,unicode.len(s) do
- if unicode.sub(s,i,i)==ch then
- return i
- end
- end
- return 0
- end
- sms = {'Запуск генераторов ...'}
- local function printCmd(text) --статус строка
- gpu.fill(settingCmd[1], settingCmd[2], settingCmd[3], settingCmd[4], ' ')
- local flood = true --скрывает флуд в виде "[20] TEST" не использовать в начале скобки!
- local vr = ''
- if text ~= nil then
- text = tostring(text)
- if flood then
- if unicode.sub(sms[#sms],1,1) == '[' then
- vr = unFind(sms[#sms], ']')+2
- else
- vr = 1
- end
- if text == unicode.sub(sms[#sms],vr) then
- RP = RP+1
- sms[#sms] = '['..RP..'] '..unicode.sub(sms[#sms],vr)
- else
- sms[#sms+1] = text
- RP = 1
- end
- else
- sms[#sms+1] = text
- end
- end
- while #sms>settingCmd[4] do
- table.remove(sms, 1)
- end
- for i = 1, #sms do
- if sms[i] ~= nil then
- gpu.set(2, settingCmd[2]-1+i, tostring(unicode.sub(sms[i], 1, settingCmd[3])))
- end
- end
- os.sleep(0)
- end
- local function getInfoTank(size)
- vr = tr.getFluidInTank(size)
- if vr.n > 0 then
- return math.ceil(vr[1].amount*100/vr[1].capacity)..' %'
- else
- return '-'
- end
- end
- local function getInfoBat(size)
- if size == 1 then
- slot = 2
- else
- slot = 1
- end
- vr = tr.getStackInSlot(size,slot)
- if vr then
- return math.ceil(vr.charge*100/vr.maxCharge)..' %'
- else
- return '-'
- end
- end
- gpuTableSetting()
- gpuTable(true)
- printCmd('Анализ батареи ...')
- if c.isAvailable("mfsu") then
- bat=c.mfsu
- printCmd('Подключно МФСУ')
- elseif c.isAvailable("mfe") then
- bat=c.mfe
- printCmd('Подключно МФЭ')
- elseif c.isAvailable("cesu") then
- bat=c.cesu
- printCmd('Подключно МЭХ')
- elseif c.isAvailable("batbox") then
- bat=c.batbox
- printCmd('Подключно энергобуфер')
- else
- printCmd('Ошибка! Энергобуфер не найден!')
- os.exit()
- end
- gpuTable()
- while true do
- if bat.getStored()*100/bat.getCapacity() <= 98 then
- for i=2,5 do
- vr = tr.getStackInSlot(i,1)
- if vr and vr.charge >= vr.maxCharge then --если батарейка в генераторе и заряжена
- st = tr.transferItem(i,1,1,1,2) --переместить её в буфер
- if st then --если получилось переместить
- printCmd('Перемещение батареи в энергобуфер')
- break
- end
- end
- end
- end
- vr = tr.getStackInSlot(1,2)
- if vr and vr.charge == 0 then --если батарейка в буфере и она разряжена
- for i=2,5 do
- if tr.getStackInSlot(i,1) == nil then
- tr.transferItem(1,i,1,2,1) --переместить её в генератор
- printCmd('Перемещение батареи в генератор')
- break
- end
- end
- end
- tb = {3, 7,
- ['col'] = {'Название', 'Энергия/топливо', 'Батарея'},
- ['line'] = {{'Генератор 1', getInfoTank(2), getInfoBat(2)},
- {'Генератор 2', getInfoTank(3), getInfoBat(3)},
- {'Генератор 3', getInfoTank(4), getInfoBat(4)},
- {'Генератор 4', getInfoTank(5), getInfoBat(5)},
- {'Энергобуфер', math.ceil(bat.getStored()*100/bat.getCapacity())..' %', getInfoBat(1)},
- {'Контроллер', math.ceil(com.energy()*100/com.maxEnergy())..' %' , '-'}}}
- gpuTable()
- os.sleep(0.5)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement