
datablock SensorData(ECMRegularSensor)
{
   detects                      = true;
   detectFOVPercent             = 1.3;
   detectRadius                 = 35;

   detectionPings               = true;
   detectsActiveJammed          = true;
   detectsCloaked               = true;
   detectsFOVOnly               = true;
   detectsPassiveJammed         = true;
   detectsUsingLOS              = false;

   jams                         = true;
   jamRadius                    = 15;
   jamsOnlyGroup                = true;
   jamsUsingLOS                 = true;

   useObjectFOV                 = false;
};

datablock ItemData(ECMGrenade)
{
   className = HandInventory;
   catagory = "Handheld";

   shapeFile = "ammo_mortar.dts";
   mass = 1;
   trueMass = 0.08;

   elasticity = 0.2;
   friction = 0.6;
   pickupRadius = 1;

   thrownItem = ECMGrenadeThrown;
   pickUpName = "some ECM grenades";
};

datablock ItemData(ECMGrenadeThrown)
{
   className = Weapon;
   shapeFile = "ammo_mortar.dts";

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

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

   kickBackStrength = 0;

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

datablock StaticShapeData(DeployedECMGrenade) : StaticShapeDamageProfile
{
   className = Sensor;
   shapeFile = "ammo_mortar.dts";

   maxDamage = 0.35;
   destroyedLevel = 0.35;
   disabledLevel = 0.35;

   dynamicType           = $TypeMasks::SensorObjectType;

   cmdCategory           = "DSupport";
   cmdIcon               = CMDSensorIcon;
   cmdMiniIconName       = "commander/MiniIcons/com_deploymotionsensor";

   explosion             = DeployablesExplosion;

   targetNameTag         = 'Thrown ECM Grenade';
   targetTypeTag         = '';

   sensorData            = ECMRegularSensor;
   sensorRadius          = ECMRegularSensor.detectRadius;
   sensorColor           = "0 128 0";

   item                  = ECMGrenadeThrown;

   debrisShapeName = "debris_generic_small.dts";
   debris = DeployableDebris;
   heatSignature = 0;
};

function ECMGrenadeThrown::onAdd(%this, %Grenade)
{
   schedule(2000, 0, AttemptDetonate, %Grenade);

   %Grenade.InWater = %Grenade.sourceObject.InWater;
}

function ECMGrenadeThrown::onDestroyed(%this, %Grenade, %prevState)
{
   parent::onDestroyed(%this, %Grenade, %prevState);

   // Schedule the Deletion of the Emitter
   %Grenade.emitter.schedule(150, delete);

   // Schedule the Deletion of the Grenade
   %Grenade.schedule(150, delete);
}


function detonateECMGrenade(%Grenade)
{
   %Team = %Grenade.sourceObject.team;

   %Transform = %Grenade.getTransform();
   %Position = posFromTransform(%Transform);
   %Rotation = rotFromTransform(%Transform);

   // Make the actual Jammer
   %Jammer = new StaticShape()
   {
      dataBlock = DeployedECMGrenade;
      position = %Position;
      rotation = %Rotation;
      owner = %Grenade.sourceObject;
      team = %Team;
   };
   MissionCleanUp.add(%Jammer);

   %Jammer.target = createTarget(%Jammer, "", "", "", %Jammer.getDataBlock().targetNameTag, %Team, 0);
   setTargetSensorGroup(%Jammer.getTarget(), %Team);

   setTargetSensorData(%Jammer.getTarget(), ECMRegularSensor);

   %PositionXY = getWords(%Position, 0, 1);
   %PositionZ = getWord(%Position, 2) + 0.4;
   %Position = %PositionXY @ " " @ %PositionZ;

   %Emitter = new ParticleEmissionDummy()
   {
      position = %Position;
      scale = "1 1 1";
      dataBlock = "defaultEmissionDummy";
      emitter = "ECMSparkEmitter";
   };
   MissionCleanup.add(%Emitter);

   %Emitter.schedule(11800, delete);

   %Jammer.Emitter = %Emitter;
   %Emitter.Grenade = %Jammer;

   %Random = getRandom() * 4000;
   %Jammer.schedule(12000 + %Random, "delete");
   %Jammer.schedule(11400 + %Random, "setCloaked", true);

   %Grenade.schedule(350, delete);
}

function AttemptDetonate(%Grenade)
{
   // If the grenade is moving, check again in 0.5 seconds to see if it has stopped
   if (VectorLen(%Grenade.getVelocity()) > 0.2)
   {
      %Grenade.MoveCheck = schedule(500, 0, AttemptDetonate, %Grenade);
      return;
   }

   detonateECMGrenade(%Grenade);
}