
// This is a special for Havoc that provides backup shields to the player.
// Based on mass energy is taken from the player and shielding is formed
// around the player.  This is not a shield to be raised when combat starts,
// It should be activated long before then.  

function Player::LightShield(%this)
{
   %MassModifier = (%this.getDataBlock().Mass / 90) * 20 + 20;
   %Energy = %this.getEnergyLevel();

   // If the Light Shield is on, then turn it off and credit 65% of the energy remaining back
   if (%this.ShieldRemaining > 0)
   {
      // If the Shielding is 
      if (%this.ShieldUp > 0)
      {
         %ReturnModifier = 0.88;
         Cancel(%this.ShieldUp);
         messageClient(%this.client, 'MsgCantSitHere', 'Formation of Light Shield Canceled. ~wfx/powered/turret_outdoor_fire.wav');
      } else {
         %ReturnModifier = 0.65;
         messageClient(%this.client, 'MsgCantSitHere', 'Light Shield Deactivated. ~wfx/powered/turret_outdoor_fire.wav');
      }

      %EnergyRemaining = (%this.ShieldRemaining / 0.25) * %MassModifier;
      %EnergyRemaining *= %ReturnModifier;
      %this.setEnergyLevel(%Energy + %EnergyRemaining);

      %this.ShieldRemaining = 0;
      return;
   }

   // You must be still to form it.
   %Velocity = %this.getVelocity();
   %Speed = VectorLen(%Velocity);
   if (%Speed > 0.1)
   {
      messageClient(%this.client, 'MsgCantSitHere', 'You must be still to form the light shield!');
      return(0);
   }

   // If the Player does not have the required energy minimum, then tell them
   if (%Energy < %MassModifier)
   {
      messageClient(%this.client, 'MsgCantSitHere', 'Not enough power to form Light Shield! ~wfx/powered/turret_outdoor_fire.wav');
      %this.ShieldRemaining = 0;
      return(0);
   }

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

   messageClient(%this.client, 'MsgCantSitHere', 'Light Shield Initializing.');

   // Freeze the Players movement
   %this.setMoveState(true);

   // Scheduling the shield raising for 2.5 seconds
   %this.ShieldUp = %this.schedule(2500, LightShieldUp);

   // Start the Special effect of the field forming
   %this.LightShieldUpEffect();
}

function Player::LightShieldUp(%this)
{
   %this.ShieldUp = 0;
   %this.ShieldRemaining = 0.25;

   %this.setMoveState(false);

   messageClient(%this.client, 'MsgCantSitHere', 'Light Shield Stable.');
   %this.LightShieldHum(4);
}

// This function plays the sound of the light shield humming softly for several seconds.
function Player::LightShieldHum(%this, %Seconds)
{
   if (%Seconds <= 0)
   {
      Cancel(%this.LightShieldHumCheck);
   }
   %this.LightShieldHum = %Seconds;

   if (%this.LightShieldHumCheck <= 0)
      %this.LightShieldHumCheck = %this.schedule(1000, LightShieldHumCheck);
}

function Player::LightShieldHumCheck(%this)
{
   // Mark that that second was played
   %this.LightShieldHum--;

   // (Play Light Shield Sound here)
   
   // If you still have some to play, the play it.
   if (%this.LightShieldHum > 0)
      %this.LightShieldHumCheck = %this.schedule(1000, LightShieldHumCheck);
}

// This loops through and makes fuzzy blue around the player and when done it makes
// the shield visible
function Player::LightShieldUpEffect(%this)
{
   // The final effect is the shield being visible
   if (%this.ShieldUp <= 0)
   {
      %this.playShieldEffect("0.0 0.0 1.0");
      return;
   }

   %this.LightShieldRaisingEffect();

   %this.schedule(750, LightShieldUpEffect);
}

// The special effect is a fuzzy blue/white disturbance around the player
// and then the shield activates
datablock ParticleData(LightShieldUpParticle)
{
   dragCoefficient      = 1.5;
   gravityCoefficient   = -0.1;
   inheritedVelFactor   = 1.0;
   constantAcceleration = 0.0;
   lifetimeMS           = 350;
   lifetimeVarianceMS   = 150;
   textureName          = "particleTest";
   colors[0]     = "0.04 0.3 0.39 0.65";
   colors[1]     = "0.12 0.68 0.82 0.25";
   colors[2]     = "0.7 1.0 1.0 0.0";
   sizes[0]      = 0.35;
   sizes[1]      = 0.20;
   sizes[2]      = 0.05;
   times[0]      = 0.0;
   times[1]      = 0.5;
   times[2]      = 1.0;
};

datablock ParticleEmitterData(LightShieldUpEmitter)
{
   ejectionPeriodMS = 10;
   periodVarianceMS = 1;

   ejectionVelocity = 1;
   velocityVariance = 0.25;

   ejectionOffset   = 0.5;

   thetaMin         = 0.0;
   thetaMax         = 40.0;

   particles = "LightShieldUpParticle";

   lifetimeMS       = 375;

};

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

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

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

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

   %Random = getRandom();
   %PositionX = getWord(%Position, 0) + (%Random - %Random / 2);
   %Random = getRandom();
   %PositionY = getWord(%Position, 1) + (%Random - %Random / 2);
   %PositionZ = getWord(%Position, 2) + 2;
   %Position = %PositionX @ " " @ %PositionY @ " " @ %PositionZ;

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

   %Item.schedule(650, delete);
}