-- / Functions / --

local function CreateNoise(GridX, GridZ, Seed)
	local GridY = math.noise(
		(GridX * Configuration.Frequency.Value) / 10,
		(GridZ * Configuration.Frequency.Value) / 10,
		Seed
	) * Random.new():NextNumber(19.5, 20)
		
	return (GridY)
end

local function CreateNewGrass(Size, Position, Parent)
	local ExistingBlock = Parent:FindFirstChild(Position.X..", "..Position.Z)
	
	if ExistingBlock then
		print("existing")
		
	else
		local Block = Instance.new("Part")
		Block.Parent = Parent

		-- / Properties / --
		Block.Name = Position.X..", "..Position.Z
		Block.Anchored = true
		Block.Size = Size
		Block.Position = Position

		-- / Visuals / --
		
		if Block.Position.Y <= -1 then
			Block.Color = Color3.fromRGB(255, 220, 114)
			Block.Material = Enum.Material.Sand
			
		elseif Block.Position.Y > -1 and Block.Position.Y <= 1.25 then
			Block.Color = Color3.fromRGB(100, 100, 100)
			Block.Material = Enum.Material.Rock
			
		else
			Block.Color = Color3.fromRGB(0, 85, 0)
			Block.Material = Enum.Material.Grass
			
		end
	end
	---game.Workspace.Terrain:FillBlock(CFrame.new(Position), Size, Enum.Material.Grass)

end

local function CreateNewChunk(ChunkPositionX, ChunkPositionZ)
	if not Chunks:FindFirstChild(ChunkPositionX..", "..ChunkPositionZ) then
		print(ChunkPositionX..", "..ChunkPositionZ)
		
		local NewChunk = Instance.new("StringValue")
		NewChunk.Parent = Chunks
		
		NewChunk.Name = ChunkPositionX..", "..ChunkPositionZ
		
		local Water = game.Workspace.Terrain:FillBlock(CFrame.new(ChunkPositionX, -10, ChunkPositionZ), Vector3.new(Configuration.ChunkSize.Value, 16, Configuration.ChunkSize.Value), Enum.Material.Water)
		
		for iX = ChunkPositionX + (Configuration.ChunkSize.Value / 2), (ChunkPositionX - (Configuration.ChunkSize.Value / 2)) + 4, -Configuration.Grid.Value do
			for iZ = ChunkPositionZ + (Configuration.ChunkSize.Value / 2), (ChunkPositionZ - (Configuration.ChunkSize.Value / 2)) + 4, -Configuration.Grid.Value do
				local GridY = CreateNoise(iX, iZ)

				CreateNewGrass(Configuration.GrassSize.Value, Vector3.new(iX, GridY, iZ), NewChunk)

			end

			task.wait()
		end
	end
end

if not Chunks:FindFirstChild("0, 0") then
	CreateNewChunk(0, 0)
	
end
