$HL2Weapons::MaxAmmo["StealthPistol"] = 24;
$HL2Weapons::AddAmmo["StealthPistol"] = 12;

datablock ProjectileData(StealthPistolProjectile : ServicePistolProjectile) {
	directDamage = 50;
	headshotMultiplier = 2;
	specialDamageType = "bullet";
};

datablock ShapeBaseImageData(StealthPistolImage : ServicePistolImage) {
	shapeFile = "base/data/shapes/empty.dts";

	item = StealthPistolItem;
	projectile = StealthPistolProjectile;

	armReady = false;
	ignore = true;

	melee = true;
};

datablock ItemData(StealthPistolItem : ServicePistolItem) {
	image = StealthPistolImage;
	uiName = "Stealth Pistol";

	maxMag = 1;
	ammoType = "StealthPistol";
};

function StealthPistolImage::onMount(%this, %obj, %slot) {
	ServicePistolImage::onMount(%this, %obj, %slot);
	%obj.playThread(1, "root");
}

function StealthPistolImage::onUnMount(%this, %obj, %slot) {
	ServicePistolImage::onUnMount(%this, %obj, %slot);
}

function StealthPistolImage::onAmmoCheck(%this, %obj, %slot) {
	ServicePistolImage::onAmmoCheck(%this, %obj, %slot);
}

function StealthPistolImage::onReloadStart(%this, %obj, %slot) {
	ServicePistolImage::onReloadStart(%this, %obj, %slot);
}

function StealthPistolImage::onReload(%this, %obj, %slot) {
	ServicePistolImage::onReload(%this, %obj, %slot);
}

function StealthPistolImage::onEmpty(%this, %obj, %slot) {
	ServicePistolImage::onEmpty(%this, %obj, %slot);
}

function StealthPistolImage::onFire( %this, %obj, %slot )
{
	if(%obj.getDamagePercent() > 1.0)
	{
	  return;
	}

	%obj.toolMag[%obj.currTool] -= 1;

	if(%obj.toolMag[%obj.currTool] < 1)
	{
	  %obj.toolMag[%obj.currTool] = 0;
	  %obj.setImageAmmo(0,0);
	}
	hl2DisplayAmmo(%this,%obj,%slot);

	%projectile = %this.projectile;
	%spread = 0.0003;
	%shellcount = 1;

	%fvec = %obj.getForwardVector();
	%fX = getWord(%fvec,0);
	%fY = getWord(%fvec,1);

	%evec = %obj.getEyeVector();
	%eX = getWord(%evec,0);
	%eY = getWord(%evec,1);
	%eZ = getWord(%evec,2);

	%eXY = mSqrt(%eX*%eX+%eY*%eY);

	%aimVec = %fX*%eXY SPC %fY*%eXY SPC %eZ;
	%obj.setVelocity(VectorAdd(%obj.getVelocity(),VectorScale(%aimVec,"-1")));
	%obj.spawnExplosion(advLittleRecoilProjectile,"0.75 0.75 0.75");
	%obj.playThread(1, "armReadyRight");
	for(%shell=0; %shell<%shellcount; %shell++)
	{
		%vector = %this.melee ? %obj.getEyeVector() : %obj.getMuzzleVector(%slot);
		%objectVelocity = %obj.getVelocity();
		%vector1 = VectorScale(%vector, %projectile.muzzleVelocity);
		%vector2 = VectorScale(%objectVelocity, %projectile.velInheritFactor);
		%velocity = VectorAdd(%vector1,%vector2);
		%x = (getRandom() - 0.5) * 10 * 3.1415926 * %spread;
		%y = (getRandom() - 0.5) * 10 * 3.1415926 * %spread;
		%z = (getRandom() - 0.5) * 10 * 3.1415926 * %spread;
		%mat = MatrixCreateFromEuler(%x @ " " @ %y @ " " @ %z);
		%velocity = MatrixMulVector(%mat, %velocity);

		%p = new (%this.projectileType)()
		{
			dataBlock = %projectile;
			initialVelocity = %velocity;
			initialPosition = %this.melee ? %obj.getEyePoint() : %obj.getMuzzlePoint(%slot);
			sourceObject = %obj;
			sourceSlot = %slot;
			client = %obj.client;
		};
		MissionCleanup.add(%p);
	}

   %obj.playThread(2, shiftRight);
   %obj.playThread(3, shiftLeft);
   %obj.schedule(100, playThread, 1, "root");
   serverPlay3d( servicePistolFireSound, %obj.getHackPosition());
}

function StealthPistolProjectile::damage(%this, %obj, %col, %fade, %pos, %normal) {
	%hasBeenUnconscious = %col.hasBeenUnconscious;
	%col.hasBeenUnconscious = true;

	ServicePistolProjectile::damage(%this, %obj, %col, %fade, %pos, %normal);
	%col.hasBeenUnconscious = %hasBeenUnconscious;
}