Bolt = 0
Phys = 0
Prop = 0
Rope = 0
Rope2 = 0
activatetime = 0

function onInit()
	_SWEPSetSound( MyIndex, "single_shot", "Weapon_Crossbow.Single" )
end

function onThink()
	if(Prop > 0 and _CurTime() > activatetime+1) then
		_EntSetPos(Owner, vecAdd(_EntGetPos(Prop), vector3(0, 0, 64)))
		_phys.EnableGravity(Owner, false)
		_EntSetVelocity(Owner, vector3(0,0,0))
	end
end

function onPrimaryAttack()
		if(Bolt > 0) then onSecondaryAttack() end
		Bolt = _EntCreate( "physics_prop" )
		Prop = _EntCreate( "physics_prop" )
		if (Bolt > 0) then
		local name = _PlayerInfo(Owner, "name")
		_EntSetName(Owner, name)
		_EntPrecacheModel( "models/props/cs_italy/orange.mdl" )
		_EntSetModel(Prop, "models/props/cs_italy/orange.mdl")
		_EntSetPos(Prop, _EntGetPos(Owner))
		_EntSpawn(Prop)
		_EntSetName(Prop, name.."prop")
		_EntSetCollisionGroup(Prop, 1)
		_PhysSetMass(Prop, "100")
		_EntPrecacheModel( "models/mixerman3d/other/arrow.mdl" )
		_EntSetModel(Bolt, "models/mixerman3d/other/arrow.mdl")
		
		_EntSetPos( Bolt, vecAdd(_PlayerGetShootPos( Owner ),vecMul(_PlayerGetShootAng( Owner ),vector3(60,60,60))))
		_EntSetAng( Bolt, _PlayerGetShootAng(Owner))
		_EntSetOwner( Bolt, Owner )
		_EntSetAng(Bolt, _PlayerGetShootAng(Owner))
		_EntSpawn(Bolt)
		_EntSetName(Bolt, name.."bolt")
		local vAim = _PlayerGetShootAng( Owner )
		if ( vAim == nil) then	return; end
			vAim.x = vAim.x * 480000
			vAim.y = vAim.y * 480000
			vAim.z = vAim.z * 480000
		_phys.ApplyForceCenter(Bolt, vAim)
		_EntitySetPhysicsAttacker(Bolt, Owner)
		_SWEPUseAmmo(MyIndex, 0, 1)
		_EntFire(Prop, "sethealth", "9999", 0)
		activatetime = _CurTime()
		
		Rope2 = _EntCreate( "keyframe_rope" )
		Rope = _EntCreate( "move_rope" )
	
		_EntSetKeyValue( Rope2, "MoveSpeed", "64" );
		_EntSetKeyValue( Rope2, "Slack", "25" );
		_EntSetKeyValue( Rope2, "Subdiv", "2" );
		_EntSetKeyValue( Rope2, "Width", "4" );
		_EntSetKeyValue( Rope2, "TextureScale", "1" );
		_EntSetKeyValue( Rope2, "RopeMaterial", "cable/rope.vmt" )
		_EntSetKeyValue( Rope2, "targetname", name.."ropekeyframe")
		_EntSetKeyValue( Rope2, "origin", Prop );
		_EntSpawn( Rope2 );
		_EntSetParent(Rope2, Prop)
		_EntActivate( Rope2 );
		
		_EntSetKeyValue( Rope , "MoveSpeed", "64" );
		_EntSetKeyValue( Rope , "Slack", "25" );
		_EntSetKeyValue( Rope , "Subdiv", "2" );
		_EntSetKeyValue( Rope , "Width", "4" );
		_EntSetKeyValue( Rope , "TextureScale", "1" );
		_EntSetKeyValue( Rope , "PositionInterpolator", "2" );
		_EntSetKeyValue( Rope , "NextKey", name.."ropekeyframe" );
		_EntSetKeyValue( Rope , "RopeMaterial", "cable/rope.vmt" );
		_EntSetKeyValue( Rope , "targetname", name.."rope" );
		_EntSetKeyValue( Rope , "origin", Bolt );
		_EntSpawn( Rope  );
		_EntSetParent(Rope, Bolt)
		_EntActivate( rope  );
	end
end

function onSecondaryAttack()
	if(Bolt > 0) then
		_EntRemove(Bolt)
		_EntRemove(Phys)
		_EntRemove(Prop)
		--_EntRemove(Rope)
		--_EntRemove(Rope2)
		Bolt = 0
		Prop = 0
		Rope = 0
		Rope2 = 0
		activatetime = 0
		_phys.EnableGravity(Owner, true)
	end
end

function onReload()
	return true
end

function getWeaponSwapHands()
	return false
end

function getWeaponFOV()
	return 75
end

function getWeaponSlot()
	return 5
end

function getWeaponSlotPos()
	return 3;	
end

function getFiresUnderwater()
	return true
end

function getReloadsSingly()
	return true
end

function getDamage()
	return 100
end

function getPrimaryShotDelay()
	return 1.25
end

function getSecondaryShotDelay()
	return 0.1
end

function getPrimaryIsAutomatic()
	return false
end

function getSecondaryIsAutomatic()
	return false
end

function getPrimaryAmmoType()
	return "XBowBolt"
end

function getSecondaryAmmoType()
	return "none";
end

function getMaxClipPrimary()
	return 1;
end

function getMaxClipSecondary()
	return -1;
end

function getDefClipPrimary()
	return 10;
end

function getDefClipSecondary()
	return -1;
end

function getPrimaryScriptOverride()
	return 1;
end

function getSecondaryScriptOverride()
	return 3;
end

function getBulletSpread()
	return vector3( 0.0, 0.0, 0.0 );
end

function getViewKick()
	return vector3( 0, 0.0, 0.0);
end

function getViewKickRandom()
	return vector3( 0.0, 0.0, 0.0 );
end

function getViewModel( )
	return "models/weapons/v_crossbow.mdl";
end

function getWorldModel( )
	return "models/weapons/w_crossbow.mdl";
end

function getClassName()
	return "weapon_graphook";
end

function getAnimPrefix()
	return "crossbow";
end

function getPrintName()
	return "Rope Grap Hook\nby JetBoom";
end
