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.1", "9KuxMwwY"
- --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 -
- -- 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, event = {...}, {}, {}
- for a,b in pairs(param) do whitelist[b] = true end
- while tostring(whitelist[event[1]]) == "nil" do
- event = {os.pullEvent()}
- end
- return event
- 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_commandb, param_colorBackground, param_colorText)
- local colorBackground, colorText = param_colorBackground or colors.black, param_colorText or colors.white
- if param_commandb.isColor() == false then
- if param_colorBackground ~= colors.black then colorBackground, colorText = colors.white, colors.black
- else colorBackground, colorText = colors.black, colors.white end
- end
- end
- -- screenWrite(mon[1], "hello world":upper(), 5, 3, colors.red, colors.black)
- screenWrite = function(param_command, param_text, param_x, param_y, param_colorBackground, param_colorText)
- param_command.setCursorPos(param_x, param_y)
- -- screenColor(param_command, param_colorBackground, param_colorText)
- param_command.write(param_text)
- -- screenColor()
- end
- monitor = function(command)
- x, y = 2, 2
- command.clear() command.setCursorPos(x, y)
- screenWrite(command, "NAME: "..os.getComputerLabel(), x, y, colors.black, colors.white) y = y + 1
- screenWrite(command, "ID: "..os.getComputerID(), x, y, colors.black, colors.white) y = y + 1
- screenWrite(command, "MEASURE TYPE: "..tostring(CONFIG.MEASURE_TYPE):upper(), x, y, colors.black, colors.white) y = y + 1
- screenWrite(command, "LEVEL: "..tostring(percentage(amount, maxAmount)).."%", x, y, colors.black, colors.white) y = y + 1
- screenWrite(command, "ENERGY: "..tostring(decimal(tonumber(amount))).."/"..tostring(decimal(tonumber(maxAmount))), x, y, colors.black, colors.white) y = y + 1
- screenWrite(command, "FLOW: ".. tostring(decimal(flowResult)), x, y, colors.black, colors.white) y = y + 1
- screenWrite(command, "ACTIVE: "..tostring(active), x, y, colors.black, colors.white) y = y + 1
- screenWrite(command, "THRESHOLD UPPER: "..tostring(CONFIG.UPPER_THRESHOLD), x, y, colors.black, colors.white) y = y + 1
- screenWrite(command, "THRESHOLD LOWER: "..tostring(CONFIG.LOWER_THRESHOLD), x, y, colors.black, colors.white) y = y + 1
- screenWrite(command, "UPDATE TIMER: "..tostring(CONFIG.UPDATE_TIME), x, y, colors.black, colors.white) y = y + 1
- screenWrite(command, "INVERT REDSTONE: "..tostring(CONFIG.INVERT_REDSTONE), x, y, colors.black, colors.white)
- 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(peripheral.wrap(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, nil
- while true do
- peripheralList = peripheral.getNames()
- amount, maxAmount = getDetails()
- if tostring(Event[1]) == "timer" then os.startTimer(tonumber(CONFIG.UPDATE_TIME)) flowResult = flow(amount) end
- if redstone.getInput(tostring(CONFIG.OUTPUT_SIDE)) ~= boolean_check(active) then os.reboot() end
- run(amount, maxAmount)
- display()
- Event = eventFilter("timer", "monitor_touch", "key")
- end
Advertisement
Add Comment
Please, Sign In to add comment