// Copyright (C) 2000 Atsushi Yamashita 
// File: MBoidCmd.cpp
// MEL Command: MBoid
// Author: Maya SDK Wizard

// Includes everything needed to register a simple MEL command with Maya.
#pragma warning (disable : 4786)

#include <maya/MSimple.h>

#include <maya/MGlobal.h>
#include <maya/MFnPlugin.h> 
//#include <maya/MSelectionList.h>
//#include <maya/MItSelectionList.h>
//#include <maya/MPoint.h>
//#include <maya/MVector.h>
//#include <maya/MMatrix.h>
//#include <maya/MDagPath.h>
//#include <maya/MFnAnimCurve.h>
//#include <maya/MFnDagNode.h>
//#include <maya/MPxCommand.h>
//#include <maya/MString.h>

#include <vector>
#include <iostream>

#include "ats_MayaBoid.h"
#include "ats_BoidGroup.h"
#include "ats_MException.h"
#include "ats_Animator.h"
#include "ats_MBoidGlobal.h"

using namespace std;
using namespace ats;

// Use helper macro to register a command with Maya.  It creates and
// registers a command that does not support undo or redo.  The 
// created class derives off of MPxCommand.
//
DeclareSimpleCommand( MBoid, "Atsushi Yamashita", "3.0");

// エラー処理 : statusでエラーが返った場合は例外でスタックの最下まで一気に戻します｡

/* * * * * * * * * * * * * * * * * * * * * *
 * -n 群れの数(数)
 * -r リーダー指定(on/off)
 * -w 数式の重み付け(協調, 慣性, 反応)
 * -b バウンディングボール(位置, 半径)
 * * * * * * * * * * * * * * * * * * * * * */

// Set keyframes to move selected object in a spiral
MStatus MBoid::doIt(const MArgList& args)
{
	MStatus status;
	try {
		// StructArg(解析文字, 引数の数<double固定>)
		StructArg n("-n", 1), w("-w", 3), b("-b", 4), f("-f", 3), s("-s", 1);
		vector<StructArg*> arg_pack;
		arg_pack.push_back(&n); // 複製する数(n)
		arg_pack.push_back(&w); // 固体の動きの要素のウェイト(n n n)
		arg_pack.push_back(&b); // バウンディングボックス(x, y, z, radius)
		arg_pack.push_back(&f); // フレーム(start, end, interval)
		arg_pack.push_back(&s); // スピード(n)
		ParseArg(args, &arg_pack);

		// コマンド入力確認
		/*for(int i=0; i < 4; i++){
			cout << *(arg_pack[i]) << endl;
		}*/

		BoidGroup group(new BoundingBall(Point3D(b.val[0], b.val[1], b.val[2]), b.val[3]));
		MakeMayaBoid(&group, n.val[0], w, b, s);
		Animator anim(&group, f.val[0], f.val[1], f.val[2]);
		anim.Run();
		status = MS::kSuccess;
	}
	catch (MException& ex){
		cerr << ex.what() << endl;
		return ex.status();
	}
	catch (exception& ex){
		cerr << ex.what() << endl;
		return MS::kFailure;
	}
	return status;
}


