//Global settings

$packSettings[MFL_PackImage]  = "7 -1 Medium Floor";
$packSetting[MFL_PackImage,0] = "5 x 5 Square Meters"; 
$packSetting[MFL_PackImage,1] = "10 x 10 square meters";
$packSetting[MFL_PackImage,2] = "20 x 20 square meters";
$packSetting[MFL_PackImage,3] = "40 x 40 square meters";
$packSetting[MFL_PackImage,4] = "5 x 20 square meters"; 
$packSetting[MFL_PackImage,5] = "20 x 5 square meters";
$packSetting[MFL_PackImage,6] = "5 x 40 square meters"; 
$packSetting[MFL_PackImage,7] = "40 x 5 square meters";

$expertSettings[MFL_PackImage] = "5 -1 Medium Floor";
$expertSetting[MFL_PackImage,0] = "0.5 meter tall";
$expertSetting[MFL_PackImage,1] = "2 meter tall";
$expertSetting[MFL_PackImage,2] = "5 meter tall";
$expertSetting[MFL_PackImage,3] = "10 meter tall";
$expertSetting[MFL_PackImage,4] = "20 meter tall";
$expertSetting[MFL_PackImage,5] = "40 meter tall";


datablock ItemData(MFL_Pack:Pack1)
{
shapeFile = "~/data/shapes/construction/crate.dts";
image = MFL_PackImage;
pickUpName = "a floor Pack";
};

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

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


function MFL_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 !$= "")
		%area = getWord($packsetting[MFL_PackImage,%player.packset],0) SPC getWord($packsetting[MFL_PackImage,%player.packset],2);
	else
		%area = "5 5";
	if (%player.ExpertSet !$= "")
		%len = getWord($expertSetting[MFL_PackImage,%player.ExpertSet],0);
	else
	        %len = "0.5";

	%size = %area SPC %len;


	
	%dep = new staticShape() {
		datablock = MFL;	
	};
        %dummy = newdummy(%dep);	
	%angle = mDegToRad(%angle);
	if (%surface.isCubic) {
		%nrm = VectorNormalize(%surface.realVec(%ray.normal));
		%nrm2 = %surface.bestSide(%ray.point,%ray.normal);  
		%pt = %surface.SideLink(%ray.point,%ray.normal,getWord(%size,1));
		%rot = VectorNormalize("0 1 0") SPC %angle;
		%pt = VectorAdd(%pt,VectorScale(%ray.normal,0.5));
		%ray.point = %surface.getEdge(%pt);
		%nrm2 = VectorNormalize(%surface.realVec(%nrm2));
		
		%rot = RotAdd(fullRot(%nrm,%nrm2),%rot);
		%dummy.setRealSize(%size);		
		%dummy.setRotation(%rot);
		%dummy.setEdge(%ray.point,"-0.5 0 0.5");
	}
	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"));
		%dummy.setRealSize(%size);

		%rot = fullrot(%nrm,%nrm2);
		%dummy.setRotation(%rot);
		%dummy.setEdge(VectorAdd(%ray.point,realvec("0 0 0" SPC %rot,"0 0 0.5")),VectorAdd("0 0 0.5",%edge));
  	}	         
	
	
	
   	%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();
}