Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make more use of DefineConsoleMethod #1072

Merged
merged 11 commits into from Dec 26, 2014
83 changes: 36 additions & 47 deletions Engine/source/T3D/aiClient.cpp
Expand Up @@ -28,6 +28,7 @@
#include "T3D/player.h"
#include "T3D/gameBase/moveManager.h"
#include "console/consoleInternal.h"
#include "console/engineAPI.h"


IMPLEMENT_CONOBJECT( AIClient );
Expand All @@ -52,6 +53,8 @@ ConsoleDocClass( AIClient,
"@ingroup Networking\n"
);

IMPLEMENT_CALLBACK(AIClient, onConnect, void, (const char* idString), (idString),"");

/**
* Constructor
*/
Expand Down Expand Up @@ -415,78 +418,69 @@ void AIClient::onAdd( const char *nameSpace ) {
/**
* Sets the move speed for an AI object
*/
ConsoleMethod( AIClient, setMoveSpeed, void, 3, 3, "ai.setMoveSpeed( float );" ) {
DefineConsoleMethod( AIClient, setMoveSpeed, void, (F32 speed), , "ai.setMoveSpeed( float );" )
{
AIClient *ai = static_cast<AIClient *>( object );
ai->setMoveSpeed( dAtof( argv[2] ) );
ai->setMoveSpeed( speed );
}

/**
* Stops all AI movement, halt!
*/
ConsoleMethod( AIClient, stop, void, 2, 2, "ai.stop();" ) {
DefineConsoleMethod( AIClient, stop, void, (),, "ai.stop();" )
{
AIClient *ai = static_cast<AIClient *>( object );
ai->setMoveMode( AIClient::ModeStop );
}

/**
* Tells the AI to aim at the location provided
*/
ConsoleMethod( AIClient, setAimLocation, void, 3, 3, "ai.setAimLocation( x y z );" ) {
DefineConsoleMethod( AIClient, setAimLocation, void, (Point3F v), , "ai.setAimLocation( x y z );" )
{
AIClient *ai = static_cast<AIClient *>( object );
Point3F v( 0.0f,0.0f,0.0f );
dSscanf( argv[2], "%f %f %f", &v.x, &v.y, &v.z );

ai->setAimLocation( v );
}

/**
* Tells the AI to move to the location provided
*/
ConsoleMethod( AIClient, setMoveDestination, void, 3, 3, "ai.setMoveDestination( x y z );" ) {
DefineConsoleMethod( AIClient, setMoveDestination, void, (Point3F v), , "ai.setMoveDestination( x y z );" )
{
AIClient *ai = static_cast<AIClient *>( object );
Point3F v( 0.0f, 0.0f, 0.0f );
dSscanf( argv[2], "%f %f", &v.x, &v.y );

ai->setMoveDestination( v );
}

/**
* Returns the point the AI is aiming at
*/
ConsoleMethod( AIClient, getAimLocation, const char *, 2, 2, "ai.getAimLocation();" ) {
DefineConsoleMethod( AIClient, getAimLocation, Point3F, (),, "ai.getAimLocation();" )
{
AIClient *ai = static_cast<AIClient *>( object );
Point3F aimPoint = ai->getAimLocation();

static const U32 bufSize = 256;
char *returnBuffer = Con::getReturnBuffer( bufSize );
dSprintf( returnBuffer, bufSize, "%f %f %f", aimPoint.x, aimPoint.y, aimPoint.z );

return returnBuffer;
return ai->getAimLocation();
}

/**
* Returns the point the AI is set to move to
*/
ConsoleMethod( AIClient, getMoveDestination, const char *, 2, 2, "ai.getMoveDestination();" ) {
DefineConsoleMethod( AIClient, getMoveDestination, Point3F, (),, "ai.getMoveDestination();" )
{
AIClient *ai = static_cast<AIClient *>( object );
Point3F movePoint = ai->getMoveDestination();

static const U32 bufSize = 256;
char *returnBuffer = Con::getReturnBuffer( bufSize );
dSprintf( returnBuffer, bufSize, "%f %f %f", movePoint.x, movePoint.y, movePoint.z );

return returnBuffer;
return ai->getMoveDestination();
}

/**
* Sets the bots target object
*/
ConsoleMethod( AIClient, setTargetObject, void, 3, 3, "ai.setTargetObject( obj );" ) {
DefineConsoleMethod( AIClient, setTargetObject, void, (const char * objName), , "ai.setTargetObject( obj );" )
{
AIClient *ai = static_cast<AIClient *>( object );

// Find the target
ShapeBase *targetObject;
if( Sim::findObject( argv[2], targetObject ) )
if( Sim::findObject( objName, targetObject ) )
ai->setTargetObject( targetObject );
else
ai->setTargetObject( NULL );
Expand All @@ -495,7 +489,8 @@ ConsoleMethod( AIClient, setTargetObject, void, 3, 3, "ai.setTargetObject( obj )
/**
* Gets the object the AI is targeting
*/
ConsoleMethod( AIClient, getTargetObject, S32, 2, 2, "ai.getTargetObject();" ) {
DefineConsoleMethod( AIClient, getTargetObject, S32, (),, "ai.getTargetObject();" )
{
AIClient *ai = static_cast<AIClient *>( object );

return ai->getTargetObject();
Expand All @@ -504,37 +499,35 @@ ConsoleMethod( AIClient, getTargetObject, S32, 2, 2, "ai.getTargetObject();" ) {
/**
* Tells the bot the mission is cycling
*/
ConsoleMethod( AIClient, missionCycleCleanup, void, 2, 2, "ai.missionCycleCleanup();" ) {
DefineConsoleMethod( AIClient, missionCycleCleanup, void, (),, "ai.missionCycleCleanup();" )
{
AIClient *ai = static_cast<AIClient*>( object );
ai->missionCycleCleanup();
}

/**
* Sets the AI to run mode
*/
ConsoleMethod( AIClient, move, void, 2, 2, "ai.move();" ) {
DefineConsoleMethod( AIClient, move, void, (),, "ai.move();" )
{
AIClient *ai = static_cast<AIClient *>( object );
ai->setMoveMode( AIClient::ModeMove );
}

/**
* Gets the AI's location in the world
*/
ConsoleMethod( AIClient, getLocation, const char *, 2, 2, "ai.getLocation();" ) {
DefineConsoleMethod( AIClient, getLocation, Point3F, (),, "ai.getLocation();" )
{
AIClient *ai = static_cast<AIClient *>( object );
Point3F locPoint = ai->getLocation();

static const U32 bufSize = 256;
char *returnBuffer = Con::getReturnBuffer( bufSize );
dSprintf( returnBuffer, bufSize, "%f %f %f", locPoint.x, locPoint.y, locPoint.z );

return returnBuffer;
return ai->getLocation();
}

/**
* Adds an AI Player to the game
*/
ConsoleFunction( aiAddPlayer, S32 , 2, 3, "aiAddPlayer( 'playerName'[, 'AIClassType'] );" ) {
DefineConsoleFunction( aiAddPlayer, S32, (const char * name, const char * ns), (""), "'playerName'[, 'AIClassType'] );")
{
// Create the player
AIClient *aiPlayer = new AIClient();
aiPlayer->registerObject();
Expand All @@ -548,18 +541,13 @@ ConsoleFunction( aiAddPlayer, S32 , 2, 3, "aiAddPlayer( 'playerName'[, 'AIClassT
SimGroup *g = Sim::getClientGroup();
g->addObject( aiPlayer );

char *name = new char[ dStrlen( argv[1] ) + 1];
char *ns = new char[ dStrlen( argv[2] ) + 1];

dStrcpy( name, argv[1] );
dStrcpy( ns, argv[2] );

// Execute the connect console function, this is the same
// onConnect function invoked for normal client connections
Con::executef( aiPlayer, "onConnect", name );
aiPlayer->onConnect_callback( name );

// Now execute the onAdd command and feed it the namespace
if( argc > 2 )
if(dStrcmp( ns,"" ) != 0 )
aiPlayer->onAdd( ns );
else
aiPlayer->onAdd( "AIClient" );
Expand All @@ -571,7 +559,8 @@ ConsoleFunction( aiAddPlayer, S32 , 2, 3, "aiAddPlayer( 'playerName'[, 'AIClassT
/**
* Tells the AI to move forward 100 units...TEST FXN
*/
ConsoleMethod( AIClient, moveForward, void, 2, 2, "ai.moveForward();" ) {
DefineConsoleMethod( AIClient, moveForward, void, (),, "ai.moveForward();" )
{

AIClient *ai = static_cast<AIClient *>( object );
ShapeBase *player = dynamic_cast<ShapeBase*>(ai->getControlObject());
Expand Down
2 changes: 2 additions & 0 deletions Engine/source/T3D/aiClient.h
Expand Up @@ -70,6 +70,8 @@ class AIClient : public AIConnection {

DECLARE_CONOBJECT( AIClient );

DECLARE_CALLBACK( void, onConnect, (const char* idString) );

enum {
ModeStop = 0,
ModeMove,
Expand Down
60 changes: 30 additions & 30 deletions Engine/source/T3D/aiConnection.cpp
Expand Up @@ -21,6 +21,7 @@
//-----------------------------------------------------------------------------

#include "T3D/aiConnection.h"
#include "console/engineAPI.h"

IMPLEMENT_CONOBJECT( AIConnection );

Expand Down Expand Up @@ -159,67 +160,67 @@ ConsoleFunction(aiConnect, S32 , 2, 20, "(...)"


//-----------------------------------------------------------------------------
ConsoleMethod(AIConnection,setMove,void,4, 4,"(string field, float value)"
DefineConsoleMethod(AIConnection, setMove, void, (const char * field, F32 value), ,"(string field, float value)"
"Set a field on the current move.\n\n"
"@param field One of {'x','y','z','yaw','pitch','roll'}\n"
"@param value Value to set field to.")
{
Move move = object->getMove();

// Ok, a little slow for now, but this is just an example..
if (!dStricmp(argv[2],"x"))
move.x = mClampF(dAtof(argv[3]),-1,1);
if (!dStricmp(field,"x"))
move.x = mClampF(value,-1,1);
else
if (!dStricmp(argv[2],"y"))
move.y = mClampF(dAtof(argv[3]),-1,1);
if (!dStricmp(field,"y"))
move.y = mClampF(value,-1,1);
else
if (!dStricmp(argv[2],"z"))
move.z = mClampF(dAtof(argv[3]),-1,1);
if (!dStricmp(field,"z"))
move.z = mClampF(value,-1,1);
else
if (!dStricmp(argv[2],"yaw"))
move.yaw = moveClamp(dAtof(argv[3]));
if (!dStricmp(field,"yaw"))
move.yaw = moveClamp(value);
else
if (!dStricmp(argv[2],"pitch"))
move.pitch = moveClamp(dAtof(argv[3]));
if (!dStricmp(field,"pitch"))
move.pitch = moveClamp(value);
else
if (!dStricmp(argv[2],"roll"))
move.roll = moveClamp(dAtof(argv[3]));
if (!dStricmp(field,"roll"))
move.roll = moveClamp(value);

//
object->setMove(&move);
}

ConsoleMethod(AIConnection,getMove,F32,3, 3,"(string field)"
DefineConsoleMethod(AIConnection,getMove,F32, (const char * field), ,"(string field)"
"Get the given field of a move.\n\n"
"@param field One of {'x','y','z','yaw','pitch','roll'}\n"
"@returns The requested field on the current move.")
{
const Move& move = object->getMove();
if (!dStricmp(argv[2],"x"))
if (!dStricmp(field,"x"))
return move.x;
if (!dStricmp(argv[2],"y"))
if (!dStricmp(field,"y"))
return move.y;
if (!dStricmp(argv[2],"z"))
if (!dStricmp(field,"z"))
return move.z;
if (!dStricmp(argv[2],"yaw"))
if (!dStricmp(field,"yaw"))
return move.yaw;
if (!dStricmp(argv[2],"pitch"))
if (!dStricmp(field,"pitch"))
return move.pitch;
if (!dStricmp(argv[2],"roll"))
if (!dStricmp(field,"roll"))
return move.roll;
return 0;
}


ConsoleMethod(AIConnection,setFreeLook,void,3, 3,"(bool isFreeLook)"
DefineConsoleMethod(AIConnection,setFreeLook,void,(bool isFreeLook), ,"(bool isFreeLook)"
"Enable/disable freelook on the current move.")
{
Move move = object->getMove();
move.freeLook = dAtob(argv[2]);
move.freeLook = isFreeLook;
object->setMove(&move);
}

ConsoleMethod(AIConnection,getFreeLook,bool,2, 2,"getFreeLook()"
DefineConsoleMethod(AIConnection, getFreeLook, bool, (), ,"getFreeLook()"
"Is freelook on for the current move?")
{
return object->getMove().freeLook;
Expand All @@ -228,21 +229,20 @@ ConsoleMethod(AIConnection,getFreeLook,bool,2, 2,"getFreeLook()"

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

ConsoleMethod(AIConnection,setTrigger,void,4, 4,"(int trigger, bool set)"
DefineConsoleMethod(AIConnection,setTrigger,void, (S32 idx, bool set), ,"(int trigger, bool set)"
"Set a trigger.")
{
S32 idx = dAtoi(argv[2]);
if (idx >= 0 && idx < MaxTriggerKeys) {
if (idx >= 0 && idx < MaxTriggerKeys)
{
Move move = object->getMove();
move.trigger[idx] = dAtob(argv[3]);
move.trigger[idx] = set;
object->setMove(&move);
}
}

ConsoleMethod(AIConnection,getTrigger,bool,4, 4,"(int trigger)"
DefineConsoleMethod(AIConnection,getTrigger,bool, (S32 idx), ,"(int trigger)"
"Is the given trigger set?")
{
S32 idx = dAtoi(argv[2]);
if (idx >= 0 && idx < MaxTriggerKeys)
return object->getMove().trigger[idx];
return false;
Expand All @@ -251,7 +251,7 @@ ConsoleMethod(AIConnection,getTrigger,bool,4, 4,"(int trigger)"

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

ConsoleMethod(AIConnection,getAddress,const char*,2, 2,"")
DefineConsoleMethod(AIConnection,getAddress,const char*,(), ,"")
{
// Override the netConnection method to return to indicate
// this is an ai connection.
Expand Down
12 changes: 5 additions & 7 deletions Engine/source/T3D/aiPlayer.cpp
Expand Up @@ -575,23 +575,21 @@ ConsoleDocFragment _setAimObject(
"AIPlayer",
"void setAimObject(GameBase targetObject, Point3F offset);"
);
ConsoleMethod( AIPlayer, setAimObject, void, 3, 4, "( GameBase obj, [Point3F offset] )"

DefineConsoleMethod( AIPlayer, setAimObject, void, ( const char * objName, Point3F offset ), (Point3F::Zero), "( GameBase obj, [Point3F offset] )"
"Sets the bot's target object. Optionally set an offset from target location."
"@hide")
{
Point3F off( 0.0f, 0.0f, 0.0f );

// Find the target
GameBase *targetObject;
if( Sim::findObject( argv[2], targetObject ) )
if( Sim::findObject( objName, targetObject ) )
{
if (argc == 4)
dSscanf( argv[3], "%g %g %g", &off.x, &off.y, &off.z );

object->setAimObject( targetObject, off );
object->setAimObject( targetObject, offset );
}
else
object->setAimObject( 0, off );
object->setAimObject( 0, offset );
}

DefineEngineMethod( AIPlayer, getAimObject, S32, (),,
Expand Down