// This function duplicates the armor a player has so that individual changes can be made.
// Calls the approriate .cs.dso file in order to define a new armor if needed or redefine
// an existing clone.  It then assigns the player to the new armor.

function Player::TempArmor(%this)
{
   %BasedOn = %this.getDataBlock().getName();

   // If the armor they are wearing is a Temp Armor, then ignore the requested change
   if (getSubStr(%dataBlock, 0, 4) $= "Temp")
   {
      %Player.TempArmorUses++;
      return;
   }

   // For speed purposes we hardcode this much of it and have 30 temparmors for each prefix
   // Not a brilliant solution, but the most feasible.
   switch$ (%BasedOn)
   {
      case "LightMaleBiodermArmor":
         %Prefix = "LMB";
      case "MediumMaleBiodermArmor":
         %Prefix = "MMB";
      case "EngineerMaleBiodermArmor":
         %Prefix = "EMB";
      case "HeavyMaleBiodermArmor":
         %Prefix = "HMB";
      case "LightMaleHumanArmor":
         %Prefix = "LMH";
      case "MediumMaleHumanArmor":
         %Prefix = "MMH";
      case "EngineerMaleHumanArmor":
         %Prefix = "EMH";
      case "HeavyMaleHumanArmor":
         %Prefix = "HMH";
      case "LightFemaleHumanArmor":
         %Prefix = "LFH";
      case "MediumFemaleHumanArmor":
         %Prefix = "MFH";
      case "EngineerFemaleHumanArmor":
         %Prefix = "EFH";
      case "HeavyFemaleHumanArmor":
         %Prefix = "HFH";
      default :
         // Later we can make it quit if it is trying to be used on an armor that is
         // not havoc.
         return;
   }

   %TempNumber = 1;
   %NewArmorName = "Temp" @ %Prefix @ %TempNumber;

   // Get next temp armor that is not already taken.
   while (%NewArmorName.UsedBy > 0) {
       // If the armor is still assiged we check to see if the player is online.
       %PlayerActive = %NewArmorName.UsedBy.client;
       if (%PlayerActive == 0)
       {
          %NewArmorName.UsedBy = 0;
       } else {
          %TempNumber++;
          %NewArmorName = "Temp" @ %Prefix @ %TempNumber;
       }
   }

   if (%NewArmorName.BasedOn $= "")
      exec("havoc/temparmor/" @ %NewArmorName @ ".cs");

   // Assign the player the new armor.
   %NewArmorName.usedBy = %Player;
   %Player.TempArmorUses = 1;
   %this.setDataBlock(%NewArmorName);
   return(%NewArmorName);
}

// This function puts the original armor back on the player
function Player::RevertArmor(%this)
{
   %Player.TempArmorUses--;
   if (%Player.TempArmorUses < 1)
   {
      // Since only temp armors have a based on, we check to see if a revert
      // is needed or not.
      %Armor = %this.getDataBlock().getName();
      %OldArmor = %Armor.basedOn;

      if (%OldArmor !$= "")
      {
         %Armor.UsedBy = 0;
         %this.setDataBlock(%OldArmor);
         %Player.TempArmorUses = 0;
      }
   }
}