//********************************************************************
// 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: State.cpp
//********************************************************************

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

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

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

	extern IrrlichtDevice* device;
	extern IVideoDriver* driver;
	extern ISceneManager* SceneManager;
	extern IGUIEnvironment* guienv;
	extern IGPUProgrammingServices* gpu;
	extern IFileSystem* filesy;

/*--------------------------------------------------------------------
Declaration global de funciones*/

extern "C"
{

	/*====================================================================
	Devuelve la X coordenada donde se encuentra la entidad*/

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

		return Entity->getAbsolutePosition().X;
	}

	/*====================================================================
	Devuelve la Y coordenada donde se encuentra la entidad*/

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

		return Entity->getAbsolutePosition().Y;
	}

	/*====================================================================
	Devuelve la Z coordenada donde se encuentra la entidad*/

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

		return Entity->getAbsolutePosition().Z;
	}

	/*====================================================================
	Devuelve la rotacion X del eje de la entidad*/

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

		return Entity->getRotation().X;
	}

	/*====================================================================
	Devuelve la rotacion Y del eje de la entidad*/

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

		return Entity->getRotation().Y;
	}

	/*====================================================================
	Devuelve la rotacion Z del eje de la entidad*/

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

		return Entity->getRotation().Z;
	}

	/*====================================================================
	Devuelve nombre de la entidad*/

	DLL_EXPORT const char* DLL_APIENTRY EntityName(ISceneNode* Entity)
	{
		if(!Entity)
			device->getLogger()->log( "ERROR in EntityName: The entity does not exist.",ELL_ERROR );

		return Entity->getName();
	}

	/*====================================================================
	Devuelve la entidad que se encuentra entre las coordenadas*/

	DLL_EXPORT void* DLL_APIENTRY LinePick(float X1,float Y1,float Z1,float X2,float Y2,float Z2)
	{
		line3d<f32> Line(X1,Y1,Z1,X2,Y2,Z2);
		return (void*)SceneManager->getSceneCollisionManager()->getSceneNodeFromRayBB(Line);
	}

	/*====================================================================
	Devuelve la distancia que hay entre las dos entidades*/

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

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

		float Result,dx,dy,dz,dxp,dyp,dzp;

		vector3df vect1=Entity1->getAbsolutePosition(); 
		vector3df vect2=Entity2->getAbsolutePosition(); 
		
		
		dx = vect2.X-vect1.X;
		dy = vect2.Y-vect1.Y;
		dz = vect2.Z-vect1.Z;

		dxp = (dx*dx);
		dyp = (dy*dy);
		dzp = (dz*dz);

		Result = sqrt(dxp+dyp+dzp);

		return Result;
	}

}