Advertisement
Joshument

Untitled

Oct 27th, 2020
987
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.43 KB | None | 0 0
  1. math.randomseed(tick())
  2.  
  3. local initialChance = 0.4
  4. local birthLimit = 26
  5. local deathLimit = 26
  6. local steps = 6
  7. local gridSize = 16
  8. local scale = 8
  9.  
  10. function generateGrid()
  11.     local newGrid = {{{}}}
  12.    
  13.     for x = 1, gridSize do
  14.         for y = 1, gridSize do
  15.             for z = 1, gridSize do
  16.                 local number = math.random(1, 10) / 10
  17.                 if number > initialChance then
  18.                     newGrid[x] = newGrid[x] or {}
  19.                     newGrid[x][y] = newGrid[x][y] or {}
  20.                     newGrid[x][y][z] = false
  21.                     print("Grid space is Dead")
  22.                 else
  23.                     newGrid[x] = newGrid[x] or {}
  24.                     newGrid[x][y] = newGrid[x][y] or {}
  25.                     newGrid[x][y][z] = true
  26.                     print("Grid Space is Alive")
  27.                 end
  28.             end
  29.         end
  30.     end
  31.    
  32.     return newGrid
  33. end
  34.  
  35. function countLivingNeighbors(grid, x, y, z)
  36.     local counter = 0
  37.    
  38.     for i = -1, 1 do
  39.         for j = -1, 1 do
  40.             for k = -1, 1 do
  41.                 if grid[x + i] == nil then
  42.                     counter = counter + 1
  43.                 elseif grid[x + i][x + j] == nil then
  44.                     counter = counter + 1
  45.                 elseif grid[x + i][x + j][x + k] == nil then
  46.                     counter = counter + 1
  47.                 elseif grid[x + i][x + j][x + k] == true then
  48.                     if i == 0 and j == 0 and k == 0 then
  49.                     else
  50.                         counter = counter + 1
  51.                     end
  52.                 end
  53.             end
  54.         end
  55.     end
  56.    
  57.     return counter
  58. end
  59.  
  60. function incrementGrid(grid)
  61.     local newGrid = {{{}}}
  62.    
  63.     for x = 1, gridSize do
  64.         for y = 1, gridSize do
  65.             for z = 1, gridSize do
  66.                 print("Current Iteration: "..x.." | "..y.." | "..z)
  67.                     local amount = countLivingNeighbors(grid, x, y, z)
  68.                     if amount < deathLimit then
  69.                         newGrid[x] = newGrid[x] or {}
  70.                         newGrid[x][y] = newGrid[x][y] or {}
  71.                         newGrid[x][y][z] = false
  72.                     elseif amount >= birthLimit then
  73.                         newGrid[x] = newGrid[x] or {}
  74.                         newGrid[x][y] = newGrid[x][y] or {}
  75.                         newGrid[x][y][z] = true
  76.                     else
  77.                         newGrid[x] = newGrid[x] or {}
  78.                         newGrid[x][y] = newGrid[x][y] or {}
  79.                         newGrid[x][y][z] = true
  80.                     end
  81.             end
  82.         end
  83.         wait()
  84.     end
  85.    
  86.     return newGrid
  87. end
  88.  
  89.  
  90.  
  91. local grid = generateGrid()
  92. print(grid[2][1][1])
  93. for i = 1, steps do
  94.     local grid = incrementGrid(grid)
  95.     print(grid[2][1][1])
  96. end
  97.  
  98. for x = 1, gridSize do
  99.     for y = 1, gridSize do
  100.         for z = 1, gridSize do
  101.             if grid[x][y][z] == true then
  102.                 local part = Instance.new("Part")
  103.                 part.Size = Vector3.new(scale, scale, scale)
  104.                 part.Position = Vector3.new(x, y, z) * scale
  105.                 part.Anchored = true
  106.                 part.Name = "CaveWall"
  107.                 part.Parent = workspace
  108.             end
  109.         end
  110.     end
  111. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement