TOOL.Category		= "( kinkood )"
TOOL.Name			= "#ClimbRope"
TOOL.Command		= nil
TOOL.ConfigName		= ""

TOOL.ClientConVar[ "set" ]		=	"100000"
TOOL.ClientConVar[ "constant" ] =	"10000"
TOOL.ClientConVar[ "damping" ]	=	"0"
TOOL.ClientConVar[ "rdamping" ] =	"0"
TOOL.ClientConVar[ "material" ] =	"cable/cable"
TOOL.ClientConVar[ "width" ]	=	"2"

if ( CLIENT ) then

	language.Add( "Tool_climbrope_name",			"Climbable Rope" )
	language.Add( "Tool_climbrope_desc",			"Creates a climbable Rope" )
	language.Add( "Tool_climbrope_set",				"Maximum length of the rope(Hanging only)" )
	language.Add( "Tool_climbrope_0",				"Primary: Set Two Points, Secondary:Create Hanging Rope" )
	language.Add( "Tool_climbrope_1",				"Set Second Point" )

	language.Add( "Undone_climbrope",				"Removed Climbable Rope" )
	language.Add( "Cleanup_climbrope",				"Climbable Rope" )
	language.Add( "Cleaned_climbrope",				"Cleaned up all Climbable Rope" )
	
	language.Add( "ClimbRopeTool_constant",			"Constant:" )
	language.Add( "ClimbRopeTool_constant_desc",	"The higher the number the less the elastic will stretch" )
	language.Add( "ClimbRopeTool_damping",			"Damping:" )
	language.Add( "ClimbRopeTool_damping_desc",		"How much energy the elastic loses" )
	language.Add( "ClimbRopeTool_rdamping",			"Relative Damping:" )
	language.Add( "ClimbRopeTool_rdamping_desc",	"Damping relative to the attached object's energy" )
	language.Add( "ClimbRopeTool_ropewidth",		"Rope Width:" )
	language.Add( "ClimbRopeTool_material",			"Material:" )

end

function TOOL:LeftClick( trace )

	if ( trace.Entity:IsValid() && trace.Entity:IsPlayer() ) then return 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 ( iNum > 0 ) then
	
		if ( CLIENT ) then
		
			self:ClearObjects()
			return true
			
		end
		
		// 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 WPos1, WPos2 = self:GetPos(1),self:GetPos(2)
		local LPos1, LPos2 = self:GetLocalPos(1),self:GetLocalPos(2)
		local length = ( WPos1 - WPos2 ):Length()
		
		local base = ents.Create( "prop_physics" )
			base:SetModel( "models/Weapons/w_bugbait.mdl" )
			base:SetNoDraw( true )
			base:DrawShadow( false )
			base:SetAngles( trace.HitNormal:Angle() )
		base:Spawn()
		local phys = base:GetPhysicsObject()
		if ( phys ) then
			phys:EnableCollisions( false )
		end
		local ent
		local tempPos1
		if(Ent1:IsWorld()) then
			tempPos1 = LPos1
		else
			tempPos1 = WPos1
		end
		if(Ent2:IsWorld()) then
			tempPos2 = LPos2
		else
			tempPos2 = WPos2
		end
		if(LPos2.z > LPos1.z) then
			ent = ents.Create( "climb_rope" )
				ent:SetPos( tempPos1 )
			ent:Spawn()
			base:SetPos( tempPos2 )
			constraint.Weld( ent, Ent1, Bone1, 0, 0 )
			
			ent:GetTable():SetConstant(self:GetClientNumber("constant"))
			ent:GetTable():SetDamping(self:GetClientNumber("damping"))
			ent:GetTable():SetRDamping(self:GetClientNumber("rdamping"))
			ent:GetTable():SetMaterial(self:GetClientInfo("material"))
			ent:GetTable():SetWidth(self:GetClientNumber("width"))
			
			ent:GetTable():SetOtherPoint( base, tempPos2 )
			constraint.Weld( base, Ent2, Bone2, 0, 0 )
		else
			ent = ents.Create( "climb_rope" )
				ent:SetPos( tempPos2 )
			ent:Spawn()
			base:SetPos( tempPos1 )
			constraint.Weld( ent, Ent2, Bone2, 0, 0 )
			
			ent:GetTable():SetConstant(self:GetClientNumber("constant"))
			ent:GetTable():SetDamping(self:GetClientNumber("damping"))
			ent:GetTable():SetRDamping(self:GetClientNumber("rdamping"))
			ent:GetTable():SetMaterial(self:GetClientInfo("material"))
			ent:GetTable():SetWidth(self:GetClientNumber("width"))
			
			ent:GetTable():SetOtherPoint( base, tempPos1 )
			constraint.Weld( base, Ent1, Bone1, 0, 0 )
		end
		
		
		// Clear the objects and set the last object as object 1
		self:ClearObjects()

		// Add The constraint to the players undo table

		undo.Create("ClimbRope")
		undo.AddEntity( ent )
		undo.AddEntity( base )
		undo.SetPlayer( self:GetOwner() )
		undo.Finish()
		
	else
	
		self:SetStage( iNum+1 )
		
	end

	return true
	
end

function TOOL:RightClick( trace )
	// The rest is only done on the server
	if (!SERVER) then return end
	//self:DoShootEffect( trace.HitPos, trace.HitNormal, trace.ntity, trace.PhysicsBone )
	
	local base = ents.Create( "prop_physics" )
		base:SetModel( "models/Weapons/w_crowbar.mdl" )
		base:SetCollisionGroup(1)
		base:SetPos( trace.HitPos + self:GetOwner():GetAimVector() * -32 )
		base:SetAngles( trace.HitNormal:Angle() * -1 )
	base:Spawn()
	base:GetPhysicsObject():EnableMotion(false)
	
	local vWeld = constraint.Weld( trace.Entity, base, 0, 0, 0 )
	
	local ent = ents.Create( "climb_rope" )
		ent:SetAngles( trace.HitNormal:Angle() )
	ent:Spawn()
	
	local min = ent:OBBMins()
	ent:SetPos( trace.HitPos - trace.HitNormal * min.z )
		
		ent:GetTable():SetConstant(self:GetClientNumber("constant"))
		ent:GetTable():SetDamping(self:GetClientNumber("damping"))
		ent:GetTable():SetRDamping(self:GetClientNumber("rdamping"))
		ent:GetTable():SetMaterial(self:GetClientInfo("material"))
		ent:GetTable():SetWidth(self:GetClientNumber("width"))
	
	ent:GetTable():SetOtherEnd( base, base:GetPos(), self:GetClientInfo("set") )
	
	
	undo.Create("ClimbRope")
	undo.AddEntity( ent )
	undo.AddEntity( base )
	undo.SetPlayer( self:GetOwner() )
	undo.Finish()
	
	return true
end

function TOOL.BuildCPanel( CPanel )

	// HEADER
	CPanel:AddControl( "Header", { Text = "#Tool_climbrope_name", Description	= "#Tool_climbrope_desc" }  )
	
	// PRESETS
	local params = { Label = "#Presets", MenuButton = 1, Folder = "climbrope", Options = {}, CVars = {} }
			
		params.Options.default = {
			climbrope_set				= 		100000,
			climbrope_constant			= 		10000,
			climbrope_damping			= 		0,
			climbrope_rdamping			= 		0,
			climbrope_material			= 		"cable/rope",
			climbrope_width				= 		2
		}
			
		table.insert( params.CVars, "climbrope_set" )
		table.insert( params.CVars, "climbrope_constant" )
		table.insert( params.CVars, "climbrope_damping" )
		table.insert( params.CVars, "climbrope_rdamping" )
		table.insert( params.CVars, "climbrope_material" )
		table.insert( params.CVars, "climbrope_width" )
		
	CPanel:AddControl( "ComboBox", params )
	
	
	// KEY
	CPanel:AddControl( "Slider", {
		Label		= 		"#Tool_climbrope_set",
		Type		= 		"Float",
		Min 		= 		100,
		Max 		= 		100000,
		Command 	= 		"climbrope_set" } )
	

	CPanel:AddControl( "Slider", {
		Label		=		"#ClimbRopeTool_constant",
		Description	=		"#ClimbRopeTool_constant_desc",
		Type		=		"Float",
		Min			=		0,
		Max			=		10000,
		Command		=		"climbrope_constant" } )
	
	CPanel:AddControl( "Slider", {
		Label		=		"#ClimbRopeTool_damping",
		Description	=		"#ClimbRopeTool_damping_desc",
		Type		=		"Float",
		Min			=		0,
		Max			=		50,
		Command		=		"climbrope_damping" } )
	
	CPanel:AddControl( "Slider", {
		Label		=		"#ClimbRopeTool_rdamping",
		Description	=		"#ClimbRopeTool_rdamping_desc",
		Type		=		"Float",
		Min			=		0,
		Max			=		1,
		Command		=		"climbrope_rdamping" } )
	
	CPanel:AddControl( "Slider", {
		Label		=		"#ClimbRopeTool_ropewidth",
		Type		=		"Float",
		Min			=		1,
		Max			=		20,
		Command		=		"climbrope_width" } )
	
	CPanel:AddControl( "MaterialGallery", {
		Label		=		"#ClimbRopeTool_material",
		Height		=		64,
		Width		=		32,
		Rows		=		1,
		Stretch		=		1,
		
		Options = {
			Rope = {
				Material				=	"cable/rope_icon",
				climbrope_material		=	"cable/rope"
			},
						
			Cable2 = {
				Material				=	"cable/cable_icon",
				climbrope_material		=	"cable/cable2"
			},
			
			XBeam = {
				Material				=	"cable/xbeam",
				climbrope_material		=	"cable/xbeam"
			},
			
			RedLaser = {
				Material				=	"cable/redlaser",
				climbrope_material		=	"cable/redlaser"
			},
			
			BlueElectric = {
				Material				=	"cable/blue_elec",
				climbrope_material		=	"cable/blue_elec"
			},
			
			PhysicsBeam = {
				Material				=	"cable/physbeam",
				climbrope_material		=		"cable/physbeam"
			},
			
			Hydra = {
				Material				=	"cable/hydra",
				climbrope_material		=	"cable/hydra"
			}
		}
	} )
end

