/*****************************************************************************
 * $Id: patrol.cc,v 1.1 2005/08/29 00:48:07 devel Exp $
 * @file
 * @author	Jim E. Brooks  http://www.jimbrooks.org
 * @brief	Test of state-machine.
 *//*
 * LEGAL:	COPYRIGHT (C) 2005 JIM E. BROOKS
 *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
 *          OF THE GNU GENERAL PUBLIC LICENSE (GPL).
 *****************************************************************************/

#define PATROL_CC 1
#include "common.hh"
#include "gfx_machine.hh"
#include "ai.hh"
using namespace gfx;

extern Machine::Opcode gSmprocAvoidCollision[];
extern bool CheckPendingCollision( Machine* machine );

// Return true when enough frames have elasped.
static bool
IfDone( Machine* machine )
{
#if DEBUG
	*gStreamDebug << "Patrol::IfDone: tick=" << machine->GetTick() << endl;
#endif
	return machine->GetTick() >= 30;
}

// Yaw.
static bool
PatrolYaw( Machine* machine )
{
#if DEBUG
	*gStreamDebug << "Patrol::PatrolYaw(): yawing \n";
#endif
	return true;
}

// Return true when takeoff speed is reached.
static bool
AccelForTakeoff( Machine* machine )
{
	UserData* data = reinterpret_cast<UserData*>( machine->GetUserData() );
	data->mSpeed += 10;
	if ( data->mSpeed < 100 )
	{
#if DEBUG
		*gStreamDebug << "Takeoff::AccelForTakeoff(): accelerating, speed=" << data->mSpeed << endl;
#endif
		return true;
	}
	else
	{
		return false;
	}
}

Machine::Opcode gSmprocTakeoff[] =
{
DEF_PROC_BEGIN( "Takeoff" ),

	// Accelerate to take-off speed.
	DEF_LABEL( "accelerate" ),
		DEF_FUNC( AccelForTakeoff ),
		OPC_RETURN_F,
		OPC_SUSPEND,
	DEF_JUMP( "accelerate" ),

DEF_PROC_END()
};

Machine::Opcode gSmprocPatrol[] =
{
DEF_PROC_BEGIN( "Patrol" ),

	// Takeoff.
	DEF_CALL( gSmprocTakeoff ),

	// Patrol by flying in a circle.
	DEF_LABEL( "yaw_loop" ),
		DEF_FUNC( CheckPendingCollision ),
		DEF_CALL_T( gSmprocAvoidCollision ),
		DEF_FUNC( PatrolYaw ),
		OPC_SUSPEND,
	DEF_FUNC( IfDone ),
	DEF_JUMP_F( "yaw_loop" ),

DEF_PROC_END()
};
