Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Script by Savage_Me55iah of CraftAU.com.au--
- --Version 1.7--
- --Needs corresponding "Savage Client v1.7" 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 = {}
- relays = {}
- function add_button(command, id, on_off)
- num = table.maxn(names) + 1
- 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",
- ["relays"] = "relays"
- }
- local settings = {
- ["mon_side"] = "top",
- ["wireless_side"] = "left",
- ["remote_modem"] = "right",
- ["heading color"] = colors.yellow,
- ["heading background"] = colors.black,
- ["heading"] = "Savage Monitor v1.7",
- ["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",
- ["output screen"] = false,
- ["relay"] = false
- }
- 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
- }
- if not fs.exists("settings") then print("Enter computer label: ") os.setComputerLabel(read() or settings["label"]) settings["label"] = os.getComputerLabel() end
- --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()
- local fileHandle = fs.open (load_save_loc["relays"], 'r')
- relays = 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()
- local fileHandle = fs.open (load_save_loc["relays"], 'w')
- fileHandle.write (textutils.serialize (relays))
- fileHandle.close()
- end
- --PERLIMINARY WORKINGS--
- function imagewrite(code)
- outputFile:write("\n")
- outputFile:write(code)
- end
- image_table = {[1] = "eeeeeeeeeeeeeeeeeeeeeeeeeee",
- [2] = "eeeeeffffffffeefffffefffffe",
- [3] = "eeeeff0000f0feef000ffff00fe",
- [4] = "eeeef0fffffffeef0f00f00f0fe",
- [5] = "eeeeff00ffeeeeef0ff000ff0fe",
- [6] = "efffffff0feeeeefffff0fffffe",
- [7] = "ef0f0000ffeeeeef0fefffef0fe",
- [8] = "efffffffffeeeeefffeeeeefffe",
- [9] = "eeeeeeeeeeeeeeeeeeeeeeeeeee"
- }
- if not fs.exists("image") 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(image_table[1])
- for num = 2,table.maxn(image_table) do
- imagewrite(image_table[num])
- end
- num = nil
- outputFile:close()
- else
- load()
- end
- function freespace()
- a = "locating free space..."
- print(a)
- freespace = table.maxn(names) + 1
- 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 : ")
- if tostring(read()) == "y" then
- save()
- end
- end
- function add_relay()
- num = table.maxn(relays) + 1
- relays[num] = tonumber(id)
- save2()
- 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")
- elseif command == "addr" then
- add_relay()
- 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
- return sides[num]
- end
- elseif e == "remote" then
- if not peripheral.call(sides[num], "isWireless") then
- return sides[num]
- end
- elseif e == nil then
- return sides[num]
- end
- end
- end
- -- print("No " .. t .. " attached to the computer")
- end
- -- MODEM REMOTE CONNECTIONS --
- remote_monitors = {}
- mod = find_peripheral("modem", "remote") or nil
- mon = find_peripheral("monitor") or nil
- if mon ~= nil then remote_monitors[1] = peripheral.wrap(mon) end
- if mod ~= nil then
- mod = peripheral.wrap(mod)
- local remote_peripherals = mod.getNamesRemote()
- remote_types = {}
- remote_rec = {}
- remote_eu = {}
- a = 1
- if remote_monitors[1] == nil then b = 1 else b = 2 end
- 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] == "cofh_thermalexpansion_energycell" then
- remote_rec[c] = peripheral.wrap(remote_peripherals[a])
- c = c + 1
- end
- if remote_types[a] == "mfsu" then
- remote_eu[d] = peripheral.wrap(remote_peripherals[a])
- d = d + 1
- end
- end
- a = a + 1
- end
- end
- rednet.open(find_peripheral("modem", "wireless"))
- if not settings["output screen"] then remote_monitors[1] = term end
- 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 = math.floor(oa_x / (settings["button_width"] + 1))
- reps_y = math.floor((oa_y / 2) - 1)
- total_buttons = reps_x * reps_y
- --print(oa_x .. "," .. oa_y .. " - " .. reps_x .. " x " .. reps_y .. " - " .. total_buttons)
- --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"] .. ": " .. settings["label"] .. ":" .. settings["id"])
- 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) + 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("Writing button " .. number .. ": " .. names[number] .. ": " .. tostring(status[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
- return text
- end
- function energy_compile()
- local 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("north")
- mj["current"] = mj["current"] + remote_rec[a].getEnergyStored("north")
- a = a + 1
- end
- local 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
- return math.floor(mj["percent"])
- elseif type == "eu" then
- eu["percent"] = eu["current"] / eu["max"] * 100
- return 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)
- mon.write("EU: " .. decimal(eu["current"]))
- endx = mon.getCursorPos()
- y = y + 1
- mon.setCursorPos(x,y)
- mon.write("EU: " .. decimal(eu["max"]))
- greaterx()
- y = y + 1
- mon.setCursorPos(x,y)
- mon.write("EU: " .. percentage("eu") .. "%")
- greaterx()
- y = y + 2
- mon.setCursorPos(x,y)
- mon.write("MJ: " .. decimal(mj["current"]) )
- greaterx()
- y = y + 1
- mon.setCursorPos(x,y)
- mon.write("MJ: " .. decimal(mj["max"]) )
- greaterx()
- y = y + 1
- mon.setCursorPos(x,y)
- mon.write("MJ: " .. percentage("mj") .. "%")
- 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 relay(command)
- if command then
- for num = 2,table.maxn(relays) do rednet.send(relays[num], textutils.serialize(names)) end
- sleep(0.1)
- for num = 2,table.maxn(relays) do rednet.send(relays[num], textutils.serialize(ids)) end
- sleep(0.1)
- for num = 2,table.maxn(relays) do rednet.send(relays[num], textutils.serialize(status)) end
- else
- rednet.send(relays[1], textutils.serialize(names))
- sleep(0.1)
- rednet.send(relays[1], textutils.serialize(ids))
- sleep(0.1)
- rednet.send(relays[1], textutils.serialize(status))
- end
- end
- 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
- return z
- end
- end
- z = z + 1
- until z > total_buttons
- end
- function message_send()
- if status[button_pressed] then
- text = "deactivate"
- status[button_pressed] = not status[button_pressed]
- else
- text = "activate"
- status[button_pressed] = not status[button_pressed]
- end
- rednet.send(ids[button_pressed], text)
- -- print(names[button_pressed] .. " " .. ids[button_pressed] .. " " .. text)
- 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 / 2) - (settings["image_x"] / 2)
- y = (oa_y / 2) - (settings["image_y"] / 2)
- paintutils.drawImage(image, x, y)
- term.restore()
- reset_text()
- screen = screen + 1
- end
- end
- function screen_saver()
- -- print("screen Saver")
- image_draw()
- os.pullEvent("monitor_touch")
- end
- -- PROGRAM --
- e = 1
- mon_screen()
- while true do
- if settings["direct_power"] or settings["screen_saver"] or not settings["output screen"] then
- os.startTimer(settings["refresh_rate"])
- end
- event, id, x, y = os.pullEvent()
- if event == "monitor_touch" or event == "mouse_click" then
- button_pressed = 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
- -- startup_buttons
- end
- end
- end
- relay(settings["relay"])
- elseif event == "rednet_message" then
- text1 = x
- e, i, text2 =os.pullEvent("rednet_message")
- e, i, text3 =os.pullEvent("rednet_message")
- names = textutils.unserialize (text1)
- ids = textutils.unserialize (text2)
- status = textutils.unserialize (text3)
- if settings["relay"] then relay(settings["relay"]) end
- 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
- end
- mon_screen()
- end
Advertisement
Add Comment
Please, Sign In to add comment