Advertisement
aty1998

de-storage.lua

Oct 21st, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.24 KB | None | 0 0
  1. -- APIs
  2.  
  3. local component = require("component")
  4. local term = require("term")
  5.  
  6. -- Constants
  7.  
  8. local name = "draconic_rf_storage"
  9. local debug = false;
  10. local step = 1;
  11.  
  12. -- Variables
  13.  
  14. local storageAddress = nil
  15. local proxy = nil
  16. local computer = component.computer
  17.  
  18. -- Functions
  19.  
  20. function printd(msg)
  21.     if debug then
  22.         print(msg)
  23.     end
  24. end
  25.  
  26. function loadStorage()
  27.     for address, componentType in component.list() do
  28.         if componentType == name then
  29.             printd(componentType .. " found at address: " .. address)
  30.             storageAddress = address
  31.         end
  32.     end
  33.  
  34.     if storageAddress ~= nil then
  35.         proxy = component.proxy(storageAddress)
  36.     end
  37.  
  38.     if proxy == nil then
  39.         return false
  40.     end
  41.  
  42.     return true
  43. end
  44.  
  45. function getEnergy()
  46.     if proxy ~= nil then
  47.         return proxy.getEnergyStored()
  48.     end
  49.     return -1
  50. end
  51.  
  52. function getMaxEnergy()
  53.     if proxy ~= nil then
  54.         return proxy.getMaxEnergyStored()
  55.     end
  56.     return -1
  57. end
  58.  
  59. function formatNumber(number)
  60.     local output = string.format("%.3f", number)
  61.     local unitVal = {1000000000000000, 1000000000000, 1000000000, 1000000, 1000}
  62.     local unit = {"P", "T", "G", "M", "k"}
  63.     local units = 5
  64.  
  65.     local toConvert = math.abs(number)
  66.     local sign = ""
  67.  
  68.     for i = 1, units do
  69.         if toConvert >= unitVal[i] then
  70.             local x = toConvert / unitVal[i]
  71.             if number < 0 then
  72.                 sign = "-"
  73.             end
  74.             output = sign .. string.format("%.3f", x) .. unit[i]
  75.             break
  76.         end
  77.     end
  78.  
  79.     return output
  80. end
  81.  
  82. function showEnergy()
  83.     local gpu = term.gpu()
  84.     term.setCursor(1, 1)
  85.     term.clearLine()
  86.     term.setCursor(1, 1)
  87.     gpu.setForeground(0xFFFFFF)
  88.     term.write("Energy: "
  89.         .. formatNumber(getEnergy())
  90.         .. " RF / "
  91.         .. formatNumber(getMaxEnergy())
  92.         .. " RF")
  93. end
  94.  
  95. function run()
  96.     term.gpu().setResolution(30, 15)
  97.  
  98.     loadStorage()
  99.     if proxy == nil then
  100.         return 1
  101.     end
  102.  
  103.     term.clear()
  104.     os.sleep(step)
  105.     while computer.isRunning() do
  106.         showEnergy()
  107.         os.sleep(step)
  108.     end
  109.  
  110.     return 0
  111. end
  112.  
  113. -- Main Program
  114.  
  115. local exitCode = run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement