RobotCup2D代码学习记录(三)

RobotCup2D代码学习记录(三)
  今天整理的文档包括3个代码部分的阅读记录,这部分的代码相对来说是比较简单的,记录如下:
bhv_basic_move.cpp

//这里的话这个cpp里面只有execute函数,和别的文件一样,该函数的作用就是用判断BasicMove是否执行
bool
Bhv_BasicMove::execute( PlayerAgent * agent )
{
    dlog.addText( Logger::TEAM,
                  __FILE__": Bhv_BasicMove" );

    //-----------------------------------------------
    // tackle
    //这里把BasicTackle当做是basicmove的一部分
    if ( Bhv_BasicTackle( 0.8, 80.0 ).execute( agent ) )
    {
        #ifdef DEBUG2014
        std::cerr << agent->world().time() << __FILE__ << agent->world().self().unum() << __FILE__ << ": basic_tackle...\n";
        #endif // DEBUG2014

        return true;
    }

    const WorldModel & wm = agent->world();
    const PlayerObject * fastest_opp = wm.interceptTable()->fastestOpponent();//这里的字面意思是最快的对手?
    const PlayerPtrCont & opps = wm.opponentsFromSelf();//应该是一个数组或者是列表的形式,存储着可以获得的对方球员的信息。
    const PlayerObject * nearest_opp = ( opps.empty()
            ? static_cast< PlayerObject * >( 0 )
            : opps.front() );//空为0,要么就是默认的第一个
    const double nearest_opp_dist = ( nearest_opp
                                      ? nearest_opp->distFromSelf()
                                      : 1000.0 );//和上面一样,如果存在就是nearest_opp->distFromSelf(),否则是1000.0
    const Vector2D nearest_opp_pos = ( nearest_opp
                                       ? nearest_opp->pos()
                                       : Vector2D( -1000.0, 0.0 ) );
 const Vector2D fastest_opp_pos = ( fastest_opp
                                       ? fastest_opp->pos()
                                       : Vector2D( -1000.0, 0.0 ) );//这两个的写法和上面相同
    /*--------------------------------------------------------*/
    // chase ball
    const int self_min = wm.interceptTable()->selfReachCycle();
    const int mate_min = wm.interceptTable()->teammateReachCycle();
    const int opp_min = wm.interceptTable()->opponentReachCycle();

//**************************
    if ( ! wm.existKickableTeammate()
         && ( self_min <= 3
              || ( self_min <= mate_min
                   && self_min < opp_min + 3 )
              )
         )
    {
        dlog.addText( Logger::TEAM,
                      __FILE__": intercept" );
        Body_Intercept().execute( agent );
        agent->setNeckAction( new Neck_OffensiveInterceptNeck() );
//这个转向函数没有理解的特别清楚,猜测就是调整转向为进攻方向
        return true;
    }

    const Vector2D target_point = Strategy::i().getPosition( wm.self().unum() );
    //const
    		double dash_power = Strategy::get_normal_dash_power( wm );

    //ht: 20140830 00:23
    if ( wm.self().unum() == 11 || wm.self().unum() == 10 || wm.self().unum() == 9 )
    {
        if ( wm.ball().pos().x > 40.0 )
        {
            if ( wm.ball().vel().x > 1.2
                 || (wm.ball().vel().x > 0.0 && wm.self().pos().x < wm.ball().pos().x - 5.5))
            {
                dash_power = ServerParam::i().maxPower() * 1.4;
            }
            else
            {
                dash_power = ServerParam::i().maxPower();
            }

        }
    }
    else if ( wm.ourDefenseLineX()+2 > wm.ball().pos().x
                ||Strategy::get_ball_area(wm.ball().pos()) == Strategy::BA_Danger)
    {
        dash_power = ServerParam::i().maxPower();
    }

    else if ( (wm.self().unum() == 7 || wm.self().unum() == 8 )
             && wm.ball().pos().x > 36.0 && wm.ball().vel().x > 1.4 )
    {
            dash_power = ServerParam::i().maxPower() ;
    }

//上面的函数都是根据一些情形来设定maxPower的具体数值
//    double dist_thr = wm.ball().distFromSelf() * 0.1;
    double dist_thr = fabs( wm.ball().pos().x - wm.self().pos().x ) * 0.1;
    if ( dist_thr < 1.0 ) dist_thr = 1.0;

    if ( ! Body_GoToPoint( target_point, dist_thr, dash_power
                           ).execute( agent ) )
    {
        Body_TurnToBall().execute( agent );
    }

    if ( wm.existKickableOpponent()
         && wm.ball().distFromSelf() < 18.0 )
    {
        agent->setNeckAction( new Neck_TurnToBall() );
    }
    else
    {
        agent->setNeckAction( new Neck_TurnToBallOrScan() );
    }
//这里有3种转向Body_TurnToBall,Neck_TurnToBall,Neck_TurnToBallOrScan。具体的区别个人理解为,身体转向球可以联系设置的体力直接冲向球,Neck_TurnToBall是将视角对向球,Neck_TurnToBallOrScan是将视角对向球或者进行扫描球的位置,也就是说并不清楚球的位置。
    return true;
}


bhv_basic_offensive_kick.cpp
//基本的进攻形式的踢球函数
bool
Bhv_BasicOffensiveKick::execute( PlayerAgent * agent )
{
    dlog.addText( Logger::TEAM,
                  __FILE__": Bhv_BasicOffensiveKick" );

    const WorldModel & wm = agent->world();

    const PlayerPtrCont & opps = wm.opponentsFromSelf();
    const PlayerObject * nearest_opp
        = ( opps.empty()
            ? static_cast< PlayerObject * >( 0 )
            : opps.front() );
    const double nearest_opp_dist = ( nearest_opp
                                      ? nearest_opp->distFromSelf()
                                      : 1000.0 );
    const Vector2D nearest_opp_pos = ( nearest_opp
                                       ? nearest_opp->pos()
                                       : Vector2D( -1000.0, 0.0 ) );
//上面的这些定义和上一个文件的相同,这些都是一些基本的参数和变量。
    Vector2D pass_point;

    if( wm.self().pos().x>32) return false;
    if ( Body_Pass::get_best_pass( wm, &pass_point, NULL, NULL ) )
    {
        if ( (wm.self().pos().x < 0.0 &&
        	pass_point.x > wm.self().pos().x -5.0)
        		||(wm.self().pos().x >= 0.0 &&
        	        	pass_point.x > wm.self().pos().x -14.0))
        {
            bool safety = true;
            const PlayerPtrCont::const_iterator opps_end = opps.end();
            for ( PlayerPtrCont::const_iterator it = opps.begin();
                  it != opps_end;
                  ++it )
            {
                if ( (*it)->pos().dist( pass_point ) < 3.0 )
	        {
                    safety = false;
                }
            }
//进行一个简单的循环判断,当存在对方球员距离传球的位置肯近的时候(3.0)时,safety = false 认为传球的是不安全的

//出现这种情况就是说明上面的循环并不能全面覆盖
            if ( nearest_opp_dist < 3.0&&safety)
              {
                  if ( Body_Pass().execute( agent ) )
                  {
                      dlog.addText( Logger::TEAM,
                                    __FILE__": (execute) do best pass" );
                      agent->debugClient().addMessage( "OffKickPass(2)" );
                      agent->setNeckAction( new Neck_TurnToLowConfTeammate() );
//出现一种新的头部转向
                      return true;
                  }
              }
            return false;

        }
        return false;
    }
    // opp is far from me

    return false;

}

//yily changes it 2013-6-2
//和上面的传球代码基本一样,只是对一些参数进行修改
bool
Bhv_BasicOffensiveKick::execute_side_cross( PlayerAgent * agent )
{
    dlog.addText( Logger::TEAM,
                  __FILE__": Bhv_BasicOffensiveKick" );

    const WorldModel & wm = agent->world();

    const PlayerPtrCont & opps = wm.opponentsFromSelf();
    const PlayerObject * nearest_opp
        = ( opps.empty()
            ? static_cast< PlayerObject * >( 0 )
            : opps.front() );
    const double nearest_opp_dist = ( nearest_opp
                                      ? nearest_opp->distFromSelf()
                                      : 1000.0 );
    const Vector2D nearest_opp_pos = ( nearest_opp
                                       ? nearest_opp->pos()
                                       : Vector2D( -1000.0, 0.0 ) );

    Vector2D pass_point;

    if( wm.self().pos().x>32 && fabs(wm.self().pos().y)<17) return false;
    if ( Body_Pass::get_best_pass( wm, &pass_point, NULL, NULL ) )
    {
        if ( (wm.self().pos().x < 0.0 &&
        	pass_point.x > wm.self().pos().x -5.0)
        		||(wm.self().pos().x >= 0.0 &&
        	        	pass_point.x > wm.self().pos().x -15.0))
        {
            bool safety = true;
            const PlayerPtrCont::const_iterator opps_end = opps.end();
            for ( PlayerPtrCont::const_iterator it = opps.begin();
                  it != opps_end;
                  ++it )
            {
                if ( (*it)->pos().dist( pass_point ) < 4.0 )
	        {
                    safety = false;
                }
            }


            if ( safety)
              {
                  if ( Body_Pass().execute( agent ) )
                  {
                      dlog.addText( Logger::TEAM,
                                    __FILE__": (execute) do best pass" );
                      agent->debugClient().addMessage( "OffKickPass(2)" );
                      agent->setNeckAction( new Neck_TurnToLowConfTeammate() );

                      return true;
                  }
              }
            return false;


        }
        return false;
    }
    // opp is far from me

    return false;
}

//基本的铲球函数
bhv_basic_tackle.cpp
bool
Bhv_BasicTackle::execute( PlayerAgent * agent )
{
    const ServerParam & SP = ServerParam::i();
    const WorldModel & wm = agent->world();

    bool use_foul = false;//当前球员是否犯规
    double tackle_prob = wm.self().tackleProbability();//铲球的可能性,应该是服务器传过来的参数

    if ( agent->config().version() >= 14.0
         && wm.self().card() == NO_CARD//没有收到过牌,很接近真实比赛
         && ( wm.ball().pos().x > SP.ourPenaltyAreaLineX() + 0.5
              || wm.ball().pos().absY() > SP.penaltyAreaHalfWidth() + 0.5 )//罚球区
         && tackle_prob < wm.self().foulProbability() )
    {
        tackle_prob = wm.self().foulProbability();
        use_foul = true;
    }

    if ( tackle_prob < M_min_probability )
    {
        dlog.addText( Logger::TEAM,
                      __FILE__": failed. low tackle_prob=%.2f < %.2f",
                      wm.self().tackleProbability(),
                      M_min_probability );
        return false;
    }

    const int self_min = wm.interceptTable()->selfReachCycle();
    const int mate_min = wm.interceptTable()->teammateReachCycle();
    const int opp_min = wm.interceptTable()->opponentReachCycle();

    const Vector2D self_reach_point = wm.ball().inertiaPoint( self_min );//惯性点

    //
    // check where the ball shall be gone without tackle
    //

    bool ball_will_be_in_our_goal = false;

    if ( self_reach_point.x < -SP.pitchHalfLength() )
    {
        const Ray2D ball_ray( wm.ball().pos(), wm.ball().vel().th() );
        const Line2D goal_line( Vector2D( -SP.pitchHalfLength(), 10.0 ),
                                Vector2D( -SP.pitchHalfLength(), -10.0 ) );

        const Vector2D intersect = ball_ray.intersection( goal_line );
        if ( intersect.isValid()
             && intersect.absY() < SP.goalHalfWidth() + 1.0 )
        {
            ball_will_be_in_our_goal = true;

            dlog.addText( Logger::TEAM,
                          __FILE__": ball will be in our goal. intersect=(%.2f %.2f)",
                          intersect.x, intersect.y );
        }
    }
//虽然上述的两个类型不太理解,但猜测执行如下:就是将球惯性滑动的轨迹与自己向目标前进的轨迹存储起来,然后判断是否有交点,如果有就认为球在我们的目标之下。
    if ( wm.existKickableOpponent()
         || ball_will_be_in_our_goal
         || ( opp_min < self_min - 3
              && opp_min < mate_min - 3 )
         || ( self_min >= 5
              && wm.ball().pos().dist2( SP.theirTeamGoalPos() ) < std::pow( 10.0, 2 )
              && ( ( SP.theirTeamGoalPos() - wm.self().pos() ).th() - wm.self().body() ).abs() < 45.0 )
         )
    {
        // try tackle
    }
    else
    {
        dlog.addText( Logger::TEAM,
                      __FILE__": Bhv_BasicTackle. not necessary" );
        return false;
    }

    if ( agent->config().version() < 12.0 )
    {
        // v11 or older
        return executeOld( agent );

    }
    else if ( agent->config().version() < 14.0 )
    {
        // v12-13
        return executeV12( agent );
    }

    // v14+
    return executeV14( agent, use_foul );
}
//上述的几个执行函数均在下面有定义
/*-------------------------------------------------------------------*/
/*!

 */

上面的执行函数的理解还存在一些问题,在进一步理解后再做记录。

  现在看待代码的速度已经有了很大度的提升,虽然部分理解上仍然有问题,还需要查看资料,但对于初步进行代码得到修改已经有了想法。预计在一周后就正式开始代码的优化修改。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值