//---------------------------------------------------------
// Deployable Bunker, By Dark Dragon DX
//---------------------------------------------------------

//This is here so you don't have to worry about it later. :D
$ReverseDeployItem[BunkerInteriorBase] = "poof nothing"; //Change This If you Want The Construction Tool To Leave A Pack Behind
$TeamDeployableMax[BunkerDeployable]   = 9000; //Change This To Whatever You Want

datablock StaticShapeData(BunkerInteriorBase) : StaticShapeDamageProfile {
	className = "Bunker";
	shapeFile = "stackable1s.dts";

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

	explosion      = HandGrenadeExplosion;
	expDmgRadius = 1.0;
	expDamage = 0.05;
	expImpulse = 200;

	dynamicType = $TypeMasks::StaticShapeObjectType;
	deployedObject = true;
	cmdCategory = "DSupport";
	cmdIcon = CMDSensorIcon;
	cmdMiniIconName = "commander/MiniIcons/com_deploymotionsensor";
	targetNameTag = 'Deployed Bunker Base';
	deployAmbientThread = true;
	debrisShapeName = "debris_generic_small.dts";
	debris = DeployableDebris;
	heatSignature = 0;
};

datablock ShapeBaseImageData(BunkerDeployableImage) {
 mass = 1;
	emap = true;
	shapeFile = "stackable1s.dts";
	item = BunkerDeployable;
	mountPoint = 1;
	offset = "0 0 0";
	deployed = DeployedBunker;
	heatSignature = 0;

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

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

	isLarge = true;
	maxDepSlope = 360;
	deploySound = ItemPickupSound;

	minDeployDis = 0.5;
	maxDeployDis = 50.0;
};

datablock ItemData(BunkerDeployable) {
	className = Pack;
	catagory = "Deployables";
	shapeFile = "stackable1s.dts";
    mass = 1;
	elasticity = 0.2;
	friction = 0.6;
	pickupRadius = 1;
	rotate = true;
	image = "BunkerDeployableImage";
	pickUpName = "a deployable interior";
	heatSignature = 0;
	emap = true;
 };

function BunkerDeployableImage::testObjectTooClose(%item) {
	return "";
}

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

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

function BunkerDeployableImage::onDeploy(%item, %plyr, %slot) {
	%className = "StaticShape";

	%playerVector = vectorNormalize(-1 * getWord(%plyr.getEyeVector(),1) SPC getWord(%plyr.getEyeVector(),0) SPC "0");
	%item.surfaceNrm2 = %playerVector;
	%rot = fullRot(%item.surfaceNrm,%item.surfaceNrm2);

	%deplObj = new (%className)() {
		dataBlock = BunkerInteriorBase;
		scale = "1 1 1";
	};

	// set orientation
	%deplObj.setTransform(%item.surfacePt SPC %rot);

	// set the recharge rate right away
	if (%deplObj.getDatablock().rechargeRate)
		%deplObj.setRechargeRate(%deplObj.getDatablock().rechargeRate);

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

	// 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]++;

	addDSurface(%item.surface,%deplObj);

	// take the deployable off the player's back and out of inventory
	%plyr.unmountImage(%slot);
	%plyr.decInventory(%item.item, 1);
    //Create Teh Bunker
    %deplobj.bunker = new interiorinstance(){
    interiorfile = "bbunkd.dif";
    position = %deplobj.getposition();
    rotation = %plyr.getrotation();
    };
    %newpos = vectoradd(%deplobj.getposition(),"0 0 -0.5");
    %deplobj.bunker.settransform(%Newpos);
    %deplobj.bunker.setrotation(%plyr.getrotation());
    %Deplobj.bunker.base = %deplobj;
    //Quite Glitchy Right Now
    TestLoop(%Deplobj.bunker);
    //RELIGHT
   schedule(1000,0,"relightall");

	return %deplObj;
}

function BunkerInteriorBase::onDestroyed(%this,%obj,%prevState) {
	if (%obj.isRemoved)
		return;
    %obj.bunker.delete();
	%obj.isRemoved = true;
	Parent::onDestroyed(%this,%obj,%prevState);
	$TeamDeployedCount[%obj.team, TreeDeployable]--;
	remDSurface(%obj);
	%obj.schedule(500, "delete");
}

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

function testloop(%obj){
if (!IsObject(%obj.base)){
%obj.delete();
return;
}
else
{
schedule(1,0,"testloop",%obj);
}
}

function relightall(){
  %count = ClientGroup.getCount();
    for(%i = 0; %i < %count; %i++)
        {
    %obj = ClientGroup.getObject(%i);
    commandtoclient(%obj,'relightmission');
    }
    }

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

