// This special allows a player to move more quickly on the ground for several seconds and they
// have a small chance of taking some damage while doing it.  There is about a minute delay time
// between uses.

function Player::PlayAdrenSounds(%Player, %Duration, %Breath)
{
   %Player.playAudio(0, AdrenSound);

   %Random = getRandom();
   if (%Random < 0.1 && %Duration > 6)
      schedule(%Random * 4500 * %Duration, 0, PlayPain, %Player);

   schedule(%Duration * 1000 - 450, %Player, PlayAudio, %Breath);
}

function Player::Adrenaline(%Player)
{
   if (%Player.cantAdrenaline == true)
   {
      messageClient(%Player.client, '', "Your Stimulant Kit does not yet have a full charge.");
      return;
   }

   %PlayerData = %Player.getDataBlock();
   %Armor = %PlayerData.getName();

   // Check to make sure the player is in light armor
   %ArmorSize = getSubStr(%Armor, 0, 5);
   if (%ArmorSize !$= "Light")
      return;

   // Prevent the Player from using this again
   %Player.cantAdrenaline = true;

   // Determine which armor to use based on what they were using
   %ArmorType = getSubStr(%Armor, 5, 11);
   switch$ (%ArmorType)
   {
      case "MaleHumanAr" :
         %NewArmor = "AdrenMaleHumanArmor";

      case "FemaleHuman" :
         %NewArmor = "AdrenFemaleHumanArmor";         

      case "MaleBioderm" :
         %NewArmor = "AdrenMaleBiodermArmor";         
   }

   // Record the Old Armor type and change the armor now
   %Player.OldDataBlock = %Armor;
   %Player.setDataBlock(%NewArmor);

   // Start the sound effects for the adrenaline
   %Duration = %NewArmor.AdrenalineDuration + getRandom() * %NewArmor.AdrenalineVariance;

   %Breath = %PlayerData.waterBreathSound;
   %Player.PlayAdrenSounds(%Duration, %Breath);

   // There is a small chance that it will hurt somewhat using this.
   %Random = getRandom();
   if (%Random < 0.35)
   {
      %Player.applyDamage(%Random * 0.5);
      %Player.setDamageFlash(0.75);
      PlayPain(%Player);

      %Message = "The stimulant burns through your veins.";
   } else {
      // Message the Player that it worked fine
      %Random = getRandom();
      if (%Random < 0.5)
      {
         %Message = "Everything else seems to slow down.";
      } else {
         %Message = "You feel yourself speed up.";
      }
   }
   messageClient(%Player.client, '', %Message);

   // Schedule the Reseting of the armor and the flag
   %Player.schedule(%Duration * 1000, AdrenalineOff, %Armor);
   %Player.schedule(%Duration * 1000 + 48000, AdrenalineReset);
}

function Player::AdrenalineOff(%Player, %Armor)
{
   if (%Armor $= %Player.OldDataBlock)
      %Player.setDataBlock(%Armor);
}

function Player::AdrenalineReset(%Player)
{
   %Player.cantAdrenaline = "";
}

function Player::AdrenalineHeal(%Player)
{
   echo("Attempting to heal player");

   // Can only help once it is really bad, i.e. 30% life left
   %DamagePercent = %Player.getDamagePercent();
   if (%DamagePercent < 0.7)
      return;

   // If player is dead, dont do a thing
   if (%Player.getState() $= "Dead")
      return;

   // Player must have a minimum of 30 energy to use this, but more is always nice
   %Energy = %Player.getEnergyLevel();
   if (%Energy < 30)
   {
      %Player.AdrenalineHeal = %Player.schedule(1250, AdrenalineHeal);
      return;
   }

   // Take the energy for the fix up
   %Player.setEnergyLevel(0);

   // Calculate what they can fix with the energy they have.
   %HealAmount = 0.05 * (%Energy / 50);
   %Player.applyRepair(%HealAmount);

   messageClient(%Player.client, '', "Administering Emergency Meds");

   // Play some sounds here
   %Player.PlayAdrenSounds(4, %Player.getDataBlock().waterBreathSound);

   // Schedule the next one, in an attempt to get the player back into the yellow
   %Player.AdrenalineHeal = %Player.schedule(1250, AdrenalineHeal);
}