⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 formations.c

📁 uva trilearn的robocup源程序
💻 C
📖 第 1 页 / 共 2 页
字号:
    \param pos new home position for the player with role number 'atIndex'    \param atIndex index of the player with role for which the home           position should be set.    \return bool indicating whether update was succesfull. */bool FormationTypeInfo::setPosHome( VecPosition pos, int atIndex ){  posHome[ atIndex ] = pos;  return true;}/*! This method sets the x coordinate of the home position for the player    with role number 'atIndex'.    \param x x coordinate for the home position    \param atIndex role in formation for which x coordinate should be set.    \return bool indicating whether update was succesfull. */bool FormationTypeInfo::setXPosHome( double x, int atIndex ){  posHome[ atIndex ].setX( x );  return true;}/*! This method sets the y coordinate of the home position for the player    with role number 'atIndex'.    \param y y coordinate for the home position    \param atIndex role number for which y coordinate should be set.    \return bool indicating whether update was succesfull. */bool FormationTypeInfo::setYPosHome( double y, int atIndex ){  posHome[ atIndex ].setY( y );  return true;}/*! This method returns the home position for the player with role number    atIndex in this formation. The home position is the position from which the    strategic position is calculated and could be interpreted as the position a    player is located when the ball is at the position (0,0).    \return home position for player number at index 'atIndex' */VecPosition FormationTypeInfo::getPosHome( int atIndex ) const{  return posHome[ atIndex ];}/*! This method sets the player type for the player with role number 'atIndex'    in this formation.    \param type new player type for role at position 'atIndex'    \param atIndex role number for which player type should be set.    \return bool indicating whether update was succesfull. */bool FormationTypeInfo::setPlayerType( PlayerT type, int atIndex ){  playerType[ atIndex ] = type;  return true;}/*! This method returns the player type for the player with role number    'atIndex' in this formation.    \return player type for player with role number 'atIndex' */PlayerT FormationTypeInfo::getPlayerType( int atIndex ) const{  return playerType[ atIndex ];}/*! This method sets the information for a player type in this formation. Note    that information is for a player TYPE and not for a player ROLE.    \param info new player type information for the player type at 'atIndex'.    \param atIndex number of player type for which information should be set.    \return bool indicating whether update was succesfull. */bool FormationTypeInfo::setPlayerTypeInfo( PlayerTypeInfo info, int atIndex ){  playerTypeInfo[ atIndex ] = info;  return true;}/*! This method returns (a pointer to) the player type information for the    player type at position 'atIndex'    \param atIndex index of which player type information should be returned    \return pointer to player type information located at index 'atIndex' */PlayerTypeInfo* FormationTypeInfo::getPlayerTypeInfo( int atIndex ){  return &playerTypeInfo[ atIndex ];}/*! This method returns (a pointer to) the player type information for the    player with role number 'iPlayerInFormation'.    \param iPlayerInFormation role number for which info should be returned    \return pointer to information for role 'iPlayerInFormation'*/PlayerTypeInfo* FormationTypeInfo::getPlayerTypeInfoOfPlayer(                                                       int iPlayerInFormation ){  return &playerTypeInfo[ playerType[iPlayerInFormation] ];}/******************************************************************************//********************** CLASS FORMATIONS **************************************//******************************************************************************//*! This is the constructor for the Formations class and needs as arguments    a formation configuration file, the current formation and the number of the    agent in this formation (normally at start-up this equals the player    number).    \param strFile string representation of the formation configuration file    \param curFt current formation type (default FT_ILLEGAL)    \param iNr number of the agent in this formation (default 1)*/Formations::Formations( const char *strFile, FormationT curFt, int iNr ){  if( strFile == NULL )  {    cerr << "(Formations::Formations) No Filename given" << endl;    return;  }  if( readFormations( strFile ) == false )    cerr << "(Formations::Formations) Error reading file" << endl;  curFormation = curFt;  setPlayerInFormation( iNr );}/*! This methods prints all the information of the different formation types to    the output stream os and furthermore prints the current formation and the    role number of the agent in this formation.    \param os output stream to which output is written. */void Formations::show( ostream &os ){  for( int i = 0 ; i < MAX_FORMATION_TYPES; i ++ )    formations[i].show( os );  os << "Current formation: " << (int)curFormation << endl       << "Player nr in formation: " << iPlayerInFormation ;}/*! This method returns the strategic position for a player. It calculates this    information by taking the home position of the current role in the    current formation and combines this with the position of the ball using the    attraction values for the current player type. The attraction values    defines the percentage of the ball coordinate that is added to the home    position of the current player type. So when the x coordindate of the home    position is 10.0, x coordinate ball is 20.0 and x attraction is 0.25. The    x coordinate of the strategic position will become 10.0 + 0.25*20.0 = 15.0.    When this value is smaller than the minimal x coordinate or larger than    the maximal x coordinate, the coordinate is changed to this minimal or    maximal coordinate respectively. Also when the behind ball value is set,    the x coordinate of the strategic position is set to this ball coordinate.    Furthermore when the strategic position is in front of the supplied    argument dMaxXInPlayMode, the x coordinate is adjusted to this value.    During normal play mode the supplied value is often the offside line.    \param iPlayer player number in formation of which strategic position                   should be determined.    \param posBall position of the ball    \param dMaxXInPlayMode, max x coordinate allowed in current play mode. */VecPosition Formations::getStrategicPosition( int iPlayer, VecPosition posBall,                  double dMaxXInPlayMode ){  VecPosition     posHome;  PlayerTypeInfo* ptInfo = formations[curFormation].                            getPlayerTypeInfoOfPlayer( iPlayer );  double x, y;  // get the home position and calculate the associated strategic position  posHome = formations[curFormation].getPosHome( iPlayer );  y = posHome.getY() + posBall.getY() * ptInfo->getAttrY();  x = posHome.getX() + posBall.getX() * ptInfo->getAttrX();  // do not move to much to the side  if( fabs( y ) > 0.5*0.75*PITCH_WIDTH )    y -= 0.5*posBall.getY() * ptInfo->getAttrY();  // when behind ball is set, do not move to point in front of ball  if( ptInfo->getBehindBall() == true && x > posBall.getX() )    x = posBall.getX();  // do not move past maximal x or before minimal x  if( x > ptInfo->getMaxX() )    x = ptInfo->getMaxX();  else if( x < ptInfo->getMinX() )    x = ptInfo->getMinX();  // when x coordinate is in front of allowed x value, change it  if( x > dMaxXInPlayMode )    x = dMaxXInPlayMode;  return VecPosition( x, y );}/*! This method reads the formations from the file 'strFile' and has the    following format:       - x coordinate of the home position for all the roles       - y coordinate of the home position for all the roles       - the player types for all the roles       - the x attraction for all the player types       - the y attraction for all the player types       - indication whether to stay behind the ball for all the player types       - minimal x coordinate for all the player types       - maximal x coordinate for all the player types    \param strFile string representation of the file.    \return bool when file was read in succesfully. */bool Formations::readFormations( const char *strFile ){  ifstream in( strFile );  if( !in )  {    cerr << "(readValues::readValues) Could not open file '" <<    strFile << "'" << endl;    return false;  }  char strLine[256], *str;  int            iLineNr          = 0, i;  int            iForm            = 0; // current formation type that is parsed  int            iLineInFormation = 0; // current offset of line in formation  bool           bReturn          = true;  PlayerTypeInfo *pt_info;  // read all lines  while( bReturn && in.getline( strLine, sizeof(strLine) ) )  {    str = &strLine[0];    iLineNr++;    // comment and empty lines should be skipped    if( !(strLine[0] == '\n' || strLine[0] == '#' || strLine[0]=='\0' ||          Parse::gotoFirstNonSpace( &str ) == '\0' ) )    {      // there are nine different lines in a formation (see comment above)      // all values for each line are parsed in one iteration      // after all 9 lines are parsed, the sequence it is resetted.      switch( iLineInFormation )      {        case 0: // first line is the number of the formation          iForm = Parse::parseFirstInt( &str );          break;        case 1: // the x coordinate of the home pos for all the players          for( i = 0 ; i < MAX_TEAMMATES ; i ++ )            formations[iForm].setXPosHome(Parse::parseFirstDouble(&str), i);          break;        case 2: // the y coordinate of the home pos for all the players          for( i = 0 ; i < MAX_TEAMMATES ; i ++ )            formations[iForm].setYPosHome(Parse::parseFirstDouble(&str), i);          break;        case 3: // the player types for all the players          for( i = 0 ; i < MAX_TEAMMATES ; i ++ )            formations[iForm].setPlayerType(                (PlayerT) Parse::parseFirstInt(&str), i);          break;        case 4: // the x attraction for all the player types          for( i = 0 ; i < MAX_PLAYER_TYPES ; i ++ )          {            pt_info = formations[iForm].getPlayerTypeInfo( i );            pt_info->setAttrX( Parse::parseFirstDouble( &str ) );          }          break;        case 5: // the y attraction for all the player types          for( i = 0 ; i < MAX_PLAYER_TYPES ; i ++ )          {            pt_info = formations[iForm].getPlayerTypeInfo( i );            pt_info->setAttrY( Parse::parseFirstDouble( &str ) );          }          break;        case 6: // stay behind the ball for all the player types          for( i = 0 ; i < MAX_PLAYER_TYPES ; i ++ )          {            pt_info = formations[iForm].getPlayerTypeInfo( i );            if( Parse::parseFirstInt( &str ) == 1 )              pt_info->setBehindBall( true );            else              pt_info->setBehindBall( false );          ;          }          break;        case 7: // the minimal x coordinate for all the player types          for( i = 0 ; i < MAX_PLAYER_TYPES ; i ++ )          {            pt_info = formations[iForm].getPlayerTypeInfo( i );            pt_info->setMinX( Parse::parseFirstDouble( &str ));          }          break;        case 8:// the maximal x coordinate for all the player types          for( i = 0 ; i < MAX_PLAYER_TYPES ; i ++ )          {            pt_info = formations[iForm].getPlayerTypeInfo( i );            pt_info->setMaxX( Parse::parseFirstDouble( &str ));          }          break;        default:          cerr << "(Formations::readFormations) error line " << iLineNr <<endl;          return false;      }      iLineInFormation++;         // go one line further      if( iLineInFormation == 9 ) // each formation consists of nine lines        iLineInFormation = 0;    }  }  return true;}/*! This method sets the current formation.    \param formation new current formation    \return bool indicating whether the update was successful */bool Formations::setFormation( FormationT formation ){  curFormation = formation;  return true;}/*! This method returns the current formation.    \return current formation  */FormationT Formations::getFormation( ) const{  return curFormation;}/*! This method sets the player number of the agent in the current formation to    'iNumber'.    \param iNumber new player number for this agent    \return bool indicating whether the update was succesfull */bool Formations::setPlayerInFormation( int iNumber ){  iPlayerInFormation = iNumber;  return true;}/*! This method returns the role number of the agent in the current formation    \return player number for this agent in the current formation */int Formations::getPlayerInFormation( ) const{  return iPlayerInFormation;}/*! This method returns the player type for the agent in the current formation    \return player type for the agent in the current formation */PlayerT Formations::getPlayerType( int iIndex ) const{  if( iIndex == -1  )    iIndex = iPlayerInFormation;  return formations[ curFormation ].getPlayerType( iIndex );}/*************************** TESTING PURPOSES *********************************//*int main( void ){  Formations fs( "formations.conf" );  fs.show( cout );}*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -