//********************************************************************
// This source file is part of Ninfa3D Engine
// 
// Copyright (C) 2008-2009 Viz_fuerte. All rights reserved.
// 
// LIMITED WARRANTY
// This software is provided "as-is", without any express or implied warranty.
// In no event shall the author be held liable for any damages arising from the use of this software.
// This is a contract. By accepting the Ninfa3D Engine (the SOFTWARE), 
// you (either an individual or a single entity, the LICENSEE) accept the terms and conditions of this licensing agreement.
// 
// If the LICENSEE does not accept the terms and conditions, then LICENSEE must not accept the SOFTWARE.
// If at a later time the LICENSEE decides not to continue licensing the SOFTWARE under these terms and 
// conditions, then the LICENSEE must delete permanently all files constituting the SOFTWARE.
//
//
// LICENSE AGREEMENT
// Ninfa3D Engine Limited grants to the LICENSEE and the LICENSEE hereby accepts a perpetual 
// nontransferable and nonexclusive license to use and distribute the SOFTWARE, for any purpose. 
// In addition, the following conditions apply:
//
// 1) The LICENSEE may not use the SOFTWARE to reverse engineer the computer algorithm used by the SOFTWARE.
// 2) The origin of the SOFTWARE must not be misrepresented; If the LICENSEE uses the SOFTWARE to distribute 
//    a product, the LICENSEE must not claim that He or She wrote the original SOFTWARE.
// 3) The LICENSEE agrees to credit the use of the Ninfa3D Engine in any program information, 
//    including credit screens, "About" dialogs, program notes or instructions and shrink-wrapped packaging.
// 4) The LICENSEE may not redistribute the SOFTWARE, except as part of a compiled software program that is 
//    not itself a physics library.
// 5) The LICENSEE agrees to notify www.mysticrealitystudio.comuv.com of any products, commercial, shareware or free 
//    that incorporate the Ninfa3D Engine technology.
//
// File: Movement.cpp
//********************************************************************

/*--------------------------------------------------------------------
Includos*/

	#include "..\..\Header Files\Ninfa3D.h"

/*--------------------------------------------------------------------
Variables externas*/

	extern IrrlichtDevice* device;
	extern IVideoDriver* driver;

/*--------------------------------------------------------------------
Declaracion global de funciones*/

extern "C"
{

	/*====================================================================
	Posiciona la entidad*/

	DLL_EXPORT void DLL_APIENTRY PositionEntity(ISceneNode* Entity,float X,float Y,float Z)
	{
		if(!Entity)
			device->getLogger()->log( "ERROR in PositionEntity: The entity does not exist.",ELL_ERROR );

			Entity->setPosition(vector3df(X,Y,Z));
	}

	/*====================================================================
	Rota la entidad*/

	DLL_EXPORT void DLL_APIENTRY RotateEntity(ISceneNode* Entity,float X,float Y,float Z)
	{
		if(!Entity)
			device->getLogger()->log( "ERROR in RotateEntity: The entity does not exist.",ELL_ERROR );

		Entity->setRotation(vector3df(X,Y,Z));
	}

	/*====================================================================
	Mueve la entidad*/

	DLL_EXPORT void DLL_APIENTRY MoveEntity(ISceneNode* Entity,float X,float Y,float Z)
	{
		if(!Entity)
			device->getLogger()->log( "ERROR in MoveEntity: The entity does not exist.",ELL_ERROR );

		vector3df move(X,Y,Z);
		matrix4 matrix;

		matrix = Entity->getRelativeTransformation();
		matrix.transformVect(move);
		Entity->setPosition(move);
		
	}

	
		/*====================================================================
	Gira la entidad* --- by nespa */

	DLL_EXPORT void DLL_APIENTRY TurnEntity(ISceneNode* Entity,float X,float Y,float Z)

	{
		
		vector3df rp(X,Y,Z);
		vector3df r = Entity->getRotation(); //get current rotation (euler) 

		matrix4 m; 
		matrix4 mp; 
		m.setRotationDegrees(r); //set matrix to current rotation 
		mp.setRotationDegrees(rp); //set second matrix to rotation to add 
		m *= mp; //multipy them 

		r = m.getRotationDegrees(); //get rotation vector from matrix (euler) 
		if(r.Y == 0.0f) 
		{
			r.Y = 0.001f; 
		}
		if(r.Y == 180.0f) 
		{
			r.Y = 180.001f;
		}
		Entity->setRotation(r); //rotate node 
	}



	
	/*====================================================================
	Apunta la entidad a otra entidad*/

	DLL_EXPORT void DLL_APIENTRY PointEntity(ISceneNode* Entity1,ISceneNode* Entity2)
	{
		if(!Entity1)
			device->getLogger()->log( "ERROR in PointEntity: The entity1 does not exist.",ELL_ERROR );

		if(!Entity2)
			device->getLogger()->log( "ERROR in PointEntity: The entity2 does not exist.",ELL_ERROR );

		vector3df origin,destin,result;
		vector3df rot;

		origin = Entity1->getAbsolutePosition();
		destin = Entity2->getAbsolutePosition();

		result = destin - origin;
		result = result.getHorizontalAngle();

		rot = result;

		Entity1->setRotation(rot);
	}

}