Advertisement
Guest User

cellmanager

a guest
Aug 1st, 2014
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.87 KB | None | 0 0
  1. --Thermal expansion energy cell charge controller--
  2. --Place your TE cell on any side of the computer and it will find it automatically--
  3. --Outputs redstone signal when cell power level drops below certain percentage--
  4. --Use <cellmanager help> to find out about program options--
  5. --Examples:-
  6.  
  7. --<cellmanger left 50>  (output signal on left of computer at 50%
  8.  
  9. --create startup file <edit startup> containing
  10. --<shell.run("cellmanager front 20")>
  11.  
  12. --to restart cell manager at server restart, with signal from front at 20%
  13.  
  14. args={...}
  15. output="back"
  16. turnOnLevel=80
  17. --find and wrap the cell
  18. function getPeripheral(thing)
  19.         local Sides = {
  20.         "left","right",
  21.         "top","bottom",
  22.         "front","back",
  23.     }
  24.      
  25.     for i,v in pairs(Sides) do
  26.         if (peripheral.isPresent(v) and peripheral.getType(v) == thing) then
  27.             print("'"..thing.."' found on side '"..v.."'.")
  28.             return peripheral.wrap(v), v
  29.         end
  30.     end
  31.  
  32.     print("ERROR: '"..thing.."' not found!")
  33.     return nil, nil
  34. end
  35. --calculate % energy level
  36. function calculateLevel(currentEnergy, maxEnergy)
  37. local level=0
  38. local fraction=0
  39.     fraction=currentEnergy/maxEnergy
  40.     level=fraction*100
  41.     return level
  42. end
  43. ---MAIN--
  44. charging=false
  45. redstone.setOutput(output,false)
  46. cell=getPeripheral("cofh_thermalexpansion_energycell")
  47. maxEnergy=cell.getMaxEnergyStored("back")
  48. currentEnergy=cell.getEnergyStored("back")
  49. term.clear()
  50. term.setCursorPos(1,1)
  51. print ("Current energy:- "..currentEnergy.."RF")
  52. print ("Max energy:- "..maxEnergy.."RF")
  53. if (args[1] == nil) then
  54.     print ("No arguments set, using default low power:- 80%, redstone signal:- back.")
  55.     print ("Use [cellmanager help] for more info.")
  56.     output="back"
  57.     turnOnLevel=80
  58. elseif (args[1] == "help") then
  59.     print ("Cellmanger help:- Use cellmanager <xxxx> <yyyy>.")
  60.     print ("")
  61.     print ("xxxx=redstone output side on computer for RF generator 'left','right','top','bottom','front','back'. Default is back side.")
  62.     print ("")
  63.     print ("yyyy=low charge percentage to start charging at (no percent sign). Default 80%.")
  64.     print ("")
  65.     print ("Add cellmanger to computer startup file to restart cellmanger on server restart.")
  66. else
  67.     output = args[2]
  68.     turnOnLevel = tonumber(args[2])
  69.     if (turnOnLevel == nil or turnOnLevel <=-1 or turnOnLevel >= 101) then
  70.     print ("Invalid low power setting, using default, 80%.")
  71.     turnOnLevel=80
  72.     else
  73.     print("Low power set to "..turnOnLevel.."%")
  74.     end
  75.     side = args[1]
  76.     if string.match(side, "back") then
  77.         print ("Redstone signal on back.")
  78.         output=side
  79.     elseif string.match(side, "left") then
  80.         print ("Redstone signal on left.")
  81.         output=side
  82.     elseif string.match(side, "right") then
  83.         print ("Redstone signal on right.")
  84.         output=side
  85.     elseif string.match(side, "front") then
  86.         print ("Redstone signal on front.")
  87.         output=side
  88.     elseif string.match(side, "top") then
  89.         print ("Redstone signal on top.")
  90.         output=side
  91.     elseif string.match(side, "bottom") then
  92.         print ("Redstone signal on bottom.")
  93.         output=side
  94.     else
  95.     print ("Redstone side incorrectly set, check your spelling, only 'left','right','top','bottom','front','back' permitted. Using default, back")
  96.     output="back"
  97.     end
  98. end
  99. print("")
  100. getLevelX,getLevelY = term.getCursorPos()
  101. while true do
  102.     currentEnergy=cell.getEnergyStored("back")
  103.     currentLevel=calculateLevel(currentEnergy,maxEnergy)
  104.     term.setCursorPos(1,getLevelY)
  105.     term.clearLine()
  106.     print("Current energy is "..currentLevel)
  107.     if (currentLevel <= turnOnLevel and charging == false) then
  108.         redstone.setOutput(output,true)
  109.         charging=true
  110.         term.setCursorPos(1,getLevelY+1)
  111.         term.clearLine()
  112.         print("Cell low power, starting charge")
  113.     elseif (currentLevel >= 99 and charging==true) then
  114.         redstone.setOutput(output,false)
  115.         charging=false
  116.         term.setCursorPos(1,getLevelY+1)
  117.         term.clearLine()
  118.         print("Cell full, stopping charge")
  119.     end
  120.     sleep(2)
  121. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement