Advertisement
HydrantHunter

WiReD

Aug 1st, 2017
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.87 KB | None | 0 0
  1. --[[     W i R e D     ]]--
  2. --[[    Monitor        ]]--
  3. --[[     Controlled    ]]--
  4. --[[   Bundled Cable   ]]--
  5. --[[     Switcher      ]]--
  6. --[[      by Dog       ]]--
  7. --[[ aka HydrantHunter ]]--
  8. --[[ pastebin:LJKH4wbX ]]--
  9. --[[
  10. Controls bundled cable output on one side of a computer.
  11.  - Side is specified via command line argument. (otherwise side defaults to back)
  12. Requires an 8x wide advanced monitor array. (either directly connected or connected via modem/network cable)
  13. ]]--
  14. if pocket then error("Computer or turtle required", 0) end
  15. local tArgs = { ... }
  16. local configData = "/data/WiReDcfg"
  17. local termX, termY = term.getSize()
  18. local fullColor = term.isColor()
  19. local cable, mon, monSide, monControls
  20. local validSides = { front = true, back = true, top = true, bottom = true, left = true, right = true }
  21. local cableSide = (tArgs[1] and validSides[tArgs[1]]) and tArgs[1] or "back"
  22. local allColors = {
  23.   { name = "White", color = colors.white, state = false, lockState = false };
  24.   { name = "Orange", color = colors.orange, state = false, lockState = false };
  25.   { name = "Magenta", color = colors.magenta, state = false, lockState = false };
  26.   { name = "Light Blue", color = colors.lightBlue, state = false, lockState = false };
  27.   { name = "Yellow", color = colors.yellow, state = false, lockState = false };
  28.   { name = "Lime", color = colors.lime, state = false, lockState = false };
  29.   { name = "Pink", color = colors.pink, state = false, lockState = false };
  30.   { name = "Gray", color = colors.gray, state = false, lockState = false };
  31.   { name = "Light Gray", color = colors.lightGray, state = false, lockState = false };
  32.   { name = "Cyan", color = colors.cyan, state = false, lockState = false };
  33.   { name = "Purple", color = colors.purple, state = false, lockState = false };
  34.   { name = "Blue", color = colors.blue, state = false, lockState = false };
  35.   { name = "Brown", color = colors.brown, state = false, lockState = false };
  36.   { name = "Green", color = colors.green, state = false, lockState = false };
  37.   { name = "Red", color = colors.red, state = false, lockState = false };
  38.   { name = "Black", color = colors.black, state = false, lockState = false };
  39. }
  40.  
  41. --# below is a subset of...
  42. --# cableBuddy 1.0.05 - a basic bundled cable API by Dog
  43. --# pastebin: Sy4zgkXS
  44. --# For instructions and documentation visit http://www.computercraft.info/forums2/index.php?/topic/28274-cablebuddy-a-basic-bundled-cable-api/
  45. do
  46.   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; }
  47.   --local validSides = { top = true; bottom = true; left = true; right = true; front = true; back = true; }
  48.   cable = {
  49.     add = function(...)
  50.       local cbCommands, side, color, newColor = { ... }
  51.       for i = 1, #cbCommands do
  52.         side = cbCommands[i]
  53.         if type(side) == "string" and validSides[side] then
  54.           for j = i + 1, #cbCommands do
  55.             color = cbCommands[j]
  56.             if type(color) == "number" then
  57.               if validColors[color] then
  58.                 newColor = color
  59.               else
  60.                 newColor = 0
  61.                 for colorValue in pairs(validColors) do
  62.                   if colors.test(color, colorValue) then
  63.                     newColor = colors.combine(newColor, colorValue)
  64.                   end
  65.                 end
  66.               end
  67.               if newColor == color then
  68.                 rs.setBundledOutput(side, colors.combine(rs.getBundledOutput(side), color))
  69.               end
  70.             else
  71.               break
  72.             end
  73.           end
  74.         end
  75.       end
  76.     end;
  77.  
  78.     remove = function(...)
  79.       local cbCommands, side, color, newColor = { ... }
  80.       for i = 1, #cbCommands do
  81.         side = cbCommands[i]
  82.         if type(side) == "string" and validSides[side] then
  83.           for j = i + 1, #cbCommands do
  84.             color = cbCommands[j]
  85.             if type(color) == "number" then
  86.               if validColors[color] then
  87.                 newColor = color
  88.               else
  89.                 newColor = 0
  90.                 for colorValue in pairs(validColors) do
  91.                   if colors.test(color, colorValue) then
  92.                     newColor = colors.combine(newColor, colorValue)
  93.                   end
  94.                 end
  95.               end
  96.               if newColor == color then
  97.                 rs.setBundledOutput(side, colors.subtract(rs.getBundledOutput(side), color))
  98.               end
  99.             else
  100.               break
  101.             end
  102.           end
  103.         end
  104.       end
  105.     end;
  106.  
  107.     findOutputColor = function(sSide, nColor)
  108.       if type(sSide) == "string" and type(nColor) == "number" and validSides[sSide] then
  109.         if validColors[nColor] then
  110.           return colors.test(rs.getBundledOutput(sSide), nColor)
  111.         else
  112.           local newColor = 0
  113.           for colorValue in pairs(validColors) do
  114.             if colors.test(nColor, colorValue) and colors.test(rs.getBundledOutput(sSide), colorValue) then
  115.               newColor = colors.combine(newColor, colorValue)
  116.             end
  117.           end
  118.           return newColor == nColor
  119.         end
  120.       end
  121.       return false
  122.     end;
  123.   }
  124. end
  125.  
  126. local function clearMon(bgColor)
  127.   mon.setBackgroundColor(bgColor or colors.black)
  128.   mon.clear()
  129. end
  130.  
  131. do
  132.   local function splitName(i)
  133.     local name = allColors[i].name
  134.     if #name > 8 then
  135.       if string.find(name, " ") then
  136.         local firstPart, lastPart = name:sub(1, string.find(name, " ") - 1), name:sub(string.find(name, " ") + 1)
  137.         if #firstPart <= 8 and #lastPart <= 8 then
  138.           return firstPart, lastPart
  139.         end
  140.       end
  141.       return name:sub(1, 8), name:sub(9)
  142.     else
  143.       return name, ""
  144.     end
  145.   end
  146.  
  147.   local function powerSwitch(x, y, state)
  148.     mon.setCursorPos(x, y)
  149.     mon.setBackgroundColor(state and colors.green or colors.gray)
  150.     mon.write("        ") --# Switch Top
  151.     mon.setBackgroundColor(state and colors.gray or colors.orange)
  152.     mon.setCursorPos(x, y + 1)
  153.     mon.write("        ") --# Switch Bottom
  154.   end
  155.  
  156.   monControls = function()
  157.     local color, state, firstName, lastName
  158.     for i = 1, #allColors do
  159.       firstName, lastName = splitName(i)
  160.       color, state = allColors[i].color, allColors[i].state
  161.       mon.setBackgroundColor(colors.black)
  162.       mon.setTextColor(color == colors.black and colors.white or color)
  163.       mon.setCursorPos((i * 10) - 8, 3)
  164.       mon.write(firstName .. string.rep(" ", 8 - #firstName))
  165.       mon.setCursorPos((i * 10) - 8, 4)
  166.       mon.write(lastName .. string.rep(" ", 8 - #lastName))
  167.       mon.setCursorPos((i * 10) - 8, 8)
  168.       if allColors[i].lockState then
  169.         mon.setTextColor(colors.red)
  170.         mon.write("LOCKED")
  171.       else
  172.         mon.setTextColor(state and colors.green or colors.orange)
  173.         mon.write(state and "ON    " or "OFF   ")
  174.       end
  175.       powerSwitch((i * 10) - 8, 5, state)
  176.     end
  177.   end
  178. end
  179.  
  180. local function monControlsStatic()
  181.   clearMon()
  182.   --# Header
  183.   local labelText = "W i R e D - Bundled Cabled Manager [" .. cableSide .. "]"
  184.   local monLabel = string.rep(" ", math.floor((monX / 2) - (#labelText / 2))) .. labelText .. string.rep(" ", math.ceil((monX / 2) - (#labelText / 2)))
  185.   mon.setBackgroundColor(colors.gray)
  186.   mon.setTextColor(colors.cyan)
  187.   mon.setCursorPos(1, 1)
  188.   mon.write(monLabel)
  189.   --# Footer
  190.   mon.setCursorPos(1, monY)
  191.   mon.setBackgroundColor(colors.gray)
  192.   mon.write(string.rep(" ", monX))
  193.   mon.setTextColor(colors.red)
  194.   mon.setCursorPos((monX / 4) - 2, monY)
  195.   mon.write("LOCK")
  196.   mon.setTextColor(colors.green)
  197.   mon.setCursorPos((monX - (monX / 4)) - 2, monY)
  198.   mon.write("UNLOCK")
  199.   mon.setCursorPos(math.floor((monX / 2) - 1), monY)
  200.   mon.setBackgroundColor(colors.black)
  201.   mon.write("   ")
  202. end
  203.  
  204. local function termScreen()
  205.   local yPos = 2
  206.   for i = 1, #allColors do
  207.     yPos, lockState = yPos + 1, allColors[i].lockState
  208.     term.setCursorPos(20, yPos)
  209.     term.setBackgroundColor(i % 2 ~= 0 and (fullColor and colors.gray or colors.black) or colors.black)
  210.     term.setTextColor(allColors[i].state and (fullColor and colors.green or colors.white) or (fullColor and colors.red or colors.white))
  211.     term.write(allColors[i].state and "ON " or "OFF")
  212.     term.setCursorPos(25, yPos)
  213.     term.setTextColor(lockState and (fullColor and colors.red or colors.white) or (fullColor and colors.green or colors.white))
  214.     term.write(lockState and "LOCKED  " or "UNLOCKED")
  215.   end
  216. end
  217.  
  218. local function termScreenStatic()
  219.   local yPos = 2
  220.   for i = 1, #allColors do
  221.     yPos, color = yPos + 1, allColors[i].color
  222.     term.setBackgroundColor(colors.black)
  223.     term.setTextColor(fullColor and (color == colors.black and colors.white or color) or colors.white)
  224.     term.setCursorPos(1, yPos)
  225.     if i % 2 ~= 0 then
  226.       term.setBackgroundColor(fullColor and colors.gray or colors.black)
  227.       term.write(string.rep(" ", termX))
  228.       term.setCursorPos(1, yPos)
  229.     end
  230.     term.write(allColors[i].name)
  231.     term.setTextColor(colors.lightGray)
  232.     term.write(":")
  233.   end
  234. end
  235.  
  236. local function userInput()
  237.   local event, data, posX, posY, continue, ccUpdate
  238.   while true do
  239.     event, data, posX, posY = os.pullEvent()
  240.     if event == "monitor_touch" and data == monSide then
  241.       ccUpdate = false
  242.       continue = true
  243.       if posY == monY and (posX < monX / 2 or posX > monX / 2) then
  244.         for i = 1, #allColors do
  245.           allColors[i].lockState = posX < monX / 2
  246.         end
  247.         ccUpdate, continue = true, false
  248.       end
  249.       if continue then
  250.         for i = 1, #allColors do
  251.           if posX >= (i * 10) - 8 and posX <= (i * 10) - 1 then
  252.             if posY == 8 then
  253.               allColors[i].lockState = not allColors[i].lockState
  254.               ccUpdate = true
  255.               break
  256.             elseif posY > 4 and posY < 7 then
  257.               if not allColors[i].lockState then
  258.                 allColors[i].state = not allColors[i].state
  259.                 if allColors[i].state then
  260.                   cable.add(cableSide, allColors[i].color)
  261.                 else
  262.                   cable.remove(cableSide, allColors[i].color)
  263.                 end
  264.                 ccUpdate = true
  265.               end
  266.               break
  267.             end
  268.           end
  269.         end
  270.       end
  271.       if ccUpdate then
  272.         if not turtle then termScreen() end
  273.         monControls()
  274.         ccUpdate = false
  275.       end
  276.     elseif event == "char" and data:lower() == "q" then
  277.       clearMon()
  278.       term.setBackgroundColor(colors.black)
  279.       term.setTextColor(colors.white)
  280.       term.setCursorPos(1, 1)
  281.       term.clear()
  282.       term.write("WiReD OFFLINE")
  283.       term.setCursorPos(1, 3)
  284.       return
  285.     end
  286.   end
  287. end
  288.  
  289. for _, side in pairs(rs.getSides()) do
  290.   if peripheral.isPresent(side) and peripheral.getType(side) == "monitor" and peripheral.call(side, "isColor") then
  291.     peripheral.call(side, "setTextScale", 0.5)
  292.     monX, monY = peripheral.call(side, "getSize")
  293.     if monX >= 164 then
  294.       mon = peripheral.wrap(side)
  295.       monSide = side
  296.       break
  297.     end
  298.   elseif peripheral.getType(side) == "modem" and not peripheral.call(side, "isWireless") then
  299.     for _, name in pairs(peripheral.call(side, "getNamesRemote")) do
  300.       if peripheral.getType(name) == "monitor" and peripheral.call(name, "isColor") then
  301.         peripheral.call(name, "setTextScale", 0.5)
  302.         monX, monY = peripheral.call(name, "getSize")
  303.         if monX >= 164 then
  304.           mon = peripheral.wrap(name)
  305.           monSide = name
  306.           break
  307.         end
  308.       end
  309.     end
  310.     if monSide then break end
  311.   end
  312. end
  313. if not mon then error("8-wide advanced monitor array required", 0) end
  314. clearMon(colors.white)
  315. mon.setTextColor(colors.black)
  316. mon.setCursorPos(2, 3)
  317. mon.write("Initializing...")
  318. term.setBackgroundColor(colors.black)
  319. term.clear()
  320. term.setCursorPos(1, 1)
  321. if not fs.exists(configData) then
  322.   if not fs.exists("/data") then fs.makeDir("/data") end
  323.   print("Please name each color. Enter nothing to skip.")
  324.   print("Names are truncated to 16 characters.\n")
  325.   for i = 1, #allColors do
  326.     print(allColors[i].name)
  327.     local colorName = read()
  328.     if colorName ~= "" then allColors[i].name = colorName:sub(1, 16) end
  329.     print("")
  330.   end
  331.   local config = fs.open(configData, "w")
  332.   config.write(textutils.serialize(allColors))
  333.   config.close()
  334.   term.clear()
  335.   term.setCursorPos(1, 1)
  336. else
  337.   for i = #allColors, 1, -1 do
  338.     allColors[i] = nil
  339.   end
  340.   local config = fs.open(configData, "r")
  341.   allColors = textutils.unserialize(config.readAll())
  342.   config.close()
  343. end
  344. for i = 1, #allColors do
  345.   allColors[i].state = cable.findOutputColor(cableSide, allColors[i].color)
  346. end
  347. term.write("WiReD running...")
  348. if not turtle then
  349.   termScreenStatic()
  350.   termScreen()
  351. end
  352. monControlsStatic()
  353. monControls()
  354. userInput()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement