--[[
Creates a bridge.
--]]

return function(StartPos,EndPos,InnerSectionCount,Parent)
	local DistanceBetween = (EndPos - StartPos).magnitude
	local BridgeLength = math.ceil(DistanceBetween/8) * 8
	local CenterCF = CFrame.new(StartPos,EndPos) * CFrame.new(0,0,-DistanceBetween/2)
	
	--Create the container.
	local Container = Instance.new("Model")
	Container.Name = "Bridge"
	Container.Parent = Parent
	
	--Create the spines.
	local Spines = {}
	local SpineCount = InnerSectionCount + 1
	for i = 1,SpineCount do
		local Spine = Instance.new("Part")
		Spine.BrickColor = BrickColor.new("Dark stone grey")
		Spine.CFrame = CenterCF * CFrame.new(6 * ((-SpineCount/2) - 0.5 + i),0,0)
		Spine.Size = Vector3.new(2,1,BridgeLength - 2)
		Spine.Anchored = true
		Spine.Parent = Container
		
		table.insert(Spines,Spine)
	end
	
	--Create the path parts.
	local RelativeOffsetStart = CFrame.new(0,1,-(BridgeLength/2) + 4)
	local BridgeSections = BridgeLength/8
	local Floors = {}
	local FloorCount = InnerSectionCount + 2
	for i = 1,FloorCount do
		local RowSections = {}
		table.insert(Floors,RowSections)
		
		for j = 0,BridgeSections - 1 do
			local RelativeOffset = RelativeOffsetStart * CFrame.new(0,0,8 * j)
			local SectionCF = CenterCF * RelativeOffset
		
			local PathPart = Instance.new("Part")
			PathPart.BrickColor = BrickColor.new("Medium stone grey")
			PathPart.CFrame = SectionCF * CFrame.new(6 * ((-SpineCount/2) - 1 + i),0,0)
			PathPart.Size = Vector3.new(6,1,8)
			PathPart.Parent = Container
			if Spines[i - 1] then WeldBetween(Spines[i - 1],PathPart) end
			if Spines[i] then WeldBetween(Spines[i],PathPart) end
			table.insert(RowSections,PathPart)
		end
	end
	
	--Create the sides.
	for i = 0,BridgeSections - 1 do
		local RelativeOffset = RelativeOffsetStart * CFrame.new(0,0,8 * i)
		local SectionCF = CenterCF * RelativeOffset
		
		--Create the left side.
		local LeftSide = Instance.new("Part")
		LeftSide.BrickColor = BrickColor.new("Medium stone grey")
		LeftSide.CFrame = SectionCF * CFrame.new(-(6 * (FloorCount/2)) + 1,1.25,0)
		LeftSide.Size = Vector3.new(2,1.5,8)
		LeftSide.Parent = Container
		WeldBetween(LeftSide,Floors[1][i + 1])
		
		local LeftTop = Instance.new("Part")
		LeftTop.BrickColor = BrickColor.new("Dark stone grey")
		LeftTop.CFrame = SectionCF * CFrame.new(-(6 * (FloorCount/2)) + 1,2.5,0)
		LeftTop.Size = Vector3.new(2,1,8)
		LeftTop.Parent = Container
		WeldBetween(LeftSide,LeftTop)
		
		--Create the right side.
		local RightSide = Instance.new("Part")
		RightSide.BrickColor = BrickColor.new("Medium stone grey")
		RightSide.CFrame = SectionCF * CFrame.new((6 * (FloorCount/2)) - 1,1.25,0)
		RightSide.Size = Vector3.new(2,1.5,8)
		RightSide.Parent = Container
		WeldBetween(RightSide,Floors[#Floors][i + 1])
		
		local RightTop = Instance.new("Part")
		RightTop.BrickColor = BrickColor.new("Dark stone grey")
		RightTop.CFrame = SectionCF * CFrame.new((6 * (FloorCount/2)) - 1,2.5,0)
		RightTop.Size = Vector3.new(2,1,8)
		RightTop.Parent = Container
		WeldBetween(RightSide,RightTop)
	end
end
