
/*

	To see the constraints do :

		ent_bbox phys_constraint

*/

TOOL.ClientConVar[ "forcelimit" ]		= "0"

TOOL.ServerConVar[ "multi_maxobjects" ]		= 16
TOOL.ServerConVar[ "multi_maxwelds" ]		= 160

function TOOL:LeftClick( trace )

	if (trace.Entity:IsValid() && trace.Entity:IsPlayer()) then return false end
	
	// If there's no physics object then we can't constraint it!
	if ( SERVER && !util.IsValidPhysicsObject( trace.Entity, trace.PhysicsBone ) ) then return false end

	if self.multiweld then 
		if ( CLIENT ) then
			self:ClearObjects()
			return true
		end
		self:DoMultiWeld()
		self.multiweld = nil
		return true
	end

	local iNum = self:NumObjects()
	local Phys = trace.Entity:GetPhysicsObjectNum( trace.PhysicsBone )
	self:SetObject( iNum + 1, trace.Entity, trace.HitPos, Phys, trace.PhysicsBone, trace.HitNormal )
	
	if ( CLIENT ) then
		if ( iNum > 0 ) then self:ClearObjects() end
		return true
	end

	if ( iNum > 0 ) then
	
		// Get client's CVars
		local forcelimit = tonumber( self:GetClientInfo( "forcelimit" ) )

		// Get information we're about to use
		local Ent1,  Ent2  = self:GetEnt(1),  self:GetEnt(2)
		local Bone1, Bone2 = self:GetBone(1), self:GetBone(2)

		local constraint = constraint.Weld( Ent1, Ent2, Bone1, Bone2, forcelimit )

		undo.Create("Weld")
		undo.AddEntity( constraint )
		undo.SetPlayer( self:GetOwner() )
		undo.Finish()
		
		self:GetOwner():AddCleanup( "constraints", constraint )

		// Clear the objects so we're ready to go again
		self:ClearObjects()

	else
	
		self:SetStage( iNum+1 )
	
	end
	
	return true

end

function TOOL:RightClick( trace )

	if ( !trace.Entity:IsValid() || trace.Entity:IsPlayer() ) then return false end
	
	// If there's no physics object then we can't constraint it!
	if ( SERVER && !util.IsValidPhysicsObject( trace.Entity, trace.PhysicsBone ) ) then return false end

	local iNum = self:NumObjects()
	
	local Phys = trace.Entity:GetPhysicsObjectNum( trace.PhysicsBone )

	if ( iNum >= tonumber( self:GetServerInfo( "multi_maxobjects" ) ) ) then
		return false
	end
	
	self:SetObject( iNum + 1, trace.Entity, trace.HitPos, Phys, trace.PhysicsBone, trace.HitNormal )
	self.multiweld = true
	self:SetStage( iNum+1 )
	
	return true
	
end

function TOOL:DoMultiWeld()

	local forcelimit = tonumber( self:GetClientInfo( "forcelimit" ) )
	local constraintTable = {}
	
	for obj1 = 1, mathx.Clamp( self:NumObjects(), 1, tonumber( self:GetServerInfo( "multi_maxobjects" ) ) ) do
		for obj2 = obj1, mathx.Clamp( self:NumObjects(), obj1, tonumber( self:GetServerInfo( "multi_maxwelds" ) ) ) do
			
			local Ent1,  Ent2  = self:GetEnt(obj1),  self:GetEnt(obj2)
			local Bone1, Bone2 = self:GetBone(obj1), self:GetBone(obj2)

			if Ent1:IsValid() and Ent2:IsValid() and Ent1 != Ent2 then
				local constraint = constraint.Weld( Ent1, Ent2, Bone1, Bone2, forcelimit )
				self:GetOwner():AddCleanup( "constraints", constraint )
				table.insert(constraintTable, constraint)
			end
		end
	end

	undo.Create("Weld")
	for _,constraint in pairs(constraintTable) do
		undo.AddEntity( constraint )
	end
	undo.SetPlayer( self:GetOwner() )
	undo.Finish()

	// Clear the objects so we're ready to go again
	self:ClearObjects()
	
	return true
	
end
