Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Script by Savage_Me55iah of CraftAU.com.au and Nuqube.co--
- local scriptName, version, pastebinCode = "EFFIECENCY_CONTROL", "v1.2", "m4Tye1Vu"
- --Other Savage_Me55iah scripts @ http://pastebin.com/u/Savage_Me55iah
- --INSTRUCTIONS ON USE--
- --Instruction video on http://youtube.com/savagemessiah5
- --Contact Savage_Me55iah @ CraftAU.com.au or Nuqube.co or http://www.computercraft.info/forums2/ with suggestions/fixes/bugs
- -- CHANGELOG --
- --V1.0
- --V1.1 -
- --V1.2 - EXPERIMENTAL
- -- SETUP --
- local tArgs = {...}
- local update_request = tostring(tArgs[1]) or nil
- do
- update = function(script_name, pastebincode)
- print("UPDATING") shell.run("pastebin get "..pastebincode.." update")
- if fs.exists("update") then
- fs.delete(script_name) fs.copy("update", script_name) fs.delete("update") print("UPDATE SUCCESS")
- else
- print("UPDATE FAILED")
- end
- sleep(2) os.reboot()
- end
- end
- if tostring(update_request) == "update" then update(shell.getRunningProgram(), pastebinCode) end
- -- label_check(script) --
- label_check = function(param_scriptName)
- local param_labelA, param_text, param_labelB = os.getComputerLabel() or false, "Computer label, blank for random label:", nil
- while tostring(param_labelA) == "false" or tostring(param_labelA) == "nil" do
- term.clear() term.setCursorPos(1,1) print(param_text)
- param_labelB = tostring(read())
- if param_labelB == "nil" then param_labelB = tostring(param_scriptName.."-"..math.random(1000, 9999)) end
- os.setComputerLabel(param_labelB) param_labelA = os.getComputerLabel() or false
- if tostring(param_labelA) == "false" or tostring(param_labelA) == "nil" then param_text = "Computer label too long, try again:" end
- end
- end
- label_check(scriptName)
- id = os.getComputerID()
- label = os.getComputerLabel() or false
- if label == false then
- print("Please Type label here: ")
- os.setComputerLabel(tostring(read()))
- end
- function boolean_check(params)
- local answer = nil
- if tostring(params) == "true" then
- answer = true
- else
- answer = false
- end
- return answer
- end
- local settings_file = "config"
- local settings_template = [[-- adjust these configuration options as necessary.
- -- IMPORTANT!, API won't load if there are spaces in the values
- COMPUTER_LABEL = "${label}"
- COMPUTER_ID = ${id}
- UPPER_THRESHOLD = ${upper_threshold}
- LOWER_THRESHOLD = ${lower_threshold}
- UPDATE_TIME = ${update_time}
- OUTPUT_SIDE = "${output_side}"
- INVERT_REDSTONE = "${invert_redstone}"
- MEASURE_TYPE = "${measure_type}"
- ]]
- local function interpolate(s, params)
- return s:gsub('($%b{})', function(w) return params[w:sub(3, -2)] or w end)
- end
- do
- save_settings = function()
- local filehandler = fs.open(tostring(settings_file), "w")
- if tostring(CONFIG) == "nil" then
- settings = {
- label = os.getComputerLabel(),
- id = os.getComputerID(),
- upper_threshold = 80,
- lower_threshold = 60,
- update_time = 5,
- output_side = "bottom",
- invert_redstone = "false",
- measure_type = "energy"
- }
- else
- settings = {
- label = CONFIG.COMPUTER_LABEL or os.getComputerLabel(),
- id = os.getComputerID(),
- upper_threshold = CONFIG.UPPER_THRESHOLD or 80,
- lower_threshold = CONFIG.LOWER_THRESHOLD or 60,
- update_time = CONFIG.UPDATE_TIME or 5,
- output_side = CONFIG.OUTPUT_SIDE or "bottom",
- invert_redstone = CONFIG.INVERT_REDSTONE or "false",
- measure_type = CONFIG.MEASURE_TYPE or "energy"
- }
- end
- filehandler.write(interpolate(settings_template, settings))
- filehandler.close()
- end
- end
- if fs.exists(settings_file) == false then
- save_settings()
- return
- else
- os.unloadAPI(settings_file)
- if os.loadAPI(settings_file) == false then print("ERROR: Could not load the settings file!") end
- CONFIG = nil
- for k, v in pairs(_G) do
- if k == settings_file then CONFIG = v break end
- end
- if CONFIG == nil then log_add("CONFIG came up nil", true) end
- end
- do
- getDetails = function()
- local resultStored, resultMax = nil, nil
- if tostring(peripheralList) ~= "nil" then
- for a,b in pairs(peripheralList) do
- local methodList, peripheralWrap = peripheral.getMethods(b), peripheral.wrap(b)
- for c,d in pairs(methodList) do
- if tostring(d) == "getStored" and tostring(CONFIG.MEASURE_TYPE) == "energy" then
- resultStored, resultMax = tonumber(resultStored or 0) + tonumber(peripheralWrap.getStored("north")), tonumber(resultMax or 0) + tonumber(peripheralWrap.getMaxEnergy("north"))
- elseif tostring(d) == "getEnergyStored" and tostring(CONFIG.MEASURE_TYPE) == "energy" then
- resultStored, resultMax = tonumber(resultStored or 0) + tonumber(peripheralWrap.getEnergyStored("north")), tonumber(resultMax or 0) + tonumber(peripheralWrap.getMaxEnergyStored("north"))
- elseif tostring(d) == "getEUStored" and tostring(CONFIG.MEASURE_TYPE) == "IC2" then
- resultStored, resultMax = tonumber(resultStored or 0) + tonumber(peripheralWrap.getEUStored()), tonumber(resultMax or 0) + tonumber(peripheralWrap.getEUCapacity())
- elseif tostring(d) == "getTankInfo" and tostring(CONFIG.MEASURE_TYPE) == "liquid" then
- local tankData = peripheralWrap.getTankInfo()
- resultStored, resultMax = tonumber(resultStored or 0) + tonumber(tankData[1]["contents"]["amount"]), tonumber(resultMax or 0) + tonumber(tankData[1]["capacity"])
- end end end
- else
- resultStored, resultMax = "ERROR", "ERROR"
- end
- return resultStored, resultMax
- end
- decimal = function(number)
- if type(number) == "number" then
- local text, num = ".", number
- if num >= 1000 then text = "K" end
- if num >= 1000000 then text = "M" end
- if num >= 1000000000 then text = "B" end
- if num >= 1000000000000 then text = "T" end
- while num >= 1000 do
- num = num / 1000
- end
- num = math.floor(num * 100) / 100
- if text ~= "." then
- text = tostring(num..text) or 0
- else
- text = tostring(num) or 0
- end
- return text
- else
- return number
- end
- end
- percentage = function(params_varibleA, params_varibleB)
- local result = math.floor((tonumber(params_varibleA) or 0) / (tonumber(params_varibleB) or 0) * 10000)/100
- return result
- end
- run = function(params_varibleC, params_varibleD)
- if tostring(params_varibleC) ~= "ERROR" then
- if percentage(params_varibleC, params_varibleD) >= tonumber(CONFIG.UPPER_THRESHOLD) then active = false end
- if percentage(params_varibleC, params_varibleD) <= tonumber(CONFIG.LOWER_THRESHOLD) then active = true end
- if boolean_check(CONFIG.INVERT_REDSTONE) then active = not active end
- redstone.setOutput(tostring(CONFIG.OUTPUT_SIDE), boolean_check(active))
- end
- end
- -- event = eventFilter("timer", "monitor_touch", "mouse_click", "key", "etc") .. returns a table only when the event matches the arg --
- eventFilter = function(...)
- local param, whitelist, result, fileHandler = {...}, {}, {}, fs.open("eventLog", 'a')
- for a,b in pairs(param) do whitelist[b] = true end
- while tostring(whitelist[result[1]]) ~= "true" do
- result = {os.pullEvent()}
- -- fileHandler.write(textutils.serialize(result))
- end
- -- fileHandler.close()
- return result
- end
- flow = function(params_varible)
- local result, rate = nil
- flow_b = tonumber(params_varible) or "ERROR"
- if tostring(flow_b) ~= "ERROR" and tostring(flow_a) ~= "nil" then
- result = flow_a - flow_b
- result = decimal(tostring(math.floor((result * -1) / (20 * tonumber(CONFIG.UPDATE_TIME)))))
- else
- result = "WAIT..."
- end
- 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
- result = result..rate
- flow_a = flow_b
- return result
- end
- -- screenColor(colors.green, colors.blue)
- screenColor = function(param_commandSide, param_colorBackground, param_colorText)
- local param_command, colorBackground, colorText = nil, param_colorBackground or colors.black, param_colorText or colors.white
- if param_commandSide ~= term then param_command = peripheral.wrap(param_commandSide) else param_command = term end
- if not param_command.isColor() then
- if param_colorBackground ~= colors.black then colorBackground, colorText = colors.white, colors.black
- else colorBackground, colorText = colors.black, colors.white end
- end
- param_command.setTextColor(colorText) param_command.setBackgroundColor(colorBackground)
- end
- -- screenWrite(mon[1], "hello world":upper(), 5, 3, colors.red, colors.black)
- screenWrite = function(param_commandSide, param_text, param_x, param_y, param_colorBackground, param_colorText)
- local param_command = nil
- if param_commandSide ~= term then param_command = peripheral.wrap(param_commandSide) else param_command = term end
- param_command.setCursorPos(param_x, param_y)
- screenColor(param_commandSide, param_colorBackground, param_colorText)
- param_command.write(param_text)
- screenColor(param_commandSide)
- end
- monitor = function(param_commandSide)
- local x, y, length, param_command, scale, limit = 2, 2, 1, nil, 5, false
- if param_commandSide ~= term then param_command = peripheral.wrap(param_commandSide) else param_command = term end
- local screenText = {"NAME: "..os.getComputerLabel(),
- "ID: "..os.getComputerID(),
- tostring(CONFIG.MEASURE_TYPE):upper()..": "..tostring(percentage(amount, maxAmount)).."%",
- tostring(CONFIG.MEASURE_TYPE):upper()..": "..decimal(tonumber(amount)).."/"..decimal(tonumber(maxAmount)),
- "FLOW: ".. tostring(decimal(flowResult)),
- "ACTIVE: "..tostring(active),
- "LIMIT TOP: "..tostring(CONFIG.UPPER_THRESHOLD),
- "LIMIT BOT: "..tostring(CONFIG.LOWER_THRESHOLD)}
- for a,b in pairs(screenText) do if length < string.len(b) then length = string.len(b) end end
- param_command.clear()
- if param_commandSide ~= term then
- param_command.setTextScale(scale)
- local monitor_x, monitor_y = param_command.getSize()
- while (((length > (monitor_x - 2)) or (table.maxn(screenText) > (monitor_y - 1))) and limit == false) do
- if scale > 1 then scale = scale - 0.5 else limit = true end
- param_command.setTextScale(scale)
- monitor_x, monitor_y = param_command.getSize()
- end
- end
- for a,b in pairs(screenText) do
- screenWrite(param_commandSide, b, x, y, colors.black, colors.white)
- y = y + 1
- end
- end
- display = function()
- monitor(term)
- if tostring(peripheralList) ~= "nil" then
- for a,b in pairs(peripheralList) do
- local methodList = peripheral.getMethods(b)
- for c,d in pairs(methodList) do
- if tostring(d) == "setCursorPos" then
- monitor(b)
- end end end end end
- end
- active = nil
- if boolean_check(CONFIG.INVERT_REDSTONE) then active = true else active = false end
- Event, flowResult, peripheralList = {[1] = "timer"}, nil, peripheral.getNames()
- while true do
- amount, maxAmount = getDetails()
- if tostring(Event[1]) == "timer" then os.startTimer(tonumber(CONFIG.UPDATE_TIME)) flowResult = flow(amount) end
- if tostring(Event[1]) == "peripheral" or tostring(Event[1]) == "peripheral_detach" or tostring(peripheralList) == "nil" then peripheralList = peripheral.getNames() end
- run(amount, maxAmount)
- display()
- Event = eventFilter("timer", "monitor_touch", "key", "peripheral_detach", "peripheral")
- if redstone.getInput(tostring(CONFIG.OUTPUT_SIDE)) ~= boolean_check(active) then os.reboot() end
- end
Advertisement
Add Comment
Please, Sign In to add comment