Advertisement
Wyvern67

Making rs.setBundledCable() consistent with the redstone api

Jan 13th, 2017
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.02 KB | None | 0 0
  1. --Takes a color sum like (colors.white + colors.orange) and a boolean
  2. -- (which is 3)
  3. --
  4. --returns t={white=state, orange=state}
  5. function colors.decompose(color, state)
  6.     if type(color) ~= "number" or color < 0 then
  7.         return false
  8.     end
  9.     if color == 0 then
  10.         return {}
  11.     end
  12.     if state == nil then
  13.         state = true
  14.     end
  15.  
  16.     local colorsName = {"white", "orange", "magenta", "lightBlue", "yellow", "lime", "pink", "gray", "lightGray", "cyan", "purple", "blue", "brown", "green", "red", "black"}
  17.     local a = color
  18.     local i = 1
  19.     local t = {}
  20.  
  21.     --convert an integer to binary
  22.     --Method: Do an euclidian division over and over again on the color
  23.     --check every time if there is a rest
  24.     --if there is indeed a rest, it means that the bit is true and that the corresponding color is in the sum
  25.     while a > 0 do
  26.         -- if a/2 has a rest then
  27.         if a%2 == 1 then
  28.             t[colorsName[i]] = state
  29.         end
  30.         a = math.floor(a/2)
  31.         i = i+1
  32.     end
  33.     return t
  34. end
  35.  
  36. --Merges two tables
  37. --t2 takes priority
  38. function table.merge(t1, t2)
  39.     local tn = {}
  40.     for i,v in pairs(t1) do
  41.         tn[i] = v
  42.     end
  43.     for i,v in pairs(t2) do
  44.         tn[i] = v
  45.     end
  46.     return tn
  47. end
  48.  
  49. function newSetBundledOutput(side, composedColor, state)
  50.     --Example: composedColors = colors.white + colors.orange
  51.     -- that's 3. Because 1 + 2 = 3
  52.     if not tonumber(composedColor) then
  53.         error("Expected string, number, bool. Got string, string")
  54.     end
  55.    
  56.     local newOutputs = colors.decompose(composedColor, state)
  57.     -- newOutputs = {white=state, orange=state}
  58.     local currentOutputs = colors.decompose(rs.getBundledOutput(side))
  59.     --We are currently outputting to the front white cable.
  60.     -- currentOutputs = {white=true}
  61.    
  62.     local outputs = table.merge(currentOutputs, newOutputs)
  63.     --outputs = {white=state, orange=state}
  64.     local colorValue = 0
  65.     for i, v in pairs(outputs) do
  66.         if v == true then
  67.             colorValue = colorValue + colors[i]
  68.         end
  69.     end
  70.     --if state == true,
  71.     --  colorValue = colors.white + colors.orange
  72.     --else,
  73.     --  colorValue = 0
  74.    
  75.     rs.setBundledOutput(side, colorValue)
  76. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement