//--------------------------------------------------------------------------
// Deployable GPT 
// By: Mostlikely
//
//--------------------------------------------------------------------------

datablock ShapeBaseImageData(BaseGPTPackImage)
{
   mass = 20;
   emap = true;
   shapeFile = "Switch.dts";
   item = BaseGPTPack;
   mountPoint = 1;
   offset = "0 -0.2 0";
   deployed = FlipFlop;
   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 = 6.0;
};

datablock ItemData(BaseGPTPack)
{
   className = Pack;
   catagory = "Deployables";
   shapeFile = "Switch.dts";
   mass = 5.0;
   elasticity = 0.2;
   friction = 0.6;
   pickupRadius = 3;
   rotate = true;
   image = "BaseGPTPackImage";
   pickUpName = "a Global Position Targeter";
   heatSignature = 0;
   emap = true;
};

function BaseGPTPack::onPickup(%this, %obj, %shape, %amount)
{

}

function BaseGPTPackImage::onDeploy(%item, %plyr, %slot)
{
echo("PLAYER GPT DEPLOY = "@%plyr);
$HasSwitch[%plyr] = false;
RemoveBeacon(%plyr); //Ok so it's deployed now take off the beacon from the fellow carrying it.
$GPTisPack[%player.team] = false; // Tell the game the GPT isn't a pack anymore

%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 = FlipFlop;
      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();


 CheckSpawn();

 //echo (%deplObj);
   return %deplObj;
}



function BaseGPTPack::onPickup(%this, %pack, %player, %amount)
{
%player.packid = %pack.GTWID;
echo("PLAYER GPT PICKUP = "@%player);
$HasSwitch[%player] = true;
echo("Player "@%player@" has the switch = "@$HasSwitch[%player]);
messageClient(%player, 'mgsGPTpickup', 'You have the GPT. Until you deploy it no one can spawn after death!!');
RemoveBeacon(%pack); //Remove the beacon from the switch
GPTbeacon(%player,%player.team); //Put it on the guy who's carrying it

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

function BaseGPTPack::onThrow(%this,%pack,%player)
{
 RemoveBeacon(%player); // Remove the beacon on the player who was carrying it
 GPTbeacon(%pack,%player.team); // Put the beacon back on the dropped GPT
 %this.GTWID = %player.packid;
 echo("PLAYER GPT THROW = "@%player);
 $HasSwitch[%player] = false;
 serverPlay3D(ItemThrowSound, %player.getTransform());
 %pack.schedulePop();
}

function CheckSpawn()
{
for (%team = 1; %team < 3; %team++)
	{
        //Give a sphere to every emitter.
        echo(%team);
        %nteam = "Team" @ %team;
        %group = nameToId("MissionGroup/Teams/"@ %nteam @"/Base0");
        %count = %group.getCount();
         
	 for (%i = 0; %i < %count; %i++)
		 {
	         %obj = %group.getObject(%i);
                 if (%obj.Getdatablock().spawns && !%obj.sphere)                
                 {                 
                 %sphere = new SpawnSphere() {
					position = PosfromTransform(%obj.GetTransform());
					rotation = "1 0 0 0";
					scale = "1 1 1";
					dataBlock = "SpawnSphereMarker";
					lockCount = "0";
					homingCount = "0";
					radius = 50;
					sphereWeight = "100";
					indoorWeight = "0";
					outdoorWeight = "100";
				             };    
                 %obj.sphere = %sphere;
                 %sphere.emiter = %obj;
                 %sphere.mysphere = 1;
                 %spheregroup = nameToId("MissionGroup/Teams/"@ %nteam @"/spawnspheres");
                 %spheregroup.add(%sphere);
                 echo("Added spawnsphere");
                 $Switch[%obj.team] = %obj;
                 }
	    }
        // Removes dead sphere's
        %group = nameToId("MissionGroup/Teams/"@ %nteam @"/spawnspheres");
        %spheres = %group.getCount();
        for (%i = 0; %i < %spheres; %i++)
            {
            %emiter = %group.getObject(%i).emiter;
            %sphere = %group.getObject(%i);
            if (!%emiter | !IsObject(%emiter))
            	%sphere.delete();
            echo("Removed spawnsphere");
            }

   }
}

function BaseGPTPackImage::testObjectTooClose(%item)
{
   echo("Checking for walls and other nearby objects");
   %fomod = "0 0 .2";
   %surfacePt = VectorAdd(%item.surfacePt,%fomod);

   %mask =    ($TypeMasks::VehicleObjectType     | $TypeMasks::MoveableObjectType   |
               $TypeMasks::StaticShapeObjectType | $TypeMasks::ForceFieldObjectType |
               $TypeMasks::ItemObjectType        | $TypeMasks::TurretObjectType);
   InitContainerRadiusSearch(%surfacePt, 1, %mask );
   %test = containerSearchNext();
   if(!%test)
   {
      InitContainerRadiusSearch(%surfacePt, 1.5, $TypeMasks::PlayerObjectType);
      %test = containerSearchNext();
   }
return(%test);
}

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

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