
//--------------------------------------------------------------------------
// Large Missile Turret
// 
// 
//--------------------------------------------------------------------------

//--------------------------------------------------------------------------
// Sounds
//--------------------------------------
datablock AudioProfile(MBLSwitchSound)
{
   filename    = "fx/powered/turret_missile_activate.wav";
   description = AudioClose3d;
   preload = true;
};

datablock AudioProfile(MBLFireSound)
{
   filename    = "fx/powered/turret_missile_fire.wav";
   description = AudioDefault3d;
   preload = true;
};

datablock ShapeBaseImageData(MissileBarrelPackImage)
{
   mass = 12;

   className    = TurretPack;

   shapeFile    = "pack_barrel_missile.dts";
   item       = MissileBarrelPack;
   mountPoint = 1;
   offset     = "0 0 0";

   turretBarrel = "MissileBarrelLarge";

   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(MissileBarrelPack)
{
   className    = Pack;
   catagory     = "Packs";

   shapeFile    = "pack_barrel_missile.dts";
   mass         = 1;

   elasticity   = 0.2;
   friction     = 0.6;
   pickupRadius = 2;
   rotate       = true;
   image        = "MissileBarrelPackImage";

   pickUpName = "a missile barrel pack";

   computeCRC = true;

};

//MissileBarrelPackImage.turretBarrel = "MissileBarrelLarge";

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

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

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

//--------------------------------------------------------------------------
// Projectile
//--------------------------------------
datablock SeekerProjectileData(TurretMissile)
{
   casingShapeName        = "weapon_missile_casement.dts";
   projectileShapeName    = "weapon_missile_projectile.dts";

   directDamage           = 0.15;
   directDamageType       = $DamageType::HighExplosion;
   hasDamageRadius        = true;
   indirectDamage         = 0.5;
   damageRadius           = 5.0;
   directDamageType       = $DamageType::HighExplosion;
   kickBackStrength       = 1500;

   flareDistance          = 180;
   flareAngle             = 15;
   minSeekHeat            = 0.5;

   explosion              = "MissileExplosion";
   velInheritFactor       = 0;

   splash                 = MissileSplash;
   baseEmitter            = MissileSmokeEmitter;
   delayEmitter           = MissileFireEmitter;
   puffEmitter            = MissilePuffEmitter;

   lifetimeMS             = 14000;
   muzzleVelocity         = 30.0;
   turningSpeed           = 90.0;
   maxVelocity            = 650.0;
   acceleration           = 65.0;
   
   proximityRadius        = 3;

   terrainAvoidanceSpeed  = 320;
   terrainScanAhead       = 65;
   terrainHeightFail      = 20;
   terrainAvoidanceRadius = 200;

   useFlechette = true;
   flechetteDelayMs = 550;
   casingDeb = FlechetteDebris;
};

//--------------------------------------------------------------------------
//-------------------------------------- Fusion Turret Image
//
datablock TurretImageData(MissileBarrelLarge)
{
   shapeFile = "turret_missile_large.dts";
   item = MissileBarrelLargePack;

   projectile = TurretMissile;
   projectileType = SeekerProjectile;

   usesEnergy = true;
   fireEnergy = 40.0;
   minEnergy = 60.0;

   isSeeker     = true;
   seekRadius   = 260;
   maxSeekAngle = 45;
   seekTime     = 0.6;
   minSeekHeat  = 0.5;

   emap = true;

   minTargetingDistance = 15;

   // Turret parameters
   activationMS      = 500;
   deactivateDelayMS = 1000;
   thinkTimeMS       = 200;
   degPerSecTheta    = 580;
   degPerSecPhi      = 1080;

   attackRadius      = 260;

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

   stateName[1]                  = "ActivateReady";
   stateSequence[1]              = "Activate";
   stateSound[1]                 = MBLSwitchSound;
   stateTimeoutValue[1]          = 2;
   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]          = 3.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]        = 2;
   stateTransitionOnLoaded[5]  = "ActivateReady";
   stateTransitionOnTimeout[5] = "Dead";

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

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

function MissileBarrelLarge::onFire(%Data, %Turret, %slot)
{
   %Energy = %Turret.getEnergyLevel();
   %Turret.setEnergyLevel(%Energy - %Data.fireEnergy);

   %MuzzleVector = %Turret.getMuzzleVector(%Slot);
   %MuzzlePoint = %Turret.getMuzzlePoint(%Slot);

   %Missile = new SeekerProjectile()
   {
      dataBlock        = %Data.projectile;
      initialDirection = %MuzzleVector;
      initialPosition  = %MuzzlePoint;
      sourceObject     = %Turret;
      sourceSlot       = %Slot;
   };
   MissionCleanup.add(%Missile);

   // Check to see what is controlling it and get the target
   if (%Turret.getControllingClient() > 0)
   {
      %Target = %Turret.getLockedTarget();
   } else {
      %Target = %Turret.getTargetObject();
   }
   
   if(%Target > 0)
   {
      // If it is locked onto an object
      %Missile.setObjectTarget(%target);
   } else {
      if(%Turret.isLocked() == true)
      {
         // If it is locked onto a location
         %Position = %Turret.getLockedPosition();
         %Missile.setPositionTarget(%Position);
      } else {
         %Missile.setNoTarget();

         // Chance of it not seeking
         %Random = getRandom();
         if (%Random > 0.06)
            %Projectile.SeekHeat = %Projectile.getDataBlock().SeekHeat(%Projectile, %Turret, %Data.minSeekHeat, %MuzzleVector, 50, 40);
      }
   }

   // Schedule the Second Shot now
   %Data.schedule(800, SecondFire, %Turret, %Slot, %Target);
}

// This is the second shot fired by the turret and it does not consume any 
// extra energy and uses the same lock as before.
function MissileBarrelLarge::SecondFire(%Data, %Turret, %Slot, %Target)
{
   %MuzzleVector = %Turret.getMuzzleVector(%Slot);
   %MuzzlePoint = %Turret.getMuzzlePoint(%Slot);

   %Missile = new SeekerProjectile()
   {
      dataBlock        = %Data.projectile;
      initialDirection = %MuzzleVector;
      initialPosition  = %MuzzlePoint;
      sourceObject     = %Turret;
      sourceSlot       = %Slot;
   };
   MissionCleanup.add(%Missile);

   if(%Target > 0)
   {
      // If it is locked onto an object
      %Missile.setObjectTarget(%target);
   } else {
      if(%Turret.isLocked() == true)
      {
         // If it is locked onto a location
         %Position = %Turret.getLockedPosition();
         %Missile.setPositionTarget(%Position);
      } else {
         %Missile.setNoTarget();

         // Chance of it not seeking
         %Random = getRandom();
         if (%Random > 0.06)
            %Projectile.SeekHeat = %Projectile.getDataBlock().SeekHeat(%Projectile, %Turret, %Data.minSeekHeat, %MuzzleVector, 50, 40);
      }
   }
}