coach.cpp

来自「2009 ROBOCUP 仿真2DSERVER 源码」· C++ 代码 · 共 1,850 行 · 第 1/4 页

CPP
1,850 行
字号
                           int player_type ){    const Team* team = NULL;    if ( M_stadium.teamLeft().name() == team_name )    {        team = &( M_stadium.teamLeft() );    }    if ( M_stadium.teamRight().name() == team_name )    {        team = &( M_stadium.teamRight() );    }    if ( team == NULL )    {        send( "(warning no_team_found)" );        return;    }    if ( player_type < 0         || player_type >= PlayerParam::instance().playerTypes() )    {        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->unum() == unum )        {            player = p;            break;        }    }    if ( player == NULL )    {        send( "(warning no_such_player)" );        return;    }    M_stadium.substitute( player, player_type );    char buf[64];    snprintf ( buf, 64, "(ok change_player_type %s %d %d)",               team_name.c_str(), unum, player_type );    send( buf );}voidCoach::send_visual_info(){    M_observer->sendVisual();// #ifdef HAVE_SSTREAM//     std::ostringstream ost;// #else//     std::ostrstream ost;// #endif//     if ( version() >= 7.0 )//         ost << "(see_global " << M_stadium.time();//     else//         ost << "(see " << M_stadium.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 << " " << M_stadium.ball().pos().x//         << " " << M_stadium.ball().pos().y//         << " " << M_stadium.ball().vel().x//         << " " << M_stadium.ball().vel().y//         << ")" ;//     const Stadium::PlayerCont::const_iterator end = M_stadium.players().end();//     for ( Stadium::PlayerCont::const_iterator p = M_stadium.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() );//             if ( version() >= 8.0 )//             {//                 double arm_dir;//                 if ( (*p)->getArm().getRelDir ( rcss::geom::Vector2D( (*p)->pos().x,//                                                                       (*p)->pos().y ),//                                                 (*p)->angleBodyCommitted()//                                                 + (*p)->angleNeckCommitted(),//                                                 arm_dir ) )//                     ost << " " << Rad2IDegRound( normalize_angle( arm_dir//                                                                   + (*p)->angleNeckCommitted()//                                                                   + (*p)->angleBodyCommitted() ) );//                 if ( (*p)->isTackling() )//                     ost << " t";//             }//             ost << ")" ;//         }//         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( false );// #endif}voidCoach::check_ball(){#ifdef HAVE_SSTREAM    std::ostringstream ost;#else    std::ostrstream ost;#endif    static const char * BallPosInfoStr[] = BALL_POS_INFO_STRINGS;    BallPosInfo info = M_stadium.ballPosInfo();    ost << "(ok check_ball " << M_stadium.time() << " " ;    ost << BallPosInfoStr[info] << ")";    ost << std::ends;#ifdef HAVE_SSTREAM    send( ost.str().c_str() );#else    ost << std::ends;    send( ost.str() );    ost.freeze( false );#endif}OnlineCoach::OnlineCoach( Stadium & stadium )    : Coach( stadium ){    M_side = NEUTRAL;    M_freeform_messages_said = 0;    M_freeform_messages_allowed = ServerParam::instance().freeformCountMax();    M_define_messages_left = -1;    M_advice_messages_left = -1;    M_info_messages_left = -1;    M_meta_messages_left = -1;    M_del_messages_left = -1;    M_rule_messages_left = -1;    M_msg_left_update_time = -1;}OnlineCoach::~OnlineCoach(){    while ( ! M_message_queue.empty() )    {        rcss::clang::Msg * msg = *M_message_queue.begin();        M_message_queue.pop_front();        delete msg;    }}voidOnlineCoach::disable(){    if ( M_assigned )    {        //std::cout << "An online coach disconnected\n";        std::cout << "An online coach disconnected : ("                  << ( side() == LEFT                       ? M_stadium.teamLeft().name()                       : M_stadium.teamRight().name() );        if ( ! name().empty() ) std::cout <<  " " << name();        std::cout << ")\n";    }    M_assigned = false;    if ( connected() )    {        RemoteClient::close();    }}boolOnlineCoach::setSenders( const double & client_version ){    M_version = client_version;    rcss::SerializerOnlineCoach::Creator creator;    if ( ! rcss::SerializerOnlineCoach::factory().getCreator( creator,                                                              (int)client_version ) )    {        return false;    }    const rcss::SerializerOnlineCoach* ser = creator();    if ( ! ser )    {        return false;    }    this->setSerializer( *ser );    // visual    rcss::VisualSenderCoach::Params visual_params( getTransport(),                                                   *this,                                                   ser->coachSerializer(),                                                   M_stadium );    rcss::VisualSenderCoach::Creator visual_cre;    if ( ! rcss::VisualSenderCoach::factory().getCreator( visual_cre,                                                          (int)client_version ) )    {        return false;    }    M_observer->setVisualSender( visual_cre( visual_params ) );    // audio    rcss::AudioSenderOnlineCoach::Params audio_params( getTransport(),                                                       *this,                                                       *ser,                                                       M_stadium );    rcss::AudioSenderOnlineCoach::Creator audio_cre;    if ( ! rcss::AudioSenderOnlineCoach::factory().getCreator( audio_cre,                                                               (int)client_version ) )    {        return false;    }    this->setAudioSender( audio_cre( audio_params ) );    // init    rcss::InitSenderOnlineCoach::Params init_params( getTransport(),                                                     *this,                                                     *ser,                                                     M_stadium );    rcss::InitSenderOnlineCoach::Creator init_cre;    if ( ! rcss::InitSenderOnlineCoach::factory().getCreator( init_cre,                                                              (int)client_version ) )    {        return false;    }    M_init_observer_olcoach = new rcss::InitObserverOnlineCoach;    //rcss::InitSenderOnlineCoach::Ptr isoc = init_cre( init_params );    //rcss::InitObserver< rcss::InitSenderOnlineCoach >::setInitSender( isoc );    M_init_observer_olcoach->setInitSender( init_cre( init_params ) );    M_assigned = true;    return true;}voidOnlineCoach::sendInit(){    M_init_observer_olcoach->sendInit();    M_init_observer_olcoach->sendServerParams();    M_init_observer_olcoach->sendPlayerParams();    M_init_observer_olcoach->sendPlayerTypes();    M_init_observer_olcoach->sendChangedPlayers();}voidOnlineCoach::send( const char * msg ){    if ( RemoteClient::send( msg, std::strlen( msg ) + 1 ) != -1 )    {        M_stadium.logger().writeOnlineCoachLog( *this, msg, SEND );    }    else    {        std::cerr << __FILE__ << ": " << __LINE__ << ": ";        perror( "Error sending to online coach" );    }}voidOnlineCoach::parseMsg( const char * msg,                       const size_t & len ){    char * str = const_cast< char * >( msg );    if ( str[ len - 1 ] != 0 )    {        if ( version() >= 8.0 )        {            send( "(warning message_not_null_terminated)" );        }        str[ len ] = 0;    }    M_stadium.logger().writeOnlineCoachLog( *this, str, RECV );    parse_command( str );}voidOnlineCoach::parse_command( const char * command ){    char com[MaxMesg];    int n;    n = std::sscanf( command, "(%[-0-9a-zA-Z.+*/?<>_]", com );    if ( n < 1 )    {        send( "(error illegal_command_form)" );        return;    }    if ( ! std::strcmp( com, "check_ball" ) )    {        check_ball();    }    else if ( ! std::strcmp( com, "look" ) )    {        look();    }    else if ( ! std::strcmp( com, "team_names" ) )    {        team_names();    }    else if ( ! std::strcmp( com, "say" ) )    {        if ( version() >= 7.0 )        {            /* parsing for new coach language */            rcss::clang::MsgBuilder builder;            builder.setFreeformMsgSize( ServerParam::instance().freeformMsgSize() );            rcss::clang::Parser parser( builder );            int ret = parser.parse( command );            if ( ret == 0 && builder.getMsg() != NULL )            {                //succeful parse                bool should_queue = false;                rcss::clang::Msg * msg = builder.detatchMsg().release();                msg->setTimeRecv( M_stadium.time() );                switch ( msg->getType() ) {                case rcss::clang::Msg::META:                    if ( M_stadium.playmode() != PM_PlayOn )                    {                        should_queue = true;                    }                    else if ( M_meta_messages_left != 0 )                    {                        //remember, negative implies we can send as many as we want                        --M_meta_messages_left;                        should_queue = true;                    }                    else                    {                        send( "(error said_too_many_meta_messages)" );                    }                    break;                case rcss::clang::Msg::FREEFORM:                    //if ( M_stadium.playmode() != PM_PlayOn                    //     || M_stadium.canSendFreeform() )                    if ( canSendFreeform() )                    {                        if ( M_freeform_messages_said < M_freeform_messages_allowed                             || M_freeform_messages_allowed < 0 )                        {                            should_queue = true;                            ++M_freeform_messages_said;                        }                        else                        {                            send( "(error said_too_many_freeform_messages)" );                        }                    }                    else                    {                        send( "(error cannot_say_freeform_while_playon)" );                    }                    break;                case rcss::clang::Msg::INFO:                    if ( M_stadium.playmode() != PM_PlayOn )                    {                        should_queue = true;                    }                    else if ( M_info_messages_left != 0 )                    {                        //remember, negative implies we can send as many as we want                        --M_info_messages_left;                        should_queue = true;                    }                    else                    {                        send( "(error said_too_many_info_messages)" );                    }                    break;                case rcss::clang::Msg::ADVICE:                    if( M_stadium.playmode() != PM_PlayOn )                    {                        should_queue = true;                    }                    else if ( M_advice_messages_left != 0 )                    {                        //remember, negative implies we can send as many as we want                        --M_advice_messages_left;                        should_queue = true;                    }                    else                    {                        send( "(error said_too_many_advice_messages)" );                    }                    break;                case rcss::clang::Msg::DEFINE:                    if ( M_stadium.playmode() != PM_PlayOn )                    {                        should_queue = true;

⌨️ 快捷键说明

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