
datablock ItemData(EMPGrenadeThrown)
{
   className = Weapon;
   shapeFile = "mortar_projectile.dts";

   mass = 0.6;
   elasticity = 0.6;
   friction = 0.35;
   pickupRadius = 1;

   maxDamage = 0.5;
   hasDamageRadius = false;
   indirectDamage = 0.0;
   damageRadius = 12.0;

   EMPDuration = 10.0;
   EMPStrength = 0.2;

   explosion = EMPBlastExplosion;
   splash = DiscSplash;

   radiusDamageType = $DamageType::Grenade;
   kickBackStrength = 0;

   lightType = "PulsingLight";
   lightColor = "0.5 1.0 1.0 0.75";
   lightTime = "1000";
   lightRadius = "0.75";
};

datablock ItemData(EMPGrenade)
{
   className = HandInventory;
   catagory = "Handheld";
   shapeFile = "mortar_projectile.dts";

   mass = 1;
   trueMass = 0.08;

   elasticity = 0.2;
   friction = 0.6;
   pickupRadius = 1;
   thrownItem = EMPGrenadeThrown;
   pickUpName = "some EMP grenades";
};

function EMPGrenadeThrown::onAdd(%this, %Grenade)
{
   schedule(2250, 0, "detonateEMPGrenade", %Grenade);
   %Grenade.InWater = %Grenade.sourceObject.InWater;
}

function detonateEMPGrenade(%Grenade)
{
   %Grenade.setDamageState(Destroyed);

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

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

   // If the grenade is in the water, make it more potent
   if (%Grenade.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.8, %EMPStrength * 1.2, %Owner);
   schedule(2000, 0, EMPingCheck, %Position, %Radius, %EMPDuration * 0.6, %EMPStrength * 1.4, %Owner);

   AreaEMP(%Position);
   
   %Grenade.schedule(450, "delete");

   if ($Host::MaxBotDifficulty > 0.9)
      MakeAvoidObject(%Position, 2600);
}