//---------------------------------------------------------
// Deployable walkway, Code by Mostlikely
//---------------------------------------------------------

datablock StaticShapeData(Deployedwwall) : StaticShapeDamageProfile
{
className = wWall;
shapeFile = "smiscf.dts"; // Pmiscf.dts, alternate

maxDamage = 0.5;
destroyedLevel = 0.5;
disabledLevel = 0.3;

   mass = 1.2;
   elasticity = 0.1;
   friction = 0.9;

   pickupRadius = 1;
   sticky=false;
    
   explosion      = HandGrenadeExplosion;
      expDmgRadius = 3.0;
      expDamage = 0.1;
      expImpulse = 200.0;
   dynamicType = $TypeMasks::StaticShapeObjectType;
   deployedObject = true;
   cmdCategory = "DSupport";
   cmdIcon = CMDSensorIcon;
   cmdMiniIconName = "commander/MiniIcons/com_deploymotionsensor";


targetNameTag = 'Blast wWall';
deployAmbientThread = true;
debrisShapeName = "debris_generic_small.dts";
debris = DeployableDebris;
heatSignature = 0;
};

datablock ShapeBaseImageData(wwallDeployableImage)
{
mass = 20;
emap = true;
shapeFile = "Stackable1s.dts";
item = wwallDeployable;
mountPoint = 1;
offset = "0 -0.1 0";
deployed = Deployedwwall;
heatSignature = 0;

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

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

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

minDeployDis = 0;
maxDeployDis = 50.0;
};

datablock ItemData(wwallDeployable)
{
className = Pack;
catagory = "Deployables";
shapeFile = "Stackable1s.dts";
mass = 5.0;
elasticity = 0.2;
friction = 0.6;
pickupRadius = 1;
rotate = true;
image = "wwallDeployableImage";
pickUpName = "a walk way";
heatSignature = 0;
emap = true;
};

function wwallDeployableImage::testObjectTooClose(%item)
{
//%mask = $TypeMasks::PlayerObjectType;
//%pos = %item.surfacePt;
//return !ContainerBoxEmpty (%mask, %pos, $MinDeployableDistance);
return "";
}

function wwallDeployableImage::testNoTerrainFound(%item)
{
// don't check this for non-Landspike turret deployables
}

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

function Deployedwwall::onDestroyed(%this, %obj, %prevState)
{
Parent::onDestroyed(%this, %obj, %prevState);
$TeamDeployedCount[%obj.team, wwallDeployable]--;
%obj.schedule(500, "delete");


for (%i=0;%i < Deployables.getCount();%i++)
   {
      %dep = Deployables.getObject(%i);
       if (%dep.dSurface == %obj)
         {
         %random = getRandom() * 1000+10;
         %dep.schedule(%random, setDamageState , Destroyed);
         }
   }
for (%ti=0;%ti < 2;%ti++)
{
%nteam = "Team" @ %ti;
%group = nameToId("MissionGroup/Teams/"@ %nteam @"/Base0");
echo(%group);
%count = %group.getCount();
       for (%i = 0; %i < %count; %i++)
		 {
                 %dep = %group.getObject(%i);
	          if (%dep.dSurface == %obj)
                     {
                     %random = getRandom() * 1000+10;
                     %dep.schedule(%random, setDamageState , Destroyed);
                     }           
                 }

}
//if (%expode)
//{
                  //if(%this.expDmgRadius)
                  //     RadiusExplosion(%obj, %obj.getWorldBoxCenter(), %this.expDmgRadius, %this.expDamage, %this.expImpulse, %obj, $DamageType::Explosion);
                  %source = %obj;

	          for (%i=0;%i<1;%i++)
                  {
	               %x = (getRandom() * 0.4) - 0.2;
          	       %y = (getRandom() * 0.4) - 0.2;
                       %z = getRandom() / 2;
                       %vec = %x SPC %y SPC %z;
                       %pos = VectorAdd(posFromTransform(%obj.getTransform()), VectorScale(%vec, 3));
                       %p = new (GrenadeProjectile)() {
                                                      dataBlock        = DeployableFireball;
                                                       initialDirection = %vec;
			                               initialPosition  = %pos;
                                                       sourceObject     = %source;
                                                       sourceSlot       = 1;
                                                       vehicleObject    = %obj;
                                                      };
                       MissionCleanup.add(%p);
                   }
}


//Functions below here give StaticShape damage to the Jumpad item.
function Deployedwwall::onDamage(%this,%obj)
{
   // Set damage state based on current damage level
   %damage = %obj.getDamageLevel();
   if(%damage >= %this.destroyedLevel)
   {
      if(%obj.getDamageState() !$= "Destroyed")
      {
         %obj.setDamageState(Destroyed);
         // if object has an explosion damage radius associated with it, apply explosion damage
         if(%this.expDmgRadius)
            RadiusExplosion(%obj, %obj.getWorldBoxCenter(), %this.expDmgRadius, %this.expDamage, %this.expImpulse, %obj, $DamageType::Explosion);
      }
   }
   else
   {
      if(%damage >= %this.disabledLevel)
      {
         if(%obj.getDamageState() !$= "Disabled")
            %obj.setDamageState(Disabled);
      }
      else
      {
         if(%obj.getDamageState() !$= "Enabled")
            %obj.setDamageState(Enabled);
      }
   }
}

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

   %obj.client.player.haswwall = true; // set for wwalcheck
   %obj.client.player.packset = 0;
}

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


/////////////////////////////////////
function wwallDeployableImage::onDeploy(%item, %plyr, %slot)
{
////////////////Get settings

%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;
%testaxes=0;

////////////////Derive Settings

%rot = %playerrot;

         %maxwidth = 5; // Max Width of blastwall
         %maxheight = 5; // Max Height of blastwall
         %movedist = 2.5; 
         %zr1 = -0.5;     
         %scale = 5 / 3.85 @ " " @ 5 / 3.1 @ " 1";   
         %deplObj.lastSurfaceNrm = %item.surfaceNrm;
         %surfaceAngle = getTerrainAngle(%item.surfaceNrm); 
         %vec1 = VectorNormalize(%plyr.getEyeVector());
         %vecx1 = getWord(%vec1, 0);
         %vecy1 = getWord(%vec1, 1);
         %vecz1 = getword(%vec1, 2);
         %vec2 = %vecy1 * -1 @ " " @ %vecx1 @ " 0";
         %vec3 = %vecy1 @ " " @ %vecx1 * -1 @ " 0";

    if (%packset == 1)
         {
         %zr1 += 2.7;
         %movedist += -2.0;
         %scale = 5 / 3.85 @ " " @ 5 / 3.1 @ " 0.5";  
         }
    else if (%packset == 2)
         {
         %zr1 += 1.8;
         %movedist += -0.5;
         }
    else if (%packset == 3)
         {
         %zr1 += 0.8;
         %movedist += -0.1;
         }
    else if (%packset == 4)
         {
         if ((%surfaceAngle < 45) && (%item.surface.getType() & $TypeMasks::TerrainObjectType))
         {
         %zr1 += 1.8;
         %movedist += -0.5;
         }
         else
         {
         %zr1 += -1.7;
         %movedist += -0.5;
         }
         }
    else if (%packset == 5)
         {
         if ((%surfaceAngle < 20) && (%item.surface.getType() & $TypeMasks::TerrainObjectType))
         {
         %zr1 += 0.8;
         %movedist += -0.1;
         }
         else
         {         
         %zr1 += -0.9;
         %movedist += 0.2;
         }
         }
    else if (%packset == 6)
         {
         if (%item.surface.getType() & $TypeMasks::TerrainObjectType)
            {
             %zr1 += 2.7;
             %movedist += -2.0;
             %scale = 5 / 3.85 @ " " @ 5 / 3.1 @ " 0.5";
            }
         else
            {
             %zr1 += -2.1;
             %movedist += -2.0;
             %scale = 5 / 3.85 @ " " @ 5 / 3.1 @ " 0.5";  
             }
         }
     else  
         {
         if ((%surfaceAngle < 5) && (%item.surface.getType() & $TypeMasks::TerrainObjectType)) 
             %zr1 = 0;           
         }
         %movepos = VectorScale(%vec3, %movedist);
         %xr1 = getWord(%movepos, 0);
         %yr1 = getWord(%movepos, 1) * -1;    
         %plac = %yr1 @ " " @ %xr1 @ " " @ %zr1;
         //%plac = 2.5 @ " " @ 0 @ " " @ -0.5;
         %deplObj = new (%className)()
         { 
            dataBlock = %item.deployed; 
            scale = %scale; 
         }; 
      
  

      %deplObj.lastSurfaceNrm = %item.surfaceNrm;
      %surfaceAngle = getTerrainAngle(%item.surfaceNrm); 

    if (%packset == 1)
         {
      %d2 = -1 * (getWord(%rot, 3) * getWord(%rot, 2));
      %d2 += 1.57;
      %rot = "0 -1.57 " @ %d2;
         }
    else if (%packset == 2)
         {
      %d2 = -1 * (getWord(%rot, 3) * getWord(%rot, 2));
      %d2 += 1.57;
      %rot = "0 -0.785 " @ %d2;
         }
    else if (%packset == 3)
         {
      %d2 = -1 * (getWord(%rot, 3) * getWord(%rot, 2));
      %d2 += 1.57;
      %rot = "0 -0.349 " @ %d2;
         }
    else if (%packset == 4)
         {
      if ((%surfaceAngle < 45) && (%item.surface.getType() & $TypeMasks::TerrainObjectType))
         {
      %d2 = -1 * (getWord(%rot, 3) * getWord(%rot, 2));
      %d2 += 1.57;
      %rot = "0 -0.785 " @ %d2;
      %mat = MatrixCreateFromEuler(%rot);
         }
      else 
         {
      %d2 = -1 * (getWord(%rot, 3) * getWord(%rot, 2));
      %d2 += 1.57;
      %rot = "0 0.785 " @ %d2;
         }
         }
      else if (%packset == 5)
         {
      if ((%surfaceAngle < 20) && (%item.surface.getType() & $TypeMasks::TerrainObjectType))
         {
      %d2 = -1 * (getWord(%rot, 3) * getWord(%rot, 2));
      %d2 += 1.57;
      %rot = "0 -0.349 " @ %d2;
         }
      else
         {
      %d2 = -1 * (getWord(%rot, 3) * getWord(%rot, 2));
      %d2 += 1.57;
      %rot = "0 0.349 " @ %d2;
         }
         }
      else if (%packset == 6)
         {
      if (%item.surface.getType() & $TypeMasks::TerrainObjectType)
         {
      %d2 = -1 * (getWord(%rot, 3) * getWord(%rot, 2));
      %d2 += 1.57;
      %rot = "0 -1.57 " @ %d2;
         }
         else
         {
         %d2 = -1 * (getWord(%rot, 3) * getWord(%rot, 2));
         %d2 += 1.57;
         %rot = "0 -1.57 " @ %d2;         
         }
         }
      else
         {
      %d2 = -1 * (getWord(%rot, 3) * getWord(%rot, 2));
      %d2 += 1.57;
      %rot = "0 0 " @ %d2;
         }     
      %mat = MatrixCreateFromEuler(%rot);
      %deplObj.setTransform(VectorAdd(%item.surfacePt,%plac) SPC getWord(%mat, 3) SPC getWord(%mat, 4) SPC GetWord(%mat, 5) SPC getWord(%mat, 6));
     

//////////////////////////Aply settings//////////////////////////////


//[[Location]]:


//exact:
	//if (%rotate)
	//	%deplObj.setDeployRotation(%Dplacement, %DNormal); 
	//else
	//	%deplObj.setTransform(%Dtransform);




//misc info
        %deplObj.dSurface = %surface;
	%deplObj.dSurfaceNrm = %surfaceNrm;
	%deplObj.dUpward = %upward;
	%deplObj.dforward = %forward;
	%deplObj.dleft = %left;

	%deplObj.lUpward = %scalez;
	%deplObj.lforward = %scalex;
	%deplObj.lleft = %scaley;

//[[Settings]]:

	//%deplObj.grounded = %grounded;
        //%deplObj.Needsfit = 1;


//[Normal Stuff]]:


// set team, owner, and handle
	%deplObj.team = %plyr.client.Team;
	%deplObj.owner = %plyr.client;

// set the sensor group if it needs one
	if(%deplObj.getTarget() != -1)
		setTargetSensorGroup(%deplObj.getTarget(), %plyr.client.team);
	%deplObj.lastSurfaceNrm = %item.surfaceNrm;

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

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

// play the deploy sound
	serverPlay3D(%item.deploySound, %deplObj.getTransform());

// increment the team count for this deployed object
	$TeamDeployedCount[%plyr.team, %item.item]++;

%deplObj.deploy();    
return %deplObj;  
}