Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

AmIFirstToBall.cc

Go to the documentation of this file.
00001 /* -*- Mode: C++ -*-
00002  * $Id: AmIFirstToBall.cc,v 1.6 2006/09/26 13:28:44 frehe Exp $
00003  *
00004  *
00005  * COPYRIGHT INFORMATION
00006  *
00007  * This file is part of RoboSoc created by Fredrik Heintz <frehe@ida.liu.se>
00008  * Copyright (C) 1999, 2000 Fredrik Heintz, Linköping University, Sweden
00009  *
00010  * You are allowed to modify and use this code as long as you retain this
00011  * notice. If you make any changes or have any comments I would appreciate
00012  * it if you send me a message. For more information, please see
00013  * http://www.ida.liu.se/~frehe/RoboCup/RoboSoc/
00014  *
00015  *
00016  * IDENTIFICATION
00017  *
00018  * Filename: AmIFirstToBall.cc
00019  * Unitname: Framework/predicate
00020  * $Revision: 1.6 $
00021  * Created by: aip3 2000-09-18
00022  * Last modified by $Author: frehe $ $Date: 2006/09/26 13:28:44 $
00023  *
00024  *
00025  * HISTORY
00026  *
00027  * $Log: AmIFirstToBall.cc,v $
00028  * Revision 1.6  2006/09/26 13:28:44  frehe
00029  * Fixed destructors which deleted views and facts which should *never* be deleted.
00030  *
00031  * Revision 1.5  2003/11/07 15:02:28  aip3
00032  * Now calculates time to ball according to a new formula (stated in H-file).
00033  * Takes a second optional argument to specify the assumed speed of the other players.
00034  *
00035  * Revision 1.4  2003/10/10 10:39:08  aip3
00036  * Commenting predicates and specifying development plans.
00037  * All of our debugging prints should now be turned off.
00038  *
00039  * Revision 1.3  2003/10/01 22:59:45  aip3
00040  * Nu utgår predikatet från att alla andra spelarna kommer att fortsätta i riktningskomposanten mot bollen med nuvarande hastighet i den riktiningen. Så ska det vara. Däremot räknar predikatet med att ens egen hastighet är konstant som den är NOW, vilket ska ändras.
00041  *
00042  * Revision 1.2  2003/09/22 18:21:44  aip3
00043  * Now this works just like AmIClosestToBall.
00044  * This will of course be changed later.
00045  *
00046  * Revision 1.1  2003/09/18 13:18:04  aip3
00047  * Added the AmIFirstToBall predicate which tests if the agent is able to reach the ball before anyone else can.
00048  *
00049  */
00050 
00063 #include "AmIFirstToBall.h"
00064 #include "view_constants.h"
00065 
00066 
00067 RS_BEGIN_NAMESPACE
00068 
00069 
00070 AmIFirstToBall::AmIFirstToBall(bool includeTeammates, float assumedSpeed)
00071   : Predicate(), agentView(NULL), ballView(NULL), playerView(NULL), includeTeammates(includeTeammates),assumedSpeed(assumedSpeed)
00072 {
00073   getView(ballView, BALL_VIEW_ID);
00074   getView(agentView, AGENT_VIEW_ID);
00075   getView(playerView, PLAYER_VIEW_ID);
00076 }
00077 
00078 
00079 AmIFirstToBall::AmIFirstToBall(const AmIFirstToBall& obj)
00080   : Predicate(obj), agentView(NULL), ballView(NULL), playerView(NULL)
00081 {
00082   getView(ballView, BALL_VIEW_ID);
00083   getView(agentView, AGENT_VIEW_ID);
00084   getView(playerView, PLAYER_VIEW_ID);
00085   includeTeammates = obj.includeTeammates;
00086   assumedSpeed = obj.assumedSpeed;
00087 }
00088 
00089 
00090 AmIFirstToBall::~AmIFirstToBall()
00091 {
00092   viewManager->releaseView(BALL_VIEW_ID);
00093   viewManager->releaseView(AGENT_VIEW_ID);
00094   viewManager->releaseView(PLAYER_VIEW_ID);
00095 }
00096 
00097 
00098 AmIFirstToBall& AmIFirstToBall::operator= (const AmIFirstToBall& obj)
00099 {
00100   if ( this != &obj ) {
00101     Predicate::operator=(obj);
00102   }
00103   ballView = obj.ballView;
00104   agentView = obj.agentView;
00105   playerView = obj.playerView;
00106   includeTeammates = obj.includeTeammates;
00107   assumedSpeed = obj.assumedSpeed;
00108 
00109   return *this;
00110 }
00111 
00112 
00113 FuzzyBool AmIFirstToBall::evaluate()
00114 {
00115   PointUE myPos, ballPos;
00116   VectorUE ballSpeed;
00117   float staminaEffect = 1; //Should be lower if tired.
00118   float myTimeToBall;
00119   myPos = agentView->getPosition();
00120   ballPos = ballView->getPosition();
00121   ballSpeed = ballView->getSpeedVector();
00122   if (myPos.isUnknown()){
00123     return FuzzyBool(0.5);
00124   }
00125   if (ballPos.isUnknown()){
00126     return FuzzyBool(0.5);
00127   }
00128   if (ballSpeed.isUnknown()){  //Assume ball is still if its speed is unknown
00129     ballSpeed = VectorUE(0,0);
00130   }
00131   std::vector<PlayerObject> others = playerView->getOpponents();
00132   if (includeTeammates){
00133     for (std::vector<PlayerObject>::const_iterator i = playerView->getTeammates().begin();
00134          i != playerView->getTeammates().end(); i++)
00135       others.push_back(*i);
00136     for (std::vector<PlayerObject>::const_iterator i = playerView->getUnknownPlayers().begin();
00137          i != playerView->getUnknownPlayers().end(); i++)
00138       others.push_back(*i);
00139   }
00140 
00141   if (others.empty()) {
00142     //    std::cerr << "first: I'm alone" <<std::endl;
00143     return FuzzyBool(1.0);
00144   }
00145   
00146   Vector ballToPlayer(ballPos.getPoint(),myPos.getPoint());
00147   float relativeBallSpeed = ballToPlayer.dotProduct(ballSpeed.getVector());
00148   /*
00149     std::cerr << "%Min position: " << myPos.getPoint() << std::endl;
00150     std::cerr << "%Min hastigetsvektor: " << mySpeed.getVector() << std::endl;
00151     std::cerr << "%Bollens position: " << ballPos.getPoint() << std::endl;
00152     std::cerr << "%Min vektor till bollen: " << toBall << std::endl;
00153     std::cerr << "%Min hastighet till bollen: " << speedToBall << std::endl;
00154   */
00155   myTimeToBall = ballToPlayer.getLength() / 
00156     ((assumedSpeed + relativeBallSpeed)*staminaEffect);
00157   if(myTimeToBall<0)
00158     return FuzzyBool(0.0); //Hinner inte ikapp bollen
00159   
00160   std::cerr << "first: MyTime: " << myTimeToBall << std::endl;
00161   // --
00162   
00163   for (std::vector<PlayerObject>::const_iterator o = others.begin();
00164        o != others.end(); o++){
00165     
00166     PointUE oPos = o->getPosition();
00167 
00168     //    std::cerr << "%Börjar kolla motståndare" << std::endl;
00169 
00170     if (oPos.isKnown()){
00171       Vector ballToOpponent(ballPos.getPoint(),oPos.getPoint());
00172       float relativeBallSpeed = ballToOpponent.dotProduct(ballSpeed.getVector());
00173       float hisTimeToBall = ballToOpponent.getLength() / 
00174         (assumedSpeed + relativeBallSpeed);
00175       //      std::cerr << "first: HisTime: " << hisTimeToBall << std::endl;
00176       if(hisTimeToBall > 0 && hisTimeToBall < myTimeToBall)
00177         return FuzzyBool(0.0);
00178     }
00179   }
00180   //  std::cerr << "%Undersöker mig själv" << std::endl;
00181   
00182   return FuzzyBool(1.0); //Did't find anyone closer.
00183 }
00184 
00185 
00186 void AmIFirstToBall::update(const UInt32)
00187 {
00188 }
00189 
00190 
00191 RS_END_NAMESPACE
00192 
00193 
00194 /* CONDITIONAL INCLUSION OF INLINE DEFINITIONS
00195  *
00196  * If the compiler or debugger does not understand the keyword inline then
00197  * include the inline definitions here, otherwise include them in the
00198  * declaration file.
00199  *
00200  * This is controlled by a flag called RS_USE_INLINE, which is usually
00201  * defined. It can be set by the user by giving the compiler an argument
00202  * usually -DRS_USE_INLINE (to define it) or -URS_USE_INLINE (to undefine it).
00203  */
00204 
00205 #ifndef RS_USE_INLINE
00206 #  include "AmIFirstToBall.icc"
00207 #endif

Generated on Mon Aug 29 07:57:53 2011 for RoboSoc by doxygen1.3-rc3