//FNPSD.cs

AddDamageType("FNPSD",   '<bitmap:add-ons/Weapon_Package_FortWars/graphics/CI_FNPSD> %1',    '%2 <bitmap:add-ons/Weapon_Package_FortWars/graphics/CI_FNPSD> %1',0.2,1);
AddDamageType("FNPSDHeadshot",   '<bitmap:add-ons/Weapon_Package_FortWars/graphics/CI_FNPSD><bitmap:add-ons/Weapon_Package_FortWars/graphics/ci_headshot> %1',    '%2 <add-ons/Weapon_Package_FortWars/graphics/CI_FNPSD><bitmap:add-ons/Weapon_Package_FortWars/graphics/ci_headshot> %1',0.75,1);
datablock ProjectileData(FNPSDProjectile)
{
   projectileShapeName = "./bullet.dts";
   directDamage        = 24;
   directDamageType    = $DamageType::FNPSD;
   radiusDamageType    = $DamageType::FNPSD;

   brickExplosionRadius = 0;
   brickExplosionImpact = true;          //destroy a brick if we hit it directly?
   brickExplosionForce  = 10;
   brickExplosionMaxVolume = 1;          //max volume of bricks that we can destroy
   brickExplosionMaxVolumeFloating = 2;  //max volume of bricks that we can destroy if they aren't connected to the ground

   impactImpulse	     = 400;
   verticalImpulse	  = 400;
   explosion           = FNPExplosion;
   particleEmitter     = FNPSDTrailEmitter;

   muzzleVelocity      = 110;
   velInheritFactor    = 1;

   armingDelay         = 00;
   lifetime            = 4000;
   fadeDelay           = 3500;
   bounceElasticity    = 0.5;
   bounceFriction      = 0.20;
   isBallistic         = true;
   gravityMod = 0.15;
   sound = bulletBySound;

   hasLight    = false;
   lightRadius = 1.0;
   lightColor  = "1 0.8 0";
   
   uiName = "FNPSD Bullet";
};

//////////
// item //
//////////
datablock ItemData(FNPSDItem)
{
	category = "Weapon";  // Mission editor category
	className = "Weapon"; // For inventory system

	 // Basic Item Properties
	shapeFile = "./FNPSDItem.dts";
	rotate = false;
	mass = 1;
	density = 0.2;
	elasticity = 0.2;
	friction = 0.6;
	emap = true;

	//gui stuff
	uiName = "Pistol A SD";
	iconName = "add-ons/Weapon_Package_FortWars/graphics/icon_FNPSD";
	doColorShift = false;

	 // Dynamic properties defined by the scripts
	image = FNPSDImage;
	canDrop = true;
};

////////////////
//weapon image//
////////////////
datablock ShapeBaseImageData(FNPSDImage)
{
   // Basic Item properties
   shapeFile = "./FNPSD.dts";
   emap = true;

   // Specify mount point & offset for 3rd person, and eye offset
   // for first person rendering.
   mountPoint = 0;
   offset = "0 0 0";
   eyeOffset = 0; //"0.7 1.2 -0.5";
   rotation = eulerToMatrix( "0 0 0" );

   // When firing from a point offset from the eye, muzzle correction
   // will adjust the muzzle vector to point to the eye LOS point.
   // Since this weapon doesn't actually fire from the muzzle point,
   // we need to turn this off.  
   correctMuzzleVector = true;

   // Add the WeaponImage namespace as a parent, WeaponImage namespace
   // provides some hooks into the inventory system.
   className = "WeaponImage";

   // Projectile && Ammo.
   item = BowItem;
   ammo = " ";
   projectile = FNPSDProjectile;
   projectileType = Projectile;

	casing = FNPShellDebris;
	shellExitDir        = "1.0 1.3 -0.3";
	shellExitOffset     = "0 0 0";
	shellExitVariance   = 15.0;	
	shellVelocity       = 7.0;

   //melee particles shoot from eye node for consistancy
   melee = false;
   //raise your arm up or not
   armReady = true;

   doColorShift = false;

   //casing = " ";

   // Images have a state system which controls how the animations
   // are run, which sounds are played, script callbacks, etc. This
   // state system is downloaded to the client so that clients can
   // predict state changes and animate accordingly.  The following
   // system supports basic ready->fire->reload transitions as
   // well as a no-ammo->dryfire idle state.

   // Initial start up state
	stateName[0]                     = "ActivateFirst";
	stateTimeoutValue[0]             = 0.8;
	stateTransitionOnTimeout[0]       = "Ready";
	stateSequence[0]                = "firstDraw";
	stateSound[0]					= FNPFirstDrawSound;

	stateName[1]                     = "Activate";
	stateTimeoutValue[1]             = 0.25;
	stateTransitionOnTimeout[1]     = "Ready";
	stateSequence[1]                = "idle";
	stateSound[1]					= FNPDrawSound;

	stateName[2]                     = "Ready";
	stateTransitionOnTriggerDown[2]  = "Fire";
	stateSequence[2]                = "idle";
	stateAllowImageChange[2]         = true;
	stateSequence[2]	= "Ready";

	stateName[3]                    = "Fire";
	stateTransitionOnTimeout[3]     = "Flame";
	stateTimeoutValue[3]            = 0.01;
	stateFire[3]                    = true;
	stateAllowImageChange[3]        = false;
	stateScript[3]                  = "onFire";
	stateWaitForTimeout[3]			= true;
	stateEmitter[3]					= FNPSDFireBurstEmitter;
	stateEmitterTime[3]				= 0.02;
	stateEmitterNode[3]				= "muzzleNode";
	stateEjectShell[3]       = true;

	stateName[4] = "Flame";
	stateSequence[4]                = "Fire";
	stateEmitter[4]					= FNPSDFirePuffEmitter;
	stateEmitterTime[4]				= 0.01;
	stateEmitterNode[4]				= "muzzleNode";
	stateTimeoutValue[4]            = 0.14;
	stateTransitionOnTimeout[4]     = "Smoke";

	stateName[5] = "Smoke";
	stateEmitter[5]					= FNPFireSmokeEmitter;
	stateEmitterTime[5]				= 3.0;
	stateEmitterNode[5]				= "muzzleNode";
	stateTimeoutValue[5]            = 0.01;
	stateTransitionOnTimeout[5]     = "Reload";

	stateName[6]			= "Reload";
	stateSequence[6]                = "Reload";
	stateTransitionOnTriggerUp[6]     = "Ready";
	stateSequence[6]	= "Ready";

};

function FNPSDImage::onFire(%this,%obj,%slot)
{
	%projectile = FNPSDProjectile;
	%spread = 0.001;
	%shellcount = 1;
	
	%obj.spawnExplosion(QuakeLittleRecoilProjectile,"1 1 1");
	%obj.playThread(2, plant);
	%num = getRandom(1, 3);
	serverPlay3D( FNPSDFireLocal @ %num @ Sound, %obj.getHackPosition() SPC "0 0 1 0");
	
	for(%shell=0; %shell<%shellcount; %shell++)
	{
		%vector = %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 = %obj.getMuzzlePoint(%slot);
			sourceObject = %obj;
			sourceSlot = %slot;
			client = %obj.client;
		};
		MissionCleanup.add(%p);
	}
	return %p;
}

function FNPSDProjectile::damage(%this,%obj,%col,%fade,%pos,%normal)
{
   if(%this.directDamage <= 0)
      return;

   %damageType = $DamageType::Direct;
   if(%this.DirectDamageType)
      %damageType = %this.DirectDamageType;

   %scale = getWord(%obj.getScale(), 2);
   %directDamage = 24;
   %damage = %directDamage;

   %sobj = %obj.sourceObject;
   if(%sobj.getType() & $TypeMasks::PlayerObjectType)
   {
      if(isObject(%sobj.client))
		%obj.sourceObject.client.centerPrint("<bitmap:add-ons/weapon_package_fortwars/graphics/hitmarker.png>", 0.3);
		%num = getRandom(1, 15);
		%obj.sourceObject.client.play2D( impactSweetener @ %num @ Sound );
		%obj.sourceObject.client.play2D( hitmarkerSound );
   }
   
   if(%col.getType() & $TypeMasks::PlayerObjectType)
   {
      %colscale = getWord(%col.getScale(),2);
      if(getword(%pos, 2) > getword(%col.getWorldBoxCenter(), 2) - 3.3*%colscale)
      {
         %directDamage = %directDamage * 2;
         %damageType = $DamageType::FNPSDHeadshot;
         
         if(%sobj.getType() & $TypeMasks::PlayerObjectType)
         {
            if(isObject(%sobj.client))
				%obj.sourceObject.client.centerPrint("<bitmap:add-ons/weapon_package_fortwars/graphics/hitmarkercrit.png>", 0.3);
				%num = getRandom(1, 7);
				%obj.sourceObject.client.play2D( impactKillshot @ %num @ Sound );
				%obj.sourceObject.client.play2D( hitmarkerSound );
         }
      }
      
      %col.damage(%obj, %pos, %directDamage, %damageType);
   }
   else
   {
      %col.damage(%obj, %pos, %directDamage, %damageType);
   }
}

function FNPSDProjectile::onExplode(%this,%obj)
{
  %num = getRandom(1, 3);
  serverPlay3D( bulletImpact @ %num @ Sound,%obj.getTransform() );
  parent::onExplode(%this,%obj);
}