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
00048 #include"BasicCoach.h"
00049 #include"Parse.h"
00050 #ifdef WIN32
00051 #include <windows.h>
00052 #else
00053 #include <sys/poll.h>
00054 #endif
00055
00056 extern Logger Log;
00067 BasicCoach::BasicCoach( ActHandler* act, WorldModel *wm, ServerSettings *ss,
00068 char* strTeamName, double dVersion, bool isTrainer )
00069
00070 {
00071 char str[MAX_MSG];
00072
00073 ACT = act;
00074 WM = wm;
00075 SS = ss;
00076 bContLoop = true;
00077 WM->setTeamName( strTeamName );
00078
00079 if( !isTrainer )
00080 sprintf( str, "(init %s (version %f))", strTeamName, dVersion );
00081 else
00082 sprintf( str, "(init (version %f))", dVersion );
00083
00084 ACT->sendMessage( str );
00085 }
00086
00087 BasicCoach::~BasicCoach( )
00088 {
00089 }
00090
00093 void BasicCoach::mainLoopNormal( )
00094 {
00095 #ifdef WIN32
00096 Sleep( 1000 );
00097 #else
00098 poll( 0, 0, 1000 );
00099 #endif
00100
00101 bool bSubstituted = false;
00102 ACT->sendMessageDirect( "(eye on)" );
00103
00104 #ifdef WIN32
00105 Sleep( 1000 );
00106 #else
00107 poll( 0, 0, 1000 );
00108 #endif
00109
00110 while( WM->getPlayMode() != PM_TIME_OVER && bContLoop )
00111 {
00112 Log.log( 1, "in loop %d %d %f",
00113 WM->getTimeLastSeeGlobalMessage().getTime(),
00114 bSubstituted,
00115 WM->isConfidenceGood( OBJECT_TEAMMATE_11 )) ;
00116 if( WM->waitForNewInformation() == false )
00117 {
00118 printf( "Shutting down coach\n" );
00119 bContLoop = false;
00120 }
00121 else if( WM->getTimeLastSeeGlobalMessage().getTime() == 0 &&
00122 bSubstituted == false &&
00123 WM->isConfidenceGood( OBJECT_TEAMMATE_11 ))
00124 {
00125
00126 for( int i = 0 ; i < MAX_HETERO_PLAYERS; i ++ )
00127 {
00128 m_player_types[i] = WM->getInfoHeteroPlayer( i );
00129
00130
00131 }
00132
00133
00134
00135 substitutePlayer( 2, 1 );
00136 substitutePlayer( 3, 1 );
00137 substitutePlayer( 4, 1 );
00138 substitutePlayer( 5, 2 );
00139 substitutePlayer( 6, 2 );
00140 substitutePlayer( 7, 2 );
00141 substitutePlayer( 8, 3 );
00142 substitutePlayer( 9, 3 );
00143 substitutePlayer( 10, 3 );
00144 substitutePlayer( 11, 4 );
00145 bSubstituted = true;
00146 }
00147
00148 if( Log.isInLogLevel( 456 ) )
00149 WM->logObjectInformation( 456, OBJECT_ILLEGAL);
00150 if( SS->getSynchMode() == true )
00151 ACT->sendMessageDirect( "(done)" );
00152 }
00153
00154 return;
00155 }
00156
00157
00160 void BasicCoach::substitutePlayer( int iPlayer, int iPlayerType )
00161 {
00162 SoccerCommand soc;
00163 soc.makeCommand( CMD_CHANGEPLAYER, (double)iPlayer, (double)iPlayerType );
00164 ACT->sendCommandDirect( soc );
00165 cerr << "coachmsg: changed player " << iPlayer << " to type " << iPlayerType
00166 << endl;
00167 }
00168
00169
00170 #ifdef WIN32
00171 DWORD WINAPI stdin_callback( LPVOID v )
00172 #else
00173 void* stdin_callback( void * v )
00174 #endif
00175 {
00176 Log.log( 1, "Starting to listen for user input" );
00177 BasicCoach* bc = (BasicCoach*)v;
00178 bc->handleStdin();
00179 return NULL;
00180 }
00181
00193 void BasicCoach::handleStdin( )
00194 {
00195 char buf[MAX_MSG];
00196
00197 while( bContLoop )
00198 {
00199 #ifdef WIN32
00200 cin.getline( buf, MAX_MSG );
00201 #else
00202 fgets( buf, MAX_MSG, stdin );
00203 #endif
00204 printf( "after fgets: %s\n", buf );
00205 executeStringCommand( buf );
00206 }
00207 }
00208
00214 void BasicCoach::showStringCommands( ostream& out )
00215 {
00216 out << "Basic commands:" << endl <<
00217 " m(ove) player_nr x y" << endl <<
00218 " q(uit)" << endl;
00219 }
00220
00225 bool BasicCoach::executeStringCommand( char *str)
00226 {
00227 switch( str[0] )
00228 {
00229 case 'm':
00230 sprintf( str, "(move %d %f %f)", Parse::parseFirstInt( &str ),
00231 Parse::parseFirstDouble( &str ),
00232 Parse::parseFirstDouble( &str ) );
00233 break;
00234 case 'q':
00235 bContLoop = false;
00236 return true;
00237 default:
00238 ;
00239 }
00240 printf( "send: %s\n", str );
00241 ACT->sendMessage( str );
00242 return true;
00243 }