ItsNoah

Matrix CC

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