yeeeeeeeeeeeee

Josh’s

Nov 7th, 2025
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. -- Dynamic Cellular Simulation with Fade + Dead-Zone Regeneration
  2. math.randomseed(os.time())
  3.  
  4. local termW, termH = term.getSize()
  5. local width, height = termW, termH
  6.  
  7. local aliveChar = "O"
  8. local deadChar = "o"
  9.  
  10. local steps = 8
  11. local fadeSpeed = 0.1
  12. local randomFlipChance = 0.02
  13.  
  14. -- How many dead cells trigger regeneration
  15. local deadThreshold = 50
  16. -- How many random blobs to spawn when dead area is too big
  17. local blobsToSpawn = 5
  18. -- Blob size radius
  19. local blobRadius = 2
  20.  
  21. local grid = {}
  22. for y = 1, height do
  23. grid[y] = {}
  24. for x = 1, width do
  25. grid[y][x] = math.random()
  26. end
  27. end
  28.  
  29. local function countAliveNeighbors(x, y)
  30. local count = 0
  31. for dy = -1,1 do
  32. for dx = -1,1 do
  33. if not (dx == 0 and dy == 0) then
  34. local nx, ny = x + dx, y + dy
  35. if nx >= 1 and nx <= width and ny >= 1 and ny <= height then
  36. if grid[ny][nx] > 0.5 then
  37. count = count + 1
  38. end
  39. end
  40. end
  41. end
  42. end
  43. return count
  44. end
  45.  
  46. local function intensityToChar(intensity)
  47. local step = math.floor(intensity * steps)
  48. if step < 1 then
  49. return deadChar
  50. else
  51. return aliveChar
  52. end
  53. end
  54.  
  55. local function spawnBlobs()
  56. for i = 1, blobsToSpawn do
  57. local bx = math.random(3, width-3)
  58. local by = math.random(3, height-3)
  59.  
  60. for dy = -blobRadius, blobRadius do
  61. for dx = -blobRadius, blobRadius do
  62. local nx, ny = bx + dx, by + dy
  63. if nx >= 1 and nx <= width and ny >= 1 and ny <= height then
  64. -- increase intensity toward alive
  65. grid[ny][nx] = math.min(1, grid[ny][nx] + math.random() * 0.5 + 0.5)
  66. end
  67. end
  68. end
  69. end
  70. end
  71.  
  72. local function updateGrid()
  73. local deadCount = 0
  74. local newGrid = {}
  75.  
  76. for y = 1, height do
  77. newGrid[y] = {}
  78. for x = 1, width do
  79. local current = grid[y][x]
  80. local neighbors = countAliveNeighbors(x, y)
  81.  
  82. -- neighbor influence
  83. local target
  84. local nInfluence = neighbors / 8
  85. if math.random() < nInfluence then
  86. target = 1
  87. elseif math.random() < randomFlipChance then
  88. target = 1 - current
  89. else
  90. target = 0
  91. end
  92.  
  93. if current < target then
  94. current = math.min(current + fadeSpeed, target)
  95. elseif current > target then
  96. current = math.max(current - fadeSpeed, target)
  97. end
  98.  
  99. newGrid[y][x] = current
  100. if current < 0.2 then
  101. deadCount = deadCount + 1
  102. end
  103. end
  104. end
  105.  
  106. grid = newGrid
  107.  
  108. -- If too many dead, spawn random living blobs
  109. if deadCount > deadThreshold then
  110. spawnBlobs()
  111. end
  112. end
  113.  
  114. local function drawGrid()
  115. term.clear()
  116. term.setCursorPos(1,1)
  117. for y = 1, height do
  118. local line = ""
  119. for x = 1, width do
  120. line = line .. intensityToChar(grid[y][x])
  121. end
  122. print(line)
  123. end
  124. end
  125.  
  126. while true do
  127. drawGrid()
  128. updateGrid()
  129. sleep(0.15)
  130. end
  131.  
Advertisement
Add Comment
Please, Sign In to add comment