00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00049 #include "SenseHandler.h"
00050 #include "Player.h"
00051 #include "Parse.h"
00052 #include <string.h>
00053 #include <pthread.h>
00054 #include <stdlib.h>
00055
00056 extern Logger Log;
00057 void printOptions( );
00058
00063 int main( int argc, char * argv[] )
00064 {
00065 pthread_t listen, sense;
00066 ServerSettings ss;
00067 PlayerSettings cs;
00068
00069
00070 char strTeamName[MAX_TEAM_NAME_LENGTH] = "UvA_Trilearn";
00071 int iPort = ss.getPort();
00072 int iMinLogLevel = 0;
00073 int iMaxLogLevel = 0;
00074 char strHost[128] = "localhost";
00075 double dVersion = 8.08;
00076 int iMode = 0;
00077 char strFormations[128] = "formations.conf";
00078 int iNr = 2;
00079 int iReconnect = -1;
00080 bool bInfo = false;
00081 bool bSuppliedLogFile = false;
00082 ofstream os;
00083
00084
00085
00086 char * str;
00087 for( int i = 1 ; i < argc ; i = i + 2 )
00088 {
00089
00090 if( i + 1 >= argc && strncmp( argv[i], "-help", 3 ) != 0 )
00091 {
00092 cout << "Need argument for option: " << argv[i] << endl;
00093 exit( 0 );
00094 }
00095
00096 if( argv[i][0] == '-' && strlen( argv[i] ) > 1)
00097 {
00098 switch( argv[i][1] )
00099 {
00100 case '?':
00101 printOptions( );
00102 exit(0);
00103 break;
00104 case 'c':
00105 if( cs.readValues( argv[i+1], ":" ) == false )
00106 cerr << "Error in reading client file: " << argv[i+1] << endl;
00107 break;
00108 case 'f':
00109 strcpy( strFormations, argv[i+1] );
00110 break;
00111 case 'h':
00112 if( strlen( argv [i]) > 2 && argv[i][2] == 'e' )
00113 {
00114 printOptions( );
00115 exit(0);
00116 }
00117 else
00118 strcpy( strHost, argv[i+1] );
00119 break;
00120 case 'i':
00121 str = &argv[i+1][0];
00122 bInfo = (Parse::parseFirstInt( &str ) == 1 ) ? true : false ;
00123 break;
00124 case 'l':
00125 str = &argv[i+1][0];
00126 iMinLogLevel = Parse::parseFirstInt( &str );
00127 while( iMinLogLevel != 0 )
00128 {
00129 if( *str == '.' || *str == '-')
00130 {
00131 *str++;
00132 iMaxLogLevel = Parse::parseFirstInt( &str );
00133 if( iMaxLogLevel == 0 ) iMaxLogLevel = iMinLogLevel;
00134 Log.addLogRange( iMinLogLevel, iMaxLogLevel );
00135 }
00136 else
00137 Log.addLogLevel( iMinLogLevel );
00138 iMinLogLevel = Parse::parseFirstInt( &str );
00139 }
00140 break;
00141 case 'm':
00142 str = &argv[i+1][0];
00143 iMode = Parse::parseFirstInt( &str );
00144 break;
00145 case 'n':
00146 str = &argv[i+1][0];
00147 iNr = Parse::parseFirstInt( &str );
00148 break;
00149 case 'o':
00150 os.open( argv[i+1] );
00151 bSuppliedLogFile = true;
00152 break;
00153 case 'p':
00154 str = &argv[i+1][0];
00155 iPort = Parse::parseFirstInt( &str );
00156 break;
00157 case 'r':
00158 str = &argv[i+1][0];
00159 iReconnect = Parse::parseFirstInt( &str );
00160 break;
00161 case 's':
00162 if( ss.readValues( argv[i+1], ":" ) == false )
00163 cerr << "Error in reading server file: " << argv[i+1] << endl;
00164 break;
00165 case 't':
00166 strcpy( strTeamName, argv[i+1] );
00167 break;
00168 case 'v':
00169 str = &argv[i+1][0];
00170 dVersion = Parse::parseFirstDouble( &str );
00171 break;
00172 default:
00173 cerr << "(main) Unknown command option: " << argv[i] << endl;
00174 }
00175 }
00176 }
00177
00178
00179 if( bInfo == true )
00180 cout << "team : " << strTeamName << endl <<
00181 "port : " << iPort << endl <<
00182 "host : " << strHost << endl <<
00183 "version : " << dVersion << endl <<
00184 "mode : " << iMode << endl <<
00185 "playernr : " << iNr << endl <<
00186 "reconnect : " << iReconnect << endl ;
00187
00188 if( bSuppliedLogFile == true )
00189 Log.setOutputStream( os );
00190 else
00191 Log.setOutputStream( cout );
00192 Log.restartTimer( );
00193
00194 Formations fs( strFormations, FT_433_OFFENSIVE, iNr-1 );
00195 WorldModel wm( &ss, &cs, &fs );
00196 Connection c( strHost, iPort, MAX_MSG );
00197 ActHandler a( &c, &wm, &ss );
00198 SenseHandler s( &c, &wm, &ss, &cs );
00199 Player bp( &a, &wm, &ss, &cs, &fs, strTeamName, dVersion, iReconnect );
00200
00201 pthread_create( &sense, NULL, sense_callback , &s);
00202
00203 if( iMode > 0 )
00204 pthread_create( &listen, NULL, stdin_callback, &bp);
00205
00206 if( iMode == 0 )
00207 bp.mainLoop();
00208 else if( iMode == 1 )
00209 bp.test_only_update();
00210
00211 c.disconnect();
00212 os.close();
00213 }
00214
00217 void printOptions( )
00218 {
00219 cout << "Command options:" << endl <<
00220 " c(lientconf) file - use file as client conf file" << endl <<
00221 " f(ormations) file - file with formation info" << endl <<
00222 " he(lp) - print this information" << endl <<
00223 " h(ost) hostname - host to connect with" << endl <<
00224 " i(nfo) 0/1 - print variables used to start" << endl <<
00225 " l(oglevel) int[..int] - level of debug info" << endl <<
00226 " m(ode) int - which mode to start up with" << endl <<
00227 " n(umber) int - player number in formation" << endl <<
00228 " o(utput) file - write log info to (screen is default)" << endl <<
00229 " p(ort) - port number to connect with" << endl <<
00230 " r(econnect) int - reconnect as player nr" << endl <<
00231 " s(erverconf) file - use file as server conf file" << endl <<
00232 " t(eamname) name - name of your team" << endl;
00233 }