matthew117

Force Core Control - matthew_117

Sep 27th, 2013
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.27 KB | None | 0 0
  1. cell0Periph = peripheral.wrap("redstone_energy_cell_0")
  2. cell1Periph = peripheral.wrap("redstone_energy_cell_1")
  3. cell2Periph = peripheral.wrap("redstone_energy_cell_2")
  4. cell3Periph = peripheral.wrap("redstone_energy_cell_3")
  5. cell4Periph = peripheral.wrap("redstone_energy_cell_4")
  6. cell5Periph = peripheral.wrap("redstone_energy_cell_5")
  7. cell6Periph = peripheral.wrap("redstone_energy_cell_6")
  8. cell7Periph = peripheral.wrap("redstone_energy_cell_7")
  9.  
  10. enginesOn = {1,1,1,1,1,1,1,1}
  11. energyPercentage = {0,0,0,0,0,0,0,0}
  12. rednetColors = {colors.white, colors.pink, colors.yellow, colors.magenta, colors.orange, colors.gray, colors.lime, colors.lightBlue}
  13.  
  14. function getProviders()
  15.   cell0 = cell0Periph.getPowerProvider()
  16.   cell1 = cell1Periph.getPowerProvider()
  17.   cell2 = cell2Periph.getPowerProvider()
  18.   cell3 = cell3Periph.getPowerProvider()
  19.   cell4 = cell4Periph.getPowerProvider()
  20.   cell5 = cell5Periph.getPowerProvider()
  21.   cell6 = cell6Periph.getPowerProvider()
  22.   cell7 = cell7Periph.getPowerProvider()
  23. end
  24.  
  25. function getEnergyLevels()
  26.   energyPercentage[1] = (cell0.energyStored / cell0.maxEnergyStored) * 100
  27.   energyPercentage[2] = (cell1.energyStored / cell1.maxEnergyStored) * 100
  28.   energyPercentage[3] = (cell2.energyStored / cell2.maxEnergyStored) * 100
  29.   energyPercentage[4] = (cell3.energyStored / cell3.maxEnergyStored) * 100
  30.   energyPercentage[5] = (cell4.energyStored / cell4.maxEnergyStored) * 100
  31.   energyPercentage[6] = (cell5.energyStored / cell5.maxEnergyStored) * 100
  32.   energyPercentage[7] = (cell6.energyStored / cell6.maxEnergyStored) * 100
  33.   energyPercentage[8] = (cell7.energyStored / cell7.maxEnergyStored) * 100
  34. end
  35.  
  36. function writeBarGraph(cellNum, value, state)
  37.    term.write("Cell "..cellNum..": [")
  38.    
  39.    w,h = term.getCursorPos()
  40.    term.write(string.rep(".",20).."]  "..state.."  "..math.floor(value))
  41.    
  42.    term.setCursorPos(w,h)
  43.    term.write(string.rep("*",value/5))
  44.  
  45. end
  46.  
  47. function writeStatus()
  48.   term.clear()
  49.  
  50.   term.setCursorPos(1,1)
  51.   writeBarGraph("0", energyPercentage[1], enginesOn[1])
  52.  
  53.   term.setCursorPos(1,2)
  54.   writeBarGraph("1", energyPercentage[2], enginesOn[2])
  55.  
  56.   term.setCursorPos(1,3)
  57.   writeBarGraph("2", energyPercentage[3], enginesOn[3])
  58.  
  59.   term.setCursorPos(1,4)
  60.   writeBarGraph("3", energyPercentage[4], enginesOn[4])
  61.  
  62.   term.setCursorPos(1,5)
  63.   writeBarGraph("4", energyPercentage[5], enginesOn[5])
  64.  
  65.   term.setCursorPos(1,6)
  66.   writeBarGraph("5", energyPercentage[6], enginesOn[6])
  67.  
  68.   term.setCursorPos(1,7)
  69.   writeBarGraph("6", energyPercentage[7], enginesOn[7])
  70.  
  71.   term.setCursorPos(1,8)
  72.   writeBarGraph("7", energyPercentage[8], enginesOn[8])
  73.  
  74. end
  75.  
  76. function setOutput()
  77.   curColors = 0
  78.  
  79.   if energyPercentage[1] < 50 then
  80.     enginesOn[1] = 1
  81.   elseif energyPercentage[1] > 90 then
  82.     enginesOn[1] = 0
  83.   end
  84.  
  85.   if energyPercentage[2] < 50 then
  86.     enginesOn[2] = 1
  87.   elseif energyPercentage[2] > 90 then
  88.     enginesOn[2] = 0
  89.   end
  90.  
  91.   if energyPercentage[3] < 50 then
  92.     enginesOn[3] = 1
  93.   elseif energyPercentage[3] > 90 then
  94.     enginesOn[3] = 0
  95.   end
  96.  
  97.   if energyPercentage[4] < 50 then
  98.     enginesOn[4] = 1
  99.   elseif energyPercentage[4] > 90 then
  100.     enginesOn[4] = 0
  101.   end
  102.  
  103.   if energyPercentage[5] < 50 then
  104.     enginesOn[5] = 1
  105.   elseif energyPercentage[5] > 90 then
  106.     enginesOn[5] = 0
  107.   end
  108.  
  109.   if energyPercentage[6] < 50 then
  110.     enginesOn[6] = 1
  111.   elseif energyPercentage[6] > 90 then
  112.     enginesOn[6] = 0
  113.   end
  114.  
  115.   if energyPercentage[7] < 50 then
  116.     enginesOn[7] = 1
  117.   elseif energyPercentage[7] > 90 then
  118.     enginesOn[7] = 0
  119.   end
  120.  
  121.   if energyPercentage[8] < 50 then
  122.     enginesOn[8] = 1
  123.   elseif energyPercentage[8] > 90 then
  124.     enginesOn[8] = 0
  125.   end
  126.  
  127.  
  128.   curColors = enginesOn[1] * rednetColors[1] + enginesOn[2] * rednetColors[2] + enginesOn[3] * rednetColors[3] + enginesOn[4] * rednetColors[4] + enginesOn[5] * rednetColors[5] + enginesOn[6] * rednetColors[6]+ enginesOn[7] * rednetColors[7] + enginesOn[8] * rednetColors[8]
  129.   rs.setBundledOutput("right", curColors)
  130. end
  131.  
  132. function findMonitor()
  133.   for i = 0,10 do
  134.     local monitor = peripheral.wrap("monitor_" .. i)
  135.     if monitor then return monitor end
  136.   end
  137.   return nil
  138. end
  139.  
  140. function getTextColorForBackground(color)
  141.   if (color == colors.white) then return colors.black
  142.   elseif (color == colors.orange) then return colors.black
  143.   elseif (color == colors.magenta) then return colors.black
  144.   elseif (color == colors.lightBlue) then return colors.black
  145.   elseif (color == colors.yellow) then return colors.black
  146.   elseif (color == colors.lime) then return colors.black
  147.   elseif (color == colors.pink) then return colors.black
  148.   elseif (color == colors.gray) then return colors.white
  149.   elseif (color == colors.lightGray) then return colors.black
  150.   elseif (color == colors.cyan) then return colors.black
  151.   elseif (color == colors.purple) then return colors.white
  152.   elseif (color == colors.blue) then return colors.white
  153.   elseif (color == colors.brown) then return colors.white
  154.   elseif (color == colors.green) then return colors.black
  155.   elseif (color == colors.red) then return colors.black
  156.   elseif (color == colors.black) then return colors.white
  157.   else return colors.black
  158.   end
  159. end
  160.  
  161. function paintSquare(x, y, w, h, monitor, color)
  162.   monitor.setBackgroundColor(color)
  163.   for i=x,x+w-1 do
  164.     for j=y,y+h-1 do
  165.       monitor.setCursorPos(i,j)
  166.       monitor.write(" ")
  167.     end
  168.   end
  169.   monitor.setBackgroundColor(colors.black)
  170.   monitor.setTextColor(colors.white)
  171. end
  172.  
  173. function renderGUI()
  174.   local monitor = findMonitor() -- can be replaced with local monitor = peripheral.wrap("monitor_#") if you're certain of the ID
  175.   if monitor then
  176.    
  177.     monitor.clear()
  178.     local w, h = monitor.getSize()
  179.    
  180.     blockSize = math.floor((w - 4) / 8)
  181.    
  182.     for i=0,7 do
  183.      
  184.       -- display program title
  185.       monitor.setBackgroundColor(colors.gray)
  186.       monitor.setCursorPos(1, 1)
  187.       monitor.write(" Force Core Status" .. string.rep(" ", w - 10))
  188.       monitor.setBackgroundColor(colors.black)
  189.      
  190.       -- display cell IDs and the rednet color related to them
  191.       monitor.setCursorPos(4+((i+1)*blockSize)-2, h-3)
  192.       monitor.setBackgroundColor(rednetColors[i+1])
  193.       monitor.setTextColor(getTextColorForBackground(rednetColors[i+1]))
  194.       monitor.write(" " .. tostring(i) .. " ")
  195.      
  196.       -- display engine status
  197.       if (enginesOn[i+1] == 1) then
  198.         paintSquare(4+((i+1)*blockSize)-1, h-1, 1, 1, monitor, colors.lime)
  199.       else
  200.         paintSquare(4+((i+1)*blockSize)-1, h-1, 1, 1, monitor, colors.red)
  201.       end
  202.      
  203.       -- display power levels as bars
  204.       local powerLevel = math.floor(energyPercentage[i+1] / 10)
  205.       local spaceAvailable = 10 - powerLevel
  206.      
  207.       paintSquare(4+((i+1)*blockSize)-2, h-14, 3, spaceAvailable, monitor, colors.gray)
  208.       if (energyPercentage[i+1] > 90) then paintSquare(4+((i+1)*blockSize)-2, h-14+spaceAvailable, 3, powerLevel, monitor, colors.cyan)
  209.       elseif (energyPercentage[i+1] > 75) then paintSquare(4+((i+1)*blockSize)-2, h-14+spaceAvailable, 3, powerLevel, monitor, colors.lime)
  210.       elseif (energyPercentage[i+1] > 50) then paintSquare(4+((i+1)*blockSize)-2, h-14+spaceAvailable, 3, powerLevel, monitor, colors.yellow)
  211.       elseif (energyPercentage[i+1] > 25) then paintSquare(4+((i+1)*blockSize)-2, h-14+spaceAvailable, 3, powerLevel, monitor, colors.orange)
  212.       elseif (energyPercentage[i+1] > 0) then paintSquare(4+((i+1)*blockSize)-2, h-14+spaceAvailable, 3, powerLevel, monitor, colors.red)
  213.       end
  214.      
  215.       -- display power levels as text
  216.       if energyPercentage[i+1] >= 1 then
  217.         local lengthOfLabel = math.floor(math.log10(energyPercentage[i+1]))
  218.         monitor.setCursorPos(4+((i+1)*blockSize)-lengthOfLabel, h-16)
  219.         monitor.write(tostring(math.floor(energyPercentage[i+1])))
  220.       else
  221.         monitor.setCursorPos(4+((i+1)*blockSize), h-16)
  222.         monitor.write("0")
  223.       end
  224.      
  225.     end
  226.    
  227.   else
  228.     local ox, oy = term.getCursorPos()
  229.     term.setCursorPos(1,10)
  230.     term.setTextColor(colors.red)
  231.     term.write("No Monitor Connected")
  232.     term.setTextColor(colors.white)
  233.     term.setCursorPos(ox, oy)
  234.   end
  235. end
  236.  
  237. while true do
  238.  
  239.   getProviders()
  240.  
  241.   getEnergyLevels()
  242.  
  243.   writeStatus()
  244.  
  245.   setOutput()
  246.  
  247.   renderGUI()
  248.    
  249.   sleep(2)
  250.  
  251. end
Advertisement
Add Comment
Please, Sign In to add comment