📄 main.cpp
字号:
/*********************************************************************************** In the name of Almighty ** ** main.cpp : Robocup 3D Soccer Simulation Team Zigorat ** (This team was previously named Gcyrus) ** ** Date: 03/20/2007 ** Author: Mahdi Hamdarsi ** Comments: This is the main part of project to link and execute agent ** ***********************************************************************************//*! \file main.cpp<pre><b>File:</b> main.cpp<b>Project:</b> Robocup Soccer Simulation Team: Zigorat<b>Authors:</b> Mahdi Hamdarsi<b>Created:</b> 03/20/2007<b>Last Revision:</b> $ID$<b>Contents:</b> This is the main part of project to link and execute agent<hr size=2><h2><b>Changes</b></h2><b>Date</b> <b>Author</b> <b>Comment</b>03/20/2007 Mahdi Initial version created</pre>*/#include <iostream> // needed for cout#include "ActHandler.h" // needed for Acthandler#include "Agent.h" // needed for Agent#include "Connection.h" // Needed for Connection#include "Parse.h" // needed to parse command line arguments#include "WorldModel.h" // needed for WorldModel#include "Formations.h"using namespace std;string strHost = "127.0.0.1"; /*!< Host to connect to */int iPort = 3100; /*!< Port to which agent is connecting */int iNumber = 0; /*!< Number of the agent */string strTeamName = "Zigorat"; /*!< Team name for the agent to register with */string strFormations = "formations.conf"; /*!< Formations configuration file */string strAgentConf = "player.conf"; /*!< Agent configuration file */bool StartAgent(TRoboCupConnection **con, WorldModel **wm, ActHandler **act, Formations **form, Agent **agent);void StopAgent(TRoboCupConnection **con, WorldModel **wm, ActHandler **act, Formations **form, Agent **agent);bool ConfigureArguments(int argc, char* argv[]);void PrintOptions();#define EXEC_MAIN /*!< For debuggin other parts, undef this *//*! This is the main function and creates and links all the different classes. First it reads in all the parameters from the command prompt and uses these values to create the classes. After all the classes are linked, the mainLoop in the Agent class is called.*/#ifdef EXEC_MAINint main(int argc, char* argv[]){ if( !ConfigureArguments( argc, argv ) ) return 1; /// Declare the global variables for everything TRoboCupConnection * connection = NULL; WorldModel * worldmodel = NULL; ActHandler * acthandler = NULL; Agent * agent = NULL; Formations * formations = NULL; /// Two functions StartAgent and StopAgent are defined to enable the agent start/stop + restart /// Currently restart is not written for the agent - it may be implemented soon if( !StartAgent( &connection, &worldmodel, &acthandler, &formations, &agent ) ) return 1; // return with 1 exit status // Start the agent main loop agent->mainLoop(); StopAgent( &connection, &worldmodel, &acthandler, &formations, &agent );}#endif // EXEC_MAIN/*! This method starts all the needed utilities for the agent like connection and then starts the agent itself, linking everything to each other \param con Connection reference to initialize \param wm WorldModel reference to initialize \param act ActHandler referenceto initialize \param form Formations reference to initialize \param agent Agent object to initialize \return bool Indicating wheather agetn started successfully */bool StartAgent(TRoboCupConnection **con, WorldModel **wm, ActHandler **act, Formations **form, Agent **agent){ StopAgent( con, wm, act, form, agent ); // halt if connection is not made *con = new TRoboCupConnection( strHost.data(), iPort ); if( !(*con)->connected() ) { delete *con; return false; } *act = new ActHandler(*con); *form = new Formations( strFormations.c_str(), FT_INITIAL, iNumber - 1 ); *wm = new WorldModel( *form ); (*wm)->setTeamName( strTeamName ); (*wm)->setAgentNumber( iNumber ); *agent = new Agent(*con, *wm, *act); return true;}/*! This method stops the agent and its utility objects and frees everything from the memory \param con Conenction refernece \param wm WorldModel reference \param act ActHandler reference \param form Formations reference \param agent Agent object reference */void StopAgent(TRoboCupConnection **con, WorldModel **wm, ActHandler **act, Formations ** form, Agent **agent){ if(*wm) { delete *wm; *wm = NULL; } if(*act) { delete *act; *act = NULL; } if(*con) { delete *con; *con = NULL; } if(*form) { delete form; *form = NULL; } if(*agent) { delete *agent; *agent = NULL; }}/*! Read in all the command options and change the associated variables in case of illegal options the agent doesn't start. \param argc Argument count of application \param argv Arguments of application \return bool indicating wheather command line arguments were parsed successfully */bool ConfigureArguments(int argc, char* argv[]){ char * str; for( int i = 1 ; i < argc ; i = i + 2 ) { // help is only option that does not have to have an argument if( i + 1 >= argc && strncmp( argv[i], "-help", 3 ) != 0 ) { cout << "Need argument for option: " << argv[i] << endl; exit( 0 ); } // read a command option if( argv[i][0] == '-' && strlen( argv[i] ) > 1) { switch( argv[i][1] ) { case 'h': // host server if((i + 1) > argc) { cout << "Need argument for option: " << argv[i] << endl; return false; } else strHost = argv[i+1]; break; case 'p': // port if((i + 1) > argc) { cout << "Need argument for option: " << argv[i] << endl; return false; } else { str = &argv[i+1][0]; iPort = Parse::parseFirstInt( &str ); } break; case 'n': // number if( (i + 1) > argc ) { cout << "Need argument for option: " << argv[i] << endl; return false; } else { str = &argv[i+1][0]; iNumber = Parse::parseFirstInt( &str ); } break; case 't': // teamname if( (i + 1) > argc ) { cout << "Need argument for option: " << argv[i] << endl; return false; } else strTeamName = &argv[i+1][0]; break; case 'f': // formation fomrations file if((i + 1) > argc) { cout << "Need argument for option: " << argv[i] << endl; return false; } else strFormations = argv[i+1]; break; case 'a': // agent fomrations file if((i + 1) > argc) { cout << "Need argument for option: " << argv[i] << endl; return false; } else strAgentConf = argv[i+1]; break; default: PrintOptions(); } } } return true;}/*! Prints any options that can be configured from the command line */void PrintOptions(){ cout << "usage: zigorat_player [-param_name param_value]" << endl; cout << " " << endl; cout << " -host : Specify the server host to connect " << endl; cout << " -port : Specify the server port to connect " << endl; cout << " -number : Specify the agents number in game " << endl; cout << " -team : Specify the team name of agent " << endl; cout << " -agentconf : Specify the agent configurations " << endl; cout << " -formations : Specify the formation configuration " << endl << endl;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -