//----------------------------------------------------- 
// Deployable Base Turret (Micromitch, Liquid, Nick418) 
//----------------------------------------------------- 

datablock ShapeBaseImageData(TurretDeployableImage) 
{ 

   mass = 15; 
   emap = true; 

   shapeFile = "turret_base_large.dts"; 
   item = TurretBasePack; 
   mountPoint = 1; 
   offset = "0 -2 -0.2";
    

   minDeployDis = 0.5; 
   maxDeployDis = 8.0; 

   deployed = TurretDeployedBase; 
   heatSignature = 0; 

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

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

   isLarge = False; 
   maxDepSlope = 360; 
   deploySound = StationDeploySound; 
}; 

datablock ItemData(TurretBasePack) 
{ 
   className = Pack; 
   catagory = "Deployables"; 
   shapeFile = "turret_base_large.dts"; 

//   origBarrel = "PlasmaBarrelLarge"; //added by nick418  --Cantspel: no need for it anymore
   mass = 3.0; 
   elasticity = 0.2; 
   friction = 0.6; 
   pickupRadius = 2; 
   rotate = False; 
   image = "TurretDeployableImage"; 
   pickUpName = "a deployable base turret"; 
   heatSignature = 0; 

   computeCRC = true; 
   emap = true; 

}; 

datablock TurretData(TurretDeployedBase) : TurretDamageProfile 
{ 
   className = DeployedTurret; 
   shapeFile = "turret_base_large.dts"; 

//   isGrounded = true;              // Added per Liquid (set rotation) 
//  deployedFrom = TurretBasePack;  // Added per BadShot (undeploy) 

   rechargeRate = 0.31; 

//   selfPower = true; 
//   needsNoPower = true; 

   mass = 5.0; 
   maxDamage = 2.25; 
   destroyedLevel = 2.25; 
   disabledLevel = 1.35; 
   repairRate = 0; 
   explosion = TurretExplosion; 
   expDmgRadius = 15.0; 
   expDamage = 0.7; 
   expImpulse = 2000.0; 

   deployedObject = true; 

   thetaMin = 15; 
   thetaMax = 140; 
   //thetaNull = 90; 

   isShielded = true; 
   energyPerDamagePoint = 50; 
   maxEnergy = 150; 

   humSound = SensorHumSound; 
   heatSignature = 1; 
   pausePowerThread = true; 

   canControl = true; 
   cmdCategory = "DTactical"; 
   cmdIcon = CMDTurretIcon; 
   cmdMiniIconName = "commander/MiniIcons/com_turret_grey"; 
   targetNameTag = 'Deployed Base'; 
   targetTypeTag = 'Turret'; 
   sensorData = TurretBaseSensorObj; 
   sensorRadius = TurretBaseSensorObj.detectRadius; 
   sensorColor = "0 212 45"; 

   firstPersonOnly = true; 

   debrisShapeName = "debris_generic.dts"; 
   debris = TurretDebris; 
}; 

function TurretBasePack::onPickup(%this, %obj, %shape, %amount) 
{ 
%player.packid = %pack.GTWID;
} 

function TurretDeployableImage::onMount(%data, %obj, %node)
{

   %obj.client.player.hasbturret = true; // set for turret
   %obj.client.player.packset = 0;  //Cantspel- O you have to love it! this makes things so much easyer.
}

function TurretDeployableImage::onUnmount(%data, %obj, %node)
{
	
  %obj.client.player.hasbturret = "";
  %obj.client.player.packset = 0;
}

function TurretDeployableImage::onDeploy(%item, %plyr, %slot) 
{
//Player
%className = "Turret";
%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 = TurretDeployedBase;
      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();

   if (%packset == 1) //Cantspel: added this to change the barrels
   {
      %origBarrel = "ELFBarrelLarge"; 
   }
    else if (%packset == 2)
   {
      %origBarrel = "MortarBarrelLarge"; 
   }
    else if (%packset == 3)
   {
      %origBarrel = "AABarrelLarge"; 
   }
    else if (%packset == 4)
   {
      %origBarrel = "MissileBarrelLarge"; 
   }
    else
   {
      %origBarrel = "PlasmaBarrelLarge"; 
   }


//   %origBarrel = %item.item.origBarrel;  //Cantspel: removed it screwes up the turret
   if(%origBarrel !$= "") 
   { 
      %deplObj.mountImage(%origBarrel, 0, false); 
//    %item.item.origBarrel = "PlasmaBarrelLarge"; // Set the default here, too. --Cantspel: no need for this
   }

 echo (%deplObj);
   return %deplObj;
}

function TurretBasePackImage::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 TurretDeployedBase::disassemble(%data, %plyr, %hTgt) 
//{ 
//   %plyr.getMountedImage($BackpackSlot).item.origBarrel = %hTgt.getMountedImage(0); 
//} 