// Shotgun
//--------------------------------------
//datablock AudioProfile(ShotgunSwitchSound)
//{
//   filename    = "fx/weapons/generic_switch.wav";
//   description = AudioClosest3d;
//   preload = true;
//};

//datablock AudioProfile(ShotgunProjectileSound)
//{
//   filename    = "fx/weapons/sniper_miss.wav";
//   description = AudioClose3d;
//   preload = true;
//};

datablock TracerProjectileData(ShotgunBullet)
{
   doDynamicClientHits = true;

   directDamage          = 0.075;
   directDamageType      = $DamageType::Pellet;
   closeRangeMultiplier  = 1.5;
   shotgunHeadMultiplier = 1.5;

   explosion           = "ChaingunExplosion";
   splash              = ChaingunSplash;

   kickBackStrength  = 0.0;
   sound 				= SniperRifleProjectileSound;

   dryVelocity       = 425.0;
   wetVelocity       = 100.0;
   velInheritFactor  = 1.0;
   fizzleTimeMS      = 1000;
   lifetimeMS        = 1000;
   explodeOnDeath    = false;
   reflectOnWaterImpactAngle = 0.0;
   explodeOnWaterImpact      = false;
   deflectionOnWaterImpact   = 0.0;
   fizzleUnderwaterMS        = 1000;

   tracerLength    = 15.0;
   tracerAlpha     = false;
   tracerMinPixels = 6;
   tracerColor     = 211.0/255.0 @ " " @ 215.0/255.0 @ " " @ 120.0/255.0 @ " 0.75";
	tracerTex[0]  	 = "special/tracer00";
	tracerTex[1]  	 = "special/tracercross";
	tracerWidth     = 0.10;
   crossSize       = 0.20;
   crossViewAng    = 0.990;
   renderCross     = true;

   decalData[0] = ChaingunDecal1;
   decalData[1] = ChaingunDecal2;
   decalData[2] = ChaingunDecal3;
   decalData[3] = ChaingunDecal4;
   decalData[4] = ChaingunDecal5;
   decalData[5] = ChaingunDecal6;
};

//--------------------------------------------------------------------------
// Ammo
//--------------------------------------

datablock ItemData(ShotgunAmmo)
{
   className = Ammo;
   catagory = "Ammo";
   shapeFile = "ammo_chaingun.dts";
   mass = 1;
   elasticity = 0.2;
   friction = 0.6;
   pickupRadius = 2;
	pickUpName = "some shotgun shells";

   computeCRC = true;
   emap = true;
};

//--------------------------------------------------------------------------
// Weapon
//--------------------------------------
datablock ItemData(Shotgun)
{
   className = Weapon;
   catagory = "Spawn Items";
   //$$ TODO - real shape file
   shapeFile = "weapon_plasma.dts";
   image = ShotgunImage;
   mass = 1;
   elasticity = 0.2;
   friction = 0.6;
   pickupRadius = 2;
	pickUpName = "a shotgun";

   computeCRC = true;
   emap=true;
};

datablock ShapeBaseImageData(ShotgunImage)
{
   className = WeaponImage;
   shapeFile = "weapon_plasma.dts";
   item = Shotgun;
   ammo = ShotgunAmmo;
   offset = "0 0 0";
   emap = true;

   projectile = ShotgunBullet;
   projectileType = TracerProjectile;

   casing              = ShellDebris;
   shellExitDir        = "0.3 1.0 1.0";
   shellExitOffset     = "0.15 -0.56 -0.1";
   shellExitVariance   = 15.0;
   shellVelocity       = 3.0;

   projectileSpread = 20.0 / 1000.0;

   // State Data
   stateName[0] = "Activate";
   stateTransitionOnTimeout[0] = "ActivateReady";
   stateTimeoutValue[0] = 0.5;
   stateSequence[0] = "Activate";
   stateSound[0] = ChaingunSwitchSound;

   stateName[1] = "ActivateReady";
   stateTransitionOnLoaded[1] = "Ready";
   stateTransitionOnNoAmmo[1] = "NoAmmo";

   stateName[2] = "Ready";
   stateTransitionOnNoAmmo[2] = "NoAmmo";
   stateTransitionOnTriggerDown[2] = "Fire";

   stateName[3] = "Fire";
   stateTransitionOnTimeout[3] = "Reload";
   stateTimeoutValue[3] = 0.5;
   stateFire[3] = true;
   stateRecoil[3] = LightRecoil;
   stateAllowImageChange[3] = false;
   stateSequence[3] = "Fire";
   stateScript[3] = "onFire";
   stateSound[3] = GrenadeFireSound;

   stateName[4]                  = "Reload";
   stateTransitionOnNoAmmo[4]    = "NoAmmo";
   stateTransitionOnTimeout[4]   = "Ready";
   stateTimeoutValue[4]          = 0.5;
   stateAllowImageChange[4]      = false;
   stateSequence[4]              = "Reload";
   stateSound[4]                 = GenericSwitchSound;
   stateEjectShell[4]            = true;

   stateName[5] = "NoAmmo";
   stateTransitionOnAmmo[5] = "Reload";
   stateSequence[5] = "NoAmmo";
   stateTransitionOnTriggerDown[5] = "DryFire";

   stateName[6]       = "DryFire";
   stateSound[6]      = GrenadeDryFireSound;
   stateTimeoutValue[6]        = 1.5;
   stateTransitionOnTimeout[6] = "NoAmmo";
};

function ShotgunImage::onFire(%data, %obj, %slot)

{
	// cloaking
	if( %obj.station $= "" && %obj.isCloaked() )
    {
		if( %obj.respawnCloakThread !$= "" )
		{
			Cancel(%obj.respawnCloakThread);
			%obj.setCloaked( false );
			%obj.respawnCloakThread = "";
		}
		else
		{
			if( %obj.getEnergyLevel() > 20 )
			{
				%obj.setCloaked( false );
				%obj.reCloak = %obj.schedule( 500, "setCloaked", true );
			}
		}
	}

	// ammo
    %obj.applyKick(-400);
    %obj.decInventory(%data.ammo,1);

    %vector = %obj.getMuzzleVector(%slot);
    %mp = %obj.getMuzzlePoint(%slot);

	for (%i=0; %i < 24; %i++)
	{
      %x = (getRandom() - 0.5) * 2 * 3.1415926 * %data.projectileSpread;
      %y = (getRandom() - 0.5) * 2 * 3.1415926 * %data.projectileSpread;
      %z = (getRandom() - 0.5) * 2 * 3.1415926 * %data.projectileSpread;
      %mat = MatrixCreateFromEuler(%x @ " " @ %y @ " " @ %z);
      %vector = MatrixMulVector(%mat, %vector);

      %p = new (%data.projectileType)() {
            dataBlock        = %data.projectile;
            initialDirection = %vector;
            initialPosition  = %mp;
            sourceObject     = %obj;
			damageFactor	 = 1;
            sourceSlot       = %slot;
        };
	}
}

function ShotgunBullet::onCollision(%data, %projectile, %targetObject, %modifier, %position, %normal)
{
   	// extra damage for being close
	%dist = vectorDist(%position, %projectile.sourceObject.getPosition());

	if (%dist < 10)
	{
		%modifier *= %data.closeRangeMultiplier;
	}

	// extra damage for head shot
	if(!(%targetObject.getType() & ($TypeMasks::InteriorObjectType | $TypeMasks::TerrainObjectType)) &&
        (%targetObject.getDataBlock().getClassName() $= "PlayerData"))
	{
		%damLoc = firstWord(%targetObject.getDamageLocation(%position));
		if(%damLoc $= "head")
		{
			%targetObject.getOwnerClient().headShot = 1;
			%modifier *= %data.shotgunHeadMultiplier;
		}
		else
		{
			%modifier = 1;
			%targetObject.getOwnerClient().headShot = 0;
		}
	}

    %targetObject.damage(%projectile.sourceObject, %position, %modifier * %data.directDamage, %data.directDamageType);
}

