//Global settings

$packSettings[LWW_PackImage]  = "7 -1 Light Walk Way";
$packSetting[LWW_PackImage,0] = "0 degrees"; 
$packSetting[LWW_PackImage,1] = "10 degrees";
$packSetting[LWW_PackImage,2] = "20 degrees";
$packSetting[LWW_PackImage,3] = "45 degrees";
$packSetting[LWW_PackImage,4] = "60 degrees"; 
$packSetting[LWW_PackImage,5] = "90 degrees";
$packSetting[LWW_PackImage,6] = "-10 degrees"; 
$packSetting[LWW_PackImage,7] = "-45 degrees";

$expertSettings[LWW_PackImage] = "3 1 Light Walk way";
$expertSetting[LWW_PackImage,0] = "Normal Size 5 5";
$expertSetting[LWW_PackImage,1] = "Wide Size 5 10";
$expertSetting[LWW_PackImage,2] = "Long Size 10 5";
$expertSetting[LWW_PackImage,3] = "Double Size 10 10";


datablock ItemData(LWW_Pack:Pack1)
{
shapeFile = "~/data/shapes/construction/crate.dts";
image = LWW_PackImage;
pickUpName = "a walk way Pack";
};

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

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


function LWW_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 !$= "")
		%angle = getWord($packSetting[LWW_PackImage,%player.packset],0);
	else
		%angle = 0;
	if (%player.ExpertSet !$= "")
		%size = getWords($expertSetting[LWW_PackImage,%player.ExpertSet],2,3) SPC "0.5";
	else
	        %size = "5 2.5 0.5";


	
	%dep = new staticShape() {
		datablock = LWW;	
	};
        %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);
		%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();
}