/***********************************************************
tooManyCatsAnim.mel  

Creates user-specified number of cats, in a group of a
user-specified radius.


USAGE: tooManyCatsAnim <int numberOfCats> <float groupRadius>
***********************************************************/

global proc tooManyCatsAnim(int $num, float $radius)
{
	string $groupList[0]; // declare an array to save the names of all the cat groups

	for ($i=0;$i<$num;$i++) // loop to create the cats
	{
		// create and position body
		string $body[] = `polyCylinder -r 1 -h 4 -sx 10 -sy 1 -sz 1 -ax 0 1 0 -rcp 0 -cuv 3 -ch 1`;
		xform -ro 90 0 0 -t 0 2.5 0 $body[0];
		// select the CVs on the front of the body and scale and move them
		select -r ($body[0]+".vtx[10:19]") ($body[0]+".vtx[21]") ;
		scale -r -p -5.96046e-008cm 2.5cm 2cm 0.725606 0.725606 0.725606 ;
		move -r -os -wd 0 0 0.215202 ;
		//create and position head
		string $head[] = `polyPyramid -w 1 -ns 4 -sh 1 -sc 0 -ax 0 1 0 -cuv 3 -ch 1`;
		xform -t 0 3.8 2.8 -ro 90 0 45 -s 1.6 2.3 1.6 $head[0];
		// create and position ears
		string $ear1[] = `polyCone -r 1 -h 2 -sx 6 -sy 1 -sz 0 -ax 0 1 0 -rcp 0 -cuv 3 -ch 1`;
		string $ear2[] = `polyCone -r 1 -h 2 -sx 6 -sy 1 -sz 0 -ax 0 1 0 -rcp 0 -cuv 3 -ch 1`;
		xform -t -0.45 4.85 2.2 -ro 0 0 12.8 -s 0.3 0.55 0.16 $ear1[0];
		xform -t 0.45 4.85 2.2 -ro 0 0 -12.8 -s 0.3 0.55 0.16 $ear2[0];

		parent $ear1[0] $ear2[0] $head[0];
		parent $head[0] $body[0];
		select -r $body[0];
		makeIdentity -apply true -t 1 -r 1 -s 1 -n 0 -pn 1;

		xform -ws -piv 0 3 2 $head[0];
		float $headFreq = rand(2,4);
		float $headRange = rand(10,30);
		float $bodyFreq = rand(4,7);
		float $bodyBounce = rand(0.1,0.3);
		expression -s ($head[0]+".rotateY = noise(time*"+$headFreq+")*"+$headRange+";") -ae 1 -uc all;
		expression -s ($body[0]+".translateY = abs(sin(time*"+$bodyFreq+"))*"+$bodyBounce) -ae 1 -uc all;

		// create tail
		string $tail[] = `polyCylinder -r 0.15 -h 3.5 -sx 8 -sy 16 -sz 1 -ax 0 1 0 -rcp 0 -cuv 3 -ch 1`;
		// move the pivot point to the bottom, so it bends from the bum
		xform -ws -piv 0 -1.75 0 $tail[0];		
		xform -t 0 4.96 -1.78 $tail[0];

		// deformer variables
		float $curv = rand(50,180); // random curvature for the BEND deformer
		float $wavelength = rand(2,5); // random wavelength for the SINE deformer
		float $amp = rand(0.1,0.3); // random amplitude for the SINE deformer
		float $yrot = rand(-360,360); // random Y rotation
		float $freq = rand(2,5); // time multiplier


		if ($i%2==1) // if the loop counter is ODD, add a bend to the tail
		{
			string $bend[] = `nonLinear -type bend  -lowBound 0 -highBound 1 -curvature 0`;
			expression -s ($bend[0]+".curvature = sin(time*"+$freq+")*"+$curv) -ae 1 -uc all;

			xform -ro 0 $yrot 0 $bend[1];
			parent $bend[1] $tail[0];
			parent $tail[0] $body[0];
		}
		else if ($i%2==0) // if the loop counter is EVEN, add a sine to the tail
		{
			string $sine[] = `nonLinear -type sine -lowBound -1 -highBound 1 -amplitude $amp -wavelength $wavelength -dropoff 1 -offset 0`;
			expression -s ($sine[0]+".offset = -time*"+$freq) -ae 1 -uc all ;

			xform -ro 0 $yrot 0 $sine[1];
			parent $sine[1] $tail[0];
			parent $tail[0] $body[0];
		}
		float $tailAngle = rand(-50,0);
		xform -ro $tailAngle 0 0 $tail[0];

		string $legs[]; // declare an array to save all the names of the legs
		for ($r=0;$r<4;$r++) // loop to create the legs, and gather their names
		{
			string $leg[] = `polyCube -w 0.5 -h 2 -d 0.5 -sx 1 -sy 1 -sz 1 -ax 0 1 0 -cuv 4 -ch 1`;
			xform -ws -piv 0 1 0 $leg[0]; // set the pivot points, so they bend from the shoulders/hips
			float $legFreq = rand(2,6);
			float $legRadius = rand(5,30);
			int $flip = rand(2);
			if ($flip == 0)
			{
				$legFreq = -$legFreq;
			}
			expression -s ($leg[0]+".rotateX = sin(time*"+$legFreq+")*"+$legRadius+";\n"+$leg[0]+".rotateZ = cos(time*"+$legFreq+")*"+$legRadius+";") -ae 1 -uc all;
			$legs[$r] = $leg[0];
		}
		// move the legs
		xform -t 0.5 0.5 1.5 $legs[0];
		xform -t 0.5 0.5 -1.5 $legs[1];
		xform -t -0.5 0.5 1.5 $legs[2];
		xform -t -0.5 0.5 -1.5 $legs[3];

		// group all the parts together
		select -r $body[0] $legs;
		string $groupName = `group`;

		// position variables
		float $trans[] = sphrand($radius);
		xform -t $trans[0] 0 $trans[2];

		// rotation variables
		float $rotationOffset = rand(-360,360);
		float $noiseAmt = rand(20,60);
		float $timeMult = rand(3);

		expression -s ($groupName+".rotateY = noise(time*"+$timeMult+")*"+$noiseAmt+"+"+$rotationOffset+";") -ae 1 -uc all;
		select -cl;
	}


}