//--------------------------------------------------------------------------
// Deployable MediumSensor 
// By: Mostlikely
//
//--------------------------------------------------------------------------

datablock ShapeBaseImageData(BaseMediumSensorPackImage)
{
   mass = 20;
   emap = true;
   shapeFile = "Stackable5m.dts";
   item = BaseMediumSensorPack;
   mountPoint = 1;
   offset = "0 -0.2 0";
   deployed = SensorMediumPulse;
   heatSignature = 0;

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

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

   //isLarge = true;
   maxDepSlope = 360; // 30
   deploySound = ItemPickupSound;

   minDeployDis = 0.5;
   maxDeployDis = 5.0;
};

datablock ItemData(BaseMediumSensorPack)
{
   className = Pack;
   catagory = "Deployables";
   shapeFile = "Stackable5m.dts";
   mass = 5.0;
   elasticity = 0.2;
   friction = 0.6;
   pickupRadius = 3;
   rotate = true;
   image = "BaseMediumSensorPackImage";
   pickUpName = "a Large Sensor"; //Cantspel: fixed from "a Medium Pulse Sensor"
   heatSignature = 1;
   emap = true;
};

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

function BaseMediumSensorPackImage::onDeploy(%item, %plyr, %slot)
{
%className = "StaticShape";
//Player
%playerrot = %item.getInitialRotation(%plyr);
%playerrotxy = -1 * GetWord(%playerrot,2) * GetWord(%playerrot,3);
%playervec = %plyr.getEyeVector();
%playerrotvec = GetWord(%playervec,0) SPC GetWord(%playervec,1) SPC "0";
%packset = %plyr.packset; 

//Surface
%surface = %item.surface;
%surfaceAngle = getTerrainAngle(%item.surfaceNrm);
%surfacenrm = VectorScale(%item.surfaceNrm,1);
%surfacePT = %item.surfacePT;

if (%surface.needsfit == 1)
   {
   %surfaceclass = %item.surface.getDataBlock().className; // TerrainBlock,InteriorInstance
   %surfacetrans = %surface.gettransform();
   %surfacepos = posFromTransform(%surfacetrans);
   %surfacerot = rotFromTransform(%surfacetrans);
   %surfaceup = %surface.up;
   %surfacefo = %surface.fo;
   }
if (%item.surface.getClassName() $= TerrainBlock)
   %surfacepos = %surfacePT;



if (GetWord(%surfacenrm,2) < 0.99)
        {
	%rot = slopfromnrm(%surfacenrm);
        }
else
       {
       %newfo = Vectorright(%surfacenrm,%playerrotvec);
       %rot = rotfromnrm(%surfacenrm,%newfo);
       //%rot = vectorAdd(%rot,"0 0 3.14");
       }


   %plyr.unmountImage(%slot);
   %plyr.decInventory(%item.item, 1);

   %pointseven = VectorAdd(%pointseven, "0 0 0.5");
   %Dplacement = %surfacept;

   %deplObj = new (%className)()  //SensorLargePulse Station
   {
      dataBlock = SensorMediumPulse;
      position = %surfacePt;
      rotation = setrot(%rot);
   };

   %deplObj.GTWID = %plyr.packid;
   $GTWGame.misSaveGame();

   %deplObj.team = %plyr.client.Team;
   %deplObj.owner = %plyr.client;

   %team = "Team" @ %plyr.team;
   %group = nameToId("MissionGroup/Teams/"@ %team @"/Base0");

   %group.add(%deplObj);	

   %deplObj.playThread($PowerThread,"Power");
 
   if(%deplObj.getTarget() != -1)
      setTargetSensorGroup(%deplObj.getTarget(), %plyr.client.team);

   %Dtransform = %Dplacement SPC setrot(%rot);
   %deplObj.setTransform(%Dtransform);
   
   %group.powerinit();

   //%DeplObj.setRechargeRate(%DeplObj.getDatablock().rechargeRate);

   
   // place the deployable in the MissionCleanup/Deployables group (AI reasons)
  
   //addToDeployGroup(%deplObj);

   //let the AI know as well...
  
   AIDeployObject(%plyr.client, %deplObj);

   //Associate all the bunker parts together for damage/repair purposes.
   
   // play the deploy sound
   serverPlay3D(%item.deploySound, %deplObj.getTransform());

   // increment the team count for this deployed object
   %deplObj.dSurface = %item.surface;
   $TeamDeployedCount[%plyr.team, %item.item]++;
   %deplObj.dSurfaceNrm = %surfaceNrm;
   %deplObj.deploy();


 echo (%deplObj);
   return %deplObj;
}

function BaseMediumSensorPack::onPickup(%this, %pack, %player, %amount)
{
%player.packid = %pack.GTWID;
//%deplObj.GTWID = %plyr.packid;
//GTWGame.misSaveGame();
}

function BaseMediumSensorPack::onThrow(%this,%pack,%player)
{
 %this.GTWID = %player.packid;
 serverPlay3D(ItemThrowSound, %player.getTransform());
 %pack.schedulePop();
}

function Sensor::onDestroyed(%data, %obj, %prevState)
{
   %obj.schedule(0, "delete");
   Parent::onDestroyed(%data, %obj, %prevState);
   echo("Destroyed a Medium Pulse Sensor - Team: "@ %obj.team);
   $TeamDeployedCount[%obj.team, BaseMediumSensorPack]--;
}


function BaseMediumSensorImage::testObjectTooClose(%item)
{
   %mask =    ($TypeMasks::VehicleObjectType     | $TypeMasks::MoveableObjectType   |
               $TypeMasks::StaticShapeObjectType | $TypeMasks::ForceFieldObjectType |
               $TypeMasks::ItemObjectType        | $TypeMasks::TurretObjectType     |
               $TypeMasks::InteriorObjectType);
   InitContainerRadiusSearch( %item.surfacePt, 20, %mask );
   %test = containerSearchNext();
   if(!%test)
   {
      InitContainerRadiusSearch( %item.surfacePt, 20, $TypeMasks::PlayerObjectType );
      %test = containerSearchNext();
      if (%test)
         %test = containerSearchNext();
   }
   return "";// %test;
}

function BaseMediumSensorPackImage::testNoInteriorFound(%item)
{
//constmod
   //return %item.surface.getClassName() $= TerrainBlock; //!$= InteriorInstance;
//constmod
}

function BaseMediumSensorPackImage::testNoTerrainFound(%item)
{
//return %item.surface.getClassName() !$= TerrainBlock;
}
