Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- CONFIGURATION
- local side = "left" -- adjust if needed
- -- WRAP & PREP
- local mon = peripheral.find("monitor")
- if not mon then error("No monitor found on side '"..side.."'.") end
- mon.setTextScale(1)
- mon.setBackgroundColor(colors.black)
- mon.clear()
- -- Monitor dimensions
- local monW, monH = mon.getSize()
- -- White square size
- local squareW, squareH = 5, 5
- -- Initial position & velocity of white square
- local x, y = 1, 1
- local dx, dy = 1, 1
- -- Binary grid setup
- local binaryGrid = {}
- local baseOneChance = 0.2
- -- Function to generate the full binary map
- function regenerateBinaryGrid()
- -- Initialize grid with 0s
- for y = 1, monH do
- binaryGrid[y] = {}
- for x = 1, monW do
- binaryGrid[y][x] = "0"
- end
- end
- local numClusters = math.random(3, 6) -- number of 1-islands
- local maxClusterSymbols = 40
- for _ = 1, numClusters do
- local centerX = math.random(5, monW - 4)
- local centerY = math.random(3, monH - 2)
- local placed = 0
- for dy = -4, 4 do
- for dx = -4, 4 do
- local dist = math.sqrt(dx * dx + dy * dy)
- local falloff = math.exp(-dist^2 / 4) -- approximate Gaussian
- if math.random() < falloff then
- local x = centerX + dx
- local y = centerY + dy
- if x >= 1 and x <= monW and y >= 1 and y <= monH and binaryGrid[y][x] == "0" then
- binaryGrid[y][x] = "1"
- placed = placed + 1
- if placed >= maxClusterSymbols then break end
- end
- end
- end
- if placed >= maxClusterSymbols then break end
- end
- end
- end
- -- Function to generate a new rightmost column with clustering
- local function generateNewColumn()
- local newCol = {}
- for row = 1, monH do
- local neighbors = 0
- if binaryGrid[row][monW] == "1" then neighbors = neighbors + 1 end
- if row > 1 and binaryGrid[row - 1][monW] == "1" then neighbors = neighbors + 1 end
- if row < monH and binaryGrid[row + 1][monW] == "1" then neighbors = neighbors + 1 end
- local chance
- if neighbors == 0 then
- chance = 0.05
- elseif neighbors == 1 then
- chance = 0.6
- elseif neighbors == 2 then
- chance = 0.8
- else
- chance = 0.95
- end
- newCol[row] = (math.random() < chance) and "1" or "0"
- end
- return newCol
- end
- -- Check if square is on a "huge cluster"
- local function isHugeCluster()
- local countOnes = 0
- local total = squareW * squareH
- for rowOffset = 0, squareH - 1 do
- local checkY = y + rowOffset
- if checkY < 1 or checkY > monH then return false end
- for colOffset = 0, squareW - 1 do
- local checkX = x + colOffset
- if checkX < 1 or checkX > monW then return false end
- if binaryGrid[checkY][checkX] == "1" then
- countOnes = countOnes + 1
- end
- end
- end
- return (countOnes / total) >= 0.75
- end
- -- Draw the binary background
- local function drawBinary()
- for row = 1, monH do
- for col = 1, monW do
- mon.setCursorPos(col, row)
- local bit = binaryGrid[row][col]
- mon.setBackgroundColor(colors.black)
- mon.setTextColor(bit == "1" and colors.green or colors.red)
- mon.write(bit)
- end
- end
- end
- -- Draw the square
- local function drawSquare(colorOverride)
- for row = 0, squareH - 1 do
- local drawY = y + row
- if drawY >= 1 and drawY <= monH then
- for col = 0, squareW - 1 do
- local drawX = x + col
- if drawX >= 1 and drawX <= monW then
- mon.setCursorPos(drawX, drawY)
- if colorOverride == "found" then
- mon.setBackgroundColor(colors.green)
- mon.setTextColor(colors.white)
- else
- mon.setBackgroundColor(colors.white)
- mon.setTextColor(colors.blue)
- end
- mon.write("#")
- end
- end
- end
- end
- end
- -- Draw bottom text
- local function drawBottomText(text, count)
- local rightText = "Count: " .. tostring(count)
- local space = monW - (#text + #rightText)
- if space < 1 then space = 1 end
- local finalLine = text .. string.rep(" ", space) .. rightText
- mon.setCursorPos(1, monH)
- mon.setBackgroundColor(colors.white)
- mon.setTextColor(colors.blue)
- mon.write(finalLine)
- end
- -- Initial setup
- regenerateBinaryGrid()
- local scrollTimer = os.clock()
- local dotsTimer = os.clock()
- local dotsCount = 0
- local dimensionCount = 0
- local paused = false
- local pauseStart = 0
- local baseText = "SEARCH IN PROGRESS"
- local pauseDuration = 5
- -- MAIN LOOP
- while true do
- local now = os.clock()
- if not paused then
- -- Update background scroll
- if now - scrollTimer >= 1 then
- for row = 1, monH do
- for col = 1, monW - 1 do
- binaryGrid[row][col] = binaryGrid[row][col + 1]
- end
- end
- local newCol = generateNewColumn()
- for row = 1, monH do
- binaryGrid[row][monW] = newCol[row]
- end
- scrollTimer = now
- end
- -- Animate dots
- if now - dotsTimer >= 0.5 then
- dotsCount = (dotsCount + 1) % 4
- dotsTimer = now
- end
- -- Draw everything
- mon.clear()
- drawBinary()
- drawSquare()
- drawBottomText(baseText .. string.rep(".", dotsCount), dimensionCount)
- -- Sleep and move square
- sleep(0.1)
- local nx, ny = x + dx, y + dy
- if nx < 1 or (nx + squareW - 1) > monW then
- dx = -dx
- nx = x + dx
- end
- if ny < 1 or (ny + squareH - 1) > monH then
- dy = -dy
- ny = y + dy
- end
- x, y = nx, ny
- if isHugeCluster() then
- paused = true
- pauseStart = now
- end
- else
- -- PAUSED: show green square + found message
- -- PAUSED: flashing green/white square with message
- mon.clear()
- drawBinary()
- -- Flicker effect: alternate every frame
- local flickerColor = math.floor((now * 10) % 2) == 0 and "found" or "normal"
- drawSquare(flickerColor)
- drawBottomText("DIMENSION FOUND, ADDING TO DICTIONARY...", dimensionCount, colors.green, colors.white)
- sleep(0.1)
- -- After delay, reset map & resume
- if now - pauseStart >= pauseDuration then
- dimensionCount = dimensionCount + 1
- regenerateBinaryGrid()
- scrollTimer = now
- dotsTimer = now
- paused = false
- else
- sleep(0.05)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment