miniminater

rain_minimal

Dec 18th, 2021 (edited)
640
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.44 KB | None | 0 0
  1. --[[
  2.     A minimal version of rain.
  3.     For those who don't need all the fancy options.
  4. ]]
  5.  
  6. local component = require("component")
  7. local event     = require("event")
  8. local gpu = component.gpu
  9.  
  10. local wid, hig = gpu.getResolution()
  11.  
  12. local args = {...}
  13. local ii = 1
  14. for i=1,#args do
  15.     if args[ii] == "-v" or args[ii] == "--version" then
  16.         print("Rain (minimal) v1.0")
  17.         print("By minater247")
  18.         os.exit()
  19.     elseif args[ii] == "-h" or args[ii] == "--help" then
  20.         print("usage: rain_minimal [-v|--version] [-h|--help]")
  21.         os.exit()
  22.     else
  23.         print("arg_error: unknown argument " .. args[ii])
  24.         print("usage: rain_minimal [-v|--version] [-h|--help]")
  25.         os.exit()
  26.     end
  27.     ii = ii + 1
  28. end
  29.  
  30.  
  31.  
  32. local function genchars(len)
  33.     local str = ""
  34.     for i=1, len do
  35.         local s = string.char(math.random(32, 126))
  36.         if s == " " then
  37.             s = "&"
  38.         end
  39.         str = str .. s
  40.     end
  41.     return str
  42. end
  43. local charset = genchars(1000)
  44. local charsetlen = 1000
  45. local currchar = 1
  46.  
  47.  
  48. local Drops = {}
  49. local function createDrop()
  50.     local drop = {}
  51.     drop.x = math.random(1, wid)
  52.     if math.random() < 0.33 then
  53.         drop.y = math.random(1, math.floor(hig / 2))
  54.     else
  55.         drop.y = 1
  56.     end
  57.     drop.char = charset:sub(currchar, currchar)
  58.     currchar = currchar + 1
  59.     if currchar > charsetlen then
  60.         currchar = 1
  61.     end
  62.     table.insert(Drops, drop)
  63. end
  64.  
  65. local function moveDropsDown()
  66.     for i, drop in ipairs(Drops) do
  67.         drop.y = drop.y + 1
  68.         if drop.y > hig then
  69.             table.remove(Drops, i)
  70.         end
  71.     end
  72. end
  73.  
  74. local Screen = {}
  75. for y=1, hig do
  76.     Screen[y] = {}
  77.     for x=1, wid do
  78.         Screen[y][x] = 0
  79.     end
  80. end
  81.  
  82. local emptyScreen = Screen
  83. local drawableScreen = Screen
  84.  
  85. local function subtractOne()
  86.     for y=1, hig do
  87.         for x=1, wid do
  88.             if Screen[y][x] > 0 then
  89.                 Screen[y][x] = Screen[y][x] - 1
  90.             end
  91.         end
  92.     end
  93. end
  94.  
  95. local screencolors = {
  96.     0x000000,
  97.     0x002400,
  98.     0x004900,
  99.     0x006d00,
  100.     0x009200,
  101.     0x00b600,
  102.     0x00db00,
  103.     0x00ff00
  104. }
  105.  
  106. local function drawScreen()
  107.     gpu.setBackground(0x000000)
  108.     for y=1, hig do
  109.         for x=1, wid do
  110.             if Screen[y][x] > 0 then
  111.                 if Screen[y][x] > #screencolors then
  112.                     gpu.setForeground(screencolors[#screencolors])
  113.                 else
  114.                     gpu.setForeground(screencolors[Screen[y][x]])
  115.                 end
  116.                 gpu.set(x, y, charset:sub(currchar, currchar))
  117.                 currchar = currchar + 1
  118.             else
  119.                 gpu.set(x, y, " ")
  120.             end
  121.         end
  122.     end
  123. end
  124.  
  125.  
  126. local function addDropsToScreen()
  127.     for i, drop in ipairs(Drops) do
  128.         Screen[drop.y][drop.x] = math.random(4, math.floor(hig / 2))
  129.     end
  130. end
  131.  
  132. local function drawDrops()
  133.     gpu.setForeground(0xFFFFFF)
  134.     for i, drop in ipairs(Drops) do
  135.         gpu.set(drop.x, drop.y, drop.char)
  136.     end
  137. end
  138.  
  139. local iter = 0
  140. while true do
  141.     moveDropsDown()
  142.     subtractOne()
  143.     if #Drops < 50 then
  144.         createDrop()
  145.     end
  146.     addDropsToScreen()
  147.     drawScreen()
  148.     drawDrops()
  149.     local ev = event.pull(0.035, "key_down")
  150.     if ev then
  151.         gpu.fill(1, 1, wid, hig, " ")
  152.         io.write("\x1b[1;1H") --terminal escape to 1,1
  153.         break
  154.     end
  155.     iter = iter + 1
  156. end
Add Comment
Please, Sign In to add comment