Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- math.randomseed(tick())
- local initialChance = 0.4
- local birthLimit = 26
- local deathLimit = 26
- local steps = 6
- local gridSize = 16
- local scale = 8
- function generateGrid()
- local newGrid = {{{}}}
- for x = 1, gridSize do
- for y = 1, gridSize do
- for z = 1, gridSize do
- local number = math.random(1, 10) / 10
- if number > initialChance then
- newGrid[x] = newGrid[x] or {}
- newGrid[x][y] = newGrid[x][y] or {}
- newGrid[x][y][z] = false
- print("Grid space is Dead")
- else
- newGrid[x] = newGrid[x] or {}
- newGrid[x][y] = newGrid[x][y] or {}
- newGrid[x][y][z] = true
- print("Grid Space is Alive")
- end
- end
- end
- end
- return newGrid
- end
- function countLivingNeighbors(grid, x, y, z)
- local counter = 0
- for i = -1, 1 do
- for j = -1, 1 do
- for k = -1, 1 do
- if grid[x + i] == nil then
- counter = counter + 1
- elseif grid[x + i][x + j] == nil then
- counter = counter + 1
- elseif grid[x + i][x + j][x + k] == nil then
- counter = counter + 1
- elseif grid[x + i][x + j][x + k] == true then
- if i == 0 and j == 0 and k == 0 then
- else
- counter = counter + 1
- end
- end
- end
- end
- end
- return counter
- end
- function incrementGrid(grid)
- local newGrid = {{{}}}
- for x = 1, gridSize do
- for y = 1, gridSize do
- for z = 1, gridSize do
- print("Current Iteration: "..x.." | "..y.." | "..z)
- local amount = countLivingNeighbors(grid, x, y, z)
- if amount < deathLimit then
- newGrid[x] = newGrid[x] or {}
- newGrid[x][y] = newGrid[x][y] or {}
- newGrid[x][y][z] = false
- elseif amount >= birthLimit then
- newGrid[x] = newGrid[x] or {}
- newGrid[x][y] = newGrid[x][y] or {}
- newGrid[x][y][z] = true
- else
- newGrid[x] = newGrid[x] or {}
- newGrid[x][y] = newGrid[x][y] or {}
- newGrid[x][y][z] = true
- end
- end
- end
- wait()
- end
- return newGrid
- end
- local grid = generateGrid()
- print(grid[2][1][1])
- for i = 1, steps do
- local grid = incrementGrid(grid)
- print(grid[2][1][1])
- end
- for x = 1, gridSize do
- for y = 1, gridSize do
- for z = 1, gridSize do
- if grid[x][y][z] == true then
- local part = Instance.new("Part")
- part.Size = Vector3.new(scale, scale, scale)
- part.Position = Vector3.new(x, y, z) * scale
- part.Anchored = true
- part.Name = "CaveWall"
- part.Parent = workspace
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement