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

// ----------------------------------------------
// Incendiary Mine

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

   AlternateObject = "IncendiaryMine";

   isMine = true;

   elasticity = 0.2;
   friction = 0.7;
   pickupRadius = 1;
 
   thrownItem = IncendiaryMineDeployed;
   pickUpName = "an incendiary mine";
};

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

   item = "IncendiaryMine";
   sticky = true;

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

   packedItem = "IncendiaryMineSingle";

   BurnStrength = 0.035;
   BurnDuration = 8;

   damageRadius = 6.5;
   radiusDamageType = $DamageType::Fire;

   explosion = IncendiaryGrenadeExplosion;

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


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

   item = "IncendiaryMine";

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

   DeployedItem = "WallIncendiaryMine";
   packedItem = "IncendiaryMineSingle";

   BurnStrength = 0.035;
   BurnDuration = 8;

   damageRadius = 6.5;
   radiusDamageType = $DamageType::Fire;

   explosion = IncendiaryGrenadeExplosion;

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

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

   mass = 1;
   trueMass = 0.06;

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

   isMine = true;

   DeployedItem = "WallIncendiaryMine";
   thrownItem = IncendiaryMineDeployed;
   pickUpName = "some incendiary 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 IncendiaryMineDeployed::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 IncendiaryMineDeployed::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 IncendiaryMineDeployed::damageObject(%Data, %Mine, %SourceObject, %Position, %Amount, %DamageType)
{
   if (%Mine.detonated == true)
      return;

   %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 IncendiaryMineDeployed::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.2;
   %Position = %PositionXY @ " " @ %PositionZ;

   // If the grenade is in the water, fizzle out
   if (%Grenade.InWater == true || CheckinWater(%Grenade) == true)
   {
      UnderwaterIncendiary(%Position);
      %Mine.schedule(50, "delete");
      return;
   }

   %Mine.setDamageState(Destroyed);

   %Owner = %Mine.sourceObject;
   %IndirectDamage = %Mine.getDatablock().indirectDamage;
   %DamageType = %Mine.getDatablock().radiusDamageType;
   %Radius = %Mine.getDatablock().damageRadius;
   %DamageStrength = %Mine.getDatablock().BurnStrength;
   %DamageDuration = %Mine.getDatablock().BurnDuration;

   %Number = (getRandom(4.0) + 1);

   // Do the OnFire checks
   IncendiaryCheck(%Position, %Radius, %DamageDuration, %DamageStrength, %Owner);
   schedule(750, 0, IncendiaryCheck, %Position, %Radius, %DamageDuration * 0.75, %DamageStrength * 0.6, %Owner);
   schedule(1500, 0, IncendiaryCheck, %Position, %Radius, %DamageDuration * 0.55, %DamageStrength * 0.5, %Owner);
   schedule(2250, 0, IncendiaryCheck, %Position, %Radius * 0.8, %DamageDuration * 0.3, %DamageStrength * 0.4, %Owner);
   schedule(3600, 0, FillAreaSmoke, %Position, %Number * 0.5, %Radius);

   FillAreaFire(%Position, %Radius * 0.6, %Number * 0.5);

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

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

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