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

WorldModel.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<stdio.h>    // needed for printf
00049 #include<errno.h>    // needed for ETIMEDOUT
00050 #ifdef WIN32
00051   #include<windows.h>
00052 #else
00053   #include<strings.h>  // needed for strcpy
00054   #include<pthread.h>  // needed for pthread_mutex_init
00055 #endif
00056 #include<string.h>   // needed for strcpy
00057 #include<math.h>     // needed for erf
00058 #include<map>        // needed for map
00059 #include"WorldModel.h"
00060 
00061 /*****************************************************************************/
00062 /********************** CLASS WORLDMODEL *************************************/
00063 /*****************************************************************************/
00064 
00070 WorldModel::WorldModel( ServerSettings *ss, PlayerSettings *ps, 
00071                         Formations *fs):agentObject(  )
00072 {
00073   dTotalVarVel   = 0.0;
00074   dTotalVarPos   = 0.0;
00075   SS             = ss;
00076   PS             = ps;
00077   formations     = fs;
00078   bNewInfo       = false;
00079 
00080   setSide         ( SIDE_ILLEGAL ); // is set by init message
00081   strTeamName[0] = '\0';
00082   setPlayMode     ( PM_BEFORE_KICK_OFF );
00083   iGoalDiff      = 0;
00084   m_sidePenalty = SIDE_ILLEGAL;
00085 
00086   int i;
00087   for( i = 0; i < MAX_TEAMMATES ; i ++ )
00088     Teammates[i].setType( SoccerTypes::getTeammateObjectFromIndex( i ) );
00089   for( i = 0; i < MAX_OPPONENTS ; i ++ )
00090     Opponents[i].setType( SoccerTypes::getOpponentObjectFromIndex( i ) );
00091   for( i = 0; i < MAX_OPPONENTS + MAX_TEAMMATES ; i ++ )
00092     UnknownPlayers[i].setType( OBJECT_ILLEGAL );
00093   for( i = 0; i < MAX_FLAGS; i ++ )
00094     Flags[i].setType( OBJECT_ILLEGAL );
00095   for( i = 0; i < MAX_LINES; i ++ )
00096     Lines[i].setType( OBJECT_ILLEGAL );
00097 
00098   iNrUnknownPlayers     = 0;
00099 
00100   Ball.setType             ( OBJECT_BALL    );
00101   agentObject.setType      ( OBJECT_ILLEGAL );
00102   agentObject.setStamina   ( Stamina(SS->getStaminaMax(),1.0,1.0) );
00103 
00104   for( i = 0 ; i < CMD_MAX_COMMANDS ; i ++ )
00105   {
00106     queuedCommands[i].commandType = (CommandT)i;
00107     performedCommands[i]          = false;
00108     iCommandCounters[i]           = 0;
00109   }
00110 
00111   iNrHoles              = 0;
00112   iNrOpponentsSeen      = 0;
00113   iNrTeammatesSeen      = 0;
00114   bsCheckBall           = BS_ILLEGAL;
00115 
00116   // initialize the mutex for bNewInfo
00117 #ifdef WIN32
00118   InitializeCriticalSection( &mutex_newInfo );
00119   event_newInfo = CreateEvent( NULL, TRUE, FALSE, NULL );
00120 #else
00121   pthread_mutex_init( &mutex_newInfo, NULL );
00122   pthread_cond_init ( &cond_newInfo,  NULL );
00123 #endif
00124   m_bRecvThink          = false;
00125   timeLastSenseMessage  = Time( 0, 1 );
00126 
00127   strcpy( m_colorPlayers[0], "ffb0b0" );   // pink
00128   strcpy( m_colorPlayers[1], "ffb000" );   // orange
00129   strcpy( m_colorPlayers[2], "00b000" );   // darkgreen
00130   strcpy( m_colorPlayers[3], "00ffff" );   // lightblue
00131   strcpy( m_colorPlayers[4], "ff00ff" );   // purple
00132   strcpy( m_colorPlayers[5], "ffff00" );   // yellow
00133   strcpy( m_colorPlayers[6], "ffffff" );   // white
00134   strcpy( m_colorPlayers[7], "0000ff" );   // blue
00135   strcpy( m_colorPlayers[8], "00ff00" );   // green
00136   strcpy( m_colorPlayers[9], "ff0000" );   // red
00137   strcpy( m_colorPlayers[10],"aaaaaa" );   // gray
00138 
00139   for( i = 0 ; i < MAX_FEATURES ; i ++ )
00140   {
00141     m_features[i].setTimeSee( Time( UnknownTime, 0 ) );
00142     m_features[i].setTimeSense( Time( UnknownTime, 0 ) );
00143   }
00144 }
00145 
00147 WorldModel::~WorldModel()
00148 {
00149 #ifdef WIN32
00150   DeleteCriticalSection( &mutex_newInfo );
00151 #endif
00152 }
00153 
00158 Object* WorldModel::getObjectPtrFromType( ObjectT o )
00159 {
00160   Object *object = NULL;
00161   if( o == OBJECT_ILLEGAL )
00162     return NULL;
00163 
00164   if( SoccerTypes::isKnownPlayer( o ) )
00165   {
00166     if( o == agentObject.getType() )
00167       object = &agentObject;
00168     else if( SoccerTypes::isTeammate( o ) )
00169       object = &Teammates[SoccerTypes::getIndex(o)];
00170     else
00171       object = &Opponents[SoccerTypes::getIndex(o)];
00172   }
00173   else if( SoccerTypes::isFlag( o ) )
00174     object = &Flags[SoccerTypes::getIndex(o)];
00175   else if( SoccerTypes::isLine( o ) )
00176     object = &Lines[SoccerTypes::getIndex(o)];
00177   else if( SoccerTypes::isBall( o ) )
00178     object = &Ball;
00179   else if( o == OBJECT_OPPONENT_GOALIE )
00180     return getObjectPtrFromType( getOppGoalieType() );
00181   else if( o == OBJECT_TEAMMATE_GOALIE )
00182     return getObjectPtrFromType( getOwnGoalieType() );
00183  return object;
00184 }
00185 
00191 void WorldModel::setTimeLastCatch( Time time )
00192 {
00193   timeLastCatch = time;
00194 }
00195 
00198 int WorldModel::getTimeSinceLastCatch()
00199 {
00200   if( timeLastCatch.getTime() == -1 )
00201     return 1000;
00202   return timeLastSenseMessage - timeLastCatch;
00203 }
00204 
00208 bool WorldModel::setTimeLastRefereeMessage( Time time )
00209 {
00210   timeLastRefMessage = time;
00211   return true;
00212 }
00213 
00216 Time WorldModel::getTimeLastRefereeMessage( )
00217 {
00218   return timeLastRefMessage;
00219 }
00220 
00225 Time WorldModel::getCurrentTime()
00226 {
00227   if( getPlayerNumber() == 0 )
00228     return getTimeLastSeeGlobalMessage();
00229   else
00230     return getTimeLastRecvSenseMessage();
00231 }
00232 
00237 int WorldModel::getCurrentCycle()
00238 {
00239   return getCurrentTime().getTime();
00240 }
00241 
00242 
00248 bool WorldModel::isTimeStopped()
00249 {
00250   return getCurrentTime().isStopped();
00251 }
00252 
00255 bool WorldModel::isLastMessageSee() const
00256 {
00257   return getTimeLastSeeMessage() == getTimeLastSenseMessage() ;
00258 }
00259 
00263 Time WorldModel::getTimeLastSeeGlobalMessage( ) const
00264 {
00265   return getTimeLastRecvSeeMessage();
00266 }
00267 
00271 bool WorldModel::setTimeLastSeeGlobalMessage( Time time )
00272 {
00273   updateRelativeFromGlobal( );
00274   return setTimeLastSeeMessage( time ); // set see message
00275 }
00276 
00279 Time WorldModel::getTimeLastSeeMessage( ) const
00280 {
00281   return timeLastSeeMessage;
00282 }
00283 
00289 Time WorldModel::getTimeLastRecvSeeMessage( ) const
00290 {
00291   return timeLastRecvSeeMessage ;
00292 }
00300 bool WorldModel::setTimeLastSeeMessage( Time time )
00301 {
00302   timeLastRecvSeeMessage  = time;
00303   if( SS->getSynchMode() == false )
00304   {
00305 #ifdef WIN32
00306     //EnterCriticalSection( &mutex_newInfo );
00307     bNewInfo            = true;
00308     SetEvent            (  event_newInfo );
00309     //LeaveCriticalSection( &mutex_newInfo );
00310 #else
00311     pthread_mutex_lock  ( &mutex_newInfo );
00312     bNewInfo            = true;
00313     pthread_cond_signal ( &cond_newInfo );
00314     pthread_mutex_unlock( &mutex_newInfo );
00315 #endif
00316   }
00317 
00318   return true;
00319 }
00320 
00323 Time WorldModel::getTimeLastSenseMessage( ) const
00324 {
00325   return timeLastSenseMessage ;
00326 }
00327 
00333 Time WorldModel::getTimeLastRecvSenseMessage( ) const
00334 {
00335   return timeLastRecvSenseMessage ;
00336 }
00337 
00338   
00345 bool WorldModel::setTimeLastSenseMessage( Time time )
00346 {
00347   timeLastRecvSenseMessage = time;
00348   if( SS->getSynchMode() == false )
00349   {
00350 #ifdef WIN32
00351     //EnterCriticalSection( &mutex_newInfo );
00352     bNewInfo = true;
00353     SetEvent            (  event_newInfo );
00354   //LeaveCriticalSection( &mutex_newInfo );
00355 #else
00356     pthread_mutex_lock  ( &mutex_newInfo );
00357     bNewInfo = true;
00358     pthread_cond_signal ( &cond_newInfo );
00359     pthread_mutex_unlock( &mutex_newInfo );
00360 #endif
00361   }
00362 
00363   return true;
00364 }
00365 
00368 Time WorldModel::getTimeLastHearMessage( ) const
00369 {
00370   return timeLastHearMessage ;
00371 }
00372 
00376 bool WorldModel::setTimeLastHearMessage( Time time )
00377 {
00378   timeLastHearMessage = time;
00379   return true;
00380 }
00381 
00385 int WorldModel::getPlayerNumber( ) const
00386 {
00387   return iPlayerNumber;
00388 }
00389 
00394 bool WorldModel::setPlayerNumber( int i )
00395 {
00396   iPlayerNumber = i;
00397   return true;
00398 }
00399 
00403 SideT WorldModel::getSide( ) const
00404 {
00405   return sideSide;
00406 }
00407 
00411 bool WorldModel::setSide( SideT s )
00412 {
00413   sideSide = s;
00414   m_iMultX = (getSide() == SIDE_LEFT ) ?  1 : -1 ;    // set the draw info
00415   m_iMultY = (getSide() == SIDE_LEFT ) ? -1 :  1 ;    // from Logger.C
00416   return true;
00417 }
00418 
00421 const char* WorldModel::getTeamName( ) const
00422 {
00423   return strTeamName ;
00424 }
00425 
00430 bool WorldModel::setTeamName( char * str )
00431 {
00432   strcpy( strTeamName, str );
00433   return true;
00434 }
00435 
00440 PlayModeT WorldModel::getPlayMode( ) const
00441 {
00442   return playMode ;
00443 }
00444 
00448 bool WorldModel::setPlayMode( PlayModeT pm )
00449 {
00450   playMode = pm;
00451   if( ( pm == PM_GOAL_KICK_LEFT  && getSide() == SIDE_LEFT  ) ||
00452       ( pm == PM_GOAL_KICK_RIGHT && getSide() == SIDE_RIGHT )    )
00453     setTimeLastCatch( getTimeLastSenseMessage() );
00454   return true;
00455 }
00456 
00461 int WorldModel::getGoalDiff( ) const
00462 {
00463   return iGoalDiff;
00464 }
00465 
00469 int WorldModel::addOneToGoalDiff( )
00470 {
00471   return ++iGoalDiff;
00472 }
00473 
00477 int WorldModel::subtractOneFromGoalDiff()
00478 {
00479   return --iGoalDiff;
00480 }
00481 
00486 int WorldModel::getNrOfCommands( CommandT c ) const
00487 {
00488   return iCommandCounters[ (int) c ];
00489 }
00490 
00500 bool WorldModel::setNrOfCommands( CommandT c, int i )
00501 {
00502   int iIndex = (int) c;
00503 
00504   // if counter is the same as before, no command is performed, otherwise it is
00505   performedCommands[iIndex] = ( iCommandCounters[iIndex] == i ) ? false : true;
00506   iCommandCounters [iIndex] = i;
00507   return true;
00508 }
00509 
00513 Time WorldModel::getTimeCheckBall( ) const
00514 {
00515   return timeCheckBall;
00516 }
00517 
00522 bool WorldModel::setTimeCheckBall( Time time )
00523 {
00524   timeCheckBall = time;
00525   return true;
00526 }
00527 
00532 BallStatusT WorldModel::getCheckBallStatus( ) const
00533 {
00534   return bsCheckBall;
00535 }
00536 
00541 bool WorldModel::setCheckBallStatus( BallStatusT bs )
00542 {
00543   bsCheckBall = bs;
00544   return true;
00545 }
00546 
00549 bool WorldModel::getRecvThink( )
00550 {
00551   return m_bRecvThink;
00552 }
00553 
00558 char* WorldModel::getCommunicationString( )
00559 {
00560   return m_strCommunicate;
00561 }
00562 
00563 
00567 ObjectT WorldModel::getObjectFocus( )
00568 {
00569   return m_objFocus;
00570 }
00571 
00572 
00576 bool WorldModel::setObjectFocus( ObjectT obj )
00577 {
00578   m_objFocus = obj;
00579   return true;
00580 }
00581 
00582 
00587 bool WorldModel::setCommunicationString( char *str )
00588 {
00589   strncpy( m_strCommunicate, str, MAX_SAY_MSG );
00590   return true;
00591 }
00592 
00593 
00602 ObjectT WorldModel::iterateObjectStart(int& iIndex,ObjectSetT g,double dConf,
00603                bool bForward)
00604 {
00605   iIndex = -1;
00606   return iterateObjectNext( iIndex, g, dConf, bForward );
00607 }
00608 
00617 ObjectT WorldModel::iterateObjectNext(int& iIndex,ObjectSetT g, double dConf,
00618               bool bForward)
00619 {
00620   ObjectT o, objGoalie = OBJECT_TEAMMATE_1;
00621   bool bContinue = true;
00622   
00623   if( g == OBJECT_SET_TEAMMATES_NO_GOALIE )
00624     objGoalie = getOwnGoalieType();
00625     
00626   if( iIndex < 0 )
00627     iIndex = (bForward==false) ? OBJECT_MAX_OBJECTS : -1 ;
00628 
00629   // when dConf is not specified it has the default value of -1.0, in this
00630   // case set it to the confidence threshold defined in PlayerSetting, but
00631   // only do this for dynamic objexts, not for flags, lines, etc. since all
00632   // should be returend.
00633   if( dConf == -1.0 && (g==OBJECT_SET_OPPONENTS || g==OBJECT_SET_PLAYERS ||
00634                         g==OBJECT_SET_TEAMMATES) )
00635     dConf = PS->getPlayerConfThr();
00636 
00637   int i = 0;
00638   if( bForward == true )
00639     i = iIndex + 1;
00640   else
00641     i = iIndex - 1;
00642   bContinue = ( bForward == false ) ? ( i >= 0 ) : ( i < OBJECT_MAX_OBJECTS ); 
00643 
00644   while( bContinue )
00645   {
00646     o = (ObjectT) i;
00647     if( SoccerTypes::isInSet( o, g, objGoalie ) )
00648     {
00649       if( getConfidence( o ) >= dConf )
00650       {
00651         iIndex = i;
00652         return o;
00653       }
00654       else if( dConf == 1.0 && getTimeLastSeeMessage() == getTimeLastSeen( o ))
00655       {
00656         iIndex = i; // confidence of 1.0 can only be in same cycle as see
00657         return o;   // message. Therefore first test should succeed normally;
00658       }             // in cases where this method is called after see message,
00659                     // but new sense has already arrived, confidence is lowered
00660     }               // but we want to return object that was seen in last see
00661                     // message; this compensates for those cases.
00662     if( bForward == true )
00663       i++;
00664     else
00665       i--;
00666     bContinue = ( bForward == false ) ? ( i >= 0 ) :( i < OBJECT_MAX_OBJECTS); 
00667   }
00668 
00669   return OBJECT_ILLEGAL;
00670 }
00671 
00676 void WorldModel::iterateObjectDone( int &iIndex )
00677 {
00678   iIndex = -1;
00679 }
00680 
00684 ObjectT WorldModel::getAgentObjectType( ) const
00685 {
00686   return agentObject.getType();
00687 }
00688 
00689 int WorldModel::getAgentIndex( ) const
00690 {
00691   return SoccerTypes::getIndex( getAgentObjectType() );
00692 }
00693 
00698 bool WorldModel::setAgentObjectType( ObjectT o )
00699 {
00700   agentObject.setType( o );
00701   return true;
00702 }
00703 
00706 AngDeg WorldModel::getAgentBodyAngleRelToNeck( ) const
00707 {
00708   return agentObject.getBodyAngleRelToNeck();
00709 }
00710 
00713 AngDeg WorldModel::getAgentGlobalNeckAngle(  ) const
00714 {
00715   return agentObject.getGlobalNeckAngle(  );
00716 }
00717 
00720 AngDeg WorldModel::getAgentGlobalBodyAngle(  )
00721 {
00722   return agentObject.getGlobalBodyAngle(  );
00723 }
00724 
00727 Stamina WorldModel::getAgentStamina( ) const
00728 {
00729   return agentObject.getStamina();
00730 }
00731 
00735 TiredNessT WorldModel::getAgentTiredNess( ) const
00736 {
00737   return getAgentStamina().getTiredNess(
00738                     SS->getRecoverDecThr(), SS->getStaminaMax() );
00739 }
00740 
00743 double WorldModel::getAgentEffort( ) const
00744 {
00745   return agentObject.getStamina().getEffort();
00746 }
00747 
00750 VecPosition WorldModel::getAgentGlobalVelocity( ) const
00751 {
00752   return agentObject.getGlobalVelocity();
00753 }
00754 
00757 double WorldModel::getAgentSpeed( ) const
00758 {
00759   return agentObject.getSpeed();
00760 }
00761 
00764 VecPosition WorldModel::getAgentGlobalPosition( ) const
00765 {
00766   return agentObject.getGlobalPosition();
00767 }
00768 
00771 bool WorldModel::setAgentViewAngle( ViewAngleT va ) 
00772 {
00773   agentObject.setViewAngle( va );
00774   return true;
00775 }
00776 
00777 
00782 ViewAngleT WorldModel::getAgentViewAngle( ) const
00783 {
00784   return agentObject.getViewAngle();
00785 }
00786 
00789 bool WorldModel::setAgentViewQuality( ViewQualityT vq ) 
00790 {
00791   agentObject.setViewQuality( vq );
00792   return true;
00793 }
00796 ViewQualityT WorldModel::getAgentViewQuality( ) const
00797 {
00798   return agentObject.getViewQuality();
00799 }
00800 
00805 double WorldModel::getAgentViewFrequency( ViewAngleT va, ViewQualityT vq )
00806 {
00807   double dViewQualityFactor ;
00808   double dViewWidthFactor   ;
00809 
00810   if( va == VA_ILLEGAL )
00811     va = getAgentViewAngle();
00812   if( vq == VQ_ILLEGAL )
00813     vq = getAgentViewQuality();
00814 
00815   switch( va )
00816   {
00817     case VA_NARROW:  dViewWidthFactor   = 0.5; break;
00818     case VA_NORMAL:  dViewWidthFactor   = 1.0; break;
00819     case VA_WIDE:    dViewWidthFactor   = 2.0; break;
00820     case VA_ILLEGAL:
00821     default:         dViewWidthFactor   = 0.0; break;
00822   }
00823 
00824   switch( vq )
00825   {
00826     case VQ_LOW:     dViewQualityFactor = 0.5; break;
00827     case VQ_HIGH:    dViewQualityFactor = 1.0; break;
00828     case VQ_ILLEGAL:
00829     default:         dViewQualityFactor = 0.0; break;
00830   }
00831 
00832   return dViewQualityFactor*dViewWidthFactor;
00833 }
00834 
00836 bool WorldModel::getAgentArmMovable( )
00837 {
00838   return agentObject.getArmMovable();
00839 }
00840 
00843 VecPosition WorldModel::getAgentArmPosition( )
00844 {
00845   return agentObject.getGlobalArmPosition();
00846 }
00847 
00850 int WorldModel::getAgentArmExpires( )
00851 {
00852   return agentObject.getArmExpires();
00853 }
00854 
00859 VecPosition  WorldModel::getBallPos()
00860 {
00861   return getGlobalPosition( OBJECT_BALL );
00862 }
00863 
00866 double WorldModel::getBallSpeed()
00867 {
00868   return Ball.getGlobalVelocity().getMagnitude();
00869 }
00870 
00873 AngDeg WorldModel::getBallDirection()
00874 {
00875   return Ball.getGlobalVelocity().getDirection();
00876 }
00877 
00882 Time WorldModel::getTimeGlobalPosition( ObjectT o )
00883 {
00884   PlayerObject *object = (PlayerObject*) getObjectPtrFromType( o );
00885   if( object != NULL )
00886     return object->getTimeGlobalPosition();
00887   return UnknownTime;
00888 }
00889 
00890 
00899 VecPosition WorldModel::getGlobalPosition( ObjectT o )
00900 {
00901   Object *object = getObjectPtrFromType( o );
00902   if( object != NULL )
00903   {
00904     if( SoccerTypes::isFlag( o ) || SoccerTypes::isGoal( o ) )
00905       return SoccerTypes::getGlobalPositionFlag( o, getSide(),
00906                                                  SS->getGoalWidth() );
00907     else
00908       return object->getGlobalPosition();
00909   }
00910   return VecPosition( UnknownDoubleValue, UnknownDoubleValue);
00911 }
00912 
00917 Time WorldModel::getTimeGlobalVelocity( ObjectT o )
00918 {
00919   PlayerObject *object = (PlayerObject*) getObjectPtrFromType( o );
00920   if( object != NULL )
00921     return object->getTimeGlobalVelocity();
00922   return UnknownTime;
00923 }
00924 
00925 
00931 VecPosition WorldModel::getGlobalVelocity( ObjectT o )
00932 {
00933   DynamicObject *object = (DynamicObject*)getObjectPtrFromType( o );
00934   if( object != NULL )
00935     return object->getGlobalVelocity(  );
00936   return VecPosition( UnknownDoubleValue, UnknownDoubleValue );
00937 
00938 }
00939 
00945 double WorldModel::getRelativeDistance( ObjectT o )
00946 {
00947   Object *object = getObjectPtrFromType( o );
00948   if( object != NULL )
00949     return object->getRelativeDistance();
00950   return UnknownDoubleValue;
00951 }
00952 
00958 VecPosition  WorldModel::getRelativePosition( ObjectT o )
00959 {
00960   Object *object = getObjectPtrFromType( o );
00961   if( object != NULL )
00962     return object->getRelativePosition();
00963   return VecPosition(UnknownDoubleValue, UnknownDoubleValue);
00964 }
00965 
00976 AngDeg WorldModel::getRelativeAngle( ObjectT o, bool bWithBody )
00977 {
00978   Object *object = getObjectPtrFromType( o );
00979   double dBody   = 0.0;
00980 
00981   if( object != NULL )
00982   {
00983     if( bWithBody == true )
00984       dBody = getAgentBodyAngleRelToNeck();
00985     return VecPosition::normalizeAngle( object->getRelativeAngle() - dBody );
00986   }
00987   return UnknownDoubleValue;
00988 }
00989 
00995 Time WorldModel::getTimeGlobalAngles( ObjectT o )
00996 {
00997   PlayerObject *object = (PlayerObject*) getObjectPtrFromType( o );
00998   if( object != NULL )
00999     return object->getTimeGlobalAngles();
01000   return Time( -1, 0);
01001 }
01002 
01008 AngDeg WorldModel::getGlobalBodyAngle( ObjectT o )
01009 {
01010   PlayerObject *object = (PlayerObject*) getObjectPtrFromType( o );
01011   if( object != NULL )
01012     return object->getGlobalBodyAngle();
01013   return UnknownAngleValue;
01014 }
01015 
01021 AngDeg WorldModel::getGlobalNeckAngle( ObjectT o )
01022 {
01023   PlayerObject *object = (PlayerObject*) getObjectPtrFromType( o );
01024   if( object != NULL )
01025     return object->getGlobalNeckAngle();
01026   return UnknownAngleValue;
01027 }
01028 
01033 AngDeg WorldModel::getGlobalAngle( ObjectT o )
01034 {
01035   if( SoccerTypes::isLine( o ) )
01036     return SoccerTypes::getGlobalAngleLine( o, getSide() );
01037   return UnknownAngleValue;
01038 }
01039 
01040 
01046 double WorldModel::getConfidence( ObjectT o)
01047 {
01048   Object *object = getObjectPtrFromType( o );
01049   if( object != NULL )
01050     return object->getConfidence( getCurrentTime() );
01051 
01052   return 0.0;
01053 }
01054 
01062 bool WorldModel::isKnownPlayer( ObjectT o )
01063 {
01064   PlayerObject *object = (PlayerObject *)getObjectPtrFromType( o );
01065   if( object != NULL  )
01066     return object->getIsKnownPlayer();
01067   return false;
01068 
01069 }
01070 
01081 ObjectT WorldModel::getOppGoalieType()
01082 {
01083   static ObjectT objGoalieType = OBJECT_ILLEGAL;
01084 
01085   if( objGoalieType != OBJECT_ILLEGAL &&
01086       isConfidenceGood( objGoalieType ) &&
01087       isKnownPlayer( objGoalieType ) )
01088     return objGoalieType;
01089 
01090   ObjectT objOppMaxX = OBJECT_ILLEGAL;
01091   double  x = -100.0, y = UnknownDoubleValue;
01092 
01093   for( int i = 0; i < MAX_OPPONENTS; i++ )
01094   {
01095     if( isConfidenceGood( Opponents[i].getType( ) ) )
01096     {
01097       if( Opponents[i].getIsGoalie() == true ) // &&
01098 //           Opponents[i].getGlobalPosition().getX() > PENALTY_X - 2.0 )
01099       {
01100         objGoalieType = Opponents[i].getType();
01101         return Opponents[i].getType();
01102       }
01103       if( Opponents[i].getGlobalPosition().getX() > x )
01104       {
01105         x          = Opponents[i].getGlobalPosition().getX();
01106         y          = Opponents[i].getGlobalPosition().getY();
01107         objOppMaxX = Opponents[i].getType();
01108       }
01109     }
01110   }
01111 
01112   // if opponent with highest x is nr 1, assume it is goalkeeper when standing
01113   // in own penalty area, otherwise assume goalkeeper closest player to goal.
01114   if( (objOppMaxX == OBJECT_OPPONENT_1 && x > PENALTY_X + 4.0 ) ||
01115       (objOppMaxX != OBJECT_ILLEGAL    && x > PITCH_LENGTH/2.0 - 6.0 &&
01116        fabs( y ) < SS->getGoalWidth()/2.0 ))
01117     return objOppMaxX;
01118   return OBJECT_ILLEGAL;
01119 }
01120 
01121 
01130 ObjectT WorldModel::getOwnGoalieType()
01131 {
01132   ObjectT objOwnMinX = OBJECT_ILLEGAL;
01133   double x = 100.0, y = UnknownDoubleValue;
01134   for( int i = 0; i < MAX_TEAMMATES; i++ )
01135   {
01136     if( isConfidenceGood( Teammates[i].getType( ) ) )
01137     {
01138       if( Teammates[i].getIsGoalie() == true )
01139         return Teammates[i].getType();
01140       if( Teammates[i].getGlobalPosition().getX() < x )
01141       {
01142         x          = Teammates[i].getGlobalPosition().getX();
01143         y          = Teammates[i].getGlobalPosition().getY();
01144         objOwnMinX = Teammates[i].getType();
01145       }
01146     }
01147   }
01148   if( ( objOwnMinX == OBJECT_TEAMMATE_1 && x < - ( PENALTY_X + 4.0 ) ) ||
01149       (objOwnMinX != OBJECT_ILLEGAL    && x < -  PITCH_LENGTH/2.0 + 6.0  &&
01150        fabs( y ) < SS->getGoalWidth()/2.0 ))
01151     return objOwnMinX;
01152   return OBJECT_ILLEGAL;
01153 }
01154 
01155 
01160 Time WorldModel::getTimeLastSeen( ObjectT o )
01161 {
01162   Object *object = getObjectPtrFromType( o );
01163   if( object != NULL )
01164     return object->getTimeLastSeen(  );
01165   return Time( -1, 0);
01166 
01167 }
01168 
01174 Time WorldModel::getTimeChangeInformation( ObjectT o )
01175 {
01176   DynamicObject *object = (DynamicObject*)getObjectPtrFromType( o );
01177   if( object != NULL )
01178     return object->getTimeChangeInformation(  );
01179   return Time( -1, 0);
01180 
01181 }
01182 
01187 VecPosition WorldModel::getGlobalPositionLastSee( ObjectT o )
01188 {
01189   DynamicObject *object = (DynamicObject*)getObjectPtrFromType( o );
01190   if( object != NULL )
01191     return object->getGlobalPositionLastSee(  );
01192   return VecPosition( UnknownDoubleValue, UnknownDoubleValue);
01193 }
01194              
01200 Time WorldModel::getTimeGlobalPositionLastSee( ObjectT o )
01201 {
01202   DynamicObject *object = (DynamicObject*)getObjectPtrFromType( o );
01203   if( object != NULL )
01204     return object->getTimeGlobalPosDerivedFromSee(  );
01205   return UnknownTime;
01206 }
01207 
01212 VecPosition WorldModel::getGlobalVelocityLastSee( ObjectT o )
01213 {
01214   DynamicObject *object = (DynamicObject*)getObjectPtrFromType( o );
01215   if( object != NULL )
01216     return object->getGlobalVelocityLastSee(  );
01217   return VecPosition( UnknownDoubleValue, UnknownDoubleValue);
01218 }
01219 
01224 AngDeg WorldModel::getGlobalBodyAngleLastSee( ObjectT o )
01225 {
01226   PlayerObject *object = (PlayerObject*)getObjectPtrFromType( o );
01227   if( object != NULL )
01228     return object->getGlobalBodyAngleLastSee(  );
01229   return UnknownAngleValue;
01230 }
01231 
01235 int WorldModel::getTackleExpires( ObjectT o )
01236 {
01237   if( o == OBJECT_ILLEGAL || o == getAgentObjectType() )
01238     return agentObject.getTackleExpires();
01239 
01240   PlayerObject *object = (PlayerObject*)getObjectPtrFromType( o );
01241   if( object == NULL )
01242     return 0;
01243   return max(0, object->getTimeTackle( ) -
01244                 getCurrentTime()         +
01245                 SS->getTackleCycles());
01246 }
01247 
01251 AngDeg WorldModel::getGlobalArmDirection( ObjectT o )
01252 {
01253   PlayerObject *object = (PlayerObject*)getObjectPtrFromType( o );
01254   if( object == NULL )
01255     return UnknownAngleValue;
01256 
01257   return object->getGlobalArm();
01258 }
01259 
01263 Time  WorldModel::getTimeGlobalArmDirection( ObjectT o )
01264 {
01265   PlayerObject *object = (PlayerObject*)getObjectPtrFromType( o );
01266   if( object == NULL )
01267     return Time(-1,0);
01268 
01269   return object->getTimeGlobalArm();
01270 }
01271 
01277 double WorldModel::getProbTackleSucceeds( ObjectT o, int iExtraCycles,
01278                                           VecPosition *pos)
01279 {
01280   if( o == OBJECT_ILLEGAL )
01281     o = getAgentObjectType();
01282      
01283   VecPosition posObject   = getGlobalPosition( o );
01284   VecPosition posBall     = (pos == NULL ) ? getBallPos() : *pos ;
01285   AngDeg      angBody     = getGlobalBodyAngle( o );
01286   int         iExtra      = 0;
01287   double      dTackleDist, dDist=0;
01288 
01289   // if opponent goalie is within 3 metres he can probably catch in next cycle
01290   // RC2003 HACK
01291   if( o == getOppGoalieType() &&
01292       posBall.getDistanceTo( o ) < 3.0  )
01293     return 1.0;
01294 
01295   if( o != getAgentObjectType() )
01296   {
01297     // get the number of cycles object was not seen and assume he moves 0.6
01298     // in every cycle. Only in case of bad body direction subtract one.
01299     // then move object position closer to the ball
01300     dDist  = posBall.getDistanceTo( posObject );
01301     iExtra = getCurrentTime() - getTimeLastSeen( o ) + iExtraCycles;
01302     AngDeg ang    = (posBall - posObject).getDirection();   
01303 
01304     // if body angle ok, 
01305     if( getCurrentTime() - getTimeGlobalAngles( o ) < 2 )
01306     {
01307       if( fabs( VecPosition::normalizeAngle( ang - angBody ) ) > 35 )
01308   iExtra --;
01309       if( getGlobalVelocity( o ).getMagnitude() < 0.2 )
01310   iExtra --;
01311     }
01312 
01313     double dExtra = 0.7*max( 0, iExtra );
01314 
01315     // if object was not seen in last see message, he stood further away
01316     // then the visible_distance. 
01317     if( getTimeLastSeen( o ) != getTimeLastSeeMessage() &&
01318         getCurrentTime() == getTimeLastSeeMessage() &&
01319         dDist - dExtra < SS->getVisibleDistance() )
01320     {
01321       Log.log( 560, "prob tackle succeeds: opp not seen raise dExtra" );
01322       dExtra = dDist - SS->getVisibleDistance();
01323     }
01324 
01325     // now incorporate that to kick the ball we may need more cycles         
01326     //    dExtra = max( 0, dExtra + iExtraCycles );    
01327 
01328     // do not move object more than 4.0 metres.
01329     posObject += VecPosition( min(4.0,min(dDist - 0.2, dExtra )), ang, POLAR );
01330 
01331     // object is directed towards ball
01332     angBody = ang;
01333   }
01334 
01335   VecPosition posBallRel  = posBall - posObject; 
01336   posBallRel.rotate( - angBody ); 
01337   if ( posBallRel.getX() > 0.0 )      // ball in front -> tackle_dist parameter
01338     dTackleDist = SS->getTackleDist(); 
01339   else
01340     dTackleDist = SS->getTackleBackDist();
01341    
01342   double dProb = 
01343      pow(fabs(posBallRel.getX())/dTackleDist         ,SS->getTackleExponent())+
01344      pow(fabs(posBallRel.getY())/SS->getTackleWidth(),SS->getTackleExponent());
01345   
01346   Log.log( 556, 
01347            "tackle relpos o %d: (%f,%f) dist %f body %f extra %d %d: prob %f",
01348            o, posBallRel.getX(),posBallRel.getY(), dDist, angBody, iExtra, 
01349            iExtraCycles, max(0,1-dProb) );
01350   
01351   return max( 0, 1 - dProb );
01352 }
01353 
01356 list<ObjectT> WorldModel::getListCloseOpponents( VecPosition pos,double dDist )
01357 {
01358   int iIndex;
01359   list<ObjectT> listOpp;
01360 
01361   for( ObjectT o = iterateObjectStart( iIndex, OBJECT_SET_OPPONENTS );
01362        o != OBJECT_ILLEGAL;
01363        o = iterateObjectNext ( iIndex, OBJECT_SET_OPPONENTS  ) )
01364   {
01365     if( getGlobalPosition( o ).getDistanceTo( pos ) < dDist )
01366       listOpp.push_back( o );
01367   }
01368   iterateObjectDone( iIndex );
01369   return listOpp;
01370 }
01371 
01374 double WorldModel::getProbTackleClosestOpp( int iExtraCycles )
01375 {
01376   ObjectT obj = getClosestInSetTo( OBJECT_SET_OPPONENTS, OBJECT_BALL );
01377   if( obj == OBJECT_ILLEGAL )
01378     return -1.0;
01379   
01380   return getProbTackleSucceeds( obj, iExtraCycles );
01381 }
01382 
01393 bool WorldModel::setIsKnownPlayer( ObjectT o, bool isKnownPlayer )
01394 {
01395   PlayerObject *object = (PlayerObject*) getObjectPtrFromType( o );
01396   return object->setIsKnownPlayer( isKnownPlayer );
01397 }
01398 
01403 bool WorldModel::setTimeLastSeen( ObjectT o, Time time )
01404 {
01405   PlayerObject *object = (PlayerObject*) getObjectPtrFromType( o );
01406   return object->setTimeLastSeen( time );
01407 }
01408 
01413 bool WorldModel::setHeteroPlayerType( ObjectT  o, int iPlayerType )
01414 {
01415   PlayerObject *object = (PlayerObject*) getObjectPtrFromType( o );
01416   if( o == getAgentObjectType() )
01417     updateSSToHeteroPlayerType( iPlayerType );
01418 
01419   return object->setHeteroPlayerType( iPlayerType );
01420 }
01421 
01424 PlayerT WorldModel::getPlayerType ( ObjectT o )
01425 {
01426   if( o == OBJECT_ILLEGAL )
01427     o = getAgentObjectType();
01428   return formations->getPlayerType( SoccerTypes::getIndex( o ) );
01429 }
01430 
01433 bool WorldModel::isInPlayerSet( ObjectT o, PlayerSetT ps )
01434 {
01435   return SoccerTypes::isPlayerTypeInSet( getPlayerType( o ), ps );
01436 }
01437 
01438 
01442 int WorldModel::getHeteroPlayerType( ObjectT obj )
01443 {
01444   PlayerObject *object = (PlayerObject*) getObjectPtrFromType( obj );
01445 
01446   return object->getHeteroPlayerType( );
01447 
01448 }
01449 
01450 
01453 VecPosition WorldModel::getPosOpponentGoal( )
01454 {
01455   ObjectT objGoal = SoccerTypes::getGoalOpponent( getSide() );
01456   if( isPenaltyUs() || isPenaltyThem() )
01457     objGoal = ( getSidePenalty() == SIDE_LEFT ) ?OBJECT_GOAL_L:OBJECT_GOAL_R ;
01458   return SoccerTypes::getGlobalPositionFlag(
01459                         objGoal,
01460                         getSide( ),
01461                         SS->getGoalWidth() );
01462 }
01463 
01466 VecPosition WorldModel::getPosOwnGoal( )
01467 {
01468   SideT sideGoal = getSide();
01469   ObjectT objGoal = SoccerTypes::getOwnGoal( sideGoal );
01470   if( isPenaltyUs() || isPenaltyThem() ) 
01471     objGoal = (getSidePenalty() == SIDE_LEFT ) ? OBJECT_GOAL_L : OBJECT_GOAL_R;
01472 
01473   return SoccerTypes::getGlobalPositionFlag(
01474                         objGoal,
01475                         getSide( ),
01476                         SS->getGoalWidth() );
01477 }
01478 
01481 double  WorldModel::getRelDistanceOpponentGoal()
01482 {
01483   VecPosition posGoal = getPosOpponentGoal();
01484   return getAgentGlobalPosition().getDistanceTo( posGoal );
01485 
01486 }
01487 
01494 double  WorldModel::getRelAngleOpponentGoal()
01495 {
01496   VecPosition posGoal;
01497   if( sideSide == SIDE_LEFT )
01498     posGoal = SoccerTypes::getGlobalPositionFlag( OBJECT_GOAL_R, sideSide );
01499   else
01500     posGoal = SoccerTypes::getGlobalPositionFlag( OBJECT_GOAL_L, sideSide );
01501 
01502   return ( posGoal - getAgentGlobalPosition()).getDirection() ;
01503 
01504 }
01505 
01509 HeteroPlayerSettings WorldModel::getInfoHeteroPlayer( int iIndex )
01510 {
01511   return pt[iIndex];
01512 }
01513 
01517 HeteroPlayerSettings WorldModel::getHeteroInfoPlayer( ObjectT obj )
01518 {
01519 //  if( obj == getAgentObjectType() || SoccerTypes::isOpponent( obj ) )
01520 //    return getInfoHeteroPlayer( agentObject.getHeteroPlayerType() );    
01521   if( ! SoccerTypes::isKnownPlayer( obj ) )
01522     obj = getAgentObjectType();
01523 
01524   PlayerObject *object = (PlayerObject*) getObjectPtrFromType( obj );
01525   int          iType   = object->getHeteroPlayerType();
01526   // default iType in Object = 0
01527   return getInfoHeteroPlayer( iType );
01528 }
01529 
01530 
01534 bool WorldModel::setSubstitutedOpp( ObjectT obj )
01535 {
01536   m_setSubstitutedOpp.insert( obj );
01537   return true;
01538 }
01539 
01542 ObjectT WorldModel::getSubstitutedOpp( )
01543 {
01544   if( m_setSubstitutedOpp.empty() == true )
01545     return OBJECT_ILLEGAL;
01546   
01547   ObjectT obj = *m_setSubstitutedOpp.begin();
01548   m_setSubstitutedOpp.erase( obj );
01549   return obj;
01550 }
01551 
01552 
01554 double WorldModel::getDashPowerRate( ObjectT obj )
01555 {
01556   return getHeteroInfoPlayer( obj ).dDashPowerRate ;
01557 }
01558 
01560 double WorldModel::getPlayerSpeedMax( ObjectT obj )
01561 {
01562   return getHeteroInfoPlayer( obj ).dPlayerSpeedMax ;
01563 }
01564 
01566 double WorldModel::getPlayerDecay( ObjectT obj )
01567 {
01568   return getHeteroInfoPlayer( obj ).dPlayerDecay ;
01569 }
01570 
01572 double WorldModel::getMaximalKickDist( ObjectT obj )
01573 {
01574   return getHeteroInfoPlayer( obj ).dMaximalKickDist ;
01575 }
01576 
01578 double WorldModel::getStaminaIncMax( ObjectT obj )
01579 {
01580   return getHeteroInfoPlayer( obj ).dStaminaIncMax ;
01581 }
01582 
01584 double WorldModel::getPlayerSize( ObjectT obj )
01585 {
01586   return getHeteroInfoPlayer( obj ).dPlayerSize ;
01587 }
01588 
01590 double WorldModel::getInertiaMoment( ObjectT obj )
01591 {
01592   return getHeteroInfoPlayer( obj ).dInertiaMoment;
01593 }
01594 
01596 double WorldModel::getEffortMax( ObjectT obj )
01597 {
01598   return getHeteroInfoPlayer( obj ).dEffortMax;
01599 }
01600 
01602 double WorldModel::getEffectiveMaxSpeed( ObjectT obj, bool bWithNoise )
01603 {
01604   double dSpeed = 0.0;
01605   HeteroPlayerSettings pt = getHeteroInfoPlayer( obj );
01606 
01607   for( int j = 0; j < 15 ; j ++ )
01608   {
01609     dSpeed *= pt.dPlayerDecay;
01610     dSpeed += SS->getMaxPower()*pt.dEffortMax*pt.dDashPowerRate;
01611     if( dSpeed > pt.dPlayerSpeedMax )
01612       dSpeed = pt.dPlayerSpeedMax;
01613     if( bWithNoise )
01614       dSpeed += sqrt(2*(pow(dSpeed * SS->getPlayerRand(), 2)));
01615   }
01616   return dSpeed;
01617 }
01618 
01627 bool WorldModel::isQueuedActionPerformed()
01628 {
01629   // for all possible commands check if it is sent in previous cycle,
01630   // but not performed
01631   for( int i = 0 ; i < CMD_MAX_COMMANDS ; i++ )
01632     if( queuedCommands[i].time   == getTimeLastSenseMessage() - 1 &&
01633         performedCommands[i]     == false )
01634       return false;
01635 
01636   return true;
01637 }
01638 
01645 bool WorldModel::isFreeKickUs( PlayModeT pm )
01646 {
01647   if( pm == PM_ILLEGAL )
01648     pm = getPlayMode();
01649 
01650   bool bLeftKick = (pm==PM_FREE_KICK_LEFT  || pm==PM_INDIRECT_FREE_KICK_LEFT );
01651   bool bRightKick= (pm==PM_FREE_KICK_RIGHT || pm==PM_INDIRECT_FREE_KICK_RIGHT);
01652 
01653   return ( bLeftKick   && getSide() == SIDE_LEFT  ) ||
01654          ( bRightKick  && getSide() == SIDE_RIGHT ) ;
01655 }
01656 
01663 bool WorldModel::isFreeKickThem( PlayModeT pm )
01664 {
01665   if( pm == PM_ILLEGAL )
01666     pm = getPlayMode();
01667 
01668   bool bLeftKick = (pm==PM_FREE_KICK_LEFT  || pm==PM_INDIRECT_FREE_KICK_LEFT );
01669   bool bRightKick= (pm==PM_FREE_KICK_RIGHT || pm==PM_INDIRECT_FREE_KICK_RIGHT);
01670 
01671   return ( bRightKick  && getSide() == SIDE_LEFT  ) ||
01672          ( bLeftKick   && getSide() == SIDE_RIGHT ) ;
01673 }
01674 
01684 bool WorldModel::isBeforeKickOff( PlayModeT pm )
01685 {
01686   if( pm == PM_ILLEGAL )
01687     pm = getPlayMode();
01688 
01689   return pm == PM_BEFORE_KICK_OFF  || pm == PM_GOAL_LEFT  ||
01690          pm == PM_GOAL_RIGHT || isKickOffUs( pm ) || isKickOffThem( pm );
01691 }
01692 
01701 bool WorldModel::isDeadBallUs( PlayModeT pm )
01702 {
01703   if( pm == PM_ILLEGAL )
01704     pm = getPlayMode();
01705 
01706   return isKickInUs  ( pm ) || isFreeKickUs  ( pm ) || isCornerKickUs     ( pm)
01707       || isKickOffUs ( pm ) || isOffsideThem ( pm ) || isFreeKickFaultThem( pm)
01708       || isGoalKickUs( pm ) || isBackPassThem( pm ) ;
01709 }
01710 
01711 
01720 bool WorldModel::isDeadBallThem( PlayModeT pm )
01721 {
01722   if( pm == PM_ILLEGAL )
01723     pm = getPlayMode();
01724 
01725   return isFreeKickThem( pm ) || isKickInThem  ( pm ) || isCornerKickThem ( pm)
01726      ||  isKickOffThem ( pm ) || isGoalKickThem( pm ) || isFreeKickFaultUs( pm)
01727      || isOffsideUs    ( pm ) || isBackPassUs  ( pm ) ;
01728 }
01729 
01730 bool WorldModel::setChangeViewCommand( SoccerCommand soc )
01731 {
01732   m_changeViewCommand = soc;
01733   return true;
01734 }
01735 
01736 SoccerCommand WorldModel::getChangeViewCommand( )
01737 {
01738   return m_changeViewCommand;
01739 }
01740 
01742 SideT WorldModel::getSidePenalty( )
01743 {
01744   return m_sidePenalty;
01745 }
01746 
01748 bool WorldModel::setSidePenalty( SideT side )
01749 {
01750   m_sidePenalty = side;
01751   return true;
01752 }
01753 
01754 
01761 bool WorldModel::isCornerKickUs( PlayModeT pm )
01762 {
01763   if( pm == PM_ILLEGAL )
01764     pm = getPlayMode();
01765 
01766   return ( pm == PM_CORNER_KICK_LEFT  && getSide() == SIDE_LEFT  ) ||
01767          ( pm == PM_CORNER_KICK_RIGHT && getSide() == SIDE_RIGHT ) ;
01768 }
01769 
01776 bool WorldModel::isCornerKickThem( PlayModeT pm )
01777 {
01778   if( pm == PM_ILLEGAL )
01779     pm = getPlayMode();
01780 
01781   return ( pm == PM_CORNER_KICK_RIGHT  && getSide() == SIDE_LEFT  ) ||
01782          ( pm == PM_CORNER_KICK_LEFT   && getSide() == SIDE_RIGHT ) ;
01783 }
01784 
01791 bool WorldModel::isOffsideUs( PlayModeT pm )
01792 {
01793   if( pm == PM_ILLEGAL )
01794     pm = getPlayMode();
01795 
01796   return ( pm == PM_OFFSIDE_RIGHT  && getSide() == SIDE_RIGHT ) ||
01797          ( pm == PM_OFFSIDE_LEFT   && getSide() == SIDE_LEFT );
01798 }
01799 
01806 bool WorldModel::isOffsideThem( PlayModeT pm )
01807 {
01808   if( pm == PM_ILLEGAL )
01809     pm = getPlayMode();
01810 
01811   return ( pm == PM_OFFSIDE_LEFT  && getSide() == SIDE_RIGHT ) ||
01812          ( pm == PM_OFFSIDE_RIGHT && getSide() == SIDE_LEFT );
01813 }
01814 
01821 bool WorldModel::isKickInUs( PlayModeT pm )
01822 {
01823   if( pm == PM_ILLEGAL )
01824     pm = getPlayMode();
01825 
01826   return ( pm == PM_KICK_IN_LEFT  && getSide() == SIDE_LEFT  ) ||
01827          ( pm == PM_KICK_IN_RIGHT && getSide() == SIDE_RIGHT ) ;
01828 }
01829 
01836 bool WorldModel::isKickInThem( PlayModeT pm )
01837 {
01838   if( pm == PM_ILLEGAL )
01839     pm = getPlayMode();
01840 
01841   return ( pm == PM_KICK_IN_RIGHT  && getSide() == SIDE_LEFT  ) ||
01842          ( pm == PM_KICK_IN_LEFT   && getSide() == SIDE_RIGHT ) ;
01843 }
01844 
01853 bool WorldModel::isFreeKickFaultUs( PlayModeT pm )
01854 {
01855   if( pm == PM_ILLEGAL )
01856     pm = getPlayMode();
01857 
01858   return ( pm == PM_FREE_KICK_FAULT_LEFT  && getSide() == SIDE_LEFT  ) ||
01859          ( pm == PM_FREE_KICK_FAULT_RIGHT && getSide() == SIDE_RIGHT ) ;
01860 }
01861 
01872 bool WorldModel::isFreeKickFaultThem( PlayModeT pm )
01873 {
01874   if( pm == PM_ILLEGAL )
01875     pm = getPlayMode();
01876 
01877   return ( pm == PM_FREE_KICK_FAULT_RIGHT  && getSide() == SIDE_LEFT  ) ||
01878          ( pm == PM_FREE_KICK_FAULT_LEFT   && getSide() == SIDE_RIGHT ) ;
01879 }
01880 
01887 bool WorldModel::isKickOffUs( PlayModeT pm )
01888 {
01889   if( pm == PM_ILLEGAL )
01890     pm = getPlayMode();
01891 
01892   return ( pm == PM_KICK_OFF_LEFT  && getSide() == SIDE_LEFT  ) ||
01893          ( pm == PM_KICK_OFF_RIGHT && getSide() == SIDE_RIGHT ) ;
01894 }
01895 
01903 bool WorldModel::isKickOffThem( PlayModeT pm )
01904 {
01905   if( pm == PM_ILLEGAL )
01906     pm = getPlayMode();
01907 
01908   return ( pm == PM_KICK_OFF_RIGHT  && getSide() == SIDE_LEFT  ) ||
01909          ( pm == PM_KICK_OFF_LEFT   && getSide() == SIDE_RIGHT ) ;
01910 }
01911 
01920 bool WorldModel::isBackPassUs( PlayModeT pm )
01921 {
01922   if( pm == PM_ILLEGAL )
01923     pm = getPlayMode();
01924 
01925   return ( pm == PM_BACK_PASS_LEFT  && getSide() == SIDE_LEFT  ) ||
01926          ( pm == PM_BACK_PASS_RIGHT && getSide() == SIDE_RIGHT ) ;
01927 }
01928 
01938 bool WorldModel::isBackPassThem( PlayModeT pm )
01939 {
01940   if( pm == PM_ILLEGAL )
01941     pm = getPlayMode();
01942 
01943   return ( pm == PM_BACK_PASS_RIGHT && getSide() == SIDE_LEFT  ) ||
01944          ( pm == PM_BACK_PASS_LEFT  && getSide() == SIDE_RIGHT ) ;
01945 }
01946 
01953 bool WorldModel::isGoalKickUs( PlayModeT pm )
01954 {
01955   if( pm == PM_ILLEGAL )
01956     pm = getPlayMode();
01957 
01958   return ( pm == PM_GOAL_KICK_LEFT  && getSide() == SIDE_LEFT  ) ||
01959          ( pm == PM_GOAL_KICK_RIGHT && getSide() == SIDE_RIGHT ) ;
01960 }
01961 
01968 bool WorldModel::isGoalKickThem( PlayModeT pm )
01969 {
01970   if( pm == PM_ILLEGAL )
01971     pm = getPlayMode();
01972 
01973   return ( pm == PM_GOAL_KICK_RIGHT  && getSide() == SIDE_LEFT  ) ||
01974          ( pm == PM_GOAL_KICK_LEFT   && getSide() == SIDE_RIGHT ) ;
01975 }
01976 
01983 bool WorldModel::isPenaltyUs( PlayModeT pm )
01984 {
01985   if( pm == PM_ILLEGAL )
01986     pm = getPlayMode();
01987 
01988   return ( (
01989              ( pm == PM_PENALTY_SETUP_LEFT ||
01990                pm == PM_PENALTY_READY_LEFT ||
01991                pm == PM_PENALTY_TAKEN_LEFT ) && getSide() == SIDE_LEFT  )   ||
01992            (
01993              ( pm == PM_PENALTY_SETUP_RIGHT ||
01994                pm == PM_PENALTY_READY_RIGHT ||
01995                pm == PM_PENALTY_TAKEN_RIGHT ) && getSide() == SIDE_RIGHT  ) );
01996 }
01997 
01998 
02005 bool WorldModel::isPenaltyThem( PlayModeT pm )
02006 {
02007   if( pm == PM_ILLEGAL )
02008     pm = getPlayMode();
02009 
02010   return ( (
02011              ( pm == PM_PENALTY_SETUP_LEFT ||
02012                pm == PM_PENALTY_READY_LEFT ||
02013                pm == PM_PENALTY_TAKEN_LEFT ) && getSide() == SIDE_RIGHT  )   ||
02014            (
02015              ( pm == PM_PENALTY_SETUP_RIGHT ||
02016                pm == PM_PENALTY_READY_RIGHT ||
02017                pm == PM_PENALTY_TAKEN_RIGHT ) && getSide() == SIDE_LEFT  ) );
02018 }
02019 
02020 bool WorldModel::isFullStateOn( SideT s )
02021 {
02022   if( s == SIDE_ILLEGAL )
02023     s = getSide();
02024   
02025   if( s == SIDE_LEFT )
02026     return SS->getFullStateLeft();
02027   else if( s == SIDE_RIGHT )
02028     return SS->getFullStateRight();
02029   else
02030     return false;
02031 }
02032 
02037 void WorldModel::show( ostream & os )
02038 {
02039   int i;
02040   os << "Worldmodel (" << getCurrentTime() << ")\n" <<
02041         "========================\n";
02042   os << "Teamname: " << getTeamName() << "\n";
02043   if( Ball.getTimeLastSeen( ).getTime() != -1 )
02044     Ball.show();
02045   os << "Teammates: " << "\n";
02046   for( i = 0; i < MAX_TEAMMATES ; i++ )
02047     if( isConfidenceGood( Teammates[i].getType() ) )
02048       Teammates[i].show( getTeamName() );
02049   os << "Opponents: " << "\n";
02050   for( i = 0; i < MAX_OPPONENTS ; i++ )
02051     if( isConfidenceGood( Opponents[i].getType() ) )
02052       Opponents[i].show( DEFAULT_OPPONENT_NAME );
02053   os << "Agent: " << "\n";
02054   agentObject.show( getTeamName() );
02055 
02056   os << "General Info: " << "\n" <<
02057           "side: "      << SoccerTypes::getSideStr( getSide() )    << "\n" <<
02058           "kicks: "     << getNrOfCommands( CMD_KICK )             << "\n" <<
02059           "turns: "     << getNrOfCommands( CMD_TURN )             << "\n" <<
02060           "dashes: "    << getNrOfCommands( CMD_DASH )             << "\n" <<
02061           "turnnecks: " << getNrOfCommands( CMD_TURNNECK )         << "\n" <<
02062           "says: "      << getNrOfCommands( CMD_SAY )              << "\n" <<
02063           "playmode: "  << SoccerTypes::getPlayModeStr( playMode ) << "\n" <<
02064           "===================================="                   << "\n";
02065 
02066 }
02067 
02071 void WorldModel::show( ObjectSetT set, ostream & os )
02072 {
02073   int iIndex;
02074   for( ObjectT o = iterateObjectStart( iIndex, set );
02075        o != OBJECT_ILLEGAL;
02076        o = iterateObjectNext( iIndex, set ) )
02077     show( o, os );
02078   os << "\n";
02079 }
02080 
02084 void WorldModel::showQueuedCommands( ostream & os )
02085 {
02086   os << "Commands in queue:\n" ;
02087   for( int i = 0; i < CMD_MAX_COMMANDS; i++ )
02088     if( queuedCommands[i].commandType != CMD_ILLEGAL )
02089       queuedCommands[i].show( os );
02090 }
02091 
02096 void WorldModel::show( ObjectT o, ostream &os )
02097 {
02098   Object *object = getObjectPtrFromType( o );
02099   if( object != NULL )
02100   {
02101     if( SoccerTypes::isPlayer( o ) )
02102     {
02103       PlayerObject *pobj = (PlayerObject*) object ;
02104       if( SoccerTypes::isTeammate( o ) )
02105         pobj->show( getTeamName(), os );
02106       else
02107         pobj->show( "Opponent", os );
02108     }
02109     else
02110       object->show( os );
02111   }
02112 }
02113 
02119 bool WorldModel::waitForNewInformation( )
02120 {
02121   bool bReturn = true;
02122   if( bNewInfo == false ) // there hasn't arrived any information yet
02123   {
02124 #ifdef WIN32
02125     DWORD waittime = PS->getServerTimeOut() * 1000;
02126     EnterCriticalSection( &mutex_newInfo );
02127     int ret;
02128     Log.logWithTime( 2, "go into conditional wait" );
02129     while( (ret = WaitForSingleObject( event_newInfo,
02130                                        waittime) ) == WAIT_ABANDONED )
02131       printf("(WorldModel::waitForNewInformation) failure in loop!!\n");
02132     Log.logWithTime( 2, "go out of conditional wait" );
02133     if( ret == WAIT_TIMEOUT ) // if no info was received but timer timed out
02134       bReturn = false;
02135     ResetEvent( event_newInfo );
02136     LeaveCriticalSection( &mutex_newInfo );
02137 #else
02138     struct timeval now;
02139     struct timespec timeout;
02140     gettimeofday(&now, NULL);
02141     timeout.tv_sec = now.tv_sec + PS->getServerTimeOut();
02142     timeout.tv_nsec = now.tv_usec*1000;
02143 
02144     // lock mutex and wait till it is unlocked by Sense thread
02145     // this happens in setTimeLastSeeMessage, setTimeLastSenseMessage
02146     // or setTimeLastSeeGlobalMessage
02147     pthread_mutex_lock( &mutex_newInfo );
02148     int ret;
02149     Log.logWithTime( 2, "go into conditional wait" );
02150     while( (ret = pthread_cond_timedwait( &cond_newInfo,
02151                     &mutex_newInfo, &timeout) ) == EINTR )
02152      printf("(WorldModel::waitForNewInformation) failure in loop!!\n");
02153     Log.logWithTime( 2, "go out of conditional wait" );
02154     if( ret == ETIMEDOUT ) // if no info was received but timer timed out
02155       bReturn = false;
02156     pthread_mutex_unlock( &mutex_newInfo );
02157 #endif
02158   }
02159   else
02160     Log.logWithTime( 2, "already new info waiting" );
02161     
02162   // update the time of the see and the sense messages not yet with the time
02163   // from the last received messages. This is done to circumvent that a time
02164   // is updated while the main thread is still determining a new action and
02165   // the last see message is not yet updated in the world model. This is
02166   // therefore then in updateAll;
02167   
02168   // reset the indication of new visual information
02169   bNewInfo = false;
02170 
02171   return bReturn;
02172 }
02173 
02190 void WorldModel::logObjectInformation( int iLogLevel, ObjectT obj )
02191 {
02192 
02193   static char   str[2048];
02194   double dConf     = PS->getPlayerConfThr();
02195   sprintf( str, "(%4d,%3d) ", getCurrentTime().getTime(),
02196                               getCurrentTime().getTimeStopped() );
02197   if( obj != OBJECT_ILLEGAL )
02198     sprintf( str, "%12.6f %12.6f %12.6f %12.6f %12.6f %12.6f",
02199                                   getGlobalPosition(obj).getX(),
02200                                   getGlobalPosition(obj).getY(),
02201                                   getGlobalVelocity(obj).getX(),
02202                                   getGlobalVelocity(obj).getY(),
02203                                   getGlobalBodyAngle(obj),
02204                                   getGlobalNeckAngle(obj) );
02205 
02206   if( getConfidence      ( OBJECT_BALL ) > dConf &&
02207       getRelativeDistance( OBJECT_BALL ) < 20.0 )
02208     sprintf( &str[strlen(str)], " %12.6f %12.6f",
02209                                   getGlobalPosition(OBJECT_BALL).getX(),
02210                                   getGlobalPosition(OBJECT_BALL).getY() );
02211   else
02212     sprintf( &str[strlen(str)], " %12.6f %12.6f", -10.0, -10.0 );
02213 
02214   if( getTimeGlobalVelocity( OBJECT_BALL ) > getTimeFromConfidence( dConf ) &&
02215       getRelativeDistance  ( OBJECT_BALL ) < 20.0 )
02216     sprintf( &str[strlen(str)], " %12.6f %12.6f",
02217                                   getGlobalVelocity(OBJECT_BALL).getX(),
02218                                   getGlobalVelocity(OBJECT_BALL).getY() );
02219   else
02220     sprintf( &str[strlen(str)], " %12.6f %12.6f", -10.0, -10.0 );
02221   sprintf( &str[strlen(str)], " %12.6f", getConfidence(OBJECT_BALL) );
02222 
02223   int    iIndex=-1;
02224   int    iIndexPlayer;
02225   for( ObjectT o = iterateObjectStart( iIndex, OBJECT_SET_PLAYERS, 0.0 );
02226        o != OBJECT_ILLEGAL;
02227        o = iterateObjectNext( iIndex, OBJECT_SET_PLAYERS, 0.0 ) )
02228   {
02229     bool bPrint = false;
02230 
02231     iIndexPlayer = (SoccerTypes::isTeammate(o))
02232                      ? SoccerTypes::getIndex(o) + 1
02233                      : SoccerTypes::getIndex(o) + 12;
02234     sprintf( &str[strlen(str)], " %d", iIndexPlayer );
02235     if( getConfidence      ( o ) > dConf && isKnownPlayer(o) &&
02236         getRelativeDistance( o ) < 20.0 )
02237     {
02238       sprintf( &str[strlen(str)], " %12.6f %12.6f",
02239                                     getGlobalPosition(o).getX(),
02240                                     getGlobalPosition(o).getY() );
02241       bPrint = true;
02242     }
02243     else
02244        sprintf( &str[strlen(str)], " %12.6f %12.6f", -10.0, -10.0 );
02245 
02246     if( getTimeGlobalVelocity( o ) > getTimeFromConfidence( dConf ) &&
02247         getRelativeDistance  ( o ) < 20.0 && isKnownPlayer(o)  )
02248     {
02249       sprintf( &str[strlen(str)], " %12.6f %12.6f",
02250                                     getGlobalVelocity(o).getX(),
02251                                     getGlobalVelocity(o).getY() );
02252       bPrint = true;
02253     }
02254     else
02255       sprintf( &str[strlen(str)], " %12.6f %12.6f", -10.0, -10.0 );
02256 
02257     if( bPrint )
02258       sprintf( &str[strlen(str)], " %12.6f", getConfidence(o) );
02259     else
02260       sprintf( &str[strlen(str)], " %12.6f", -10.0 );
02261   }
02262   if( getCurrentCycle() != 3000  )
02263       Log.log( iLogLevel, str );
02264 }
02265 
02274 void WorldModel::logDrawInfo( int iLogLevel  )
02275 {
02276   if( getCurrentCycle() % ( SS->getHalfTime() * SS->getSimulatorStep()) == 0 )
02277     return ;
02278 
02279   bool   bReturn1 = false, bReturn2 = false;
02280   double dConfThr = PS->getPlayerConfThr();
02281   int    iIndex;
02282   char   strMsg1[MAX_MSG], strMsg2[MAX_MSG];
02283   char   strColorLeft[]  = "ffff00" ;
02284   char   strColorRight[] = "0000ff" ;
02285   if( getSide() != SIDE_LEFT )
02286   {
02287     sprintf( strColorLeft, strColorRight );
02288     sprintf( strColorRight, "ffff00" );
02289   }
02290 
02291 
02292   sprintf( strMsg1, "_2D_ CIRCLE col=%s ", strColorLeft ) ;
02293   // first each players logs the positions of all the teammates
02294   for( ObjectT o = iterateObjectStart( iIndex, OBJECT_SET_TEAMMATES, dConfThr );
02295        o != OBJECT_ILLEGAL;
02296        o = iterateObjectNext ( iIndex, OBJECT_SET_TEAMMATES, dConfThr ) )
02297   {
02298      bReturn1 = true;
02299 
02300      // add all the global positions to the string
02301      sprintf( &strMsg1[strlen(strMsg1)], "(%1.2f,%1.2f,1.3) ",
02302       m_iMultX*getGlobalPosition( o ).getX(),
02303       m_iMultY*getGlobalPosition( o ).getY() );
02304   }
02305   iterateObjectDone( iIndex );
02306 
02307   if( bReturn1 == false )
02308     sprintf( strMsg1, "_2D_ " );
02309   else
02310     strcat( strMsg1, "; " );
02311 
02312   sprintf( strMsg2, "CIRCLE col=%s ", strColorRight ) ;
02313   for( ObjectT o = iterateObjectStart( iIndex, OBJECT_SET_OPPONENTS, dConfThr);
02314        o != OBJECT_ILLEGAL;
02315        o = iterateObjectNext ( iIndex, OBJECT_SET_OPPONENTS, dConfThr ) )
02316   {
02317      bReturn2 = true;
02318 
02319      // add all the global positions to the string
02320      sprintf( &strMsg2[strlen(strMsg2)], "(%1.2f,%1.2f,1.3) ",
02321       m_iMultX*getGlobalPosition( o ).getX(),
02322       m_iMultY*getGlobalPosition( o ).getY() );
02323   }
02324   iterateObjectDone( iIndex );
02325   strcat( strMsg2, ";" );
02326 
02327   if( bReturn2 == true )
02328     strcat( strMsg1, strMsg2 );
02329   if( bReturn1 == true || bReturn2 == true)
02330     LogDraw.log( iLogLevel, strMsg1 );
02331 
02332   return ;
02333 }
02334 
02342 void WorldModel::logCoordInfo( int iLogLevel  )
02343 {
02344   if( getCurrentCycle() % ( SS->getHalfTime() * SS->getSimulatorStep()) == 0 )
02345     return ;
02346 
02347   bool   bReturn = false;
02348   int    iIndex;
02349   double dConfThr = PS->getPlayerConfThr();
02350   char   strMsg[MAX_MSG];
02351 
02352   map<double, ObjectT > mapDistToBall;
02353   VecPosition posBall = getGlobalPosition( OBJECT_BALL ), posFirst;
02354   int i = 0;
02355 
02356   for( ObjectT o = iterateObjectStart( iIndex, OBJECT_SET_TEAMMATES, dConfThr);
02357        o != OBJECT_ILLEGAL;
02358        o = iterateObjectNext ( iIndex, OBJECT_SET_TEAMMATES, dConfThr ) )
02359   {
02360      // add all the global positions to the string
02361      mapDistToBall.insert(
02362        make_pair( getGlobalPosition( o ).getDistanceTo( posBall ), o ) );
02363   }
02364   iterateObjectDone( iIndex );
02365 
02366   sprintf( strMsg, "all_2D_ ");
02367   for(  map<double, ObjectT >::iterator iter = mapDistToBall.begin();
02368         iter != mapDistToBall.end()  ;
02369         iter ++ )
02370   {
02371     if( i == 0 )
02372       posFirst = getGlobalPosition( iter->second );
02373     else if( iter->second == getAgentObjectType() && i < 4 &&
02374              iter->first < 20.0 )
02375     {
02376       bReturn = true;
02377       sprintf( &strMsg[strlen(strMsg)],
02378                        "LINE col=%s (%1.2f,%1.2f,%1.2f,%1.2f);",
02379         m_colorPlayers[ SoccerTypes::getIndex( getAgentObjectType() ) ],
02380         m_iMultX*posFirst.getX(),
02381         m_iMultY*posFirst.getY(),
02382         m_iMultX*getGlobalPosition( iter->second ).getX(),
02383         m_iMultY*getGlobalPosition( iter->second ).getY() );
02384     }
02385 
02386     i++;
02387   }
02388 
02389   if( bReturn == true )
02390     LogDraw.log( iLogLevel, strMsg );
02391 }
02392 
02393 
02394 /* This method logs a circle to the specified log level.
02395    \param iLogLevel log level to which the circle should be printed
02396    \param pos center position of the circle
02397    \param dRadius radius of the circle
02398    \param bAll indication whether it should be printed to specific drawlevel
02399           or not (default false). */
02400 bool WorldModel::logCircle( int iLogLevel, VecPosition pos, double  dRadius,
02401                             bool  bAll                                       )
02402 {
02403   if( getCurrentCycle() % ( SS->getHalfTime() * SS->getSimulatorStep()) == 0 )
02404     return false;
02405 
02406   return LogDraw.log( iLogLevel, "%s_2D_ CIRCLE col=%s (%1.2f,%1.2f,%f) ;",
02407            ( bAll == true ) ? "all" : "" ,
02408            m_colorPlayers[ getAgentIndex() ],
02409            m_iMultX*pos.getX(), m_iMultY*pos.getY(), dRadius );
02410 }
02411 
02412 /* This method logs a line to the specified log level.
02413    \param iLogLevel log level to which the circle should be printed
02414    \param pos1 first point of line
02415    \param pos2 second point of line
02416    \param bAll indication whether it should be printed to specific drawlevel
02417           or not (default false). */
02418 bool WorldModel::logLine( int  iLogLevel, VecPosition pos1, VecPosition pos2,
02419                           bool bAll              )
02420 {
02421   if( getCurrentCycle() % ( SS->getHalfTime() * SS->getSimulatorStep()) == 0 )
02422     return false;
02423 
02424   return LogDraw.log( iLogLevel, 
02425                       "%s_2D_ LINE col=%s (%1.2f,%1.2f,%1.2f,%1.2f) ;",
02426                       ( bAll == true ) ? "all" : "" ,
02427                       m_colorPlayers[ getAgentIndex() ],
02428                       m_iMultX*pos1.getX(), m_iMultY*pos1.getY(),
02429                       m_iMultX*pos2.getX(), m_iMultY*pos2.getY() );
02430 
02431 }
02432 
02433 /* This method logs the ball position and its velocity.
02434 
02435    \param iLogLevel log level to which the ball information should be
02436    printed.*/
02437 bool WorldModel::logDrawBallInfo( int  iLogLevel )
02438 {
02439   int  iPoints = 20;
02440   char strMsg[MAX_MSG];
02441   char *strColor = "ff0000";
02442 
02443   VecPosition vel     = getGlobalVelocity( OBJECT_BALL );
02444   VecPosition pos     = getBallPos(), pos1, pos2;
02445   AngDeg      angOrth = VecPosition::normalizeAngle( vel.getDirection( )+90 );
02446   VecPosition posBall = getBallPos();
02447   sprintf( strMsg, "_2D_ CIRCLE col=%s (%1.2f,%1.2f,0.3) ; ",
02448      strColor, m_iMultX*posBall.getX(), m_iMultY*posBall.getY() );
02449      
02450   for( int i = 0; i < iPoints; i ++ )
02451   {
02452     // create all the orthogonal lines
02453     pos += vel;
02454     pos1 = pos + VecPosition( -0.5*vel.getMagnitude(), angOrth, POLAR );
02455     pos2 = pos + VecPosition(  0.5*vel.getMagnitude(), angOrth, POLAR );
02456     sprintf( &strMsg[strlen(strMsg)], "LINE col=%s (%1.2f,%1.2f,%1.2f,%1.2f);",
02457             strColor, m_iMultX*pos1.getX(), m_iMultY*pos1.getY(),
02458             m_iMultX*pos2.getX(), m_iMultY*pos2.getY() );
02459     vel *= SS->getBallDecay();
02460   }
02461   
02462   // finally create the line through all orthogonal lines
02463   sprintf( &strMsg[strlen(strMsg)], "LINE col=%s (%1.2f,%1.2f,%1.2f,%1.2f) ;",
02464           strColor, m_iMultX*getBallPos().getX(), m_iMultY*getBallPos().getY(),
02465           m_iMultX*pos.getX(), m_iMultY*pos.getY() );
02466 
02467   return LogDraw.log( iLogLevel, strMsg );
02468 }
02469 
02475 bool WorldModel::isFeatureRelevant( FeatureT type )
02476 {
02477   int  iIndex  = (int)type;
02478   bool bReturn = true;
02479   static Time  timeKickedUsed = -1;
02480 
02481 #if 0
02482   Log.log( 460, "check feature (%d,%d,%d) relevance now (%d,%d,%d)",
02483          m_features[iIndex].getTimeSee().getTime(), 
02484          m_features[iIndex].getTimeSense().getTime(),
02485          m_features[iIndex].getTimeHear().getTime(),
02486          getTimeLastSeeMessage().getTime(), 
02487          getTimeLastSenseMessage().getTime(),
02488          getTimeLastHearMessage().getTime() );
02489 #endif
02490   // feature is relevant when see and hear time is larger or equal than the  
02491   // current time
02492   bReturn  = m_features[iIndex].getTimeSee()  >= getTimeLastSeeMessage() &&
02493              m_features[iIndex].getTimeHear() >= getTimeLastHearMessage();
02494              
02495   // if ball kicked, also recheck 
02496   if( timeKickedUsed != getTimeLastSenseMessage() )
02497   {
02498     bReturn &= ( m_bPerformedKick == false );
02499     timeKickedUsed = getTimeLastSenseMessage();
02500   }
02501 
02502   // in case of interception, also recheck after sense message
02503   if( type ==  FEATURE_INTERCEPTION_POINT_BALL ||
02504       type ==  FEATURE_INTERCEPT_CLOSE        )
02505     bReturn &= m_features[iIndex].getTimeSense() >= getTimeLastSenseMessage();
02506   return bReturn;
02507 }
02508 
02512 Feature WorldModel::getFeature( FeatureT type )
02513 {
02514   return m_features[(int)type];
02515 }
02516 
02522 bool WorldModel::setFeature( FeatureT type, Feature feature )
02523 {
02524   m_features[(int)type] = feature;
02525   return true;
02526 }
02527 
02530 void WorldModel::drawCoordinationGraph( )
02531 {
02532   bool partOfGraph = false; // is the player part of the coordination graph?
02533   static bool bFirst = true;
02534   static ofstream ofile;  
02535   char strTmp[128];
02536   if( bFirst == true )
02537   {
02538     sprintf( strTmp, "logs/coord%d.txt", getPlayerNumber() );
02539     ofile.open( strTmp );
02540     bFirst = false;
02541   }
02542   
02543   ObjectT objAgent = getAgentObjectType();
02544   ObjectT objClosestToBall = getFastestInSetTo( OBJECT_SET_TEAMMATES, 
02545                                                 OBJECT_BALL );
02546   ObjectT objTeam = OBJECT_ILLEGAL;
02547 
02548   VecPosition vpAgent = getAgentGlobalPosition();
02549   VecPosition vpTeam = getGlobalPosition( objTeam ); // -1000.0, 1000.0
02550 
02551 #if 0
02552   if ( objClosestToBall == objAgent ) // Agent is closest to ball
02553     // find the closest player that has a x-coordinate greater than the agent's
02554    {
02555      partOfGraph = true;
02556      
02557      int iIndex;
02558      ObjectSetT set = OBJECT_SET_TEAMMATES;
02559      for( ObjectT o = iterateObjectStart( iIndex, set );
02560           o != OBJECT_ILLEGAL;
02561           o = iterateObjectNext ( iIndex, set ) )
02562      {
02563        VecPosition vpNew = getGlobalPosition( o );     
02564        Log.log( 701, "i am fastest, check %d, last_x %f, his_x %f  \
02565                            last_rel %f, his_rel  %f",
02566                 o, vpTeam.getX(), vpNew.getX(), 
02567                 getRelativeDistance( objTeam ),
02568                 getRelativeDistance( o ) );
02569        if( objTeam == OBJECT_ILLEGAL)
02570        {
02571          if( vpNew.getX() > vpAgent.getX() - 5)
02572          {
02573            objTeam = o;            
02574            vpTeam = vpNew;
02575          }
02576        }
02577        else if ( vpNew.getX() > vpTeam.getX() && o != objAgent &&
02578                  vpNew.getX() > vpAgent.getX() - 5 )
02579        {
02580          if ( getRelativeDistance( o ) < getRelativeDistance( objTeam ) )
02581          {
02582            objTeam = o;
02583            vpTeam = vpNew;
02584          }
02585        }
02586      }
02587      iterateObjectDone( iIndex );
02588    }                            
02589   else // Agent is not closest to ball
02590   {
02591   }
02592 #else // Jelle
02593   objAgent = getAgentObjectType();
02594   
02595   objClosestToBall = getFastestInSetTo( OBJECT_SET_TEAMMATES, OBJECT_BALL );
02596   VecPosition posFastest   = getGlobalPosition( objClosestToBall );
02597   objTeam = OBJECT_ILLEGAL;
02598 
02599   vpAgent = getAgentGlobalPosition();
02600   vpTeam  = getGlobalPosition( objTeam ); // -1000.0, 1000.0
02601 
02602   // serach for closest teammate with x higher than 5 of guy with ball
02603   int iIndex;
02604   ObjectSetT set = OBJECT_SET_TEAMMATES;
02605   for( ObjectT o = iterateObjectStart( iIndex, set );
02606         o != OBJECT_ILLEGAL;
02607         o = iterateObjectNext ( iIndex, set ) )
02608   {
02609     VecPosition vpNew = getGlobalPosition( o );     
02610     Log.log( 701, "fastest %d, check %d, last_x %f, his_x %f dist_old %f dist_new %f last_rel %f, his_rel  %f",
02611              objClosestToBall, o, vpTeam.getX(), vpNew.getX(), 
02612              posFastest.getDistanceTo( vpTeam ), 
02613              posFastest.getDistanceTo( vpNew ), 
02614              getRelativeDistance( objTeam ),
02615              getRelativeDistance( o ) );
02616     if( objTeam == OBJECT_ILLEGAL)
02617     {
02618       if( vpNew.getX() > posFastest.getX() - 5 && o != objClosestToBall )
02619         {
02620           objTeam = o;            
02621           vpTeam = vpNew;
02622         }
02623     }
02624     else if ( /*vpNew.getX() > vpTeam.getX() && */o != objClosestToBall &&
02625               vpNew.getX() > posFastest.getX() - 5)
02626     {
02627       if( posFastest.getDistanceTo( vpNew ) < 
02628           posFastest.getDistanceTo( vpTeam ) )
02629       {
02630         objTeam = o;
02631         vpTeam = vpNew;
02632       }
02633     }
02634   }  
02635   iterateObjectDone( iIndex );                               
02636   Log.log( 701, "team %d fastest %d me %d", objTeam, objClosestToBall, objAgent  );
02637   if( objAgent == objTeam || objAgent == objClosestToBall )
02638     partOfGraph = true;
02639 #endif   
02640   if( partOfGraph == true && getCurrentTime().isStopped() == false )
02641   {
02642     Log.log( 701, 
02643              "create line with me (%d) (%f,%f) and closest team %d (%f,%f)", 
02644              SoccerTypes::getIndex(getAgentObjectType()) +1, 
02645              posFastest.getX(),posFastest.getY(),     
02646              SoccerTypes::getIndex(objTeam) +1,
02647              vpTeam.getX(),vpTeam.getY()          );
02648     logLine( 701, posFastest, vpTeam, true );  
02649     ofile << getCurrentCycle() << ": " << 
02650       SoccerTypes::getObjectStr( strTmp, objTeam, getTeamName() ) << " "
02651           << SoccerTypes::getIndex( objTeam ) << 
02652       endl;
02653   }
02654 }

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