doomyears

RubiconLogo

Aug 2nd, 2025 (edited)
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.24 KB | None | 0 0
  1. -- CONFIGURATION
  2. local side = "left" -- adjust if needed
  3.  
  4. -- WRAP & PREP
  5. local mon = peripheral.find("monitor")
  6. if not mon then error("No monitor found on side '"..side.."'.") end
  7. mon.setTextScale(1)
  8. mon.setBackgroundColor(colors.black)
  9. mon.clear()
  10.  
  11. -- Monitor dimensions
  12. local monW, monH = mon.getSize()
  13.  
  14. -- White square size
  15. local squareW, squareH = 5, 5
  16.  
  17. -- Initial position & velocity of white square
  18. local x, y = 1, 1
  19. local dx, dy = 1, 1
  20.  
  21. -- Binary grid setup
  22. local binaryGrid = {}
  23. local baseOneChance = 0.2
  24.  
  25. -- Function to generate the full binary map
  26. function regenerateBinaryGrid()
  27.   -- Initialize grid with 0s
  28.   for y = 1, monH do
  29.     binaryGrid[y] = {}
  30.     for x = 1, monW do
  31.       binaryGrid[y][x] = "0"
  32.     end
  33.   end
  34.  
  35.   local numClusters = math.random(3, 6) -- number of 1-islands
  36.   local maxClusterSymbols = 40
  37.  
  38.   for _ = 1, numClusters do
  39.     local centerX = math.random(5, monW - 4)
  40.     local centerY = math.random(3, monH - 2)
  41.  
  42.     local placed = 0
  43.     for dy = -4, 4 do
  44.       for dx = -4, 4 do
  45.         local dist = math.sqrt(dx * dx + dy * dy)
  46.         local falloff = math.exp(-dist^2 / 4) -- approximate Gaussian
  47.  
  48.         if math.random() < falloff then
  49.           local x = centerX + dx
  50.           local y = centerY + dy
  51.           if x >= 1 and x <= monW and y >= 1 and y <= monH and binaryGrid[y][x] == "0" then
  52.             binaryGrid[y][x] = "1"
  53.             placed = placed + 1
  54.             if placed >= maxClusterSymbols then break end
  55.           end
  56.         end
  57.       end
  58.       if placed >= maxClusterSymbols then break end
  59.     end
  60.   end
  61. end
  62.  
  63.  
  64. -- Function to generate a new rightmost column with clustering
  65. local function generateNewColumn()
  66.   local newCol = {}
  67.   for row = 1, monH do
  68.     local neighbors = 0
  69.     if binaryGrid[row][monW] == "1" then neighbors = neighbors + 1 end
  70.     if row > 1 and binaryGrid[row - 1][monW] == "1" then neighbors = neighbors + 1 end
  71.     if row < monH and binaryGrid[row + 1][monW] == "1" then neighbors = neighbors + 1 end
  72.  
  73.     local chance
  74.     if neighbors == 0 then
  75.       chance = 0.05
  76.     elseif neighbors == 1 then
  77.       chance = 0.6
  78.     elseif neighbors == 2 then
  79.       chance = 0.8
  80.     else
  81.       chance = 0.95
  82.     end
  83.  
  84.     newCol[row] = (math.random() < chance) and "1" or "0"
  85.   end
  86.   return newCol
  87. end
  88.  
  89. -- Check if square is on a "huge cluster"
  90. local function isHugeCluster()
  91.   local countOnes = 0
  92.   local total = squareW * squareH
  93.   for rowOffset = 0, squareH - 1 do
  94.     local checkY = y + rowOffset
  95.     if checkY < 1 or checkY > monH then return false end
  96.     for colOffset = 0, squareW - 1 do
  97.       local checkX = x + colOffset
  98.       if checkX < 1 or checkX > monW then return false end
  99.       if binaryGrid[checkY][checkX] == "1" then
  100.         countOnes = countOnes + 1
  101.       end
  102.     end
  103.   end
  104.   return (countOnes / total) >= 0.75
  105. end
  106.  
  107. -- Draw the binary background
  108. local function drawBinary()
  109.   for row = 1, monH do
  110.     for col = 1, monW do
  111.       mon.setCursorPos(col, row)
  112.       local bit = binaryGrid[row][col]
  113.       mon.setBackgroundColor(colors.black)
  114.       mon.setTextColor(bit == "1" and colors.green or colors.red)
  115.       mon.write(bit)
  116.     end
  117.   end
  118. end
  119.  
  120. -- Draw the square
  121. local function drawSquare(colorOverride)
  122.   for row = 0, squareH - 1 do
  123.     local drawY = y + row
  124.     if drawY >= 1 and drawY <= monH then
  125.       for col = 0, squareW - 1 do
  126.         local drawX = x + col
  127.         if drawX >= 1 and drawX <= monW then
  128.           mon.setCursorPos(drawX, drawY)
  129.           if colorOverride == "found" then
  130.             mon.setBackgroundColor(colors.green)
  131.             mon.setTextColor(colors.white)
  132.           else
  133.             mon.setBackgroundColor(colors.white)
  134.             mon.setTextColor(colors.blue)
  135.           end
  136.           mon.write("#")
  137.         end
  138.       end
  139.     end
  140.   end
  141. end
  142.  
  143. -- Draw bottom text
  144. local function drawBottomText(text, count)
  145.   local rightText = "Count: " .. tostring(count)
  146.   local space = monW - (#text + #rightText)
  147.   if space < 1 then space = 1 end
  148.   local finalLine = text .. string.rep(" ", space) .. rightText
  149.   mon.setCursorPos(1, monH)
  150.   mon.setBackgroundColor(colors.white)
  151.   mon.setTextColor(colors.blue)
  152.   mon.write(finalLine)
  153. end
  154.  
  155. -- Initial setup
  156. regenerateBinaryGrid()
  157. local scrollTimer = os.clock()
  158. local dotsTimer = os.clock()
  159. local dotsCount = 0
  160. local dimensionCount = 0
  161. local paused = false
  162. local pauseStart = 0
  163. local baseText = "SEARCH IN PROGRESS"
  164. local pauseDuration = 5
  165.  
  166. -- MAIN LOOP
  167. while true do
  168.   local now = os.clock()
  169.  
  170.   if not paused then
  171.     -- Update background scroll
  172.     if now - scrollTimer >= 1 then
  173.       for row = 1, monH do
  174.         for col = 1, monW - 1 do
  175.           binaryGrid[row][col] = binaryGrid[row][col + 1]
  176.         end
  177.       end
  178.       local newCol = generateNewColumn()
  179.       for row = 1, monH do
  180.         binaryGrid[row][monW] = newCol[row]
  181.       end
  182.       scrollTimer = now
  183.     end
  184.  
  185.     -- Animate dots
  186.     if now - dotsTimer >= 0.5 then
  187.       dotsCount = (dotsCount + 1) % 4
  188.       dotsTimer = now
  189.     end
  190.  
  191.     -- Draw everything
  192.     mon.clear()
  193.     drawBinary()
  194.     drawSquare()
  195.     drawBottomText(baseText .. string.rep(".", dotsCount), dimensionCount)
  196.  
  197.     -- Sleep and move square
  198.     sleep(0.1)
  199.  
  200.     local nx, ny = x + dx, y + dy
  201.     if nx < 1 or (nx + squareW - 1) > monW then
  202.       dx = -dx
  203.       nx = x + dx
  204.     end
  205.     if ny < 1 or (ny + squareH - 1) > monH then
  206.       dy = -dy
  207.       ny = y + dy
  208.     end
  209.  
  210.     x, y = nx, ny
  211.  
  212.     if isHugeCluster() then
  213.       paused = true
  214.       pauseStart = now
  215.     end
  216.  
  217.   else
  218.     -- PAUSED: show green square + found message
  219.     -- PAUSED: flashing green/white square with message
  220.     mon.clear()
  221.     drawBinary()
  222.  
  223.     -- Flicker effect: alternate every frame
  224.     local flickerColor = math.floor((now * 10) % 2) == 0 and "found" or "normal"
  225.     drawSquare(flickerColor)
  226.  
  227.     drawBottomText("DIMENSION FOUND, ADDING TO DICTIONARY...", dimensionCount, colors.green, colors.white)
  228.     sleep(0.1)
  229.  
  230.     -- After delay, reset map & resume
  231.     if now - pauseStart >= pauseDuration then
  232.       dimensionCount = dimensionCount + 1
  233.       regenerateBinaryGrid()
  234.       scrollTimer = now
  235.       dotsTimer = now
  236.       paused = false
  237.     else
  238.       sleep(0.05)
  239.     end
  240.   end
  241. end
  242.  
Advertisement
Add Comment
Please, Sign In to add comment