
// --------------------------------------------------------------------------
// 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.
// --------------------------------------------------------------------------

// ----------------------------------------------
// Stun Mine

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

   AlternateObject = "StunMine";

   isMine = true;

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

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

   sticky = true;

   mass = 0;
   elasticity = 0;
   friction = 1;

   bubbleEmitTime = 1.0;
   explosion = FlashGrenadeExplosion;
   underwaterExplosion = "UnderwaterGrenadeExplosion";
   splash              = GrenadeSplash;

   baseEmitter         = GrenadeSmokeEmitter;
   bubbleEmitter       = GrenadeBubbleEmitter;

   packedItem = "StunMineSingle";

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

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

   mass = 0.75;
   elasticity = 0.2;
   friction = 0.6;

   bubbleEmitTime = 1.0;
   explosion = FlashGrenadeExplosion;
   underwaterExplosion = "UnderwaterGrenadeExplosion";
   splash              = GrenadeSplash;

   baseEmitter         = GrenadeSmokeEmitter;
   bubbleEmitter       = GrenadeBubbleEmitter;

   packedItem = "StunMineSingle";
   DeployedItem = "WallStunMine";

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

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

   mass = 1;
   trueMass = 0.06;

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

   DeployedItem = WallStunMine;

   isMine = true;

   thrownItem = StunMineDeployed;
   pickUpName = "some stun 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 StunMineDeployed::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 StunMineDeployed::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 StunMineDeployed::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 StunMineDeployed::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);

   // If the Grenade is in the water, just fizzle out
   if (%Mine.InWater == true)
   {
      UnderwaterIncendiary(%Position);
   } else {
      SmokeBomb(%Position, 5, 20);
   }

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

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

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