Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Script by Savage_Me55iah of CraftAU.com.au--
- --Version 1.4--
- --Needs corresponding "Savage Client v1.4" script @ http://pastebin.com/vbdYtpra--
- --type "monitor client" to auto download--
- --INSTRUCTIONS ON USE--
- --save this program as startup or monitor and type in startup: shell.run("monitor")--
- --example client computer: id-5 label-power
- --to add example client type "montior add 5 power"
- --to remove example client type "monitor remove 5 power"
- --to show settings of buttons type "monitor settings"
- --to adjust the settings of script type "edit settings", changing them here won't work once this program has been run once
- --to reset everything type "monitor reset"
- --buttons will not work without the online button on, turning the online button will shut off all other buttons
- --connect the monitors using network cables and wired modems
- local tArgs = { ... }
- local command = tostring(tArgs[1]) or false
- local name1 = tostring(tArgs[3]) or false
- local id = tonumber(tArgs[2]) or false
- local name2 = tostring(tArgs[5]) or false
- local id2 = tonumber(tArgs[4]) or false
- --TABLES--
- print("tables")
- names = {}
- ids = {}
- status = {}
- function add_button(command, id, on_off)
- num = 1
- while names[num] ~= nil do
- num = num + 1
- end
- names[num] = command
- ids[num] = id
- status[num] = on_off
- end
- add_button("online", 0, true)
- local load_save_loc = {
- ["load_save_loc"] = "load_save_loc",
- ["settings"] = "settings",
- ["names"] = "names",
- ["ids"] = "ids",
- ["status"] = "status",
- ["eu"] = "eu",
- ["mj"] = "mj",
- }
- local settings = {
- ["mon_side"] = "top",
- ["wireless_side"] = "left",
- ["remote_modem"] = "right",
- ["heading color"] = colors.yellow,
- ["heading background"] = colors.black,
- ["heading"] = "Savage Monitor v1.4",
- ["active color"] = colors.green,
- ["inactive color"] = colors.red,
- ["id"] = os.getComputerID(),
- ["label"] = "Server",
- ["refresh_rate"] = 10,
- ["text_scale"] = 1,
- ["button_width"] = 5,
- ["energy_stats"] = false,
- ["open_peripherals"] = false,
- ["direct_power"] = false,
- ["energy_room"] = 0,
- ["eu_id"] = 1000,
- ["mj_id"] = 1001,
- ["screen_saver"] = true,
- ["timeout"] = 50,
- ["image_x"] = 27,
- ["image_y"] = 9,
- ["client"] = "vbdYtpra"
- }
- local button_x1 = {
- [1] = 2
- }
- local button_x2 = {
- [1] = 12
- }
- local button_y1 = {
- [1] = 4
- }
- local eu = {
- ["max"] = 0,
- ["current"] = 0,
- ["percent"] = 0
- }
- local mj = {
- ["max"] = 0,
- ["current"] = 0,
- ["percent"] = 0
- }
- --LOAD & SAVE FUNCTIONS--
- function load()
- print("Loading...")
- local fileHandle = fs.open (load_save_loc["load_save_loc"], 'r')
- load_save_loc = textutils.unserialize (fileHandle.readAll())
- fileHandle.close()
- local fileHandle = fs.open (load_save_loc["settings"], 'r')
- settings = textutils.unserialize (fileHandle.readAll())
- fileHandle.close()
- local fileHandle = fs.open (load_save_loc["names"], 'r')
- names = textutils.unserialize (fileHandle.readAll())
- fileHandle.close()
- local fileHandle = fs.open (load_save_loc["ids"], 'r')
- ids = textutils.unserialize (fileHandle.readAll())
- fileHandle.close()
- local fileHandle = fs.open (load_save_loc["status"], 'r')
- status = textutils.unserialize (fileHandle.readAll())
- fileHandle.close()
- local fileHandle = fs.open (load_save_loc["eu"], 'r')
- eu = textutils.unserialize (fileHandle.readAll())
- fileHandle.close()
- local fileHandle = fs.open (load_save_loc["mj"], 'r')
- mj = textutils.unserialize (fileHandle.readAll())
- fileHandle.close()
- end
- function save()
- print("Saving...")
- local fileHandle = fs.open (load_save_loc["load_save_loc"], 'w')
- fileHandle.write (textutils.serialize (load_save_loc))
- fileHandle.close()
- local fileHandle = fs.open (load_save_loc["settings"], 'w')
- fileHandle.write (textutils.serialize (settings))
- fileHandle.close()
- local fileHandle = fs.open (load_save_loc["names"], 'w')
- fileHandle.write (textutils.serialize (names))
- fileHandle.close()
- local fileHandle = fs.open (load_save_loc["ids"], 'w')
- fileHandle.write (textutils.serialize (ids))
- fileHandle.close()
- local fileHandle = fs.open (load_save_loc["status"], 'w')
- fileHandle.write (textutils.serialize (status))
- fileHandle.close()
- local fileHandle = fs.open (load_save_loc["eu"], 'w')
- fileHandle.write (textutils.serialize (eu))
- fileHandle.close()
- local fileHandle = fs.open (load_save_loc["mj"], 'w')
- fileHandle.write (textutils.serialize (mj))
- fileHandle.close()
- end
- --PERLIMINARY WORKINGS--
- if not fs.exists("image") or command == "reset" then
- save()
- -- fs.delete("startup")
- -- outputFile = io.open("startup", "a")
- -- text = tostring(shell.getRunningProgram())
- -- outputFile:write("shell.run(" .. text .. ")")
- -- outputFile:close()
- fs.delete("image")
- outputFile = io.open("image", "w")
- outputFile:write("eeeeeeeeeeeeeeeeeeeeeeeeeee")
- outputFile:write("\n")
- outputFile:write("eeeeeffffffffeefffffefffffe")
- outputFile:write("\n")
- outputFile:write("eeeeff0000f0feef000ffff00fe")
- outputFile:write("\n")
- outputFile:write("eeeef0fffffffeef0f00f00f0fe")
- outputFile:write("\n")
- outputFile:write("eeeeff00ffeeeeef0ff000ff0fe")
- outputFile:write("\n")
- outputFile:write("efffffff0feeeeefffff0fffffe")
- outputFile:write("\n")
- outputFile:write("ef0f0000ffeeeeef0fefffef0fe")
- outputFile:write("\n")
- outputFile:write("efffffffffeeeeefffeeeeefffe")
- outputFile:write("\n")
- outputFile:write("eeeeeeeeeeeeeeeeeeeeeeeeeee")
- outputFile:close()
- else
- load()
- end
- function freespace()
- a = "locating free space..."
- print(a)
- x = 1
- repeat
- x = x + 1
- until names[x] == nil
- freespace = x
- a = "free space located at: "
- print(a .. freespace)
- end
- function target()
- a = "locating target..."
- print(a)
- x = 1
- repeat
- x = x + 1
- until names[x] == name1 or names[x] == nil
- if names[x] == name1 then
- target = x
- a = "target located at: "
- print(a .. target)
- else
- print("target button not found")
- target = "not found"
- end
- end
- function add_button()
- a = "adding..."
- print(a)
- freespace()
- names[freespace] = name1
- ids[freespace] = id
- status[freespace] = false
- a = "add successful"
- print(a)
- end
- function remove_button()
- a = "removing..."
- print(a)
- target()
- if target ~= "not found" then
- freespace()
- source = freespace - 1
- names[target] = names[source]
- ids[target] = ids[source]
- status[target] = status[source]
- names[source] = nil
- ids[source] = nil
- status[source] = nil
- a = "removal successful"
- print(a)
- end
- end
- function change_button()
- print("changing " .. name1 .. id .. " to " .. name2 .. id2)
- remove_button()
- name1 = name2
- id = id2
- add_button()
- print("change successful")
- end
- function show_settings()
- x = 2
- while names[x] ~= nil do
- print(x .. ": " .. names[x] .. " " .. ids[x])
- x = x + 1
- end
- os.pullEvent()
- end
- function save2()
- print("confirm save? Y/N : ")
- saving = read()
- saving = tostring(saving)
- if saving == "y" then
- save()
- end
- end
- if command == "add" then
- add_button()
- save2()
- elseif command == "remove" then
- remove_button()
- save2()
- --elseif command == "change" then
- -- change_button()
- -- save2()
- elseif command == "settings" then
- show_settings()
- elseif command == "client" then
- shell.run("pastebin get " .. variables["client"] .. " client")
- end
- function find_peripheral(t, e)
- e = e or nil
- sides = {[1] = "front", [2] = "back", [3] = "left", [4] = "right", [5] = "top", [6] = "bottom"}
- peripheral_found = false
- for num=1, 6 do
- type = peripheral.getType(sides[num])
- if type == t then
- if e == "wireless" then
- if peripheral.call(sides[num], "isWireless") then
- peripheral_side = sides[num]
- peripheral_found = true
- end
- elseif e == "remote" then
- if not peripheral.call(sides[num], "isWireless") then
- peripheral_side = sides[num]
- peripheral_found = true
- end
- elseif e == nil then
- peripheral_side = sides[num]
- peripheral_found = true
- end
- end
- end
- if peripheral_found ~= true then
- error("No " .. t .. " attached to the computer", 0)
- end
- end
- os.setComputerLabel(settings["label"])
- print("Connecting Modem...")
- find_peripheral("modem", "remote")
- mod = peripheral.wrap(peripheral_side)
- --mod = peripheral.wrap(settings["remote_modem"])
- print("Modem Connected")
- local remote_peripherals = mod.getNamesRemote()
- remote_types = {}
- remote_monitors = {}
- remote_rec = {}
- remote_eu = {}
- a = 1
- b = 1
- c = 1
- d = 1
- while remote_peripherals[a] ~= nil do
- remote_types[a] = mod.getTypeRemote(remote_peripherals[a])
- if remote_types[a] == "monitor" then
- remote_monitors[b] = peripheral.wrap(remote_peripherals[a])
- b = b + 1
- end
- if settings["open_peripherals"] and settings["energy_stats"] and settings["direct_power"] then
- if remote_types[a] == "redstone_energy_cell" then
- remote_rec[c] = peripheral.wrap(remote_peripherals[a])
- c = c + 1
- end
- if remote_types[a] == "batbox" then
- remote_eu[d] = peripheral.wrap(remote_peripherals[a])
- d = d + 1
- end
- end
- a = a + 1
- end
- --print("Connecting Monitor...")
- --findPeripheral( "monitor", e )
- --mon = peripheral.wrap(peripheral_side)
- --mon = peripheral.wrap(settings["mon_side"])
- --print("Connected")
- print("Connecting Wireless...")
- find_peripheral("modem", "wireless")
- rednet.open(peripheral_side)
- print("Connected")
- print("calculating...")
- mon = remote_monitors[1]
- mon.setTextScale(settings["text_scale"])
- oa_x, oa_y = mon.getSize()
- if settings["energy_stats"] and settings["open_peripherals"] then
- oa_x = oa_x - 10
- end
- reps_x = oa_x / (settings["button_width"] + 1)
- reps_x = math.floor(reps_x)
- reps_y = oa_y / 2
- reps_y = reps_y - 1
- reps_y = math.floor(reps_y)
- total_buttons = reps_x * reps_y
- print(oa_x .. "," .. oa_y .. " - " .. reps_x .. " x " .. reps_y .. " - " .. total_buttons)
- e = 1
- --FUNCTIONS DRAWING MONITOR SCREEN--
- print("Drawing mon screen")
- function reset_text()
- print("Reset text")
- mon.setTextColor(colors.white)
- mon.setBackgroundColor(colors.black)
- end
- function heading()
- print("heading")
- mon.setCursorPos(2,2)
- mon.setTextColor(settings["heading color"])
- mon.setBackgroundColor(settings["heading background"])
- mon.write(settings["heading"])
- reset_text()
- end
- function button_draw(number, xpos, ypos)
- mon.setCursorPos(xpos, ypos)
- button_x1[number], button_y1[number] = mon.getCursorPos()
- if names[number] ~= nil then
- if status[number] then
- mon.setBackgroundColor(settings["active color"])
- else
- mon.setBackgroundColor(settings["inactive color"])
- end
- text = names[number]:upper()
- text_length = string.len(text)
- text_length = text_length + 2
- if text_length > settings["button_width"] then
- settings["button_width"] = text_length
- end
- mon.write(" " .. text .. " ")
- currpos_x, currpos_y = mon.getCursorPos()
- while currpos_x ~= (settings["button_width"] + xpos) do
- mon.write(" ")
- currpos_x, currpos_y = mon.getCursorPos()
- end
- button_x2[z], y = mon.getCursorPos()
- reset_text()
- print("Adding button " .. number .. ": " .. names[number])
- end
- end
- -- ENERGY FUNCTIONS --
- function box(color, left, top, right, bot)
- term.redirect(mon)
- paintutils.drawLine(right, top, left, top, color)
- paintutils.drawLine(left, top, left, bot, color)
- paintutils.drawLine(right, bot, left, bot, color)
- paintutils.drawLine(right, top, right, bot, color)
- term.restore()
- end
- function decimal(number)
- text = nil
- num = nil
- if number > 999 then
- num = number / 1000
- text = "K"
- if num > 999 then
- num = num / 1000
- text = "M"
- if num > 999 then
- num = num / 1000
- text = "B"
- end
- end
- num = math.floor(num)
- end
- a = num or 0
- text = text or 0
- text = a .. text
- end
- function energy_compile()
- a = 1
- mj["max"] = 0
- mj["current"] = 0
- mj["percent"] = 0
- while remote_rec[a] ~= nil do
- mj["max"] = mj["max"] + remote_rec[a].getMaxEnergyStored()
- mj["current"] = mj["current"] + remote_rec[a].getEnergyStored()
- a = a + 1
- end
- a = 1
- eu["max"] = 0
- eu["current"] = 0
- eu["percent"] = 0
- while remote_eu[a] ~= nil do
- eu["max"] = eu["max"] + remote_eu[a].getCapacity()
- eu["current"] = eu["current"] + remote_eu[a].getStored()
- a = a + 1
- end
- end
- function percentage(type)
- if type == "mj" then
- mj["percent"] = mj["current"] / mj["max"] * 100
- text = math.floor(mj["percent"])
- elseif type == "eu" then
- eu["percent"] = eu["current"] / eu["max"] * 100
- text = math.floor(eu["percent"])
- end
- end
- function greaterx()
- currx = mon.getCursorPos()
- if currx > endx then
- endx = currx
- end
- end
- function energy_stats(startx, starty)
- if settings["direct_power"] then
- energy_compile()
- end
- x = startx + 2
- y = starty + 2
- mon.setTextColor(colors.white)
- mon.setCursorPos(x,y)
- if eu["current"] ~= nil then
- decimal(eu["current"])
- end
- mon.write("EU: " .. text)
- endx = mon.getCursorPos()
- y = y + 1
- mon.setCursorPos(x,y)
- decimal(eu["max"])
- mon.write("EU: " .. text)
- greaterx()
- y = y + 1
- percentage("eu")
- mon.setCursorPos(x,y)
- mon.write("EU: " .. text .. "%")
- greaterx()
- y = y + 2
- decimal(mj["current"])
- mon.setCursorPos(x,y)
- mon.write("MJ: " .. text)
- greaterx()
- y = y + 1
- decimal(mj["max"])
- mon.setCursorPos(x,y)
- mon.write("MJ: " .. text)
- greaterx()
- y = y + 1
- percentage("mj")
- mon.setCursorPos(x,y)
- mon.write("MJ: " .. text .. "%")
- greaterx()
- endy = y + 2
- endx = endx + 1
- box(colors.white, startx, starty, endx, endy)
- end
- -- MONITOR SCREEN PROGRAM --
- function mon_screen()
- screen = 1
- while remote_monitors[screen] ~= nil do
- mon = remote_monitors[screen]
- print("drawing monitor")
- mon.clear()
- heading()
- x = 2
- y = 4
- if settings["energy_stats"] then
- energy_stats(x, y)
- settings["energy_room"] = endx
- x = endx + 1
- end
- z = 1
- for num=1,reps_x do
- y = 4
- for num=1,reps_y do
- button_draw(z, x, y)
- y = y + 2
- z = z + 1
- end
- x = x + settings["button_width"] + 1
- end
- save()
- screen = screen + 1
- end
- end
- --FUNCTION BUTTON DETECT--
- function select_button()
- z = 1
- button_pressed = nil
- repeat
- if names[z] ~= nil then
- if x > button_x1[z] and x < button_x2[z] and y == button_y1[z] then
- button_pressed = z
- end
- end
- z = z + 1
- until button_pressed ~= nil or z > total_buttons
- end
- function message_send()
- if status[button_pressed] then
- text = "deactivate"
- status[button_pressed] = false
- else
- text = "activate"
- status[button_pressed] = true
- end
- rednet.send(ids[button_pressed], text)
- print(names[button_pressed] .. " " .. ids[button_pressed] .. " " .. text)
- mon_screen()
- end
- function offline()
- status[1] = false
- a = 2
- while names[a] ~= nil do
- if status[a] then
- status[a] = false
- text = "deactivate"
- rednet.send(ids[a], text)
- end
- a = a + 1
- end
- mon_screen()
- end
- -- SCREEN SAVER --
- function image_draw()
- screen = 1
- image = {}
- image = paintutils.loadImage("image")
- while remote_monitors[screen] ~= nil do
- print("image: " .. screen)
- mon = remote_monitors[screen]
- mon.clear()
- oa_x, oa_y = mon.getSize()
- term.redirect(mon)
- x = oa_x - settings["image_x"]
- x = math.random(1, x)
- y = oa_y - settings["image_y"]
- y = math.random(1, y)
- paintutils.drawImage(image, x, y)
- term.restore()
- reset_text()
- screen = screen + 1
- end
- end
- function screen_saver()
- print("screen Saver")
- while event ~= "monitor_touch" do
- os.startTimer(settings["refresh_rate"])
- image_draw()
- event, id, x, y = os.pullEvent()
- end
- end
- -- PROGRAM --
- mon_screen()
- while true do
- if settings["direct_power"] or settings["screen_saver"] then
- os.startTimer(settings["refresh_rate"])
- end
- event, id, x, y = os.pullEvent()
- if event == "monitor_touch" then
- select_button()
- e = 1
- if names[button_pressed] ~= nil then
- if button_pressed ~= 1 then
- if status[1] then
- message_send()
- end
- else
- if status[1] then
- offline()
- else
- status[1] = true
- end
- end
- end
- mon_screen()
- elseif event == "rednet_message" then
- if id == settings["eu_id"] then
- eu = textutils.unserialize (x)
- elseif id == settings["mj_id"] then
- mj = textutils.unserialize (x)
- end
- mon_screen()
- elseif event == "timer" then
- print("timer")
- if settings["screen_saver"] then
- e = e + 1
- print("Timeout: " .. e)
- if e == settings["timeout"] then
- screen_saver()
- e = 1
- end
- end
- mon_screen()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment