14.4 GenerateTeamToTargetDistanceVector
This is an improvement and generalization of the DistanceToBallArrayTeammates
which uses vectors instead pointers and can be used to generated a sorted list of distance,playerId pairs for each of those agents to the respective target. The function loops through each of our teams player WorldObject
’s and calculates the distance to the VecPosition target
. A pair
object is defined with the distance being the first parameter and the ID being the second. This pair
is pushed to the back of vector<pair<double,int> > distances
which is a vector
of these pairs. When then call the sort
function which sorts this vector based on the first component. Once again it is important to note we pay special attention to when i
is equal to _playernumber
which is the player number of the agent currently executing this section of code, as we need to get the distance using a separate function.
vector<pair<double,int > > NaoBehavior::GenerateTeamToTargetDistanceVector(int _playernumber, VecPosition target){
target.setZ(0);
vector<pair<double,int> > distances;
for(int i = WO_TEAMMATE1; i<WO_TEAMMATE1+NUM_AGENTS;i++){ //OUR PLAYERS
WorldObject* teammate = worldModel->getWorldObject(i);
VecPosition temp;
if(i == _playernumber){
temp = worldModel->getMyPosition();
}
else{
temp = teammate->pos;
}
temp.setZ(0);
float distance = temp.getDistanceTo(target);
pair<double, int> temppoint = {distance,i};
distances.push_back(temppoint);
}
sort(distances.begin(), distances.end());
return distances;
}