gamoholic

Lava-backup

Jul 8th, 2013
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.22 KB | None | 0 0
  1. os.loadAPI("ocs/apis/sensor")
  2. local prox=sensor.wrap("top")
  3. redstone.setOutput("right",true)
  4.  
  5. function findTargets()
  6.     targets = {}
  7.     numTargets = 0
  8.     totalCap = 0
  9.     for target in pairs(prox.getTargets()) do
  10.         local details = prox.getTargetDetails(target)
  11.         if details.Capacity == 10000000 or details.Capacity == 100000000 then
  12.             numTargets = numTargets + 1
  13.             targets[numTargets] = target
  14.             totalCap = totalCap + details.Capacity
  15.         end
  16.     end
  17.     formatCap = comma_value(totalCap)
  18. end
  19.  
  20. function comma_value(amount)
  21.     local formatted = amount
  22.     while true do  
  23.         formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
  24.         if (k==0) then
  25.             break
  26.         end
  27.     end
  28.     return formatted
  29. end
  30.  
  31. function clear()
  32.     term.clear()
  33.     term.setCursorPos (1,1)
  34. end
  35.  
  36. function main()
  37.     while run == true do
  38.         local totalStored = 0
  39.         local deadBats = 0
  40.         for target = 1, numTargets do
  41.             local details = prox.getTargetDetails(targets[target])
  42.             totalStored = totalStored + details.Stored
  43.             if details.Stored < 10000 then
  44.                 deadBats = deadBats + 1
  45.             end
  46.         end
  47.         local formatStored = comma_value(totalStored)
  48.        
  49.         local energy = (totalStored / totalCap) * 100
  50.  
  51.         clear()
  52.         print("# Batteries : " ..numTargets)
  53.         print("Dead Batts  : " ..deadBats)
  54.         print("Tot Capacity: " ..formatCap.. " EU")
  55.         print("Total Stored: " ..formatStored.. " EU")
  56.         print("Energy Level: " ..energy.. "%")
  57.         if redstone.getOutput("right") then
  58.             print("Generators  : OFF")
  59.         else
  60.             print("Generators  : ON")
  61.         end
  62.         print()
  63.         print("Press r to re-scan for batteries.")
  64.         print("Press f to force the generators on.")
  65.         print("Press o to force the generators off.")
  66.         print("Press x to exit.")
  67.        
  68.         if pause == false then
  69.             if energy < 50 or (deadBats / numTargets) > 0.5 then
  70.                 redstone.setOutput("right",false)
  71.             end
  72.             if energy > 95 then
  73.                 redstone.setOutput("right",true)
  74.             end
  75.         end
  76.        
  77.         sleep (4)
  78.     end
  79. end
  80.  
  81. function commands()
  82.     while run == true do
  83.         event, param = os.pullEvent("char")
  84.         if param == "r" then
  85.             clear()
  86.             pause = true
  87.             print("Re-scanning, please wait.")
  88.             sleep(1)
  89.             findTargets()
  90.             pause = false
  91.         elseif param == "f" then
  92.             clear()
  93.             print("You have forced the generators on.")
  94.             sleep(1)
  95.             redstone.setOutput("right",false)
  96.         elseif param == "o" then
  97.             clear()
  98.             print("You have forced the generators off.")
  99.             sleep(1)
  100.             redstone.setOutput("right",true)
  101.         elseif param == "x" then
  102.             run = false
  103.         end
  104.     end
  105. end
  106.  
  107. term.redirect(peripheral.wrap("left"))
  108. clear()
  109. print("Welcome to lava-backup! Please wait.")
  110.  
  111. run = true
  112. pause = false
  113. findTargets()
  114.  
  115. parallel.waitForAll(main, commands)
  116.  
  117. clear()
  118. print("Good bye!")
Advertisement
Add Comment
Please, Sign In to add comment