Advertisement
MajorVictory

Badlands Base Main Power Control

Sep 16th, 2013
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.35 KB | None | 0 0
  1.  
  2. local width = 60
  3. local refreshrate = 0.1
  4.  
  5. local bridge = peripheral.wrap("left")
  6. local net = peripheral.wrap("right")
  7.  
  8. local gainColor = 0x00DD00
  9. local steadyColor = 0x000000
  10. local lossColor = 0xDD0000
  11.  
  12. bridge.clear()
  13.  
  14.  
  15. local storageUnits = {
  16.     { ["id"] = "", ["name"] = "Total: 0%", ["lastValue"] = 0 },
  17.     { ["id"] = "batbox_6", ["name"] = "#1", ["lastValue"] = 0 },
  18.     { ["id"] = "batbox_7", ["name"] = "#2", ["lastValue"] = 0 },
  19.     { ["id"] = "batbox_8", ["name"] = "#3", ["lastValue"] = 0 },
  20.     { ["id"] = "batbox_9", ["name"] = "#4", ["lastValue"] = 0 },
  21.     { ["id"] = "batbox_10", ["name"] = "#5", ["lastValue"] = 0 },
  22.     { ["id"] = "batbox_11", ["name"] = "#6", ["lastValue"] = 0 },
  23.     { ["id"] = "batbox_12", ["name"] = "#7", ["lastValue"] = 0 },
  24. }
  25.  
  26. local offset = 7
  27. for key, storageUnit in pairs(storageUnits) do
  28.     pxOffset = (offset * 10) + 5
  29.     storageUnit["label"] = bridge.addText(width+8, pxOffset, storageUnit["name"], steadyColor)
  30.     storageUnit["bar"] = bridge.addBox(5, pxOffset+1, 0, 5, 0xCC0000, 0.9)
  31.     storageUnit["bar"].setZIndex(2)
  32.     storageUnit["bg"] = bridge.addBox(4, pxOffset, width+2, 7, 0x000000, 0.5)
  33.     offset = offset + 1
  34. end
  35.  
  36. function roundNum(val, decimal)
  37.     if (decimal) then
  38.         return math.floor(((val * 10^decimal) + 0.5) / (10^decimal))
  39.     else
  40.         return math.floor(val+0.5)
  41.     end
  42. end
  43.  
  44. while true do
  45.  
  46.     local totalCapacity = 0
  47.     local totalStored = 0
  48.     local totalPercent = 0
  49.  
  50.     for i=#storageUnits,1,-1 do
  51.         storageUnit = storageUnits[i]
  52.  
  53.         if storageUnit["id"] == "" then
  54.  
  55.             if totalCapacity > 0 then
  56.                 storageUnit["bar"].setWidth(width / totalCapacity * totalStored)
  57.                 storageUnit["label"].setText("Total: "..roundNum(totalPercent, 2).."% ("..(totalStored - storageUnit["lastValue"])..")")
  58.  
  59.  
  60.                 if storageUnit["lastValue"] > totalStored then
  61.                     storageUnit["label"].setColor(lossColor)
  62.  
  63.                 elseif storageUnit["lastValue"] < totalStored then
  64.                     storageUnit["label"].setColor(gainColor)
  65.  
  66.                 elseif storageUnit["lastValue"] == totalStored then
  67.                     storageUnit["label"].setColor(steadyColor)
  68.  
  69.  
  70.                 end
  71.  
  72.                 storageUnit["lastValue"] = totalStored
  73.  
  74.             end
  75.  
  76.         elseif net.isPresentRemote(storageUnit["id"]) then
  77.             capacity = net.callRemote(storageUnit["id"], "getCapacity")
  78.             amount = net.callRemote(storageUnit["id"], "getStored")
  79.             storageUnit["bar"].setWidth(width / capacity * amount)
  80.  
  81.             totalCapacity = totalCapacity + capacity
  82.             totalStored = totalStored + amount
  83.  
  84.             if totalCapacity > 0 then
  85.                 totalPercent = (totalStored / totalCapacity) * 100
  86.             end
  87.  
  88.             storageUnit["label"].setText(storageUnit["name"]..": "..roundNum(totalPercent, 2).."% ("..(amount - storageUnit["lastValue"])..")")
  89.  
  90.             if storageUnit["lastValue"] > amount then
  91.                 storageUnit["label"].setColor(lossColor)
  92.  
  93.             elseif storageUnit["lastValue"] < amount then
  94.                 storageUnit["label"].setColor(gainColor)
  95.  
  96.             elseif storageUnit["lastValue"] == amount then
  97.                 storageUnit["label"].setColor(steadyColor)
  98.  
  99.  
  100.             end
  101.  
  102.             storageUnit["lastValue"] = amount
  103.  
  104.         else
  105.             storageUnit["bar"].delete()
  106.             storageUnit["bg"].delete()
  107.             storageUnit["label"].delete()
  108.             table.remove(storageUnits, i)
  109.         end
  110.     end
  111.  
  112.     if totalPercent < 80 then
  113.         rs.setOutput("back", false) -- Connect EU-Splitter cable
  114.     end
  115.  
  116.     if totalPercent >= 100 then
  117.         rs.setOutput("back", true) -- Disconnect EU-Splitter cable
  118.     end
  119.  
  120.  
  121.     sleep(refreshrate)
  122. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement