Advertisement
Joshument

Untitled

Jan 8th, 2021
1,036
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.59 KB | None | 0 0
  1. math.randomseed(tick())
  2.  
  3. local scale = 4
  4. local size = 50
  5. local offset = Vector3.new(0, 0, 0)
  6.  
  7. local aliveChance = 0.45
  8. local birthLimit = 12
  9. local deathLimit = 13
  10. local stepAmount = 4
  11.  
  12.  
  13. local grid = {}
  14.  
  15. -- Generates a 2d grid based on certain dimensions
  16. for i = 1, size do
  17.     grid[i] = grid[i] or {}
  18.     for j = 1, size do
  19.         grid[i][j] = grid[i][j] or {}
  20.         for k = 1, size do
  21.             if math.random() <= aliveChance then
  22.                 grid[i][j][k] = "O"
  23.             else
  24.                 grid[i][j][k] = "X"
  25.             end
  26.         end
  27.     end
  28. end
  29. print(grid)
  30. -- Function to print the grid for troubleshooting
  31. function printGrid(grid)
  32.     for i, v in pairs(grid) do
  33.         for j, k in pairs(v) do
  34.             local printString = table.concat(k, "")
  35.             print(printString)
  36.         end
  37.     end
  38. end
  39. -- Function to count neighbors in a 2d array
  40. function countNeighbors(grid, xPos, yPos, zPos)
  41.     local neighbors = 0
  42.    
  43.     for x = -1, 1 do
  44.         for y = -1, 1 do
  45.             for z = -1, 1 do
  46.                 if xPos + x < 1 or xPos + x > size or yPos + y < 1 or yPos + y > size or zPos + z < 1 or zPos + z > size then
  47.                     neighbors += 1
  48.                 else
  49.                     local neighbor = grid[xPos + x][yPos + y][zPos + z]
  50.                     if neighbor == "O" then
  51.                         neighbors += 1
  52.                     end
  53.                 end
  54.             end
  55.         end
  56.     end
  57.  
  58.     return neighbors
  59. end
  60.  
  61. print(countNeighbors(grid, 1, 1, 1))
  62. -- Function to perform a step of cellular automata
  63. function doSimulationStep(grid)
  64.     local newGrid = {}
  65.     for i = 1, size do
  66.         newGrid[i] = newGrid[i] or {}
  67.         for j = 1, size do
  68.             newGrid[i][j] = newGrid[i][j] or {}
  69.         end
  70.     end
  71.     for x, v in pairs(grid) do
  72.         for y, w in pairs(v) do
  73.             for z, u in pairs(w) do
  74.                 local neighbors = countNeighbors(grid, x, y, z)
  75.                 if u == "O" then
  76.                     if neighbors < deathLimit then
  77.                         newGrid[x][y][z] = "X"
  78.                     else
  79.                         newGrid[x][y][z] = "O"
  80.                     end
  81.                 else
  82.                     if neighbors > birthLimit then
  83.                         newGrid[x][y][z] = "O"
  84.                     else
  85.                         newGrid[x][y][z] = "X"
  86.                     end
  87.                 end
  88.             end
  89.         end
  90.         wait()
  91.     end
  92.    
  93.     return newGrid
  94. end
  95. -- Function to place grid into workspace
  96. function placeGrid(grid)
  97.     for x, v in pairs(grid) do
  98.         for y, w in pairs(v) do
  99.             for z, u in pairs(w) do
  100.                 if u == "O" and countNeighbors(grid, x, y, z) < 26 then
  101.                     local part = Instance.new("Part")
  102.                     part.Size = Vector3.new(1, 1, 1) * scale
  103.                     part.Position = (Vector3.new(x, y, z) + offset) * scale
  104.                     part.Color = Color3.fromRGB(y * 2 + 64, y * 2 + 64, y * 2 + 64)
  105.                     part.Name = y
  106.                     part.Anchored = true
  107.                     part.Parent = workspace.CaveParts
  108.                 end
  109.             end
  110.         end
  111.         wait()
  112.     end
  113. end
  114.  
  115. for i = 1, stepAmount do
  116.     grid = doSimulationStep(grid)
  117.     print(grid)
  118. end
  119.  
  120. placeGrid(grid)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement