Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ W i R e D ]]--
- --[[ Monitor ]]--
- --[[ Controlled ]]--
- --[[ Bundled Cable ]]--
- --[[ Switcher ]]--
- --[[ by Dog ]]--
- --[[ aka HydrantHunter ]]--
- --[[ pastebin:LJKH4wbX ]]--
- --[[
- Controls bundled cable output on one side of a computer.
- - Side is specified via command line argument. (otherwise side defaults to back)
- Requires an 8x wide advanced monitor array. (either directly connected or connected via modem/network cable)
- ]]--
- if pocket then error("Computer or turtle required", 0) end
- local tArgs = { ... }
- local configData = "/data/WiReDcfg"
- local termX, termY = term.getSize()
- local fullColor = term.isColor()
- local cable, mon, monSide, monControls
- local validSides = { front = true, back = true, top = true, bottom = true, left = true, right = true }
- local cableSide = (tArgs[1] and validSides[tArgs[1]]) and tArgs[1] or "back"
- local allColors = {
- { name = "White", color = colors.white, state = false, lockState = false };
- { name = "Orange", color = colors.orange, state = false, lockState = false };
- { name = "Magenta", color = colors.magenta, state = false, lockState = false };
- { name = "Light Blue", color = colors.lightBlue, state = false, lockState = false };
- { name = "Yellow", color = colors.yellow, state = false, lockState = false };
- { name = "Lime", color = colors.lime, state = false, lockState = false };
- { name = "Pink", color = colors.pink, state = false, lockState = false };
- { name = "Gray", color = colors.gray, state = false, lockState = false };
- { name = "Light Gray", color = colors.lightGray, state = false, lockState = false };
- { name = "Cyan", color = colors.cyan, state = false, lockState = false };
- { name = "Purple", color = colors.purple, state = false, lockState = false };
- { name = "Blue", color = colors.blue, state = false, lockState = false };
- { name = "Brown", color = colors.brown, state = false, lockState = false };
- { name = "Green", color = colors.green, state = false, lockState = false };
- { name = "Red", color = colors.red, state = false, lockState = false };
- { name = "Black", color = colors.black, state = false, lockState = false };
- }
- --# below is a subset of...
- --# cableBuddy 1.0.05 - a basic bundled cable API by Dog
- --# pastebin: Sy4zgkXS
- --# For instructions and documentation visit http://www.computercraft.info/forums2/index.php?/topic/28274-cablebuddy-a-basic-bundled-cable-api/
- do
- local validColors = { [1] = true; [2] = true; [4] = true; [8] = true; [16] = true; [32] = true; [64] = true; [128] = true; [256] = true; [512] = true; [1024] = true; [2048] = true; [4096] = true; [8192] = true; [16384] = true, [32768] = true; }
- --local validSides = { top = true; bottom = true; left = true; right = true; front = true; back = true; }
- cable = {
- add = function(...)
- local cbCommands, side, color, newColor = { ... }
- for i = 1, #cbCommands do
- side = cbCommands[i]
- if type(side) == "string" and validSides[side] then
- for j = i + 1, #cbCommands do
- color = cbCommands[j]
- if type(color) == "number" then
- if validColors[color] then
- newColor = color
- else
- newColor = 0
- for colorValue in pairs(validColors) do
- if colors.test(color, colorValue) then
- newColor = colors.combine(newColor, colorValue)
- end
- end
- end
- if newColor == color then
- rs.setBundledOutput(side, colors.combine(rs.getBundledOutput(side), color))
- end
- else
- break
- end
- end
- end
- end
- end;
- remove = function(...)
- local cbCommands, side, color, newColor = { ... }
- for i = 1, #cbCommands do
- side = cbCommands[i]
- if type(side) == "string" and validSides[side] then
- for j = i + 1, #cbCommands do
- color = cbCommands[j]
- if type(color) == "number" then
- if validColors[color] then
- newColor = color
- else
- newColor = 0
- for colorValue in pairs(validColors) do
- if colors.test(color, colorValue) then
- newColor = colors.combine(newColor, colorValue)
- end
- end
- end
- if newColor == color then
- rs.setBundledOutput(side, colors.subtract(rs.getBundledOutput(side), color))
- end
- else
- break
- end
- end
- end
- end
- end;
- findOutputColor = function(sSide, nColor)
- if type(sSide) == "string" and type(nColor) == "number" and validSides[sSide] then
- if validColors[nColor] then
- return colors.test(rs.getBundledOutput(sSide), nColor)
- else
- local newColor = 0
- for colorValue in pairs(validColors) do
- if colors.test(nColor, colorValue) and colors.test(rs.getBundledOutput(sSide), colorValue) then
- newColor = colors.combine(newColor, colorValue)
- end
- end
- return newColor == nColor
- end
- end
- return false
- end;
- }
- end
- local function clearMon(bgColor)
- mon.setBackgroundColor(bgColor or colors.black)
- mon.clear()
- end
- do
- local function splitName(i)
- local name = allColors[i].name
- if #name > 8 then
- if string.find(name, " ") then
- local firstPart, lastPart = name:sub(1, string.find(name, " ") - 1), name:sub(string.find(name, " ") + 1)
- if #firstPart <= 8 and #lastPart <= 8 then
- return firstPart, lastPart
- end
- end
- return name:sub(1, 8), name:sub(9)
- else
- return name, ""
- end
- end
- local function powerSwitch(x, y, state)
- mon.setCursorPos(x, y)
- mon.setBackgroundColor(state and colors.green or colors.gray)
- mon.write(" ") --# Switch Top
- mon.setBackgroundColor(state and colors.gray or colors.orange)
- mon.setCursorPos(x, y + 1)
- mon.write(" ") --# Switch Bottom
- end
- monControls = function()
- local color, state, firstName, lastName
- for i = 1, #allColors do
- firstName, lastName = splitName(i)
- color, state = allColors[i].color, allColors[i].state
- mon.setBackgroundColor(colors.black)
- mon.setTextColor(color == colors.black and colors.white or color)
- mon.setCursorPos((i * 10) - 8, 3)
- mon.write(firstName .. string.rep(" ", 8 - #firstName))
- mon.setCursorPos((i * 10) - 8, 4)
- mon.write(lastName .. string.rep(" ", 8 - #lastName))
- mon.setCursorPos((i * 10) - 8, 8)
- if allColors[i].lockState then
- mon.setTextColor(colors.red)
- mon.write("LOCKED")
- else
- mon.setTextColor(state and colors.green or colors.orange)
- mon.write(state and "ON " or "OFF ")
- end
- powerSwitch((i * 10) - 8, 5, state)
- end
- end
- end
- local function monControlsStatic()
- clearMon()
- --# Header
- local labelText = "W i R e D - Bundled Cabled Manager [" .. cableSide .. "]"
- local monLabel = string.rep(" ", math.floor((monX / 2) - (#labelText / 2))) .. labelText .. string.rep(" ", math.ceil((monX / 2) - (#labelText / 2)))
- mon.setBackgroundColor(colors.gray)
- mon.setTextColor(colors.cyan)
- mon.setCursorPos(1, 1)
- mon.write(monLabel)
- --# Footer
- mon.setCursorPos(1, monY)
- mon.setBackgroundColor(colors.gray)
- mon.write(string.rep(" ", monX))
- mon.setTextColor(colors.red)
- mon.setCursorPos((monX / 4) - 2, monY)
- mon.write("LOCK")
- mon.setTextColor(colors.green)
- mon.setCursorPos((monX - (monX / 4)) - 2, monY)
- mon.write("UNLOCK")
- mon.setCursorPos(math.floor((monX / 2) - 1), monY)
- mon.setBackgroundColor(colors.black)
- mon.write(" ")
- end
- local function termScreen()
- local yPos = 2
- for i = 1, #allColors do
- yPos, lockState = yPos + 1, allColors[i].lockState
- term.setCursorPos(20, yPos)
- term.setBackgroundColor(i % 2 ~= 0 and (fullColor and colors.gray or colors.black) or colors.black)
- term.setTextColor(allColors[i].state and (fullColor and colors.green or colors.white) or (fullColor and colors.red or colors.white))
- term.write(allColors[i].state and "ON " or "OFF")
- term.setCursorPos(25, yPos)
- term.setTextColor(lockState and (fullColor and colors.red or colors.white) or (fullColor and colors.green or colors.white))
- term.write(lockState and "LOCKED " or "UNLOCKED")
- end
- end
- local function termScreenStatic()
- local yPos = 2
- for i = 1, #allColors do
- yPos, color = yPos + 1, allColors[i].color
- term.setBackgroundColor(colors.black)
- term.setTextColor(fullColor and (color == colors.black and colors.white or color) or colors.white)
- term.setCursorPos(1, yPos)
- if i % 2 ~= 0 then
- term.setBackgroundColor(fullColor and colors.gray or colors.black)
- term.write(string.rep(" ", termX))
- term.setCursorPos(1, yPos)
- end
- term.write(allColors[i].name)
- term.setTextColor(colors.lightGray)
- term.write(":")
- end
- end
- local function userInput()
- local event, data, posX, posY, continue, ccUpdate
- while true do
- event, data, posX, posY = os.pullEvent()
- if event == "monitor_touch" and data == monSide then
- ccUpdate = false
- continue = true
- if posY == monY and (posX < monX / 2 or posX > monX / 2) then
- for i = 1, #allColors do
- allColors[i].lockState = posX < monX / 2
- end
- ccUpdate, continue = true, false
- end
- if continue then
- for i = 1, #allColors do
- if posX >= (i * 10) - 8 and posX <= (i * 10) - 1 then
- if posY == 8 then
- allColors[i].lockState = not allColors[i].lockState
- ccUpdate = true
- break
- elseif posY > 4 and posY < 7 then
- if not allColors[i].lockState then
- allColors[i].state = not allColors[i].state
- if allColors[i].state then
- cable.add(cableSide, allColors[i].color)
- else
- cable.remove(cableSide, allColors[i].color)
- end
- ccUpdate = true
- end
- break
- end
- end
- end
- end
- if ccUpdate then
- if not turtle then termScreen() end
- monControls()
- ccUpdate = false
- end
- elseif event == "char" and data:lower() == "q" then
- clearMon()
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(1, 1)
- term.clear()
- term.write("WiReD OFFLINE")
- term.setCursorPos(1, 3)
- return
- end
- end
- end
- for _, side in pairs(rs.getSides()) do
- if peripheral.isPresent(side) and peripheral.getType(side) == "monitor" and peripheral.call(side, "isColor") then
- peripheral.call(side, "setTextScale", 0.5)
- monX, monY = peripheral.call(side, "getSize")
- if monX >= 164 then
- mon = peripheral.wrap(side)
- monSide = side
- break
- end
- elseif peripheral.getType(side) == "modem" and not peripheral.call(side, "isWireless") then
- for _, name in pairs(peripheral.call(side, "getNamesRemote")) do
- if peripheral.getType(name) == "monitor" and peripheral.call(name, "isColor") then
- peripheral.call(name, "setTextScale", 0.5)
- monX, monY = peripheral.call(name, "getSize")
- if monX >= 164 then
- mon = peripheral.wrap(name)
- monSide = name
- break
- end
- end
- end
- if monSide then break end
- end
- end
- if not mon then error("8-wide advanced monitor array required", 0) end
- clearMon(colors.white)
- mon.setTextColor(colors.black)
- mon.setCursorPos(2, 3)
- mon.write("Initializing...")
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1, 1)
- if not fs.exists(configData) then
- if not fs.exists("/data") then fs.makeDir("/data") end
- print("Please name each color. Enter nothing to skip.")
- print("Names are truncated to 16 characters.\n")
- for i = 1, #allColors do
- print(allColors[i].name)
- local colorName = read()
- if colorName ~= "" then allColors[i].name = colorName:sub(1, 16) end
- print("")
- end
- local config = fs.open(configData, "w")
- config.write(textutils.serialize(allColors))
- config.close()
- term.clear()
- term.setCursorPos(1, 1)
- else
- for i = #allColors, 1, -1 do
- allColors[i] = nil
- end
- local config = fs.open(configData, "r")
- allColors = textutils.unserialize(config.readAll())
- config.close()
- end
- for i = 1, #allColors do
- allColors[i].state = cable.findOutputColor(cableSide, allColors[i].color)
- end
- term.write("WiReD running...")
- if not turtle then
- termScreenStatic()
- termScreen()
- end
- monControlsStatic()
- monControls()
- userInput()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement