📄 basicplayer.cpp
字号:
return SoccerCommand( CMD_KICK, dPower, angActual ); } // else determine vector that is in direction 'velDes' (magnitude is lower) // 在上面没有 return 的情况下一定是速度或者力度不可达到,将尽力踢球 dPower = SS->getMaxPower();//最大力度 double dSpeed = WM->getActualKickPowerRate() * dPower;//预测踢球后的实际球速 //这里是为了让球尽量的偏向目的速度的角度。原因是KICK角度有一定的限制? double tmp = velBall.rotate(-velDes.getDirection()).getY();//球速逆时针旋转(目的速度的方向),然后取Y坐标 angActual = velDes.getDirection() - asinDeg( tmp / dSpeed );//求出实际踢球角度 angActual = VecPosition::normalizeAngle( angActual - angBody );//标准化 return SoccerCommand( CMD_KICK, dPower, angActual );}/*! This skill enables an agent to catch the ball and can only be executed when the agent is a goalkeeper. It returns a catch command that takes the angle of the ball relative to the body of the agent as its only argument. The correct value for this argument is computed by determining the global direction between the current ball position and the agent's current position and by making this direction relative to the agent's global body angle. \return SoccerCommand to catch the ball *//* 扑球,守门员基本指令 */SoccerCommand BasicPlayer::catchBall(){ // true means returned angle is relative to body instead of neck // true 表示返回的角度是相对于身体的,代替了原来的相对于脖子 return SoccerCommand( CMD_CATCH, WM->getRelativeAngle( OBJECT_BALL, true ));}/*! This skill enables an agent to communicate with other players on the field. It receives a string message as its only argument and returns a say command that causes the message to be broadcast to all players within a certain distance from the speaker. \return SoccerCommand to say the specified string 'str' *//* 球员的喊话功能,令球员在一定范围内将str字串参数内容广播 */SoccerCommand BasicPlayer::communicate( char *str ){ return SoccerCommand( CMD_SAY, str );}/*! This method returns a 'move' command to teleport the agent directly to the specified global position. \param pos global position to which should be moved. \return SoccerCommand to move directly to 'pos'. *//* 利用MOVE指令将球员移动到一个指定的位置 */SoccerCommand BasicPlayer::teleportToPos( VecPosition pos ){ return SoccerCommand( CMD_MOVE, pos.getX(), pos.getY() );}/*! This method returns a 'attentionto' command to listen to the specified object. In most occasions this is a teammate. *//* 生成一个指令使得球员只听指定对象的喊话(广播),通常将会设定为队友 */SoccerCommand BasicPlayer::listenTo( ObjectT obj ){ //当指定的obj球员未知 if( !SoccerTypes::isKnownPlayer( obj ) ) return SoccerCommand( CMD_ATTENTIONTO, -1.0, -1.0 ); //已知的情况下 return SoccerCommand( CMD_ATTENTIONTO, 1.0, // 1.0 denotes our team(1.0 象征着是我们的队伍) SoccerTypes::getIndex( obj ) + 1 );}/*! This method returns the command to tackle the ball. *//* 拦球,抢球 */SoccerCommand BasicPlayer::tackle( ){ return SoccerCommand( CMD_TACKLE, 100.0 );}/********************** INTERMEDIATE LEVEL SKILLS ****************************//********************** 两人协作层次技术动作 ***************************//*! This skill enables an agent to turn his body towards an object o which is supplied to it as an argument. To this end the object's global position o in the next cycle is predicted based on its current velocity. This predicted position is passed as an argument to the turnBodyToPoint skill which generates a turn command that causes the agent to turn his body towards the object. \param o object to which agent wants to turn \return SoccerCommand that turns to this object *//* 将身体转向一个指定的物体 */SoccerCommand BasicPlayer::turnBodyToObject( ObjectT o ){ return turnBodyToPoint( WM->predictPosAfterNrCycles(o, 1) );}/*! This skill enables an agent to turn his neck towards an object. It receives as arguments this object o as well as a primary action command 'soc' that will be executed by the agent at the end of the current cycle. Turning the neck towards an object amounts to predicting the object's global position in the next cycle and passing this predicted position together with the 'soc' command as arguments to the turnNeckToPoint skill. This low-level skill will then generate a turn neck command that causes the agent to turn his neck towards the given object. Note that the 'soc' command is supplied as an argument for predicting the agent's global position and neck angle after executing the command. This is necessary because a turn neck command can be executed in the same cycle as a kick, dash, turn , move or catch command. \param o object to which the agent wants to turn his neck \param soc SoccerCommand that is performed in this cycle. \return SoccerCommand that turns the neck of the agent to this object *//* 将脖子转向一个物体 这几个函数的功能类似个人基本技能里的功能,仅仅是将绝对点更改为对象 */SoccerCommand BasicPlayer::turnNeckToObject( ObjectT o, SoccerCommand soc ){ //转向估计两个周期以后的目标对象点,然后转向那个点 return turnNeckToPoint( WM->predictPosAfterNrCycles(o, 2), soc );}/*! 这是一个比较智能的指令,用于在需要转体的角度大于一定程度时再进行转体,否则返回一个冲刺指令 就是说,这个只有在满足一定条件后才会转体,在身体角度于目标点小于标准时将不会转体。 指令的意义就是靠近一个点,角度偏大的时候转体,角度小的时候将直接冲刺。 \param posTurnTo 转体的目标点 \param angWhenToTurn 转体的条件,就是大于多少角度之后再进行转体 \param pos \param vel \param angBody 这里用于设定球员的默认状态,当然您空着这个参数时, 算法将会从世界模型里取得当前球员的状态 \return 满足转动条件时转体,否则返回一个冲刺指令*/inline SoccerCommand BasicPlayer::directTowards( VecPosition posTurnTo, AngDeg angWhenToTurn, VecPosition *pos, VecPosition *vel, AngDeg *angBody ){// return turnBodyToPoint( posTurnTo );// 上面是这个程序的简单实现方法,直接返回转体指令,在这里采用下面的详细算法 // copy values or initialize when not set // 如果没有初始化位置的话就从WorldMode取得球员当前状态 VecPosition posAgent= (pos ==NULL)?WM->getAgentGlobalPosition ():*pos; VecPosition velAgent= (vel ==NULL)?WM->getAgentGlobalVelocity ():*vel; AngDeg angBodyAgent= (angBody==NULL)?WM->getAgentGlobalBodyAngle():*angBody; // first predict what happens when the agents rolls out. // 首先预测球员转体后将发生什么 VecPosition posPred = WM->predictFinalAgentPos();//预测球员最终位置 AngDeg angTo = ( posTurnTo - posPred ).getDirection();//取得相对方向 AngDeg ang = VecPosition::normalizeAngle( angTo - angBodyAgent );//标准化 AngDeg angNeck = 0;//记录脖子的角度 //循环检测直到 iTurn>=5 或者身体旋转角度已经大于规定的 angWhenToTurn //这里 iTurn 记录了于几个周期之后将转到规定的“最小开始转体角度”(anWhenToTurn) //而 angWhenToTurn 规定的是“转体角度”>anWhenToTurn这个值的情况下,才会进行转体 int iTurn = 0; //while( fabs( ang ) > angWhenToTurn && iTurn < 5 ) //一般来说3个周期足以转动到目标点,这里取5比较大,耗费速度,所以考虑改成4或者3 while( fabs( ang ) > angWhenToTurn && iTurn < 4 ) { iTurn++; //估计转体后的球员状态 WM->predictStateAfterTurn( WM->getAngleForTurn( ang, velAgent.getMagnitude() ), //旋转所需的实际角度 &posAgent, &velAgent, &angBodyAgent, &angNeck ); ang = VecPosition::normalizeAngle( angTo - angBodyAgent ); //身体的预测角度和需要需要转动到的方向的差距 } Log.log( 509, "direct towards: %d turns", iTurn ); posAgent = (pos ==NULL)?WM->getAgentGlobalPosition ():*pos; velAgent = (vel ==NULL)?WM->getAgentGlobalVelocity ():*vel; angBodyAgent = (angBody==NULL)?WM->getAgentGlobalBodyAngle():*angBody; //根据满足转动条件的周期数来确定进行什么动作 switch( iTurn ) { //不需要旋转 case 0: cerr << "direct towards: 0 turns" ; return SoccerCommand( CMD_ILLEGAL ); //在1~2个周期可以转向目标点 case 1: case 2: return turnBodyToPoint( posTurnTo, 2 ); //否则就想目标点冲刺 default: return dashToPoint( (pos==NULL)?WM->getAgentGlobalPosition ():*pos ); // stop }}/*! This skill enables an agent to move to a global position 'pos' on the field which is supplied to it as an argument. Since the agent can only move forwards or backwards into the direction of his body, the crucial decision in the execution of this skill is whether he should perform a turn or a dash. Turning has the advantage that in the next cycle the agent will be orientated correctly towards the point he wants to reach. However, it has the disadvantage that performing the turn will cost a cycle and will reduce the agent's velocity since no acceleration vector is added in that cycle. Apart from the target position 'pos', this skill receives several additional arguments for determining whether a turn or dash should be performed in the current situation. If the target point is in front of the agent then a dash is performed when the relative angle to this point is smaller than a given angle 'angWhenToTurn'. However, if the target point is behind the agent then a dash is only performed if the distance to point is less than a given value 'dDistBack' and if the angle relative to the back direction of the agent is smaller than 'angWhenToTurn'. In all other cases a turn is performed. Note that in the case of the goalkeeper it is sometimes desirable that he moves backwards towards his goal in order to keep sight of the rest of the field. To this end an additional boolean argument 'bMoveBack' is supplied to this skill that indicates whether the agent should always move backwards to the target point. If this value equals true then the agent will turn his back towards the target point if the angle relative to his back direction is larger than 'angToTurn'. In all other cases he will perform a (backward) dash towards 'posTo' regardless of whether the distance to this point is larger than 'dDistBack'. \param posTo global target position to which the agent wants to move \param angWhenToTurn angle determining when turn command is returned \param dDistBack when posTo lies closer than this value to the back of the agent (and within angWhenToTurn) a backward dash is returned \param bMoveBack boolean determing whether to move backwards to 'posTo' \return SoccerCommand that determines next action to move to 'posTo' *//* 这个技能使得球员可以移动向一个点,因为球员正能向正前或者正后方移动,所以在和目标点达到一定的相对 角度之后,球员将做一个转体动作。这个函数的功能类似上一个函数,但是它可以设定背向冲刺的一些参数。 实际上上面的directTowards是用作moveToPos的子函数的。 posTo : 目标点 angWhenToTurn : 转体的角度条件,当身体与目标点大于此角度时转体 dDistBack : 如果目标点在后方一定距离以内,将会允许向后跑,这里修改默认值为1.0 (跑动条件必须是在angWhenToTurn转体条件不满足时) bMoveBack : 指定是否一直背向跑向目标点 返回一个冲刺(可能是背向的)或者一个转体命令 */SoccerCommand BasicPlayer::moveToPos( VecPosition posTo, AngDeg angWhenToTurn, double dDistBack, bool bMoveBack, int iCycles ){// previously we only turned relative to position in next cycle, now take// angle relative to position when you're totally rolled out...// VecPosition posPred = WM->predictAgentPos( 1, 0 ); VecPosition posAgent = WM->getAgentGlobalPosition();//取得球员当前点 VecPosition posPred = WM->predictFinalAgentPos();//取得最终的角度 AngDeg angBody = WM->getAgentGlobalBodyAngle();//身体的角度 AngDeg angTo = ( posTo - posPred ).getDirection();//需要转体的角度 angTo = VecPosition::normalizeAngle( angTo - angBody );//标准化 AngDeg angBackTo = VecPosition::normalizeAngle( angTo + 180 );//如果一直向后跑,角度增加180度(反向) double dDist = posAgent.getDistanceTo( posTo );//取得距离球的位置 Log.log( 509, "moveToPos (%f,%f): body %f to %f diff %f now %f when %f", posTo.getX(), posTo.getY(), angBody, ( posTo - posPred ).getDirection(), angTo, ( posTo - WM->predictAgentPos( 1, 0 )).getDirection(), angWhenToTurn ); //如果指定一直向后跑 if( bMoveBack ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -