//--------------------------------------------------------------------------
// Deployable Large Inventory station
// By: Mostlikely
//
//--------------------------------------------------------------------------

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

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

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

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

   minDeployDis = 0.5;
   maxDeployDis = 5.0;
};

datablock ItemData(BaseInventoryPack)
{
   className = Pack;
   catagory = "Deployables";
   shapeFile = "Stackable5m.dts";
   mass = 5.0;
   elasticity = 0.2;
   friction = 0.6;
   pickupRadius = 1;
   rotate = true;
   image = "BaseInventoryPackImage";
   pickUpName = "a Large Inventory station";
   heatSignature = 0;
   emap = true;
};

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

function BaseInventoryPackImage::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 = %item.surfaceNrm;
%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)()  //Inventory Station
   {
      dataBlock = StationInventory;
      position = %surfacePt;
      rotation = setrot(%rot);
   };

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

   %Dtransform = %Dplacement SPC setrot(%rot);
   %deplObj.setTransform(%Dtransform);
   //%deplObj.setSelfPowered();

  


   %deplObj.team = %plyr.client.Team;
   %deplObj.owner = %plyr.client;
 
   if(%deplObj.getTarget() != -1)
      setTargetSensorGroup(%deplObj.getTarget(), %plyr.client.team);

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

   %group.add(%deplObj);

   %deplObj.playThread($PowerThread,"Power");

   // 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
   %group.powerinit();

   $TeamDeployedCount[%plyr.team, %item.item]++;
   %deplObj.dSurfaceNrm = %surfaceNrm;

   %deplObj.deploy();
   %deplObj.dSurface = %item.surface;
if (%item.surface.getClassName() $= TerrainBlock)
	%DeplObj.setDamageLevel(%DeplObj.getMaxDamage()/2);


 echo (%deplObj);
   return %deplObj;
}


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

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

function BaseInventoryPackImage::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 BaseInventoryPackImage::testNoTerrainFound(%item)
{
//return %item.surface.getClassName() !$= TerrainBlock;
}
