14.7 isClosestPlayertoBall
This function returns a boolean result depending on whether this current agent is the closest agent on the field to the ball. It takes in two vector<pair<double,int > >
, one representing all the distances of our players to the ball and the other all the distances of the opponents players to the ball. These vectors can be generated using GenerateTeamToTargetDistanceVector
and GenerateOppToTargetDistanceVector
respectively. Now it is important to note these vectors are sorted with respect to distance. Therefore, we can access the closest players for either team at index 0
. Once we have each of the closest distances we compare to see which team is closest if(minOppDis+1 < minTeamDis)
. We chose to incorporate the +1
just so that there is a bit of leniency in favor of our team thinking it is closer more often. If even with that leaniancy we are not closest then we return false
as if our team is not closest then we can’t be the closest. If some member of our team is closest we check if the second
component of the pair<double,int >
which represents the player id is the same as the input parameter _playerNumber
. If it is then Yes this player is the closest player to the ball on the field.
bool NaoBehavior::isClosestPlayertoBall(int _playerNumber, vector<pair<double,int > > TeamDistToBall, vector<pair<double,int > > OppDistToBall){
double minOppDis = OppDistToBall[0].first;
double minTeamDis = TeamDistToBall[0].first;
if(minOppDis+1 < minTeamDis){
return false;
}
else{
if(TeamDistToBall[0].second != _playerNumber){
return false;
}
else{
return true;
}
}
}
The following function achieves the same goal but was another older/slower approach to solving the same problem. This is achieved by looping through both distance arrays and finding the respective closest distance an opponent is away form the ball as well as the respective distance of a teammate. We then compare these to minimum values to determine if our team is the closest. The idea here is that if our min is smaller than the opponents min then our team is overall closer to the ball. We then check if our own distance is equal to the min distance, if so then we can return TRUE
since our player is therefore the closest player to the ball.
bool NaoBehavior::isClosestPlayertoBall(int pNum, double* _opponentDistances, double* _teamMateDistances){
double minOppDis = MAXFLOAT;
for(int z =0; z<NUM_AGENTS; z++){
if(_opponentDistances[z]< minOppDis){
minOppDis = _opponentDistances[z];
}
}
double minTeamDis = MAXFLOAT;
for(int z=0;z<NUM_AGENTS;z++){
if((_teamMateDistances[z]) < minTeamDis){
minTeamDis = _teamMateDistances[z];
}
}
if(minOppDis < minTeamDis){
return false;
}
else{
if(minTeamDis == _teamMateDistances[pNum-1]){
return true;
}
else{
return false;
}
}
}