Advertisement
fingercomp

[OC] [CX] lamp.lua

Nov 12th, 2015
500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Lamp-o-mat. Controls CX lamps using OC computer. Minecraft. --
  2. global_lamps = {"cf0", "95c", "ad9", "a1a", "d97", "afb", "cc5", "335", "a75"}
  3. global_exit = false
  4. local com = require("component")
  5. local event = require("event")
  6.  
  7. base = {__index = base}
  8. function base.__call(self, count)
  9.   if not global_exit then
  10.     count = count or math.huge
  11.     self.init()
  12.     if not self.customSet then
  13.       self.getLamps(self)
  14.       local lamps = self.lamps
  15.       local r = self.r
  16.       local g = self.g
  17.       local b = self.b
  18.       local formula = self.formula
  19.       local noExit = true
  20.       local cycle = 1
  21.       while noExit and cycle <= count and not global_exit do
  22.         for num, i in ipairs(lamps) do
  23.           pcall(com.invoke, i, "setLampColor", formula(r(), g(), b()))
  24.           if event.pull(0.6, "key_down") then
  25.             noExit = false
  26.             global_exit = true
  27.           end
  28.         end
  29.         cycle = cycle + 1
  30.       end
  31.     else
  32.       self.customSet(self, count)
  33.     end
  34.     for i in com.list("colorful_lamp") do
  35.       com.invoke(i, "setLampColor", 0)
  36.     end
  37.     self.destruct()
  38.   end
  39. end
  40.  
  41. --- WAVE ---
  42.  
  43. wave = setmetatable({}, base)
  44. function wave:getLamps()
  45.   local lampOrder = global_lamps
  46.   local lamps = {}
  47.   for i = 1, #lampOrder, 1 do
  48.     if com.get(lampOrder[i]) then
  49.       table.insert(lamps, com.get(lampOrder[i]))
  50.     end
  51.   end
  52.   wave.lamps = lamps
  53. end
  54. wave.r = function() return math.random(0, 16) end
  55. wave.g = function() return math.random(16, 31) end
  56. wave.b = function() return math.random(0, 16) end
  57. wave.formula = function(r, g, b)
  58.   return r * 32 * 32 + g * 32 + b
  59. end
  60. function wave.added(_, addr, name)
  61.   if name == "colorful_lamp" then
  62.     table.insert(wave.lamps, addr)
  63.   end
  64. end
  65. function wave.removed(_, addr, name)
  66.   if name == "colorful_lamp" then
  67.     table.remove(wave.lamps, addr)
  68.   end
  69. end
  70. function wave.init()
  71.   event.listen("component_added", wave.added)
  72.   event.listen("component_removed", wave.removed)
  73. end
  74. function wave.destruct()
  75.   event.ignore("component_added", wave.added)
  76.   event.ignore("component_removed", wave.removed)
  77. end
  78.  
  79. --- SMOOTH WAVE ---
  80.  
  81. smwv = setmetatable({}, base)
  82. smwv.r = function() return math.random(16, 31) end
  83. smwv.g = function() return math.random(16, 31) end
  84. smwv.b = function() return math.random(16, 31) end
  85. function smwv.onKeyDown()
  86.   smwv.noExit = false
  87.   global_exit = true
  88.   print("Please wait until cycle ends...")
  89. end
  90. function smwv.added(_, addr, name)
  91.   if name == "colorful_lamp" then
  92.     table.insert(smwv.lamps, {addr, 0})
  93.     pcall(com.invoke, addr, "setLampColor", 0)
  94.     print("New lamp available lamp, adding: " .. addr)
  95.   end
  96. end
  97. function smwv.removed(_, addr, name)
  98.   if name == "colorful_lamp" then
  99.     for i = 1, #smwv.lamps, 1 do
  100.       if addr == smwv.lamps[i][1] then
  101.         table.remove(smwv.lamps, i)
  102.         print("Lamp is unavailable, removing: " .. addr)
  103.       end
  104.     end
  105.   end
  106. end
  107. function smwv.init()
  108.   smwv.noExit = true
  109.   event.listen("key_down", smwv.onKeyDown)
  110.   event.listen("component_added", smwv.added)
  111.   event.listen("component_removed", smwv.removed)
  112. end
  113. function smwv.destruct()
  114.   event.ignore("key_down", smwv.onKeyDown)
  115.   event.ignore("component_added", smwv.added)
  116.   event.ignore("component_removed", smwv.removed)
  117. end
  118. function smwv:set()
  119.   for i = 1, #self.lamps, 1 do
  120.     pcall(com.invoke, self.lamps[i][1], "setLampColor", self.lamps[i][2])
  121.   end
  122. end
  123. function smwv:customSet(count)
  124.   local lamps = global_lamps
  125.   self.lamps = {}
  126.   for _, i in ipairs(lamps) do
  127.     if ({pcall(com.get, i)})[1] then
  128.       table.insert(self.lamps, {com.get(i), 0})
  129.     end
  130.   end
  131.   local cycle = 1
  132.   while smwv.noExit and cycle <= count do
  133.     local color = self.r() * 32 * 32 + self.g() * 32 + self.b()
  134.     for i = 1, #self.lamps, 1 do
  135.       if self.lamps[i] then
  136.         self.lamps[i][2] = color
  137.         if i > 1 then
  138.           for j = 1, #self.lamps, 1 do
  139.             if self.lamps[j] and j ~= i then
  140.               local prevcolor = self.lamps[j][2] or 0
  141.               local pr = math.floor(prevcolor / 32 / 32)
  142.               local pg = math.floor((prevcolor - pr * 32 * 32) / 32)
  143.               local pb = prevcolor - pr * 32 * 32 - pg * 32
  144.               local mlpr = ((#self.lamps - 1) / (#self.lamps))
  145.               local newcolor = math.floor(pr * mlpr) * 32 * 32 + math.floor(pg * mlpr) * 32 + math.floor(pb * mlpr)
  146.               self.lamps[j][2] = self.lamps[j] and math.floor(newcolor) or nil
  147.             end
  148.           end
  149.         end
  150.         self:set()
  151.         os.sleep(0.1)
  152.       end
  153.     end
  154.     cycle = cycle + 1
  155.   end
  156. end
  157.  
  158. --- GROUP BLINK ---
  159.  
  160. blink = setmetatable({}, base)
  161. blink.r = function() return math.random(0, 31) end
  162. blink.g = function() return math.random(0, 31) end
  163. blink.b = function() return math.random(0, 31) end
  164. blink.r1 = function() return math.random(0, 31) end
  165. blink.g1 = function() return math.random(0, 31) end
  166. blink.b1 = function() return math.random(0, 31) end
  167. function blink.added(_, addr, name)
  168.   if name == "colorful_lamp" then
  169.     if blink.next then
  170.       table.insert(blink.even, addr)
  171.     else
  172.       table.insert(blink.odd, addr)
  173.     end
  174.     blink.lamps[addr] = blink.next
  175.     print("New lamp available: " .. addr .. ", " .. (blink.next and "even" or "odd"))
  176.     blink.next = not blink.next
  177.   end
  178. end
  179. function blink.removed(_, addr, name)
  180.   if name == "colorful_lamp" then
  181.     if blink.lamps[addr] == true then
  182.       table.remove(blink.even, addr)
  183.     else
  184.       table.remove(blink.odd, addr)
  185.     end
  186.     blink.lamps[addr] = nil
  187.   end
  188. end
  189. function blink.onKeyDown()
  190.   blink.noExit = false
  191.   global_exit = true
  192.   print("Please wait until cycle ends...")
  193. end
  194. function blink.init()
  195.   event.listen("component_added", blink.added)
  196.   event.listen("component_removed", blink.removed)
  197.   event.listen("key_down", blink.onKeyDown)
  198. end
  199. function blink.destruct()
  200.   event.ignore("component_added", blink.added)
  201.   event.ignore("component_removed", blink.removed)
  202.   event.ignore("key_down", blink.onKeyDown)
  203. end
  204. function blink:set(cur, color)
  205.   local lamps = {}
  206.   if cur then
  207.     lamps = self.even
  208.   else
  209.     lamps = self.odd
  210.   end
  211.   for _, i in ipairs(lamps) do
  212.     pcall(com.invoke, i, "setLampColor", color)
  213.   end
  214. end
  215. function blink.clear()
  216.   for i in com.list("colorful_lamp") do
  217.     pcall(com.invoke, i, "setLampColor", 0)
  218.   end
  219. end
  220. function blink:customSet(count)
  221.   local cycle = 1
  222.   self.noExit = true
  223.   local lamps = global_lamps
  224.   self.odd = {}
  225.   self.even = {}
  226.   self.lamps = {}
  227.   self.next = false
  228.   for _, i in ipairs(lamps) do
  229.     if com.get(i) then
  230.       if self.next then
  231.         table.insert(self.even, com.get(i))
  232.       else
  233.         table.insert(self.odd, com.get(i))
  234.       end
  235.       self.lamps[com.get(i)] = self.next
  236.       self.next = not self.next
  237.     end
  238.   end
  239.   while self.noExit and cycle <= count do
  240.     self.color1 = self.r() * 32 * 32 + self.g() * 32 + self.b()
  241.     self.color2 = self.r1() * 32 * 32 + self.g1() * 32 + self.b1()
  242.     self:set(false, self.color1)
  243.     self:set(true, self.color2)
  244.     os.sleep(0.4)
  245.     self.clear()
  246.     os.sleep(0.4)
  247.     cycle = cycle + 1
  248.   end
  249. end
  250.  
  251. --- RANDOM ---
  252.  
  253. rand = setmetatable({}, base)
  254. function rand:getLamps()
  255.   self.lamps = {}
  256.   for i in com.list("colorful_lamp") do
  257.     table.insert(self.lamps, i)
  258.   end
  259. end
  260. rand.r = function() return math.random(0, 31) end
  261. rand.g = function() return math.random(0, 31) end
  262. rand.b = function() return math.random(0, 31) end
  263. rand.formula = function(r, g, b) return r * 32 * 32 + g * 32 + b end
  264. function rand.init() end
  265. function rand.destruct() end
  266.  
  267. --- BIGLAMP ---
  268.  
  269. big = setmetatable({}, base)
  270. big.r = function() return math.random(0, 31) end
  271. big.g = function() return math.random(0, 31) end
  272. big.b = function() return math.random(0, 31) end
  273. function big.onKeyDown()
  274.   big.noExit = false
  275.   global_exit = true
  276.   print("Please wait until cycle ends...")
  277. end
  278. function big.init()
  279.   event.listen("key_down", big.onKeyDown)
  280. end
  281. function big.destruct()
  282.   event.ignore("key_down", big.onKeyDown)
  283. end
  284. function big:customSet(count)
  285.   self.noExit = true
  286.   local cycle = 0
  287.   local color = {0, 0, 0}
  288.   local mlpr = 8/9
  289.   while self.noExit and cycle <= count do
  290.     color = {math.floor(color[1] * mlpr), math.floor(color[2] * mlpr), math.floor(color[3] * mlpr)}
  291.     if color[1] < 1 and color[2] < 1 and color[3] < 1 then
  292.       color = {self.r(), self.g(), self.b()}
  293.       cycle = cycle + 1
  294.       os.sleep(0.8)
  295.       if cycle > count then break end
  296.     end
  297.     for i in com.list("colorful_lamp") do
  298.       pcall(com.invoke, i, "setLampColor", color[1] * 32 * 32 + color[2] * 32 + color[3])
  299.     end
  300.     os.sleep(0.3)
  301.   end
  302. end
  303.  
  304.  
  305. --- MAIN ---
  306.  
  307. while not global_exit do
  308.   big(5)
  309.   rand(2)
  310.   wave(5)
  311.   smwv(20)
  312.   blink(5)
  313. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement