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

📄 worldmodel.cpp

📁 根据 trilearn_base_sources-3.3 (一般称做UVA底层)修改的robocup球队代码,对某些模块进行了详细的注释(例如BasicPlayer,WorldMode模块),并且进行
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/*Copyright (c) 2000-2003, Jelle Kok, University of AmsterdamAll rights reserved.Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditions are met:1. Redistributions of source code must retain the above copyright notice, thislist of conditions and the following disclaimer.2. Redistributions in binary form must reproduce the above copyright notice,this list of conditions and the following disclaimer in the documentationand/or other materials provided with the distribution.3. Neither the name of the University of Amsterdam nor the names of itscontributors may be used to endorse or promote products derived from thissoftware without specific prior written permission.THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THEIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AREDISCLAIMED. IN NO EVENT SHALL  THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLEFOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIALDAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS ORSERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVERCAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USEOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*//*这个WorldModel模块可以获得当前世界模型的基本信息,它的很多函数是直接返回一个一个值。WorldModel是辅助高层决策判断的模块,在这里提供的大多是未经过处理的信息。更多的更高级的功能会在WorldModelHighLevel.cpp里出现,而世界模型的预测在WorldModelPredict.cpp完成。一次注释:05计算机2 张家翔二次注释,修改:05计算机2 席浩洋*//*! \file WorldModel.cpp<pre><b>File:</b>          WorldModel.cpp<b>Project:</b>       Robocup Soccer Simulation Team: UvA Trilearn<b>Authors:</b>       Jelle Kok<b>Created:</b>       12/02/2001<b>Last Revision:</b> $ID$<b>Contents:</b>      class definitions of WorldModel. This class contains               the methods of the WorldModel that process the incoming               information and analyze the current state of the world.<hr size=2><h2><b>Changes</b></h2><b>Date</b>             <b>Author</b>          <b>Comment</b>12/02/2001       Jelle Kok       Initial version created</pre>*/#include<stdio.h>    // needed for printf#include<errno.h>    // needed for ETIMEDOUT#ifdef WIN32  #include<windows.h>#else  #include<strings.h>  // needed for strcpy  #include<pthread.h>  // needed for pthread_mutex_init#endif#include<string.h>   // needed for strcpy#include<math.h>     // needed for erf#include<map>        // needed for map#include"WorldModel.h"/*****************************************************************************//********************** CLASS WORLDMODEL *************************************//*****************************************************************************//*! This constructor creates the worldmodel, all variables are initialized by    default values    \param ss reference to class in which all server parameters are stored    \param ps reference to class in which all client parameters are stored    \param fs reference to class in which all formation information is stored*///世界模型构造函数,取得服务器的设定,球员的设定,以及阵形的设定WorldModel::WorldModel( ServerSettings *ss, PlayerSettings *ps,                         Formations *fs):agentObject(  )            {  //服务器信息,队员信息,阵形;  dTotalVarVel   = 0.0;  dTotalVarPos   = 0.0;  SS             = ss;  PS             = ps;  formations     = fs;  bNewInfo       = false;  setSide         ( SIDE_ILLEGAL ); // is set by init message(由初始化信息所设置)  strTeamName[0] = '\0';    //队名默认为空  setPlayMode     ( PM_BEFORE_KICK_OFF );   //设置比赛模式  iGoalDiff      = 0;       //比分差距为0  m_sidePenalty  = SIDE_ILLEGAL;    //罚球的边界线  int i;  for( i = 0; i < MAX_TEAMMATES ; i ++ )    //初始化我方队员    Teammates[i].setType( SoccerTypes::getTeammateObjectFromIndex( i ) );  for( i = 0; i < MAX_OPPONENTS ; i ++ )    //初始化敌方队员    Opponents[i].setType( SoccerTypes::getOpponentObjectFromIndex( i ) );  for( i = 0; i < MAX_OPPONENTS + MAX_TEAMMATES ; i ++ )    //设置未知球员(最大记录22个)    UnknownPlayers[i].setType( OBJECT_ILLEGAL );  for( i = 0; i < MAX_FLAGS; i ++ )         //设置标志(MAX_FLAGS=55)    Flags[i].setType( OBJECT_ILLEGAL );     for( i = 0; i < MAX_LINES; i ++ )         //设置边线(MAX_line=4)    Lines[i].setType( OBJECT_ILLEGAL );  Ball.setType             ( OBJECT_BALL    );  //设置足球  agentObject.setType      ( OBJECT_ILLEGAL );  //设置当前球员  agentObject.setStamina   ( Stamina(SS->getStaminaMax(),1.0,1.0) );  //设置体力   //初始化所有的指令  for( i = 0 ; i < CMD_MAX_COMMANDS ; i ++ )    {    queuedCommands[i].commandType = (CommandT)i;  //队列里的命令类型赋值    performedCommands[i]          = false;        //初始化指令不要被执行    iCommandCounters[i]           = 0;            //指令总数为0  }  iNrUnknownPlayers     = 0;    //用于统计当前的未知球员个数  iNrHoles              = 0;  iNrOpponentsSeen      = 0;    //用于统计当前可见的对方球员个数  iNrTeammatesSeen      = 0;    //用于统计当前可见的我方球员个数  bsCheckBall           = BS_ILLEGAL;  // initialize the mutex for bNewInfo#ifdef WIN32  InitializeCriticalSection( &mutex_newInfo );  event_newInfo = CreateEvent( NULL, TRUE, FALSE, NULL );#else  pthread_mutex_init( &mutex_newInfo, NULL );  pthread_cond_init ( &cond_newInfo,  NULL );#endif  m_bRecvThink          = false;           //同步方法指出的我们是否准备好  timeLastSenseMessage  = Time( 0, 1 );    //上次的感觉信息的时间                                           //设置队员的标志颜色  strcpy( m_colorPlayers[0], "ffb0b0" );   // pink  strcpy( m_colorPlayers[1], "ffb000" );   // orange  strcpy( m_colorPlayers[2], "00b000" );   // darkgreen  strcpy( m_colorPlayers[3], "00ffff" );   // lightblue  strcpy( m_colorPlayers[4], "ff00ff" );   // purple  strcpy( m_colorPlayers[5], "ffff00" );   // yellow  strcpy( m_colorPlayers[6], "ffffff" );   // white  strcpy( m_colorPlayers[7], "0000ff" );   // blue  strcpy( m_colorPlayers[8], "00ff00" );   // green  strcpy( m_colorPlayers[9], "ff0000" );   // red  strcpy( m_colorPlayers[10],"aaaaaa" );   // gray  //未知周期(将来)的视觉和感觉,用来预测  for( i = 0 ; i < MAX_FEATURES ; i ++ )  {    m_features[i].setTimeSee( Time( UnknownTime, 0 ) );    m_features[i].setTimeSense( Time( UnknownTime, 0 ) );  }}/*! Destructor *///析构函数WorldModel::~WorldModel()   {#ifdef WIN32  DeleteCriticalSection( &mutex_newInfo );#endif}/*! This method returns a pointer to the Object information of the object    type that is passed as the first argument.    \param o ObjectType of which information should be returned    \return pointer to object information of supplied ObjectT argument *///  该方法返回一个指向对象类型 O 信息的一个指针//  ObjectT o : 要返回的对象Object* WorldModel::getObjectPtrFromType( ObjectT o ){  Object *object = NULL;  if( o == OBJECT_ILLEGAL )   //如果是非法对象(illegal object)    return NULL;    //如果是可见球员  if( SoccerTypes::isKnownPlayer( o ) )  {    //智能体自身    if( o == agentObject.getType() )        object = &agentObject;    //队友    else if( SoccerTypes::isTeammate( o ) )      object = &Teammates[SoccerTypes::getIndex(o)];    //敌方    else      object = &Opponents[SoccerTypes::getIndex(o)];  }  else if( SoccerTypes::isFlag( o ) )   //如果是标志对象    object = &Flags[SoccerTypes::getIndex(o)];  else if( SoccerTypes::isLine( o ) )   //如果是边对象    object = &Lines[SoccerTypes::getIndex(o)];  else if( SoccerTypes::isBall( o ) )   //如果是球对象    object = &Ball;  else if( o == OBJECT_OPPONENT_GOALIE )  //如果是对方球门    return getObjectPtrFromType( getOppGoalieType() );  else if( o == OBJECT_TEAMMATE_GOALIE )  //如果是我方球门    return getObjectPtrFromType( getOwnGoalieType() ); //对象类型的指针信息 return object;} /*! This method sets the time of the last catch cycle. This information is    received by the SenseHandler when the referee has sent this message.    After a catch, the goalie is not allowed to catch the ball for    catch_ban_cycles (defined in ServerSettings).    \param iTime time the ball was catched. *///  设置上次扑球的时间,是从SenseHandler收到的由裁判传递的信息。//  因为守门员扑球之后一段时间内不允许再次扑球void WorldModel::setTimeLastCatch( Time time ){  timeLastCatch = time;}/*! This method returns the number of cycles since the last catch.    \return cycles since last catch. *///  得到从上次扑球以后经过的周期数int WorldModel::getTimeSinceLastCatch(){  if( timeLastCatch.getTime() == -1 )    return 1000;  return timeLastSenseMessage - timeLastCatch;}/*! This method sets the time of the last received referee message. This    information is received by the SenseHandler.    \param iTime time the referee sent the last message. *///  设置上次接受裁判信息的时间bool WorldModel::setTimeLastRefereeMessage( Time time ){  timeLastRefMessage = time;  return true;}/*! This method returns the time of the last received referee message.    \return time of last received referee message. *///  返回接受上次裁判信息的时间Time WorldModel::getTimeLastRefereeMessage( ){  return timeLastRefMessage;}/*! This method returns the current time. In case of a player this is the    time of the last sense message, in case of the coach this is the time of    the last see_global message.    \return actual time *///  返回当前的时间//  如果是球员调用,返回上次感觉到信息的时间//  如果是教练调用,返回上次收到全局信息的时间Time WorldModel::getCurrentTime(){  if( getPlayerNumber() == 0 )    return getTimeLastSeeGlobalMessage();  else    return getTimeLastRecvSenseMessage();}/*! This method returns the current cycle number. In case of a player this is    the cycle of the last sense message, in case of the coach this is the cycle    of the last see_global message.    \return actual time *///  返回当前周期数,返回规则同上int WorldModel::getCurrentCycle(){  return getCurrentTime().getTime();}/*! This method returns whether the time of the server stands    still. This occurs during non play-on modes (kick_in, kick_off,    etc.).    \return bool indicating whether time of the server stands still. *///  返回服务器的时间是否停止了,在不是PLAY_ON模式的时候会停止。bool WorldModel::isTimeStopped(){  return getCurrentTime().isStopped();}/*! This method returns whether the last received message was a see or not.    \return bool indicating whether the last received message was a see.*///  上次的感觉的信息是否是视觉信息bool WorldModel::isLastMessageSee() const{  //判断上次视觉信息被感觉的时间和感觉信息的时间是否相等  return getTimeLastSeeMessage() == getTimeLastSenseMessage() ;}/*! This method returns the time of the last see global message.    This message can only be received by the coach.    \return time of last see_global message *///  得到上次获得全局信息的时间(只有教练会或者这种信息)Time WorldModel::getTimeLastSeeGlobalMessage( ) const    {  return getTimeLastRecvSeeMessage();}/*! This method sets the time of the last see_global message.    \param time see message has arrived    \return true when update was succesful *///  设置上次获得全局信息的时间bool WorldModel::setTimeLastSeeGlobalMessage( Time time ){  updateRelativeFromGlobal( );  return setTimeLastSeeMessage( time ); // set see message}/*! This method returns the time of the last see message    \return time of last see message *///  得到上次获得视觉信息的时间Time WorldModel::getTimeLastSeeMessage( ) const   {  return timeLastSeeMessage;}/*! This method returns the time of the last received see message.    The difference with getTimeLastSeeMessage is that that method returns    the last see message that has been updated in the world model. In most    cases these are equal.    \return time of last received sense message *///  得到上次获得视觉信息的时间//  与上一个函数不同的是,那个是已经更新过视觉信息的时间,一般这两个函数返回相等Time WorldModel::getTimeLastRecvSeeMessage( ) const  {  return timeLastRecvSeeMessage ;}/*! This method sets the time of the last see message. It also sends a    condition signal to indicate that variable bNewInfo has    changed. When main thread is stopped in waitForNewInformation, it    is unblocked.    \param time see message has arrived    \return true when update was succesful *///  设置上次获得视觉信息的时间bool WorldModel::setTimeLastSeeMessage( Time time ){  timeLastRecvSeeMessage  = time;

⌨️ 快捷键说明

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