Joshument

Untitled

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