#include "CAIPlayerObject.h"
#include "CGame.h"


//----------------------------------------------------------------//
//	Constructor/Deconstructor
//----------------------------------------------------------------//

CAIPlayerObject::CAIPlayerObject()
{
	m_AIState=AI_Idle;


}

CAIPlayerObject::~CAIPlayerObject()
{


}

//----------------------------------------------------------------//
//	
//----------------------------------------------------------------//

void CAIPlayerObject::doAI()
{
	switch(m_AIState)
	{
	
	case AI_Idle:
	{
		//If idle, find a destination and go to it

		//lets pick one at random, except not the opposite of what we were just doing
		int iDirection = rand() % 4;
		Direction oppDirection = CMap::getOppositeDirection(m_directionMoving);
		STRINGC sDirection;

		switch (iDirection)
		{
		case 0:
					
			changeDirection(N);
			break;

		case 1:
			
			
			changeDirection(S);		
			break;
		case 2:
			

			changeDirection(E);
			break;
		
		case 3:
						
			changeDirection(W);
			break;
		}
			
		

		//g_Console.AddMessage(STRINGC("Switching Dir: ") + sDirection);

		Move(m_directionMoving);
		m_AIState = AI_Moving;				

		break;

		
	}


	case AI_Moving:

		Move(m_directionMoving);
		
		if(m_Position.x == m_immediateDestination.x && m_Position.y == m_immediateDestination.y)
		{
			m_AIState = AI_Idle;

		}

		


		break;


	}

	//randomly drop some bombs for show

	if(rand() % 500 == 0)
	{
		dropBomb();
	}


}

//----------------------------------------------------------------//
//
//----------------------------------------------------------------//

void CAIPlayerObject::changeDirection(Direction iDirection)
{
	switch (iDirection)
		{
		case N:
					
			
			m_directionMoving = N;			
			m_immediateDestination.x = m_Position.x;
			m_immediateDestination.y = m_Position.y - g_Game.getTileSize();
			
			break;

		case S:			
			
			
			m_directionMoving = S;
			m_immediateDestination.x = m_Position.x;
			m_immediateDestination.y = m_Position.y + g_Game.getTileSize();

			break;
		case E:			
			
			m_directionMoving = E;
			m_immediateDestination.x = m_Position.x + g_Game.getTileSize();
			m_immediateDestination.y = m_Position.y;
			break;
		
		case W:						
			
			m_directionMoving = W;
			m_immediateDestination.x = m_Position.x - g_Game.getTileSize();
			m_immediateDestination.y = m_Position.y;
			break;
		}

}


//----------------------------------------------------------------//
//	
//----------------------------------------------------------------//

void CAIPlayerObject::doAICollision(CGameObject* obj)
{

	switch(obj->typ)	//figure out what type it is.
	{

	case typBlock:		
		{		
			//stop moving
			m_AIState = AI_Idle;	
			
		}
		
		break;
	case typExplosion:
		{
			//he's dead doesnt matter
		
		}

		break;

	case typBomb:
		{
			//stop moving
			if(m_AIState == AI_Idle && this->getLastBombPlaced()->ID!=obj->ID)
			{							
				//turn around
				changeDirection(CMap::getOppositeDirection(m_directionMoving));
				m_AIState = AI_Moving;
			}
			else
			{
				m_AIState = AI_Idle;
			}
		
		}

	case typPlayer:
		{
			//Drop a bomb!
			//dropBomb();

		}

	case typAIBombDangerZone:
		{
			
		}

		break;

	default:

		break;

	}


}