player.cpp

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

CPP
2,189
字号
Player::move( double x,              double y ){    if ( ! M_command_done )    {        if ( M_stadium.playmode() == PM_BeforeKickOff ||             M_stadium.playmode() == PM_AfterGoal_Right ||             M_stadium.playmode() == PM_AfterGoal_Left             )        {            M_pos.x = x * side();            M_pos.y = y * side();            M_stadium.collisions();        }        else if ( ( M_stadium.playmode() == PM_FreeKick_Left                    || M_stadium.playmode() == PM_FreeKick_Right )                  && M_stadium.ballCatcher() == this )        {            if ( ServerParam::instance().goalieMaxMoves() < 0                 || M_goalie_moves_since_catch < ServerParam::instance().goalieMaxMoves() )            {                M_pos.x = x * side();                M_pos.y = y * side();                ++M_goalie_moves_since_catch;            }            else            {                send( "(error too_many_moves)" );            }        }        else        {            return;        }        M_command_done = true;        ++M_move_count;    }}voidPlayer::change_view( rcss::pcom::VIEW_WIDTH viewWidth,                     rcss::pcom::VIEW_QUALITY viewQuality ){    if ( M_synch_see         && viewQuality != rcss::pcom::HIGH  )    {        return;    }    if ( viewWidth == rcss::pcom::NARROW )    {        if ( ! M_synch_see )        {            M_vis_angle = defangle / 2.0;            M_vis_send = 2;        }        else        {            M_vis_angle = defangle * ( 2.0 / 3.0 ); // == sim_step / send_step            M_vis_send = 1;        }    }    else if ( viewWidth == rcss::pcom::NORMAL )    {        if ( ! M_synch_see )        {            M_vis_angle = defangle;            M_vis_send = 4;        }        else        {            M_vis_angle = defangle * ( 4.0 / 3.0 ); // == 2 * sim_step / send_step            M_vis_send = 2;        }    }    else if ( viewWidth == rcss::pcom::WIDE )    {        if ( ! M_synch_see )        {            M_vis_angle = defangle * 2.0;            M_vis_send = 8;        }        else        {            M_vis_angle = defangle * ( 6.0 / 3.0 ); // == 3 * sim_step / send_step            M_vis_send = 3;        }    }    else    {        return;    }    M_view_width = viewWidth;    if ( viewQuality == rcss::pcom::HIGH )    {        M_highquality = true;    }    else if ( viewQuality == rcss::pcom::LOW )    {        if ( ! M_synch_see )        {            M_vis_send /= 2;            M_highquality = false;        }        else        {            return;        }    }    else    {        return;    }    ++M_change_view_count;}voidPlayer::change_view( rcss::pcom::VIEW_WIDTH viewWidth ){    if ( viewWidth == rcss::pcom::NARROW )    {        if ( ! M_synch_see )        {            M_vis_angle = defangle / 2.0;            M_vis_send = 2;        }        else        {            M_vis_angle = defangle * ( 2.0 / 3.0 ); // == sim_step / send_step            M_vis_send = 1;        }    }    else if ( viewWidth == rcss::pcom::NORMAL )    {        if ( ! M_synch_see )        {            M_vis_angle = defangle;            M_vis_send = 4;        }        else        {            M_vis_angle = defangle * ( 4.0 / 3.0 ); // == 2 * sim_step / send_step            M_vis_send = 2;        }    }    else if ( viewWidth == rcss::pcom::WIDE )    {        if ( ! M_synch_see )        {            M_vis_angle = defangle * 2.0;            M_vis_send = 8;        }        else        {            M_vis_angle = defangle * ( 6.0 / 3.0 ); // == 3 * sim_step / send_step            M_vis_send = 3;        }    }    else    {        return;    }    M_view_width = viewWidth;    M_highquality = true;    ++M_change_view_count;}voidPlayer::compression( int level ){#ifdef HAVE_LIBZ    if ( level > 9 )    {        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}voidPlayer::bye(){    disable();    M_command_done = true;}voidPlayer::done(){    M_done_received = true;}voidPlayer::pointto( bool on, double dist, double head ){    if ( on == false )    {        M_arm.stopPointing();    }    else    {        M_arm.pointTo( rcss::geom::Vector2D( pos().x, pos().y ),                       angleBodyCommitted() + angleNeckCommitted(),                       rcss::geom::polarVector2D( dist, Deg2Rad( head ) ) );    }}voidPlayer::attentionto( bool on,                     rcss::pcom::TEAM team_side,                     std::string team_name,                     int at_unum ){    if ( on == false )    {        // turn attention to off        focusOff();        ++M_attentionto_count;    }    else    {        const Team * at_team = NULL;        if ( team_side == rcss::pcom::OUR )        {            at_team = team();        }        else if ( team_side == rcss::pcom::OPP )        {            if ( side() == LEFT )            {                at_team = &( M_stadium.teamRight() );            }            else            {                at_team = &( M_stadium.teamLeft() );            }        }        else if ( team_side == rcss::pcom::LEFT_SIDE )        {            at_team = &( M_stadium.teamLeft() );        }        else if ( team_side == rcss::pcom::RIGHT_SIDE )        {            at_team = &( M_stadium.teamRight() );        }        else if ( team_name == M_stadium.teamLeft().name() )        {            at_team = &( M_stadium.teamLeft() );        }        else if ( team_name == M_stadium.teamRight().name() )        {            at_team = &( M_stadium.teamRight() );        }        else        {            return;        }        if ( ! at_team->enabled() )        {            return;        }        if ( at_unum < 1 || at_unum > at_team->size() )        {            return;        }        // make sure we aren't trying to focus on ourselves.        if ( at_team == team() && at_unum == unum() )        {            return;        }        for ( int i = 0; i < at_team->size(); ++i )        {            const Player * p = at_team->player( i );            if ( p && at_unum == p->unum() )            {                // turn attention to on                focusOn( *( at_team->player( at_unum - 1  ) ) );                ++M_attentionto_count;                break;            }        }    }}voidPlayer::tackle( double power_or_angle ){    if ( ! M_command_done         && ! isTackling() )    {        M_command_done = true;        M_tackle_cycles = ServerParam::instance().tackleCycles();        ++M_tackle_count;        PVector player_2_ball = M_stadium.ball().pos() - pos();        player_2_ball.rotate( - angleBodyCommitted() );        double tackle_dist = ( player_2_ball.x > 0.0                               ? ServerParam::instance().tackleDist()                               : ServerParam::instance().tackleBackDist() );        if ( std::fabs( tackle_dist ) <= 1.0e-5 )        {            M_state |= TACKLE_FAULT;            return;        }        // tackle failure probability        double prob = ( std::pow( std::fabs( player_2_ball.x ) / tackle_dist,                                  ServerParam::instance().tackleExponent() )                        + std::pow( std::fabs( player_2_ball.y )                                    / ServerParam::instance().tackleWidth(),                                    ServerParam::instance().tackleExponent() ) );        if ( prob < 1.0 )        {            boost::bernoulli_distribution<> rng( 1 - prob );            boost::variate_generator< rcss::random::DefaultRNG &,                boost::bernoulli_distribution<> >                dst( rcss::random::DefaultRNG::instance(), rng );            if ( dst() )            {                M_state |= TACKLE;                if ( M_stadium.playmode() == PM_BeforeKickOff ||                     M_stadium.playmode() == PM_AfterGoal_Left ||                     M_stadium.playmode() == PM_AfterGoal_Right  ||                     M_stadium.playmode() == PM_OffSide_Left ||                     M_stadium.playmode() == PM_OffSide_Right ||                     M_stadium.playmode() == PM_Back_Pass_Left ||                     M_stadium.playmode() == PM_Back_Pass_Right ||                     M_stadium.playmode() == PM_Free_Kick_Fault_Left ||                     M_stadium.playmode() == PM_Free_Kick_Fault_Right ||                     M_stadium.playmode() == PM_TimeOver )                {                    return;                }                double power_rate = 1.0;                PVector accel( 0.0, 0.0 );                // 2008-02-07 akiyama                // new tackle model based on the Thomas Gabel's proposal                if ( version() >= 12.0 )                {                    double angle = NormalizeMoment( power_or_angle );                    double eff_power                        = ( ServerParam::instance().maxBackTacklePower()                            + ( ( ServerParam::instance().maxTacklePower()                                  - ServerParam::instance().maxBackTacklePower() )                                * ( 1.0 - ( std::fabs( angle ) / M_PI ) )                                //* ( 1.0 - std::pow( std::fabs( angle ) / M_PI, 2.0 ) )                                )                            )                        * ServerParam::instance().tacklePowerRate();                    eff_power *= 1.0 - 0.5*( std::fabs( player_2_ball.th() ) / M_PI );                    accel = PVector::fromPolar( eff_power,                                                angle + angleBodyCommitted() );//                     std::cerr << M_stadium.time()//                               << ": v12 tackle arg=" << power_or_angle//                               << " angle=" << angle//                               << " angl_diff=" << Rad2Deg( player_2_ball.th() )//                               << " eff_power=" << eff_power//                               << std::endl;                }                else                {                    // Calculate the accleration that will be applied to the ball                    double power = NormalizeTacklePower( power_or_angle );                    double eff_power = power * ServerParam::instance().tacklePowerRate();                    if ( power >= 0.0 )                    {                        power_rate = power / ServerParam::instance().maxTacklePower();                    }                    else if ( std::fabs( power ) < 1.0e-10 )                    {                        power_rate = 0.0;                    }                    else if ( std::fabs( ServerParam::instance().maxBackTacklePower() ) > 1.0e-10 )                    {                        power_rate = -power / ServerParam::instance().maxBackTacklePower();                    }                    // Reduce the acceleration by the probability of kicking it.                    // For instance when the ball is on the boundry of the tackle                    // area, the acceleration is very low as one only barely touches                    // the ball.                    //eff_power *= 1 - prob;                    eff_power *= 1.0 - 0.5*( std::fabs( player_2_ball.th() ) / M_PI );                    accel = PVector::fromPolar( eff_power,                                                angleBodyCommitted() );//                     std::cerr << M_stadium.time()//                               << ": v11 tackle arg=" << power_or_angle//                               << " power=" << power//                               << " angl_diff=" << Rad2Deg( player_2_ball.th() )//                               << " eff_power=" << eff_power//                               << std::endl;                }//                 // pfr 8/14/00: for RC2000 evaluation//                 // add noise to kick//                 {//                     double maxrnd = ( M_kick_rand * power * ( 1 - prob )//                                       / ServerParam::instance().maxPower() );

⌨️ 快捷键说明

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