yeeeeeeeeeeeee

Random

Nov 5th, 2025
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. -- Game-of-Life style simulation with random change + fade, for ComputerCraft Tweaked
  2. math.randomseed(os.time())
  3.  
  4. -- Settings
  5. local termW, termH = term.getSize()
  6. local width, height = termW, termH -- use full terminal size
  7. local aliveChar = "O"
  8. local deadChar = "o"
  9. local steps = 8 -- number of fade steps (0=dead → full alive)
  10. local fadeSpeed = 0.1 -- how quickly intensity moves toward target
  11. local randomFlipChance = 0.02 -- baseline random flip each tick
  12.  
  13. -- grid holds intensities 0..1
  14. local grid = {}
  15. for y = 1, height do
  16. grid[y] = {}
  17. for x = 1, width do
  18. grid[y][x] = math.random() -- random starting intensity
  19. end
  20. end
  21.  
  22. -- count alive neighbours (intensity > 0.5 counts)
  23. local function countNeighbours(x, y)
  24. local count = 0
  25. for dy = -1,1 do
  26. for dx = -1,1 do
  27. if not (dx == 0 and dy == 0) then
  28. local nx, ny = x + dx, y + dy
  29. if nx >= 1 and nx <= width and ny >= 1 and ny <= height then
  30. if grid[ny][nx] > 0.5 then count = count + 1 end
  31. end
  32. end
  33. end
  34. end
  35. return count
  36. end
  37.  
  38. -- map intensity to a symbol
  39. local function intensityToChar(intensity)
  40. local step = math.floor(intensity * steps)
  41. if step < 1 then
  42. return deadChar
  43. else
  44. return aliveChar
  45. end
  46. end
  47.  
  48. -- update grid
  49. local function updateGrid()
  50. local newGrid = {}
  51. for y = 1, height do
  52. newGrid[y] = {}
  53. for x = 1, width do
  54. local neighbours = countNeighbours(x, y)
  55. local current = grid[y][x]
  56. -- neighbour influence: more neighbours → target alive
  57. local target
  58. local neighborInfluence = neighbours / 8
  59. if math.random() < neighborInfluence then
  60. target = 1
  61. elseif math.random() < randomFlipChance then
  62. target = 1 - current
  63. else
  64. target = 0
  65. end
  66. -- fade toward target
  67. if current < target then
  68. current = math.min(current + fadeSpeed, target)
  69. elseif current > target then
  70. current = math.max(current - fadeSpeed, target)
  71. end
  72. newGrid[y][x] = current
  73. end
  74. end
  75. grid = newGrid
  76. end
  77.  
  78. -- draw grid
  79. local function drawGrid()
  80. term.clear()
  81. term.setCursorPos(1,1)
  82. for y = 1, height do
  83. local line = ""
  84. for x = 1, width do
  85. line = line .. intensityToChar(grid[y][x])
  86. end
  87. print(line)
  88. end
  89. end
  90.  
  91. -- main loop
  92. while true do
  93. drawGrid()
  94. updateGrid()
  95. sleep(0.2)
  96. end
  97.  
Advertisement
Add Comment
Please, Sign In to add comment