📄 soccertypes.cpp
字号:
/*********************************************************************************** In the name of Almighty ** ** SoccerTypes.cpp : Robocup 3D Soccer Simulation Team Zigorat ** (This team was previously named Gcyrus) ** ** Date: 03/20/2007 ** Author: Mahdi Hamdarsi ** Comments: This file contains class definition for SoccerCommand ** (Some materials are owned & copyrighted by Jelle Kok) ** ***********************************************************************************//*! \file SoccerTypes.cpp<pre><b>File:</b> SoccerTypes.cpp<b>Project:</b> Robocup Soccer Simulation Team: Zigorat<b>Authors:</b> Mahdi Hamdarsi<b>Created:</b> 03/20/2007<b>Last Revision:</b> $ID$<b>Contents:</b> This file contains class definition for SoccerCommand<hr size=2><h2><b>Changes</b></h2><b>Date</b> <b>Author</b> <b>Comment</b>03/20/2007 Mahdi Initial version created</pre>*//*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.*/#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 SOCCERCOMMAND ***********************************//*****************************************************************************//*! This is the constructor for SoccerCommand and initilizez the object with information of a joint \param com Command type: CMD_SAY, etc. \param jid JointID to modify attributes \param d1 Joints First plane speed \param d2 Joints second plane speed */SoccerCommand::SoccerCommand( CommandT com, JointT jid, double d1, double d2 ){ commandType = CMD_ILLEGAL; JID = JID_ILLEGAL; dSpeed1 = UnknownDoubleValue; dSpeed2 = UnknownDoubleValue; strcpy(str, ""); makeCommand( com, jid, d1, d2 );}/*! This is the constructor for SoccerCommand that initializes the object with a say message \param com Type of command \param msg Message to be sent*/SoccerCommand::SoccerCommand( CommandT com, char *msg ){ commandType = CMD_ILLEGAL; JID = JID_ILLEGAL; dSpeed1 = UnknownDoubleValue; dSpeed2 = UnknownDoubleValue; strcpy(str, ""); makeCommand( com, msg );}/*! This method creates a text message from the information in soccer command that can be digested by server and turn into real actions \param strMsg Source to put the text message in */bool SoccerCommand::makeCommandString( char *strMsg ) const{ switch( commandType ) { case CMD_JOINT: sprintf( strMsg,"(%s %f %f)", SoccerTypes::getJointServerMsg( JID ), dSpeed1, dSpeed2); return true; case CMD_SAY: sprintf( strMsg, "(say %s)", str); return true; default: return false; }}/*! This method creates a joint change speed command \param com Command type \param jid Joint identifier to change speed \param d1 First planes joint angular speed \param d2 Second planes joint angular speed */void SoccerCommand::makeCommand( CommandT com, JointT jid, double d1, double d2 ){ if( com == CMD_JOINT && jid != JID_ILLEGAL ) { commandType = com; JID = jid; dSpeed1 = d1; dSpeed2 = d2; }}/*! This method copies information create a say message \param com command type \param msg message text to send */void SoccerCommand::makeCommand( CommandT com, char *msg ){ if( com == CMD_SAY && strlen(msg) > 0 ) strcpy( str, msg );}/*! This method checks weather information in class are valid or not \return bool indicating weather information residing in class are valid*/bool SoccerCommand::isIllegal( ){ return ( commandType == CMD_ILLEGAL );}/*! This operator logs internal information of joint to specified output \param os output stream to log information to \param cmd command to log information to os \return output stream */ostream & operator << (ostream &os, SoccerCommand cmd ){ cmd.show( os ); return os;}/*! This method prints internal information to specifies output \param os output stream to send information to*/void SoccerCommand::show( ostream& os ){ char str[1024]; makeCommandString( str ); os << str;}/*! This method return the command to as a message that server can understand \param str destination to write server message to \return bool indicating weather message is valid or not */bool SoccerCommand::getCommandString( char *str ) const{ return makeCommandString( str );}/*****************************************************************************//********************* CLASS SOCCERTYPES *************************************//*****************************************************************************//*! This constant defines the string names corresponding to the ObjectT names, defined in SoccerTypes.h. Players are not added since they depend on the team name. Note that the order is important, since the names are in the same order as the ObjectT enumeration. */const char * ObjectNames[] ={ "(ball)", "(goal l t)", "(goal l b)", "(goal r t)", "(goal r b)", "(goal ?)", "(corner l t)", "(corner l b)", "(corner r t)", "(corner r b)", "(corner ?)" };/*! This constants define the name of the joints */const char * JointNames[] ={ "llj1", "rlj1", "llj2_3", "rlj2_3", "llj4", "rlj4", "llj5_6", "rlj5_6", "laj1_2", "raj1_2", "laj3", "raj3", "laj4", "raj4", "joint_illegal"};/*! This constants define the string commands that server can understand them */const char * JointCommands[] ={ "lle1", "rle1", "lle2_3", "rle2_3", "lle4", "rle4", "lle5_6", "rle5_6", "lae1_2", "rae1_2", "lae3", "rae3", "lae4", "rae4", "joint_illegal"};/*! these constants define the names of the sensors */const char * SenseNames[] ={ "lf", "rf", "sense_illegal"};/*! These constants define all of the strings that can mean a playmode */const char * PlayModeNames[] = { "BeforeKickOff", "KickOff_Left", "KickOff_Right", "PlayOn", "KickIn_Left", "KickIn_Right", "corner_kick_left", "corner_kick_right", "goal_kick_left", "goal_kick_right", "offside_left", "offside_right", "quit", "Goal_Left", "Goal_Right", "free_kick_left", "free_kick_right", "illegal"};/*! This constants represent all of the strings that mean a side info */const char * SideNames[] ={ "our", "left", "right", "illegal"};/*! This method returns the string that corresponds to a specific object. This string name is exactly the same as the (short) name of the RoboCup Simulation. \param strBuf is the string in which the string representation is stored \param o ObjectT that has to be converted to the string representation \return pointer to strBuf, which contains the string representation */char* SoccerTypes::getObjectStr( char* strBuf, ObjectT o ){ if( o >= OBJECT_BALL && (int)o <= OBJECT_CORNER_UNKNOWN ) sprintf( strBuf, ObjectNames[(int)o] ); else if( isKnownPlayer( o ) && isTeammate( o ) ) sprintf( strBuf, "(teammate #%d)", getIndex( o ) + 1); else if( isKnownPlayer( o ) && isOpponent( o ) ) sprintf( strBuf, "(opponent #%d)", getIndex( o ) + 1); else if( o == OBJECT_PLAYER_UNKNOWN ) sprintf( strBuf, "(unknown player)" ); else sprintf( strBuf, "illegal: %d", (int)o ); return strBuf;}/*! This method returns an ObjectT that corresponds to the string passed as the first argument. The string representation equals the representation used in the Soccer Server. Format is with parenthesis, so possible arguments for str are (ball), (p Team_L 1), etc. \param ptrStr pointer to string containing string representation of object \return return the corresponding ObjectT, OBJECT_ILLEGAL in case of error */ObjectT SoccerTypes::getObjectFromStr( char* ptrStr ){ ObjectT o = OBJECT_ILLEGAL; switch( ptrStr[0] ) { case 'b': // (ball) case 'B': // (B) in case of ball very close o = OBJECT_BALL; break; case 'F': if( strlen( ptrStr ) < 3 ) return OBJECT_ILLEGAL; if( ptrStr[1] == '1' ) // Flag_1_l o = (ptrStr[2] == 'L') ? OBJECT_CORNER_L_T : OBJECT_CORNER_R_T; if( ptrStr[1] == '2' ) // Flag_2_l o = (ptrStr[2] == 'L') ? OBJECT_CORNER_L_B : OBJECT_CORNER_R_B; break; case 'G': if( strlen( ptrStr ) < 3 ) return OBJECT_ILLEGAL; if( ptrStr[1] == '1' ) // Goal_1_l o = (ptrStr[2] == 'L') ? OBJECT_GOAL_L_T : OBJECT_GOAL_R_T;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -