1ng0

CC - Matrix Screensaver

Nov 26th, 2015
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.99 KB | None | 0 0
  1. --(c) 2013 Felix Maxwell
  2. --License: CC BY-SA 3.0
  3. local fps = 8 --Determines how long the system will wait between each update
  4. local maxLifetime = 40 --Max lifetime of each char
  5. local minLifetime = 8 --Min lifetime of each char
  6. local maxSourcesPerTick = 5 --Maximum number of sources created each tick
  7. local sourceWeight = 0 --Affects the chance that no sources will be generated
  8. local greenWeight = 8 --Threshhold out of 10 that determines when characters will switch from lime to green
  9. local grayWeight = 2 --Same as about, but from green to gray
  10. function getMonitors()
  11. local monitors = {}
  12. if checkMonitorSide( "top" ) then table.insert( monitors, "top" ) end
  13. if checkMonitorSide( "bottom" ) then table.insert( monitors, "bottom" ) end
  14. if checkMonitorSide( "left" ) then table.insert( monitors, "left" ) end
  15. if checkMonitorSide( "right" ) then table.insert( monitors, "right" ) end
  16. if checkMonitorSide( "front" ) then table.insert( monitors, "front" ) end
  17. if checkMonitorSide( "back" ) then table.insert( monitors, "back" ) end
  18. return monitors
  19. end
  20. function checkMonitorSide( side )
  21. if peripheral.isPresent( side ) then
  22.   if peripheral.getType(side) == "monitor" then
  23.    return true
  24.   end
  25. end
  26. return false
  27. end
  28. function printMonitorStats( side )
  29. local x, y = peripheral.call(side, "getSize")
  30. local color = "No"
  31. if peripheral.call(side, "isColor") then
  32.   color = "Yes"
  33. end
  34. print("Side:"..side.." Size:("..x..", "..y..") Color?"..color)
  35. end
  36. function askMonitor()
  37. local monitors = getMonitors()
  38. if #monitors == 0 then
  39.   print("No monitors found, add more!")
  40.   return nill
  41. elseif #monitors == 1 then
  42.   return monitors[1]
  43. else
  44.   while true do
  45.    print("Multiple monitors found, please pick one.")
  46.    for i,v in ipairs(monitors) do
  47.         write("["..(i).."] ")
  48.         printMonitorStats( v )
  49.    end
  50.    write("Selection: ")
  51.    local sel = tonumber(io.read())
  52.    if sel < 1 or sel > #monitors then
  53.         print("")
  54.         print("Invalid number.")
  55.    else
  56.         return monitors[sel]
  57.    end
  58.   end
  59. end
  60. end
  61. function printCharAt( monitor, x, y, char )
  62. monitor.setCursorPos( x, y )
  63. monitor.write( char )
  64. end
  65. function printGrid( monitor, grid, color )
  66. for i=1,#grid do
  67.   for o=1,#grid[i] do
  68.    if color then monitor.setTextColor( grid[i][o]["color"] ) end
  69.    printCharAt( monitor, i, o, grid[i][o]["char"] )
  70.   end
  71. end
  72. end
  73. function colorLifetime( life, originalLifetime )
  74. local lifetimePart = originalLifetime/10
  75. if life < grayWeight*lifetimePart then
  76.   return colors.gray
  77. elseif life < greenWeight*lifetimePart then
  78.   return colors.green
  79. else
  80.   return colors.lime
  81. end
  82. end
  83. function getRandomChar()
  84. local randTable = {"1","2","3","4","5","6","7","8","9","0","!","@","#","$","%","^","&","*","(",")","_","-","+","=","~","`",",","<",">",".","/","?",":","{","}","[","]","\\","\"","\'"}
  85. return randTable[math.random(1, #randTable)]
  86. end
  87. function tick( screen )
  88. --update lifetimes
  89. for x=1,#screen do
  90.   for y=1,#screen[x] do
  91.    screen[x][y]["curLife"] = screen[x][y]["curLife"] - 1
  92.   end
  93. end
  94. --make the sources 'fall' and delete timed out chars
  95. for x=1,#screen do
  96.   for y=1,#screen[x] do
  97.    if screen[x][y]["type"] == "source" and screen[x][y]["curLife"] == 0 then
  98.         screen[x][y]["type"] = "char"
  99.         screen[x][y]["lifetime"] = math.random(minLifetime, maxLifetime)
  100.         screen[x][y]["curLife"] = screen[x][y]["lifetime"]
  101.         screen[x][y]["color"] = colors.lime
  102.  
  103.         if y < #screen[x] then
  104.          screen[x][y+1]["char"] = getRandomChar()
  105.          screen[x][y+1]["lifetime"] = 1
  106.          screen[x][y+1]["curLife"] = 1
  107.          screen[x][y+1]["type"] = "source"
  108.          screen[x][y+1]["color"] = colors.white
  109.         end
  110.    elseif screen[x][y]["curLife"] < 0 then
  111.         screen[x][y]["char"] = " "
  112.         screen[x][y]["lifetime"] = 0
  113.         screen[x][y]["curLife"] = 0
  114.         screen[x][y]["type"] = "blank"
  115.         screen[x][y]["color"] = colors.black
  116.    elseif screen[x][y]["type"] == "char" then
  117.         screen[x][y]["color"] = colorLifetime( screen[x][y]["curLife"], screen[x][y]["lifetime"] )
  118.    end
  119.   end
  120. end
  121.  
  122. --create new character sources
  123. local newSources = math.random( 0-sourceWeight, maxSourcesPerTick )
  124. for i=1,newSources do
  125.   local col = math.random(1, #screen)
  126.   screen[col][1]["char"] = getRandomChar()
  127.   screen[col][1]["lifetime"] = 1
  128.   screen[col][1]["curLife"] = 1
  129.   screen[col][1]["type"] = "source"
  130.   screen[col][1]["color"] = colors.white
  131. end
  132.  
  133. return screen
  134. end
  135. function setup( w, h )
  136. local retTab = {}
  137. for x=1,w do
  138.   retTab[x] = {}
  139.   for y=1,h do
  140.    retTab[x][y] = {}
  141.    retTab[x][y]["char"] = " "
  142.    retTab[x][y]["lifetime"] = 0
  143.    retTab[x][y]["curLife"] = 0
  144.    retTab[x][y]["type"] = "blank"
  145.    retTab[x][y]["color"] = colors.black
  146.   end
  147. end
  148. return retTab
  149. end
  150. function run()
  151. local monitor = peripheral.wrap( askMonitor() )
  152. local color = monitor.isColor()
  153. local w, h = monitor.getSize()
  154. local screen = setup( w, h )
  155. while true do
  156.   screen = tick( screen )
  157.   printGrid( monitor, screen, color )
  158.   os.sleep(1/fps)
  159. end
  160. end
  161. run()
Add Comment
Please, Sign In to add comment