coach.cpp
来自「2009 ROBOCUP 仿真2DSERVER 源码」· C++ 代码 · 共 1,850 行 · 第 1/4 页
CPP
1,850 行
{ M_stadium.set_ball( NEUTRAL, PVector( x, y ), PVector( velx, vely ) ); } else { send( "(error illegal_command_form)" ); return; } } else { char teamname[128]; int unum = 0; if ( std::sscanf( obj_name.c_str(), PLAYER_NAME_FORMAT, teamname, &unum ) != 2 || unum < 1 || MAX_PLAYER < unum ) { send( "(error illegal_object_form)" ); return; } Side side = ( M_stadium.teamLeft().name() == teamname ? LEFT : M_stadium.teamRight().name() == teamname ? RIGHT : NEUTRAL ); PVector pos( x, y ); PVector vel( velx, vely ); ang = Deg2Rad( rcss::bound( ServerParam::instance().minMoment(), ang, ServerParam::instance().maxMoment() ) ); if ( n == 3 ) { M_stadium.movePlayer( side, unum, pos, NULL, NULL ); } else if ( n == 4 ) { M_stadium.movePlayer( side, unum, pos, &ang, NULL ); } else if ( n == 6 ) { M_stadium.movePlayer( side, unum, pos, &ang, &vel ); } else { send( "(error illegal_command_form)" ); return; } } send( "(ok move)" );}voidCoach::ear( std::string mode ){ if ( mode == "on" ) { M_hear = true; send( "(ok ear on)" ); } else if ( mode == "off" ) { M_hear = false; send( "(ok ear off)" ); } else { send( "(error illegal_command_form)" ); }}voidCoach::eye( std::string mode ){ if ( mode == "on" ) { M_eye = true; //send( "(ok eye on)" ); } else if ( mode == "off" ) { M_eye = false; //send( "(ok eye off)" ); } else { send( "(error illegal_command_form)" ); return; } sendOKEye();}voidCoach::compression( int level ){#ifdef HAVE_LIBZ if( level > 9 ) { send( "(error illegal_command_form)" ); return; }#ifdef HAVE_SSTREAM std::ostringstream reply; reply << "(ok compression " << level << ")"; send ( reply.str().c_str() );#else std::ostrstream reply; reply << "(ok compression " << level << ")" << std::ends; send ( reply.str() ); reply.freeze( false );#endif setCompressionLevel ( level );#else send ( "(warning compression_unsupported)" );#endif}voidCoach::look(){ if ( M_assigned ) { M_observer->sendLook(); }// #ifdef HAVE_SSTREAM// std::ostringstream ost;// #else// std::ostrstream ost;// #endif// ost << "(ok look " << stad.time();// if ( version() >= 7.0 )// ost << " (" << GOAL_L_NAME_SHORT;// else// ost << " (" << GOAL_L_NAME;// ost << " " << -(PITCH_LENGTH/2.0)// << " " << 0.0// << ")" ;// if ( version() >= 7.0 )// ost << " (" << GOAL_R_NAME_SHORT;// else// ost << " (" << GOAL_R_NAME;// ost << " " << (PITCH_LENGTH/2.0)// << " " << 0.0// << ")" ;// if ( version() >= 7.0 )// ost << " (" << BALL_NAME_SHORT;// else// ost << " (" << BALL_NAME;// ost << " " << stad.ball().pos().x// << " " << stad.ball().pos().y// << " " << stad.ball().vel().x// << " " << stad.ball().vel().y// << ")" ;// const Stadium::PlayerCont::const_iterator end = stad.players().end();// for ( Stadium::PlayerCont::const_iterator p = stad.players().begin();// p != end;// ++p )// {// if ( (*p)->alive == DISABLE )// continue ;// if ( version() >= 7.0 )// ost << " (" << (*p)->shortName()// << " " << (*p)->pos().x// << " " << (*p)->pos().y// << " " << (*p)->vel().x// << " " << (*p)->vel().y// << " " << Rad2IDegRound( (*p)->angleBodyCommitted() )// << " " << Rad2IDegRound( (*p)->angleNeckCommitted() )// << ")" ;// else// ost << " (" << (*p)->name()// << " " << (*p)->pos().x// << " " << (*p)->pos().y// << " " << Rad2IDeg( (*p)->angleBodyCommitted() )// << " " << Rad2IDeg( (*p)->angleNeckCommitted() )// << " " << (*p)->vel().x// << " " << (*p)->vel().y// << ")" ;// }// ost << ")" << std::endl;// #ifdef HAVE_SSTREAM// send(ost.str().c_str());// #else// ost << std::ends;// send(ost.str());// ost.freeze();// #endif}voidCoach::team_names(){#ifdef HAVE_SSTREAM std::ostringstream ost;#else std::ostrstream ost;#endif ost << "(ok team_names"; if ( ! M_stadium.teamLeft().name().empty() ) { ost << " (team l " << M_stadium.teamLeft().name() << ")"; } if ( ! M_stadium.teamRight().name().empty() ) { ost << " (team r " << M_stadium.teamRight().name() << ")"; } ost << ")" << std::endl;#ifdef HAVE_SSTREAM send( ost.str().c_str() );#else ost<< std::ends; send( ost.str() ); ost.freeze();#endif}voidCoach::recover(){ M_stadium.recoveryPlayers();#ifdef HAVE_SSTREAM std::ostringstream ost; ost << "(ok recover)" << std::endl; send( ost.str().c_str() );#else std::ostrstream ost; ost << "(ok recover)" << std::endl << std::ends; send( ost.str() ); ost.freeze();#endif}voidOnlineCoach::change_player_types( const char * command ){ if ( M_stadium.time() > 0 ) { send( "(warning only_before_kick_off)" ); return; } else if ( M_stadium.time() >= ( ( ServerParam::instance().halfTime() * ServerParam::instance().nrNormalHalfs() ) + ( ServerParam::instance().extraHalfTime() * ServerParam::instance().nrExtraHalfs() ) ) ) { send( "(warning no_subs_left)" ); return; } const Team * team = NULL; if ( side() == LEFT ) { team = &( M_stadium.teamLeft() ); } if ( side() == RIGHT ) { team = &( M_stadium.teamRight() ); } if ( team == NULL ) { send( "(warning no_team_found)" ); return; } if ( team->subsCount() >= PlayerParam::instance().subsMax() ) { send( "(warning no_subs_left)" ); return; } int n_read = 0; std::sscanf( command, " (change_player_types %n ", &n_read ); if ( n_read == 0 ) { send( "(error illegal_command_form)" ); return; } command += n_read; // // parse the player type assignment // // key: unum, value: type id std::map< const Player *, int > new_assign_map; while ( *command == '(' ) { int unum = 0, type = -1; int n_read = 0; if ( std::sscanf( command, " ( %d %d ) %n ", &unum, &type, &n_read ) != 2 || n_read == 0 ) { send( "(error illegal_command_form)" ); return; } command += n_read; if ( type < 0 || PlayerParam::instance().playerTypes() <= type ) { send( "(error out_of_range_player_type)" ); return; } const Player * player = NULL; for ( int i = 0; i < team->size(); ++i ) { const Player * p = team->player( i ); if ( p && p->state() != DISABLE && p->unum() == unum ) { player = p; break; } } if ( ! player ) { send( "(error no_such_player)" ); return; } if ( player->isGoalie() && type != 0 ) { send( "(warning cannot_change_goalie)" ); return; } new_assign_map.insert( std::make_pair( player, type ) ); } if ( new_assign_map.empty() ) { send( "(error illegal_command_form)" ); return; } // copy the assigment map contained in the command const std::map< const Player *, int > command_assign_map = new_assign_map; // add the prevous assignment for ( int i = 0; i < team->size(); ++i ) { const Player * p = team->player( i ); if ( p ) { std::map< const Player *, int >::iterator it = new_assign_map.find( p ); if ( it == new_assign_map.end() ) { new_assign_map.insert( std::make_pair( p, p->playerTypeId() ) ); } } } // check the total number of each player type { // key: type id, value: count std::map< int, int > type_count; for ( int id = 0; id < PlayerParam::instance().playerTypes(); ++id ) { type_count[ id ] = 0; } for ( std::map< const Player *, int >::iterator it = new_assign_map.begin(); it != new_assign_map.end(); ++it ) { type_count[ it->second ] += 1; } for ( std::map< int, int >::const_iterator it = type_count.begin(); it != type_count.end(); ++it ) { if ( ServerParam::instance().verboseMode() ) { std::cout << "change_player_types: the number of type " << it->first << " = " << it->second << std::endl; } if ( it->first == 0 && PlayerParam::instance().allowMultDefaultType() ) { // no error } else if ( it->second > PlayerParam::instance().ptMax() ) { char err_msg[128]; snprintf( err_msg, 128, "(warning max_of_type_%d_on_field)", it->first ); send( err_msg ); return; } } } // substitute & send ok message for ( std::map< const Player *, int >::const_iterator it = command_assign_map.begin(); it != command_assign_map.end(); ++it ) { if ( team->subsCount() >= PlayerParam::instance().subsMax() ) { send( "(warning no_subs_left)" ); return; } if ( ServerParam::instance().verboseMode() ) { std::cerr << "change_player_types: substitute (player " << team->name() << ' ' << it->first->unum() << ") to type " << it->second << std::endl; } M_stadium.substitute( it->first, it->second ); char buf[64]; snprintf( buf, 64, "(ok change_player_type %d %d)", it->first->unum(), it->second ); send( buf ); }}voidCoach::change_player_type( const std::string & team_name, int unum,
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?