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