/** 
 * \file LevelUtils.cpp
 * \brief This file contains some level helper functions calculating various algorithms.
 * Basicly it is created to reduce the huge size of Level.cpp
 * 
 * \author Petar Bajic, MPE (C) All Rights Reserved, Homepage: www.mystic-peanut.com
 * \date January, 16 2007.
 */

#include "LevelUtils.h"

/**
 * \brief Useful function for calculating float distance between two vector points
 * \author Petar Bajic 
 * \date January, 16 2007.
 */
float distance2(D3DXVECTOR3 p, D3DXVECTOR3 s)
{
	return sqrt((p.x - s.x)*(p.x - s.x) + (p.z - s.z)*(p.z - s.z));
}

/**
 * \brief angle for object to face toward given point
 * \param objpoint position of object
 * \param point point where object is looking at
 * \author Petar Bajic 
 * \date January, 16 2007.
 */
float facePoint(D3DXVECTOR3 objpoint, D3DXVECTOR3 point)
{
	float facing = 0.0f;
    float x = point.x - objpoint.x;
    float z = point.z - objpoint.z;
 
    if (x > 0)
           facing = acos(z/sqrt(z*z + x*x));
    else
        if ((x != 0)||(z != 0))
            facing = -acos(z/sqrt(z*z + x*x));
    
    return facing;
}

/**
 * \brief determines if object is close to another object (sheep)
 * \author Petar Bajic 
 * \date January, 16 2007.
 */
bool CloseToSheep(D3DXVECTOR3 from, D3DXVECTOR3 to)
{
	if (((from.x - to.x) < 4.5) && ((from.x - to.x) > -4.5) 
		&& ((from.z - to.z) < 4.5) && ((from.z - to.z) > -4.5))
		return true;
	else
		return false;
}

/**
 * \brief determines if object is touching close to another object (sheep)
 * \author Petar Bajic 
 * \date January, 16 2007.
 */
bool ReachedSheep(D3DXVECTOR3 from, D3DXVECTOR3 to)
{
	if (((from.x - to.x) < 2) && ((from.x - to.x) > -2) 
		&& ((from.z - to.z) < 2) && ((from.z - to.z) > -2))
		return true;
	else
		return false;
}

