Advertisement
1amw31rd

ComputerCraft - Screensaver

Jun 10th, 2013
720
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.62 KB | None | 0 0
  1. -- Constants
  2. local colorList = {colors.blue, colors.green, colors.lime, colors.yellow, colors.orange, colors.red}
  3. local timerLength = 0.1
  4. local corneredCountPercent = 0.05
  5.  
  6. -- Variables
  7. local grid = {}
  8. local lastPixel = {}
  9. local currentColorCount = 1
  10. local timer = nil
  11. local restartTimer = nil
  12. local corneredCount = 1
  13. local width, height = 0
  14. local rangeBetweenColors = 50
  15.  
  16. --931
  17.  
  18. -- Load tools API
  19. if os.loadAPI("tools")==false then error("Failed to load Tools API!") end
  20.  
  21. -- Init monitor
  22. local monitor = peripheral.find("monitor")
  23. if monitor==nil then error("Monitor not found!") end
  24. term.redirect(monitor)
  25. width, height = term.getSize()
  26. corneredCount = width*height*corneredCountPercent
  27. rangeBetweenColors = (width*height)/(table.getn(colorList)*2)
  28.  
  29. -- Draw functions
  30. local function getCurrentColor()
  31.     local colorIndex = math.ceil(currentColorCount/rangeBetweenColors)
  32.     if colorIndex > table.getn(colorList) then
  33.         return colorList[table.getn(colorList)]
  34.     else
  35.         return colorList[colorIndex]
  36.     end
  37. end
  38.  
  39. -- Logic functions
  40. local function isPositionValid(x, y)
  41.     return grid[x]~=nil and grid[x][y]==false
  42. end
  43. local function checkForCorneredIsValidPosition(x, y, checkedPositions)
  44.     return isPositionValid(x, y) and checkedPositions[x]~=nil and checkedPositions[x][y]==false
  45. end
  46. local function recursiveCheckForCornered(x, y, checkedPositions, count)
  47.     if count[1]>=corneredCount then return false end
  48.     count[1]=count[1]+1
  49.     checkedPositions[x][y] = true
  50.     if checkForCorneredIsValidPosition(x+1, y, checkedPositions) and not recursiveCheckForCornered(x+1, y, checkedPositions, count) then return false end
  51.     if checkForCorneredIsValidPosition(x-1, y, checkedPositions) and not recursiveCheckForCornered(x-1, y, checkedPositions, count) then return false end
  52.     if checkForCorneredIsValidPosition(x, y+1, checkedPositions) and not recursiveCheckForCornered(x, y+1, checkedPositions, count) then return false end
  53.     if checkForCorneredIsValidPosition(x, y-1, checkedPositions) and not recursiveCheckForCornered(x, y-1, checkedPositions, count) then return false end
  54.     return true
  55. end
  56. local function isPositionCornered(x, y)
  57.     local checkedPositions = {}
  58.     for i = 1, width do
  59.         checkedPositions[i] = {}
  60.         for j = 1, height do
  61.             checkedPositions[i][j] = false
  62.         end
  63.     end
  64.  
  65.     local count = {0}
  66.     local isCornered = recursiveCheckForCornered(x, y, checkedPositions, count)
  67.     return isCornered, count[1]
  68.        
  69.    
  70.     --if isPositionValid(x+1, y) then return false end
  71.     --if isPositionValid(x-1, y) then return false end
  72.     --if isPositionValid(x, y+1) then return false end
  73.     --if isPositionValid(x, y-1) then return false end
  74.     --return true
  75. end
  76. local function update()
  77.     currentColorCount = currentColorCount + 1
  78.     local directions = {1,2,3,4}
  79.     local corneredPosition = nil
  80.     local corneredCount = 0
  81.    
  82.     while table.getn(directions) > 0 do
  83.         local direction = table.remove(directions, math.random(1,table.getn(directions)))
  84.        
  85.         local x = lastPixel[1]
  86.         local y = lastPixel[2]
  87.        
  88.         if direction==1 then x=x+1
  89.         elseif direction==2 then x=x-1
  90.         elseif direction==3 then y=y+1
  91.         else y=y-1 end
  92.        
  93.         if isPositionValid(x, y) then
  94.             local isCornered, cCorneredCount = isPositionCornered(x, y)
  95.             if isCornered then
  96.                 if cCorneredCount >= corneredCount then
  97.                     corneredPosition = {x, y}
  98.                     corneredCount = cCorneredCount
  99.                 end
  100.             else
  101.                 lastPixel = {x, y}
  102.                 paintutils.drawPixel(x, y, getCurrentColor())
  103.                 grid[x][y] = true
  104.                 return true
  105.             end
  106.         end
  107.     end
  108.    
  109.     if corneredPosition~=nil then
  110.         lastPixel = {corneredPosition[1], corneredPosition[2]}
  111.         paintutils.drawPixel(corneredPosition[1], corneredPosition[2], getCurrentColor())
  112.         grid[corneredPosition[1]][corneredPosition[2]] = true
  113.         return true
  114.     end
  115.    
  116.     restartTimer = os.startTimer(3)
  117.     return false
  118. end
  119.  
  120. -- Loop
  121. os.queueEvent("restart")
  122. while true do
  123.     local event, a, b, c = os.pullEvent()
  124.    
  125.     if event=="restart" then
  126.         tools.gui.reset()
  127.         grid = {}
  128.         for i = 1, width do
  129.             grid[i] = {}
  130.             for j = 1, height do
  131.                 grid[i][j] = false
  132.             end
  133.         end
  134.         lastPixel = {math.random(1, width), math.random(1, height)}
  135.         currentColorCount = 1
  136.         paintutils.drawPixel(lastPixel[1], lastPixel[2], getCurrentColor())
  137.         grid[lastPixel[1]][lastPixel[2]] = true
  138.         timer = os.startTimer(timerLength)
  139.     elseif event=="timer" then
  140.         if a==timer then
  141.             if update() then
  142.                 timer = os.startTimer(timerLength)
  143.             end
  144.         elseif a==restartTimer then
  145.             os.queueEvent("restart")
  146.         end
  147.     elseif event=="char" then
  148.         os.queueEvent(event, a)
  149.         tools.gui.reset()
  150.         break
  151.     elseif event=="monitor_touch" or event=="key" then
  152.         tools.gui.reset()
  153.         break
  154.     end
  155. end
  156.  
  157. term.restore()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement