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
00030
00050 #include "SenseHandler.h"
00051 #include "Player.h"
00052 #include "Parse.h"
00053 #include <string.h>
00054 #ifdef WIN32
00055 #include <windows.h>
00056 #else
00057 #include <pthread.h>
00058 #endif
00059 #include <stdlib.h>
00060
00061 extern Logger Log;
00062 extern Logger LogDraw;
00064 void printOptions( );
00065
00070 int main( int argc, char * argv[] )
00071 {
00072
00073 #ifdef WIN32
00074 HANDLE listen, sense;
00075 #else
00076 pthread_t listen, sense;
00077 #endif
00078
00079 ServerSettings ss;
00080 PlayerSettings cs;
00081
00082
00083 char strTeamName[MAX_TEAM_NAME_LENGTH] = "UvA_Trilearn";
00084 int iPort = ss.getPort();
00085 int iMinLogLevel ;
00086 int iMaxLogLevel ;
00087 char strHost[128] = "localhost";
00088 double dVersion = 9.3;
00089 int iMode = 0;
00090 char strFormations[128] = "formations.conf";
00091 int iNr = 2;
00092 int iReconnect = -1;
00093 bool bInfo = false;
00094 bool bSuppliedLogFile = false;
00095 bool bSuppliedLogDrawFile = false;
00096 ofstream os;
00097 ofstream osDraw;
00098
00099
00100
00101 char * str;
00102 for( int i = 1 ; i < argc ; i = i + 2 )
00103 {
00104
00105 if( i + 1 >= argc && strncmp( argv[i], "-help", 3 ) != 0 )
00106 {
00107 cout << "Need argument for option: " << argv[i] << endl;
00108 exit( 0 );
00109 }
00110
00111 if( argv[i][0] == '-' && strlen( argv[i] ) > 1)
00112 {
00113 switch( argv[i][1] )
00114 {
00115 case '?':
00116 printOptions( );
00117 exit(0);
00118 break;
00119 case 'a':
00120 osDraw.open( argv[i+1] );
00121 bSuppliedLogDrawFile = true;
00122 break;
00123 case 'c':
00124 if( cs.readValues( argv[i+1], ":" ) == false )
00125 cerr << "Error in reading client file: " << argv[i+1] << endl;
00126 break;
00127 case 'd':
00128 str = &argv[i+1][0];
00129 iMinLogLevel = Parse::parseFirstInt( &str );
00130 while( iMinLogLevel != 0 )
00131 {
00132 if( *str == '.' || *str == '-')
00133 {
00134 *str += 1 ;
00135 iMaxLogLevel = Parse::parseFirstInt( &str );
00136 if( iMaxLogLevel == 0 ) iMaxLogLevel = iMinLogLevel;
00137 LogDraw.addLogRange( iMinLogLevel, iMaxLogLevel );
00138 }
00139 else
00140 LogDraw.addLogLevel( iMinLogLevel );
00141 iMinLogLevel = Parse::parseFirstInt( &str );
00142 }
00143 break;
00144 case 'f':
00145 strcpy( strFormations, argv[i+1] );
00146 break;
00147 case 'h':
00148 if( strlen( argv [i]) > 2 && argv[i][2] == 'e' )
00149 {
00150 printOptions( );
00151 exit(0);
00152 }
00153 else
00154 strcpy( strHost, argv[i+1] );
00155 break;
00156 case 'i':
00157 str = &argv[i+1][0];
00158 bInfo = (Parse::parseFirstInt( &str ) == 1 ) ? true : false ;
00159 break;
00160 case 'l':
00161 str = &argv[i+1][0];
00162 iMinLogLevel = Parse::parseFirstInt( &str );
00163 while( iMinLogLevel != 0 )
00164 {
00165 if( *str == '.' || *str == '-')
00166 {
00167 *str += 1 ;
00168 iMaxLogLevel = Parse::parseFirstInt( &str );
00169 if( iMaxLogLevel == 0 ) iMaxLogLevel = iMinLogLevel;
00170 Log.addLogRange( iMinLogLevel, iMaxLogLevel );
00171 }
00172 else
00173 Log.addLogLevel( iMinLogLevel );
00174 iMinLogLevel = Parse::parseFirstInt( &str );
00175 }
00176 break;
00177 case 'm':
00178 str = &argv[i+1][0];
00179 iMode = Parse::parseFirstInt( &str );
00180 break;
00181 case 'n':
00182 str = &argv[i+1][0];
00183 iNr = Parse::parseFirstInt( &str );
00184 break;
00185 case 'o':
00186 os.open( argv[i+1] );
00187 bSuppliedLogFile = true;
00188 break;
00189 case 'p':
00190 str = &argv[i+1][0];
00191 iPort = Parse::parseFirstInt( &str );
00192 break;
00193 case 'r':
00194 str = &argv[i+1][0];
00195 iReconnect = Parse::parseFirstInt( &str );
00196 break;
00197 case 's':
00198 if( ss.readValues( argv[i+1], ":" ) == false )
00199 cerr << "Error in reading server file: " << argv[i+1] << endl;
00200 break;
00201 case 't':
00202 strcpy( strTeamName, argv[i+1] );
00203 break;
00204 case 'v':
00205 str = &argv[i+1][0];
00206 dVersion = Parse::parseFirstDouble( &str );
00207 break;
00208 default:
00209 cerr << "(main) Unknown command option: " << argv[i] << endl;
00210 }
00211 }
00212 }
00213 if( bInfo == true )
00214 {
00215 cout << "team : " << strTeamName << endl <<
00216 "port : " << iPort << endl <<
00217 "host : " << strHost << endl <<
00218 "version : " << dVersion << endl <<
00219 "mode : " << iMode << endl <<
00220 "playernr : " << iNr << endl <<
00221 "reconnect : " << iReconnect << endl ;
00222 Log.showLogLevels( cout );
00223 LogDraw.showLogLevels( cout );
00224 }
00225 if( bSuppliedLogFile == true )
00226 Log.setOutputStream( os );
00227 else
00228 Log.setOutputStream( cout );
00229 if( bSuppliedLogDrawFile == true )
00230 LogDraw.setOutputStream( osDraw );
00231 else
00232 LogDraw.setOutputStream( cout );
00233
00234 Log.restartTimer( );
00235
00236 Formations fs( strFormations, (FormationT)cs.getInitialFormation(), iNr-1 );
00237
00238 WorldModel wm( &ss, &cs, &fs );
00239 Connection c( strHost, iPort, MAX_MSG );
00240 ActHandler a( &c, &wm, &ss );
00241 SenseHandler s( &c, &wm, &ss, &cs );
00242 Player bp( &a, &wm, &ss, &cs, &fs, strTeamName, dVersion, iReconnect );
00243
00244 #ifdef WIN32
00245 DWORD id1;
00246 sense = CreateThread(NULL, 0, &sense_callback, &s, 0, &id1);
00247 if (sense == NULL)
00248 {
00249 cerr << "creat thread error" << endl;
00250 return false;
00251 }
00252 #else
00253 pthread_create( &sense, NULL, sense_callback , &s);
00254 #endif
00255
00256 if( iMode > 0 && iMode < 9 )
00257 #ifdef WIN32
00258 {
00259 DWORD id2;
00260 listen = CreateThread(NULL, 0, &stdin_callback, &bp, 0, &id2);
00261 if ( listen == NULL)
00262 {
00263 cerr << "create thread error" << endl;
00264 return false;
00265 }
00266 }
00267 #else
00268 pthread_create( &listen, NULL, stdin_callback, &bp);
00269 #endif
00270
00271 if( iMode == 0 )
00272 bp.mainLoop();
00273
00274 c.disconnect();
00275 os.close();
00276 }
00277
00280 void printOptions( )
00281 {
00282 cout << "Command options:" << endl <<
00283 " a file - write drawing log info to " << endl <<
00284
00285
00286 " c(lientconf) file - use file as client conf file" << endl <<
00287 " d(rawloglevel) int[..int] - level(s) of drawing debug info" << endl <<
00288 " f(ormations) file - file with formation info" << endl <<
00289 " he(lp) - print this information" << endl <<
00290 " h(ost) hostname - host to connect with" << endl <<
00291 " i(nfo) 0/1 - print variables used to start" << endl <<
00292 " l(oglevel) int[..int] - level of debug info" << endl <<
00293 " m(ode) int - which mode to start up with" << endl <<
00294 " n(umber) int - player number in formation" << endl <<
00295 " o(utput) file - write log info to (screen is default)" << endl <<
00296 " p(ort) - port number to connect with" << endl <<
00297 " r(econnect) int - reconnect as player nr" << endl <<
00298 " s(erverconf) file - use file as server conf file" << endl <<
00299 " t(eamname) name - name of your team" << endl;
00300 }