Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

BasicCoach.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2000-2003, Jelle Kok, University of Amsterdam
00003 All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are met:
00007 
00008 1. Redistributions of source code must retain the above copyright notice, this
00009 list of conditions and the following disclaimer.
00010 
00011 2. Redistributions in binary form must reproduce the above copyright notice,
00012 this list of conditions and the following disclaimer in the documentation
00013 and/or other materials provided with the distribution.
00014 
00015 3. Neither the name of the University of Amsterdam nor the names of its
00016 contributors may be used to endorse or promote products derived from this
00017 software without specific prior written permission.
00018 
00019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00020 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
00023 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00024 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00025 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00026 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00027 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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       // read (and write) all player_type information 
00126       for( int i = 0 ; i < MAX_HETERO_PLAYERS; i ++ )
00127       {
00128          m_player_types[i] = WM->getInfoHeteroPlayer( i );
00129 //       cout << i << ": " ;
00130 //       m_player_types[i].show( cout );
00131       }
00132 
00133       // just substitute some players (define your own methods to
00134       // determine which player types should be substituted )
00135       substitutePlayer(  2, 1 );  // substitute player 2 to type 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 ); // does unblock with signal !!!!!
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':                               // move
00230       sprintf( str, "(move %d %f %f)", Parse::parseFirstInt( &str ),
00231                                        Parse::parseFirstDouble( &str ),
00232                                        Parse::parseFirstDouble( &str ) );
00233       break;
00234     case 'q':                             // quit
00235       bContLoop = false;
00236       return true;
00237     default:                             // default: send entered string
00238       ;
00239   }
00240   printf( "send: %s\n", str );
00241   ACT->sendMessage( str );
00242   return true;
00243 }

Generated on Fri Nov 7 11:45:39 2003 for UvA Trilearn 2003 Base Code by doxygen1.2.12 written by Dimitri van Heesch, © 1997-2001