craftyoyo

mfsu mon

May 31st, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --################CONFIG########################
  2.  
  3. --boolean props to control use
  4. ic2 = true
  5. mj = true
  6.  
  7. --Point in which to begin charging (1 - 99)
  8. chargePercent = 25
  9.  
  10. --Configure storage devices
  11. --List storage (names received from wired modem) in these tables
  12. --If Auto-detection is enabled these tables can be left blank
  13. ic2Storage = {
  14.     "batbox_0" 
  15. }
  16. mjStorage = {
  17.    
  18. }
  19. --Auto-detect other storage on network?
  20. detection = true
  21.  
  22. --Configure energy Generation.
  23. --Each item needs the type, direction bundle is attached and wire color
  24. --["type"]["direction"]["color"]
  25. generators = {
  26.     {
  27.         ["type"] = "mj",
  28.         ["direction"] = "bottom",
  29.         ["color"] = 4
  30.     },
  31.     {
  32.         ["type"] = "eu",
  33.         ["direction"] = "bottom",
  34.         ["color"] = 1
  35.     }
  36. }
  37.  
  38. --###########END OF CONFIG#######################
  39.  
  40. local function findLocalDevice(deviceType)
  41.     for i, side in pairs(rs.getSides()) do
  42.         if (peripheral.isPresent(side)) then
  43.             if (string.lower(peripheral.getType(side)) == string.lower(deviceType)) then
  44.                 return side;
  45.             end
  46.         end
  47.     end
  48.     return nil;
  49. end
  50.  
  51. local function getStorage()
  52.     maxEU = 0
  53.     maxMJ= 0
  54.     --Grab max storage
  55.     if(ic2) then
  56.         for key,value in ipairs(ic2Storage) do
  57.             if(net.isPresentRemote(value)) then
  58.                 maxEU = maxEU + net.callRemote(value,"getCapacity")
  59.             else
  60.                 ic2Storage[key] = nil
  61.             end
  62.         end
  63.     end
  64.     if(mj) then
  65.         for key,value in ipairs(mjStorage) do
  66.             if(net.isPresentRemote(value)) then
  67.                 maxMJ = maxMJ + net.callRemote(value,"getMaxEnergyStored")
  68.             else
  69.                 mjStorage[key] = nil
  70.             end
  71.         end
  72.     end
  73. end
  74.  
  75. local function findStorage()
  76.     local added = false
  77.     local new = true
  78.     if (mj) then
  79.         for i, remote in ipairs(net.getNamesRemote()) do
  80.             if(string.find(string.lower(remote),"redstone_energy_cell")) then
  81.                 for key, value in ipairs(mjStorage) do
  82.                     if (remote == value) then
  83.                         new = false
  84.                     end
  85.                 end
  86.                 if (new) then
  87.                     table.insert(mjStorage, remote)
  88.                     added = true
  89.                 else
  90.                     new = true
  91.                 end
  92.             end
  93.         end
  94.     end
  95.  
  96.     if (ic2) then
  97.         for i, remote in ipairs(net.getNamesRemote()) do
  98.             if(string.find(string.lower(remote), "batbox")) then
  99.                 for key, value in ipairs(ic2Storage) do
  100.                     if (remote == value) then
  101.                         new = false
  102.                     end
  103.                 end
  104.                 if (new) then
  105.                     table.insert(ic2Storage, remote)
  106.                     added = true
  107.                 else
  108.                     new = true
  109.                 end
  110.             end
  111.         end
  112.     end
  113.     return added
  114. end
  115.  
  116. local function setGeneration(strType, boolean, list)
  117.     local redstoneValue = {}
  118.  
  119.     for key, value in pairs(list) do
  120.         if (value["type"] == strType) then
  121.             if(redstoneValue[value["direction"]] == nil)then
  122.                 redstoneValue[value["direction"]] = redstone.getBundledOutput(value["direction"])
  123.             end
  124.             if (boolean) then
  125.                 if (not colors.test(redstoneValue[value["direction"]],value["color"])) then
  126.                     redstoneValue[value["direction"]] = redstoneValue[value["direction"]] + value["color"]
  127.                 end
  128.             else
  129.                 if (colors.test(redstoneValue[value["direction"]],value["color"])) then
  130.                     redstoneValue[value["direction"]] = redstoneValue[value["direction"]] - value["color"]
  131.                 end
  132.             end
  133.         end
  134.     end
  135.     -- Set redstone bundle
  136.     for key, value in pairs(redstoneValue) do
  137.         redstone.setBundledOutput(key, value)
  138.     end
  139. end
  140. local function int()
  141.     --Check for monitor
  142.     if(findLocalDevice("monitor")) then
  143.         term.clear()
  144.         term.write("monitor connected")
  145.         term.redirect(peripheral.wrap(findLocalDevice("monitor")))
  146.     end
  147.  
  148.     --Prepare the screen
  149.     term.clear()
  150.     term.setCursorPos(1,1)
  151.     term.write("Power Management")
  152.     term.setCursorPos(1,2)
  153.     term.write("EU = NA")
  154.     term.setCursorPos(1,3)
  155.     term.write("0/0")
  156.     term.setCursorPos(1,4)
  157.     term.write("MJ = NA")
  158.     term.setCursorPos(1,5)
  159.     term.write("0/0")
  160.     term.setCursorPos(1,6)
  161.  
  162.     if(not (ic2 or mj))then
  163.         term.write("Please choose sources in the config")
  164.         shell.exit()
  165.     end
  166.  
  167.     --Variables to storage amounts
  168.     storedEU = 0
  169.     maxEU = 0
  170.     storedMJ = 0
  171.     maxMJ = 0
  172.     euPercent = 0
  173.     mjPercent = 0
  174.     --Other Vars
  175.     bridge = nil
  176.     net = nil
  177.  
  178.     --Find network
  179.     if (findLocalDevice("modem") == nil) then
  180.         term.write("Cannot find Network")
  181.         shell.exit()
  182.     else
  183.         net = peripheral.wrap(findLocalDevice("modem"))
  184.     end
  185.  
  186.     if(detection) then
  187.         findStorage()
  188.     end
  189.  
  190.     getStorage()
  191.  
  192.     --Dectect Bridge
  193.     if (findLocalDevice("terminal_glasses_bridge")) then
  194.         bridge  = peripheral.wrap(findLocalDevice("terminal_glasses_bridge"))
  195.     end
  196.     if (bridge) then
  197.     --Prepare Glasses
  198.         bridge.clear()
  199.         bridge.addText(1,1,"Power Management",colors.gray)
  200.         euText = bridge.addText(1,10, "EU = NA", colors.gray)
  201.         bridge.addBox(3, 20, 100, 5, colors.gray, 100)
  202.         euBar = bridge.addBox(3, 20, 0, 5, colors.red, 100)
  203.         euBar.setZIndex(4)
  204.         mjText = bridge.addText(1,28, "MJ = NA", colors.gray)
  205.         bridge.addBox(3, 38, 100, 5, colors.gray, 100)
  206.         mjBar = bridge.addBox(3, 38, 0, 5, colors.red, 100)
  207.         mjBar.setZIndex(4)
  208.     end
  209. end
  210. local function main()
  211.     storedMJ = 0
  212.     storedEU = 0
  213.     if(ic2) then
  214.         --Get amount of stored EU
  215.         for key,value in ipairs(ic2Storage) do
  216.             if(net.isPresentRemote(value)) then
  217.                 storedEU = storedEU + net.callRemote(value,"getStored")
  218.             else
  219.                 getStorage()
  220.             end
  221.         end
  222.     end
  223.     if(mj) then
  224.         --Get amount of stored MJ
  225.         for key,value in ipairs(mjStorage) do
  226.             if(net.isPresentRemote(value)) then
  227.                 storedMJ = storedMJ + net.callRemote(value,"getEnergyStored")
  228.             else
  229.                 getStorage()
  230.             end
  231.         end
  232.     end
  233.     euPercent = math.floor((storedEU / maxEU) * 100)
  234.     mjPercent = math.floor((storedMJ / maxMJ) * 100)
  235.     --control status
  236.     if(ic2) then
  237.         if (euPercent < chargePercent) then
  238.             setGeneration("eu",true,generators)
  239.         elseif((euPercent >= 100) or (maxEU == 0))then
  240.             setGeneration("eu",false,generators)
  241.         end
  242.         --update screen
  243.         term.setCursorPos(1,2)
  244.         term.clearLine()
  245.         term.write("EU = " .. euPercent .. "%")
  246.         term.setCursorPos(1,3)
  247.  
  248.         --quick fix for IC2 output buffer
  249.         if (storedEU > maxEU) then
  250.             storedEU = maxEU
  251.         end
  252.  
  253.         term.write(storedEU .. "/" .. maxEU)
  254.     end
  255.  
  256.     if (mj) then
  257.         if (mjPercent < chargePercent) then
  258.             setGeneration("mj",true, generators)
  259.         elseif((mjPercent >= 100) or (maxMJ == 0)) then
  260.             setGeneration("mj", false, generators)
  261.         end
  262.  
  263.         term.setCursorPos(1,4)
  264.         term.clearLine()
  265.         term.write("MJ = " .. mjPercent .. "%")
  266.         term.setCursorPos(1,5)
  267.         term.write(math.floor(storedMJ) .. "/" .. maxMJ)
  268.     end
  269.     term.setCursorPos(1,6)
  270.  
  271.     if(bridge) then
  272.         --paint glasses
  273.         if(ic2) then
  274.             euText.setText("EU = " .. euPercent .. "%")
  275.             euBar.setWidth(euPercent)
  276.         end
  277.         if(mj) then
  278.             mjText.setText("MJ = " .. mjPercent .. "%")
  279.             mjBar.setWidth(mjPercent)
  280.         end
  281.     end
  282.     sleep(1)
  283. end
  284.  
  285. local function eventListener()
  286.     sleep(0)
  287.     p1, p2 = os.pullEventRaw()
  288.     if p1 == "terminate" then
  289.         quit = true
  290.     end
  291. end
  292.  
  293. local function autoDetect()
  294.     local found = findStorage()
  295.     if(found) then
  296.         getStorage()
  297.     end
  298. end
  299.  
  300. quit = false
  301. int()
  302.  
  303. while true do
  304.     parallel.waitForAny(eventListener, main)
  305.  
  306.     if quit then
  307.         for i, value in pairs(rs.getSides()) do
  308.             rs.setBundledOutput(value, 0)
  309.         end
  310.         term.clear()
  311.         term.restore()
  312.         term.clear()
  313.         term.setCursorPos(1,1)
  314.         if (findLocalDevice("terminal_glasses_bridge")) then
  315.             peripheral.call(findLocalDevice("terminal_glasses_bridge"),"clear")
  316.         end
  317.         print("Terminated")
  318.         break
  319.     end
  320.     if(detection) then
  321.         autoDetect()
  322.     end
  323.  
  324.     sleep(0)
  325. end
Add Comment
Please, Sign In to add comment