/* Copyright Steve Rabin, 2009. 
 * All rights reserved worldwide.
 *
 * This software is provided "as is" without express or implied
 * warranties. You may freely copy and compile this source into
 * applications you distribute provided that the copyright text
 * below is included in the resulting source code, for example:
 * "Portions Copyright Steve Rabin, 2008"
 */


#include "DXUT.h"
#include "agent.h"
#include "database.h"
#include "movement.h"
#include "body.h"
#include "terrain.h"

extern D3DXVECTOR3 g_click3D;


//Add new states here
enum StateName {
	STATE_Initialize,	//Note: the first enum is the starting state
	STATE_MoveToGoal,
	STATE_Idle
};

//Add new substates here
enum SubstateName {
	//empty
};

bool Agent::States( State_Machine_Event event, MSG_Object * msg, int state, int substate )
{
BeginStateMachine

	//Global message responses
	OnMsg( MSG_Reset )
		ResetStateMachine();

	OnMsg( MSG_SetHeuristicWeight )
		m_owner->GetMovement().SetHeuristicWeight( msg->GetFloatData() );

	OnMsg( MSG_SetHeuristicCalc )
		m_owner->GetMovement().SetHeuristicCalc( msg->GetIntData() );

	OnMsg( MSG_SetSmoothing )
		m_owner->GetMovement().SetSmoothPath( msg->GetBoolData() );

	OnMsg( MSG_SetRubberbanding )
		m_owner->GetMovement().SetRubberbandPath( msg->GetBoolData() );

	OnMsg( MSG_SetAgentSpeed )
		m_animStyle = msg->GetIntData();

	OnMsg( MSG_SetSingleStep )
		m_owner->GetMovement().SingleStep( msg->GetBoolData() );

	OnMsg( MSG_SetGoal )
		int row = -1;
		int col = -1;
		if(g_terrain.GetRowColumn( &g_click3D, &row, &col ))
		{
			if( row >= 0 && row < 20 && col >= 0 && col < 20)
			{
				if( !g_terrain.IsWall( row, col ) )
				{ 
					g_terrain.ResetColors();
					g_terrain.SetColor( row, col, DEBUG_COLOR_BLUE );

					if(m_animStyle == 0) { m_owner->GetMovement().SetWalkSpeed(); }
					else if(m_animStyle == 1) { m_owner->GetMovement().SetJogSpeed(); }
					else { m_owner->GetMovement().SetWalkSpeed(); }

					m_owner->GetMovement().ComputePath( row, col ); 
					ChangeState( STATE_MoveToGoal );
				}
			}
		}


	///////////////////////////////////////////////////////////////
	DeclareState( STATE_Initialize )

		OnEnter
			m_animStyle = 1;
			m_owner->GetMovement().SetIdleSpeed();
			

	///////////////////////////////////////////////////////////////
	DeclareState( STATE_MoveToGoal )

		OnEnter

		OnMsg( MSG_Arrived )
			ChangeState( STATE_Idle );

		OnMsg( MSG_SetAgentSpeed )
			m_animStyle = msg->GetIntData();
			if(m_animStyle == 0) { m_owner->GetMovement().SetWalkSpeed(); }
			else if(m_animStyle == 1) { m_owner->GetMovement().SetJogSpeed(); }
			else { m_owner->GetMovement().SetWalkSpeed(); }


	///////////////////////////////////////////////////////////////
	DeclareState( STATE_Idle )

		OnEnter
			m_owner->GetMovement().SetIdleSpeed();


EndStateMachine
}

