
datablock AudioProfile(AlarmAlert)
{
   filename    = "fx/powered/station_denied.wav";
   description = LouderAudioClose3d;
   preload = true;
};

datablock ShapeBaseImageData(BaseAlarmImage)
{
   mass = 4;
   emap = true;

   shapeFile = "ammo_mine.dts";

   item = BaseAlarmPack;
   deployed = BaseAlarm;

   mountPoint = 1;
   offset = "0 0 0";

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

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

   maxDepSlope = 360;
   deploySound = SensorDeploySound;
};

datablock ItemData(BaseAlarmPack)
{
   className = Pack;
   catagory = "Deployables";

   shapeFile = "ammo_mine.dts";
   mass = 4;

   elasticity = 0.2;
   friction = 0.6;
   pickupRadius = 2;

   rotate = true;

   image = BaseAlarmImage;
   pickUpName = "a base alarm";

   computeCRC = true;
   emap = true;
};

datablock StaticShapeData(BaseAlarm) : StaticShapeDamageProfile
{
   className = Sensor;
   shapeFile = "ammo_mine.dts";

   maxDamage = 0.6;
   destroyedLevel = 0.6;
   disabledLevel = 0.4;

   item = BaseAlarmPack;
   image = BaseAlarmImage;

   scale = "0.5 0.5 0.18";

   detectionRadius = 50;

   explosion = DeployablesExplosion;
   dynamicType = $TypeMasks::SensorObjectType;

   name = "Base Alarm";

   deployedObject = true;

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

   targetNameTag = 'Base Alarm';
   targetTypeTag = 'Sensor';

   isShielded = true;
   energyPerDamagePoint = 80;
   maxEnergy = 35;
   rechargeRate = 0.1;

   debrisShapeName = "debris_generic_small.dts";
   debris = DeployableDebris;

   heatSignature = 0;
};

function BaseAlarmImage::onActivate(%data, %Player, %slot)
{
   ShapeBaseImageData::onActivate(%data, %Player, %slot);
}

// This returns a rotation that is perpendicular to the surface
function BaseAlarmImage::getInitialRotation(%Item, %Player)
{
   %RotationalAxis = vectorNormalize(vectorCross(%Item.surfaceNrm, "0 0 1"));
   // If the item is on a wall, then turn it sideways
   if (getWord(%Item.surfaceNrm, 2) == 1 || getWord(%Item.surfaceNrm, 2) == -1)
      %RotationalAxis = vectorNormalize(vectorCross(%Item.surfaceNrm, "0 1 0"));

   %Rotation = %RotationalAxis @ " " @ mACos(vectorDot(%Item.surfaceNrm, "0 0 1"));

   %Item.owner = %Player.client;

   return(%Rotation);
}

// Schedule the arming once the object has been placed.
function BaseAlarm::onAdd(%data, %Alarm, %Position)
{
   %Alarm.getDataBlock().schedule(350, Arm, %Alarm);
}

function BaseAlarm::Arm(%data, %Alarm)
{
   %data = %Alarm.getDataBlock();

   %Position = posFromTransform(%Alarm.getTransform());
   %Radius = %data.detectionRadius;

   // Name the Alarm based on what it is watching
   %data.NameSelf(%Alarm, %Position, %Radius / 2);

   // Start the searching.
   %data.schedule(1500, ProximityCheck, %Alarm, %Position, %Radius);

   %Alarm.target = createTarget(%Alarm, "", "", "", %Data.targetNameTag, %Alarm.Team, 0);
   setTargetSensorGroup(%Alarm.getTarget(), %Alarm.Team);
}

function BaseAlarm::onDestroyed(%data, %Alarm, %prevState)
{
   // Allow it to be repurchased
   $TeamDeployedCount[%Alarm.team, BaseAlarmPack]--;

   if (%Alarm.beacon !$= "")
      %Object.beacon.delete();

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

function BaseAlarm::NameSelf(%data, %Alarm, %Position, %Radius)
{
   %Mask = $TypeMasks::StaticShapeObjectType | $TypeMasks::ItemObjectType | $TypeMasks::StationObjectType;

   InitContainerRadiusSearch(%Position, %Radius, %Mask);

   // Based on a priority list, we name the alarm
   while ((%Object = containerSearchNext()) !$= "0")
   {
      %Name = %Object.getDataBlock().getName();
      switch$ (%Name)
      {
         case "Flag" :
            %NewName = "Flag Alarm";
      
         case "GeneratorLarge" :
            if (%NewName !$= "Flag Alarm")
               %NewName = "Generator Alarm";

         case "SolarPanel" :
            if (%NewName !$= "Flag Alarm" && %NewName !$= "Generator Alarm")
               %NewName = "Solar Panel Alarm";

         case "StationVehicle" : 
            if (%NewName !$= "Flag Alarm" && %NewName !$= "Generator Alarm" && %NewName !$= "Solar Panel Alarm")
               %NewName = "Vehicle Pad Alarm";

         case "StationVehiclePad" : 
            if (%NewName !$= "Flag Alarm" && %NewName !$= "Generator Alarm" && %NewName !$= "Solar Panel Alarm")
               %NewName = "Vehicle Pad Alarm";

         default :
            if (%NewName $= "")
               %NewName = "Base Alarm";
      }
   }

   %Alarm.setName(%NewName);
}

function BaseAlarm::ProximityCheck(%data, %Alarm, %Position, %Radius)
{
   %AlarmTeam = %Alarm.team;

   if (%AlarmTeam == 0)
      return;

   %Found = false;

   InitContainerRadiusSearch(%Position, %Radius, $TypeMasks::PlayerObjectType);

   // Based on a priority list, we name the alarm
   while ((%Player = containerSearchNext()) !$= "0")
   {
      %PlayerTeam = %Player.team;

      // Are they friendly
      if (%AlarmTeam == %PlayerTeam)
         continue;

      // Are they cloaked, ignore them.
      if (%Player.isCloaked() == true)
         continue;

      // If the sensor jammer is on, ignore them.
      if (%Player.jammed == true)
         continue;

      // Are they hidden from LOS
      %Coverage = calcExplosionCoverage(%Position, %Player,
                                        ($TypeMasks::InteriorObjectType |
                                         $TypeMasks::TerrainObjectType |
                                         $TypeMasks::ForceFieldObjectType |
                                         $TypeMasks::VehicleObjectType));

      if (%Coverage <= 0.1)
         continue;

      // If not, then we have found any enemy
      %Found = true;
      break;
   }

   if (%Found == true)
   {
      %Name = %Alarm.getName();
      if (%Name $= "")
         %Name = %Alarm.getDataBlock().name;

      %Message = %Name @ " : Enemy Detected!";
      messageTeam(%AlarmTeam, 0, %Message);

      %Alarm.playAudio(2, AlarmAlert);
      %Alarm.schedule(1000, playAudio, 2, AlarmAlert);

      %Alarm.schedule(3500, playAudio, 2, AlarmAlert);
      %Alarm.schedule(4500, playAudio, 2, AlarmAlert);

      %data.schedule(7000, ProximityCheck, %Alarm, %Position, %Radius);

      if (%Alarm.Beacon !$= "")
         %Alarm.Beacon.delate();

      %CenterPosition = %Alarm.getWorldBoxCenter();
      %Alarm.Beacon  =  MakeTeamWaypoint(%CenterPosition, %AlarmTeam, 15000, true);
   } else {
      %data.schedule(1250, ProximityCheck, %Alarm, %Position, %Radius);
   }
}

function BaseAlarm::ItemSwap(%Data, %Object)
{
   %Transform = %Object.getTransform();
   %Position = posFromTransform(%Transform);
   %Rotation = rotFromTransform(%Transform);

   %Rotation4 = getWord(%Rotation, 3);

   // if it was hanging upside down.
   if (%Rotation4 > 1.57)
   {
      %PositionXY = getWords(%Position, 0, 1);
      %PositionZ = getWord(%Position, 2);
      %Position = %PositionXY @ " " @ %PositionZ - 1.0;
   }

   %Item = %Object.getDataBlock().item;

   %NewItem = new Item()
   {
      dataBlock = %Item;
      position = %Position;
   };
   MissionCleanup.add(%NewItem);

   $TeamDeployedCount[%Object.team, %Item]--;

   if (%Rotation4 > 1.57)
      %NewItem.applyImpulse(%Position, "0 0 -10");
   else
      %NewItem.applyImpulse(%Position, "0 0 10");

   if (%Object.beacon !$= "")
      %Object.beacon.delete();

   %Object.delete();

   %NewItem.schedulePop();
}

