
/*
	file : tools.h

	Dragon Wings (c) 2005 Philipp Roßberger, Sebastian Roll
		www.einszunull.net
		www.sebastianroll.de

	This software is provided 'as-is', without any express or implied
	warranty.  In no event will the authors be held liable for any damages
	arising from the use of this software.

	Permission is granted to anyone to use this software for any purpose,
	including commercial applications, and to alter it and redistribute it
	freely, subject to the following restrictions:

	1. The origin of this software must not be misrepresented; you must not
		claim that you wrote the original software. If you use this software
		in a product, an acknowledgment in the product documentation would be
		appreciated but is not required.
	2. Altered source versions must be plainly marked as such, and must not be
		misrepresented as being the original software.
	3. This notice may not be removed or altered from any source distribution.

	Used software libraries from other people (thank you very much):

	The Irrlicht Engine:
		Copyright (c) 2002-2005 Nikolaus Gebhardt
		www.irrlicht.sourceforge.net
  
	The Character Animation Library (cal3D):
		Copyright (c) 2001, 2002 Bruno 'Beosil' Heidelberger
		www.cal3d.sourceforge.net

	The FMOD audio library:
		Copyright (c), Firelight Technologies Pty, Ltd. 1999-2004
		www.fmod.org

	Article - Using Cal3D with Irrlicht:
		Copyright (c) Thomas Suter 2004
		www.thomas.webtracker.ch/jahia/Jahia/cache/off/pid/672

*/




stringc toStringC(int i)
{
	char buf[16];
	itoa(i, buf, 10);
	buf[15] = 0;
	stringc result(buf);
	return result;
}

stringw toStringW(int i)
{
	wchar_t buf[16];
	_itow(i, buf, 10);
	buf[15] = 0;
	stringw result(buf);
	return result;
}

stringw toStringW(char* name, v3 v)
{
	char buf[128];
	sprintf(buf, "[%s] x:%+.4f y:%+.4f z:%+.4f", name, v.X, v.Y, v.Z); 
	buf[127] = 0;
	stringw result(buf);
	return result;
}

stringw toStringW(char* name, CalQuaternion q)
{
	char buf[128];
	sprintf(buf, "[%s] x:%+.4f y:%+.4f z:%+.4f w:%+.4f", name, q.x, q.y, q.z, q.w); 
	buf[127] = 0;
	stringw result(buf);
	return result;
}

stringw toStringW(char* name, quaternion q)
{
	char buf[128];
	sprintf(buf, "[%s] x:%+.4f y:%+.4f z:%+.4f w:%+.4f", name, q.X, q.Y, q.Z, q.W); 
	buf[127] = 0;
	stringw result(buf);
	return result;
}

stringw toStringW(char* name, int i)
{
	char buf[64];
	sprintf(buf, "%s:%i", name, i); 
	buf[63] = 0;
	stringw result(buf);
	return result;
}

stringw toStringW(char* name, float f)
{
	char buf[64];
	sprintf(buf, "%s:%f", name, f);
	buf[63] = 0;
	stringw result(buf);
	return result;
}

void addDbg(stringw text)
{
	stringw old(dbg->getText());
	old.append("\n");
	stringw now(old + text);
	dbg->setText(now.c_str());
}

void addDbg(stringc text)
{
	stringw old(dbg->getText());
	old.append("\n");
	old.append(text.c_str());
	stringw now(old);
	dbg->setText(now.c_str());
}

void addDbg(char* text)
{
	stringw old(dbg->getText());
	old.append("\n");
	stringw now(old + text);
	dbg->setText(now.c_str());
}

void addDbg(char* text, v3 vector)
{
	addDbg(toStringW(text, vector));
}

void addDbg(char* text, CalQuaternion q)
{
	addDbg(toStringW(text, q));
}

void addDbg(char* text, quaternion q)
{
	addDbg(toStringW(text, q));
}

void addDbg(char* text, int i)
{
	addDbg(toStringW(text, i));
}

void addDbg(char* text, float f)
{
	addDbg(toStringW(text, f));
}

void addDbg(char* text, float fArray3[3])
{
	v3 v(fArray3[0], fArray3[1], fArray3[2]);
	addDbg(toStringW(text, v));
}

void clrDbg()
{
	dbg->setText(stringw("").c_str());
}

v3 getTargetAngle(vector3df v, vector3df r) 
{ 
   //v -current node position 
   //r -target node position 

   vector3df angle; 
   float x,y,z; 
   x = r.X - v.X; 
   y = r.Y - v.Y; 
   z = r.Z - v.Z; 
    
   //angle in X-Z plane 
   angle.Y = atan2 (x, z); 
   angle.Y *= (180 / PI); //converting from rad to degrees 
    
   //just making sure angle is somewhere between 0-360 degrees 
   if(angle.Y < 0) angle.Y += 360; 
   if(angle.Y >= 360) angle.Y -= 360; 
    
   //angle in Y-Z plane while Z and X axes are already rotated around Y 
   float z1 = sqrt(x*x + z*z); 
    
   angle.X = atan2 (z1, y); 
   angle.X *= (180 / PI); //converting from rad to degrees 
   angle.X -= 90; 
    
   //just making sure angle is somewhere between 0-360 degrees 
   if(angle.X < 0) angle.X += 360; 
   if(angle.X >= 360) angle.X -= 360; 

   return angle; 
} 

const stringc makeMediaString(char* fileName)
{
	stringc result(MEDIAPATH);
	result.append(fileName);
	return result;
}

const stringc makeMediaString(irr::core::stringc fileName)
{
	stringc result(MEDIAPATH);
	result.append(fileName);
	return result;
}

const stringc makeASCII(int i)
{
	char buf[16];
	itoa(i, buf, 10);
	irr::core::stringc result(buf);
	return result;
}

int isInRange(int valToCheck, int minRange, int maxRange)
{
	return ( (valToCheck >= minRange) && (valToCheck <= maxRange) );
}

int isInRange(float valToCheck, float minRange, float maxRange)
{
	return ( (valToCheck >= minRange) && (valToCheck <= maxRange) );
}

// maps a float (in) from its range (inMin to inMax) to a targetRange (targetMin to targetMax)
// example: mapToScale(-0.7f, 0.3f, 30.0f, -45.0f, 45.0f)
// an input angle of 30.0 between its range from -45.0 to +45.0 degrees
// shold be mapped to another range from 0.1 to 0.9
// result would be 0.1 + ( (30+45) / 90 * (0.1-0.9) ) = 0.766
float mapToScale(float targetMin, float targetMax, float in, float inMin, float inMax)
{
	float result = 0.0f;
	
	if ( inMin<inMax && targetMin<targetMax && inMin<=in && inMax>=in )
	{
	// bring in to zero
	float zeroedresult = ( abs(in - inMin)) / ( abs(inMin - inMax) ) * ( abs(targetMin - targetMax) );
	result = targetMin + abs(zeroedresult);
	}
	else
	{
		cout << "mapToScale() RANGE ERROR" << endl;
	}

	return result;
}

CalQuaternion irrToCalQuaternion(quaternion qIn)
{
	CalQuaternion result(qIn.X, qIn.Y, qIn.Z, qIn.W);
	return result;
}