Advertisement
HydrantHunter

cableBuddy

Jan 29th, 2017
435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.80 KB | None | 0 0
  1. --# cableBuddy 1.0.05 - a basic bundled cable API by Dog
  2. --# pastebin: Sy4zgkXS
  3. --# For instructions and documentation visit http://www.computercraft.info/forums2/index.php?/topic/28274-cablebuddy-a-basic-bundled-cable-api/
  4. 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; }
  5. local validSides = { top = true; bottom = true; left = true; right = true; front = true; back = true; }
  6. local cable = {
  7.   set = function(...)
  8.     local cbCommands, side, color, newColor = { ... }
  9.     for i = 1, #cbCommands, 2 do
  10.       side = cbCommands[i]
  11.       if type(side) == "string" and validSides[side] then
  12.         color = cbCommands[i + 1]
  13.         if type(color) == "number" then
  14.           if validColors[color] then
  15.             newColor = color
  16.           else
  17.             newColor = 0
  18.             for colorValue in pairs(validColors) do
  19.               if colors.test(color, colorValue) then
  20.                 newColor = colors.combine(newColor, colorValue)
  21.               end
  22.             end
  23.           end
  24.           if newColor == color then
  25.             rs.setBundledOutput(side, color)
  26.           end
  27.         end
  28.       end
  29.     end
  30.   end;
  31.  
  32.   add = function(...)
  33.     local cbCommands, side, color, newColor = { ... }
  34.     for i = 1, #cbCommands do
  35.       side = cbCommands[i]
  36.       if type(side) == "string" and validSides[side] then
  37.         for j = i + 1, #cbCommands do
  38.           color = cbCommands[j]
  39.           if type(color) == "number" then
  40.             if validColors[color] then
  41.               newColor = color
  42.             else
  43.               newColor = 0
  44.               for colorValue in pairs(validColors) do
  45.                 if colors.test(color, colorValue) then
  46.                   newColor = colors.combine(newColor, colorValue)
  47.                 end
  48.               end
  49.             end
  50.             if newColor == color then
  51.               rs.setBundledOutput(side, colors.combine(rs.getBundledOutput(side), color))
  52.             end
  53.           else
  54.             break
  55.           end
  56.         end
  57.       end
  58.     end
  59.   end;
  60.  
  61.   remove = function(...)
  62.     local cbCommands, side, color, newColor = { ... }
  63.     for i = 1, #cbCommands do
  64.       side = cbCommands[i]
  65.       if type(side) == "string" and validSides[side] then
  66.         for j = i + 1, #cbCommands do
  67.           color = cbCommands[j]
  68.           if type(color) == "number" then
  69.             if validColors[color] then
  70.               newColor = color
  71.             else
  72.               newColor = 0
  73.               for colorValue in pairs(validColors) do
  74.                 if colors.test(color, colorValue) then
  75.                   newColor = colors.combine(newColor, colorValue)
  76.                 end
  77.               end
  78.             end
  79.             if newColor == color then
  80.               rs.setBundledOutput(side, colors.subtract(rs.getBundledOutput(side), color))
  81.             end
  82.           else
  83.             break
  84.           end
  85.         end
  86.       end
  87.     end
  88.   end;
  89.  
  90.   off = function(...)
  91.     local cbCommands, side = { ... }
  92.     for i = 1, #cbCommands do
  93.       side = cbCommands[i]
  94.       if type(side) == "string" and validSides[side] then
  95.         rs.setBundledOutput(side, 0)
  96.       end
  97.     end
  98.   end;
  99.  
  100.   getInput = function(sSide)
  101.     if type(sSide) == "string" and validSides[sSide] then
  102.       return rs.getBundledInput(sSide)
  103.     end
  104.     return 0
  105.   end;
  106.  
  107.   getOutput = function(sSide)
  108.     if type(sSide) == "string" and validSides[sSide] then
  109.       return rs.getBundledOutput(sSide)
  110.     end
  111.     return 0
  112.   end;
  113.  
  114.   findInputColor = function(sSide, nColor)
  115.     if type(sSide) == "string" and type(nColor) == "number" and validSides[sSide] then
  116.       if validColors[nColor] then
  117.         return rs.testBundledInput(sSide, nColor)
  118.       else
  119.         local newColor = 0
  120.         for colorValue in pairs(validColors) do
  121.           if colors.test(nColor, colorValue) and rs.testBundledInput(sSide, colorValue) then
  122.             newColor = colors.combine(newColor, colorValue)
  123.           end
  124.         end
  125.         return newColor == nColor
  126.       end
  127.     end
  128.     return false
  129.   end;
  130.  
  131.   findOutputColor = function(sSide, nColor)
  132.     if type(sSide) == "string" and type(nColor) == "number" and validSides[sSide] then
  133.       if validColors[nColor] then
  134.         return colors.test(rs.getBundledOutput(sSide), nColor)
  135.       else
  136.         local newColor = 0
  137.         for colorValue in pairs(validColors) do
  138.           if colors.test(nColor, colorValue) and colors.test(rs.getBundledOutput(sSide), colorValue) then
  139.             newColor = colors.combine(newColor, colorValue)
  140.           end
  141.         end
  142.         return newColor == nColor
  143.       end
  144.     end
  145.     return false
  146.   end;
  147. }
  148. return cable
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement