Guest User

ComputerCraft ScreenSaver

a guest
Apr 24th, 2015
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.12 KB | None | 0 0
  1. --[[
  2.   File:      wubwub
  3.   Purpose:   A screensaver that emulates audio meters
  4.   Author:    da404lewzer
  5.   Project:   http://turtlescripts.com/project/gjdh0b
  6.   License:   Creative Commons Attribution-ShareAlike 3.0 Unported License.
  7.              http://creativecommons.org/licenses/by-sa/3.0/
  8. ]]
  9.  
  10. local largeFont = 0.5
  11. local smallFont = 0.5
  12. local tArgs = { ... }
  13. local mon
  14. local w, h = 1,1
  15. local ss_running = true
  16. local mons = {}
  17. local curMsg = {}
  18. local curColor = {}
  19.  
  20.  
  21. --colors
  22. local blues = {colors.blue, colors.cyan, colors.white}
  23. local greens = {colors.green, colors.lime, colors.white}
  24. local reds = {colors.red, colors.pink, colors.white}
  25. local grays = {colors.white, colors.lightGray, colors.gray, colors.black}
  26. local fire = {colors.white, colors.yellow, colors.orange, colors.red}
  27. local RedilRed = {colors.cyan, colors.yellow, colors.orange, colors.red}
  28. local hollow = {colors.black, colors.white}
  29. local merica = {colors.blue, colors.white, colors.red}
  30. local wtf = {
  31.     colors.white,
  32.     colors.orange,
  33.     colors.magenta,
  34.     colors.lightBlue,
  35.     colors.yellow,
  36.     colors.lime,
  37.     colors.pink,
  38.     colors.gray,
  39.     colors.lightGray,
  40.     colors.cyan,
  41.     colors.purple,
  42.     colors.blue,
  43.     colors.brown,
  44.     colors.green,
  45.     colors.red
  46. }
  47.  
  48. --basic settings
  49. local randomize = true
  50.  
  51. --these arrays allow you to define all the possible pallets
  52. local peakChoices =     {blues, grays, greens, reds, wtf}
  53. local barColorChoices = {fire, RedilRed, hollow, merica, greens, blues, reds, grays, wtf}
  54.  
  55. --these arrays allow you to specify per-monitor settings
  56. local positions =   {"term",    "top",      "bottom",   "left",     "right",    "front",    "back"}     --position/order of the monitor
  57. local reverse =     {false,     false,      true,       false,      false,      false,      false}      --this means "flip" the axis
  58. local horizontal =  {true,      true,       true,       false,      true,       true,       true}       --true if horizontal, false if vertical
  59. local peaks =       {blues,     blues,      blues,      blues,      blues,      blues,      blues}      --which peak colors should we show?
  60. local barColors =   {fire,      fire,       fire,       fire,       fire,       fire,       fire}       --which bar colors should we show?
  61. local columns =     {4,         2,          5,          3,          6,          4,          4}          --number of columns to render
  62. local modes =       {"stack3",  "stack3",   "stack3",   "stack3",   "stack3",   "stack3",   "stack3"}   --how do we paint the bars? "split3", "solid" OR "stack3"
  63.  
  64. --do we split the meter down the middle?
  65. local centered = {true, false, false, false, true, false, false}
  66.  
  67. --not settings
  68. local nextChange, lastClock = {},{}
  69. local skipMove = 1
  70.  
  71. function randomizeMonitor(monitor, monID)
  72.   peaks[monID] = peakChoices[math.random(1,#peakChoices)]
  73.   barColors[monID] = barColorChoices[math.random(1,#barColorChoices)]
  74.   reverse[monID] = math.random(0,1)==1
  75.   horizontal[monID] = math.random(0,1)==1
  76.   centered[monID] = math.random(0,1)==1
  77.   nextChange[monID] = math.random(3,6)
  78.   lastClock[monID] = os.clock()
  79. end
  80.  
  81. function getColorMode(monitor)
  82.     if monitor.isColor() then
  83.         return 2
  84.     else
  85.         return 1
  86.     end
  87. end
  88. local chars = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z","0","1","2","3","4","5","6","7","8","9", "@", "#", "$", "%", "&", "?", "!", "*"}
  89.  
  90. function regen(monID, c, s)
  91.     len = math.random(0,math.random(1,s))
  92.     local lastHeight=0
  93.     local lastTime = os.clock()
  94.     if mons[monID].cols[c] then
  95.       lastHeight = mons[monID].cols[c].lastHeight
  96.       lastTime = mons[monID].cols[c].lastTime
  97.       if len > lastHeight then
  98.         lastHeight = len
  99.         lastTime = os.clock()
  100.       end
  101.       if lastHeight > s-1 then
  102.         lastHeight = s-1
  103.       end
  104.       if os.clock() - lastTime > 1 then
  105.         lastHeight=lastHeight-1
  106.         if lastHeight < 0 then
  107.           lastHeight = 0
  108.         end
  109.       end
  110.     end
  111.     mons[monID].cols[c] = {length=len, x=c, y=s, speed=math.random(1,3), lastHeight=lastHeight, lastTime=lastTime}
  112. end
  113. function update(monitor, monID)
  114.     mon = monitor
  115.     w, h = mon.getSize()
  116.     local colorMode = getColorMode(mon)
  117.     if colorMode == 1 then
  118.       mon.setBackgroundColor(colors.black)
  119.     else
  120.       mon.setBackgroundColor(colors.black)
  121.     end
  122.     mon.clear()
  123.     if horizontal[monID] then
  124.      
  125.       local hh = h
  126.       if centered[monID] then hh = h/2 end
  127.      
  128.       local cols = w / columns[monID]
  129.       local c, lastUpdate = -1
  130.       for cd=0, w-1 do
  131.           c = math.floor(cd/cols)+1
  132.           if lastUpdate ~= c then
  133.             lastUpdate = c
  134.             regen(monID, c, hh)
  135.           end
  136.           if mons[monID] then
  137.               col = mons[monID].cols[c]
  138.               if col then
  139.                   local x, y, i
  140.                   for ii=0, col.length-1, 1 do
  141.                       local y = hh-ii
  142.                       if colorMode == 1 then
  143.                           mon.setBackgroundColor(colors.white)
  144.                       else
  145.                           if modes[monID] == "stack3" then
  146.                               local tmpPercent = col.length / #barColors[monID]
  147.                               for cc=1, #barColors[monID] do
  148.                                 local curSpot = tmpPercent*(#barColors[monID] - (cc-1))
  149.                                 if ii < curSpot then
  150.                                   mon.setBackgroundColor(barColors[monID][#barColors[monID] - (cc-1)])
  151.                                 end
  152.                               end
  153.                           elseif modes[monID] == "solid" then
  154.                               local tmpPercent = hh / #barColors[monID]
  155.                               for cc=1, #barColors[monID] do
  156.                                 local curSpot = tmpPercent*(#barColors[monID] - (cc-1))
  157.                                 if col.length < curSpot then
  158.                                   mon.setBackgroundColor(barColors[monID][#barColors[monID] - (cc-1)])
  159.                                 end
  160.                               end
  161.                           elseif modes[monID] == "split3" then
  162.                               local tmpPercent = hh / #barColors[monID]
  163.                               for cc=1, #barColors[monID] do
  164.                                 local curSpot = tmpPercent*(#barColors[monID] - (cc-1))
  165.                                 if ii < curSpot then
  166.                                   mon.setBackgroundColor(barColors[monID][#barColors[monID] - (cc-1)])
  167.                                 end
  168.                               end
  169.                           end
  170.                       end
  171.                       if reverse[monID] then
  172.                           mon.setCursorPos(cd+1, hh - y)
  173.                           mon.write(" ")
  174.                           if centered[monID] then
  175.                               mon.setCursorPos(cd+1, hh + y)
  176.                               mon.write(" ")
  177.                           end
  178.                       else
  179.                           mon.setCursorPos(cd+1, y)
  180.                           mon.write(" ")
  181.                           if centered[monID] then
  182.                               mon.setCursorPos(cd+1, hh + (hh-y)+1)
  183.                               mon.write(" ")
  184.                           end
  185.                       end
  186.                   end
  187.                  
  188.                   local peakY = hh-col.lastHeight
  189.                   if colorMode == 1 then
  190.                     mon.setBackgroundColor(colors.white)
  191.                   else
  192.                     local tmpPercent = hh / #peaks[monID]
  193.                     for cc=1, #peaks[monID] do
  194.                       local curSpot = tmpPercent*(#peaks[monID] - (cc-1))
  195.                       if peakY < curSpot then
  196.                         mon.setBackgroundColor(peaks[monID][#peaks[monID] - (cc-1)])
  197.                       end
  198.                     end
  199.                   end
  200.                   if reverse[monID] then
  201.                       mon.setCursorPos(cd+1, hh - peakY)
  202.                       mon.write(" ")
  203.                       if centered[monID] then
  204.                           mon.setCursorPos(cd+1, hh + peakY)
  205.                           mon.write(" ")
  206.                       end
  207.                   else
  208.                       mon.setCursorPos(cd+1, peakY)
  209.                       mon.write(" ")
  210.                       if centered[monID] then
  211.                           mon.setCursorPos(cd+1, hh + (hh-peakY)+1)
  212.                           mon.write(" ")
  213.                       end
  214.                   end
  215.               else
  216.                   regen(monID, c, hh)
  217.               end
  218.           else
  219.               regen(monID, c, hh)
  220.           end
  221.       end
  222.     else
  223.    
  224.       local ww = w
  225.       if centered[monID] then ww = w/2 end
  226.      
  227.       local cols = h / columns[monID]
  228.       local c, lastUpdate = -1
  229.       for rd=0, h-1 do
  230.           c = math.floor(rd/cols)+1
  231.           if lastUpdate ~= c then
  232.             lastUpdate = c
  233.             regen(monID, c, ww)
  234.           end
  235.           if mons[monID] then
  236.               col = mons[monID].cols[c]
  237.               if col then
  238.                   local x, y, i
  239.                   for ii=0, col.length-1, 1 do
  240.                       local y = ww-ii
  241.                       if colorMode == 1 then
  242.                           mon.setBackgroundColor(colors.white)
  243.                       else
  244.                           if modes[monID] == "stack3" then
  245.                               local tmpPercent = col.length / #barColors[monID]
  246.                               for cc=1, #barColors[monID] do
  247.                                 local curSpot = tmpPercent*(#barColors[monID] - (cc-1))
  248.                                 if ii < curSpot then
  249.                                   mon.setBackgroundColor(barColors[monID][#barColors[monID] - (cc-1)])
  250.                                 end
  251.                               end
  252.                           elseif modes[monID] == "solid" then
  253.                               local tmpPercent = ww / #barColors[monID]
  254.                               for cc=1, #barColors[monID] do
  255.                                 local curSpot = tmpPercent*(#barColors[monID] - (cc-1))
  256.                                 if col.length < curSpot then
  257.                                   mon.setBackgroundColor(barColors[monID][#barColors[monID] - (cc-1)])
  258.                                 end
  259.                               end
  260.                           elseif modes[monID] == "split3" then
  261.                               local tmpPercent = ww / #barColors[monID]
  262.                               for cc=1, #barColors[monID] do
  263.                                 local curSpot = tmpPercent*(#barColors[monID] - (cc-1))
  264.                                 if ii < curSpot then
  265.                                   mon.setBackgroundColor(barColors[monID][#barColors[monID] - (cc-1)])
  266.                                 end
  267.                               end
  268.                           end
  269.                       end
  270.                       if reverse[monID] then
  271.                           mon.setCursorPos(ww - y, rd+1)
  272.                           mon.write(" ")
  273.                           if centered[monID] then
  274.                               mon.setCursorPos(ww + y, rd+1)
  275.                               mon.write(" ")
  276.                           end
  277.                       else
  278.                           mon.setCursorPos(y, rd+1)
  279.                           mon.write(" ")
  280.                           if centered[monID] then
  281.                               mon.setCursorPos(ww + (ww-y), (rd)+1)
  282.                               mon.write(" ")
  283.                           end
  284.                       end
  285.                   end
  286.                  
  287.                   local peakY = ww-col.lastHeight
  288.                   if colorMode == 1 then
  289.                     mon.setBackgroundColor(colors.white)
  290.                   else
  291.                     local tmpPercent = w / #peaks[monID]
  292.                     for cc=1, #peaks[monID] do
  293.                       local curSpot = tmpPercent*(#peaks[monID] - (cc-1))
  294.                       if peakY < curSpot then
  295.                         mon.setBackgroundColor(peaks[monID][#peaks[monID] - (cc-1)])
  296.                       end
  297.                     end
  298.                   end
  299.                   if reverse[monID] then
  300.                       mon.setCursorPos(ww - peakY, rd+1)
  301.                       mon.write(" ")
  302.                       if centered[monID] then
  303.                           mon.setCursorPos(ww + peakY, rd+1)
  304.                           mon.write(" ")
  305.                       end
  306.                   else
  307.                       mon.setCursorPos(peakY, rd+1)
  308.                       mon.write(" ")
  309.                       if centered[monID] then
  310.                           mon.setCursorPos(ww + (ww-peakY), (rd)+1)
  311.                           mon.write(" ")
  312.                       end
  313.                   end
  314.               else
  315.                   regen(monID, c, w)
  316.               end
  317.           else
  318.               regen(monID, c, w)
  319.           end
  320.       end
  321.     end
  322. end
  323. function updateTemplate(monitor, monID)
  324.     if monitor then
  325.         w,h = monitor.getSize()
  326.         if w and h then
  327.             if not mons[monID] then
  328.                 if monitor ~= term then
  329.                     if w > 7 and h > 5 then
  330.                         monitor.setTextScale(largeFont)
  331.                     else
  332.                         monitor.setTextScale(smallFont)
  333.                     end
  334.                     w,h = monitor.getSize()
  335.                 end
  336.                 mons[monID] = {cols={}}
  337.                 if randomize then
  338.                   randomizeMonitor(monitor, monID)
  339.                 end
  340.             end
  341.             if randomize then
  342.               if os.clock() - lastClock[monID] > nextChange[monID] then
  343.                 randomizeMonitor(monitor, monID)
  344.               end
  345.             end
  346.             update(monitor, monID)
  347.         end
  348.     end
  349. end
  350. function updateAll()
  351.     for i=1, #positions do
  352.         if positions[i] == "term" then
  353.             updateTemplate(term, i)
  354.         elseif peripheral.getType(positions[i]) == "monitor" then
  355.             updateTemplate(peripheral.wrap(positions[i]), i)
  356.         end
  357.     end
  358. end
  359. function cleanUp()
  360.     for i=1, #positions do
  361.         if positions[i] == "term" then
  362.             term.setBackgroundColor(colors.black)
  363.             term.setTextColor(colors.white)
  364.             term.clear()
  365.             term.setCursorPos(1,1)
  366.         elseif peripheral.getType(positions[i]) == "monitor" then
  367.             local mon = peripheral.wrap(positions[i])
  368.             if mon then
  369.                 mon.setBackgroundColor(colors.black)
  370.                 mon.setTextColor(colors.white)
  371.                 mon.setTextScale(1)
  372.                 mon.clear()
  373.                 mon.setCursorPos(1,1)
  374.             end
  375.         end
  376.     end
  377. end
  378. local ss_timer = os.startTimer(0)
  379. while ss_running do
  380.     local event, p1, p2 = os.pullEvent()
  381.     if event == "key" then
  382.         ss_running = false
  383.     else
  384.         if event == "timer" and p1 == ss_timer then
  385.             ss_timer = os.startTimer(0.05)
  386.             updateAll()
  387.         end
  388.     end
  389. end
  390.  
  391. cleanUp()
Add Comment
Please, Sign In to add comment