
// --------------------------------------------------------------------------
// Havoc Team Friendly Mines
// These mines will only go off if an enemy is near or enemy piloted vehicle
// The Anti-Personnel are tracked all as MineDeployed so that the total of 25
// is not exceeded, but the players determine the types.  Also if a mine can't
// deploy for some reason, it tells the user and does not explode just waits
// to be picked up.
// --------------------------------------------------------------------------

// ----------------------------------------------
// EMP Mine

// This is a single undeployed mine that can be used if picked up, the same as a regular one.
datablock ItemData(EMPMineSingle)
{     
   className = HandInventory;
   category = "Dropped";
   item = "EMPMine";
   shapeFile = "mine.dts";
   mass = 0.75;

   AlternateObject = "EMPMine";

   isMine = true;

   elasticity = 0.2;
   friction = 0.7;
   pickupRadius = 1;
 
   thrownItem = MineDeployed;
   pickUpName = "an EMP mine";
};

datablock StaticShapeData(WallEMPMine) : StaticShapeDamageProfile
{
   className = Weapon;
   category = "Mine";
   shapeFile = "mine.dts";
   item = "EMPMine";

   mass = 0.75;
   elasticity = 0.3;
   friction = 0.6;

   EMPDuration = 8.0;
   EMPStrength = 0.25;

   explosion = EMPBlastExplosion;
   splash = DiscSplash;

   packedItem = "EMPMineSingle";

   maxDamage = 0.35;
   hasDamageRadius = false;
   damageRadius = 9.0;
   aiAvoidThis = true;
   dynamicType = $TypeMasks::DamagableItemObjectType;
   spacing = 6.0;
   proximity = 2.5;
   armTime = 2000;
};

datablock ItemData(EMPMineDeployed)
{
   className = Weapon;
   category = "Mine";
   shapeFile = "mine.dts";
   item = "EMPMine";

   mass = 0.75;
   elasticity = 0.3;
   friction = 0.6;

   EMPDuration = 8.0;
   EMPStrength = 0.25;

   explosion = EMPBlastExplosion;
   splash = DiscSplash;

   DeployedItem = "WallEMPMine";
   packedItem = "EMPMineSingle";

   maxDamage = 0.35;
   hasDamageRadius = false;
   damageRadius = 9.0;
   aiAvoidThis = true;
   dynamicType = $TypeMasks::DamagableItemObjectType;
   spacing = 6.0;
   proximity = 2.5;
   armTime = 2000;
};

datablock ItemData(EMPMine)
{     
   className = HandInventory;
   catagory = "Handheld";
   shapeFile = "mine.dts";

   mass = 1;
   trueMass = 0.06;

   elasticity = 0.2;
   friction = 0.7;
   pickupRadius = 1;

   isMine = true;

   DeployedItem = "WallEMPMine";
   thrownItem = EMPMineDeployed;
   pickUpName = "some EMP mines";
};

// This is the initial mine throw it starts several things, including a check for
// to stabilize the mine.  If a mine was thrown over an edge or slides for a long
// time we disable the mine.  We give the mine 12 seconds to arm itself or we
// remove it
function EMPMineDeployed::onThrow(%this, %Mine)
{
   %Mine.armed = false;
   %Mine.damaged = 0;
   %Mine.detonated = false;
   %Mine.team = %Mine.sourceObject.team;
   %Mine.MineCheck = schedule(1500, 0, "MineCheck", %Mine);
   %Mine.ArmCheck = schedule(12000, 0, "MineArmingCheck", %Mine);
}

// This makes the mine purely proximity, not contact.
function EMPMineDeployed::onCollision(%data, %Mine, %col)
{

}

// This function applied damage to the mine, it can cause it to explode.
// if the damage is sufficient.  One idea is to make the person who shot
// the mine responsible for the effect if that is what caused it to explode.
function EMPMineDeployed::damageObject(%Data, %Mine, %SourceObject, %Position, %Amount, %DamageType)
{
   if (%Mine.detonated == true)
      return(0);

   %Mine.damaged += %Amount;

   if (%Mine.damaged >= %Data.maxDamage)
   {
      // If it is not armed, just make a pop
      if (%Mine.armed == true)
      {
         %Mine.setDamageState(Destroyed);
      } else {
         %Position = posFromTransform(%Mine.getTransform());

         %Item = new Item()
         {
            datablock = MineFizzle;
            position = %Position;
         };
         MissionCleanUp.add(%Item);

         %Mine.schedule(450, delete);
         schedule(500, 0, detonateGrenade, %Item);
      }
   }
}

// This function does the actual explosion, gives credit to the one shooting it
function EMPMineDeployed::onDestroyed(%data, %Mine, %lastState)
{
   if (%Mine.detonated == true)
      return;

   // Mark the mine as blown so that all associated functions halt
   %Mine.detonated = true;

   // Cancel any potential schedules
   Cancel(%Mine.MineCheck);
   Cancel(%Mine.ArmCheck);
   Cancel(%Mine.MineArm);
   Cancel(%Mine.MineCheckVicinity);

   // Allow the mine to be replaced
   $TeamDeployedCount[%Mine.Team, MineDeployed]--;

   %Transform = %Mine.getTransform();
   %Position = posFromTransform(%Transform);
   %PositionXY = getWords(%Position, 0, 1);
   %PositionZ = getWord(%Position, 2) + 0.4;
   %Position = %PositionXY @ " " @ %PositionZ;

   %Owner = %Mine.sourceObject;
   %Radius = %Mine.getDatablock().damageRadius;
   %EMPStrength = %Mine.getDatablock().EMPStrength;
   %EMPDuration = %Mine.getDatablock().EMPDuration;

   // If the grenade is in the water, make it more potent
   if (%Mine.InWater == true)
   {
      %EMPStrength *= 0.6;
      %EMPDuration *= 1.4;
   }

   // Do the EMP checks
   EMPingCheck(%Position, %Radius, %EMPDuration, %EMPStrength, %Owner);
   schedule(1000, 0, EMPingCheck, %Position, %Radius, %EMPDuration * 0.75, %EMPStrength * 1.2, %Owner);
   schedule(2000, 0, EMPingCheck, %Position, %Radius, %EMPDuration * 0.5, %EMPStrength * 1.4, %Owner);

   AreaEMP(%Position);

   %Mine.schedule(450, "delete");
}

function WallEMPMine::damageObject(%Data, %Mine, %SourceObject, %Position, %Amount, %DamageType)
{
   MineDeployed::damageObject(%Data, %Mine, %SourceObject, %Position, %Amount, %DamageType);
}

function WallEMPMine::onDestroyed(%data, %Mine, %lastState)
{
   MineDeployed::onDestroyed(%data, %Mine, %lastState);
}