📄 basicplayer.cpp
字号:
/*
Copyright (c) 2000-2003, Jelle Kok, University of Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the University of Amsterdam nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*! \file BasicPlayer.cpp
<pre>
<b>File:</b> BasicPlayer.cpp
<b>Project:</b> Robocup Soccer Simulation Team: UvA Trilearn
<b>Authors:</b> Jelle Kok
<b>Created:</b> 10/12/2000
<b>Last Revision:</b> $ID$
<b>Contents:</b> This file contains the class declaration for the
BasicPlayer. The BasicPlayer is the class where the
available skills for the agent are defined.
<hr size=2>
<h2><b>Changes</b></h2>
<b>Date</b> <b>Author</b> <b>Comment</b>:
10/12/2000 Jelle Kok Initial version created
</pre>
*/
#include "BasicPlayer.h"
#include "Parse.h" // parseFirstInt
/********************** LOW-LEVEL SKILLS *************************************/
///*! This skill enables an agent to align his neck with his body. It returns a
// turn neck command that takes the angle of the agent's body relative to his
// neck as its only argument.
// \return SoccerCommand turn_neck command that aligns neck with body */
//SoccerCommand BasicPlayer::alignNeckWithBody( )
//{
// return SoccerCommand( CMD_TURNNECK, WM->getAgentBodyAngleRelToNeck( ) );
//}
/*! This skill enables an agent to turn his body towards a given point. It
receives a global position 'pos' on the field and returns a turn command
that will turn the agent's body towards this point. To this end the agent's
global position in the next cycle is predicted based on his current
velocity. This is done to compensate for the fact that the remaining
velocity will move the agent to another position in the next cycle. The
global angle between the given position and the predicted position is then
determined after which the agent's global body direction is subtracted from
this angle in order to make it relative to the agent's body. Finally,
the resulting angle is normalized and adjusted to compensate for the
inertia moment and speed of the agent. If it is impossible to turn towards
the given position in a single cycle then the agent turns as far as
possible.
\param pos position to which body should be turned
\param iCycles denotes the number of cycles that are used to update the
the agent position. The resulting position is compared with
'pos' to determine the desired turning angle.
\return SoccerCommand turn command to turn body to the desired point */
SoccerCommand BasicPlayer::turnBodyToPoint( VecPosition pos, int iCycles )
{
VecPosition posGlobal = WM->predictAgentPos(iCycles, 0);
AngDeg angTurn = (pos - posGlobal).getDirection();
angTurn -= WM->getAgentGlobalBodyAngle();
angTurn = VecPosition::normalizeAngle( angTurn );
angTurn = WM->getAngleForTurn( angTurn, WM->getAgentSpeed(),
WM->getAgentObjectType() );
return SoccerCommand( CMD_TURN, angTurn );
}
/*! This skill enables an agent to turn his back towards a given point 'pos'.
The only difference between this skill and turnBodyToPoint is that the
angle between the given position and the predicted position of the agent
in the next cycle is now made relative to the back of the agent by
subtracting the agent's global back direction. This skill can for example
be used by the goalkeeper in case he wants to move back to his goal while
keeping sight of the rest of the field.
\param pos position to which the agent's back should be turned
\param iCycles denotes the number of cycles that are used to update the
the agent position. The resulting position is compared with
'pos' to determine the desired turning angle.
\return SoccerCommand command to turn agent's back to the desired point */
SoccerCommand BasicPlayer::turnBackToPoint( VecPosition pos, int iCycles )
{
VecPosition posGlobal = WM->predictAgentPos(iCycles, 0);
AngDeg angTurn = (pos - posGlobal).getDirection();
angTurn -= (WM->getAgentGlobalBodyAngle() + 180);
angTurn = VecPosition::normalizeAngle( angTurn );
angTurn = WM->getAngleForTurn( angTurn, WM->getAgentSpeed(),
WM->getAgentObjectType() );
return SoccerCommand( CMD_TURN, angTurn );
}
///*! This skill enables an agent to turn his neck towards a given point. It
// receives a global position 'pos' on the field as well as a primary action
// command 'soc' that will be executed by the agent at the end of the current
// cycle and returns a turn neck command that will turn the agent's neck
// towards 'pos'. To this end the agent's global position and neck direction
// after executing the cmd command are predicted using methods from the world
// model. The global angle between the given position and the predicted
// position is then determined after which the predicted neck direction is
// subtracted from this angle in order to make it relative to the agent's
// neck. Finally, the resulting angle is normalized and directly passed as an
// argument to the turn neck command since the actual angle with which a
// player turns his neck is by definition equal to this argument.
// If the resulting turn angle causes the absolute angle between
// the agent's neck and body to exceed the maximum value, then the agent turns
// his neck as far as possible. Note that it is necessary to supply the
// selected primary command as an argument to this skill, since a turn neck
// command can be executed in the same cycle as a kick, dash, turn , move
// or catch command.
// \param pos position to which neck should be turned
// \param soc SoccerCommand that is executed in the same cycle
// \return SoccerCommand turn command to turn neck to the desired point */
SoccerCommand BasicPlayer::turnNeckToPoint(VecPosition pos, SoccerCommand soc)
{
VecPosition posMe, velMe;
AngDeg angBody, angNeck, angActual;
Stamina sta;
// predict agent information after command 'soc' is performed
// calculate the desired global angle of the neck
// calculate the desired angle of the neck relative to the body
WM->predictAgentStateAfterCommand(soc,&posMe,&velMe,&angBody,&angNeck,&sta);
AngDeg angDesGlobNeck = (pos - posMe).getDirection();
AngDeg angNeckRelToBody= VecPosition::normalizeAngle(angDesGlobNeck-angBody);
// calculate the current angle of the body relative to the neck
// check if the desired neck angle relative to the body is possible:
// if angle is smaller than the minimum or larger than the maximum neck angle
// turn neck to the minimum or maximum neck angle + the current neck angle
// else calculate the desired angle relative to the body
AngDeg angBodyRelToNeck = VecPosition::normalizeAngle(angBody-angNeck);
if( angNeckRelToBody < SS->getMinNeckAng() )
angActual = SS->getMinNeckAng() + angBodyRelToNeck;
else if( angNeckRelToBody > SS->getMaxNeckAng() )
angActual = SS->getMaxNeckAng() + angBodyRelToNeck;
else
angActual = angNeckRelToBody + angBodyRelToNeck;
return SoccerCommand( CMD_TURNNECK, angActual );
}
///*! 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, 1), soc );
}
///*! This skill enables an agent to turn his neck towards a given point. It
// receives a global position 'pos' on the field as well as a primary action
// command 'soc' that will be executed by the agent at the end of the current
// cycle and returns a turn neck command that will turn the agent's neck
// towards 'pos'. To this end the agent's global position and neck direction
// after executing the cmd command are predicted using methods from the world
// model. The global angle between the given position and the predicted
// position is then determined after which the predicted neck direction is
// subtracted from this angle in order to make it relative to the agent's
// neck. Finally, the resulting angle is normalized and directly passed as an
// argument to the turn neck command since the actual angle with which a
// player turns his neck is by definition equal to this argument.
// If the resulting turn angle causes the absolute angle between
// the agent's neck and body to exceed the maximum value, then the agent turns
// his neck as far as possible. Note that it is necessary to supply the
// selected primary command as an argument to this skill, since a turn neck
// command can be executed in the same cycle as a kick, dash, turn , move
// or catch command.
// \param pos position to which neck should be turned
// \param soc SoccerCommand that is executed in the same cycle
// \return SoccerCommand turn command to turn neck to the desired point */
//SoccerCommand BasicPlayer::turnNeckToPoint(VecPosition pos, SoccerCommand soc)
//{
// VecPosition posMe, velMe;
// AngDeg angBody, angNeck, angActual;
// Stamina sta;
//
// // predict agent information after command 'soc' is performed
// // calculate the desired global angle of the neck
// // calculate the desired angle of the neck relative to the body
// WM->predictAgentStateAfterCommand(soc,&posMe,&velMe,&angBody,&angNeck,&sta);
// AngDeg angDesGlobNeck = (pos - posMe).getDirection();
// AngDeg angNeckRelToBody= VecPosition::normalizeAngle(angDesGlobNeck-angBody);
//
// // calculate the current angle of the body relative to the neck
// // check if the desired neck angle relative to the body is possible:
// // if angle is smaller than the minimum or larger than the maximum neck angle
// // turn neck to the minimum or maximum neck angle + the current neck angle
// // else calculate the desired angle relative to the body
// AngDeg angBodyRelToNeck = VecPosition::normalizeAngle(angBody-angNeck);
// if( angNeckRelToBody < SS->getMinNeckAng() )
// angActual = SS->getMinNeckAng() + angBodyRelToNeck;
// else if( angNeckRelToBody > SS->getMaxNeckAng() )
// angActual = SS->getMaxNeckAng() + angBodyRelToNeck;
// else
// angActual = angNeckRelToBody + angBodyRelToNeck;
// return SoccerCommand( CMD_TURNNECK, angActual );
//}
/*! This skill enables an agent to search for the ball when he cannot see it.
It returns a turn command that causes the agent to turn his body by an
angle that equals the width of his current view cone (denoted by the
ViewAngle attribute in the AgentObject class). In this way the agent will
see an entirely different part of the field after the turn which maximizes
the chance that he will see the ball in the next cycle. Note that the
agent turns towards the direction in which the ball was last observed to
avoid turning back and forth without ever seeing the ball. Furthermore
the inertia moment of the agent is taken into account to compensate for
the current speed of the agent.
\return SoccerCommand that searches for the ball. */
SoccerCommand BasicPlayer::searchBall()
{
static Time timeLastSearch;
static SoccerCommand soc;
static int iSign = 1;
VecPosition posBall =WM->getBallPos();
VecPosition posAgent=WM->getAgentGlobalPosition();
AngDeg angBall =(posBall-WM->getAgentGlobalPosition()).getDirection();
AngDeg angBody =WM->getAgentGlobalBodyAngle();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -