Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

beameffector.cpp

Go to the documentation of this file.
00001 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*-
00002 
00003    this file is part of rcssserver3D
00004    Fri May 9 2003
00005    Copyright (C) 2002,2003 Koblenz University
00006    Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group
00007    $Id: beameffector.cpp,v 1.9 2004/12/21 23:04:46 tomhoward Exp $
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; version 2 of the License.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00021 */
00022 #include "beamaction.h"
00023 #include "beameffector.h"
00024 #include <soccer/soccerbase/soccerbase.h>
00025 #include <soccer/agentstate/agentstate.h>
00026 #include <cmath>
00027 
00028 using namespace boost;
00029 using namespace oxygen;
00030 using namespace salt;
00031 using namespace std;
00032 
00033 BeamEffector::BeamEffector() : oxygen::Effector()
00034 {
00035 }
00036 
00037 BeamEffector::~BeamEffector()
00038 {
00039 }
00040 
00041 #ifdef __APPLE_CC__
00042 bool
00043 isfinite( float f )
00044 {
00045     return f == f && f != f * 0.5f;
00046 }
00047 #endif
00048 
00049 bool
00050 BeamEffector::Realize(boost::shared_ptr<ActionObject> action)
00051 {
00052     if (
00053         (mBody.get() == 0) ||
00054         (mGameState.get() == 0) ||
00055         (mAgentState.get() == 0)
00056         )
00057     {
00058         return false;
00059     }
00060 
00061     shared_ptr<BeamAction> beamAction =
00062         shared_dynamic_cast<BeamAction>(action);
00063 
00064     if (beamAction.get() == 0)
00065     {
00066         GetLog()->Error()
00067             << "ERROR: (BeamEffector) cannot realize an unknown ActionObject\n";
00068         return false;
00069     }
00070 
00071     // the beam effector only has an effect in PM_BeforeKickOff
00072     if (mGameState->GetPlayMode() == PM_BeforeKickOff)
00073     {
00074         Vector3f pos = beamAction->GetPosition();
00075 
00076         // reject nan or infinite numbers in the beam position
00077         if (
00078             (! isfinite(pos[0])) ||
00079             (! isfinite(pos[1])) ||
00080             (! isfinite(pos[2]))
00081             )
00082             {
00083                 return false;
00084             }
00085 
00086         // an agent can only beam within it's own field half
00087         float minX = -mFieldLength/2 + mAgentRadius;
00088         pos[0] = std::max<float>(pos[0],minX);
00089         pos[0] = std::min<float>(pos[0],0.0f);
00090 
00091         float minY = -mFieldWidth/2 + mAgentRadius;
00092         float maxY = mFieldWidth/2 - mAgentRadius;
00093         pos[1] = std::max<float>(minY,pos[1]);
00094         pos[1] = std::min<float>(maxY,pos[1]);
00095 
00096         pos[2] = mAgentRadius;
00097 
00098         // swap x and y coordinates accordingly for the current
00099         // team; after the flip pos is global and not independent
00100         // on the team
00101         pos = SoccerBase::FlipView
00102             (
00103                 pos,
00104                 mAgentState->GetTeamIndex()
00105                 );
00106 
00107         mBody->SetPosition(pos);
00108         mBody->SetVelocity(Vector3f(0,0,0));
00109         mBody->SetAngularVelocity(Vector3f(0,0,0));
00110     }
00111 
00112     return true;
00113 }
00114 
00115 shared_ptr<ActionObject>
00116 BeamEffector::GetActionObject(const Predicate& predicate)
00117 {
00118   if (predicate.name != GetPredicate())
00119     {
00120       GetLog()->Error() << "ERROR: (BeamEffector) invalid predicate"
00121                         << predicate.name << "\n";
00122       return shared_ptr<ActionObject>();
00123     }
00124 
00125   Vector3f pos;
00126   if (! predicate.GetValue(predicate.begin(), pos))
00127   {
00128       GetLog()->Error()
00129           << "ERROR: (BeamEffector) Vector3f parameter expected\n";
00130       return shared_ptr<ActionObject>(new ActionObject(GetPredicate()));
00131   }
00132 
00133   return shared_ptr<ActionObject>(new BeamAction(GetPredicate(),pos));
00134 }
00135 
00136 void
00137 BeamEffector::OnLink()
00138 {
00139     SoccerBase::GetBody(*this,mBody);
00140     SoccerBase::GetGameState(*this, mGameState);
00141     SoccerBase::GetAgentState(*this,mAgentState);
00142 
00143     mFieldWidth = 64.0;
00144     SoccerBase::GetSoccerVar(*this,"FieldWidth",mFieldWidth);
00145 
00146     mFieldLength = 100.0;
00147     SoccerBase::GetSoccerVar(*this,"FieldLength",mFieldLength);
00148 
00149     mAgentRadius = 0.22;
00150     SoccerBase::GetSoccerVar(*this,"AgentRadius",mAgentRadius);
00151 }
00152 
00153 void
00154 BeamEffector::OnUnlink()
00155 {
00156     mBody.reset();
00157     mGameState.reset();
00158     mAgentState.reset();
00159 }
00160 

Generated on Thu Apr 6 15:25:37 2006 for rcssserver3d by  doxygen 1.4.4