Advertisement
nO_OnE_910

Computercraft Energy Cell GUI

Sep 30th, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.75 KB | None | 0 0
  1. local monitor = peripheral.wrap("monitor_1")
  2. local sender, message
  3. rednet.open("right")
  4.  
  5. monitor.setTextScale(1)--0.75)
  6. sizeX, sizeY = monitor.getSize()
  7.  
  8. function tablelength(T)
  9.     local count = 0
  10.     for i in pairs(T) do count = count + 1 end
  11.     return count
  12. end
  13. function rearrange(data)
  14.     local temp = {}
  15.     temp[1] = data[6]
  16.     temp[2] = data[5]
  17.     temp[3] = data[4]
  18.     temp[4] = data[3]
  19.     temp[5] = data[2]
  20.     temp[6] = data[1]
  21.     temp[7] = data[10]
  22.     temp[8] = data[9]
  23.     temp[9] = data[8]
  24.     temp[10] = data[7]
  25.     return temp
  26. end
  27. function getData(data, value, index)
  28.     if not index then
  29.         local temp = 0
  30.         for i, j in pairs(data) do
  31.             temp = temp + (j[value] or 0)
  32.         end
  33.         return temp
  34.     end
  35.     return data[index][value]
  36. end
  37. function getOutput(data, m, n, val)
  38.     local temp = 0
  39.     for i = m, n do
  40.         if getData(data, "stored", i) > val then temp = temp + val end
  41.     end
  42.     return temp
  43. end
  44. function drawLine(x1, x2, y, color)
  45.     preX, preY = monitor.getCursorPos()
  46.     monitor.setCursorPos(x1, y)
  47.     monitor.setBackgroundColor(color)
  48.     monitor.write(" ")
  49.     if x2 > x1 then monitor.write(string.rep(" ", x2 - x1)) end
  50.     monitor.setCursorPos(preX, preY)
  51.     monitor.setBackgroundColor(colors.black)
  52. end
  53. function getColor(state)
  54.     if state > .75 then return colors.green end
  55.     if state > .60 then return colors.lime end
  56.     if state > .30 then return colors.yellow end
  57.     if state > .15 then return colors.orange end
  58.     return colors.red
  59. end
  60. function round(num, idp)
  61.     local mult = 10^(idp or 0)
  62.     return math.floor(num * mult + 0.5) / mult
  63. end
  64. function drawBar(minx, miny, maxx, maxy, state, color, frame)
  65.     color = color or getColor(state)
  66.     frame = frame or colors.gray
  67.     for i = miny, maxy do
  68.         drawLine(minx, maxx, i, frame)
  69.     end
  70.     miny = miny + 1
  71.     maxy = maxy - 1
  72.     minx = minx + 1
  73.     maxx = maxx - 1
  74.     local l = miny + math.floor((1.0 - state) * (maxy - miny))
  75.     if l > miny then
  76.         for i = miny, l - 1 do
  77.             drawLine(minx, maxx, i, colors.black)
  78.         end
  79.     end
  80.     if state == 0 then return nil end
  81.     for y = maxy, l, -1 do
  82.         drawLine(minx, maxx, y, color)
  83.     end
  84. end
  85. function form(rf)
  86.     local suf = " RF"
  87.     if rf > 1000 then
  88.         rf = round(rf / 1000, 2)
  89.         suf = " kRF"
  90.     end
  91.     if rf > 1000 then
  92.         rf = round(rf / 1000, 2)
  93.         suf = " mRF"
  94.     end
  95.     return tostring(rf) .. suf
  96. end
  97. local data = 100
  98. local i = -5
  99. monitor.clear()
  100. monitor.setCursorPos(2, sizeY - 3)
  101. monitor.write("Storage")
  102. monitor.setCursorPos(2, sizeY - 6)
  103. monitor.write("Output")
  104. monitor.setCursorPos(2, sizeY - 9)
  105. monitor.write("Capacity")
  106.  
  107. while true do
  108.     repeat
  109.         repeat
  110.             sender, message = rednet.receive("nOBatteryStatus", 5)
  111.         until message ~= nil
  112.         data = textutils.unserialize(message)
  113.     until tablelength(data) == 10
  114.     data = rearrange(data)
  115.     local stored = getData(data, "stored")
  116.     local maximum = getData(data, "max")
  117.     local out = getOutput(data, 1, 5, 2000)
  118.     drawBar(sizeX / 2 + 1, 2, sizeX - 1, sizeY - 1, stored / maximum)
  119.     monitor.setCursorPos(1, sizeY - 1)
  120.     monitor.write(string.rep(" ", math.floor(sizeX / 2)))
  121.     monitor.setCursorPos(1, sizeY - 2)
  122.     monitor.write(string.rep(" ", math.floor(sizeX / 2)))
  123.     monitor.setCursorPos(1, sizeY - 5)
  124.     monitor.write(string.rep(" ", math.floor(sizeX / 2)))
  125.     monitor.setCursorPos(1, sizeY - 8)
  126.     monitor.write(string.rep(" ", math.floor(sizeX / 2)))
  127.     monitor.setCursorPos(2, sizeY - 1)
  128.     monitor.write("("..form(stored)..")")
  129.     monitor.setCursorPos(2, sizeY - 2)
  130.     monitor.write(tostring(round(stored / maximum * 100, 2)).." %")
  131.     monitor.setCursorPos(2, sizeY - 5)
  132.     monitor.write(tostring(out).." RF/t")
  133.     monitor.setCursorPos(2, sizeY - 8)
  134.     monitor.write(form(maximum))
  135.     local send = {}
  136.     send["idt"] = "Energy"
  137.     send["idn"] = 1
  138.     send["stored"] = stored
  139.     send["max"] = maximum
  140.     send["out"] = out
  141.     send = textutils.serialize(send)
  142.     rednet.broadcast(send, "nOStatusBroadcast")
  143. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement