Cool Custom Cellular Automata

Experience
https://www.roblox.com/games/9606705838/LinearCellAuto

Clips

non-optimized code

local Cells = workspace:WaitForChild("Cells")
local CellStore = game.Lighting:WaitForChild("Cell")
local RunService = game:GetService("RunService")
local Plr = game.Players.LocalPlayer
local Mouse = Plr:GetMouse()
local CellMap = {}
local Speed = 8

for x = 0,49 do
	CellMap[x+1] = {}
	for z = 0,49 do
		local Cell = CellStore:Clone()
		Cell.Parent = Cells
		Cell.Position = Vector3.new(x,0,z)
		Cell.Name = tostring(x).."0"..tostring(z)
		CellMap[x+1][z+1] = Cell
		--[[if math.random(1,100) == 1 then
			Cell.Color = Color3.new(1, 1, 1)
		end]]
	end
	wait()
end

function CheckAlive(v)
	if v and v.Color ~= Color3.new(0,0,0) then
		return true
	end
	return false
end

function CheckAlive2(v)
	if v and v.Color ~= Color3.new(0,0,0) then
		return true
	elseif v then
		return true
	end
	return false
end

function Kill(v)
	if v then
		v.Color = Color3.new(0,0,0)
	end
end

script.Parent:WaitForChild("TextButton").MouseButton1Down:Connect(function()
	Speed = 0
	script.Parent.TextBox.Text = "0"
	RunService.Heartbeat:Wait()
	RunService.Heartbeat:Wait()
	for x = 0,51 do
		for z = 0,51 do
			if Cells:FindFirstChild(tostring(x).."0"..tostring(z)) then
				Kill(Cells:FindFirstChild(tostring(x).."0"..tostring(z)))
			end
		end
	end
end)

Mouse.Button1Down:Connect(function()
	local X = math.floor(Mouse.Hit.X + 0.4)
	local Z = math.floor(Mouse.Hit.Z + 0.4)
	if Cells:FindFirstChild(tostring(X).."0"..tostring(Z)) then
		local v = Cells:FindFirstChild(tostring(X).."0"..tostring(Z))
		if v and v.Color == Color3.new(0,0,0) then
			v.Color = Color3.new(1,1,1)
			v.VAL.Value = CellStore.VAL.Value
		else
			Kill(v)
		end
	end
end)

function Bloomify()
	if Speed > 0 then
		local Bads = {}
		local Alives = {}

		for x = 1,#CellMap do
			for z,v in pairs(CellMap[x]) do

				if CheckAlive(v) == true then
					local Right = Cells:FindFirstChild(tostring(x-1 + 1).."0"..tostring(z-1))
					local Left  = Cells:FindFirstChild(tostring(x-1 - 1).."0"..tostring(z-1))
					local Top   = Cells:FindFirstChild(tostring(x-1).."0"..tostring(z-1 + 1))
					local Down  = Cells:FindFirstChild(tostring(x-1).."0"..tostring(z-1 - 1))
					local Count = 0
					
					if CheckAlive(Right) == true then Count += 1 end if CheckAlive(Left) == true then Count += 1 end if CheckAlive(Top) == true then Count += 1 end if CheckAlive(Down) == true then Count += 1 end
					
					if CheckAlive(Top) == true or CheckAlive(Down) == true or CheckAlive(Left) == true or CheckAlive(Right) == true then
						if Count >= 3 then
							table.insert(Bads,v)
						end
					else
						local Corner1 = Cells:FindFirstChild(tostring(x-1 + 1).."0"..tostring(z-1 + 1))
						local Corner2 = Cells:FindFirstChild(tostring(x-1 - 1).."0"..tostring(z-1 + 1))
						local Corner3 = Cells:FindFirstChild(tostring(x-1 + 1).."0"..tostring(z-1 - 1))
						local Corner4 = Cells:FindFirstChild(tostring(x-1 - 1).."0"..tostring(z-1 - 1))
						
						if CheckAlive(Corner1) == true or CheckAlive(Corner2) == true or CheckAlive(Corner3) == true or CheckAlive(Corner4) == true or Count ~= 0 then
							table.insert(Bads,v)
						end
					end
					
					v.VAL.Value = Count
				end

			end
			--[[if x%20 == 0 then
				RunService.Heartbeat:Wait()
			end]]
		end

		for i,v in pairs(Bads) do
			Kill(v)
		end
		Bads = {}


		for x = 1,#CellMap do
			for z,v in pairs(CellMap[x]) do
				if CheckAlive(v) == true then
					local Right = Cells:FindFirstChild(tostring(x-1 + 1).."0"..tostring(z-1))
					local Left  = Cells:FindFirstChild(tostring(x-1 - 1).."0"..tostring(z-1))
					local Top   = Cells:FindFirstChild(tostring(x-1).."0"..tostring(z-1 + 1))
					local Down  = Cells:FindFirstChild(tostring(x-1).."0"..tostring(z-1 - 1))
					local Count = 0

					if CheckAlive(Right) == true then Count += 1 end if CheckAlive(Left) == true then Count += 1 end if CheckAlive(Top) == true then Count += 1 end if CheckAlive(Down) == true then Count += 1 end
					
					if not (CheckAlive(Top) == true and CheckAlive(Down) == true and CheckAlive(Left) == true and CheckAlive(Right) == true) then
						if CheckAlive(Left) == true then
							if Top then
								table.insert(Alives,{Top,Count})
							end
							if Down then
								table.insert(Alives,{Down,Count})
							end
							if Top and Down then
								table.insert(Bads,v)
							end
						end
						if CheckAlive(Right) == true then
							if Down then
								table.insert(Alives,{Down,Count})
							end
							if Top then
								table.insert(Alives,{Top,Count})
							end
							if Top and Down then
								table.insert(Bads,v)
							end
						end
						if CheckAlive(Top) == true then
							if Right then
								table.insert(Alives,{Right,Count})
							end
							if Left then
								table.insert(Alives,{Left,Count})
							end
							if Right and Left then
								table.insert(Bads,v)
							end
						end
						if CheckAlive(Down) == true then
							if Left then
								table.insert(Alives,{Left,Count})
							end
							if Right then
								table.insert(Alives,{Right,Count})
							end
							if Right and Left then
								table.insert(Bads,v)
							end
						end
					else
						table.insert(Bads,v)
					end
				end

			end
			if x%Speed == 0 then
				RunService.Heartbeat:Wait()
			end
		end
		
		for i,v in pairs(Bads) do
			Kill(v)
		end
		
		for i,v in pairs(Alives) do
			local col = Color3.new(1,1,1)
			v[2] += v[1].VAL.Value
			if v[2] == 1 then
				col = Color3.new(1, 0, 0)
			elseif v[2] == 2 then
				col = Color3.new(1, 0.25, 0)
			elseif v[2] == 3 then
				col = Color3.new(1, 0.5, 0)
			elseif v[2] == 4 then
				col = Color3.new(1, 1, 0)
			elseif v[2] == 5 then
				col = Color3.new(0.5, 1, 0)
			elseif v[2] == 6 then
				col = Color3.new(0, 1, 0)
			end
			v[1].Color = col
		end
	else
		RunService.Heartbeat:Wait()
	end
end

local Text = script.Parent.TextBox
while RunService.Heartbeat:Wait() do
	if tonumber(Text.Text) and tonumber(Text.Text) >= 0 then
		Speed = tonumber(Text.Text)
	end
	Bloomify()
end

--[[for i,v in pairs(Cells:GetChildren()) do
	local n = v.Name
	v.Name = " "
	if Cells:FindFirstChild(n) then
		print(n)
	end
	v.Name = n
end]]

I would appreciate feedback!

5 Likes

Looks beautiful! Maybe try this in 3D?

1 Like

Woah! This is a really smart idea and very neat! I still have no idea what it is though…

As brickman said it would also be cool to do this in 3d.

1 Like