
//--------------------------------------------------------------------------
// Large Depth Charge Turret Barrel
//--------------------------------------------------------------------------

datablock ShapeBaseImageData(DepthChargeBarrelPackImage)
{
   mass = 10;

   className    = TurretPack;

   shapeFile  = "pack_barrel_mortar.dts";
   item       = DepthChargeBarrelPack;
   mountPoint = 1;
   offset   = "0 0 0";

   turretBarrel = "DepthChargeBarrelLarge";

   stateName[0] = "Idle";
   stateTransitionOnTriggerDown[0] = "Activate";

   stateName[1] = "Activate";
   stateScript[1] = "onActivate";
   stateTransitionOnTriggerUp[1] = "Deactivate";

   stateName[2] = "Deactivate";
   stateScript[2] = "onDeactivate";
   stateTransitionOnTimeOut[2] = "Idle";

   isLarge = false;
};

datablock ItemData(DepthChargeBarrelPack)
{
   className    = Pack;
   catagory     = "Packs";
   shapeFile    = "pack_barrel_mortar.dts";
   mass         = 1;
   elasticity   = 0.2;
   friction     = 0.6;
   pickupRadius = 2;
   rotate       = true;
   image        = "DepthChargeBarrelPackImage";
   pickUpName = "a depth charge barrel pack";

   computeCRC = true;

};

function DepthChargeBarrelPackImage::onActivate(%data, %obj, %slot)
{
   checkTurretMount(%data, %obj, %slot);
}

function DepthChargeBarrelPackImage::onDeactivate(%data, %obj, %slot)
{
   %obj.setImageTrigger($BackpackSlot, false);
}

function DepthChargeBarrelPack::onPickup(%this, %obj, %shape, %amount)
{
   // created to prevent console errors
}

datablock ExplosionData(DepthChargeEffect)
{
   soundProfile = DepthChargeSound;

   shockwave = UnderwaterMortarShockwave;
   shockwaveOnTerrain = true;

   subExplosion[0] = UnderwaterMortarSubExplosion1;
   subExplosion[1] = UnderwaterMortarSubExplosion2;
   subExplosion[2] = UnderwaterMortarSubExplosion3;

   emitter[0] = MortarExplosionBubbleEmitter;
   emitter[1] = UnderwaterExplosionSparksEmitter;
   
   shakeCamera = true;
   camShakeFreq = "24 26 22";
   camShakeAmp = "45 55 45";
   camShakeDuration = 2.25;
   camShakeRadius = 35.0;
};

datablock ItemData(DepthChargeShot)
{
   shapeFile = "stackable1m.dts";
   scale     = "0.35 0.5 0.5";

   mass = 0.5;
   elasticity = 0.0;
   friction = 1;
   drag = 0.3;

   sticky = true;

   hasDamageRadius = true;
   explosion = DepthChargeEffect;

   indirectDamage      = 0.7;
   damageRadius        = 18.0;
   radiusDamageType    = $DamageType::Explosion;
   kickBackStrength    = 4000;

   depth = 8;

   maxDamage = 0.3;
   emap = true;

   heatSignature = 1.0;
};

datablock ItemData(PrematureDepthChargeShot)
{
   shapeFile = "stackable1m.dts";
   scale     = "0.35 0.5 0.5";

   mass = 0.5;
   elasticity = 0.0;
   friction = 1;
   sticky = true;

   hasDamageRadius     = true;
   indirectDamage      = 0.2;
   damageRadius        = 12.0;
   radiusDamageType    = $DamageType::Explosion;
   kickBackStrength    = 1200;

   explosion = "SatchelMainExplosion";

   maxDamage = 1000;
   emap = true;
};

//--------------------------------------------------------------------------
//-------------------------------------- Fusion Turret Image

datablock TurretImageData(DepthChargeBarrelLarge)
{
   shapeFile = "turret_mortar_large.dts";
   item = DepthChargeBarrelPack;

   AlternateHUD = MortarBarrelLarge;

   IgnoreVehicle = true;

   projectile = MortarTurretShot;
   projectileType = GrenadeProjectile;

   usesEnergy = true;
   fireEnergy = 30;
   minEnergy = 30;
   emap = true;

   minTargetingDistance = 25;

   // Turret parameters
   activationMS      = 1000;
   deactivateDelayMS = 1500;
   thinkTimeMS       = 200;
   degPerSecTheta    = 580;
   degPerSecPhi      = 960;
   attackRadius      = 160;

   // State transitions
   stateName[0]                  = "Activate";
   stateTransitionOnNotLoaded[0] = "Dead";
   stateTransitionOnLoaded[0]    = "ActivateReady";

   stateName[1]                  = "ActivateReady";
   stateSequence[1]              = "Activate";
   stateSound[1]                 = MBLSwitchSound;
   stateTimeoutValue[1]          = 1;
   stateTransitionOnTimeout[1]   = "Ready";
   stateTransitionOnNotLoaded[1] = "Deactivate";
   stateTransitionOnNoAmmo[1]    = "NoAmmo";

   stateName[2]                    = "Ready";
   stateTransitionOnNotLoaded[2]   = "Deactivate";
   stateTransitionOnTriggerDown[2] = "Fire";
   stateTransitionOnNoAmmo[2]      = "NoAmmo";

   stateName[3]                = "Fire";
   stateTransitionOnTimeout[3] = "Reload";
   stateTimeoutValue[3]        = 0.3;
   stateFire[3]                = true;
   stateRecoil[3]              = LightRecoil;
   stateAllowImageChange[3]    = false;
   stateSequence[3]            = "Fire";
   stateSound[3]               = MBLFireSound;
   stateScript[3]              = "onFire";

   stateName[4]                  = "Reload";
   stateTimeoutValue[4]          = 1.5;
   stateAllowImageChange[4]      = false;
   stateSequence[4]              = "Reload";
   stateTransitionOnTimeout[4]   = "Ready";
   stateTransitionOnNotLoaded[4] = "Deactivate";
   stateTransitionOnNoAmmo[4]    = "NoAmmo";

   stateName[5]                = "Deactivate";
   stateSequence[5]            = "Activate";
   stateDirection[5]           = false;
   stateTimeoutValue[5]        = 1;
   stateTransitionOnLoaded[5]  = "ActivateReady";
   stateTransitionOnTimeout[5] = "Dead";

   stateName[6]               = "Dead";
   stateTransitionOnLoaded[6] = "ActivateReady";

   stateName[7]             = "NoAmmo";
   stateTransitionOnAmmo[7] = "Reload";
   stateSequence[7]         = "NoAmmo";
};

function DepthChargeBarrelLarge::onFire(%Data, %Turret, %Slot)
{   
   %Vector = %Turret.getMuzzleVector(%Slot);
   %Position = %Turret.getMuzzlePoint(%Slot);

   %Charge = new Item() 
   {
      dataBlock        = DepthChargeShot;
      Position         = %Position;
      sourceObject     = %Turret;
      sourceSlot       = %Slot;
      scale            = "0.35 0.5 0.5";
   };
   MissionCleanup.add(%Charge);

   %TargetPosition = posFromTransform(%Turret.getTargetObject().getTransform());
   %Distance = VectorLen(VectorSub(%TargetPosition, %Position)) / 8;

   %Force = %Distance + 4;

   if (%Turret.inWater == true)
      %force *= 3;

   %Charge.ApplyImpulse(%Position, VectorScale(%Vector, %Force));

   %Turret.lastProjectile = %Charge;

   DCTrackTarget(%Charge, %Turret.getTargetObject(), 1000);
}

function DCTrackTarget(%DepthCharge, %Target, %LastDistance)
{
   // If the Depth Charge no longer exists, stop now
   if (isObject(%DepthCharge) == false)
      return;

   %CurrentPosition = posFromTransform(%DepthCharge.getTransform());
   %TargetPosition = posFromTransform(%Target.getTransform());

   %CurrentDistance = VectorLen(VectorSub(%CurrentPosition, %TargetPosition));

   if (%CurrentDistance < %LastDistance && %CurrentDistance > 12)
   {
      schedule(450, 0, DCTrackTarget, %DepthCharge, %Target, %CurrentDistance);
   } else {
      %DepthCharge.setDamageState(Destroyed);
   }
}

function DepthChargeShot::onCollision(%Data, %DepthCharge, %ColidingObject)
{
   %DepthCharge.setDamageState(Destroyed);
}

function DepthChargeShot::onStickyCollision(%Data, %Bomb)
{
   %Bomb.setDamageState(Destroyed);
}

function DepthChargeShot::onDestroyed(%Data, %DepthCharge, %prevState)
{
   // If it was exploded this way already, ignore the second time
   if (%DepthCharge.detonated == true)
      return;

   // If it was not called by being set destroyed, do that now
   if (%DepthCharge.getDamageState() !$= "Destroyed")
      %DepthCharge.setDamageState(Destroyed);

   %Position = posFromTransform(%DepthCharge.getTransform()); 

   if (%DepthCharge.InWater == true)
   {
      %Radius = %Data.damageRadius;
      %Damage = %Data.indirectDamage;
      %KickBack = %Data.kickBackStrength;
      %Owner = %DepthCharge.sourceObject;
      %DamageType = %Data.radiusDamageType;

      RadiusExplosion(%DepthCharge, %Position, %Radius, %Damage, %KickBack, %Owner, %DamageType);
   } else {
      %Rotation = rotFromTransform(%DepthCharge.getTransform());

      %Item = new Item()
      {
         dataBlock = PrematureDepthChargeShot;
         position = %Position;
         rotation = %Rotation;
         sourceObject = %DepthCharge.sourceObject;
      };
      MissionCleanUp.Add(%Item);

      schedule(650, 0, detonateGrenade, %Item);
   }
   if (%Bomb.DepthCheck !$= "")
      cancel(%DepthCharge.AltitudeCheck);   

   %DepthCharge.detonated = true;
   %DepthCharge.schedule(600,"delete");
}

// If the Bomb hits water then we note that and start the depth checking
function DepthChargeShot::onEnterLiquid(%data, %Bomb, %coverage, %type)
{
   %Bomb.InWater = true;

   // If the Bomb lands in lava, just delete it.
   switch(%type)
   {
      case 4:
         //Lava
         %Bomb.schedule(550, delete);
         return;

      case 5:
         //Hot Lava
         %Bomb.schedule(550, delete);
         return;

      case 6:    
         //Crusty Lava
         %Bomb.schedule(550, delete);
         return;
   }

   // Slow the Depth Charge by 30%
   %Velocity = %Bomb.getVelocity();
   %Bomb.setVelocity(VectorScale(%Velocity, 0.7));

   // Note the Water Line and figure out how far down we should detonate
   %Position = posFromTransform(%Bomb.getTransform());
   %Random = (getRandom() * 5) + 20;
   %Depth = getWord(%Position, 2) - %Random;

   // Start the Depth Checking
   %Bomb.DepthCheck = schedule(250, 0, CheckBombDepth, %Bomb, %Depth);

   %Bomb.setHeat(0);
}