Savage_Me55iah

CC_EFFIECENCY_CONTROL_EXPERIMENTAL

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