datablock ParticleData(LaserBeamParticle)
{
   textureName          = "~/data/shapes/rifle/smokeParticle";

   dragCoeffiecient     = 0.0;
   gravityCoefficient   = -0.2;   // rises slowly
   inheritedVelFactor   = 0.00;

   lifetimeMS           = 250;  // lasts 0.7 second
   lifetimeVarianceMS   = 150;   // ...more or less

   useInvAlpha = false;
   spinRandomMin = -30.0;
   spinRandomMax = 30.0;

   colors[0]     = "1.00 0.00 0.00 1.0";
   colors[1]     = "1.00 0.00 0.00 1.0";
   colors[2]     = "0 0 0 0";

   sizes[0]      = 0.25;
   sizes[1]      = 0.5;
   sizes[2]      = 1.0;

   times[0]      = 0.0;
   times[1]      = 0.3;
   times[2]      = 1.0;
};

datablock ParticleEmitterData(LaserBeamEmitter)
{
   ejectionPeriodMS = 10;
   periodVarianceMS = 5;

   ejectionVelocity = 0.25;
   velocityVariance = 0.10;

   thetaMin         = 0.0;
   thetaMax         = 90.0;  

   particles = LaserBeamParticle;
};


//-----------------------------------------------------------------------------
// Projectile Explosion

datablock ParticleData(LaserBeamExplosionParticle)
{
   dragCoefficient      = 2;
   gravityCoefficient   = 0.2;
   inheritedVelFactor   = 0.2;
   constantAcceleration = 0.0;
   lifetimeMS           = 750;
   lifetimeVarianceMS   = 150;
   textureName          = "~/data/shapes/rifle/smokeParticle";
   colors[0]     = "1.00 0.00 0.00 1.0";
   colors[1]     = "0.26 0.00 0.00 0.0";
   sizes[0]      = 0.5;
   sizes[1]      = 1.0;
};

datablock ParticleEmitterData(LaserBeamExplosionEmitter)
{
   ejectionPeriodMS = 7;
   periodVarianceMS = 0;
   ejectionVelocity = 2;
   velocityVariance = 1.0;
   ejectionOffset   = 0.0;
   thetaMin         = 0;
   thetaMax         = 60;
   phiReferenceVel  = 0;
   phiVariance      = 360;
   overrideAdvances = false;
   particles = "LaserBeamExplosionParticle";
};

datablock ExplosionData(LaserBeamExplosion)
{
   //particleEmitter = laserBeamExplosionEmitter;
   //emitter = laserBeamExplosionEmitter;
   particleDensity = 40;
   particleRadius = 1;

   faceViewer     = true;
   //explosionShape = "~/data/shapes/explosions/red_yellow/biggest_explosion.dts";
   explosionScale = "1 1 1";

   shakeCamera = true;
   camShakeFreq = "10.0 11.0 10.0";
   camShakeAmp = "5.0 5.0 5.0";
   camShakeDuration = 0.5;
   camShakeRadius = 100.0;

   // Dynamic light
   lightStartRadius = 6;
   lightEndRadius = 3;
   lightStartColor = "0.5 0 0";
   lightEndColor = "0 0 0";
};


//-----------------------------------------------------------------------------
// Projectile Object

datablock BeamData(LaserBeamProjectile)
{
   //projectileShapeName = "~/data/shapes/crossbow/projectile.dts";
   directDamage        = 0;
   radiusDamage        = 0;
   damageRadius        = 0.5;
   explosion           = LaserBeamExplosion;
   //particleEmitter     = LaserBeamEmitter;

   muzzleVelocity      = 10;
   velInheritFactor    = 0.3;

   armingDelay         = 0;
   lifetime            = 50000;
   fadeDelay           = 50000;
   bounceElasticity    = 0;
   bounceFriction      = 0;
   isBallistic         = true;
   gravityMod = 1.0;

   hasLight    = false;
   lightRadius = 4.0;
   lightColor  = "0 0 0";

   beamStartRadius = 0.2;
   beamEndRadius = 0.5;
   materialList = "~/data/shapes/laserbeam/red.dml";
   interval = 0;
};

//datablock BeamData(LaserBeamProjectile)
//{
//   //projectileShapeName = "~/data/shapes/laserBeam/projectile.dts";
//   directDamage        = 100;
//   radiusDamage        = 0;
//   damageRadius        = 0;
//   explosion           = LaserBeamExplosion;
//   //particleEmitter     = LaserBeamEmitter;
//
//   lifetime            = 250;
//   fadeDelay           = 0;
//
//   hasLight    = true;
//   lightRadius = 4.0;
//   lightColor     = "0.56 0.36 0.26";
//   
//   // RFB ->
//   beamStyle = 1;
//   beamStartRadius = 0.2;
//   beamEndRadius = 0.0;
//   beamLength = 500;
//   materialList = "~/data/shapes/laserbeam/red.dml";
//   interval = 0;
//   // <- RFB
//};

function LaserBeamProjectile::onCollision(%this,%obj,%col,%fade,%pos,%normal)
{
   if (%obj.sourceObject == %col)
      return;

   //echo("LaserBeamProjectile::onCollision()1");
   // Apply damage to the object all shape base objects
   //if (!(%col.getType() & $TypeMasks::TerrainObjectType | $TypeMasks::InteriorObjectType))
   //{
      //echo("LaserBeamProjectile::onCollision()2");
      if (%col.getType() & $TypeMasks::ShapeBaseObjectType | $TypeMasks::PlayerObjectType | $TypeMasks::ItemObjectType)
      {
         //echo("LaserBeamProjectile::onCollision()3");
         %col.damage(%obj,%pos,%this.directDamage,"LaserBeam");
      }
   //}

   // Radius damage is a support scripts defined in radiusDamage.cs
   radiusDamage(%obj,%pos,%this.damageRadius,%this.radiusDamage,"LaserBeam",0);
}

function LaserBeamProjectile::onExplode()
{
}

function LaserBeamProjectile::damage(%this, %obj, %sourceObject, %position, %damage, %damageType)
{
   //echo("LaserBeamProjectile::damage()");
   %obj.applyDamage(%damage);
}

datablock AudioProfile(LaserBeamFireSound) 
{ 
   filename = "~/data/sound/shipphaser1.wav";
   description = AudioDefault3d; 
   preload = true; 
};

datablock ItemData(LaserBeam)
{
   // Mission editor category
   category = "Weapon";

   // Hook into Item Weapon class hierarchy. The weapon namespace
   // provides common weapon handling functions in addition to hooks
   // into the inventory system.
   className = "Weapon";

   // Basic Item properties
   shapeFile = "~/data/shapes/rifle/weapon.dts";
   mass = 1;
   elasticity = 0.2;
   friction = 0.6;
   emap = true;

	// Dynamic properties defined by the scripts
	pickUpName = "a LaserBeam";
	image = LaserBeamImage;
    directUse = 1;
};

function LaserBeamImage::onFire(%data,%obj,%slot)
{
   Parent::onFire(%data,%obj,%slot);
   %obj.nextWeaponFire = 1;
   schedule(%data.fireTimeout, 0, "fireNextGun", %obj);   
}

function LaserBeam2Image::onFire(%data,%obj,%slot)
{
   Parent::onFire(%data,%obj,%slot);
   %obj.nextWeaponFire = 0;
   schedule(%data.fireTimeout, 0, "fireNextGun", %obj);
}

function LaserBeamImage::onTriggerDown(%this, %obj, %slot)
{
}

function LaserBeamImage::onTriggerUp(%this, %obj, %slot)
{
}

function fireNextGun(%obj)
{
   if (!isObject(%obj))
      return;

   if(%obj.fireWeapon)
   {
      if(%obj.nextWeaponFire == 0)
      {
         %obj.setImageTrigger(0, true);
         %obj.setImageTrigger(1, false);
      }
      else
      {
         %obj.setImageTrigger(0, false);
         %obj.setImageTrigger(1, true);
      }
   }
   else
   {
      %obj.setImageTrigger(0, false);
      %obj.setImageTrigger(1, false);
   }
}

function ShapeBaseImageData::onFire(%data, %obj, %slot)
{
	%projectile = %data.projectile;
	
	%muzzleVector = %obj.getMuzzleVector(%slot);
   %objectVelocity = %obj.getVelocity();
   
   %muzzleVelocity = VectorAdd(
      VectorScale(%muzzleVector, %projectile.muzzleVelocity),
      VectorScale(%objectVelocity, %projectile.velInheritFactor));
   
   %vehicle = 0;
   
       %p = new (%data.projectileType)() {
         dataBlock        = %data.projectile;
         initialVelocity  = %muzzleVelocity;
         initialDirection = %obj.getMuzzleVector(%slot);
         initialPosition  = %obj.getMuzzlePoint(%slot);
         sourceObject     = %obj;
         sourceSlot       = %slot;
         vehicleObject    = %vehicle;
      };
 
   if (isObject(%obj.lastProjectile) && %obj.deleteLastProjectile)
      %obj.lastProjectile.delete();

   %obj.lastProjectile = %p;
   %obj.deleteLastProjectile = %data.deleteLastProjectile;
   
   MissionCleanup.add(%p);
 
   return %p;
}

datablock ShapeBaseImageData(LaserBeamImage)
{
   // Basic Item properties
   shapeFile = "~/data/shapes/rifle/weapon.dts";
   emap = true;
   //ammo = LaserBeamAmmo;

   // Specify mount point & offset for 3rd person, and eye offset
   // for first person rendering.
   mountPoint = 0;
   offset = "0 0 0";
   correctMuzzleVector = false;
   className = "WeaponImage";

   projectile = LaserBeamProjectile;
   projectileType = Beam;

   usesEnergy = true; 
   useMountEnergy = false;
   minEnergy = 0.01;
   fireEnergy = 0.1;
   fireTimeout = 50000;

   // Initial start up state
   stateName[0]                     = "Preactivate";
   stateTransitionOnLoaded[0]       = "Activate";
   stateTransitionOnNoAmmo[0]       = "Activate";

   // Activating the gun.  Called when the weapon is first
   // mounted and there is ammo.
   stateName[1]                     = "Activate";
   stateTransitionOnTimeout[1]      = "Ready";
   stateTimeoutValue[1]             = 1;
   stateSequence[1]                 = "Activate";

   // Ready to fire, just waiting for the trigger
   stateName[2]                     = "Ready";
   stateTransitionOnNoAmmo[2]       = "Activate";
   stateTransitionOnTriggerDown[2]  = "Fire";

   // Fire the weapon. Calls the fire script which does 
   // the actual work.
   stateName[3]                     = "Fire";
   stateTransitionOnTimeout[3]      = "Reload";
   stateTimeoutValue[3]             = 1;
   stateFire[3]                     = true;
   stateRecoil[3]                   = LightRecoil;
   stateAllowImageChange[3]         = false;
   stateSequence[3]                 = "Fire";
   stateScript[3]                   = "onFire";
   stateEnergyDrain[3]              = 0.1;
   stateSound[3] = "LaserBeamFireSound"; // **** Added in sound tutorial

   // Play the relead animation, and transition into
   stateName[4]                     = "Reload";
   stateTransitionOnNoAmmo[4]       = "Activate";
   stateTransitionOnTimeout[4]      = "Ready";
   stateTimeoutValue[4]             = 0.5;
   stateAllowImageChange[4]         = false;
   stateSequence[4]                 = "Reload";
   stateEjectShell[4]               = true;

   // No ammo in the weapon, just idle until something
   // shows up. Play the dry fire sound if the trigger is
   // pulled.
   stateName[5]                     = "Activate";
   stateTransitionOnAmmo[5]         = "Reload";
   stateSequence[5]                 = "Activate";
   stateTransitionOnTriggerDown[5]  = "DryFire";

   // No ammo dry fire
   stateName[6]                     = "DryFire";
   stateTimeoutValue[6]             = 1.0;
   stateTransitionOnTimeout[6]      = "Activate";
};

//datablock ShapeBaseImageData(LaserBeam2Image : LaserBeamImage)
//{
//   mountPoint = 1;
//};