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

📄 soccertypes.cpp

📁 自己写的robocup-2d程序
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/*
Copyright (c) 2000-2003, Jelle Kok, University of Amsterdam
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list 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 documentation
and/or other materials provided with the distribution.

3. Neither the name of the University of Amsterdam nor the names of its
contributors may be used to endorse or promote products derived from this
software 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, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/*! \file SoccerTypes.cpp
<pre>
<b>File:</b>          SoccerTypes.cpp
<b>Project:</b>       Robocup Soccer Simulation Team: UvA Trilearn
<b>Authors:</b>       Jelle Kok
<b>Created:</b>       28/11/2000
<b>Last Revision:</b> $ID$
<b>Contents:</b>      This file contains the different enumerations and
               constants that are important in the Soccer Server.
               Furthermore it contains the class SoccerCommand which is
               used to denote the different possible soccer commands and
               the class SoccerTypes that contains all kind of static
               methods to translate text strings that are received from
               the server into the soccer types (=enumerations) that
               are defined here.  Finally it contains the Time class which
               holds a two-tuple that represents the time in the soccer server.

<hr size=2>
<h2><b>Changes</b></h2>
<b>Date</b>             <b>Author</b>          <b>Comment</b>
28/11/2000       Jelle Kok       Initial version created
</pre>
*/

#include <iostream>       // needed for outputsteam in showcerr
#include <stdio.h>        // needed for sprintf
#ifdef Solaris
  #include <strings.h>    // needed for strncmp
#else
  #include <string.h>     // needed for strncmp
#endif

#include "SoccerTypes.h"
#include "Parse.h"


/*****************************************************************************/
/********************* CLASS TIME ********************************************/
/*****************************************************************************/

/*! This is the constructor for the Time class, it receives two arguments. The
    actual time and how long the time has stopped.
    \param iTime actual time
    \param iStopped number of cycles time stopped */
Time::Time( int iTime, int iStopped )
{
  m_iTime    = iTime;
  m_iStopped = iStopped;
}

/*! This method updates the time to 'iTime'. When the actual time was already
    'iTime' the current time is kept unchanged and the time stopped is raised
    with one. Otherwise the actual time is changed to 'iTime' and the stopped
    time is set to 0.
    \param iTime new time
    \return boolean indicating whether update was successful */
bool Time::updateTime( int iTime )
{
  if( m_iTime == iTime )
    m_iStopped++;
  else
  {
    m_iTime    = iTime;
    m_iStopped = 0;
  }
  return true;
}

/*! This methods sets the stopped time, which denotes the number of cycles time
    stood still.
    \param iTime new stopped time
    \return boolean indicating whether update was successful */
bool Time::setTimeStopped( int iTime )
{
  m_iStopped = iTime;
  return true;
}

/*! This method returns the actual time, that is the number of cycles that have
    passed.
    \return actual time. */
int Time::getTime( )
{
  return m_iTime;
}

/*! This method returns the time the time has stopped, that is the number of
    cycles the time stood on the current value.
    \return stopped time */
int Time::getTimeStopped( )
{
  return m_iStopped;
}

/*! This method returns the time difference between two time objects.
    \param t Time with which current time should be compared
    \return time difference */
int Time::getTimeDifference( Time t )
{
  if( getTime() < t.getTime() )
    return getTime() - t.getTime() - t.getTimeStopped();
  else if( getTime() == t.getTime() )
    return getTimeStopped() - t.getTimeStopped();
  else
    return getTime() - t.getTime();
}

/*! This method returns a boolean value indicating whether the time
    currently is stopped.

    \return boolean indicating whether the time currently is stopped */
bool Time::isStopped( )
{
  return m_iStopped != 0;
}

/*! This method returns a new time class denoting the time when
    'iCycles' are added to the current time.  There are different
    situations possible. When the added time is positive and the time
    stands still, the cycles are added to the stopped time, otherwise
    they are added to the actual time. When the added time is negative
    and the time stands still, the cycles are subtracted from the
    stopped time.  Otherwise the time is subtracted from the actual
    time.

    \param iCycles denotes the time that should be added (when negative
    subtracted) to the current time

    \return new time object with 'iCycles' added to the current time. 'iCycles'
    can be negative in which case a subtraction is performed. */
Time Time::getTimeAddedWith( int iCycles )
{
  int iTime    = getTime();
  int iStopped = getTimeStopped();

  if( iCycles > 0 )                            // add cycles
  {
    if( iStopped > 0 )                         // time stopped
      iStopped += iCycles;                     // add it to stopped cycles
    else
      iTime    += iCycles;                     // otherwise add to normal time
  }
  else                                         // subtract cycles
  {
    if( iStopped > 0 && iStopped >= iCycles)   // time stopped and enough time
      iStopped += iCycles;                     // subtract cycle (iCycles=neg)
    else if( iStopped > 0 )                    // stopped but not enough time
    {
      iStopped = 0;                            // take as many as possible
      iCycles += iStopped;
      iTime   += iCycles;                      // and take rest from m_iTime
    }
    else                                       // time not stopped
      iTime   += iCycles;                      // take all from m_iTime
    if( iTime < 0 )
      iTime = 0;                               // negative time not possible
  }
  return Time( iTime, iStopped );
}

/*! This method adds 'iCycles' to the current time. The current values are
    updated. The method getTimeAddedWith is used to calculated the new time.
    \param iCycles time added to the current time
    \return boolean indicating whether update was successful */
bool Time::addToTime( int iCycles )
{
  *this = getTimeAddedWith( iCycles );
  return true;
}

/*! This method prints the time to the specified output stream. Time is printed
    as the two tuple (t,s) where t denotes the actual time and s the number of
    stopped cycles.
    \param os output stream to which output is written (default cout) */
void Time::show( ostream &os )
{
  os << "(" << getTime() << "," << getTimeStopped() << ")";
}

/*! This method returns the time as if 'i' cycles would be added to the current
    time. The method getTimeAddedWith is used for this. No changes are made to
    the current object.
    \param iCycles denotes the time that should be added to the current time
    \return new time object with 'iCycles' added to the current time */
Time Time::operator + ( const int &i )
{
  return getTimeAddedWith( i );
}

/*! This method returns the result of adding time 't' to the current
    time. No changes are made to the current object.
    It is defined by (t1,s1) + (t2,s2) = (t1+t2,s2). The stopped time of the
    first time tuple is neglected, since this has already been passed.
    \param t Time object that should be added to the current time
    \return new time object with 't' added to the current time */
Time Time::operator + ( Time t )
{
  return Time( getTime() + t.getTime(), t.getTimeStopped() );
}

/*! This method returns the time as if 'i' cycles would be subtracted from the
    current time. The method getTimeAddedWith is used for this. No changes are
    made to the current object.
    \param iCycles denotes the time that should be subtracted from the time
    \return new time object with 'iCycles' subracted  */
Time Time::operator - ( const int &i )
{
  return getTimeAddedWith( -i );
}

/*! This method returns the result the difference between the two times and
    is equal to the method getTimeDifference.
    \param t Time object that should be subtracted from the current time
    \return new time object with 't' subtracted from the current time */
int Time::operator - ( Time t )
{
  return getTimeDifference( t );
}

/*! This method returns a new time object (i,0). The argument 'i' is thus
    denoted as the actual time.
    \param i new time
    \return time object */
void Time::operator = ( const int &i )
{
  updateTime( i );
  setTimeStopped( 0 );
}

/*! This method updates the time by adding 'i' cycles to the current
    time. The method addToTime is used for this.
    \param i denotes the time that should be added to the current time */
void Time::operator += ( const int &i )
{
  addToTime( i );
}

/*! This method updates the time by adding the time 't' to the current
    time.
    It is defined by (t1,s1) + (t2,s2) = (t1+t2,s2). The stopped time of the
    first time tuple is neglected, since this has already been passed.
    \param t Time object that should be added to the current time */
void Time::operator += ( Time t )
{
  updateTime    ( getTime() + t.getTime() );
  setTimeStopped( t.getTimeStopped()      );
}

/*! This method updates the time by subtractign 'i' cycles from the
    current time. The method addToTime is used for this with '-i' as
    its argument.
    \param i denotes the time that should be subtracted */
void Time::operator -= ( const int &i )
{
  addToTime( -i );
}

/*! This method updates the time by subtracting time 't' from the current
    time. It is defined by (t1,s1) + (t2,s2) = (t1-t2,0). The stopped time is
    set to zero.
    \param t Time object that should be subtracted from the current time */
void Time::operator -= ( Time t )
{
  updateTime    ( getTime() - t.getTime() );
  setTimeStopped( 0                       );
}


/*! This method returns a boolean indicating whether the current time
    is inequal to the time specified by the integer 'i'. 'i' is first
    converted to the time object (i,0). When the time difference
    returned by getTimeDifference between these two time objects is
    inequal to zero true is returned, false otherwise

    \param i actual time with which current time should be compared
    \return bool indicating whether current time equals specified time */
bool Time::operator != ( const int &i )
{
  return getTimeDifference( Time(i, 0) ) != 0;
}

/*! This method returns a boolean indicating whether the current time
    is inequal to the time t. When the time difference returned by
    getTimeDifference between these two time objects is inequal to
    zero true is returned, false otherwise.

    \param t time with which current time should be compared
    \return bool indicating whether current time equals specified time */
bool Time::operator != ( Time t )
{
  return getTimeDifference( t ) != 0;
}

/*! This method returns a boolean indicating whether the current time
    equals the time as specified by the integer 'i'. 'i' is first
    converted to the time object (i,0). When the time difference
    returned by getTimeDifference between these two time objects
    equals zero, true is returned, false otherwise

    \param i actual time with which current time should be compared
    \return bool indicating whether current time equals specified time */
bool Time::operator == ( const int &i )
{
  return !( *this != i );
}

/*! This method returns a boolean indicating whether the current time equals

⌨️ 快捷键说明

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