Savage_Me55iah

CC_EFFECIENCY_CONTROL

Apr 8th, 2015
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.95 KB | None | 0 0
  1. --Script by Savage_Me55iah of CraftAU.com.au and Nuqube.co--
  2. local scriptName, version, pastebinCode = "EFFIECENCY_CONTROL", "v1.1", "9KuxMwwY"
  3.  
  4. --Other Savage_Me55iah scripts @ http://pastebin.com/u/Savage_Me55iah
  5.  
  6. --INSTRUCTIONS ON USE--
  7. --Instruction video on http://youtube.com/savagemessiah5
  8.  
  9. --Contact Savage_Me55iah @ CraftAU.com.au or Nuqube.co or http://www.computercraft.info/forums2/ with suggestions/fixes/bugs
  10.  
  11. -- CHANGELOG --
  12. --V1.0
  13. --V1.1 -
  14.  
  15. -- SETUP --
  16.  
  17. local tArgs = {...}
  18. local update_request = tostring(tArgs[1]) or nil
  19.  
  20. do
  21.     update = function(script_name, pastebincode)
  22.         print("UPDATING") shell.run("pastebin get "..pastebincode.." update")
  23.         if fs.exists("update") then
  24.             fs.delete(script_name) fs.copy("update", script_name) fs.delete("update") print("UPDATE SUCCESS")
  25.         else
  26.             print("UPDATE FAILED")
  27.         end
  28.         sleep(2) os.reboot()
  29.     end
  30. end
  31.  
  32. if tostring(update_request) == "update" then update(shell.getRunningProgram(), pastebinCode) end
  33.  
  34. --  label_check(script) --
  35. label_check = function(param_scriptName)
  36.     local param_labelA, param_text, param_labelB = os.getComputerLabel() or false, "Computer label, blank for random label:", nil
  37.     while tostring(param_labelA) == "false" or tostring(param_labelA) == "nil" do
  38.         term.clear() term.setCursorPos(1,1) print(param_text)
  39.         param_labelB = tostring(read())
  40.         if param_labelB == "nil" then param_labelB = tostring(param_scriptName.."-"..math.random(1000, 9999)) end
  41.         os.setComputerLabel(param_labelB) param_labelA = os.getComputerLabel() or false
  42.         if tostring(param_labelA) == "false" or tostring(param_labelA) == "nil" then param_text = "Computer label too long, try again:" end
  43.     end
  44. end
  45. label_check(scriptName)
  46.  
  47. id = os.getComputerID()
  48. label = os.getComputerLabel() or false
  49. if label == false then
  50.     print("Please Type label here: ")
  51.     os.setComputerLabel(tostring(read()))
  52. end
  53.  
  54. function boolean_check(params)
  55.     local answer = nil
  56.     if tostring(params) == "true" then
  57.         answer = true
  58.     else
  59.         answer = false
  60.     end
  61.     return answer
  62. end
  63.  
  64. local settings_file = "config"
  65.  
  66. local settings_template = [[-- adjust these configuration options as necessary.
  67. -- IMPORTANT!, API won't load if there are spaces in the values
  68. COMPUTER_LABEL = "${label}"
  69. COMPUTER_ID = ${id}
  70.  
  71. UPPER_THRESHOLD = ${upper_threshold}
  72. LOWER_THRESHOLD = ${lower_threshold}
  73. UPDATE_TIME = ${update_time}
  74. OUTPUT_SIDE = "${output_side}"
  75. INVERT_REDSTONE = "${invert_redstone}"
  76. MEASURE_TYPE = "${measure_type}"
  77. ]]
  78.  
  79. local function interpolate(s, params)
  80.     return s:gsub('($%b{})', function(w) return params[w:sub(3, -2)] or w end)
  81. end
  82.  
  83. do  
  84.     save_settings = function()
  85.         local filehandler = fs.open(tostring(settings_file), "w")
  86.        
  87.         if tostring(CONFIG) == "nil" then
  88.             settings = {
  89.                 label = os.getComputerLabel(),
  90.                 id = os.getComputerID(),
  91.                 upper_threshold = 80,
  92.                 lower_threshold = 60,
  93.                 update_time = 5,
  94.                 output_side = "bottom",
  95.                 invert_redstone = "false",
  96.                 measure_type = "energy"
  97.             }
  98.         else
  99.             settings = {
  100.                 label = CONFIG.COMPUTER_LABEL or os.getComputerLabel(),
  101.                 id = os.getComputerID(),
  102.                 upper_threshold = CONFIG.UPPER_THRESHOLD or 80,
  103.                 lower_threshold = CONFIG.LOWER_THRESHOLD or 60,
  104.                 update_time = CONFIG.UPDATE_TIME or 5,
  105.                 output_side = CONFIG.OUTPUT_SIDE or "bottom",
  106.                 invert_redstone = CONFIG.INVERT_REDSTONE or "false",
  107.                 measure_type = CONFIG.MEASURE_TYPE or "energy"
  108.             }
  109.         end
  110.        
  111.         filehandler.write(interpolate(settings_template, settings))
  112.         filehandler.close()
  113.     end
  114. end
  115.  
  116. if fs.exists(settings_file) == false then
  117.     save_settings()
  118.     return
  119. else
  120.     os.unloadAPI(settings_file)
  121.     if os.loadAPI(settings_file) == false then print("ERROR: Could not load the settings file!") end
  122.     CONFIG = nil
  123.     for k, v in pairs(_G) do
  124.         if k == settings_file then CONFIG = v break end
  125.     end
  126.     if CONFIG == nil then log_add("CONFIG came up nil", true) end
  127. end
  128.  
  129. do
  130.     getDetails = function()
  131.         local resultStored, resultMax = nil, nil
  132.         if tostring(peripheralList) ~= "nil" then
  133.             for a,b in pairs(peripheralList) do
  134.                 local methodList, peripheralWrap = peripheral.getMethods(b), peripheral.wrap(b)
  135.                 for c,d in pairs(methodList) do
  136.                     if tostring(d) == "getStored" and tostring(CONFIG.MEASURE_TYPE) == "energy" then
  137.                         resultStored, resultMax = tonumber(resultStored or 0) + tonumber(peripheralWrap.getStored("north")), tonumber(resultMax or 0) + tonumber(peripheralWrap.getMaxEnergy("north"))
  138.                     elseif tostring(d) == "getEnergyStored" and tostring(CONFIG.MEASURE_TYPE) == "energy" then
  139.                         resultStored, resultMax = tonumber(resultStored or 0) + tonumber(peripheralWrap.getEnergyStored("north")), tonumber(resultMax or 0) + tonumber(peripheralWrap.getMaxEnergyStored("north"))
  140.                     elseif tostring(d) == "getEUStored" and tostring(CONFIG.MEASURE_TYPE) == "IC2" then
  141.                         resultStored, resultMax = tonumber(resultStored or 0) + tonumber(peripheralWrap.getEUStored()), tonumber(resultMax or 0) + tonumber(peripheralWrap.getEUCapacity())
  142.                     elseif tostring(d) == "getTankInfo" and tostring(CONFIG.MEASURE_TYPE) == "liquid" then
  143.                         local tankData = peripheralWrap.getTankInfo()
  144.                         resultStored, resultMax = tonumber(resultStored or 0) + tonumber(tankData[1]["contents"]["amount"]), tonumber(resultMax or 0) + tonumber(tankData[1]["capacity"])
  145.             end end end
  146.         else
  147.             resultStored, resultMax = "ERROR", "ERROR"
  148.         end
  149.         return resultStored, resultMax
  150.     end
  151.    
  152.     decimal = function(number)
  153.         if type(number) == "number" then
  154.             local text, num = ".", number
  155.             if num >= 1000 then text = "K" end
  156.             if num >= 1000000 then text = "M" end
  157.             if num >= 1000000000 then text = "B" end
  158.             if num >= 1000000000000 then text = "T" end
  159.             while num >= 1000 do
  160.                 num = num / 1000
  161.             end
  162.             num = math.floor(num * 100) / 100
  163.             if text ~= "." then
  164.                 text = tostring(num..text) or 0
  165.             else
  166.                 text = tostring(num) or 0
  167.             end
  168.             return text
  169.         else
  170.             return number
  171.         end
  172.     end
  173.  
  174.     percentage = function(params_varibleA, params_varibleB)
  175.         local result = math.floor((tonumber(params_varibleA) or 0) / (tonumber(params_varibleB) or 0) * 10000)/100
  176.         return result
  177.     end
  178.  
  179.     run = function(params_varibleC, params_varibleD)
  180.         if tostring(params_varibleC) ~= "ERROR" then
  181.             if percentage(params_varibleC, params_varibleD) >= tonumber(CONFIG.UPPER_THRESHOLD) then active = false end
  182.             if percentage(params_varibleC, params_varibleD) <= tonumber(CONFIG.LOWER_THRESHOLD) then active = true end
  183.             if boolean_check(CONFIG.INVERT_REDSTONE) then active = not active end
  184.             redstone.setOutput(tostring(CONFIG.OUTPUT_SIDE), boolean_check(active))
  185.         end
  186.     end
  187.    
  188. --  event = eventFilter("timer", "monitor_touch", "mouse_click", "key", "etc") .. returns a table only when the event matches the arg --
  189.     eventFilter = function(...)
  190.         local param, whitelist, event = {...}, {}, {}
  191.         for a,b in pairs(param) do whitelist[b] = true end
  192.         while tostring(whitelist[event[1]]) == "nil" do
  193.             event = {os.pullEvent()}
  194.         end
  195.         return event
  196.     end
  197.    
  198.     flow = function(params_varible)
  199.         local result, rate = nil
  200.         flow_b = tonumber(params_varible) or "ERROR"
  201.         if tostring(flow_b) ~= "ERROR" and tostring(flow_a) ~= "nil" then
  202.             result = flow_a - flow_b
  203.             result = decimal(tostring(math.floor((result * -1) / (20 * tonumber(CONFIG.UPDATE_TIME)))))
  204.         else
  205.             result = "WAIT..."
  206.         end
  207.         if tostring(CONFIG.MEASURE_TYPE) == "energy" then rate = "rf/t" elseif tostring(CONFIG.MEASURE_TYPE) == "liquid" then rate = "mb/t" elseif tostring(CONFIG.MEASURE_TYPE) == "IC2" then rate = "eu/t" end
  208.         result = result..rate
  209.         flow_a = flow_b
  210.         return result
  211.     end
  212.    
  213. -- screenColor(colors.green, colors.blue)
  214.     screenColor = function(param_commandb, param_colorBackground, param_colorText)
  215.         local colorBackground, colorText = param_colorBackground or colors.black, param_colorText or colors.white
  216.         if param_commandb.isColor() == false then
  217.             if param_colorBackground ~= colors.black then colorBackground, colorText = colors.white, colors.black
  218.             else colorBackground, colorText = colors.black, colors.white end
  219.         end
  220.     end
  221.  
  222. --  screenWrite(mon[1], "hello world":upper(), 5, 3, colors.red, colors.black)
  223.     screenWrite = function(param_command, param_text, param_x, param_y, param_colorBackground, param_colorText)
  224.         param_command.setCursorPos(param_x, param_y)
  225. --        screenColor(param_command, param_colorBackground, param_colorText)
  226.         param_command.write(param_text)
  227. --        screenColor()
  228.     end
  229.    
  230.     monitor = function(command)
  231.         x, y = 2, 2
  232.         command.clear() command.setCursorPos(x, y)
  233.         screenWrite(command, "NAME: "..os.getComputerLabel(), x, y, colors.black, colors.white) y = y + 1
  234.         screenWrite(command, "ID: "..os.getComputerID(), x, y, colors.black, colors.white) y = y + 1
  235.         screenWrite(command, "MEASURE TYPE: "..tostring(CONFIG.MEASURE_TYPE):upper(), x, y, colors.black, colors.white) y = y + 1
  236.         screenWrite(command, "LEVEL: "..tostring(percentage(amount, maxAmount)).."%", x, y, colors.black, colors.white) y = y + 1
  237.         screenWrite(command, "ENERGY: "..tostring(decimal(tonumber(amount))).."/"..tostring(decimal(tonumber(maxAmount))), x, y, colors.black, colors.white) y = y + 1
  238.         screenWrite(command, "FLOW: ".. tostring(decimal(flowResult)), x, y, colors.black, colors.white) y = y + 1
  239.         screenWrite(command, "ACTIVE: "..tostring(active), x, y, colors.black, colors.white) y = y + 1
  240.         screenWrite(command, "THRESHOLD UPPER: "..tostring(CONFIG.UPPER_THRESHOLD), x, y, colors.black, colors.white) y = y + 1
  241.         screenWrite(command, "THRESHOLD LOWER: "..tostring(CONFIG.LOWER_THRESHOLD), x, y, colors.black, colors.white) y = y + 1
  242.         screenWrite(command, "UPDATE TIMER: "..tostring(CONFIG.UPDATE_TIME), x, y, colors.black, colors.white) y = y + 1
  243.         screenWrite(command, "INVERT REDSTONE: "..tostring(CONFIG.INVERT_REDSTONE), x, y, colors.black, colors.white)
  244.     end
  245.    
  246.     display = function()
  247.         monitor(term)
  248.         if tostring(peripheralList) ~= "nil" then
  249.             for a,b in pairs(peripheralList) do
  250.                 local methodList = peripheral.getMethods(b)
  251.                 for c,d in pairs(methodList) do
  252.                     if tostring(d) == "setCursorPos" then
  253.                         monitor(peripheral.wrap(b))
  254.     end end end end end
  255. end
  256.  
  257. active = nil
  258. if boolean_check(CONFIG.INVERT_REDSTONE) then active = true else active = false end
  259. Event, flowResult, peripheralList = {[1] = "timer"}, nil, nil
  260.  
  261. while true do
  262.     peripheralList = peripheral.getNames()
  263.     amount, maxAmount = getDetails()
  264.     if tostring(Event[1]) == "timer" then os.startTimer(tonumber(CONFIG.UPDATE_TIME)) flowResult = flow(amount) end
  265.     if redstone.getInput(tostring(CONFIG.OUTPUT_SIDE)) ~= boolean_check(active) then os.reboot() end
  266.     run(amount, maxAmount)
  267.     display()
  268.     Event = eventFilter("timer", "monitor_touch", "key")
  269. end
Advertisement
Add Comment
Please, Sign In to add comment