Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Constants
- local colorList = {colors.blue, colors.green, colors.lime, colors.yellow, colors.orange, colors.red}
- local timerLength = 0.1
- local corneredCountPercent = 0.05
- -- Variables
- local grid = {}
- local lastPixel = {}
- local currentColorCount = 1
- local timer = nil
- local restartTimer = nil
- local corneredCount = 1
- local width, height = 0
- local rangeBetweenColors = 50
- --931
- -- Load tools API
- if os.loadAPI("tools")==false then error("Failed to load Tools API!") end
- -- Init monitor
- local monitor = peripheral.find("monitor")
- if monitor==nil then error("Monitor not found!") end
- term.redirect(monitor)
- width, height = term.getSize()
- corneredCount = width*height*corneredCountPercent
- rangeBetweenColors = (width*height)/(table.getn(colorList)*2)
- -- Draw functions
- local function getCurrentColor()
- local colorIndex = math.ceil(currentColorCount/rangeBetweenColors)
- if colorIndex > table.getn(colorList) then
- return colorList[table.getn(colorList)]
- else
- return colorList[colorIndex]
- end
- end
- -- Logic functions
- local function isPositionValid(x, y)
- return grid[x]~=nil and grid[x][y]==false
- end
- local function checkForCorneredIsValidPosition(x, y, checkedPositions)
- return isPositionValid(x, y) and checkedPositions[x]~=nil and checkedPositions[x][y]==false
- end
- local function recursiveCheckForCornered(x, y, checkedPositions, count)
- if count[1]>=corneredCount then return false end
- count[1]=count[1]+1
- checkedPositions[x][y] = true
- if checkForCorneredIsValidPosition(x+1, y, checkedPositions) and not recursiveCheckForCornered(x+1, y, checkedPositions, count) then return false end
- if checkForCorneredIsValidPosition(x-1, y, checkedPositions) and not recursiveCheckForCornered(x-1, y, checkedPositions, count) then return false end
- if checkForCorneredIsValidPosition(x, y+1, checkedPositions) and not recursiveCheckForCornered(x, y+1, checkedPositions, count) then return false end
- if checkForCorneredIsValidPosition(x, y-1, checkedPositions) and not recursiveCheckForCornered(x, y-1, checkedPositions, count) then return false end
- return true
- end
- local function isPositionCornered(x, y)
- local checkedPositions = {}
- for i = 1, width do
- checkedPositions[i] = {}
- for j = 1, height do
- checkedPositions[i][j] = false
- end
- end
- local count = {0}
- local isCornered = recursiveCheckForCornered(x, y, checkedPositions, count)
- return isCornered, count[1]
- --if isPositionValid(x+1, y) then return false end
- --if isPositionValid(x-1, y) then return false end
- --if isPositionValid(x, y+1) then return false end
- --if isPositionValid(x, y-1) then return false end
- --return true
- end
- local function update()
- currentColorCount = currentColorCount + 1
- local directions = {1,2,3,4}
- local corneredPosition = nil
- local corneredCount = 0
- while table.getn(directions) > 0 do
- local direction = table.remove(directions, math.random(1,table.getn(directions)))
- local x = lastPixel[1]
- local y = lastPixel[2]
- if direction==1 then x=x+1
- elseif direction==2 then x=x-1
- elseif direction==3 then y=y+1
- else y=y-1 end
- if isPositionValid(x, y) then
- local isCornered, cCorneredCount = isPositionCornered(x, y)
- if isCornered then
- if cCorneredCount >= corneredCount then
- corneredPosition = {x, y}
- corneredCount = cCorneredCount
- end
- else
- lastPixel = {x, y}
- paintutils.drawPixel(x, y, getCurrentColor())
- grid[x][y] = true
- return true
- end
- end
- end
- if corneredPosition~=nil then
- lastPixel = {corneredPosition[1], corneredPosition[2]}
- paintutils.drawPixel(corneredPosition[1], corneredPosition[2], getCurrentColor())
- grid[corneredPosition[1]][corneredPosition[2]] = true
- return true
- end
- restartTimer = os.startTimer(3)
- return false
- end
- -- Loop
- os.queueEvent("restart")
- while true do
- local event, a, b, c = os.pullEvent()
- if event=="restart" then
- tools.gui.reset()
- grid = {}
- for i = 1, width do
- grid[i] = {}
- for j = 1, height do
- grid[i][j] = false
- end
- end
- lastPixel = {math.random(1, width), math.random(1, height)}
- currentColorCount = 1
- paintutils.drawPixel(lastPixel[1], lastPixel[2], getCurrentColor())
- grid[lastPixel[1]][lastPixel[2]] = true
- timer = os.startTimer(timerLength)
- elseif event=="timer" then
- if a==timer then
- if update() then
- timer = os.startTimer(timerLength)
- end
- elseif a==restartTimer then
- os.queueEvent("restart")
- end
- elseif event=="char" then
- os.queueEvent(event, a)
- tools.gui.reset()
- break
- elseif event=="monitor_touch" or event=="key" then
- tools.gui.reset()
- break
- end
- end
- term.restore()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement