/* UI Code */

proc do_cesWindow()
{
	if( `window -ex system_gen_win` )
	{
		deleteUI system_gen_win;
	}

	window -t "Celestial Body Generator" -s 1 -w 400 -h 300 system_gen_win;

	columnLayout -p "system_gen_win" -w 260 -h 300  system_gen_collumn;

			text -l " " -p "system_gen_collumn" -h 20 -w 100 -al "center" 01Text;	

		rowLayout -p "system_gen_collumn" -nc 2 -h 40
					-cw 1 130 	-cal 1 "right"  
					-cw 2 145 	-cal 2 "right"
					name_row;
			text -l "Object Name" -p "name_row" -h 25 -w 100 -al "center" nameText;		
			textField -p "name_row" -h 25 -w 235 name_field;


		rowLayout -p "system_gen_collumn" -nc 4 
					-cw 1 130 	-cal 1 "right"  
					-cw 2 60	-cal 2 "right"
					-cw 3 70 	-cal 3 "right"
					-cw 4 60 	-cal 4 "right"
					type_select_row;
	
			text -l "Celestial Object Type" -p "type_select_row" -h 80 -w 120 -al "center" typeText;		

			radioCollection -p "type_select_row" type_collection;
		
			radioButton -label "Star" -sl 		-onc ""
				-cl "type_collection"	-h 80 star;

			radioButton -label "Planet" 		-onc ""
				-cl "type_collection" 	-h 80 planet;

			radioButton -label "Detritus" 	-onc ""
				-cl "type_collection"	-h 80 detritus;

		rowLayout -p "system_gen_collumn" -nc 3 
					-cw 1 130 	-cal 1 "right"  
					-cw 2 145	-cal 2 "center"
					-cw 3 90 	-cal 3 "center"
					OrbitalParent;
			
			text -l "Orbital Parent" -p "OrbitalParent" -h 25 -w 100 -al "center" orbitText;
			textField -p "OrbitalParent" -h 25 -w 130 parent_field;
			button -l "Use Selection" -p "OrbitalParent" -h 25 -w 80 -al "center" 

		-c "textField -e -text `selectParent`  parent_field;"

			parent_button;
		

		rowLayout -p "system_gen_collumn" -nc 5 
					-cw 1 130 	-cal 1 "right" 
					-cw 2 60	-cal 2 "center"
					-cw 3 40 	-cal 3 "center"
					-cw 4 40 	-cal 4 "center"
					-cw 5 40 	-cal 5 "center"
					OrbitalParams;
			text -l "Orbital Distance" -p "OrbitalParams" -h 25 -w 100 -al "center" orbitText;
			floatField -p "OrbitalParams" -h 25 -w 50 orbit_field;

			radioCollection -p "type_select_row" param_collection;
		
			radioButton -label "X" -sl 		-onc ""
				-cl "param_collection"	-h 25 x;

			radioButton -label "Y" 		-onc ""
				-cl "param_collection" 	-h 25 y;

			radioButton -label "Z" 	-onc ""
				-cl "param_collection"	-h 25 z;



		rowLayout -p "system_gen_collumn" -nc 4
					-cw 1 130 	-cal 1 "right" 
					-cw 2 60	-cal 2 "center"
					-cw 3 115 	-cal 3 "center"
					-cw 4 60 	-cal 4 "center"
					Attributes;

			text -l "Mass" -p "Attributes" -h 25 -w 100 -al "center" massText;
			floatField -p "Attributes" -h 25 -w 50 mass_field;

			text -l "Diameter" -p "Attributes" -h 25 -w 110 -al "center" diaText;
			floatField -p "Attributes" -h 25 -w 50 dia_field;



		text -l " " -p "system_gen_collumn" -h 20 -w 100 -al "center" 02Text;	


	
		rowLayout -p "system_gen_collumn" -nc 4 
					-cw 1 130 	-cal 1 "right" 
					-cw 2 60	-cal 2 "center"
					-cw 3 85 	-cal 3 "center"
					-cw 4 85 	-cal 4 "center"
					rowButtons;

			text -l "Has Life" -p "rowButtons" -h 25 -w 100 -al "center" lifeText;
			
			radioCollection -p "type_select_row" life_collection;
		
			radioButton -label "Yes"  		
				-cl "life_collection"	-h 25 alive;

			radioButton -label "No" -sl		
				-cl "life_collection" 	-h 25 dead;

			button -l "Create" -p "rowButtons" -h 25 -w 80 -al "center" 
  -c "evalCreation(`textField -q -tx  name_field`,`textField -q -tx  parent_field`, `radioCollection -q -sl type_collection`, `radioCollection -q -sl param_collection`, `radioCollection -q -sl life_collection`, `floatField -q -v orbit_field` , `floatField -q -v mass_field`, `floatField -q -v dia_field` );"
			create_button;

		rowLayout -p "system_gen_collumn" -nc 1 
					-cw 1 40 	-cal 1 "center" 
					clearButton;
			button -l "Clear Physics" -p "clearButton" -h 25 -w 80 -al "center" 
  -c "clearAll"
			clear_button;
		
	showWindow system_gen_win;

}

proc string selectParent()
{
	string $selected[] = `selectedNodes`; 
	if( `size $selected` > 1)
	{
		 
		print "ERROR: You cannot use more then one parent, please select ONLY ONE object \n";

		return "";
	} 

	return $selected[0];
}

proc evalCreation(string $name, string $parent, string $type, string $param, string $life, float $orbit, float $mass, float $diameter )
{
	float $parentMass;
	float $parentDia;

	if($mass < 0.001)
	{
		$mass = 0.001;
	}


	if($diameter <= 0 )
	{
		$diameter = 0.001;
	}

	if($parent != "")
	{
		$parentMass = `getAttr ($parent + ".ms")`;
		$parentDia = `getAttr ($parent + ".dia")`;

		if($mass > $parentMass)
		{
			$mass /= $parentMass;
		}

		if($orbit < ($parentDia / 2))
		{
			$orbit += ($parentDia + $diameter);
		}
		else if(($orbit + $diameter) < ($parentDia / 2))
		{
			$diameter = ($parentDia / 4);
		}
	}

	switch ($type)
		{
    case "star":
        	starGen		($name, $parent, $param, $life, $orbit, $mass, $diameter);	
        break;
    case "planet":
        	planetGen	($name, $parent, $param, $life, $orbit, $mass, $diameter);	
        break;
    case "detritus":
        	detritusGen	($name, $parent, $param, $life, $orbit, $mass, $diameter);	
        break;
		}

}


/* Solar_System Creation Code */

proc starGen		(string $name, string $parent, string $param, string $life, float $orbit, float $mass, float $diameter)
{
	global string $Phys_Obs[];
	global int $Obs_Count;
	global int $spaced;

	int $count;

	string $hold[];
	float $parPos[3] = {0, 0, 0};
	float $parVol[3] = {0, 0, 0};
	string $axi[3]	 = {"x", "y" , "z"};


	$hold = `sphere -n  $name  -ax 0 1 0  -r ($diameter/2) -s 2 -nsp 2 -ch 01`;
	$name = strip($hold[0]);
	setAttr -keyable false ($name + ".rx");
	setAttr -keyable false ($name + ".ry");
	setAttr -keyable false ($name + ".rz");
	setAttr -keyable false ($name + ".sx");
	setAttr -keyable false ($name + ".sy");
	setAttr -keyable false ($name + ".sz");
	setAttr -keyable false ($name +  ".v");
	
	select -r $name;

	addAttr -at "float" -ln "Diameter" -sn "dia" -dv $diameter -k 0;

	$Phys_Obs[$Obs_Count] = `Set_Physics $name`;

	$Obs_Count ++;

	setAttr ($name +  ".ms") $mass;

	if($parent != "")
		{
		
		$parPos = `Get_Pos $parent`;

		for($count = 0; $count < 3; $count ++)
			{
			$parVol[$count] = `getAttr ($parent + ".v" + $axi[$count])`;
			}

		for($count = 0; $count < 3; $count ++)
			{
			setAttr ($name + ".v" + $axi[$count]) $parVol[$count];
			setAttr ($name + ".t" + $axi[$count]) $parPos[$count];
			}
			setAttr ($name + ".t" + $param) (`getAttr ($name + ".t" + $param)` + $orbit);

		switch ($param)
			{
    			case "x":
        			setAttr ($name + ".vz")	(`Get_Orbit $name $parent "x"`);
       			 break;
   			case "y":
        			setAttr ($name + ".vx")	(`Get_Orbit $name $parent "y"`);
       			 break;
    			case "z":
        			setAttr ($name + ".vy")	(`Get_Orbit $name $parent "z"`);
       			 break;
			}


		parent $name $parent;

			playButtonStepForward;



		}



}

proc planetGen		(string $name, string $parent, string $param, string $life, float $orbit, float $mass, float $diameter)
{
	global string $Phys_Obs[];
	global int $Obs_Count;

	int $count;

	string $hold[];
	float $parPos[3] = {0, 0, 0};
	float $parVol[3] = {0, 0, 0};
	string $axi[3]	 = {"x", "y" , "z"};

	$hold = `sphere -n  $name  -ax 0 1 0  -r ($diameter/2) -s 4 -nsp 4 -ch 01`;
	$name = strip($hold[0]);
	setAttr -keyable false ($name + ".rx");
	setAttr -keyable false ($name + ".ry");
	setAttr -keyable false ($name + ".rz");
	setAttr -keyable false ($name + ".sx");
	setAttr -keyable false ($name + ".sy");
	setAttr -keyable false ($name + ".sz");
	setAttr -keyable false ($name +  ".v");
	
	select -r $name;

	addAttr -at "float" -ln "Diameter" -sn "dia" -dv $diameter -k 0;

	$Phys_Obs[$Obs_Count] = `Set_Physics $name`;

	$Obs_Count ++;

	setAttr ($name +  ".ms") $mass;

	if($parent != "")
		{
		
		$parPos = `Get_Pos $parent`;

		for($count = 0; $count < 3; $count ++)
			{
			$parVol[$count] = `getAttr ($parent + ".v" + $axi[$count])`;
			}

		for($count = 0; $count < 3; $count ++)
			{
			setAttr ($name + ".v" + $axi[$count]) $parVol[$count];
			setAttr ($name + ".t" + $axi[$count]) $parPos[$count];
			}
			setAttr ($name + ".t" + $param) (`getAttr ($name + ".t" + $param)` + $orbit);

		switch ($param)
			{
    			case "x":
        			setAttr ($name + ".vz")	(`Get_Orbit $name $parent "x"`);
       			 break;
   			case "y":
        			setAttr ($name + ".vx")	(`Get_Orbit $name $parent "y"`);
       			 break;
    			case "z":
        			setAttr ($name + ".vy")	(`Get_Orbit $name $parent "z"`);
       			 break;
			}


		parent $name $parent;

			playButtonStepForward;



		}

}

proc detritusGen	(string $name, string $parent, string $param, string $life, float $orbit, float $mass, float $diameter)
{
	global string $Phys_Obs[];
	global int $Obs_Count;

	int $count;

	string $hold[];
	float $parPos[3] = {0, 0, 0};
	float $parVol[3] = {0, 0, 0};
	string $axi[3]	 = {"x", "y" , "z"};

	$hold = `polySphere -n $name -r ($diameter/2) -sx 3 -sy 3  -ax 0 1 0 -tx 0 -ch 0`;
	$name = strip($hold[0]);
	setAttr -keyable false ($name + ".rx");
	setAttr -keyable false ($name + ".ry");
	setAttr -keyable false ($name + ".rz");
	setAttr -keyable false ($name + ".sx");
	setAttr -keyable false ($name + ".sy");
	setAttr -keyable false ($name + ".sz");
	setAttr -keyable false ($name +  ".v");
	
	select -r $name;

	addAttr -at "float" -ln "Diameter" -sn "dia" -dv $diameter -k 0;

	$Phys_Obs[$Obs_Count] = `Set_Physics $name`;

	$Obs_Count ++;

	setAttr ($name +  ".ms") $mass;

	if($parent != "")
		{
		
		$parPos = `Get_Pos $parent`;

		for($count = 0; $count < 3; $count ++)
			{
			$parVol[$count] = `getAttr ($parent + ".v" + $axi[$count])`;
			}

		for($count = 0; $count < 3; $count ++)
			{
			setAttr ($name + ".v" + $axi[$count]) $parVol[$count];
			setAttr ($name + ".t" + $axi[$count]) $parPos[$count];
			}
			setAttr ($name + ".t" + $param) (`getAttr ($name + ".t" + $param)` + $orbit);

		switch ($param)
			{
    			case "x":
        			setAttr ($name + ".vz")	(`Get_Orbit $name $parent "x"`);
       			 break;
   			case "y":
        			setAttr ($name + ".vx")	(`Get_Orbit $name $parent "y"`);
       			 break;
    			case "z":
        			setAttr ($name + ".vy")	(`Get_Orbit $name $parent "z"`);
       			 break;
			}


		parent $name $parent;

			playButtonStepForward;



		}
}


proc spaceGen( float $width)
{
	float $offset = $width ;
	float $rad = $width / 2;
	string $axi[3] = {"x", "y" , "z"};
	string $let;
	

	polySphere -n "space" -r $rad -sx 12 -sy 12  -ax 0 1 0 -tx 0 -ch 0;
	
	select -r space;

	addAttr -at "float" -ln "Diameter" -sn "dia" -dv $width -min $width -max $width -k 1;

}


/* Solar_System Event Code */

proc Border()
{
	global string $Phys_Obs[];
	global int $Obs_Count;

	int $count;
	float $dis;
	float $dia = ` getAttr space.dia `;

	float $objectPos[];
	float $spacePos[];


	string $axi[3] = {"x", "y" , "z"};
	string $let;
	string $charCheck = "+";


	for($count = 0; $count < $Obs_Count; $count ++)
	{
		$objectPos = `Get_Pos $Phys_Obs[$count]`;
		$spacePos = `Get_Pos "space" `;
		
		$dis = `Get_Dis $spacePos $objectPos`;
		print ($Phys_Obs[$count] + " " + $dis + " " + $dia + "\n");
		if($dis >= ($dia/2))
		{
			for( $let in $axi)
			{
				setAttr ($Phys_Obs[$count] + ".v" + $let) ( 0 - `getAttr($Phys_Obs[$count] + ".v" + $let)`);
			}
		}
	}

}

proc clearAll()
{
	global string $Phys_Obs[];
	global int $Obs_Count;
	int $count;

	clear($Phys_Obs);
	$Obs_Count = 0;

}


proc collision(string $name)
{
	global string $Phys_Obs[];
	global int $Obs_Count;
	int $count;
	int $boom;

	float $dis;
	float $obDia = `getAttr( $name + ".dia")`;
	float $obMass = `getAttr( $name + ".ms")`;
	float $srcDia;
	float $srcMass;
	float $scale = getAttr( $name + ".sx");
	

	float $objectPos[];
	float $srcPos[];

	string $axi[3] = {"x", "y" , "z"};
	string $let;
	string $charCheck = "+";	

	for($count = 0; $count < $Obs_Count; $count ++)
	{
		$srcDia = getAttr( $Phys_Obs[$count] + ".dia");
		$srcMass = getAttr( $Phys_Obs[$count] + ".ms");

		$objectPos	= `Get_Pos $name`;
		$srcPos		= `Get_Pos $Phys_Obs[$count]`;		
		$dis		= `Get_Dis $objectPos $srcPos`;

		if($Phys_Obs[$count] == $name)
		{
			
		}
		else if( (($srcDia/4) + $dis) <= ($obDia/2) )
		{

			if($srcMass <= $obMass)
			{
				setAttr( $name + ".ms") ($obMass + ($srcMass));
				for($let in $axi)
				{
					setAttr( $name + ".s" + $let) ( ( ($obMass/$srcMass) * (1/($scale * $obMass)) ) + $scale );
					$scale = getAttr( $name + ".sx");

				}
				setAttr( $name + ".dia") ($obDia * $scale);
				$scale = getAttr( $name + ".sx");
				$obDia = `getAttr( $name + ".dia")`;
	
				delete $Phys_Obs[$count];
				removeElement $count;
			}		

		}


	}

}

    Source: geocities.com/nighzmarquls/Code

               ( geocities.com/nighzmarquls)