Guest

Untitled

By: a guest on Oct 28th, 2009  |  syntax: C++  |  size: 1.79 KB  |  hits: 89  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. bool FieldPlayer::isClosestTeamMemberToSupportingOpponent()const
  2. {
  3.  
  4.     if(this->Team()->Opponents()->SupportingPlayer() == 0)
  5.         return false;
  6.  
  7.     else
  8.         return this == Team()->ClosestPlayerToSupportingOpponent();
  9.  
  10. }
  11.  
  12.  
  13. //------------------ ClosestPlayerToSupportingOpponent ------------------------
  14. //
  15. //  Nicolas Bertoa
  16. //
  17. //  returns the closest FIELD player, in the Wait, ReturnToHomeRegion or
  18. //  MarkSupportingOpponent state, to the supporting opponent.
  19. //
  20. //-----------------------------------------------------------------------------
  21. FieldPlayer* SoccerTeam::ClosestPlayerToSupportingOpponent()
  22. {
  23.  
  24.     // Get the supporting opponent player position
  25.     PlayerBase* supportingOpp = Opponents()->SupportingPlayer();
  26.  
  27.     assert(supportingOpp != 0);
  28.  
  29.     Vector2D supportingOppPosition = supportingOpp->Pos();
  30.  
  31.     // Get the closest player
  32.     double minDistance = MaxDouble;
  33.     FieldPlayer* closestPlayer = 0;
  34.     std::vector<PlayerBase*>::const_iterator it = m_Players.begin();
  35.        
  36.     for (it; it != m_Players.end(); ++it)
  37.     {  
  38.  
  39.         if ( (*it)->Role() != PlayerBase::goal_keeper )
  40.         {
  41.  
  42.             //cast to a field player
  43.             FieldPlayer* plyr = static_cast<FieldPlayer*>(*it);
  44.  
  45.             if (plyr->GetFSM()->isInState(*Wait::Instance()) ||
  46.                 plyr->GetFSM()->isInState(*ReturnToHomeRegion::Instance()) ||
  47.                 plyr->GetFSM()->isInState(*MarkSupportingOpponent::Instance()))
  48.             {
  49.  
  50.                 // get the distance between this player and the supporting
  51.                 // opponent player.
  52.                 double tempDistance = Vec2DDistance(supportingOppPosition, plyr->Pos());
  53.  
  54.                 // Update the min distance and the player.
  55.                 if (tempDistance < minDistance)
  56.                 {
  57.                                
  58.                     minDistance = tempDistance;
  59.                     closestPlayer = plyr;
  60.  
  61.                 }
  62.  
  63.             }
  64.  
  65.         }
  66.  
  67.     }
  68.  
  69.     return closestPlayer;
  70.  
  71. }