//--------------------------------------------------------------------------
// Deployable Solar
// By: Mostlikely
//
//--------------------------------------------------------------------------

datablock ShapeBaseImageData(BaseSolarPackImage)
{
   mass = 10;
   emap = true;
   shapeFile = "Stackable5m.dts";
   item = BaseSolarPack;
   mountPoint = 1;
   offset = "0 -0.2 0";
   deployed = SolarPanel;
   heatSignature = 0;

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

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

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

   minDeployDis = 0.5;
   maxDeployDis = 5.0;
};

datablock ItemData(BaseSolarPack)
{
   className = Pack;
   catagory = "Deployables";
   shapeFile = "Stackable5m.dts";
   mass = 5.0;
   elasticity = 0.2;
   friction = 0.6;
   pickupRadius = 3;
   rotate = true;
   image = "BaseSolarPackImage";
   pickUpName = "a deployable Solar Array";
   heatSignature = 0;
   emap = true;
};

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

function BaseSolarPackImage::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);
        %zrot = GetWord(%rot,2);
	%yrot = GetWord(%rot,0);
	%newfo= -1 * MSin(%zrot) * MCos(%yrot) SPC MCos(%zrot) * MCos(%yrot) SPC  MSin(%yrot);         

        %newfo =  VectorNormalize(VectorScale(VectorRight(%surfacenrm,%newfo),-1));
        }
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;

   %fomod = "0 0 0";//VectorScale(%newfo,2);

   %deplObj = new (%className)()  //SolarStation
   {
      dataBlock = SolarPanel;
      position = VectorAdd(%surfacePt,%fomod);
      rotation = setrot(%rot);
   };

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

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

   %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);

   %group.powerinit();

   %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
   %deplObj.dSurface = %item.surface;
if (%item.surface.getClassName() $= TerrainBlock)
	%DeplObj.setDamageLevel(%DeplObj.getMaxDamage()/2);

   $TeamDeployedCount[%plyr.team, %item.item]++;
   echo(%item.item);
   //messageClient(%plyr.client, 'MsgDeployFailed',%item.surfaceNrm , %errorSnd);         
   %deplObj.dSurfaceNrm = %item.surfaceNrm;
   %deplObj.deploy();

 echo (%deplObj);
   return %deplObj;
}



function BaseSolarPackImage::testObjectTooClose(%item)
{
if (GetWord(%item.surfacenrm,2) < 0.99)
        {
	%rot = slopfromnrm(%item.surfacenrm);
        %zrot = GetWord(%rot,2);
	%xrot = GetWord(%rot,0);
	%newfo= -1 * MSin(%zrot) * MCos(%yrot) SPC MCos(%zrot) * MCos(%yrot) SPC  MSin(%yrot);         
        %newfo =  VectorNormalize(VectorScale(VectorRight(%surfacenrm,%newfo),-1));
        }
else
       {
       %newfo = Vectorright(%item.surfacenrm,%playerrotvec);
       %rot = rotfromnrm(%item.surfacenrm,%newfo);       
       }

   %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 BaseSolarPackImage::testNoInteriorFound(%item)
{
//constmod
   //return %item.surface.getClassName() $= TerrainBlock; //!$= InteriorInstance;
//constmod
}

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

function SolarPanel::disassemble(%data, %plyr, %hTgt)
{
  %group = %hTgt.getGroup(); 
  Parent::disassemble(%data, %plyr, %hTgt);    
  %group.schedule(510, "powerinit");
}

function SolarPanel::onDestroyed(%data, %obj, %prevState)
{
   %obj.schedule(0, "delete");
   Parent::onDestroyed(%data, %obj, %prevState);
   echo("Destroyed a Solar Array - Team: "@ %obj.team);
   $TeamDeployedCount[%obj.team, basesolarpack]--;
}

function BaseSolarPackImage::onMount(%data, %obj, %node)
{
 
}

function BaseSolarPackImage::onUnmount(%data, %obj, %node)
{
  
}

