
// This is a special for Havoc that slows the users velocity
// 50% each time and takes 1 energy for every 4kph that it slows
// the player.  There is a minimum of 5 energy needed to activate
// and it always consumes at least 3 energy.  If the amount of
// energy needed to slow the player is greater than the energy
// on hand, then the Dampener shuts down.

function Player::InertialDampen(%this)
{
   // If the Inertial Dampner is on, then turn it off
   if (%this.IDampener > 0)
   {
      Cancel(%this.IDampener);
      %this.IDampener = 0;

      messageClient(%this.client, 'MsgCantSitHere', 'Inertial Dampner Deactivated.');
      //** Play Sound here for turning off
      return(0);
   }

   %this.IDampener = 1;

   // If the Player does not have the required 5 energy minimum,
   // then ignore the player and play a sound
   if (%this.getEnergyLevel() < 10)
   {
      messageClient(%this.client, 'MsgCantSitHere', 'Not enough power to form Dampening Field! ~wfx/powered/turret_outdoor_fire.wav');
      %this.IDampener = 0;
      return(0);
   }

   messageClient(%this.client, 'MsgCantSitHere', 'Inertial Dampner Activated.');

   // Show the Player as being Dampened
   %this.IDampener = %this.schedule(500, InertialCheck);
}

function Player::InertialCheck(%this)
{
   %Velocity = %this.getVelocity();
   if (VectorLen(%Velocity) <= 0.1)
   {
      %this.IDampener = 0;
      messageClient(%this.client, 'MsgCantSitHere', 'Landed, Deactivating Inertial Dampner. ~wfx/powered/turret_outdoor_fire.wav');
      return(0);
   }

   if (%this.getObjectMount())
   {
      %this.IDampener = 0;
      messageClient(%this.client, 'MsgCantSitHere', 'Boarded Vehicle, Deactivating Inertial Dampner. ~wfx/powered/turret_outdoor_fire.wav');
      return(0);
   }

   %Energy = %this.getEnergyLevel();

   // Half the players current velocity
   %VelX = getWord(%Velocity, 0);
   %VelY = getWord(%Velocity, 1);
   %VelZ = getWord(%Velocity, 2);
   %NewVelocity = (%Velx * 0.5) @ " " @ (%VelY * 0.5) @ " " @ (%VelZ * 0.5);
  
   // Set take energy based on how much the player was slowed
   %NewVelX = getWord(%NewVelocity, 0);
   %NewVelY = getWord(%NewVelocity, 1);
   %NewVelZ = getWord(%NewVelocity, 2);
   %EnergyTaken = ((%VelX - %NewVelX) + (%VelY - %NewVelY) + (%VelZ - %NewVelZ)) * 3;

   // If the energy required is greater than the energy on hand, fail
   if (%EnergyTaken > %Energy || %Energy < 10)
   {
      %this.IDampener = 0;
      messageClient(%this.client, 'MsgCantSitHere', 'Not enough power to maintain Dampening Field! ~wfx/powered/turret_outdoor_fire.wav');
      return(0);
   }
   if (%EnergyTaken < 5.0)
      %EnergyTaken = 5.0;

   %this.setEnergyLevel(%Energy - %EnergyTaken);

   %this.DampenEffect();
   %this.schedule(500, DampenEffect);
   // If the player is almost at a stop, make thier appearance somewhat fuzzy.
   if (%EnergyTaken < 5.5)
   {
      %this.playShieldEffect("0.0 0.0 1.0");    
   }

   // Set the New Velocity
   %this.setVelocity(%NewVelocity);

   // Add heat so missiles can lock on
   %Heat = %this.getHeat();
   %this.setHeat(%Heat + 20);
   
   // Schedule the next check in 0.75 seconds.
   %this.IDampener = %this.schedule(500, InertialCheck);
}

// The special effect is a fuzzy blue/white stream around the player

datablock ParticleData(DampnerParticle)
{
   dragCoefficient      = 1.5;
   gravityCoefficient   = 0;
   inheritedVelFactor   = 1.0;
   constantAcceleration = 0.0;
   lifetimeMS           = 250;
   lifetimeVarianceMS   = 0;
   textureName          = "particleTest";
   colors[0]     = "0.4 0.85 0.85 .4";
   colors[1]     = "0.3 0.7 0.7 0.75";
   colors[2]     = "0.78 1.0 1.0 .05";
   sizes[0]      = 0.40;
   sizes[1]      = 0.15;
   sizes[2]      = 0.10;
   times[0]      = 0.0;
   times[1]      = 0.6;
   times[2]      = 1.0;
};

datablock ParticleEmitterData(DampnerEmitter)
{
   ejectionPeriodMS = 6;
   periodVarianceMS = 0;
   ejectionVelocity = 3;
   velocityVariance = 2.9;
   ejectionOffset   = 0.5;
   thetaMin         = 0;
   thetaMax         = 90;
   phiReferenceVel  = 1;
   phiVariance      = 360;
   overrideAdvances = false;
   particles = "DampnerParticle";
};

datablock GrenadeProjectileData(DampnerEffectProjectile)
{
   projectileShapeName = "turret_muzzlepoint.dts";
   directDamage        = 0.0;
   hasDamageRadius     = false;

   baseEmitter         = DampnerEmitter;
   bubbleEmitter       = GrenadeBubbleEmitter;
   emitterDelay        = -1;

   grenadeElasticity = 0.35;
   grenadeFriction   = 0.2;
   armingDelayMS     = 1000;
};

function Player::DampenEffect(%this)
{
   %Velocity = %this.getVelocity();
   %Transform = %this.getTransform();
   %Position = posFromTransform(%Transform);

   %PlayerVector = VectorNormalize(getWords(%this.getEyeVector(), 0, 1) @ " 0");

   %Random = getRandom() * 2;
   %PositionX = getWord(%Position, 0) + (%Random - %Random / 2);
   %Random = getRandom();
   %PositionY = getWord(%Position, 1) + (%Random - %Random / 2);
   %PositionZ = getWord(%Position, 2) + 1.2;
   %Position2 = %PositionX @ " " @ %PositionY @ " " @ %PositionZ;
   %Position = VectorAdd(%Position2, VectorScale(%PlayerVector, 2));

   %Item = new GrenadeProjectile()
   {
      dataBlock = DampnerEffectProjectile;
      initialDirection = %Velocity;
      initialPosition  = %Position;
   };
   MissionCleanup.add(%Item);

   %Item.schedule(650, delete);
}