datablock ParticleData(ScanP )
{
   dragCoeffiecient     = 1.0;
   gravityCoefficient   = 0;
   inheritedVelFactor   = 0.0;

   lifetimeMS           = 200;  
   lifetimeVarianceMS   = 0;
   constantAcceleration = "0 0 0";
   constantAcceleration3D = "0 0 0";
   textureName          = "~/data/shapes/player/splash";
   windcoefficient = 0;
   useInvAlpha =     false;

   spinRandomMin = 0.0;
   spinRandomMax = 0.0;

   colors[0]     = "0.3 0.8 0.3 1";
   colors[1]     = "0.3 0.8 0.3 1";
   colors[2]     = "0.3 0.8 0.3 0";
   sizes[0]      = 5;
   sizes[1]      = 5;
   sizes[2]      = 5;
   times[0]      = 0.0;
   times[1]      = 0.5;
   times[2]      = 1.0;
   //TextPoint[0]  = "0.1 -0.1";
   //TextPoint[1]  = "8 8";
   //TextPoint[2]  = "-8 8";
   //TextPoint[3]  = "-0.1 -0.1";
   TextPoint[0]  = "0 -4";
   TextPoint[1]  = "8 4";
   TextPoint[2]  = "0 4";
   TextPoint[3]  = "-8 4";
};

datablock ParticleEmitterData(ScanE )
{
   ejectionPeriodMS = 10;
   periodVarianceMS = 1;

   ejectionVelocity = "0.1 0 0";  // A little oomph at the back end
   ejectionVelocity3D = "0 0 0";  // A little oomph at the back end
   velocityVariance = "0 0 0";
   velocityVariance3D = "0 0 0";

   ejectionoffset = "10 0 0";
   offsetvariance = "0 0 0";
   ejectionoffset3D = "0 0 0";
   offsetvariance3D = "0 0 0";
   linkposition = False;
   linkrotation = False;
   linkscale = False;
   thetaMin         = 0.0;
   thetaMax         = 0.0;
   PhiVariance = 360;
   phiReferenceVel = 0;
   internalRotation = "1 0 0 0";
   internalPosition = "0 0 0";
   internalScale = "1 1 1";
   linkposition = true;
   linkScale = false;
   linkrotation = true;
   orientParticles  = True;
   orientOnVelocity =false;
   orientinverse = false;
   particles = "ScanP";
   isChain = false;
   uniform = False;
};

datablock fxLightData(ScanL)
{
FlareBitmap = "common/lighting/corona";
Colour = "0.2 0.8 0.2";
FlareColour = "0.2 0.8 0.2";
   LinkFlareSize = True;
AnimRadius = True;
AnimBrightness = True;
 FlareOn = True;
   FlareTP = true;
MinRadius = 1;
MaxRadius = 5.0;
MinBrightness = 0.5;
MaxBrightness = 1.0;
BrightnessTime = 10.0;
RadiusTime = 10.0;
ConstantSize = 1.0;
NearSize = 2.0;
FarSize = 0.5;
NearDistance = 10.0;
FarDistance = 30.0;
FadeTime = 0.1;
BlendMode = 0;
StartOffset = "5 0 0";
RadiusKeys = "ATAZA";
BrightnessKeys = "ATAZA";
};



function Createcrane(%trans,%size) {

   %base = new staticShape()
       {
       datablock = Box1;
       scale = "1 1 1";
       };
   %base.setRealSize(VectorMultiply(%size,3 SPC 3 SPC 1));
   %base.setDynamicTransform(%trans,0);
   %base.iscubic = 1;

   %base.pole = %pole = new staticShape()
       {
       datablock = Box1;
       scale = "1 1 1";
       };
   %pole.setRealSize(VectorMultiply(%size,0.5 SPC 0.5 SPC 5));
   %pole.setDynamicTransform("0 0 1 1 0 0 0",0);
   %pole.iscubic = 1;
   %pole.base = %base;
   %base.MountObject(%pole,0);   

   %base.spole = %spole = new staticShape()
       {
       datablock = Box1;
       scale = "1 1 1";
       };
   %spole.setRealSize(VectorMultiply(%size,0.5 SPC 0.5 SPC 4));
   %spole.setDynamicTransform("0 0 0.25 0 1 0" SPC 1.57/2,0);
   %spole.iscubic = 1;
   %spole.base = %base;
   %pole.MountObject(%spole,1);   


   %base.larm = %larm = new staticShape()
       {
       datablock = Box1;
       scale = "1 1 1";
       };
   %larm.setRealSize(VectorMultiply(%size,0.5 SPC 0.5 SPC 10));
   %larm.setDynamicTransform("0 1 0.75 0 1 0 1.57",0);
   %larm.iscubic = 1;
   %larm.base = %base;
   %pole.MountObject(%larm,0);

   %base.slarm = %slarm = new staticShape()
       {
       datablock = Box1;
       scale = "1 1 1";
       };
   %slarm.setRealSize(VectorMultiply(%size,0.5 SPC 0.5 SPC 10));
   %slarm.setDynamicTransform("0 -2 0 1 0 0 0",0);
   %slarm.iscubic = 1;
   %slarm.base = %base;
   %larm.MountObject(%slarm,1);

   %base.uarm = %uarm= new staticShape()
       {
       datablock = Box1;
       scale = "1 1 1";
       };
   %uarm.setRealSize(VectorMultiply(%size,0.5 SPC 0.5 SPC 10));
   %uarm.setDynamicTransform("0 -1 0.975 0 1 0 1.57",0);
   %uarm.iscubic = 1;
   %uarm.base = %base;
   %larm.MountObject(%uarm,0);

   %base.suarm = %suarm= new staticShape()
       {
       datablock = Box1;
       scale = "1 1 1";
       };
   %suarm.setRealSize(VectorMultiply(%size,0.49 SPC 0.49 SPC 6.5));
   %suarm.setDynamicTransform("0 -1 0.5 0 1 0" SPC 1.57/2,0);
   %suarm.iscubic = 1;
   %suarm.base = %base;
   %larm.MountObject(%suarm,2);


   %base.clamp = %clamp= new staticShape()
       {
       datablock = Box1;
       scale = "1 1 1";
       };
   %clamp.setRealSize(VectorMultiply(%size,1 SPC 0.25 SPC 0.5));
   %clamp.setDynamicTransform("0 1 1 0 1 0 3.14",0);
   %clamp.iscubic = 1;
   %clamp.base = %base;
   %uarm.MountObject(%clamp,0);

   %base.sclamp = %sclamp= new staticShape()
       {
       datablock = Box1;
       scale = "1 1 1";
       };
   %sclamp.setRealSize(VectorMultiply(%size,1 SPC 0.25 SPC 0.5));
   %sclamp.setDynamicTransform("0 -3 0 0 1 0 0",0);
   %sclamp.iscubic = 1;
   %sclamp.base = %base;
   %clamp.MountObject(%sclamp,1);
echo(%base);
}

function SetCrane(%obj,%pos,%nrm) {
	%center = %obj.larm.getEdge("0 -2 -1");
	%dist = VectorDist(%center,%pos);
	%vec = virVec(%obj,VectorNormalize(VectorSub(%pos,%center)));
	%idist = (%dist / GetWord(%obj.getRealSize(),2))/(9.75*2);
	if (%idist < 1)
	   %ang = mACos(%idist);
	else
	   %ang = 0;
	%ang2 = VectorAngle("0 0 1",%vec);

	%time1 = VectorAngle(VectorNormalize(getWords(%vec,0,1)),virVec(%obj,RealVec(%obj.pole,"-1 0 0")))/$PI*3;
        %time  = %ang/$Pi*3;
        %time2 = %ang2/$Pi*3;
        %time = %time < %time2 ? %time2 : %time;

	%obj.larm.setDynamicTransform("0 1 0.75 0 1 0" SPC %ang2-%ang,%time);
	%obj.uarm.setDynamicTransform("0 -1 0.975 0 1 0" SPC %ang*2,%time);
	%obj.suarm.setDynamicTransform("0 -1 0.5 0 1 0" SPC %ang,%time);
	%obj.suarm.setDynamicRealSize(VectorScale("0.49 0.49" SPC %idist*9.5,getWord(%obj.getRealSize(),2)),%time);

	%svec = mCos(%ang2-%ang) * 4.75 SPC mSin(%ang2-%ang) * 4.75 SPC 0;
	%svec = VectorAdd("2.5 0 0",%svec);
	%slen = VectorDist(%svec,"0 0 0");
	%ang3 = VectorAngle(%svec,"2.5 0 0");
	%obj.spole.setDynamicTransform("0 0 0.25 0 1 0" SPC %ang3,%time);
	%obj.spole.setDynamicRealSize(VectorScale("0.49 0.49" SPC %slen,getWord(%obj.getRealSize(),2)),%time);

	%rot = FullRot("0 0 1",VectorScale(%vec,-1));
	//%rot2 = rotadd("0 1 0" SPC -%ang2-%ang,invrot(%rot));
	%rot2 = "0 1 0" SPC -%ang2-%ang;
	if (%nrm !$= "") {   
		%vec = VectorNormalize(validateVal(MatrixMulVector("0 0 0" SPC %rot ,"1 0 0")));
		%nrm = VectorCross(%vec,VectorCross(%nrm,%vec));
		//%rot2 = rotAdd(%rot2,invRot(%obj.getRotation()));
		%rot2 = "0 1 0" SPC VectorAngle(virVec(%obj,VectorScale(%nrm,-1)),"0 0 1")-%ang2-%ang;//rotadd(%rot2,fullrot(virVec(%obj,VectorScale(%nrm,-1)),"0 0 1"));
	}

	%obj.pole.setDynamicTransform("0 0 1" SPC %rot,%time1);
	%obj.clamp.setDynamicTransform("0 0.75 1" SPC %rot2,%time);
	
	return %time > %time1 ? %time : %time1;
}

function GameBase::getRotation(%obj) {
	return GetWords(%obj.getTransform(),3,6);
}

function pickup(%crane,%obj) {
	%pos = VectorSub(%obj.getTransform(),%crane.clamp.getTransform());
	%pos = virVec(%crane.clamp,%pos);
	%pos = VectorDivide(%pos,%crane.clamp.getRealSize());
	%rot = rotadd(invrot(%crane.clamp.getRotation()),%obj.getRotation());
	%obj.setDynamicTransform(%pos SPC %rot,0);
	%crane.clamp.MountObject(%obj,1);
	%obj.setDynamicTransform(%pos SPC %rot,0);
}

function drop(%crane,%obj,%pos) {
	%crane.unMountObject(%obj);
	%obj.schedule(100,"unMount");
	%obj.schedule(200,"setDynamicTransform",%obj.getTransform(),0);
	%obj.schedule(350,"setDynamicTransform",%pos,0.65);
        %obj.schedule(500,"unMount");
}

function PickObject(%crane,%obj,%newtrans) {
	%vec = VectorSub(%obj.getEdge("0 0 -1"),%crane.larm.getTransform());
	%ivec = VirVec(%obj,%vec);
	%res1 = containerRayCast(%obj.getEdge("0 0 0"),%crane.larm.getTransform(),-1,%obj);
	%res2 = containerRayCast(getWords(%res1,1,3),%obj.getEdge("0 0 0"),-1,%res);
	%nrm = realvec(%obj,getWords(%res2,4,6));
        if (%res2 == %obj)
   	   %pt = VectorAdd(getWords(%res2,1,3),VectorScale(%nrm,0.5)) SPC %obj.getRotation();
        else
          %pt = VectorAdd(%obj.getEdge("0 0 -1"),VectorScale(%nrm,0.5)) SPC %obj.getRotation();

	%time = SetCrane(%crane,%pt,%nrm);
	schedule(%time*1000+50,0,"dropObject",%crane,%obj,%nrm,%newtrans);
}

function dropObject(%crane,%obj,%nrm,%pt) {
        %offset= %pt;
	pickup(%crane,%obj);
	%time=setCrane(%crane,%offset,%nrm);
	schedule(%time*1000+200,0,"drop",%crane,%obj,%offset);
	schedule(%time*1000+1000,0,"goport",%crane);
}

function portall(%crane,%transform,%angle) {
	%crane.objlst = "";

	InitContainerRadiusSearch(%crane.getTransform(), 50, $TypeMasks::ShapeBaseObjectType);
	%amount = 0;
        %center = "0 0 0";
	%dir = realvec(%crane,"1 0 0");
	while ((%t = containerSearchNext()) != 0) {
		if (%t.isCubic &&  %t.base != %crane && %t != %crane) {
                        %objdir = VectorNormalize(VectorSub(%t.getEdge("0 0 0"),%crane.pole.getEdge("0 0 1")));
                        
                        if (VectorAngle(%objdir,%dir) < %angle) {

				%crane.objlst = trim(%crane.objlst SPC %t);
                                %amount++; 
                                %offset = VectorSub(%crane.getTransform(),%t.getPosition());                             
                                %center = VectorAdd(%center,virVec(%crane,%offset));                                
                        }
		}
	}
        scan(%crane,2000);
        if (%amount) {
           %crane.storecenter = VectorScale(%center,1/%amount);
        %crane.storetransform = %transform;
        
	schedule(2000,0,"goport",%crane);
        }
}

function goport(%crane) {
        %transform = %crane.storetransform;

	for (%i=0;%i<getWordCount(%crane.objlst);%i++)
	    {
	    %obj = getWord(%crane.objlst,%i);
	    if (isObject(%obj)) {      
		    %t = %obj;
	    if (%t != %crane || %t.base != %crane) { 

                %offset = virVec(%crane,VectorSub(%crane.getTransform(),%t.getPosition())); 
                %offset = VectorSub(%offset,%crane.storecenter);                 
                %rot = getWords(%transform,3,6);
                %pos = MatrixMulVector("0 0 0" SPC %rot ,%offset);
                %pos = VectorAdd(%pos,getWords(%transform,0,2));
                %pos = VectorAdd(realVec(%crane,%pos),%crane.getTransform());
                %rot = rotadd(%rot,%t.getRotation());

	        PickObject(%crane,%obj,%pos SPC %rot);                 
                %crane.objlst = RemoveWord(%crane.objlst,%i);                
	        return;
            }
        }
    }
reset(%crane);
}

function scan(%crane,%time) {
	%em = CreateEmitter("",ScanE);
	%em.setDynamicTransform("0 0 0.5 0 1 0" SPC 1.57*0.5,0);
	%em.setDynamicTransform("0 0 0.5 0 1 0"SPC 1.57*1.5,%time/2000);
	%em.schedule(%time/2,"setDynamicTransform","0 0 0.5 0 1 0"SPC 1.57*0.5,%time/2000);
	%crane.pole.MountObject(%em,3);
      
	%em.schedule(%time,"delete");

}

function reset(%crane) {
setCrane(%crane,%crane.getedge("1 0 1"));
}