JerryWester

[ComputerCraft] Pipes Screensaver

Jun 25th, 2020
2,204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.58 KB | None | 0 0
  1. local pullEventBackup = os.pullEvent
  2. os.pullEvent = os.pullEventRaw
  3. term.clear()
  4.  
  5. -- this program accepts 1 optional argument, being intensity. Intensity determines the likelihood of the line to turn. Default is 0.05, max is 0.5 (mostly turning) and min is 0 (straight lines)
  6. local args = {...}
  7. local intensity = tonumber(args[1]) or 0.05
  8. if intensity > 0.5 then intensity = 0.5 end
  9. if intensity < 0 then intensity = 0 end
  10.  
  11. local linego = false
  12. local redraw = os.startTimer(0.05)
  13. local startfrom = {["x"] = 0, ["y"] = 0, ["dir"] = 0}
  14. local termx, termy = term.getSize()
  15. -- directions: 0 - down; 1 - left; 2 - up; 3 - right
  16.  
  17. while true do
  18.  
  19.     e, p1, p2, p3 = os.pullEvent()
  20.     if e == 'terminate' then
  21.         term.setBackgroundColor(2^15)
  22.         term.clear()
  23.         term.setCursorPos(1,1)
  24.         if term.isColor() then
  25.             term.setTextColor(2^14)
  26.         end
  27.         print('Terminated')
  28.         os.pullEvent = pullEventBackup
  29.         break
  30.     end
  31.  
  32.     if e == 'key' then
  33.         if p1 == keys.q then
  34.             term.setBackgroundColor(2^15)
  35.             term.clear()
  36.             term.setCursorPos(1,1)
  37.             sleep(0.05)
  38.             os.pullEvent = pullEventBackup
  39.             break
  40.         end
  41.     end
  42.  
  43.     if e == 'timer' then
  44.  
  45.         if startfrom.x > termx or startfrom.x < 1 or startfrom.y > termy or startfrom.y < 1 then
  46.             linego = false
  47.         end
  48.  
  49.         if not linego then
  50.             linego = true
  51.             math.randomseed(os.time()*1000)
  52.             term.setBackgroundColor(math.pow(2, ((math.log(term.getBackgroundColor())/math.log(2)) + (math.random(1,14)))%15))
  53.             local perimeter = (termx*2) + (termx*2)
  54.             local startnum = math.random(1,perimeter)
  55.             if startnum > (termx*2) + termy then
  56.                 -- left
  57.                 startfrom.x = 1
  58.                 startfrom.y = startnum - ((termx*2) + termy)
  59.                 startfrom.dir = 3
  60.             elseif startnum > termx + termy then
  61.                 -- bottom
  62.                 startfrom.y = termy
  63.                 startfrom.x = startnum - (termx + termy)
  64.                 startfrom.dir = 2
  65.             elseif startnum > termx then
  66.                 -- right
  67.                 startfrom.x = termx
  68.                 startfrom.y = startnum - termx
  69.                 startfrom.dir = 1
  70.             else
  71.                 -- top
  72.                 startfrom.y = 1
  73.                 startfrom.x = startnum
  74.                 startfrom.dir = 0
  75.             end
  76.         end
  77.  
  78.         term.setCursorPos(startfrom.x, startfrom.y)
  79.         write(' ')
  80.  
  81.         local rnd = math.random()
  82.  
  83.         if rnd < intensity then
  84.             startfrom.dir = (startfrom.dir - 1) % 4
  85.         elseif rnd > (1 - intensity) then
  86.             startfrom.dir = (startfrom.dir + 1) % 4
  87.         end
  88.  
  89.         if startfrom.dir == 0 then
  90.             startfrom.y = startfrom.y + 1
  91.         elseif startfrom.dir == 1 then
  92.             startfrom.x = startfrom.x - 1
  93.         elseif startfrom.dir == 2 then
  94.             startfrom.y = startfrom.y - 1
  95.         else
  96.             startfrom.x = startfrom.x + 1
  97.         end
  98.  
  99.         redraw = os.startTimer(0.05)
  100.     end
  101.  
  102. end
Advertisement
Add Comment
Please, Sign In to add comment