Advertisement
Wyvern67

Computercraft redstone API

Jun 11th, 2018
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.45 KB | None | 0 0
  1. local tArgs = {...}
  2.  
  3. function printUsage()
  4.     print("Usage: ")
  5.     print("redstone [pulse] <side> <state>")
  6.     print("redstone [pulse] bundle <side1> <color1> [color2] ... [colorN] <state>")
  7.     print("ex: ")
  8.     print("redstone bundle top red blue on")
  9.     print("redstone pulse top")
  10.     print("redstone pulse bundle top white")
  11.     error("Incorrect command")
  12. end
  13.  
  14. function table.search(t, value)
  15.     for i,v in pairs(t) do
  16.         if v == value then
  17.             return i
  18.         end
  19.     end
  20.     return false
  21. end
  22. function isSide(s)
  23.     local sides = {"front", "back", "top", "bottom", "left", "right"}
  24.     return table.search(sides, s)
  25. end
  26.  
  27. function all(state)
  28.     local sides = {"top", "bottom", "left", "right", "front", "back"}
  29.     for i,v in pairs(sides) do
  30.         rs.setOutput(v, state)
  31.         setBundledOutput(v, 65535, state)
  32.     end
  33.     return true
  34. end
  35.  
  36. function colors.decompose(color, state)
  37.     --Arguments: int color, [bool state]
  38.     --The color is a color-sum like (colors.white + colors.orange)
  39.     -- (which is 3)
  40.     --
  41.     --returns t={white=state, orange=state}
  42.  
  43.     if type(color) ~= "number" or color < 0 then
  44.         return false
  45.     end
  46.     if color == 0 then
  47.         return {}
  48.     end
  49.     if state == nil then
  50.         state = true
  51.     end
  52.  
  53.     local colorsName = {"white", "orange", "magenta", "lightBlue", "yellow", "lime", "pink", "gray", "lightGray", "cyan", "purple", "blue", "brown", "green", "red", "black"}
  54.     local a = color
  55.     local i = 1
  56.     local t = {}
  57.     --divide by 2 successively
  58.     --and it works and does stuff because uh
  59.     --uh
  60.     --yeah just look up successive division by 2
  61.     while a > 0 do
  62.         -- if a/2 has a rest then
  63.         if a%2 == 1 then
  64.             t[colorsName[i]] = state
  65.         end
  66.         a = math.floor(a/2)
  67.         i = i+1
  68.     end
  69.     return t
  70. end
  71.  
  72. function table.merge(t1, t2)
  73.     --t2 takes priority
  74.     local tn = {}
  75.     for i,v in pairs(t1) do
  76.         tn[i] = v
  77.     end
  78.     for i,v in pairs(t2) do
  79.         tn[i] = v
  80.     end
  81.     return tn
  82. end
  83.  
  84. --rs.setBundledOutput(side, 0)
  85.  
  86. function setBundledOutput(side, composedColor, state)
  87.     --Example: composedColors = colors.white + colors.orange
  88.     -- that's 3. Because 1 + 2 = 3
  89.     if not tonumber(composedColor) then
  90.         error("Expected string, number, bool. Got string, string")
  91.     end
  92.    
  93.     local newOutputs = colors.decompose(composedColor, state)
  94.     -- newOutputs = {white=state, orange=state}
  95.     local currentOutputs = colors.decompose(rs.getBundledOutput(side))
  96.     --We are currently outputting to the front white cable.
  97.     -- currentOutputs = {white=true}
  98.    
  99.     local outputs = table.merge(currentOutputs, newOutputs)
  100.     --outputs = {white=state, orange=state}
  101.     local colorValue = 0
  102.     for i, v in pairs(outputs) do
  103.         if v == true then
  104.             colorValue = colorValue + colors[i]
  105.         end
  106.     end
  107.     --if state == true,
  108.     --  colorValue = colors.white + colors.orange
  109.     --else,
  110.     --  colorValue = 0
  111.    
  112.     rs.setBundledOutput(side, colorValue)
  113. end
  114.  
  115. function stringToBoolean(s)
  116.     if s == "on" or s == "1" or s == "true"
  117.     then
  118.         return true
  119.     end
  120.  
  121.     if s == "off" or s == "0" or s == "false" then
  122.         return false
  123.     end
  124.  
  125.     return nil
  126. end
  127.  
  128. function pulse(side, duration, state)
  129.     local sleep = sleep
  130.     if duration == nil then
  131.         sleep = function() end
  132.     end
  133.     rs.setOutput(side, state)
  134.     sleep(duration)
  135.     rs.setOutput(side, not state)
  136. end
  137.  
  138. local action, side, duration, state
  139.  
  140. local action = tArgs[1]
  141. if action == "pulse" then
  142.     duration = 1--tonumber(tArgs[3]) or 0.5
  143.     table.remove(tArgs, 1)
  144.     action = tArgs[1]
  145. end
  146.  
  147. if stringToBoolean(action) == false then
  148.     return all(false)
  149. elseif stringToBoolean(action) == true then
  150.     return all(true)
  151. elseif action == "bundle" then
  152.     local cables = {}
  153.     local i,v = 2,nil
  154.     local side = nil
  155.     while i <= #tArgs do
  156.         v = tArgs[i]
  157.         if isSide(v) then
  158.             side = v
  159.             if type(cables[v]) ~= "table" then
  160.                 cables[v] = {}
  161.             end
  162.         elseif stringToBoolean(v) ~= nil then
  163.             if cables[side].state then
  164.                 printUsage()
  165.             end
  166.             cables[side].state = stringToBoolean(v)
  167.         else
  168.             if not side then
  169.                 printUsage()
  170.             end
  171.             cables[side][v] = true
  172.         end
  173.         i=i+1
  174.     end
  175.  
  176.     for sideN,sideV in pairs(cables) do
  177.         --for each side
  178.         for color,_ in pairs(sideV) do
  179.             --of each color
  180.             if color ~= "state" then
  181.                 --print(color .. " cable in " .. sideN .. " is " .. tostring(sideV.state))
  182.                 setBundledOutput(sideN, colors[color], sideV.state)
  183.             end
  184.         end
  185.     end
  186. elseif tArgs[1] ~= nil then --if action == a side
  187.     action = "set"
  188.     side = tArgs[1]
  189.     state = stringToBoolean(tArgs[2])
  190.  
  191.     if state == nil then
  192.         state = not rs.getOutput(side)
  193.     end
  194.  
  195.     return pulse(side, duration, state)
  196. else
  197.     printUsage()
  198. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement