//FNP.cs

AddDamageType("FNP",   '<bitmap:add-ons/Weapon_Package_FortWars/graphics/CI_FNP> %1',    '%2 <bitmap:add-ons/Weapon_Package_FortWars/graphics/CI_FNP> %1',0.2,1);
AddDamageType("FNPHeadshot",   '<bitmap:add-ons/Weapon_Package_FortWars/graphics/CI_FNP><bitmap:add-ons/Weapon_Package_FortWars/graphics/ci_headshot> %1',    '%2 <add-ons/Weapon_Package_FortWars/graphics/CI_FNP><bitmap:add-ons/Weapon_Package_FortWars/graphics/ci_headshot> %1',0.75,1);
datablock ProjectileData(FNPProjectile)
{
   projectileShapeName = "./bullet.dts";
   directDamage        = 25;
   directDamageType    = $DamageType::FNP;
   radiusDamageType    = $DamageType::FNP;

   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     = FNPtrailEmitter;

   muzzleVelocity      = 120;
   velInheritFactor    = 1;

   armingDelay         = 00;
   lifetime            = 4000;
   fadeDelay           = 3500;
   bounceElasticity    = 0.5;
   bounceFriction      = 0.20;
   isBallistic         = true;
   gravityMod = 0.1;
   sound = bulletBySound;

   hasLight    = true;
   lightRadius = 1.5;
   lightColor  = "1 0.8 0";
   
   uiName = "FNP Bullet";
};

//////////
// item //
//////////
datablock ItemData(FNPItem)
{
	category = "Weapon";  // Mission editor category
	className = "Weapon"; // For inventory system

	 // Basic Item Properties
	shapeFile = "./fnp.dts";
	rotate = false;
	mass = 1;
	density = 0.2;
	elasticity = 0.2;
	friction = 0.6;
	emap = true;

	//gui stuff
	uiName = "Pistol A";
	iconName = "add-ons/Weapon_Package_FortWars/graphics/icon_FNP";
	doColorShift = false;

	 // Dynamic properties defined by the scripts
	image = FNPImage;
	canDrop = true;
};

////////////////
//weapon image//
////////////////
datablock ShapeBaseImageData(FNPImage)
{
   // Basic Item properties
   shapeFile = "./fnp.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 = FNPProjectile;
   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;

	stateName[3]                    = "Fire";
	stateTransitionOnTimeout[3]     = "Flame";
	stateTimeoutValue[3]            = 0.01;
	stateFire[3]                    = true;
	stateAllowImageChange[3]        = false;
	stateScript[3]                  = "onFire";
	stateWaitForTimeout[3]			= true;
	stateEmitter[3]					= FNPFireFlameEmitter;
	stateEmitterTime[3]				= 0.02;
	stateEmitterNode[3]				= "muzzleNode";
	stateEjectShell[3]       = true;

	stateName[4] = "Flame";
	stateSequence[4]                = "Fire";
	stateEmitter[4]					= FNPFireGlowEmitter;
	stateEmitterTime[4]				= 0.05;
	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]	= "Ready";

};

function FNPImage::onFire(%this,%obj,%slot)
{
	%projectile = FNPProjectile;
	%spread = 0.0015;
	%shellcount = 1;	
	
	%obj.spawnExplosion(QuakeLittleRecoilProjectile,"1 1 1");
	%obj.playThread(2, plant);
	%num = getRandom(1, 3);
	serverPlay3D( FNPFireLocal @ %num @ Sound, %obj.getHackPosition() SPC "0 0 1 0");
	serverplay2d( FNPFireGlobal @ %num @ Sound );
	
	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 FNPProjectile::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 = 25;
   %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::FNPHeadshot;
         
         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 FNPProjectile::onExplode(%this,%obj)
{
  %num = getRandom(1, 3);
  serverPlay3D( bulletImpact @ %num @ Sound,%obj.getTransform() );
  parent::onExplode(%this,%obj);
}