Advertisement
Asioron

generator_control

May 18th, 2019
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.50 KB | None | 0 0
  1. -------------------------------------------------------
  2. --                     Программа                     --
  3. --        Контроллер полужидкостных генераторов      --
  4. --        для компьютеров из мода OpenComputers      --
  5. --          проект http://computercraft.ru           --
  6. --          Version 0.1 (Высокого класса)            --
  7. --                  2019, © Asior                    --
  8. -------------------------------------------------------
  9.  
  10. local c = require("component")
  11. local unicode = require('unicode')
  12. local com = require('computer')
  13. local tr = c.transposer
  14. local gpu = c.gpu
  15.  
  16. tb = {3, 7,
  17. ['col'] =   {'Название', 'Энергия/топливо', 'Батарея'},
  18. ['line'] = {{'Генератор 1', 'load', 'load'},
  19.             {'Генератор 2', 'load', 'load'},
  20.             {'Генератор 3', 'load', 'load'},
  21.             {'Генератор 4', 'load', 'load'},
  22.             {'Энергобуфер', 'load', 'load'},
  23.             {'Контроллер', 'load', '-'}}}
  24. tbSetting = {}
  25. local function gpuTableSetting()
  26.   tbSetting.handLine = false --равнение по краю 1 строки
  27.   tbSetting.handCol = false --равнение по краю 1 стобца
  28.   tbSetting.cent = true --общее равнение по центру
  29.   tbSetting.acrosScreen = true --растяжение по ширине экрана
  30.   if not tbSetting.acrosScreen then
  31.     local maxLen, vr = 0
  32.     for i = 1, tb[1] do
  33.       if tostring(tb.col[i]) ~= 'nil' then
  34.         vr = unicode.len(tostring(tb.col[i]))
  35.         if vr > maxLen then
  36.           maxLen = vr
  37.         end
  38.       end
  39.     end
  40.     for i = 1, tb[2]-1 do
  41.       if tostring(tb.line[i][1]) ~= 'nil' then
  42.         vr = unicode.len(tostring(tb.line[i][1]))
  43.         if vr > maxLen then
  44.           maxLen = vr
  45.         end
  46.       end
  47.     end
  48.     tbSetting.x = maxLen*tb[1]+tb[1]
  49.   else
  50.     tbSetting.x, tbSetting.y = gpu.getResolution()
  51.   end
  52.   tbSetting.pos = math.ceil(tbSetting.x/tb[1])
  53.   settingCmd = {2,tb[2]+3,tbSetting.x-2,tbSetting.y-(tb[2]+3)}
  54. end
  55.  
  56. local function gpuTable(tp) --1:отрисовка границы; пустота: отрисовка самой таблицы
  57.   local vr, cent = ''
  58.   if tp then
  59.     gpu.set(1, 1, '╔')
  60.     gpu.set(2, 1, string.rep('═', tbSetting.x-2))
  61.     gpu.set(tbSetting.x, 1, '╗')
  62.     gpu.set(2, tb[2]+2, string.rep('═', tbSetting.x-2))
  63.     vr = 1
  64.     for i=1, tb[1] do
  65.       gpu.set(vr, 1, '╦')
  66.       gpu.set(vr, tb[2]+2, '╩')
  67.       vr = vr+tbSetting.pos
  68.     end
  69.     gpu.set(1, tb[2]+2, '╠')
  70.     gpu.set(tbSetting.x, tb[2]+2, '╣')
  71.     local x,y = gpu.getResolution()
  72.     for i=tb[2]+3, y-1 do
  73.       gpu.set(1, i, '║')
  74.       gpu.set(tbSetting.x, i, '║')
  75.     end
  76.     gpu.set(2, y, string.rep('═', tbSetting.x-2))
  77.     gpu.set(1, y, '╚')
  78.     gpu.set(tbSetting.x, y, '╝')
  79.   else
  80.     for y = 1, tb[2] do
  81.       vr = 1
  82.       gpu.set(2, y+1, string.rep(' ', tbSetting.x-2)..'║')
  83.       for i=1, tb[1] do
  84.         if y == 1 then
  85.           text = tostring(tb.col[i])
  86.         else
  87.           text = tostring(tb.line[y-1][i])
  88.         end
  89.         if unicode.len(text) > tbSetting.pos then
  90.           text = unicode.sub(text, 1, tbSetting.pos-3)
  91.         end
  92.         cent = math.ceil(tbSetting.pos/2)-math.ceil(unicode.len(text)/2)
  93.         if (i == 1 and tbSetting.handCol) or (y == 1 and tbSetting.handLine) then
  94.           gpu.set(vr+1, y+1, text)
  95.         else
  96.           if tbSetting.cent then
  97.             gpu.set(vr+cent+1, y+1, text)
  98.           else
  99.             gpu.set(vr+1, y+1, text)
  100.           end
  101.         end
  102.         gpu.set(vr, y+1, '║')
  103.         vr = vr+tbSetting.pos
  104.       end
  105.     end
  106.   end
  107. end
  108.  
  109. local function unFind(s,ch) --аналог strind.find() поиск в строке только 1 символа
  110.   for i=1,unicode.len(s) do
  111.     if unicode.sub(s,i,i)==ch then
  112.       return i
  113.     end
  114.   end
  115.   return 0
  116. end
  117.  
  118. sms = {'Запуск генераторов ...'}
  119. local function printCmd(text) --статус строка
  120.   gpu.fill(settingCmd[1], settingCmd[2], settingCmd[3], settingCmd[4], ' ')
  121.   local flood = true --скрывает флуд в виде "[20] TEST" не использовать в начале скобки!
  122.   local vr = ''
  123.   if text ~= nil then
  124.     text = tostring(text)
  125.     if flood then
  126.       if unicode.sub(sms[#sms],1,1) == '[' then
  127.         vr = unFind(sms[#sms], ']')+2
  128.       else
  129.         vr = 1
  130.       end
  131.       if text == unicode.sub(sms[#sms],vr) then
  132.         RP = RP+1
  133.         sms[#sms] = '['..RP..'] '..unicode.sub(sms[#sms],vr)
  134.       else
  135.         sms[#sms+1] = text
  136.         RP = 1
  137.       end
  138.     else
  139.       sms[#sms+1] = text
  140.     end
  141.   end
  142.   while #sms>settingCmd[4] do
  143.     table.remove(sms, 1)
  144.   end
  145.   for i = 1, #sms do
  146.     if sms[i] ~= nil then
  147.       gpu.set(2, settingCmd[2]-1+i, tostring(unicode.sub(sms[i], 1, settingCmd[3])))
  148.     end
  149.   end
  150.   os.sleep(0)
  151. end
  152.  
  153. local function getInfoTank(size)
  154.   vr = tr.getFluidInTank(size)
  155.   if vr.n > 0 then
  156.     return math.ceil(vr[1].amount*100/vr[1].capacity)..' %'
  157.   else
  158.     return '-'
  159.   end
  160. end
  161.  
  162. local function getInfoBat(size)
  163.   if size == 1 then
  164.     slot = 2
  165.   else
  166.     slot = 1
  167.   end
  168.   vr = tr.getStackInSlot(size,slot)
  169.   if vr then
  170.     return math.ceil(vr.charge*100/vr.maxCharge)..' %'
  171.   else
  172.     return '-'
  173.   end
  174. end
  175.  
  176. gpuTableSetting()
  177. gpuTable(true)
  178. printCmd('Анализ батареи ...')
  179. if c.isAvailable("mfsu") then
  180.   bat=c.mfsu
  181.   printCmd('Подключно МФСУ')
  182. elseif c.isAvailable("mfe") then
  183.   bat=c.mfe
  184.   printCmd('Подключно МФЭ')
  185. elseif c.isAvailable("cesu") then
  186.   bat=c.cesu
  187.   printCmd('Подключно МЭХ')
  188. elseif c.isAvailable("batbox") then
  189.   bat=c.batbox
  190.   printCmd('Подключно энергобуфер')
  191. else
  192.   printCmd('Ошибка! Энергобуфер не найден!')
  193.   os.exit()
  194. end
  195.  
  196. gpuTable()
  197. while true do
  198.   if bat.getStored()*100/bat.getCapacity() <= 98 then
  199.     for i=2,5 do
  200.       vr = tr.getStackInSlot(i,1)
  201.       if vr and vr.charge >= vr.maxCharge then --если батарейка в генераторе и заряжена
  202.         st = tr.transferItem(i,1,1,1,2) --переместить её в буфер
  203.         if st then --если получилось переместить
  204.           printCmd('Перемещение батареи в энергобуфер')
  205.           break
  206.         end
  207.       end
  208.     end
  209.   end
  210.   vr = tr.getStackInSlot(1,2)
  211.   if vr and vr.charge == 0 then --если батарейка в буфере и она разряжена
  212.     for i=2,5 do
  213.       if tr.getStackInSlot(i,1) == nil then
  214.         tr.transferItem(1,i,1,2,1) --переместить её в генератор
  215.         printCmd('Перемещение батареи в генератор')
  216.         break
  217.       end
  218.     end
  219.   end
  220.   tb = {3, 7,
  221.   ['col'] =   {'Название', 'Энергия/топливо', 'Батарея'},
  222.   ['line'] = {{'Генератор 1', getInfoTank(2), getInfoBat(2)},
  223.             {'Генератор 2', getInfoTank(3), getInfoBat(3)},
  224.             {'Генератор 3', getInfoTank(4), getInfoBat(4)},
  225.             {'Генератор 4', getInfoTank(5), getInfoBat(5)},
  226.             {'Энергобуфер', math.ceil(bat.getStored()*100/bat.getCapacity())..' %', getInfoBat(1)},
  227.             {'Контроллер', math.ceil(com.energy()*100/com.maxEnergy())..' %' , '-'}}}
  228.   gpuTable()
  229.   os.sleep(0.5)
  230. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement