//Custom functions by Mostlikely
//There's a lot of outdated stuff here
//But those things are needed for basic deploying until I can update the code.
//Be sure to mail any questions to "sebastiaan_keek@hotmail.com"


// Basic dynamic movement script.
// Fps reduction and lag producing [[A bit]]
// To be expanded/uniformed in later versions

function lev(%num)
{
if (%num < 0)
return -1;
else
return 1;
}

function abs(%num)
{
return %num * lev(%num);
}

function DynamicMove(%obj,%time,%vector,%end)
{
%pos = posFromTransform(%Obj.gettransform());
%rot = rotFromTransform(%Obj.gettransform());
%newpos = VectorAdd(%pos,%vector);
if (VectorDist(%end,%newpos) < VectorDist(%end,%pos))
   {
   %newtrans = %newpos SPC %rot;
   schedule(%time, %obj, "dynamicMove", %obj, %time, %vector,%end);
   }
else 
%newtrans = %end SPC %rot;	
%Obj.setTransform(%newtrans);
}

//Basic test.. don't use this function jet... it won't stop

function DynamicRotate(%obj,%time,%pace,%end)
{
%rot = getrot(getRotation(%obj));
%rx = GetWord(%rot,0);
%ry = GetWord(%rot,1);
%rz = GetWord(%rot,2);
%xr = %rx+ GetWord(%pace,0);
%yr = %ry + GetWord(%pace,1);
%zr = %rz + GetWord(%pace,2);
%newrot = setrot2(%xr SPC %yr SPC %zr);
setRotation(%obj, %newrot);

	schedule(50, %obj,"dynamicRotate", %obj, 50, %pace, %end ,%plyr);
}

//DynamicScale
//Better than dynamicmove...
//To be expanded.

function DynamicScale(%obj,%time,%pace,%end)
{
%scale = %obj.GetScale();
%sx = GetWord(%scale,0);
%sy = GetWord(%scale,1);
%sz = GetWord(%scale,2);
%newx = %sx + GetWord(%pace,0);
%newy = %sy + GetWord(%pace,1);
%newz = %sz + GetWord(%pace,2);


%done = 1;
if (Abs(%newx - GetWord(%end,0)) > Abs(%sx - GetWord(%end,0)))
   %xscale = GetWord(%end,0); // If we didn't make progress.. 
else 
   {
   %xscale = %newx; // If we did make progress..   
   %done = 0;
   }
if (Abs(%newy - GetWord(%end,1)) > Abs(%sy - GetWord(%end,1)))
   %yscale = GetWord(%end,1); // If we didn't make progress.. 
else 
   {
   %yscale = %newy; // If we did make progress..   
   %done = 0;
   }
if (Abs(%newz - GetWord(%end,2)) > Abs(%sz - GetWord(%end,2)))
   %zscale = GetWord(%end,2); // If we didn't make progress.. 
else 
   {
   %zscale = %newz; // If we did make progress..   
   %done = 0;
   }

%newscale = %xscale SPC %yscale SPC %zscale;
%obj.setScale(%newscale);
if (%done == 0)
	schedule(%time, %obj,"dynamicScale", %obj, %time, %pace, %end ,%plyr);
}

//Returns xyz rotation from tribes uniformalized 4 digit rotation.

function getrot(%rotation)
{
%xr = Getword(%rotation,0)*Getword(%rotation,3);
%yr = Getword(%rotation,1)*Getword(%rotation,3);                
%zr = Getword(%rotation,2)*Getword(%rotation,3);
return %xr SPC %yr SPC %zr;
}

//Returns tribes rotation from xyz rotation in an outdated way.

function setrot(%rot)
{
%mat = MatrixCreateFromEuler(%rot);
return GetWord(%mat,3) SPC GetWord(%mat,4) SPC GetWord(%mat,5) SPC GetWord(%mat,6);
}

//Controlled version setrot
//Soon to be used in deploying.

function setrot2(%rot)
{
%vn = VectorNormalize(%rot);
if (getWord(%vn,0) != 0)
%zn = getWord(%rot,0) / GetWord(%vn,0);
else if (getWord(%vn,1) != 0)
%zn = getword(%rot,1) / GetWord(%vn,1);
else if (getWord(%vn,2) != 0)
%zn = getWord(%rot,2) / GetWord(%vn,2);
else 
%zn = 0;
return %vn SPC %zn;
}

//Returns a rotation from a normal that matches the normal and faces up the slope


function slopfromnrm(%up,%fo)
{
if (Abs(GetWord(%up,2)) < 0.99)
	{
	%x=GetWord(%up,0);
	%y=GetWord(%up,1);
	%z=GetWord(%up,2);
	%xy = Mpow((Mpow(%x,2)+Mpow(%y,2)),0.5);
	%zrot = MAtan(%y,%x)-1.57;
	%xrot = -MAtan(%xy,%z);
	return  %xrot SPC "0" SPC %zrot;
	}
else
	return rotfromnrm(%up,%fo);
}

function fofromslop(%up,%fo)
{
if (Abs(GetWord(%up,2)) < 0.99)
	{
	%x=GetWord(%up,0);
	%y=GetWord(%up,1);
	%z=GetWord(%up,2);
	%xy = Mpow((Mpow(%x,2)+Mpow(%y,2)),0.5);
	%zrot = -1 * MAtan(%y,%x);
	%xrot = MAtan(%xy,%z);
	%newfo= MSin(%zrot) * MCos(%yrot) SPC MCos(%zrot) * MCos(%yrot) SPC  MSin(%yrot);         
	}
else
	%newfo = %fo;//VectorCross(%up,%fo);
return %newfo;//vectorright(%up,%newfo);
}

function modup(%up,%dir,%mod)
{
%rot = slopfromnrm(%up,"1 0 0");
if (GetWord(%up,2) == 0)
{
%xrot = GetWord(%rot,0); 
%zrot = %dir+%mod;
}
else
{
%xrot = GetWord(%rot,0)+%mod; 
%zrot = %dir;
}
%nx = MSin(%zrot) * MSin(%xrot);
%ny = MCos(%zrot) * MSin(%xrot);
%nz = MCos(%xrot);
return %nx SPC %ny SPC %nz;
}

//expanded version of slopfromnrm 
//Doesn't not work properly.. to be updated.

function rotfromnrm(%up,%le)
{
%le = VectorScale(vectorright(%up,%le),-1);
%fo = VectorCross(%up,%le);
%x = GetWord(%fo,0);
%y = GetWord(%fo,1);
%zrot = MAtan(%y,%x);
if (GetWord(%up,2) < 0 )
	%xrot = 3.14;
else
	%xrot = 0;
//%up = rotatenormal(%up,0,0,-1 * %zrot);
//%fo = rotatenormal(%fo,0,0,-1 * %zrot);
//%le = rotatenormal(%le,0,0,-1 * %zrot);
//%x = GetWord(%fo,0);
//%y = GetWord(%fo,2);
//%yrot =  MAtan(%y,%x);
//%up = rotatenormal(%up,0,-1 * %yrot,0);
//%fo = rotatenormal(%fo,0,-1 * %yrot,0);
//%le = rotatenormal(%le,0,-1 * %yrot,0);
//%x = GetWord(%up,1);
//%y = GetWord(%up,2);
//%xrot =  MAtan(%x,%y);
//%up = rotatenormal(%up, %xrot,0,0);
//%fo = rotatenormal(%fo, %xrot,0,0);
//%le = rotatenormal(%le, %xrot,0,0);
return  %xrot SPC "0" SPC %zrot + 3.14;
}

//Should rotate things in the same order as tribes2 does.

function rotatenormal(%vector,%xr,%yr,%zr)
{
                // z > x
                // z > y
                // y > x
//x rot
%x = GetWord(%vector,0);
%y = GetWord(%vector,1);
%z = GetWord(%vector,2);
%vector =  %x SPC rotatedot(%y SPC %z, %xr);
//y rot
%x = GetWord(%vector,0);
%y = GetWord(%vector,1);
%z = GetWord(%vector,2);
%vector = GetWord(rotatedot(%x SPC %z,%yr),0) SPC %y SPC GetWord(rotatedot(%x SPC %z,%yr),1);
//z rot
%x = GetWord(%vector,0);
%y = GetWord(%vector,1);
%z = GetWord(%vector,2);
%vector =  rotatedot(%x SPC %y, %zr) SPC %z;
return %vector;
}

//2dimensional rotation... used in some calculations

function rotatedot(%vector,%rot)
{
%nx = MCos(%rot) * GetWord(%vector,0) + -1 *  MSin(%rot) * GetWord(%vector,1);
%ny = MSin(%rot) * GetWord(%vector,0) + MCos(%rot) * GetWord(%vector,1); 
%nvector = %nx SPC %ny;
return %nvector;
}

//Returns the proper y as when a proper x and problematic y are introduced

function vectorright(%mainvec,%chanvec)
{
%temp = VectorCross(%mainvec,%chanvec);
return VectorNormalize(VectorCross(%mainvec,%temp));
}

//Returns the vector that %vector is heading in %direction

function vectorproject(%vector,%direction)
{
%v = %vector;//VectorSub(%item.surfacePt,%item.surface.gettransform());
%a = %direction; //VectorCross(%item.surface.up,%item.surfaceNrm);
return VectorScale(%a,(VectorDot(%a,%v) / MPow (VectorLen(%a),2)));
}

//Returns the lengt of %vector in %direction

function vectorcouple(%vector,%direction)
{
%v = %vector;//VectorSub(%item.surfacePt,%item.surface.gettransform());
%a = %direction; //VectorCross(%item.surface.up,%item.surfaceNrm);
return (VectorDot(%a,%v) / MPow (VectorLen(%a),2));
}

// See's if %position with %size is inside the %borders.

function borderlink(%position,%size,%borderup,%borderdown)
 {
 if ((%size * 2) > (%borderup - %borderdown))
  		 return (%borderup + %borderdown)/2;
 else if (%position + %size > %borderup)
 		return %borderup - %size;
 else if (%position - %size < %borderdown)
		return %borderdown + %size;
 else
		return %position;
 }

