Advertisement
Stiepen

Intelligent rainbow light for computercraft + cclights

Dec 8th, 2012
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.78 KB | None | 0 0
  1. local sides = {"left", "right", "top", "bottom", "front", "back"}
  2. local colors = { "green", "brown", "black", "pink", "yellow", "orange", "magenta", "purple", "cyan", "red", "white", "lightBlue", "gray", "lime", "blue" }
  3.  
  4. local lights = {}
  5.  
  6. local function sin2(num)
  7.   return math.sin(math.rad(num))
  8. end
  9.  
  10. local function updateCable(side)
  11.   for k, v in ipairs(colors) do
  12.     if peripheral.getType(side..":"..v) == "Light" then
  13.       lights[side..":"..v] = true
  14.     else
  15.       lights[side..":"..v] = nil
  16.     end
  17.   end
  18. end
  19.  
  20. for k, v in ipairs(sides) do
  21.   local n = peripheral.getType(v)
  22.   if n == "Light" then
  23.     lights[v] = true
  24.   elseif n == "cable" then
  25.     lights[v] = nil
  26.     updateCable(v)
  27.   else
  28.     lights[v] = nil
  29.   end
  30. end
  31.  
  32. local function setColorRGB(r, g, b)
  33.   for k, v in pairs(lights) do
  34.     if v then
  35.       peripheral.call(k, "setColorRGB", r, g, b)
  36.     end
  37.   end
  38. end
  39.  
  40. while true do
  41.   for a = 1, 180 do
  42.     local r, g, b = sin2(a) * 255, sin2(a+120) * 255, sin2(a-120) * 255
  43.     r = math.sqrt(r * r)
  44.     g = math.sqrt(g * g)
  45.     b = math.sqrt(b * b)
  46.     --print("r:",r," g:", g, " b:", b)
  47.     pcall(
  48.       function() setColorRGB(r, g, b) end
  49.     )
  50.     local cont = os.startTimer(0.2)
  51.     while true do
  52.       e, p1, p2 = os.pullEvent()
  53.       if e == "timer" and p1 == cont then
  54.         break
  55.       elseif e == "peripheral_attach" then
  56.         local n = peripheral.getType()
  57.         if n == "Light" then
  58.           lights[p1] = true
  59.         elseif n == "cable" then
  60.           lights[p1] = nil
  61.           updateCable(p1)
  62.         else
  63.           lights[p1] = nil
  64.         end
  65.       elseif e == "peripheral_deattach" then
  66.         lights[p1] = nil
  67.         updateCable(p1)
  68.       end
  69.     end
  70.     --term.clear()
  71.     --term.setCursorPos(1, 1)
  72.   end
  73. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement