//Global settings

$packSettings[LSB_PackImage]  = "7 -1 Light Support Beam";
$packSetting[LSB_PackImage,0] = "0.5 Meter Beam"; 
$packSetting[LSB_PackImage,1] = "1";
$packSetting[LSB_PackImage,2] = "4";
$packSetting[LSB_PackImage,3] = "8";
$packSetting[LSB_PackImage,4] = "40"; 
$packSetting[LSB_PackImage,5] = "160";
$packSetting[LSB_PackImage,6] = "Auto scaling beam"; 
$packSetting[LSB_PackImage,7] = "Auto scaling pad";

datablock ItemData(LSB_Pack:Pack1)
{
shapeFile = "~/data/shapes/construction/crate.dts";
image = LSB_PackImage;
pickUpName = "a light support beam pack";
};

datablock ShapeBaseImageData(LSB_PackImage:PackImage1)
{
shapeFile = "~/data/shapes/construction/crate.dts";
item = LSB_Pack;
};

datablock StaticShapeData(LSB)
{  
   //shapeFile = "~/data/shapes/construction/crate.dts";
   shapeFile = "~/data/shapes/construction/smiscf.dts";
   emap = true;
};


function LSB::onCollision(%this,%obj,%col,%vec,%speed)
{
Parent::onCollision(%this,%obj,%col,%vec,%speed);
echo(collision1);
}


function LSB_Pack::onUse(%data,%cl) {
	if (%cl.player.getMountedImage(2) != %data.image.getId())
		return; 

   	%player = %cl.player;
	%ray = %player.EyeRay();

	if (!%ray) 
		return;
	%surface = %ray.object;

	if (%player.packSet == 6 || %player.packSet == 7)
	    %sizes = "0.5 500 8";
	else if (%player.packset $= "")
	   %sizes = "0.5 0.5 0.5";
	else 
	    %sizes = VectorScale("1 1 1",getWord($packSetting[LSB_PackImage,%player.packset],0));
	%joint = "0.5 0.5";
	%mask = $DeployableMask;
	if (%surface.isCubic) {
		%nrm = VectorNormalize(%surface.realVec(%ray.normal));
		%nrm2 = VectorNormalize(%surface.realVec(%surface.bestDir(%ray.normal)));  
		%ray.point = %surface.CubeFix(%ray.point,%joint);
		}
	else {
		%playerVector = vectorNormalize(-1 * getWord(%player.getEyeVector(),1) SPC getWord(%player.getEyeVector(),0) SPC "0");
		%nrm = %ray.normal;
	        if (vAbs(vRound(%nrm,100)) $= "0 0 1")
	  		%nrm2 = %playerVector;
	        else
			%nrm2 = vectorNormalize(vectorCross(%Nrm,"0 0 1"));
	}	         
	%len = RayDist(%ray.point,%nrm,getWord(%sizes,0),getWord(%sizes,1),getWord(%sizes,2),%mask,%exclude SPC %surface);

	%dep = new staticShape() {
		datablock = LSB;	
	};
        %dummy = newdummy(%dep);	
	if (%player.packSet == 7) {
		%pos = VectorAdd(%ray.point,VectorScale(%nrm,0.5*%len));
		%dir = %nrm2;
		%lenl = RayDist(%pos,%dir,getWord(%sizes,0),getWord(%sizes,1),getWord(%sizes,2),%mask,%exclude SPC %surface);
		%lenr = RayDist(%pos,VectorScale(%dir,-1),getWord(%sizes,0),getWord(%sizes,1),getWord(%sizes,2),%mask,%exclude SPC %surface);
		%size = limit(%lenl+%lenr,getWord(%sizes,0),getWord(%sizes,1));

		%center = VectorAdd(%pos,VectorScale(%dir,(%lenl-%lenr)*0.5));
		%rot = fullrot(VectorCross(%dir,%nrm),%nrm);
		%size = %len SPC %size SPC getWord(%sizes,0);	
		%dummy.setrealSize(%size);
		%dummy.setRotation(%rot);
		%dummy.setEdge(%center,"0 0 0");                
		}
	else
		{				
		%dummy.setRealSize(%joint SPC %len);
		%dummy.setRotation(fullrot(%nrm,%nrm2));
		%dummy.setEdge(%ray.point,"0 0 -0.5");		                
		}
   	%dep.setScale("0.25 0.25 2");

        if (%surface.isCubic) {
		%rot = invrot(%surface.getRotation());
		%off = "0 0 0.5";
		%dep.setDynamicTransform(VectorAdd(%surface.findEdge(%player.geteyePoint()),%off) SPC rotadd(%rot,fullrot(%player.getEyeVector(),VectorCross(%player.getEyeVector(),"0 0 -1"))),0);
		%dep.setDynamicTransform(VectorAdd(%surface.findEdge(%dummy.getPosition()),%off) SPC rotadd(%rot,%dummy.getRotation()),1);		
		%dep.lastDynTrans = VectorAdd(%surface.findEdge(%dummy.getPosition()),%off) SPC rotadd(%rot,%dummy.getRotation());
		%surface.MountObject(%dep,0);
		//%dep.schedule(1500,"Unmount");
        }
	else {
		%dep.setDynamicTransform(%player.geteyePoint() SPC fullrot(%player.getEyeVector(),VectorCross(%player.getEyeVector(),"0 0 -1")),0);
		%dep.setDynamicTransform(%dummy.getTransform(),1);
                %dep.lastDynTrans = %dummy.getTransform();
	}
		
	%dep.setDynamicScale(%dummy.getScale(),1.5);
	%dep.isCubic = 1;

	%dummy.delete();
	%ray.delete();
}